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 /* 23ae115bc7Smrj * Copyright 2007 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 * 647c478bd9Sstevel@tonic-gate * ZONE_IS_READY: zsched (the kernel dummy process for a zone) is 657c478bd9Sstevel@tonic-gate * ready. The zone is made visible after the ZSD constructor callbacks are 667c478bd9Sstevel@tonic-gate * executed. A zone remains in this state until it transitions into 677c478bd9Sstevel@tonic-gate * the ZONE_IS_BOOTING state as a result of a call to zone_boot(). 687c478bd9Sstevel@tonic-gate * 697c478bd9Sstevel@tonic-gate * ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start 707c478bd9Sstevel@tonic-gate * init. Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN 717c478bd9Sstevel@tonic-gate * state. 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * ZONE_IS_RUNNING: The zone is open for business: zsched has 747c478bd9Sstevel@tonic-gate * successfully started init. A zone remains in this state until 757c478bd9Sstevel@tonic-gate * zone_shutdown() is called. 767c478bd9Sstevel@tonic-gate * 777c478bd9Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is 787c478bd9Sstevel@tonic-gate * killing all processes running in the zone. The zone remains 797c478bd9Sstevel@tonic-gate * in this state until there are no more user processes running in the zone. 807c478bd9Sstevel@tonic-gate * zone_create(), zone_enter(), and zone_destroy() on this zone will fail. 817c478bd9Sstevel@tonic-gate * Since zone_shutdown() is restartable, it may be called successfully 827c478bd9Sstevel@tonic-gate * multiple times for the same zone_t. Setting of the zone's state to 837c478bd9Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check 847c478bd9Sstevel@tonic-gate * the zone's status without worrying about it being a moving target. 857c478bd9Sstevel@tonic-gate * 867c478bd9Sstevel@tonic-gate * ZONE_IS_EMPTY: zone_shutdown() has been called, and there 877c478bd9Sstevel@tonic-gate * are no more user processes in the zone. The zone remains in this 887c478bd9Sstevel@tonic-gate * state until there are no more kernel threads associated with the 897c478bd9Sstevel@tonic-gate * zone. zone_create(), zone_enter(), and zone_destroy() on this zone will 907c478bd9Sstevel@tonic-gate * fail. 917c478bd9Sstevel@tonic-gate * 927c478bd9Sstevel@tonic-gate * ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone 937c478bd9Sstevel@tonic-gate * have exited. zone_shutdown() returns. Henceforth it is not possible to 947c478bd9Sstevel@tonic-gate * join the zone or create kernel threads therein. 957c478bd9Sstevel@tonic-gate * 967c478bd9Sstevel@tonic-gate * ZONE_IS_DYING: zone_destroy() has been called on the zone; zone 977c478bd9Sstevel@tonic-gate * remains in this state until zsched exits. Calls to zone_find_by_*() 987c478bd9Sstevel@tonic-gate * return NULL from now on. 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate * ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0). There are no 1017c478bd9Sstevel@tonic-gate * processes or threads doing work on behalf of the zone. The zone is 1027c478bd9Sstevel@tonic-gate * removed from the list of active zones. zone_destroy() returns, and 1037c478bd9Sstevel@tonic-gate * the zone can be recreated. 1047c478bd9Sstevel@tonic-gate * 1057c478bd9Sstevel@tonic-gate * ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor 1067c478bd9Sstevel@tonic-gate * callbacks are executed, and all memory associated with the zone is 1077c478bd9Sstevel@tonic-gate * freed. 1087c478bd9Sstevel@tonic-gate * 1097c478bd9Sstevel@tonic-gate * Threads can wait for the zone to enter a requested state by using 1107c478bd9Sstevel@tonic-gate * zone_status_wait() or zone_status_timedwait() with the desired 1117c478bd9Sstevel@tonic-gate * state passed in as an argument. Zone state transitions are 1127c478bd9Sstevel@tonic-gate * uni-directional; it is not possible to move back to an earlier state. 1137c478bd9Sstevel@tonic-gate * 1147c478bd9Sstevel@tonic-gate * 1157c478bd9Sstevel@tonic-gate * Zone-Specific Data: 1167c478bd9Sstevel@tonic-gate * 1177c478bd9Sstevel@tonic-gate * Subsystems needing to maintain zone-specific data can store that 1187c478bd9Sstevel@tonic-gate * data using the ZSD mechanism. This provides a zone-specific data 1197c478bd9Sstevel@tonic-gate * store, similar to thread-specific data (see pthread_getspecific(3C) 1207c478bd9Sstevel@tonic-gate * or the TSD code in uts/common/disp/thread.c. Also, ZSD can be used 1217c478bd9Sstevel@tonic-gate * to register callbacks to be invoked when a zone is created, shut 1227c478bd9Sstevel@tonic-gate * down, or destroyed. This can be used to initialize zone-specific 1237c478bd9Sstevel@tonic-gate * data for new zones and to clean up when zones go away. 1247c478bd9Sstevel@tonic-gate * 1257c478bd9Sstevel@tonic-gate * 1267c478bd9Sstevel@tonic-gate * Data Structures: 1277c478bd9Sstevel@tonic-gate * 1287c478bd9Sstevel@tonic-gate * The per-zone structure (zone_t) is reference counted, and freed 1297c478bd9Sstevel@tonic-gate * when all references are released. zone_hold and zone_rele can be 1307c478bd9Sstevel@tonic-gate * used to adjust the reference count. In addition, reference counts 1317c478bd9Sstevel@tonic-gate * associated with the cred_t structure are tracked separately using 1327c478bd9Sstevel@tonic-gate * zone_cred_hold and zone_cred_rele. 1337c478bd9Sstevel@tonic-gate * 1347c478bd9Sstevel@tonic-gate * Pointers to active zone_t's are stored in two hash tables; one 1357c478bd9Sstevel@tonic-gate * for searching by id, the other for searching by name. Lookups 1367c478bd9Sstevel@tonic-gate * can be performed on either basis, using zone_find_by_id and 1377c478bd9Sstevel@tonic-gate * zone_find_by_name. Both return zone_t pointers with the zone 1387c478bd9Sstevel@tonic-gate * held, so zone_rele should be called when the pointer is no longer 1397c478bd9Sstevel@tonic-gate * needed. Zones can also be searched by path; zone_find_by_path 1407c478bd9Sstevel@tonic-gate * returns the zone with which a path name is associated (global 1417c478bd9Sstevel@tonic-gate * zone if the path is not within some other zone's file system 1427c478bd9Sstevel@tonic-gate * hierarchy). This currently requires iterating through each zone, 1437c478bd9Sstevel@tonic-gate * so it is slower than an id or name search via a hash table. 1447c478bd9Sstevel@tonic-gate * 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * Locking: 1477c478bd9Sstevel@tonic-gate * 1487c478bd9Sstevel@tonic-gate * zonehash_lock: This is a top-level global lock used to protect the 1497c478bd9Sstevel@tonic-gate * zone hash tables and lists. Zones cannot be created or destroyed 1507c478bd9Sstevel@tonic-gate * while this lock is held. 1517c478bd9Sstevel@tonic-gate * zone_status_lock: This is a global lock protecting zone state. 1527c478bd9Sstevel@tonic-gate * Zones cannot change state while this lock is held. It also 1537c478bd9Sstevel@tonic-gate * protects the list of kernel threads associated with a zone. 1547c478bd9Sstevel@tonic-gate * zone_lock: This is a per-zone lock used to protect several fields of 1557c478bd9Sstevel@tonic-gate * the zone_t (see <sys/zone.h> for details). In addition, holding 1567c478bd9Sstevel@tonic-gate * this lock means that the zone cannot go away. 1570209230bSgjelinek * zone_nlwps_lock: This is a per-zone lock used to protect the fields 1580209230bSgjelinek * related to the zone.max-lwps rctl. 1590209230bSgjelinek * zone_mem_lock: This is a per-zone lock used to protect the fields 1600209230bSgjelinek * related to the zone.max-locked-memory and zone.max-swap rctls. 1617c478bd9Sstevel@tonic-gate * zsd_key_lock: This is a global lock protecting the key state for ZSD. 1627c478bd9Sstevel@tonic-gate * zone_deathrow_lock: This is a global lock protecting the "deathrow" 1637c478bd9Sstevel@tonic-gate * list (a list of zones in the ZONE_IS_DEAD state). 1647c478bd9Sstevel@tonic-gate * 1657c478bd9Sstevel@tonic-gate * Ordering requirements: 1667c478bd9Sstevel@tonic-gate * pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock --> 1677c478bd9Sstevel@tonic-gate * zone_lock --> zsd_key_lock --> pidlock --> p_lock 1687c478bd9Sstevel@tonic-gate * 1690209230bSgjelinek * When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is: 1700209230bSgjelinek * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 1710209230bSgjelinek * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 1720209230bSgjelinek * 1737c478bd9Sstevel@tonic-gate * Blocking memory allocations are permitted while holding any of the 1747c478bd9Sstevel@tonic-gate * zone locks. 1757c478bd9Sstevel@tonic-gate * 1767c478bd9Sstevel@tonic-gate * 1777c478bd9Sstevel@tonic-gate * System Call Interface: 1787c478bd9Sstevel@tonic-gate * 1797c478bd9Sstevel@tonic-gate * The zone subsystem can be managed and queried from user level with 1807c478bd9Sstevel@tonic-gate * the following system calls (all subcodes of the primary "zone" 1817c478bd9Sstevel@tonic-gate * system call): 1827c478bd9Sstevel@tonic-gate * - zone_create: creates a zone with selected attributes (name, 183fa9e4066Sahrens * root path, privileges, resource controls, ZFS datasets) 1847c478bd9Sstevel@tonic-gate * - zone_enter: allows the current process to enter a zone 1857c478bd9Sstevel@tonic-gate * - zone_getattr: reports attributes of a zone 1863f2f09c1Sdp * - zone_setattr: set attributes of a zone 1873f2f09c1Sdp * - zone_boot: set 'init' running for the zone 1887c478bd9Sstevel@tonic-gate * - zone_list: lists all zones active in the system 1897c478bd9Sstevel@tonic-gate * - zone_lookup: looks up zone id based on name 1907c478bd9Sstevel@tonic-gate * - zone_shutdown: initiates shutdown process (see states above) 1917c478bd9Sstevel@tonic-gate * - zone_destroy: completes shutdown process (see states above) 1927c478bd9Sstevel@tonic-gate * 1937c478bd9Sstevel@tonic-gate */ 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h> 1967c478bd9Sstevel@tonic-gate #include <sys/cred.h> 1977c478bd9Sstevel@tonic-gate #include <c2/audit.h> 1987c478bd9Sstevel@tonic-gate #include <sys/debug.h> 1997c478bd9Sstevel@tonic-gate #include <sys/file.h> 2007c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 2010209230bSgjelinek #include <sys/kstat.h> 2027c478bd9Sstevel@tonic-gate #include <sys/mutex.h> 20345916cd2Sjpk #include <sys/note.h> 2047c478bd9Sstevel@tonic-gate #include <sys/pathname.h> 2057c478bd9Sstevel@tonic-gate #include <sys/proc.h> 2067c478bd9Sstevel@tonic-gate #include <sys/project.h> 207cf8f45c7Sdstaff #include <sys/sysevent.h> 2087c478bd9Sstevel@tonic-gate #include <sys/task.h> 2097c478bd9Sstevel@tonic-gate #include <sys/systm.h> 2107c478bd9Sstevel@tonic-gate #include <sys/types.h> 2117c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 2127c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 2137c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 2147c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 2157c478bd9Sstevel@tonic-gate #include <sys/policy.h> 2167c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h> 2177c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h> 2187c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h> 2197c478bd9Sstevel@tonic-gate #include <sys/class.h> 2207c478bd9Sstevel@tonic-gate #include <sys/pool.h> 2217c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h> 2227c478bd9Sstevel@tonic-gate #include <sys/pset.h> 2237c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 2247c478bd9Sstevel@tonic-gate #include <sys/callb.h> 2257c478bd9Sstevel@tonic-gate #include <sys/vmparam.h> 2267c478bd9Sstevel@tonic-gate #include <sys/corectl.h> 227824c205fSml93401 #include <sys/ipc_impl.h> 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate #include <sys/door.h> 2307c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 2337c478bd9Sstevel@tonic-gate #include <sys/session.h> 2347c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 2357c478bd9Sstevel@tonic-gate #include <sys/modhash.h> 2363f2f09c1Sdp #include <sys/sunddi.h> 2377c478bd9Sstevel@tonic-gate #include <sys/nvpair.h> 2387c478bd9Sstevel@tonic-gate #include <sys/rctl.h> 2397c478bd9Sstevel@tonic-gate #include <sys/fss.h> 2409acbbeafSnn35248 #include <sys/brand.h> 2417c478bd9Sstevel@tonic-gate #include <sys/zone.h> 242f4b3ec61Sdh155122 #include <net/if.h> 243c97ad5cdSakolb #include <sys/cpucaps.h> 2440209230bSgjelinek #include <vm/seg.h> 2450209230bSgjelinek 2467c478bd9Sstevel@tonic-gate /* 2477c478bd9Sstevel@tonic-gate * cv used to signal that all references to the zone have been released. This 2487c478bd9Sstevel@tonic-gate * needs to be global since there may be multiple waiters, and the first to 2497c478bd9Sstevel@tonic-gate * wake up will free the zone_t, hence we cannot use zone->zone_cv. 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate static kcondvar_t zone_destroy_cv; 2527c478bd9Sstevel@tonic-gate /* 2537c478bd9Sstevel@tonic-gate * Lock used to serialize access to zone_cv. This could have been per-zone, 2547c478bd9Sstevel@tonic-gate * but then we'd need another lock for zone_destroy_cv, and why bother? 2557c478bd9Sstevel@tonic-gate */ 2567c478bd9Sstevel@tonic-gate static kmutex_t zone_status_lock; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* 2597c478bd9Sstevel@tonic-gate * ZSD-related global variables. 2607c478bd9Sstevel@tonic-gate */ 2617c478bd9Sstevel@tonic-gate static kmutex_t zsd_key_lock; /* protects the following two */ 2627c478bd9Sstevel@tonic-gate /* 2637c478bd9Sstevel@tonic-gate * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate static zone_key_t zsd_keyval = 0; 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * Global list of registered keys. We use this when a new zone is created. 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate static list_t zsd_registered_keys; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate int zone_hash_size = 256; 27245916cd2Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel; 2737c478bd9Sstevel@tonic-gate static kmutex_t zonehash_lock; 2747c478bd9Sstevel@tonic-gate static uint_t zonecount; 2757c478bd9Sstevel@tonic-gate static id_space_t *zoneid_space; 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate /* 2787c478bd9Sstevel@tonic-gate * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 2797c478bd9Sstevel@tonic-gate * kernel proper runs, and which manages all other zones. 2807c478bd9Sstevel@tonic-gate * 2817c478bd9Sstevel@tonic-gate * Although not declared as static, the variable "zone0" should not be used 2827c478bd9Sstevel@tonic-gate * except for by code that needs to reference the global zone early on in boot, 2837c478bd9Sstevel@tonic-gate * before it is fully initialized. All other consumers should use 2847c478bd9Sstevel@tonic-gate * 'global_zone'. 2857c478bd9Sstevel@tonic-gate */ 2867c478bd9Sstevel@tonic-gate zone_t zone0; 2877c478bd9Sstevel@tonic-gate zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate /* 2907c478bd9Sstevel@tonic-gate * List of active zones, protected by zonehash_lock. 2917c478bd9Sstevel@tonic-gate */ 2927c478bd9Sstevel@tonic-gate static list_t zone_active; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * List of destroyed zones that still have outstanding cred references. 2967c478bd9Sstevel@tonic-gate * Used for debugging. Uses a separate lock to avoid lock ordering 2977c478bd9Sstevel@tonic-gate * problems in zone_free. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate static list_t zone_deathrow; 3007c478bd9Sstevel@tonic-gate static kmutex_t zone_deathrow_lock; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */ 3037c478bd9Sstevel@tonic-gate uint_t maxzones = 8192; 3047c478bd9Sstevel@tonic-gate 305cf8f45c7Sdstaff /* Event channel to sent zone state change notifications */ 306cf8f45c7Sdstaff evchan_t *zone_event_chan; 307cf8f45c7Sdstaff 308cf8f45c7Sdstaff /* 309cf8f45c7Sdstaff * This table holds the mapping from kernel zone states to 310cf8f45c7Sdstaff * states visible in the state notification API. 311cf8f45c7Sdstaff * The idea is that we only expose "obvious" states and 312cf8f45c7Sdstaff * do not expose states which are just implementation details. 313cf8f45c7Sdstaff */ 314cf8f45c7Sdstaff const char *zone_status_table[] = { 315cf8f45c7Sdstaff ZONE_EVENT_UNINITIALIZED, /* uninitialized */ 316cf8f45c7Sdstaff ZONE_EVENT_READY, /* ready */ 317cf8f45c7Sdstaff ZONE_EVENT_READY, /* booting */ 318cf8f45c7Sdstaff ZONE_EVENT_RUNNING, /* running */ 319cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* shutting_down */ 320cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* empty */ 321cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* down */ 322cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* dying */ 323cf8f45c7Sdstaff ZONE_EVENT_UNINITIALIZED, /* dead */ 324cf8f45c7Sdstaff }; 325cf8f45c7Sdstaff 3267c478bd9Sstevel@tonic-gate /* 3277c478bd9Sstevel@tonic-gate * This isn't static so lint doesn't complain. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares; 330c6939658Ssl108498 rctl_hndl_t rc_zone_locked_mem; 3310209230bSgjelinek rctl_hndl_t rc_zone_max_swap; 332c97ad5cdSakolb rctl_hndl_t rc_zone_cpu_cap; 3337c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps; 334824c205fSml93401 rctl_hndl_t rc_zone_shmmax; 335824c205fSml93401 rctl_hndl_t rc_zone_shmmni; 336824c205fSml93401 rctl_hndl_t rc_zone_semmni; 337824c205fSml93401 rctl_hndl_t rc_zone_msgmni; 3387c478bd9Sstevel@tonic-gate /* 3397c478bd9Sstevel@tonic-gate * Synchronization primitives used to synchronize between mounts and zone 3407c478bd9Sstevel@tonic-gate * creation/destruction. 3417c478bd9Sstevel@tonic-gate */ 3427c478bd9Sstevel@tonic-gate static int mounts_in_progress; 3437c478bd9Sstevel@tonic-gate static kcondvar_t mount_cv; 3447c478bd9Sstevel@tonic-gate static kmutex_t mount_lock; 3457c478bd9Sstevel@tonic-gate 3463f2f09c1Sdp const char * const zone_default_initname = "/sbin/init"; 34745916cd2Sjpk static char * const zone_prefix = "/zone/"; 3487c478bd9Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid); 349f4b3ec61Sdh155122 static int zone_add_datalink(zoneid_t, char *); 350f4b3ec61Sdh155122 static int zone_remove_datalink(zoneid_t, char *); 351f4b3ec61Sdh155122 static int zone_check_datalink(zoneid_t *, char *); 352f4b3ec61Sdh155122 static int zone_list_datalink(zoneid_t, int *, char *); 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate /* 355821c4a97Sdp * Bump this number when you alter the zone syscall interfaces; this is 356821c4a97Sdp * because we need to have support for previous API versions in libc 357821c4a97Sdp * to support patching; libc calls into the kernel to determine this number. 358821c4a97Sdp * 359821c4a97Sdp * Version 1 of the API is the version originally shipped with Solaris 10 360821c4a97Sdp * Version 2 alters the zone_create system call in order to support more 361821c4a97Sdp * arguments by moving the args into a structure; and to do better 362821c4a97Sdp * error reporting when zone_create() fails. 363821c4a97Sdp * Version 3 alters the zone_create system call in order to support the 364821c4a97Sdp * import of ZFS datasets to zones. 36545916cd2Sjpk * Version 4 alters the zone_create system call in order to support 36645916cd2Sjpk * Trusted Extensions. 3673f2f09c1Sdp * Version 5 alters the zone_boot system call, and converts its old 3683f2f09c1Sdp * bootargs parameter to be set by the zone_setattr API instead. 369f4b3ec61Sdh155122 * Version 6 adds the flag argument to zone_create. 370821c4a97Sdp */ 371f4b3ec61Sdh155122 static const int ZONE_SYSCALL_API_VERSION = 6; 372821c4a97Sdp 373821c4a97Sdp /* 3747c478bd9Sstevel@tonic-gate * Certain filesystems (such as NFS and autofs) need to know which zone 3757c478bd9Sstevel@tonic-gate * the mount is being placed in. Because of this, we need to be able to 3767c478bd9Sstevel@tonic-gate * ensure that a zone isn't in the process of being created such that 3777c478bd9Sstevel@tonic-gate * nfs_mount() thinks it is in the global zone, while by the time it 3787c478bd9Sstevel@tonic-gate * gets added the list of mounted zones, it ends up on zoneA's mount 3797c478bd9Sstevel@tonic-gate * list. 3807c478bd9Sstevel@tonic-gate * 3817c478bd9Sstevel@tonic-gate * The following functions: block_mounts()/resume_mounts() and 3827c478bd9Sstevel@tonic-gate * mount_in_progress()/mount_completed() are used by zones and the VFS 3837c478bd9Sstevel@tonic-gate * layer (respectively) to synchronize zone creation and new mounts. 3847c478bd9Sstevel@tonic-gate * 3857c478bd9Sstevel@tonic-gate * The semantics are like a reader-reader lock such that there may 3867c478bd9Sstevel@tonic-gate * either be multiple mounts (or zone creations, if that weren't 3877c478bd9Sstevel@tonic-gate * serialized by zonehash_lock) in progress at the same time, but not 3887c478bd9Sstevel@tonic-gate * both. 3897c478bd9Sstevel@tonic-gate * 3907c478bd9Sstevel@tonic-gate * We use cv's so the user can ctrl-C out of the operation if it's 3917c478bd9Sstevel@tonic-gate * taking too long. 3927c478bd9Sstevel@tonic-gate * 3937c478bd9Sstevel@tonic-gate * The semantics are such that there is unfair bias towards the 3947c478bd9Sstevel@tonic-gate * "current" operation. This means that zone creations may starve if 3957c478bd9Sstevel@tonic-gate * there is a rapid succession of new mounts coming in to the system, or 3967c478bd9Sstevel@tonic-gate * there is a remote possibility that zones will be created at such a 3977c478bd9Sstevel@tonic-gate * rate that new mounts will not be able to proceed. 3987c478bd9Sstevel@tonic-gate */ 3997c478bd9Sstevel@tonic-gate /* 4007c478bd9Sstevel@tonic-gate * Prevent new mounts from progressing to the point of calling 4017c478bd9Sstevel@tonic-gate * VFS_MOUNT(). If there are already mounts in this "region", wait for 4027c478bd9Sstevel@tonic-gate * them to complete. 4037c478bd9Sstevel@tonic-gate */ 4047c478bd9Sstevel@tonic-gate static int 4057c478bd9Sstevel@tonic-gate block_mounts(void) 4067c478bd9Sstevel@tonic-gate { 4077c478bd9Sstevel@tonic-gate int retval = 0; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate /* 4107c478bd9Sstevel@tonic-gate * Since it may block for a long time, block_mounts() shouldn't be 4117c478bd9Sstevel@tonic-gate * called with zonehash_lock held. 4127c478bd9Sstevel@tonic-gate */ 4137c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 4147c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4157c478bd9Sstevel@tonic-gate while (mounts_in_progress > 0) { 4167c478bd9Sstevel@tonic-gate if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 4177c478bd9Sstevel@tonic-gate goto signaled; 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate /* 4207c478bd9Sstevel@tonic-gate * A negative value of mounts_in_progress indicates that mounts 4217c478bd9Sstevel@tonic-gate * have been blocked by (-mounts_in_progress) different callers. 4227c478bd9Sstevel@tonic-gate */ 4237c478bd9Sstevel@tonic-gate mounts_in_progress--; 4247c478bd9Sstevel@tonic-gate retval = 1; 4257c478bd9Sstevel@tonic-gate signaled: 4267c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4277c478bd9Sstevel@tonic-gate return (retval); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate /* 4317c478bd9Sstevel@tonic-gate * The VFS layer may progress with new mounts as far as we're concerned. 4327c478bd9Sstevel@tonic-gate * Allow them to progress if we were the last obstacle. 4337c478bd9Sstevel@tonic-gate */ 4347c478bd9Sstevel@tonic-gate static void 4357c478bd9Sstevel@tonic-gate resume_mounts(void) 4367c478bd9Sstevel@tonic-gate { 4377c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4387c478bd9Sstevel@tonic-gate if (++mounts_in_progress == 0) 4397c478bd9Sstevel@tonic-gate cv_broadcast(&mount_cv); 4407c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate /* 4447c478bd9Sstevel@tonic-gate * The VFS layer is busy with a mount; zones should wait until all 4457c478bd9Sstevel@tonic-gate * mounts are completed to progress. 4467c478bd9Sstevel@tonic-gate */ 4477c478bd9Sstevel@tonic-gate void 4487c478bd9Sstevel@tonic-gate mount_in_progress(void) 4497c478bd9Sstevel@tonic-gate { 4507c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4517c478bd9Sstevel@tonic-gate while (mounts_in_progress < 0) 4527c478bd9Sstevel@tonic-gate cv_wait(&mount_cv, &mount_lock); 4537c478bd9Sstevel@tonic-gate mounts_in_progress++; 4547c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate /* 4587c478bd9Sstevel@tonic-gate * VFS is done with one mount; wake up any waiting block_mounts() 4597c478bd9Sstevel@tonic-gate * callers if this is the last mount. 4607c478bd9Sstevel@tonic-gate */ 4617c478bd9Sstevel@tonic-gate void 4627c478bd9Sstevel@tonic-gate mount_completed(void) 4637c478bd9Sstevel@tonic-gate { 4647c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4657c478bd9Sstevel@tonic-gate if (--mounts_in_progress == 0) 4667c478bd9Sstevel@tonic-gate cv_broadcast(&mount_cv); 4677c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate /* 4717c478bd9Sstevel@tonic-gate * ZSD routines. 4727c478bd9Sstevel@tonic-gate * 4737c478bd9Sstevel@tonic-gate * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 4747c478bd9Sstevel@tonic-gate * defined by the pthread_key_create() and related interfaces. 4757c478bd9Sstevel@tonic-gate * 4767c478bd9Sstevel@tonic-gate * Kernel subsystems may register one or more data items and/or 4777c478bd9Sstevel@tonic-gate * callbacks to be executed when a zone is created, shutdown, or 4787c478bd9Sstevel@tonic-gate * destroyed. 4797c478bd9Sstevel@tonic-gate * 4807c478bd9Sstevel@tonic-gate * Unlike the thread counterpart, destructor callbacks will be executed 4817c478bd9Sstevel@tonic-gate * even if the data pointer is NULL and/or there are no constructor 4827c478bd9Sstevel@tonic-gate * callbacks, so it is the responsibility of such callbacks to check for 4837c478bd9Sstevel@tonic-gate * NULL data values if necessary. 4847c478bd9Sstevel@tonic-gate * 4857c478bd9Sstevel@tonic-gate * The locking strategy and overall picture is as follows: 4867c478bd9Sstevel@tonic-gate * 4877c478bd9Sstevel@tonic-gate * When someone calls zone_key_create(), a template ZSD entry is added to the 4887c478bd9Sstevel@tonic-gate * global list "zsd_registered_keys", protected by zsd_key_lock. The 4897c478bd9Sstevel@tonic-gate * constructor callback is called immediately on all existing zones, and a 4907c478bd9Sstevel@tonic-gate * copy of the ZSD entry added to the per-zone zone_zsd list (protected by 4917c478bd9Sstevel@tonic-gate * zone_lock). As this operation requires the list of zones, the list of 4927c478bd9Sstevel@tonic-gate * registered keys, and the per-zone list of ZSD entries to remain constant 4937c478bd9Sstevel@tonic-gate * throughout the entire operation, it must grab zonehash_lock, zone_lock for 4947c478bd9Sstevel@tonic-gate * all existing zones, and zsd_key_lock, in that order. Similar locking is 4957c478bd9Sstevel@tonic-gate * needed when zone_key_delete() is called. It is thus sufficient to hold 4967c478bd9Sstevel@tonic-gate * zsd_key_lock *or* zone_lock to prevent additions to or removals from the 4977c478bd9Sstevel@tonic-gate * per-zone zone_zsd list. 4987c478bd9Sstevel@tonic-gate * 4997c478bd9Sstevel@tonic-gate * Note that this implementation does not make a copy of the ZSD entry if a 5007c478bd9Sstevel@tonic-gate * constructor callback is not provided. A zone_getspecific() on such an 5017c478bd9Sstevel@tonic-gate * uninitialized ZSD entry will return NULL. 5027c478bd9Sstevel@tonic-gate * 5037c478bd9Sstevel@tonic-gate * When new zones are created constructor callbacks for all registered ZSD 5047c478bd9Sstevel@tonic-gate * entries will be called. 5057c478bd9Sstevel@tonic-gate * 5067c478bd9Sstevel@tonic-gate * The framework does not provide any locking around zone_getspecific() and 5077c478bd9Sstevel@tonic-gate * zone_setspecific() apart from that needed for internal consistency, so 5087c478bd9Sstevel@tonic-gate * callers interested in atomic "test-and-set" semantics will need to provide 5097c478bd9Sstevel@tonic-gate * their own locking. 5107c478bd9Sstevel@tonic-gate */ 5117c478bd9Sstevel@tonic-gate void 5127c478bd9Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 5137c478bd9Sstevel@tonic-gate void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 5147c478bd9Sstevel@tonic-gate { 5157c478bd9Sstevel@tonic-gate struct zsd_entry *zsdp; 5167c478bd9Sstevel@tonic-gate struct zsd_entry *t; 5177c478bd9Sstevel@tonic-gate struct zone *zone; 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP); 5207c478bd9Sstevel@tonic-gate zsdp->zsd_data = NULL; 5217c478bd9Sstevel@tonic-gate zsdp->zsd_create = create; 5227c478bd9Sstevel@tonic-gate zsdp->zsd_shutdown = shutdown; 5237c478bd9Sstevel@tonic-gate zsdp->zsd_destroy = destroy; 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* stop the world */ 5267c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5277c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5287c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5317c478bd9Sstevel@tonic-gate *keyp = zsdp->zsd_key = ++zsd_keyval; 5327c478bd9Sstevel@tonic-gate ASSERT(zsd_keyval != 0); 5337c478bd9Sstevel@tonic-gate list_insert_tail(&zsd_registered_keys, zsdp); 5347c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate if (create != NULL) { 5377c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5387c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 5397c478bd9Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 5407c478bd9Sstevel@tonic-gate t->zsd_key = *keyp; 5417c478bd9Sstevel@tonic-gate t->zsd_data = (*create)(zone->zone_id); 5427c478bd9Sstevel@tonic-gate t->zsd_create = create; 5437c478bd9Sstevel@tonic-gate t->zsd_shutdown = shutdown; 5447c478bd9Sstevel@tonic-gate t->zsd_destroy = destroy; 5457c478bd9Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5497c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5507c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 5517c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 5527c478bd9Sstevel@tonic-gate } 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate /* 5557c478bd9Sstevel@tonic-gate * Helper function to find the zsd_entry associated with the key in the 5567c478bd9Sstevel@tonic-gate * given list. 5577c478bd9Sstevel@tonic-gate */ 5587c478bd9Sstevel@tonic-gate static struct zsd_entry * 5597c478bd9Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key) 5607c478bd9Sstevel@tonic-gate { 5617c478bd9Sstevel@tonic-gate struct zsd_entry *zsd; 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 5647c478bd9Sstevel@tonic-gate if (zsd->zsd_key == key) { 5657c478bd9Sstevel@tonic-gate /* 5667c478bd9Sstevel@tonic-gate * Move to head of list to keep list in MRU order. 5677c478bd9Sstevel@tonic-gate */ 5687c478bd9Sstevel@tonic-gate if (zsd != list_head(l)) { 5697c478bd9Sstevel@tonic-gate list_remove(l, zsd); 5707c478bd9Sstevel@tonic-gate list_insert_head(l, zsd); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate return (zsd); 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate } 5757c478bd9Sstevel@tonic-gate return (NULL); 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate /* 5797c478bd9Sstevel@tonic-gate * Function called when a module is being unloaded, or otherwise wishes 5807c478bd9Sstevel@tonic-gate * to unregister its ZSD key and callbacks. 5817c478bd9Sstevel@tonic-gate */ 5827c478bd9Sstevel@tonic-gate int 5837c478bd9Sstevel@tonic-gate zone_key_delete(zone_key_t key) 5847c478bd9Sstevel@tonic-gate { 5857c478bd9Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 5867c478bd9Sstevel@tonic-gate zone_t *zone; 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* Zone create/delete waits for us */ 5897c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5907c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5917c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5947c478bd9Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 5957c478bd9Sstevel@tonic-gate if (zsdp == NULL) 5967c478bd9Sstevel@tonic-gate goto notfound; 5977c478bd9Sstevel@tonic-gate list_remove(&zsd_registered_keys, zsdp); 5987c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 6017c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 6027c478bd9Sstevel@tonic-gate struct zsd_entry *del; 6037c478bd9Sstevel@tonic-gate void *data; 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate if (!(zone->zone_flags & ZF_DESTROYED)) { 6067c478bd9Sstevel@tonic-gate del = zsd_find(&zone->zone_zsd, key); 6077c478bd9Sstevel@tonic-gate if (del != NULL) { 6087c478bd9Sstevel@tonic-gate data = del->zsd_data; 6097c478bd9Sstevel@tonic-gate ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 6107c478bd9Sstevel@tonic-gate ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 6117c478bd9Sstevel@tonic-gate list_remove(&zone->zone_zsd, del); 6127c478bd9Sstevel@tonic-gate kmem_free(del, sizeof (*del)); 6137c478bd9Sstevel@tonic-gate } else { 6147c478bd9Sstevel@tonic-gate data = NULL; 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate if (zsdp->zsd_shutdown) 6177c478bd9Sstevel@tonic-gate zsdp->zsd_shutdown(zone->zone_id, data); 6187c478bd9Sstevel@tonic-gate if (zsdp->zsd_destroy) 6197c478bd9Sstevel@tonic-gate zsdp->zsd_destroy(zone->zone_id, data); 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 6247c478bd9Sstevel@tonic-gate kmem_free(zsdp, sizeof (*zsdp)); 6257c478bd9Sstevel@tonic-gate return (0); 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate notfound: 6287c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6297c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 6307c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 6317c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6327c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 6337c478bd9Sstevel@tonic-gate return (-1); 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate /* 6377c478bd9Sstevel@tonic-gate * ZSD counterpart of pthread_setspecific(). 6387c478bd9Sstevel@tonic-gate */ 6397c478bd9Sstevel@tonic-gate int 6407c478bd9Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 6417c478bd9Sstevel@tonic-gate { 6427c478bd9Sstevel@tonic-gate struct zsd_entry *t; 6437c478bd9Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6467c478bd9Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6477c478bd9Sstevel@tonic-gate if (t != NULL) { 6487c478bd9Sstevel@tonic-gate /* 6497c478bd9Sstevel@tonic-gate * Replace old value with new 6507c478bd9Sstevel@tonic-gate */ 6517c478bd9Sstevel@tonic-gate t->zsd_data = (void *)data; 6527c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6537c478bd9Sstevel@tonic-gate return (0); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate /* 6567c478bd9Sstevel@tonic-gate * If there was no previous value, go through the list of registered 6577c478bd9Sstevel@tonic-gate * keys. 6587c478bd9Sstevel@tonic-gate * 6597c478bd9Sstevel@tonic-gate * We avoid grabbing zsd_key_lock until we are sure we need it; this is 6607c478bd9Sstevel@tonic-gate * necessary for shutdown callbacks to be able to execute without fear 6617c478bd9Sstevel@tonic-gate * of deadlock. 6627c478bd9Sstevel@tonic-gate */ 6637c478bd9Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 6647c478bd9Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 6657c478bd9Sstevel@tonic-gate if (zsdp == NULL) { /* Key was not registered */ 6667c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6677c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6687c478bd9Sstevel@tonic-gate return (-1); 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate 6717c478bd9Sstevel@tonic-gate /* 6727c478bd9Sstevel@tonic-gate * Add a zsd_entry to this zone, using the template we just retrieved 6737c478bd9Sstevel@tonic-gate * to initialize the constructor and destructor(s). 6747c478bd9Sstevel@tonic-gate */ 6757c478bd9Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 6767c478bd9Sstevel@tonic-gate t->zsd_key = key; 6777c478bd9Sstevel@tonic-gate t->zsd_data = (void *)data; 6787c478bd9Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 6797c478bd9Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 6807c478bd9Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 6817c478bd9Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 6827c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6837c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6847c478bd9Sstevel@tonic-gate return (0); 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate /* 6887c478bd9Sstevel@tonic-gate * ZSD counterpart of pthread_getspecific(). 6897c478bd9Sstevel@tonic-gate */ 6907c478bd9Sstevel@tonic-gate void * 6917c478bd9Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone) 6927c478bd9Sstevel@tonic-gate { 6937c478bd9Sstevel@tonic-gate struct zsd_entry *t; 6947c478bd9Sstevel@tonic-gate void *data; 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6977c478bd9Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6987c478bd9Sstevel@tonic-gate data = (t == NULL ? NULL : t->zsd_data); 6997c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7007c478bd9Sstevel@tonic-gate return (data); 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate /* 7047c478bd9Sstevel@tonic-gate * Function used to initialize a zone's list of ZSD callbacks and data 7057c478bd9Sstevel@tonic-gate * when the zone is being created. The callbacks are initialized from 7067c478bd9Sstevel@tonic-gate * the template list (zsd_registered_keys), and the constructor 7077c478bd9Sstevel@tonic-gate * callback executed (if one exists). 7087c478bd9Sstevel@tonic-gate * 7097c478bd9Sstevel@tonic-gate * This is called before the zone is made publicly available, hence no 7107c478bd9Sstevel@tonic-gate * need to grab zone_lock. 7117c478bd9Sstevel@tonic-gate * 7127c478bd9Sstevel@tonic-gate * Although we grab and release zsd_key_lock, new entries cannot be 7137c478bd9Sstevel@tonic-gate * added to or removed from the zsd_registered_keys list until we 7147c478bd9Sstevel@tonic-gate * release zonehash_lock, so there isn't a window for a 7157c478bd9Sstevel@tonic-gate * zone_key_create() to come in after we've dropped zsd_key_lock but 7167c478bd9Sstevel@tonic-gate * before the zone is added to the zone list, such that the constructor 7177c478bd9Sstevel@tonic-gate * callbacks aren't executed for the new zone. 7187c478bd9Sstevel@tonic-gate */ 7197c478bd9Sstevel@tonic-gate static void 7207c478bd9Sstevel@tonic-gate zone_zsd_configure(zone_t *zone) 7217c478bd9Sstevel@tonic-gate { 7227c478bd9Sstevel@tonic-gate struct zsd_entry *zsdp; 7237c478bd9Sstevel@tonic-gate struct zsd_entry *t; 7247c478bd9Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 7277c478bd9Sstevel@tonic-gate ASSERT(list_head(&zone->zone_zsd) == NULL); 7287c478bd9Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7297c478bd9Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7307c478bd9Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7317c478bd9Sstevel@tonic-gate if (zsdp->zsd_create != NULL) { 7327c478bd9Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 7337c478bd9Sstevel@tonic-gate t->zsd_key = zsdp->zsd_key; 7347c478bd9Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 7357c478bd9Sstevel@tonic-gate t->zsd_data = (*t->zsd_create)(zoneid); 7367c478bd9Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 7377c478bd9Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 7387c478bd9Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate } 7417c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate /* 7477c478bd9Sstevel@tonic-gate * Helper function to execute shutdown or destructor callbacks. 7487c478bd9Sstevel@tonic-gate */ 7497c478bd9Sstevel@tonic-gate static void 7507c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 7517c478bd9Sstevel@tonic-gate { 7527c478bd9Sstevel@tonic-gate struct zsd_entry *zsdp; 7537c478bd9Sstevel@tonic-gate struct zsd_entry *t; 7547c478bd9Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 7557c478bd9Sstevel@tonic-gate 7567c478bd9Sstevel@tonic-gate ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 7577c478bd9Sstevel@tonic-gate ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 7587c478bd9Sstevel@tonic-gate ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 7597c478bd9Sstevel@tonic-gate 7607c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 7617c478bd9Sstevel@tonic-gate if (ct == ZSD_DESTROY) { 7627c478bd9Sstevel@tonic-gate if (zone->zone_flags & ZF_DESTROYED) { 7637c478bd9Sstevel@tonic-gate /* 7647c478bd9Sstevel@tonic-gate * Make sure destructors are only called once. 7657c478bd9Sstevel@tonic-gate */ 7667c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7677c478bd9Sstevel@tonic-gate return; 7687c478bd9Sstevel@tonic-gate } 7697c478bd9Sstevel@tonic-gate zone->zone_flags |= ZF_DESTROYED; 7707c478bd9Sstevel@tonic-gate } 7717c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate /* 7747c478bd9Sstevel@tonic-gate * Both zsd_key_lock and zone_lock need to be held in order to add or 7757c478bd9Sstevel@tonic-gate * remove a ZSD key, (either globally as part of 7767c478bd9Sstevel@tonic-gate * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is 7777c478bd9Sstevel@tonic-gate * possible through zone_setspecific()), so it's sufficient to hold 7787c478bd9Sstevel@tonic-gate * zsd_key_lock here. 7797c478bd9Sstevel@tonic-gate * 7807c478bd9Sstevel@tonic-gate * This is a good thing, since we don't want to recursively try to grab 7817c478bd9Sstevel@tonic-gate * zone_lock if a callback attempts to do something like a crfree() or 7827c478bd9Sstevel@tonic-gate * zone_rele(). 7837c478bd9Sstevel@tonic-gate */ 7847c478bd9Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7857c478bd9Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7867c478bd9Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7877c478bd9Sstevel@tonic-gate zone_key_t key = zsdp->zsd_key; 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate /* Skip if no callbacks registered */ 7907c478bd9Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL) 7917c478bd9Sstevel@tonic-gate continue; 7927c478bd9Sstevel@tonic-gate if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL) 7937c478bd9Sstevel@tonic-gate continue; 7947c478bd9Sstevel@tonic-gate /* 7957c478bd9Sstevel@tonic-gate * Call the callback with the zone-specific data if we can find 7967c478bd9Sstevel@tonic-gate * any, otherwise with NULL. 7977c478bd9Sstevel@tonic-gate */ 7987c478bd9Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 7997c478bd9Sstevel@tonic-gate if (t != NULL) { 8007c478bd9Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 8017c478bd9Sstevel@tonic-gate t->zsd_shutdown(zoneid, t->zsd_data); 8027c478bd9Sstevel@tonic-gate } else { 8037c478bd9Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 8047c478bd9Sstevel@tonic-gate t->zsd_destroy(zoneid, t->zsd_data); 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate } else { 8077c478bd9Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 8087c478bd9Sstevel@tonic-gate zsdp->zsd_shutdown(zoneid, NULL); 8097c478bd9Sstevel@tonic-gate } else { 8107c478bd9Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 8117c478bd9Sstevel@tonic-gate zsdp->zsd_destroy(zoneid, NULL); 8127c478bd9Sstevel@tonic-gate } 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate } 8157c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 8167c478bd9Sstevel@tonic-gate } 8177c478bd9Sstevel@tonic-gate 8187c478bd9Sstevel@tonic-gate /* 8197c478bd9Sstevel@tonic-gate * Called when the zone is going away; free ZSD-related memory, and 8207c478bd9Sstevel@tonic-gate * destroy the zone_zsd list. 8217c478bd9Sstevel@tonic-gate */ 8227c478bd9Sstevel@tonic-gate static void 8237c478bd9Sstevel@tonic-gate zone_free_zsd(zone_t *zone) 8247c478bd9Sstevel@tonic-gate { 8257c478bd9Sstevel@tonic-gate struct zsd_entry *t, *next; 8267c478bd9Sstevel@tonic-gate 8277c478bd9Sstevel@tonic-gate /* 8287c478bd9Sstevel@tonic-gate * Free all the zsd_entry's we had on this zone. 8297c478bd9Sstevel@tonic-gate */ 8307c478bd9Sstevel@tonic-gate for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 8317c478bd9Sstevel@tonic-gate next = list_next(&zone->zone_zsd, t); 8327c478bd9Sstevel@tonic-gate list_remove(&zone->zone_zsd, t); 8337c478bd9Sstevel@tonic-gate kmem_free(t, sizeof (*t)); 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate list_destroy(&zone->zone_zsd); 8367c478bd9Sstevel@tonic-gate } 8377c478bd9Sstevel@tonic-gate 8387c478bd9Sstevel@tonic-gate /* 839fa9e4066Sahrens * Frees memory associated with the zone dataset list. 840fa9e4066Sahrens */ 841fa9e4066Sahrens static void 842fa9e4066Sahrens zone_free_datasets(zone_t *zone) 843fa9e4066Sahrens { 844fa9e4066Sahrens zone_dataset_t *t, *next; 845fa9e4066Sahrens 846fa9e4066Sahrens for (t = list_head(&zone->zone_datasets); t != NULL; t = next) { 847fa9e4066Sahrens next = list_next(&zone->zone_datasets, t); 848fa9e4066Sahrens list_remove(&zone->zone_datasets, t); 849fa9e4066Sahrens kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1); 850fa9e4066Sahrens kmem_free(t, sizeof (*t)); 851fa9e4066Sahrens } 852fa9e4066Sahrens list_destroy(&zone->zone_datasets); 853fa9e4066Sahrens } 854fa9e4066Sahrens 855fa9e4066Sahrens /* 8567c478bd9Sstevel@tonic-gate * zone.cpu-shares resource control support. 8577c478bd9Sstevel@tonic-gate */ 8587c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 8597c478bd9Sstevel@tonic-gate static rctl_qty_t 8607c478bd9Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 8617c478bd9Sstevel@tonic-gate { 8627c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8637c478bd9Sstevel@tonic-gate return (p->p_zone->zone_shares); 8647c478bd9Sstevel@tonic-gate } 8657c478bd9Sstevel@tonic-gate 8667c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 8677c478bd9Sstevel@tonic-gate static int 8687c478bd9Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 8697c478bd9Sstevel@tonic-gate rctl_qty_t nv) 8707c478bd9Sstevel@tonic-gate { 8717c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8727c478bd9Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 8737c478bd9Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 8747c478bd9Sstevel@tonic-gate return (0); 8757c478bd9Sstevel@tonic-gate 8767c478bd9Sstevel@tonic-gate e->rcep_p.zone->zone_shares = nv; 8777c478bd9Sstevel@tonic-gate return (0); 8787c478bd9Sstevel@tonic-gate } 8797c478bd9Sstevel@tonic-gate 8807c478bd9Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = { 8817c478bd9Sstevel@tonic-gate rcop_no_action, 8827c478bd9Sstevel@tonic-gate zone_cpu_shares_usage, 8837c478bd9Sstevel@tonic-gate zone_cpu_shares_set, 8847c478bd9Sstevel@tonic-gate rcop_no_test 8857c478bd9Sstevel@tonic-gate }; 8867c478bd9Sstevel@tonic-gate 887c97ad5cdSakolb /* 888c97ad5cdSakolb * zone.cpu-cap resource control support. 889c97ad5cdSakolb */ 890c97ad5cdSakolb /*ARGSUSED*/ 891c97ad5cdSakolb static rctl_qty_t 892c97ad5cdSakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p) 893c97ad5cdSakolb { 894c97ad5cdSakolb ASSERT(MUTEX_HELD(&p->p_lock)); 895c97ad5cdSakolb return (cpucaps_zone_get(p->p_zone)); 896c97ad5cdSakolb } 897c97ad5cdSakolb 898c97ad5cdSakolb /*ARGSUSED*/ 899c97ad5cdSakolb static int 900c97ad5cdSakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 901c97ad5cdSakolb rctl_qty_t nv) 902c97ad5cdSakolb { 903c97ad5cdSakolb zone_t *zone = e->rcep_p.zone; 904c97ad5cdSakolb 905c97ad5cdSakolb ASSERT(MUTEX_HELD(&p->p_lock)); 906c97ad5cdSakolb ASSERT(e->rcep_t == RCENTITY_ZONE); 907c97ad5cdSakolb 908c97ad5cdSakolb if (zone == NULL) 909c97ad5cdSakolb return (0); 910c97ad5cdSakolb 911c97ad5cdSakolb /* 912c97ad5cdSakolb * set cap to the new value. 913c97ad5cdSakolb */ 914c97ad5cdSakolb return (cpucaps_zone_set(zone, nv)); 915c97ad5cdSakolb } 916c97ad5cdSakolb 917c97ad5cdSakolb static rctl_ops_t zone_cpu_cap_ops = { 918c97ad5cdSakolb rcop_no_action, 919c97ad5cdSakolb zone_cpu_cap_get, 920c97ad5cdSakolb zone_cpu_cap_set, 921c97ad5cdSakolb rcop_no_test 922c97ad5cdSakolb }; 923c97ad5cdSakolb 9247c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 9257c478bd9Sstevel@tonic-gate static rctl_qty_t 9267c478bd9Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p) 9277c478bd9Sstevel@tonic-gate { 9287c478bd9Sstevel@tonic-gate rctl_qty_t nlwps; 9297c478bd9Sstevel@tonic-gate zone_t *zone = p->p_zone; 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 9347c478bd9Sstevel@tonic-gate nlwps = zone->zone_nlwps; 9357c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate return (nlwps); 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 9417c478bd9Sstevel@tonic-gate static int 9427c478bd9Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 9437c478bd9Sstevel@tonic-gate rctl_qty_t incr, uint_t flags) 9447c478bd9Sstevel@tonic-gate { 9457c478bd9Sstevel@tonic-gate rctl_qty_t nlwps; 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 9487c478bd9Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 9497c478bd9Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 9507c478bd9Sstevel@tonic-gate return (0); 9517c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 9527c478bd9Sstevel@tonic-gate nlwps = e->rcep_p.zone->zone_nlwps; 9537c478bd9Sstevel@tonic-gate 9547c478bd9Sstevel@tonic-gate if (nlwps + incr > rcntl->rcv_value) 9557c478bd9Sstevel@tonic-gate return (1); 9567c478bd9Sstevel@tonic-gate 9577c478bd9Sstevel@tonic-gate return (0); 9587c478bd9Sstevel@tonic-gate } 9597c478bd9Sstevel@tonic-gate 9607c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 9617c478bd9Sstevel@tonic-gate static int 962c6939658Ssl108498 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) 963c6939658Ssl108498 { 9647c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 9657c478bd9Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 9667c478bd9Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 9677c478bd9Sstevel@tonic-gate return (0); 9687c478bd9Sstevel@tonic-gate e->rcep_p.zone->zone_nlwps_ctl = nv; 9697c478bd9Sstevel@tonic-gate return (0); 9707c478bd9Sstevel@tonic-gate } 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = { 9737c478bd9Sstevel@tonic-gate rcop_no_action, 9747c478bd9Sstevel@tonic-gate zone_lwps_usage, 9757c478bd9Sstevel@tonic-gate zone_lwps_set, 9767c478bd9Sstevel@tonic-gate zone_lwps_test, 9777c478bd9Sstevel@tonic-gate }; 9787c478bd9Sstevel@tonic-gate 979824c205fSml93401 /*ARGSUSED*/ 980824c205fSml93401 static int 981824c205fSml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 982824c205fSml93401 rctl_qty_t incr, uint_t flags) 983824c205fSml93401 { 984824c205fSml93401 rctl_qty_t v; 985824c205fSml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 986824c205fSml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 987824c205fSml93401 v = e->rcep_p.zone->zone_shmmax + incr; 988824c205fSml93401 if (v > rval->rcv_value) 989824c205fSml93401 return (1); 990824c205fSml93401 return (0); 991824c205fSml93401 } 992824c205fSml93401 993824c205fSml93401 static rctl_ops_t zone_shmmax_ops = { 994824c205fSml93401 rcop_no_action, 995824c205fSml93401 rcop_no_usage, 996824c205fSml93401 rcop_no_set, 997824c205fSml93401 zone_shmmax_test 998824c205fSml93401 }; 999824c205fSml93401 1000824c205fSml93401 /*ARGSUSED*/ 1001824c205fSml93401 static int 1002824c205fSml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1003824c205fSml93401 rctl_qty_t incr, uint_t flags) 1004824c205fSml93401 { 1005824c205fSml93401 rctl_qty_t v; 1006824c205fSml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 1007824c205fSml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 1008824c205fSml93401 v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr; 1009824c205fSml93401 if (v > rval->rcv_value) 1010824c205fSml93401 return (1); 1011824c205fSml93401 return (0); 1012824c205fSml93401 } 1013824c205fSml93401 1014824c205fSml93401 static rctl_ops_t zone_shmmni_ops = { 1015824c205fSml93401 rcop_no_action, 1016824c205fSml93401 rcop_no_usage, 1017824c205fSml93401 rcop_no_set, 1018824c205fSml93401 zone_shmmni_test 1019824c205fSml93401 }; 1020824c205fSml93401 1021824c205fSml93401 /*ARGSUSED*/ 1022824c205fSml93401 static int 1023824c205fSml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1024824c205fSml93401 rctl_qty_t incr, uint_t flags) 1025824c205fSml93401 { 1026824c205fSml93401 rctl_qty_t v; 1027824c205fSml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 1028824c205fSml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 1029824c205fSml93401 v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr; 1030824c205fSml93401 if (v > rval->rcv_value) 1031824c205fSml93401 return (1); 1032824c205fSml93401 return (0); 1033824c205fSml93401 } 1034824c205fSml93401 1035824c205fSml93401 static rctl_ops_t zone_semmni_ops = { 1036824c205fSml93401 rcop_no_action, 1037824c205fSml93401 rcop_no_usage, 1038824c205fSml93401 rcop_no_set, 1039824c205fSml93401 zone_semmni_test 1040824c205fSml93401 }; 1041824c205fSml93401 1042824c205fSml93401 /*ARGSUSED*/ 1043824c205fSml93401 static int 1044824c205fSml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1045824c205fSml93401 rctl_qty_t incr, uint_t flags) 1046824c205fSml93401 { 1047824c205fSml93401 rctl_qty_t v; 1048824c205fSml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 1049824c205fSml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 1050824c205fSml93401 v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr; 1051824c205fSml93401 if (v > rval->rcv_value) 1052824c205fSml93401 return (1); 1053824c205fSml93401 return (0); 1054824c205fSml93401 } 1055824c205fSml93401 1056824c205fSml93401 static rctl_ops_t zone_msgmni_ops = { 1057824c205fSml93401 rcop_no_action, 1058824c205fSml93401 rcop_no_usage, 1059824c205fSml93401 rcop_no_set, 1060824c205fSml93401 zone_msgmni_test 1061824c205fSml93401 }; 1062824c205fSml93401 1063c6939658Ssl108498 /*ARGSUSED*/ 1064c6939658Ssl108498 static rctl_qty_t 1065c6939658Ssl108498 zone_locked_mem_usage(rctl_t *rctl, struct proc *p) 1066c6939658Ssl108498 { 1067c6939658Ssl108498 rctl_qty_t q; 1068c6939658Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 10690209230bSgjelinek mutex_enter(&p->p_zone->zone_mem_lock); 1070c6939658Ssl108498 q = p->p_zone->zone_locked_mem; 10710209230bSgjelinek mutex_exit(&p->p_zone->zone_mem_lock); 1072c6939658Ssl108498 return (q); 1073c6939658Ssl108498 } 1074c6939658Ssl108498 1075c6939658Ssl108498 /*ARGSUSED*/ 1076c6939658Ssl108498 static int 1077c6939658Ssl108498 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 1078c6939658Ssl108498 rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 1079c6939658Ssl108498 { 1080c6939658Ssl108498 rctl_qty_t q; 10810209230bSgjelinek zone_t *z; 10820209230bSgjelinek 10830209230bSgjelinek z = e->rcep_p.zone; 1084c6939658Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 10850209230bSgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 10860209230bSgjelinek q = z->zone_locked_mem; 1087c6939658Ssl108498 if (q + incr > rcntl->rcv_value) 1088c6939658Ssl108498 return (1); 1089c6939658Ssl108498 return (0); 1090c6939658Ssl108498 } 1091c6939658Ssl108498 1092c6939658Ssl108498 /*ARGSUSED*/ 1093c6939658Ssl108498 static int 1094c6939658Ssl108498 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1095c6939658Ssl108498 rctl_qty_t nv) 1096c6939658Ssl108498 { 1097c6939658Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 1098c6939658Ssl108498 ASSERT(e->rcep_t == RCENTITY_ZONE); 1099c6939658Ssl108498 if (e->rcep_p.zone == NULL) 1100c6939658Ssl108498 return (0); 1101c6939658Ssl108498 e->rcep_p.zone->zone_locked_mem_ctl = nv; 1102c6939658Ssl108498 return (0); 1103c6939658Ssl108498 } 1104c6939658Ssl108498 1105c6939658Ssl108498 static rctl_ops_t zone_locked_mem_ops = { 1106c6939658Ssl108498 rcop_no_action, 1107c6939658Ssl108498 zone_locked_mem_usage, 1108c6939658Ssl108498 zone_locked_mem_set, 1109c6939658Ssl108498 zone_locked_mem_test 1110c6939658Ssl108498 }; 1111824c205fSml93401 11120209230bSgjelinek /*ARGSUSED*/ 11130209230bSgjelinek static rctl_qty_t 11140209230bSgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p) 11150209230bSgjelinek { 11160209230bSgjelinek rctl_qty_t q; 11170209230bSgjelinek zone_t *z = p->p_zone; 11180209230bSgjelinek 11190209230bSgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 11200209230bSgjelinek mutex_enter(&z->zone_mem_lock); 11210209230bSgjelinek q = z->zone_max_swap; 11220209230bSgjelinek mutex_exit(&z->zone_mem_lock); 11230209230bSgjelinek return (q); 11240209230bSgjelinek } 11250209230bSgjelinek 11260209230bSgjelinek /*ARGSUSED*/ 11270209230bSgjelinek static int 11280209230bSgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 11290209230bSgjelinek rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 11300209230bSgjelinek { 11310209230bSgjelinek rctl_qty_t q; 11320209230bSgjelinek zone_t *z; 11330209230bSgjelinek 11340209230bSgjelinek z = e->rcep_p.zone; 11350209230bSgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 11360209230bSgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 11370209230bSgjelinek q = z->zone_max_swap; 11380209230bSgjelinek if (q + incr > rcntl->rcv_value) 11390209230bSgjelinek return (1); 11400209230bSgjelinek return (0); 11410209230bSgjelinek } 11420209230bSgjelinek 11430209230bSgjelinek /*ARGSUSED*/ 11440209230bSgjelinek static int 11450209230bSgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 11460209230bSgjelinek rctl_qty_t nv) 11470209230bSgjelinek { 11480209230bSgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 11490209230bSgjelinek ASSERT(e->rcep_t == RCENTITY_ZONE); 11500209230bSgjelinek if (e->rcep_p.zone == NULL) 11510209230bSgjelinek return (0); 11520209230bSgjelinek e->rcep_p.zone->zone_max_swap_ctl = nv; 11530209230bSgjelinek return (0); 11540209230bSgjelinek } 11550209230bSgjelinek 11560209230bSgjelinek static rctl_ops_t zone_max_swap_ops = { 11570209230bSgjelinek rcop_no_action, 11580209230bSgjelinek zone_max_swap_usage, 11590209230bSgjelinek zone_max_swap_set, 11600209230bSgjelinek zone_max_swap_test 11610209230bSgjelinek }; 11620209230bSgjelinek 11637c478bd9Sstevel@tonic-gate /* 11647c478bd9Sstevel@tonic-gate * Helper function to brand the zone with a unique ID. 11657c478bd9Sstevel@tonic-gate */ 11667c478bd9Sstevel@tonic-gate static void 11677c478bd9Sstevel@tonic-gate zone_uniqid(zone_t *zone) 11687c478bd9Sstevel@tonic-gate { 11697c478bd9Sstevel@tonic-gate static uint64_t uniqid = 0; 11707c478bd9Sstevel@tonic-gate 11717c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 11727c478bd9Sstevel@tonic-gate zone->zone_uniqid = uniqid++; 11737c478bd9Sstevel@tonic-gate } 11747c478bd9Sstevel@tonic-gate 11757c478bd9Sstevel@tonic-gate /* 11767c478bd9Sstevel@tonic-gate * Returns a held pointer to the "kcred" for the specified zone. 11777c478bd9Sstevel@tonic-gate */ 11787c478bd9Sstevel@tonic-gate struct cred * 11797c478bd9Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid) 11807c478bd9Sstevel@tonic-gate { 11817c478bd9Sstevel@tonic-gate zone_t *zone; 11827c478bd9Sstevel@tonic-gate cred_t *cr; 11837c478bd9Sstevel@tonic-gate 11847c478bd9Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) 11857c478bd9Sstevel@tonic-gate return (NULL); 11867c478bd9Sstevel@tonic-gate cr = zone->zone_kcred; 11877c478bd9Sstevel@tonic-gate crhold(cr); 11887c478bd9Sstevel@tonic-gate zone_rele(zone); 11897c478bd9Sstevel@tonic-gate return (cr); 11907c478bd9Sstevel@tonic-gate } 11917c478bd9Sstevel@tonic-gate 11920209230bSgjelinek static int 11930209230bSgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw) 11940209230bSgjelinek { 11950209230bSgjelinek zone_t *zone = ksp->ks_private; 11960209230bSgjelinek zone_kstat_t *zk = ksp->ks_data; 11970209230bSgjelinek 11980209230bSgjelinek if (rw == KSTAT_WRITE) 11990209230bSgjelinek return (EACCES); 12000209230bSgjelinek 12010209230bSgjelinek zk->zk_usage.value.ui64 = zone->zone_locked_mem; 12020209230bSgjelinek zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl; 12030209230bSgjelinek return (0); 12040209230bSgjelinek } 12050209230bSgjelinek 12060209230bSgjelinek static int 12070209230bSgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw) 12080209230bSgjelinek { 12090209230bSgjelinek zone_t *zone = ksp->ks_private; 12100209230bSgjelinek zone_kstat_t *zk = ksp->ks_data; 12110209230bSgjelinek 12120209230bSgjelinek if (rw == KSTAT_WRITE) 12130209230bSgjelinek return (EACCES); 12140209230bSgjelinek 12150209230bSgjelinek zk->zk_usage.value.ui64 = zone->zone_max_swap; 12160209230bSgjelinek zk->zk_value.value.ui64 = zone->zone_max_swap_ctl; 12170209230bSgjelinek return (0); 12180209230bSgjelinek } 12190209230bSgjelinek 12200209230bSgjelinek static void 12210209230bSgjelinek zone_kstat_create(zone_t *zone) 12220209230bSgjelinek { 12230209230bSgjelinek kstat_t *ksp; 12240209230bSgjelinek zone_kstat_t *zk; 12250209230bSgjelinek 12260209230bSgjelinek ksp = rctl_kstat_create_zone(zone, "lockedmem", KSTAT_TYPE_NAMED, 12270209230bSgjelinek sizeof (zone_kstat_t) / sizeof (kstat_named_t), 12280209230bSgjelinek KSTAT_FLAG_VIRTUAL); 12290209230bSgjelinek 12300209230bSgjelinek if (ksp == NULL) 12310209230bSgjelinek return; 12320209230bSgjelinek 12330209230bSgjelinek zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 12340209230bSgjelinek ksp->ks_data_size += strlen(zone->zone_name) + 1; 12350209230bSgjelinek kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 12360209230bSgjelinek kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 12370209230bSgjelinek kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 12380209230bSgjelinek kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 12390209230bSgjelinek ksp->ks_update = zone_lockedmem_kstat_update; 12400209230bSgjelinek ksp->ks_private = zone; 12410209230bSgjelinek kstat_install(ksp); 12420209230bSgjelinek 12430209230bSgjelinek zone->zone_lockedmem_kstat = ksp; 12440209230bSgjelinek 12450209230bSgjelinek ksp = rctl_kstat_create_zone(zone, "swapresv", KSTAT_TYPE_NAMED, 12460209230bSgjelinek sizeof (zone_kstat_t) / sizeof (kstat_named_t), 12470209230bSgjelinek KSTAT_FLAG_VIRTUAL); 12480209230bSgjelinek 12490209230bSgjelinek if (ksp == NULL) 12500209230bSgjelinek return; 12510209230bSgjelinek 12520209230bSgjelinek zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 12530209230bSgjelinek ksp->ks_data_size += strlen(zone->zone_name) + 1; 12540209230bSgjelinek kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 12550209230bSgjelinek kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 12560209230bSgjelinek kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 12570209230bSgjelinek kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 12580209230bSgjelinek ksp->ks_update = zone_swapresv_kstat_update; 12590209230bSgjelinek ksp->ks_private = zone; 12600209230bSgjelinek kstat_install(ksp); 12610209230bSgjelinek 12620209230bSgjelinek zone->zone_swapresv_kstat = ksp; 12630209230bSgjelinek } 12640209230bSgjelinek 12650209230bSgjelinek static void 12660209230bSgjelinek zone_kstat_delete(zone_t *zone) 12670209230bSgjelinek { 12680209230bSgjelinek void *data; 12690209230bSgjelinek 12700209230bSgjelinek if (zone->zone_lockedmem_kstat != NULL) { 12710209230bSgjelinek data = zone->zone_lockedmem_kstat->ks_data; 12720209230bSgjelinek kstat_delete(zone->zone_lockedmem_kstat); 12730209230bSgjelinek kmem_free(data, sizeof (zone_kstat_t)); 12740209230bSgjelinek } 12750209230bSgjelinek if (zone->zone_swapresv_kstat != NULL) { 12760209230bSgjelinek data = zone->zone_swapresv_kstat->ks_data; 12770209230bSgjelinek kstat_delete(zone->zone_swapresv_kstat); 12780209230bSgjelinek kmem_free(data, sizeof (zone_kstat_t)); 12790209230bSgjelinek } 12800209230bSgjelinek } 12810209230bSgjelinek 12827c478bd9Sstevel@tonic-gate /* 12837c478bd9Sstevel@tonic-gate * Called very early on in boot to initialize the ZSD list so that 12847c478bd9Sstevel@tonic-gate * zone_key_create() can be called before zone_init(). It also initializes 12857c478bd9Sstevel@tonic-gate * portions of zone0 which may be used before zone_init() is called. The 12867c478bd9Sstevel@tonic-gate * variable "global_zone" will be set when zone0 is fully initialized by 12877c478bd9Sstevel@tonic-gate * zone_init(). 12887c478bd9Sstevel@tonic-gate */ 12897c478bd9Sstevel@tonic-gate void 12907c478bd9Sstevel@tonic-gate zone_zsd_init(void) 12917c478bd9Sstevel@tonic-gate { 12927c478bd9Sstevel@tonic-gate mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 12937c478bd9Sstevel@tonic-gate mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 12947c478bd9Sstevel@tonic-gate list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 12957c478bd9Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 12967c478bd9Sstevel@tonic-gate list_create(&zone_active, sizeof (zone_t), 12977c478bd9Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 12987c478bd9Sstevel@tonic-gate list_create(&zone_deathrow, sizeof (zone_t), 12997c478bd9Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 13007c478bd9Sstevel@tonic-gate 13017c478bd9Sstevel@tonic-gate mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 13027c478bd9Sstevel@tonic-gate mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 13030209230bSgjelinek mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 13047c478bd9Sstevel@tonic-gate zone0.zone_shares = 1; 13050209230bSgjelinek zone0.zone_nlwps = 0; 13067c478bd9Sstevel@tonic-gate zone0.zone_nlwps_ctl = INT_MAX; 13070209230bSgjelinek zone0.zone_locked_mem = 0; 13080209230bSgjelinek zone0.zone_locked_mem_ctl = UINT64_MAX; 13090209230bSgjelinek ASSERT(zone0.zone_max_swap == 0); 13100209230bSgjelinek zone0.zone_max_swap_ctl = UINT64_MAX; 1311824c205fSml93401 zone0.zone_shmmax = 0; 1312824c205fSml93401 zone0.zone_ipc.ipcq_shmmni = 0; 1313824c205fSml93401 zone0.zone_ipc.ipcq_semmni = 0; 1314824c205fSml93401 zone0.zone_ipc.ipcq_msgmni = 0; 13157c478bd9Sstevel@tonic-gate zone0.zone_name = GLOBAL_ZONENAME; 13167c478bd9Sstevel@tonic-gate zone0.zone_nodename = utsname.nodename; 13177c478bd9Sstevel@tonic-gate zone0.zone_domain = srpc_domain; 13187c478bd9Sstevel@tonic-gate zone0.zone_ref = 1; 13197c478bd9Sstevel@tonic-gate zone0.zone_id = GLOBAL_ZONEID; 13207c478bd9Sstevel@tonic-gate zone0.zone_status = ZONE_IS_RUNNING; 13217c478bd9Sstevel@tonic-gate zone0.zone_rootpath = "/"; 13227c478bd9Sstevel@tonic-gate zone0.zone_rootpathlen = 2; 13237c478bd9Sstevel@tonic-gate zone0.zone_psetid = ZONE_PS_INVAL; 13247c478bd9Sstevel@tonic-gate zone0.zone_ncpus = 0; 13257c478bd9Sstevel@tonic-gate zone0.zone_ncpus_online = 0; 13267c478bd9Sstevel@tonic-gate zone0.zone_proc_initpid = 1; 13273f2f09c1Sdp zone0.zone_initname = initname; 13280209230bSgjelinek zone0.zone_lockedmem_kstat = NULL; 13290209230bSgjelinek zone0.zone_swapresv_kstat = NULL; 13307c478bd9Sstevel@tonic-gate list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 13317c478bd9Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 13327c478bd9Sstevel@tonic-gate list_insert_head(&zone_active, &zone0); 13337c478bd9Sstevel@tonic-gate 13347c478bd9Sstevel@tonic-gate /* 13357c478bd9Sstevel@tonic-gate * The root filesystem is not mounted yet, so zone_rootvp cannot be set 13367c478bd9Sstevel@tonic-gate * to anything meaningful. It is assigned to be 'rootdir' in 13377c478bd9Sstevel@tonic-gate * vfs_mountroot(). 13387c478bd9Sstevel@tonic-gate */ 13397c478bd9Sstevel@tonic-gate zone0.zone_rootvp = NULL; 13407c478bd9Sstevel@tonic-gate zone0.zone_vfslist = NULL; 13413f2f09c1Sdp zone0.zone_bootargs = initargs; 13427c478bd9Sstevel@tonic-gate zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 13437c478bd9Sstevel@tonic-gate /* 13447c478bd9Sstevel@tonic-gate * The global zone has all privileges 13457c478bd9Sstevel@tonic-gate */ 13467c478bd9Sstevel@tonic-gate priv_fillset(zone0.zone_privset); 13477c478bd9Sstevel@tonic-gate /* 13487c478bd9Sstevel@tonic-gate * Add p0 to the global zone 13497c478bd9Sstevel@tonic-gate */ 13507c478bd9Sstevel@tonic-gate zone0.zone_zsched = &p0; 13517c478bd9Sstevel@tonic-gate p0.p_zone = &zone0; 13527c478bd9Sstevel@tonic-gate } 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate /* 135545916cd2Sjpk * Compute a hash value based on the contents of the label and the DOI. The 135645916cd2Sjpk * hash algorithm is somewhat arbitrary, but is based on the observation that 135745916cd2Sjpk * humans will likely pick labels that differ by amounts that work out to be 135845916cd2Sjpk * multiples of the number of hash chains, and thus stirring in some primes 135945916cd2Sjpk * should help. 136045916cd2Sjpk */ 136145916cd2Sjpk static uint_t 136245916cd2Sjpk hash_bylabel(void *hdata, mod_hash_key_t key) 136345916cd2Sjpk { 136445916cd2Sjpk const ts_label_t *lab = (ts_label_t *)key; 136545916cd2Sjpk const uint32_t *up, *ue; 136645916cd2Sjpk uint_t hash; 136745916cd2Sjpk int i; 136845916cd2Sjpk 136945916cd2Sjpk _NOTE(ARGUNUSED(hdata)); 137045916cd2Sjpk 137145916cd2Sjpk hash = lab->tsl_doi + (lab->tsl_doi << 1); 137245916cd2Sjpk /* we depend on alignment of label, but not representation */ 137345916cd2Sjpk up = (const uint32_t *)&lab->tsl_label; 137445916cd2Sjpk ue = up + sizeof (lab->tsl_label) / sizeof (*up); 137545916cd2Sjpk i = 1; 137645916cd2Sjpk while (up < ue) { 137745916cd2Sjpk /* using 2^n + 1, 1 <= n <= 16 as source of many primes */ 137845916cd2Sjpk hash += *up + (*up << ((i % 16) + 1)); 137945916cd2Sjpk up++; 138045916cd2Sjpk i++; 138145916cd2Sjpk } 138245916cd2Sjpk return (hash); 138345916cd2Sjpk } 138445916cd2Sjpk 138545916cd2Sjpk /* 138645916cd2Sjpk * All that mod_hash cares about here is zero (equal) versus non-zero (not 138745916cd2Sjpk * equal). This may need to be changed if less than / greater than is ever 138845916cd2Sjpk * needed. 138945916cd2Sjpk */ 139045916cd2Sjpk static int 139145916cd2Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 139245916cd2Sjpk { 139345916cd2Sjpk ts_label_t *lab1 = (ts_label_t *)key1; 139445916cd2Sjpk ts_label_t *lab2 = (ts_label_t *)key2; 139545916cd2Sjpk 139645916cd2Sjpk return (label_equal(lab1, lab2) ? 0 : 1); 139745916cd2Sjpk } 139845916cd2Sjpk 139945916cd2Sjpk /* 14007c478bd9Sstevel@tonic-gate * Called by main() to initialize the zones framework. 14017c478bd9Sstevel@tonic-gate */ 14027c478bd9Sstevel@tonic-gate void 14037c478bd9Sstevel@tonic-gate zone_init(void) 14047c478bd9Sstevel@tonic-gate { 14057c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rde; 14067c478bd9Sstevel@tonic-gate rctl_val_t *dval; 14077c478bd9Sstevel@tonic-gate rctl_set_t *set; 14087c478bd9Sstevel@tonic-gate rctl_alloc_gp_t *gp; 14097c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 1410cf8f45c7Sdstaff int res; 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate ASSERT(curproc == &p0); 14137c478bd9Sstevel@tonic-gate 14147c478bd9Sstevel@tonic-gate /* 14157c478bd9Sstevel@tonic-gate * Create ID space for zone IDs. ID 0 is reserved for the 14167c478bd9Sstevel@tonic-gate * global zone. 14177c478bd9Sstevel@tonic-gate */ 14187c478bd9Sstevel@tonic-gate zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 14197c478bd9Sstevel@tonic-gate 14207c478bd9Sstevel@tonic-gate /* 14217c478bd9Sstevel@tonic-gate * Initialize generic zone resource controls, if any. 14227c478bd9Sstevel@tonic-gate */ 14237c478bd9Sstevel@tonic-gate rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 14247c478bd9Sstevel@tonic-gate RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 142519f92332Sml93401 RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER, 1426c97ad5cdSakolb FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops); 1427c97ad5cdSakolb 1428c97ad5cdSakolb rc_zone_cpu_cap = rctl_register("zone.cpu-cap", 1429c97ad5cdSakolb RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS | 1430c97ad5cdSakolb RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER | 1431c97ad5cdSakolb RCTL_GLOBAL_INFINITE, 1432c97ad5cdSakolb MAXCAP, MAXCAP, &zone_cpu_cap_ops); 14337c478bd9Sstevel@tonic-gate 14347c478bd9Sstevel@tonic-gate rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 14357c478bd9Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 14367c478bd9Sstevel@tonic-gate INT_MAX, INT_MAX, &zone_lwps_ops); 14377c478bd9Sstevel@tonic-gate /* 1438824c205fSml93401 * System V IPC resource controls 1439824c205fSml93401 */ 1440824c205fSml93401 rc_zone_msgmni = rctl_register("zone.max-msg-ids", 1441824c205fSml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1442824c205fSml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops); 1443824c205fSml93401 1444824c205fSml93401 rc_zone_semmni = rctl_register("zone.max-sem-ids", 1445824c205fSml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1446824c205fSml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops); 1447824c205fSml93401 1448824c205fSml93401 rc_zone_shmmni = rctl_register("zone.max-shm-ids", 1449824c205fSml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1450824c205fSml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops); 1451824c205fSml93401 1452824c205fSml93401 rc_zone_shmmax = rctl_register("zone.max-shm-memory", 1453824c205fSml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1454824c205fSml93401 RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops); 1455824c205fSml93401 1456824c205fSml93401 /* 14577c478bd9Sstevel@tonic-gate * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 14587c478bd9Sstevel@tonic-gate * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 14597c478bd9Sstevel@tonic-gate */ 14607c478bd9Sstevel@tonic-gate dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 14617c478bd9Sstevel@tonic-gate bzero(dval, sizeof (rctl_val_t)); 14627c478bd9Sstevel@tonic-gate dval->rcv_value = 1; 14637c478bd9Sstevel@tonic-gate dval->rcv_privilege = RCPRIV_PRIVILEGED; 14647c478bd9Sstevel@tonic-gate dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 14657c478bd9Sstevel@tonic-gate dval->rcv_action_recip_pid = -1; 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate rde = rctl_dict_lookup("zone.cpu-shares"); 14687c478bd9Sstevel@tonic-gate (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 14697c478bd9Sstevel@tonic-gate 1470c6939658Ssl108498 rc_zone_locked_mem = rctl_register("zone.max-locked-memory", 1471c6939658Ssl108498 RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 1472c6939658Ssl108498 RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 1473c6939658Ssl108498 &zone_locked_mem_ops); 14740209230bSgjelinek 14750209230bSgjelinek rc_zone_max_swap = rctl_register("zone.max-swap", 14760209230bSgjelinek RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 14770209230bSgjelinek RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 14780209230bSgjelinek &zone_max_swap_ops); 14790209230bSgjelinek 14807c478bd9Sstevel@tonic-gate /* 14817c478bd9Sstevel@tonic-gate * Initialize the ``global zone''. 14827c478bd9Sstevel@tonic-gate */ 14837c478bd9Sstevel@tonic-gate set = rctl_set_create(); 14847c478bd9Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 14857c478bd9Sstevel@tonic-gate mutex_enter(&p0.p_lock); 14867c478bd9Sstevel@tonic-gate e.rcep_p.zone = &zone0; 14877c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 14887c478bd9Sstevel@tonic-gate zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 14897c478bd9Sstevel@tonic-gate gp); 14907c478bd9Sstevel@tonic-gate 14917c478bd9Sstevel@tonic-gate zone0.zone_nlwps = p0.p_lwpcnt; 14927c478bd9Sstevel@tonic-gate zone0.zone_ntasks = 1; 14937c478bd9Sstevel@tonic-gate mutex_exit(&p0.p_lock); 14949acbbeafSnn35248 zone0.zone_restart_init = B_TRUE; 14959acbbeafSnn35248 zone0.zone_brand = &native_brand; 14967c478bd9Sstevel@tonic-gate rctl_prealloc_destroy(gp); 14977c478bd9Sstevel@tonic-gate /* 14980209230bSgjelinek * pool_default hasn't been initialized yet, so we let pool_init() 14990209230bSgjelinek * take care of making sure the global zone is in the default pool. 15007c478bd9Sstevel@tonic-gate */ 150145916cd2Sjpk 150245916cd2Sjpk /* 15030209230bSgjelinek * Initialize global zone kstats 15040209230bSgjelinek */ 15050209230bSgjelinek zone_kstat_create(&zone0); 15060209230bSgjelinek 15070209230bSgjelinek /* 150845916cd2Sjpk * Initialize zone label. 150945916cd2Sjpk * mlp are initialized when tnzonecfg is loaded. 151045916cd2Sjpk */ 151145916cd2Sjpk zone0.zone_slabel = l_admin_low; 151245916cd2Sjpk rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 151345916cd2Sjpk label_hold(l_admin_low); 151445916cd2Sjpk 15157c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 15167c478bd9Sstevel@tonic-gate zone_uniqid(&zone0); 15177c478bd9Sstevel@tonic-gate ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 151845916cd2Sjpk 15197c478bd9Sstevel@tonic-gate zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 15207c478bd9Sstevel@tonic-gate mod_hash_null_valdtor); 15217c478bd9Sstevel@tonic-gate zonehashbyname = mod_hash_create_strhash("zone_by_name", 15227c478bd9Sstevel@tonic-gate zone_hash_size, mod_hash_null_valdtor); 152345916cd2Sjpk /* 152445916cd2Sjpk * maintain zonehashbylabel only for labeled systems 152545916cd2Sjpk */ 152645916cd2Sjpk if (is_system_labeled()) 152745916cd2Sjpk zonehashbylabel = mod_hash_create_extended("zone_by_label", 152845916cd2Sjpk zone_hash_size, mod_hash_null_keydtor, 152945916cd2Sjpk mod_hash_null_valdtor, hash_bylabel, NULL, 153045916cd2Sjpk hash_labelkey_cmp, KM_SLEEP); 15317c478bd9Sstevel@tonic-gate zonecount = 1; 15327c478bd9Sstevel@tonic-gate 15337c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 15347c478bd9Sstevel@tonic-gate (mod_hash_val_t)&zone0); 15357c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 15367c478bd9Sstevel@tonic-gate (mod_hash_val_t)&zone0); 153748451833Scarlsonj if (is_system_labeled()) { 153848451833Scarlsonj zone0.zone_flags |= ZF_HASHED_LABEL; 153945916cd2Sjpk (void) mod_hash_insert(zonehashbylabel, 154045916cd2Sjpk (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0); 154148451833Scarlsonj } 154245916cd2Sjpk mutex_exit(&zonehash_lock); 154345916cd2Sjpk 15447c478bd9Sstevel@tonic-gate /* 15457c478bd9Sstevel@tonic-gate * We avoid setting zone_kcred until now, since kcred is initialized 15467c478bd9Sstevel@tonic-gate * sometime after zone_zsd_init() and before zone_init(). 15477c478bd9Sstevel@tonic-gate */ 15487c478bd9Sstevel@tonic-gate zone0.zone_kcred = kcred; 15497c478bd9Sstevel@tonic-gate /* 15507c478bd9Sstevel@tonic-gate * The global zone is fully initialized (except for zone_rootvp which 15517c478bd9Sstevel@tonic-gate * will be set when the root filesystem is mounted). 15527c478bd9Sstevel@tonic-gate */ 15537c478bd9Sstevel@tonic-gate global_zone = &zone0; 1554cf8f45c7Sdstaff 1555cf8f45c7Sdstaff /* 1556cf8f45c7Sdstaff * Setup an event channel to send zone status change notifications on 1557cf8f45c7Sdstaff */ 1558cf8f45c7Sdstaff res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan, 1559cf8f45c7Sdstaff EVCH_CREAT); 1560cf8f45c7Sdstaff 1561cf8f45c7Sdstaff if (res) 1562cf8f45c7Sdstaff panic("Sysevent_evc_bind failed during zone setup.\n"); 15630209230bSgjelinek 15647c478bd9Sstevel@tonic-gate } 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate static void 15677c478bd9Sstevel@tonic-gate zone_free(zone_t *zone) 15687c478bd9Sstevel@tonic-gate { 15697c478bd9Sstevel@tonic-gate ASSERT(zone != global_zone); 15707c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ntasks == 0); 15717c478bd9Sstevel@tonic-gate ASSERT(zone->zone_nlwps == 0); 15727c478bd9Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 0); 15737c478bd9Sstevel@tonic-gate ASSERT(zone->zone_kcred == NULL); 15747c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 15757c478bd9Sstevel@tonic-gate zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 15767c478bd9Sstevel@tonic-gate 1577c97ad5cdSakolb /* 1578c97ad5cdSakolb * Remove any zone caps. 1579c97ad5cdSakolb */ 1580c97ad5cdSakolb cpucaps_zone_remove(zone); 1581c97ad5cdSakolb 1582c97ad5cdSakolb ASSERT(zone->zone_cpucap == NULL); 1583c97ad5cdSakolb 15847c478bd9Sstevel@tonic-gate /* remove from deathrow list */ 15857c478bd9Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_DEAD) { 15867c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 15877c478bd9Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 15887c478bd9Sstevel@tonic-gate list_remove(&zone_deathrow, zone); 15897c478bd9Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 15907c478bd9Sstevel@tonic-gate } 15917c478bd9Sstevel@tonic-gate 15927c478bd9Sstevel@tonic-gate zone_free_zsd(zone); 1593fa9e4066Sahrens zone_free_datasets(zone); 15947c478bd9Sstevel@tonic-gate 15957c478bd9Sstevel@tonic-gate if (zone->zone_rootvp != NULL) 15967c478bd9Sstevel@tonic-gate VN_RELE(zone->zone_rootvp); 15977c478bd9Sstevel@tonic-gate if (zone->zone_rootpath) 15987c478bd9Sstevel@tonic-gate kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 15997c478bd9Sstevel@tonic-gate if (zone->zone_name != NULL) 16007c478bd9Sstevel@tonic-gate kmem_free(zone->zone_name, ZONENAME_MAX); 160145916cd2Sjpk if (zone->zone_slabel != NULL) 160245916cd2Sjpk label_rele(zone->zone_slabel); 16037c478bd9Sstevel@tonic-gate if (zone->zone_nodename != NULL) 16047c478bd9Sstevel@tonic-gate kmem_free(zone->zone_nodename, _SYS_NMLN); 16057c478bd9Sstevel@tonic-gate if (zone->zone_domain != NULL) 16067c478bd9Sstevel@tonic-gate kmem_free(zone->zone_domain, _SYS_NMLN); 16077c478bd9Sstevel@tonic-gate if (zone->zone_privset != NULL) 16087c478bd9Sstevel@tonic-gate kmem_free(zone->zone_privset, sizeof (priv_set_t)); 16097c478bd9Sstevel@tonic-gate if (zone->zone_rctls != NULL) 16107c478bd9Sstevel@tonic-gate rctl_set_free(zone->zone_rctls); 16117c478bd9Sstevel@tonic-gate if (zone->zone_bootargs != NULL) 16123f2f09c1Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 16133f2f09c1Sdp if (zone->zone_initname != NULL) 16143f2f09c1Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 16157c478bd9Sstevel@tonic-gate id_free(zoneid_space, zone->zone_id); 16167c478bd9Sstevel@tonic-gate mutex_destroy(&zone->zone_lock); 16177c478bd9Sstevel@tonic-gate cv_destroy(&zone->zone_cv); 161845916cd2Sjpk rw_destroy(&zone->zone_mlps.mlpl_rwlock); 16197c478bd9Sstevel@tonic-gate kmem_free(zone, sizeof (zone_t)); 16207c478bd9Sstevel@tonic-gate } 16217c478bd9Sstevel@tonic-gate 16227c478bd9Sstevel@tonic-gate /* 16237c478bd9Sstevel@tonic-gate * See block comment at the top of this file for information about zone 16247c478bd9Sstevel@tonic-gate * status values. 16257c478bd9Sstevel@tonic-gate */ 16267c478bd9Sstevel@tonic-gate /* 16277c478bd9Sstevel@tonic-gate * Convenience function for setting zone status. 16287c478bd9Sstevel@tonic-gate */ 16297c478bd9Sstevel@tonic-gate static void 16307c478bd9Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status) 16317c478bd9Sstevel@tonic-gate { 1632cf8f45c7Sdstaff 1633cf8f45c7Sdstaff nvlist_t *nvl = NULL; 16347c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zone_status_lock)); 16357c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 16367c478bd9Sstevel@tonic-gate status >= zone_status_get(zone)); 1637cf8f45c7Sdstaff 1638cf8f45c7Sdstaff if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) || 1639cf8f45c7Sdstaff nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) || 1640cf8f45c7Sdstaff nvlist_add_string(nvl, ZONE_CB_NEWSTATE, 1641cf8f45c7Sdstaff zone_status_table[status]) || 1642cf8f45c7Sdstaff nvlist_add_string(nvl, ZONE_CB_OLDSTATE, 1643cf8f45c7Sdstaff zone_status_table[zone->zone_status]) || 1644cf8f45c7Sdstaff nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) || 1645cf8f45c7Sdstaff nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) || 1646cf8f45c7Sdstaff sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS, 16473f2f09c1Sdp ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) { 1648cf8f45c7Sdstaff #ifdef DEBUG 1649cf8f45c7Sdstaff (void) printf( 1650cf8f45c7Sdstaff "Failed to allocate and send zone state change event.\n"); 1651cf8f45c7Sdstaff #endif 1652cf8f45c7Sdstaff } 1653cf8f45c7Sdstaff nvlist_free(nvl); 1654cf8f45c7Sdstaff 16557c478bd9Sstevel@tonic-gate zone->zone_status = status; 1656cf8f45c7Sdstaff 16577c478bd9Sstevel@tonic-gate cv_broadcast(&zone->zone_cv); 16587c478bd9Sstevel@tonic-gate } 16597c478bd9Sstevel@tonic-gate 16607c478bd9Sstevel@tonic-gate /* 16617c478bd9Sstevel@tonic-gate * Public function to retrieve the zone status. The zone status may 16627c478bd9Sstevel@tonic-gate * change after it is retrieved. 16637c478bd9Sstevel@tonic-gate */ 16647c478bd9Sstevel@tonic-gate zone_status_t 16657c478bd9Sstevel@tonic-gate zone_status_get(zone_t *zone) 16667c478bd9Sstevel@tonic-gate { 16677c478bd9Sstevel@tonic-gate return (zone->zone_status); 16687c478bd9Sstevel@tonic-gate } 16697c478bd9Sstevel@tonic-gate 16707c478bd9Sstevel@tonic-gate static int 16717c478bd9Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 16727c478bd9Sstevel@tonic-gate { 16733f2f09c1Sdp char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP); 16743f2f09c1Sdp int err = 0; 16757c478bd9Sstevel@tonic-gate 16763f2f09c1Sdp ASSERT(zone != global_zone); 16773f2f09c1Sdp if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0) 16783f2f09c1Sdp goto done; /* EFAULT or ENAMETOOLONG */ 16793f2f09c1Sdp 16803f2f09c1Sdp if (zone->zone_bootargs != NULL) 16813f2f09c1Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 16823f2f09c1Sdp 16833f2f09c1Sdp zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP); 16843f2f09c1Sdp (void) strcpy(zone->zone_bootargs, bootargs); 16853f2f09c1Sdp 16863f2f09c1Sdp done: 16873f2f09c1Sdp kmem_free(bootargs, BOOTARGS_MAX); 16883f2f09c1Sdp return (err); 16897c478bd9Sstevel@tonic-gate } 16907c478bd9Sstevel@tonic-gate 16913f2f09c1Sdp static int 169259f2ff5cSedp zone_set_brand(zone_t *zone, const char *brand) 169359f2ff5cSedp { 169459f2ff5cSedp struct brand_attr *attrp; 169559f2ff5cSedp brand_t *bp; 169659f2ff5cSedp 169759f2ff5cSedp attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP); 169859f2ff5cSedp if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) { 169959f2ff5cSedp kmem_free(attrp, sizeof (struct brand_attr)); 170059f2ff5cSedp return (EFAULT); 170159f2ff5cSedp } 170259f2ff5cSedp 170359f2ff5cSedp bp = brand_register_zone(attrp); 170459f2ff5cSedp kmem_free(attrp, sizeof (struct brand_attr)); 170559f2ff5cSedp if (bp == NULL) 170659f2ff5cSedp return (EINVAL); 170759f2ff5cSedp 170859f2ff5cSedp /* 170959f2ff5cSedp * This is the only place where a zone can change it's brand. 171059f2ff5cSedp * We already need to hold zone_status_lock to check the zone 171159f2ff5cSedp * status, so we'll just use that lock to serialize zone 171259f2ff5cSedp * branding requests as well. 171359f2ff5cSedp */ 171459f2ff5cSedp mutex_enter(&zone_status_lock); 171559f2ff5cSedp 171659f2ff5cSedp /* Re-Branding is not allowed and the zone can't be booted yet */ 171759f2ff5cSedp if ((ZONE_IS_BRANDED(zone)) || 171859f2ff5cSedp (zone_status_get(zone) >= ZONE_IS_BOOTING)) { 171959f2ff5cSedp mutex_exit(&zone_status_lock); 172059f2ff5cSedp brand_unregister_zone(bp); 172159f2ff5cSedp return (EINVAL); 172259f2ff5cSedp } 172359f2ff5cSedp 172459f2ff5cSedp if (is_system_labeled() && 172559f2ff5cSedp strncmp(attrp->ba_brandname, NATIVE_BRAND_NAME, MAXNAMELEN) != 0) { 172659f2ff5cSedp mutex_exit(&zone_status_lock); 172759f2ff5cSedp brand_unregister_zone(bp); 172859f2ff5cSedp return (EPERM); 172959f2ff5cSedp } 173059f2ff5cSedp 173159f2ff5cSedp zone->zone_brand = bp; 173259f2ff5cSedp mutex_exit(&zone_status_lock); 173359f2ff5cSedp return (0); 173459f2ff5cSedp } 173559f2ff5cSedp 173659f2ff5cSedp static int 17373f2f09c1Sdp zone_set_initname(zone_t *zone, const char *zone_initname) 17383f2f09c1Sdp { 17393f2f09c1Sdp char initname[INITNAME_SZ]; 17403f2f09c1Sdp size_t len; 17413f2f09c1Sdp int err = 0; 17423f2f09c1Sdp 17433f2f09c1Sdp ASSERT(zone != global_zone); 17443f2f09c1Sdp if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0) 17453f2f09c1Sdp return (err); /* EFAULT or ENAMETOOLONG */ 17463f2f09c1Sdp 17473f2f09c1Sdp if (zone->zone_initname != NULL) 17483f2f09c1Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 17493f2f09c1Sdp 17503f2f09c1Sdp zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP); 17513f2f09c1Sdp (void) strcpy(zone->zone_initname, initname); 17527c478bd9Sstevel@tonic-gate return (0); 17537c478bd9Sstevel@tonic-gate } 17547c478bd9Sstevel@tonic-gate 17550209230bSgjelinek static int 17560209230bSgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap) 17570209230bSgjelinek { 17580209230bSgjelinek uint64_t mcap; 17590209230bSgjelinek int err = 0; 17600209230bSgjelinek 17610209230bSgjelinek if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0) 17620209230bSgjelinek zone->zone_phys_mcap = mcap; 17630209230bSgjelinek 17640209230bSgjelinek return (err); 17650209230bSgjelinek } 17660209230bSgjelinek 17670209230bSgjelinek static int 17680209230bSgjelinek zone_set_sched_class(zone_t *zone, const char *new_class) 17690209230bSgjelinek { 17700209230bSgjelinek char sched_class[PC_CLNMSZ]; 17710209230bSgjelinek id_t classid; 17720209230bSgjelinek int err; 17730209230bSgjelinek 17740209230bSgjelinek ASSERT(zone != global_zone); 17750209230bSgjelinek if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0) 17760209230bSgjelinek return (err); /* EFAULT or ENAMETOOLONG */ 17770209230bSgjelinek 17780209230bSgjelinek if (getcid(sched_class, &classid) != 0 || classid == syscid) 17790209230bSgjelinek return (set_errno(EINVAL)); 17800209230bSgjelinek zone->zone_defaultcid = classid; 17810209230bSgjelinek ASSERT(zone->zone_defaultcid > 0 && 17820209230bSgjelinek zone->zone_defaultcid < loaded_classes); 17830209230bSgjelinek 17840209230bSgjelinek return (0); 17850209230bSgjelinek } 17860209230bSgjelinek 17877c478bd9Sstevel@tonic-gate /* 17887c478bd9Sstevel@tonic-gate * Block indefinitely waiting for (zone_status >= status) 17897c478bd9Sstevel@tonic-gate */ 17907c478bd9Sstevel@tonic-gate void 17917c478bd9Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status) 17927c478bd9Sstevel@tonic-gate { 17937c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 17947c478bd9Sstevel@tonic-gate 17957c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 17967c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 17977c478bd9Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 17987c478bd9Sstevel@tonic-gate } 17997c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 18007c478bd9Sstevel@tonic-gate } 18017c478bd9Sstevel@tonic-gate 18027c478bd9Sstevel@tonic-gate /* 18037c478bd9Sstevel@tonic-gate * Private CPR-safe version of zone_status_wait(). 18047c478bd9Sstevel@tonic-gate */ 18057c478bd9Sstevel@tonic-gate static void 18067c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 18077c478bd9Sstevel@tonic-gate { 18087c478bd9Sstevel@tonic-gate callb_cpr_t cprinfo; 18097c478bd9Sstevel@tonic-gate 18107c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 18117c478bd9Sstevel@tonic-gate 18127c478bd9Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 18137c478bd9Sstevel@tonic-gate str); 18147c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 18157c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 18167c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 18177c478bd9Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 18187c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 18197c478bd9Sstevel@tonic-gate } 18207c478bd9Sstevel@tonic-gate /* 18217c478bd9Sstevel@tonic-gate * zone_status_lock is implicitly released by the following. 18227c478bd9Sstevel@tonic-gate */ 18237c478bd9Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 18247c478bd9Sstevel@tonic-gate } 18257c478bd9Sstevel@tonic-gate 18267c478bd9Sstevel@tonic-gate /* 18277c478bd9Sstevel@tonic-gate * Block until zone enters requested state or signal is received. Return (0) 18287c478bd9Sstevel@tonic-gate * if signaled, non-zero otherwise. 18297c478bd9Sstevel@tonic-gate */ 18307c478bd9Sstevel@tonic-gate int 18317c478bd9Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status) 18327c478bd9Sstevel@tonic-gate { 18337c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 18347c478bd9Sstevel@tonic-gate 18357c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 18367c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 18377c478bd9Sstevel@tonic-gate if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 18387c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 18397c478bd9Sstevel@tonic-gate return (0); 18407c478bd9Sstevel@tonic-gate } 18417c478bd9Sstevel@tonic-gate } 18427c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 18437c478bd9Sstevel@tonic-gate return (1); 18447c478bd9Sstevel@tonic-gate } 18457c478bd9Sstevel@tonic-gate 18467c478bd9Sstevel@tonic-gate /* 18477c478bd9Sstevel@tonic-gate * Block until the zone enters the requested state or the timeout expires, 18487c478bd9Sstevel@tonic-gate * whichever happens first. Return (-1) if operation timed out, time remaining 18497c478bd9Sstevel@tonic-gate * otherwise. 18507c478bd9Sstevel@tonic-gate */ 18517c478bd9Sstevel@tonic-gate clock_t 18527c478bd9Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 18537c478bd9Sstevel@tonic-gate { 18547c478bd9Sstevel@tonic-gate clock_t timeleft = 0; 18557c478bd9Sstevel@tonic-gate 18567c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 18577c478bd9Sstevel@tonic-gate 18587c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 18597c478bd9Sstevel@tonic-gate while (zone->zone_status < status && timeleft != -1) { 18607c478bd9Sstevel@tonic-gate timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 18617c478bd9Sstevel@tonic-gate } 18627c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 18637c478bd9Sstevel@tonic-gate return (timeleft); 18647c478bd9Sstevel@tonic-gate } 18657c478bd9Sstevel@tonic-gate 18667c478bd9Sstevel@tonic-gate /* 18677c478bd9Sstevel@tonic-gate * Block until the zone enters the requested state, the current process is 18687c478bd9Sstevel@tonic-gate * signaled, or the timeout expires, whichever happens first. Return (-1) if 18697c478bd9Sstevel@tonic-gate * operation timed out, 0 if signaled, time remaining otherwise. 18707c478bd9Sstevel@tonic-gate */ 18717c478bd9Sstevel@tonic-gate clock_t 18727c478bd9Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 18737c478bd9Sstevel@tonic-gate { 18747c478bd9Sstevel@tonic-gate clock_t timeleft = tim - lbolt; 18757c478bd9Sstevel@tonic-gate 18767c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 18777c478bd9Sstevel@tonic-gate 18787c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 18797c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 18807c478bd9Sstevel@tonic-gate timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 18817c478bd9Sstevel@tonic-gate tim); 18827c478bd9Sstevel@tonic-gate if (timeleft <= 0) 18837c478bd9Sstevel@tonic-gate break; 18847c478bd9Sstevel@tonic-gate } 18857c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 18867c478bd9Sstevel@tonic-gate return (timeleft); 18877c478bd9Sstevel@tonic-gate } 18887c478bd9Sstevel@tonic-gate 18897c478bd9Sstevel@tonic-gate /* 18907c478bd9Sstevel@tonic-gate * Zones have two reference counts: one for references from credential 18917c478bd9Sstevel@tonic-gate * structures (zone_cred_ref), and one (zone_ref) for everything else. 18927c478bd9Sstevel@tonic-gate * This is so we can allow a zone to be rebooted while there are still 18937c478bd9Sstevel@tonic-gate * outstanding cred references, since certain drivers cache dblks (which 18947c478bd9Sstevel@tonic-gate * implicitly results in cached creds). We wait for zone_ref to drop to 18957c478bd9Sstevel@tonic-gate * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 18967c478bd9Sstevel@tonic-gate * later freed when the zone_cred_ref drops to 0, though nothing other 18977c478bd9Sstevel@tonic-gate * than the zone id and privilege set should be accessed once the zone 18987c478bd9Sstevel@tonic-gate * is "dead". 18997c478bd9Sstevel@tonic-gate * 19007c478bd9Sstevel@tonic-gate * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 19017c478bd9Sstevel@tonic-gate * to force halt/reboot to block waiting for the zone_cred_ref to drop 19027c478bd9Sstevel@tonic-gate * to 0. This can be useful to flush out other sources of cached creds 19037c478bd9Sstevel@tonic-gate * that may be less innocuous than the driver case. 19047c478bd9Sstevel@tonic-gate */ 19057c478bd9Sstevel@tonic-gate 19067c478bd9Sstevel@tonic-gate int zone_wait_for_cred = 0; 19077c478bd9Sstevel@tonic-gate 19087c478bd9Sstevel@tonic-gate static void 19097c478bd9Sstevel@tonic-gate zone_hold_locked(zone_t *z) 19107c478bd9Sstevel@tonic-gate { 19117c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&z->zone_lock)); 19127c478bd9Sstevel@tonic-gate z->zone_ref++; 19137c478bd9Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 19147c478bd9Sstevel@tonic-gate } 19157c478bd9Sstevel@tonic-gate 19167c478bd9Sstevel@tonic-gate void 19177c478bd9Sstevel@tonic-gate zone_hold(zone_t *z) 19187c478bd9Sstevel@tonic-gate { 19197c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 19207c478bd9Sstevel@tonic-gate zone_hold_locked(z); 19217c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 19227c478bd9Sstevel@tonic-gate } 19237c478bd9Sstevel@tonic-gate 19247c478bd9Sstevel@tonic-gate /* 19257c478bd9Sstevel@tonic-gate * If the non-cred ref count drops to 1 and either the cred ref count 19267c478bd9Sstevel@tonic-gate * is 0 or we aren't waiting for cred references, the zone is ready to 19277c478bd9Sstevel@tonic-gate * be destroyed. 19287c478bd9Sstevel@tonic-gate */ 19297c478bd9Sstevel@tonic-gate #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 19307c478bd9Sstevel@tonic-gate (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 19317c478bd9Sstevel@tonic-gate 19327c478bd9Sstevel@tonic-gate void 19337c478bd9Sstevel@tonic-gate zone_rele(zone_t *z) 19347c478bd9Sstevel@tonic-gate { 19357c478bd9Sstevel@tonic-gate boolean_t wakeup; 19367c478bd9Sstevel@tonic-gate 19377c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 19387c478bd9Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 19397c478bd9Sstevel@tonic-gate z->zone_ref--; 19407c478bd9Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 19417c478bd9Sstevel@tonic-gate /* no more refs, free the structure */ 19427c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 19437c478bd9Sstevel@tonic-gate zone_free(z); 19447c478bd9Sstevel@tonic-gate return; 19457c478bd9Sstevel@tonic-gate } 19467c478bd9Sstevel@tonic-gate /* signal zone_destroy so the zone can finish halting */ 19477c478bd9Sstevel@tonic-gate wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 19487c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 19497c478bd9Sstevel@tonic-gate 19507c478bd9Sstevel@tonic-gate if (wakeup) { 19517c478bd9Sstevel@tonic-gate /* 19527c478bd9Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 19537c478bd9Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 19547c478bd9Sstevel@tonic-gate */ 19557c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 19567c478bd9Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 19577c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 19587c478bd9Sstevel@tonic-gate } 19597c478bd9Sstevel@tonic-gate } 19607c478bd9Sstevel@tonic-gate 19617c478bd9Sstevel@tonic-gate void 19627c478bd9Sstevel@tonic-gate zone_cred_hold(zone_t *z) 19637c478bd9Sstevel@tonic-gate { 19647c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 19657c478bd9Sstevel@tonic-gate z->zone_cred_ref++; 19667c478bd9Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 19677c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 19687c478bd9Sstevel@tonic-gate } 19697c478bd9Sstevel@tonic-gate 19707c478bd9Sstevel@tonic-gate void 19717c478bd9Sstevel@tonic-gate zone_cred_rele(zone_t *z) 19727c478bd9Sstevel@tonic-gate { 19737c478bd9Sstevel@tonic-gate boolean_t wakeup; 19747c478bd9Sstevel@tonic-gate 19757c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 19767c478bd9Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 19777c478bd9Sstevel@tonic-gate z->zone_cred_ref--; 19787c478bd9Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 19797c478bd9Sstevel@tonic-gate /* no more refs, free the structure */ 19807c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 19817c478bd9Sstevel@tonic-gate zone_free(z); 19827c478bd9Sstevel@tonic-gate return; 19837c478bd9Sstevel@tonic-gate } 19847c478bd9Sstevel@tonic-gate /* 19857c478bd9Sstevel@tonic-gate * If zone_destroy is waiting for the cred references to drain 19867c478bd9Sstevel@tonic-gate * out, and they have, signal it. 19877c478bd9Sstevel@tonic-gate */ 19887c478bd9Sstevel@tonic-gate wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 19897c478bd9Sstevel@tonic-gate zone_status_get(z) >= ZONE_IS_DEAD); 19907c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 19917c478bd9Sstevel@tonic-gate 19927c478bd9Sstevel@tonic-gate if (wakeup) { 19937c478bd9Sstevel@tonic-gate /* 19947c478bd9Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 19957c478bd9Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 19967c478bd9Sstevel@tonic-gate */ 19977c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 19987c478bd9Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 19997c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 20007c478bd9Sstevel@tonic-gate } 20017c478bd9Sstevel@tonic-gate } 20027c478bd9Sstevel@tonic-gate 20037c478bd9Sstevel@tonic-gate void 20047c478bd9Sstevel@tonic-gate zone_task_hold(zone_t *z) 20057c478bd9Sstevel@tonic-gate { 20067c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 20077c478bd9Sstevel@tonic-gate z->zone_ntasks++; 20087c478bd9Sstevel@tonic-gate ASSERT(z->zone_ntasks != 0); 20097c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 20107c478bd9Sstevel@tonic-gate } 20117c478bd9Sstevel@tonic-gate 20127c478bd9Sstevel@tonic-gate void 20137c478bd9Sstevel@tonic-gate zone_task_rele(zone_t *zone) 20147c478bd9Sstevel@tonic-gate { 20157c478bd9Sstevel@tonic-gate uint_t refcnt; 20167c478bd9Sstevel@tonic-gate 20177c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 20187c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ntasks != 0); 20197c478bd9Sstevel@tonic-gate refcnt = --zone->zone_ntasks; 20207c478bd9Sstevel@tonic-gate if (refcnt > 1) { /* Common case */ 20217c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 20227c478bd9Sstevel@tonic-gate return; 20237c478bd9Sstevel@tonic-gate } 20247c478bd9Sstevel@tonic-gate zone_hold_locked(zone); /* so we can use the zone_t later */ 20257c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 20267c478bd9Sstevel@tonic-gate if (refcnt == 1) { 20277c478bd9Sstevel@tonic-gate /* 20287c478bd9Sstevel@tonic-gate * See if the zone is shutting down. 20297c478bd9Sstevel@tonic-gate */ 20307c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 20317c478bd9Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 20327c478bd9Sstevel@tonic-gate goto out; 20337c478bd9Sstevel@tonic-gate } 20347c478bd9Sstevel@tonic-gate 20357c478bd9Sstevel@tonic-gate /* 20367c478bd9Sstevel@tonic-gate * Make sure the ntasks didn't change since we 20377c478bd9Sstevel@tonic-gate * dropped zone_lock. 20387c478bd9Sstevel@tonic-gate */ 20397c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 20407c478bd9Sstevel@tonic-gate if (refcnt != zone->zone_ntasks) { 20417c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 20427c478bd9Sstevel@tonic-gate goto out; 20437c478bd9Sstevel@tonic-gate } 20447c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 20457c478bd9Sstevel@tonic-gate 20467c478bd9Sstevel@tonic-gate /* 20477c478bd9Sstevel@tonic-gate * No more user processes in the zone. The zone is empty. 20487c478bd9Sstevel@tonic-gate */ 20497c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 20507c478bd9Sstevel@tonic-gate goto out; 20517c478bd9Sstevel@tonic-gate } 20527c478bd9Sstevel@tonic-gate 20537c478bd9Sstevel@tonic-gate ASSERT(refcnt == 0); 20547c478bd9Sstevel@tonic-gate /* 20557c478bd9Sstevel@tonic-gate * zsched has exited; the zone is dead. 20567c478bd9Sstevel@tonic-gate */ 20577c478bd9Sstevel@tonic-gate zone->zone_zsched = NULL; /* paranoia */ 20587c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 20597c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DEAD); 20607c478bd9Sstevel@tonic-gate out: 20617c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 20627c478bd9Sstevel@tonic-gate zone_rele(zone); 20637c478bd9Sstevel@tonic-gate } 20647c478bd9Sstevel@tonic-gate 20657c478bd9Sstevel@tonic-gate zoneid_t 20667c478bd9Sstevel@tonic-gate getzoneid(void) 20677c478bd9Sstevel@tonic-gate { 20687c478bd9Sstevel@tonic-gate return (curproc->p_zone->zone_id); 20697c478bd9Sstevel@tonic-gate } 20707c478bd9Sstevel@tonic-gate 20717c478bd9Sstevel@tonic-gate /* 20727c478bd9Sstevel@tonic-gate * Internal versions of zone_find_by_*(). These don't zone_hold() or 20737c478bd9Sstevel@tonic-gate * check the validity of a zone's state. 20747c478bd9Sstevel@tonic-gate */ 20757c478bd9Sstevel@tonic-gate static zone_t * 20767c478bd9Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid) 20777c478bd9Sstevel@tonic-gate { 20787c478bd9Sstevel@tonic-gate mod_hash_val_t hv; 20797c478bd9Sstevel@tonic-gate zone_t *zone = NULL; 20807c478bd9Sstevel@tonic-gate 20817c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 20827c478bd9Sstevel@tonic-gate 20837c478bd9Sstevel@tonic-gate if (mod_hash_find(zonehashbyid, 20847c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 20857c478bd9Sstevel@tonic-gate zone = (zone_t *)hv; 20867c478bd9Sstevel@tonic-gate return (zone); 20877c478bd9Sstevel@tonic-gate } 20887c478bd9Sstevel@tonic-gate 20897c478bd9Sstevel@tonic-gate static zone_t * 209045916cd2Sjpk zone_find_all_by_label(const ts_label_t *label) 209145916cd2Sjpk { 209245916cd2Sjpk mod_hash_val_t hv; 209345916cd2Sjpk zone_t *zone = NULL; 209445916cd2Sjpk 209545916cd2Sjpk ASSERT(MUTEX_HELD(&zonehash_lock)); 209645916cd2Sjpk 209745916cd2Sjpk /* 209845916cd2Sjpk * zonehashbylabel is not maintained for unlabeled systems 209945916cd2Sjpk */ 210045916cd2Sjpk if (!is_system_labeled()) 210145916cd2Sjpk return (NULL); 210245916cd2Sjpk if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0) 210345916cd2Sjpk zone = (zone_t *)hv; 210445916cd2Sjpk return (zone); 210545916cd2Sjpk } 210645916cd2Sjpk 210745916cd2Sjpk static zone_t * 21087c478bd9Sstevel@tonic-gate zone_find_all_by_name(char *name) 21097c478bd9Sstevel@tonic-gate { 21107c478bd9Sstevel@tonic-gate mod_hash_val_t hv; 21117c478bd9Sstevel@tonic-gate zone_t *zone = NULL; 21127c478bd9Sstevel@tonic-gate 21137c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 21147c478bd9Sstevel@tonic-gate 21157c478bd9Sstevel@tonic-gate if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 21167c478bd9Sstevel@tonic-gate zone = (zone_t *)hv; 21177c478bd9Sstevel@tonic-gate return (zone); 21187c478bd9Sstevel@tonic-gate } 21197c478bd9Sstevel@tonic-gate 21207c478bd9Sstevel@tonic-gate /* 21217c478bd9Sstevel@tonic-gate * Public interface for looking up a zone by zoneid. Only returns the zone if 21227c478bd9Sstevel@tonic-gate * it is fully initialized, and has not yet begun the zone_destroy() sequence. 21237c478bd9Sstevel@tonic-gate * Caller must call zone_rele() once it is done with the zone. 21247c478bd9Sstevel@tonic-gate * 21257c478bd9Sstevel@tonic-gate * The zone may begin the zone_destroy() sequence immediately after this 21267c478bd9Sstevel@tonic-gate * function returns, but may be safely used until zone_rele() is called. 21277c478bd9Sstevel@tonic-gate */ 21287c478bd9Sstevel@tonic-gate zone_t * 21297c478bd9Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid) 21307c478bd9Sstevel@tonic-gate { 21317c478bd9Sstevel@tonic-gate zone_t *zone; 21327c478bd9Sstevel@tonic-gate zone_status_t status; 21337c478bd9Sstevel@tonic-gate 21347c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 21357c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 21367c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 21377c478bd9Sstevel@tonic-gate return (NULL); 21387c478bd9Sstevel@tonic-gate } 21397c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 21407c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 21417c478bd9Sstevel@tonic-gate /* 21427c478bd9Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 21437c478bd9Sstevel@tonic-gate */ 21447c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 21457c478bd9Sstevel@tonic-gate return (NULL); 21467c478bd9Sstevel@tonic-gate } 21477c478bd9Sstevel@tonic-gate zone_hold(zone); 21487c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 21497c478bd9Sstevel@tonic-gate return (zone); 21507c478bd9Sstevel@tonic-gate } 21517c478bd9Sstevel@tonic-gate 21527c478bd9Sstevel@tonic-gate /* 215345916cd2Sjpk * Similar to zone_find_by_id, but using zone label as the key. 215445916cd2Sjpk */ 215545916cd2Sjpk zone_t * 215645916cd2Sjpk zone_find_by_label(const ts_label_t *label) 215745916cd2Sjpk { 215845916cd2Sjpk zone_t *zone; 215942bc57c4Srica zone_status_t status; 216045916cd2Sjpk 216145916cd2Sjpk mutex_enter(&zonehash_lock); 216245916cd2Sjpk if ((zone = zone_find_all_by_label(label)) == NULL) { 216345916cd2Sjpk mutex_exit(&zonehash_lock); 216445916cd2Sjpk return (NULL); 216545916cd2Sjpk } 216642bc57c4Srica 216742bc57c4Srica status = zone_status_get(zone); 216842bc57c4Srica if (status > ZONE_IS_DOWN) { 216945916cd2Sjpk /* 217045916cd2Sjpk * For all practical purposes the zone doesn't exist. 217145916cd2Sjpk */ 217242bc57c4Srica mutex_exit(&zonehash_lock); 217342bc57c4Srica return (NULL); 217445916cd2Sjpk } 217542bc57c4Srica zone_hold(zone); 217645916cd2Sjpk mutex_exit(&zonehash_lock); 217745916cd2Sjpk return (zone); 217845916cd2Sjpk } 217945916cd2Sjpk 218045916cd2Sjpk /* 21817c478bd9Sstevel@tonic-gate * Similar to zone_find_by_id, but using zone name as the key. 21827c478bd9Sstevel@tonic-gate */ 21837c478bd9Sstevel@tonic-gate zone_t * 21847c478bd9Sstevel@tonic-gate zone_find_by_name(char *name) 21857c478bd9Sstevel@tonic-gate { 21867c478bd9Sstevel@tonic-gate zone_t *zone; 21877c478bd9Sstevel@tonic-gate zone_status_t status; 21887c478bd9Sstevel@tonic-gate 21897c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 21907c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_name(name)) == NULL) { 21917c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 21927c478bd9Sstevel@tonic-gate return (NULL); 21937c478bd9Sstevel@tonic-gate } 21947c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 21957c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 21967c478bd9Sstevel@tonic-gate /* 21977c478bd9Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 21987c478bd9Sstevel@tonic-gate */ 21997c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 22007c478bd9Sstevel@tonic-gate return (NULL); 22017c478bd9Sstevel@tonic-gate } 22027c478bd9Sstevel@tonic-gate zone_hold(zone); 22037c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 22047c478bd9Sstevel@tonic-gate return (zone); 22057c478bd9Sstevel@tonic-gate } 22067c478bd9Sstevel@tonic-gate 22077c478bd9Sstevel@tonic-gate /* 22087c478bd9Sstevel@tonic-gate * Similar to zone_find_by_id(), using the path as a key. For instance, 22097c478bd9Sstevel@tonic-gate * if there is a zone "foo" rooted at /foo/root, and the path argument 22107c478bd9Sstevel@tonic-gate * is "/foo/root/proc", it will return the held zone_t corresponding to 22117c478bd9Sstevel@tonic-gate * zone "foo". 22127c478bd9Sstevel@tonic-gate * 22137c478bd9Sstevel@tonic-gate * zone_find_by_path() always returns a non-NULL value, since at the 22147c478bd9Sstevel@tonic-gate * very least every path will be contained in the global zone. 22157c478bd9Sstevel@tonic-gate * 22167c478bd9Sstevel@tonic-gate * As with the other zone_find_by_*() functions, the caller is 22177c478bd9Sstevel@tonic-gate * responsible for zone_rele()ing the return value of this function. 22187c478bd9Sstevel@tonic-gate */ 22197c478bd9Sstevel@tonic-gate zone_t * 22207c478bd9Sstevel@tonic-gate zone_find_by_path(const char *path) 22217c478bd9Sstevel@tonic-gate { 22227c478bd9Sstevel@tonic-gate zone_t *zone; 22237c478bd9Sstevel@tonic-gate zone_t *zret = NULL; 22247c478bd9Sstevel@tonic-gate zone_status_t status; 22257c478bd9Sstevel@tonic-gate 22267c478bd9Sstevel@tonic-gate if (path == NULL) { 22277c478bd9Sstevel@tonic-gate /* 22287c478bd9Sstevel@tonic-gate * Call from rootconf(). 22297c478bd9Sstevel@tonic-gate */ 22307c478bd9Sstevel@tonic-gate zone_hold(global_zone); 22317c478bd9Sstevel@tonic-gate return (global_zone); 22327c478bd9Sstevel@tonic-gate } 22337c478bd9Sstevel@tonic-gate ASSERT(*path == '/'); 22347c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 22357c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 22367c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 22377c478bd9Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(path, zone)) 22387c478bd9Sstevel@tonic-gate zret = zone; 22397c478bd9Sstevel@tonic-gate } 22407c478bd9Sstevel@tonic-gate ASSERT(zret != NULL); 22417c478bd9Sstevel@tonic-gate status = zone_status_get(zret); 22427c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 22437c478bd9Sstevel@tonic-gate /* 22447c478bd9Sstevel@tonic-gate * Zone practically doesn't exist. 22457c478bd9Sstevel@tonic-gate */ 22467c478bd9Sstevel@tonic-gate zret = global_zone; 22477c478bd9Sstevel@tonic-gate } 22487c478bd9Sstevel@tonic-gate zone_hold(zret); 22497c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 22507c478bd9Sstevel@tonic-gate return (zret); 22517c478bd9Sstevel@tonic-gate } 22527c478bd9Sstevel@tonic-gate 22537c478bd9Sstevel@tonic-gate /* 22547c478bd9Sstevel@tonic-gate * Get the number of cpus visible to this zone. The system-wide global 22557c478bd9Sstevel@tonic-gate * 'ncpus' is returned if pools are disabled, the caller is in the 22567c478bd9Sstevel@tonic-gate * global zone, or a NULL zone argument is passed in. 22577c478bd9Sstevel@tonic-gate */ 22587c478bd9Sstevel@tonic-gate int 22597c478bd9Sstevel@tonic-gate zone_ncpus_get(zone_t *zone) 22607c478bd9Sstevel@tonic-gate { 22617c478bd9Sstevel@tonic-gate int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 22627c478bd9Sstevel@tonic-gate 22637c478bd9Sstevel@tonic-gate return (myncpus != 0 ? myncpus : ncpus); 22647c478bd9Sstevel@tonic-gate } 22657c478bd9Sstevel@tonic-gate 22667c478bd9Sstevel@tonic-gate /* 22677c478bd9Sstevel@tonic-gate * Get the number of online cpus visible to this zone. The system-wide 22687c478bd9Sstevel@tonic-gate * global 'ncpus_online' is returned if pools are disabled, the caller 22697c478bd9Sstevel@tonic-gate * is in the global zone, or a NULL zone argument is passed in. 22707c478bd9Sstevel@tonic-gate */ 22717c478bd9Sstevel@tonic-gate int 22727c478bd9Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone) 22737c478bd9Sstevel@tonic-gate { 22747c478bd9Sstevel@tonic-gate int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 22757c478bd9Sstevel@tonic-gate 22767c478bd9Sstevel@tonic-gate return (myncpus_online != 0 ? myncpus_online : ncpus_online); 22777c478bd9Sstevel@tonic-gate } 22787c478bd9Sstevel@tonic-gate 22797c478bd9Sstevel@tonic-gate /* 22807c478bd9Sstevel@tonic-gate * Return the pool to which the zone is currently bound. 22817c478bd9Sstevel@tonic-gate */ 22827c478bd9Sstevel@tonic-gate pool_t * 22837c478bd9Sstevel@tonic-gate zone_pool_get(zone_t *zone) 22847c478bd9Sstevel@tonic-gate { 22857c478bd9Sstevel@tonic-gate ASSERT(pool_lock_held()); 22867c478bd9Sstevel@tonic-gate 22877c478bd9Sstevel@tonic-gate return (zone->zone_pool); 22887c478bd9Sstevel@tonic-gate } 22897c478bd9Sstevel@tonic-gate 22907c478bd9Sstevel@tonic-gate /* 22917c478bd9Sstevel@tonic-gate * Set the zone's pool pointer and update the zone's visibility to match 22927c478bd9Sstevel@tonic-gate * the resources in the new pool. 22937c478bd9Sstevel@tonic-gate */ 22947c478bd9Sstevel@tonic-gate void 22957c478bd9Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool) 22967c478bd9Sstevel@tonic-gate { 22977c478bd9Sstevel@tonic-gate ASSERT(pool_lock_held()); 22987c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 22997c478bd9Sstevel@tonic-gate 23007c478bd9Sstevel@tonic-gate zone->zone_pool = pool; 23017c478bd9Sstevel@tonic-gate zone_pset_set(zone, pool->pool_pset->pset_id); 23027c478bd9Sstevel@tonic-gate } 23037c478bd9Sstevel@tonic-gate 23047c478bd9Sstevel@tonic-gate /* 23057c478bd9Sstevel@tonic-gate * Return the cached value of the id of the processor set to which the 23067c478bd9Sstevel@tonic-gate * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 23077c478bd9Sstevel@tonic-gate * facility is disabled. 23087c478bd9Sstevel@tonic-gate */ 23097c478bd9Sstevel@tonic-gate psetid_t 23107c478bd9Sstevel@tonic-gate zone_pset_get(zone_t *zone) 23117c478bd9Sstevel@tonic-gate { 23127c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 23137c478bd9Sstevel@tonic-gate 23147c478bd9Sstevel@tonic-gate return (zone->zone_psetid); 23157c478bd9Sstevel@tonic-gate } 23167c478bd9Sstevel@tonic-gate 23177c478bd9Sstevel@tonic-gate /* 23187c478bd9Sstevel@tonic-gate * Set the cached value of the id of the processor set to which the zone 23197c478bd9Sstevel@tonic-gate * is currently bound. Also update the zone's visibility to match the 23207c478bd9Sstevel@tonic-gate * resources in the new processor set. 23217c478bd9Sstevel@tonic-gate */ 23227c478bd9Sstevel@tonic-gate void 23237c478bd9Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid) 23247c478bd9Sstevel@tonic-gate { 23257c478bd9Sstevel@tonic-gate psetid_t oldpsetid; 23267c478bd9Sstevel@tonic-gate 23277c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 23287c478bd9Sstevel@tonic-gate oldpsetid = zone_pset_get(zone); 23297c478bd9Sstevel@tonic-gate 23307c478bd9Sstevel@tonic-gate if (oldpsetid == newpsetid) 23317c478bd9Sstevel@tonic-gate return; 23327c478bd9Sstevel@tonic-gate /* 23337c478bd9Sstevel@tonic-gate * Global zone sees all. 23347c478bd9Sstevel@tonic-gate */ 23357c478bd9Sstevel@tonic-gate if (zone != global_zone) { 23367c478bd9Sstevel@tonic-gate zone->zone_psetid = newpsetid; 23377c478bd9Sstevel@tonic-gate if (newpsetid != ZONE_PS_INVAL) 23387c478bd9Sstevel@tonic-gate pool_pset_visibility_add(newpsetid, zone); 23397c478bd9Sstevel@tonic-gate if (oldpsetid != ZONE_PS_INVAL) 23407c478bd9Sstevel@tonic-gate pool_pset_visibility_remove(oldpsetid, zone); 23417c478bd9Sstevel@tonic-gate } 23427c478bd9Sstevel@tonic-gate /* 23437c478bd9Sstevel@tonic-gate * Disabling pools, so we should start using the global values 23447c478bd9Sstevel@tonic-gate * for ncpus and ncpus_online. 23457c478bd9Sstevel@tonic-gate */ 23467c478bd9Sstevel@tonic-gate if (newpsetid == ZONE_PS_INVAL) { 23477c478bd9Sstevel@tonic-gate zone->zone_ncpus = 0; 23487c478bd9Sstevel@tonic-gate zone->zone_ncpus_online = 0; 23497c478bd9Sstevel@tonic-gate } 23507c478bd9Sstevel@tonic-gate } 23517c478bd9Sstevel@tonic-gate 23527c478bd9Sstevel@tonic-gate /* 23537c478bd9Sstevel@tonic-gate * Walk the list of active zones and issue the provided callback for 23547c478bd9Sstevel@tonic-gate * each of them. 23557c478bd9Sstevel@tonic-gate * 23567c478bd9Sstevel@tonic-gate * Caller must not be holding any locks that may be acquired under 23577c478bd9Sstevel@tonic-gate * zonehash_lock. See comment at the beginning of the file for a list of 23587c478bd9Sstevel@tonic-gate * common locks and their interactions with zones. 23597c478bd9Sstevel@tonic-gate */ 23607c478bd9Sstevel@tonic-gate int 23617c478bd9Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data) 23627c478bd9Sstevel@tonic-gate { 23637c478bd9Sstevel@tonic-gate zone_t *zone; 23647c478bd9Sstevel@tonic-gate int ret = 0; 23657c478bd9Sstevel@tonic-gate zone_status_t status; 23667c478bd9Sstevel@tonic-gate 23677c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 23687c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 23697c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 23707c478bd9Sstevel@tonic-gate /* 23717c478bd9Sstevel@tonic-gate * Skip zones that shouldn't be externally visible. 23727c478bd9Sstevel@tonic-gate */ 23737c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 23747c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 23757c478bd9Sstevel@tonic-gate continue; 23767c478bd9Sstevel@tonic-gate /* 23777c478bd9Sstevel@tonic-gate * Bail immediately if any callback invocation returns a 23787c478bd9Sstevel@tonic-gate * non-zero value. 23797c478bd9Sstevel@tonic-gate */ 23807c478bd9Sstevel@tonic-gate ret = (*cb)(zone, data); 23817c478bd9Sstevel@tonic-gate if (ret != 0) 23827c478bd9Sstevel@tonic-gate break; 23837c478bd9Sstevel@tonic-gate } 23847c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 23857c478bd9Sstevel@tonic-gate return (ret); 23867c478bd9Sstevel@tonic-gate } 23877c478bd9Sstevel@tonic-gate 23887c478bd9Sstevel@tonic-gate static int 23897c478bd9Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath) 23907c478bd9Sstevel@tonic-gate { 23917c478bd9Sstevel@tonic-gate vnode_t *vp; 23927c478bd9Sstevel@tonic-gate int trycount; 23937c478bd9Sstevel@tonic-gate int error = 0; 23947c478bd9Sstevel@tonic-gate char *path; 23957c478bd9Sstevel@tonic-gate struct pathname upn, pn; 23967c478bd9Sstevel@tonic-gate size_t pathlen; 23977c478bd9Sstevel@tonic-gate 23987c478bd9Sstevel@tonic-gate if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 23997c478bd9Sstevel@tonic-gate return (error); 24007c478bd9Sstevel@tonic-gate 24017c478bd9Sstevel@tonic-gate pn_alloc(&pn); 24027c478bd9Sstevel@tonic-gate 24037c478bd9Sstevel@tonic-gate /* prevent infinite loop */ 24047c478bd9Sstevel@tonic-gate trycount = 10; 24057c478bd9Sstevel@tonic-gate for (;;) { 24067c478bd9Sstevel@tonic-gate if (--trycount <= 0) { 24077c478bd9Sstevel@tonic-gate error = ESTALE; 24087c478bd9Sstevel@tonic-gate goto out; 24097c478bd9Sstevel@tonic-gate } 24107c478bd9Sstevel@tonic-gate 24117c478bd9Sstevel@tonic-gate if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 24127c478bd9Sstevel@tonic-gate /* 24137c478bd9Sstevel@tonic-gate * VOP_ACCESS() may cover 'vp' with a new 24147c478bd9Sstevel@tonic-gate * filesystem, if 'vp' is an autoFS vnode. 24157c478bd9Sstevel@tonic-gate * Get the new 'vp' if so. 24167c478bd9Sstevel@tonic-gate */ 24177c478bd9Sstevel@tonic-gate if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 && 2418*25d2dc23Seh208807 (!vn_ismntpt(vp) || 24197c478bd9Sstevel@tonic-gate (error = traverse(&vp)) == 0)) { 24207c478bd9Sstevel@tonic-gate pathlen = pn.pn_pathlen + 2; 24217c478bd9Sstevel@tonic-gate path = kmem_alloc(pathlen, KM_SLEEP); 24227c478bd9Sstevel@tonic-gate (void) strncpy(path, pn.pn_path, 24237c478bd9Sstevel@tonic-gate pn.pn_pathlen + 1); 24247c478bd9Sstevel@tonic-gate path[pathlen - 2] = '/'; 24257c478bd9Sstevel@tonic-gate path[pathlen - 1] = '\0'; 24267c478bd9Sstevel@tonic-gate pn_free(&pn); 24277c478bd9Sstevel@tonic-gate pn_free(&upn); 24287c478bd9Sstevel@tonic-gate 24297c478bd9Sstevel@tonic-gate /* Success! */ 24307c478bd9Sstevel@tonic-gate break; 24317c478bd9Sstevel@tonic-gate } 24327c478bd9Sstevel@tonic-gate VN_RELE(vp); 24337c478bd9Sstevel@tonic-gate } 24347c478bd9Sstevel@tonic-gate if (error != ESTALE) 24357c478bd9Sstevel@tonic-gate goto out; 24367c478bd9Sstevel@tonic-gate } 24377c478bd9Sstevel@tonic-gate 24387c478bd9Sstevel@tonic-gate ASSERT(error == 0); 24397c478bd9Sstevel@tonic-gate zone->zone_rootvp = vp; /* we hold a reference to vp */ 24407c478bd9Sstevel@tonic-gate zone->zone_rootpath = path; 24417c478bd9Sstevel@tonic-gate zone->zone_rootpathlen = pathlen; 244248451833Scarlsonj if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0) 244348451833Scarlsonj zone->zone_flags |= ZF_IS_SCRATCH; 24447c478bd9Sstevel@tonic-gate return (0); 24457c478bd9Sstevel@tonic-gate 24467c478bd9Sstevel@tonic-gate out: 24477c478bd9Sstevel@tonic-gate pn_free(&pn); 24487c478bd9Sstevel@tonic-gate pn_free(&upn); 24497c478bd9Sstevel@tonic-gate return (error); 24507c478bd9Sstevel@tonic-gate } 24517c478bd9Sstevel@tonic-gate 24527c478bd9Sstevel@tonic-gate #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 24537c478bd9Sstevel@tonic-gate ((c) >= 'a' && (c) <= 'z') || \ 24547c478bd9Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'Z')) 24557c478bd9Sstevel@tonic-gate 24567c478bd9Sstevel@tonic-gate static int 24577c478bd9Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname) 24587c478bd9Sstevel@tonic-gate { 24597c478bd9Sstevel@tonic-gate char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 24607c478bd9Sstevel@tonic-gate size_t len; 24617c478bd9Sstevel@tonic-gate int i, err; 24627c478bd9Sstevel@tonic-gate 24637c478bd9Sstevel@tonic-gate if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 24647c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 24657c478bd9Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 24667c478bd9Sstevel@tonic-gate } 24677c478bd9Sstevel@tonic-gate 24687c478bd9Sstevel@tonic-gate /* must be less than ZONENAME_MAX */ 24697c478bd9Sstevel@tonic-gate if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 24707c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 24717c478bd9Sstevel@tonic-gate return (EINVAL); 24727c478bd9Sstevel@tonic-gate } 24737c478bd9Sstevel@tonic-gate 24747c478bd9Sstevel@tonic-gate /* 24757c478bd9Sstevel@tonic-gate * Name must start with an alphanumeric and must contain only 24767c478bd9Sstevel@tonic-gate * alphanumerics, '-', '_' and '.'. 24777c478bd9Sstevel@tonic-gate */ 24787c478bd9Sstevel@tonic-gate if (!isalnum(kname[0])) { 24797c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 24807c478bd9Sstevel@tonic-gate return (EINVAL); 24817c478bd9Sstevel@tonic-gate } 24827c478bd9Sstevel@tonic-gate for (i = 1; i < len - 1; i++) { 24837c478bd9Sstevel@tonic-gate if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 24847c478bd9Sstevel@tonic-gate kname[i] != '.') { 24857c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 24867c478bd9Sstevel@tonic-gate return (EINVAL); 24877c478bd9Sstevel@tonic-gate } 24887c478bd9Sstevel@tonic-gate } 24897c478bd9Sstevel@tonic-gate 24907c478bd9Sstevel@tonic-gate zone->zone_name = kname; 24917c478bd9Sstevel@tonic-gate return (0); 24927c478bd9Sstevel@tonic-gate } 24937c478bd9Sstevel@tonic-gate 24947c478bd9Sstevel@tonic-gate /* 24957c478bd9Sstevel@tonic-gate * Similar to thread_create(), but makes sure the thread is in the appropriate 24967c478bd9Sstevel@tonic-gate * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 24977c478bd9Sstevel@tonic-gate */ 24987c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 24997c478bd9Sstevel@tonic-gate kthread_t * 25007c478bd9Sstevel@tonic-gate zthread_create( 25017c478bd9Sstevel@tonic-gate caddr_t stk, 25027c478bd9Sstevel@tonic-gate size_t stksize, 25037c478bd9Sstevel@tonic-gate void (*proc)(), 25047c478bd9Sstevel@tonic-gate void *arg, 25057c478bd9Sstevel@tonic-gate size_t len, 25067c478bd9Sstevel@tonic-gate pri_t pri) 25077c478bd9Sstevel@tonic-gate { 25087c478bd9Sstevel@tonic-gate kthread_t *t; 25097c478bd9Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 25107c478bd9Sstevel@tonic-gate proc_t *pp = zone->zone_zsched; 25117c478bd9Sstevel@tonic-gate 25127c478bd9Sstevel@tonic-gate zone_hold(zone); /* Reference to be dropped when thread exits */ 25137c478bd9Sstevel@tonic-gate 25147c478bd9Sstevel@tonic-gate /* 25157c478bd9Sstevel@tonic-gate * No-one should be trying to create threads if the zone is shutting 25167c478bd9Sstevel@tonic-gate * down and there aren't any kernel threads around. See comment 25177c478bd9Sstevel@tonic-gate * in zthread_exit(). 25187c478bd9Sstevel@tonic-gate */ 25197c478bd9Sstevel@tonic-gate ASSERT(!(zone->zone_kthreads == NULL && 25207c478bd9Sstevel@tonic-gate zone_status_get(zone) >= ZONE_IS_EMPTY)); 25217c478bd9Sstevel@tonic-gate /* 25227c478bd9Sstevel@tonic-gate * Create a thread, but don't let it run until we've finished setting 25237c478bd9Sstevel@tonic-gate * things up. 25247c478bd9Sstevel@tonic-gate */ 25257c478bd9Sstevel@tonic-gate t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 25267c478bd9Sstevel@tonic-gate ASSERT(t->t_forw == NULL); 25277c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 25287c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 25297c478bd9Sstevel@tonic-gate t->t_forw = t->t_back = t; 25307c478bd9Sstevel@tonic-gate } else { 25317c478bd9Sstevel@tonic-gate kthread_t *tx = zone->zone_kthreads; 25327c478bd9Sstevel@tonic-gate 25337c478bd9Sstevel@tonic-gate t->t_forw = tx; 25347c478bd9Sstevel@tonic-gate t->t_back = tx->t_back; 25357c478bd9Sstevel@tonic-gate tx->t_back->t_forw = t; 25367c478bd9Sstevel@tonic-gate tx->t_back = t; 25377c478bd9Sstevel@tonic-gate } 25387c478bd9Sstevel@tonic-gate zone->zone_kthreads = t; 25397c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 25407c478bd9Sstevel@tonic-gate 25417c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 25427c478bd9Sstevel@tonic-gate t->t_proc_flag |= TP_ZTHREAD; 25437c478bd9Sstevel@tonic-gate project_rele(t->t_proj); 25447c478bd9Sstevel@tonic-gate t->t_proj = project_hold(pp->p_task->tk_proj); 25457c478bd9Sstevel@tonic-gate 25467c478bd9Sstevel@tonic-gate /* 25477c478bd9Sstevel@tonic-gate * Setup complete, let it run. 25487c478bd9Sstevel@tonic-gate */ 25497c478bd9Sstevel@tonic-gate thread_lock(t); 25507c478bd9Sstevel@tonic-gate t->t_schedflag |= TS_ALLSTART; 25517c478bd9Sstevel@tonic-gate setrun_locked(t); 25527c478bd9Sstevel@tonic-gate thread_unlock(t); 25537c478bd9Sstevel@tonic-gate 25547c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 25557c478bd9Sstevel@tonic-gate 25567c478bd9Sstevel@tonic-gate return (t); 25577c478bd9Sstevel@tonic-gate } 25587c478bd9Sstevel@tonic-gate 25597c478bd9Sstevel@tonic-gate /* 25607c478bd9Sstevel@tonic-gate * Similar to thread_exit(). Must be called by threads created via 25617c478bd9Sstevel@tonic-gate * zthread_exit(). 25627c478bd9Sstevel@tonic-gate */ 25637c478bd9Sstevel@tonic-gate void 25647c478bd9Sstevel@tonic-gate zthread_exit(void) 25657c478bd9Sstevel@tonic-gate { 25667c478bd9Sstevel@tonic-gate kthread_t *t = curthread; 25677c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 25687c478bd9Sstevel@tonic-gate zone_t *zone = pp->p_zone; 25697c478bd9Sstevel@tonic-gate 25707c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 25717c478bd9Sstevel@tonic-gate 25727c478bd9Sstevel@tonic-gate /* 25737c478bd9Sstevel@tonic-gate * Reparent to p0 25747c478bd9Sstevel@tonic-gate */ 2575b4b07f87Sjosephb kpreempt_disable(); 25767c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 25777c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_ZTHREAD; 25787c478bd9Sstevel@tonic-gate t->t_procp = &p0; 25797c478bd9Sstevel@tonic-gate hat_thread_exit(t); 25807c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2581b4b07f87Sjosephb kpreempt_enable(); 25827c478bd9Sstevel@tonic-gate 25837c478bd9Sstevel@tonic-gate if (t->t_back == t) { 25847c478bd9Sstevel@tonic-gate ASSERT(t->t_forw == t); 25857c478bd9Sstevel@tonic-gate /* 25867c478bd9Sstevel@tonic-gate * If the zone is empty, once the thread count 25877c478bd9Sstevel@tonic-gate * goes to zero no further kernel threads can be 25887c478bd9Sstevel@tonic-gate * created. This is because if the creator is a process 25897c478bd9Sstevel@tonic-gate * in the zone, then it must have exited before the zone 25907c478bd9Sstevel@tonic-gate * state could be set to ZONE_IS_EMPTY. 25917c478bd9Sstevel@tonic-gate * Otherwise, if the creator is a kernel thread in the 25927c478bd9Sstevel@tonic-gate * zone, the thread count is non-zero. 25937c478bd9Sstevel@tonic-gate * 25947c478bd9Sstevel@tonic-gate * This really means that non-zone kernel threads should 25957c478bd9Sstevel@tonic-gate * not create zone kernel threads. 25967c478bd9Sstevel@tonic-gate */ 25977c478bd9Sstevel@tonic-gate zone->zone_kthreads = NULL; 25987c478bd9Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_EMPTY) { 25997c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 2600c97ad5cdSakolb /* 2601c97ad5cdSakolb * Remove any CPU caps on this zone. 2602c97ad5cdSakolb */ 2603c97ad5cdSakolb cpucaps_zone_remove(zone); 26047c478bd9Sstevel@tonic-gate } 26057c478bd9Sstevel@tonic-gate } else { 26067c478bd9Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 26077c478bd9Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 26087c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == t) 26097c478bd9Sstevel@tonic-gate zone->zone_kthreads = t->t_forw; 26107c478bd9Sstevel@tonic-gate } 26117c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 26127c478bd9Sstevel@tonic-gate zone_rele(zone); 26137c478bd9Sstevel@tonic-gate thread_exit(); 26147c478bd9Sstevel@tonic-gate /* NOTREACHED */ 26157c478bd9Sstevel@tonic-gate } 26167c478bd9Sstevel@tonic-gate 26177c478bd9Sstevel@tonic-gate static void 26187c478bd9Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 26197c478bd9Sstevel@tonic-gate { 26207c478bd9Sstevel@tonic-gate vnode_t *oldvp; 26217c478bd9Sstevel@tonic-gate 26227c478bd9Sstevel@tonic-gate /* we're going to hold a reference here to the directory */ 26237c478bd9Sstevel@tonic-gate VN_HOLD(vp); 26247c478bd9Sstevel@tonic-gate 26257c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT 26267c478bd9Sstevel@tonic-gate if (audit_active) /* update abs cwd/root path see c2audit.c */ 26277c478bd9Sstevel@tonic-gate audit_chdirec(vp, vpp); 26287c478bd9Sstevel@tonic-gate #endif 26297c478bd9Sstevel@tonic-gate 26307c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 26317c478bd9Sstevel@tonic-gate oldvp = *vpp; 26327c478bd9Sstevel@tonic-gate *vpp = vp; 26337c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 26347c478bd9Sstevel@tonic-gate if (oldvp != NULL) 26357c478bd9Sstevel@tonic-gate VN_RELE(oldvp); 26367c478bd9Sstevel@tonic-gate } 26377c478bd9Sstevel@tonic-gate 26387c478bd9Sstevel@tonic-gate /* 26397c478bd9Sstevel@tonic-gate * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 26407c478bd9Sstevel@tonic-gate */ 26417c478bd9Sstevel@tonic-gate static int 26427c478bd9Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 26437c478bd9Sstevel@tonic-gate { 26447c478bd9Sstevel@tonic-gate nvpair_t *nvp = NULL; 26457c478bd9Sstevel@tonic-gate boolean_t priv_set = B_FALSE; 26467c478bd9Sstevel@tonic-gate boolean_t limit_set = B_FALSE; 26477c478bd9Sstevel@tonic-gate boolean_t action_set = B_FALSE; 26487c478bd9Sstevel@tonic-gate 26497c478bd9Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 26507c478bd9Sstevel@tonic-gate const char *name; 26517c478bd9Sstevel@tonic-gate uint64_t ui64; 26527c478bd9Sstevel@tonic-gate 26537c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 26547c478bd9Sstevel@tonic-gate if (nvpair_type(nvp) != DATA_TYPE_UINT64) 26557c478bd9Sstevel@tonic-gate return (EINVAL); 26567c478bd9Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &ui64); 26577c478bd9Sstevel@tonic-gate if (strcmp(name, "privilege") == 0) { 26587c478bd9Sstevel@tonic-gate /* 26597c478bd9Sstevel@tonic-gate * Currently only privileged values are allowed, but 26607c478bd9Sstevel@tonic-gate * this may change in the future. 26617c478bd9Sstevel@tonic-gate */ 26627c478bd9Sstevel@tonic-gate if (ui64 != RCPRIV_PRIVILEGED) 26637c478bd9Sstevel@tonic-gate return (EINVAL); 26647c478bd9Sstevel@tonic-gate rv->rcv_privilege = ui64; 26657c478bd9Sstevel@tonic-gate priv_set = B_TRUE; 26667c478bd9Sstevel@tonic-gate } else if (strcmp(name, "limit") == 0) { 26677c478bd9Sstevel@tonic-gate rv->rcv_value = ui64; 26687c478bd9Sstevel@tonic-gate limit_set = B_TRUE; 26697c478bd9Sstevel@tonic-gate } else if (strcmp(name, "action") == 0) { 26707c478bd9Sstevel@tonic-gate if (ui64 != RCTL_LOCAL_NOACTION && 26717c478bd9Sstevel@tonic-gate ui64 != RCTL_LOCAL_DENY) 26727c478bd9Sstevel@tonic-gate return (EINVAL); 26737c478bd9Sstevel@tonic-gate rv->rcv_flagaction = ui64; 26747c478bd9Sstevel@tonic-gate action_set = B_TRUE; 26757c478bd9Sstevel@tonic-gate } else { 26767c478bd9Sstevel@tonic-gate return (EINVAL); 26777c478bd9Sstevel@tonic-gate } 26787c478bd9Sstevel@tonic-gate } 26797c478bd9Sstevel@tonic-gate 26807c478bd9Sstevel@tonic-gate if (!(priv_set && limit_set && action_set)) 26817c478bd9Sstevel@tonic-gate return (EINVAL); 26827c478bd9Sstevel@tonic-gate rv->rcv_action_signal = 0; 26837c478bd9Sstevel@tonic-gate rv->rcv_action_recipient = NULL; 26847c478bd9Sstevel@tonic-gate rv->rcv_action_recip_pid = -1; 26857c478bd9Sstevel@tonic-gate rv->rcv_firing_time = 0; 26867c478bd9Sstevel@tonic-gate 26877c478bd9Sstevel@tonic-gate return (0); 26887c478bd9Sstevel@tonic-gate } 26897c478bd9Sstevel@tonic-gate 26903f2f09c1Sdp /* 26913f2f09c1Sdp * Non-global zone version of start_init. 26923f2f09c1Sdp */ 26937c478bd9Sstevel@tonic-gate void 26943f2f09c1Sdp zone_start_init(void) 26957c478bd9Sstevel@tonic-gate { 26967c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 26979acbbeafSnn35248 zone_t *z = p->p_zone; 26983f2f09c1Sdp 26993f2f09c1Sdp ASSERT(!INGLOBALZONE(curproc)); 27007c478bd9Sstevel@tonic-gate 27017c478bd9Sstevel@tonic-gate /* 27029acbbeafSnn35248 * For all purposes (ZONE_ATTR_INITPID and restart_init), 27039acbbeafSnn35248 * storing just the pid of init is sufficient. 27049acbbeafSnn35248 */ 27059acbbeafSnn35248 z->zone_proc_initpid = p->p_pid; 27069acbbeafSnn35248 27079acbbeafSnn35248 /* 27083f2f09c1Sdp * We maintain zone_boot_err so that we can return the cause of the 27093f2f09c1Sdp * failure back to the caller of the zone_boot syscall. 27107c478bd9Sstevel@tonic-gate */ 27113f2f09c1Sdp p->p_zone->zone_boot_err = start_init_common(); 27127c478bd9Sstevel@tonic-gate 27137c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 27149acbbeafSnn35248 if (z->zone_boot_err != 0) { 27157c478bd9Sstevel@tonic-gate /* 27167c478bd9Sstevel@tonic-gate * Make sure we are still in the booting state-- we could have 27177c478bd9Sstevel@tonic-gate * raced and already be shutting down, or even further along. 27187c478bd9Sstevel@tonic-gate */ 2719c97ad5cdSakolb if (zone_status_get(z) == ZONE_IS_BOOTING) { 27209acbbeafSnn35248 zone_status_set(z, ZONE_IS_SHUTTING_DOWN); 2721c97ad5cdSakolb } 27227c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 27237c478bd9Sstevel@tonic-gate /* It's gone bad, dispose of the process */ 27249acbbeafSnn35248 if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) { 272597eda132Sraf mutex_enter(&p->p_lock); 272697eda132Sraf ASSERT(p->p_flag & SEXITLWPS); 27277c478bd9Sstevel@tonic-gate lwp_exit(); 27287c478bd9Sstevel@tonic-gate } 27297c478bd9Sstevel@tonic-gate } else { 27309acbbeafSnn35248 if (zone_status_get(z) == ZONE_IS_BOOTING) 27319acbbeafSnn35248 zone_status_set(z, ZONE_IS_RUNNING); 27327c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 27337c478bd9Sstevel@tonic-gate /* cause the process to return to userland. */ 27347c478bd9Sstevel@tonic-gate lwp_rtt(); 27357c478bd9Sstevel@tonic-gate } 27367c478bd9Sstevel@tonic-gate } 27377c478bd9Sstevel@tonic-gate 27387c478bd9Sstevel@tonic-gate struct zsched_arg { 27397c478bd9Sstevel@tonic-gate zone_t *zone; 27407c478bd9Sstevel@tonic-gate nvlist_t *nvlist; 27417c478bd9Sstevel@tonic-gate }; 27427c478bd9Sstevel@tonic-gate 27437c478bd9Sstevel@tonic-gate /* 27447c478bd9Sstevel@tonic-gate * Per-zone "sched" workalike. The similarity to "sched" doesn't have 27457c478bd9Sstevel@tonic-gate * anything to do with scheduling, but rather with the fact that 27467c478bd9Sstevel@tonic-gate * per-zone kernel threads are parented to zsched, just like regular 27477c478bd9Sstevel@tonic-gate * kernel threads are parented to sched (p0). 27487c478bd9Sstevel@tonic-gate * 27497c478bd9Sstevel@tonic-gate * zsched is also responsible for launching init for the zone. 27507c478bd9Sstevel@tonic-gate */ 27517c478bd9Sstevel@tonic-gate static void 27527c478bd9Sstevel@tonic-gate zsched(void *arg) 27537c478bd9Sstevel@tonic-gate { 27547c478bd9Sstevel@tonic-gate struct zsched_arg *za = arg; 27557c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 27567c478bd9Sstevel@tonic-gate proc_t *initp = proc_init; 27577c478bd9Sstevel@tonic-gate zone_t *zone = za->zone; 27587c478bd9Sstevel@tonic-gate cred_t *cr, *oldcred; 27597c478bd9Sstevel@tonic-gate rctl_set_t *set; 27607c478bd9Sstevel@tonic-gate rctl_alloc_gp_t *gp; 27617c478bd9Sstevel@tonic-gate contract_t *ct = NULL; 27627c478bd9Sstevel@tonic-gate task_t *tk, *oldtk; 27637c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 27647c478bd9Sstevel@tonic-gate kproject_t *pj; 27657c478bd9Sstevel@tonic-gate 27667c478bd9Sstevel@tonic-gate nvlist_t *nvl = za->nvlist; 27677c478bd9Sstevel@tonic-gate nvpair_t *nvp = NULL; 27687c478bd9Sstevel@tonic-gate 2769ae115bc7Smrj bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched")); 2770ae115bc7Smrj bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched")); 2771ae115bc7Smrj PTOU(pp)->u_argc = 0; 2772ae115bc7Smrj PTOU(pp)->u_argv = NULL; 2773ae115bc7Smrj PTOU(pp)->u_envp = NULL; 27747c478bd9Sstevel@tonic-gate closeall(P_FINFO(pp)); 27757c478bd9Sstevel@tonic-gate 27767c478bd9Sstevel@tonic-gate /* 27777c478bd9Sstevel@tonic-gate * We are this zone's "zsched" process. As the zone isn't generally 27787c478bd9Sstevel@tonic-gate * visible yet we don't need to grab any locks before initializing its 27797c478bd9Sstevel@tonic-gate * zone_proc pointer. 27807c478bd9Sstevel@tonic-gate */ 27817c478bd9Sstevel@tonic-gate zone_hold(zone); /* this hold is released by zone_destroy() */ 27827c478bd9Sstevel@tonic-gate zone->zone_zsched = pp; 27837c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 27847c478bd9Sstevel@tonic-gate pp->p_zone = zone; 27857c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 27867c478bd9Sstevel@tonic-gate 27877c478bd9Sstevel@tonic-gate /* 27887c478bd9Sstevel@tonic-gate * Disassociate process from its 'parent'; parent ourselves to init 27897c478bd9Sstevel@tonic-gate * (pid 1) and change other values as needed. 27907c478bd9Sstevel@tonic-gate */ 27917c478bd9Sstevel@tonic-gate sess_create(); 27927c478bd9Sstevel@tonic-gate 27937c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 27947c478bd9Sstevel@tonic-gate proc_detach(pp); 27957c478bd9Sstevel@tonic-gate pp->p_ppid = 1; 27967c478bd9Sstevel@tonic-gate pp->p_flag |= SZONETOP; 27977c478bd9Sstevel@tonic-gate pp->p_ancpid = 1; 27987c478bd9Sstevel@tonic-gate pp->p_parent = initp; 27997c478bd9Sstevel@tonic-gate pp->p_psibling = NULL; 28007c478bd9Sstevel@tonic-gate if (initp->p_child) 28017c478bd9Sstevel@tonic-gate initp->p_child->p_psibling = pp; 28027c478bd9Sstevel@tonic-gate pp->p_sibling = initp->p_child; 28037c478bd9Sstevel@tonic-gate initp->p_child = pp; 28047c478bd9Sstevel@tonic-gate 28057c478bd9Sstevel@tonic-gate /* Decrement what newproc() incremented. */ 28067c478bd9Sstevel@tonic-gate upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 28077c478bd9Sstevel@tonic-gate /* 28087c478bd9Sstevel@tonic-gate * Our credentials are about to become kcred-like, so we don't care 28097c478bd9Sstevel@tonic-gate * about the caller's ruid. 28107c478bd9Sstevel@tonic-gate */ 28117c478bd9Sstevel@tonic-gate upcount_inc(crgetruid(kcred), zone->zone_id); 28127c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 28137c478bd9Sstevel@tonic-gate 28147c478bd9Sstevel@tonic-gate /* 28157c478bd9Sstevel@tonic-gate * getting out of global zone, so decrement lwp counts 28167c478bd9Sstevel@tonic-gate */ 28177c478bd9Sstevel@tonic-gate pj = pp->p_task->tk_proj; 28187c478bd9Sstevel@tonic-gate mutex_enter(&global_zone->zone_nlwps_lock); 28197c478bd9Sstevel@tonic-gate pj->kpj_nlwps -= pp->p_lwpcnt; 28207c478bd9Sstevel@tonic-gate global_zone->zone_nlwps -= pp->p_lwpcnt; 28217c478bd9Sstevel@tonic-gate mutex_exit(&global_zone->zone_nlwps_lock); 28227c478bd9Sstevel@tonic-gate 28237c478bd9Sstevel@tonic-gate /* 2824c6939658Ssl108498 * Decrement locked memory counts on old zone and project. 2825c6939658Ssl108498 */ 28260209230bSgjelinek mutex_enter(&global_zone->zone_mem_lock); 2827c6939658Ssl108498 global_zone->zone_locked_mem -= pp->p_locked_mem; 2828c6939658Ssl108498 pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 28290209230bSgjelinek mutex_exit(&global_zone->zone_mem_lock); 2830c6939658Ssl108498 2831c6939658Ssl108498 /* 28327c478bd9Sstevel@tonic-gate * Create and join a new task in project '0' of this zone. 28337c478bd9Sstevel@tonic-gate * 28347c478bd9Sstevel@tonic-gate * We don't need to call holdlwps() since we know we're the only lwp in 28357c478bd9Sstevel@tonic-gate * this process. 28367c478bd9Sstevel@tonic-gate * 28377c478bd9Sstevel@tonic-gate * task_join() returns with p_lock held. 28387c478bd9Sstevel@tonic-gate */ 28397c478bd9Sstevel@tonic-gate tk = task_create(0, zone); 28407c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 28417c478bd9Sstevel@tonic-gate oldtk = task_join(tk, 0); 2842c6939658Ssl108498 2843c6939658Ssl108498 pj = pp->p_task->tk_proj; 2844c6939658Ssl108498 28450209230bSgjelinek mutex_enter(&zone->zone_mem_lock); 2846c6939658Ssl108498 zone->zone_locked_mem += pp->p_locked_mem; 2847c6939658Ssl108498 pj->kpj_data.kpd_locked_mem += pp->p_locked_mem; 28480209230bSgjelinek mutex_exit(&zone->zone_mem_lock); 28497c478bd9Sstevel@tonic-gate 28507c478bd9Sstevel@tonic-gate /* 28517c478bd9Sstevel@tonic-gate * add lwp counts to zsched's zone, and increment project's task count 28527c478bd9Sstevel@tonic-gate * due to the task created in the above tasksys_settaskid 28537c478bd9Sstevel@tonic-gate */ 2854c6939658Ssl108498 28557c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 28567c478bd9Sstevel@tonic-gate pj->kpj_nlwps += pp->p_lwpcnt; 28577c478bd9Sstevel@tonic-gate pj->kpj_ntasks += 1; 28587c478bd9Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 28597c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 28607c478bd9Sstevel@tonic-gate 2861c6939658Ssl108498 mutex_exit(&curproc->p_lock); 2862c6939658Ssl108498 mutex_exit(&cpu_lock); 2863c6939658Ssl108498 task_rele(oldtk); 2864c6939658Ssl108498 28657c478bd9Sstevel@tonic-gate /* 28667c478bd9Sstevel@tonic-gate * The process was created by a process in the global zone, hence the 28677c478bd9Sstevel@tonic-gate * credentials are wrong. We might as well have kcred-ish credentials. 28687c478bd9Sstevel@tonic-gate */ 28697c478bd9Sstevel@tonic-gate cr = zone->zone_kcred; 28707c478bd9Sstevel@tonic-gate crhold(cr); 28717c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 28727c478bd9Sstevel@tonic-gate oldcred = pp->p_cred; 28737c478bd9Sstevel@tonic-gate pp->p_cred = cr; 28747c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 28757c478bd9Sstevel@tonic-gate crfree(oldcred); 28767c478bd9Sstevel@tonic-gate 28777c478bd9Sstevel@tonic-gate /* 28787c478bd9Sstevel@tonic-gate * Hold credentials again (for thread) 28797c478bd9Sstevel@tonic-gate */ 28807c478bd9Sstevel@tonic-gate crhold(cr); 28817c478bd9Sstevel@tonic-gate 28827c478bd9Sstevel@tonic-gate /* 28837c478bd9Sstevel@tonic-gate * p_lwpcnt can't change since this is a kernel process. 28847c478bd9Sstevel@tonic-gate */ 28857c478bd9Sstevel@tonic-gate crset(pp, cr); 28867c478bd9Sstevel@tonic-gate 28877c478bd9Sstevel@tonic-gate /* 28887c478bd9Sstevel@tonic-gate * Chroot 28897c478bd9Sstevel@tonic-gate */ 28907c478bd9Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 28917c478bd9Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 28927c478bd9Sstevel@tonic-gate 28937c478bd9Sstevel@tonic-gate /* 28947c478bd9Sstevel@tonic-gate * Initialize zone's rctl set. 28957c478bd9Sstevel@tonic-gate */ 28967c478bd9Sstevel@tonic-gate set = rctl_set_create(); 28977c478bd9Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 28987c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 28997c478bd9Sstevel@tonic-gate e.rcep_p.zone = zone; 29007c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 29017c478bd9Sstevel@tonic-gate zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 29027c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 29037c478bd9Sstevel@tonic-gate rctl_prealloc_destroy(gp); 29047c478bd9Sstevel@tonic-gate 29057c478bd9Sstevel@tonic-gate /* 29067c478bd9Sstevel@tonic-gate * Apply the rctls passed in to zone_create(). This is basically a list 29077c478bd9Sstevel@tonic-gate * assignment: all of the old values are removed and the new ones 29087c478bd9Sstevel@tonic-gate * inserted. That is, if an empty list is passed in, all values are 29097c478bd9Sstevel@tonic-gate * removed. 29107c478bd9Sstevel@tonic-gate */ 29117c478bd9Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 29127c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rde; 29137c478bd9Sstevel@tonic-gate rctl_hndl_t hndl; 29147c478bd9Sstevel@tonic-gate char *name; 29157c478bd9Sstevel@tonic-gate nvlist_t **nvlarray; 29167c478bd9Sstevel@tonic-gate uint_t i, nelem; 29177c478bd9Sstevel@tonic-gate int error; /* For ASSERT()s */ 29187c478bd9Sstevel@tonic-gate 29197c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 29207c478bd9Sstevel@tonic-gate hndl = rctl_hndl_lookup(name); 29217c478bd9Sstevel@tonic-gate ASSERT(hndl != -1); 29227c478bd9Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 29237c478bd9Sstevel@tonic-gate ASSERT(rde != NULL); 29247c478bd9Sstevel@tonic-gate 29257c478bd9Sstevel@tonic-gate for (; /* ever */; ) { 29267c478bd9Sstevel@tonic-gate rctl_val_t oval; 29277c478bd9Sstevel@tonic-gate 29287c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 29297c478bd9Sstevel@tonic-gate error = rctl_local_get(hndl, NULL, &oval, pp); 29307c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 29317c478bd9Sstevel@tonic-gate ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 29327c478bd9Sstevel@tonic-gate ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 29337c478bd9Sstevel@tonic-gate if (oval.rcv_privilege == RCPRIV_SYSTEM) 29347c478bd9Sstevel@tonic-gate break; 29357c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 29367c478bd9Sstevel@tonic-gate error = rctl_local_delete(hndl, &oval, pp); 29377c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 29387c478bd9Sstevel@tonic-gate ASSERT(error == 0); 29397c478bd9Sstevel@tonic-gate } 29407c478bd9Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 29417c478bd9Sstevel@tonic-gate ASSERT(error == 0); 29427c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 29437c478bd9Sstevel@tonic-gate rctl_val_t *nvalp; 29447c478bd9Sstevel@tonic-gate 29457c478bd9Sstevel@tonic-gate nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 29467c478bd9Sstevel@tonic-gate error = nvlist2rctlval(nvlarray[i], nvalp); 29477c478bd9Sstevel@tonic-gate ASSERT(error == 0); 29487c478bd9Sstevel@tonic-gate /* 29497c478bd9Sstevel@tonic-gate * rctl_local_insert can fail if the value being 29507c478bd9Sstevel@tonic-gate * inserted is a duplicate; this is OK. 29517c478bd9Sstevel@tonic-gate */ 29527c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 29537c478bd9Sstevel@tonic-gate if (rctl_local_insert(hndl, nvalp, pp) != 0) 29547c478bd9Sstevel@tonic-gate kmem_cache_free(rctl_val_cache, nvalp); 29557c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 29567c478bd9Sstevel@tonic-gate } 29577c478bd9Sstevel@tonic-gate } 29587c478bd9Sstevel@tonic-gate /* 29597c478bd9Sstevel@tonic-gate * Tell the world that we're done setting up. 29607c478bd9Sstevel@tonic-gate * 29617c478bd9Sstevel@tonic-gate * At this point we want to set the zone status to ZONE_IS_READY 29627c478bd9Sstevel@tonic-gate * and atomically set the zone's processor set visibility. Once 29637c478bd9Sstevel@tonic-gate * we drop pool_lock() this zone will automatically get updated 29647c478bd9Sstevel@tonic-gate * to reflect any future changes to the pools configuration. 29657c478bd9Sstevel@tonic-gate */ 29667c478bd9Sstevel@tonic-gate pool_lock(); 29677c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 29687c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 29697c478bd9Sstevel@tonic-gate zone_uniqid(zone); 29707c478bd9Sstevel@tonic-gate zone_zsd_configure(zone); 29717c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) 29727c478bd9Sstevel@tonic-gate zone_pset_set(zone, pool_default->pool_pset->pset_id); 29737c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 29747c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 29757c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_READY); 29767c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 29777c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 29787c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 29797c478bd9Sstevel@tonic-gate pool_unlock(); 29807c478bd9Sstevel@tonic-gate 29817c478bd9Sstevel@tonic-gate /* 29827c478bd9Sstevel@tonic-gate * Once we see the zone transition to the ZONE_IS_BOOTING state, 29837c478bd9Sstevel@tonic-gate * we launch init, and set the state to running. 29847c478bd9Sstevel@tonic-gate */ 29857c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 29867c478bd9Sstevel@tonic-gate 29877c478bd9Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_BOOTING) { 29887c478bd9Sstevel@tonic-gate id_t cid; 29897c478bd9Sstevel@tonic-gate 29907c478bd9Sstevel@tonic-gate /* 29917c478bd9Sstevel@tonic-gate * Ok, this is a little complicated. We need to grab the 29927c478bd9Sstevel@tonic-gate * zone's pool's scheduling class ID; note that by now, we 29937c478bd9Sstevel@tonic-gate * are already bound to a pool if we need to be (zoneadmd 29947c478bd9Sstevel@tonic-gate * will have done that to us while we're in the READY 29957c478bd9Sstevel@tonic-gate * state). *But* the scheduling class for the zone's 'init' 29967c478bd9Sstevel@tonic-gate * must be explicitly passed to newproc, which doesn't 29977c478bd9Sstevel@tonic-gate * respect pool bindings. 29987c478bd9Sstevel@tonic-gate * 29997c478bd9Sstevel@tonic-gate * We hold the pool_lock across the call to newproc() to 30007c478bd9Sstevel@tonic-gate * close the obvious race: the pool's scheduling class 30017c478bd9Sstevel@tonic-gate * could change before we manage to create the LWP with 30027c478bd9Sstevel@tonic-gate * classid 'cid'. 30037c478bd9Sstevel@tonic-gate */ 30047c478bd9Sstevel@tonic-gate pool_lock(); 30050209230bSgjelinek if (zone->zone_defaultcid > 0) 30060209230bSgjelinek cid = zone->zone_defaultcid; 30070209230bSgjelinek else 30087c478bd9Sstevel@tonic-gate cid = pool_get_class(zone->zone_pool); 30097c478bd9Sstevel@tonic-gate if (cid == -1) 30107c478bd9Sstevel@tonic-gate cid = defaultcid; 30117c478bd9Sstevel@tonic-gate 30127c478bd9Sstevel@tonic-gate /* 30137c478bd9Sstevel@tonic-gate * If this fails, zone_boot will ultimately fail. The 30147c478bd9Sstevel@tonic-gate * state of the zone will be set to SHUTTING_DOWN-- userland 30157c478bd9Sstevel@tonic-gate * will have to tear down the zone, and fail, or try again. 30167c478bd9Sstevel@tonic-gate */ 30173f2f09c1Sdp if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid, 30187c478bd9Sstevel@tonic-gate minclsyspri - 1, &ct)) != 0) { 30197c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 30207c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 30217c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 30227c478bd9Sstevel@tonic-gate } 30237c478bd9Sstevel@tonic-gate pool_unlock(); 30247c478bd9Sstevel@tonic-gate } 30257c478bd9Sstevel@tonic-gate 30267c478bd9Sstevel@tonic-gate /* 30277c478bd9Sstevel@tonic-gate * Wait for zone_destroy() to be called. This is what we spend 30287c478bd9Sstevel@tonic-gate * most of our life doing. 30297c478bd9Sstevel@tonic-gate */ 30307c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 30317c478bd9Sstevel@tonic-gate 30327c478bd9Sstevel@tonic-gate if (ct) 30337c478bd9Sstevel@tonic-gate /* 30347c478bd9Sstevel@tonic-gate * At this point the process contract should be empty. 30357c478bd9Sstevel@tonic-gate * (Though if it isn't, it's not the end of the world.) 30367c478bd9Sstevel@tonic-gate */ 30377c478bd9Sstevel@tonic-gate VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 30387c478bd9Sstevel@tonic-gate 30397c478bd9Sstevel@tonic-gate /* 30407c478bd9Sstevel@tonic-gate * Allow kcred to be freed when all referring processes 30417c478bd9Sstevel@tonic-gate * (including this one) go away. We can't just do this in 30427c478bd9Sstevel@tonic-gate * zone_free because we need to wait for the zone_cred_ref to 30437c478bd9Sstevel@tonic-gate * drop to 0 before calling zone_free, and the existence of 30447c478bd9Sstevel@tonic-gate * zone_kcred will prevent that. Thus, we call crfree here to 30457c478bd9Sstevel@tonic-gate * balance the crdup in zone_create. The crhold calls earlier 30467c478bd9Sstevel@tonic-gate * in zsched will be dropped when the thread and process exit. 30477c478bd9Sstevel@tonic-gate */ 30487c478bd9Sstevel@tonic-gate crfree(zone->zone_kcred); 30497c478bd9Sstevel@tonic-gate zone->zone_kcred = NULL; 30507c478bd9Sstevel@tonic-gate 30517c478bd9Sstevel@tonic-gate exit(CLD_EXITED, 0); 30527c478bd9Sstevel@tonic-gate } 30537c478bd9Sstevel@tonic-gate 30547c478bd9Sstevel@tonic-gate /* 30557c478bd9Sstevel@tonic-gate * Helper function to determine if there are any submounts of the 30567c478bd9Sstevel@tonic-gate * provided path. Used to make sure the zone doesn't "inherit" any 30577c478bd9Sstevel@tonic-gate * mounts from before it is created. 30587c478bd9Sstevel@tonic-gate */ 30597c478bd9Sstevel@tonic-gate static uint_t 30607c478bd9Sstevel@tonic-gate zone_mount_count(const char *rootpath) 30617c478bd9Sstevel@tonic-gate { 30627c478bd9Sstevel@tonic-gate vfs_t *vfsp; 30637c478bd9Sstevel@tonic-gate uint_t count = 0; 30647c478bd9Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 30657c478bd9Sstevel@tonic-gate 30667c478bd9Sstevel@tonic-gate /* 30677c478bd9Sstevel@tonic-gate * Holding zonehash_lock prevents race conditions with 30687c478bd9Sstevel@tonic-gate * vfs_list_add()/vfs_list_remove() since we serialize with 30697c478bd9Sstevel@tonic-gate * zone_find_by_path(). 30707c478bd9Sstevel@tonic-gate */ 30717c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 30727c478bd9Sstevel@tonic-gate /* 30737c478bd9Sstevel@tonic-gate * The rootpath must end with a '/' 30747c478bd9Sstevel@tonic-gate */ 30757c478bd9Sstevel@tonic-gate ASSERT(rootpath[rootpathlen - 1] == '/'); 30767c478bd9Sstevel@tonic-gate 30777c478bd9Sstevel@tonic-gate /* 30787c478bd9Sstevel@tonic-gate * This intentionally does not count the rootpath itself if that 30797c478bd9Sstevel@tonic-gate * happens to be a mount point. 30807c478bd9Sstevel@tonic-gate */ 30817c478bd9Sstevel@tonic-gate vfs_list_read_lock(); 30827c478bd9Sstevel@tonic-gate vfsp = rootvfs; 30837c478bd9Sstevel@tonic-gate do { 30847c478bd9Sstevel@tonic-gate if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 30857c478bd9Sstevel@tonic-gate rootpathlen) == 0) 30867c478bd9Sstevel@tonic-gate count++; 30877c478bd9Sstevel@tonic-gate vfsp = vfsp->vfs_next; 30887c478bd9Sstevel@tonic-gate } while (vfsp != rootvfs); 30897c478bd9Sstevel@tonic-gate vfs_list_unlock(); 30907c478bd9Sstevel@tonic-gate return (count); 30917c478bd9Sstevel@tonic-gate } 30927c478bd9Sstevel@tonic-gate 30937c478bd9Sstevel@tonic-gate /* 30947c478bd9Sstevel@tonic-gate * Helper function to make sure that a zone created on 'rootpath' 30957c478bd9Sstevel@tonic-gate * wouldn't end up containing other zones' rootpaths. 30967c478bd9Sstevel@tonic-gate */ 30977c478bd9Sstevel@tonic-gate static boolean_t 30987c478bd9Sstevel@tonic-gate zone_is_nested(const char *rootpath) 30997c478bd9Sstevel@tonic-gate { 31007c478bd9Sstevel@tonic-gate zone_t *zone; 31017c478bd9Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 31027c478bd9Sstevel@tonic-gate size_t len; 31037c478bd9Sstevel@tonic-gate 31047c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 31057c478bd9Sstevel@tonic-gate 31067c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 31077c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 31087c478bd9Sstevel@tonic-gate if (zone == global_zone) 31097c478bd9Sstevel@tonic-gate continue; 31107c478bd9Sstevel@tonic-gate len = strlen(zone->zone_rootpath); 31117c478bd9Sstevel@tonic-gate if (strncmp(rootpath, zone->zone_rootpath, 31127c478bd9Sstevel@tonic-gate MIN(rootpathlen, len)) == 0) 31137c478bd9Sstevel@tonic-gate return (B_TRUE); 31147c478bd9Sstevel@tonic-gate } 31157c478bd9Sstevel@tonic-gate return (B_FALSE); 31167c478bd9Sstevel@tonic-gate } 31177c478bd9Sstevel@tonic-gate 31187c478bd9Sstevel@tonic-gate static int 3119821c4a97Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs, 3120821c4a97Sdp size_t zone_privssz) 31217c478bd9Sstevel@tonic-gate { 31227c478bd9Sstevel@tonic-gate priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 31237c478bd9Sstevel@tonic-gate 3124821c4a97Sdp if (zone_privssz < sizeof (priv_set_t)) 3125821c4a97Sdp return (set_errno(ENOMEM)); 3126821c4a97Sdp 31277c478bd9Sstevel@tonic-gate if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 31287c478bd9Sstevel@tonic-gate kmem_free(privs, sizeof (priv_set_t)); 31297c478bd9Sstevel@tonic-gate return (EFAULT); 31307c478bd9Sstevel@tonic-gate } 31317c478bd9Sstevel@tonic-gate 31327c478bd9Sstevel@tonic-gate zone->zone_privset = privs; 31337c478bd9Sstevel@tonic-gate return (0); 31347c478bd9Sstevel@tonic-gate } 31357c478bd9Sstevel@tonic-gate 31367c478bd9Sstevel@tonic-gate /* 31377c478bd9Sstevel@tonic-gate * We make creative use of nvlists to pass in rctls from userland. The list is 31387c478bd9Sstevel@tonic-gate * a list of the following structures: 31397c478bd9Sstevel@tonic-gate * 31407c478bd9Sstevel@tonic-gate * (name = rctl_name, value = nvpair_list_array) 31417c478bd9Sstevel@tonic-gate * 31427c478bd9Sstevel@tonic-gate * Where each element of the nvpair_list_array is of the form: 31437c478bd9Sstevel@tonic-gate * 31447c478bd9Sstevel@tonic-gate * [(name = "privilege", value = RCPRIV_PRIVILEGED), 31457c478bd9Sstevel@tonic-gate * (name = "limit", value = uint64_t), 31467c478bd9Sstevel@tonic-gate * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 31477c478bd9Sstevel@tonic-gate */ 31487c478bd9Sstevel@tonic-gate static int 31497c478bd9Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 31507c478bd9Sstevel@tonic-gate { 31517c478bd9Sstevel@tonic-gate nvpair_t *nvp = NULL; 31527c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL; 31537c478bd9Sstevel@tonic-gate char *kbuf; 31547c478bd9Sstevel@tonic-gate int error; 31557c478bd9Sstevel@tonic-gate rctl_val_t rv; 31567c478bd9Sstevel@tonic-gate 31577c478bd9Sstevel@tonic-gate *nvlp = NULL; 31587c478bd9Sstevel@tonic-gate 31597c478bd9Sstevel@tonic-gate if (buflen == 0) 31607c478bd9Sstevel@tonic-gate return (0); 31617c478bd9Sstevel@tonic-gate 31627c478bd9Sstevel@tonic-gate if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 31637c478bd9Sstevel@tonic-gate return (ENOMEM); 31647c478bd9Sstevel@tonic-gate if (copyin(ubuf, kbuf, buflen)) { 31657c478bd9Sstevel@tonic-gate error = EFAULT; 31667c478bd9Sstevel@tonic-gate goto out; 31677c478bd9Sstevel@tonic-gate } 31687c478bd9Sstevel@tonic-gate if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 31697c478bd9Sstevel@tonic-gate /* 31707c478bd9Sstevel@tonic-gate * nvl may have been allocated/free'd, but the value set to 31717c478bd9Sstevel@tonic-gate * non-NULL, so we reset it here. 31727c478bd9Sstevel@tonic-gate */ 31737c478bd9Sstevel@tonic-gate nvl = NULL; 31747c478bd9Sstevel@tonic-gate error = EINVAL; 31757c478bd9Sstevel@tonic-gate goto out; 31767c478bd9Sstevel@tonic-gate } 31777c478bd9Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 31787c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rde; 31797c478bd9Sstevel@tonic-gate rctl_hndl_t hndl; 31807c478bd9Sstevel@tonic-gate nvlist_t **nvlarray; 31817c478bd9Sstevel@tonic-gate uint_t i, nelem; 31827c478bd9Sstevel@tonic-gate char *name; 31837c478bd9Sstevel@tonic-gate 31847c478bd9Sstevel@tonic-gate error = EINVAL; 31857c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 31867c478bd9Sstevel@tonic-gate if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 31877c478bd9Sstevel@tonic-gate != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 31887c478bd9Sstevel@tonic-gate goto out; 31897c478bd9Sstevel@tonic-gate } 31907c478bd9Sstevel@tonic-gate if ((hndl = rctl_hndl_lookup(name)) == -1) { 31917c478bd9Sstevel@tonic-gate goto out; 31927c478bd9Sstevel@tonic-gate } 31937c478bd9Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 31947c478bd9Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 31957c478bd9Sstevel@tonic-gate ASSERT(error == 0); 31967c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 31977c478bd9Sstevel@tonic-gate if (error = nvlist2rctlval(nvlarray[i], &rv)) 31987c478bd9Sstevel@tonic-gate goto out; 31997c478bd9Sstevel@tonic-gate } 32007c478bd9Sstevel@tonic-gate if (rctl_invalid_value(rde, &rv)) { 32017c478bd9Sstevel@tonic-gate error = EINVAL; 32027c478bd9Sstevel@tonic-gate goto out; 32037c478bd9Sstevel@tonic-gate } 32047c478bd9Sstevel@tonic-gate } 32057c478bd9Sstevel@tonic-gate error = 0; 32067c478bd9Sstevel@tonic-gate *nvlp = nvl; 32077c478bd9Sstevel@tonic-gate out: 32087c478bd9Sstevel@tonic-gate kmem_free(kbuf, buflen); 32097c478bd9Sstevel@tonic-gate if (error && nvl != NULL) 32107c478bd9Sstevel@tonic-gate nvlist_free(nvl); 32117c478bd9Sstevel@tonic-gate return (error); 32127c478bd9Sstevel@tonic-gate } 32137c478bd9Sstevel@tonic-gate 32147c478bd9Sstevel@tonic-gate int 32157c478bd9Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) { 32167c478bd9Sstevel@tonic-gate if (er_out != NULL) { 32177c478bd9Sstevel@tonic-gate if (copyout(&er_ext, er_out, sizeof (int))) { 32187c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 32197c478bd9Sstevel@tonic-gate } 32207c478bd9Sstevel@tonic-gate } 32217c478bd9Sstevel@tonic-gate return (set_errno(er_error)); 32227c478bd9Sstevel@tonic-gate } 32237c478bd9Sstevel@tonic-gate 322445916cd2Sjpk static int 322545916cd2Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi) 322645916cd2Sjpk { 322745916cd2Sjpk ts_label_t *tsl; 322845916cd2Sjpk bslabel_t blab; 322945916cd2Sjpk 323045916cd2Sjpk /* Get label from user */ 323145916cd2Sjpk if (copyin(lab, &blab, sizeof (blab)) != 0) 323245916cd2Sjpk return (EFAULT); 323345916cd2Sjpk tsl = labelalloc(&blab, doi, KM_NOSLEEP); 323445916cd2Sjpk if (tsl == NULL) 323545916cd2Sjpk return (ENOMEM); 323645916cd2Sjpk 323745916cd2Sjpk zone->zone_slabel = tsl; 323845916cd2Sjpk return (0); 323945916cd2Sjpk } 324045916cd2Sjpk 32417c478bd9Sstevel@tonic-gate /* 3242fa9e4066Sahrens * Parses a comma-separated list of ZFS datasets into a per-zone dictionary. 3243fa9e4066Sahrens */ 3244fa9e4066Sahrens static int 3245fa9e4066Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen) 3246fa9e4066Sahrens { 3247fa9e4066Sahrens char *kbuf; 3248fa9e4066Sahrens char *dataset, *next; 3249fa9e4066Sahrens zone_dataset_t *zd; 3250fa9e4066Sahrens size_t len; 3251fa9e4066Sahrens 3252fa9e4066Sahrens if (ubuf == NULL || buflen == 0) 3253fa9e4066Sahrens return (0); 3254fa9e4066Sahrens 3255fa9e4066Sahrens if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 3256fa9e4066Sahrens return (ENOMEM); 3257fa9e4066Sahrens 3258fa9e4066Sahrens if (copyin(ubuf, kbuf, buflen) != 0) { 3259fa9e4066Sahrens kmem_free(kbuf, buflen); 3260fa9e4066Sahrens return (EFAULT); 3261fa9e4066Sahrens } 3262fa9e4066Sahrens 3263fa9e4066Sahrens dataset = next = kbuf; 3264fa9e4066Sahrens for (;;) { 3265fa9e4066Sahrens zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP); 3266fa9e4066Sahrens 3267fa9e4066Sahrens next = strchr(dataset, ','); 3268fa9e4066Sahrens 3269fa9e4066Sahrens if (next == NULL) 3270fa9e4066Sahrens len = strlen(dataset); 3271fa9e4066Sahrens else 3272fa9e4066Sahrens len = next - dataset; 3273fa9e4066Sahrens 3274fa9e4066Sahrens zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP); 3275fa9e4066Sahrens bcopy(dataset, zd->zd_dataset, len); 3276fa9e4066Sahrens zd->zd_dataset[len] = '\0'; 3277fa9e4066Sahrens 3278fa9e4066Sahrens list_insert_head(&zone->zone_datasets, zd); 3279fa9e4066Sahrens 3280fa9e4066Sahrens if (next == NULL) 3281fa9e4066Sahrens break; 3282fa9e4066Sahrens 3283fa9e4066Sahrens dataset = next + 1; 3284fa9e4066Sahrens } 3285fa9e4066Sahrens 3286fa9e4066Sahrens kmem_free(kbuf, buflen); 3287fa9e4066Sahrens return (0); 3288fa9e4066Sahrens } 3289fa9e4066Sahrens 3290fa9e4066Sahrens /* 32917c478bd9Sstevel@tonic-gate * System call to create/initialize a new zone named 'zone_name', rooted 32927c478bd9Sstevel@tonic-gate * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 329345916cd2Sjpk * and initialized with the zone-wide rctls described in 'rctlbuf', and 329445916cd2Sjpk * with labeling set by 'match', 'doi', and 'label'. 32957c478bd9Sstevel@tonic-gate * 32967c478bd9Sstevel@tonic-gate * If extended error is non-null, we may use it to return more detailed 32977c478bd9Sstevel@tonic-gate * error information. 32987c478bd9Sstevel@tonic-gate */ 32997c478bd9Sstevel@tonic-gate static zoneid_t 33007c478bd9Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root, 3301821c4a97Sdp const priv_set_t *zone_privs, size_t zone_privssz, 3302821c4a97Sdp caddr_t rctlbuf, size_t rctlbufsz, 330345916cd2Sjpk caddr_t zfsbuf, size_t zfsbufsz, int *extended_error, 3304f4b3ec61Sdh155122 int match, uint32_t doi, const bslabel_t *label, 3305f4b3ec61Sdh155122 int flags) 33067c478bd9Sstevel@tonic-gate { 33077c478bd9Sstevel@tonic-gate struct zsched_arg zarg; 33087c478bd9Sstevel@tonic-gate nvlist_t *rctls = NULL; 33097c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 33107c478bd9Sstevel@tonic-gate zone_t *zone, *ztmp; 33117c478bd9Sstevel@tonic-gate zoneid_t zoneid; 33127c478bd9Sstevel@tonic-gate int error; 33137c478bd9Sstevel@tonic-gate int error2 = 0; 33147c478bd9Sstevel@tonic-gate char *str; 33157c478bd9Sstevel@tonic-gate cred_t *zkcr; 331648451833Scarlsonj boolean_t insert_label_hash; 33177c478bd9Sstevel@tonic-gate 33187c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 33197c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 33207c478bd9Sstevel@tonic-gate 33217c478bd9Sstevel@tonic-gate /* can't boot zone from within chroot environment */ 33227c478bd9Sstevel@tonic-gate if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 33237c478bd9Sstevel@tonic-gate return (zone_create_error(ENOTSUP, ZE_CHROOTED, 33247c478bd9Sstevel@tonic-gate extended_error)); 33257c478bd9Sstevel@tonic-gate 33267c478bd9Sstevel@tonic-gate zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 33277c478bd9Sstevel@tonic-gate zoneid = zone->zone_id = id_alloc(zoneid_space); 33287c478bd9Sstevel@tonic-gate zone->zone_status = ZONE_IS_UNINITIALIZED; 33297c478bd9Sstevel@tonic-gate zone->zone_pool = pool_default; 33307c478bd9Sstevel@tonic-gate zone->zone_pool_mod = gethrtime(); 33317c478bd9Sstevel@tonic-gate zone->zone_psetid = ZONE_PS_INVAL; 33327c478bd9Sstevel@tonic-gate zone->zone_ncpus = 0; 33337c478bd9Sstevel@tonic-gate zone->zone_ncpus_online = 0; 33349acbbeafSnn35248 zone->zone_restart_init = B_TRUE; 33359acbbeafSnn35248 zone->zone_brand = &native_brand; 33369acbbeafSnn35248 zone->zone_initname = NULL; 33377c478bd9Sstevel@tonic-gate mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 33387c478bd9Sstevel@tonic-gate mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 33390209230bSgjelinek mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 33407c478bd9Sstevel@tonic-gate cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 33417c478bd9Sstevel@tonic-gate list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 33427c478bd9Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 3343fa9e4066Sahrens list_create(&zone->zone_datasets, sizeof (zone_dataset_t), 3344fa9e4066Sahrens offsetof(zone_dataset_t, zd_linkage)); 334545916cd2Sjpk rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 33467c478bd9Sstevel@tonic-gate 3347f4b3ec61Sdh155122 if (flags & ZCF_NET_EXCL) { 3348f4b3ec61Sdh155122 zone->zone_flags |= ZF_NET_EXCL; 3349f4b3ec61Sdh155122 } 3350f4b3ec61Sdh155122 33517c478bd9Sstevel@tonic-gate if ((error = zone_set_name(zone, zone_name)) != 0) { 33527c478bd9Sstevel@tonic-gate zone_free(zone); 33537c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 33547c478bd9Sstevel@tonic-gate } 33557c478bd9Sstevel@tonic-gate 33567c478bd9Sstevel@tonic-gate if ((error = zone_set_root(zone, zone_root)) != 0) { 33577c478bd9Sstevel@tonic-gate zone_free(zone); 33587c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 33597c478bd9Sstevel@tonic-gate } 3360821c4a97Sdp if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) { 33617c478bd9Sstevel@tonic-gate zone_free(zone); 33627c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 33637c478bd9Sstevel@tonic-gate } 33647c478bd9Sstevel@tonic-gate 33657c478bd9Sstevel@tonic-gate /* initialize node name to be the same as zone name */ 33667c478bd9Sstevel@tonic-gate zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 33677c478bd9Sstevel@tonic-gate (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 33687c478bd9Sstevel@tonic-gate zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 33697c478bd9Sstevel@tonic-gate 33707c478bd9Sstevel@tonic-gate zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 33717c478bd9Sstevel@tonic-gate zone->zone_domain[0] = '\0'; 33727c478bd9Sstevel@tonic-gate zone->zone_shares = 1; 3373824c205fSml93401 zone->zone_shmmax = 0; 3374824c205fSml93401 zone->zone_ipc.ipcq_shmmni = 0; 3375824c205fSml93401 zone->zone_ipc.ipcq_semmni = 0; 3376824c205fSml93401 zone->zone_ipc.ipcq_msgmni = 0; 33777c478bd9Sstevel@tonic-gate zone->zone_bootargs = NULL; 33783f2f09c1Sdp zone->zone_initname = 33793f2f09c1Sdp kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP); 33803f2f09c1Sdp (void) strcpy(zone->zone_initname, zone_default_initname); 33810209230bSgjelinek zone->zone_nlwps = 0; 33820209230bSgjelinek zone->zone_nlwps_ctl = INT_MAX; 3383c6939658Ssl108498 zone->zone_locked_mem = 0; 3384c6939658Ssl108498 zone->zone_locked_mem_ctl = UINT64_MAX; 33850209230bSgjelinek zone->zone_max_swap = 0; 33860209230bSgjelinek zone->zone_max_swap_ctl = UINT64_MAX; 33870209230bSgjelinek zone0.zone_lockedmem_kstat = NULL; 33880209230bSgjelinek zone0.zone_swapresv_kstat = NULL; 33897c478bd9Sstevel@tonic-gate 33907c478bd9Sstevel@tonic-gate /* 33917c478bd9Sstevel@tonic-gate * Zsched initializes the rctls. 33927c478bd9Sstevel@tonic-gate */ 33937c478bd9Sstevel@tonic-gate zone->zone_rctls = NULL; 33947c478bd9Sstevel@tonic-gate 33957c478bd9Sstevel@tonic-gate if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 33967c478bd9Sstevel@tonic-gate zone_free(zone); 33977c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 33987c478bd9Sstevel@tonic-gate } 33997c478bd9Sstevel@tonic-gate 3400fa9e4066Sahrens if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) { 3401fa9e4066Sahrens zone_free(zone); 3402fa9e4066Sahrens return (set_errno(error)); 3403fa9e4066Sahrens } 3404fa9e4066Sahrens 34057c478bd9Sstevel@tonic-gate /* 340645916cd2Sjpk * Read in the trusted system parameters: 340745916cd2Sjpk * match flag and sensitivity label. 340845916cd2Sjpk */ 340945916cd2Sjpk zone->zone_match = match; 341048451833Scarlsonj if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 341145916cd2Sjpk error = zone_set_label(zone, label, doi); 341245916cd2Sjpk if (error != 0) { 341345916cd2Sjpk zone_free(zone); 341445916cd2Sjpk return (set_errno(error)); 341545916cd2Sjpk } 341648451833Scarlsonj insert_label_hash = B_TRUE; 341745916cd2Sjpk } else { 341845916cd2Sjpk /* all zones get an admin_low label if system is not labeled */ 341945916cd2Sjpk zone->zone_slabel = l_admin_low; 342045916cd2Sjpk label_hold(l_admin_low); 342148451833Scarlsonj insert_label_hash = B_FALSE; 342245916cd2Sjpk } 342345916cd2Sjpk 342445916cd2Sjpk /* 34257c478bd9Sstevel@tonic-gate * Stop all lwps since that's what normally happens as part of fork(). 34267c478bd9Sstevel@tonic-gate * This needs to happen before we grab any locks to avoid deadlock 34277c478bd9Sstevel@tonic-gate * (another lwp in the process could be waiting for the held lock). 34287c478bd9Sstevel@tonic-gate */ 34297c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 34307c478bd9Sstevel@tonic-gate zone_free(zone); 34317c478bd9Sstevel@tonic-gate if (rctls) 34327c478bd9Sstevel@tonic-gate nvlist_free(rctls); 34337c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 34347c478bd9Sstevel@tonic-gate } 34357c478bd9Sstevel@tonic-gate 34367c478bd9Sstevel@tonic-gate if (block_mounts() == 0) { 34377c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 34387c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 34397c478bd9Sstevel@tonic-gate continuelwps(pp); 34407c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 34417c478bd9Sstevel@tonic-gate zone_free(zone); 34427c478bd9Sstevel@tonic-gate if (rctls) 34437c478bd9Sstevel@tonic-gate nvlist_free(rctls); 34447c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 34457c478bd9Sstevel@tonic-gate } 34467c478bd9Sstevel@tonic-gate 34477c478bd9Sstevel@tonic-gate /* 34487c478bd9Sstevel@tonic-gate * Set up credential for kernel access. After this, any errors 34497c478bd9Sstevel@tonic-gate * should go through the dance in errout rather than calling 34507c478bd9Sstevel@tonic-gate * zone_free directly. 34517c478bd9Sstevel@tonic-gate */ 34527c478bd9Sstevel@tonic-gate zone->zone_kcred = crdup(kcred); 34537c478bd9Sstevel@tonic-gate crsetzone(zone->zone_kcred, zone); 34547c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 34557c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 34567c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 34577c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 34587c478bd9Sstevel@tonic-gate 34597c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 34607c478bd9Sstevel@tonic-gate /* 34617c478bd9Sstevel@tonic-gate * Make sure zone doesn't already exist. 346245916cd2Sjpk * 346345916cd2Sjpk * If the system and zone are labeled, 346445916cd2Sjpk * make sure no other zone exists that has the same label. 34657c478bd9Sstevel@tonic-gate */ 346645916cd2Sjpk if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL || 346748451833Scarlsonj (insert_label_hash && 346845916cd2Sjpk (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) { 34697c478bd9Sstevel@tonic-gate zone_status_t status; 34707c478bd9Sstevel@tonic-gate 34717c478bd9Sstevel@tonic-gate status = zone_status_get(ztmp); 34727c478bd9Sstevel@tonic-gate if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 34737c478bd9Sstevel@tonic-gate error = EEXIST; 34747c478bd9Sstevel@tonic-gate else 34757c478bd9Sstevel@tonic-gate error = EBUSY; 34767c478bd9Sstevel@tonic-gate goto errout; 34777c478bd9Sstevel@tonic-gate } 34787c478bd9Sstevel@tonic-gate 34797c478bd9Sstevel@tonic-gate /* 34807c478bd9Sstevel@tonic-gate * Don't allow zone creations which would cause one zone's rootpath to 34817c478bd9Sstevel@tonic-gate * be accessible from that of another (non-global) zone. 34827c478bd9Sstevel@tonic-gate */ 34837c478bd9Sstevel@tonic-gate if (zone_is_nested(zone->zone_rootpath)) { 34847c478bd9Sstevel@tonic-gate error = EBUSY; 34857c478bd9Sstevel@tonic-gate goto errout; 34867c478bd9Sstevel@tonic-gate } 34877c478bd9Sstevel@tonic-gate 34887c478bd9Sstevel@tonic-gate ASSERT(zonecount != 0); /* check for leaks */ 34897c478bd9Sstevel@tonic-gate if (zonecount + 1 > maxzones) { 34907c478bd9Sstevel@tonic-gate error = ENOMEM; 34917c478bd9Sstevel@tonic-gate goto errout; 34927c478bd9Sstevel@tonic-gate } 34937c478bd9Sstevel@tonic-gate 34947c478bd9Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 34957c478bd9Sstevel@tonic-gate error = EBUSY; 34967c478bd9Sstevel@tonic-gate error2 = ZE_AREMOUNTS; 34977c478bd9Sstevel@tonic-gate goto errout; 34987c478bd9Sstevel@tonic-gate } 34997c478bd9Sstevel@tonic-gate 35007c478bd9Sstevel@tonic-gate /* 35017c478bd9Sstevel@tonic-gate * Zone is still incomplete, but we need to drop all locks while 35027c478bd9Sstevel@tonic-gate * zsched() initializes this zone's kernel process. We 35037c478bd9Sstevel@tonic-gate * optimistically add the zone to the hashtable and associated 35047c478bd9Sstevel@tonic-gate * lists so a parallel zone_create() doesn't try to create the 35057c478bd9Sstevel@tonic-gate * same zone. 35067c478bd9Sstevel@tonic-gate */ 35077c478bd9Sstevel@tonic-gate zonecount++; 35087c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, 35097c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id, 35107c478bd9Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 35117c478bd9Sstevel@tonic-gate str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 35127c478bd9Sstevel@tonic-gate (void) strcpy(str, zone->zone_name); 35137c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 35147c478bd9Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 351548451833Scarlsonj if (insert_label_hash) { 351645916cd2Sjpk (void) mod_hash_insert(zonehashbylabel, 351745916cd2Sjpk (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone); 351848451833Scarlsonj zone->zone_flags |= ZF_HASHED_LABEL; 351945916cd2Sjpk } 352045916cd2Sjpk 35217c478bd9Sstevel@tonic-gate /* 35227c478bd9Sstevel@tonic-gate * Insert into active list. At this point there are no 'hold's 35237c478bd9Sstevel@tonic-gate * on the zone, but everyone else knows not to use it, so we can 35247c478bd9Sstevel@tonic-gate * continue to use it. zsched() will do a zone_hold() if the 35257c478bd9Sstevel@tonic-gate * newproc() is successful. 35267c478bd9Sstevel@tonic-gate */ 35277c478bd9Sstevel@tonic-gate list_insert_tail(&zone_active, zone); 35287c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35297c478bd9Sstevel@tonic-gate 35307c478bd9Sstevel@tonic-gate zarg.zone = zone; 35317c478bd9Sstevel@tonic-gate zarg.nvlist = rctls; 35327c478bd9Sstevel@tonic-gate /* 35337c478bd9Sstevel@tonic-gate * The process, task, and project rctls are probably wrong; 35347c478bd9Sstevel@tonic-gate * we need an interface to get the default values of all rctls, 35357c478bd9Sstevel@tonic-gate * and initialize zsched appropriately. I'm not sure that that 35367c478bd9Sstevel@tonic-gate * makes much of a difference, though. 35377c478bd9Sstevel@tonic-gate */ 35387c478bd9Sstevel@tonic-gate if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) { 35397c478bd9Sstevel@tonic-gate /* 35407c478bd9Sstevel@tonic-gate * We need to undo all globally visible state. 35417c478bd9Sstevel@tonic-gate */ 35427c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 35437c478bd9Sstevel@tonic-gate list_remove(&zone_active, zone); 354448451833Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) { 354545916cd2Sjpk ASSERT(zone->zone_slabel != NULL); 354645916cd2Sjpk (void) mod_hash_destroy(zonehashbylabel, 354745916cd2Sjpk (mod_hash_key_t)zone->zone_slabel); 354845916cd2Sjpk } 35497c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 35507c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_name); 35517c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 35527c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 35537c478bd9Sstevel@tonic-gate ASSERT(zonecount > 1); 35547c478bd9Sstevel@tonic-gate zonecount--; 35557c478bd9Sstevel@tonic-gate goto errout; 35567c478bd9Sstevel@tonic-gate } 35577c478bd9Sstevel@tonic-gate 35587c478bd9Sstevel@tonic-gate /* 35597c478bd9Sstevel@tonic-gate * Zone creation can't fail from now on. 35607c478bd9Sstevel@tonic-gate */ 35617c478bd9Sstevel@tonic-gate 35627c478bd9Sstevel@tonic-gate /* 35630209230bSgjelinek * Create zone kstats 35640209230bSgjelinek */ 35650209230bSgjelinek zone_kstat_create(zone); 35660209230bSgjelinek 35670209230bSgjelinek /* 35687c478bd9Sstevel@tonic-gate * Let the other lwps continue. 35697c478bd9Sstevel@tonic-gate */ 35707c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 35717c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 35727c478bd9Sstevel@tonic-gate continuelwps(pp); 35737c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 35747c478bd9Sstevel@tonic-gate 35757c478bd9Sstevel@tonic-gate /* 35767c478bd9Sstevel@tonic-gate * Wait for zsched to finish initializing the zone. 35777c478bd9Sstevel@tonic-gate */ 35787c478bd9Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_READY); 35797c478bd9Sstevel@tonic-gate /* 35807c478bd9Sstevel@tonic-gate * The zone is fully visible, so we can let mounts progress. 35817c478bd9Sstevel@tonic-gate */ 35827c478bd9Sstevel@tonic-gate resume_mounts(); 35837c478bd9Sstevel@tonic-gate if (rctls) 35847c478bd9Sstevel@tonic-gate nvlist_free(rctls); 35857c478bd9Sstevel@tonic-gate 35867c478bd9Sstevel@tonic-gate return (zoneid); 35877c478bd9Sstevel@tonic-gate 35887c478bd9Sstevel@tonic-gate errout: 35897c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35907c478bd9Sstevel@tonic-gate /* 35917c478bd9Sstevel@tonic-gate * Let the other lwps continue. 35927c478bd9Sstevel@tonic-gate */ 35937c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 35947c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 35957c478bd9Sstevel@tonic-gate continuelwps(pp); 35967c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 35977c478bd9Sstevel@tonic-gate 35987c478bd9Sstevel@tonic-gate resume_mounts(); 35997c478bd9Sstevel@tonic-gate if (rctls) 36007c478bd9Sstevel@tonic-gate nvlist_free(rctls); 36017c478bd9Sstevel@tonic-gate /* 36027c478bd9Sstevel@tonic-gate * There is currently one reference to the zone, a cred_ref from 36037c478bd9Sstevel@tonic-gate * zone_kcred. To free the zone, we call crfree, which will call 36047c478bd9Sstevel@tonic-gate * zone_cred_rele, which will call zone_free. 36057c478bd9Sstevel@tonic-gate */ 36067c478bd9Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 36077c478bd9Sstevel@tonic-gate ASSERT(zone->zone_kcred->cr_ref == 1); 36087c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 36097c478bd9Sstevel@tonic-gate zkcr = zone->zone_kcred; 36107c478bd9Sstevel@tonic-gate zone->zone_kcred = NULL; 36117c478bd9Sstevel@tonic-gate crfree(zkcr); /* triggers call to zone_free */ 36127c478bd9Sstevel@tonic-gate return (zone_create_error(error, error2, extended_error)); 36137c478bd9Sstevel@tonic-gate } 36147c478bd9Sstevel@tonic-gate 36157c478bd9Sstevel@tonic-gate /* 36167c478bd9Sstevel@tonic-gate * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 36173f2f09c1Sdp * the heavy lifting. initname is the path to the program to launch 36183f2f09c1Sdp * at the "top" of the zone; if this is NULL, we use the system default, 36193f2f09c1Sdp * which is stored at zone_default_initname. 36207c478bd9Sstevel@tonic-gate */ 36217c478bd9Sstevel@tonic-gate static int 36223f2f09c1Sdp zone_boot(zoneid_t zoneid) 36237c478bd9Sstevel@tonic-gate { 36247c478bd9Sstevel@tonic-gate int err; 36257c478bd9Sstevel@tonic-gate zone_t *zone; 36267c478bd9Sstevel@tonic-gate 36277c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 36287c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 36297c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 36307c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 36317c478bd9Sstevel@tonic-gate 36327c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 36337c478bd9Sstevel@tonic-gate /* 36347c478bd9Sstevel@tonic-gate * Look for zone under hash lock to prevent races with calls to 36357c478bd9Sstevel@tonic-gate * zone_shutdown, zone_destroy, etc. 36367c478bd9Sstevel@tonic-gate */ 36377c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 36387c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36397c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 36407c478bd9Sstevel@tonic-gate } 36417c478bd9Sstevel@tonic-gate 36427c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 36437c478bd9Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_READY) { 36447c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 36457c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36467c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 36477c478bd9Sstevel@tonic-gate } 36487c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_BOOTING); 36497c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 36507c478bd9Sstevel@tonic-gate 36517c478bd9Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 36527c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36537c478bd9Sstevel@tonic-gate 36547c478bd9Sstevel@tonic-gate if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 36557c478bd9Sstevel@tonic-gate zone_rele(zone); 36567c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 36577c478bd9Sstevel@tonic-gate } 36587c478bd9Sstevel@tonic-gate 36597c478bd9Sstevel@tonic-gate /* 36607c478bd9Sstevel@tonic-gate * Boot (starting init) might have failed, in which case the zone 36617c478bd9Sstevel@tonic-gate * will go to the SHUTTING_DOWN state; an appropriate errno will 36627c478bd9Sstevel@tonic-gate * be placed in zone->zone_boot_err, and so we return that. 36637c478bd9Sstevel@tonic-gate */ 36647c478bd9Sstevel@tonic-gate err = zone->zone_boot_err; 36657c478bd9Sstevel@tonic-gate zone_rele(zone); 36667c478bd9Sstevel@tonic-gate return (err ? set_errno(err) : 0); 36677c478bd9Sstevel@tonic-gate } 36687c478bd9Sstevel@tonic-gate 36697c478bd9Sstevel@tonic-gate /* 36707c478bd9Sstevel@tonic-gate * Kills all user processes in the zone, waiting for them all to exit 36717c478bd9Sstevel@tonic-gate * before returning. 36727c478bd9Sstevel@tonic-gate */ 36737c478bd9Sstevel@tonic-gate static int 36747c478bd9Sstevel@tonic-gate zone_empty(zone_t *zone) 36757c478bd9Sstevel@tonic-gate { 36767c478bd9Sstevel@tonic-gate int waitstatus; 36777c478bd9Sstevel@tonic-gate 36787c478bd9Sstevel@tonic-gate /* 36797c478bd9Sstevel@tonic-gate * We need to drop zonehash_lock before killing all 36807c478bd9Sstevel@tonic-gate * processes, otherwise we'll deadlock with zone_find_* 36817c478bd9Sstevel@tonic-gate * which can be called from the exit path. 36827c478bd9Sstevel@tonic-gate */ 36837c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 36847c478bd9Sstevel@tonic-gate while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz, 36857c478bd9Sstevel@tonic-gate ZONE_IS_EMPTY)) == -1) { 36867c478bd9Sstevel@tonic-gate killall(zone->zone_id); 36877c478bd9Sstevel@tonic-gate } 36887c478bd9Sstevel@tonic-gate /* 36897c478bd9Sstevel@tonic-gate * return EINTR if we were signaled 36907c478bd9Sstevel@tonic-gate */ 36917c478bd9Sstevel@tonic-gate if (waitstatus == 0) 36927c478bd9Sstevel@tonic-gate return (EINTR); 36937c478bd9Sstevel@tonic-gate return (0); 36947c478bd9Sstevel@tonic-gate } 36957c478bd9Sstevel@tonic-gate 36967c478bd9Sstevel@tonic-gate /* 369745916cd2Sjpk * This function implements the policy for zone visibility. 369845916cd2Sjpk * 369945916cd2Sjpk * In standard Solaris, a non-global zone can only see itself. 370045916cd2Sjpk * 370145916cd2Sjpk * In Trusted Extensions, a labeled zone can lookup any zone whose label 370245916cd2Sjpk * it dominates. For this test, the label of the global zone is treated as 370345916cd2Sjpk * admin_high so it is special-cased instead of being checked for dominance. 370445916cd2Sjpk * 370545916cd2Sjpk * Returns true if zone attributes are viewable, false otherwise. 370645916cd2Sjpk */ 370745916cd2Sjpk static boolean_t 370845916cd2Sjpk zone_list_access(zone_t *zone) 370945916cd2Sjpk { 371045916cd2Sjpk 371145916cd2Sjpk if (curproc->p_zone == global_zone || 371245916cd2Sjpk curproc->p_zone == zone) { 371345916cd2Sjpk return (B_TRUE); 371448451833Scarlsonj } else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 371545916cd2Sjpk bslabel_t *curproc_label; 371645916cd2Sjpk bslabel_t *zone_label; 371745916cd2Sjpk 371845916cd2Sjpk curproc_label = label2bslabel(curproc->p_zone->zone_slabel); 371945916cd2Sjpk zone_label = label2bslabel(zone->zone_slabel); 372045916cd2Sjpk 372145916cd2Sjpk if (zone->zone_id != GLOBAL_ZONEID && 372245916cd2Sjpk bldominates(curproc_label, zone_label)) { 372345916cd2Sjpk return (B_TRUE); 372445916cd2Sjpk } else { 372545916cd2Sjpk return (B_FALSE); 372645916cd2Sjpk } 372745916cd2Sjpk } else { 372845916cd2Sjpk return (B_FALSE); 372945916cd2Sjpk } 373045916cd2Sjpk } 373145916cd2Sjpk 373245916cd2Sjpk /* 37337c478bd9Sstevel@tonic-gate * Systemcall to start the zone's halt sequence. By the time this 37347c478bd9Sstevel@tonic-gate * function successfully returns, all user processes and kernel threads 37357c478bd9Sstevel@tonic-gate * executing in it will have exited, ZSD shutdown callbacks executed, 37367c478bd9Sstevel@tonic-gate * and the zone status set to ZONE_IS_DOWN. 37377c478bd9Sstevel@tonic-gate * 37387c478bd9Sstevel@tonic-gate * It is possible that the call will interrupt itself if the caller is the 37397c478bd9Sstevel@tonic-gate * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 37407c478bd9Sstevel@tonic-gate */ 37417c478bd9Sstevel@tonic-gate static int 37427c478bd9Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 37437c478bd9Sstevel@tonic-gate { 37447c478bd9Sstevel@tonic-gate int error; 37457c478bd9Sstevel@tonic-gate zone_t *zone; 37467c478bd9Sstevel@tonic-gate zone_status_t status; 37477c478bd9Sstevel@tonic-gate 37487c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 37497c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 37507c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 37517c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 37527c478bd9Sstevel@tonic-gate 37537c478bd9Sstevel@tonic-gate /* 37547c478bd9Sstevel@tonic-gate * Block mounts so that VFS_MOUNT() can get an accurate view of 37557c478bd9Sstevel@tonic-gate * the zone's status with regards to ZONE_IS_SHUTTING down. 37567c478bd9Sstevel@tonic-gate * 37577c478bd9Sstevel@tonic-gate * e.g. NFS can fail the mount if it determines that the zone 37587c478bd9Sstevel@tonic-gate * has already begun the shutdown sequence. 37597c478bd9Sstevel@tonic-gate */ 37607c478bd9Sstevel@tonic-gate if (block_mounts() == 0) 37617c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 37627c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 37637c478bd9Sstevel@tonic-gate /* 37647c478bd9Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 37657c478bd9Sstevel@tonic-gate * calls to zone_shutdown and zone_destroy. 37667c478bd9Sstevel@tonic-gate */ 37677c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 37687c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 37697c478bd9Sstevel@tonic-gate resume_mounts(); 37707c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 37717c478bd9Sstevel@tonic-gate } 37727c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 37737c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 37747c478bd9Sstevel@tonic-gate /* 37757c478bd9Sstevel@tonic-gate * Fail if the zone isn't fully initialized yet. 37767c478bd9Sstevel@tonic-gate */ 37777c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY) { 37787c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 37797c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 37807c478bd9Sstevel@tonic-gate resume_mounts(); 37817c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 37827c478bd9Sstevel@tonic-gate } 37837c478bd9Sstevel@tonic-gate /* 37847c478bd9Sstevel@tonic-gate * If conditions required for zone_shutdown() to return have been met, 37857c478bd9Sstevel@tonic-gate * return success. 37867c478bd9Sstevel@tonic-gate */ 37877c478bd9Sstevel@tonic-gate if (status >= ZONE_IS_DOWN) { 37887c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 37897c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 37907c478bd9Sstevel@tonic-gate resume_mounts(); 37917c478bd9Sstevel@tonic-gate return (0); 37927c478bd9Sstevel@tonic-gate } 37937c478bd9Sstevel@tonic-gate /* 37947c478bd9Sstevel@tonic-gate * If zone_shutdown() hasn't been called before, go through the motions. 37957c478bd9Sstevel@tonic-gate * If it has, there's nothing to do but wait for the kernel threads to 37967c478bd9Sstevel@tonic-gate * drain. 37977c478bd9Sstevel@tonic-gate */ 37987c478bd9Sstevel@tonic-gate if (status < ZONE_IS_EMPTY) { 37997c478bd9Sstevel@tonic-gate uint_t ntasks; 38007c478bd9Sstevel@tonic-gate 38017c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 38027c478bd9Sstevel@tonic-gate if ((ntasks = zone->zone_ntasks) != 1) { 38037c478bd9Sstevel@tonic-gate /* 38047c478bd9Sstevel@tonic-gate * There's still stuff running. 38057c478bd9Sstevel@tonic-gate */ 38067c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 38077c478bd9Sstevel@tonic-gate } 38087c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 38097c478bd9Sstevel@tonic-gate if (ntasks == 1) { 38107c478bd9Sstevel@tonic-gate /* 38117c478bd9Sstevel@tonic-gate * The only way to create another task is through 38127c478bd9Sstevel@tonic-gate * zone_enter(), which will block until we drop 38137c478bd9Sstevel@tonic-gate * zonehash_lock. The zone is empty. 38147c478bd9Sstevel@tonic-gate */ 38157c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 38167c478bd9Sstevel@tonic-gate /* 38177c478bd9Sstevel@tonic-gate * Skip ahead to ZONE_IS_DOWN 38187c478bd9Sstevel@tonic-gate */ 38197c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 38207c478bd9Sstevel@tonic-gate } else { 38217c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 38227c478bd9Sstevel@tonic-gate } 38237c478bd9Sstevel@tonic-gate } 38247c478bd9Sstevel@tonic-gate } 38257c478bd9Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 38267c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 38277c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38287c478bd9Sstevel@tonic-gate resume_mounts(); 38297c478bd9Sstevel@tonic-gate 38307c478bd9Sstevel@tonic-gate if (error = zone_empty(zone)) { 38317c478bd9Sstevel@tonic-gate zone_rele(zone); 38327c478bd9Sstevel@tonic-gate return (set_errno(error)); 38337c478bd9Sstevel@tonic-gate } 38347c478bd9Sstevel@tonic-gate /* 38357c478bd9Sstevel@tonic-gate * After the zone status goes to ZONE_IS_DOWN this zone will no 38367c478bd9Sstevel@tonic-gate * longer be notified of changes to the pools configuration, so 38377c478bd9Sstevel@tonic-gate * in order to not end up with a stale pool pointer, we point 38387c478bd9Sstevel@tonic-gate * ourselves at the default pool and remove all resource 38397c478bd9Sstevel@tonic-gate * visibility. This is especially important as the zone_t may 38407c478bd9Sstevel@tonic-gate * languish on the deathrow for a very long time waiting for 38417c478bd9Sstevel@tonic-gate * cred's to drain out. 38427c478bd9Sstevel@tonic-gate * 38437c478bd9Sstevel@tonic-gate * This rebinding of the zone can happen multiple times 38447c478bd9Sstevel@tonic-gate * (presumably due to interrupted or parallel systemcalls) 38457c478bd9Sstevel@tonic-gate * without any adverse effects. 38467c478bd9Sstevel@tonic-gate */ 38477c478bd9Sstevel@tonic-gate if (pool_lock_intr() != 0) { 38487c478bd9Sstevel@tonic-gate zone_rele(zone); 38497c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 38507c478bd9Sstevel@tonic-gate } 38517c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 38527c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 38537c478bd9Sstevel@tonic-gate zone_pool_set(zone, pool_default); 38547c478bd9Sstevel@tonic-gate /* 38557c478bd9Sstevel@tonic-gate * The zone no longer needs to be able to see any cpus. 38567c478bd9Sstevel@tonic-gate */ 38577c478bd9Sstevel@tonic-gate zone_pset_set(zone, ZONE_PS_INVAL); 38587c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 38597c478bd9Sstevel@tonic-gate } 38607c478bd9Sstevel@tonic-gate pool_unlock(); 38617c478bd9Sstevel@tonic-gate 38627c478bd9Sstevel@tonic-gate /* 38637c478bd9Sstevel@tonic-gate * ZSD shutdown callbacks can be executed multiple times, hence 38647c478bd9Sstevel@tonic-gate * it is safe to not be holding any locks across this call. 38657c478bd9Sstevel@tonic-gate */ 38667c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 38677c478bd9Sstevel@tonic-gate 38687c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 38697c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 38707c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 38717c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 38727c478bd9Sstevel@tonic-gate 38737c478bd9Sstevel@tonic-gate /* 38747c478bd9Sstevel@tonic-gate * Wait for kernel threads to drain. 38757c478bd9Sstevel@tonic-gate */ 38767c478bd9Sstevel@tonic-gate if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 38777c478bd9Sstevel@tonic-gate zone_rele(zone); 38787c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 38797c478bd9Sstevel@tonic-gate } 38809acbbeafSnn35248 388152978630Ssl108498 /* 388252978630Ssl108498 * Zone can be become down/destroyable even if the above wait 388352978630Ssl108498 * returns EINTR, so any code added here may never execute. 388452978630Ssl108498 * (i.e. don't add code here) 388552978630Ssl108498 */ 38869acbbeafSnn35248 38877c478bd9Sstevel@tonic-gate zone_rele(zone); 38887c478bd9Sstevel@tonic-gate return (0); 38897c478bd9Sstevel@tonic-gate } 38907c478bd9Sstevel@tonic-gate 38917c478bd9Sstevel@tonic-gate /* 38927c478bd9Sstevel@tonic-gate * Systemcall entry point to finalize the zone halt process. The caller 3893824c205fSml93401 * must have already successfully called zone_shutdown(). 38947c478bd9Sstevel@tonic-gate * 38957c478bd9Sstevel@tonic-gate * Upon successful completion, the zone will have been fully destroyed: 38967c478bd9Sstevel@tonic-gate * zsched will have exited, destructor callbacks executed, and the zone 38977c478bd9Sstevel@tonic-gate * removed from the list of active zones. 38987c478bd9Sstevel@tonic-gate */ 38997c478bd9Sstevel@tonic-gate static int 39007c478bd9Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 39017c478bd9Sstevel@tonic-gate { 39027c478bd9Sstevel@tonic-gate uint64_t uniqid; 39037c478bd9Sstevel@tonic-gate zone_t *zone; 39047c478bd9Sstevel@tonic-gate zone_status_t status; 39057c478bd9Sstevel@tonic-gate 39067c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 39077c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 39087c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 39097c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 39107c478bd9Sstevel@tonic-gate 39117c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 39127c478bd9Sstevel@tonic-gate /* 39137c478bd9Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 39147c478bd9Sstevel@tonic-gate * calls to zone_destroy. 39157c478bd9Sstevel@tonic-gate */ 39167c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 39177c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39187c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 39197c478bd9Sstevel@tonic-gate } 39207c478bd9Sstevel@tonic-gate 39217c478bd9Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 39227c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39237c478bd9Sstevel@tonic-gate return (set_errno(EBUSY)); 39247c478bd9Sstevel@tonic-gate } 39257c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 39267c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 39277c478bd9Sstevel@tonic-gate if (status < ZONE_IS_DOWN) { 39287c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 39297c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39307c478bd9Sstevel@tonic-gate return (set_errno(EBUSY)); 39317c478bd9Sstevel@tonic-gate } else if (status == ZONE_IS_DOWN) { 39327c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 39337c478bd9Sstevel@tonic-gate } 39347c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 39357c478bd9Sstevel@tonic-gate zone_hold(zone); 39367c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39377c478bd9Sstevel@tonic-gate 39387c478bd9Sstevel@tonic-gate /* 39397c478bd9Sstevel@tonic-gate * wait for zsched to exit 39407c478bd9Sstevel@tonic-gate */ 39417c478bd9Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_DEAD); 39427c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_DESTROY); 3943f4b3ec61Sdh155122 zone->zone_netstack = NULL; 39447c478bd9Sstevel@tonic-gate uniqid = zone->zone_uniqid; 39457c478bd9Sstevel@tonic-gate zone_rele(zone); 39467c478bd9Sstevel@tonic-gate zone = NULL; /* potentially free'd */ 39477c478bd9Sstevel@tonic-gate 39487c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 39497c478bd9Sstevel@tonic-gate for (; /* ever */; ) { 39507c478bd9Sstevel@tonic-gate boolean_t unref; 39517c478bd9Sstevel@tonic-gate 39527c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL || 39537c478bd9Sstevel@tonic-gate zone->zone_uniqid != uniqid) { 39547c478bd9Sstevel@tonic-gate /* 39557c478bd9Sstevel@tonic-gate * The zone has gone away. Necessary conditions 39567c478bd9Sstevel@tonic-gate * are met, so we return success. 39577c478bd9Sstevel@tonic-gate */ 39587c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39597c478bd9Sstevel@tonic-gate return (0); 39607c478bd9Sstevel@tonic-gate } 39617c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 39627c478bd9Sstevel@tonic-gate unref = ZONE_IS_UNREF(zone); 39637c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 39647c478bd9Sstevel@tonic-gate if (unref) { 39657c478bd9Sstevel@tonic-gate /* 39667c478bd9Sstevel@tonic-gate * There is only one reference to the zone -- that 39677c478bd9Sstevel@tonic-gate * added when the zone was added to the hashtables -- 39687c478bd9Sstevel@tonic-gate * and things will remain this way until we drop 39697c478bd9Sstevel@tonic-gate * zonehash_lock... we can go ahead and cleanup the 39707c478bd9Sstevel@tonic-gate * zone. 39717c478bd9Sstevel@tonic-gate */ 39727c478bd9Sstevel@tonic-gate break; 39737c478bd9Sstevel@tonic-gate } 39747c478bd9Sstevel@tonic-gate 39757c478bd9Sstevel@tonic-gate if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 39767c478bd9Sstevel@tonic-gate /* Signaled */ 39777c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39787c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 39797c478bd9Sstevel@tonic-gate } 39807c478bd9Sstevel@tonic-gate 39817c478bd9Sstevel@tonic-gate } 39827c478bd9Sstevel@tonic-gate 3983c97ad5cdSakolb /* 3984c97ad5cdSakolb * Remove CPU cap for this zone now since we're not going to 3985c97ad5cdSakolb * fail below this point. 3986c97ad5cdSakolb */ 3987c97ad5cdSakolb cpucaps_zone_remove(zone); 3988c97ad5cdSakolb 3989c97ad5cdSakolb /* Get rid of the zone's kstats */ 39900209230bSgjelinek zone_kstat_delete(zone); 39910209230bSgjelinek 399252978630Ssl108498 /* Say goodbye to brand framework. */ 399352978630Ssl108498 brand_unregister_zone(zone->zone_brand); 399452978630Ssl108498 39957c478bd9Sstevel@tonic-gate /* 39967c478bd9Sstevel@tonic-gate * It is now safe to let the zone be recreated; remove it from the 39977c478bd9Sstevel@tonic-gate * lists. The memory will not be freed until the last cred 39987c478bd9Sstevel@tonic-gate * reference goes away. 39997c478bd9Sstevel@tonic-gate */ 40007c478bd9Sstevel@tonic-gate ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 40017c478bd9Sstevel@tonic-gate zonecount--; 40027c478bd9Sstevel@tonic-gate /* remove from active list and hash tables */ 40037c478bd9Sstevel@tonic-gate list_remove(&zone_active, zone); 40047c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 40057c478bd9Sstevel@tonic-gate (mod_hash_key_t)zone->zone_name); 40067c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 40077c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 400848451833Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) 400945916cd2Sjpk (void) mod_hash_destroy(zonehashbylabel, 401045916cd2Sjpk (mod_hash_key_t)zone->zone_slabel); 40117c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40127c478bd9Sstevel@tonic-gate 4013108322fbScarlsonj /* 4014108322fbScarlsonj * Release the root vnode; we're not using it anymore. Nor should any 4015108322fbScarlsonj * other thread that might access it exist. 4016108322fbScarlsonj */ 4017108322fbScarlsonj if (zone->zone_rootvp != NULL) { 4018108322fbScarlsonj VN_RELE(zone->zone_rootvp); 4019108322fbScarlsonj zone->zone_rootvp = NULL; 4020108322fbScarlsonj } 4021108322fbScarlsonj 40227c478bd9Sstevel@tonic-gate /* add to deathrow list */ 40237c478bd9Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 40247c478bd9Sstevel@tonic-gate list_insert_tail(&zone_deathrow, zone); 40257c478bd9Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 40267c478bd9Sstevel@tonic-gate 40277c478bd9Sstevel@tonic-gate /* 40287c478bd9Sstevel@tonic-gate * Drop last reference (which was added by zsched()), this will 40297c478bd9Sstevel@tonic-gate * free the zone unless there are outstanding cred references. 40307c478bd9Sstevel@tonic-gate */ 40317c478bd9Sstevel@tonic-gate zone_rele(zone); 40327c478bd9Sstevel@tonic-gate return (0); 40337c478bd9Sstevel@tonic-gate } 40347c478bd9Sstevel@tonic-gate 40357c478bd9Sstevel@tonic-gate /* 40367c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_getattr(2). 40377c478bd9Sstevel@tonic-gate */ 40387c478bd9Sstevel@tonic-gate static ssize_t 40397c478bd9Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 40407c478bd9Sstevel@tonic-gate { 40417c478bd9Sstevel@tonic-gate size_t size; 40427c478bd9Sstevel@tonic-gate int error = 0, err; 40437c478bd9Sstevel@tonic-gate zone_t *zone; 40447c478bd9Sstevel@tonic-gate char *zonepath; 40453f2f09c1Sdp char *outstr; 40467c478bd9Sstevel@tonic-gate zone_status_t zone_status; 40477c478bd9Sstevel@tonic-gate pid_t initpid; 4048c97ad5cdSakolb boolean_t global = (curzone == global_zone); 4049c97ad5cdSakolb boolean_t inzone = (curzone->zone_id == zoneid); 4050f4b3ec61Sdh155122 ushort_t flags; 40517c478bd9Sstevel@tonic-gate 40527c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 40537c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 40547c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40557c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 40567c478bd9Sstevel@tonic-gate } 40577c478bd9Sstevel@tonic-gate zone_status = zone_status_get(zone); 40587c478bd9Sstevel@tonic-gate if (zone_status < ZONE_IS_READY) { 40597c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40607c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 40617c478bd9Sstevel@tonic-gate } 40627c478bd9Sstevel@tonic-gate zone_hold(zone); 40637c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40647c478bd9Sstevel@tonic-gate 40657c478bd9Sstevel@tonic-gate /* 406645916cd2Sjpk * If not in the global zone, don't show information about other zones, 406745916cd2Sjpk * unless the system is labeled and the local zone's label dominates 406845916cd2Sjpk * the other zone. 40697c478bd9Sstevel@tonic-gate */ 407045916cd2Sjpk if (!zone_list_access(zone)) { 40717c478bd9Sstevel@tonic-gate zone_rele(zone); 40727c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 40737c478bd9Sstevel@tonic-gate } 40747c478bd9Sstevel@tonic-gate 40757c478bd9Sstevel@tonic-gate switch (attr) { 40767c478bd9Sstevel@tonic-gate case ZONE_ATTR_ROOT: 40777c478bd9Sstevel@tonic-gate if (global) { 40787c478bd9Sstevel@tonic-gate /* 40797c478bd9Sstevel@tonic-gate * Copy the path to trim the trailing "/" (except for 40807c478bd9Sstevel@tonic-gate * the global zone). 40817c478bd9Sstevel@tonic-gate */ 40827c478bd9Sstevel@tonic-gate if (zone != global_zone) 40837c478bd9Sstevel@tonic-gate size = zone->zone_rootpathlen - 1; 40847c478bd9Sstevel@tonic-gate else 40857c478bd9Sstevel@tonic-gate size = zone->zone_rootpathlen; 40867c478bd9Sstevel@tonic-gate zonepath = kmem_alloc(size, KM_SLEEP); 40877c478bd9Sstevel@tonic-gate bcopy(zone->zone_rootpath, zonepath, size); 40887c478bd9Sstevel@tonic-gate zonepath[size - 1] = '\0'; 40897c478bd9Sstevel@tonic-gate } else { 4090c97ad5cdSakolb if (inzone || !is_system_labeled()) { 40917c478bd9Sstevel@tonic-gate /* 409245916cd2Sjpk * Caller is not in the global zone. 409345916cd2Sjpk * if the query is on the current zone 409445916cd2Sjpk * or the system is not labeled, 409545916cd2Sjpk * just return faked-up path for current zone. 40967c478bd9Sstevel@tonic-gate */ 40977c478bd9Sstevel@tonic-gate zonepath = "/"; 40987c478bd9Sstevel@tonic-gate size = 2; 409945916cd2Sjpk } else { 410045916cd2Sjpk /* 410145916cd2Sjpk * Return related path for current zone. 410245916cd2Sjpk */ 410345916cd2Sjpk int prefix_len = strlen(zone_prefix); 410445916cd2Sjpk int zname_len = strlen(zone->zone_name); 410545916cd2Sjpk 410645916cd2Sjpk size = prefix_len + zname_len + 1; 410745916cd2Sjpk zonepath = kmem_alloc(size, KM_SLEEP); 410845916cd2Sjpk bcopy(zone_prefix, zonepath, prefix_len); 410945916cd2Sjpk bcopy(zone->zone_name, zonepath + 411045916cd2Sjpk prefix_len, zname_len); 411145916cd2Sjpk zonepath[size - 1] = '\0'; 411245916cd2Sjpk } 41137c478bd9Sstevel@tonic-gate } 41147c478bd9Sstevel@tonic-gate if (bufsize > size) 41157c478bd9Sstevel@tonic-gate bufsize = size; 41167c478bd9Sstevel@tonic-gate if (buf != NULL) { 41177c478bd9Sstevel@tonic-gate err = copyoutstr(zonepath, buf, bufsize, NULL); 41187c478bd9Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 41197c478bd9Sstevel@tonic-gate error = EFAULT; 41207c478bd9Sstevel@tonic-gate } 4121c97ad5cdSakolb if (global || (is_system_labeled() && !inzone)) 41227c478bd9Sstevel@tonic-gate kmem_free(zonepath, size); 41237c478bd9Sstevel@tonic-gate break; 41247c478bd9Sstevel@tonic-gate 41257c478bd9Sstevel@tonic-gate case ZONE_ATTR_NAME: 41267c478bd9Sstevel@tonic-gate size = strlen(zone->zone_name) + 1; 41277c478bd9Sstevel@tonic-gate if (bufsize > size) 41287c478bd9Sstevel@tonic-gate bufsize = size; 41297c478bd9Sstevel@tonic-gate if (buf != NULL) { 41307c478bd9Sstevel@tonic-gate err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 41317c478bd9Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 41327c478bd9Sstevel@tonic-gate error = EFAULT; 41337c478bd9Sstevel@tonic-gate } 41347c478bd9Sstevel@tonic-gate break; 41357c478bd9Sstevel@tonic-gate 41367c478bd9Sstevel@tonic-gate case ZONE_ATTR_STATUS: 41377c478bd9Sstevel@tonic-gate /* 41387c478bd9Sstevel@tonic-gate * Since we're not holding zonehash_lock, the zone status 41397c478bd9Sstevel@tonic-gate * may be anything; leave it up to userland to sort it out. 41407c478bd9Sstevel@tonic-gate */ 41417c478bd9Sstevel@tonic-gate size = sizeof (zone_status); 41427c478bd9Sstevel@tonic-gate if (bufsize > size) 41437c478bd9Sstevel@tonic-gate bufsize = size; 41447c478bd9Sstevel@tonic-gate zone_status = zone_status_get(zone); 41457c478bd9Sstevel@tonic-gate if (buf != NULL && 41467c478bd9Sstevel@tonic-gate copyout(&zone_status, buf, bufsize) != 0) 41477c478bd9Sstevel@tonic-gate error = EFAULT; 41487c478bd9Sstevel@tonic-gate break; 4149f4b3ec61Sdh155122 case ZONE_ATTR_FLAGS: 4150f4b3ec61Sdh155122 size = sizeof (zone->zone_flags); 4151f4b3ec61Sdh155122 if (bufsize > size) 4152f4b3ec61Sdh155122 bufsize = size; 4153f4b3ec61Sdh155122 flags = zone->zone_flags; 4154f4b3ec61Sdh155122 if (buf != NULL && 4155f4b3ec61Sdh155122 copyout(&flags, buf, bufsize) != 0) 4156f4b3ec61Sdh155122 error = EFAULT; 4157f4b3ec61Sdh155122 break; 41587c478bd9Sstevel@tonic-gate case ZONE_ATTR_PRIVSET: 41597c478bd9Sstevel@tonic-gate size = sizeof (priv_set_t); 41607c478bd9Sstevel@tonic-gate if (bufsize > size) 41617c478bd9Sstevel@tonic-gate bufsize = size; 41627c478bd9Sstevel@tonic-gate if (buf != NULL && 41637c478bd9Sstevel@tonic-gate copyout(zone->zone_privset, buf, bufsize) != 0) 41647c478bd9Sstevel@tonic-gate error = EFAULT; 41657c478bd9Sstevel@tonic-gate break; 41667c478bd9Sstevel@tonic-gate case ZONE_ATTR_UNIQID: 41677c478bd9Sstevel@tonic-gate size = sizeof (zone->zone_uniqid); 41687c478bd9Sstevel@tonic-gate if (bufsize > size) 41697c478bd9Sstevel@tonic-gate bufsize = size; 41707c478bd9Sstevel@tonic-gate if (buf != NULL && 41717c478bd9Sstevel@tonic-gate copyout(&zone->zone_uniqid, buf, bufsize) != 0) 41727c478bd9Sstevel@tonic-gate error = EFAULT; 41737c478bd9Sstevel@tonic-gate break; 41747c478bd9Sstevel@tonic-gate case ZONE_ATTR_POOLID: 41757c478bd9Sstevel@tonic-gate { 41767c478bd9Sstevel@tonic-gate pool_t *pool; 41777c478bd9Sstevel@tonic-gate poolid_t poolid; 41787c478bd9Sstevel@tonic-gate 41797c478bd9Sstevel@tonic-gate if (pool_lock_intr() != 0) { 41807c478bd9Sstevel@tonic-gate error = EINTR; 41817c478bd9Sstevel@tonic-gate break; 41827c478bd9Sstevel@tonic-gate } 41837c478bd9Sstevel@tonic-gate pool = zone_pool_get(zone); 41847c478bd9Sstevel@tonic-gate poolid = pool->pool_id; 41857c478bd9Sstevel@tonic-gate pool_unlock(); 41867c478bd9Sstevel@tonic-gate size = sizeof (poolid); 41877c478bd9Sstevel@tonic-gate if (bufsize > size) 41887c478bd9Sstevel@tonic-gate bufsize = size; 41897c478bd9Sstevel@tonic-gate if (buf != NULL && copyout(&poolid, buf, size) != 0) 41907c478bd9Sstevel@tonic-gate error = EFAULT; 41917c478bd9Sstevel@tonic-gate } 41927c478bd9Sstevel@tonic-gate break; 419345916cd2Sjpk case ZONE_ATTR_SLBL: 419445916cd2Sjpk size = sizeof (bslabel_t); 419545916cd2Sjpk if (bufsize > size) 419645916cd2Sjpk bufsize = size; 419745916cd2Sjpk if (zone->zone_slabel == NULL) 419845916cd2Sjpk error = EINVAL; 419945916cd2Sjpk else if (buf != NULL && 420045916cd2Sjpk copyout(label2bslabel(zone->zone_slabel), buf, 420145916cd2Sjpk bufsize) != 0) 420245916cd2Sjpk error = EFAULT; 420345916cd2Sjpk break; 42047c478bd9Sstevel@tonic-gate case ZONE_ATTR_INITPID: 42057c478bd9Sstevel@tonic-gate size = sizeof (initpid); 42067c478bd9Sstevel@tonic-gate if (bufsize > size) 42077c478bd9Sstevel@tonic-gate bufsize = size; 42087c478bd9Sstevel@tonic-gate initpid = zone->zone_proc_initpid; 42097c478bd9Sstevel@tonic-gate if (initpid == -1) { 42107c478bd9Sstevel@tonic-gate error = ESRCH; 42117c478bd9Sstevel@tonic-gate break; 42127c478bd9Sstevel@tonic-gate } 42137c478bd9Sstevel@tonic-gate if (buf != NULL && 42147c478bd9Sstevel@tonic-gate copyout(&initpid, buf, bufsize) != 0) 42157c478bd9Sstevel@tonic-gate error = EFAULT; 42167c478bd9Sstevel@tonic-gate break; 42179acbbeafSnn35248 case ZONE_ATTR_BRAND: 42189acbbeafSnn35248 size = strlen(zone->zone_brand->b_name) + 1; 42199acbbeafSnn35248 42209acbbeafSnn35248 if (bufsize > size) 42219acbbeafSnn35248 bufsize = size; 42229acbbeafSnn35248 if (buf != NULL) { 42239acbbeafSnn35248 err = copyoutstr(zone->zone_brand->b_name, buf, 42249acbbeafSnn35248 bufsize, NULL); 42259acbbeafSnn35248 if (err != 0 && err != ENAMETOOLONG) 42269acbbeafSnn35248 error = EFAULT; 42279acbbeafSnn35248 } 42289acbbeafSnn35248 break; 42293f2f09c1Sdp case ZONE_ATTR_INITNAME: 42303f2f09c1Sdp size = strlen(zone->zone_initname) + 1; 42313f2f09c1Sdp if (bufsize > size) 42323f2f09c1Sdp bufsize = size; 42333f2f09c1Sdp if (buf != NULL) { 42343f2f09c1Sdp err = copyoutstr(zone->zone_initname, buf, bufsize, 42353f2f09c1Sdp NULL); 42363f2f09c1Sdp if (err != 0 && err != ENAMETOOLONG) 42373f2f09c1Sdp error = EFAULT; 42383f2f09c1Sdp } 42393f2f09c1Sdp break; 42403f2f09c1Sdp case ZONE_ATTR_BOOTARGS: 42413f2f09c1Sdp if (zone->zone_bootargs == NULL) 42423f2f09c1Sdp outstr = ""; 42433f2f09c1Sdp else 42443f2f09c1Sdp outstr = zone->zone_bootargs; 42453f2f09c1Sdp size = strlen(outstr) + 1; 42463f2f09c1Sdp if (bufsize > size) 42473f2f09c1Sdp bufsize = size; 42483f2f09c1Sdp if (buf != NULL) { 42493f2f09c1Sdp err = copyoutstr(outstr, buf, bufsize, NULL); 42503f2f09c1Sdp if (err != 0 && err != ENAMETOOLONG) 42513f2f09c1Sdp error = EFAULT; 42523f2f09c1Sdp } 42533f2f09c1Sdp break; 42540209230bSgjelinek case ZONE_ATTR_PHYS_MCAP: 42550209230bSgjelinek size = sizeof (zone->zone_phys_mcap); 42560209230bSgjelinek if (bufsize > size) 42570209230bSgjelinek bufsize = size; 42580209230bSgjelinek if (buf != NULL && 42590209230bSgjelinek copyout(&zone->zone_phys_mcap, buf, bufsize) != 0) 42600209230bSgjelinek error = EFAULT; 42610209230bSgjelinek break; 42620209230bSgjelinek case ZONE_ATTR_SCHED_CLASS: 42630209230bSgjelinek mutex_enter(&class_lock); 42640209230bSgjelinek 42650209230bSgjelinek if (zone->zone_defaultcid >= loaded_classes) 42660209230bSgjelinek outstr = ""; 42670209230bSgjelinek else 42680209230bSgjelinek outstr = sclass[zone->zone_defaultcid].cl_name; 42690209230bSgjelinek size = strlen(outstr) + 1; 42700209230bSgjelinek if (bufsize > size) 42710209230bSgjelinek bufsize = size; 42720209230bSgjelinek if (buf != NULL) { 42730209230bSgjelinek err = copyoutstr(outstr, buf, bufsize, NULL); 42740209230bSgjelinek if (err != 0 && err != ENAMETOOLONG) 42750209230bSgjelinek error = EFAULT; 42760209230bSgjelinek } 42770209230bSgjelinek 42780209230bSgjelinek mutex_exit(&class_lock); 42790209230bSgjelinek break; 42807c478bd9Sstevel@tonic-gate default: 42819acbbeafSnn35248 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) { 42829acbbeafSnn35248 size = bufsize; 42839acbbeafSnn35248 error = ZBROP(zone)->b_getattr(zone, attr, buf, &size); 42849acbbeafSnn35248 } else { 42857c478bd9Sstevel@tonic-gate error = EINVAL; 42867c478bd9Sstevel@tonic-gate } 42879acbbeafSnn35248 } 42887c478bd9Sstevel@tonic-gate zone_rele(zone); 42897c478bd9Sstevel@tonic-gate 42907c478bd9Sstevel@tonic-gate if (error) 42917c478bd9Sstevel@tonic-gate return (set_errno(error)); 42927c478bd9Sstevel@tonic-gate return ((ssize_t)size); 42937c478bd9Sstevel@tonic-gate } 42947c478bd9Sstevel@tonic-gate 42957c478bd9Sstevel@tonic-gate /* 42963f2f09c1Sdp * Systemcall entry point for zone_setattr(2). 42973f2f09c1Sdp */ 42983f2f09c1Sdp /*ARGSUSED*/ 42993f2f09c1Sdp static int 43003f2f09c1Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 43013f2f09c1Sdp { 43023f2f09c1Sdp zone_t *zone; 43033f2f09c1Sdp zone_status_t zone_status; 43043f2f09c1Sdp int err; 43053f2f09c1Sdp 43063f2f09c1Sdp if (secpolicy_zone_config(CRED()) != 0) 43073f2f09c1Sdp return (set_errno(EPERM)); 43083f2f09c1Sdp 43093f2f09c1Sdp /* 43100209230bSgjelinek * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the 43110209230bSgjelinek * global zone. 43123f2f09c1Sdp */ 43130209230bSgjelinek if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) { 43143f2f09c1Sdp return (set_errno(EINVAL)); 43153f2f09c1Sdp } 43163f2f09c1Sdp 43173f2f09c1Sdp mutex_enter(&zonehash_lock); 43183f2f09c1Sdp if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 43193f2f09c1Sdp mutex_exit(&zonehash_lock); 43203f2f09c1Sdp return (set_errno(EINVAL)); 43213f2f09c1Sdp } 43223f2f09c1Sdp zone_hold(zone); 43233f2f09c1Sdp mutex_exit(&zonehash_lock); 43243f2f09c1Sdp 43250209230bSgjelinek /* 43260209230bSgjelinek * At present most attributes can only be set on non-running, 43270209230bSgjelinek * non-global zones. 43280209230bSgjelinek */ 43293f2f09c1Sdp zone_status = zone_status_get(zone); 43300209230bSgjelinek if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY) 43313f2f09c1Sdp goto done; 43323f2f09c1Sdp 43333f2f09c1Sdp switch (attr) { 43343f2f09c1Sdp case ZONE_ATTR_INITNAME: 43353f2f09c1Sdp err = zone_set_initname(zone, (const char *)buf); 43363f2f09c1Sdp break; 43373f2f09c1Sdp case ZONE_ATTR_BOOTARGS: 43383f2f09c1Sdp err = zone_set_bootargs(zone, (const char *)buf); 43393f2f09c1Sdp break; 43409acbbeafSnn35248 case ZONE_ATTR_BRAND: 434159f2ff5cSedp err = zone_set_brand(zone, (const char *)buf); 43429acbbeafSnn35248 break; 43430209230bSgjelinek case ZONE_ATTR_PHYS_MCAP: 43440209230bSgjelinek err = zone_set_phys_mcap(zone, (const uint64_t *)buf); 43450209230bSgjelinek break; 43460209230bSgjelinek case ZONE_ATTR_SCHED_CLASS: 43470209230bSgjelinek err = zone_set_sched_class(zone, (const char *)buf); 43480209230bSgjelinek break; 43493f2f09c1Sdp default: 43509acbbeafSnn35248 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) 43519acbbeafSnn35248 err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize); 43529acbbeafSnn35248 else 43533f2f09c1Sdp err = EINVAL; 43543f2f09c1Sdp } 43553f2f09c1Sdp 43563f2f09c1Sdp done: 43573f2f09c1Sdp zone_rele(zone); 43583f2f09c1Sdp return (err != 0 ? set_errno(err) : 0); 43593f2f09c1Sdp } 43603f2f09c1Sdp 43613f2f09c1Sdp /* 43627c478bd9Sstevel@tonic-gate * Return zero if the process has at least one vnode mapped in to its 43637c478bd9Sstevel@tonic-gate * address space which shouldn't be allowed to change zones. 43640209230bSgjelinek * 43650209230bSgjelinek * Also return zero if the process has any shared mappings which reserve 43660209230bSgjelinek * swap. This is because the counting for zone.max-swap does not allow swap 43670209230bSgjelinek * revervation to be shared between zones. zone swap reservation is counted 43680209230bSgjelinek * on zone->zone_max_swap. 43697c478bd9Sstevel@tonic-gate */ 43707c478bd9Sstevel@tonic-gate static int 43717c478bd9Sstevel@tonic-gate as_can_change_zones(void) 43727c478bd9Sstevel@tonic-gate { 43737c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 43747c478bd9Sstevel@tonic-gate struct seg *seg; 43757c478bd9Sstevel@tonic-gate struct as *as = pp->p_as; 43767c478bd9Sstevel@tonic-gate vnode_t *vp; 43777c478bd9Sstevel@tonic-gate int allow = 1; 43787c478bd9Sstevel@tonic-gate 43797c478bd9Sstevel@tonic-gate ASSERT(pp->p_as != &kas); 43800209230bSgjelinek AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 43817c478bd9Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 43820209230bSgjelinek 43830209230bSgjelinek /* 43840209230bSgjelinek * Cannot enter zone with shared anon memory which 43850209230bSgjelinek * reserves swap. See comment above. 43860209230bSgjelinek */ 43870209230bSgjelinek if (seg_can_change_zones(seg) == B_FALSE) { 43880209230bSgjelinek allow = 0; 43890209230bSgjelinek break; 43900209230bSgjelinek } 43917c478bd9Sstevel@tonic-gate /* 43927c478bd9Sstevel@tonic-gate * if we can't get a backing vnode for this segment then skip 43937c478bd9Sstevel@tonic-gate * it. 43947c478bd9Sstevel@tonic-gate */ 43957c478bd9Sstevel@tonic-gate vp = NULL; 43967c478bd9Sstevel@tonic-gate if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 43977c478bd9Sstevel@tonic-gate continue; 43987c478bd9Sstevel@tonic-gate if (!vn_can_change_zones(vp)) { /* bail on first match */ 43997c478bd9Sstevel@tonic-gate allow = 0; 44007c478bd9Sstevel@tonic-gate break; 44017c478bd9Sstevel@tonic-gate } 44027c478bd9Sstevel@tonic-gate } 44030209230bSgjelinek AS_LOCK_EXIT(as, &as->a_lock); 44047c478bd9Sstevel@tonic-gate return (allow); 44057c478bd9Sstevel@tonic-gate } 44067c478bd9Sstevel@tonic-gate 44077c478bd9Sstevel@tonic-gate /* 44080209230bSgjelinek * Count swap reserved by curproc's address space 44090209230bSgjelinek */ 44100209230bSgjelinek static size_t 44110209230bSgjelinek as_swresv(void) 44120209230bSgjelinek { 44130209230bSgjelinek proc_t *pp = curproc; 44140209230bSgjelinek struct seg *seg; 44150209230bSgjelinek struct as *as = pp->p_as; 44160209230bSgjelinek size_t swap = 0; 44170209230bSgjelinek 44180209230bSgjelinek ASSERT(pp->p_as != &kas); 44190209230bSgjelinek ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 44200209230bSgjelinek for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) 44210209230bSgjelinek swap += seg_swresv(seg); 44220209230bSgjelinek 44230209230bSgjelinek return (swap); 44240209230bSgjelinek } 44250209230bSgjelinek 44260209230bSgjelinek /* 44277c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_enter(). 44287c478bd9Sstevel@tonic-gate * 44297c478bd9Sstevel@tonic-gate * The current process is injected into said zone. In the process 44307c478bd9Sstevel@tonic-gate * it will change its project membership, privileges, rootdir/cwd, 44317c478bd9Sstevel@tonic-gate * zone-wide rctls, and pool association to match those of the zone. 44327c478bd9Sstevel@tonic-gate * 44337c478bd9Sstevel@tonic-gate * The first zone_enter() called while the zone is in the ZONE_IS_READY 44347c478bd9Sstevel@tonic-gate * state will transition it to ZONE_IS_RUNNING. Processes may only 44357c478bd9Sstevel@tonic-gate * enter a zone that is "ready" or "running". 44367c478bd9Sstevel@tonic-gate */ 44377c478bd9Sstevel@tonic-gate static int 44387c478bd9Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 44397c478bd9Sstevel@tonic-gate { 44407c478bd9Sstevel@tonic-gate zone_t *zone; 44417c478bd9Sstevel@tonic-gate vnode_t *vp; 44427c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 44437c478bd9Sstevel@tonic-gate contract_t *ct; 44447c478bd9Sstevel@tonic-gate cont_process_t *ctp; 44457c478bd9Sstevel@tonic-gate task_t *tk, *oldtk; 44467c478bd9Sstevel@tonic-gate kproject_t *zone_proj0; 44477c478bd9Sstevel@tonic-gate cred_t *cr, *newcr; 44487c478bd9Sstevel@tonic-gate pool_t *oldpool, *newpool; 44497c478bd9Sstevel@tonic-gate sess_t *sp; 44507c478bd9Sstevel@tonic-gate uid_t uid; 44517c478bd9Sstevel@tonic-gate zone_status_t status; 44527c478bd9Sstevel@tonic-gate int err = 0; 44537c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 44540209230bSgjelinek size_t swap; 4455c97ad5cdSakolb kthread_id_t t; 44567c478bd9Sstevel@tonic-gate 44577c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 44587c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 44597c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 44607c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 44617c478bd9Sstevel@tonic-gate 44627c478bd9Sstevel@tonic-gate /* 44637c478bd9Sstevel@tonic-gate * Stop all lwps so we don't need to hold a lock to look at 44647c478bd9Sstevel@tonic-gate * curproc->p_zone. This needs to happen before we grab any 44657c478bd9Sstevel@tonic-gate * locks to avoid deadlock (another lwp in the process could 44667c478bd9Sstevel@tonic-gate * be waiting for the held lock). 44677c478bd9Sstevel@tonic-gate */ 44687c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 44697c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 44707c478bd9Sstevel@tonic-gate 44717c478bd9Sstevel@tonic-gate /* 44727c478bd9Sstevel@tonic-gate * Make sure we're not changing zones with files open or mapped in 44737c478bd9Sstevel@tonic-gate * to our address space which shouldn't be changing zones. 44747c478bd9Sstevel@tonic-gate */ 44757c478bd9Sstevel@tonic-gate if (!files_can_change_zones()) { 44767c478bd9Sstevel@tonic-gate err = EBADF; 44777c478bd9Sstevel@tonic-gate goto out; 44787c478bd9Sstevel@tonic-gate } 44797c478bd9Sstevel@tonic-gate if (!as_can_change_zones()) { 44807c478bd9Sstevel@tonic-gate err = EFAULT; 44817c478bd9Sstevel@tonic-gate goto out; 44827c478bd9Sstevel@tonic-gate } 44837c478bd9Sstevel@tonic-gate 44847c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 44857c478bd9Sstevel@tonic-gate if (pp->p_zone != global_zone) { 44867c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44877c478bd9Sstevel@tonic-gate err = EINVAL; 44887c478bd9Sstevel@tonic-gate goto out; 44897c478bd9Sstevel@tonic-gate } 44907c478bd9Sstevel@tonic-gate 44917c478bd9Sstevel@tonic-gate zone = zone_find_all_by_id(zoneid); 44927c478bd9Sstevel@tonic-gate if (zone == NULL) { 44937c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44947c478bd9Sstevel@tonic-gate err = EINVAL; 44957c478bd9Sstevel@tonic-gate goto out; 44967c478bd9Sstevel@tonic-gate } 44977c478bd9Sstevel@tonic-gate 44987c478bd9Sstevel@tonic-gate /* 44997c478bd9Sstevel@tonic-gate * To prevent processes in a zone from holding contracts on 45007c478bd9Sstevel@tonic-gate * extrazonal resources, and to avoid process contract 45017c478bd9Sstevel@tonic-gate * memberships which span zones, contract holders and processes 45027c478bd9Sstevel@tonic-gate * which aren't the sole members of their encapsulating process 45037c478bd9Sstevel@tonic-gate * contracts are not allowed to zone_enter. 45047c478bd9Sstevel@tonic-gate */ 45057c478bd9Sstevel@tonic-gate ctp = pp->p_ct_process; 45067c478bd9Sstevel@tonic-gate ct = &ctp->conp_contract; 45077c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 45087c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 45097c478bd9Sstevel@tonic-gate if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 45107c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 45117c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 45127c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45137c478bd9Sstevel@tonic-gate pool_unlock(); 45147c478bd9Sstevel@tonic-gate err = EINVAL; 45157c478bd9Sstevel@tonic-gate goto out; 45167c478bd9Sstevel@tonic-gate } 45177c478bd9Sstevel@tonic-gate 45187c478bd9Sstevel@tonic-gate /* 45197c478bd9Sstevel@tonic-gate * Moreover, we don't allow processes whose encapsulating 45207c478bd9Sstevel@tonic-gate * process contracts have inherited extrazonal contracts. 45217c478bd9Sstevel@tonic-gate * While it would be easier to eliminate all process contracts 45227c478bd9Sstevel@tonic-gate * with inherited contracts, we need to be able to give a 45237c478bd9Sstevel@tonic-gate * restarted init (or other zone-penetrating process) its 45247c478bd9Sstevel@tonic-gate * predecessor's contracts. 45257c478bd9Sstevel@tonic-gate */ 45267c478bd9Sstevel@tonic-gate if (ctp->conp_ninherited != 0) { 45277c478bd9Sstevel@tonic-gate contract_t *next; 45287c478bd9Sstevel@tonic-gate for (next = list_head(&ctp->conp_inherited); next; 45297c478bd9Sstevel@tonic-gate next = list_next(&ctp->conp_inherited, next)) { 45307c478bd9Sstevel@tonic-gate if (contract_getzuniqid(next) != zone->zone_uniqid) { 45317c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 45327c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 45337c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45347c478bd9Sstevel@tonic-gate pool_unlock(); 45357c478bd9Sstevel@tonic-gate err = EINVAL; 45367c478bd9Sstevel@tonic-gate goto out; 45377c478bd9Sstevel@tonic-gate } 45387c478bd9Sstevel@tonic-gate } 45397c478bd9Sstevel@tonic-gate } 45407c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 45417c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 45427c478bd9Sstevel@tonic-gate 45437c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 45447c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 45457c478bd9Sstevel@tonic-gate /* 45467c478bd9Sstevel@tonic-gate * Can't join 45477c478bd9Sstevel@tonic-gate */ 45487c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45497c478bd9Sstevel@tonic-gate err = EINVAL; 45507c478bd9Sstevel@tonic-gate goto out; 45517c478bd9Sstevel@tonic-gate } 45527c478bd9Sstevel@tonic-gate 45537c478bd9Sstevel@tonic-gate /* 45547c478bd9Sstevel@tonic-gate * Make sure new priv set is within the permitted set for caller 45557c478bd9Sstevel@tonic-gate */ 45567c478bd9Sstevel@tonic-gate if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 45577c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45587c478bd9Sstevel@tonic-gate err = EPERM; 45597c478bd9Sstevel@tonic-gate goto out; 45607c478bd9Sstevel@tonic-gate } 45617c478bd9Sstevel@tonic-gate /* 45627c478bd9Sstevel@tonic-gate * We want to momentarily drop zonehash_lock while we optimistically 45637c478bd9Sstevel@tonic-gate * bind curproc to the pool it should be running in. This is safe 45647c478bd9Sstevel@tonic-gate * since the zone can't disappear (we have a hold on it). 45657c478bd9Sstevel@tonic-gate */ 45667c478bd9Sstevel@tonic-gate zone_hold(zone); 45677c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45687c478bd9Sstevel@tonic-gate 45697c478bd9Sstevel@tonic-gate /* 45707c478bd9Sstevel@tonic-gate * Grab pool_lock to keep the pools configuration from changing 45717c478bd9Sstevel@tonic-gate * and to stop ourselves from getting rebound to another pool 45727c478bd9Sstevel@tonic-gate * until we join the zone. 45737c478bd9Sstevel@tonic-gate */ 45747c478bd9Sstevel@tonic-gate if (pool_lock_intr() != 0) { 45757c478bd9Sstevel@tonic-gate zone_rele(zone); 45767c478bd9Sstevel@tonic-gate err = EINTR; 45777c478bd9Sstevel@tonic-gate goto out; 45787c478bd9Sstevel@tonic-gate } 45797c478bd9Sstevel@tonic-gate ASSERT(secpolicy_pool(CRED()) == 0); 45807c478bd9Sstevel@tonic-gate /* 45817c478bd9Sstevel@tonic-gate * Bind ourselves to the pool currently associated with the zone. 45827c478bd9Sstevel@tonic-gate */ 45837c478bd9Sstevel@tonic-gate oldpool = curproc->p_pool; 45847c478bd9Sstevel@tonic-gate newpool = zone_pool_get(zone); 45857c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED && newpool != oldpool && 45867c478bd9Sstevel@tonic-gate (err = pool_do_bind(newpool, P_PID, P_MYID, 45877c478bd9Sstevel@tonic-gate POOL_BIND_ALL)) != 0) { 45887c478bd9Sstevel@tonic-gate pool_unlock(); 45897c478bd9Sstevel@tonic-gate zone_rele(zone); 45907c478bd9Sstevel@tonic-gate goto out; 45917c478bd9Sstevel@tonic-gate } 45927c478bd9Sstevel@tonic-gate 45937c478bd9Sstevel@tonic-gate /* 45947c478bd9Sstevel@tonic-gate * Grab cpu_lock now; we'll need it later when we call 45957c478bd9Sstevel@tonic-gate * task_join(). 45967c478bd9Sstevel@tonic-gate */ 45977c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 45987c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 45997c478bd9Sstevel@tonic-gate /* 46007c478bd9Sstevel@tonic-gate * Make sure the zone hasn't moved on since we dropped zonehash_lock. 46017c478bd9Sstevel@tonic-gate */ 46027c478bd9Sstevel@tonic-gate if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 46037c478bd9Sstevel@tonic-gate /* 46047c478bd9Sstevel@tonic-gate * Can't join anymore. 46057c478bd9Sstevel@tonic-gate */ 46067c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 46077c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 46087c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED && 46097c478bd9Sstevel@tonic-gate newpool != oldpool) 46107c478bd9Sstevel@tonic-gate (void) pool_do_bind(oldpool, P_PID, P_MYID, 46117c478bd9Sstevel@tonic-gate POOL_BIND_ALL); 46127c478bd9Sstevel@tonic-gate pool_unlock(); 46137c478bd9Sstevel@tonic-gate zone_rele(zone); 46147c478bd9Sstevel@tonic-gate err = EINVAL; 46157c478bd9Sstevel@tonic-gate goto out; 46167c478bd9Sstevel@tonic-gate } 46177c478bd9Sstevel@tonic-gate 46180209230bSgjelinek /* 46190209230bSgjelinek * a_lock must be held while transfering locked memory and swap 46200209230bSgjelinek * reservation from the global zone to the non global zone because 46210209230bSgjelinek * asynchronous faults on the processes' address space can lock 46220209230bSgjelinek * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE 46230209230bSgjelinek * segments respectively. 46240209230bSgjelinek */ 46250209230bSgjelinek AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER); 46260209230bSgjelinek swap = as_swresv(); 46277c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 46287c478bd9Sstevel@tonic-gate zone_proj0 = zone->zone_zsched->p_task->tk_proj; 46297c478bd9Sstevel@tonic-gate /* verify that we do not exceed and task or lwp limits */ 46307c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 46317c478bd9Sstevel@tonic-gate /* add new lwps to zone and zone's proj0 */ 46327c478bd9Sstevel@tonic-gate zone_proj0->kpj_nlwps += pp->p_lwpcnt; 46337c478bd9Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 46347c478bd9Sstevel@tonic-gate /* add 1 task to zone's proj0 */ 46357c478bd9Sstevel@tonic-gate zone_proj0->kpj_ntasks += 1; 46367c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 46377c478bd9Sstevel@tonic-gate 46380209230bSgjelinek mutex_enter(&zone->zone_mem_lock); 4639c6939658Ssl108498 zone->zone_locked_mem += pp->p_locked_mem; 4640c6939658Ssl108498 zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem; 46410209230bSgjelinek zone->zone_max_swap += swap; 46420209230bSgjelinek mutex_exit(&zone->zone_mem_lock); 4643c6939658Ssl108498 4644c1a9a9c3Skrishna mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock)); 4645c1a9a9c3Skrishna zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem; 4646c1a9a9c3Skrishna mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock)); 4647c1a9a9c3Skrishna 46487c478bd9Sstevel@tonic-gate /* remove lwps from proc's old zone and old project */ 46497c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_zone->zone_nlwps_lock); 46507c478bd9Sstevel@tonic-gate pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 46517c478bd9Sstevel@tonic-gate pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 46527c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_zone->zone_nlwps_lock); 46537c478bd9Sstevel@tonic-gate 46540209230bSgjelinek mutex_enter(&pp->p_zone->zone_mem_lock); 4655c6939658Ssl108498 pp->p_zone->zone_locked_mem -= pp->p_locked_mem; 4656c6939658Ssl108498 pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 46570209230bSgjelinek pp->p_zone->zone_max_swap -= swap; 46580209230bSgjelinek mutex_exit(&pp->p_zone->zone_mem_lock); 4659c6939658Ssl108498 4660c1a9a9c3Skrishna mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 4661c1a9a9c3Skrishna pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem; 4662c1a9a9c3Skrishna mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 4663c1a9a9c3Skrishna 4664c6939658Ssl108498 mutex_exit(&pp->p_lock); 46650209230bSgjelinek AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock); 4666c6939658Ssl108498 46677c478bd9Sstevel@tonic-gate /* 46687c478bd9Sstevel@tonic-gate * Joining the zone cannot fail from now on. 46697c478bd9Sstevel@tonic-gate * 46707c478bd9Sstevel@tonic-gate * This means that a lot of the following code can be commonized and 46717c478bd9Sstevel@tonic-gate * shared with zsched(). 46727c478bd9Sstevel@tonic-gate */ 46737c478bd9Sstevel@tonic-gate 46747c478bd9Sstevel@tonic-gate /* 46757c478bd9Sstevel@tonic-gate * Reset the encapsulating process contract's zone. 46767c478bd9Sstevel@tonic-gate */ 46777c478bd9Sstevel@tonic-gate ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 46787c478bd9Sstevel@tonic-gate contract_setzuniqid(ct, zone->zone_uniqid); 46797c478bd9Sstevel@tonic-gate 46807c478bd9Sstevel@tonic-gate /* 46817c478bd9Sstevel@tonic-gate * Create a new task and associate the process with the project keyed 46827c478bd9Sstevel@tonic-gate * by (projid,zoneid). 46837c478bd9Sstevel@tonic-gate * 46847c478bd9Sstevel@tonic-gate * We might as well be in project 0; the global zone's projid doesn't 46857c478bd9Sstevel@tonic-gate * make much sense in a zone anyhow. 46867c478bd9Sstevel@tonic-gate * 46877c478bd9Sstevel@tonic-gate * This also increments zone_ntasks, and returns with p_lock held. 46887c478bd9Sstevel@tonic-gate */ 46897c478bd9Sstevel@tonic-gate tk = task_create(0, zone); 46907c478bd9Sstevel@tonic-gate oldtk = task_join(tk, 0); 46917c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 46927c478bd9Sstevel@tonic-gate 46937c478bd9Sstevel@tonic-gate pp->p_flag |= SZONETOP; 46947c478bd9Sstevel@tonic-gate pp->p_zone = zone; 46957c478bd9Sstevel@tonic-gate 46967c478bd9Sstevel@tonic-gate /* 46977c478bd9Sstevel@tonic-gate * call RCTLOP_SET functions on this proc 46987c478bd9Sstevel@tonic-gate */ 46997c478bd9Sstevel@tonic-gate e.rcep_p.zone = zone; 47007c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 47017c478bd9Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 47027c478bd9Sstevel@tonic-gate RCD_CALLBACK); 47037c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 47047c478bd9Sstevel@tonic-gate 47057c478bd9Sstevel@tonic-gate /* 47067c478bd9Sstevel@tonic-gate * We don't need to hold any of zsched's locks here; not only do we know 47077c478bd9Sstevel@tonic-gate * the process and zone aren't going away, we know its session isn't 47087c478bd9Sstevel@tonic-gate * changing either. 47097c478bd9Sstevel@tonic-gate * 47107c478bd9Sstevel@tonic-gate * By joining zsched's session here, we mimic the behavior in the 47117c478bd9Sstevel@tonic-gate * global zone of init's sid being the pid of sched. We extend this 47127c478bd9Sstevel@tonic-gate * to all zlogin-like zone_enter()'ing processes as well. 47137c478bd9Sstevel@tonic-gate */ 47147c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 47157c478bd9Sstevel@tonic-gate sp = zone->zone_zsched->p_sessp; 47169acbbeafSnn35248 sess_hold(zone->zone_zsched); 47177c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 47187c478bd9Sstevel@tonic-gate pgexit(pp); 47199acbbeafSnn35248 sess_rele(pp->p_sessp, B_TRUE); 47207c478bd9Sstevel@tonic-gate pp->p_sessp = sp; 47217c478bd9Sstevel@tonic-gate pgjoin(pp, zone->zone_zsched->p_pidp); 47220209230bSgjelinek 47230209230bSgjelinek /* 4724c97ad5cdSakolb * If any threads are scheduled to be placed on zone wait queue they 4725c97ad5cdSakolb * should abandon the idea since the wait queue is changing. 4726c97ad5cdSakolb * We need to be holding pidlock & p_lock to do this. 4727c97ad5cdSakolb */ 4728c97ad5cdSakolb if ((t = pp->p_tlist) != NULL) { 4729c97ad5cdSakolb do { 4730c97ad5cdSakolb thread_lock(t); 4731c97ad5cdSakolb /* 4732c97ad5cdSakolb * Kick this thread so that he doesn't sit 4733c97ad5cdSakolb * on a wrong wait queue. 4734c97ad5cdSakolb */ 4735c97ad5cdSakolb if (ISWAITING(t)) 4736c97ad5cdSakolb setrun_locked(t); 4737c97ad5cdSakolb 4738c97ad5cdSakolb if (t->t_schedflag & TS_ANYWAITQ) 4739c97ad5cdSakolb t->t_schedflag &= ~ TS_ANYWAITQ; 4740c97ad5cdSakolb 4741c97ad5cdSakolb thread_unlock(t); 4742c97ad5cdSakolb } while ((t = t->t_forw) != pp->p_tlist); 4743c97ad5cdSakolb } 4744c97ad5cdSakolb 4745c97ad5cdSakolb /* 47460209230bSgjelinek * If there is a default scheduling class for the zone and it is not 47470209230bSgjelinek * the class we are currently in, change all of the threads in the 47480209230bSgjelinek * process to the new class. We need to be holding pidlock & p_lock 47490209230bSgjelinek * when we call parmsset so this is a good place to do it. 47500209230bSgjelinek */ 47510209230bSgjelinek if (zone->zone_defaultcid > 0 && 47520209230bSgjelinek zone->zone_defaultcid != curthread->t_cid) { 47530209230bSgjelinek pcparms_t pcparms; 47540209230bSgjelinek 47550209230bSgjelinek pcparms.pc_cid = zone->zone_defaultcid; 47560209230bSgjelinek pcparms.pc_clparms[0] = 0; 47570209230bSgjelinek 47580209230bSgjelinek /* 47590209230bSgjelinek * If setting the class fails, we still want to enter the zone. 47600209230bSgjelinek */ 47610209230bSgjelinek if ((t = pp->p_tlist) != NULL) { 47620209230bSgjelinek do { 47630209230bSgjelinek (void) parmsset(&pcparms, t); 47640209230bSgjelinek } while ((t = t->t_forw) != pp->p_tlist); 47650209230bSgjelinek } 47660209230bSgjelinek } 47670209230bSgjelinek 47687c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 47697c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 47707c478bd9Sstevel@tonic-gate 47717c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 47727c478bd9Sstevel@tonic-gate /* 47737c478bd9Sstevel@tonic-gate * We're firmly in the zone; let pools progress. 47747c478bd9Sstevel@tonic-gate */ 47757c478bd9Sstevel@tonic-gate pool_unlock(); 47767c478bd9Sstevel@tonic-gate task_rele(oldtk); 47777c478bd9Sstevel@tonic-gate /* 47787c478bd9Sstevel@tonic-gate * We don't need to retain a hold on the zone since we already 47797c478bd9Sstevel@tonic-gate * incremented zone_ntasks, so the zone isn't going anywhere. 47807c478bd9Sstevel@tonic-gate */ 47817c478bd9Sstevel@tonic-gate zone_rele(zone); 47827c478bd9Sstevel@tonic-gate 47837c478bd9Sstevel@tonic-gate /* 47847c478bd9Sstevel@tonic-gate * Chroot 47857c478bd9Sstevel@tonic-gate */ 47867c478bd9Sstevel@tonic-gate vp = zone->zone_rootvp; 47877c478bd9Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 47887c478bd9Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 47897c478bd9Sstevel@tonic-gate 47907c478bd9Sstevel@tonic-gate /* 47917c478bd9Sstevel@tonic-gate * Change process credentials 47927c478bd9Sstevel@tonic-gate */ 47937c478bd9Sstevel@tonic-gate newcr = cralloc(); 47947c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 47957c478bd9Sstevel@tonic-gate cr = pp->p_cred; 47967c478bd9Sstevel@tonic-gate crcopy_to(cr, newcr); 47977c478bd9Sstevel@tonic-gate crsetzone(newcr, zone); 47987c478bd9Sstevel@tonic-gate pp->p_cred = newcr; 47997c478bd9Sstevel@tonic-gate 48007c478bd9Sstevel@tonic-gate /* 48017c478bd9Sstevel@tonic-gate * Restrict all process privilege sets to zone limit 48027c478bd9Sstevel@tonic-gate */ 48037c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 48047c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 48057c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 48067c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 48077c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 48087c478bd9Sstevel@tonic-gate crset(pp, newcr); 48097c478bd9Sstevel@tonic-gate 48107c478bd9Sstevel@tonic-gate /* 48117c478bd9Sstevel@tonic-gate * Adjust upcount to reflect zone entry. 48127c478bd9Sstevel@tonic-gate */ 48137c478bd9Sstevel@tonic-gate uid = crgetruid(newcr); 48147c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 48157c478bd9Sstevel@tonic-gate upcount_dec(uid, GLOBAL_ZONEID); 48167c478bd9Sstevel@tonic-gate upcount_inc(uid, zoneid); 48177c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 48187c478bd9Sstevel@tonic-gate 48197c478bd9Sstevel@tonic-gate /* 48207c478bd9Sstevel@tonic-gate * Set up core file path and content. 48217c478bd9Sstevel@tonic-gate */ 48227c478bd9Sstevel@tonic-gate set_core_defaults(); 48237c478bd9Sstevel@tonic-gate 48247c478bd9Sstevel@tonic-gate out: 48257c478bd9Sstevel@tonic-gate /* 48267c478bd9Sstevel@tonic-gate * Let the other lwps continue. 48277c478bd9Sstevel@tonic-gate */ 48287c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 48297c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 48307c478bd9Sstevel@tonic-gate continuelwps(pp); 48317c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 48327c478bd9Sstevel@tonic-gate 48337c478bd9Sstevel@tonic-gate return (err != 0 ? set_errno(err) : 0); 48347c478bd9Sstevel@tonic-gate } 48357c478bd9Sstevel@tonic-gate 48367c478bd9Sstevel@tonic-gate /* 48377c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_list(2). 48387c478bd9Sstevel@tonic-gate * 48397c478bd9Sstevel@tonic-gate * Processes running in a (non-global) zone only see themselves. 484045916cd2Sjpk * On labeled systems, they see all zones whose label they dominate. 48417c478bd9Sstevel@tonic-gate */ 48427c478bd9Sstevel@tonic-gate static int 48437c478bd9Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones) 48447c478bd9Sstevel@tonic-gate { 48457c478bd9Sstevel@tonic-gate zoneid_t *zoneids; 484648451833Scarlsonj zone_t *zone, *myzone; 48477c478bd9Sstevel@tonic-gate uint_t user_nzones, real_nzones; 484845916cd2Sjpk uint_t domi_nzones; 484945916cd2Sjpk int error; 48507c478bd9Sstevel@tonic-gate 48517c478bd9Sstevel@tonic-gate if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 48527c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 48537c478bd9Sstevel@tonic-gate 485448451833Scarlsonj myzone = curproc->p_zone; 485548451833Scarlsonj if (myzone != global_zone) { 485645916cd2Sjpk bslabel_t *mybslab; 485745916cd2Sjpk 485845916cd2Sjpk if (!is_system_labeled()) { 48597c478bd9Sstevel@tonic-gate /* just return current zone */ 486045916cd2Sjpk real_nzones = domi_nzones = 1; 48617c478bd9Sstevel@tonic-gate zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 486248451833Scarlsonj zoneids[0] = myzone->zone_id; 48637c478bd9Sstevel@tonic-gate } else { 486445916cd2Sjpk /* return all zones that are dominated */ 48657c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 48667c478bd9Sstevel@tonic-gate real_nzones = zonecount; 486745916cd2Sjpk domi_nzones = 0; 486845916cd2Sjpk if (real_nzones > 0) { 486945916cd2Sjpk zoneids = kmem_alloc(real_nzones * 487045916cd2Sjpk sizeof (zoneid_t), KM_SLEEP); 487148451833Scarlsonj mybslab = label2bslabel(myzone->zone_slabel); 487245916cd2Sjpk for (zone = list_head(&zone_active); 487345916cd2Sjpk zone != NULL; 487445916cd2Sjpk zone = list_next(&zone_active, zone)) { 487545916cd2Sjpk if (zone->zone_id == GLOBAL_ZONEID) 487645916cd2Sjpk continue; 487748451833Scarlsonj if (zone != myzone && 487848451833Scarlsonj (zone->zone_flags & ZF_IS_SCRATCH)) 487948451833Scarlsonj continue; 488048451833Scarlsonj /* 488148451833Scarlsonj * Note that a label always dominates 488248451833Scarlsonj * itself, so myzone is always included 488348451833Scarlsonj * in the list. 488448451833Scarlsonj */ 488545916cd2Sjpk if (bldominates(mybslab, 488645916cd2Sjpk label2bslabel(zone->zone_slabel))) { 488745916cd2Sjpk zoneids[domi_nzones++] = 488845916cd2Sjpk zone->zone_id; 488945916cd2Sjpk } 489045916cd2Sjpk } 489145916cd2Sjpk } 489245916cd2Sjpk mutex_exit(&zonehash_lock); 489345916cd2Sjpk } 489445916cd2Sjpk } else { 489545916cd2Sjpk mutex_enter(&zonehash_lock); 489645916cd2Sjpk real_nzones = zonecount; 489745916cd2Sjpk domi_nzones = 0; 489845916cd2Sjpk if (real_nzones > 0) { 48997c478bd9Sstevel@tonic-gate zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 49007c478bd9Sstevel@tonic-gate KM_SLEEP); 49017c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 49027c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 490345916cd2Sjpk zoneids[domi_nzones++] = zone->zone_id; 490445916cd2Sjpk ASSERT(domi_nzones == real_nzones); 49057c478bd9Sstevel@tonic-gate } 49067c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 49077c478bd9Sstevel@tonic-gate } 49087c478bd9Sstevel@tonic-gate 490945916cd2Sjpk /* 491045916cd2Sjpk * If user has allocated space for fewer entries than we found, then 491145916cd2Sjpk * return only up to his limit. Either way, tell him exactly how many 491245916cd2Sjpk * we found. 491345916cd2Sjpk */ 491445916cd2Sjpk if (domi_nzones < user_nzones) 491545916cd2Sjpk user_nzones = domi_nzones; 491645916cd2Sjpk error = 0; 491745916cd2Sjpk if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) { 49187c478bd9Sstevel@tonic-gate error = EFAULT; 491945916cd2Sjpk } else if (zoneidlist != NULL && user_nzones != 0) { 49207c478bd9Sstevel@tonic-gate if (copyout(zoneids, zoneidlist, 49217c478bd9Sstevel@tonic-gate user_nzones * sizeof (zoneid_t)) != 0) 49227c478bd9Sstevel@tonic-gate error = EFAULT; 49237c478bd9Sstevel@tonic-gate } 49247c478bd9Sstevel@tonic-gate 492545916cd2Sjpk if (real_nzones > 0) 49267c478bd9Sstevel@tonic-gate kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 49277c478bd9Sstevel@tonic-gate 492845916cd2Sjpk if (error != 0) 49297c478bd9Sstevel@tonic-gate return (set_errno(error)); 49307c478bd9Sstevel@tonic-gate else 49317c478bd9Sstevel@tonic-gate return (0); 49327c478bd9Sstevel@tonic-gate } 49337c478bd9Sstevel@tonic-gate 49347c478bd9Sstevel@tonic-gate /* 49357c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_lookup(2). 49367c478bd9Sstevel@tonic-gate * 493745916cd2Sjpk * Non-global zones are only able to see themselves and (on labeled systems) 493845916cd2Sjpk * the zones they dominate. 49397c478bd9Sstevel@tonic-gate */ 49407c478bd9Sstevel@tonic-gate static zoneid_t 49417c478bd9Sstevel@tonic-gate zone_lookup(const char *zone_name) 49427c478bd9Sstevel@tonic-gate { 49437c478bd9Sstevel@tonic-gate char *kname; 49447c478bd9Sstevel@tonic-gate zone_t *zone; 49457c478bd9Sstevel@tonic-gate zoneid_t zoneid; 49467c478bd9Sstevel@tonic-gate int err; 49477c478bd9Sstevel@tonic-gate 49487c478bd9Sstevel@tonic-gate if (zone_name == NULL) { 49497c478bd9Sstevel@tonic-gate /* return caller's zone id */ 49507c478bd9Sstevel@tonic-gate return (getzoneid()); 49517c478bd9Sstevel@tonic-gate } 49527c478bd9Sstevel@tonic-gate 49537c478bd9Sstevel@tonic-gate kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 49547c478bd9Sstevel@tonic-gate if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 49557c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 49567c478bd9Sstevel@tonic-gate return (set_errno(err)); 49577c478bd9Sstevel@tonic-gate } 49587c478bd9Sstevel@tonic-gate 49597c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 49607c478bd9Sstevel@tonic-gate zone = zone_find_all_by_name(kname); 49617c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 496245916cd2Sjpk /* 496345916cd2Sjpk * In a non-global zone, can only lookup global and own name. 496445916cd2Sjpk * In Trusted Extensions zone label dominance rules apply. 496545916cd2Sjpk */ 496645916cd2Sjpk if (zone == NULL || 496745916cd2Sjpk zone_status_get(zone) < ZONE_IS_READY || 496845916cd2Sjpk !zone_list_access(zone)) { 49697c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 49707c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 497145916cd2Sjpk } else { 49727c478bd9Sstevel@tonic-gate zoneid = zone->zone_id; 49737c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 49747c478bd9Sstevel@tonic-gate return (zoneid); 49757c478bd9Sstevel@tonic-gate } 497645916cd2Sjpk } 49777c478bd9Sstevel@tonic-gate 4978821c4a97Sdp static int 4979821c4a97Sdp zone_version(int *version_arg) 4980821c4a97Sdp { 4981821c4a97Sdp int version = ZONE_SYSCALL_API_VERSION; 4982821c4a97Sdp 4983821c4a97Sdp if (copyout(&version, version_arg, sizeof (int)) != 0) 4984821c4a97Sdp return (set_errno(EFAULT)); 4985821c4a97Sdp return (0); 4986821c4a97Sdp } 4987821c4a97Sdp 49887c478bd9Sstevel@tonic-gate /* ARGSUSED */ 49897c478bd9Sstevel@tonic-gate long 4990fa9e4066Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4) 49917c478bd9Sstevel@tonic-gate { 49927c478bd9Sstevel@tonic-gate zone_def zs; 49937c478bd9Sstevel@tonic-gate 49947c478bd9Sstevel@tonic-gate switch (cmd) { 49957c478bd9Sstevel@tonic-gate case ZONE_CREATE: 49967c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 49977c478bd9Sstevel@tonic-gate if (copyin(arg1, &zs, sizeof (zone_def))) { 49987c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 49997c478bd9Sstevel@tonic-gate } 50007c478bd9Sstevel@tonic-gate } else { 50017c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 50027c478bd9Sstevel@tonic-gate zone_def32 zs32; 50037c478bd9Sstevel@tonic-gate 50047c478bd9Sstevel@tonic-gate if (copyin(arg1, &zs32, sizeof (zone_def32))) { 50057c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 50067c478bd9Sstevel@tonic-gate } 50077c478bd9Sstevel@tonic-gate zs.zone_name = 50087c478bd9Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_name; 50097c478bd9Sstevel@tonic-gate zs.zone_root = 50107c478bd9Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_root; 50117c478bd9Sstevel@tonic-gate zs.zone_privs = 50127c478bd9Sstevel@tonic-gate (const struct priv_set *) 50137c478bd9Sstevel@tonic-gate (unsigned long)zs32.zone_privs; 50146f70df68Sdp zs.zone_privssz = zs32.zone_privssz; 50157c478bd9Sstevel@tonic-gate zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 50167c478bd9Sstevel@tonic-gate zs.rctlbufsz = zs32.rctlbufsz; 5017fa9e4066Sahrens zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf; 5018fa9e4066Sahrens zs.zfsbufsz = zs32.zfsbufsz; 50197c478bd9Sstevel@tonic-gate zs.extended_error = 50207c478bd9Sstevel@tonic-gate (int *)(unsigned long)zs32.extended_error; 502145916cd2Sjpk zs.match = zs32.match; 502245916cd2Sjpk zs.doi = zs32.doi; 502345916cd2Sjpk zs.label = (const bslabel_t *)(uintptr_t)zs32.label; 5024f4b3ec61Sdh155122 zs.flags = zs32.flags; 50257c478bd9Sstevel@tonic-gate #else 50267c478bd9Sstevel@tonic-gate panic("get_udatamodel() returned bogus result\n"); 50277c478bd9Sstevel@tonic-gate #endif 50287c478bd9Sstevel@tonic-gate } 50297c478bd9Sstevel@tonic-gate 50307c478bd9Sstevel@tonic-gate return (zone_create(zs.zone_name, zs.zone_root, 5031821c4a97Sdp zs.zone_privs, zs.zone_privssz, 5032821c4a97Sdp (caddr_t)zs.rctlbuf, zs.rctlbufsz, 5033fa9e4066Sahrens (caddr_t)zs.zfsbuf, zs.zfsbufsz, 503445916cd2Sjpk zs.extended_error, zs.match, zs.doi, 5035f4b3ec61Sdh155122 zs.label, zs.flags)); 50367c478bd9Sstevel@tonic-gate case ZONE_BOOT: 50373f2f09c1Sdp return (zone_boot((zoneid_t)(uintptr_t)arg1)); 50387c478bd9Sstevel@tonic-gate case ZONE_DESTROY: 50397c478bd9Sstevel@tonic-gate return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 50407c478bd9Sstevel@tonic-gate case ZONE_GETATTR: 50417c478bd9Sstevel@tonic-gate return (zone_getattr((zoneid_t)(uintptr_t)arg1, 50427c478bd9Sstevel@tonic-gate (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 50433f2f09c1Sdp case ZONE_SETATTR: 50443f2f09c1Sdp return (zone_setattr((zoneid_t)(uintptr_t)arg1, 50453f2f09c1Sdp (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 50467c478bd9Sstevel@tonic-gate case ZONE_ENTER: 50477c478bd9Sstevel@tonic-gate return (zone_enter((zoneid_t)(uintptr_t)arg1)); 50487c478bd9Sstevel@tonic-gate case ZONE_LIST: 50497c478bd9Sstevel@tonic-gate return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 50507c478bd9Sstevel@tonic-gate case ZONE_SHUTDOWN: 50517c478bd9Sstevel@tonic-gate return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 50527c478bd9Sstevel@tonic-gate case ZONE_LOOKUP: 50537c478bd9Sstevel@tonic-gate return (zone_lookup((const char *)arg1)); 5054821c4a97Sdp case ZONE_VERSION: 5055821c4a97Sdp return (zone_version((int *)arg1)); 5056f4b3ec61Sdh155122 case ZONE_ADD_DATALINK: 5057f4b3ec61Sdh155122 return (zone_add_datalink((zoneid_t)(uintptr_t)arg1, 5058f4b3ec61Sdh155122 (char *)arg2)); 5059f4b3ec61Sdh155122 case ZONE_DEL_DATALINK: 5060f4b3ec61Sdh155122 return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1, 5061f4b3ec61Sdh155122 (char *)arg2)); 5062f4b3ec61Sdh155122 case ZONE_CHECK_DATALINK: 5063f4b3ec61Sdh155122 return (zone_check_datalink((zoneid_t *)arg1, (char *)arg2)); 5064f4b3ec61Sdh155122 case ZONE_LIST_DATALINK: 5065f4b3ec61Sdh155122 return (zone_list_datalink((zoneid_t)(uintptr_t)arg1, 5066f4b3ec61Sdh155122 (int *)arg2, (char *)arg3)); 50677c478bd9Sstevel@tonic-gate default: 50687c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 50697c478bd9Sstevel@tonic-gate } 50707c478bd9Sstevel@tonic-gate } 50717c478bd9Sstevel@tonic-gate 50727c478bd9Sstevel@tonic-gate struct zarg { 50737c478bd9Sstevel@tonic-gate zone_t *zone; 50747c478bd9Sstevel@tonic-gate zone_cmd_arg_t arg; 50757c478bd9Sstevel@tonic-gate }; 50767c478bd9Sstevel@tonic-gate 50777c478bd9Sstevel@tonic-gate static int 50787c478bd9Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp) 50797c478bd9Sstevel@tonic-gate { 50807c478bd9Sstevel@tonic-gate char *buf; 50817c478bd9Sstevel@tonic-gate size_t buflen; 50827c478bd9Sstevel@tonic-gate int error; 50837c478bd9Sstevel@tonic-gate 50847c478bd9Sstevel@tonic-gate buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 50857c478bd9Sstevel@tonic-gate buf = kmem_alloc(buflen, KM_SLEEP); 50867c478bd9Sstevel@tonic-gate (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 50877c478bd9Sstevel@tonic-gate error = door_ki_open(buf, doorp); 50887c478bd9Sstevel@tonic-gate kmem_free(buf, buflen); 50897c478bd9Sstevel@tonic-gate return (error); 50907c478bd9Sstevel@tonic-gate } 50917c478bd9Sstevel@tonic-gate 50927c478bd9Sstevel@tonic-gate static void 50937c478bd9Sstevel@tonic-gate zone_release_door(door_handle_t *doorp) 50947c478bd9Sstevel@tonic-gate { 50957c478bd9Sstevel@tonic-gate door_ki_rele(*doorp); 50967c478bd9Sstevel@tonic-gate *doorp = NULL; 50977c478bd9Sstevel@tonic-gate } 50987c478bd9Sstevel@tonic-gate 50997c478bd9Sstevel@tonic-gate static void 51007c478bd9Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp) 51017c478bd9Sstevel@tonic-gate { 51027c478bd9Sstevel@tonic-gate door_handle_t door = NULL; 51037c478bd9Sstevel@tonic-gate door_arg_t darg, save_arg; 51047c478bd9Sstevel@tonic-gate char *zone_name; 51057c478bd9Sstevel@tonic-gate size_t zone_namelen; 51067c478bd9Sstevel@tonic-gate zoneid_t zoneid; 51077c478bd9Sstevel@tonic-gate zone_t *zone; 51087c478bd9Sstevel@tonic-gate zone_cmd_arg_t arg; 51097c478bd9Sstevel@tonic-gate uint64_t uniqid; 51107c478bd9Sstevel@tonic-gate size_t size; 51117c478bd9Sstevel@tonic-gate int error; 51127c478bd9Sstevel@tonic-gate int retry; 51137c478bd9Sstevel@tonic-gate 51147c478bd9Sstevel@tonic-gate zone = zargp->zone; 51157c478bd9Sstevel@tonic-gate arg = zargp->arg; 51167c478bd9Sstevel@tonic-gate kmem_free(zargp, sizeof (*zargp)); 51177c478bd9Sstevel@tonic-gate 51187c478bd9Sstevel@tonic-gate zone_namelen = strlen(zone->zone_name) + 1; 51197c478bd9Sstevel@tonic-gate zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 51207c478bd9Sstevel@tonic-gate bcopy(zone->zone_name, zone_name, zone_namelen); 51217c478bd9Sstevel@tonic-gate zoneid = zone->zone_id; 51227c478bd9Sstevel@tonic-gate uniqid = zone->zone_uniqid; 51237c478bd9Sstevel@tonic-gate /* 51247c478bd9Sstevel@tonic-gate * zoneadmd may be down, but at least we can empty out the zone. 51257c478bd9Sstevel@tonic-gate * We can ignore the return value of zone_empty() since we're called 51267c478bd9Sstevel@tonic-gate * from a kernel thread and know we won't be delivered any signals. 51277c478bd9Sstevel@tonic-gate */ 51287c478bd9Sstevel@tonic-gate ASSERT(curproc == &p0); 51297c478bd9Sstevel@tonic-gate (void) zone_empty(zone); 51307c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 51317c478bd9Sstevel@tonic-gate zone_rele(zone); 51327c478bd9Sstevel@tonic-gate 51337c478bd9Sstevel@tonic-gate size = sizeof (arg); 51347c478bd9Sstevel@tonic-gate darg.rbuf = (char *)&arg; 51357c478bd9Sstevel@tonic-gate darg.data_ptr = (char *)&arg; 51367c478bd9Sstevel@tonic-gate darg.rsize = size; 51377c478bd9Sstevel@tonic-gate darg.data_size = size; 51387c478bd9Sstevel@tonic-gate darg.desc_ptr = NULL; 51397c478bd9Sstevel@tonic-gate darg.desc_num = 0; 51407c478bd9Sstevel@tonic-gate 51417c478bd9Sstevel@tonic-gate save_arg = darg; 51427c478bd9Sstevel@tonic-gate /* 51437c478bd9Sstevel@tonic-gate * Since we're not holding a reference to the zone, any number of 51447c478bd9Sstevel@tonic-gate * things can go wrong, including the zone disappearing before we get a 51457c478bd9Sstevel@tonic-gate * chance to talk to zoneadmd. 51467c478bd9Sstevel@tonic-gate */ 51477c478bd9Sstevel@tonic-gate for (retry = 0; /* forever */; retry++) { 51487c478bd9Sstevel@tonic-gate if (door == NULL && 51497c478bd9Sstevel@tonic-gate (error = zone_lookup_door(zone_name, &door)) != 0) { 51507c478bd9Sstevel@tonic-gate goto next; 51517c478bd9Sstevel@tonic-gate } 51527c478bd9Sstevel@tonic-gate ASSERT(door != NULL); 51537c478bd9Sstevel@tonic-gate 51547c478bd9Sstevel@tonic-gate if ((error = door_ki_upcall(door, &darg)) == 0) { 51557c478bd9Sstevel@tonic-gate break; 51567c478bd9Sstevel@tonic-gate } 51577c478bd9Sstevel@tonic-gate switch (error) { 51587c478bd9Sstevel@tonic-gate case EINTR: 51597c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 51607c478bd9Sstevel@tonic-gate case EAGAIN: /* process may be forking */ 51617c478bd9Sstevel@tonic-gate /* 51627c478bd9Sstevel@tonic-gate * Back off for a bit 51637c478bd9Sstevel@tonic-gate */ 51647c478bd9Sstevel@tonic-gate break; 51657c478bd9Sstevel@tonic-gate case EBADF: 51667c478bd9Sstevel@tonic-gate zone_release_door(&door); 51677c478bd9Sstevel@tonic-gate if (zone_lookup_door(zone_name, &door) != 0) { 51687c478bd9Sstevel@tonic-gate /* 51697c478bd9Sstevel@tonic-gate * zoneadmd may be dead, but it may come back to 51707c478bd9Sstevel@tonic-gate * life later. 51717c478bd9Sstevel@tonic-gate */ 51727c478bd9Sstevel@tonic-gate break; 51737c478bd9Sstevel@tonic-gate } 51747c478bd9Sstevel@tonic-gate break; 51757c478bd9Sstevel@tonic-gate default: 51767c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 51777c478bd9Sstevel@tonic-gate "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 51787c478bd9Sstevel@tonic-gate error); 51797c478bd9Sstevel@tonic-gate goto out; 51807c478bd9Sstevel@tonic-gate } 51817c478bd9Sstevel@tonic-gate next: 51827c478bd9Sstevel@tonic-gate /* 51837c478bd9Sstevel@tonic-gate * If this isn't the same zone_t that we originally had in mind, 51847c478bd9Sstevel@tonic-gate * then this is the same as if two kadmin requests come in at 51857c478bd9Sstevel@tonic-gate * the same time: the first one wins. This means we lose, so we 51867c478bd9Sstevel@tonic-gate * bail. 51877c478bd9Sstevel@tonic-gate */ 51887c478bd9Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) { 51897c478bd9Sstevel@tonic-gate /* 51907c478bd9Sstevel@tonic-gate * Problem is solved. 51917c478bd9Sstevel@tonic-gate */ 51927c478bd9Sstevel@tonic-gate break; 51937c478bd9Sstevel@tonic-gate } 51947c478bd9Sstevel@tonic-gate if (zone->zone_uniqid != uniqid) { 51957c478bd9Sstevel@tonic-gate /* 51967c478bd9Sstevel@tonic-gate * zoneid recycled 51977c478bd9Sstevel@tonic-gate */ 51987c478bd9Sstevel@tonic-gate zone_rele(zone); 51997c478bd9Sstevel@tonic-gate break; 52007c478bd9Sstevel@tonic-gate } 52017c478bd9Sstevel@tonic-gate /* 52027c478bd9Sstevel@tonic-gate * We could zone_status_timedwait(), but there doesn't seem to 52037c478bd9Sstevel@tonic-gate * be much point in doing that (plus, it would mean that 52047c478bd9Sstevel@tonic-gate * zone_free() isn't called until this thread exits). 52057c478bd9Sstevel@tonic-gate */ 52067c478bd9Sstevel@tonic-gate zone_rele(zone); 52077c478bd9Sstevel@tonic-gate delay(hz); 52087c478bd9Sstevel@tonic-gate darg = save_arg; 52097c478bd9Sstevel@tonic-gate } 52107c478bd9Sstevel@tonic-gate out: 52117c478bd9Sstevel@tonic-gate if (door != NULL) { 52127c478bd9Sstevel@tonic-gate zone_release_door(&door); 52137c478bd9Sstevel@tonic-gate } 52147c478bd9Sstevel@tonic-gate kmem_free(zone_name, zone_namelen); 52157c478bd9Sstevel@tonic-gate thread_exit(); 52167c478bd9Sstevel@tonic-gate } 52177c478bd9Sstevel@tonic-gate 52187c478bd9Sstevel@tonic-gate /* 52193f2f09c1Sdp * Entry point for uadmin() to tell the zone to go away or reboot. Analog to 52203f2f09c1Sdp * kadmin(). The caller is a process in the zone. 52217c478bd9Sstevel@tonic-gate * 52227c478bd9Sstevel@tonic-gate * In order to shutdown the zone, we will hand off control to zoneadmd 52237c478bd9Sstevel@tonic-gate * (running in the global zone) via a door. We do a half-hearted job at 52247c478bd9Sstevel@tonic-gate * killing all processes in the zone, create a kernel thread to contact 52257c478bd9Sstevel@tonic-gate * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 52267c478bd9Sstevel@tonic-gate * a form of generation number used to let zoneadmd (as well as 52277c478bd9Sstevel@tonic-gate * zone_destroy()) know exactly which zone they're re talking about. 52287c478bd9Sstevel@tonic-gate */ 52297c478bd9Sstevel@tonic-gate int 52303f2f09c1Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp) 52317c478bd9Sstevel@tonic-gate { 52327c478bd9Sstevel@tonic-gate struct zarg *zargp; 52337c478bd9Sstevel@tonic-gate zone_cmd_t zcmd; 52347c478bd9Sstevel@tonic-gate zone_t *zone; 52357c478bd9Sstevel@tonic-gate 52367c478bd9Sstevel@tonic-gate zone = curproc->p_zone; 52377c478bd9Sstevel@tonic-gate ASSERT(getzoneid() != GLOBAL_ZONEID); 52387c478bd9Sstevel@tonic-gate 52397c478bd9Sstevel@tonic-gate switch (cmd) { 52407c478bd9Sstevel@tonic-gate case A_SHUTDOWN: 52417c478bd9Sstevel@tonic-gate switch (fcn) { 52427c478bd9Sstevel@tonic-gate case AD_HALT: 52437c478bd9Sstevel@tonic-gate case AD_POWEROFF: 52447c478bd9Sstevel@tonic-gate zcmd = Z_HALT; 52457c478bd9Sstevel@tonic-gate break; 52467c478bd9Sstevel@tonic-gate case AD_BOOT: 52477c478bd9Sstevel@tonic-gate zcmd = Z_REBOOT; 52487c478bd9Sstevel@tonic-gate break; 52497c478bd9Sstevel@tonic-gate case AD_IBOOT: 52507c478bd9Sstevel@tonic-gate case AD_SBOOT: 52517c478bd9Sstevel@tonic-gate case AD_SIBOOT: 52527c478bd9Sstevel@tonic-gate case AD_NOSYNC: 52537c478bd9Sstevel@tonic-gate return (ENOTSUP); 52547c478bd9Sstevel@tonic-gate default: 52557c478bd9Sstevel@tonic-gate return (EINVAL); 52567c478bd9Sstevel@tonic-gate } 52577c478bd9Sstevel@tonic-gate break; 52587c478bd9Sstevel@tonic-gate case A_REBOOT: 52597c478bd9Sstevel@tonic-gate zcmd = Z_REBOOT; 52607c478bd9Sstevel@tonic-gate break; 52617c478bd9Sstevel@tonic-gate case A_FTRACE: 52627c478bd9Sstevel@tonic-gate case A_REMOUNT: 52637c478bd9Sstevel@tonic-gate case A_FREEZE: 52647c478bd9Sstevel@tonic-gate case A_DUMP: 52657c478bd9Sstevel@tonic-gate return (ENOTSUP); 52667c478bd9Sstevel@tonic-gate default: 52677c478bd9Sstevel@tonic-gate ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 52687c478bd9Sstevel@tonic-gate return (EINVAL); 52697c478bd9Sstevel@tonic-gate } 52707c478bd9Sstevel@tonic-gate 52717c478bd9Sstevel@tonic-gate if (secpolicy_zone_admin(credp, B_FALSE)) 52727c478bd9Sstevel@tonic-gate return (EPERM); 52737c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 52743f2f09c1Sdp 52757c478bd9Sstevel@tonic-gate /* 52767c478bd9Sstevel@tonic-gate * zone_status can't be ZONE_IS_EMPTY or higher since curproc 52777c478bd9Sstevel@tonic-gate * is in the zone. 52787c478bd9Sstevel@tonic-gate */ 52797c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 52807c478bd9Sstevel@tonic-gate if (zone_status_get(zone) > ZONE_IS_RUNNING) { 52817c478bd9Sstevel@tonic-gate /* 52827c478bd9Sstevel@tonic-gate * This zone is already on its way down. 52837c478bd9Sstevel@tonic-gate */ 52847c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 52857c478bd9Sstevel@tonic-gate return (0); 52867c478bd9Sstevel@tonic-gate } 52877c478bd9Sstevel@tonic-gate /* 52887c478bd9Sstevel@tonic-gate * Prevent future zone_enter()s 52897c478bd9Sstevel@tonic-gate */ 52907c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 52917c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 52927c478bd9Sstevel@tonic-gate 52937c478bd9Sstevel@tonic-gate /* 52947c478bd9Sstevel@tonic-gate * Kill everyone now and call zoneadmd later. 52957c478bd9Sstevel@tonic-gate * zone_ki_call_zoneadmd() will do a more thorough job of this 52967c478bd9Sstevel@tonic-gate * later. 52977c478bd9Sstevel@tonic-gate */ 52987c478bd9Sstevel@tonic-gate killall(zone->zone_id); 52997c478bd9Sstevel@tonic-gate /* 53007c478bd9Sstevel@tonic-gate * Now, create the thread to contact zoneadmd and do the rest of the 53017c478bd9Sstevel@tonic-gate * work. This thread can't be created in our zone otherwise 53027c478bd9Sstevel@tonic-gate * zone_destroy() would deadlock. 53037c478bd9Sstevel@tonic-gate */ 53043f2f09c1Sdp zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP); 53057c478bd9Sstevel@tonic-gate zargp->arg.cmd = zcmd; 53067c478bd9Sstevel@tonic-gate zargp->arg.uniqid = zone->zone_uniqid; 53073f2f09c1Sdp zargp->zone = zone; 53087c478bd9Sstevel@tonic-gate (void) strcpy(zargp->arg.locale, "C"); 53093f2f09c1Sdp /* mdep was already copied in for us by uadmin */ 53103f2f09c1Sdp if (mdep != NULL) 53113f2f09c1Sdp (void) strlcpy(zargp->arg.bootbuf, mdep, 53123f2f09c1Sdp sizeof (zargp->arg.bootbuf)); 53133f2f09c1Sdp zone_hold(zone); 53147c478bd9Sstevel@tonic-gate 53157c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 53167c478bd9Sstevel@tonic-gate TS_RUN, minclsyspri); 53177c478bd9Sstevel@tonic-gate exit(CLD_EXITED, 0); 53187c478bd9Sstevel@tonic-gate 53197c478bd9Sstevel@tonic-gate return (EINVAL); 53207c478bd9Sstevel@tonic-gate } 53217c478bd9Sstevel@tonic-gate 53227c478bd9Sstevel@tonic-gate /* 53237c478bd9Sstevel@tonic-gate * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 53247c478bd9Sstevel@tonic-gate * status to ZONE_IS_SHUTTING_DOWN. 53257c478bd9Sstevel@tonic-gate */ 53267c478bd9Sstevel@tonic-gate void 53277c478bd9Sstevel@tonic-gate zone_shutdown_global(void) 53287c478bd9Sstevel@tonic-gate { 53297c478bd9Sstevel@tonic-gate ASSERT(curproc->p_zone == global_zone); 53307c478bd9Sstevel@tonic-gate 53317c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 53327c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 53337c478bd9Sstevel@tonic-gate zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 53347c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 53357c478bd9Sstevel@tonic-gate } 5336fa9e4066Sahrens 5337fa9e4066Sahrens /* 5338fa9e4066Sahrens * Returns true if the named dataset is visible in the current zone. 5339fa9e4066Sahrens * The 'write' parameter is set to 1 if the dataset is also writable. 5340fa9e4066Sahrens */ 5341fa9e4066Sahrens int 5342fa9e4066Sahrens zone_dataset_visible(const char *dataset, int *write) 5343fa9e4066Sahrens { 5344fa9e4066Sahrens zone_dataset_t *zd; 5345fa9e4066Sahrens size_t len; 5346fa9e4066Sahrens zone_t *zone = curproc->p_zone; 5347fa9e4066Sahrens 5348fa9e4066Sahrens if (dataset[0] == '\0') 5349fa9e4066Sahrens return (0); 5350fa9e4066Sahrens 5351fa9e4066Sahrens /* 5352fa9e4066Sahrens * Walk the list once, looking for datasets which match exactly, or 5353fa9e4066Sahrens * specify a dataset underneath an exported dataset. If found, return 5354fa9e4066Sahrens * true and note that it is writable. 5355fa9e4066Sahrens */ 5356fa9e4066Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 5357fa9e4066Sahrens zd = list_next(&zone->zone_datasets, zd)) { 5358fa9e4066Sahrens 5359fa9e4066Sahrens len = strlen(zd->zd_dataset); 5360fa9e4066Sahrens if (strlen(dataset) >= len && 5361fa9e4066Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 536295c9592aSmaybee (dataset[len] == '\0' || dataset[len] == '/' || 536395c9592aSmaybee dataset[len] == '@')) { 5364fa9e4066Sahrens if (write) 5365fa9e4066Sahrens *write = 1; 5366fa9e4066Sahrens return (1); 5367fa9e4066Sahrens } 5368fa9e4066Sahrens } 5369fa9e4066Sahrens 5370fa9e4066Sahrens /* 5371fa9e4066Sahrens * Walk the list a second time, searching for datasets which are parents 5372fa9e4066Sahrens * of exported datasets. These should be visible, but read-only. 5373fa9e4066Sahrens * 5374fa9e4066Sahrens * Note that we also have to support forms such as 'pool/dataset/', with 5375fa9e4066Sahrens * a trailing slash. 5376fa9e4066Sahrens */ 5377fa9e4066Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 5378fa9e4066Sahrens zd = list_next(&zone->zone_datasets, zd)) { 5379fa9e4066Sahrens 5380fa9e4066Sahrens len = strlen(dataset); 5381fa9e4066Sahrens if (dataset[len - 1] == '/') 5382fa9e4066Sahrens len--; /* Ignore trailing slash */ 5383fa9e4066Sahrens if (len < strlen(zd->zd_dataset) && 5384fa9e4066Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 5385fa9e4066Sahrens zd->zd_dataset[len] == '/') { 5386fa9e4066Sahrens if (write) 5387fa9e4066Sahrens *write = 0; 5388fa9e4066Sahrens return (1); 5389fa9e4066Sahrens } 5390fa9e4066Sahrens } 5391fa9e4066Sahrens 5392fa9e4066Sahrens return (0); 5393fa9e4066Sahrens } 539445916cd2Sjpk 539545916cd2Sjpk /* 539645916cd2Sjpk * zone_find_by_any_path() - 539745916cd2Sjpk * 539845916cd2Sjpk * kernel-private routine similar to zone_find_by_path(), but which 539945916cd2Sjpk * effectively compares against zone paths rather than zonerootpath 540045916cd2Sjpk * (i.e., the last component of zonerootpaths, which should be "root/", 540145916cd2Sjpk * are not compared.) This is done in order to accurately identify all 540245916cd2Sjpk * paths, whether zone-visible or not, including those which are parallel 540345916cd2Sjpk * to /root/, such as /dev/, /home/, etc... 540445916cd2Sjpk * 540545916cd2Sjpk * If the specified path does not fall under any zone path then global 540645916cd2Sjpk * zone is returned. 540745916cd2Sjpk * 540845916cd2Sjpk * The treat_abs parameter indicates whether the path should be treated as 540945916cd2Sjpk * an absolute path although it does not begin with "/". (This supports 541045916cd2Sjpk * nfs mount syntax such as host:any/path.) 541145916cd2Sjpk * 541245916cd2Sjpk * The caller is responsible for zone_rele of the returned zone. 541345916cd2Sjpk */ 541445916cd2Sjpk zone_t * 541545916cd2Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs) 541645916cd2Sjpk { 541745916cd2Sjpk zone_t *zone; 541845916cd2Sjpk int path_offset = 0; 541945916cd2Sjpk 542045916cd2Sjpk if (path == NULL) { 542145916cd2Sjpk zone_hold(global_zone); 542245916cd2Sjpk return (global_zone); 542345916cd2Sjpk } 542445916cd2Sjpk 542545916cd2Sjpk if (*path != '/') { 542645916cd2Sjpk ASSERT(treat_abs); 542745916cd2Sjpk path_offset = 1; 542845916cd2Sjpk } 542945916cd2Sjpk 543045916cd2Sjpk mutex_enter(&zonehash_lock); 543145916cd2Sjpk for (zone = list_head(&zone_active); zone != NULL; 543245916cd2Sjpk zone = list_next(&zone_active, zone)) { 543345916cd2Sjpk char *c; 543445916cd2Sjpk size_t pathlen; 54350f95d722Smp46848 char *rootpath_start; 543645916cd2Sjpk 543745916cd2Sjpk if (zone == global_zone) /* skip global zone */ 543845916cd2Sjpk continue; 543945916cd2Sjpk 544045916cd2Sjpk /* scan backwards to find start of last component */ 544145916cd2Sjpk c = zone->zone_rootpath + zone->zone_rootpathlen - 2; 544245916cd2Sjpk do { 544345916cd2Sjpk c--; 544445916cd2Sjpk } while (*c != '/'); 544545916cd2Sjpk 54460f95d722Smp46848 pathlen = c - zone->zone_rootpath + 1 - path_offset; 54470f95d722Smp46848 rootpath_start = (zone->zone_rootpath + path_offset); 54480f95d722Smp46848 if (strncmp(path, rootpath_start, pathlen) == 0) 544945916cd2Sjpk break; 545045916cd2Sjpk } 545145916cd2Sjpk if (zone == NULL) 545245916cd2Sjpk zone = global_zone; 545345916cd2Sjpk zone_hold(zone); 545445916cd2Sjpk mutex_exit(&zonehash_lock); 545545916cd2Sjpk return (zone); 545645916cd2Sjpk } 5457f4b3ec61Sdh155122 5458f4b3ec61Sdh155122 /* List of data link names which are accessible from the zone */ 5459f4b3ec61Sdh155122 struct dlnamelist { 5460f4b3ec61Sdh155122 char dlnl_name[LIFNAMSIZ]; 5461f4b3ec61Sdh155122 struct dlnamelist *dlnl_next; 5462f4b3ec61Sdh155122 }; 5463f4b3ec61Sdh155122 5464f4b3ec61Sdh155122 5465f4b3ec61Sdh155122 /* 5466f4b3ec61Sdh155122 * Check whether the datalink name (dlname) itself is present. 5467f4b3ec61Sdh155122 * Return true if found. 5468f4b3ec61Sdh155122 */ 5469f4b3ec61Sdh155122 static boolean_t 5470f4b3ec61Sdh155122 zone_dlname(zone_t *zone, char *dlname) 5471f4b3ec61Sdh155122 { 5472f4b3ec61Sdh155122 struct dlnamelist *dlnl; 5473f4b3ec61Sdh155122 boolean_t found = B_FALSE; 5474f4b3ec61Sdh155122 5475f4b3ec61Sdh155122 mutex_enter(&zone->zone_lock); 5476f4b3ec61Sdh155122 for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) { 5477f4b3ec61Sdh155122 if (strncmp(dlnl->dlnl_name, dlname, LIFNAMSIZ) == 0) { 5478f4b3ec61Sdh155122 found = B_TRUE; 5479f4b3ec61Sdh155122 break; 5480f4b3ec61Sdh155122 } 5481f4b3ec61Sdh155122 } 5482f4b3ec61Sdh155122 mutex_exit(&zone->zone_lock); 5483f4b3ec61Sdh155122 return (found); 5484f4b3ec61Sdh155122 } 5485f4b3ec61Sdh155122 5486f4b3ec61Sdh155122 /* 5487f4b3ec61Sdh155122 * Add an data link name for the zone. Does not check for duplicates. 5488f4b3ec61Sdh155122 */ 5489f4b3ec61Sdh155122 static int 5490f4b3ec61Sdh155122 zone_add_datalink(zoneid_t zoneid, char *dlname) 5491f4b3ec61Sdh155122 { 5492f4b3ec61Sdh155122 struct dlnamelist *dlnl; 5493f4b3ec61Sdh155122 zone_t *zone; 5494f4b3ec61Sdh155122 zone_t *thiszone; 5495f4b3ec61Sdh155122 int err; 5496f4b3ec61Sdh155122 5497f4b3ec61Sdh155122 dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP); 5498f4b3ec61Sdh155122 if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) { 5499f4b3ec61Sdh155122 kmem_free(dlnl, sizeof (struct dlnamelist)); 5500f4b3ec61Sdh155122 return (set_errno(err)); 5501f4b3ec61Sdh155122 } 5502f4b3ec61Sdh155122 5503f4b3ec61Sdh155122 thiszone = zone_find_by_id(zoneid); 5504f4b3ec61Sdh155122 if (thiszone == NULL) { 5505f4b3ec61Sdh155122 kmem_free(dlnl, sizeof (struct dlnamelist)); 5506f4b3ec61Sdh155122 return (set_errno(ENXIO)); 5507f4b3ec61Sdh155122 } 5508f4b3ec61Sdh155122 5509f4b3ec61Sdh155122 /* 5510f4b3ec61Sdh155122 * Verify that the datalink name isn't already used by a different 5511f4b3ec61Sdh155122 * zone while allowing duplicate entries for the same zone (e.g. due 5512f4b3ec61Sdh155122 * to both using IPv4 and IPv6 on an interface) 5513f4b3ec61Sdh155122 */ 5514f4b3ec61Sdh155122 mutex_enter(&zonehash_lock); 5515f4b3ec61Sdh155122 for (zone = list_head(&zone_active); zone != NULL; 5516f4b3ec61Sdh155122 zone = list_next(&zone_active, zone)) { 5517f4b3ec61Sdh155122 if (zone->zone_id == zoneid) 5518f4b3ec61Sdh155122 continue; 5519f4b3ec61Sdh155122 5520f4b3ec61Sdh155122 if (zone_dlname(zone, dlnl->dlnl_name)) { 5521f4b3ec61Sdh155122 mutex_exit(&zonehash_lock); 5522f4b3ec61Sdh155122 zone_rele(thiszone); 5523f4b3ec61Sdh155122 kmem_free(dlnl, sizeof (struct dlnamelist)); 5524f4b3ec61Sdh155122 return (set_errno(EPERM)); 5525f4b3ec61Sdh155122 } 5526f4b3ec61Sdh155122 } 5527f4b3ec61Sdh155122 mutex_enter(&thiszone->zone_lock); 5528f4b3ec61Sdh155122 dlnl->dlnl_next = thiszone->zone_dl_list; 5529f4b3ec61Sdh155122 thiszone->zone_dl_list = dlnl; 5530f4b3ec61Sdh155122 mutex_exit(&thiszone->zone_lock); 5531f4b3ec61Sdh155122 mutex_exit(&zonehash_lock); 5532f4b3ec61Sdh155122 zone_rele(thiszone); 5533f4b3ec61Sdh155122 return (0); 5534f4b3ec61Sdh155122 } 5535f4b3ec61Sdh155122 5536f4b3ec61Sdh155122 static int 5537f4b3ec61Sdh155122 zone_remove_datalink(zoneid_t zoneid, char *dlname) 5538f4b3ec61Sdh155122 { 5539f4b3ec61Sdh155122 struct dlnamelist *dlnl, *odlnl, **dlnlp; 5540f4b3ec61Sdh155122 zone_t *zone; 5541f4b3ec61Sdh155122 int err; 5542f4b3ec61Sdh155122 5543f4b3ec61Sdh155122 dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP); 5544f4b3ec61Sdh155122 if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) { 5545f4b3ec61Sdh155122 kmem_free(dlnl, sizeof (struct dlnamelist)); 5546f4b3ec61Sdh155122 return (set_errno(err)); 5547f4b3ec61Sdh155122 } 5548f4b3ec61Sdh155122 zone = zone_find_by_id(zoneid); 5549f4b3ec61Sdh155122 if (zone == NULL) { 5550f4b3ec61Sdh155122 kmem_free(dlnl, sizeof (struct dlnamelist)); 5551f4b3ec61Sdh155122 return (set_errno(EINVAL)); 5552f4b3ec61Sdh155122 } 5553f4b3ec61Sdh155122 5554f4b3ec61Sdh155122 mutex_enter(&zone->zone_lock); 5555f4b3ec61Sdh155122 /* Look for match */ 5556f4b3ec61Sdh155122 dlnlp = &zone->zone_dl_list; 5557f4b3ec61Sdh155122 while (*dlnlp != NULL) { 5558f4b3ec61Sdh155122 if (strncmp(dlnl->dlnl_name, (*dlnlp)->dlnl_name, 5559f4b3ec61Sdh155122 LIFNAMSIZ) == 0) 5560f4b3ec61Sdh155122 goto found; 5561f4b3ec61Sdh155122 dlnlp = &((*dlnlp)->dlnl_next); 5562f4b3ec61Sdh155122 } 5563f4b3ec61Sdh155122 mutex_exit(&zone->zone_lock); 5564f4b3ec61Sdh155122 zone_rele(zone); 5565f4b3ec61Sdh155122 kmem_free(dlnl, sizeof (struct dlnamelist)); 5566f4b3ec61Sdh155122 return (set_errno(ENXIO)); 5567f4b3ec61Sdh155122 5568f4b3ec61Sdh155122 found: 5569f4b3ec61Sdh155122 odlnl = *dlnlp; 5570f4b3ec61Sdh155122 *dlnlp = (*dlnlp)->dlnl_next; 5571f4b3ec61Sdh155122 kmem_free(odlnl, sizeof (struct dlnamelist)); 5572f4b3ec61Sdh155122 5573f4b3ec61Sdh155122 mutex_exit(&zone->zone_lock); 5574f4b3ec61Sdh155122 zone_rele(zone); 5575f4b3ec61Sdh155122 kmem_free(dlnl, sizeof (struct dlnamelist)); 5576f4b3ec61Sdh155122 return (0); 5577f4b3ec61Sdh155122 } 5578f4b3ec61Sdh155122 5579f4b3ec61Sdh155122 /* 5580f4b3ec61Sdh155122 * Using the zoneidp as ALL_ZONES, we can lookup which zone is using datalink 5581f4b3ec61Sdh155122 * name (dlname); otherwise we just check if the specified zoneidp has access 5582f4b3ec61Sdh155122 * to the datalink name. 5583f4b3ec61Sdh155122 */ 5584f4b3ec61Sdh155122 static int 5585f4b3ec61Sdh155122 zone_check_datalink(zoneid_t *zoneidp, char *dlname) 5586f4b3ec61Sdh155122 { 5587f4b3ec61Sdh155122 zoneid_t id; 5588f4b3ec61Sdh155122 char *dln; 5589f4b3ec61Sdh155122 zone_t *zone; 5590f4b3ec61Sdh155122 int err = 0; 5591f4b3ec61Sdh155122 boolean_t allzones = B_FALSE; 5592f4b3ec61Sdh155122 5593f4b3ec61Sdh155122 if (copyin(zoneidp, &id, sizeof (id)) != 0) { 5594f4b3ec61Sdh155122 return (set_errno(EFAULT)); 5595f4b3ec61Sdh155122 } 5596f4b3ec61Sdh155122 dln = kmem_zalloc(LIFNAMSIZ, KM_SLEEP); 5597f4b3ec61Sdh155122 if ((err = copyinstr(dlname, dln, LIFNAMSIZ, NULL)) != 0) { 5598f4b3ec61Sdh155122 kmem_free(dln, LIFNAMSIZ); 5599f4b3ec61Sdh155122 return (set_errno(err)); 5600f4b3ec61Sdh155122 } 5601f4b3ec61Sdh155122 5602f4b3ec61Sdh155122 if (id == ALL_ZONES) 5603f4b3ec61Sdh155122 allzones = B_TRUE; 5604f4b3ec61Sdh155122 5605f4b3ec61Sdh155122 /* 5606f4b3ec61Sdh155122 * Check whether datalink name is already used. 5607f4b3ec61Sdh155122 */ 5608f4b3ec61Sdh155122 mutex_enter(&zonehash_lock); 5609f4b3ec61Sdh155122 for (zone = list_head(&zone_active); zone != NULL; 5610f4b3ec61Sdh155122 zone = list_next(&zone_active, zone)) { 5611f4b3ec61Sdh155122 if (allzones || (id == zone->zone_id)) { 5612f4b3ec61Sdh155122 if (!zone_dlname(zone, dln)) 5613f4b3ec61Sdh155122 continue; 5614f4b3ec61Sdh155122 if (allzones) 5615f4b3ec61Sdh155122 err = copyout(&zone->zone_id, zoneidp, 5616f4b3ec61Sdh155122 sizeof (*zoneidp)); 5617f4b3ec61Sdh155122 5618f4b3ec61Sdh155122 mutex_exit(&zonehash_lock); 5619f4b3ec61Sdh155122 kmem_free(dln, LIFNAMSIZ); 5620f4b3ec61Sdh155122 return (err ? set_errno(EFAULT) : 0); 5621f4b3ec61Sdh155122 } 5622f4b3ec61Sdh155122 } 5623f4b3ec61Sdh155122 5624f4b3ec61Sdh155122 /* datalink name is not found in any active zone. */ 5625f4b3ec61Sdh155122 mutex_exit(&zonehash_lock); 5626f4b3ec61Sdh155122 kmem_free(dln, LIFNAMSIZ); 5627f4b3ec61Sdh155122 return (set_errno(ENXIO)); 5628f4b3ec61Sdh155122 } 5629f4b3ec61Sdh155122 5630f4b3ec61Sdh155122 /* 5631f4b3ec61Sdh155122 * Get the names of the datalinks assigned to a zone. 5632f4b3ec61Sdh155122 * Here *nump is the number of datalinks, and the assumption 5633f4b3ec61Sdh155122 * is that the caller will gurantee that the the supplied buffer is 5634f4b3ec61Sdh155122 * big enough to hold at least #*nump datalink names, that is, 5635f4b3ec61Sdh155122 * LIFNAMSIZ X *nump 5636f4b3ec61Sdh155122 * On return, *nump will be the "new" number of datalinks, if it 5637f4b3ec61Sdh155122 * ever changed. 5638f4b3ec61Sdh155122 */ 5639f4b3ec61Sdh155122 static int 5640f4b3ec61Sdh155122 zone_list_datalink(zoneid_t zoneid, int *nump, char *buf) 5641f4b3ec61Sdh155122 { 5642f4b3ec61Sdh155122 int num, dlcount; 5643f4b3ec61Sdh155122 zone_t *zone; 5644f4b3ec61Sdh155122 struct dlnamelist *dlnl; 5645f4b3ec61Sdh155122 char *ptr; 5646f4b3ec61Sdh155122 5647f4b3ec61Sdh155122 if (copyin(nump, &dlcount, sizeof (dlcount)) != 0) 5648f4b3ec61Sdh155122 return (set_errno(EFAULT)); 5649f4b3ec61Sdh155122 5650f4b3ec61Sdh155122 zone = zone_find_by_id(zoneid); 5651f4b3ec61Sdh155122 if (zone == NULL) { 5652f4b3ec61Sdh155122 return (set_errno(ENXIO)); 5653f4b3ec61Sdh155122 } 5654f4b3ec61Sdh155122 5655f4b3ec61Sdh155122 num = 0; 5656f4b3ec61Sdh155122 mutex_enter(&zone->zone_lock); 5657f4b3ec61Sdh155122 ptr = buf; 5658f4b3ec61Sdh155122 for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) { 5659f4b3ec61Sdh155122 /* 5660f4b3ec61Sdh155122 * If the list changed and the new number is bigger 5661f4b3ec61Sdh155122 * than what the caller supplied, just count, don't 5662f4b3ec61Sdh155122 * do copyout 5663f4b3ec61Sdh155122 */ 5664f4b3ec61Sdh155122 if (++num > dlcount) 5665f4b3ec61Sdh155122 continue; 5666f4b3ec61Sdh155122 if (copyout(dlnl->dlnl_name, ptr, LIFNAMSIZ) != 0) { 5667f4b3ec61Sdh155122 mutex_exit(&zone->zone_lock); 5668f4b3ec61Sdh155122 zone_rele(zone); 5669f4b3ec61Sdh155122 return (set_errno(EFAULT)); 5670f4b3ec61Sdh155122 } 5671f4b3ec61Sdh155122 ptr += LIFNAMSIZ; 5672f4b3ec61Sdh155122 } 5673f4b3ec61Sdh155122 mutex_exit(&zone->zone_lock); 5674f4b3ec61Sdh155122 zone_rele(zone); 5675f4b3ec61Sdh155122 5676f4b3ec61Sdh155122 /* Increased or decreased, caller should be notified. */ 5677f4b3ec61Sdh155122 if (num != dlcount) { 5678f4b3ec61Sdh155122 if (copyout(&num, nump, sizeof (num)) != 0) { 5679f4b3ec61Sdh155122 return (set_errno(EFAULT)); 5680f4b3ec61Sdh155122 } 5681f4b3ec61Sdh155122 } 5682f4b3ec61Sdh155122 return (0); 5683f4b3ec61Sdh155122 } 5684f4b3ec61Sdh155122 5685f4b3ec61Sdh155122 /* 5686f4b3ec61Sdh155122 * Public interface for looking up a zone by zoneid. It's a customized version 5687f4b3ec61Sdh155122 * for netstack_zone_create(), it: 5688f4b3ec61Sdh155122 * 1. Doesn't acquire the zonehash_lock, since it is called from 5689f4b3ec61Sdh155122 * zone_key_create() or zone_zsd_configure(), lock already held. 5690f4b3ec61Sdh155122 * 2. Doesn't check the status of the zone. 5691f4b3ec61Sdh155122 * 3. It will be called even before zone_init is called, in that case the 5692f4b3ec61Sdh155122 * address of zone0 is returned directly, and netstack_zone_create() 5693f4b3ec61Sdh155122 * will only assign a value to zone0.zone_netstack, won't break anything. 5694f4b3ec61Sdh155122 */ 5695f4b3ec61Sdh155122 zone_t * 5696f4b3ec61Sdh155122 zone_find_by_id_nolock(zoneid_t zoneid) 5697f4b3ec61Sdh155122 { 5698f4b3ec61Sdh155122 ASSERT(MUTEX_HELD(&zonehash_lock)); 5699f4b3ec61Sdh155122 5700f4b3ec61Sdh155122 if (zonehashbyid == NULL) 5701f4b3ec61Sdh155122 return (&zone0); 5702f4b3ec61Sdh155122 else 5703f4b3ec61Sdh155122 return (zone_find_all_by_id(zoneid)); 5704f4b3ec61Sdh155122 } 5705