17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 545916cd2Sjpk * Common Development and Distribution License (the "License"). 645916cd2Sjpk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 2197eda132Sraf 227c478bd9Sstevel@tonic-gate /* 23*bd41d0a8Snordmark * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Zones 317c478bd9Sstevel@tonic-gate * 327c478bd9Sstevel@tonic-gate * A zone is a named collection of processes, namespace constraints, 337c478bd9Sstevel@tonic-gate * and other system resources which comprise a secure and manageable 347c478bd9Sstevel@tonic-gate * application containment facility. 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * Zones (represented by the reference counted zone_t) are tracked in 377c478bd9Sstevel@tonic-gate * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs 387c478bd9Sstevel@tonic-gate * (zoneid_t) are used to track zone association. Zone IDs are 397c478bd9Sstevel@tonic-gate * dynamically generated when the zone is created; if a persistent 407c478bd9Sstevel@tonic-gate * identifier is needed (core files, accounting logs, audit trail, 417c478bd9Sstevel@tonic-gate * etc.), the zone name should be used. 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * Global Zone: 457c478bd9Sstevel@tonic-gate * 467c478bd9Sstevel@tonic-gate * The global zone (zoneid 0) is automatically associated with all 477c478bd9Sstevel@tonic-gate * system resources that have not been bound to a user-created zone. 487c478bd9Sstevel@tonic-gate * This means that even systems where zones are not in active use 497c478bd9Sstevel@tonic-gate * have a global zone, and all processes, mounts, etc. are 507c478bd9Sstevel@tonic-gate * associated with that zone. The global zone is generally 517c478bd9Sstevel@tonic-gate * unconstrained in terms of privileges and access, though the usual 527c478bd9Sstevel@tonic-gate * credential and privilege based restrictions apply. 537c478bd9Sstevel@tonic-gate * 547c478bd9Sstevel@tonic-gate * 557c478bd9Sstevel@tonic-gate * Zone States: 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * The states in which a zone may be in and the transitions are as 587c478bd9Sstevel@tonic-gate * follows: 597c478bd9Sstevel@tonic-gate * 607c478bd9Sstevel@tonic-gate * ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially 617c478bd9Sstevel@tonic-gate * initialized zone is added to the list of active zones on the system but 627c478bd9Sstevel@tonic-gate * isn't accessible. 637c478bd9Sstevel@tonic-gate * 64*bd41d0a8Snordmark * ZONE_IS_INITIALIZED: Initialization complete except the ZSD callbacks are 65*bd41d0a8Snordmark * not yet completed. Not possible to enter the zone, but attributes can 66*bd41d0a8Snordmark * be retrieved. 67*bd41d0a8Snordmark * 687c478bd9Sstevel@tonic-gate * ZONE_IS_READY: zsched (the kernel dummy process for a zone) is 697c478bd9Sstevel@tonic-gate * ready. The zone is made visible after the ZSD constructor callbacks are 707c478bd9Sstevel@tonic-gate * executed. A zone remains in this state until it transitions into 717c478bd9Sstevel@tonic-gate * the ZONE_IS_BOOTING state as a result of a call to zone_boot(). 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start 747c478bd9Sstevel@tonic-gate * init. Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN 757c478bd9Sstevel@tonic-gate * state. 767c478bd9Sstevel@tonic-gate * 777c478bd9Sstevel@tonic-gate * ZONE_IS_RUNNING: The zone is open for business: zsched has 787c478bd9Sstevel@tonic-gate * successfully started init. A zone remains in this state until 797c478bd9Sstevel@tonic-gate * zone_shutdown() is called. 807c478bd9Sstevel@tonic-gate * 817c478bd9Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is 827c478bd9Sstevel@tonic-gate * killing all processes running in the zone. The zone remains 837c478bd9Sstevel@tonic-gate * in this state until there are no more user processes running in the zone. 847c478bd9Sstevel@tonic-gate * zone_create(), zone_enter(), and zone_destroy() on this zone will fail. 857c478bd9Sstevel@tonic-gate * Since zone_shutdown() is restartable, it may be called successfully 867c478bd9Sstevel@tonic-gate * multiple times for the same zone_t. Setting of the zone's state to 877c478bd9Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check 887c478bd9Sstevel@tonic-gate * the zone's status without worrying about it being a moving target. 897c478bd9Sstevel@tonic-gate * 907c478bd9Sstevel@tonic-gate * ZONE_IS_EMPTY: zone_shutdown() has been called, and there 917c478bd9Sstevel@tonic-gate * are no more user processes in the zone. The zone remains in this 927c478bd9Sstevel@tonic-gate * state until there are no more kernel threads associated with the 937c478bd9Sstevel@tonic-gate * zone. zone_create(), zone_enter(), and zone_destroy() on this zone will 947c478bd9Sstevel@tonic-gate * fail. 957c478bd9Sstevel@tonic-gate * 967c478bd9Sstevel@tonic-gate * ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone 977c478bd9Sstevel@tonic-gate * have exited. zone_shutdown() returns. Henceforth it is not possible to 987c478bd9Sstevel@tonic-gate * join the zone or create kernel threads therein. 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate * ZONE_IS_DYING: zone_destroy() has been called on the zone; zone 1017c478bd9Sstevel@tonic-gate * remains in this state until zsched exits. Calls to zone_find_by_*() 1027c478bd9Sstevel@tonic-gate * return NULL from now on. 1037c478bd9Sstevel@tonic-gate * 1047c478bd9Sstevel@tonic-gate * ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0). There are no 1057c478bd9Sstevel@tonic-gate * processes or threads doing work on behalf of the zone. The zone is 1067c478bd9Sstevel@tonic-gate * removed from the list of active zones. zone_destroy() returns, and 1077c478bd9Sstevel@tonic-gate * the zone can be recreated. 1087c478bd9Sstevel@tonic-gate * 1097c478bd9Sstevel@tonic-gate * ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor 1107c478bd9Sstevel@tonic-gate * callbacks are executed, and all memory associated with the zone is 1117c478bd9Sstevel@tonic-gate * freed. 1127c478bd9Sstevel@tonic-gate * 1137c478bd9Sstevel@tonic-gate * Threads can wait for the zone to enter a requested state by using 1147c478bd9Sstevel@tonic-gate * zone_status_wait() or zone_status_timedwait() with the desired 1157c478bd9Sstevel@tonic-gate * state passed in as an argument. Zone state transitions are 1167c478bd9Sstevel@tonic-gate * uni-directional; it is not possible to move back to an earlier state. 1177c478bd9Sstevel@tonic-gate * 1187c478bd9Sstevel@tonic-gate * 1197c478bd9Sstevel@tonic-gate * Zone-Specific Data: 1207c478bd9Sstevel@tonic-gate * 1217c478bd9Sstevel@tonic-gate * Subsystems needing to maintain zone-specific data can store that 1227c478bd9Sstevel@tonic-gate * data using the ZSD mechanism. This provides a zone-specific data 1237c478bd9Sstevel@tonic-gate * store, similar to thread-specific data (see pthread_getspecific(3C) 1247c478bd9Sstevel@tonic-gate * or the TSD code in uts/common/disp/thread.c. Also, ZSD can be used 1257c478bd9Sstevel@tonic-gate * to register callbacks to be invoked when a zone is created, shut 1267c478bd9Sstevel@tonic-gate * down, or destroyed. This can be used to initialize zone-specific 1277c478bd9Sstevel@tonic-gate * data for new zones and to clean up when zones go away. 1287c478bd9Sstevel@tonic-gate * 1297c478bd9Sstevel@tonic-gate * 1307c478bd9Sstevel@tonic-gate * Data Structures: 1317c478bd9Sstevel@tonic-gate * 1327c478bd9Sstevel@tonic-gate * The per-zone structure (zone_t) is reference counted, and freed 1337c478bd9Sstevel@tonic-gate * when all references are released. zone_hold and zone_rele can be 1347c478bd9Sstevel@tonic-gate * used to adjust the reference count. In addition, reference counts 1357c478bd9Sstevel@tonic-gate * associated with the cred_t structure are tracked separately using 1367c478bd9Sstevel@tonic-gate * zone_cred_hold and zone_cred_rele. 1377c478bd9Sstevel@tonic-gate * 1387c478bd9Sstevel@tonic-gate * Pointers to active zone_t's are stored in two hash tables; one 1397c478bd9Sstevel@tonic-gate * for searching by id, the other for searching by name. Lookups 1407c478bd9Sstevel@tonic-gate * can be performed on either basis, using zone_find_by_id and 1417c478bd9Sstevel@tonic-gate * zone_find_by_name. Both return zone_t pointers with the zone 1427c478bd9Sstevel@tonic-gate * held, so zone_rele should be called when the pointer is no longer 1437c478bd9Sstevel@tonic-gate * needed. Zones can also be searched by path; zone_find_by_path 1447c478bd9Sstevel@tonic-gate * returns the zone with which a path name is associated (global 1457c478bd9Sstevel@tonic-gate * zone if the path is not within some other zone's file system 1467c478bd9Sstevel@tonic-gate * hierarchy). This currently requires iterating through each zone, 1477c478bd9Sstevel@tonic-gate * so it is slower than an id or name search via a hash table. 1487c478bd9Sstevel@tonic-gate * 1497c478bd9Sstevel@tonic-gate * 1507c478bd9Sstevel@tonic-gate * Locking: 1517c478bd9Sstevel@tonic-gate * 1527c478bd9Sstevel@tonic-gate * zonehash_lock: This is a top-level global lock used to protect the 1537c478bd9Sstevel@tonic-gate * zone hash tables and lists. Zones cannot be created or destroyed 1547c478bd9Sstevel@tonic-gate * while this lock is held. 1557c478bd9Sstevel@tonic-gate * zone_status_lock: This is a global lock protecting zone state. 1567c478bd9Sstevel@tonic-gate * Zones cannot change state while this lock is held. It also 1577c478bd9Sstevel@tonic-gate * protects the list of kernel threads associated with a zone. 1587c478bd9Sstevel@tonic-gate * zone_lock: This is a per-zone lock used to protect several fields of 1597c478bd9Sstevel@tonic-gate * the zone_t (see <sys/zone.h> for details). In addition, holding 1607c478bd9Sstevel@tonic-gate * this lock means that the zone cannot go away. 1610209230bSgjelinek * zone_nlwps_lock: This is a per-zone lock used to protect the fields 1620209230bSgjelinek * related to the zone.max-lwps rctl. 1630209230bSgjelinek * zone_mem_lock: This is a per-zone lock used to protect the fields 1640209230bSgjelinek * related to the zone.max-locked-memory and zone.max-swap rctls. 1657c478bd9Sstevel@tonic-gate * zsd_key_lock: This is a global lock protecting the key state for ZSD. 1667c478bd9Sstevel@tonic-gate * zone_deathrow_lock: This is a global lock protecting the "deathrow" 1677c478bd9Sstevel@tonic-gate * list (a list of zones in the ZONE_IS_DEAD state). 1687c478bd9Sstevel@tonic-gate * 1697c478bd9Sstevel@tonic-gate * Ordering requirements: 1707c478bd9Sstevel@tonic-gate * pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock --> 1717c478bd9Sstevel@tonic-gate * zone_lock --> zsd_key_lock --> pidlock --> p_lock 1727c478bd9Sstevel@tonic-gate * 1730209230bSgjelinek * When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is: 1740209230bSgjelinek * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 1750209230bSgjelinek * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 1760209230bSgjelinek * 1777c478bd9Sstevel@tonic-gate * Blocking memory allocations are permitted while holding any of the 1787c478bd9Sstevel@tonic-gate * zone locks. 1797c478bd9Sstevel@tonic-gate * 1807c478bd9Sstevel@tonic-gate * 1817c478bd9Sstevel@tonic-gate * System Call Interface: 1827c478bd9Sstevel@tonic-gate * 1837c478bd9Sstevel@tonic-gate * The zone subsystem can be managed and queried from user level with 1847c478bd9Sstevel@tonic-gate * the following system calls (all subcodes of the primary "zone" 1857c478bd9Sstevel@tonic-gate * system call): 1867c478bd9Sstevel@tonic-gate * - zone_create: creates a zone with selected attributes (name, 187fa9e4066Sahrens * root path, privileges, resource controls, ZFS datasets) 1887c478bd9Sstevel@tonic-gate * - zone_enter: allows the current process to enter a zone 1897c478bd9Sstevel@tonic-gate * - zone_getattr: reports attributes of a zone 1903f2f09c1Sdp * - zone_setattr: set attributes of a zone 1913f2f09c1Sdp * - zone_boot: set 'init' running for the zone 1927c478bd9Sstevel@tonic-gate * - zone_list: lists all zones active in the system 1937c478bd9Sstevel@tonic-gate * - zone_lookup: looks up zone id based on name 1947c478bd9Sstevel@tonic-gate * - zone_shutdown: initiates shutdown process (see states above) 1957c478bd9Sstevel@tonic-gate * - zone_destroy: completes shutdown process (see states above) 1967c478bd9Sstevel@tonic-gate * 1977c478bd9Sstevel@tonic-gate */ 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h> 2007c478bd9Sstevel@tonic-gate #include <sys/cred.h> 2017c478bd9Sstevel@tonic-gate #include <c2/audit.h> 2027c478bd9Sstevel@tonic-gate #include <sys/debug.h> 2037c478bd9Sstevel@tonic-gate #include <sys/file.h> 2047c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 2050209230bSgjelinek #include <sys/kstat.h> 2067c478bd9Sstevel@tonic-gate #include <sys/mutex.h> 20745916cd2Sjpk #include <sys/note.h> 2087c478bd9Sstevel@tonic-gate #include <sys/pathname.h> 2097c478bd9Sstevel@tonic-gate #include <sys/proc.h> 2107c478bd9Sstevel@tonic-gate #include <sys/project.h> 211cf8f45c7Sdstaff #include <sys/sysevent.h> 2127c478bd9Sstevel@tonic-gate #include <sys/task.h> 2137c478bd9Sstevel@tonic-gate #include <sys/systm.h> 2147c478bd9Sstevel@tonic-gate #include <sys/types.h> 2157c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 2167c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 2177c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 2187c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 2197c478bd9Sstevel@tonic-gate #include <sys/policy.h> 2207c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h> 2217c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h> 2227c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h> 2237c478bd9Sstevel@tonic-gate #include <sys/class.h> 2247c478bd9Sstevel@tonic-gate #include <sys/pool.h> 2257c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h> 2267c478bd9Sstevel@tonic-gate #include <sys/pset.h> 2277c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 2287c478bd9Sstevel@tonic-gate #include <sys/callb.h> 2297c478bd9Sstevel@tonic-gate #include <sys/vmparam.h> 2307c478bd9Sstevel@tonic-gate #include <sys/corectl.h> 231824c205fSml #include <sys/ipc_impl.h> 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate #include <sys/door.h> 2347c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 235*bd41d0a8Snordmark #include <sys/sdt.h> 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 2387c478bd9Sstevel@tonic-gate #include <sys/session.h> 2397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 2407c478bd9Sstevel@tonic-gate #include <sys/modhash.h> 2413f2f09c1Sdp #include <sys/sunddi.h> 2427c478bd9Sstevel@tonic-gate #include <sys/nvpair.h> 2437c478bd9Sstevel@tonic-gate #include <sys/rctl.h> 2447c478bd9Sstevel@tonic-gate #include <sys/fss.h> 2459acbbeafSnn #include <sys/brand.h> 2467c478bd9Sstevel@tonic-gate #include <sys/zone.h> 247f4b3ec61Sdh #include <net/if.h> 248c97ad5cdSakolb #include <sys/cpucaps.h> 2490209230bSgjelinek #include <vm/seg.h> 2500209230bSgjelinek 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * cv used to signal that all references to the zone have been released. This 2537c478bd9Sstevel@tonic-gate * needs to be global since there may be multiple waiters, and the first to 2547c478bd9Sstevel@tonic-gate * wake up will free the zone_t, hence we cannot use zone->zone_cv. 2557c478bd9Sstevel@tonic-gate */ 2567c478bd9Sstevel@tonic-gate static kcondvar_t zone_destroy_cv; 2577c478bd9Sstevel@tonic-gate /* 2587c478bd9Sstevel@tonic-gate * Lock used to serialize access to zone_cv. This could have been per-zone, 2597c478bd9Sstevel@tonic-gate * but then we'd need another lock for zone_destroy_cv, and why bother? 2607c478bd9Sstevel@tonic-gate */ 2617c478bd9Sstevel@tonic-gate static kmutex_t zone_status_lock; 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate /* 2647c478bd9Sstevel@tonic-gate * ZSD-related global variables. 2657c478bd9Sstevel@tonic-gate */ 2667c478bd9Sstevel@tonic-gate static kmutex_t zsd_key_lock; /* protects the following two */ 2677c478bd9Sstevel@tonic-gate /* 2687c478bd9Sstevel@tonic-gate * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 2697c478bd9Sstevel@tonic-gate */ 2707c478bd9Sstevel@tonic-gate static zone_key_t zsd_keyval = 0; 2717c478bd9Sstevel@tonic-gate /* 2727c478bd9Sstevel@tonic-gate * Global list of registered keys. We use this when a new zone is created. 2737c478bd9Sstevel@tonic-gate */ 2747c478bd9Sstevel@tonic-gate static list_t zsd_registered_keys; 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate int zone_hash_size = 256; 27745916cd2Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel; 2787c478bd9Sstevel@tonic-gate static kmutex_t zonehash_lock; 2797c478bd9Sstevel@tonic-gate static uint_t zonecount; 2807c478bd9Sstevel@tonic-gate static id_space_t *zoneid_space; 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate /* 2837c478bd9Sstevel@tonic-gate * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 2847c478bd9Sstevel@tonic-gate * kernel proper runs, and which manages all other zones. 2857c478bd9Sstevel@tonic-gate * 2867c478bd9Sstevel@tonic-gate * Although not declared as static, the variable "zone0" should not be used 2877c478bd9Sstevel@tonic-gate * except for by code that needs to reference the global zone early on in boot, 2887c478bd9Sstevel@tonic-gate * before it is fully initialized. All other consumers should use 2897c478bd9Sstevel@tonic-gate * 'global_zone'. 2907c478bd9Sstevel@tonic-gate */ 2917c478bd9Sstevel@tonic-gate zone_t zone0; 2927c478bd9Sstevel@tonic-gate zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * List of active zones, protected by zonehash_lock. 2967c478bd9Sstevel@tonic-gate */ 2977c478bd9Sstevel@tonic-gate static list_t zone_active; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate /* 3007c478bd9Sstevel@tonic-gate * List of destroyed zones that still have outstanding cred references. 3017c478bd9Sstevel@tonic-gate * Used for debugging. Uses a separate lock to avoid lock ordering 3027c478bd9Sstevel@tonic-gate * problems in zone_free. 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate static list_t zone_deathrow; 3057c478bd9Sstevel@tonic-gate static kmutex_t zone_deathrow_lock; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */ 3087c478bd9Sstevel@tonic-gate uint_t maxzones = 8192; 3097c478bd9Sstevel@tonic-gate 310cf8f45c7Sdstaff /* Event channel to sent zone state change notifications */ 311cf8f45c7Sdstaff evchan_t *zone_event_chan; 312cf8f45c7Sdstaff 313cf8f45c7Sdstaff /* 314cf8f45c7Sdstaff * This table holds the mapping from kernel zone states to 315cf8f45c7Sdstaff * states visible in the state notification API. 316cf8f45c7Sdstaff * The idea is that we only expose "obvious" states and 317cf8f45c7Sdstaff * do not expose states which are just implementation details. 318cf8f45c7Sdstaff */ 319cf8f45c7Sdstaff const char *zone_status_table[] = { 320cf8f45c7Sdstaff ZONE_EVENT_UNINITIALIZED, /* uninitialized */ 321*bd41d0a8Snordmark ZONE_EVENT_INITIALIZED, /* initialized */ 322cf8f45c7Sdstaff ZONE_EVENT_READY, /* ready */ 323cf8f45c7Sdstaff ZONE_EVENT_READY, /* booting */ 324cf8f45c7Sdstaff ZONE_EVENT_RUNNING, /* running */ 325cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* shutting_down */ 326cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* empty */ 327cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* down */ 328cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* dying */ 329cf8f45c7Sdstaff ZONE_EVENT_UNINITIALIZED, /* dead */ 330cf8f45c7Sdstaff }; 331cf8f45c7Sdstaff 3327c478bd9Sstevel@tonic-gate /* 3337c478bd9Sstevel@tonic-gate * This isn't static so lint doesn't complain. 3347c478bd9Sstevel@tonic-gate */ 3357c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares; 336c6939658Ssl rctl_hndl_t rc_zone_locked_mem; 3370209230bSgjelinek rctl_hndl_t rc_zone_max_swap; 338c97ad5cdSakolb rctl_hndl_t rc_zone_cpu_cap; 3397c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps; 340824c205fSml rctl_hndl_t rc_zone_shmmax; 341824c205fSml rctl_hndl_t rc_zone_shmmni; 342824c205fSml rctl_hndl_t rc_zone_semmni; 343824c205fSml rctl_hndl_t rc_zone_msgmni; 3447c478bd9Sstevel@tonic-gate /* 3457c478bd9Sstevel@tonic-gate * Synchronization primitives used to synchronize between mounts and zone 3467c478bd9Sstevel@tonic-gate * creation/destruction. 3477c478bd9Sstevel@tonic-gate */ 3487c478bd9Sstevel@tonic-gate static int mounts_in_progress; 3497c478bd9Sstevel@tonic-gate static kcondvar_t mount_cv; 3507c478bd9Sstevel@tonic-gate static kmutex_t mount_lock; 3517c478bd9Sstevel@tonic-gate 3523f2f09c1Sdp const char * const zone_default_initname = "/sbin/init"; 35345916cd2Sjpk static char * const zone_prefix = "/zone/"; 3547c478bd9Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid); 355f4b3ec61Sdh static int zone_add_datalink(zoneid_t, char *); 356f4b3ec61Sdh static int zone_remove_datalink(zoneid_t, char *); 357f4b3ec61Sdh static int zone_check_datalink(zoneid_t *, char *); 358f4b3ec61Sdh static int zone_list_datalink(zoneid_t, int *, char *); 3597c478bd9Sstevel@tonic-gate 360*bd41d0a8Snordmark typedef boolean_t zsd_applyfn_t(kmutex_t *, boolean_t, zone_t *, zone_key_t); 361*bd41d0a8Snordmark 362*bd41d0a8Snordmark static void zsd_apply_all_zones(zsd_applyfn_t *, zone_key_t); 363*bd41d0a8Snordmark static void zsd_apply_all_keys(zsd_applyfn_t *, zone_t *); 364*bd41d0a8Snordmark static boolean_t zsd_apply_create(kmutex_t *, boolean_t, zone_t *, zone_key_t); 365*bd41d0a8Snordmark static boolean_t zsd_apply_shutdown(kmutex_t *, boolean_t, zone_t *, 366*bd41d0a8Snordmark zone_key_t); 367*bd41d0a8Snordmark static boolean_t zsd_apply_destroy(kmutex_t *, boolean_t, zone_t *, zone_key_t); 368*bd41d0a8Snordmark static boolean_t zsd_wait_for_creator(zone_t *, struct zsd_entry *, 369*bd41d0a8Snordmark kmutex_t *); 370*bd41d0a8Snordmark static boolean_t zsd_wait_for_inprogress(zone_t *, struct zsd_entry *, 371*bd41d0a8Snordmark kmutex_t *); 372*bd41d0a8Snordmark 373821c4a97Sdp /* 374821c4a97Sdp * Bump this number when you alter the zone syscall interfaces; this is 375821c4a97Sdp * because we need to have support for previous API versions in libc 376821c4a97Sdp * to support patching; libc calls into the kernel to determine this number. 377821c4a97Sdp * 378821c4a97Sdp * Version 1 of the API is the version originally shipped with Solaris 10 379821c4a97Sdp * Version 2 alters the zone_create system call in order to support more 380821c4a97Sdp * arguments by moving the args into a structure; and to do better 381821c4a97Sdp * error reporting when zone_create() fails. 382821c4a97Sdp * Version 3 alters the zone_create system call in order to support the 383821c4a97Sdp * import of ZFS datasets to zones. 38445916cd2Sjpk * Version 4 alters the zone_create system call in order to support 38545916cd2Sjpk * Trusted Extensions. 3863f2f09c1Sdp * Version 5 alters the zone_boot system call, and converts its old 3873f2f09c1Sdp * bootargs parameter to be set by the zone_setattr API instead. 388f4b3ec61Sdh * Version 6 adds the flag argument to zone_create. 389821c4a97Sdp */ 390f4b3ec61Sdh static const int ZONE_SYSCALL_API_VERSION = 6; 391821c4a97Sdp 3927c478bd9Sstevel@tonic-gate /* 3937c478bd9Sstevel@tonic-gate * Certain filesystems (such as NFS and autofs) need to know which zone 3947c478bd9Sstevel@tonic-gate * the mount is being placed in. Because of this, we need to be able to 3957c478bd9Sstevel@tonic-gate * ensure that a zone isn't in the process of being created such that 3967c478bd9Sstevel@tonic-gate * nfs_mount() thinks it is in the global zone, while by the time it 3977c478bd9Sstevel@tonic-gate * gets added the list of mounted zones, it ends up on zoneA's mount 3987c478bd9Sstevel@tonic-gate * list. 3997c478bd9Sstevel@tonic-gate * 4007c478bd9Sstevel@tonic-gate * The following functions: block_mounts()/resume_mounts() and 4017c478bd9Sstevel@tonic-gate * mount_in_progress()/mount_completed() are used by zones and the VFS 4027c478bd9Sstevel@tonic-gate * layer (respectively) to synchronize zone creation and new mounts. 4037c478bd9Sstevel@tonic-gate * 4047c478bd9Sstevel@tonic-gate * The semantics are like a reader-reader lock such that there may 4057c478bd9Sstevel@tonic-gate * either be multiple mounts (or zone creations, if that weren't 4067c478bd9Sstevel@tonic-gate * serialized by zonehash_lock) in progress at the same time, but not 4077c478bd9Sstevel@tonic-gate * both. 4087c478bd9Sstevel@tonic-gate * 4097c478bd9Sstevel@tonic-gate * We use cv's so the user can ctrl-C out of the operation if it's 4107c478bd9Sstevel@tonic-gate * taking too long. 4117c478bd9Sstevel@tonic-gate * 4127c478bd9Sstevel@tonic-gate * The semantics are such that there is unfair bias towards the 4137c478bd9Sstevel@tonic-gate * "current" operation. This means that zone creations may starve if 4147c478bd9Sstevel@tonic-gate * there is a rapid succession of new mounts coming in to the system, or 4157c478bd9Sstevel@tonic-gate * there is a remote possibility that zones will be created at such a 4167c478bd9Sstevel@tonic-gate * rate that new mounts will not be able to proceed. 4177c478bd9Sstevel@tonic-gate */ 4187c478bd9Sstevel@tonic-gate /* 4197c478bd9Sstevel@tonic-gate * Prevent new mounts from progressing to the point of calling 4207c478bd9Sstevel@tonic-gate * VFS_MOUNT(). If there are already mounts in this "region", wait for 4217c478bd9Sstevel@tonic-gate * them to complete. 4227c478bd9Sstevel@tonic-gate */ 4237c478bd9Sstevel@tonic-gate static int 4247c478bd9Sstevel@tonic-gate block_mounts(void) 4257c478bd9Sstevel@tonic-gate { 4267c478bd9Sstevel@tonic-gate int retval = 0; 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate /* 4297c478bd9Sstevel@tonic-gate * Since it may block for a long time, block_mounts() shouldn't be 4307c478bd9Sstevel@tonic-gate * called with zonehash_lock held. 4317c478bd9Sstevel@tonic-gate */ 4327c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 4337c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4347c478bd9Sstevel@tonic-gate while (mounts_in_progress > 0) { 4357c478bd9Sstevel@tonic-gate if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 4367c478bd9Sstevel@tonic-gate goto signaled; 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate /* 4397c478bd9Sstevel@tonic-gate * A negative value of mounts_in_progress indicates that mounts 4407c478bd9Sstevel@tonic-gate * have been blocked by (-mounts_in_progress) different callers. 4417c478bd9Sstevel@tonic-gate */ 4427c478bd9Sstevel@tonic-gate mounts_in_progress--; 4437c478bd9Sstevel@tonic-gate retval = 1; 4447c478bd9Sstevel@tonic-gate signaled: 4457c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4467c478bd9Sstevel@tonic-gate return (retval); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * The VFS layer may progress with new mounts as far as we're concerned. 4517c478bd9Sstevel@tonic-gate * Allow them to progress if we were the last obstacle. 4527c478bd9Sstevel@tonic-gate */ 4537c478bd9Sstevel@tonic-gate static void 4547c478bd9Sstevel@tonic-gate resume_mounts(void) 4557c478bd9Sstevel@tonic-gate { 4567c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4577c478bd9Sstevel@tonic-gate if (++mounts_in_progress == 0) 4587c478bd9Sstevel@tonic-gate cv_broadcast(&mount_cv); 4597c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate /* 4637c478bd9Sstevel@tonic-gate * The VFS layer is busy with a mount; zones should wait until all 4647c478bd9Sstevel@tonic-gate * mounts are completed to progress. 4657c478bd9Sstevel@tonic-gate */ 4667c478bd9Sstevel@tonic-gate void 4677c478bd9Sstevel@tonic-gate mount_in_progress(void) 4687c478bd9Sstevel@tonic-gate { 4697c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4707c478bd9Sstevel@tonic-gate while (mounts_in_progress < 0) 4717c478bd9Sstevel@tonic-gate cv_wait(&mount_cv, &mount_lock); 4727c478bd9Sstevel@tonic-gate mounts_in_progress++; 4737c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate /* 4777c478bd9Sstevel@tonic-gate * VFS is done with one mount; wake up any waiting block_mounts() 4787c478bd9Sstevel@tonic-gate * callers if this is the last mount. 4797c478bd9Sstevel@tonic-gate */ 4807c478bd9Sstevel@tonic-gate void 4817c478bd9Sstevel@tonic-gate mount_completed(void) 4827c478bd9Sstevel@tonic-gate { 4837c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4847c478bd9Sstevel@tonic-gate if (--mounts_in_progress == 0) 4857c478bd9Sstevel@tonic-gate cv_broadcast(&mount_cv); 4867c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate /* 4907c478bd9Sstevel@tonic-gate * ZSD routines. 4917c478bd9Sstevel@tonic-gate * 4927c478bd9Sstevel@tonic-gate * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 4937c478bd9Sstevel@tonic-gate * defined by the pthread_key_create() and related interfaces. 4947c478bd9Sstevel@tonic-gate * 4957c478bd9Sstevel@tonic-gate * Kernel subsystems may register one or more data items and/or 4967c478bd9Sstevel@tonic-gate * callbacks to be executed when a zone is created, shutdown, or 4977c478bd9Sstevel@tonic-gate * destroyed. 4987c478bd9Sstevel@tonic-gate * 4997c478bd9Sstevel@tonic-gate * Unlike the thread counterpart, destructor callbacks will be executed 5007c478bd9Sstevel@tonic-gate * even if the data pointer is NULL and/or there are no constructor 5017c478bd9Sstevel@tonic-gate * callbacks, so it is the responsibility of such callbacks to check for 5027c478bd9Sstevel@tonic-gate * NULL data values if necessary. 5037c478bd9Sstevel@tonic-gate * 5047c478bd9Sstevel@tonic-gate * The locking strategy and overall picture is as follows: 5057c478bd9Sstevel@tonic-gate * 5067c478bd9Sstevel@tonic-gate * When someone calls zone_key_create(), a template ZSD entry is added to the 507*bd41d0a8Snordmark * global list "zsd_registered_keys", protected by zsd_key_lock. While 508*bd41d0a8Snordmark * holding that lock all the existing zones are marked as 509*bd41d0a8Snordmark * ZSD_CREATE_NEEDED and a copy of the ZSD entry added to the per-zone 510*bd41d0a8Snordmark * zone_zsd list (protected by zone_lock). The global list is updated first 511*bd41d0a8Snordmark * (under zone_key_lock) to make sure that newly created zones use the 512*bd41d0a8Snordmark * most recent list of keys. Then under zonehash_lock we walk the zones 513*bd41d0a8Snordmark * and mark them. Similar locking is used in zone_key_delete(). 5147c478bd9Sstevel@tonic-gate * 515*bd41d0a8Snordmark * The actual create, shutdown, and destroy callbacks are done without 516*bd41d0a8Snordmark * holding any lock. And zsd_flags are used to ensure that the operations 517*bd41d0a8Snordmark * completed so that when zone_key_create (and zone_create) is done, as well as 518*bd41d0a8Snordmark * zone_key_delete (and zone_destroy) is done, all the necessary callbacks 519*bd41d0a8Snordmark * are completed. 5207c478bd9Sstevel@tonic-gate * 5217c478bd9Sstevel@tonic-gate * When new zones are created constructor callbacks for all registered ZSD 522*bd41d0a8Snordmark * entries will be called. That also uses the above two phases of marking 523*bd41d0a8Snordmark * what needs to be done, and then running the callbacks without holding 524*bd41d0a8Snordmark * any locks. 5257c478bd9Sstevel@tonic-gate * 5267c478bd9Sstevel@tonic-gate * The framework does not provide any locking around zone_getspecific() and 5277c478bd9Sstevel@tonic-gate * zone_setspecific() apart from that needed for internal consistency, so 5287c478bd9Sstevel@tonic-gate * callers interested in atomic "test-and-set" semantics will need to provide 5297c478bd9Sstevel@tonic-gate * their own locking. 5307c478bd9Sstevel@tonic-gate */ 5317c478bd9Sstevel@tonic-gate 532*bd41d0a8Snordmark /* 533*bd41d0a8Snordmark * Helper function to find the zsd_entry associated with the key in the 534*bd41d0a8Snordmark * given list. 535*bd41d0a8Snordmark */ 536*bd41d0a8Snordmark static struct zsd_entry * 537*bd41d0a8Snordmark zsd_find(list_t *l, zone_key_t key) 538*bd41d0a8Snordmark { 539*bd41d0a8Snordmark struct zsd_entry *zsd; 5407c478bd9Sstevel@tonic-gate 541*bd41d0a8Snordmark for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 542*bd41d0a8Snordmark if (zsd->zsd_key == key) { 543*bd41d0a8Snordmark return (zsd); 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate } 546*bd41d0a8Snordmark return (NULL); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate /* 5507c478bd9Sstevel@tonic-gate * Helper function to find the zsd_entry associated with the key in the 551*bd41d0a8Snordmark * given list. Move it to the front of the list. 5527c478bd9Sstevel@tonic-gate */ 5537c478bd9Sstevel@tonic-gate static struct zsd_entry * 554*bd41d0a8Snordmark zsd_find_mru(list_t *l, zone_key_t key) 5557c478bd9Sstevel@tonic-gate { 5567c478bd9Sstevel@tonic-gate struct zsd_entry *zsd; 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 5597c478bd9Sstevel@tonic-gate if (zsd->zsd_key == key) { 5607c478bd9Sstevel@tonic-gate /* 5617c478bd9Sstevel@tonic-gate * Move to head of list to keep list in MRU order. 5627c478bd9Sstevel@tonic-gate */ 5637c478bd9Sstevel@tonic-gate if (zsd != list_head(l)) { 5647c478bd9Sstevel@tonic-gate list_remove(l, zsd); 5657c478bd9Sstevel@tonic-gate list_insert_head(l, zsd); 5667c478bd9Sstevel@tonic-gate } 5677c478bd9Sstevel@tonic-gate return (zsd); 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate return (NULL); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 573*bd41d0a8Snordmark void 574*bd41d0a8Snordmark zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 575*bd41d0a8Snordmark void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 576*bd41d0a8Snordmark { 577*bd41d0a8Snordmark struct zsd_entry *zsdp; 578*bd41d0a8Snordmark struct zsd_entry *t; 579*bd41d0a8Snordmark struct zone *zone; 580*bd41d0a8Snordmark zone_key_t key; 581*bd41d0a8Snordmark 582*bd41d0a8Snordmark zsdp = kmem_zalloc(sizeof (*zsdp), KM_SLEEP); 583*bd41d0a8Snordmark zsdp->zsd_data = NULL; 584*bd41d0a8Snordmark zsdp->zsd_create = create; 585*bd41d0a8Snordmark zsdp->zsd_shutdown = shutdown; 586*bd41d0a8Snordmark zsdp->zsd_destroy = destroy; 587*bd41d0a8Snordmark 588*bd41d0a8Snordmark /* 589*bd41d0a8Snordmark * Insert in global list of callbacks. Makes future zone creations 590*bd41d0a8Snordmark * see it. 591*bd41d0a8Snordmark */ 592*bd41d0a8Snordmark mutex_enter(&zsd_key_lock); 593*bd41d0a8Snordmark *keyp = key = zsdp->zsd_key = ++zsd_keyval; 594*bd41d0a8Snordmark ASSERT(zsd_keyval != 0); 595*bd41d0a8Snordmark list_insert_tail(&zsd_registered_keys, zsdp); 596*bd41d0a8Snordmark mutex_exit(&zsd_key_lock); 597*bd41d0a8Snordmark 598*bd41d0a8Snordmark /* 599*bd41d0a8Snordmark * Insert for all existing zones and mark them as needing 600*bd41d0a8Snordmark * a create callback. 601*bd41d0a8Snordmark */ 602*bd41d0a8Snordmark mutex_enter(&zonehash_lock); /* stop the world */ 603*bd41d0a8Snordmark for (zone = list_head(&zone_active); zone != NULL; 604*bd41d0a8Snordmark zone = list_next(&zone_active, zone)) { 605*bd41d0a8Snordmark zone_status_t status; 606*bd41d0a8Snordmark 607*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 608*bd41d0a8Snordmark 609*bd41d0a8Snordmark /* Skip zones that are on the way down or not yet up */ 610*bd41d0a8Snordmark status = zone_status_get(zone); 611*bd41d0a8Snordmark if (status >= ZONE_IS_DOWN || 612*bd41d0a8Snordmark status == ZONE_IS_UNINITIALIZED) { 613*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 614*bd41d0a8Snordmark continue; 615*bd41d0a8Snordmark } 616*bd41d0a8Snordmark 617*bd41d0a8Snordmark t = zsd_find_mru(&zone->zone_zsd, key); 618*bd41d0a8Snordmark if (t != NULL) { 619*bd41d0a8Snordmark /* 620*bd41d0a8Snordmark * A zsd_configure already inserted it after 621*bd41d0a8Snordmark * we dropped zsd_key_lock above. 622*bd41d0a8Snordmark */ 623*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 624*bd41d0a8Snordmark continue; 625*bd41d0a8Snordmark } 626*bd41d0a8Snordmark t = kmem_zalloc(sizeof (*t), KM_SLEEP); 627*bd41d0a8Snordmark t->zsd_key = key; 628*bd41d0a8Snordmark t->zsd_create = create; 629*bd41d0a8Snordmark t->zsd_shutdown = shutdown; 630*bd41d0a8Snordmark t->zsd_destroy = destroy; 631*bd41d0a8Snordmark if (create != NULL) { 632*bd41d0a8Snordmark t->zsd_flags = ZSD_CREATE_NEEDED; 633*bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__needed, 634*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 635*bd41d0a8Snordmark } 636*bd41d0a8Snordmark list_insert_tail(&zone->zone_zsd, t); 637*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 638*bd41d0a8Snordmark } 639*bd41d0a8Snordmark mutex_exit(&zonehash_lock); 640*bd41d0a8Snordmark 641*bd41d0a8Snordmark if (create != NULL) { 642*bd41d0a8Snordmark /* Now call the create callback for this key */ 643*bd41d0a8Snordmark zsd_apply_all_zones(zsd_apply_create, key); 644*bd41d0a8Snordmark } 645*bd41d0a8Snordmark } 646*bd41d0a8Snordmark 6477c478bd9Sstevel@tonic-gate /* 6487c478bd9Sstevel@tonic-gate * Function called when a module is being unloaded, or otherwise wishes 6497c478bd9Sstevel@tonic-gate * to unregister its ZSD key and callbacks. 650*bd41d0a8Snordmark * 651*bd41d0a8Snordmark * Remove from the global list and determine the functions that need to 652*bd41d0a8Snordmark * be called under a global lock. Then call the functions without 653*bd41d0a8Snordmark * holding any locks. Finally free up the zone_zsd entries. (The apply 654*bd41d0a8Snordmark * functions need to access the zone_zsd entries to find zsd_data etc.) 6557c478bd9Sstevel@tonic-gate */ 6567c478bd9Sstevel@tonic-gate int 6577c478bd9Sstevel@tonic-gate zone_key_delete(zone_key_t key) 6587c478bd9Sstevel@tonic-gate { 6597c478bd9Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 6607c478bd9Sstevel@tonic-gate zone_t *zone; 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 663*bd41d0a8Snordmark zsdp = zsd_find_mru(&zsd_registered_keys, key); 664*bd41d0a8Snordmark if (zsdp == NULL) { 665*bd41d0a8Snordmark mutex_exit(&zsd_key_lock); 666*bd41d0a8Snordmark return (-1); 667*bd41d0a8Snordmark } 6687c478bd9Sstevel@tonic-gate list_remove(&zsd_registered_keys, zsdp); 6697c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6707c478bd9Sstevel@tonic-gate 671*bd41d0a8Snordmark mutex_enter(&zonehash_lock); 6727c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 6737c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 6747c478bd9Sstevel@tonic-gate struct zsd_entry *del; 675*bd41d0a8Snordmark 676*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 677*bd41d0a8Snordmark del = zsd_find_mru(&zone->zone_zsd, key); 678*bd41d0a8Snordmark if (del == NULL) { 679*bd41d0a8Snordmark /* 680*bd41d0a8Snordmark * Somebody else got here first e.g the zone going 681*bd41d0a8Snordmark * away. 682*bd41d0a8Snordmark */ 683*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 684*bd41d0a8Snordmark continue; 685*bd41d0a8Snordmark } 686*bd41d0a8Snordmark ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 687*bd41d0a8Snordmark ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 688*bd41d0a8Snordmark if (del->zsd_shutdown != NULL && 689*bd41d0a8Snordmark (del->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) { 690*bd41d0a8Snordmark del->zsd_flags |= ZSD_SHUTDOWN_NEEDED; 691*bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__needed, 692*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 693*bd41d0a8Snordmark } 694*bd41d0a8Snordmark if (del->zsd_destroy != NULL && 695*bd41d0a8Snordmark (del->zsd_flags & ZSD_DESTROY_ALL) == 0) { 696*bd41d0a8Snordmark del->zsd_flags |= ZSD_DESTROY_NEEDED; 697*bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__needed, 698*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 6997c478bd9Sstevel@tonic-gate } 7007c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 7037c478bd9Sstevel@tonic-gate kmem_free(zsdp, sizeof (*zsdp)); 7047c478bd9Sstevel@tonic-gate 705*bd41d0a8Snordmark /* Now call the shutdown and destroy callback for this key */ 706*bd41d0a8Snordmark zsd_apply_all_zones(zsd_apply_shutdown, key); 707*bd41d0a8Snordmark zsd_apply_all_zones(zsd_apply_destroy, key); 708*bd41d0a8Snordmark 709*bd41d0a8Snordmark /* Now we can free up the zsdp structures in each zone */ 710*bd41d0a8Snordmark mutex_enter(&zonehash_lock); 7117c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 712*bd41d0a8Snordmark zone = list_next(&zone_active, zone)) { 713*bd41d0a8Snordmark struct zsd_entry *del; 714*bd41d0a8Snordmark 715*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 716*bd41d0a8Snordmark del = zsd_find(&zone->zone_zsd, key); 717*bd41d0a8Snordmark if (del != NULL) { 718*bd41d0a8Snordmark list_remove(&zone->zone_zsd, del); 719*bd41d0a8Snordmark ASSERT(!(del->zsd_flags & ZSD_ALL_INPROGRESS)); 720*bd41d0a8Snordmark kmem_free(del, sizeof (*del)); 721*bd41d0a8Snordmark } 7227c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 723*bd41d0a8Snordmark } 7247c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 725*bd41d0a8Snordmark 726*bd41d0a8Snordmark return (0); 7277c478bd9Sstevel@tonic-gate } 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate /* 7307c478bd9Sstevel@tonic-gate * ZSD counterpart of pthread_setspecific(). 731*bd41d0a8Snordmark * 732*bd41d0a8Snordmark * Since all zsd callbacks, including those with no create function, 733*bd41d0a8Snordmark * have an entry in zone_zsd, if the key is registered it is part of 734*bd41d0a8Snordmark * the zone_zsd list. 735*bd41d0a8Snordmark * Return an error if the key wasn't registerd. 7367c478bd9Sstevel@tonic-gate */ 7377c478bd9Sstevel@tonic-gate int 7387c478bd9Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 7397c478bd9Sstevel@tonic-gate { 7407c478bd9Sstevel@tonic-gate struct zsd_entry *t; 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 743*bd41d0a8Snordmark t = zsd_find_mru(&zone->zone_zsd, key); 7447c478bd9Sstevel@tonic-gate if (t != NULL) { 7457c478bd9Sstevel@tonic-gate /* 7467c478bd9Sstevel@tonic-gate * Replace old value with new 7477c478bd9Sstevel@tonic-gate */ 7487c478bd9Sstevel@tonic-gate t->zsd_data = (void *)data; 7497c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7507c478bd9Sstevel@tonic-gate return (0); 7517c478bd9Sstevel@tonic-gate } 7527c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 753*bd41d0a8Snordmark return (-1); 7547c478bd9Sstevel@tonic-gate } 7557c478bd9Sstevel@tonic-gate 7567c478bd9Sstevel@tonic-gate /* 7577c478bd9Sstevel@tonic-gate * ZSD counterpart of pthread_getspecific(). 7587c478bd9Sstevel@tonic-gate */ 7597c478bd9Sstevel@tonic-gate void * 7607c478bd9Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone) 7617c478bd9Sstevel@tonic-gate { 7627c478bd9Sstevel@tonic-gate struct zsd_entry *t; 7637c478bd9Sstevel@tonic-gate void *data; 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 766*bd41d0a8Snordmark t = zsd_find_mru(&zone->zone_zsd, key); 7677c478bd9Sstevel@tonic-gate data = (t == NULL ? NULL : t->zsd_data); 7687c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7697c478bd9Sstevel@tonic-gate return (data); 7707c478bd9Sstevel@tonic-gate } 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate /* 7737c478bd9Sstevel@tonic-gate * Function used to initialize a zone's list of ZSD callbacks and data 7747c478bd9Sstevel@tonic-gate * when the zone is being created. The callbacks are initialized from 775*bd41d0a8Snordmark * the template list (zsd_registered_keys). The constructor callback is 776*bd41d0a8Snordmark * executed later (once the zone exists and with locks dropped). 7777c478bd9Sstevel@tonic-gate */ 7787c478bd9Sstevel@tonic-gate static void 7797c478bd9Sstevel@tonic-gate zone_zsd_configure(zone_t *zone) 7807c478bd9Sstevel@tonic-gate { 7817c478bd9Sstevel@tonic-gate struct zsd_entry *zsdp; 7827c478bd9Sstevel@tonic-gate struct zsd_entry *t; 7837c478bd9Sstevel@tonic-gate 7847c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 7857c478bd9Sstevel@tonic-gate ASSERT(list_head(&zone->zone_zsd) == NULL); 786*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 7877c478bd9Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7887c478bd9Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7897c478bd9Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 790*bd41d0a8Snordmark /* 791*bd41d0a8Snordmark * Since this zone is ZONE_IS_UNCONFIGURED, zone_key_create 792*bd41d0a8Snordmark * should not have added anything to it. 793*bd41d0a8Snordmark */ 794*bd41d0a8Snordmark ASSERT(zsd_find(&zone->zone_zsd, zsdp->zsd_key) == NULL); 795*bd41d0a8Snordmark 796*bd41d0a8Snordmark t = kmem_zalloc(sizeof (*t), KM_SLEEP); 797*bd41d0a8Snordmark t->zsd_key = zsdp->zsd_key; 798*bd41d0a8Snordmark t->zsd_create = zsdp->zsd_create; 799*bd41d0a8Snordmark t->zsd_shutdown = zsdp->zsd_shutdown; 800*bd41d0a8Snordmark t->zsd_destroy = zsdp->zsd_destroy; 8017c478bd9Sstevel@tonic-gate if (zsdp->zsd_create != NULL) { 802*bd41d0a8Snordmark t->zsd_flags = ZSD_CREATE_NEEDED; 803*bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__needed, 804*bd41d0a8Snordmark zone_t *, zone, zone_key_t, zsdp->zsd_key); 8057c478bd9Sstevel@tonic-gate } 806*bd41d0a8Snordmark list_insert_tail(&zone->zone_zsd, t); 8077c478bd9Sstevel@tonic-gate } 8087c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 809*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 8107c478bd9Sstevel@tonic-gate } 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 8137c478bd9Sstevel@tonic-gate 8147c478bd9Sstevel@tonic-gate /* 8157c478bd9Sstevel@tonic-gate * Helper function to execute shutdown or destructor callbacks. 8167c478bd9Sstevel@tonic-gate */ 8177c478bd9Sstevel@tonic-gate static void 8187c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 8197c478bd9Sstevel@tonic-gate { 8207c478bd9Sstevel@tonic-gate struct zsd_entry *t; 8217c478bd9Sstevel@tonic-gate 8227c478bd9Sstevel@tonic-gate ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 8237c478bd9Sstevel@tonic-gate ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 8247c478bd9Sstevel@tonic-gate ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate /* 827*bd41d0a8Snordmark * Run the callback solely based on what is registered for the zone 828*bd41d0a8Snordmark * in zone_zsd. The global list can change independently of this 829*bd41d0a8Snordmark * as keys are registered and unregistered and we don't register new 830*bd41d0a8Snordmark * callbacks for a zone that is in the process of going away. 8317c478bd9Sstevel@tonic-gate */ 832*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 833*bd41d0a8Snordmark for (t = list_head(&zone->zone_zsd); t != NULL; 834*bd41d0a8Snordmark t = list_next(&zone->zone_zsd, t)) { 835*bd41d0a8Snordmark zone_key_t key = t->zsd_key; 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate /* Skip if no callbacks registered */ 838*bd41d0a8Snordmark 839*bd41d0a8Snordmark if (ct == ZSD_SHUTDOWN) { 840*bd41d0a8Snordmark if (t->zsd_shutdown != NULL && 841*bd41d0a8Snordmark (t->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) { 842*bd41d0a8Snordmark t->zsd_flags |= ZSD_SHUTDOWN_NEEDED; 843*bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__needed, 844*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 8457c478bd9Sstevel@tonic-gate } 8467c478bd9Sstevel@tonic-gate } else { 847*bd41d0a8Snordmark if (t->zsd_destroy != NULL && 848*bd41d0a8Snordmark (t->zsd_flags & ZSD_DESTROY_ALL) == 0) { 849*bd41d0a8Snordmark t->zsd_flags |= ZSD_DESTROY_NEEDED; 850*bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__needed, 851*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate } 8547c478bd9Sstevel@tonic-gate } 855*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 856*bd41d0a8Snordmark 857*bd41d0a8Snordmark /* Now call the shutdown and destroy callback for this key */ 858*bd41d0a8Snordmark zsd_apply_all_keys(zsd_apply_shutdown, zone); 859*bd41d0a8Snordmark zsd_apply_all_keys(zsd_apply_destroy, zone); 860*bd41d0a8Snordmark 8617c478bd9Sstevel@tonic-gate } 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate /* 8647c478bd9Sstevel@tonic-gate * Called when the zone is going away; free ZSD-related memory, and 8657c478bd9Sstevel@tonic-gate * destroy the zone_zsd list. 8667c478bd9Sstevel@tonic-gate */ 8677c478bd9Sstevel@tonic-gate static void 8687c478bd9Sstevel@tonic-gate zone_free_zsd(zone_t *zone) 8697c478bd9Sstevel@tonic-gate { 8707c478bd9Sstevel@tonic-gate struct zsd_entry *t, *next; 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate /* 8737c478bd9Sstevel@tonic-gate * Free all the zsd_entry's we had on this zone. 8747c478bd9Sstevel@tonic-gate */ 875*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 8767c478bd9Sstevel@tonic-gate for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 8777c478bd9Sstevel@tonic-gate next = list_next(&zone->zone_zsd, t); 8787c478bd9Sstevel@tonic-gate list_remove(&zone->zone_zsd, t); 879*bd41d0a8Snordmark ASSERT(!(t->zsd_flags & ZSD_ALL_INPROGRESS)); 8807c478bd9Sstevel@tonic-gate kmem_free(t, sizeof (*t)); 8817c478bd9Sstevel@tonic-gate } 8827c478bd9Sstevel@tonic-gate list_destroy(&zone->zone_zsd); 883*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 884*bd41d0a8Snordmark 885*bd41d0a8Snordmark } 886*bd41d0a8Snordmark 887*bd41d0a8Snordmark /* 888*bd41d0a8Snordmark * Apply a function to all zones for particular key value. 889*bd41d0a8Snordmark * 890*bd41d0a8Snordmark * The applyfn has to drop zonehash_lock if it does some work, and 891*bd41d0a8Snordmark * then reacquire it before it returns. 892*bd41d0a8Snordmark * When the lock is dropped we don't follow list_next even 893*bd41d0a8Snordmark * if it is possible to do so without any hazards. This is 894*bd41d0a8Snordmark * because we want the design to allow for the list of zones 895*bd41d0a8Snordmark * to change in any arbitrary way during the time the 896*bd41d0a8Snordmark * lock was dropped. 897*bd41d0a8Snordmark * 898*bd41d0a8Snordmark * It is safe to restart the loop at list_head since the applyfn 899*bd41d0a8Snordmark * changes the zsd_flags as it does work, so a subsequent 900*bd41d0a8Snordmark * pass through will have no effect in applyfn, hence the loop will terminate 901*bd41d0a8Snordmark * in at worst O(N^2). 902*bd41d0a8Snordmark */ 903*bd41d0a8Snordmark static void 904*bd41d0a8Snordmark zsd_apply_all_zones(zsd_applyfn_t *applyfn, zone_key_t key) 905*bd41d0a8Snordmark { 906*bd41d0a8Snordmark zone_t *zone; 907*bd41d0a8Snordmark 908*bd41d0a8Snordmark mutex_enter(&zonehash_lock); 909*bd41d0a8Snordmark zone = list_head(&zone_active); 910*bd41d0a8Snordmark while (zone != NULL) { 911*bd41d0a8Snordmark if ((applyfn)(&zonehash_lock, B_FALSE, zone, key)) { 912*bd41d0a8Snordmark /* Lock dropped - restart at head */ 913*bd41d0a8Snordmark zone = list_head(&zone_active); 914*bd41d0a8Snordmark } else { 915*bd41d0a8Snordmark zone = list_next(&zone_active, zone); 916*bd41d0a8Snordmark } 917*bd41d0a8Snordmark } 918*bd41d0a8Snordmark mutex_exit(&zonehash_lock); 919*bd41d0a8Snordmark } 920*bd41d0a8Snordmark 921*bd41d0a8Snordmark /* 922*bd41d0a8Snordmark * Apply a function to all keys for a particular zone. 923*bd41d0a8Snordmark * 924*bd41d0a8Snordmark * The applyfn has to drop zonehash_lock if it does some work, and 925*bd41d0a8Snordmark * then reacquire it before it returns. 926*bd41d0a8Snordmark * When the lock is dropped we don't follow list_next even 927*bd41d0a8Snordmark * if it is possible to do so without any hazards. This is 928*bd41d0a8Snordmark * because we want the design to allow for the list of zsd callbacks 929*bd41d0a8Snordmark * to change in any arbitrary way during the time the 930*bd41d0a8Snordmark * lock was dropped. 931*bd41d0a8Snordmark * 932*bd41d0a8Snordmark * It is safe to restart the loop at list_head since the applyfn 933*bd41d0a8Snordmark * changes the zsd_flags as it does work, so a subsequent 934*bd41d0a8Snordmark * pass through will have no effect in applyfn, hence the loop will terminate 935*bd41d0a8Snordmark * in at worst O(N^2). 936*bd41d0a8Snordmark */ 937*bd41d0a8Snordmark static void 938*bd41d0a8Snordmark zsd_apply_all_keys(zsd_applyfn_t *applyfn, zone_t *zone) 939*bd41d0a8Snordmark { 940*bd41d0a8Snordmark struct zsd_entry *t; 941*bd41d0a8Snordmark 942*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 943*bd41d0a8Snordmark t = list_head(&zone->zone_zsd); 944*bd41d0a8Snordmark while (t != NULL) { 945*bd41d0a8Snordmark if ((applyfn)(NULL, B_TRUE, zone, t->zsd_key)) { 946*bd41d0a8Snordmark /* Lock dropped - restart at head */ 947*bd41d0a8Snordmark t = list_head(&zone->zone_zsd); 948*bd41d0a8Snordmark } else { 949*bd41d0a8Snordmark t = list_next(&zone->zone_zsd, t); 950*bd41d0a8Snordmark } 951*bd41d0a8Snordmark } 952*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 953*bd41d0a8Snordmark } 954*bd41d0a8Snordmark 955*bd41d0a8Snordmark /* 956*bd41d0a8Snordmark * Call the create function for the zone and key if CREATE_NEEDED 957*bd41d0a8Snordmark * is set. 958*bd41d0a8Snordmark * If some other thread gets here first and sets CREATE_INPROGRESS, then 959*bd41d0a8Snordmark * we wait for that thread to complete so that we can ensure that 960*bd41d0a8Snordmark * all the callbacks are done when we've looped over all zones/keys. 961*bd41d0a8Snordmark * 962*bd41d0a8Snordmark * When we call the create function, we drop the global held by the 963*bd41d0a8Snordmark * caller, and return true to tell the caller it needs to re-evalute the 964*bd41d0a8Snordmark * state. 965*bd41d0a8Snordmark * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 966*bd41d0a8Snordmark * remains held on exit. 967*bd41d0a8Snordmark */ 968*bd41d0a8Snordmark static boolean_t 969*bd41d0a8Snordmark zsd_apply_create(kmutex_t *lockp, boolean_t zone_lock_held, 970*bd41d0a8Snordmark zone_t *zone, zone_key_t key) 971*bd41d0a8Snordmark { 972*bd41d0a8Snordmark void *result; 973*bd41d0a8Snordmark struct zsd_entry *t; 974*bd41d0a8Snordmark boolean_t dropped; 975*bd41d0a8Snordmark 976*bd41d0a8Snordmark if (lockp != NULL) { 977*bd41d0a8Snordmark ASSERT(MUTEX_HELD(lockp)); 978*bd41d0a8Snordmark } 979*bd41d0a8Snordmark if (zone_lock_held) { 980*bd41d0a8Snordmark ASSERT(MUTEX_HELD(&zone->zone_lock)); 981*bd41d0a8Snordmark } else { 982*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 983*bd41d0a8Snordmark } 984*bd41d0a8Snordmark 985*bd41d0a8Snordmark t = zsd_find(&zone->zone_zsd, key); 986*bd41d0a8Snordmark if (t == NULL) { 987*bd41d0a8Snordmark /* 988*bd41d0a8Snordmark * Somebody else got here first e.g the zone going 989*bd41d0a8Snordmark * away. 990*bd41d0a8Snordmark */ 991*bd41d0a8Snordmark if (!zone_lock_held) 992*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 993*bd41d0a8Snordmark return (B_FALSE); 994*bd41d0a8Snordmark } 995*bd41d0a8Snordmark dropped = B_FALSE; 996*bd41d0a8Snordmark if (zsd_wait_for_inprogress(zone, t, lockp)) 997*bd41d0a8Snordmark dropped = B_TRUE; 998*bd41d0a8Snordmark 999*bd41d0a8Snordmark if (t->zsd_flags & ZSD_CREATE_NEEDED) { 1000*bd41d0a8Snordmark t->zsd_flags &= ~ZSD_CREATE_NEEDED; 1001*bd41d0a8Snordmark t->zsd_flags |= ZSD_CREATE_INPROGRESS; 1002*bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__inprogress, 1003*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1004*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1005*bd41d0a8Snordmark if (lockp != NULL) 1006*bd41d0a8Snordmark mutex_exit(lockp); 1007*bd41d0a8Snordmark 1008*bd41d0a8Snordmark dropped = B_TRUE; 1009*bd41d0a8Snordmark ASSERT(t->zsd_create != NULL); 1010*bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__start, 1011*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1012*bd41d0a8Snordmark 1013*bd41d0a8Snordmark result = (*t->zsd_create)(zone->zone_id); 1014*bd41d0a8Snordmark 1015*bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__end, 1016*bd41d0a8Snordmark zone_t *, zone, voidn *, result); 1017*bd41d0a8Snordmark 1018*bd41d0a8Snordmark ASSERT(result != NULL); 1019*bd41d0a8Snordmark if (lockp != NULL) 1020*bd41d0a8Snordmark mutex_enter(lockp); 1021*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1022*bd41d0a8Snordmark t->zsd_data = result; 1023*bd41d0a8Snordmark t->zsd_flags &= ~ZSD_CREATE_INPROGRESS; 1024*bd41d0a8Snordmark t->zsd_flags |= ZSD_CREATE_COMPLETED; 1025*bd41d0a8Snordmark cv_broadcast(&t->zsd_cv); 1026*bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__completed, 1027*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1028*bd41d0a8Snordmark } 1029*bd41d0a8Snordmark if (!zone_lock_held) 1030*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1031*bd41d0a8Snordmark return (dropped); 1032*bd41d0a8Snordmark } 1033*bd41d0a8Snordmark 1034*bd41d0a8Snordmark /* 1035*bd41d0a8Snordmark * Call the shutdown function for the zone and key if SHUTDOWN_NEEDED 1036*bd41d0a8Snordmark * is set. 1037*bd41d0a8Snordmark * If some other thread gets here first and sets *_INPROGRESS, then 1038*bd41d0a8Snordmark * we wait for that thread to complete so that we can ensure that 1039*bd41d0a8Snordmark * all the callbacks are done when we've looped over all zones/keys. 1040*bd41d0a8Snordmark * 1041*bd41d0a8Snordmark * When we call the shutdown function, we drop the global held by the 1042*bd41d0a8Snordmark * caller, and return true to tell the caller it needs to re-evalute the 1043*bd41d0a8Snordmark * state. 1044*bd41d0a8Snordmark * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 1045*bd41d0a8Snordmark * remains held on exit. 1046*bd41d0a8Snordmark */ 1047*bd41d0a8Snordmark static boolean_t 1048*bd41d0a8Snordmark zsd_apply_shutdown(kmutex_t *lockp, boolean_t zone_lock_held, 1049*bd41d0a8Snordmark zone_t *zone, zone_key_t key) 1050*bd41d0a8Snordmark { 1051*bd41d0a8Snordmark struct zsd_entry *t; 1052*bd41d0a8Snordmark void *data; 1053*bd41d0a8Snordmark boolean_t dropped; 1054*bd41d0a8Snordmark 1055*bd41d0a8Snordmark if (lockp != NULL) { 1056*bd41d0a8Snordmark ASSERT(MUTEX_HELD(lockp)); 1057*bd41d0a8Snordmark } 1058*bd41d0a8Snordmark if (zone_lock_held) { 1059*bd41d0a8Snordmark ASSERT(MUTEX_HELD(&zone->zone_lock)); 1060*bd41d0a8Snordmark } else { 1061*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1062*bd41d0a8Snordmark } 1063*bd41d0a8Snordmark 1064*bd41d0a8Snordmark t = zsd_find(&zone->zone_zsd, key); 1065*bd41d0a8Snordmark if (t == NULL) { 1066*bd41d0a8Snordmark /* 1067*bd41d0a8Snordmark * Somebody else got here first e.g the zone going 1068*bd41d0a8Snordmark * away. 1069*bd41d0a8Snordmark */ 1070*bd41d0a8Snordmark if (!zone_lock_held) 1071*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1072*bd41d0a8Snordmark return (B_FALSE); 1073*bd41d0a8Snordmark } 1074*bd41d0a8Snordmark dropped = B_FALSE; 1075*bd41d0a8Snordmark if (zsd_wait_for_creator(zone, t, lockp)) 1076*bd41d0a8Snordmark dropped = B_TRUE; 1077*bd41d0a8Snordmark 1078*bd41d0a8Snordmark if (zsd_wait_for_inprogress(zone, t, lockp)) 1079*bd41d0a8Snordmark dropped = B_TRUE; 1080*bd41d0a8Snordmark 1081*bd41d0a8Snordmark if (t->zsd_flags & ZSD_SHUTDOWN_NEEDED) { 1082*bd41d0a8Snordmark t->zsd_flags &= ~ZSD_SHUTDOWN_NEEDED; 1083*bd41d0a8Snordmark t->zsd_flags |= ZSD_SHUTDOWN_INPROGRESS; 1084*bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__inprogress, 1085*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1086*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1087*bd41d0a8Snordmark if (lockp != NULL) 1088*bd41d0a8Snordmark mutex_exit(lockp); 1089*bd41d0a8Snordmark dropped = B_TRUE; 1090*bd41d0a8Snordmark 1091*bd41d0a8Snordmark ASSERT(t->zsd_shutdown != NULL); 1092*bd41d0a8Snordmark data = t->zsd_data; 1093*bd41d0a8Snordmark 1094*bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__start, 1095*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1096*bd41d0a8Snordmark 1097*bd41d0a8Snordmark (t->zsd_shutdown)(zone->zone_id, data); 1098*bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__end, 1099*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1100*bd41d0a8Snordmark 1101*bd41d0a8Snordmark if (lockp != NULL) 1102*bd41d0a8Snordmark mutex_enter(lockp); 1103*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1104*bd41d0a8Snordmark t->zsd_flags &= ~ZSD_SHUTDOWN_INPROGRESS; 1105*bd41d0a8Snordmark t->zsd_flags |= ZSD_SHUTDOWN_COMPLETED; 1106*bd41d0a8Snordmark cv_broadcast(&t->zsd_cv); 1107*bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__completed, 1108*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1109*bd41d0a8Snordmark } 1110*bd41d0a8Snordmark if (!zone_lock_held) 1111*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1112*bd41d0a8Snordmark return (dropped); 1113*bd41d0a8Snordmark } 1114*bd41d0a8Snordmark 1115*bd41d0a8Snordmark /* 1116*bd41d0a8Snordmark * Call the destroy function for the zone and key if DESTROY_NEEDED 1117*bd41d0a8Snordmark * is set. 1118*bd41d0a8Snordmark * If some other thread gets here first and sets *_INPROGRESS, then 1119*bd41d0a8Snordmark * we wait for that thread to complete so that we can ensure that 1120*bd41d0a8Snordmark * all the callbacks are done when we've looped over all zones/keys. 1121*bd41d0a8Snordmark * 1122*bd41d0a8Snordmark * When we call the destroy function, we drop the global held by the 1123*bd41d0a8Snordmark * caller, and return true to tell the caller it needs to re-evalute the 1124*bd41d0a8Snordmark * state. 1125*bd41d0a8Snordmark * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 1126*bd41d0a8Snordmark * remains held on exit. 1127*bd41d0a8Snordmark */ 1128*bd41d0a8Snordmark static boolean_t 1129*bd41d0a8Snordmark zsd_apply_destroy(kmutex_t *lockp, boolean_t zone_lock_held, 1130*bd41d0a8Snordmark zone_t *zone, zone_key_t key) 1131*bd41d0a8Snordmark { 1132*bd41d0a8Snordmark struct zsd_entry *t; 1133*bd41d0a8Snordmark void *data; 1134*bd41d0a8Snordmark boolean_t dropped; 1135*bd41d0a8Snordmark 1136*bd41d0a8Snordmark if (lockp != NULL) { 1137*bd41d0a8Snordmark ASSERT(MUTEX_HELD(lockp)); 1138*bd41d0a8Snordmark } 1139*bd41d0a8Snordmark if (zone_lock_held) { 1140*bd41d0a8Snordmark ASSERT(MUTEX_HELD(&zone->zone_lock)); 1141*bd41d0a8Snordmark } else { 1142*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1143*bd41d0a8Snordmark } 1144*bd41d0a8Snordmark 1145*bd41d0a8Snordmark t = zsd_find(&zone->zone_zsd, key); 1146*bd41d0a8Snordmark if (t == NULL) { 1147*bd41d0a8Snordmark /* 1148*bd41d0a8Snordmark * Somebody else got here first e.g the zone going 1149*bd41d0a8Snordmark * away. 1150*bd41d0a8Snordmark */ 1151*bd41d0a8Snordmark if (!zone_lock_held) 1152*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1153*bd41d0a8Snordmark return (B_FALSE); 1154*bd41d0a8Snordmark } 1155*bd41d0a8Snordmark dropped = B_FALSE; 1156*bd41d0a8Snordmark if (zsd_wait_for_creator(zone, t, lockp)) 1157*bd41d0a8Snordmark dropped = B_TRUE; 1158*bd41d0a8Snordmark 1159*bd41d0a8Snordmark if (zsd_wait_for_inprogress(zone, t, lockp)) 1160*bd41d0a8Snordmark dropped = B_TRUE; 1161*bd41d0a8Snordmark 1162*bd41d0a8Snordmark if (t->zsd_flags & ZSD_DESTROY_NEEDED) { 1163*bd41d0a8Snordmark t->zsd_flags &= ~ZSD_DESTROY_NEEDED; 1164*bd41d0a8Snordmark t->zsd_flags |= ZSD_DESTROY_INPROGRESS; 1165*bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__inprogress, 1166*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1167*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1168*bd41d0a8Snordmark if (lockp != NULL) 1169*bd41d0a8Snordmark mutex_exit(lockp); 1170*bd41d0a8Snordmark dropped = B_TRUE; 1171*bd41d0a8Snordmark 1172*bd41d0a8Snordmark ASSERT(t->zsd_destroy != NULL); 1173*bd41d0a8Snordmark data = t->zsd_data; 1174*bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__start, 1175*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1176*bd41d0a8Snordmark 1177*bd41d0a8Snordmark (t->zsd_destroy)(zone->zone_id, data); 1178*bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__end, 1179*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1180*bd41d0a8Snordmark 1181*bd41d0a8Snordmark if (lockp != NULL) 1182*bd41d0a8Snordmark mutex_enter(lockp); 1183*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1184*bd41d0a8Snordmark t->zsd_data = NULL; 1185*bd41d0a8Snordmark t->zsd_flags &= ~ZSD_DESTROY_INPROGRESS; 1186*bd41d0a8Snordmark t->zsd_flags |= ZSD_DESTROY_COMPLETED; 1187*bd41d0a8Snordmark cv_broadcast(&t->zsd_cv); 1188*bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__completed, 1189*bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1190*bd41d0a8Snordmark } 1191*bd41d0a8Snordmark if (!zone_lock_held) 1192*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1193*bd41d0a8Snordmark return (dropped); 1194*bd41d0a8Snordmark } 1195*bd41d0a8Snordmark 1196*bd41d0a8Snordmark /* 1197*bd41d0a8Snordmark * Wait for any CREATE_NEEDED flag to be cleared. 1198*bd41d0a8Snordmark * Returns true if lockp was temporarily dropped while waiting. 1199*bd41d0a8Snordmark */ 1200*bd41d0a8Snordmark static boolean_t 1201*bd41d0a8Snordmark zsd_wait_for_creator(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp) 1202*bd41d0a8Snordmark { 1203*bd41d0a8Snordmark boolean_t dropped = B_FALSE; 1204*bd41d0a8Snordmark 1205*bd41d0a8Snordmark while (t->zsd_flags & ZSD_CREATE_NEEDED) { 1206*bd41d0a8Snordmark DTRACE_PROBE2(zsd__wait__for__creator, 1207*bd41d0a8Snordmark zone_t *, zone, struct zsd_entry *, t); 1208*bd41d0a8Snordmark if (lockp != NULL) { 1209*bd41d0a8Snordmark dropped = B_TRUE; 1210*bd41d0a8Snordmark mutex_exit(lockp); 1211*bd41d0a8Snordmark } 1212*bd41d0a8Snordmark cv_wait(&t->zsd_cv, &zone->zone_lock); 1213*bd41d0a8Snordmark if (lockp != NULL) { 1214*bd41d0a8Snordmark /* First drop zone_lock to preserve order */ 1215*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1216*bd41d0a8Snordmark mutex_enter(lockp); 1217*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1218*bd41d0a8Snordmark } 1219*bd41d0a8Snordmark } 1220*bd41d0a8Snordmark return (dropped); 1221*bd41d0a8Snordmark } 1222*bd41d0a8Snordmark 1223*bd41d0a8Snordmark /* 1224*bd41d0a8Snordmark * Wait for any INPROGRESS flag to be cleared. 1225*bd41d0a8Snordmark * Returns true if lockp was temporarily dropped while waiting. 1226*bd41d0a8Snordmark */ 1227*bd41d0a8Snordmark static boolean_t 1228*bd41d0a8Snordmark zsd_wait_for_inprogress(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp) 1229*bd41d0a8Snordmark { 1230*bd41d0a8Snordmark boolean_t dropped = B_FALSE; 1231*bd41d0a8Snordmark 1232*bd41d0a8Snordmark while (t->zsd_flags & ZSD_ALL_INPROGRESS) { 1233*bd41d0a8Snordmark DTRACE_PROBE2(zsd__wait__for__inprogress, 1234*bd41d0a8Snordmark zone_t *, zone, struct zsd_entry *, t); 1235*bd41d0a8Snordmark if (lockp != NULL) { 1236*bd41d0a8Snordmark dropped = B_TRUE; 1237*bd41d0a8Snordmark mutex_exit(lockp); 1238*bd41d0a8Snordmark } 1239*bd41d0a8Snordmark cv_wait(&t->zsd_cv, &zone->zone_lock); 1240*bd41d0a8Snordmark if (lockp != NULL) { 1241*bd41d0a8Snordmark /* First drop zone_lock to preserve order */ 1242*bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1243*bd41d0a8Snordmark mutex_enter(lockp); 1244*bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1245*bd41d0a8Snordmark } 1246*bd41d0a8Snordmark } 1247*bd41d0a8Snordmark return (dropped); 12487c478bd9Sstevel@tonic-gate } 12497c478bd9Sstevel@tonic-gate 1250fa9e4066Sahrens /* 1251fa9e4066Sahrens * Frees memory associated with the zone dataset list. 1252fa9e4066Sahrens */ 1253fa9e4066Sahrens static void 1254fa9e4066Sahrens zone_free_datasets(zone_t *zone) 1255fa9e4066Sahrens { 1256fa9e4066Sahrens zone_dataset_t *t, *next; 1257fa9e4066Sahrens 1258fa9e4066Sahrens for (t = list_head(&zone->zone_datasets); t != NULL; t = next) { 1259fa9e4066Sahrens next = list_next(&zone->zone_datasets, t); 1260fa9e4066Sahrens list_remove(&zone->zone_datasets, t); 1261fa9e4066Sahrens kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1); 1262fa9e4066Sahrens kmem_free(t, sizeof (*t)); 1263fa9e4066Sahrens } 1264fa9e4066Sahrens list_destroy(&zone->zone_datasets); 1265fa9e4066Sahrens } 1266fa9e4066Sahrens 12677c478bd9Sstevel@tonic-gate /* 12687c478bd9Sstevel@tonic-gate * zone.cpu-shares resource control support. 12697c478bd9Sstevel@tonic-gate */ 12707c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 12717c478bd9Sstevel@tonic-gate static rctl_qty_t 12727c478bd9Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 12737c478bd9Sstevel@tonic-gate { 12747c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 12757c478bd9Sstevel@tonic-gate return (p->p_zone->zone_shares); 12767c478bd9Sstevel@tonic-gate } 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 12797c478bd9Sstevel@tonic-gate static int 12807c478bd9Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 12817c478bd9Sstevel@tonic-gate rctl_qty_t nv) 12827c478bd9Sstevel@tonic-gate { 12837c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 12847c478bd9Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 12857c478bd9Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 12867c478bd9Sstevel@tonic-gate return (0); 12877c478bd9Sstevel@tonic-gate 12887c478bd9Sstevel@tonic-gate e->rcep_p.zone->zone_shares = nv; 12897c478bd9Sstevel@tonic-gate return (0); 12907c478bd9Sstevel@tonic-gate } 12917c478bd9Sstevel@tonic-gate 12927c478bd9Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = { 12937c478bd9Sstevel@tonic-gate rcop_no_action, 12947c478bd9Sstevel@tonic-gate zone_cpu_shares_usage, 12957c478bd9Sstevel@tonic-gate zone_cpu_shares_set, 12967c478bd9Sstevel@tonic-gate rcop_no_test 12977c478bd9Sstevel@tonic-gate }; 12987c478bd9Sstevel@tonic-gate 1299c97ad5cdSakolb /* 1300c97ad5cdSakolb * zone.cpu-cap resource control support. 1301c97ad5cdSakolb */ 1302c97ad5cdSakolb /*ARGSUSED*/ 1303c97ad5cdSakolb static rctl_qty_t 1304c97ad5cdSakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p) 1305c97ad5cdSakolb { 1306c97ad5cdSakolb ASSERT(MUTEX_HELD(&p->p_lock)); 1307c97ad5cdSakolb return (cpucaps_zone_get(p->p_zone)); 1308c97ad5cdSakolb } 1309c97ad5cdSakolb 1310c97ad5cdSakolb /*ARGSUSED*/ 1311c97ad5cdSakolb static int 1312c97ad5cdSakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1313c97ad5cdSakolb rctl_qty_t nv) 1314c97ad5cdSakolb { 1315c97ad5cdSakolb zone_t *zone = e->rcep_p.zone; 1316c97ad5cdSakolb 1317c97ad5cdSakolb ASSERT(MUTEX_HELD(&p->p_lock)); 1318c97ad5cdSakolb ASSERT(e->rcep_t == RCENTITY_ZONE); 1319c97ad5cdSakolb 1320c97ad5cdSakolb if (zone == NULL) 1321c97ad5cdSakolb return (0); 1322c97ad5cdSakolb 1323c97ad5cdSakolb /* 1324c97ad5cdSakolb * set cap to the new value. 1325c97ad5cdSakolb */ 1326c97ad5cdSakolb return (cpucaps_zone_set(zone, nv)); 1327c97ad5cdSakolb } 1328c97ad5cdSakolb 1329c97ad5cdSakolb static rctl_ops_t zone_cpu_cap_ops = { 1330c97ad5cdSakolb rcop_no_action, 1331c97ad5cdSakolb zone_cpu_cap_get, 1332c97ad5cdSakolb zone_cpu_cap_set, 1333c97ad5cdSakolb rcop_no_test 1334c97ad5cdSakolb }; 1335c97ad5cdSakolb 13367c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13377c478bd9Sstevel@tonic-gate static rctl_qty_t 13387c478bd9Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p) 13397c478bd9Sstevel@tonic-gate { 13407c478bd9Sstevel@tonic-gate rctl_qty_t nlwps; 13417c478bd9Sstevel@tonic-gate zone_t *zone = p->p_zone; 13427c478bd9Sstevel@tonic-gate 13437c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13447c478bd9Sstevel@tonic-gate 13457c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 13467c478bd9Sstevel@tonic-gate nlwps = zone->zone_nlwps; 13477c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate return (nlwps); 13507c478bd9Sstevel@tonic-gate } 13517c478bd9Sstevel@tonic-gate 13527c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13537c478bd9Sstevel@tonic-gate static int 13547c478bd9Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 13557c478bd9Sstevel@tonic-gate rctl_qty_t incr, uint_t flags) 13567c478bd9Sstevel@tonic-gate { 13577c478bd9Sstevel@tonic-gate rctl_qty_t nlwps; 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13607c478bd9Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 13617c478bd9Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 13627c478bd9Sstevel@tonic-gate return (0); 13637c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 13647c478bd9Sstevel@tonic-gate nlwps = e->rcep_p.zone->zone_nlwps; 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate if (nlwps + incr > rcntl->rcv_value) 13677c478bd9Sstevel@tonic-gate return (1); 13687c478bd9Sstevel@tonic-gate 13697c478bd9Sstevel@tonic-gate return (0); 13707c478bd9Sstevel@tonic-gate } 13717c478bd9Sstevel@tonic-gate 13727c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13737c478bd9Sstevel@tonic-gate static int 1374c6939658Ssl zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) 1375c6939658Ssl { 13767c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13777c478bd9Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 13787c478bd9Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 13797c478bd9Sstevel@tonic-gate return (0); 13807c478bd9Sstevel@tonic-gate e->rcep_p.zone->zone_nlwps_ctl = nv; 13817c478bd9Sstevel@tonic-gate return (0); 13827c478bd9Sstevel@tonic-gate } 13837c478bd9Sstevel@tonic-gate 13847c478bd9Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = { 13857c478bd9Sstevel@tonic-gate rcop_no_action, 13867c478bd9Sstevel@tonic-gate zone_lwps_usage, 13877c478bd9Sstevel@tonic-gate zone_lwps_set, 13887c478bd9Sstevel@tonic-gate zone_lwps_test, 13897c478bd9Sstevel@tonic-gate }; 13907c478bd9Sstevel@tonic-gate 1391824c205fSml /*ARGSUSED*/ 1392824c205fSml static int 1393824c205fSml zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1394824c205fSml rctl_qty_t incr, uint_t flags) 1395824c205fSml { 1396824c205fSml rctl_qty_t v; 1397824c205fSml ASSERT(MUTEX_HELD(&p->p_lock)); 1398824c205fSml ASSERT(e->rcep_t == RCENTITY_ZONE); 1399824c205fSml v = e->rcep_p.zone->zone_shmmax + incr; 1400824c205fSml if (v > rval->rcv_value) 1401824c205fSml return (1); 1402824c205fSml return (0); 1403824c205fSml } 1404824c205fSml 1405824c205fSml static rctl_ops_t zone_shmmax_ops = { 1406824c205fSml rcop_no_action, 1407824c205fSml rcop_no_usage, 1408824c205fSml rcop_no_set, 1409824c205fSml zone_shmmax_test 1410824c205fSml }; 1411824c205fSml 1412824c205fSml /*ARGSUSED*/ 1413824c205fSml static int 1414824c205fSml zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1415824c205fSml rctl_qty_t incr, uint_t flags) 1416824c205fSml { 1417824c205fSml rctl_qty_t v; 1418824c205fSml ASSERT(MUTEX_HELD(&p->p_lock)); 1419824c205fSml ASSERT(e->rcep_t == RCENTITY_ZONE); 1420824c205fSml v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr; 1421824c205fSml if (v > rval->rcv_value) 1422824c205fSml return (1); 1423824c205fSml return (0); 1424824c205fSml } 1425824c205fSml 1426824c205fSml static rctl_ops_t zone_shmmni_ops = { 1427824c205fSml rcop_no_action, 1428824c205fSml rcop_no_usage, 1429824c205fSml rcop_no_set, 1430824c205fSml zone_shmmni_test 1431824c205fSml }; 1432824c205fSml 1433824c205fSml /*ARGSUSED*/ 1434824c205fSml static int 1435824c205fSml zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1436824c205fSml rctl_qty_t incr, uint_t flags) 1437824c205fSml { 1438824c205fSml rctl_qty_t v; 1439824c205fSml ASSERT(MUTEX_HELD(&p->p_lock)); 1440824c205fSml ASSERT(e->rcep_t == RCENTITY_ZONE); 1441824c205fSml v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr; 1442824c205fSml if (v > rval->rcv_value) 1443824c205fSml return (1); 1444824c205fSml return (0); 1445824c205fSml } 1446824c205fSml 1447824c205fSml static rctl_ops_t zone_semmni_ops = { 1448824c205fSml rcop_no_action, 1449824c205fSml rcop_no_usage, 1450824c205fSml rcop_no_set, 1451824c205fSml zone_semmni_test 1452824c205fSml }; 1453824c205fSml 1454824c205fSml /*ARGSUSED*/ 1455824c205fSml static int 1456824c205fSml zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1457824c205fSml rctl_qty_t incr, uint_t flags) 1458824c205fSml { 1459824c205fSml rctl_qty_t v; 1460824c205fSml ASSERT(MUTEX_HELD(&p->p_lock)); 1461824c205fSml ASSERT(e->rcep_t == RCENTITY_ZONE); 1462824c205fSml v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr; 1463824c205fSml if (v > rval->rcv_value) 1464824c205fSml return (1); 1465824c205fSml return (0); 1466824c205fSml } 1467824c205fSml 1468824c205fSml static rctl_ops_t zone_msgmni_ops = { 1469824c205fSml rcop_no_action, 1470824c205fSml rcop_no_usage, 1471824c205fSml rcop_no_set, 1472824c205fSml zone_msgmni_test 1473824c205fSml }; 1474824c205fSml 1475c6939658Ssl /*ARGSUSED*/ 1476c6939658Ssl static rctl_qty_t 1477c6939658Ssl zone_locked_mem_usage(rctl_t *rctl, struct proc *p) 1478c6939658Ssl { 1479c6939658Ssl rctl_qty_t q; 1480c6939658Ssl ASSERT(MUTEX_HELD(&p->p_lock)); 14810209230bSgjelinek mutex_enter(&p->p_zone->zone_mem_lock); 1482c6939658Ssl q = p->p_zone->zone_locked_mem; 14830209230bSgjelinek mutex_exit(&p->p_zone->zone_mem_lock); 1484c6939658Ssl return (q); 1485c6939658Ssl } 1486c6939658Ssl 1487c6939658Ssl /*ARGSUSED*/ 1488c6939658Ssl static int 1489c6939658Ssl zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 1490c6939658Ssl rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 1491c6939658Ssl { 1492c6939658Ssl rctl_qty_t q; 14930209230bSgjelinek zone_t *z; 14940209230bSgjelinek 14950209230bSgjelinek z = e->rcep_p.zone; 1496c6939658Ssl ASSERT(MUTEX_HELD(&p->p_lock)); 14970209230bSgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 14980209230bSgjelinek q = z->zone_locked_mem; 1499c6939658Ssl if (q + incr > rcntl->rcv_value) 1500c6939658Ssl return (1); 1501c6939658Ssl return (0); 1502c6939658Ssl } 1503c6939658Ssl 1504c6939658Ssl /*ARGSUSED*/ 1505c6939658Ssl static int 1506c6939658Ssl zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1507c6939658Ssl rctl_qty_t nv) 1508c6939658Ssl { 1509c6939658Ssl ASSERT(MUTEX_HELD(&p->p_lock)); 1510c6939658Ssl ASSERT(e->rcep_t == RCENTITY_ZONE); 1511c6939658Ssl if (e->rcep_p.zone == NULL) 1512c6939658Ssl return (0); 1513c6939658Ssl e->rcep_p.zone->zone_locked_mem_ctl = nv; 1514c6939658Ssl return (0); 1515c6939658Ssl } 1516c6939658Ssl 1517c6939658Ssl static rctl_ops_t zone_locked_mem_ops = { 1518c6939658Ssl rcop_no_action, 1519c6939658Ssl zone_locked_mem_usage, 1520c6939658Ssl zone_locked_mem_set, 1521c6939658Ssl zone_locked_mem_test 1522c6939658Ssl }; 1523824c205fSml 15240209230bSgjelinek /*ARGSUSED*/ 15250209230bSgjelinek static rctl_qty_t 15260209230bSgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p) 15270209230bSgjelinek { 15280209230bSgjelinek rctl_qty_t q; 15290209230bSgjelinek zone_t *z = p->p_zone; 15300209230bSgjelinek 15310209230bSgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 15320209230bSgjelinek mutex_enter(&z->zone_mem_lock); 15330209230bSgjelinek q = z->zone_max_swap; 15340209230bSgjelinek mutex_exit(&z->zone_mem_lock); 15350209230bSgjelinek return (q); 15360209230bSgjelinek } 15370209230bSgjelinek 15380209230bSgjelinek /*ARGSUSED*/ 15390209230bSgjelinek static int 15400209230bSgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 15410209230bSgjelinek rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 15420209230bSgjelinek { 15430209230bSgjelinek rctl_qty_t q; 15440209230bSgjelinek zone_t *z; 15450209230bSgjelinek 15460209230bSgjelinek z = e->rcep_p.zone; 15470209230bSgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 15480209230bSgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 15490209230bSgjelinek q = z->zone_max_swap; 15500209230bSgjelinek if (q + incr > rcntl->rcv_value) 15510209230bSgjelinek return (1); 15520209230bSgjelinek return (0); 15530209230bSgjelinek } 15540209230bSgjelinek 15550209230bSgjelinek /*ARGSUSED*/ 15560209230bSgjelinek static int 15570209230bSgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 15580209230bSgjelinek rctl_qty_t nv) 15590209230bSgjelinek { 15600209230bSgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 15610209230bSgjelinek ASSERT(e->rcep_t == RCENTITY_ZONE); 15620209230bSgjelinek if (e->rcep_p.zone == NULL) 15630209230bSgjelinek return (0); 15640209230bSgjelinek e->rcep_p.zone->zone_max_swap_ctl = nv; 15650209230bSgjelinek return (0); 15660209230bSgjelinek } 15670209230bSgjelinek 15680209230bSgjelinek static rctl_ops_t zone_max_swap_ops = { 15690209230bSgjelinek rcop_no_action, 15700209230bSgjelinek zone_max_swap_usage, 15710209230bSgjelinek zone_max_swap_set, 15720209230bSgjelinek zone_max_swap_test 15730209230bSgjelinek }; 15740209230bSgjelinek 15757c478bd9Sstevel@tonic-gate /* 15767c478bd9Sstevel@tonic-gate * Helper function to brand the zone with a unique ID. 15777c478bd9Sstevel@tonic-gate */ 15787c478bd9Sstevel@tonic-gate static void 15797c478bd9Sstevel@tonic-gate zone_uniqid(zone_t *zone) 15807c478bd9Sstevel@tonic-gate { 15817c478bd9Sstevel@tonic-gate static uint64_t uniqid = 0; 15827c478bd9Sstevel@tonic-gate 15837c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 15847c478bd9Sstevel@tonic-gate zone->zone_uniqid = uniqid++; 15857c478bd9Sstevel@tonic-gate } 15867c478bd9Sstevel@tonic-gate 15877c478bd9Sstevel@tonic-gate /* 15887c478bd9Sstevel@tonic-gate * Returns a held pointer to the "kcred" for the specified zone. 15897c478bd9Sstevel@tonic-gate */ 15907c478bd9Sstevel@tonic-gate struct cred * 15917c478bd9Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid) 15927c478bd9Sstevel@tonic-gate { 15937c478bd9Sstevel@tonic-gate zone_t *zone; 15947c478bd9Sstevel@tonic-gate cred_t *cr; 15957c478bd9Sstevel@tonic-gate 15967c478bd9Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) 15977c478bd9Sstevel@tonic-gate return (NULL); 15987c478bd9Sstevel@tonic-gate cr = zone->zone_kcred; 15997c478bd9Sstevel@tonic-gate crhold(cr); 16007c478bd9Sstevel@tonic-gate zone_rele(zone); 16017c478bd9Sstevel@tonic-gate return (cr); 16027c478bd9Sstevel@tonic-gate } 16037c478bd9Sstevel@tonic-gate 16040209230bSgjelinek static int 16050209230bSgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw) 16060209230bSgjelinek { 16070209230bSgjelinek zone_t *zone = ksp->ks_private; 16080209230bSgjelinek zone_kstat_t *zk = ksp->ks_data; 16090209230bSgjelinek 16100209230bSgjelinek if (rw == KSTAT_WRITE) 16110209230bSgjelinek return (EACCES); 16120209230bSgjelinek 16130209230bSgjelinek zk->zk_usage.value.ui64 = zone->zone_locked_mem; 16140209230bSgjelinek zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl; 16150209230bSgjelinek return (0); 16160209230bSgjelinek } 16170209230bSgjelinek 16180209230bSgjelinek static int 16190209230bSgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw) 16200209230bSgjelinek { 16210209230bSgjelinek zone_t *zone = ksp->ks_private; 16220209230bSgjelinek zone_kstat_t *zk = ksp->ks_data; 16230209230bSgjelinek 16240209230bSgjelinek if (rw == KSTAT_WRITE) 16250209230bSgjelinek return (EACCES); 16260209230bSgjelinek 16270209230bSgjelinek zk->zk_usage.value.ui64 = zone->zone_max_swap; 16280209230bSgjelinek zk->zk_value.value.ui64 = zone->zone_max_swap_ctl; 16290209230bSgjelinek return (0); 16300209230bSgjelinek } 16310209230bSgjelinek 16320209230bSgjelinek static void 16330209230bSgjelinek zone_kstat_create(zone_t *zone) 16340209230bSgjelinek { 16350209230bSgjelinek kstat_t *ksp; 16360209230bSgjelinek zone_kstat_t *zk; 16370209230bSgjelinek 16380209230bSgjelinek ksp = rctl_kstat_create_zone(zone, "lockedmem", KSTAT_TYPE_NAMED, 16390209230bSgjelinek sizeof (zone_kstat_t) / sizeof (kstat_named_t), 16400209230bSgjelinek KSTAT_FLAG_VIRTUAL); 16410209230bSgjelinek 16420209230bSgjelinek if (ksp == NULL) 16430209230bSgjelinek return; 16440209230bSgjelinek 16450209230bSgjelinek zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 16460209230bSgjelinek ksp->ks_data_size += strlen(zone->zone_name) + 1; 16470209230bSgjelinek kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 16480209230bSgjelinek kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 16490209230bSgjelinek kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 16500209230bSgjelinek kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 16510209230bSgjelinek ksp->ks_update = zone_lockedmem_kstat_update; 16520209230bSgjelinek ksp->ks_private = zone; 16530209230bSgjelinek kstat_install(ksp); 16540209230bSgjelinek 16550209230bSgjelinek zone->zone_lockedmem_kstat = ksp; 16560209230bSgjelinek 16570209230bSgjelinek ksp = rctl_kstat_create_zone(zone, "swapresv", KSTAT_TYPE_NAMED, 16580209230bSgjelinek sizeof (zone_kstat_t) / sizeof (kstat_named_t), 16590209230bSgjelinek KSTAT_FLAG_VIRTUAL); 16600209230bSgjelinek 16610209230bSgjelinek if (ksp == NULL) 16620209230bSgjelinek return; 16630209230bSgjelinek 16640209230bSgjelinek zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 16650209230bSgjelinek ksp->ks_data_size += strlen(zone->zone_name) + 1; 16660209230bSgjelinek kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 16670209230bSgjelinek kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 16680209230bSgjelinek kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 16690209230bSgjelinek kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 16700209230bSgjelinek ksp->ks_update = zone_swapresv_kstat_update; 16710209230bSgjelinek ksp->ks_private = zone; 16720209230bSgjelinek kstat_install(ksp); 16730209230bSgjelinek 16740209230bSgjelinek zone->zone_swapresv_kstat = ksp; 16750209230bSgjelinek } 16760209230bSgjelinek 16770209230bSgjelinek static void 16780209230bSgjelinek zone_kstat_delete(zone_t *zone) 16790209230bSgjelinek { 16800209230bSgjelinek void *data; 16810209230bSgjelinek 16820209230bSgjelinek if (zone->zone_lockedmem_kstat != NULL) { 16830209230bSgjelinek data = zone->zone_lockedmem_kstat->ks_data; 16840209230bSgjelinek kstat_delete(zone->zone_lockedmem_kstat); 16850209230bSgjelinek kmem_free(data, sizeof (zone_kstat_t)); 16860209230bSgjelinek } 16870209230bSgjelinek if (zone->zone_swapresv_kstat != NULL) { 16880209230bSgjelinek data = zone->zone_swapresv_kstat->ks_data; 16890209230bSgjelinek kstat_delete(zone->zone_swapresv_kstat); 16900209230bSgjelinek kmem_free(data, sizeof (zone_kstat_t)); 16910209230bSgjelinek } 16920209230bSgjelinek } 16930209230bSgjelinek 16947c478bd9Sstevel@tonic-gate /* 16957c478bd9Sstevel@tonic-gate * Called very early on in boot to initialize the ZSD list so that 16967c478bd9Sstevel@tonic-gate * zone_key_create() can be called before zone_init(). It also initializes 16977c478bd9Sstevel@tonic-gate * portions of zone0 which may be used before zone_init() is called. The 16987c478bd9Sstevel@tonic-gate * variable "global_zone" will be set when zone0 is fully initialized by 16997c478bd9Sstevel@tonic-gate * zone_init(). 17007c478bd9Sstevel@tonic-gate */ 17017c478bd9Sstevel@tonic-gate void 17027c478bd9Sstevel@tonic-gate zone_zsd_init(void) 17037c478bd9Sstevel@tonic-gate { 17047c478bd9Sstevel@tonic-gate mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 17057c478bd9Sstevel@tonic-gate mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 17067c478bd9Sstevel@tonic-gate list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 17077c478bd9Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 17087c478bd9Sstevel@tonic-gate list_create(&zone_active, sizeof (zone_t), 17097c478bd9Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 17107c478bd9Sstevel@tonic-gate list_create(&zone_deathrow, sizeof (zone_t), 17117c478bd9Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 17127c478bd9Sstevel@tonic-gate 17137c478bd9Sstevel@tonic-gate mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 17147c478bd9Sstevel@tonic-gate mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 17150209230bSgjelinek mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 17167c478bd9Sstevel@tonic-gate zone0.zone_shares = 1; 17170209230bSgjelinek zone0.zone_nlwps = 0; 17187c478bd9Sstevel@tonic-gate zone0.zone_nlwps_ctl = INT_MAX; 17190209230bSgjelinek zone0.zone_locked_mem = 0; 17200209230bSgjelinek zone0.zone_locked_mem_ctl = UINT64_MAX; 17210209230bSgjelinek ASSERT(zone0.zone_max_swap == 0); 17220209230bSgjelinek zone0.zone_max_swap_ctl = UINT64_MAX; 1723824c205fSml zone0.zone_shmmax = 0; 1724824c205fSml zone0.zone_ipc.ipcq_shmmni = 0; 1725824c205fSml zone0.zone_ipc.ipcq_semmni = 0; 1726824c205fSml zone0.zone_ipc.ipcq_msgmni = 0; 17277c478bd9Sstevel@tonic-gate zone0.zone_name = GLOBAL_ZONENAME; 17287c478bd9Sstevel@tonic-gate zone0.zone_nodename = utsname.nodename; 17297c478bd9Sstevel@tonic-gate zone0.zone_domain = srpc_domain; 17307c478bd9Sstevel@tonic-gate zone0.zone_ref = 1; 17317c478bd9Sstevel@tonic-gate zone0.zone_id = GLOBAL_ZONEID; 17327c478bd9Sstevel@tonic-gate zone0.zone_status = ZONE_IS_RUNNING; 17337c478bd9Sstevel@tonic-gate zone0.zone_rootpath = "/"; 17347c478bd9Sstevel@tonic-gate zone0.zone_rootpathlen = 2; 17357c478bd9Sstevel@tonic-gate zone0.zone_psetid = ZONE_PS_INVAL; 17367c478bd9Sstevel@tonic-gate zone0.zone_ncpus = 0; 17377c478bd9Sstevel@tonic-gate zone0.zone_ncpus_online = 0; 17387c478bd9Sstevel@tonic-gate zone0.zone_proc_initpid = 1; 17393f2f09c1Sdp zone0.zone_initname = initname; 17400209230bSgjelinek zone0.zone_lockedmem_kstat = NULL; 17410209230bSgjelinek zone0.zone_swapresv_kstat = NULL; 17427c478bd9Sstevel@tonic-gate list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 17437c478bd9Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 17447c478bd9Sstevel@tonic-gate list_insert_head(&zone_active, &zone0); 17457c478bd9Sstevel@tonic-gate 17467c478bd9Sstevel@tonic-gate /* 17477c478bd9Sstevel@tonic-gate * The root filesystem is not mounted yet, so zone_rootvp cannot be set 17487c478bd9Sstevel@tonic-gate * to anything meaningful. It is assigned to be 'rootdir' in 17497c478bd9Sstevel@tonic-gate * vfs_mountroot(). 17507c478bd9Sstevel@tonic-gate */ 17517c478bd9Sstevel@tonic-gate zone0.zone_rootvp = NULL; 17527c478bd9Sstevel@tonic-gate zone0.zone_vfslist = NULL; 17533f2f09c1Sdp zone0.zone_bootargs = initargs; 17547c478bd9Sstevel@tonic-gate zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 17557c478bd9Sstevel@tonic-gate /* 17567c478bd9Sstevel@tonic-gate * The global zone has all privileges 17577c478bd9Sstevel@tonic-gate */ 17587c478bd9Sstevel@tonic-gate priv_fillset(zone0.zone_privset); 17597c478bd9Sstevel@tonic-gate /* 17607c478bd9Sstevel@tonic-gate * Add p0 to the global zone 17617c478bd9Sstevel@tonic-gate */ 17627c478bd9Sstevel@tonic-gate zone0.zone_zsched = &p0; 17637c478bd9Sstevel@tonic-gate p0.p_zone = &zone0; 17647c478bd9Sstevel@tonic-gate } 17657c478bd9Sstevel@tonic-gate 176645916cd2Sjpk /* 176745916cd2Sjpk * Compute a hash value based on the contents of the label and the DOI. The 176845916cd2Sjpk * hash algorithm is somewhat arbitrary, but is based on the observation that 176945916cd2Sjpk * humans will likely pick labels that differ by amounts that work out to be 177045916cd2Sjpk * multiples of the number of hash chains, and thus stirring in some primes 177145916cd2Sjpk * should help. 177245916cd2Sjpk */ 177345916cd2Sjpk static uint_t 177445916cd2Sjpk hash_bylabel(void *hdata, mod_hash_key_t key) 177545916cd2Sjpk { 177645916cd2Sjpk const ts_label_t *lab = (ts_label_t *)key; 177745916cd2Sjpk const uint32_t *up, *ue; 177845916cd2Sjpk uint_t hash; 177945916cd2Sjpk int i; 178045916cd2Sjpk 178145916cd2Sjpk _NOTE(ARGUNUSED(hdata)); 178245916cd2Sjpk 178345916cd2Sjpk hash = lab->tsl_doi + (lab->tsl_doi << 1); 178445916cd2Sjpk /* we depend on alignment of label, but not representation */ 178545916cd2Sjpk up = (const uint32_t *)&lab->tsl_label; 178645916cd2Sjpk ue = up + sizeof (lab->tsl_label) / sizeof (*up); 178745916cd2Sjpk i = 1; 178845916cd2Sjpk while (up < ue) { 178945916cd2Sjpk /* using 2^n + 1, 1 <= n <= 16 as source of many primes */ 179045916cd2Sjpk hash += *up + (*up << ((i % 16) + 1)); 179145916cd2Sjpk up++; 179245916cd2Sjpk i++; 179345916cd2Sjpk } 179445916cd2Sjpk return (hash); 179545916cd2Sjpk } 179645916cd2Sjpk 179745916cd2Sjpk /* 179845916cd2Sjpk * All that mod_hash cares about here is zero (equal) versus non-zero (not 179945916cd2Sjpk * equal). This may need to be changed if less than / greater than is ever 180045916cd2Sjpk * needed. 180145916cd2Sjpk */ 180245916cd2Sjpk static int 180345916cd2Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 180445916cd2Sjpk { 180545916cd2Sjpk ts_label_t *lab1 = (ts_label_t *)key1; 180645916cd2Sjpk ts_label_t *lab2 = (ts_label_t *)key2; 180745916cd2Sjpk 180845916cd2Sjpk return (label_equal(lab1, lab2) ? 0 : 1); 180945916cd2Sjpk } 181045916cd2Sjpk 18117c478bd9Sstevel@tonic-gate /* 18127c478bd9Sstevel@tonic-gate * Called by main() to initialize the zones framework. 18137c478bd9Sstevel@tonic-gate */ 18147c478bd9Sstevel@tonic-gate void 18157c478bd9Sstevel@tonic-gate zone_init(void) 18167c478bd9Sstevel@tonic-gate { 18177c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rde; 18187c478bd9Sstevel@tonic-gate rctl_val_t *dval; 18197c478bd9Sstevel@tonic-gate rctl_set_t *set; 18207c478bd9Sstevel@tonic-gate rctl_alloc_gp_t *gp; 18217c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 1822cf8f45c7Sdstaff int res; 18237c478bd9Sstevel@tonic-gate 18247c478bd9Sstevel@tonic-gate ASSERT(curproc == &p0); 18257c478bd9Sstevel@tonic-gate 18267c478bd9Sstevel@tonic-gate /* 18277c478bd9Sstevel@tonic-gate * Create ID space for zone IDs. ID 0 is reserved for the 18287c478bd9Sstevel@tonic-gate * global zone. 18297c478bd9Sstevel@tonic-gate */ 18307c478bd9Sstevel@tonic-gate zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 18317c478bd9Sstevel@tonic-gate 18327c478bd9Sstevel@tonic-gate /* 18337c478bd9Sstevel@tonic-gate * Initialize generic zone resource controls, if any. 18347c478bd9Sstevel@tonic-gate */ 18357c478bd9Sstevel@tonic-gate rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 18367c478bd9Sstevel@tonic-gate RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 183719f92332Sml RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER, 1838c97ad5cdSakolb FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops); 1839c97ad5cdSakolb 1840c97ad5cdSakolb rc_zone_cpu_cap = rctl_register("zone.cpu-cap", 1841c97ad5cdSakolb RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS | 1842c97ad5cdSakolb RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER | 1843c97ad5cdSakolb RCTL_GLOBAL_INFINITE, 1844c97ad5cdSakolb MAXCAP, MAXCAP, &zone_cpu_cap_ops); 18457c478bd9Sstevel@tonic-gate 18467c478bd9Sstevel@tonic-gate rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 18477c478bd9Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 18487c478bd9Sstevel@tonic-gate INT_MAX, INT_MAX, &zone_lwps_ops); 1849824c205fSml /* 1850824c205fSml * System V IPC resource controls 1851824c205fSml */ 1852824c205fSml rc_zone_msgmni = rctl_register("zone.max-msg-ids", 1853824c205fSml RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1854824c205fSml RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops); 1855824c205fSml 1856824c205fSml rc_zone_semmni = rctl_register("zone.max-sem-ids", 1857824c205fSml RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1858824c205fSml RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops); 1859824c205fSml 1860824c205fSml rc_zone_shmmni = rctl_register("zone.max-shm-ids", 1861824c205fSml RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1862824c205fSml RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops); 1863824c205fSml 1864824c205fSml rc_zone_shmmax = rctl_register("zone.max-shm-memory", 1865824c205fSml RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1866824c205fSml RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops); 1867824c205fSml 18687c478bd9Sstevel@tonic-gate /* 18697c478bd9Sstevel@tonic-gate * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 18707c478bd9Sstevel@tonic-gate * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 18717c478bd9Sstevel@tonic-gate */ 18727c478bd9Sstevel@tonic-gate dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 18737c478bd9Sstevel@tonic-gate bzero(dval, sizeof (rctl_val_t)); 18747c478bd9Sstevel@tonic-gate dval->rcv_value = 1; 18757c478bd9Sstevel@tonic-gate dval->rcv_privilege = RCPRIV_PRIVILEGED; 18767c478bd9Sstevel@tonic-gate dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 18777c478bd9Sstevel@tonic-gate dval->rcv_action_recip_pid = -1; 18787c478bd9Sstevel@tonic-gate 18797c478bd9Sstevel@tonic-gate rde = rctl_dict_lookup("zone.cpu-shares"); 18807c478bd9Sstevel@tonic-gate (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 18817c478bd9Sstevel@tonic-gate 1882c6939658Ssl rc_zone_locked_mem = rctl_register("zone.max-locked-memory", 1883c6939658Ssl RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 1884c6939658Ssl RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 1885c6939658Ssl &zone_locked_mem_ops); 18860209230bSgjelinek 18870209230bSgjelinek rc_zone_max_swap = rctl_register("zone.max-swap", 18880209230bSgjelinek RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 18890209230bSgjelinek RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 18900209230bSgjelinek &zone_max_swap_ops); 18910209230bSgjelinek 18927c478bd9Sstevel@tonic-gate /* 18937c478bd9Sstevel@tonic-gate * Initialize the ``global zone''. 18947c478bd9Sstevel@tonic-gate */ 18957c478bd9Sstevel@tonic-gate set = rctl_set_create(); 18967c478bd9Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 18977c478bd9Sstevel@tonic-gate mutex_enter(&p0.p_lock); 18987c478bd9Sstevel@tonic-gate e.rcep_p.zone = &zone0; 18997c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 19007c478bd9Sstevel@tonic-gate zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 19017c478bd9Sstevel@tonic-gate gp); 19027c478bd9Sstevel@tonic-gate 19037c478bd9Sstevel@tonic-gate zone0.zone_nlwps = p0.p_lwpcnt; 19047c478bd9Sstevel@tonic-gate zone0.zone_ntasks = 1; 19057c478bd9Sstevel@tonic-gate mutex_exit(&p0.p_lock); 19069acbbeafSnn zone0.zone_restart_init = B_TRUE; 19079acbbeafSnn zone0.zone_brand = &native_brand; 19087c478bd9Sstevel@tonic-gate rctl_prealloc_destroy(gp); 19097c478bd9Sstevel@tonic-gate /* 19100209230bSgjelinek * pool_default hasn't been initialized yet, so we let pool_init() 19110209230bSgjelinek * take care of making sure the global zone is in the default pool. 19120209230bSgjelinek */ 19130209230bSgjelinek 19140209230bSgjelinek /* 19150209230bSgjelinek * Initialize global zone kstats 19167c478bd9Sstevel@tonic-gate */ 19170209230bSgjelinek zone_kstat_create(&zone0); 191845916cd2Sjpk 191945916cd2Sjpk /* 192045916cd2Sjpk * Initialize zone label. 192145916cd2Sjpk * mlp are initialized when tnzonecfg is loaded. 192245916cd2Sjpk */ 192345916cd2Sjpk zone0.zone_slabel = l_admin_low; 192445916cd2Sjpk rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 192545916cd2Sjpk label_hold(l_admin_low); 192645916cd2Sjpk 19277c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 19287c478bd9Sstevel@tonic-gate zone_uniqid(&zone0); 19297c478bd9Sstevel@tonic-gate ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 193045916cd2Sjpk 19317c478bd9Sstevel@tonic-gate zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 19327c478bd9Sstevel@tonic-gate mod_hash_null_valdtor); 19337c478bd9Sstevel@tonic-gate zonehashbyname = mod_hash_create_strhash("zone_by_name", 19347c478bd9Sstevel@tonic-gate zone_hash_size, mod_hash_null_valdtor); 193545916cd2Sjpk /* 193645916cd2Sjpk * maintain zonehashbylabel only for labeled systems 193745916cd2Sjpk */ 193845916cd2Sjpk if (is_system_labeled()) 193945916cd2Sjpk zonehashbylabel = mod_hash_create_extended("zone_by_label", 194045916cd2Sjpk zone_hash_size, mod_hash_null_keydtor, 194145916cd2Sjpk mod_hash_null_valdtor, hash_bylabel, NULL, 194245916cd2Sjpk hash_labelkey_cmp, KM_SLEEP); 19437c478bd9Sstevel@tonic-gate zonecount = 1; 19447c478bd9Sstevel@tonic-gate 19457c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 19467c478bd9Sstevel@tonic-gate (mod_hash_val_t)&zone0); 19477c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 19487c478bd9Sstevel@tonic-gate (mod_hash_val_t)&zone0); 194948451833Scarlsonj if (is_system_labeled()) { 195048451833Scarlsonj zone0.zone_flags |= ZF_HASHED_LABEL; 195145916cd2Sjpk (void) mod_hash_insert(zonehashbylabel, 195245916cd2Sjpk (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0); 195348451833Scarlsonj } 195445916cd2Sjpk mutex_exit(&zonehash_lock); 195545916cd2Sjpk 19567c478bd9Sstevel@tonic-gate /* 19577c478bd9Sstevel@tonic-gate * We avoid setting zone_kcred until now, since kcred is initialized 19587c478bd9Sstevel@tonic-gate * sometime after zone_zsd_init() and before zone_init(). 19597c478bd9Sstevel@tonic-gate */ 19607c478bd9Sstevel@tonic-gate zone0.zone_kcred = kcred; 19617c478bd9Sstevel@tonic-gate /* 19627c478bd9Sstevel@tonic-gate * The global zone is fully initialized (except for zone_rootvp which 19637c478bd9Sstevel@tonic-gate * will be set when the root filesystem is mounted). 19647c478bd9Sstevel@tonic-gate */ 19657c478bd9Sstevel@tonic-gate global_zone = &zone0; 1966cf8f45c7Sdstaff 1967cf8f45c7Sdstaff /* 1968cf8f45c7Sdstaff * Setup an event channel to send zone status change notifications on 1969cf8f45c7Sdstaff */ 1970cf8f45c7Sdstaff res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan, 1971cf8f45c7Sdstaff EVCH_CREAT); 1972cf8f45c7Sdstaff 1973cf8f45c7Sdstaff if (res) 1974cf8f45c7Sdstaff panic("Sysevent_evc_bind failed during zone setup.\n"); 19750209230bSgjelinek 19767c478bd9Sstevel@tonic-gate } 19777c478bd9Sstevel@tonic-gate 19787c478bd9Sstevel@tonic-gate static void 19797c478bd9Sstevel@tonic-gate zone_free(zone_t *zone) 19807c478bd9Sstevel@tonic-gate { 19817c478bd9Sstevel@tonic-gate ASSERT(zone != global_zone); 19827c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ntasks == 0); 19837c478bd9Sstevel@tonic-gate ASSERT(zone->zone_nlwps == 0); 19847c478bd9Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 0); 19857c478bd9Sstevel@tonic-gate ASSERT(zone->zone_kcred == NULL); 19867c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 19877c478bd9Sstevel@tonic-gate zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 19887c478bd9Sstevel@tonic-gate 1989c97ad5cdSakolb /* 1990c97ad5cdSakolb * Remove any zone caps. 1991c97ad5cdSakolb */ 1992c97ad5cdSakolb cpucaps_zone_remove(zone); 1993c97ad5cdSakolb 1994c97ad5cdSakolb ASSERT(zone->zone_cpucap == NULL); 1995c97ad5cdSakolb 19967c478bd9Sstevel@tonic-gate /* remove from deathrow list */ 19977c478bd9Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_DEAD) { 19987c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 19997c478bd9Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 20007c478bd9Sstevel@tonic-gate list_remove(&zone_deathrow, zone); 20017c478bd9Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 20027c478bd9Sstevel@tonic-gate } 20037c478bd9Sstevel@tonic-gate 20047c478bd9Sstevel@tonic-gate zone_free_zsd(zone); 2005fa9e4066Sahrens zone_free_datasets(zone); 20067c478bd9Sstevel@tonic-gate 20077c478bd9Sstevel@tonic-gate if (zone->zone_rootvp != NULL) 20087c478bd9Sstevel@tonic-gate VN_RELE(zone->zone_rootvp); 20097c478bd9Sstevel@tonic-gate if (zone->zone_rootpath) 20107c478bd9Sstevel@tonic-gate kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 20117c478bd9Sstevel@tonic-gate if (zone->zone_name != NULL) 20127c478bd9Sstevel@tonic-gate kmem_free(zone->zone_name, ZONENAME_MAX); 201345916cd2Sjpk if (zone->zone_slabel != NULL) 201445916cd2Sjpk label_rele(zone->zone_slabel); 20157c478bd9Sstevel@tonic-gate if (zone->zone_nodename != NULL) 20167c478bd9Sstevel@tonic-gate kmem_free(zone->zone_nodename, _SYS_NMLN); 20177c478bd9Sstevel@tonic-gate if (zone->zone_domain != NULL) 20187c478bd9Sstevel@tonic-gate kmem_free(zone->zone_domain, _SYS_NMLN); 20197c478bd9Sstevel@tonic-gate if (zone->zone_privset != NULL) 20207c478bd9Sstevel@tonic-gate kmem_free(zone->zone_privset, sizeof (priv_set_t)); 20217c478bd9Sstevel@tonic-gate if (zone->zone_rctls != NULL) 20227c478bd9Sstevel@tonic-gate rctl_set_free(zone->zone_rctls); 20237c478bd9Sstevel@tonic-gate if (zone->zone_bootargs != NULL) 20243f2f09c1Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 20253f2f09c1Sdp if (zone->zone_initname != NULL) 20263f2f09c1Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 20277c478bd9Sstevel@tonic-gate id_free(zoneid_space, zone->zone_id); 20287c478bd9Sstevel@tonic-gate mutex_destroy(&zone->zone_lock); 20297c478bd9Sstevel@tonic-gate cv_destroy(&zone->zone_cv); 203045916cd2Sjpk rw_destroy(&zone->zone_mlps.mlpl_rwlock); 20317c478bd9Sstevel@tonic-gate kmem_free(zone, sizeof (zone_t)); 20327c478bd9Sstevel@tonic-gate } 20337c478bd9Sstevel@tonic-gate 20347c478bd9Sstevel@tonic-gate /* 20357c478bd9Sstevel@tonic-gate * See block comment at the top of this file for information about zone 20367c478bd9Sstevel@tonic-gate * status values. 20377c478bd9Sstevel@tonic-gate */ 20387c478bd9Sstevel@tonic-gate /* 20397c478bd9Sstevel@tonic-gate * Convenience function for setting zone status. 20407c478bd9Sstevel@tonic-gate */ 20417c478bd9Sstevel@tonic-gate static void 20427c478bd9Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status) 20437c478bd9Sstevel@tonic-gate { 2044cf8f45c7Sdstaff 2045cf8f45c7Sdstaff nvlist_t *nvl = NULL; 20467c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zone_status_lock)); 20477c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 20487c478bd9Sstevel@tonic-gate status >= zone_status_get(zone)); 2049cf8f45c7Sdstaff 2050cf8f45c7Sdstaff if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) || 2051cf8f45c7Sdstaff nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) || 2052cf8f45c7Sdstaff nvlist_add_string(nvl, ZONE_CB_NEWSTATE, 20533f2f09c1Sdp zone_status_table[status]) || 2054cf8f45c7Sdstaff nvlist_add_string(nvl, ZONE_CB_OLDSTATE, 20553f2f09c1Sdp zone_status_table[zone->zone_status]) || 2056cf8f45c7Sdstaff nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) || 2057cf8f45c7Sdstaff nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) || 2058cf8f45c7Sdstaff sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS, 20593f2f09c1Sdp ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) { 2060cf8f45c7Sdstaff #ifdef DEBUG 2061cf8f45c7Sdstaff (void) printf( 2062cf8f45c7Sdstaff "Failed to allocate and send zone state change event.\n"); 2063cf8f45c7Sdstaff #endif 2064cf8f45c7Sdstaff } 2065cf8f45c7Sdstaff nvlist_free(nvl); 2066cf8f45c7Sdstaff 20677c478bd9Sstevel@tonic-gate zone->zone_status = status; 2068cf8f45c7Sdstaff 20697c478bd9Sstevel@tonic-gate cv_broadcast(&zone->zone_cv); 20707c478bd9Sstevel@tonic-gate } 20717c478bd9Sstevel@tonic-gate 20727c478bd9Sstevel@tonic-gate /* 20737c478bd9Sstevel@tonic-gate * Public function to retrieve the zone status. The zone status may 20747c478bd9Sstevel@tonic-gate * change after it is retrieved. 20757c478bd9Sstevel@tonic-gate */ 20767c478bd9Sstevel@tonic-gate zone_status_t 20777c478bd9Sstevel@tonic-gate zone_status_get(zone_t *zone) 20787c478bd9Sstevel@tonic-gate { 20797c478bd9Sstevel@tonic-gate return (zone->zone_status); 20807c478bd9Sstevel@tonic-gate } 20817c478bd9Sstevel@tonic-gate 20827c478bd9Sstevel@tonic-gate static int 20837c478bd9Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 20847c478bd9Sstevel@tonic-gate { 20853f2f09c1Sdp char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP); 20863f2f09c1Sdp int err = 0; 20873f2f09c1Sdp 20883f2f09c1Sdp ASSERT(zone != global_zone); 20893f2f09c1Sdp if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0) 20903f2f09c1Sdp goto done; /* EFAULT or ENAMETOOLONG */ 20913f2f09c1Sdp 20923f2f09c1Sdp if (zone->zone_bootargs != NULL) 20933f2f09c1Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 20943f2f09c1Sdp 20953f2f09c1Sdp zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP); 20963f2f09c1Sdp (void) strcpy(zone->zone_bootargs, bootargs); 20973f2f09c1Sdp 20983f2f09c1Sdp done: 20993f2f09c1Sdp kmem_free(bootargs, BOOTARGS_MAX); 21003f2f09c1Sdp return (err); 21013f2f09c1Sdp } 21023f2f09c1Sdp 210359f2ff5cSedp static int 210459f2ff5cSedp zone_set_brand(zone_t *zone, const char *brand) 210559f2ff5cSedp { 210659f2ff5cSedp struct brand_attr *attrp; 210759f2ff5cSedp brand_t *bp; 210859f2ff5cSedp 210959f2ff5cSedp attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP); 211059f2ff5cSedp if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) { 211159f2ff5cSedp kmem_free(attrp, sizeof (struct brand_attr)); 211259f2ff5cSedp return (EFAULT); 211359f2ff5cSedp } 211459f2ff5cSedp 211559f2ff5cSedp bp = brand_register_zone(attrp); 211659f2ff5cSedp kmem_free(attrp, sizeof (struct brand_attr)); 211759f2ff5cSedp if (bp == NULL) 211859f2ff5cSedp return (EINVAL); 211959f2ff5cSedp 212059f2ff5cSedp /* 212159f2ff5cSedp * This is the only place where a zone can change it's brand. 212259f2ff5cSedp * We already need to hold zone_status_lock to check the zone 212359f2ff5cSedp * status, so we'll just use that lock to serialize zone 212459f2ff5cSedp * branding requests as well. 212559f2ff5cSedp */ 212659f2ff5cSedp mutex_enter(&zone_status_lock); 212759f2ff5cSedp 212859f2ff5cSedp /* Re-Branding is not allowed and the zone can't be booted yet */ 212959f2ff5cSedp if ((ZONE_IS_BRANDED(zone)) || 213059f2ff5cSedp (zone_status_get(zone) >= ZONE_IS_BOOTING)) { 213159f2ff5cSedp mutex_exit(&zone_status_lock); 213259f2ff5cSedp brand_unregister_zone(bp); 213359f2ff5cSedp return (EINVAL); 213459f2ff5cSedp } 213559f2ff5cSedp 213659f2ff5cSedp if (is_system_labeled() && 213759f2ff5cSedp strncmp(attrp->ba_brandname, NATIVE_BRAND_NAME, MAXNAMELEN) != 0) { 213859f2ff5cSedp mutex_exit(&zone_status_lock); 213959f2ff5cSedp brand_unregister_zone(bp); 214059f2ff5cSedp return (EPERM); 214159f2ff5cSedp } 214259f2ff5cSedp 2143319378d9Seh /* set up the brand specific data */ 214459f2ff5cSedp zone->zone_brand = bp; 2145319378d9Seh ZBROP(zone)->b_init_brand_data(zone); 2146319378d9Seh 214759f2ff5cSedp mutex_exit(&zone_status_lock); 214859f2ff5cSedp return (0); 214959f2ff5cSedp } 215059f2ff5cSedp 21513f2f09c1Sdp static int 21523f2f09c1Sdp zone_set_initname(zone_t *zone, const char *zone_initname) 21533f2f09c1Sdp { 21543f2f09c1Sdp char initname[INITNAME_SZ]; 21557c478bd9Sstevel@tonic-gate size_t len; 21563f2f09c1Sdp int err = 0; 21577c478bd9Sstevel@tonic-gate 21583f2f09c1Sdp ASSERT(zone != global_zone); 21593f2f09c1Sdp if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0) 21607c478bd9Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 21617c478bd9Sstevel@tonic-gate 21623f2f09c1Sdp if (zone->zone_initname != NULL) 21633f2f09c1Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 21643f2f09c1Sdp 21653f2f09c1Sdp zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP); 21663f2f09c1Sdp (void) strcpy(zone->zone_initname, initname); 21677c478bd9Sstevel@tonic-gate return (0); 21687c478bd9Sstevel@tonic-gate } 21697c478bd9Sstevel@tonic-gate 21700209230bSgjelinek static int 21710209230bSgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap) 21720209230bSgjelinek { 21730209230bSgjelinek uint64_t mcap; 21740209230bSgjelinek int err = 0; 21750209230bSgjelinek 21760209230bSgjelinek if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0) 21770209230bSgjelinek zone->zone_phys_mcap = mcap; 21780209230bSgjelinek 21790209230bSgjelinek return (err); 21800209230bSgjelinek } 21810209230bSgjelinek 21820209230bSgjelinek static int 21830209230bSgjelinek zone_set_sched_class(zone_t *zone, const char *new_class) 21840209230bSgjelinek { 21850209230bSgjelinek char sched_class[PC_CLNMSZ]; 21860209230bSgjelinek id_t classid; 21870209230bSgjelinek int err; 21880209230bSgjelinek 21890209230bSgjelinek ASSERT(zone != global_zone); 21900209230bSgjelinek if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0) 21910209230bSgjelinek return (err); /* EFAULT or ENAMETOOLONG */ 21920209230bSgjelinek 21930209230bSgjelinek if (getcid(sched_class, &classid) != 0 || classid == syscid) 21940209230bSgjelinek return (set_errno(EINVAL)); 21950209230bSgjelinek zone->zone_defaultcid = classid; 21960209230bSgjelinek ASSERT(zone->zone_defaultcid > 0 && 21970209230bSgjelinek zone->zone_defaultcid < loaded_classes); 21980209230bSgjelinek 21990209230bSgjelinek return (0); 22000209230bSgjelinek } 22010209230bSgjelinek 22027c478bd9Sstevel@tonic-gate /* 22037c478bd9Sstevel@tonic-gate * Block indefinitely waiting for (zone_status >= status) 22047c478bd9Sstevel@tonic-gate */ 22057c478bd9Sstevel@tonic-gate void 22067c478bd9Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status) 22077c478bd9Sstevel@tonic-gate { 22087c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 22097c478bd9Sstevel@tonic-gate 22107c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22117c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 22127c478bd9Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 22137c478bd9Sstevel@tonic-gate } 22147c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22157c478bd9Sstevel@tonic-gate } 22167c478bd9Sstevel@tonic-gate 22177c478bd9Sstevel@tonic-gate /* 22187c478bd9Sstevel@tonic-gate * Private CPR-safe version of zone_status_wait(). 22197c478bd9Sstevel@tonic-gate */ 22207c478bd9Sstevel@tonic-gate static void 22217c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 22227c478bd9Sstevel@tonic-gate { 22237c478bd9Sstevel@tonic-gate callb_cpr_t cprinfo; 22247c478bd9Sstevel@tonic-gate 22257c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 22267c478bd9Sstevel@tonic-gate 22277c478bd9Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 22287c478bd9Sstevel@tonic-gate str); 22297c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22307c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 22317c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 22327c478bd9Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 22337c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 22347c478bd9Sstevel@tonic-gate } 22357c478bd9Sstevel@tonic-gate /* 22367c478bd9Sstevel@tonic-gate * zone_status_lock is implicitly released by the following. 22377c478bd9Sstevel@tonic-gate */ 22387c478bd9Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 22397c478bd9Sstevel@tonic-gate } 22407c478bd9Sstevel@tonic-gate 22417c478bd9Sstevel@tonic-gate /* 22427c478bd9Sstevel@tonic-gate * Block until zone enters requested state or signal is received. Return (0) 22437c478bd9Sstevel@tonic-gate * if signaled, non-zero otherwise. 22447c478bd9Sstevel@tonic-gate */ 22457c478bd9Sstevel@tonic-gate int 22467c478bd9Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status) 22477c478bd9Sstevel@tonic-gate { 22487c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 22497c478bd9Sstevel@tonic-gate 22507c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22517c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 22527c478bd9Sstevel@tonic-gate if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 22537c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22547c478bd9Sstevel@tonic-gate return (0); 22557c478bd9Sstevel@tonic-gate } 22567c478bd9Sstevel@tonic-gate } 22577c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22587c478bd9Sstevel@tonic-gate return (1); 22597c478bd9Sstevel@tonic-gate } 22607c478bd9Sstevel@tonic-gate 22617c478bd9Sstevel@tonic-gate /* 22627c478bd9Sstevel@tonic-gate * Block until the zone enters the requested state or the timeout expires, 22637c478bd9Sstevel@tonic-gate * whichever happens first. Return (-1) if operation timed out, time remaining 22647c478bd9Sstevel@tonic-gate * otherwise. 22657c478bd9Sstevel@tonic-gate */ 22667c478bd9Sstevel@tonic-gate clock_t 22677c478bd9Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 22687c478bd9Sstevel@tonic-gate { 22697c478bd9Sstevel@tonic-gate clock_t timeleft = 0; 22707c478bd9Sstevel@tonic-gate 22717c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 22727c478bd9Sstevel@tonic-gate 22737c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22747c478bd9Sstevel@tonic-gate while (zone->zone_status < status && timeleft != -1) { 22757c478bd9Sstevel@tonic-gate timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 22767c478bd9Sstevel@tonic-gate } 22777c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22787c478bd9Sstevel@tonic-gate return (timeleft); 22797c478bd9Sstevel@tonic-gate } 22807c478bd9Sstevel@tonic-gate 22817c478bd9Sstevel@tonic-gate /* 22827c478bd9Sstevel@tonic-gate * Block until the zone enters the requested state, the current process is 22837c478bd9Sstevel@tonic-gate * signaled, or the timeout expires, whichever happens first. Return (-1) if 22847c478bd9Sstevel@tonic-gate * operation timed out, 0 if signaled, time remaining otherwise. 22857c478bd9Sstevel@tonic-gate */ 22867c478bd9Sstevel@tonic-gate clock_t 22877c478bd9Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 22887c478bd9Sstevel@tonic-gate { 22897c478bd9Sstevel@tonic-gate clock_t timeleft = tim - lbolt; 22907c478bd9Sstevel@tonic-gate 22917c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 22927c478bd9Sstevel@tonic-gate 22937c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22947c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 22957c478bd9Sstevel@tonic-gate timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 22967c478bd9Sstevel@tonic-gate tim); 22977c478bd9Sstevel@tonic-gate if (timeleft <= 0) 22987c478bd9Sstevel@tonic-gate break; 22997c478bd9Sstevel@tonic-gate } 23007c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 23017c478bd9Sstevel@tonic-gate return (timeleft); 23027c478bd9Sstevel@tonic-gate } 23037c478bd9Sstevel@tonic-gate 23047c478bd9Sstevel@tonic-gate /* 23057c478bd9Sstevel@tonic-gate * Zones have two reference counts: one for references from credential 23067c478bd9Sstevel@tonic-gate * structures (zone_cred_ref), and one (zone_ref) for everything else. 23077c478bd9Sstevel@tonic-gate * This is so we can allow a zone to be rebooted while there are still 23087c478bd9Sstevel@tonic-gate * outstanding cred references, since certain drivers cache dblks (which 23097c478bd9Sstevel@tonic-gate * implicitly results in cached creds). We wait for zone_ref to drop to 23107c478bd9Sstevel@tonic-gate * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 23117c478bd9Sstevel@tonic-gate * later freed when the zone_cred_ref drops to 0, though nothing other 23127c478bd9Sstevel@tonic-gate * than the zone id and privilege set should be accessed once the zone 23137c478bd9Sstevel@tonic-gate * is "dead". 23147c478bd9Sstevel@tonic-gate * 23157c478bd9Sstevel@tonic-gate * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 23167c478bd9Sstevel@tonic-gate * to force halt/reboot to block waiting for the zone_cred_ref to drop 23177c478bd9Sstevel@tonic-gate * to 0. This can be useful to flush out other sources of cached creds 23187c478bd9Sstevel@tonic-gate * that may be less innocuous than the driver case. 23197c478bd9Sstevel@tonic-gate */ 23207c478bd9Sstevel@tonic-gate 23217c478bd9Sstevel@tonic-gate int zone_wait_for_cred = 0; 23227c478bd9Sstevel@tonic-gate 23237c478bd9Sstevel@tonic-gate static void 23247c478bd9Sstevel@tonic-gate zone_hold_locked(zone_t *z) 23257c478bd9Sstevel@tonic-gate { 23267c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&z->zone_lock)); 23277c478bd9Sstevel@tonic-gate z->zone_ref++; 23287c478bd9Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 23297c478bd9Sstevel@tonic-gate } 23307c478bd9Sstevel@tonic-gate 23317c478bd9Sstevel@tonic-gate void 23327c478bd9Sstevel@tonic-gate zone_hold(zone_t *z) 23337c478bd9Sstevel@tonic-gate { 23347c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 23357c478bd9Sstevel@tonic-gate zone_hold_locked(z); 23367c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 23377c478bd9Sstevel@tonic-gate } 23387c478bd9Sstevel@tonic-gate 23397c478bd9Sstevel@tonic-gate /* 23407c478bd9Sstevel@tonic-gate * If the non-cred ref count drops to 1 and either the cred ref count 23417c478bd9Sstevel@tonic-gate * is 0 or we aren't waiting for cred references, the zone is ready to 23427c478bd9Sstevel@tonic-gate * be destroyed. 23437c478bd9Sstevel@tonic-gate */ 23447c478bd9Sstevel@tonic-gate #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 23457c478bd9Sstevel@tonic-gate (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 23467c478bd9Sstevel@tonic-gate 23477c478bd9Sstevel@tonic-gate void 23487c478bd9Sstevel@tonic-gate zone_rele(zone_t *z) 23497c478bd9Sstevel@tonic-gate { 23507c478bd9Sstevel@tonic-gate boolean_t wakeup; 23517c478bd9Sstevel@tonic-gate 23527c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 23537c478bd9Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 23547c478bd9Sstevel@tonic-gate z->zone_ref--; 23557c478bd9Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 23567c478bd9Sstevel@tonic-gate /* no more refs, free the structure */ 23577c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 23587c478bd9Sstevel@tonic-gate zone_free(z); 23597c478bd9Sstevel@tonic-gate return; 23607c478bd9Sstevel@tonic-gate } 23617c478bd9Sstevel@tonic-gate /* signal zone_destroy so the zone can finish halting */ 23627c478bd9Sstevel@tonic-gate wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 23637c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 23647c478bd9Sstevel@tonic-gate 23657c478bd9Sstevel@tonic-gate if (wakeup) { 23667c478bd9Sstevel@tonic-gate /* 23677c478bd9Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 23687c478bd9Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 23697c478bd9Sstevel@tonic-gate */ 23707c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 23717c478bd9Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 23727c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 23737c478bd9Sstevel@tonic-gate } 23747c478bd9Sstevel@tonic-gate } 23757c478bd9Sstevel@tonic-gate 23767c478bd9Sstevel@tonic-gate void 23777c478bd9Sstevel@tonic-gate zone_cred_hold(zone_t *z) 23787c478bd9Sstevel@tonic-gate { 23797c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 23807c478bd9Sstevel@tonic-gate z->zone_cred_ref++; 23817c478bd9Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 23827c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 23837c478bd9Sstevel@tonic-gate } 23847c478bd9Sstevel@tonic-gate 23857c478bd9Sstevel@tonic-gate void 23867c478bd9Sstevel@tonic-gate zone_cred_rele(zone_t *z) 23877c478bd9Sstevel@tonic-gate { 23887c478bd9Sstevel@tonic-gate boolean_t wakeup; 23897c478bd9Sstevel@tonic-gate 23907c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 23917c478bd9Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 23927c478bd9Sstevel@tonic-gate z->zone_cred_ref--; 23937c478bd9Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 23947c478bd9Sstevel@tonic-gate /* no more refs, free the structure */ 23957c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 23967c478bd9Sstevel@tonic-gate zone_free(z); 23977c478bd9Sstevel@tonic-gate return; 23987c478bd9Sstevel@tonic-gate } 23997c478bd9Sstevel@tonic-gate /* 24007c478bd9Sstevel@tonic-gate * If zone_destroy is waiting for the cred references to drain 24017c478bd9Sstevel@tonic-gate * out, and they have, signal it. 24027c478bd9Sstevel@tonic-gate */ 24037c478bd9Sstevel@tonic-gate wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 24047c478bd9Sstevel@tonic-gate zone_status_get(z) >= ZONE_IS_DEAD); 24057c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 24067c478bd9Sstevel@tonic-gate 24077c478bd9Sstevel@tonic-gate if (wakeup) { 24087c478bd9Sstevel@tonic-gate /* 24097c478bd9Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 24107c478bd9Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 24117c478bd9Sstevel@tonic-gate */ 24127c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 24137c478bd9Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 24147c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 24157c478bd9Sstevel@tonic-gate } 24167c478bd9Sstevel@tonic-gate } 24177c478bd9Sstevel@tonic-gate 24187c478bd9Sstevel@tonic-gate void 24197c478bd9Sstevel@tonic-gate zone_task_hold(zone_t *z) 24207c478bd9Sstevel@tonic-gate { 24217c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 24227c478bd9Sstevel@tonic-gate z->zone_ntasks++; 24237c478bd9Sstevel@tonic-gate ASSERT(z->zone_ntasks != 0); 24247c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 24257c478bd9Sstevel@tonic-gate } 24267c478bd9Sstevel@tonic-gate 24277c478bd9Sstevel@tonic-gate void 24287c478bd9Sstevel@tonic-gate zone_task_rele(zone_t *zone) 24297c478bd9Sstevel@tonic-gate { 24307c478bd9Sstevel@tonic-gate uint_t refcnt; 24317c478bd9Sstevel@tonic-gate 24327c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 24337c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ntasks != 0); 24347c478bd9Sstevel@tonic-gate refcnt = --zone->zone_ntasks; 24357c478bd9Sstevel@tonic-gate if (refcnt > 1) { /* Common case */ 24367c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 24377c478bd9Sstevel@tonic-gate return; 24387c478bd9Sstevel@tonic-gate } 24397c478bd9Sstevel@tonic-gate zone_hold_locked(zone); /* so we can use the zone_t later */ 24407c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 24417c478bd9Sstevel@tonic-gate if (refcnt == 1) { 24427c478bd9Sstevel@tonic-gate /* 24437c478bd9Sstevel@tonic-gate * See if the zone is shutting down. 24447c478bd9Sstevel@tonic-gate */ 24457c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24467c478bd9Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 24477c478bd9Sstevel@tonic-gate goto out; 24487c478bd9Sstevel@tonic-gate } 24497c478bd9Sstevel@tonic-gate 24507c478bd9Sstevel@tonic-gate /* 24517c478bd9Sstevel@tonic-gate * Make sure the ntasks didn't change since we 24527c478bd9Sstevel@tonic-gate * dropped zone_lock. 24537c478bd9Sstevel@tonic-gate */ 24547c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 24557c478bd9Sstevel@tonic-gate if (refcnt != zone->zone_ntasks) { 24567c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 24577c478bd9Sstevel@tonic-gate goto out; 24587c478bd9Sstevel@tonic-gate } 24597c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 24607c478bd9Sstevel@tonic-gate 24617c478bd9Sstevel@tonic-gate /* 24627c478bd9Sstevel@tonic-gate * No more user processes in the zone. The zone is empty. 24637c478bd9Sstevel@tonic-gate */ 24647c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 24657c478bd9Sstevel@tonic-gate goto out; 24667c478bd9Sstevel@tonic-gate } 24677c478bd9Sstevel@tonic-gate 24687c478bd9Sstevel@tonic-gate ASSERT(refcnt == 0); 24697c478bd9Sstevel@tonic-gate /* 24707c478bd9Sstevel@tonic-gate * zsched has exited; the zone is dead. 24717c478bd9Sstevel@tonic-gate */ 24727c478bd9Sstevel@tonic-gate zone->zone_zsched = NULL; /* paranoia */ 24737c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24747c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DEAD); 24757c478bd9Sstevel@tonic-gate out: 24767c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 24777c478bd9Sstevel@tonic-gate zone_rele(zone); 24787c478bd9Sstevel@tonic-gate } 24797c478bd9Sstevel@tonic-gate 24807c478bd9Sstevel@tonic-gate zoneid_t 24817c478bd9Sstevel@tonic-gate getzoneid(void) 24827c478bd9Sstevel@tonic-gate { 24837c478bd9Sstevel@tonic-gate return (curproc->p_zone->zone_id); 24847c478bd9Sstevel@tonic-gate } 24857c478bd9Sstevel@tonic-gate 24867c478bd9Sstevel@tonic-gate /* 24877c478bd9Sstevel@tonic-gate * Internal versions of zone_find_by_*(). These don't zone_hold() or 24887c478bd9Sstevel@tonic-gate * check the validity of a zone's state. 24897c478bd9Sstevel@tonic-gate */ 24907c478bd9Sstevel@tonic-gate static zone_t * 24917c478bd9Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid) 24927c478bd9Sstevel@tonic-gate { 24937c478bd9Sstevel@tonic-gate mod_hash_val_t hv; 24947c478bd9Sstevel@tonic-gate zone_t *zone = NULL; 24957c478bd9Sstevel@tonic-gate 24967c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 24977c478bd9Sstevel@tonic-gate 24987c478bd9Sstevel@tonic-gate if (mod_hash_find(zonehashbyid, 24997c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 25007c478bd9Sstevel@tonic-gate zone = (zone_t *)hv; 25017c478bd9Sstevel@tonic-gate return (zone); 25027c478bd9Sstevel@tonic-gate } 25037c478bd9Sstevel@tonic-gate 250445916cd2Sjpk static zone_t * 250545916cd2Sjpk zone_find_all_by_label(const ts_label_t *label) 250645916cd2Sjpk { 250745916cd2Sjpk mod_hash_val_t hv; 250845916cd2Sjpk zone_t *zone = NULL; 250945916cd2Sjpk 251045916cd2Sjpk ASSERT(MUTEX_HELD(&zonehash_lock)); 251145916cd2Sjpk 251245916cd2Sjpk /* 251345916cd2Sjpk * zonehashbylabel is not maintained for unlabeled systems 251445916cd2Sjpk */ 251545916cd2Sjpk if (!is_system_labeled()) 251645916cd2Sjpk return (NULL); 251745916cd2Sjpk if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0) 251845916cd2Sjpk zone = (zone_t *)hv; 251945916cd2Sjpk return (zone); 252045916cd2Sjpk } 252145916cd2Sjpk 25227c478bd9Sstevel@tonic-gate static zone_t * 25237c478bd9Sstevel@tonic-gate zone_find_all_by_name(char *name) 25247c478bd9Sstevel@tonic-gate { 25257c478bd9Sstevel@tonic-gate mod_hash_val_t hv; 25267c478bd9Sstevel@tonic-gate zone_t *zone = NULL; 25277c478bd9Sstevel@tonic-gate 25287c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 25297c478bd9Sstevel@tonic-gate 25307c478bd9Sstevel@tonic-gate if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 25317c478bd9Sstevel@tonic-gate zone = (zone_t *)hv; 25327c478bd9Sstevel@tonic-gate return (zone); 25337c478bd9Sstevel@tonic-gate } 25347c478bd9Sstevel@tonic-gate 25357c478bd9Sstevel@tonic-gate /* 25367c478bd9Sstevel@tonic-gate * Public interface for looking up a zone by zoneid. Only returns the zone if 25377c478bd9Sstevel@tonic-gate * it is fully initialized, and has not yet begun the zone_destroy() sequence. 25387c478bd9Sstevel@tonic-gate * Caller must call zone_rele() once it is done with the zone. 25397c478bd9Sstevel@tonic-gate * 25407c478bd9Sstevel@tonic-gate * The zone may begin the zone_destroy() sequence immediately after this 25417c478bd9Sstevel@tonic-gate * function returns, but may be safely used until zone_rele() is called. 25427c478bd9Sstevel@tonic-gate */ 25437c478bd9Sstevel@tonic-gate zone_t * 25447c478bd9Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid) 25457c478bd9Sstevel@tonic-gate { 25467c478bd9Sstevel@tonic-gate zone_t *zone; 25477c478bd9Sstevel@tonic-gate zone_status_t status; 25487c478bd9Sstevel@tonic-gate 25497c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 25507c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 25517c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 25527c478bd9Sstevel@tonic-gate return (NULL); 25537c478bd9Sstevel@tonic-gate } 25547c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 25557c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 25567c478bd9Sstevel@tonic-gate /* 25577c478bd9Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 25587c478bd9Sstevel@tonic-gate */ 25597c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 25607c478bd9Sstevel@tonic-gate return (NULL); 25617c478bd9Sstevel@tonic-gate } 25627c478bd9Sstevel@tonic-gate zone_hold(zone); 25637c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 25647c478bd9Sstevel@tonic-gate return (zone); 25657c478bd9Sstevel@tonic-gate } 25667c478bd9Sstevel@tonic-gate 256745916cd2Sjpk /* 256845916cd2Sjpk * Similar to zone_find_by_id, but using zone label as the key. 256945916cd2Sjpk */ 257045916cd2Sjpk zone_t * 257145916cd2Sjpk zone_find_by_label(const ts_label_t *label) 257245916cd2Sjpk { 257345916cd2Sjpk zone_t *zone; 257442bc57c4Srica zone_status_t status; 257545916cd2Sjpk 257645916cd2Sjpk mutex_enter(&zonehash_lock); 257745916cd2Sjpk if ((zone = zone_find_all_by_label(label)) == NULL) { 257845916cd2Sjpk mutex_exit(&zonehash_lock); 257945916cd2Sjpk return (NULL); 258045916cd2Sjpk } 258142bc57c4Srica 258242bc57c4Srica status = zone_status_get(zone); 258342bc57c4Srica if (status > ZONE_IS_DOWN) { 258445916cd2Sjpk /* 258545916cd2Sjpk * For all practical purposes the zone doesn't exist. 258645916cd2Sjpk */ 258742bc57c4Srica mutex_exit(&zonehash_lock); 258842bc57c4Srica return (NULL); 258945916cd2Sjpk } 259042bc57c4Srica zone_hold(zone); 259145916cd2Sjpk mutex_exit(&zonehash_lock); 259245916cd2Sjpk return (zone); 259345916cd2Sjpk } 259445916cd2Sjpk 25957c478bd9Sstevel@tonic-gate /* 25967c478bd9Sstevel@tonic-gate * Similar to zone_find_by_id, but using zone name as the key. 25977c478bd9Sstevel@tonic-gate */ 25987c478bd9Sstevel@tonic-gate zone_t * 25997c478bd9Sstevel@tonic-gate zone_find_by_name(char *name) 26007c478bd9Sstevel@tonic-gate { 26017c478bd9Sstevel@tonic-gate zone_t *zone; 26027c478bd9Sstevel@tonic-gate zone_status_t status; 26037c478bd9Sstevel@tonic-gate 26047c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 26057c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_name(name)) == NULL) { 26067c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 26077c478bd9Sstevel@tonic-gate return (NULL); 26087c478bd9Sstevel@tonic-gate } 26097c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 26107c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 26117c478bd9Sstevel@tonic-gate /* 26127c478bd9Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 26137c478bd9Sstevel@tonic-gate */ 26147c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 26157c478bd9Sstevel@tonic-gate return (NULL); 26167c478bd9Sstevel@tonic-gate } 26177c478bd9Sstevel@tonic-gate zone_hold(zone); 26187c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 26197c478bd9Sstevel@tonic-gate return (zone); 26207c478bd9Sstevel@tonic-gate } 26217c478bd9Sstevel@tonic-gate 26227c478bd9Sstevel@tonic-gate /* 26237c478bd9Sstevel@tonic-gate * Similar to zone_find_by_id(), using the path as a key. For instance, 26247c478bd9Sstevel@tonic-gate * if there is a zone "foo" rooted at /foo/root, and the path argument 26257c478bd9Sstevel@tonic-gate * is "/foo/root/proc", it will return the held zone_t corresponding to 26267c478bd9Sstevel@tonic-gate * zone "foo". 26277c478bd9Sstevel@tonic-gate * 26287c478bd9Sstevel@tonic-gate * zone_find_by_path() always returns a non-NULL value, since at the 26297c478bd9Sstevel@tonic-gate * very least every path will be contained in the global zone. 26307c478bd9Sstevel@tonic-gate * 26317c478bd9Sstevel@tonic-gate * As with the other zone_find_by_*() functions, the caller is 26327c478bd9Sstevel@tonic-gate * responsible for zone_rele()ing the return value of this function. 26337c478bd9Sstevel@tonic-gate */ 26347c478bd9Sstevel@tonic-gate zone_t * 26357c478bd9Sstevel@tonic-gate zone_find_by_path(const char *path) 26367c478bd9Sstevel@tonic-gate { 26377c478bd9Sstevel@tonic-gate zone_t *zone; 26387c478bd9Sstevel@tonic-gate zone_t *zret = NULL; 26397c478bd9Sstevel@tonic-gate zone_status_t status; 26407c478bd9Sstevel@tonic-gate 26417c478bd9Sstevel@tonic-gate if (path == NULL) { 26427c478bd9Sstevel@tonic-gate /* 26437c478bd9Sstevel@tonic-gate * Call from rootconf(). 26447c478bd9Sstevel@tonic-gate */ 26457c478bd9Sstevel@tonic-gate zone_hold(global_zone); 26467c478bd9Sstevel@tonic-gate return (global_zone); 26477c478bd9Sstevel@tonic-gate } 26487c478bd9Sstevel@tonic-gate ASSERT(*path == '/'); 26497c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 26507c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 26517c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 26527c478bd9Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(path, zone)) 26537c478bd9Sstevel@tonic-gate zret = zone; 26547c478bd9Sstevel@tonic-gate } 26557c478bd9Sstevel@tonic-gate ASSERT(zret != NULL); 26567c478bd9Sstevel@tonic-gate status = zone_status_get(zret); 26577c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 26587c478bd9Sstevel@tonic-gate /* 26597c478bd9Sstevel@tonic-gate * Zone practically doesn't exist. 26607c478bd9Sstevel@tonic-gate */ 26617c478bd9Sstevel@tonic-gate zret = global_zone; 26627c478bd9Sstevel@tonic-gate } 26637c478bd9Sstevel@tonic-gate zone_hold(zret); 26647c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 26657c478bd9Sstevel@tonic-gate return (zret); 26667c478bd9Sstevel@tonic-gate } 26677c478bd9Sstevel@tonic-gate 26687c478bd9Sstevel@tonic-gate /* 26697c478bd9Sstevel@tonic-gate * Get the number of cpus visible to this zone. The system-wide global 26707c478bd9Sstevel@tonic-gate * 'ncpus' is returned if pools are disabled, the caller is in the 26717c478bd9Sstevel@tonic-gate * global zone, or a NULL zone argument is passed in. 26727c478bd9Sstevel@tonic-gate */ 26737c478bd9Sstevel@tonic-gate int 26747c478bd9Sstevel@tonic-gate zone_ncpus_get(zone_t *zone) 26757c478bd9Sstevel@tonic-gate { 26767c478bd9Sstevel@tonic-gate int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 26777c478bd9Sstevel@tonic-gate 26787c478bd9Sstevel@tonic-gate return (myncpus != 0 ? myncpus : ncpus); 26797c478bd9Sstevel@tonic-gate } 26807c478bd9Sstevel@tonic-gate 26817c478bd9Sstevel@tonic-gate /* 26827c478bd9Sstevel@tonic-gate * Get the number of online cpus visible to this zone. The system-wide 26837c478bd9Sstevel@tonic-gate * global 'ncpus_online' is returned if pools are disabled, the caller 26847c478bd9Sstevel@tonic-gate * is in the global zone, or a NULL zone argument is passed in. 26857c478bd9Sstevel@tonic-gate */ 26867c478bd9Sstevel@tonic-gate int 26877c478bd9Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone) 26887c478bd9Sstevel@tonic-gate { 26897c478bd9Sstevel@tonic-gate int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 26907c478bd9Sstevel@tonic-gate 26917c478bd9Sstevel@tonic-gate return (myncpus_online != 0 ? myncpus_online : ncpus_online); 26927c478bd9Sstevel@tonic-gate } 26937c478bd9Sstevel@tonic-gate 26947c478bd9Sstevel@tonic-gate /* 26957c478bd9Sstevel@tonic-gate * Return the pool to which the zone is currently bound. 26967c478bd9Sstevel@tonic-gate */ 26977c478bd9Sstevel@tonic-gate pool_t * 26987c478bd9Sstevel@tonic-gate zone_pool_get(zone_t *zone) 26997c478bd9Sstevel@tonic-gate { 27007c478bd9Sstevel@tonic-gate ASSERT(pool_lock_held()); 27017c478bd9Sstevel@tonic-gate 27027c478bd9Sstevel@tonic-gate return (zone->zone_pool); 27037c478bd9Sstevel@tonic-gate } 27047c478bd9Sstevel@tonic-gate 27057c478bd9Sstevel@tonic-gate /* 27067c478bd9Sstevel@tonic-gate * Set the zone's pool pointer and update the zone's visibility to match 27077c478bd9Sstevel@tonic-gate * the resources in the new pool. 27087c478bd9Sstevel@tonic-gate */ 27097c478bd9Sstevel@tonic-gate void 27107c478bd9Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool) 27117c478bd9Sstevel@tonic-gate { 27127c478bd9Sstevel@tonic-gate ASSERT(pool_lock_held()); 27137c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 27147c478bd9Sstevel@tonic-gate 27157c478bd9Sstevel@tonic-gate zone->zone_pool = pool; 27167c478bd9Sstevel@tonic-gate zone_pset_set(zone, pool->pool_pset->pset_id); 27177c478bd9Sstevel@tonic-gate } 27187c478bd9Sstevel@tonic-gate 27197c478bd9Sstevel@tonic-gate /* 27207c478bd9Sstevel@tonic-gate * Return the cached value of the id of the processor set to which the 27217c478bd9Sstevel@tonic-gate * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 27227c478bd9Sstevel@tonic-gate * facility is disabled. 27237c478bd9Sstevel@tonic-gate */ 27247c478bd9Sstevel@tonic-gate psetid_t 27257c478bd9Sstevel@tonic-gate zone_pset_get(zone_t *zone) 27267c478bd9Sstevel@tonic-gate { 27277c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 27287c478bd9Sstevel@tonic-gate 27297c478bd9Sstevel@tonic-gate return (zone->zone_psetid); 27307c478bd9Sstevel@tonic-gate } 27317c478bd9Sstevel@tonic-gate 27327c478bd9Sstevel@tonic-gate /* 27337c478bd9Sstevel@tonic-gate * Set the cached value of the id of the processor set to which the zone 27347c478bd9Sstevel@tonic-gate * is currently bound. Also update the zone's visibility to match the 27357c478bd9Sstevel@tonic-gate * resources in the new processor set. 27367c478bd9Sstevel@tonic-gate */ 27377c478bd9Sstevel@tonic-gate void 27387c478bd9Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid) 27397c478bd9Sstevel@tonic-gate { 27407c478bd9Sstevel@tonic-gate psetid_t oldpsetid; 27417c478bd9Sstevel@tonic-gate 27427c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 27437c478bd9Sstevel@tonic-gate oldpsetid = zone_pset_get(zone); 27447c478bd9Sstevel@tonic-gate 27457c478bd9Sstevel@tonic-gate if (oldpsetid == newpsetid) 27467c478bd9Sstevel@tonic-gate return; 27477c478bd9Sstevel@tonic-gate /* 27487c478bd9Sstevel@tonic-gate * Global zone sees all. 27497c478bd9Sstevel@tonic-gate */ 27507c478bd9Sstevel@tonic-gate if (zone != global_zone) { 27517c478bd9Sstevel@tonic-gate zone->zone_psetid = newpsetid; 27527c478bd9Sstevel@tonic-gate if (newpsetid != ZONE_PS_INVAL) 27537c478bd9Sstevel@tonic-gate pool_pset_visibility_add(newpsetid, zone); 27547c478bd9Sstevel@tonic-gate if (oldpsetid != ZONE_PS_INVAL) 27557c478bd9Sstevel@tonic-gate pool_pset_visibility_remove(oldpsetid, zone); 27567c478bd9Sstevel@tonic-gate } 27577c478bd9Sstevel@tonic-gate /* 27587c478bd9Sstevel@tonic-gate * Disabling pools, so we should start using the global values 27597c478bd9Sstevel@tonic-gate * for ncpus and ncpus_online. 27607c478bd9Sstevel@tonic-gate */ 27617c478bd9Sstevel@tonic-gate if (newpsetid == ZONE_PS_INVAL) { 27627c478bd9Sstevel@tonic-gate zone->zone_ncpus = 0; 27637c478bd9Sstevel@tonic-gate zone->zone_ncpus_online = 0; 27647c478bd9Sstevel@tonic-gate } 27657c478bd9Sstevel@tonic-gate } 27667c478bd9Sstevel@tonic-gate 27677c478bd9Sstevel@tonic-gate /* 27687c478bd9Sstevel@tonic-gate * Walk the list of active zones and issue the provided callback for 27697c478bd9Sstevel@tonic-gate * each of them. 27707c478bd9Sstevel@tonic-gate * 27717c478bd9Sstevel@tonic-gate * Caller must not be holding any locks that may be acquired under 27727c478bd9Sstevel@tonic-gate * zonehash_lock. See comment at the beginning of the file for a list of 27737c478bd9Sstevel@tonic-gate * common locks and their interactions with zones. 27747c478bd9Sstevel@tonic-gate */ 27757c478bd9Sstevel@tonic-gate int 27767c478bd9Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data) 27777c478bd9Sstevel@tonic-gate { 27787c478bd9Sstevel@tonic-gate zone_t *zone; 27797c478bd9Sstevel@tonic-gate int ret = 0; 27807c478bd9Sstevel@tonic-gate zone_status_t status; 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 27837c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 27847c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 27857c478bd9Sstevel@tonic-gate /* 27867c478bd9Sstevel@tonic-gate * Skip zones that shouldn't be externally visible. 27877c478bd9Sstevel@tonic-gate */ 27887c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 27897c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 27907c478bd9Sstevel@tonic-gate continue; 27917c478bd9Sstevel@tonic-gate /* 27927c478bd9Sstevel@tonic-gate * Bail immediately if any callback invocation returns a 27937c478bd9Sstevel@tonic-gate * non-zero value. 27947c478bd9Sstevel@tonic-gate */ 27957c478bd9Sstevel@tonic-gate ret = (*cb)(zone, data); 27967c478bd9Sstevel@tonic-gate if (ret != 0) 27977c478bd9Sstevel@tonic-gate break; 27987c478bd9Sstevel@tonic-gate } 27997c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 28007c478bd9Sstevel@tonic-gate return (ret); 28017c478bd9Sstevel@tonic-gate } 28027c478bd9Sstevel@tonic-gate 28037c478bd9Sstevel@tonic-gate static int 28047c478bd9Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath) 28057c478bd9Sstevel@tonic-gate { 28067c478bd9Sstevel@tonic-gate vnode_t *vp; 28077c478bd9Sstevel@tonic-gate int trycount; 28087c478bd9Sstevel@tonic-gate int error = 0; 28097c478bd9Sstevel@tonic-gate char *path; 28107c478bd9Sstevel@tonic-gate struct pathname upn, pn; 28117c478bd9Sstevel@tonic-gate size_t pathlen; 28127c478bd9Sstevel@tonic-gate 28137c478bd9Sstevel@tonic-gate if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 28147c478bd9Sstevel@tonic-gate return (error); 28157c478bd9Sstevel@tonic-gate 28167c478bd9Sstevel@tonic-gate pn_alloc(&pn); 28177c478bd9Sstevel@tonic-gate 28187c478bd9Sstevel@tonic-gate /* prevent infinite loop */ 28197c478bd9Sstevel@tonic-gate trycount = 10; 28207c478bd9Sstevel@tonic-gate for (;;) { 28217c478bd9Sstevel@tonic-gate if (--trycount <= 0) { 28227c478bd9Sstevel@tonic-gate error = ESTALE; 28237c478bd9Sstevel@tonic-gate goto out; 28247c478bd9Sstevel@tonic-gate } 28257c478bd9Sstevel@tonic-gate 28267c478bd9Sstevel@tonic-gate if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 28277c478bd9Sstevel@tonic-gate /* 28287c478bd9Sstevel@tonic-gate * VOP_ACCESS() may cover 'vp' with a new 28297c478bd9Sstevel@tonic-gate * filesystem, if 'vp' is an autoFS vnode. 28307c478bd9Sstevel@tonic-gate * Get the new 'vp' if so. 28317c478bd9Sstevel@tonic-gate */ 2832da6c28aaSamw if ((error = 2833da6c28aaSamw VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) == 0 && 283425d2dc23Seh (!vn_ismntpt(vp) || 28357c478bd9Sstevel@tonic-gate (error = traverse(&vp)) == 0)) { 28367c478bd9Sstevel@tonic-gate pathlen = pn.pn_pathlen + 2; 28377c478bd9Sstevel@tonic-gate path = kmem_alloc(pathlen, KM_SLEEP); 28387c478bd9Sstevel@tonic-gate (void) strncpy(path, pn.pn_path, 28397c478bd9Sstevel@tonic-gate pn.pn_pathlen + 1); 28407c478bd9Sstevel@tonic-gate path[pathlen - 2] = '/'; 28417c478bd9Sstevel@tonic-gate path[pathlen - 1] = '\0'; 28427c478bd9Sstevel@tonic-gate pn_free(&pn); 28437c478bd9Sstevel@tonic-gate pn_free(&upn); 28447c478bd9Sstevel@tonic-gate 28457c478bd9Sstevel@tonic-gate /* Success! */ 28467c478bd9Sstevel@tonic-gate break; 28477c478bd9Sstevel@tonic-gate } 28487c478bd9Sstevel@tonic-gate VN_RELE(vp); 28497c478bd9Sstevel@tonic-gate } 28507c478bd9Sstevel@tonic-gate if (error != ESTALE) 28517c478bd9Sstevel@tonic-gate goto out; 28527c478bd9Sstevel@tonic-gate } 28537c478bd9Sstevel@tonic-gate 28547c478bd9Sstevel@tonic-gate ASSERT(error == 0); 28557c478bd9Sstevel@tonic-gate zone->zone_rootvp = vp; /* we hold a reference to vp */ 28567c478bd9Sstevel@tonic-gate zone->zone_rootpath = path; 28577c478bd9Sstevel@tonic-gate zone->zone_rootpathlen = pathlen; 285848451833Scarlsonj if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0) 285948451833Scarlsonj zone->zone_flags |= ZF_IS_SCRATCH; 28607c478bd9Sstevel@tonic-gate return (0); 28617c478bd9Sstevel@tonic-gate 28627c478bd9Sstevel@tonic-gate out: 28637c478bd9Sstevel@tonic-gate pn_free(&pn); 28647c478bd9Sstevel@tonic-gate pn_free(&upn); 28657c478bd9Sstevel@tonic-gate return (error); 28667c478bd9Sstevel@tonic-gate } 28677c478bd9Sstevel@tonic-gate 28687c478bd9Sstevel@tonic-gate #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 28697c478bd9Sstevel@tonic-gate ((c) >= 'a' && (c) <= 'z') || \ 28707c478bd9Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'Z')) 28717c478bd9Sstevel@tonic-gate 28727c478bd9Sstevel@tonic-gate static int 28737c478bd9Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname) 28747c478bd9Sstevel@tonic-gate { 28757c478bd9Sstevel@tonic-gate char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 28767c478bd9Sstevel@tonic-gate size_t len; 28777c478bd9Sstevel@tonic-gate int i, err; 28787c478bd9Sstevel@tonic-gate 28797c478bd9Sstevel@tonic-gate if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 28807c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 28817c478bd9Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 28827c478bd9Sstevel@tonic-gate } 28837c478bd9Sstevel@tonic-gate 28847c478bd9Sstevel@tonic-gate /* must be less than ZONENAME_MAX */ 28857c478bd9Sstevel@tonic-gate if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 28867c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 28877c478bd9Sstevel@tonic-gate return (EINVAL); 28887c478bd9Sstevel@tonic-gate } 28897c478bd9Sstevel@tonic-gate 28907c478bd9Sstevel@tonic-gate /* 28917c478bd9Sstevel@tonic-gate * Name must start with an alphanumeric and must contain only 28927c478bd9Sstevel@tonic-gate * alphanumerics, '-', '_' and '.'. 28937c478bd9Sstevel@tonic-gate */ 28947c478bd9Sstevel@tonic-gate if (!isalnum(kname[0])) { 28957c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 28967c478bd9Sstevel@tonic-gate return (EINVAL); 28977c478bd9Sstevel@tonic-gate } 28987c478bd9Sstevel@tonic-gate for (i = 1; i < len - 1; i++) { 28997c478bd9Sstevel@tonic-gate if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 29007c478bd9Sstevel@tonic-gate kname[i] != '.') { 29017c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 29027c478bd9Sstevel@tonic-gate return (EINVAL); 29037c478bd9Sstevel@tonic-gate } 29047c478bd9Sstevel@tonic-gate } 29057c478bd9Sstevel@tonic-gate 29067c478bd9Sstevel@tonic-gate zone->zone_name = kname; 29077c478bd9Sstevel@tonic-gate return (0); 29087c478bd9Sstevel@tonic-gate } 29097c478bd9Sstevel@tonic-gate 29107c478bd9Sstevel@tonic-gate /* 29117c478bd9Sstevel@tonic-gate * Similar to thread_create(), but makes sure the thread is in the appropriate 29127c478bd9Sstevel@tonic-gate * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 29137c478bd9Sstevel@tonic-gate */ 29147c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 29157c478bd9Sstevel@tonic-gate kthread_t * 29167c478bd9Sstevel@tonic-gate zthread_create( 29177c478bd9Sstevel@tonic-gate caddr_t stk, 29187c478bd9Sstevel@tonic-gate size_t stksize, 29197c478bd9Sstevel@tonic-gate void (*proc)(), 29207c478bd9Sstevel@tonic-gate void *arg, 29217c478bd9Sstevel@tonic-gate size_t len, 29227c478bd9Sstevel@tonic-gate pri_t pri) 29237c478bd9Sstevel@tonic-gate { 29247c478bd9Sstevel@tonic-gate kthread_t *t; 29257c478bd9Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 29267c478bd9Sstevel@tonic-gate proc_t *pp = zone->zone_zsched; 29277c478bd9Sstevel@tonic-gate 29287c478bd9Sstevel@tonic-gate zone_hold(zone); /* Reference to be dropped when thread exits */ 29297c478bd9Sstevel@tonic-gate 29307c478bd9Sstevel@tonic-gate /* 29317c478bd9Sstevel@tonic-gate * No-one should be trying to create threads if the zone is shutting 29327c478bd9Sstevel@tonic-gate * down and there aren't any kernel threads around. See comment 29337c478bd9Sstevel@tonic-gate * in zthread_exit(). 29347c478bd9Sstevel@tonic-gate */ 29357c478bd9Sstevel@tonic-gate ASSERT(!(zone->zone_kthreads == NULL && 29367c478bd9Sstevel@tonic-gate zone_status_get(zone) >= ZONE_IS_EMPTY)); 29377c478bd9Sstevel@tonic-gate /* 29387c478bd9Sstevel@tonic-gate * Create a thread, but don't let it run until we've finished setting 29397c478bd9Sstevel@tonic-gate * things up. 29407c478bd9Sstevel@tonic-gate */ 29417c478bd9Sstevel@tonic-gate t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 29427c478bd9Sstevel@tonic-gate ASSERT(t->t_forw == NULL); 29437c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 29447c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 29457c478bd9Sstevel@tonic-gate t->t_forw = t->t_back = t; 29467c478bd9Sstevel@tonic-gate } else { 29477c478bd9Sstevel@tonic-gate kthread_t *tx = zone->zone_kthreads; 29487c478bd9Sstevel@tonic-gate 29497c478bd9Sstevel@tonic-gate t->t_forw = tx; 29507c478bd9Sstevel@tonic-gate t->t_back = tx->t_back; 29517c478bd9Sstevel@tonic-gate tx->t_back->t_forw = t; 29527c478bd9Sstevel@tonic-gate tx->t_back = t; 29537c478bd9Sstevel@tonic-gate } 29547c478bd9Sstevel@tonic-gate zone->zone_kthreads = t; 29557c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 29567c478bd9Sstevel@tonic-gate 29577c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 29587c478bd9Sstevel@tonic-gate t->t_proc_flag |= TP_ZTHREAD; 29597c478bd9Sstevel@tonic-gate project_rele(t->t_proj); 29607c478bd9Sstevel@tonic-gate t->t_proj = project_hold(pp->p_task->tk_proj); 29617c478bd9Sstevel@tonic-gate 29627c478bd9Sstevel@tonic-gate /* 29637c478bd9Sstevel@tonic-gate * Setup complete, let it run. 29647c478bd9Sstevel@tonic-gate */ 29657c478bd9Sstevel@tonic-gate thread_lock(t); 29667c478bd9Sstevel@tonic-gate t->t_schedflag |= TS_ALLSTART; 29677c478bd9Sstevel@tonic-gate setrun_locked(t); 29687c478bd9Sstevel@tonic-gate thread_unlock(t); 29697c478bd9Sstevel@tonic-gate 29707c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 29717c478bd9Sstevel@tonic-gate 29727c478bd9Sstevel@tonic-gate return (t); 29737c478bd9Sstevel@tonic-gate } 29747c478bd9Sstevel@tonic-gate 29757c478bd9Sstevel@tonic-gate /* 29767c478bd9Sstevel@tonic-gate * Similar to thread_exit(). Must be called by threads created via 29777c478bd9Sstevel@tonic-gate * zthread_exit(). 29787c478bd9Sstevel@tonic-gate */ 29797c478bd9Sstevel@tonic-gate void 29807c478bd9Sstevel@tonic-gate zthread_exit(void) 29817c478bd9Sstevel@tonic-gate { 29827c478bd9Sstevel@tonic-gate kthread_t *t = curthread; 29837c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 29847c478bd9Sstevel@tonic-gate zone_t *zone = pp->p_zone; 29857c478bd9Sstevel@tonic-gate 29867c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 29877c478bd9Sstevel@tonic-gate 29887c478bd9Sstevel@tonic-gate /* 29897c478bd9Sstevel@tonic-gate * Reparent to p0 29907c478bd9Sstevel@tonic-gate */ 2991b4b07f87Sjosephb kpreempt_disable(); 29927c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 29937c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_ZTHREAD; 29947c478bd9Sstevel@tonic-gate t->t_procp = &p0; 29957c478bd9Sstevel@tonic-gate hat_thread_exit(t); 29967c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2997b4b07f87Sjosephb kpreempt_enable(); 29987c478bd9Sstevel@tonic-gate 29997c478bd9Sstevel@tonic-gate if (t->t_back == t) { 30007c478bd9Sstevel@tonic-gate ASSERT(t->t_forw == t); 30017c478bd9Sstevel@tonic-gate /* 30027c478bd9Sstevel@tonic-gate * If the zone is empty, once the thread count 30037c478bd9Sstevel@tonic-gate * goes to zero no further kernel threads can be 30047c478bd9Sstevel@tonic-gate * created. This is because if the creator is a process 30057c478bd9Sstevel@tonic-gate * in the zone, then it must have exited before the zone 30067c478bd9Sstevel@tonic-gate * state could be set to ZONE_IS_EMPTY. 30077c478bd9Sstevel@tonic-gate * Otherwise, if the creator is a kernel thread in the 30087c478bd9Sstevel@tonic-gate * zone, the thread count is non-zero. 30097c478bd9Sstevel@tonic-gate * 30107c478bd9Sstevel@tonic-gate * This really means that non-zone kernel threads should 30117c478bd9Sstevel@tonic-gate * not create zone kernel threads. 30127c478bd9Sstevel@tonic-gate */ 30137c478bd9Sstevel@tonic-gate zone->zone_kthreads = NULL; 30147c478bd9Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_EMPTY) { 30157c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 3016c97ad5cdSakolb /* 3017c97ad5cdSakolb * Remove any CPU caps on this zone. 3018c97ad5cdSakolb */ 3019c97ad5cdSakolb cpucaps_zone_remove(zone); 30207c478bd9Sstevel@tonic-gate } 30217c478bd9Sstevel@tonic-gate } else { 30227c478bd9Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 30237c478bd9Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 30247c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == t) 30257c478bd9Sstevel@tonic-gate zone->zone_kthreads = t->t_forw; 30267c478bd9Sstevel@tonic-gate } 30277c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 30287c478bd9Sstevel@tonic-gate zone_rele(zone); 30297c478bd9Sstevel@tonic-gate thread_exit(); 30307c478bd9Sstevel@tonic-gate /* NOTREACHED */ 30317c478bd9Sstevel@tonic-gate } 30327c478bd9Sstevel@tonic-gate 30337c478bd9Sstevel@tonic-gate static void 30347c478bd9Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 30357c478bd9Sstevel@tonic-gate { 30367c478bd9Sstevel@tonic-gate vnode_t *oldvp; 30377c478bd9Sstevel@tonic-gate 30387c478bd9Sstevel@tonic-gate /* we're going to hold a reference here to the directory */ 30397c478bd9Sstevel@tonic-gate VN_HOLD(vp); 30407c478bd9Sstevel@tonic-gate 30417c478bd9Sstevel@tonic-gate if (audit_active) /* update abs cwd/root path see c2audit.c */ 30427c478bd9Sstevel@tonic-gate audit_chdirec(vp, vpp); 30437c478bd9Sstevel@tonic-gate 30447c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 30457c478bd9Sstevel@tonic-gate oldvp = *vpp; 30467c478bd9Sstevel@tonic-gate *vpp = vp; 30477c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 30487c478bd9Sstevel@tonic-gate if (oldvp != NULL) 30497c478bd9Sstevel@tonic-gate VN_RELE(oldvp); 30507c478bd9Sstevel@tonic-gate } 30517c478bd9Sstevel@tonic-gate 30527c478bd9Sstevel@tonic-gate /* 30537c478bd9Sstevel@tonic-gate * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 30547c478bd9Sstevel@tonic-gate */ 30557c478bd9Sstevel@tonic-gate static int 30567c478bd9Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 30577c478bd9Sstevel@tonic-gate { 30587c478bd9Sstevel@tonic-gate nvpair_t *nvp = NULL; 30597c478bd9Sstevel@tonic-gate boolean_t priv_set = B_FALSE; 30607c478bd9Sstevel@tonic-gate boolean_t limit_set = B_FALSE; 30617c478bd9Sstevel@tonic-gate boolean_t action_set = B_FALSE; 30627c478bd9Sstevel@tonic-gate 30637c478bd9Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 30647c478bd9Sstevel@tonic-gate const char *name; 30657c478bd9Sstevel@tonic-gate uint64_t ui64; 30667c478bd9Sstevel@tonic-gate 30677c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 30687c478bd9Sstevel@tonic-gate if (nvpair_type(nvp) != DATA_TYPE_UINT64) 30697c478bd9Sstevel@tonic-gate return (EINVAL); 30707c478bd9Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &ui64); 30717c478bd9Sstevel@tonic-gate if (strcmp(name, "privilege") == 0) { 30727c478bd9Sstevel@tonic-gate /* 30737c478bd9Sstevel@tonic-gate * Currently only privileged values are allowed, but 30747c478bd9Sstevel@tonic-gate * this may change in the future. 30757c478bd9Sstevel@tonic-gate */ 30767c478bd9Sstevel@tonic-gate if (ui64 != RCPRIV_PRIVILEGED) 30777c478bd9Sstevel@tonic-gate return (EINVAL); 30787c478bd9Sstevel@tonic-gate rv->rcv_privilege = ui64; 30797c478bd9Sstevel@tonic-gate priv_set = B_TRUE; 30807c478bd9Sstevel@tonic-gate } else if (strcmp(name, "limit") == 0) { 30817c478bd9Sstevel@tonic-gate rv->rcv_value = ui64; 30827c478bd9Sstevel@tonic-gate limit_set = B_TRUE; 30837c478bd9Sstevel@tonic-gate } else if (strcmp(name, "action") == 0) { 30847c478bd9Sstevel@tonic-gate if (ui64 != RCTL_LOCAL_NOACTION && 30857c478bd9Sstevel@tonic-gate ui64 != RCTL_LOCAL_DENY) 30867c478bd9Sstevel@tonic-gate return (EINVAL); 30877c478bd9Sstevel@tonic-gate rv->rcv_flagaction = ui64; 30887c478bd9Sstevel@tonic-gate action_set = B_TRUE; 30897c478bd9Sstevel@tonic-gate } else { 30907c478bd9Sstevel@tonic-gate return (EINVAL); 30917c478bd9Sstevel@tonic-gate } 30927c478bd9Sstevel@tonic-gate } 30937c478bd9Sstevel@tonic-gate 30947c478bd9Sstevel@tonic-gate if (!(priv_set && limit_set && action_set)) 30957c478bd9Sstevel@tonic-gate return (EINVAL); 30967c478bd9Sstevel@tonic-gate rv->rcv_action_signal = 0; 30977c478bd9Sstevel@tonic-gate rv->rcv_action_recipient = NULL; 30987c478bd9Sstevel@tonic-gate rv->rcv_action_recip_pid = -1; 30997c478bd9Sstevel@tonic-gate rv->rcv_firing_time = 0; 31007c478bd9Sstevel@tonic-gate 31017c478bd9Sstevel@tonic-gate return (0); 31027c478bd9Sstevel@tonic-gate } 31037c478bd9Sstevel@tonic-gate 31043f2f09c1Sdp /* 31053f2f09c1Sdp * Non-global zone version of start_init. 31063f2f09c1Sdp */ 31077c478bd9Sstevel@tonic-gate void 31083f2f09c1Sdp zone_start_init(void) 31097c478bd9Sstevel@tonic-gate { 31107c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 31119acbbeafSnn zone_t *z = p->p_zone; 31127c478bd9Sstevel@tonic-gate 31133f2f09c1Sdp ASSERT(!INGLOBALZONE(curproc)); 31147c478bd9Sstevel@tonic-gate 31159acbbeafSnn /* 31169acbbeafSnn * For all purposes (ZONE_ATTR_INITPID and restart_init), 31179acbbeafSnn * storing just the pid of init is sufficient. 31189acbbeafSnn */ 31199acbbeafSnn z->zone_proc_initpid = p->p_pid; 31209acbbeafSnn 31217c478bd9Sstevel@tonic-gate /* 31223f2f09c1Sdp * We maintain zone_boot_err so that we can return the cause of the 31233f2f09c1Sdp * failure back to the caller of the zone_boot syscall. 31247c478bd9Sstevel@tonic-gate */ 31253f2f09c1Sdp p->p_zone->zone_boot_err = start_init_common(); 31267c478bd9Sstevel@tonic-gate 31277c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 31289acbbeafSnn if (z->zone_boot_err != 0) { 31297c478bd9Sstevel@tonic-gate /* 31307c478bd9Sstevel@tonic-gate * Make sure we are still in the booting state-- we could have 31317c478bd9Sstevel@tonic-gate * raced and already be shutting down, or even further along. 31327c478bd9Sstevel@tonic-gate */ 3133c97ad5cdSakolb if (zone_status_get(z) == ZONE_IS_BOOTING) { 31349acbbeafSnn zone_status_set(z, ZONE_IS_SHUTTING_DOWN); 3135c97ad5cdSakolb } 31367c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31377c478bd9Sstevel@tonic-gate /* It's gone bad, dispose of the process */ 31389acbbeafSnn if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) { 313997eda132Sraf mutex_enter(&p->p_lock); 314097eda132Sraf ASSERT(p->p_flag & SEXITLWPS); 31417c478bd9Sstevel@tonic-gate lwp_exit(); 31427c478bd9Sstevel@tonic-gate } 31437c478bd9Sstevel@tonic-gate } else { 31449acbbeafSnn if (zone_status_get(z) == ZONE_IS_BOOTING) 31459acbbeafSnn zone_status_set(z, ZONE_IS_RUNNING); 31467c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31477c478bd9Sstevel@tonic-gate /* cause the process to return to userland. */ 31487c478bd9Sstevel@tonic-gate lwp_rtt(); 31497c478bd9Sstevel@tonic-gate } 31507c478bd9Sstevel@tonic-gate } 31517c478bd9Sstevel@tonic-gate 31527c478bd9Sstevel@tonic-gate struct zsched_arg { 31537c478bd9Sstevel@tonic-gate zone_t *zone; 31547c478bd9Sstevel@tonic-gate nvlist_t *nvlist; 31557c478bd9Sstevel@tonic-gate }; 31567c478bd9Sstevel@tonic-gate 31577c478bd9Sstevel@tonic-gate /* 31587c478bd9Sstevel@tonic-gate * Per-zone "sched" workalike. The similarity to "sched" doesn't have 31597c478bd9Sstevel@tonic-gate * anything to do with scheduling, but rather with the fact that 31607c478bd9Sstevel@tonic-gate * per-zone kernel threads are parented to zsched, just like regular 31617c478bd9Sstevel@tonic-gate * kernel threads are parented to sched (p0). 31627c478bd9Sstevel@tonic-gate * 31637c478bd9Sstevel@tonic-gate * zsched is also responsible for launching init for the zone. 31647c478bd9Sstevel@tonic-gate */ 31657c478bd9Sstevel@tonic-gate static void 31667c478bd9Sstevel@tonic-gate zsched(void *arg) 31677c478bd9Sstevel@tonic-gate { 31687c478bd9Sstevel@tonic-gate struct zsched_arg *za = arg; 31697c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 31707c478bd9Sstevel@tonic-gate proc_t *initp = proc_init; 31717c478bd9Sstevel@tonic-gate zone_t *zone = za->zone; 31727c478bd9Sstevel@tonic-gate cred_t *cr, *oldcred; 31737c478bd9Sstevel@tonic-gate rctl_set_t *set; 31747c478bd9Sstevel@tonic-gate rctl_alloc_gp_t *gp; 31757c478bd9Sstevel@tonic-gate contract_t *ct = NULL; 31767c478bd9Sstevel@tonic-gate task_t *tk, *oldtk; 31777c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 31787c478bd9Sstevel@tonic-gate kproject_t *pj; 31797c478bd9Sstevel@tonic-gate 31807c478bd9Sstevel@tonic-gate nvlist_t *nvl = za->nvlist; 31817c478bd9Sstevel@tonic-gate nvpair_t *nvp = NULL; 31827c478bd9Sstevel@tonic-gate 3183ae115bc7Smrj bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched")); 3184ae115bc7Smrj bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched")); 3185ae115bc7Smrj PTOU(pp)->u_argc = 0; 3186ae115bc7Smrj PTOU(pp)->u_argv = NULL; 3187ae115bc7Smrj PTOU(pp)->u_envp = NULL; 31887c478bd9Sstevel@tonic-gate closeall(P_FINFO(pp)); 31897c478bd9Sstevel@tonic-gate 31907c478bd9Sstevel@tonic-gate /* 31917c478bd9Sstevel@tonic-gate * We are this zone's "zsched" process. As the zone isn't generally 31927c478bd9Sstevel@tonic-gate * visible yet we don't need to grab any locks before initializing its 31937c478bd9Sstevel@tonic-gate * zone_proc pointer. 31947c478bd9Sstevel@tonic-gate */ 31957c478bd9Sstevel@tonic-gate zone_hold(zone); /* this hold is released by zone_destroy() */ 31967c478bd9Sstevel@tonic-gate zone->zone_zsched = pp; 31977c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 31987c478bd9Sstevel@tonic-gate pp->p_zone = zone; 31997c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 32007c478bd9Sstevel@tonic-gate 32017c478bd9Sstevel@tonic-gate /* 32027c478bd9Sstevel@tonic-gate * Disassociate process from its 'parent'; parent ourselves to init 32037c478bd9Sstevel@tonic-gate * (pid 1) and change other values as needed. 32047c478bd9Sstevel@tonic-gate */ 32057c478bd9Sstevel@tonic-gate sess_create(); 32067c478bd9Sstevel@tonic-gate 32077c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 32087c478bd9Sstevel@tonic-gate proc_detach(pp); 32097c478bd9Sstevel@tonic-gate pp->p_ppid = 1; 32107c478bd9Sstevel@tonic-gate pp->p_flag |= SZONETOP; 32117c478bd9Sstevel@tonic-gate pp->p_ancpid = 1; 32127c478bd9Sstevel@tonic-gate pp->p_parent = initp; 32137c478bd9Sstevel@tonic-gate pp->p_psibling = NULL; 32147c478bd9Sstevel@tonic-gate if (initp->p_child) 32157c478bd9Sstevel@tonic-gate initp->p_child->p_psibling = pp; 32167c478bd9Sstevel@tonic-gate pp->p_sibling = initp->p_child; 32177c478bd9Sstevel@tonic-gate initp->p_child = pp; 32187c478bd9Sstevel@tonic-gate 32197c478bd9Sstevel@tonic-gate /* Decrement what newproc() incremented. */ 32207c478bd9Sstevel@tonic-gate upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 32217c478bd9Sstevel@tonic-gate /* 32227c478bd9Sstevel@tonic-gate * Our credentials are about to become kcred-like, so we don't care 32237c478bd9Sstevel@tonic-gate * about the caller's ruid. 32247c478bd9Sstevel@tonic-gate */ 32257c478bd9Sstevel@tonic-gate upcount_inc(crgetruid(kcred), zone->zone_id); 32267c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 32277c478bd9Sstevel@tonic-gate 32287c478bd9Sstevel@tonic-gate /* 32297c478bd9Sstevel@tonic-gate * getting out of global zone, so decrement lwp counts 32307c478bd9Sstevel@tonic-gate */ 32317c478bd9Sstevel@tonic-gate pj = pp->p_task->tk_proj; 32327c478bd9Sstevel@tonic-gate mutex_enter(&global_zone->zone_nlwps_lock); 32337c478bd9Sstevel@tonic-gate pj->kpj_nlwps -= pp->p_lwpcnt; 32347c478bd9Sstevel@tonic-gate global_zone->zone_nlwps -= pp->p_lwpcnt; 32357c478bd9Sstevel@tonic-gate mutex_exit(&global_zone->zone_nlwps_lock); 32367c478bd9Sstevel@tonic-gate 3237c6939658Ssl /* 3238c6939658Ssl * Decrement locked memory counts on old zone and project. 3239c6939658Ssl */ 32400209230bSgjelinek mutex_enter(&global_zone->zone_mem_lock); 3241c6939658Ssl global_zone->zone_locked_mem -= pp->p_locked_mem; 3242c6939658Ssl pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 32430209230bSgjelinek mutex_exit(&global_zone->zone_mem_lock); 3244c6939658Ssl 32457c478bd9Sstevel@tonic-gate /* 32467c478bd9Sstevel@tonic-gate * Create and join a new task in project '0' of this zone. 32477c478bd9Sstevel@tonic-gate * 32487c478bd9Sstevel@tonic-gate * We don't need to call holdlwps() since we know we're the only lwp in 32497c478bd9Sstevel@tonic-gate * this process. 32507c478bd9Sstevel@tonic-gate * 32517c478bd9Sstevel@tonic-gate * task_join() returns with p_lock held. 32527c478bd9Sstevel@tonic-gate */ 32537c478bd9Sstevel@tonic-gate tk = task_create(0, zone); 32547c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 32557c478bd9Sstevel@tonic-gate oldtk = task_join(tk, 0); 3256c6939658Ssl 3257c6939658Ssl pj = pp->p_task->tk_proj; 3258c6939658Ssl 32590209230bSgjelinek mutex_enter(&zone->zone_mem_lock); 3260c6939658Ssl zone->zone_locked_mem += pp->p_locked_mem; 3261c6939658Ssl pj->kpj_data.kpd_locked_mem += pp->p_locked_mem; 32620209230bSgjelinek mutex_exit(&zone->zone_mem_lock); 32637c478bd9Sstevel@tonic-gate 32647c478bd9Sstevel@tonic-gate /* 32657c478bd9Sstevel@tonic-gate * add lwp counts to zsched's zone, and increment project's task count 32667c478bd9Sstevel@tonic-gate * due to the task created in the above tasksys_settaskid 32677c478bd9Sstevel@tonic-gate */ 3268c6939658Ssl 32697c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 32707c478bd9Sstevel@tonic-gate pj->kpj_nlwps += pp->p_lwpcnt; 32717c478bd9Sstevel@tonic-gate pj->kpj_ntasks += 1; 32727c478bd9Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 32737c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 32747c478bd9Sstevel@tonic-gate 3275c6939658Ssl mutex_exit(&curproc->p_lock); 3276c6939658Ssl mutex_exit(&cpu_lock); 3277c6939658Ssl task_rele(oldtk); 3278c6939658Ssl 32797c478bd9Sstevel@tonic-gate /* 32807c478bd9Sstevel@tonic-gate * The process was created by a process in the global zone, hence the 32817c478bd9Sstevel@tonic-gate * credentials are wrong. We might as well have kcred-ish credentials. 32827c478bd9Sstevel@tonic-gate */ 32837c478bd9Sstevel@tonic-gate cr = zone->zone_kcred; 32847c478bd9Sstevel@tonic-gate crhold(cr); 32857c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 32867c478bd9Sstevel@tonic-gate oldcred = pp->p_cred; 32877c478bd9Sstevel@tonic-gate pp->p_cred = cr; 32887c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 32897c478bd9Sstevel@tonic-gate crfree(oldcred); 32907c478bd9Sstevel@tonic-gate 32917c478bd9Sstevel@tonic-gate /* 32927c478bd9Sstevel@tonic-gate * Hold credentials again (for thread) 32937c478bd9Sstevel@tonic-gate */ 32947c478bd9Sstevel@tonic-gate crhold(cr); 32957c478bd9Sstevel@tonic-gate 32967c478bd9Sstevel@tonic-gate /* 32977c478bd9Sstevel@tonic-gate * p_lwpcnt can't change since this is a kernel process. 32987c478bd9Sstevel@tonic-gate */ 32997c478bd9Sstevel@tonic-gate crset(pp, cr); 33007c478bd9Sstevel@tonic-gate 33017c478bd9Sstevel@tonic-gate /* 33027c478bd9Sstevel@tonic-gate * Chroot 33037c478bd9Sstevel@tonic-gate */ 33047c478bd9Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 33057c478bd9Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 33067c478bd9Sstevel@tonic-gate 33077c478bd9Sstevel@tonic-gate /* 33087c478bd9Sstevel@tonic-gate * Initialize zone's rctl set. 33097c478bd9Sstevel@tonic-gate */ 33107c478bd9Sstevel@tonic-gate set = rctl_set_create(); 33117c478bd9Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 33127c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 33137c478bd9Sstevel@tonic-gate e.rcep_p.zone = zone; 33147c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 33157c478bd9Sstevel@tonic-gate zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 33167c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 33177c478bd9Sstevel@tonic-gate rctl_prealloc_destroy(gp); 33187c478bd9Sstevel@tonic-gate 33197c478bd9Sstevel@tonic-gate /* 33207c478bd9Sstevel@tonic-gate * Apply the rctls passed in to zone_create(). This is basically a list 33217c478bd9Sstevel@tonic-gate * assignment: all of the old values are removed and the new ones 33227c478bd9Sstevel@tonic-gate * inserted. That is, if an empty list is passed in, all values are 33237c478bd9Sstevel@tonic-gate * removed. 33247c478bd9Sstevel@tonic-gate */ 33257c478bd9Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 33267c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rde; 33277c478bd9Sstevel@tonic-gate rctl_hndl_t hndl; 33287c478bd9Sstevel@tonic-gate char *name; 33297c478bd9Sstevel@tonic-gate nvlist_t **nvlarray; 33307c478bd9Sstevel@tonic-gate uint_t i, nelem; 33317c478bd9Sstevel@tonic-gate int error; /* For ASSERT()s */ 33327c478bd9Sstevel@tonic-gate 33337c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 33347c478bd9Sstevel@tonic-gate hndl = rctl_hndl_lookup(name); 33357c478bd9Sstevel@tonic-gate ASSERT(hndl != -1); 33367c478bd9Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 33377c478bd9Sstevel@tonic-gate ASSERT(rde != NULL); 33387c478bd9Sstevel@tonic-gate 33397c478bd9Sstevel@tonic-gate for (; /* ever */; ) { 33407c478bd9Sstevel@tonic-gate rctl_val_t oval; 33417c478bd9Sstevel@tonic-gate 33427c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 33437c478bd9Sstevel@tonic-gate error = rctl_local_get(hndl, NULL, &oval, pp); 33447c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 33457c478bd9Sstevel@tonic-gate ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 33467c478bd9Sstevel@tonic-gate ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 33477c478bd9Sstevel@tonic-gate if (oval.rcv_privilege == RCPRIV_SYSTEM) 33487c478bd9Sstevel@tonic-gate break; 33497c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 33507c478bd9Sstevel@tonic-gate error = rctl_local_delete(hndl, &oval, pp); 33517c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 33527c478bd9Sstevel@tonic-gate ASSERT(error == 0); 33537c478bd9Sstevel@tonic-gate } 33547c478bd9Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 33557c478bd9Sstevel@tonic-gate ASSERT(error == 0); 33567c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 33577c478bd9Sstevel@tonic-gate rctl_val_t *nvalp; 33587c478bd9Sstevel@tonic-gate 33597c478bd9Sstevel@tonic-gate nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 33607c478bd9Sstevel@tonic-gate error = nvlist2rctlval(nvlarray[i], nvalp); 33617c478bd9Sstevel@tonic-gate ASSERT(error == 0); 33627c478bd9Sstevel@tonic-gate /* 33637c478bd9Sstevel@tonic-gate * rctl_local_insert can fail if the value being 33647c478bd9Sstevel@tonic-gate * inserted is a duplicate; this is OK. 33657c478bd9Sstevel@tonic-gate */ 33667c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 33677c478bd9Sstevel@tonic-gate if (rctl_local_insert(hndl, nvalp, pp) != 0) 33687c478bd9Sstevel@tonic-gate kmem_cache_free(rctl_val_cache, nvalp); 33697c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 33707c478bd9Sstevel@tonic-gate } 33717c478bd9Sstevel@tonic-gate } 33727c478bd9Sstevel@tonic-gate /* 33737c478bd9Sstevel@tonic-gate * Tell the world that we're done setting up. 33747c478bd9Sstevel@tonic-gate * 3375*bd41d0a8Snordmark * At this point we want to set the zone status to ZONE_IS_INITIALIZED 33767c478bd9Sstevel@tonic-gate * and atomically set the zone's processor set visibility. Once 33777c478bd9Sstevel@tonic-gate * we drop pool_lock() this zone will automatically get updated 33787c478bd9Sstevel@tonic-gate * to reflect any future changes to the pools configuration. 3379*bd41d0a8Snordmark * 3380*bd41d0a8Snordmark * Note that after we drop the locks below (zonehash_lock in 3381*bd41d0a8Snordmark * particular) other operations such as a zone_getattr call can 3382*bd41d0a8Snordmark * now proceed and observe the zone. That is the reason for doing a 3383*bd41d0a8Snordmark * state transition to the INITIALIZED state. 33847c478bd9Sstevel@tonic-gate */ 33857c478bd9Sstevel@tonic-gate pool_lock(); 33867c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 33877c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 33887c478bd9Sstevel@tonic-gate zone_uniqid(zone); 33897c478bd9Sstevel@tonic-gate zone_zsd_configure(zone); 33907c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) 33917c478bd9Sstevel@tonic-gate zone_pset_set(zone, pool_default->pool_pset->pset_id); 33927c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 33937c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 3394*bd41d0a8Snordmark zone_status_set(zone, ZONE_IS_INITIALIZED); 33957c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 33967c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 33977c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 33987c478bd9Sstevel@tonic-gate pool_unlock(); 33997c478bd9Sstevel@tonic-gate 3400*bd41d0a8Snordmark /* Now call the create callback for this key */ 3401*bd41d0a8Snordmark zsd_apply_all_keys(zsd_apply_create, zone); 3402*bd41d0a8Snordmark 3403*bd41d0a8Snordmark /* The callbacks are complete. Mark ZONE_IS_READY */ 3404*bd41d0a8Snordmark mutex_enter(&zone_status_lock); 3405*bd41d0a8Snordmark ASSERT(zone_status_get(zone) == ZONE_IS_INITIALIZED); 3406*bd41d0a8Snordmark zone_status_set(zone, ZONE_IS_READY); 3407*bd41d0a8Snordmark mutex_exit(&zone_status_lock); 3408*bd41d0a8Snordmark 34097c478bd9Sstevel@tonic-gate /* 34107c478bd9Sstevel@tonic-gate * Once we see the zone transition to the ZONE_IS_BOOTING state, 34117c478bd9Sstevel@tonic-gate * we launch init, and set the state to running. 34127c478bd9Sstevel@tonic-gate */ 34137c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 34147c478bd9Sstevel@tonic-gate 34157c478bd9Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_BOOTING) { 34167c478bd9Sstevel@tonic-gate id_t cid; 34177c478bd9Sstevel@tonic-gate 34187c478bd9Sstevel@tonic-gate /* 34197c478bd9Sstevel@tonic-gate * Ok, this is a little complicated. We need to grab the 34207c478bd9Sstevel@tonic-gate * zone's pool's scheduling class ID; note that by now, we 34217c478bd9Sstevel@tonic-gate * are already bound to a pool if we need to be (zoneadmd 34227c478bd9Sstevel@tonic-gate * will have done that to us while we're in the READY 34237c478bd9Sstevel@tonic-gate * state). *But* the scheduling class for the zone's 'init' 34247c478bd9Sstevel@tonic-gate * must be explicitly passed to newproc, which doesn't 34257c478bd9Sstevel@tonic-gate * respect pool bindings. 34267c478bd9Sstevel@tonic-gate * 34277c478bd9Sstevel@tonic-gate * We hold the pool_lock across the call to newproc() to 34287c478bd9Sstevel@tonic-gate * close the obvious race: the pool's scheduling class 34297c478bd9Sstevel@tonic-gate * could change before we manage to create the LWP with 34307c478bd9Sstevel@tonic-gate * classid 'cid'. 34317c478bd9Sstevel@tonic-gate */ 34327c478bd9Sstevel@tonic-gate pool_lock(); 34330209230bSgjelinek if (zone->zone_defaultcid > 0) 34340209230bSgjelinek cid = zone->zone_defaultcid; 34350209230bSgjelinek else 34360209230bSgjelinek cid = pool_get_class(zone->zone_pool); 34377c478bd9Sstevel@tonic-gate if (cid == -1) 34387c478bd9Sstevel@tonic-gate cid = defaultcid; 34397c478bd9Sstevel@tonic-gate 34407c478bd9Sstevel@tonic-gate /* 34417c478bd9Sstevel@tonic-gate * If this fails, zone_boot will ultimately fail. The 34427c478bd9Sstevel@tonic-gate * state of the zone will be set to SHUTTING_DOWN-- userland 34437c478bd9Sstevel@tonic-gate * will have to tear down the zone, and fail, or try again. 34447c478bd9Sstevel@tonic-gate */ 34453f2f09c1Sdp if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid, 34467c478bd9Sstevel@tonic-gate minclsyspri - 1, &ct)) != 0) { 34477c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 34487c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 34497c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 34507c478bd9Sstevel@tonic-gate } 34517c478bd9Sstevel@tonic-gate pool_unlock(); 34527c478bd9Sstevel@tonic-gate } 34537c478bd9Sstevel@tonic-gate 34547c478bd9Sstevel@tonic-gate /* 34557c478bd9Sstevel@tonic-gate * Wait for zone_destroy() to be called. This is what we spend 34567c478bd9Sstevel@tonic-gate * most of our life doing. 34577c478bd9Sstevel@tonic-gate */ 34587c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 34597c478bd9Sstevel@tonic-gate 34607c478bd9Sstevel@tonic-gate if (ct) 34617c478bd9Sstevel@tonic-gate /* 34627c478bd9Sstevel@tonic-gate * At this point the process contract should be empty. 34637c478bd9Sstevel@tonic-gate * (Though if it isn't, it's not the end of the world.) 34647c478bd9Sstevel@tonic-gate */ 34657c478bd9Sstevel@tonic-gate VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 34667c478bd9Sstevel@tonic-gate 34677c478bd9Sstevel@tonic-gate /* 34687c478bd9Sstevel@tonic-gate * Allow kcred to be freed when all referring processes 34697c478bd9Sstevel@tonic-gate * (including this one) go away. We can't just do this in 34707c478bd9Sstevel@tonic-gate * zone_free because we need to wait for the zone_cred_ref to 34717c478bd9Sstevel@tonic-gate * drop to 0 before calling zone_free, and the existence of 34727c478bd9Sstevel@tonic-gate * zone_kcred will prevent that. Thus, we call crfree here to 34737c478bd9Sstevel@tonic-gate * balance the crdup in zone_create. The crhold calls earlier 34747c478bd9Sstevel@tonic-gate * in zsched will be dropped when the thread and process exit. 34757c478bd9Sstevel@tonic-gate */ 34767c478bd9Sstevel@tonic-gate crfree(zone->zone_kcred); 34777c478bd9Sstevel@tonic-gate zone->zone_kcred = NULL; 34787c478bd9Sstevel@tonic-gate 34797c478bd9Sstevel@tonic-gate exit(CLD_EXITED, 0); 34807c478bd9Sstevel@tonic-gate } 34817c478bd9Sstevel@tonic-gate 34827c478bd9Sstevel@tonic-gate /* 34837c478bd9Sstevel@tonic-gate * Helper function to determine if there are any submounts of the 34847c478bd9Sstevel@tonic-gate * provided path. Used to make sure the zone doesn't "inherit" any 34857c478bd9Sstevel@tonic-gate * mounts from before it is created. 34867c478bd9Sstevel@tonic-gate */ 34877c478bd9Sstevel@tonic-gate static uint_t 34887c478bd9Sstevel@tonic-gate zone_mount_count(const char *rootpath) 34897c478bd9Sstevel@tonic-gate { 34907c478bd9Sstevel@tonic-gate vfs_t *vfsp; 34917c478bd9Sstevel@tonic-gate uint_t count = 0; 34927c478bd9Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 34937c478bd9Sstevel@tonic-gate 34947c478bd9Sstevel@tonic-gate /* 34957c478bd9Sstevel@tonic-gate * Holding zonehash_lock prevents race conditions with 34967c478bd9Sstevel@tonic-gate * vfs_list_add()/vfs_list_remove() since we serialize with 34977c478bd9Sstevel@tonic-gate * zone_find_by_path(). 34987c478bd9Sstevel@tonic-gate */ 34997c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 35007c478bd9Sstevel@tonic-gate /* 35017c478bd9Sstevel@tonic-gate * The rootpath must end with a '/' 35027c478bd9Sstevel@tonic-gate */ 35037c478bd9Sstevel@tonic-gate ASSERT(rootpath[rootpathlen - 1] == '/'); 35047c478bd9Sstevel@tonic-gate 35057c478bd9Sstevel@tonic-gate /* 35067c478bd9Sstevel@tonic-gate * This intentionally does not count the rootpath itself if that 35077c478bd9Sstevel@tonic-gate * happens to be a mount point. 35087c478bd9Sstevel@tonic-gate */ 35097c478bd9Sstevel@tonic-gate vfs_list_read_lock(); 35107c478bd9Sstevel@tonic-gate vfsp = rootvfs; 35117c478bd9Sstevel@tonic-gate do { 35127c478bd9Sstevel@tonic-gate if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 35137c478bd9Sstevel@tonic-gate rootpathlen) == 0) 35147c478bd9Sstevel@tonic-gate count++; 35157c478bd9Sstevel@tonic-gate vfsp = vfsp->vfs_next; 35167c478bd9Sstevel@tonic-gate } while (vfsp != rootvfs); 35177c478bd9Sstevel@tonic-gate vfs_list_unlock(); 35187c478bd9Sstevel@tonic-gate return (count); 35197c478bd9Sstevel@tonic-gate } 35207c478bd9Sstevel@tonic-gate 35217c478bd9Sstevel@tonic-gate /* 35227c478bd9Sstevel@tonic-gate * Helper function to make sure that a zone created on 'rootpath' 35237c478bd9Sstevel@tonic-gate * wouldn't end up containing other zones' rootpaths. 35247c478bd9Sstevel@tonic-gate */ 35257c478bd9Sstevel@tonic-gate static boolean_t 35267c478bd9Sstevel@tonic-gate zone_is_nested(const char *rootpath) 35277c478bd9Sstevel@tonic-gate { 35287c478bd9Sstevel@tonic-gate zone_t *zone; 35297c478bd9Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 35307c478bd9Sstevel@tonic-gate size_t len; 35317c478bd9Sstevel@tonic-gate 35327c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 35337c478bd9Sstevel@tonic-gate 35347c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 35357c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 35367c478bd9Sstevel@tonic-gate if (zone == global_zone) 35377c478bd9Sstevel@tonic-gate continue; 35387c478bd9Sstevel@tonic-gate len = strlen(zone->zone_rootpath); 35397c478bd9Sstevel@tonic-gate if (strncmp(rootpath, zone->zone_rootpath, 35407c478bd9Sstevel@tonic-gate MIN(rootpathlen, len)) == 0) 35417c478bd9Sstevel@tonic-gate return (B_TRUE); 35427c478bd9Sstevel@tonic-gate } 35437c478bd9Sstevel@tonic-gate return (B_FALSE); 35447c478bd9Sstevel@tonic-gate } 35457c478bd9Sstevel@tonic-gate 35467c478bd9Sstevel@tonic-gate static int 3547821c4a97Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs, 3548821c4a97Sdp size_t zone_privssz) 35497c478bd9Sstevel@tonic-gate { 35507c478bd9Sstevel@tonic-gate priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 35517c478bd9Sstevel@tonic-gate 3552821c4a97Sdp if (zone_privssz < sizeof (priv_set_t)) 3553821c4a97Sdp return (set_errno(ENOMEM)); 3554821c4a97Sdp 35557c478bd9Sstevel@tonic-gate if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 35567c478bd9Sstevel@tonic-gate kmem_free(privs, sizeof (priv_set_t)); 35577c478bd9Sstevel@tonic-gate return (EFAULT); 35587c478bd9Sstevel@tonic-gate } 35597c478bd9Sstevel@tonic-gate 35607c478bd9Sstevel@tonic-gate zone->zone_privset = privs; 35617c478bd9Sstevel@tonic-gate return (0); 35627c478bd9Sstevel@tonic-gate } 35637c478bd9Sstevel@tonic-gate 35647c478bd9Sstevel@tonic-gate /* 35657c478bd9Sstevel@tonic-gate * We make creative use of nvlists to pass in rctls from userland. The list is 35667c478bd9Sstevel@tonic-gate * a list of the following structures: 35677c478bd9Sstevel@tonic-gate * 35687c478bd9Sstevel@tonic-gate * (name = rctl_name, value = nvpair_list_array) 35697c478bd9Sstevel@tonic-gate * 35707c478bd9Sstevel@tonic-gate * Where each element of the nvpair_list_array is of the form: 35717c478bd9Sstevel@tonic-gate * 35727c478bd9Sstevel@tonic-gate * [(name = "privilege", value = RCPRIV_PRIVILEGED), 35737c478bd9Sstevel@tonic-gate * (name = "limit", value = uint64_t), 35747c478bd9Sstevel@tonic-gate * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 35757c478bd9Sstevel@tonic-gate */ 35767c478bd9Sstevel@tonic-gate static int 35777c478bd9Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 35787c478bd9Sstevel@tonic-gate { 35797c478bd9Sstevel@tonic-gate nvpair_t *nvp = NULL; 35807c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL; 35817c478bd9Sstevel@tonic-gate char *kbuf; 35827c478bd9Sstevel@tonic-gate int error; 35837c478bd9Sstevel@tonic-gate rctl_val_t rv; 35847c478bd9Sstevel@tonic-gate 35857c478bd9Sstevel@tonic-gate *nvlp = NULL; 35867c478bd9Sstevel@tonic-gate 35877c478bd9Sstevel@tonic-gate if (buflen == 0) 35887c478bd9Sstevel@tonic-gate return (0); 35897c478bd9Sstevel@tonic-gate 35907c478bd9Sstevel@tonic-gate if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 35917c478bd9Sstevel@tonic-gate return (ENOMEM); 35927c478bd9Sstevel@tonic-gate if (copyin(ubuf, kbuf, buflen)) { 35937c478bd9Sstevel@tonic-gate error = EFAULT; 35947c478bd9Sstevel@tonic-gate goto out; 35957c478bd9Sstevel@tonic-gate } 35967c478bd9Sstevel@tonic-gate if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 35977c478bd9Sstevel@tonic-gate /* 35987c478bd9Sstevel@tonic-gate * nvl may have been allocated/free'd, but the value set to 35997c478bd9Sstevel@tonic-gate * non-NULL, so we reset it here. 36007c478bd9Sstevel@tonic-gate */ 36017c478bd9Sstevel@tonic-gate nvl = NULL; 36027c478bd9Sstevel@tonic-gate error = EINVAL; 36037c478bd9Sstevel@tonic-gate goto out; 36047c478bd9Sstevel@tonic-gate } 36057c478bd9Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 36067c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rde; 36077c478bd9Sstevel@tonic-gate rctl_hndl_t hndl; 36087c478bd9Sstevel@tonic-gate nvlist_t **nvlarray; 36097c478bd9Sstevel@tonic-gate uint_t i, nelem; 36107c478bd9Sstevel@tonic-gate char *name; 36117c478bd9Sstevel@tonic-gate 36127c478bd9Sstevel@tonic-gate error = EINVAL; 36137c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 36147c478bd9Sstevel@tonic-gate if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 36157c478bd9Sstevel@tonic-gate != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 36167c478bd9Sstevel@tonic-gate goto out; 36177c478bd9Sstevel@tonic-gate } 36187c478bd9Sstevel@tonic-gate if ((hndl = rctl_hndl_lookup(name)) == -1) { 36197c478bd9Sstevel@tonic-gate goto out; 36207c478bd9Sstevel@tonic-gate } 36217c478bd9Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 36227c478bd9Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 36237c478bd9Sstevel@tonic-gate ASSERT(error == 0); 36247c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 36257c478bd9Sstevel@tonic-gate if (error = nvlist2rctlval(nvlarray[i], &rv)) 36267c478bd9Sstevel@tonic-gate goto out; 36277c478bd9Sstevel@tonic-gate } 36287c478bd9Sstevel@tonic-gate if (rctl_invalid_value(rde, &rv)) { 36297c478bd9Sstevel@tonic-gate error = EINVAL; 36307c478bd9Sstevel@tonic-gate goto out; 36317c478bd9Sstevel@tonic-gate } 36327c478bd9Sstevel@tonic-gate } 36337c478bd9Sstevel@tonic-gate error = 0; 36347c478bd9Sstevel@tonic-gate *nvlp = nvl; 36357c478bd9Sstevel@tonic-gate out: 36367c478bd9Sstevel@tonic-gate kmem_free(kbuf, buflen); 36377c478bd9Sstevel@tonic-gate if (error && nvl != NULL) 36387c478bd9Sstevel@tonic-gate nvlist_free(nvl); 36397c478bd9Sstevel@tonic-gate return (error); 36407c478bd9Sstevel@tonic-gate } 36417c478bd9Sstevel@tonic-gate 36427c478bd9Sstevel@tonic-gate int 36437c478bd9Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) { 36447c478bd9Sstevel@tonic-gate if (er_out != NULL) { 36457c478bd9Sstevel@tonic-gate if (copyout(&er_ext, er_out, sizeof (int))) { 36467c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 36477c478bd9Sstevel@tonic-gate } 36487c478bd9Sstevel@tonic-gate } 36497c478bd9Sstevel@tonic-gate return (set_errno(er_error)); 36507c478bd9Sstevel@tonic-gate } 36517c478bd9Sstevel@tonic-gate 365245916cd2Sjpk static int 365345916cd2Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi) 365445916cd2Sjpk { 365545916cd2Sjpk ts_label_t *tsl; 365645916cd2Sjpk bslabel_t blab; 365745916cd2Sjpk 365845916cd2Sjpk /* Get label from user */ 365945916cd2Sjpk if (copyin(lab, &blab, sizeof (blab)) != 0) 366045916cd2Sjpk return (EFAULT); 366145916cd2Sjpk tsl = labelalloc(&blab, doi, KM_NOSLEEP); 366245916cd2Sjpk if (tsl == NULL) 366345916cd2Sjpk return (ENOMEM); 366445916cd2Sjpk 366545916cd2Sjpk zone->zone_slabel = tsl; 366645916cd2Sjpk return (0); 366745916cd2Sjpk } 366845916cd2Sjpk 3669fa9e4066Sahrens /* 3670fa9e4066Sahrens * Parses a comma-separated list of ZFS datasets into a per-zone dictionary. 3671fa9e4066Sahrens */ 3672fa9e4066Sahrens static int 3673fa9e4066Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen) 3674fa9e4066Sahrens { 3675fa9e4066Sahrens char *kbuf; 3676fa9e4066Sahrens char *dataset, *next; 3677fa9e4066Sahrens zone_dataset_t *zd; 3678fa9e4066Sahrens size_t len; 3679fa9e4066Sahrens 3680fa9e4066Sahrens if (ubuf == NULL || buflen == 0) 3681fa9e4066Sahrens return (0); 3682fa9e4066Sahrens 3683fa9e4066Sahrens if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 3684fa9e4066Sahrens return (ENOMEM); 3685fa9e4066Sahrens 3686fa9e4066Sahrens if (copyin(ubuf, kbuf, buflen) != 0) { 3687fa9e4066Sahrens kmem_free(kbuf, buflen); 3688fa9e4066Sahrens return (EFAULT); 3689fa9e4066Sahrens } 3690fa9e4066Sahrens 3691fa9e4066Sahrens dataset = next = kbuf; 3692fa9e4066Sahrens for (;;) { 3693fa9e4066Sahrens zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP); 3694fa9e4066Sahrens 3695fa9e4066Sahrens next = strchr(dataset, ','); 3696fa9e4066Sahrens 3697fa9e4066Sahrens if (next == NULL) 3698fa9e4066Sahrens len = strlen(dataset); 3699fa9e4066Sahrens else 3700fa9e4066Sahrens len = next - dataset; 3701fa9e4066Sahrens 3702fa9e4066Sahrens zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP); 3703fa9e4066Sahrens bcopy(dataset, zd->zd_dataset, len); 3704fa9e4066Sahrens zd->zd_dataset[len] = '\0'; 3705fa9e4066Sahrens 3706fa9e4066Sahrens list_insert_head(&zone->zone_datasets, zd); 3707fa9e4066Sahrens 3708fa9e4066Sahrens if (next == NULL) 3709fa9e4066Sahrens break; 3710fa9e4066Sahrens 3711fa9e4066Sahrens dataset = next + 1; 3712fa9e4066Sahrens } 3713fa9e4066Sahrens 3714fa9e4066Sahrens kmem_free(kbuf, buflen); 3715fa9e4066Sahrens return (0); 3716fa9e4066Sahrens } 3717fa9e4066Sahrens 37187c478bd9Sstevel@tonic-gate /* 37197c478bd9Sstevel@tonic-gate * System call to create/initialize a new zone named 'zone_name', rooted 37207c478bd9Sstevel@tonic-gate * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 372145916cd2Sjpk * and initialized with the zone-wide rctls described in 'rctlbuf', and 372245916cd2Sjpk * with labeling set by 'match', 'doi', and 'label'. 37237c478bd9Sstevel@tonic-gate * 37247c478bd9Sstevel@tonic-gate * If extended error is non-null, we may use it to return more detailed 37257c478bd9Sstevel@tonic-gate * error information. 37267c478bd9Sstevel@tonic-gate */ 37277c478bd9Sstevel@tonic-gate static zoneid_t 37287c478bd9Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root, 3729821c4a97Sdp const priv_set_t *zone_privs, size_t zone_privssz, 3730821c4a97Sdp caddr_t rctlbuf, size_t rctlbufsz, 373145916cd2Sjpk caddr_t zfsbuf, size_t zfsbufsz, int *extended_error, 3732f4b3ec61Sdh int match, uint32_t doi, const bslabel_t *label, 3733f4b3ec61Sdh int flags) 37347c478bd9Sstevel@tonic-gate { 37357c478bd9Sstevel@tonic-gate struct zsched_arg zarg; 37367c478bd9Sstevel@tonic-gate nvlist_t *rctls = NULL; 37377c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 37387c478bd9Sstevel@tonic-gate zone_t *zone, *ztmp; 37397c478bd9Sstevel@tonic-gate zoneid_t zoneid; 37407c478bd9Sstevel@tonic-gate int error; 37417c478bd9Sstevel@tonic-gate int error2 = 0; 37427c478bd9Sstevel@tonic-gate char *str; 37437c478bd9Sstevel@tonic-gate cred_t *zkcr; 374448451833Scarlsonj boolean_t insert_label_hash; 37457c478bd9Sstevel@tonic-gate 37467c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 37477c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 37487c478bd9Sstevel@tonic-gate 37497c478bd9Sstevel@tonic-gate /* can't boot zone from within chroot environment */ 37507c478bd9Sstevel@tonic-gate if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 37517c478bd9Sstevel@tonic-gate return (zone_create_error(ENOTSUP, ZE_CHROOTED, 3752821c4a97Sdp extended_error)); 37537c478bd9Sstevel@tonic-gate 37547c478bd9Sstevel@tonic-gate zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 37557c478bd9Sstevel@tonic-gate zoneid = zone->zone_id = id_alloc(zoneid_space); 37567c478bd9Sstevel@tonic-gate zone->zone_status = ZONE_IS_UNINITIALIZED; 37577c478bd9Sstevel@tonic-gate zone->zone_pool = pool_default; 37587c478bd9Sstevel@tonic-gate zone->zone_pool_mod = gethrtime(); 37597c478bd9Sstevel@tonic-gate zone->zone_psetid = ZONE_PS_INVAL; 37607c478bd9Sstevel@tonic-gate zone->zone_ncpus = 0; 37617c478bd9Sstevel@tonic-gate zone->zone_ncpus_online = 0; 37629acbbeafSnn zone->zone_restart_init = B_TRUE; 37639acbbeafSnn zone->zone_brand = &native_brand; 37649acbbeafSnn zone->zone_initname = NULL; 37657c478bd9Sstevel@tonic-gate mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 37667c478bd9Sstevel@tonic-gate mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 37670209230bSgjelinek mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 37687c478bd9Sstevel@tonic-gate cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 37697c478bd9Sstevel@tonic-gate list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 37707c478bd9Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 3771fa9e4066Sahrens list_create(&zone->zone_datasets, sizeof (zone_dataset_t), 3772fa9e4066Sahrens offsetof(zone_dataset_t, zd_linkage)); 377345916cd2Sjpk rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 37747c478bd9Sstevel@tonic-gate 3775f4b3ec61Sdh if (flags & ZCF_NET_EXCL) { 3776f4b3ec61Sdh zone->zone_flags |= ZF_NET_EXCL; 3777f4b3ec61Sdh } 3778f4b3ec61Sdh 37797c478bd9Sstevel@tonic-gate if ((error = zone_set_name(zone, zone_name)) != 0) { 37807c478bd9Sstevel@tonic-gate zone_free(zone); 37817c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 37827c478bd9Sstevel@tonic-gate } 37837c478bd9Sstevel@tonic-gate 37847c478bd9Sstevel@tonic-gate if ((error = zone_set_root(zone, zone_root)) != 0) { 37857c478bd9Sstevel@tonic-gate zone_free(zone); 37867c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 37877c478bd9Sstevel@tonic-gate } 3788821c4a97Sdp if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) { 37897c478bd9Sstevel@tonic-gate zone_free(zone); 37907c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 37917c478bd9Sstevel@tonic-gate } 37927c478bd9Sstevel@tonic-gate 37937c478bd9Sstevel@tonic-gate /* initialize node name to be the same as zone name */ 37947c478bd9Sstevel@tonic-gate zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 37957c478bd9Sstevel@tonic-gate (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 37967c478bd9Sstevel@tonic-gate zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 37977c478bd9Sstevel@tonic-gate 37987c478bd9Sstevel@tonic-gate zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 37997c478bd9Sstevel@tonic-gate zone->zone_domain[0] = '\0'; 38007c478bd9Sstevel@tonic-gate zone->zone_shares = 1; 3801824c205fSml zone->zone_shmmax = 0; 3802824c205fSml zone->zone_ipc.ipcq_shmmni = 0; 3803824c205fSml zone->zone_ipc.ipcq_semmni = 0; 3804824c205fSml zone->zone_ipc.ipcq_msgmni = 0; 38057c478bd9Sstevel@tonic-gate zone->zone_bootargs = NULL; 38063f2f09c1Sdp zone->zone_initname = 38073f2f09c1Sdp kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP); 38083f2f09c1Sdp (void) strcpy(zone->zone_initname, zone_default_initname); 38090209230bSgjelinek zone->zone_nlwps = 0; 38100209230bSgjelinek zone->zone_nlwps_ctl = INT_MAX; 3811c6939658Ssl zone->zone_locked_mem = 0; 3812c6939658Ssl zone->zone_locked_mem_ctl = UINT64_MAX; 38130209230bSgjelinek zone->zone_max_swap = 0; 38140209230bSgjelinek zone->zone_max_swap_ctl = UINT64_MAX; 38150209230bSgjelinek zone0.zone_lockedmem_kstat = NULL; 38160209230bSgjelinek zone0.zone_swapresv_kstat = NULL; 38177c478bd9Sstevel@tonic-gate 38187c478bd9Sstevel@tonic-gate /* 38197c478bd9Sstevel@tonic-gate * Zsched initializes the rctls. 38207c478bd9Sstevel@tonic-gate */ 38217c478bd9Sstevel@tonic-gate zone->zone_rctls = NULL; 38227c478bd9Sstevel@tonic-gate 38237c478bd9Sstevel@tonic-gate if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 38247c478bd9Sstevel@tonic-gate zone_free(zone); 38257c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 38267c478bd9Sstevel@tonic-gate } 38277c478bd9Sstevel@tonic-gate 3828fa9e4066Sahrens if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) { 3829fa9e4066Sahrens zone_free(zone); 3830fa9e4066Sahrens return (set_errno(error)); 3831fa9e4066Sahrens } 3832fa9e4066Sahrens 383345916cd2Sjpk /* 383445916cd2Sjpk * Read in the trusted system parameters: 383545916cd2Sjpk * match flag and sensitivity label. 383645916cd2Sjpk */ 383745916cd2Sjpk zone->zone_match = match; 383848451833Scarlsonj if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 38397e6639c2Skp /* Fail if requested to set doi to anything but system's doi */ 38407e6639c2Skp if (doi != 0 && doi != default_doi) { 38417e6639c2Skp zone_free(zone); 38427e6639c2Skp return (set_errno(EINVAL)); 38437e6639c2Skp } 38447e6639c2Skp /* Always apply system's doi to the zone */ 38457e6639c2Skp error = zone_set_label(zone, label, default_doi); 384645916cd2Sjpk if (error != 0) { 384745916cd2Sjpk zone_free(zone); 384845916cd2Sjpk return (set_errno(error)); 384945916cd2Sjpk } 385048451833Scarlsonj insert_label_hash = B_TRUE; 385145916cd2Sjpk } else { 385245916cd2Sjpk /* all zones get an admin_low label if system is not labeled */ 385345916cd2Sjpk zone->zone_slabel = l_admin_low; 385445916cd2Sjpk label_hold(l_admin_low); 385548451833Scarlsonj insert_label_hash = B_FALSE; 385645916cd2Sjpk } 385745916cd2Sjpk 38587c478bd9Sstevel@tonic-gate /* 38597c478bd9Sstevel@tonic-gate * Stop all lwps since that's what normally happens as part of fork(). 38607c478bd9Sstevel@tonic-gate * This needs to happen before we grab any locks to avoid deadlock 38617c478bd9Sstevel@tonic-gate * (another lwp in the process could be waiting for the held lock). 38627c478bd9Sstevel@tonic-gate */ 38637c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 38647c478bd9Sstevel@tonic-gate zone_free(zone); 38657c478bd9Sstevel@tonic-gate if (rctls) 38667c478bd9Sstevel@tonic-gate nvlist_free(rctls); 38677c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 38687c478bd9Sstevel@tonic-gate } 38697c478bd9Sstevel@tonic-gate 38707c478bd9Sstevel@tonic-gate if (block_mounts() == 0) { 38717c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 38727c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 38737c478bd9Sstevel@tonic-gate continuelwps(pp); 38747c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 38757c478bd9Sstevel@tonic-gate zone_free(zone); 38767c478bd9Sstevel@tonic-gate if (rctls) 38777c478bd9Sstevel@tonic-gate nvlist_free(rctls); 38787c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 38797c478bd9Sstevel@tonic-gate } 38807c478bd9Sstevel@tonic-gate 38817c478bd9Sstevel@tonic-gate /* 38827c478bd9Sstevel@tonic-gate * Set up credential for kernel access. After this, any errors 38837c478bd9Sstevel@tonic-gate * should go through the dance in errout rather than calling 38847c478bd9Sstevel@tonic-gate * zone_free directly. 38857c478bd9Sstevel@tonic-gate */ 38867c478bd9Sstevel@tonic-gate zone->zone_kcred = crdup(kcred); 38877c478bd9Sstevel@tonic-gate crsetzone(zone->zone_kcred, zone); 38887c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 38897c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 38907c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 38917c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 38927c478bd9Sstevel@tonic-gate 38937c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 38947c478bd9Sstevel@tonic-gate /* 38957c478bd9Sstevel@tonic-gate * Make sure zone doesn't already exist. 389645916cd2Sjpk * 389745916cd2Sjpk * If the system and zone are labeled, 389845916cd2Sjpk * make sure no other zone exists that has the same label. 38997c478bd9Sstevel@tonic-gate */ 390045916cd2Sjpk if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL || 390148451833Scarlsonj (insert_label_hash && 390245916cd2Sjpk (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) { 39037c478bd9Sstevel@tonic-gate zone_status_t status; 39047c478bd9Sstevel@tonic-gate 39057c478bd9Sstevel@tonic-gate status = zone_status_get(ztmp); 39067c478bd9Sstevel@tonic-gate if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 39077c478bd9Sstevel@tonic-gate error = EEXIST; 39087c478bd9Sstevel@tonic-gate else 39097c478bd9Sstevel@tonic-gate error = EBUSY; 39108c55461bSton 39118c55461bSton if (insert_label_hash) 39128c55461bSton error2 = ZE_LABELINUSE; 39138c55461bSton 39147c478bd9Sstevel@tonic-gate goto errout; 39157c478bd9Sstevel@tonic-gate } 39167c478bd9Sstevel@tonic-gate 39177c478bd9Sstevel@tonic-gate /* 39187c478bd9Sstevel@tonic-gate * Don't allow zone creations which would cause one zone's rootpath to 39197c478bd9Sstevel@tonic-gate * be accessible from that of another (non-global) zone. 39207c478bd9Sstevel@tonic-gate */ 39217c478bd9Sstevel@tonic-gate if (zone_is_nested(zone->zone_rootpath)) { 39227c478bd9Sstevel@tonic-gate error = EBUSY; 39237c478bd9Sstevel@tonic-gate goto errout; 39247c478bd9Sstevel@tonic-gate } 39257c478bd9Sstevel@tonic-gate 39267c478bd9Sstevel@tonic-gate ASSERT(zonecount != 0); /* check for leaks */ 39277c478bd9Sstevel@tonic-gate if (zonecount + 1 > maxzones) { 39287c478bd9Sstevel@tonic-gate error = ENOMEM; 39297c478bd9Sstevel@tonic-gate goto errout; 39307c478bd9Sstevel@tonic-gate } 39317c478bd9Sstevel@tonic-gate 39327c478bd9Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 39337c478bd9Sstevel@tonic-gate error = EBUSY; 39347c478bd9Sstevel@tonic-gate error2 = ZE_AREMOUNTS; 39357c478bd9Sstevel@tonic-gate goto errout; 39367c478bd9Sstevel@tonic-gate } 39377c478bd9Sstevel@tonic-gate 39387c478bd9Sstevel@tonic-gate /* 39397c478bd9Sstevel@tonic-gate * Zone is still incomplete, but we need to drop all locks while 39407c478bd9Sstevel@tonic-gate * zsched() initializes this zone's kernel process. We 39417c478bd9Sstevel@tonic-gate * optimistically add the zone to the hashtable and associated 39427c478bd9Sstevel@tonic-gate * lists so a parallel zone_create() doesn't try to create the 39437c478bd9Sstevel@tonic-gate * same zone. 39447c478bd9Sstevel@tonic-gate */ 39457c478bd9Sstevel@tonic-gate zonecount++; 39467c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, 39477c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id, 39487c478bd9Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 39497c478bd9Sstevel@tonic-gate str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 39507c478bd9Sstevel@tonic-gate (void) strcpy(str, zone->zone_name); 39517c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 39527c478bd9Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 395348451833Scarlsonj if (insert_label_hash) { 395445916cd2Sjpk (void) mod_hash_insert(zonehashbylabel, 395545916cd2Sjpk (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone); 395648451833Scarlsonj zone->zone_flags |= ZF_HASHED_LABEL; 395745916cd2Sjpk } 395845916cd2Sjpk 39597c478bd9Sstevel@tonic-gate /* 39607c478bd9Sstevel@tonic-gate * Insert into active list. At this point there are no 'hold's 39617c478bd9Sstevel@tonic-gate * on the zone, but everyone else knows not to use it, so we can 39627c478bd9Sstevel@tonic-gate * continue to use it. zsched() will do a zone_hold() if the 39637c478bd9Sstevel@tonic-gate * newproc() is successful. 39647c478bd9Sstevel@tonic-gate */ 39657c478bd9Sstevel@tonic-gate list_insert_tail(&zone_active, zone); 39667c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39677c478bd9Sstevel@tonic-gate 39687c478bd9Sstevel@tonic-gate zarg.zone = zone; 39697c478bd9Sstevel@tonic-gate zarg.nvlist = rctls; 39707c478bd9Sstevel@tonic-gate /* 39717c478bd9Sstevel@tonic-gate * The process, task, and project rctls are probably wrong; 39727c478bd9Sstevel@tonic-gate * we need an interface to get the default values of all rctls, 39737c478bd9Sstevel@tonic-gate * and initialize zsched appropriately. I'm not sure that that 39747c478bd9Sstevel@tonic-gate * makes much of a difference, though. 39757c478bd9Sstevel@tonic-gate */ 39767c478bd9Sstevel@tonic-gate if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) { 39777c478bd9Sstevel@tonic-gate /* 39787c478bd9Sstevel@tonic-gate * We need to undo all globally visible state. 39797c478bd9Sstevel@tonic-gate */ 39807c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 39817c478bd9Sstevel@tonic-gate list_remove(&zone_active, zone); 398248451833Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) { 398345916cd2Sjpk ASSERT(zone->zone_slabel != NULL); 398445916cd2Sjpk (void) mod_hash_destroy(zonehashbylabel, 398545916cd2Sjpk (mod_hash_key_t)zone->zone_slabel); 398645916cd2Sjpk } 39877c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 39887c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_name); 39897c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 39907c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 39917c478bd9Sstevel@tonic-gate ASSERT(zonecount > 1); 39927c478bd9Sstevel@tonic-gate zonecount--; 39937c478bd9Sstevel@tonic-gate goto errout; 39947c478bd9Sstevel@tonic-gate } 39957c478bd9Sstevel@tonic-gate 39967c478bd9Sstevel@tonic-gate /* 39977c478bd9Sstevel@tonic-gate * Zone creation can't fail from now on. 39987c478bd9Sstevel@tonic-gate */ 39997c478bd9Sstevel@tonic-gate 40000209230bSgjelinek /* 40010209230bSgjelinek * Create zone kstats 40020209230bSgjelinek */ 40030209230bSgjelinek zone_kstat_create(zone); 40040209230bSgjelinek 40057c478bd9Sstevel@tonic-gate /* 40067c478bd9Sstevel@tonic-gate * Let the other lwps continue. 40077c478bd9Sstevel@tonic-gate */ 40087c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 40097c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 40107c478bd9Sstevel@tonic-gate continuelwps(pp); 40117c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 40127c478bd9Sstevel@tonic-gate 40137c478bd9Sstevel@tonic-gate /* 40147c478bd9Sstevel@tonic-gate * Wait for zsched to finish initializing the zone. 40157c478bd9Sstevel@tonic-gate */ 40167c478bd9Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_READY); 40177c478bd9Sstevel@tonic-gate /* 40187c478bd9Sstevel@tonic-gate * The zone is fully visible, so we can let mounts progress. 40197c478bd9Sstevel@tonic-gate */ 40207c478bd9Sstevel@tonic-gate resume_mounts(); 40217c478bd9Sstevel@tonic-gate if (rctls) 40227c478bd9Sstevel@tonic-gate nvlist_free(rctls); 40237c478bd9Sstevel@tonic-gate 40247c478bd9Sstevel@tonic-gate return (zoneid); 40257c478bd9Sstevel@tonic-gate 40267c478bd9Sstevel@tonic-gate errout: 40277c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40287c478bd9Sstevel@tonic-gate /* 40297c478bd9Sstevel@tonic-gate * Let the other lwps continue. 40307c478bd9Sstevel@tonic-gate */ 40317c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 40327c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 40337c478bd9Sstevel@tonic-gate continuelwps(pp); 40347c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 40357c478bd9Sstevel@tonic-gate 40367c478bd9Sstevel@tonic-gate resume_mounts(); 40377c478bd9Sstevel@tonic-gate if (rctls) 40387c478bd9Sstevel@tonic-gate nvlist_free(rctls); 40397c478bd9Sstevel@tonic-gate /* 40407c478bd9Sstevel@tonic-gate * There is currently one reference to the zone, a cred_ref from 40417c478bd9Sstevel@tonic-gate * zone_kcred. To free the zone, we call crfree, which will call 40427c478bd9Sstevel@tonic-gate * zone_cred_rele, which will call zone_free. 40437c478bd9Sstevel@tonic-gate */ 40447c478bd9Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 40457c478bd9Sstevel@tonic-gate ASSERT(zone->zone_kcred->cr_ref == 1); 40467c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 40477c478bd9Sstevel@tonic-gate zkcr = zone->zone_kcred; 40487c478bd9Sstevel@tonic-gate zone->zone_kcred = NULL; 40497c478bd9Sstevel@tonic-gate crfree(zkcr); /* triggers call to zone_free */ 40507c478bd9Sstevel@tonic-gate return (zone_create_error(error, error2, extended_error)); 40517c478bd9Sstevel@tonic-gate } 40527c478bd9Sstevel@tonic-gate 40537c478bd9Sstevel@tonic-gate /* 40547c478bd9Sstevel@tonic-gate * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 40553f2f09c1Sdp * the heavy lifting. initname is the path to the program to launch 40563f2f09c1Sdp * at the "top" of the zone; if this is NULL, we use the system default, 40573f2f09c1Sdp * which is stored at zone_default_initname. 40587c478bd9Sstevel@tonic-gate */ 40597c478bd9Sstevel@tonic-gate static int 40603f2f09c1Sdp zone_boot(zoneid_t zoneid) 40617c478bd9Sstevel@tonic-gate { 40627c478bd9Sstevel@tonic-gate int err; 40637c478bd9Sstevel@tonic-gate zone_t *zone; 40647c478bd9Sstevel@tonic-gate 40657c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 40667c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 40677c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 40687c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 40697c478bd9Sstevel@tonic-gate 40707c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 40717c478bd9Sstevel@tonic-gate /* 40727c478bd9Sstevel@tonic-gate * Look for zone under hash lock to prevent races with calls to 40737c478bd9Sstevel@tonic-gate * zone_shutdown, zone_destroy, etc. 40747c478bd9Sstevel@tonic-gate */ 40757c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 40767c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40777c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 40787c478bd9Sstevel@tonic-gate } 40797c478bd9Sstevel@tonic-gate 40807c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 40817c478bd9Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_READY) { 40827c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 40837c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40847c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 40857c478bd9Sstevel@tonic-gate } 40867c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_BOOTING); 40877c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 40887c478bd9Sstevel@tonic-gate 40897c478bd9Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 40907c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40917c478bd9Sstevel@tonic-gate 40927c478bd9Sstevel@tonic-gate if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 40937c478bd9Sstevel@tonic-gate zone_rele(zone); 40947c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 40957c478bd9Sstevel@tonic-gate } 40967c478bd9Sstevel@tonic-gate 40977c478bd9Sstevel@tonic-gate /* 40987c478bd9Sstevel@tonic-gate * Boot (starting init) might have failed, in which case the zone 40997c478bd9Sstevel@tonic-gate * will go to the SHUTTING_DOWN state; an appropriate errno will 41007c478bd9Sstevel@tonic-gate * be placed in zone->zone_boot_err, and so we return that. 41017c478bd9Sstevel@tonic-gate */ 41027c478bd9Sstevel@tonic-gate err = zone->zone_boot_err; 41037c478bd9Sstevel@tonic-gate zone_rele(zone); 41047c478bd9Sstevel@tonic-gate return (err ? set_errno(err) : 0); 41057c478bd9Sstevel@tonic-gate } 41067c478bd9Sstevel@tonic-gate 41077c478bd9Sstevel@tonic-gate /* 41087c478bd9Sstevel@tonic-gate * Kills all user processes in the zone, waiting for them all to exit 41097c478bd9Sstevel@tonic-gate * before returning. 41107c478bd9Sstevel@tonic-gate */ 41117c478bd9Sstevel@tonic-gate static int 41127c478bd9Sstevel@tonic-gate zone_empty(zone_t *zone) 41137c478bd9Sstevel@tonic-gate { 41147c478bd9Sstevel@tonic-gate int waitstatus; 41157c478bd9Sstevel@tonic-gate 41167c478bd9Sstevel@tonic-gate /* 41177c478bd9Sstevel@tonic-gate * We need to drop zonehash_lock before killing all 41187c478bd9Sstevel@tonic-gate * processes, otherwise we'll deadlock with zone_find_* 41197c478bd9Sstevel@tonic-gate * which can be called from the exit path. 41207c478bd9Sstevel@tonic-gate */ 41217c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 41227c478bd9Sstevel@tonic-gate while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz, 41237c478bd9Sstevel@tonic-gate ZONE_IS_EMPTY)) == -1) { 41247c478bd9Sstevel@tonic-gate killall(zone->zone_id); 41257c478bd9Sstevel@tonic-gate } 41267c478bd9Sstevel@tonic-gate /* 41277c478bd9Sstevel@tonic-gate * return EINTR if we were signaled 41287c478bd9Sstevel@tonic-gate */ 41297c478bd9Sstevel@tonic-gate if (waitstatus == 0) 41307c478bd9Sstevel@tonic-gate return (EINTR); 41317c478bd9Sstevel@tonic-gate return (0); 41327c478bd9Sstevel@tonic-gate } 41337c478bd9Sstevel@tonic-gate 413445916cd2Sjpk /* 413545916cd2Sjpk * This function implements the policy for zone visibility. 413645916cd2Sjpk * 413745916cd2Sjpk * In standard Solaris, a non-global zone can only see itself. 413845916cd2Sjpk * 413945916cd2Sjpk * In Trusted Extensions, a labeled zone can lookup any zone whose label 414045916cd2Sjpk * it dominates. For this test, the label of the global zone is treated as 414145916cd2Sjpk * admin_high so it is special-cased instead of being checked for dominance. 414245916cd2Sjpk * 414345916cd2Sjpk * Returns true if zone attributes are viewable, false otherwise. 414445916cd2Sjpk */ 414545916cd2Sjpk static boolean_t 414645916cd2Sjpk zone_list_access(zone_t *zone) 414745916cd2Sjpk { 414845916cd2Sjpk 414945916cd2Sjpk if (curproc->p_zone == global_zone || 415045916cd2Sjpk curproc->p_zone == zone) { 415145916cd2Sjpk return (B_TRUE); 415248451833Scarlsonj } else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 415345916cd2Sjpk bslabel_t *curproc_label; 415445916cd2Sjpk bslabel_t *zone_label; 415545916cd2Sjpk 415645916cd2Sjpk curproc_label = label2bslabel(curproc->p_zone->zone_slabel); 415745916cd2Sjpk zone_label = label2bslabel(zone->zone_slabel); 415845916cd2Sjpk 415945916cd2Sjpk if (zone->zone_id != GLOBAL_ZONEID && 416045916cd2Sjpk bldominates(curproc_label, zone_label)) { 416145916cd2Sjpk return (B_TRUE); 416245916cd2Sjpk } else { 416345916cd2Sjpk return (B_FALSE); 416445916cd2Sjpk } 416545916cd2Sjpk } else { 416645916cd2Sjpk return (B_FALSE); 416745916cd2Sjpk } 416845916cd2Sjpk } 416945916cd2Sjpk 41707c478bd9Sstevel@tonic-gate /* 41717c478bd9Sstevel@tonic-gate * Systemcall to start the zone's halt sequence. By the time this 41727c478bd9Sstevel@tonic-gate * function successfully returns, all user processes and kernel threads 41737c478bd9Sstevel@tonic-gate * executing in it will have exited, ZSD shutdown callbacks executed, 41747c478bd9Sstevel@tonic-gate * and the zone status set to ZONE_IS_DOWN. 41757c478bd9Sstevel@tonic-gate * 41767c478bd9Sstevel@tonic-gate * It is possible that the call will interrupt itself if the caller is the 41777c478bd9Sstevel@tonic-gate * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 41787c478bd9Sstevel@tonic-gate */ 41797c478bd9Sstevel@tonic-gate static int 41807c478bd9Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 41817c478bd9Sstevel@tonic-gate { 41827c478bd9Sstevel@tonic-gate int error; 41837c478bd9Sstevel@tonic-gate zone_t *zone; 41847c478bd9Sstevel@tonic-gate zone_status_t status; 41857c478bd9Sstevel@tonic-gate 41867c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 41877c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 41887c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 41897c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 41907c478bd9Sstevel@tonic-gate 41917c478bd9Sstevel@tonic-gate /* 41927c478bd9Sstevel@tonic-gate * Block mounts so that VFS_MOUNT() can get an accurate view of 41937c478bd9Sstevel@tonic-gate * the zone's status with regards to ZONE_IS_SHUTTING down. 41947c478bd9Sstevel@tonic-gate * 41957c478bd9Sstevel@tonic-gate * e.g. NFS can fail the mount if it determines that the zone 41967c478bd9Sstevel@tonic-gate * has already begun the shutdown sequence. 41977c478bd9Sstevel@tonic-gate */ 41987c478bd9Sstevel@tonic-gate if (block_mounts() == 0) 41997c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 42007c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 42017c478bd9Sstevel@tonic-gate /* 42027c478bd9Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 42037c478bd9Sstevel@tonic-gate * calls to zone_shutdown and zone_destroy. 42047c478bd9Sstevel@tonic-gate */ 42057c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 42067c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42077c478bd9Sstevel@tonic-gate resume_mounts(); 42087c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 42097c478bd9Sstevel@tonic-gate } 42107c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 42117c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 42127c478bd9Sstevel@tonic-gate /* 42137c478bd9Sstevel@tonic-gate * Fail if the zone isn't fully initialized yet. 42147c478bd9Sstevel@tonic-gate */ 42157c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY) { 42167c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 42177c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42187c478bd9Sstevel@tonic-gate resume_mounts(); 42197c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 42207c478bd9Sstevel@tonic-gate } 42217c478bd9Sstevel@tonic-gate /* 42227c478bd9Sstevel@tonic-gate * If conditions required for zone_shutdown() to return have been met, 42237c478bd9Sstevel@tonic-gate * return success. 42247c478bd9Sstevel@tonic-gate */ 42257c478bd9Sstevel@tonic-gate if (status >= ZONE_IS_DOWN) { 42267c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 42277c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42287c478bd9Sstevel@tonic-gate resume_mounts(); 42297c478bd9Sstevel@tonic-gate return (0); 42307c478bd9Sstevel@tonic-gate } 42317c478bd9Sstevel@tonic-gate /* 42327c478bd9Sstevel@tonic-gate * If zone_shutdown() hasn't been called before, go through the motions. 42337c478bd9Sstevel@tonic-gate * If it has, there's nothing to do but wait for the kernel threads to 42347c478bd9Sstevel@tonic-gate * drain. 42357c478bd9Sstevel@tonic-gate */ 42367c478bd9Sstevel@tonic-gate if (status < ZONE_IS_EMPTY) { 42377c478bd9Sstevel@tonic-gate uint_t ntasks; 42387c478bd9Sstevel@tonic-gate 42397c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 42407c478bd9Sstevel@tonic-gate if ((ntasks = zone->zone_ntasks) != 1) { 42417c478bd9Sstevel@tonic-gate /* 42427c478bd9Sstevel@tonic-gate * There's still stuff running. 42437c478bd9Sstevel@tonic-gate */ 42447c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 42457c478bd9Sstevel@tonic-gate } 42467c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 42477c478bd9Sstevel@tonic-gate if (ntasks == 1) { 42487c478bd9Sstevel@tonic-gate /* 42497c478bd9Sstevel@tonic-gate * The only way to create another task is through 42507c478bd9Sstevel@tonic-gate * zone_enter(), which will block until we drop 42517c478bd9Sstevel@tonic-gate * zonehash_lock. The zone is empty. 42527c478bd9Sstevel@tonic-gate */ 42537c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 42547c478bd9Sstevel@tonic-gate /* 42557c478bd9Sstevel@tonic-gate * Skip ahead to ZONE_IS_DOWN 42567c478bd9Sstevel@tonic-gate */ 42577c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 42587c478bd9Sstevel@tonic-gate } else { 42597c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 42607c478bd9Sstevel@tonic-gate } 42617c478bd9Sstevel@tonic-gate } 42627c478bd9Sstevel@tonic-gate } 42637c478bd9Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 42647c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 42657c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42667c478bd9Sstevel@tonic-gate resume_mounts(); 42677c478bd9Sstevel@tonic-gate 42687c478bd9Sstevel@tonic-gate if (error = zone_empty(zone)) { 42697c478bd9Sstevel@tonic-gate zone_rele(zone); 42707c478bd9Sstevel@tonic-gate return (set_errno(error)); 42717c478bd9Sstevel@tonic-gate } 42727c478bd9Sstevel@tonic-gate /* 42737c478bd9Sstevel@tonic-gate * After the zone status goes to ZONE_IS_DOWN this zone will no 42747c478bd9Sstevel@tonic-gate * longer be notified of changes to the pools configuration, so 42757c478bd9Sstevel@tonic-gate * in order to not end up with a stale pool pointer, we point 42767c478bd9Sstevel@tonic-gate * ourselves at the default pool and remove all resource 42777c478bd9Sstevel@tonic-gate * visibility. This is especially important as the zone_t may 42787c478bd9Sstevel@tonic-gate * languish on the deathrow for a very long time waiting for 42797c478bd9Sstevel@tonic-gate * cred's to drain out. 42807c478bd9Sstevel@tonic-gate * 42817c478bd9Sstevel@tonic-gate * This rebinding of the zone can happen multiple times 42827c478bd9Sstevel@tonic-gate * (presumably due to interrupted or parallel systemcalls) 42837c478bd9Sstevel@tonic-gate * without any adverse effects. 42847c478bd9Sstevel@tonic-gate */ 42857c478bd9Sstevel@tonic-gate if (pool_lock_intr() != 0) { 42867c478bd9Sstevel@tonic-gate zone_rele(zone); 42877c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 42887c478bd9Sstevel@tonic-gate } 42897c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 42907c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 42917c478bd9Sstevel@tonic-gate zone_pool_set(zone, pool_default); 42927c478bd9Sstevel@tonic-gate /* 42937c478bd9Sstevel@tonic-gate * The zone no longer needs to be able to see any cpus. 42947c478bd9Sstevel@tonic-gate */ 42957c478bd9Sstevel@tonic-gate zone_pset_set(zone, ZONE_PS_INVAL); 42967c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 42977c478bd9Sstevel@tonic-gate } 42987c478bd9Sstevel@tonic-gate pool_unlock(); 42997c478bd9Sstevel@tonic-gate 43007c478bd9Sstevel@tonic-gate /* 43017c478bd9Sstevel@tonic-gate * ZSD shutdown callbacks can be executed multiple times, hence 43027c478bd9Sstevel@tonic-gate * it is safe to not be holding any locks across this call. 43037c478bd9Sstevel@tonic-gate */ 43047c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 43057c478bd9Sstevel@tonic-gate 43067c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 43077c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 43087c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 43097c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 43107c478bd9Sstevel@tonic-gate 43117c478bd9Sstevel@tonic-gate /* 43127c478bd9Sstevel@tonic-gate * Wait for kernel threads to drain. 43137c478bd9Sstevel@tonic-gate */ 43147c478bd9Sstevel@tonic-gate if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 43157c478bd9Sstevel@tonic-gate zone_rele(zone); 43167c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 43177c478bd9Sstevel@tonic-gate } 43189acbbeafSnn 431952978630Ssl /* 432052978630Ssl * Zone can be become down/destroyable even if the above wait 432152978630Ssl * returns EINTR, so any code added here may never execute. 432252978630Ssl * (i.e. don't add code here) 432352978630Ssl */ 43249acbbeafSnn 43257c478bd9Sstevel@tonic-gate zone_rele(zone); 43267c478bd9Sstevel@tonic-gate return (0); 43277c478bd9Sstevel@tonic-gate } 43287c478bd9Sstevel@tonic-gate 43297c478bd9Sstevel@tonic-gate /* 43307c478bd9Sstevel@tonic-gate * Systemcall entry point to finalize the zone halt process. The caller 4331824c205fSml * must have already successfully called zone_shutdown(). 43327c478bd9Sstevel@tonic-gate * 43337c478bd9Sstevel@tonic-gate * Upon successful completion, the zone will have been fully destroyed: 43347c478bd9Sstevel@tonic-gate * zsched will have exited, destructor callbacks executed, and the zone 43357c478bd9Sstevel@tonic-gate * removed from the list of active zones. 43367c478bd9Sstevel@tonic-gate */ 43377c478bd9Sstevel@tonic-gate static int 43387c478bd9Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 43397c478bd9Sstevel@tonic-gate { 43407c478bd9Sstevel@tonic-gate uint64_t uniqid; 43417c478bd9Sstevel@tonic-gate zone_t *zone; 43427c478bd9Sstevel@tonic-gate zone_status_t status; 43437c478bd9Sstevel@tonic-gate 43447c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 43457c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 43467c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 43477c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 43487c478bd9Sstevel@tonic-gate 43497c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 43507c478bd9Sstevel@tonic-gate /* 43517c478bd9Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 43527c478bd9Sstevel@tonic-gate * calls to zone_destroy. 43537c478bd9Sstevel@tonic-gate */ 43547c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 43557c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43567c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 43577c478bd9Sstevel@tonic-gate } 43587c478bd9Sstevel@tonic-gate 43597c478bd9Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 43607c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43617c478bd9Sstevel@tonic-gate return (set_errno(EBUSY)); 43627c478bd9Sstevel@tonic-gate } 43637c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 43647c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 43657c478bd9Sstevel@tonic-gate if (status < ZONE_IS_DOWN) { 43667c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 43677c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43687c478bd9Sstevel@tonic-gate return (set_errno(EBUSY)); 43697c478bd9Sstevel@tonic-gate } else if (status == ZONE_IS_DOWN) { 43707c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 43717c478bd9Sstevel@tonic-gate } 43727c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 43737c478bd9Sstevel@tonic-gate zone_hold(zone); 43747c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43757c478bd9Sstevel@tonic-gate 43767c478bd9Sstevel@tonic-gate /* 43777c478bd9Sstevel@tonic-gate * wait for zsched to exit 43787c478bd9Sstevel@tonic-gate */ 43797c478bd9Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_DEAD); 43807c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_DESTROY); 4381f4b3ec61Sdh zone->zone_netstack = NULL; 43827c478bd9Sstevel@tonic-gate uniqid = zone->zone_uniqid; 43837c478bd9Sstevel@tonic-gate zone_rele(zone); 43847c478bd9Sstevel@tonic-gate zone = NULL; /* potentially free'd */ 43857c478bd9Sstevel@tonic-gate 43867c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 43877c478bd9Sstevel@tonic-gate for (; /* ever */; ) { 43887c478bd9Sstevel@tonic-gate boolean_t unref; 43897c478bd9Sstevel@tonic-gate 43907c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL || 43917c478bd9Sstevel@tonic-gate zone->zone_uniqid != uniqid) { 43927c478bd9Sstevel@tonic-gate /* 43937c478bd9Sstevel@tonic-gate * The zone has gone away. Necessary conditions 43947c478bd9Sstevel@tonic-gate * are met, so we return success. 43957c478bd9Sstevel@tonic-gate */ 43967c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43977c478bd9Sstevel@tonic-gate return (0); 43987c478bd9Sstevel@tonic-gate } 43997c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 44007c478bd9Sstevel@tonic-gate unref = ZONE_IS_UNREF(zone); 44017c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 44027c478bd9Sstevel@tonic-gate if (unref) { 44037c478bd9Sstevel@tonic-gate /* 44047c478bd9Sstevel@tonic-gate * There is only one reference to the zone -- that 44057c478bd9Sstevel@tonic-gate * added when the zone was added to the hashtables -- 44067c478bd9Sstevel@tonic-gate * and things will remain this way until we drop 44077c478bd9Sstevel@tonic-gate * zonehash_lock... we can go ahead and cleanup the 44087c478bd9Sstevel@tonic-gate * zone. 44097c478bd9Sstevel@tonic-gate */ 44107c478bd9Sstevel@tonic-gate break; 44117c478bd9Sstevel@tonic-gate } 44127c478bd9Sstevel@tonic-gate 44137c478bd9Sstevel@tonic-gate if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 44147c478bd9Sstevel@tonic-gate /* Signaled */ 44157c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44167c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 44177c478bd9Sstevel@tonic-gate } 44187c478bd9Sstevel@tonic-gate 44197c478bd9Sstevel@tonic-gate } 44207c478bd9Sstevel@tonic-gate 4421c97ad5cdSakolb /* 4422c97ad5cdSakolb * Remove CPU cap for this zone now since we're not going to 4423c97ad5cdSakolb * fail below this point. 4424c97ad5cdSakolb */ 4425c97ad5cdSakolb cpucaps_zone_remove(zone); 4426c97ad5cdSakolb 4427c97ad5cdSakolb /* Get rid of the zone's kstats */ 44280209230bSgjelinek zone_kstat_delete(zone); 44290209230bSgjelinek 4430319378d9Seh /* free brand specific data */ 4431319378d9Seh if (ZONE_IS_BRANDED(zone)) 4432319378d9Seh ZBROP(zone)->b_free_brand_data(zone); 4433319378d9Seh 443452978630Ssl /* Say goodbye to brand framework. */ 443552978630Ssl brand_unregister_zone(zone->zone_brand); 443652978630Ssl 44377c478bd9Sstevel@tonic-gate /* 44387c478bd9Sstevel@tonic-gate * It is now safe to let the zone be recreated; remove it from the 44397c478bd9Sstevel@tonic-gate * lists. The memory will not be freed until the last cred 44407c478bd9Sstevel@tonic-gate * reference goes away. 44417c478bd9Sstevel@tonic-gate */ 44427c478bd9Sstevel@tonic-gate ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 44437c478bd9Sstevel@tonic-gate zonecount--; 44447c478bd9Sstevel@tonic-gate /* remove from active list and hash tables */ 44457c478bd9Sstevel@tonic-gate list_remove(&zone_active, zone); 44467c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 44477c478bd9Sstevel@tonic-gate (mod_hash_key_t)zone->zone_name); 44487c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 44497c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 445048451833Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) 445145916cd2Sjpk (void) mod_hash_destroy(zonehashbylabel, 445245916cd2Sjpk (mod_hash_key_t)zone->zone_slabel); 44537c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44547c478bd9Sstevel@tonic-gate 4455108322fbScarlsonj /* 4456108322fbScarlsonj * Release the root vnode; we're not using it anymore. Nor should any 4457108322fbScarlsonj * other thread that might access it exist. 4458108322fbScarlsonj */ 4459108322fbScarlsonj if (zone->zone_rootvp != NULL) { 4460108322fbScarlsonj VN_RELE(zone->zone_rootvp); 4461108322fbScarlsonj zone->zone_rootvp = NULL; 4462108322fbScarlsonj } 4463108322fbScarlsonj 44647c478bd9Sstevel@tonic-gate /* add to deathrow list */ 44657c478bd9Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 44667c478bd9Sstevel@tonic-gate list_insert_tail(&zone_deathrow, zone); 44677c478bd9Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 44687c478bd9Sstevel@tonic-gate 44697c478bd9Sstevel@tonic-gate /* 44707c478bd9Sstevel@tonic-gate * Drop last reference (which was added by zsched()), this will 44717c478bd9Sstevel@tonic-gate * free the zone unless there are outstanding cred references. 44727c478bd9Sstevel@tonic-gate */ 44737c478bd9Sstevel@tonic-gate zone_rele(zone); 44747c478bd9Sstevel@tonic-gate return (0); 44757c478bd9Sstevel@tonic-gate } 44767c478bd9Sstevel@tonic-gate 44777c478bd9Sstevel@tonic-gate /* 44787c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_getattr(2). 44797c478bd9Sstevel@tonic-gate */ 44807c478bd9Sstevel@tonic-gate static ssize_t 44817c478bd9Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 44827c478bd9Sstevel@tonic-gate { 44837c478bd9Sstevel@tonic-gate size_t size; 44847c478bd9Sstevel@tonic-gate int error = 0, err; 44857c478bd9Sstevel@tonic-gate zone_t *zone; 44867c478bd9Sstevel@tonic-gate char *zonepath; 44873f2f09c1Sdp char *outstr; 44887c478bd9Sstevel@tonic-gate zone_status_t zone_status; 44897c478bd9Sstevel@tonic-gate pid_t initpid; 4490c97ad5cdSakolb boolean_t global = (curzone == global_zone); 4491c97ad5cdSakolb boolean_t inzone = (curzone->zone_id == zoneid); 4492f4b3ec61Sdh ushort_t flags; 44937c478bd9Sstevel@tonic-gate 44947c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 44957c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 44967c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44977c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 44987c478bd9Sstevel@tonic-gate } 44997c478bd9Sstevel@tonic-gate zone_status = zone_status_get(zone); 4500*bd41d0a8Snordmark if (zone_status < ZONE_IS_INITIALIZED) { 45017c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45027c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 45037c478bd9Sstevel@tonic-gate } 45047c478bd9Sstevel@tonic-gate zone_hold(zone); 45057c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45067c478bd9Sstevel@tonic-gate 45077c478bd9Sstevel@tonic-gate /* 450845916cd2Sjpk * If not in the global zone, don't show information about other zones, 450945916cd2Sjpk * unless the system is labeled and the local zone's label dominates 451045916cd2Sjpk * the other zone. 45117c478bd9Sstevel@tonic-gate */ 451245916cd2Sjpk if (!zone_list_access(zone)) { 45137c478bd9Sstevel@tonic-gate zone_rele(zone); 45147c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 45157c478bd9Sstevel@tonic-gate } 45167c478bd9Sstevel@tonic-gate 45177c478bd9Sstevel@tonic-gate switch (attr) { 45187c478bd9Sstevel@tonic-gate case ZONE_ATTR_ROOT: 45197c478bd9Sstevel@tonic-gate if (global) { 45207c478bd9Sstevel@tonic-gate /* 45217c478bd9Sstevel@tonic-gate * Copy the path to trim the trailing "/" (except for 45227c478bd9Sstevel@tonic-gate * the global zone). 45237c478bd9Sstevel@tonic-gate */ 45247c478bd9Sstevel@tonic-gate if (zone != global_zone) 45257c478bd9Sstevel@tonic-gate size = zone->zone_rootpathlen - 1; 45267c478bd9Sstevel@tonic-gate else 45277c478bd9Sstevel@tonic-gate size = zone->zone_rootpathlen; 45287c478bd9Sstevel@tonic-gate zonepath = kmem_alloc(size, KM_SLEEP); 45297c478bd9Sstevel@tonic-gate bcopy(zone->zone_rootpath, zonepath, size); 45307c478bd9Sstevel@tonic-gate zonepath[size - 1] = '\0'; 45317c478bd9Sstevel@tonic-gate } else { 4532c97ad5cdSakolb if (inzone || !is_system_labeled()) { 453345916cd2Sjpk /* 453445916cd2Sjpk * Caller is not in the global zone. 453545916cd2Sjpk * if the query is on the current zone 453645916cd2Sjpk * or the system is not labeled, 453745916cd2Sjpk * just return faked-up path for current zone. 453845916cd2Sjpk */ 453945916cd2Sjpk zonepath = "/"; 454045916cd2Sjpk size = 2; 454145916cd2Sjpk } else { 454245916cd2Sjpk /* 454345916cd2Sjpk * Return related path for current zone. 454445916cd2Sjpk */ 454545916cd2Sjpk int prefix_len = strlen(zone_prefix); 454645916cd2Sjpk int zname_len = strlen(zone->zone_name); 454745916cd2Sjpk 454845916cd2Sjpk size = prefix_len + zname_len + 1; 454945916cd2Sjpk zonepath = kmem_alloc(size, KM_SLEEP); 455045916cd2Sjpk bcopy(zone_prefix, zonepath, prefix_len); 455145916cd2Sjpk bcopy(zone->zone_name, zonepath + 45523f2f09c1Sdp prefix_len, zname_len); 455345916cd2Sjpk zonepath[size - 1] = '\0'; 455445916cd2Sjpk } 45557c478bd9Sstevel@tonic-gate } 45567c478bd9Sstevel@tonic-gate if (bufsize > size) 45577c478bd9Sstevel@tonic-gate bufsize = size; 45587c478bd9Sstevel@tonic-gate if (buf != NULL) { 45597c478bd9Sstevel@tonic-gate err = copyoutstr(zonepath, buf, bufsize, NULL); 45607c478bd9Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 45617c478bd9Sstevel@tonic-gate error = EFAULT; 45627c478bd9Sstevel@tonic-gate } 4563c97ad5cdSakolb if (global || (is_system_labeled() && !inzone)) 45647c478bd9Sstevel@tonic-gate kmem_free(zonepath, size); 45657c478bd9Sstevel@tonic-gate break; 45667c478bd9Sstevel@tonic-gate 45677c478bd9Sstevel@tonic-gate case ZONE_ATTR_NAME: 45687c478bd9Sstevel@tonic-gate size = strlen(zone->zone_name) + 1; 45697c478bd9Sstevel@tonic-gate if (bufsize > size) 45707c478bd9Sstevel@tonic-gate bufsize = size; 45717c478bd9Sstevel@tonic-gate if (buf != NULL) { 45727c478bd9Sstevel@tonic-gate err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 45737c478bd9Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 45747c478bd9Sstevel@tonic-gate error = EFAULT; 45757c478bd9Sstevel@tonic-gate } 45767c478bd9Sstevel@tonic-gate break; 45777c478bd9Sstevel@tonic-gate 45787c478bd9Sstevel@tonic-gate case ZONE_ATTR_STATUS: 45797c478bd9Sstevel@tonic-gate /* 45807c478bd9Sstevel@tonic-gate * Since we're not holding zonehash_lock, the zone status 45817c478bd9Sstevel@tonic-gate * may be anything; leave it up to userland to sort it out. 45827c478bd9Sstevel@tonic-gate */ 45837c478bd9Sstevel@tonic-gate size = sizeof (zone_status); 45847c478bd9Sstevel@tonic-gate if (bufsize > size) 45857c478bd9Sstevel@tonic-gate bufsize = size; 45867c478bd9Sstevel@tonic-gate zone_status = zone_status_get(zone); 45877c478bd9Sstevel@tonic-gate if (buf != NULL && 45887c478bd9Sstevel@tonic-gate copyout(&zone_status, buf, bufsize) != 0) 45897c478bd9Sstevel@tonic-gate error = EFAULT; 45907c478bd9Sstevel@tonic-gate break; 4591f4b3ec61Sdh case ZONE_ATTR_FLAGS: 4592f4b3ec61Sdh size = sizeof (zone->zone_flags); 4593f4b3ec61Sdh if (bufsize > size) 4594f4b3ec61Sdh bufsize = size; 4595f4b3ec61Sdh flags = zone->zone_flags; 4596f4b3ec61Sdh if (buf != NULL && 4597f4b3ec61Sdh copyout(&flags, buf, bufsize) != 0) 4598f4b3ec61Sdh error = EFAULT; 4599f4b3ec61Sdh break; 46007c478bd9Sstevel@tonic-gate case ZONE_ATTR_PRIVSET: 46017c478bd9Sstevel@tonic-gate size = sizeof (priv_set_t); 46027c478bd9Sstevel@tonic-gate if (bufsize > size) 46037c478bd9Sstevel@tonic-gate bufsize = size; 46047c478bd9Sstevel@tonic-gate if (buf != NULL && 46057c478bd9Sstevel@tonic-gate copyout(zone->zone_privset, buf, bufsize) != 0) 46067c478bd9Sstevel@tonic-gate error = EFAULT; 46077c478bd9Sstevel@tonic-gate break; 46087c478bd9Sstevel@tonic-gate case ZONE_ATTR_UNIQID: 46097c478bd9Sstevel@tonic-gate size = sizeof (zone->zone_uniqid); 46107c478bd9Sstevel@tonic-gate if (bufsize > size) 46117c478bd9Sstevel@tonic-gate bufsize = size; 46127c478bd9Sstevel@tonic-gate if (buf != NULL && 46137c478bd9Sstevel@tonic-gate copyout(&zone->zone_uniqid, buf, bufsize) != 0) 46147c478bd9Sstevel@tonic-gate error = EFAULT; 46157c478bd9Sstevel@tonic-gate break; 46167c478bd9Sstevel@tonic-gate case ZONE_ATTR_POOLID: 46177c478bd9Sstevel@tonic-gate { 46187c478bd9Sstevel@tonic-gate pool_t *pool; 46197c478bd9Sstevel@tonic-gate poolid_t poolid; 46207c478bd9Sstevel@tonic-gate 46217c478bd9Sstevel@tonic-gate if (pool_lock_intr() != 0) { 46227c478bd9Sstevel@tonic-gate error = EINTR; 46237c478bd9Sstevel@tonic-gate break; 46247c478bd9Sstevel@tonic-gate } 46257c478bd9Sstevel@tonic-gate pool = zone_pool_get(zone); 46267c478bd9Sstevel@tonic-gate poolid = pool->pool_id; 46277c478bd9Sstevel@tonic-gate pool_unlock(); 46287c478bd9Sstevel@tonic-gate size = sizeof (poolid); 46297c478bd9Sstevel@tonic-gate if (bufsize > size) 46307c478bd9Sstevel@tonic-gate bufsize = size; 46317c478bd9Sstevel@tonic-gate if (buf != NULL && copyout(&poolid, buf, size) != 0) 46327c478bd9Sstevel@tonic-gate error = EFAULT; 46337c478bd9Sstevel@tonic-gate } 46347c478bd9Sstevel@tonic-gate break; 463545916cd2Sjpk case ZONE_ATTR_SLBL: 463645916cd2Sjpk size = sizeof (bslabel_t); 463745916cd2Sjpk if (bufsize > size) 463845916cd2Sjpk bufsize = size; 463945916cd2Sjpk if (zone->zone_slabel == NULL) 464045916cd2Sjpk error = EINVAL; 464145916cd2Sjpk else if (buf != NULL && 464245916cd2Sjpk copyout(label2bslabel(zone->zone_slabel), buf, 464345916cd2Sjpk bufsize) != 0) 464445916cd2Sjpk error = EFAULT; 464545916cd2Sjpk break; 46467c478bd9Sstevel@tonic-gate case ZONE_ATTR_INITPID: 46477c478bd9Sstevel@tonic-gate size = sizeof (initpid); 46487c478bd9Sstevel@tonic-gate if (bufsize > size) 46497c478bd9Sstevel@tonic-gate bufsize = size; 46507c478bd9Sstevel@tonic-gate initpid = zone->zone_proc_initpid; 46517c478bd9Sstevel@tonic-gate if (initpid == -1) { 46527c478bd9Sstevel@tonic-gate error = ESRCH; 46537c478bd9Sstevel@tonic-gate break; 46547c478bd9Sstevel@tonic-gate } 46557c478bd9Sstevel@tonic-gate if (buf != NULL && 46567c478bd9Sstevel@tonic-gate copyout(&initpid, buf, bufsize) != 0) 46577c478bd9Sstevel@tonic-gate error = EFAULT; 46587c478bd9Sstevel@tonic-gate break; 46599acbbeafSnn case ZONE_ATTR_BRAND: 46609acbbeafSnn size = strlen(zone->zone_brand->b_name) + 1; 46619acbbeafSnn 46629acbbeafSnn if (bufsize > size) 46639acbbeafSnn bufsize = size; 46649acbbeafSnn if (buf != NULL) { 46659acbbeafSnn err = copyoutstr(zone->zone_brand->b_name, buf, 46669acbbeafSnn bufsize, NULL); 46679acbbeafSnn if (err != 0 && err != ENAMETOOLONG) 46689acbbeafSnn error = EFAULT; 46699acbbeafSnn } 46709acbbeafSnn break; 46713f2f09c1Sdp case ZONE_ATTR_INITNAME: 46723f2f09c1Sdp size = strlen(zone->zone_initname) + 1; 46733f2f09c1Sdp if (bufsize > size) 46743f2f09c1Sdp bufsize = size; 46753f2f09c1Sdp if (buf != NULL) { 46763f2f09c1Sdp err = copyoutstr(zone->zone_initname, buf, bufsize, 46773f2f09c1Sdp NULL); 46783f2f09c1Sdp if (err != 0 && err != ENAMETOOLONG) 46793f2f09c1Sdp error = EFAULT; 46803f2f09c1Sdp } 46813f2f09c1Sdp break; 46823f2f09c1Sdp case ZONE_ATTR_BOOTARGS: 46833f2f09c1Sdp if (zone->zone_bootargs == NULL) 46843f2f09c1Sdp outstr = ""; 46853f2f09c1Sdp else 46863f2f09c1Sdp outstr = zone->zone_bootargs; 46873f2f09c1Sdp size = strlen(outstr) + 1; 46883f2f09c1Sdp if (bufsize > size) 46893f2f09c1Sdp bufsize = size; 46903f2f09c1Sdp if (buf != NULL) { 46913f2f09c1Sdp err = copyoutstr(outstr, buf, bufsize, NULL); 46923f2f09c1Sdp if (err != 0 && err != ENAMETOOLONG) 46933f2f09c1Sdp error = EFAULT; 46943f2f09c1Sdp } 46953f2f09c1Sdp break; 46960209230bSgjelinek case ZONE_ATTR_PHYS_MCAP: 46970209230bSgjelinek size = sizeof (zone->zone_phys_mcap); 46980209230bSgjelinek if (bufsize > size) 46990209230bSgjelinek bufsize = size; 47000209230bSgjelinek if (buf != NULL && 47010209230bSgjelinek copyout(&zone->zone_phys_mcap, buf, bufsize) != 0) 47020209230bSgjelinek error = EFAULT; 47030209230bSgjelinek break; 47040209230bSgjelinek case ZONE_ATTR_SCHED_CLASS: 47050209230bSgjelinek mutex_enter(&class_lock); 47060209230bSgjelinek 47070209230bSgjelinek if (zone->zone_defaultcid >= loaded_classes) 47080209230bSgjelinek outstr = ""; 47090209230bSgjelinek else 47100209230bSgjelinek outstr = sclass[zone->zone_defaultcid].cl_name; 47110209230bSgjelinek size = strlen(outstr) + 1; 47120209230bSgjelinek if (bufsize > size) 47130209230bSgjelinek bufsize = size; 47140209230bSgjelinek if (buf != NULL) { 47150209230bSgjelinek err = copyoutstr(outstr, buf, bufsize, NULL); 47160209230bSgjelinek if (err != 0 && err != ENAMETOOLONG) 47170209230bSgjelinek error = EFAULT; 47180209230bSgjelinek } 47190209230bSgjelinek 47200209230bSgjelinek mutex_exit(&class_lock); 47210209230bSgjelinek break; 47227c478bd9Sstevel@tonic-gate default: 47239acbbeafSnn if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) { 47249acbbeafSnn size = bufsize; 47259acbbeafSnn error = ZBROP(zone)->b_getattr(zone, attr, buf, &size); 47269acbbeafSnn } else { 47279acbbeafSnn error = EINVAL; 47289acbbeafSnn } 47297c478bd9Sstevel@tonic-gate } 47307c478bd9Sstevel@tonic-gate zone_rele(zone); 47317c478bd9Sstevel@tonic-gate 47327c478bd9Sstevel@tonic-gate if (error) 47337c478bd9Sstevel@tonic-gate return (set_errno(error)); 47347c478bd9Sstevel@tonic-gate return ((ssize_t)size); 47357c478bd9Sstevel@tonic-gate } 47367c478bd9Sstevel@tonic-gate 47373f2f09c1Sdp /* 47383f2f09c1Sdp * Systemcall entry point for zone_setattr(2). 47393f2f09c1Sdp */ 47403f2f09c1Sdp /*ARGSUSED*/ 47413f2f09c1Sdp static int 47423f2f09c1Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 47433f2f09c1Sdp { 47443f2f09c1Sdp zone_t *zone; 47453f2f09c1Sdp zone_status_t zone_status; 47463f2f09c1Sdp int err; 47473f2f09c1Sdp 47483f2f09c1Sdp if (secpolicy_zone_config(CRED()) != 0) 47493f2f09c1Sdp return (set_errno(EPERM)); 47503f2f09c1Sdp 47513f2f09c1Sdp /* 47520209230bSgjelinek * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the 47530209230bSgjelinek * global zone. 47543f2f09c1Sdp */ 47550209230bSgjelinek if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) { 47563f2f09c1Sdp return (set_errno(EINVAL)); 47573f2f09c1Sdp } 47583f2f09c1Sdp 47593f2f09c1Sdp mutex_enter(&zonehash_lock); 47603f2f09c1Sdp if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 47613f2f09c1Sdp mutex_exit(&zonehash_lock); 47623f2f09c1Sdp return (set_errno(EINVAL)); 47633f2f09c1Sdp } 47643f2f09c1Sdp zone_hold(zone); 47653f2f09c1Sdp mutex_exit(&zonehash_lock); 47663f2f09c1Sdp 47670209230bSgjelinek /* 47680209230bSgjelinek * At present most attributes can only be set on non-running, 47690209230bSgjelinek * non-global zones. 47700209230bSgjelinek */ 47713f2f09c1Sdp zone_status = zone_status_get(zone); 47720209230bSgjelinek if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY) 47733f2f09c1Sdp goto done; 47743f2f09c1Sdp 47753f2f09c1Sdp switch (attr) { 47763f2f09c1Sdp case ZONE_ATTR_INITNAME: 47773f2f09c1Sdp err = zone_set_initname(zone, (const char *)buf); 47783f2f09c1Sdp break; 47793f2f09c1Sdp case ZONE_ATTR_BOOTARGS: 47803f2f09c1Sdp err = zone_set_bootargs(zone, (const char *)buf); 47813f2f09c1Sdp break; 47829acbbeafSnn case ZONE_ATTR_BRAND: 478359f2ff5cSedp err = zone_set_brand(zone, (const char *)buf); 47849acbbeafSnn break; 47850209230bSgjelinek case ZONE_ATTR_PHYS_MCAP: 47860209230bSgjelinek err = zone_set_phys_mcap(zone, (const uint64_t *)buf); 47870209230bSgjelinek break; 47880209230bSgjelinek case ZONE_ATTR_SCHED_CLASS: 47890209230bSgjelinek err = zone_set_sched_class(zone, (const char *)buf); 47900209230bSgjelinek break; 47913f2f09c1Sdp default: 47929acbbeafSnn if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) 47939acbbeafSnn err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize); 47949acbbeafSnn else 47959acbbeafSnn err = EINVAL; 47963f2f09c1Sdp } 47973f2f09c1Sdp 47983f2f09c1Sdp done: 47993f2f09c1Sdp zone_rele(zone); 48003f2f09c1Sdp return (err != 0 ? set_errno(err) : 0); 48013f2f09c1Sdp } 48023f2f09c1Sdp 48037c478bd9Sstevel@tonic-gate /* 48047c478bd9Sstevel@tonic-gate * Return zero if the process has at least one vnode mapped in to its 48057c478bd9Sstevel@tonic-gate * address space which shouldn't be allowed to change zones. 48060209230bSgjelinek * 48070209230bSgjelinek * Also return zero if the process has any shared mappings which reserve 48080209230bSgjelinek * swap. This is because the counting for zone.max-swap does not allow swap 4809da6c28aaSamw * reservation to be shared between zones. zone swap reservation is counted 48100209230bSgjelinek * on zone->zone_max_swap. 48117c478bd9Sstevel@tonic-gate */ 48127c478bd9Sstevel@tonic-gate static int 48137c478bd9Sstevel@tonic-gate as_can_change_zones(void) 48147c478bd9Sstevel@tonic-gate { 48157c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 48167c478bd9Sstevel@tonic-gate struct seg *seg; 48177c478bd9Sstevel@tonic-gate struct as *as = pp->p_as; 48187c478bd9Sstevel@tonic-gate vnode_t *vp; 48197c478bd9Sstevel@tonic-gate int allow = 1; 48207c478bd9Sstevel@tonic-gate 48217c478bd9Sstevel@tonic-gate ASSERT(pp->p_as != &kas); 48220209230bSgjelinek AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 48237c478bd9Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 48240209230bSgjelinek 48250209230bSgjelinek /* 48260209230bSgjelinek * Cannot enter zone with shared anon memory which 48270209230bSgjelinek * reserves swap. See comment above. 48280209230bSgjelinek */ 48290209230bSgjelinek if (seg_can_change_zones(seg) == B_FALSE) { 48300209230bSgjelinek allow = 0; 48310209230bSgjelinek break; 48320209230bSgjelinek } 48337c478bd9Sstevel@tonic-gate /* 48347c478bd9Sstevel@tonic-gate * if we can't get a backing vnode for this segment then skip 48357c478bd9Sstevel@tonic-gate * it. 48367c478bd9Sstevel@tonic-gate */ 48377c478bd9Sstevel@tonic-gate vp = NULL; 48387c478bd9Sstevel@tonic-gate if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 48397c478bd9Sstevel@tonic-gate continue; 48407c478bd9Sstevel@tonic-gate if (!vn_can_change_zones(vp)) { /* bail on first match */ 48417c478bd9Sstevel@tonic-gate allow = 0; 48427c478bd9Sstevel@tonic-gate break; 48437c478bd9Sstevel@tonic-gate } 48447c478bd9Sstevel@tonic-gate } 48450209230bSgjelinek AS_LOCK_EXIT(as, &as->a_lock); 48467c478bd9Sstevel@tonic-gate return (allow); 48477c478bd9Sstevel@tonic-gate } 48487c478bd9Sstevel@tonic-gate 48490209230bSgjelinek /* 48500209230bSgjelinek * Count swap reserved by curproc's address space 48510209230bSgjelinek */ 48520209230bSgjelinek static size_t 48530209230bSgjelinek as_swresv(void) 48540209230bSgjelinek { 48550209230bSgjelinek proc_t *pp = curproc; 48560209230bSgjelinek struct seg *seg; 48570209230bSgjelinek struct as *as = pp->p_as; 48580209230bSgjelinek size_t swap = 0; 48590209230bSgjelinek 48600209230bSgjelinek ASSERT(pp->p_as != &kas); 48610209230bSgjelinek ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 48620209230bSgjelinek for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) 48630209230bSgjelinek swap += seg_swresv(seg); 48640209230bSgjelinek 48650209230bSgjelinek return (swap); 48660209230bSgjelinek } 48670209230bSgjelinek 48687c478bd9Sstevel@tonic-gate /* 48697c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_enter(). 48707c478bd9Sstevel@tonic-gate * 48717c478bd9Sstevel@tonic-gate * The current process is injected into said zone. In the process 48727c478bd9Sstevel@tonic-gate * it will change its project membership, privileges, rootdir/cwd, 48737c478bd9Sstevel@tonic-gate * zone-wide rctls, and pool association to match those of the zone. 48747c478bd9Sstevel@tonic-gate * 48757c478bd9Sstevel@tonic-gate * The first zone_enter() called while the zone is in the ZONE_IS_READY 48767c478bd9Sstevel@tonic-gate * state will transition it to ZONE_IS_RUNNING. Processes may only 48777c478bd9Sstevel@tonic-gate * enter a zone that is "ready" or "running". 48787c478bd9Sstevel@tonic-gate */ 48797c478bd9Sstevel@tonic-gate static int 48807c478bd9Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 48817c478bd9Sstevel@tonic-gate { 48827c478bd9Sstevel@tonic-gate zone_t *zone; 48837c478bd9Sstevel@tonic-gate vnode_t *vp; 48847c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 48857c478bd9Sstevel@tonic-gate contract_t *ct; 48867c478bd9Sstevel@tonic-gate cont_process_t *ctp; 48877c478bd9Sstevel@tonic-gate task_t *tk, *oldtk; 48887c478bd9Sstevel@tonic-gate kproject_t *zone_proj0; 48897c478bd9Sstevel@tonic-gate cred_t *cr, *newcr; 48907c478bd9Sstevel@tonic-gate pool_t *oldpool, *newpool; 48917c478bd9Sstevel@tonic-gate sess_t *sp; 48927c478bd9Sstevel@tonic-gate uid_t uid; 48937c478bd9Sstevel@tonic-gate zone_status_t status; 48947c478bd9Sstevel@tonic-gate int err = 0; 48957c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 48960209230bSgjelinek size_t swap; 4897c97ad5cdSakolb kthread_id_t t; 48987c478bd9Sstevel@tonic-gate 48997c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 49007c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 49017c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 49027c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 49037c478bd9Sstevel@tonic-gate 49047c478bd9Sstevel@tonic-gate /* 49057c478bd9Sstevel@tonic-gate * Stop all lwps so we don't need to hold a lock to look at 49067c478bd9Sstevel@tonic-gate * curproc->p_zone. This needs to happen before we grab any 49077c478bd9Sstevel@tonic-gate * locks to avoid deadlock (another lwp in the process could 49087c478bd9Sstevel@tonic-gate * be waiting for the held lock). 49097c478bd9Sstevel@tonic-gate */ 49107c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 49117c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 49127c478bd9Sstevel@tonic-gate 49137c478bd9Sstevel@tonic-gate /* 49147c478bd9Sstevel@tonic-gate * Make sure we're not changing zones with files open or mapped in 49157c478bd9Sstevel@tonic-gate * to our address space which shouldn't be changing zones. 49167c478bd9Sstevel@tonic-gate */ 49177c478bd9Sstevel@tonic-gate if (!files_can_change_zones()) { 49187c478bd9Sstevel@tonic-gate err = EBADF; 49197c478bd9Sstevel@tonic-gate goto out; 49207c478bd9Sstevel@tonic-gate } 49217c478bd9Sstevel@tonic-gate if (!as_can_change_zones()) { 49227c478bd9Sstevel@tonic-gate err = EFAULT; 49237c478bd9Sstevel@tonic-gate goto out; 49247c478bd9Sstevel@tonic-gate } 49257c478bd9Sstevel@tonic-gate 49267c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 49277c478bd9Sstevel@tonic-gate if (pp->p_zone != global_zone) { 49287c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 49297c478bd9Sstevel@tonic-gate err = EINVAL; 49307c478bd9Sstevel@tonic-gate goto out; 49317c478bd9Sstevel@tonic-gate } 49327c478bd9Sstevel@tonic-gate 49337c478bd9Sstevel@tonic-gate zone = zone_find_all_by_id(zoneid); 49347c478bd9Sstevel@tonic-gate if (zone == NULL) { 49357c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 49367c478bd9Sstevel@tonic-gate err = EINVAL; 49377c478bd9Sstevel@tonic-gate goto out; 49387c478bd9Sstevel@tonic-gate } 49397c478bd9Sstevel@tonic-gate 49407c478bd9Sstevel@tonic-gate /* 49417c478bd9Sstevel@tonic-gate * To prevent processes in a zone from holding contracts on 49427c478bd9Sstevel@tonic-gate * extrazonal resources, and to avoid process contract 49437c478bd9Sstevel@tonic-gate * memberships which span zones, contract holders and processes 49447c478bd9Sstevel@tonic-gate * which aren't the sole members of their encapsulating process 49457c478bd9Sstevel@tonic-gate * contracts are not allowed to zone_enter. 49467c478bd9Sstevel@tonic-gate */ 49477c478bd9Sstevel@tonic-gate ctp = pp->p_ct_process; 49487c478bd9Sstevel@tonic-gate ct = &ctp->conp_contract; 49497c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 49507c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 49517c478bd9Sstevel@tonic-gate if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 49527c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 49537c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 49547c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 49557c478bd9Sstevel@tonic-gate err = EINVAL; 49567c478bd9Sstevel@tonic-gate goto out; 49577c478bd9Sstevel@tonic-gate } 49587c478bd9Sstevel@tonic-gate 49597c478bd9Sstevel@tonic-gate /* 49607c478bd9Sstevel@tonic-gate * Moreover, we don't allow processes whose encapsulating 49617c478bd9Sstevel@tonic-gate * process contracts have inherited extrazonal contracts. 49627c478bd9Sstevel@tonic-gate * While it would be easier to eliminate all process contracts 49637c478bd9Sstevel@tonic-gate * with inherited contracts, we need to be able to give a 49647c478bd9Sstevel@tonic-gate * restarted init (or other zone-penetrating process) its 49657c478bd9Sstevel@tonic-gate * predecessor's contracts. 49667c478bd9Sstevel@tonic-gate */ 49677c478bd9Sstevel@tonic-gate if (ctp->conp_ninherited != 0) { 49687c478bd9Sstevel@tonic-gate contract_t *next; 49697c478bd9Sstevel@tonic-gate for (next = list_head(&ctp->conp_inherited); next; 49707c478bd9Sstevel@tonic-gate next = list_next(&ctp->conp_inherited, next)) { 49717c478bd9Sstevel@tonic-gate if (contract_getzuniqid(next) != zone->zone_uniqid) { 49727c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 49737c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 49747c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 49757c478bd9Sstevel@tonic-gate err = EINVAL; 49767c478bd9Sstevel@tonic-gate goto out; 49777c478bd9Sstevel@tonic-gate } 49787c478bd9Sstevel@tonic-gate } 49797c478bd9Sstevel@tonic-gate } 49807c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 49817c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 49827c478bd9Sstevel@tonic-gate 49837c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 49847c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 49857c478bd9Sstevel@tonic-gate /* 49867c478bd9Sstevel@tonic-gate * Can't join 49877c478bd9Sstevel@tonic-gate */ 49887c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 49897c478bd9Sstevel@tonic-gate err = EINVAL; 49907c478bd9Sstevel@tonic-gate goto out; 49917c478bd9Sstevel@tonic-gate } 49927c478bd9Sstevel@tonic-gate 49937c478bd9Sstevel@tonic-gate /* 49947c478bd9Sstevel@tonic-gate * Make sure new priv set is within the permitted set for caller 49957c478bd9Sstevel@tonic-gate */ 49967c478bd9Sstevel@tonic-gate if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 49977c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 49987c478bd9Sstevel@tonic-gate err = EPERM; 49997c478bd9Sstevel@tonic-gate goto out; 50007c478bd9Sstevel@tonic-gate } 50017c478bd9Sstevel@tonic-gate /* 50027c478bd9Sstevel@tonic-gate * We want to momentarily drop zonehash_lock while we optimistically 50037c478bd9Sstevel@tonic-gate * bind curproc to the pool it should be running in. This is safe 50047c478bd9Sstevel@tonic-gate * since the zone can't disappear (we have a hold on it). 50057c478bd9Sstevel@tonic-gate */ 50067c478bd9Sstevel@tonic-gate zone_hold(zone); 50077c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 50087c478bd9Sstevel@tonic-gate 50097c478bd9Sstevel@tonic-gate /* 50107c478bd9Sstevel@tonic-gate * Grab pool_lock to keep the pools configuration from changing 50117c478bd9Sstevel@tonic-gate * and to stop ourselves from getting rebound to another pool 50127c478bd9Sstevel@tonic-gate * until we join the zone. 50137c478bd9Sstevel@tonic-gate */ 50147c478bd9Sstevel@tonic-gate if (pool_lock_intr() != 0) { 50157c478bd9Sstevel@tonic-gate zone_rele(zone); 50167c478bd9Sstevel@tonic-gate err = EINTR; 50177c478bd9Sstevel@tonic-gate goto out; 50187c478bd9Sstevel@tonic-gate } 50197c478bd9Sstevel@tonic-gate ASSERT(secpolicy_pool(CRED()) == 0); 50207c478bd9Sstevel@tonic-gate /* 50217c478bd9Sstevel@tonic-gate * Bind ourselves to the pool currently associated with the zone. 50227c478bd9Sstevel@tonic-gate */ 50237c478bd9Sstevel@tonic-gate oldpool = curproc->p_pool; 50247c478bd9Sstevel@tonic-gate newpool = zone_pool_get(zone); 50257c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED && newpool != oldpool && 50267c478bd9Sstevel@tonic-gate (err = pool_do_bind(newpool, P_PID, P_MYID, 50277c478bd9Sstevel@tonic-gate POOL_BIND_ALL)) != 0) { 50287c478bd9Sstevel@tonic-gate pool_unlock(); 50297c478bd9Sstevel@tonic-gate zone_rele(zone); 50307c478bd9Sstevel@tonic-gate goto out; 50317c478bd9Sstevel@tonic-gate } 50327c478bd9Sstevel@tonic-gate 50337c478bd9Sstevel@tonic-gate /* 50347c478bd9Sstevel@tonic-gate * Grab cpu_lock now; we'll need it later when we call 50357c478bd9Sstevel@tonic-gate * task_join(). 50367c478bd9Sstevel@tonic-gate */ 50377c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 50387c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 50397c478bd9Sstevel@tonic-gate /* 50407c478bd9Sstevel@tonic-gate * Make sure the zone hasn't moved on since we dropped zonehash_lock. 50417c478bd9Sstevel@tonic-gate */ 50427c478bd9Sstevel@tonic-gate if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 50437c478bd9Sstevel@tonic-gate /* 50447c478bd9Sstevel@tonic-gate * Can't join anymore. 50457c478bd9Sstevel@tonic-gate */ 50467c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 50477c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 50487c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED && 50497c478bd9Sstevel@tonic-gate newpool != oldpool) 50507c478bd9Sstevel@tonic-gate (void) pool_do_bind(oldpool, P_PID, P_MYID, 50517c478bd9Sstevel@tonic-gate POOL_BIND_ALL); 50527c478bd9Sstevel@tonic-gate pool_unlock(); 50537c478bd9Sstevel@tonic-gate zone_rele(zone); 50547c478bd9Sstevel@tonic-gate err = EINVAL; 50557c478bd9Sstevel@tonic-gate goto out; 50567c478bd9Sstevel@tonic-gate } 50577c478bd9Sstevel@tonic-gate 50580209230bSgjelinek /* 50590209230bSgjelinek * a_lock must be held while transfering locked memory and swap 50600209230bSgjelinek * reservation from the global zone to the non global zone because 50610209230bSgjelinek * asynchronous faults on the processes' address space can lock 50620209230bSgjelinek * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE 50630209230bSgjelinek * segments respectively. 50640209230bSgjelinek */ 50650209230bSgjelinek AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER); 50660209230bSgjelinek swap = as_swresv(); 50677c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 50687c478bd9Sstevel@tonic-gate zone_proj0 = zone->zone_zsched->p_task->tk_proj; 50697c478bd9Sstevel@tonic-gate /* verify that we do not exceed and task or lwp limits */ 50707c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 50717c478bd9Sstevel@tonic-gate /* add new lwps to zone and zone's proj0 */ 50727c478bd9Sstevel@tonic-gate zone_proj0->kpj_nlwps += pp->p_lwpcnt; 50737c478bd9Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 50747c478bd9Sstevel@tonic-gate /* add 1 task to zone's proj0 */ 50757c478bd9Sstevel@tonic-gate zone_proj0->kpj_ntasks += 1; 50767c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 50777c478bd9Sstevel@tonic-gate 50780209230bSgjelinek mutex_enter(&zone->zone_mem_lock); 5079c6939658Ssl zone->zone_locked_mem += pp->p_locked_mem; 5080c6939658Ssl zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem; 50810209230bSgjelinek zone->zone_max_swap += swap; 50820209230bSgjelinek mutex_exit(&zone->zone_mem_lock); 5083c6939658Ssl 5084c1a9a9c3Skrishna mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock)); 5085c1a9a9c3Skrishna zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem; 5086c1a9a9c3Skrishna mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock)); 5087c1a9a9c3Skrishna 50887c478bd9Sstevel@tonic-gate /* remove lwps from proc's old zone and old project */ 50897c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_zone->zone_nlwps_lock); 50907c478bd9Sstevel@tonic-gate pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 50917c478bd9Sstevel@tonic-gate pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 50927c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_zone->zone_nlwps_lock); 50937c478bd9Sstevel@tonic-gate 50940209230bSgjelinek mutex_enter(&pp->p_zone->zone_mem_lock); 5095c6939658Ssl pp->p_zone->zone_locked_mem -= pp->p_locked_mem; 5096c6939658Ssl pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 50970209230bSgjelinek pp->p_zone->zone_max_swap -= swap; 50980209230bSgjelinek mutex_exit(&pp->p_zone->zone_mem_lock); 5099c6939658Ssl 5100c1a9a9c3Skrishna mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 5101c1a9a9c3Skrishna pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem; 5102c1a9a9c3Skrishna mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 5103c1a9a9c3Skrishna 5104c6939658Ssl mutex_exit(&pp->p_lock); 51050209230bSgjelinek AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock); 5106c6939658Ssl 51077c478bd9Sstevel@tonic-gate /* 51087c478bd9Sstevel@tonic-gate * Joining the zone cannot fail from now on. 51097c478bd9Sstevel@tonic-gate * 51107c478bd9Sstevel@tonic-gate * This means that a lot of the following code can be commonized and 51117c478bd9Sstevel@tonic-gate * shared with zsched(). 51127c478bd9Sstevel@tonic-gate */ 51137c478bd9Sstevel@tonic-gate 51147c478bd9Sstevel@tonic-gate /* 51157c478bd9Sstevel@tonic-gate * Reset the encapsulating process contract's zone. 51167c478bd9Sstevel@tonic-gate */ 51177c478bd9Sstevel@tonic-gate ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 51187c478bd9Sstevel@tonic-gate contract_setzuniqid(ct, zone->zone_uniqid); 51197c478bd9Sstevel@tonic-gate 51207c478bd9Sstevel@tonic-gate /* 51217c478bd9Sstevel@tonic-gate * Create a new task and associate the process with the project keyed 51227c478bd9Sstevel@tonic-gate * by (projid,zoneid). 51237c478bd9Sstevel@tonic-gate * 51247c478bd9Sstevel@tonic-gate * We might as well be in project 0; the global zone's projid doesn't 51257c478bd9Sstevel@tonic-gate * make much sense in a zone anyhow. 51267c478bd9Sstevel@tonic-gate * 51277c478bd9Sstevel@tonic-gate * This also increments zone_ntasks, and returns with p_lock held. 51287c478bd9Sstevel@tonic-gate */ 51297c478bd9Sstevel@tonic-gate tk = task_create(0, zone); 51307c478bd9Sstevel@tonic-gate oldtk = task_join(tk, 0); 51317c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 51327c478bd9Sstevel@tonic-gate 51337c478bd9Sstevel@tonic-gate pp->p_flag |= SZONETOP; 51347c478bd9Sstevel@tonic-gate pp->p_zone = zone; 51357c478bd9Sstevel@tonic-gate 51367c478bd9Sstevel@tonic-gate /* 51377c478bd9Sstevel@tonic-gate * call RCTLOP_SET functions on this proc 51387c478bd9Sstevel@tonic-gate */ 51397c478bd9Sstevel@tonic-gate e.rcep_p.zone = zone; 51407c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 51417c478bd9Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 51427c478bd9Sstevel@tonic-gate RCD_CALLBACK); 51437c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 51447c478bd9Sstevel@tonic-gate 51457c478bd9Sstevel@tonic-gate /* 51467c478bd9Sstevel@tonic-gate * We don't need to hold any of zsched's locks here; not only do we know 51477c478bd9Sstevel@tonic-gate * the process and zone aren't going away, we know its session isn't 51487c478bd9Sstevel@tonic-gate * changing either. 51497c478bd9Sstevel@tonic-gate * 51507c478bd9Sstevel@tonic-gate * By joining zsched's session here, we mimic the behavior in the 51517c478bd9Sstevel@tonic-gate * global zone of init's sid being the pid of sched. We extend this 51527c478bd9Sstevel@tonic-gate * to all zlogin-like zone_enter()'ing processes as well. 51537c478bd9Sstevel@tonic-gate */ 51547c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 51557c478bd9Sstevel@tonic-gate sp = zone->zone_zsched->p_sessp; 51569acbbeafSnn sess_hold(zone->zone_zsched); 51577c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 51587c478bd9Sstevel@tonic-gate pgexit(pp); 51599acbbeafSnn sess_rele(pp->p_sessp, B_TRUE); 51607c478bd9Sstevel@tonic-gate pp->p_sessp = sp; 51617c478bd9Sstevel@tonic-gate pgjoin(pp, zone->zone_zsched->p_pidp); 51620209230bSgjelinek 5163c97ad5cdSakolb /* 5164c97ad5cdSakolb * If any threads are scheduled to be placed on zone wait queue they 5165c97ad5cdSakolb * should abandon the idea since the wait queue is changing. 5166c97ad5cdSakolb * We need to be holding pidlock & p_lock to do this. 5167c97ad5cdSakolb */ 5168c97ad5cdSakolb if ((t = pp->p_tlist) != NULL) { 5169c97ad5cdSakolb do { 5170c97ad5cdSakolb thread_lock(t); 5171c97ad5cdSakolb /* 5172c97ad5cdSakolb * Kick this thread so that he doesn't sit 5173c97ad5cdSakolb * on a wrong wait queue. 5174c97ad5cdSakolb */ 5175c97ad5cdSakolb if (ISWAITING(t)) 5176c97ad5cdSakolb setrun_locked(t); 5177c97ad5cdSakolb 5178c97ad5cdSakolb if (t->t_schedflag & TS_ANYWAITQ) 5179c97ad5cdSakolb t->t_schedflag &= ~ TS_ANYWAITQ; 5180c97ad5cdSakolb 5181c97ad5cdSakolb thread_unlock(t); 5182c97ad5cdSakolb } while ((t = t->t_forw) != pp->p_tlist); 5183c97ad5cdSakolb } 5184c97ad5cdSakolb 51850209230bSgjelinek /* 51860209230bSgjelinek * If there is a default scheduling class for the zone and it is not 51870209230bSgjelinek * the class we are currently in, change all of the threads in the 51880209230bSgjelinek * process to the new class. We need to be holding pidlock & p_lock 51890209230bSgjelinek * when we call parmsset so this is a good place to do it. 51900209230bSgjelinek */ 51910209230bSgjelinek if (zone->zone_defaultcid > 0 && 51920209230bSgjelinek zone->zone_defaultcid != curthread->t_cid) { 51930209230bSgjelinek pcparms_t pcparms; 51940209230bSgjelinek 51950209230bSgjelinek pcparms.pc_cid = zone->zone_defaultcid; 51960209230bSgjelinek pcparms.pc_clparms[0] = 0; 51970209230bSgjelinek 51980209230bSgjelinek /* 51990209230bSgjelinek * If setting the class fails, we still want to enter the zone. 52000209230bSgjelinek */ 52010209230bSgjelinek if ((t = pp->p_tlist) != NULL) { 52020209230bSgjelinek do { 52030209230bSgjelinek (void) parmsset(&pcparms, t); 52040209230bSgjelinek } while ((t = t->t_forw) != pp->p_tlist); 52050209230bSgjelinek } 52060209230bSgjelinek } 52070209230bSgjelinek 52087c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 52097c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 52107c478bd9Sstevel@tonic-gate 52117c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 52127c478bd9Sstevel@tonic-gate /* 52137c478bd9Sstevel@tonic-gate * We're firmly in the zone; let pools progress. 52147c478bd9Sstevel@tonic-gate */ 52157c478bd9Sstevel@tonic-gate pool_unlock(); 52167c478bd9Sstevel@tonic-gate task_rele(oldtk); 52177c478bd9Sstevel@tonic-gate /* 52187c478bd9Sstevel@tonic-gate * We don't need to retain a hold on the zone since we already 52197c478bd9Sstevel@tonic-gate * incremented zone_ntasks, so the zone isn't going anywhere. 52207c478bd9Sstevel@tonic-gate */ 52217c478bd9Sstevel@tonic-gate zone_rele(zone); 52227c478bd9Sstevel@tonic-gate 52237c478bd9Sstevel@tonic-gate /* 52247c478bd9Sstevel@tonic-gate * Chroot 52257c478bd9Sstevel@tonic-gate */ 52267c478bd9Sstevel@tonic-gate vp = zone->zone_rootvp; 52277c478bd9Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 52287c478bd9Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 52297c478bd9Sstevel@tonic-gate 52307c478bd9Sstevel@tonic-gate /* 52317c478bd9Sstevel@tonic-gate * Change process credentials 52327c478bd9Sstevel@tonic-gate */ 52337c478bd9Sstevel@tonic-gate newcr = cralloc(); 52347c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 52357c478bd9Sstevel@tonic-gate cr = pp->p_cred; 52367c478bd9Sstevel@tonic-gate crcopy_to(cr, newcr); 52377c478bd9Sstevel@tonic-gate crsetzone(newcr, zone); 52387c478bd9Sstevel@tonic-gate pp->p_cred = newcr; 52397c478bd9Sstevel@tonic-gate 52407c478bd9Sstevel@tonic-gate /* 52417c478bd9Sstevel@tonic-gate * Restrict all process privilege sets to zone limit 52427c478bd9Sstevel@tonic-gate */ 52437c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 52447c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 52457c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 52467c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 52477c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 52487c478bd9Sstevel@tonic-gate crset(pp, newcr); 52497c478bd9Sstevel@tonic-gate 52507c478bd9Sstevel@tonic-gate /* 52517c478bd9Sstevel@tonic-gate * Adjust upcount to reflect zone entry. 52527c478bd9Sstevel@tonic-gate */ 52537c478bd9Sstevel@tonic-gate uid = crgetruid(newcr); 52547c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 52557c478bd9Sstevel@tonic-gate upcount_dec(uid, GLOBAL_ZONEID); 52567c478bd9Sstevel@tonic-gate upcount_inc(uid, zoneid); 52577c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 52587c478bd9Sstevel@tonic-gate 52597c478bd9Sstevel@tonic-gate /* 52607c478bd9Sstevel@tonic-gate * Set up core file path and content. 52617c478bd9Sstevel@tonic-gate */ 52627c478bd9Sstevel@tonic-gate set_core_defaults(); 52637c478bd9Sstevel@tonic-gate 52647c478bd9Sstevel@tonic-gate out: 52657c478bd9Sstevel@tonic-gate /* 52667c478bd9Sstevel@tonic-gate * Let the other lwps continue. 52677c478bd9Sstevel@tonic-gate */ 52687c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 52697c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 52707c478bd9Sstevel@tonic-gate continuelwps(pp); 52717c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 52727c478bd9Sstevel@tonic-gate 52737c478bd9Sstevel@tonic-gate return (err != 0 ? set_errno(err) : 0); 52747c478bd9Sstevel@tonic-gate } 52757c478bd9Sstevel@tonic-gate 52767c478bd9Sstevel@tonic-gate /* 52777c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_list(2). 52787c478bd9Sstevel@tonic-gate * 52797c478bd9Sstevel@tonic-gate * Processes running in a (non-global) zone only see themselves. 528045916cd2Sjpk * On labeled systems, they see all zones whose label they dominate. 52817c478bd9Sstevel@tonic-gate */ 52827c478bd9Sstevel@tonic-gate static int 52837c478bd9Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones) 52847c478bd9Sstevel@tonic-gate { 52857c478bd9Sstevel@tonic-gate zoneid_t *zoneids; 528648451833Scarlsonj zone_t *zone, *myzone; 52877c478bd9Sstevel@tonic-gate uint_t user_nzones, real_nzones; 528845916cd2Sjpk uint_t domi_nzones; 528945916cd2Sjpk int error; 52907c478bd9Sstevel@tonic-gate 52917c478bd9Sstevel@tonic-gate if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 52927c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 52937c478bd9Sstevel@tonic-gate 529448451833Scarlsonj myzone = curproc->p_zone; 529548451833Scarlsonj if (myzone != global_zone) { 529645916cd2Sjpk bslabel_t *mybslab; 529745916cd2Sjpk 529845916cd2Sjpk if (!is_system_labeled()) { 529945916cd2Sjpk /* just return current zone */ 530045916cd2Sjpk real_nzones = domi_nzones = 1; 530145916cd2Sjpk zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 530248451833Scarlsonj zoneids[0] = myzone->zone_id; 530345916cd2Sjpk } else { 530445916cd2Sjpk /* return all zones that are dominated */ 530545916cd2Sjpk mutex_enter(&zonehash_lock); 530645916cd2Sjpk real_nzones = zonecount; 530745916cd2Sjpk domi_nzones = 0; 530845916cd2Sjpk if (real_nzones > 0) { 530945916cd2Sjpk zoneids = kmem_alloc(real_nzones * 531045916cd2Sjpk sizeof (zoneid_t), KM_SLEEP); 531148451833Scarlsonj mybslab = label2bslabel(myzone->zone_slabel); 531245916cd2Sjpk for (zone = list_head(&zone_active); 531345916cd2Sjpk zone != NULL; 531445916cd2Sjpk zone = list_next(&zone_active, zone)) { 531545916cd2Sjpk if (zone->zone_id == GLOBAL_ZONEID) 531645916cd2Sjpk continue; 531748451833Scarlsonj if (zone != myzone && 531848451833Scarlsonj (zone->zone_flags & ZF_IS_SCRATCH)) 531948451833Scarlsonj continue; 532048451833Scarlsonj /* 532148451833Scarlsonj * Note that a label always dominates 532248451833Scarlsonj * itself, so myzone is always included 532348451833Scarlsonj * in the list. 532448451833Scarlsonj */ 532545916cd2Sjpk if (bldominates(mybslab, 532645916cd2Sjpk label2bslabel(zone->zone_slabel))) { 532745916cd2Sjpk zoneids[domi_nzones++] = 532845916cd2Sjpk zone->zone_id; 532945916cd2Sjpk } 533045916cd2Sjpk } 533145916cd2Sjpk } 533245916cd2Sjpk mutex_exit(&zonehash_lock); 533345916cd2Sjpk } 53347c478bd9Sstevel@tonic-gate } else { 53357c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 53367c478bd9Sstevel@tonic-gate real_nzones = zonecount; 533745916cd2Sjpk domi_nzones = 0; 533845916cd2Sjpk if (real_nzones > 0) { 53397c478bd9Sstevel@tonic-gate zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 53407c478bd9Sstevel@tonic-gate KM_SLEEP); 53417c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 53427c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 534345916cd2Sjpk zoneids[domi_nzones++] = zone->zone_id; 534445916cd2Sjpk ASSERT(domi_nzones == real_nzones); 53457c478bd9Sstevel@tonic-gate } 53467c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 53477c478bd9Sstevel@tonic-gate } 53487c478bd9Sstevel@tonic-gate 534945916cd2Sjpk /* 535045916cd2Sjpk * If user has allocated space for fewer entries than we found, then 535145916cd2Sjpk * return only up to his limit. Either way, tell him exactly how many 535245916cd2Sjpk * we found. 535345916cd2Sjpk */ 535445916cd2Sjpk if (domi_nzones < user_nzones) 535545916cd2Sjpk user_nzones = domi_nzones; 535645916cd2Sjpk error = 0; 535745916cd2Sjpk if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) { 53587c478bd9Sstevel@tonic-gate error = EFAULT; 535945916cd2Sjpk } else if (zoneidlist != NULL && user_nzones != 0) { 53607c478bd9Sstevel@tonic-gate if (copyout(zoneids, zoneidlist, 53617c478bd9Sstevel@tonic-gate user_nzones * sizeof (zoneid_t)) != 0) 53627c478bd9Sstevel@tonic-gate error = EFAULT; 53637c478bd9Sstevel@tonic-gate } 53647c478bd9Sstevel@tonic-gate 536545916cd2Sjpk if (real_nzones > 0) 53667c478bd9Sstevel@tonic-gate kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 53677c478bd9Sstevel@tonic-gate 536845916cd2Sjpk if (error != 0) 53697c478bd9Sstevel@tonic-gate return (set_errno(error)); 53707c478bd9Sstevel@tonic-gate else 53717c478bd9Sstevel@tonic-gate return (0); 53727c478bd9Sstevel@tonic-gate } 53737c478bd9Sstevel@tonic-gate 53747c478bd9Sstevel@tonic-gate /* 53757c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_lookup(2). 53767c478bd9Sstevel@tonic-gate * 537745916cd2Sjpk * Non-global zones are only able to see themselves and (on labeled systems) 537845916cd2Sjpk * the zones they dominate. 53797c478bd9Sstevel@tonic-gate */ 53807c478bd9Sstevel@tonic-gate static zoneid_t 53817c478bd9Sstevel@tonic-gate zone_lookup(const char *zone_name) 53827c478bd9Sstevel@tonic-gate { 53837c478bd9Sstevel@tonic-gate char *kname; 53847c478bd9Sstevel@tonic-gate zone_t *zone; 53857c478bd9Sstevel@tonic-gate zoneid_t zoneid; 53867c478bd9Sstevel@tonic-gate int err; 53877c478bd9Sstevel@tonic-gate 53887c478bd9Sstevel@tonic-gate if (zone_name == NULL) { 53897c478bd9Sstevel@tonic-gate /* return caller's zone id */ 53907c478bd9Sstevel@tonic-gate return (getzoneid()); 53917c478bd9Sstevel@tonic-gate } 53927c478bd9Sstevel@tonic-gate 53937c478bd9Sstevel@tonic-gate kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 53947c478bd9Sstevel@tonic-gate if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 53957c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 53967c478bd9Sstevel@tonic-gate return (set_errno(err)); 53977c478bd9Sstevel@tonic-gate } 53987c478bd9Sstevel@tonic-gate 53997c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 54007c478bd9Sstevel@tonic-gate zone = zone_find_all_by_name(kname); 54017c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 540245916cd2Sjpk /* 540345916cd2Sjpk * In a non-global zone, can only lookup global and own name. 540445916cd2Sjpk * In Trusted Extensions zone label dominance rules apply. 540545916cd2Sjpk */ 540645916cd2Sjpk if (zone == NULL || 540745916cd2Sjpk zone_status_get(zone) < ZONE_IS_READY || 540845916cd2Sjpk !zone_list_access(zone)) { 54097c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 54107c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 541145916cd2Sjpk } else { 541245916cd2Sjpk zoneid = zone->zone_id; 541345916cd2Sjpk mutex_exit(&zonehash_lock); 541445916cd2Sjpk return (zoneid); 54157c478bd9Sstevel@tonic-gate } 54167c478bd9Sstevel@tonic-gate } 54177c478bd9Sstevel@tonic-gate 5418821c4a97Sdp static int 5419821c4a97Sdp zone_version(int *version_arg) 5420821c4a97Sdp { 5421821c4a97Sdp int version = ZONE_SYSCALL_API_VERSION; 5422821c4a97Sdp 5423821c4a97Sdp if (copyout(&version, version_arg, sizeof (int)) != 0) 5424821c4a97Sdp return (set_errno(EFAULT)); 5425821c4a97Sdp return (0); 5426821c4a97Sdp } 5427821c4a97Sdp 54287c478bd9Sstevel@tonic-gate /* ARGSUSED */ 54297c478bd9Sstevel@tonic-gate long 5430fa9e4066Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4) 54317c478bd9Sstevel@tonic-gate { 54327c478bd9Sstevel@tonic-gate zone_def zs; 54337c478bd9Sstevel@tonic-gate 54347c478bd9Sstevel@tonic-gate switch (cmd) { 54357c478bd9Sstevel@tonic-gate case ZONE_CREATE: 54367c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 54377c478bd9Sstevel@tonic-gate if (copyin(arg1, &zs, sizeof (zone_def))) { 54387c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 54397c478bd9Sstevel@tonic-gate } 54407c478bd9Sstevel@tonic-gate } else { 54417c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 54427c478bd9Sstevel@tonic-gate zone_def32 zs32; 54437c478bd9Sstevel@tonic-gate 54447c478bd9Sstevel@tonic-gate if (copyin(arg1, &zs32, sizeof (zone_def32))) { 54457c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 54467c478bd9Sstevel@tonic-gate } 54477c478bd9Sstevel@tonic-gate zs.zone_name = 54487c478bd9Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_name; 54497c478bd9Sstevel@tonic-gate zs.zone_root = 54507c478bd9Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_root; 54517c478bd9Sstevel@tonic-gate zs.zone_privs = 54527c478bd9Sstevel@tonic-gate (const struct priv_set *) 54537c478bd9Sstevel@tonic-gate (unsigned long)zs32.zone_privs; 54546f70df68Sdp zs.zone_privssz = zs32.zone_privssz; 54557c478bd9Sstevel@tonic-gate zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 54567c478bd9Sstevel@tonic-gate zs.rctlbufsz = zs32.rctlbufsz; 5457fa9e4066Sahrens zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf; 5458fa9e4066Sahrens zs.zfsbufsz = zs32.zfsbufsz; 54597c478bd9Sstevel@tonic-gate zs.extended_error = 54607c478bd9Sstevel@tonic-gate (int *)(unsigned long)zs32.extended_error; 546145916cd2Sjpk zs.match = zs32.match; 546245916cd2Sjpk zs.doi = zs32.doi; 546345916cd2Sjpk zs.label = (const bslabel_t *)(uintptr_t)zs32.label; 5464f4b3ec61Sdh zs.flags = zs32.flags; 54657c478bd9Sstevel@tonic-gate #else 54667c478bd9Sstevel@tonic-gate panic("get_udatamodel() returned bogus result\n"); 54677c478bd9Sstevel@tonic-gate #endif 54687c478bd9Sstevel@tonic-gate } 54697c478bd9Sstevel@tonic-gate 54707c478bd9Sstevel@tonic-gate return (zone_create(zs.zone_name, zs.zone_root, 5471821c4a97Sdp zs.zone_privs, zs.zone_privssz, 5472821c4a97Sdp (caddr_t)zs.rctlbuf, zs.rctlbufsz, 5473821c4a97Sdp (caddr_t)zs.zfsbuf, zs.zfsbufsz, 547445916cd2Sjpk zs.extended_error, zs.match, zs.doi, 5475f4b3ec61Sdh zs.label, zs.flags)); 54767c478bd9Sstevel@tonic-gate case ZONE_BOOT: 54773f2f09c1Sdp return (zone_boot((zoneid_t)(uintptr_t)arg1)); 54787c478bd9Sstevel@tonic-gate case ZONE_DESTROY: 54797c478bd9Sstevel@tonic-gate return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 54807c478bd9Sstevel@tonic-gate case ZONE_GETATTR: 54817c478bd9Sstevel@tonic-gate return (zone_getattr((zoneid_t)(uintptr_t)arg1, 54827c478bd9Sstevel@tonic-gate (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 54833f2f09c1Sdp case ZONE_SETATTR: 54843f2f09c1Sdp return (zone_setattr((zoneid_t)(uintptr_t)arg1, 54853f2f09c1Sdp (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 54867c478bd9Sstevel@tonic-gate case ZONE_ENTER: 54877c478bd9Sstevel@tonic-gate return (zone_enter((zoneid_t)(uintptr_t)arg1)); 54887c478bd9Sstevel@tonic-gate case ZONE_LIST: 54897c478bd9Sstevel@tonic-gate return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 54907c478bd9Sstevel@tonic-gate case ZONE_SHUTDOWN: 54917c478bd9Sstevel@tonic-gate return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 54927c478bd9Sstevel@tonic-gate case ZONE_LOOKUP: 54937c478bd9Sstevel@tonic-gate return (zone_lookup((const char *)arg1)); 5494821c4a97Sdp case ZONE_VERSION: 5495821c4a97Sdp return (zone_version((int *)arg1)); 5496f4b3ec61Sdh case ZONE_ADD_DATALINK: 5497f4b3ec61Sdh return (zone_add_datalink((zoneid_t)(uintptr_t)arg1, 5498f4b3ec61Sdh (char *)arg2)); 5499f4b3ec61Sdh case ZONE_DEL_DATALINK: 5500f4b3ec61Sdh return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1, 5501f4b3ec61Sdh (char *)arg2)); 5502f4b3ec61Sdh case ZONE_CHECK_DATALINK: 5503f4b3ec61Sdh return (zone_check_datalink((zoneid_t *)arg1, (char *)arg2)); 5504f4b3ec61Sdh case ZONE_LIST_DATALINK: 5505f4b3ec61Sdh return (zone_list_datalink((zoneid_t)(uintptr_t)arg1, 5506f4b3ec61Sdh (int *)arg2, (char *)arg3)); 55077c478bd9Sstevel@tonic-gate default: 55087c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 55097c478bd9Sstevel@tonic-gate } 55107c478bd9Sstevel@tonic-gate } 55117c478bd9Sstevel@tonic-gate 55127c478bd9Sstevel@tonic-gate struct zarg { 55137c478bd9Sstevel@tonic-gate zone_t *zone; 55147c478bd9Sstevel@tonic-gate zone_cmd_arg_t arg; 55157c478bd9Sstevel@tonic-gate }; 55167c478bd9Sstevel@tonic-gate 55177c478bd9Sstevel@tonic-gate static int 55187c478bd9Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp) 55197c478bd9Sstevel@tonic-gate { 55207c478bd9Sstevel@tonic-gate char *buf; 55217c478bd9Sstevel@tonic-gate size_t buflen; 55227c478bd9Sstevel@tonic-gate int error; 55237c478bd9Sstevel@tonic-gate 55247c478bd9Sstevel@tonic-gate buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 55257c478bd9Sstevel@tonic-gate buf = kmem_alloc(buflen, KM_SLEEP); 55267c478bd9Sstevel@tonic-gate (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 55277c478bd9Sstevel@tonic-gate error = door_ki_open(buf, doorp); 55287c478bd9Sstevel@tonic-gate kmem_free(buf, buflen); 55297c478bd9Sstevel@tonic-gate return (error); 55307c478bd9Sstevel@tonic-gate } 55317c478bd9Sstevel@tonic-gate 55327c478bd9Sstevel@tonic-gate static void 55337c478bd9Sstevel@tonic-gate zone_release_door(door_handle_t *doorp) 55347c478bd9Sstevel@tonic-gate { 55357c478bd9Sstevel@tonic-gate door_ki_rele(*doorp); 55367c478bd9Sstevel@tonic-gate *doorp = NULL; 55377c478bd9Sstevel@tonic-gate } 55387c478bd9Sstevel@tonic-gate 55397c478bd9Sstevel@tonic-gate static void 55407c478bd9Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp) 55417c478bd9Sstevel@tonic-gate { 55427c478bd9Sstevel@tonic-gate door_handle_t door = NULL; 55437c478bd9Sstevel@tonic-gate door_arg_t darg, save_arg; 55447c478bd9Sstevel@tonic-gate char *zone_name; 55457c478bd9Sstevel@tonic-gate size_t zone_namelen; 55467c478bd9Sstevel@tonic-gate zoneid_t zoneid; 55477c478bd9Sstevel@tonic-gate zone_t *zone; 55487c478bd9Sstevel@tonic-gate zone_cmd_arg_t arg; 55497c478bd9Sstevel@tonic-gate uint64_t uniqid; 55507c478bd9Sstevel@tonic-gate size_t size; 55517c478bd9Sstevel@tonic-gate int error; 55527c478bd9Sstevel@tonic-gate int retry; 55537c478bd9Sstevel@tonic-gate 55547c478bd9Sstevel@tonic-gate zone = zargp->zone; 55557c478bd9Sstevel@tonic-gate arg = zargp->arg; 55567c478bd9Sstevel@tonic-gate kmem_free(zargp, sizeof (*zargp)); 55577c478bd9Sstevel@tonic-gate 55587c478bd9Sstevel@tonic-gate zone_namelen = strlen(zone->zone_name) + 1; 55597c478bd9Sstevel@tonic-gate zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 55607c478bd9Sstevel@tonic-gate bcopy(zone->zone_name, zone_name, zone_namelen); 55617c478bd9Sstevel@tonic-gate zoneid = zone->zone_id; 55627c478bd9Sstevel@tonic-gate uniqid = zone->zone_uniqid; 55637c478bd9Sstevel@tonic-gate /* 55647c478bd9Sstevel@tonic-gate * zoneadmd may be down, but at least we can empty out the zone. 55657c478bd9Sstevel@tonic-gate * We can ignore the return value of zone_empty() since we're called 55667c478bd9Sstevel@tonic-gate * from a kernel thread and know we won't be delivered any signals. 55677c478bd9Sstevel@tonic-gate */ 55687c478bd9Sstevel@tonic-gate ASSERT(curproc == &p0); 55697c478bd9Sstevel@tonic-gate (void) zone_empty(zone); 55707c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 55717c478bd9Sstevel@tonic-gate zone_rele(zone); 55727c478bd9Sstevel@tonic-gate 55737c478bd9Sstevel@tonic-gate size = sizeof (arg); 55747c478bd9Sstevel@tonic-gate darg.rbuf = (char *)&arg; 55757c478bd9Sstevel@tonic-gate darg.data_ptr = (char *)&arg; 55767c478bd9Sstevel@tonic-gate darg.rsize = size; 55777c478bd9Sstevel@tonic-gate darg.data_size = size; 55787c478bd9Sstevel@tonic-gate darg.desc_ptr = NULL; 55797c478bd9Sstevel@tonic-gate darg.desc_num = 0; 55807c478bd9Sstevel@tonic-gate 55817c478bd9Sstevel@tonic-gate save_arg = darg; 55827c478bd9Sstevel@tonic-gate /* 55837c478bd9Sstevel@tonic-gate * Since we're not holding a reference to the zone, any number of 55847c478bd9Sstevel@tonic-gate * things can go wrong, including the zone disappearing before we get a 55857c478bd9Sstevel@tonic-gate * chance to talk to zoneadmd. 55867c478bd9Sstevel@tonic-gate */ 55877c478bd9Sstevel@tonic-gate for (retry = 0; /* forever */; retry++) { 55887c478bd9Sstevel@tonic-gate if (door == NULL && 55897c478bd9Sstevel@tonic-gate (error = zone_lookup_door(zone_name, &door)) != 0) { 55907c478bd9Sstevel@tonic-gate goto next; 55917c478bd9Sstevel@tonic-gate } 55927c478bd9Sstevel@tonic-gate ASSERT(door != NULL); 55937c478bd9Sstevel@tonic-gate 55947c478bd9Sstevel@tonic-gate if ((error = door_ki_upcall(door, &darg)) == 0) { 55957c478bd9Sstevel@tonic-gate break; 55967c478bd9Sstevel@tonic-gate } 55977c478bd9Sstevel@tonic-gate switch (error) { 55987c478bd9Sstevel@tonic-gate case EINTR: 55997c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 56007c478bd9Sstevel@tonic-gate case EAGAIN: /* process may be forking */ 56017c478bd9Sstevel@tonic-gate /* 56027c478bd9Sstevel@tonic-gate * Back off for a bit 56037c478bd9Sstevel@tonic-gate */ 56047c478bd9Sstevel@tonic-gate break; 56057c478bd9Sstevel@tonic-gate case EBADF: 56067c478bd9Sstevel@tonic-gate zone_release_door(&door); 56077c478bd9Sstevel@tonic-gate if (zone_lookup_door(zone_name, &door) != 0) { 56087c478bd9Sstevel@tonic-gate /* 56097c478bd9Sstevel@tonic-gate * zoneadmd may be dead, but it may come back to 56107c478bd9Sstevel@tonic-gate * life later. 56117c478bd9Sstevel@tonic-gate */ 56127c478bd9Sstevel@tonic-gate break; 56137c478bd9Sstevel@tonic-gate } 56147c478bd9Sstevel@tonic-gate break; 56157c478bd9Sstevel@tonic-gate default: 56167c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 56177c478bd9Sstevel@tonic-gate "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 56187c478bd9Sstevel@tonic-gate error); 56197c478bd9Sstevel@tonic-gate goto out; 56207c478bd9Sstevel@tonic-gate } 56217c478bd9Sstevel@tonic-gate next: 56227c478bd9Sstevel@tonic-gate /* 56237c478bd9Sstevel@tonic-gate * If this isn't the same zone_t that we originally had in mind, 56247c478bd9Sstevel@tonic-gate * then this is the same as if two kadmin requests come in at 56257c478bd9Sstevel@tonic-gate * the same time: the first one wins. This means we lose, so we 56267c478bd9Sstevel@tonic-gate * bail. 56277c478bd9Sstevel@tonic-gate */ 56287c478bd9Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) { 56297c478bd9Sstevel@tonic-gate /* 56307c478bd9Sstevel@tonic-gate * Problem is solved. 56317c478bd9Sstevel@tonic-gate */ 56327c478bd9Sstevel@tonic-gate break; 56337c478bd9Sstevel@tonic-gate } 56347c478bd9Sstevel@tonic-gate if (zone->zone_uniqid != uniqid) { 56357c478bd9Sstevel@tonic-gate /* 56367c478bd9Sstevel@tonic-gate * zoneid recycled 56377c478bd9Sstevel@tonic-gate */ 56387c478bd9Sstevel@tonic-gate zone_rele(zone); 56397c478bd9Sstevel@tonic-gate break; 56407c478bd9Sstevel@tonic-gate } 56417c478bd9Sstevel@tonic-gate /* 56427c478bd9Sstevel@tonic-gate * We could zone_status_timedwait(), but there doesn't seem to 56437c478bd9Sstevel@tonic-gate * be much point in doing that (plus, it would mean that 56447c478bd9Sstevel@tonic-gate * zone_free() isn't called until this thread exits). 56457c478bd9Sstevel@tonic-gate */ 56467c478bd9Sstevel@tonic-gate zone_rele(zone); 56477c478bd9Sstevel@tonic-gate delay(hz); 56487c478bd9Sstevel@tonic-gate darg = save_arg; 56497c478bd9Sstevel@tonic-gate } 56507c478bd9Sstevel@tonic-gate out: 56517c478bd9Sstevel@tonic-gate if (door != NULL) { 56527c478bd9Sstevel@tonic-gate zone_release_door(&door); 56537c478bd9Sstevel@tonic-gate } 56547c478bd9Sstevel@tonic-gate kmem_free(zone_name, zone_namelen); 56557c478bd9Sstevel@tonic-gate thread_exit(); 56567c478bd9Sstevel@tonic-gate } 56577c478bd9Sstevel@tonic-gate 56587c478bd9Sstevel@tonic-gate /* 56593f2f09c1Sdp * Entry point for uadmin() to tell the zone to go away or reboot. Analog to 56603f2f09c1Sdp * kadmin(). The caller is a process in the zone. 56617c478bd9Sstevel@tonic-gate * 56627c478bd9Sstevel@tonic-gate * In order to shutdown the zone, we will hand off control to zoneadmd 56637c478bd9Sstevel@tonic-gate * (running in the global zone) via a door. We do a half-hearted job at 56647c478bd9Sstevel@tonic-gate * killing all processes in the zone, create a kernel thread to contact 56657c478bd9Sstevel@tonic-gate * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 56667c478bd9Sstevel@tonic-gate * a form of generation number used to let zoneadmd (as well as 56677c478bd9Sstevel@tonic-gate * zone_destroy()) know exactly which zone they're re talking about. 56687c478bd9Sstevel@tonic-gate */ 56697c478bd9Sstevel@tonic-gate int 56703f2f09c1Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp) 56717c478bd9Sstevel@tonic-gate { 56727c478bd9Sstevel@tonic-gate struct zarg *zargp; 56737c478bd9Sstevel@tonic-gate zone_cmd_t zcmd; 56747c478bd9Sstevel@tonic-gate zone_t *zone; 56757c478bd9Sstevel@tonic-gate 56767c478bd9Sstevel@tonic-gate zone = curproc->p_zone; 56777c478bd9Sstevel@tonic-gate ASSERT(getzoneid() != GLOBAL_ZONEID); 56787c478bd9Sstevel@tonic-gate 56797c478bd9Sstevel@tonic-gate switch (cmd) { 56807c478bd9Sstevel@tonic-gate case A_SHUTDOWN: 56817c478bd9Sstevel@tonic-gate switch (fcn) { 56827c478bd9Sstevel@tonic-gate case AD_HALT: 56837c478bd9Sstevel@tonic-gate case AD_POWEROFF: 56847c478bd9Sstevel@tonic-gate zcmd = Z_HALT; 56857c478bd9Sstevel@tonic-gate break; 56867c478bd9Sstevel@tonic-gate case AD_BOOT: 56877c478bd9Sstevel@tonic-gate zcmd = Z_REBOOT; 56887c478bd9Sstevel@tonic-gate break; 56897c478bd9Sstevel@tonic-gate case AD_IBOOT: 56907c478bd9Sstevel@tonic-gate case AD_SBOOT: 56917c478bd9Sstevel@tonic-gate case AD_SIBOOT: 56927c478bd9Sstevel@tonic-gate case AD_NOSYNC: 56937c478bd9Sstevel@tonic-gate return (ENOTSUP); 56947c478bd9Sstevel@tonic-gate default: 56957c478bd9Sstevel@tonic-gate return (EINVAL); 56967c478bd9Sstevel@tonic-gate } 56977c478bd9Sstevel@tonic-gate break; 56987c478bd9Sstevel@tonic-gate case A_REBOOT: 56997c478bd9Sstevel@tonic-gate zcmd = Z_REBOOT; 57007c478bd9Sstevel@tonic-gate break; 57017c478bd9Sstevel@tonic-gate case A_FTRACE: 57027c478bd9Sstevel@tonic-gate case A_REMOUNT: 57037c478bd9Sstevel@tonic-gate case A_FREEZE: 57047c478bd9Sstevel@tonic-gate case A_DUMP: 57057c478bd9Sstevel@tonic-gate return (ENOTSUP); 57067c478bd9Sstevel@tonic-gate default: 57077c478bd9Sstevel@tonic-gate ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 57087c478bd9Sstevel@tonic-gate return (EINVAL); 57097c478bd9Sstevel@tonic-gate } 57107c478bd9Sstevel@tonic-gate 57117c478bd9Sstevel@tonic-gate if (secpolicy_zone_admin(credp, B_FALSE)) 57127c478bd9Sstevel@tonic-gate return (EPERM); 57137c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 57143f2f09c1Sdp 57157c478bd9Sstevel@tonic-gate /* 57167c478bd9Sstevel@tonic-gate * zone_status can't be ZONE_IS_EMPTY or higher since curproc 57177c478bd9Sstevel@tonic-gate * is in the zone. 57187c478bd9Sstevel@tonic-gate */ 57197c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 57207c478bd9Sstevel@tonic-gate if (zone_status_get(zone) > ZONE_IS_RUNNING) { 57217c478bd9Sstevel@tonic-gate /* 57227c478bd9Sstevel@tonic-gate * This zone is already on its way down. 57237c478bd9Sstevel@tonic-gate */ 57247c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 57257c478bd9Sstevel@tonic-gate return (0); 57267c478bd9Sstevel@tonic-gate } 57277c478bd9Sstevel@tonic-gate /* 57287c478bd9Sstevel@tonic-gate * Prevent future zone_enter()s 57297c478bd9Sstevel@tonic-gate */ 57307c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 57317c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 57327c478bd9Sstevel@tonic-gate 57337c478bd9Sstevel@tonic-gate /* 57347c478bd9Sstevel@tonic-gate * Kill everyone now and call zoneadmd later. 57357c478bd9Sstevel@tonic-gate * zone_ki_call_zoneadmd() will do a more thorough job of this 57367c478bd9Sstevel@tonic-gate * later. 57377c478bd9Sstevel@tonic-gate */ 57387c478bd9Sstevel@tonic-gate killall(zone->zone_id); 57397c478bd9Sstevel@tonic-gate /* 57407c478bd9Sstevel@tonic-gate * Now, create the thread to contact zoneadmd and do the rest of the 57417c478bd9Sstevel@tonic-gate * work. This thread can't be created in our zone otherwise 57427c478bd9Sstevel@tonic-gate * zone_destroy() would deadlock. 57437c478bd9Sstevel@tonic-gate */ 57443f2f09c1Sdp zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP); 57457c478bd9Sstevel@tonic-gate zargp->arg.cmd = zcmd; 57467c478bd9Sstevel@tonic-gate zargp->arg.uniqid = zone->zone_uniqid; 57473f2f09c1Sdp zargp->zone = zone; 57487c478bd9Sstevel@tonic-gate (void) strcpy(zargp->arg.locale, "C"); 57493f2f09c1Sdp /* mdep was already copied in for us by uadmin */ 57503f2f09c1Sdp if (mdep != NULL) 57513f2f09c1Sdp (void) strlcpy(zargp->arg.bootbuf, mdep, 57523f2f09c1Sdp sizeof (zargp->arg.bootbuf)); 57533f2f09c1Sdp zone_hold(zone); 57547c478bd9Sstevel@tonic-gate 57557c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 57567c478bd9Sstevel@tonic-gate TS_RUN, minclsyspri); 57577c478bd9Sstevel@tonic-gate exit(CLD_EXITED, 0); 57587c478bd9Sstevel@tonic-gate 57597c478bd9Sstevel@tonic-gate return (EINVAL); 57607c478bd9Sstevel@tonic-gate } 57617c478bd9Sstevel@tonic-gate 57627c478bd9Sstevel@tonic-gate /* 57637c478bd9Sstevel@tonic-gate * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 57647c478bd9Sstevel@tonic-gate * status to ZONE_IS_SHUTTING_DOWN. 57657c478bd9Sstevel@tonic-gate */ 57667c478bd9Sstevel@tonic-gate void 57677c478bd9Sstevel@tonic-gate zone_shutdown_global(void) 57687c478bd9Sstevel@tonic-gate { 57697c478bd9Sstevel@tonic-gate ASSERT(curproc->p_zone == global_zone); 57707c478bd9Sstevel@tonic-gate 57717c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 57727c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 57737c478bd9Sstevel@tonic-gate zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 57747c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 57757c478bd9Sstevel@tonic-gate } 5776fa9e4066Sahrens 5777fa9e4066Sahrens /* 5778fa9e4066Sahrens * Returns true if the named dataset is visible in the current zone. 5779fa9e4066Sahrens * The 'write' parameter is set to 1 if the dataset is also writable. 5780fa9e4066Sahrens */ 5781fa9e4066Sahrens int 5782fa9e4066Sahrens zone_dataset_visible(const char *dataset, int *write) 5783fa9e4066Sahrens { 5784fa9e4066Sahrens zone_dataset_t *zd; 5785fa9e4066Sahrens size_t len; 5786fa9e4066Sahrens zone_t *zone = curproc->p_zone; 5787fa9e4066Sahrens 5788fa9e4066Sahrens if (dataset[0] == '\0') 5789fa9e4066Sahrens return (0); 5790fa9e4066Sahrens 5791fa9e4066Sahrens /* 5792fa9e4066Sahrens * Walk the list once, looking for datasets which match exactly, or 5793fa9e4066Sahrens * specify a dataset underneath an exported dataset. If found, return 5794fa9e4066Sahrens * true and note that it is writable. 5795fa9e4066Sahrens */ 5796fa9e4066Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 5797fa9e4066Sahrens zd = list_next(&zone->zone_datasets, zd)) { 5798fa9e4066Sahrens 5799fa9e4066Sahrens len = strlen(zd->zd_dataset); 5800fa9e4066Sahrens if (strlen(dataset) >= len && 5801fa9e4066Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 580295c9592aSmaybee (dataset[len] == '\0' || dataset[len] == '/' || 580395c9592aSmaybee dataset[len] == '@')) { 5804fa9e4066Sahrens if (write) 5805fa9e4066Sahrens *write = 1; 5806fa9e4066Sahrens return (1); 5807fa9e4066Sahrens } 5808fa9e4066Sahrens } 5809fa9e4066Sahrens 5810fa9e4066Sahrens /* 5811fa9e4066Sahrens * Walk the list a second time, searching for datasets which are parents 5812fa9e4066Sahrens * of exported datasets. These should be visible, but read-only. 5813fa9e4066Sahrens * 5814fa9e4066Sahrens * Note that we also have to support forms such as 'pool/dataset/', with 5815fa9e4066Sahrens * a trailing slash. 5816fa9e4066Sahrens */ 5817fa9e4066Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 5818fa9e4066Sahrens zd = list_next(&zone->zone_datasets, zd)) { 5819fa9e4066Sahrens 5820fa9e4066Sahrens len = strlen(dataset); 5821fa9e4066Sahrens if (dataset[len - 1] == '/') 5822fa9e4066Sahrens len--; /* Ignore trailing slash */ 5823fa9e4066Sahrens if (len < strlen(zd->zd_dataset) && 5824fa9e4066Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 5825fa9e4066Sahrens zd->zd_dataset[len] == '/') { 5826fa9e4066Sahrens if (write) 5827fa9e4066Sahrens *write = 0; 5828fa9e4066Sahrens return (1); 5829fa9e4066Sahrens } 5830fa9e4066Sahrens } 5831fa9e4066Sahrens 5832fa9e4066Sahrens return (0); 5833fa9e4066Sahrens } 583445916cd2Sjpk 583545916cd2Sjpk /* 583645916cd2Sjpk * zone_find_by_any_path() - 583745916cd2Sjpk * 583845916cd2Sjpk * kernel-private routine similar to zone_find_by_path(), but which 583945916cd2Sjpk * effectively compares against zone paths rather than zonerootpath 584045916cd2Sjpk * (i.e., the last component of zonerootpaths, which should be "root/", 584145916cd2Sjpk * are not compared.) This is done in order to accurately identify all 584245916cd2Sjpk * paths, whether zone-visible or not, including those which are parallel 584345916cd2Sjpk * to /root/, such as /dev/, /home/, etc... 584445916cd2Sjpk * 584545916cd2Sjpk * If the specified path does not fall under any zone path then global 584645916cd2Sjpk * zone is returned. 584745916cd2Sjpk * 584845916cd2Sjpk * The treat_abs parameter indicates whether the path should be treated as 584945916cd2Sjpk * an absolute path although it does not begin with "/". (This supports 585045916cd2Sjpk * nfs mount syntax such as host:any/path.) 585145916cd2Sjpk * 585245916cd2Sjpk * The caller is responsible for zone_rele of the returned zone. 585345916cd2Sjpk */ 585445916cd2Sjpk zone_t * 585545916cd2Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs) 585645916cd2Sjpk { 585745916cd2Sjpk zone_t *zone; 585845916cd2Sjpk int path_offset = 0; 585945916cd2Sjpk 586045916cd2Sjpk if (path == NULL) { 586145916cd2Sjpk zone_hold(global_zone); 586245916cd2Sjpk return (global_zone); 586345916cd2Sjpk } 586445916cd2Sjpk 586545916cd2Sjpk if (*path != '/') { 586645916cd2Sjpk ASSERT(treat_abs); 586745916cd2Sjpk path_offset = 1; 586845916cd2Sjpk } 586945916cd2Sjpk 587045916cd2Sjpk mutex_enter(&zonehash_lock); 587145916cd2Sjpk for (zone = list_head(&zone_active); zone != NULL; 587245916cd2Sjpk zone = list_next(&zone_active, zone)) { 587345916cd2Sjpk char *c; 587445916cd2Sjpk size_t pathlen; 58750f95d722Smp char *rootpath_start; 587645916cd2Sjpk 587745916cd2Sjpk if (zone == global_zone) /* skip global zone */ 587845916cd2Sjpk continue; 587945916cd2Sjpk 588045916cd2Sjpk /* scan backwards to find start of last component */ 588145916cd2Sjpk c = zone->zone_rootpath + zone->zone_rootpathlen - 2; 588245916cd2Sjpk do { 588345916cd2Sjpk c--; 588445916cd2Sjpk } while (*c != '/'); 588545916cd2Sjpk 58860f95d722Smp pathlen = c - zone->zone_rootpath + 1 - path_offset; 58870f95d722Smp rootpath_start = (zone->zone_rootpath + path_offset); 58880f95d722Smp if (strncmp(path, rootpath_start, pathlen) == 0) 588945916cd2Sjpk break; 589045916cd2Sjpk } 589145916cd2Sjpk if (zone == NULL) 589245916cd2Sjpk zone = global_zone; 589345916cd2Sjpk zone_hold(zone); 589445916cd2Sjpk mutex_exit(&zonehash_lock); 589545916cd2Sjpk return (zone); 589645916cd2Sjpk } 5897f4b3ec61Sdh 5898f4b3ec61Sdh /* List of data link names which are accessible from the zone */ 5899f4b3ec61Sdh struct dlnamelist { 5900f4b3ec61Sdh char dlnl_name[LIFNAMSIZ]; 5901f4b3ec61Sdh struct dlnamelist *dlnl_next; 5902f4b3ec61Sdh }; 5903f4b3ec61Sdh 5904f4b3ec61Sdh 5905f4b3ec61Sdh /* 5906f4b3ec61Sdh * Check whether the datalink name (dlname) itself is present. 5907f4b3ec61Sdh * Return true if found. 5908f4b3ec61Sdh */ 5909f4b3ec61Sdh static boolean_t 5910f4b3ec61Sdh zone_dlname(zone_t *zone, char *dlname) 5911f4b3ec61Sdh { 5912f4b3ec61Sdh struct dlnamelist *dlnl; 5913f4b3ec61Sdh boolean_t found = B_FALSE; 5914f4b3ec61Sdh 5915f4b3ec61Sdh mutex_enter(&zone->zone_lock); 5916f4b3ec61Sdh for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) { 5917f4b3ec61Sdh if (strncmp(dlnl->dlnl_name, dlname, LIFNAMSIZ) == 0) { 5918f4b3ec61Sdh found = B_TRUE; 5919f4b3ec61Sdh break; 5920f4b3ec61Sdh } 5921f4b3ec61Sdh } 5922f4b3ec61Sdh mutex_exit(&zone->zone_lock); 5923f4b3ec61Sdh return (found); 5924f4b3ec61Sdh } 5925f4b3ec61Sdh 5926f4b3ec61Sdh /* 5927f4b3ec61Sdh * Add an data link name for the zone. Does not check for duplicates. 5928f4b3ec61Sdh */ 5929f4b3ec61Sdh static int 5930f4b3ec61Sdh zone_add_datalink(zoneid_t zoneid, char *dlname) 5931f4b3ec61Sdh { 5932f4b3ec61Sdh struct dlnamelist *dlnl; 5933f4b3ec61Sdh zone_t *zone; 5934f4b3ec61Sdh zone_t *thiszone; 5935f4b3ec61Sdh int err; 5936f4b3ec61Sdh 5937f4b3ec61Sdh dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP); 5938f4b3ec61Sdh if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) { 5939f4b3ec61Sdh kmem_free(dlnl, sizeof (struct dlnamelist)); 5940f4b3ec61Sdh return (set_errno(err)); 5941f4b3ec61Sdh } 5942f4b3ec61Sdh 5943f4b3ec61Sdh thiszone = zone_find_by_id(zoneid); 5944f4b3ec61Sdh if (thiszone == NULL) { 5945f4b3ec61Sdh kmem_free(dlnl, sizeof (struct dlnamelist)); 5946f4b3ec61Sdh return (set_errno(ENXIO)); 5947f4b3ec61Sdh } 5948f4b3ec61Sdh 5949f4b3ec61Sdh /* 5950f4b3ec61Sdh * Verify that the datalink name isn't already used by a different 5951f4b3ec61Sdh * zone while allowing duplicate entries for the same zone (e.g. due 5952f4b3ec61Sdh * to both using IPv4 and IPv6 on an interface) 5953f4b3ec61Sdh */ 5954f4b3ec61Sdh mutex_enter(&zonehash_lock); 5955f4b3ec61Sdh for (zone = list_head(&zone_active); zone != NULL; 5956f4b3ec61Sdh zone = list_next(&zone_active, zone)) { 5957f4b3ec61Sdh if (zone->zone_id == zoneid) 5958f4b3ec61Sdh continue; 5959f4b3ec61Sdh 5960f4b3ec61Sdh if (zone_dlname(zone, dlnl->dlnl_name)) { 5961f4b3ec61Sdh mutex_exit(&zonehash_lock); 5962f4b3ec61Sdh zone_rele(thiszone); 5963f4b3ec61Sdh kmem_free(dlnl, sizeof (struct dlnamelist)); 5964f4b3ec61Sdh return (set_errno(EPERM)); 5965f4b3ec61Sdh } 5966f4b3ec61Sdh } 5967f4b3ec61Sdh mutex_enter(&thiszone->zone_lock); 5968f4b3ec61Sdh dlnl->dlnl_next = thiszone->zone_dl_list; 5969f4b3ec61Sdh thiszone->zone_dl_list = dlnl; 5970f4b3ec61Sdh mutex_exit(&thiszone->zone_lock); 5971f4b3ec61Sdh mutex_exit(&zonehash_lock); 5972f4b3ec61Sdh zone_rele(thiszone); 5973f4b3ec61Sdh return (0); 5974f4b3ec61Sdh } 5975f4b3ec61Sdh 5976f4b3ec61Sdh static int 5977f4b3ec61Sdh zone_remove_datalink(zoneid_t zoneid, char *dlname) 5978f4b3ec61Sdh { 5979f4b3ec61Sdh struct dlnamelist *dlnl, *odlnl, **dlnlp; 5980f4b3ec61Sdh zone_t *zone; 5981f4b3ec61Sdh int err; 5982f4b3ec61Sdh 5983f4b3ec61Sdh dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP); 5984f4b3ec61Sdh if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) { 5985f4b3ec61Sdh kmem_free(dlnl, sizeof (struct dlnamelist)); 5986f4b3ec61Sdh return (set_errno(err)); 5987f4b3ec61Sdh } 5988f4b3ec61Sdh zone = zone_find_by_id(zoneid); 5989f4b3ec61Sdh if (zone == NULL) { 5990f4b3ec61Sdh kmem_free(dlnl, sizeof (struct dlnamelist)); 5991f4b3ec61Sdh return (set_errno(EINVAL)); 5992f4b3ec61Sdh } 5993f4b3ec61Sdh 5994f4b3ec61Sdh mutex_enter(&zone->zone_lock); 5995f4b3ec61Sdh /* Look for match */ 5996f4b3ec61Sdh dlnlp = &zone->zone_dl_list; 5997f4b3ec61Sdh while (*dlnlp != NULL) { 5998f4b3ec61Sdh if (strncmp(dlnl->dlnl_name, (*dlnlp)->dlnl_name, 5999f4b3ec61Sdh LIFNAMSIZ) == 0) 6000f4b3ec61Sdh goto found; 6001f4b3ec61Sdh dlnlp = &((*dlnlp)->dlnl_next); 6002f4b3ec61Sdh } 6003f4b3ec61Sdh mutex_exit(&zone->zone_lock); 6004f4b3ec61Sdh zone_rele(zone); 6005f4b3ec61Sdh kmem_free(dlnl, sizeof (struct dlnamelist)); 6006f4b3ec61Sdh return (set_errno(ENXIO)); 6007f4b3ec61Sdh 6008f4b3ec61Sdh found: 6009f4b3ec61Sdh odlnl = *dlnlp; 6010f4b3ec61Sdh *dlnlp = (*dlnlp)->dlnl_next; 6011f4b3ec61Sdh kmem_free(odlnl, sizeof (struct dlnamelist)); 6012f4b3ec61Sdh 6013f4b3ec61Sdh mutex_exit(&zone->zone_lock); 6014f4b3ec61Sdh zone_rele(zone); 6015f4b3ec61Sdh kmem_free(dlnl, sizeof (struct dlnamelist)); 6016f4b3ec61Sdh return (0); 6017f4b3ec61Sdh } 6018f4b3ec61Sdh 6019f4b3ec61Sdh /* 6020f4b3ec61Sdh * Using the zoneidp as ALL_ZONES, we can lookup which zone is using datalink 6021f4b3ec61Sdh * name (dlname); otherwise we just check if the specified zoneidp has access 6022f4b3ec61Sdh * to the datalink name. 6023f4b3ec61Sdh */ 6024f4b3ec61Sdh static int 6025f4b3ec61Sdh zone_check_datalink(zoneid_t *zoneidp, char *dlname) 6026f4b3ec61Sdh { 6027f4b3ec61Sdh zoneid_t id; 6028f4b3ec61Sdh char *dln; 6029f4b3ec61Sdh zone_t *zone; 6030f4b3ec61Sdh int err = 0; 6031f4b3ec61Sdh boolean_t allzones = B_FALSE; 6032f4b3ec61Sdh 6033f4b3ec61Sdh if (copyin(zoneidp, &id, sizeof (id)) != 0) { 6034f4b3ec61Sdh return (set_errno(EFAULT)); 6035f4b3ec61Sdh } 6036f4b3ec61Sdh dln = kmem_zalloc(LIFNAMSIZ, KM_SLEEP); 6037f4b3ec61Sdh if ((err = copyinstr(dlname, dln, LIFNAMSIZ, NULL)) != 0) { 6038f4b3ec61Sdh kmem_free(dln, LIFNAMSIZ); 6039f4b3ec61Sdh return (set_errno(err)); 6040f4b3ec61Sdh } 6041f4b3ec61Sdh 6042f4b3ec61Sdh if (id == ALL_ZONES) 6043f4b3ec61Sdh allzones = B_TRUE; 6044f4b3ec61Sdh 6045f4b3ec61Sdh /* 6046f4b3ec61Sdh * Check whether datalink name is already used. 6047f4b3ec61Sdh */ 6048f4b3ec61Sdh mutex_enter(&zonehash_lock); 6049f4b3ec61Sdh for (zone = list_head(&zone_active); zone != NULL; 6050f4b3ec61Sdh zone = list_next(&zone_active, zone)) { 6051f4b3ec61Sdh if (allzones || (id == zone->zone_id)) { 6052f4b3ec61Sdh if (!zone_dlname(zone, dln)) 6053f4b3ec61Sdh continue; 6054f4b3ec61Sdh if (allzones) 6055f4b3ec61Sdh err = copyout(&zone->zone_id, zoneidp, 6056f4b3ec61Sdh sizeof (*zoneidp)); 6057f4b3ec61Sdh 6058f4b3ec61Sdh mutex_exit(&zonehash_lock); 6059f4b3ec61Sdh kmem_free(dln, LIFNAMSIZ); 6060f4b3ec61Sdh return (err ? set_errno(EFAULT) : 0); 6061f4b3ec61Sdh } 6062f4b3ec61Sdh } 6063f4b3ec61Sdh 6064f4b3ec61Sdh /* datalink name is not found in any active zone. */ 6065f4b3ec61Sdh mutex_exit(&zonehash_lock); 6066f4b3ec61Sdh kmem_free(dln, LIFNAMSIZ); 6067f4b3ec61Sdh return (set_errno(ENXIO)); 6068f4b3ec61Sdh } 6069f4b3ec61Sdh 6070f4b3ec61Sdh /* 6071f4b3ec61Sdh * Get the names of the datalinks assigned to a zone. 6072f4b3ec61Sdh * Here *nump is the number of datalinks, and the assumption 6073da6c28aaSamw * is that the caller will guarantee that the the supplied buffer is 6074f4b3ec61Sdh * big enough to hold at least #*nump datalink names, that is, 6075f4b3ec61Sdh * LIFNAMSIZ X *nump 6076f4b3ec61Sdh * On return, *nump will be the "new" number of datalinks, if it 6077f4b3ec61Sdh * ever changed. 6078f4b3ec61Sdh */ 6079f4b3ec61Sdh static int 6080f4b3ec61Sdh zone_list_datalink(zoneid_t zoneid, int *nump, char *buf) 6081f4b3ec61Sdh { 6082f4b3ec61Sdh int num, dlcount; 6083f4b3ec61Sdh zone_t *zone; 6084f4b3ec61Sdh struct dlnamelist *dlnl; 6085f4b3ec61Sdh char *ptr; 6086f4b3ec61Sdh 6087f4b3ec61Sdh if (copyin(nump, &dlcount, sizeof (dlcount)) != 0) 6088f4b3ec61Sdh return (set_errno(EFAULT)); 6089f4b3ec61Sdh 6090f4b3ec61Sdh zone = zone_find_by_id(zoneid); 6091f4b3ec61Sdh if (zone == NULL) { 6092f4b3ec61Sdh return (set_errno(ENXIO)); 6093f4b3ec61Sdh } 6094f4b3ec61Sdh 6095f4b3ec61Sdh num = 0; 6096f4b3ec61Sdh mutex_enter(&zone->zone_lock); 6097f4b3ec61Sdh ptr = buf; 6098f4b3ec61Sdh for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) { 6099f4b3ec61Sdh /* 6100f4b3ec61Sdh * If the list changed and the new number is bigger 6101f4b3ec61Sdh * than what the caller supplied, just count, don't 6102f4b3ec61Sdh * do copyout 6103f4b3ec61Sdh */ 6104f4b3ec61Sdh if (++num > dlcount) 6105f4b3ec61Sdh continue; 6106f4b3ec61Sdh if (copyout(dlnl->dlnl_name, ptr, LIFNAMSIZ) != 0) { 6107f4b3ec61Sdh mutex_exit(&zone->zone_lock); 6108f4b3ec61Sdh zone_rele(zone); 6109f4b3ec61Sdh return (set_errno(EFAULT)); 6110f4b3ec61Sdh } 6111f4b3ec61Sdh ptr += LIFNAMSIZ; 6112f4b3ec61Sdh } 6113f4b3ec61Sdh mutex_exit(&zone->zone_lock); 6114f4b3ec61Sdh zone_rele(zone); 6115f4b3ec61Sdh 6116f4b3ec61Sdh /* Increased or decreased, caller should be notified. */ 6117f4b3ec61Sdh if (num != dlcount) { 6118f4b3ec61Sdh if (copyout(&num, nump, sizeof (num)) != 0) { 6119f4b3ec61Sdh return (set_errno(EFAULT)); 6120f4b3ec61Sdh } 6121f4b3ec61Sdh } 6122f4b3ec61Sdh return (0); 6123f4b3ec61Sdh } 6124f4b3ec61Sdh 6125f4b3ec61Sdh /* 6126f4b3ec61Sdh * Public interface for looking up a zone by zoneid. It's a customized version 6127*bd41d0a8Snordmark * for netstack_zone_create(). It can only be called from the zsd create 6128*bd41d0a8Snordmark * callbacks, since it doesn't have reference on the zone structure hence if 6129*bd41d0a8Snordmark * it is called elsewhere the zone could disappear after the zonehash_lock 6130*bd41d0a8Snordmark * is dropped. 6131*bd41d0a8Snordmark * 6132*bd41d0a8Snordmark * Furthermore it 6133*bd41d0a8Snordmark * 1. Doesn't check the status of the zone. 6134*bd41d0a8Snordmark * 2. It will be called even before zone_init is called, in that case the 6135f4b3ec61Sdh * address of zone0 is returned directly, and netstack_zone_create() 6136f4b3ec61Sdh * will only assign a value to zone0.zone_netstack, won't break anything. 6137*bd41d0a8Snordmark * 3. Returns without the zone being held. 6138f4b3ec61Sdh */ 6139f4b3ec61Sdh zone_t * 6140f4b3ec61Sdh zone_find_by_id_nolock(zoneid_t zoneid) 6141f4b3ec61Sdh { 6142*bd41d0a8Snordmark zone_t *zone; 6143f4b3ec61Sdh 6144*bd41d0a8Snordmark mutex_enter(&zonehash_lock); 6145f4b3ec61Sdh if (zonehashbyid == NULL) 6146*bd41d0a8Snordmark zone = &zone0; 6147f4b3ec61Sdh else 6148*bd41d0a8Snordmark zone = zone_find_all_by_id(zoneid); 6149*bd41d0a8Snordmark mutex_exit(&zonehash_lock); 6150*bd41d0a8Snordmark return (zone); 6151f4b3ec61Sdh } 6152