17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 545916cd2Sjpk * Common Development and Distribution License (the "License"). 645916cd2Sjpk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 2197eda132Sraf 227c478bd9Sstevel@tonic-gate /* 23134a1f4eSCasper H.S. Dik * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * Zones 287c478bd9Sstevel@tonic-gate * 297c478bd9Sstevel@tonic-gate * A zone is a named collection of processes, namespace constraints, 307c478bd9Sstevel@tonic-gate * and other system resources which comprise a secure and manageable 317c478bd9Sstevel@tonic-gate * application containment facility. 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * Zones (represented by the reference counted zone_t) are tracked in 347c478bd9Sstevel@tonic-gate * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs 357c478bd9Sstevel@tonic-gate * (zoneid_t) are used to track zone association. Zone IDs are 367c478bd9Sstevel@tonic-gate * dynamically generated when the zone is created; if a persistent 377c478bd9Sstevel@tonic-gate * identifier is needed (core files, accounting logs, audit trail, 387c478bd9Sstevel@tonic-gate * etc.), the zone name should be used. 397c478bd9Sstevel@tonic-gate * 407c478bd9Sstevel@tonic-gate * 417c478bd9Sstevel@tonic-gate * Global Zone: 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate * The global zone (zoneid 0) is automatically associated with all 447c478bd9Sstevel@tonic-gate * system resources that have not been bound to a user-created zone. 457c478bd9Sstevel@tonic-gate * This means that even systems where zones are not in active use 467c478bd9Sstevel@tonic-gate * have a global zone, and all processes, mounts, etc. are 477c478bd9Sstevel@tonic-gate * associated with that zone. The global zone is generally 487c478bd9Sstevel@tonic-gate * unconstrained in terms of privileges and access, though the usual 497c478bd9Sstevel@tonic-gate * credential and privilege based restrictions apply. 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * Zone States: 537c478bd9Sstevel@tonic-gate * 547c478bd9Sstevel@tonic-gate * The states in which a zone may be in and the transitions are as 557c478bd9Sstevel@tonic-gate * follows: 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially 587c478bd9Sstevel@tonic-gate * initialized zone is added to the list of active zones on the system but 597c478bd9Sstevel@tonic-gate * isn't accessible. 607c478bd9Sstevel@tonic-gate * 61bd41d0a8Snordmark * ZONE_IS_INITIALIZED: Initialization complete except the ZSD callbacks are 62bd41d0a8Snordmark * not yet completed. Not possible to enter the zone, but attributes can 63bd41d0a8Snordmark * be retrieved. 64bd41d0a8Snordmark * 657c478bd9Sstevel@tonic-gate * ZONE_IS_READY: zsched (the kernel dummy process for a zone) is 667c478bd9Sstevel@tonic-gate * ready. The zone is made visible after the ZSD constructor callbacks are 677c478bd9Sstevel@tonic-gate * executed. A zone remains in this state until it transitions into 687c478bd9Sstevel@tonic-gate * the ZONE_IS_BOOTING state as a result of a call to zone_boot(). 697c478bd9Sstevel@tonic-gate * 707c478bd9Sstevel@tonic-gate * ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start 717c478bd9Sstevel@tonic-gate * init. Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN 727c478bd9Sstevel@tonic-gate * state. 737c478bd9Sstevel@tonic-gate * 747c478bd9Sstevel@tonic-gate * ZONE_IS_RUNNING: The zone is open for business: zsched has 757c478bd9Sstevel@tonic-gate * successfully started init. A zone remains in this state until 767c478bd9Sstevel@tonic-gate * zone_shutdown() is called. 777c478bd9Sstevel@tonic-gate * 787c478bd9Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is 797c478bd9Sstevel@tonic-gate * killing all processes running in the zone. The zone remains 807c478bd9Sstevel@tonic-gate * in this state until there are no more user processes running in the zone. 817c478bd9Sstevel@tonic-gate * zone_create(), zone_enter(), and zone_destroy() on this zone will fail. 827c478bd9Sstevel@tonic-gate * Since zone_shutdown() is restartable, it may be called successfully 837c478bd9Sstevel@tonic-gate * multiple times for the same zone_t. Setting of the zone's state to 847c478bd9Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check 857c478bd9Sstevel@tonic-gate * the zone's status without worrying about it being a moving target. 867c478bd9Sstevel@tonic-gate * 877c478bd9Sstevel@tonic-gate * ZONE_IS_EMPTY: zone_shutdown() has been called, and there 887c478bd9Sstevel@tonic-gate * are no more user processes in the zone. The zone remains in this 897c478bd9Sstevel@tonic-gate * state until there are no more kernel threads associated with the 907c478bd9Sstevel@tonic-gate * zone. zone_create(), zone_enter(), and zone_destroy() on this zone will 917c478bd9Sstevel@tonic-gate * fail. 927c478bd9Sstevel@tonic-gate * 937c478bd9Sstevel@tonic-gate * ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone 947c478bd9Sstevel@tonic-gate * have exited. zone_shutdown() returns. Henceforth it is not possible to 957c478bd9Sstevel@tonic-gate * join the zone or create kernel threads therein. 967c478bd9Sstevel@tonic-gate * 977c478bd9Sstevel@tonic-gate * ZONE_IS_DYING: zone_destroy() has been called on the zone; zone 987c478bd9Sstevel@tonic-gate * remains in this state until zsched exits. Calls to zone_find_by_*() 997c478bd9Sstevel@tonic-gate * return NULL from now on. 1007c478bd9Sstevel@tonic-gate * 1017c478bd9Sstevel@tonic-gate * ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0). There are no 1027c478bd9Sstevel@tonic-gate * processes or threads doing work on behalf of the zone. The zone is 1037c478bd9Sstevel@tonic-gate * removed from the list of active zones. zone_destroy() returns, and 1047c478bd9Sstevel@tonic-gate * the zone can be recreated. 1057c478bd9Sstevel@tonic-gate * 1067c478bd9Sstevel@tonic-gate * ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor 1077c478bd9Sstevel@tonic-gate * callbacks are executed, and all memory associated with the zone is 1087c478bd9Sstevel@tonic-gate * freed. 1097c478bd9Sstevel@tonic-gate * 1107c478bd9Sstevel@tonic-gate * Threads can wait for the zone to enter a requested state by using 1117c478bd9Sstevel@tonic-gate * zone_status_wait() or zone_status_timedwait() with the desired 1127c478bd9Sstevel@tonic-gate * state passed in as an argument. Zone state transitions are 1137c478bd9Sstevel@tonic-gate * uni-directional; it is not possible to move back to an earlier state. 1147c478bd9Sstevel@tonic-gate * 1157c478bd9Sstevel@tonic-gate * 1167c478bd9Sstevel@tonic-gate * Zone-Specific Data: 1177c478bd9Sstevel@tonic-gate * 1187c478bd9Sstevel@tonic-gate * Subsystems needing to maintain zone-specific data can store that 1197c478bd9Sstevel@tonic-gate * data using the ZSD mechanism. This provides a zone-specific data 1207c478bd9Sstevel@tonic-gate * store, similar to thread-specific data (see pthread_getspecific(3C) 1217c478bd9Sstevel@tonic-gate * or the TSD code in uts/common/disp/thread.c. Also, ZSD can be used 1227c478bd9Sstevel@tonic-gate * to register callbacks to be invoked when a zone is created, shut 1237c478bd9Sstevel@tonic-gate * down, or destroyed. This can be used to initialize zone-specific 1247c478bd9Sstevel@tonic-gate * data for new zones and to clean up when zones go away. 1257c478bd9Sstevel@tonic-gate * 1267c478bd9Sstevel@tonic-gate * 1277c478bd9Sstevel@tonic-gate * Data Structures: 1287c478bd9Sstevel@tonic-gate * 1297c478bd9Sstevel@tonic-gate * The per-zone structure (zone_t) is reference counted, and freed 1307c478bd9Sstevel@tonic-gate * when all references are released. zone_hold and zone_rele can be 1317c478bd9Sstevel@tonic-gate * used to adjust the reference count. In addition, reference counts 1327c478bd9Sstevel@tonic-gate * associated with the cred_t structure are tracked separately using 1337c478bd9Sstevel@tonic-gate * zone_cred_hold and zone_cred_rele. 1347c478bd9Sstevel@tonic-gate * 1357c478bd9Sstevel@tonic-gate * Pointers to active zone_t's are stored in two hash tables; one 1367c478bd9Sstevel@tonic-gate * for searching by id, the other for searching by name. Lookups 1377c478bd9Sstevel@tonic-gate * can be performed on either basis, using zone_find_by_id and 1387c478bd9Sstevel@tonic-gate * zone_find_by_name. Both return zone_t pointers with the zone 1397c478bd9Sstevel@tonic-gate * held, so zone_rele should be called when the pointer is no longer 1407c478bd9Sstevel@tonic-gate * needed. Zones can also be searched by path; zone_find_by_path 1417c478bd9Sstevel@tonic-gate * returns the zone with which a path name is associated (global 1427c478bd9Sstevel@tonic-gate * zone if the path is not within some other zone's file system 1437c478bd9Sstevel@tonic-gate * hierarchy). This currently requires iterating through each zone, 1447c478bd9Sstevel@tonic-gate * so it is slower than an id or name search via a hash table. 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * 1477c478bd9Sstevel@tonic-gate * Locking: 1487c478bd9Sstevel@tonic-gate * 1497c478bd9Sstevel@tonic-gate * zonehash_lock: This is a top-level global lock used to protect the 1507c478bd9Sstevel@tonic-gate * zone hash tables and lists. Zones cannot be created or destroyed 1517c478bd9Sstevel@tonic-gate * while this lock is held. 1527c478bd9Sstevel@tonic-gate * zone_status_lock: This is a global lock protecting zone state. 1537c478bd9Sstevel@tonic-gate * Zones cannot change state while this lock is held. It also 1547c478bd9Sstevel@tonic-gate * protects the list of kernel threads associated with a zone. 1557c478bd9Sstevel@tonic-gate * zone_lock: This is a per-zone lock used to protect several fields of 1567c478bd9Sstevel@tonic-gate * the zone_t (see <sys/zone.h> for details). In addition, holding 1577c478bd9Sstevel@tonic-gate * this lock means that the zone cannot go away. 1580209230bSgjelinek * zone_nlwps_lock: This is a per-zone lock used to protect the fields 1590209230bSgjelinek * related to the zone.max-lwps rctl. 1600209230bSgjelinek * zone_mem_lock: This is a per-zone lock used to protect the fields 1610209230bSgjelinek * related to the zone.max-locked-memory and zone.max-swap rctls. 1620fbb751dSJohn Levon * zone_rctl_lock: This is a per-zone lock used to protect other rctls, 1630fbb751dSJohn Levon * currently just max_lofi 1647c478bd9Sstevel@tonic-gate * zsd_key_lock: This is a global lock protecting the key state for ZSD. 1657c478bd9Sstevel@tonic-gate * zone_deathrow_lock: This is a global lock protecting the "deathrow" 1667c478bd9Sstevel@tonic-gate * list (a list of zones in the ZONE_IS_DEAD state). 1677c478bd9Sstevel@tonic-gate * 1687c478bd9Sstevel@tonic-gate * Ordering requirements: 1697c478bd9Sstevel@tonic-gate * pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock --> 1707c478bd9Sstevel@tonic-gate * zone_lock --> zsd_key_lock --> pidlock --> p_lock 1717c478bd9Sstevel@tonic-gate * 1720209230bSgjelinek * When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is: 1730209230bSgjelinek * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 174ff19e029SMenno Lageman * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_nlwps_lock 1750209230bSgjelinek * 1767c478bd9Sstevel@tonic-gate * Blocking memory allocations are permitted while holding any of the 1777c478bd9Sstevel@tonic-gate * zone locks. 1787c478bd9Sstevel@tonic-gate * 1797c478bd9Sstevel@tonic-gate * 1807c478bd9Sstevel@tonic-gate * System Call Interface: 1817c478bd9Sstevel@tonic-gate * 1827c478bd9Sstevel@tonic-gate * The zone subsystem can be managed and queried from user level with 1837c478bd9Sstevel@tonic-gate * the following system calls (all subcodes of the primary "zone" 1847c478bd9Sstevel@tonic-gate * system call): 1857c478bd9Sstevel@tonic-gate * - zone_create: creates a zone with selected attributes (name, 186fa9e4066Sahrens * root path, privileges, resource controls, ZFS datasets) 1877c478bd9Sstevel@tonic-gate * - zone_enter: allows the current process to enter a zone 1887c478bd9Sstevel@tonic-gate * - zone_getattr: reports attributes of a zone 1893f2f09c1Sdp * - zone_setattr: set attributes of a zone 1903f2f09c1Sdp * - zone_boot: set 'init' running for the zone 1917c478bd9Sstevel@tonic-gate * - zone_list: lists all zones active in the system 1927c478bd9Sstevel@tonic-gate * - zone_lookup: looks up zone id based on name 1937c478bd9Sstevel@tonic-gate * - zone_shutdown: initiates shutdown process (see states above) 1947c478bd9Sstevel@tonic-gate * - zone_destroy: completes shutdown process (see states above) 1957c478bd9Sstevel@tonic-gate * 1967c478bd9Sstevel@tonic-gate */ 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h> 1997c478bd9Sstevel@tonic-gate #include <sys/cred.h> 2007c478bd9Sstevel@tonic-gate #include <c2/audit.h> 2017c478bd9Sstevel@tonic-gate #include <sys/debug.h> 2027c478bd9Sstevel@tonic-gate #include <sys/file.h> 2037c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 2040209230bSgjelinek #include <sys/kstat.h> 2057c478bd9Sstevel@tonic-gate #include <sys/mutex.h> 20645916cd2Sjpk #include <sys/note.h> 2077c478bd9Sstevel@tonic-gate #include <sys/pathname.h> 2087c478bd9Sstevel@tonic-gate #include <sys/proc.h> 2097c478bd9Sstevel@tonic-gate #include <sys/project.h> 210cf8f45c7Sdstaff #include <sys/sysevent.h> 2117c478bd9Sstevel@tonic-gate #include <sys/task.h> 2127c478bd9Sstevel@tonic-gate #include <sys/systm.h> 2137c478bd9Sstevel@tonic-gate #include <sys/types.h> 2147c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 2157c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 2167c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 2177c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 2187c478bd9Sstevel@tonic-gate #include <sys/policy.h> 2197c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h> 2207c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h> 2217c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h> 2227c478bd9Sstevel@tonic-gate #include <sys/class.h> 2237c478bd9Sstevel@tonic-gate #include <sys/pool.h> 2247c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h> 2257c478bd9Sstevel@tonic-gate #include <sys/pset.h> 2267c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 2277c478bd9Sstevel@tonic-gate #include <sys/callb.h> 2287c478bd9Sstevel@tonic-gate #include <sys/vmparam.h> 2297c478bd9Sstevel@tonic-gate #include <sys/corectl.h> 230824c205fSml93401 #include <sys/ipc_impl.h> 231134a1f4eSCasper H.S. Dik #include <sys/klpd.h> 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate #include <sys/door.h> 2347c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 235bd41d0a8Snordmark #include <sys/sdt.h> 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 2387c478bd9Sstevel@tonic-gate #include <sys/session.h> 2397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 2407c478bd9Sstevel@tonic-gate #include <sys/modhash.h> 2413f2f09c1Sdp #include <sys/sunddi.h> 2427c478bd9Sstevel@tonic-gate #include <sys/nvpair.h> 2437c478bd9Sstevel@tonic-gate #include <sys/rctl.h> 2447c478bd9Sstevel@tonic-gate #include <sys/fss.h> 2459acbbeafSnn35248 #include <sys/brand.h> 2467c478bd9Sstevel@tonic-gate #include <sys/zone.h> 247f4b3ec61Sdh155122 #include <net/if.h> 248c97ad5cdSakolb #include <sys/cpucaps.h> 2490209230bSgjelinek #include <vm/seg.h> 2502b24ab6bSSebastien Roy #include <sys/mac.h> 2512b24ab6bSSebastien Roy 2522b24ab6bSSebastien Roy /* List of data link IDs which are accessible from the zone */ 2532b24ab6bSSebastien Roy typedef struct zone_dl { 2542b24ab6bSSebastien Roy datalink_id_t zdl_id; 255*550b6e40SSowmini Varadhan nvlist_t *zdl_net; 2562b24ab6bSSebastien Roy list_node_t zdl_linkage; 2572b24ab6bSSebastien Roy } zone_dl_t; 2580209230bSgjelinek 2597c478bd9Sstevel@tonic-gate /* 2607c478bd9Sstevel@tonic-gate * cv used to signal that all references to the zone have been released. This 2617c478bd9Sstevel@tonic-gate * needs to be global since there may be multiple waiters, and the first to 2627c478bd9Sstevel@tonic-gate * wake up will free the zone_t, hence we cannot use zone->zone_cv. 2637c478bd9Sstevel@tonic-gate */ 2647c478bd9Sstevel@tonic-gate static kcondvar_t zone_destroy_cv; 2657c478bd9Sstevel@tonic-gate /* 2667c478bd9Sstevel@tonic-gate * Lock used to serialize access to zone_cv. This could have been per-zone, 2677c478bd9Sstevel@tonic-gate * but then we'd need another lock for zone_destroy_cv, and why bother? 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate static kmutex_t zone_status_lock; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate /* 2727c478bd9Sstevel@tonic-gate * ZSD-related global variables. 2737c478bd9Sstevel@tonic-gate */ 2747c478bd9Sstevel@tonic-gate static kmutex_t zsd_key_lock; /* protects the following two */ 2757c478bd9Sstevel@tonic-gate /* 2767c478bd9Sstevel@tonic-gate * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 2777c478bd9Sstevel@tonic-gate */ 2787c478bd9Sstevel@tonic-gate static zone_key_t zsd_keyval = 0; 2797c478bd9Sstevel@tonic-gate /* 2807c478bd9Sstevel@tonic-gate * Global list of registered keys. We use this when a new zone is created. 2817c478bd9Sstevel@tonic-gate */ 2827c478bd9Sstevel@tonic-gate static list_t zsd_registered_keys; 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate int zone_hash_size = 256; 28545916cd2Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel; 2867c478bd9Sstevel@tonic-gate static kmutex_t zonehash_lock; 2877c478bd9Sstevel@tonic-gate static uint_t zonecount; 2887c478bd9Sstevel@tonic-gate static id_space_t *zoneid_space; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate /* 2917c478bd9Sstevel@tonic-gate * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 2927c478bd9Sstevel@tonic-gate * kernel proper runs, and which manages all other zones. 2937c478bd9Sstevel@tonic-gate * 2947c478bd9Sstevel@tonic-gate * Although not declared as static, the variable "zone0" should not be used 2957c478bd9Sstevel@tonic-gate * except for by code that needs to reference the global zone early on in boot, 2967c478bd9Sstevel@tonic-gate * before it is fully initialized. All other consumers should use 2977c478bd9Sstevel@tonic-gate * 'global_zone'. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate zone_t zone0; 3007c478bd9Sstevel@tonic-gate zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate /* 3037c478bd9Sstevel@tonic-gate * List of active zones, protected by zonehash_lock. 3047c478bd9Sstevel@tonic-gate */ 3057c478bd9Sstevel@tonic-gate static list_t zone_active; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate /* 3087c478bd9Sstevel@tonic-gate * List of destroyed zones that still have outstanding cred references. 3097c478bd9Sstevel@tonic-gate * Used for debugging. Uses a separate lock to avoid lock ordering 3107c478bd9Sstevel@tonic-gate * problems in zone_free. 3117c478bd9Sstevel@tonic-gate */ 3127c478bd9Sstevel@tonic-gate static list_t zone_deathrow; 3137c478bd9Sstevel@tonic-gate static kmutex_t zone_deathrow_lock; 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */ 3167c478bd9Sstevel@tonic-gate uint_t maxzones = 8192; 3177c478bd9Sstevel@tonic-gate 318cf8f45c7Sdstaff /* Event channel to sent zone state change notifications */ 319cf8f45c7Sdstaff evchan_t *zone_event_chan; 320cf8f45c7Sdstaff 321cf8f45c7Sdstaff /* 322cf8f45c7Sdstaff * This table holds the mapping from kernel zone states to 323cf8f45c7Sdstaff * states visible in the state notification API. 324cf8f45c7Sdstaff * The idea is that we only expose "obvious" states and 325cf8f45c7Sdstaff * do not expose states which are just implementation details. 326cf8f45c7Sdstaff */ 327cf8f45c7Sdstaff const char *zone_status_table[] = { 328cf8f45c7Sdstaff ZONE_EVENT_UNINITIALIZED, /* uninitialized */ 329bd41d0a8Snordmark ZONE_EVENT_INITIALIZED, /* initialized */ 330cf8f45c7Sdstaff ZONE_EVENT_READY, /* ready */ 331cf8f45c7Sdstaff ZONE_EVENT_READY, /* booting */ 332cf8f45c7Sdstaff ZONE_EVENT_RUNNING, /* running */ 333cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* shutting_down */ 334cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* empty */ 335cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* down */ 336cf8f45c7Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* dying */ 337cf8f45c7Sdstaff ZONE_EVENT_UNINITIALIZED, /* dead */ 338cf8f45c7Sdstaff }; 339cf8f45c7Sdstaff 3407c478bd9Sstevel@tonic-gate /* 3417c478bd9Sstevel@tonic-gate * This isn't static so lint doesn't complain. 3427c478bd9Sstevel@tonic-gate */ 3437c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares; 344c6939658Ssl108498 rctl_hndl_t rc_zone_locked_mem; 3450209230bSgjelinek rctl_hndl_t rc_zone_max_swap; 3460fbb751dSJohn Levon rctl_hndl_t rc_zone_max_lofi; 347c97ad5cdSakolb rctl_hndl_t rc_zone_cpu_cap; 3487c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps; 349ff19e029SMenno Lageman rctl_hndl_t rc_zone_nprocs; 350824c205fSml93401 rctl_hndl_t rc_zone_shmmax; 351824c205fSml93401 rctl_hndl_t rc_zone_shmmni; 352824c205fSml93401 rctl_hndl_t rc_zone_semmni; 353824c205fSml93401 rctl_hndl_t rc_zone_msgmni; 3547c478bd9Sstevel@tonic-gate /* 3557c478bd9Sstevel@tonic-gate * Synchronization primitives used to synchronize between mounts and zone 3567c478bd9Sstevel@tonic-gate * creation/destruction. 3577c478bd9Sstevel@tonic-gate */ 3587c478bd9Sstevel@tonic-gate static int mounts_in_progress; 3597c478bd9Sstevel@tonic-gate static kcondvar_t mount_cv; 3607c478bd9Sstevel@tonic-gate static kmutex_t mount_lock; 3617c478bd9Sstevel@tonic-gate 3623f2f09c1Sdp const char * const zone_default_initname = "/sbin/init"; 36345916cd2Sjpk static char * const zone_prefix = "/zone/"; 3647c478bd9Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid); 3652b24ab6bSSebastien Roy static int zone_add_datalink(zoneid_t, datalink_id_t); 3662b24ab6bSSebastien Roy static int zone_remove_datalink(zoneid_t, datalink_id_t); 3672b24ab6bSSebastien Roy static int zone_list_datalink(zoneid_t, int *, datalink_id_t *); 368*550b6e40SSowmini Varadhan static int zone_set_network(zoneid_t, zone_net_data_t *); 369*550b6e40SSowmini Varadhan static int zone_get_network(zoneid_t, zone_net_data_t *); 3707c478bd9Sstevel@tonic-gate 371bd41d0a8Snordmark typedef boolean_t zsd_applyfn_t(kmutex_t *, boolean_t, zone_t *, zone_key_t); 372bd41d0a8Snordmark 373bd41d0a8Snordmark static void zsd_apply_all_zones(zsd_applyfn_t *, zone_key_t); 374bd41d0a8Snordmark static void zsd_apply_all_keys(zsd_applyfn_t *, zone_t *); 375bd41d0a8Snordmark static boolean_t zsd_apply_create(kmutex_t *, boolean_t, zone_t *, zone_key_t); 376bd41d0a8Snordmark static boolean_t zsd_apply_shutdown(kmutex_t *, boolean_t, zone_t *, 377bd41d0a8Snordmark zone_key_t); 378bd41d0a8Snordmark static boolean_t zsd_apply_destroy(kmutex_t *, boolean_t, zone_t *, zone_key_t); 379bd41d0a8Snordmark static boolean_t zsd_wait_for_creator(zone_t *, struct zsd_entry *, 380bd41d0a8Snordmark kmutex_t *); 381bd41d0a8Snordmark static boolean_t zsd_wait_for_inprogress(zone_t *, struct zsd_entry *, 382bd41d0a8Snordmark kmutex_t *); 383bd41d0a8Snordmark 3847c478bd9Sstevel@tonic-gate /* 385821c4a97Sdp * Bump this number when you alter the zone syscall interfaces; this is 386821c4a97Sdp * because we need to have support for previous API versions in libc 387821c4a97Sdp * to support patching; libc calls into the kernel to determine this number. 388821c4a97Sdp * 389821c4a97Sdp * Version 1 of the API is the version originally shipped with Solaris 10 390821c4a97Sdp * Version 2 alters the zone_create system call in order to support more 391821c4a97Sdp * arguments by moving the args into a structure; and to do better 392821c4a97Sdp * error reporting when zone_create() fails. 393821c4a97Sdp * Version 3 alters the zone_create system call in order to support the 394821c4a97Sdp * import of ZFS datasets to zones. 39545916cd2Sjpk * Version 4 alters the zone_create system call in order to support 39645916cd2Sjpk * Trusted Extensions. 3973f2f09c1Sdp * Version 5 alters the zone_boot system call, and converts its old 3983f2f09c1Sdp * bootargs parameter to be set by the zone_setattr API instead. 399f4b3ec61Sdh155122 * Version 6 adds the flag argument to zone_create. 400821c4a97Sdp */ 401f4b3ec61Sdh155122 static const int ZONE_SYSCALL_API_VERSION = 6; 402821c4a97Sdp 403821c4a97Sdp /* 4047c478bd9Sstevel@tonic-gate * Certain filesystems (such as NFS and autofs) need to know which zone 4057c478bd9Sstevel@tonic-gate * the mount is being placed in. Because of this, we need to be able to 4067c478bd9Sstevel@tonic-gate * ensure that a zone isn't in the process of being created such that 4077c478bd9Sstevel@tonic-gate * nfs_mount() thinks it is in the global zone, while by the time it 4087c478bd9Sstevel@tonic-gate * gets added the list of mounted zones, it ends up on zoneA's mount 4097c478bd9Sstevel@tonic-gate * list. 4107c478bd9Sstevel@tonic-gate * 4117c478bd9Sstevel@tonic-gate * The following functions: block_mounts()/resume_mounts() and 4127c478bd9Sstevel@tonic-gate * mount_in_progress()/mount_completed() are used by zones and the VFS 4137c478bd9Sstevel@tonic-gate * layer (respectively) to synchronize zone creation and new mounts. 4147c478bd9Sstevel@tonic-gate * 4157c478bd9Sstevel@tonic-gate * The semantics are like a reader-reader lock such that there may 4167c478bd9Sstevel@tonic-gate * either be multiple mounts (or zone creations, if that weren't 4177c478bd9Sstevel@tonic-gate * serialized by zonehash_lock) in progress at the same time, but not 4187c478bd9Sstevel@tonic-gate * both. 4197c478bd9Sstevel@tonic-gate * 4207c478bd9Sstevel@tonic-gate * We use cv's so the user can ctrl-C out of the operation if it's 4217c478bd9Sstevel@tonic-gate * taking too long. 4227c478bd9Sstevel@tonic-gate * 4237c478bd9Sstevel@tonic-gate * The semantics are such that there is unfair bias towards the 4247c478bd9Sstevel@tonic-gate * "current" operation. This means that zone creations may starve if 4257c478bd9Sstevel@tonic-gate * there is a rapid succession of new mounts coming in to the system, or 4267c478bd9Sstevel@tonic-gate * there is a remote possibility that zones will be created at such a 4277c478bd9Sstevel@tonic-gate * rate that new mounts will not be able to proceed. 4287c478bd9Sstevel@tonic-gate */ 4297c478bd9Sstevel@tonic-gate /* 4307c478bd9Sstevel@tonic-gate * Prevent new mounts from progressing to the point of calling 4317c478bd9Sstevel@tonic-gate * VFS_MOUNT(). If there are already mounts in this "region", wait for 4327c478bd9Sstevel@tonic-gate * them to complete. 4337c478bd9Sstevel@tonic-gate */ 4347c478bd9Sstevel@tonic-gate static int 4357c478bd9Sstevel@tonic-gate block_mounts(void) 4367c478bd9Sstevel@tonic-gate { 4377c478bd9Sstevel@tonic-gate int retval = 0; 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate /* 4407c478bd9Sstevel@tonic-gate * Since it may block for a long time, block_mounts() shouldn't be 4417c478bd9Sstevel@tonic-gate * called with zonehash_lock held. 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 4447c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4457c478bd9Sstevel@tonic-gate while (mounts_in_progress > 0) { 4467c478bd9Sstevel@tonic-gate if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 4477c478bd9Sstevel@tonic-gate goto signaled; 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * A negative value of mounts_in_progress indicates that mounts 4517c478bd9Sstevel@tonic-gate * have been blocked by (-mounts_in_progress) different callers. 4527c478bd9Sstevel@tonic-gate */ 4537c478bd9Sstevel@tonic-gate mounts_in_progress--; 4547c478bd9Sstevel@tonic-gate retval = 1; 4557c478bd9Sstevel@tonic-gate signaled: 4567c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4577c478bd9Sstevel@tonic-gate return (retval); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate /* 4617c478bd9Sstevel@tonic-gate * The VFS layer may progress with new mounts as far as we're concerned. 4627c478bd9Sstevel@tonic-gate * Allow them to progress if we were the last obstacle. 4637c478bd9Sstevel@tonic-gate */ 4647c478bd9Sstevel@tonic-gate static void 4657c478bd9Sstevel@tonic-gate resume_mounts(void) 4667c478bd9Sstevel@tonic-gate { 4677c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4687c478bd9Sstevel@tonic-gate if (++mounts_in_progress == 0) 4697c478bd9Sstevel@tonic-gate cv_broadcast(&mount_cv); 4707c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate /* 4747c478bd9Sstevel@tonic-gate * The VFS layer is busy with a mount; zones should wait until all 4757c478bd9Sstevel@tonic-gate * mounts are completed to progress. 4767c478bd9Sstevel@tonic-gate */ 4777c478bd9Sstevel@tonic-gate void 4787c478bd9Sstevel@tonic-gate mount_in_progress(void) 4797c478bd9Sstevel@tonic-gate { 4807c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4817c478bd9Sstevel@tonic-gate while (mounts_in_progress < 0) 4827c478bd9Sstevel@tonic-gate cv_wait(&mount_cv, &mount_lock); 4837c478bd9Sstevel@tonic-gate mounts_in_progress++; 4847c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate /* 4887c478bd9Sstevel@tonic-gate * VFS is done with one mount; wake up any waiting block_mounts() 4897c478bd9Sstevel@tonic-gate * callers if this is the last mount. 4907c478bd9Sstevel@tonic-gate */ 4917c478bd9Sstevel@tonic-gate void 4927c478bd9Sstevel@tonic-gate mount_completed(void) 4937c478bd9Sstevel@tonic-gate { 4947c478bd9Sstevel@tonic-gate mutex_enter(&mount_lock); 4957c478bd9Sstevel@tonic-gate if (--mounts_in_progress == 0) 4967c478bd9Sstevel@tonic-gate cv_broadcast(&mount_cv); 4977c478bd9Sstevel@tonic-gate mutex_exit(&mount_lock); 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate /* 5017c478bd9Sstevel@tonic-gate * ZSD routines. 5027c478bd9Sstevel@tonic-gate * 5037c478bd9Sstevel@tonic-gate * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 5047c478bd9Sstevel@tonic-gate * defined by the pthread_key_create() and related interfaces. 5057c478bd9Sstevel@tonic-gate * 5067c478bd9Sstevel@tonic-gate * Kernel subsystems may register one or more data items and/or 5077c478bd9Sstevel@tonic-gate * callbacks to be executed when a zone is created, shutdown, or 5087c478bd9Sstevel@tonic-gate * destroyed. 5097c478bd9Sstevel@tonic-gate * 5107c478bd9Sstevel@tonic-gate * Unlike the thread counterpart, destructor callbacks will be executed 5117c478bd9Sstevel@tonic-gate * even if the data pointer is NULL and/or there are no constructor 5127c478bd9Sstevel@tonic-gate * callbacks, so it is the responsibility of such callbacks to check for 5137c478bd9Sstevel@tonic-gate * NULL data values if necessary. 5147c478bd9Sstevel@tonic-gate * 5157c478bd9Sstevel@tonic-gate * The locking strategy and overall picture is as follows: 5167c478bd9Sstevel@tonic-gate * 5177c478bd9Sstevel@tonic-gate * When someone calls zone_key_create(), a template ZSD entry is added to the 518bd41d0a8Snordmark * global list "zsd_registered_keys", protected by zsd_key_lock. While 519bd41d0a8Snordmark * holding that lock all the existing zones are marked as 520bd41d0a8Snordmark * ZSD_CREATE_NEEDED and a copy of the ZSD entry added to the per-zone 521bd41d0a8Snordmark * zone_zsd list (protected by zone_lock). The global list is updated first 522bd41d0a8Snordmark * (under zone_key_lock) to make sure that newly created zones use the 523bd41d0a8Snordmark * most recent list of keys. Then under zonehash_lock we walk the zones 524bd41d0a8Snordmark * and mark them. Similar locking is used in zone_key_delete(). 5257c478bd9Sstevel@tonic-gate * 526bd41d0a8Snordmark * The actual create, shutdown, and destroy callbacks are done without 527bd41d0a8Snordmark * holding any lock. And zsd_flags are used to ensure that the operations 528bd41d0a8Snordmark * completed so that when zone_key_create (and zone_create) is done, as well as 529bd41d0a8Snordmark * zone_key_delete (and zone_destroy) is done, all the necessary callbacks 530bd41d0a8Snordmark * are completed. 5317c478bd9Sstevel@tonic-gate * 5327c478bd9Sstevel@tonic-gate * When new zones are created constructor callbacks for all registered ZSD 533bd41d0a8Snordmark * entries will be called. That also uses the above two phases of marking 534bd41d0a8Snordmark * what needs to be done, and then running the callbacks without holding 535bd41d0a8Snordmark * any locks. 5367c478bd9Sstevel@tonic-gate * 5377c478bd9Sstevel@tonic-gate * The framework does not provide any locking around zone_getspecific() and 5387c478bd9Sstevel@tonic-gate * zone_setspecific() apart from that needed for internal consistency, so 5397c478bd9Sstevel@tonic-gate * callers interested in atomic "test-and-set" semantics will need to provide 5407c478bd9Sstevel@tonic-gate * their own locking. 5417c478bd9Sstevel@tonic-gate */ 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate /* 5447c478bd9Sstevel@tonic-gate * Helper function to find the zsd_entry associated with the key in the 5457c478bd9Sstevel@tonic-gate * given list. 5467c478bd9Sstevel@tonic-gate */ 5477c478bd9Sstevel@tonic-gate static struct zsd_entry * 5487c478bd9Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key) 5497c478bd9Sstevel@tonic-gate { 5507c478bd9Sstevel@tonic-gate struct zsd_entry *zsd; 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 5537c478bd9Sstevel@tonic-gate if (zsd->zsd_key == key) { 554bd41d0a8Snordmark return (zsd); 555bd41d0a8Snordmark } 556bd41d0a8Snordmark } 557bd41d0a8Snordmark return (NULL); 558bd41d0a8Snordmark } 559bd41d0a8Snordmark 560bd41d0a8Snordmark /* 561bd41d0a8Snordmark * Helper function to find the zsd_entry associated with the key in the 562bd41d0a8Snordmark * given list. Move it to the front of the list. 563bd41d0a8Snordmark */ 564bd41d0a8Snordmark static struct zsd_entry * 565bd41d0a8Snordmark zsd_find_mru(list_t *l, zone_key_t key) 566bd41d0a8Snordmark { 567bd41d0a8Snordmark struct zsd_entry *zsd; 568bd41d0a8Snordmark 569bd41d0a8Snordmark for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 570bd41d0a8Snordmark if (zsd->zsd_key == key) { 5717c478bd9Sstevel@tonic-gate /* 5727c478bd9Sstevel@tonic-gate * Move to head of list to keep list in MRU order. 5737c478bd9Sstevel@tonic-gate */ 5747c478bd9Sstevel@tonic-gate if (zsd != list_head(l)) { 5757c478bd9Sstevel@tonic-gate list_remove(l, zsd); 5767c478bd9Sstevel@tonic-gate list_insert_head(l, zsd); 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate return (zsd); 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate return (NULL); 5827c478bd9Sstevel@tonic-gate } 5837c478bd9Sstevel@tonic-gate 584bd41d0a8Snordmark void 585bd41d0a8Snordmark zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 586bd41d0a8Snordmark void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 587bd41d0a8Snordmark { 588bd41d0a8Snordmark struct zsd_entry *zsdp; 589bd41d0a8Snordmark struct zsd_entry *t; 590bd41d0a8Snordmark struct zone *zone; 591bd41d0a8Snordmark zone_key_t key; 592bd41d0a8Snordmark 593bd41d0a8Snordmark zsdp = kmem_zalloc(sizeof (*zsdp), KM_SLEEP); 594bd41d0a8Snordmark zsdp->zsd_data = NULL; 595bd41d0a8Snordmark zsdp->zsd_create = create; 596bd41d0a8Snordmark zsdp->zsd_shutdown = shutdown; 597bd41d0a8Snordmark zsdp->zsd_destroy = destroy; 598bd41d0a8Snordmark 599bd41d0a8Snordmark /* 600bd41d0a8Snordmark * Insert in global list of callbacks. Makes future zone creations 601bd41d0a8Snordmark * see it. 602bd41d0a8Snordmark */ 603bd41d0a8Snordmark mutex_enter(&zsd_key_lock); 604fe16170aSPramod Batni key = zsdp->zsd_key = ++zsd_keyval; 605bd41d0a8Snordmark ASSERT(zsd_keyval != 0); 606bd41d0a8Snordmark list_insert_tail(&zsd_registered_keys, zsdp); 607bd41d0a8Snordmark mutex_exit(&zsd_key_lock); 608bd41d0a8Snordmark 609bd41d0a8Snordmark /* 610bd41d0a8Snordmark * Insert for all existing zones and mark them as needing 611bd41d0a8Snordmark * a create callback. 612bd41d0a8Snordmark */ 613bd41d0a8Snordmark mutex_enter(&zonehash_lock); /* stop the world */ 614bd41d0a8Snordmark for (zone = list_head(&zone_active); zone != NULL; 615bd41d0a8Snordmark zone = list_next(&zone_active, zone)) { 616bd41d0a8Snordmark zone_status_t status; 617bd41d0a8Snordmark 618bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 619bd41d0a8Snordmark 620bd41d0a8Snordmark /* Skip zones that are on the way down or not yet up */ 621bd41d0a8Snordmark status = zone_status_get(zone); 622bd41d0a8Snordmark if (status >= ZONE_IS_DOWN || 623bd41d0a8Snordmark status == ZONE_IS_UNINITIALIZED) { 624bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 625bd41d0a8Snordmark continue; 626bd41d0a8Snordmark } 627bd41d0a8Snordmark 628bd41d0a8Snordmark t = zsd_find_mru(&zone->zone_zsd, key); 629bd41d0a8Snordmark if (t != NULL) { 630bd41d0a8Snordmark /* 631bd41d0a8Snordmark * A zsd_configure already inserted it after 632bd41d0a8Snordmark * we dropped zsd_key_lock above. 633bd41d0a8Snordmark */ 634bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 635bd41d0a8Snordmark continue; 636bd41d0a8Snordmark } 637bd41d0a8Snordmark t = kmem_zalloc(sizeof (*t), KM_SLEEP); 638bd41d0a8Snordmark t->zsd_key = key; 639bd41d0a8Snordmark t->zsd_create = create; 640bd41d0a8Snordmark t->zsd_shutdown = shutdown; 641bd41d0a8Snordmark t->zsd_destroy = destroy; 642bd41d0a8Snordmark if (create != NULL) { 643bd41d0a8Snordmark t->zsd_flags = ZSD_CREATE_NEEDED; 644bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__needed, 645bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 646bd41d0a8Snordmark } 647bd41d0a8Snordmark list_insert_tail(&zone->zone_zsd, t); 648bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 649bd41d0a8Snordmark } 650bd41d0a8Snordmark mutex_exit(&zonehash_lock); 651bd41d0a8Snordmark 652bd41d0a8Snordmark if (create != NULL) { 653bd41d0a8Snordmark /* Now call the create callback for this key */ 654bd41d0a8Snordmark zsd_apply_all_zones(zsd_apply_create, key); 655bd41d0a8Snordmark } 656fe16170aSPramod Batni /* 657fe16170aSPramod Batni * It is safe for consumers to use the key now, make it 658fe16170aSPramod Batni * globally visible. Specifically zone_getspecific() will 659fe16170aSPramod Batni * always successfully return the zone specific data associated 660fe16170aSPramod Batni * with the key. 661fe16170aSPramod Batni */ 662fe16170aSPramod Batni *keyp = key; 663fe16170aSPramod Batni 664bd41d0a8Snordmark } 665bd41d0a8Snordmark 6667c478bd9Sstevel@tonic-gate /* 6677c478bd9Sstevel@tonic-gate * Function called when a module is being unloaded, or otherwise wishes 6687c478bd9Sstevel@tonic-gate * to unregister its ZSD key and callbacks. 669bd41d0a8Snordmark * 670bd41d0a8Snordmark * Remove from the global list and determine the functions that need to 671bd41d0a8Snordmark * be called under a global lock. Then call the functions without 672bd41d0a8Snordmark * holding any locks. Finally free up the zone_zsd entries. (The apply 673bd41d0a8Snordmark * functions need to access the zone_zsd entries to find zsd_data etc.) 6747c478bd9Sstevel@tonic-gate */ 6757c478bd9Sstevel@tonic-gate int 6767c478bd9Sstevel@tonic-gate zone_key_delete(zone_key_t key) 6777c478bd9Sstevel@tonic-gate { 6787c478bd9Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 6797c478bd9Sstevel@tonic-gate zone_t *zone; 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 682bd41d0a8Snordmark zsdp = zsd_find_mru(&zsd_registered_keys, key); 683bd41d0a8Snordmark if (zsdp == NULL) { 684bd41d0a8Snordmark mutex_exit(&zsd_key_lock); 685bd41d0a8Snordmark return (-1); 686bd41d0a8Snordmark } 6877c478bd9Sstevel@tonic-gate list_remove(&zsd_registered_keys, zsdp); 6887c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6897c478bd9Sstevel@tonic-gate 690bd41d0a8Snordmark mutex_enter(&zonehash_lock); 6917c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 6927c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 6937c478bd9Sstevel@tonic-gate struct zsd_entry *del; 6947c478bd9Sstevel@tonic-gate 695bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 696bd41d0a8Snordmark del = zsd_find_mru(&zone->zone_zsd, key); 697bd41d0a8Snordmark if (del == NULL) { 698bd41d0a8Snordmark /* 699bd41d0a8Snordmark * Somebody else got here first e.g the zone going 700bd41d0a8Snordmark * away. 701bd41d0a8Snordmark */ 702bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 703bd41d0a8Snordmark continue; 704bd41d0a8Snordmark } 7057c478bd9Sstevel@tonic-gate ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 7067c478bd9Sstevel@tonic-gate ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 707bd41d0a8Snordmark if (del->zsd_shutdown != NULL && 708bd41d0a8Snordmark (del->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) { 709bd41d0a8Snordmark del->zsd_flags |= ZSD_SHUTDOWN_NEEDED; 710bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__needed, 711bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 7127c478bd9Sstevel@tonic-gate } 713bd41d0a8Snordmark if (del->zsd_destroy != NULL && 714bd41d0a8Snordmark (del->zsd_flags & ZSD_DESTROY_ALL) == 0) { 715bd41d0a8Snordmark del->zsd_flags |= ZSD_DESTROY_NEEDED; 716bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__needed, 717bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 7187c478bd9Sstevel@tonic-gate } 7197c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 7227c478bd9Sstevel@tonic-gate kmem_free(zsdp, sizeof (*zsdp)); 7237c478bd9Sstevel@tonic-gate 724bd41d0a8Snordmark /* Now call the shutdown and destroy callback for this key */ 725bd41d0a8Snordmark zsd_apply_all_zones(zsd_apply_shutdown, key); 726bd41d0a8Snordmark zsd_apply_all_zones(zsd_apply_destroy, key); 727bd41d0a8Snordmark 728bd41d0a8Snordmark /* Now we can free up the zsdp structures in each zone */ 729bd41d0a8Snordmark mutex_enter(&zonehash_lock); 7307c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 731bd41d0a8Snordmark zone = list_next(&zone_active, zone)) { 732bd41d0a8Snordmark struct zsd_entry *del; 733bd41d0a8Snordmark 734bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 735bd41d0a8Snordmark del = zsd_find(&zone->zone_zsd, key); 736bd41d0a8Snordmark if (del != NULL) { 737bd41d0a8Snordmark list_remove(&zone->zone_zsd, del); 738bd41d0a8Snordmark ASSERT(!(del->zsd_flags & ZSD_ALL_INPROGRESS)); 739bd41d0a8Snordmark kmem_free(del, sizeof (*del)); 740bd41d0a8Snordmark } 7417c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 742bd41d0a8Snordmark } 7437c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 744bd41d0a8Snordmark 745bd41d0a8Snordmark return (0); 7467c478bd9Sstevel@tonic-gate } 7477c478bd9Sstevel@tonic-gate 7487c478bd9Sstevel@tonic-gate /* 7497c478bd9Sstevel@tonic-gate * ZSD counterpart of pthread_setspecific(). 750bd41d0a8Snordmark * 751bd41d0a8Snordmark * Since all zsd callbacks, including those with no create function, 752bd41d0a8Snordmark * have an entry in zone_zsd, if the key is registered it is part of 753bd41d0a8Snordmark * the zone_zsd list. 754bd41d0a8Snordmark * Return an error if the key wasn't registerd. 7557c478bd9Sstevel@tonic-gate */ 7567c478bd9Sstevel@tonic-gate int 7577c478bd9Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 7587c478bd9Sstevel@tonic-gate { 7597c478bd9Sstevel@tonic-gate struct zsd_entry *t; 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 762bd41d0a8Snordmark t = zsd_find_mru(&zone->zone_zsd, key); 7637c478bd9Sstevel@tonic-gate if (t != NULL) { 7647c478bd9Sstevel@tonic-gate /* 7657c478bd9Sstevel@tonic-gate * Replace old value with new 7667c478bd9Sstevel@tonic-gate */ 7677c478bd9Sstevel@tonic-gate t->zsd_data = (void *)data; 7687c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7697c478bd9Sstevel@tonic-gate return (0); 7707c478bd9Sstevel@tonic-gate } 7717c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7727c478bd9Sstevel@tonic-gate return (-1); 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate /* 7767c478bd9Sstevel@tonic-gate * ZSD counterpart of pthread_getspecific(). 7777c478bd9Sstevel@tonic-gate */ 7787c478bd9Sstevel@tonic-gate void * 7797c478bd9Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone) 7807c478bd9Sstevel@tonic-gate { 7817c478bd9Sstevel@tonic-gate struct zsd_entry *t; 7827c478bd9Sstevel@tonic-gate void *data; 7837c478bd9Sstevel@tonic-gate 7847c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 785bd41d0a8Snordmark t = zsd_find_mru(&zone->zone_zsd, key); 7867c478bd9Sstevel@tonic-gate data = (t == NULL ? NULL : t->zsd_data); 7877c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7887c478bd9Sstevel@tonic-gate return (data); 7897c478bd9Sstevel@tonic-gate } 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate /* 7927c478bd9Sstevel@tonic-gate * Function used to initialize a zone's list of ZSD callbacks and data 7937c478bd9Sstevel@tonic-gate * when the zone is being created. The callbacks are initialized from 794bd41d0a8Snordmark * the template list (zsd_registered_keys). The constructor callback is 795bd41d0a8Snordmark * executed later (once the zone exists and with locks dropped). 7967c478bd9Sstevel@tonic-gate */ 7977c478bd9Sstevel@tonic-gate static void 7987c478bd9Sstevel@tonic-gate zone_zsd_configure(zone_t *zone) 7997c478bd9Sstevel@tonic-gate { 8007c478bd9Sstevel@tonic-gate struct zsd_entry *zsdp; 8017c478bd9Sstevel@tonic-gate struct zsd_entry *t; 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 8047c478bd9Sstevel@tonic-gate ASSERT(list_head(&zone->zone_zsd) == NULL); 805bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 8067c478bd9Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 8077c478bd9Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 8087c478bd9Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 809bd41d0a8Snordmark /* 810bd41d0a8Snordmark * Since this zone is ZONE_IS_UNCONFIGURED, zone_key_create 811bd41d0a8Snordmark * should not have added anything to it. 812bd41d0a8Snordmark */ 813bd41d0a8Snordmark ASSERT(zsd_find(&zone->zone_zsd, zsdp->zsd_key) == NULL); 814bd41d0a8Snordmark 815bd41d0a8Snordmark t = kmem_zalloc(sizeof (*t), KM_SLEEP); 8167c478bd9Sstevel@tonic-gate t->zsd_key = zsdp->zsd_key; 8177c478bd9Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 8187c478bd9Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 8197c478bd9Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 820bd41d0a8Snordmark if (zsdp->zsd_create != NULL) { 821bd41d0a8Snordmark t->zsd_flags = ZSD_CREATE_NEEDED; 822bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__needed, 823bd41d0a8Snordmark zone_t *, zone, zone_key_t, zsdp->zsd_key); 824bd41d0a8Snordmark } 8257c478bd9Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 828bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 8297c478bd9Sstevel@tonic-gate } 8307c478bd9Sstevel@tonic-gate 8317c478bd9Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate /* 8347c478bd9Sstevel@tonic-gate * Helper function to execute shutdown or destructor callbacks. 8357c478bd9Sstevel@tonic-gate */ 8367c478bd9Sstevel@tonic-gate static void 8377c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 8387c478bd9Sstevel@tonic-gate { 8397c478bd9Sstevel@tonic-gate struct zsd_entry *t; 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 8427c478bd9Sstevel@tonic-gate ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 8437c478bd9Sstevel@tonic-gate ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 8447c478bd9Sstevel@tonic-gate 845bd41d0a8Snordmark /* 846bd41d0a8Snordmark * Run the callback solely based on what is registered for the zone 847bd41d0a8Snordmark * in zone_zsd. The global list can change independently of this 848bd41d0a8Snordmark * as keys are registered and unregistered and we don't register new 849bd41d0a8Snordmark * callbacks for a zone that is in the process of going away. 850bd41d0a8Snordmark */ 8517c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 852bd41d0a8Snordmark for (t = list_head(&zone->zone_zsd); t != NULL; 853bd41d0a8Snordmark t = list_next(&zone->zone_zsd, t)) { 854bd41d0a8Snordmark zone_key_t key = t->zsd_key; 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate /* Skip if no callbacks registered */ 857bd41d0a8Snordmark 8587c478bd9Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 859bd41d0a8Snordmark if (t->zsd_shutdown != NULL && 860bd41d0a8Snordmark (t->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) { 861bd41d0a8Snordmark t->zsd_flags |= ZSD_SHUTDOWN_NEEDED; 862bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__needed, 863bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 8647c478bd9Sstevel@tonic-gate } 8657c478bd9Sstevel@tonic-gate } else { 866bd41d0a8Snordmark if (t->zsd_destroy != NULL && 867bd41d0a8Snordmark (t->zsd_flags & ZSD_DESTROY_ALL) == 0) { 868bd41d0a8Snordmark t->zsd_flags |= ZSD_DESTROY_NEEDED; 869bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__needed, 870bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate } 874bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 875bd41d0a8Snordmark 876bd41d0a8Snordmark /* Now call the shutdown and destroy callback for this key */ 877bd41d0a8Snordmark zsd_apply_all_keys(zsd_apply_shutdown, zone); 878bd41d0a8Snordmark zsd_apply_all_keys(zsd_apply_destroy, zone); 879bd41d0a8Snordmark 8807c478bd9Sstevel@tonic-gate } 8817c478bd9Sstevel@tonic-gate 8827c478bd9Sstevel@tonic-gate /* 8837c478bd9Sstevel@tonic-gate * Called when the zone is going away; free ZSD-related memory, and 8847c478bd9Sstevel@tonic-gate * destroy the zone_zsd list. 8857c478bd9Sstevel@tonic-gate */ 8867c478bd9Sstevel@tonic-gate static void 8877c478bd9Sstevel@tonic-gate zone_free_zsd(zone_t *zone) 8887c478bd9Sstevel@tonic-gate { 8897c478bd9Sstevel@tonic-gate struct zsd_entry *t, *next; 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate /* 8927c478bd9Sstevel@tonic-gate * Free all the zsd_entry's we had on this zone. 8937c478bd9Sstevel@tonic-gate */ 894bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 8957c478bd9Sstevel@tonic-gate for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 8967c478bd9Sstevel@tonic-gate next = list_next(&zone->zone_zsd, t); 8977c478bd9Sstevel@tonic-gate list_remove(&zone->zone_zsd, t); 898bd41d0a8Snordmark ASSERT(!(t->zsd_flags & ZSD_ALL_INPROGRESS)); 8997c478bd9Sstevel@tonic-gate kmem_free(t, sizeof (*t)); 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate list_destroy(&zone->zone_zsd); 902bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 903bd41d0a8Snordmark 904bd41d0a8Snordmark } 905bd41d0a8Snordmark 906bd41d0a8Snordmark /* 907bd41d0a8Snordmark * Apply a function to all zones for particular key value. 908bd41d0a8Snordmark * 909bd41d0a8Snordmark * The applyfn has to drop zonehash_lock if it does some work, and 910bd41d0a8Snordmark * then reacquire it before it returns. 911bd41d0a8Snordmark * When the lock is dropped we don't follow list_next even 912bd41d0a8Snordmark * if it is possible to do so without any hazards. This is 913bd41d0a8Snordmark * because we want the design to allow for the list of zones 914bd41d0a8Snordmark * to change in any arbitrary way during the time the 915bd41d0a8Snordmark * lock was dropped. 916bd41d0a8Snordmark * 917bd41d0a8Snordmark * It is safe to restart the loop at list_head since the applyfn 918bd41d0a8Snordmark * changes the zsd_flags as it does work, so a subsequent 919bd41d0a8Snordmark * pass through will have no effect in applyfn, hence the loop will terminate 920bd41d0a8Snordmark * in at worst O(N^2). 921bd41d0a8Snordmark */ 922bd41d0a8Snordmark static void 923bd41d0a8Snordmark zsd_apply_all_zones(zsd_applyfn_t *applyfn, zone_key_t key) 924bd41d0a8Snordmark { 925bd41d0a8Snordmark zone_t *zone; 926bd41d0a8Snordmark 927bd41d0a8Snordmark mutex_enter(&zonehash_lock); 928bd41d0a8Snordmark zone = list_head(&zone_active); 929bd41d0a8Snordmark while (zone != NULL) { 930bd41d0a8Snordmark if ((applyfn)(&zonehash_lock, B_FALSE, zone, key)) { 931bd41d0a8Snordmark /* Lock dropped - restart at head */ 932bd41d0a8Snordmark zone = list_head(&zone_active); 933bd41d0a8Snordmark } else { 934bd41d0a8Snordmark zone = list_next(&zone_active, zone); 935bd41d0a8Snordmark } 936bd41d0a8Snordmark } 937bd41d0a8Snordmark mutex_exit(&zonehash_lock); 938bd41d0a8Snordmark } 939bd41d0a8Snordmark 940bd41d0a8Snordmark /* 941bd41d0a8Snordmark * Apply a function to all keys for a particular zone. 942bd41d0a8Snordmark * 943bd41d0a8Snordmark * The applyfn has to drop zonehash_lock if it does some work, and 944bd41d0a8Snordmark * then reacquire it before it returns. 945bd41d0a8Snordmark * When the lock is dropped we don't follow list_next even 946bd41d0a8Snordmark * if it is possible to do so without any hazards. This is 947bd41d0a8Snordmark * because we want the design to allow for the list of zsd callbacks 948bd41d0a8Snordmark * to change in any arbitrary way during the time the 949bd41d0a8Snordmark * lock was dropped. 950bd41d0a8Snordmark * 951bd41d0a8Snordmark * It is safe to restart the loop at list_head since the applyfn 952bd41d0a8Snordmark * changes the zsd_flags as it does work, so a subsequent 953bd41d0a8Snordmark * pass through will have no effect in applyfn, hence the loop will terminate 954bd41d0a8Snordmark * in at worst O(N^2). 955bd41d0a8Snordmark */ 956bd41d0a8Snordmark static void 957bd41d0a8Snordmark zsd_apply_all_keys(zsd_applyfn_t *applyfn, zone_t *zone) 958bd41d0a8Snordmark { 959bd41d0a8Snordmark struct zsd_entry *t; 960bd41d0a8Snordmark 961bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 962bd41d0a8Snordmark t = list_head(&zone->zone_zsd); 963bd41d0a8Snordmark while (t != NULL) { 964bd41d0a8Snordmark if ((applyfn)(NULL, B_TRUE, zone, t->zsd_key)) { 965bd41d0a8Snordmark /* Lock dropped - restart at head */ 966bd41d0a8Snordmark t = list_head(&zone->zone_zsd); 967bd41d0a8Snordmark } else { 968bd41d0a8Snordmark t = list_next(&zone->zone_zsd, t); 969bd41d0a8Snordmark } 970bd41d0a8Snordmark } 971bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 972bd41d0a8Snordmark } 973bd41d0a8Snordmark 974bd41d0a8Snordmark /* 975bd41d0a8Snordmark * Call the create function for the zone and key if CREATE_NEEDED 976bd41d0a8Snordmark * is set. 977bd41d0a8Snordmark * If some other thread gets here first and sets CREATE_INPROGRESS, then 978bd41d0a8Snordmark * we wait for that thread to complete so that we can ensure that 979bd41d0a8Snordmark * all the callbacks are done when we've looped over all zones/keys. 980bd41d0a8Snordmark * 981bd41d0a8Snordmark * When we call the create function, we drop the global held by the 982bd41d0a8Snordmark * caller, and return true to tell the caller it needs to re-evalute the 983bd41d0a8Snordmark * state. 984bd41d0a8Snordmark * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 985bd41d0a8Snordmark * remains held on exit. 986bd41d0a8Snordmark */ 987bd41d0a8Snordmark static boolean_t 988bd41d0a8Snordmark zsd_apply_create(kmutex_t *lockp, boolean_t zone_lock_held, 989bd41d0a8Snordmark zone_t *zone, zone_key_t key) 990bd41d0a8Snordmark { 991bd41d0a8Snordmark void *result; 992bd41d0a8Snordmark struct zsd_entry *t; 993bd41d0a8Snordmark boolean_t dropped; 994bd41d0a8Snordmark 995bd41d0a8Snordmark if (lockp != NULL) { 996bd41d0a8Snordmark ASSERT(MUTEX_HELD(lockp)); 997bd41d0a8Snordmark } 998bd41d0a8Snordmark if (zone_lock_held) { 999bd41d0a8Snordmark ASSERT(MUTEX_HELD(&zone->zone_lock)); 1000bd41d0a8Snordmark } else { 1001bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1002bd41d0a8Snordmark } 1003bd41d0a8Snordmark 1004bd41d0a8Snordmark t = zsd_find(&zone->zone_zsd, key); 1005bd41d0a8Snordmark if (t == NULL) { 1006bd41d0a8Snordmark /* 1007bd41d0a8Snordmark * Somebody else got here first e.g the zone going 1008bd41d0a8Snordmark * away. 1009bd41d0a8Snordmark */ 1010bd41d0a8Snordmark if (!zone_lock_held) 1011bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1012bd41d0a8Snordmark return (B_FALSE); 1013bd41d0a8Snordmark } 1014bd41d0a8Snordmark dropped = B_FALSE; 1015bd41d0a8Snordmark if (zsd_wait_for_inprogress(zone, t, lockp)) 1016bd41d0a8Snordmark dropped = B_TRUE; 1017bd41d0a8Snordmark 1018bd41d0a8Snordmark if (t->zsd_flags & ZSD_CREATE_NEEDED) { 1019bd41d0a8Snordmark t->zsd_flags &= ~ZSD_CREATE_NEEDED; 1020bd41d0a8Snordmark t->zsd_flags |= ZSD_CREATE_INPROGRESS; 1021bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__inprogress, 1022bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1023bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1024bd41d0a8Snordmark if (lockp != NULL) 1025bd41d0a8Snordmark mutex_exit(lockp); 1026bd41d0a8Snordmark 1027bd41d0a8Snordmark dropped = B_TRUE; 1028bd41d0a8Snordmark ASSERT(t->zsd_create != NULL); 1029bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__start, 1030bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1031bd41d0a8Snordmark 1032bd41d0a8Snordmark result = (*t->zsd_create)(zone->zone_id); 1033bd41d0a8Snordmark 1034bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__end, 1035bd41d0a8Snordmark zone_t *, zone, voidn *, result); 1036bd41d0a8Snordmark 1037bd41d0a8Snordmark ASSERT(result != NULL); 1038bd41d0a8Snordmark if (lockp != NULL) 1039bd41d0a8Snordmark mutex_enter(lockp); 1040bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1041bd41d0a8Snordmark t->zsd_data = result; 1042bd41d0a8Snordmark t->zsd_flags &= ~ZSD_CREATE_INPROGRESS; 1043bd41d0a8Snordmark t->zsd_flags |= ZSD_CREATE_COMPLETED; 1044bd41d0a8Snordmark cv_broadcast(&t->zsd_cv); 1045bd41d0a8Snordmark DTRACE_PROBE2(zsd__create__completed, 1046bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1047bd41d0a8Snordmark } 1048bd41d0a8Snordmark if (!zone_lock_held) 1049bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1050bd41d0a8Snordmark return (dropped); 1051bd41d0a8Snordmark } 1052bd41d0a8Snordmark 1053bd41d0a8Snordmark /* 1054bd41d0a8Snordmark * Call the shutdown function for the zone and key if SHUTDOWN_NEEDED 1055bd41d0a8Snordmark * is set. 1056bd41d0a8Snordmark * If some other thread gets here first and sets *_INPROGRESS, then 1057bd41d0a8Snordmark * we wait for that thread to complete so that we can ensure that 1058bd41d0a8Snordmark * all the callbacks are done when we've looped over all zones/keys. 1059bd41d0a8Snordmark * 1060bd41d0a8Snordmark * When we call the shutdown function, we drop the global held by the 1061bd41d0a8Snordmark * caller, and return true to tell the caller it needs to re-evalute the 1062bd41d0a8Snordmark * state. 1063bd41d0a8Snordmark * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 1064bd41d0a8Snordmark * remains held on exit. 1065bd41d0a8Snordmark */ 1066bd41d0a8Snordmark static boolean_t 1067bd41d0a8Snordmark zsd_apply_shutdown(kmutex_t *lockp, boolean_t zone_lock_held, 1068bd41d0a8Snordmark zone_t *zone, zone_key_t key) 1069bd41d0a8Snordmark { 1070bd41d0a8Snordmark struct zsd_entry *t; 1071bd41d0a8Snordmark void *data; 1072bd41d0a8Snordmark boolean_t dropped; 1073bd41d0a8Snordmark 1074bd41d0a8Snordmark if (lockp != NULL) { 1075bd41d0a8Snordmark ASSERT(MUTEX_HELD(lockp)); 1076bd41d0a8Snordmark } 1077bd41d0a8Snordmark if (zone_lock_held) { 1078bd41d0a8Snordmark ASSERT(MUTEX_HELD(&zone->zone_lock)); 1079bd41d0a8Snordmark } else { 1080bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1081bd41d0a8Snordmark } 1082bd41d0a8Snordmark 1083bd41d0a8Snordmark t = zsd_find(&zone->zone_zsd, key); 1084bd41d0a8Snordmark if (t == NULL) { 1085bd41d0a8Snordmark /* 1086bd41d0a8Snordmark * Somebody else got here first e.g the zone going 1087bd41d0a8Snordmark * away. 1088bd41d0a8Snordmark */ 1089bd41d0a8Snordmark if (!zone_lock_held) 1090bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1091bd41d0a8Snordmark return (B_FALSE); 1092bd41d0a8Snordmark } 1093bd41d0a8Snordmark dropped = B_FALSE; 1094bd41d0a8Snordmark if (zsd_wait_for_creator(zone, t, lockp)) 1095bd41d0a8Snordmark dropped = B_TRUE; 1096bd41d0a8Snordmark 1097bd41d0a8Snordmark if (zsd_wait_for_inprogress(zone, t, lockp)) 1098bd41d0a8Snordmark dropped = B_TRUE; 1099bd41d0a8Snordmark 1100bd41d0a8Snordmark if (t->zsd_flags & ZSD_SHUTDOWN_NEEDED) { 1101bd41d0a8Snordmark t->zsd_flags &= ~ZSD_SHUTDOWN_NEEDED; 1102bd41d0a8Snordmark t->zsd_flags |= ZSD_SHUTDOWN_INPROGRESS; 1103bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__inprogress, 1104bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1105bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1106bd41d0a8Snordmark if (lockp != NULL) 1107bd41d0a8Snordmark mutex_exit(lockp); 1108bd41d0a8Snordmark dropped = B_TRUE; 1109bd41d0a8Snordmark 1110bd41d0a8Snordmark ASSERT(t->zsd_shutdown != NULL); 1111bd41d0a8Snordmark data = t->zsd_data; 1112bd41d0a8Snordmark 1113bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__start, 1114bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1115bd41d0a8Snordmark 1116bd41d0a8Snordmark (t->zsd_shutdown)(zone->zone_id, data); 1117bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__end, 1118bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1119bd41d0a8Snordmark 1120bd41d0a8Snordmark if (lockp != NULL) 1121bd41d0a8Snordmark mutex_enter(lockp); 1122bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1123bd41d0a8Snordmark t->zsd_flags &= ~ZSD_SHUTDOWN_INPROGRESS; 1124bd41d0a8Snordmark t->zsd_flags |= ZSD_SHUTDOWN_COMPLETED; 1125bd41d0a8Snordmark cv_broadcast(&t->zsd_cv); 1126bd41d0a8Snordmark DTRACE_PROBE2(zsd__shutdown__completed, 1127bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1128bd41d0a8Snordmark } 1129bd41d0a8Snordmark if (!zone_lock_held) 1130bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1131bd41d0a8Snordmark return (dropped); 1132bd41d0a8Snordmark } 1133bd41d0a8Snordmark 1134bd41d0a8Snordmark /* 1135bd41d0a8Snordmark * Call the destroy function for the zone and key if DESTROY_NEEDED 1136bd41d0a8Snordmark * is set. 1137bd41d0a8Snordmark * If some other thread gets here first and sets *_INPROGRESS, then 1138bd41d0a8Snordmark * we wait for that thread to complete so that we can ensure that 1139bd41d0a8Snordmark * all the callbacks are done when we've looped over all zones/keys. 1140bd41d0a8Snordmark * 1141bd41d0a8Snordmark * When we call the destroy function, we drop the global held by the 1142bd41d0a8Snordmark * caller, and return true to tell the caller it needs to re-evalute the 1143bd41d0a8Snordmark * state. 1144bd41d0a8Snordmark * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 1145bd41d0a8Snordmark * remains held on exit. 1146bd41d0a8Snordmark */ 1147bd41d0a8Snordmark static boolean_t 1148bd41d0a8Snordmark zsd_apply_destroy(kmutex_t *lockp, boolean_t zone_lock_held, 1149bd41d0a8Snordmark zone_t *zone, zone_key_t key) 1150bd41d0a8Snordmark { 1151bd41d0a8Snordmark struct zsd_entry *t; 1152bd41d0a8Snordmark void *data; 1153bd41d0a8Snordmark boolean_t dropped; 1154bd41d0a8Snordmark 1155bd41d0a8Snordmark if (lockp != NULL) { 1156bd41d0a8Snordmark ASSERT(MUTEX_HELD(lockp)); 1157bd41d0a8Snordmark } 1158bd41d0a8Snordmark if (zone_lock_held) { 1159bd41d0a8Snordmark ASSERT(MUTEX_HELD(&zone->zone_lock)); 1160bd41d0a8Snordmark } else { 1161bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1162bd41d0a8Snordmark } 1163bd41d0a8Snordmark 1164bd41d0a8Snordmark t = zsd_find(&zone->zone_zsd, key); 1165bd41d0a8Snordmark if (t == NULL) { 1166bd41d0a8Snordmark /* 1167bd41d0a8Snordmark * Somebody else got here first e.g the zone going 1168bd41d0a8Snordmark * away. 1169bd41d0a8Snordmark */ 1170bd41d0a8Snordmark if (!zone_lock_held) 1171bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1172bd41d0a8Snordmark return (B_FALSE); 1173bd41d0a8Snordmark } 1174bd41d0a8Snordmark dropped = B_FALSE; 1175bd41d0a8Snordmark if (zsd_wait_for_creator(zone, t, lockp)) 1176bd41d0a8Snordmark dropped = B_TRUE; 1177bd41d0a8Snordmark 1178bd41d0a8Snordmark if (zsd_wait_for_inprogress(zone, t, lockp)) 1179bd41d0a8Snordmark dropped = B_TRUE; 1180bd41d0a8Snordmark 1181bd41d0a8Snordmark if (t->zsd_flags & ZSD_DESTROY_NEEDED) { 1182bd41d0a8Snordmark t->zsd_flags &= ~ZSD_DESTROY_NEEDED; 1183bd41d0a8Snordmark t->zsd_flags |= ZSD_DESTROY_INPROGRESS; 1184bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__inprogress, 1185bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1186bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1187bd41d0a8Snordmark if (lockp != NULL) 1188bd41d0a8Snordmark mutex_exit(lockp); 1189bd41d0a8Snordmark dropped = B_TRUE; 1190bd41d0a8Snordmark 1191bd41d0a8Snordmark ASSERT(t->zsd_destroy != NULL); 1192bd41d0a8Snordmark data = t->zsd_data; 1193bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__start, 1194bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1195bd41d0a8Snordmark 1196bd41d0a8Snordmark (t->zsd_destroy)(zone->zone_id, data); 1197bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__end, 1198bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1199bd41d0a8Snordmark 1200bd41d0a8Snordmark if (lockp != NULL) 1201bd41d0a8Snordmark mutex_enter(lockp); 1202bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1203bd41d0a8Snordmark t->zsd_data = NULL; 1204bd41d0a8Snordmark t->zsd_flags &= ~ZSD_DESTROY_INPROGRESS; 1205bd41d0a8Snordmark t->zsd_flags |= ZSD_DESTROY_COMPLETED; 1206bd41d0a8Snordmark cv_broadcast(&t->zsd_cv); 1207bd41d0a8Snordmark DTRACE_PROBE2(zsd__destroy__completed, 1208bd41d0a8Snordmark zone_t *, zone, zone_key_t, key); 1209bd41d0a8Snordmark } 1210bd41d0a8Snordmark if (!zone_lock_held) 1211bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1212bd41d0a8Snordmark return (dropped); 1213bd41d0a8Snordmark } 1214bd41d0a8Snordmark 1215bd41d0a8Snordmark /* 1216bd41d0a8Snordmark * Wait for any CREATE_NEEDED flag to be cleared. 1217bd41d0a8Snordmark * Returns true if lockp was temporarily dropped while waiting. 1218bd41d0a8Snordmark */ 1219bd41d0a8Snordmark static boolean_t 1220bd41d0a8Snordmark zsd_wait_for_creator(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp) 1221bd41d0a8Snordmark { 1222bd41d0a8Snordmark boolean_t dropped = B_FALSE; 1223bd41d0a8Snordmark 1224bd41d0a8Snordmark while (t->zsd_flags & ZSD_CREATE_NEEDED) { 1225bd41d0a8Snordmark DTRACE_PROBE2(zsd__wait__for__creator, 1226bd41d0a8Snordmark zone_t *, zone, struct zsd_entry *, t); 1227bd41d0a8Snordmark if (lockp != NULL) { 1228bd41d0a8Snordmark dropped = B_TRUE; 1229bd41d0a8Snordmark mutex_exit(lockp); 1230bd41d0a8Snordmark } 1231bd41d0a8Snordmark cv_wait(&t->zsd_cv, &zone->zone_lock); 1232bd41d0a8Snordmark if (lockp != NULL) { 1233bd41d0a8Snordmark /* First drop zone_lock to preserve order */ 1234bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1235bd41d0a8Snordmark mutex_enter(lockp); 1236bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1237bd41d0a8Snordmark } 1238bd41d0a8Snordmark } 1239bd41d0a8Snordmark return (dropped); 1240bd41d0a8Snordmark } 1241bd41d0a8Snordmark 1242bd41d0a8Snordmark /* 1243bd41d0a8Snordmark * Wait for any INPROGRESS flag to be cleared. 1244bd41d0a8Snordmark * Returns true if lockp was temporarily dropped while waiting. 1245bd41d0a8Snordmark */ 1246bd41d0a8Snordmark static boolean_t 1247bd41d0a8Snordmark zsd_wait_for_inprogress(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp) 1248bd41d0a8Snordmark { 1249bd41d0a8Snordmark boolean_t dropped = B_FALSE; 1250bd41d0a8Snordmark 1251bd41d0a8Snordmark while (t->zsd_flags & ZSD_ALL_INPROGRESS) { 1252bd41d0a8Snordmark DTRACE_PROBE2(zsd__wait__for__inprogress, 1253bd41d0a8Snordmark zone_t *, zone, struct zsd_entry *, t); 1254bd41d0a8Snordmark if (lockp != NULL) { 1255bd41d0a8Snordmark dropped = B_TRUE; 1256bd41d0a8Snordmark mutex_exit(lockp); 1257bd41d0a8Snordmark } 1258bd41d0a8Snordmark cv_wait(&t->zsd_cv, &zone->zone_lock); 1259bd41d0a8Snordmark if (lockp != NULL) { 1260bd41d0a8Snordmark /* First drop zone_lock to preserve order */ 1261bd41d0a8Snordmark mutex_exit(&zone->zone_lock); 1262bd41d0a8Snordmark mutex_enter(lockp); 1263bd41d0a8Snordmark mutex_enter(&zone->zone_lock); 1264bd41d0a8Snordmark } 1265bd41d0a8Snordmark } 1266bd41d0a8Snordmark return (dropped); 12677c478bd9Sstevel@tonic-gate } 12687c478bd9Sstevel@tonic-gate 12697c478bd9Sstevel@tonic-gate /* 1270fa9e4066Sahrens * Frees memory associated with the zone dataset list. 1271fa9e4066Sahrens */ 1272fa9e4066Sahrens static void 1273fa9e4066Sahrens zone_free_datasets(zone_t *zone) 1274fa9e4066Sahrens { 1275fa9e4066Sahrens zone_dataset_t *t, *next; 1276fa9e4066Sahrens 1277fa9e4066Sahrens for (t = list_head(&zone->zone_datasets); t != NULL; t = next) { 1278fa9e4066Sahrens next = list_next(&zone->zone_datasets, t); 1279fa9e4066Sahrens list_remove(&zone->zone_datasets, t); 1280fa9e4066Sahrens kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1); 1281fa9e4066Sahrens kmem_free(t, sizeof (*t)); 1282fa9e4066Sahrens } 1283fa9e4066Sahrens list_destroy(&zone->zone_datasets); 1284fa9e4066Sahrens } 1285fa9e4066Sahrens 1286fa9e4066Sahrens /* 12877c478bd9Sstevel@tonic-gate * zone.cpu-shares resource control support. 12887c478bd9Sstevel@tonic-gate */ 12897c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 12907c478bd9Sstevel@tonic-gate static rctl_qty_t 12917c478bd9Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 12927c478bd9Sstevel@tonic-gate { 12937c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 12947c478bd9Sstevel@tonic-gate return (p->p_zone->zone_shares); 12957c478bd9Sstevel@tonic-gate } 12967c478bd9Sstevel@tonic-gate 12977c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 12987c478bd9Sstevel@tonic-gate static int 12997c478bd9Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 13007c478bd9Sstevel@tonic-gate rctl_qty_t nv) 13017c478bd9Sstevel@tonic-gate { 13027c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13037c478bd9Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 13047c478bd9Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 13057c478bd9Sstevel@tonic-gate return (0); 13067c478bd9Sstevel@tonic-gate 13077c478bd9Sstevel@tonic-gate e->rcep_p.zone->zone_shares = nv; 13087c478bd9Sstevel@tonic-gate return (0); 13097c478bd9Sstevel@tonic-gate } 13107c478bd9Sstevel@tonic-gate 13117c478bd9Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = { 13127c478bd9Sstevel@tonic-gate rcop_no_action, 13137c478bd9Sstevel@tonic-gate zone_cpu_shares_usage, 13147c478bd9Sstevel@tonic-gate zone_cpu_shares_set, 13157c478bd9Sstevel@tonic-gate rcop_no_test 13167c478bd9Sstevel@tonic-gate }; 13177c478bd9Sstevel@tonic-gate 1318c97ad5cdSakolb /* 1319c97ad5cdSakolb * zone.cpu-cap resource control support. 1320c97ad5cdSakolb */ 1321c97ad5cdSakolb /*ARGSUSED*/ 1322c97ad5cdSakolb static rctl_qty_t 1323c97ad5cdSakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p) 1324c97ad5cdSakolb { 1325c97ad5cdSakolb ASSERT(MUTEX_HELD(&p->p_lock)); 1326c97ad5cdSakolb return (cpucaps_zone_get(p->p_zone)); 1327c97ad5cdSakolb } 1328c97ad5cdSakolb 1329c97ad5cdSakolb /*ARGSUSED*/ 1330c97ad5cdSakolb static int 1331c97ad5cdSakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1332c97ad5cdSakolb rctl_qty_t nv) 1333c97ad5cdSakolb { 1334c97ad5cdSakolb zone_t *zone = e->rcep_p.zone; 1335c97ad5cdSakolb 1336c97ad5cdSakolb ASSERT(MUTEX_HELD(&p->p_lock)); 1337c97ad5cdSakolb ASSERT(e->rcep_t == RCENTITY_ZONE); 1338c97ad5cdSakolb 1339c97ad5cdSakolb if (zone == NULL) 1340c97ad5cdSakolb return (0); 1341c97ad5cdSakolb 1342c97ad5cdSakolb /* 1343c97ad5cdSakolb * set cap to the new value. 1344c97ad5cdSakolb */ 1345c97ad5cdSakolb return (cpucaps_zone_set(zone, nv)); 1346c97ad5cdSakolb } 1347c97ad5cdSakolb 1348c97ad5cdSakolb static rctl_ops_t zone_cpu_cap_ops = { 1349c97ad5cdSakolb rcop_no_action, 1350c97ad5cdSakolb zone_cpu_cap_get, 1351c97ad5cdSakolb zone_cpu_cap_set, 1352c97ad5cdSakolb rcop_no_test 1353c97ad5cdSakolb }; 1354c97ad5cdSakolb 13557c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13567c478bd9Sstevel@tonic-gate static rctl_qty_t 13577c478bd9Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p) 13587c478bd9Sstevel@tonic-gate { 13597c478bd9Sstevel@tonic-gate rctl_qty_t nlwps; 13607c478bd9Sstevel@tonic-gate zone_t *zone = p->p_zone; 13617c478bd9Sstevel@tonic-gate 13627c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13637c478bd9Sstevel@tonic-gate 13647c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 13657c478bd9Sstevel@tonic-gate nlwps = zone->zone_nlwps; 13667c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 13677c478bd9Sstevel@tonic-gate 13687c478bd9Sstevel@tonic-gate return (nlwps); 13697c478bd9Sstevel@tonic-gate } 13707c478bd9Sstevel@tonic-gate 13717c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13727c478bd9Sstevel@tonic-gate static int 13737c478bd9Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 13747c478bd9Sstevel@tonic-gate rctl_qty_t incr, uint_t flags) 13757c478bd9Sstevel@tonic-gate { 13767c478bd9Sstevel@tonic-gate rctl_qty_t nlwps; 13777c478bd9Sstevel@tonic-gate 13787c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13797c478bd9Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 13807c478bd9Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 13817c478bd9Sstevel@tonic-gate return (0); 13827c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 13837c478bd9Sstevel@tonic-gate nlwps = e->rcep_p.zone->zone_nlwps; 13847c478bd9Sstevel@tonic-gate 13857c478bd9Sstevel@tonic-gate if (nlwps + incr > rcntl->rcv_value) 13867c478bd9Sstevel@tonic-gate return (1); 13877c478bd9Sstevel@tonic-gate 13887c478bd9Sstevel@tonic-gate return (0); 13897c478bd9Sstevel@tonic-gate } 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13927c478bd9Sstevel@tonic-gate static int 1393c6939658Ssl108498 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) 1394c6939658Ssl108498 { 13957c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13967c478bd9Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 13977c478bd9Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 13987c478bd9Sstevel@tonic-gate return (0); 13997c478bd9Sstevel@tonic-gate e->rcep_p.zone->zone_nlwps_ctl = nv; 14007c478bd9Sstevel@tonic-gate return (0); 14017c478bd9Sstevel@tonic-gate } 14027c478bd9Sstevel@tonic-gate 14037c478bd9Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = { 14047c478bd9Sstevel@tonic-gate rcop_no_action, 14057c478bd9Sstevel@tonic-gate zone_lwps_usage, 14067c478bd9Sstevel@tonic-gate zone_lwps_set, 14077c478bd9Sstevel@tonic-gate zone_lwps_test, 14087c478bd9Sstevel@tonic-gate }; 14097c478bd9Sstevel@tonic-gate 1410824c205fSml93401 /*ARGSUSED*/ 1411ff19e029SMenno Lageman static rctl_qty_t 1412ff19e029SMenno Lageman zone_procs_usage(rctl_t *r, proc_t *p) 1413ff19e029SMenno Lageman { 1414ff19e029SMenno Lageman rctl_qty_t nprocs; 1415ff19e029SMenno Lageman zone_t *zone = p->p_zone; 1416ff19e029SMenno Lageman 1417ff19e029SMenno Lageman ASSERT(MUTEX_HELD(&p->p_lock)); 1418ff19e029SMenno Lageman 1419ff19e029SMenno Lageman mutex_enter(&zone->zone_nlwps_lock); 1420ff19e029SMenno Lageman nprocs = zone->zone_nprocs; 1421ff19e029SMenno Lageman mutex_exit(&zone->zone_nlwps_lock); 1422ff19e029SMenno Lageman 1423ff19e029SMenno Lageman return (nprocs); 1424ff19e029SMenno Lageman } 1425ff19e029SMenno Lageman 1426ff19e029SMenno Lageman /*ARGSUSED*/ 1427ff19e029SMenno Lageman static int 1428ff19e029SMenno Lageman zone_procs_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 1429ff19e029SMenno Lageman rctl_qty_t incr, uint_t flags) 1430ff19e029SMenno Lageman { 1431ff19e029SMenno Lageman rctl_qty_t nprocs; 1432ff19e029SMenno Lageman 1433ff19e029SMenno Lageman ASSERT(MUTEX_HELD(&p->p_lock)); 1434ff19e029SMenno Lageman ASSERT(e->rcep_t == RCENTITY_ZONE); 1435ff19e029SMenno Lageman if (e->rcep_p.zone == NULL) 1436ff19e029SMenno Lageman return (0); 1437ff19e029SMenno Lageman ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 1438ff19e029SMenno Lageman nprocs = e->rcep_p.zone->zone_nprocs; 1439ff19e029SMenno Lageman 1440ff19e029SMenno Lageman if (nprocs + incr > rcntl->rcv_value) 1441ff19e029SMenno Lageman return (1); 1442ff19e029SMenno Lageman 1443ff19e029SMenno Lageman return (0); 1444ff19e029SMenno Lageman } 1445ff19e029SMenno Lageman 1446ff19e029SMenno Lageman /*ARGSUSED*/ 1447ff19e029SMenno Lageman static int 1448ff19e029SMenno Lageman zone_procs_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) 1449ff19e029SMenno Lageman { 1450ff19e029SMenno Lageman ASSERT(MUTEX_HELD(&p->p_lock)); 1451ff19e029SMenno Lageman ASSERT(e->rcep_t == RCENTITY_ZONE); 1452ff19e029SMenno Lageman if (e->rcep_p.zone == NULL) 1453ff19e029SMenno Lageman return (0); 1454ff19e029SMenno Lageman e->rcep_p.zone->zone_nprocs_ctl = nv; 1455ff19e029SMenno Lageman return (0); 1456ff19e029SMenno Lageman } 1457ff19e029SMenno Lageman 1458ff19e029SMenno Lageman static rctl_ops_t zone_procs_ops = { 1459ff19e029SMenno Lageman rcop_no_action, 1460ff19e029SMenno Lageman zone_procs_usage, 1461ff19e029SMenno Lageman zone_procs_set, 1462ff19e029SMenno Lageman zone_procs_test, 1463ff19e029SMenno Lageman }; 1464ff19e029SMenno Lageman 1465ff19e029SMenno Lageman /*ARGSUSED*/ 1466824c205fSml93401 static int 1467824c205fSml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1468824c205fSml93401 rctl_qty_t incr, uint_t flags) 1469824c205fSml93401 { 1470824c205fSml93401 rctl_qty_t v; 1471824c205fSml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 1472824c205fSml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 1473824c205fSml93401 v = e->rcep_p.zone->zone_shmmax + incr; 1474824c205fSml93401 if (v > rval->rcv_value) 1475824c205fSml93401 return (1); 1476824c205fSml93401 return (0); 1477824c205fSml93401 } 1478824c205fSml93401 1479824c205fSml93401 static rctl_ops_t zone_shmmax_ops = { 1480824c205fSml93401 rcop_no_action, 1481824c205fSml93401 rcop_no_usage, 1482824c205fSml93401 rcop_no_set, 1483824c205fSml93401 zone_shmmax_test 1484824c205fSml93401 }; 1485824c205fSml93401 1486824c205fSml93401 /*ARGSUSED*/ 1487824c205fSml93401 static int 1488824c205fSml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1489824c205fSml93401 rctl_qty_t incr, uint_t flags) 1490824c205fSml93401 { 1491824c205fSml93401 rctl_qty_t v; 1492824c205fSml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 1493824c205fSml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 1494824c205fSml93401 v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr; 1495824c205fSml93401 if (v > rval->rcv_value) 1496824c205fSml93401 return (1); 1497824c205fSml93401 return (0); 1498824c205fSml93401 } 1499824c205fSml93401 1500824c205fSml93401 static rctl_ops_t zone_shmmni_ops = { 1501824c205fSml93401 rcop_no_action, 1502824c205fSml93401 rcop_no_usage, 1503824c205fSml93401 rcop_no_set, 1504824c205fSml93401 zone_shmmni_test 1505824c205fSml93401 }; 1506824c205fSml93401 1507824c205fSml93401 /*ARGSUSED*/ 1508824c205fSml93401 static int 1509824c205fSml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1510824c205fSml93401 rctl_qty_t incr, uint_t flags) 1511824c205fSml93401 { 1512824c205fSml93401 rctl_qty_t v; 1513824c205fSml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 1514824c205fSml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 1515824c205fSml93401 v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr; 1516824c205fSml93401 if (v > rval->rcv_value) 1517824c205fSml93401 return (1); 1518824c205fSml93401 return (0); 1519824c205fSml93401 } 1520824c205fSml93401 1521824c205fSml93401 static rctl_ops_t zone_semmni_ops = { 1522824c205fSml93401 rcop_no_action, 1523824c205fSml93401 rcop_no_usage, 1524824c205fSml93401 rcop_no_set, 1525824c205fSml93401 zone_semmni_test 1526824c205fSml93401 }; 1527824c205fSml93401 1528824c205fSml93401 /*ARGSUSED*/ 1529824c205fSml93401 static int 1530824c205fSml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1531824c205fSml93401 rctl_qty_t incr, uint_t flags) 1532824c205fSml93401 { 1533824c205fSml93401 rctl_qty_t v; 1534824c205fSml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 1535824c205fSml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 1536824c205fSml93401 v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr; 1537824c205fSml93401 if (v > rval->rcv_value) 1538824c205fSml93401 return (1); 1539824c205fSml93401 return (0); 1540824c205fSml93401 } 1541824c205fSml93401 1542824c205fSml93401 static rctl_ops_t zone_msgmni_ops = { 1543824c205fSml93401 rcop_no_action, 1544824c205fSml93401 rcop_no_usage, 1545824c205fSml93401 rcop_no_set, 1546824c205fSml93401 zone_msgmni_test 1547824c205fSml93401 }; 1548824c205fSml93401 1549c6939658Ssl108498 /*ARGSUSED*/ 1550c6939658Ssl108498 static rctl_qty_t 1551c6939658Ssl108498 zone_locked_mem_usage(rctl_t *rctl, struct proc *p) 1552c6939658Ssl108498 { 1553c6939658Ssl108498 rctl_qty_t q; 1554c6939658Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 15550209230bSgjelinek mutex_enter(&p->p_zone->zone_mem_lock); 1556c6939658Ssl108498 q = p->p_zone->zone_locked_mem; 15570209230bSgjelinek mutex_exit(&p->p_zone->zone_mem_lock); 1558c6939658Ssl108498 return (q); 1559c6939658Ssl108498 } 1560c6939658Ssl108498 1561c6939658Ssl108498 /*ARGSUSED*/ 1562c6939658Ssl108498 static int 1563c6939658Ssl108498 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 1564c6939658Ssl108498 rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 1565c6939658Ssl108498 { 1566c6939658Ssl108498 rctl_qty_t q; 15670209230bSgjelinek zone_t *z; 15680209230bSgjelinek 15690209230bSgjelinek z = e->rcep_p.zone; 1570c6939658Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 15710209230bSgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 15720209230bSgjelinek q = z->zone_locked_mem; 1573c6939658Ssl108498 if (q + incr > rcntl->rcv_value) 1574c6939658Ssl108498 return (1); 1575c6939658Ssl108498 return (0); 1576c6939658Ssl108498 } 1577c6939658Ssl108498 1578c6939658Ssl108498 /*ARGSUSED*/ 1579c6939658Ssl108498 static int 1580c6939658Ssl108498 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1581c6939658Ssl108498 rctl_qty_t nv) 1582c6939658Ssl108498 { 1583c6939658Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 1584c6939658Ssl108498 ASSERT(e->rcep_t == RCENTITY_ZONE); 1585c6939658Ssl108498 if (e->rcep_p.zone == NULL) 1586c6939658Ssl108498 return (0); 1587c6939658Ssl108498 e->rcep_p.zone->zone_locked_mem_ctl = nv; 1588c6939658Ssl108498 return (0); 1589c6939658Ssl108498 } 1590c6939658Ssl108498 1591c6939658Ssl108498 static rctl_ops_t zone_locked_mem_ops = { 1592c6939658Ssl108498 rcop_no_action, 1593c6939658Ssl108498 zone_locked_mem_usage, 1594c6939658Ssl108498 zone_locked_mem_set, 1595c6939658Ssl108498 zone_locked_mem_test 1596c6939658Ssl108498 }; 1597824c205fSml93401 15980209230bSgjelinek /*ARGSUSED*/ 15990209230bSgjelinek static rctl_qty_t 16000209230bSgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p) 16010209230bSgjelinek { 16020209230bSgjelinek rctl_qty_t q; 16030209230bSgjelinek zone_t *z = p->p_zone; 16040209230bSgjelinek 16050209230bSgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 16060209230bSgjelinek mutex_enter(&z->zone_mem_lock); 16070209230bSgjelinek q = z->zone_max_swap; 16080209230bSgjelinek mutex_exit(&z->zone_mem_lock); 16090209230bSgjelinek return (q); 16100209230bSgjelinek } 16110209230bSgjelinek 16120209230bSgjelinek /*ARGSUSED*/ 16130209230bSgjelinek static int 16140209230bSgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 16150209230bSgjelinek rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 16160209230bSgjelinek { 16170209230bSgjelinek rctl_qty_t q; 16180209230bSgjelinek zone_t *z; 16190209230bSgjelinek 16200209230bSgjelinek z = e->rcep_p.zone; 16210209230bSgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 16220209230bSgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 16230209230bSgjelinek q = z->zone_max_swap; 16240209230bSgjelinek if (q + incr > rcntl->rcv_value) 16250209230bSgjelinek return (1); 16260209230bSgjelinek return (0); 16270209230bSgjelinek } 16280209230bSgjelinek 16290209230bSgjelinek /*ARGSUSED*/ 16300209230bSgjelinek static int 16310209230bSgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 16320209230bSgjelinek rctl_qty_t nv) 16330209230bSgjelinek { 16340209230bSgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 16350209230bSgjelinek ASSERT(e->rcep_t == RCENTITY_ZONE); 16360209230bSgjelinek if (e->rcep_p.zone == NULL) 16370209230bSgjelinek return (0); 16380209230bSgjelinek e->rcep_p.zone->zone_max_swap_ctl = nv; 16390209230bSgjelinek return (0); 16400209230bSgjelinek } 16410209230bSgjelinek 16420209230bSgjelinek static rctl_ops_t zone_max_swap_ops = { 16430209230bSgjelinek rcop_no_action, 16440209230bSgjelinek zone_max_swap_usage, 16450209230bSgjelinek zone_max_swap_set, 16460209230bSgjelinek zone_max_swap_test 16470209230bSgjelinek }; 16480209230bSgjelinek 16490fbb751dSJohn Levon /*ARGSUSED*/ 16500fbb751dSJohn Levon static rctl_qty_t 16510fbb751dSJohn Levon zone_max_lofi_usage(rctl_t *rctl, struct proc *p) 16520fbb751dSJohn Levon { 16530fbb751dSJohn Levon rctl_qty_t q; 16540fbb751dSJohn Levon zone_t *z = p->p_zone; 16550fbb751dSJohn Levon 16560fbb751dSJohn Levon ASSERT(MUTEX_HELD(&p->p_lock)); 16570fbb751dSJohn Levon mutex_enter(&z->zone_rctl_lock); 16580fbb751dSJohn Levon q = z->zone_max_lofi; 16590fbb751dSJohn Levon mutex_exit(&z->zone_rctl_lock); 16600fbb751dSJohn Levon return (q); 16610fbb751dSJohn Levon } 16620fbb751dSJohn Levon 16630fbb751dSJohn Levon /*ARGSUSED*/ 16640fbb751dSJohn Levon static int 16650fbb751dSJohn Levon zone_max_lofi_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 16660fbb751dSJohn Levon rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 16670fbb751dSJohn Levon { 16680fbb751dSJohn Levon rctl_qty_t q; 16690fbb751dSJohn Levon zone_t *z; 16700fbb751dSJohn Levon 16710fbb751dSJohn Levon z = e->rcep_p.zone; 16720fbb751dSJohn Levon ASSERT(MUTEX_HELD(&p->p_lock)); 16730fbb751dSJohn Levon ASSERT(MUTEX_HELD(&z->zone_rctl_lock)); 16740fbb751dSJohn Levon q = z->zone_max_lofi; 16750fbb751dSJohn Levon if (q + incr > rcntl->rcv_value) 16760fbb751dSJohn Levon return (1); 16770fbb751dSJohn Levon return (0); 16780fbb751dSJohn Levon } 16790fbb751dSJohn Levon 16800fbb751dSJohn Levon /*ARGSUSED*/ 16810fbb751dSJohn Levon static int 16820fbb751dSJohn Levon zone_max_lofi_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 16830fbb751dSJohn Levon rctl_qty_t nv) 16840fbb751dSJohn Levon { 16850fbb751dSJohn Levon ASSERT(MUTEX_HELD(&p->p_lock)); 16860fbb751dSJohn Levon ASSERT(e->rcep_t == RCENTITY_ZONE); 16870fbb751dSJohn Levon if (e->rcep_p.zone == NULL) 16880fbb751dSJohn Levon return (0); 16890fbb751dSJohn Levon e->rcep_p.zone->zone_max_lofi_ctl = nv; 16900fbb751dSJohn Levon return (0); 16910fbb751dSJohn Levon } 16920fbb751dSJohn Levon 16930fbb751dSJohn Levon static rctl_ops_t zone_max_lofi_ops = { 16940fbb751dSJohn Levon rcop_no_action, 16950fbb751dSJohn Levon zone_max_lofi_usage, 16960fbb751dSJohn Levon zone_max_lofi_set, 16970fbb751dSJohn Levon zone_max_lofi_test 16980fbb751dSJohn Levon }; 16990fbb751dSJohn Levon 17007c478bd9Sstevel@tonic-gate /* 17017c478bd9Sstevel@tonic-gate * Helper function to brand the zone with a unique ID. 17027c478bd9Sstevel@tonic-gate */ 17037c478bd9Sstevel@tonic-gate static void 17047c478bd9Sstevel@tonic-gate zone_uniqid(zone_t *zone) 17057c478bd9Sstevel@tonic-gate { 17067c478bd9Sstevel@tonic-gate static uint64_t uniqid = 0; 17077c478bd9Sstevel@tonic-gate 17087c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 17097c478bd9Sstevel@tonic-gate zone->zone_uniqid = uniqid++; 17107c478bd9Sstevel@tonic-gate } 17117c478bd9Sstevel@tonic-gate 17127c478bd9Sstevel@tonic-gate /* 17137c478bd9Sstevel@tonic-gate * Returns a held pointer to the "kcred" for the specified zone. 17147c478bd9Sstevel@tonic-gate */ 17157c478bd9Sstevel@tonic-gate struct cred * 17167c478bd9Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid) 17177c478bd9Sstevel@tonic-gate { 17187c478bd9Sstevel@tonic-gate zone_t *zone; 17197c478bd9Sstevel@tonic-gate cred_t *cr; 17207c478bd9Sstevel@tonic-gate 17217c478bd9Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) 17227c478bd9Sstevel@tonic-gate return (NULL); 17237c478bd9Sstevel@tonic-gate cr = zone->zone_kcred; 17247c478bd9Sstevel@tonic-gate crhold(cr); 17257c478bd9Sstevel@tonic-gate zone_rele(zone); 17267c478bd9Sstevel@tonic-gate return (cr); 17277c478bd9Sstevel@tonic-gate } 17287c478bd9Sstevel@tonic-gate 17290209230bSgjelinek static int 17300209230bSgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw) 17310209230bSgjelinek { 17320209230bSgjelinek zone_t *zone = ksp->ks_private; 17330209230bSgjelinek zone_kstat_t *zk = ksp->ks_data; 17340209230bSgjelinek 17350209230bSgjelinek if (rw == KSTAT_WRITE) 17360209230bSgjelinek return (EACCES); 17370209230bSgjelinek 17380209230bSgjelinek zk->zk_usage.value.ui64 = zone->zone_locked_mem; 17390209230bSgjelinek zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl; 17400209230bSgjelinek return (0); 17410209230bSgjelinek } 17420209230bSgjelinek 17430209230bSgjelinek static int 1744ff19e029SMenno Lageman zone_nprocs_kstat_update(kstat_t *ksp, int rw) 1745ff19e029SMenno Lageman { 1746ff19e029SMenno Lageman zone_t *zone = ksp->ks_private; 1747ff19e029SMenno Lageman zone_kstat_t *zk = ksp->ks_data; 1748ff19e029SMenno Lageman 1749ff19e029SMenno Lageman if (rw == KSTAT_WRITE) 1750ff19e029SMenno Lageman return (EACCES); 1751ff19e029SMenno Lageman 1752ff19e029SMenno Lageman zk->zk_usage.value.ui64 = zone->zone_nprocs; 1753ff19e029SMenno Lageman zk->zk_value.value.ui64 = zone->zone_nprocs_ctl; 1754ff19e029SMenno Lageman return (0); 1755ff19e029SMenno Lageman } 1756ff19e029SMenno Lageman 1757ff19e029SMenno Lageman static int 17580209230bSgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw) 17590209230bSgjelinek { 17600209230bSgjelinek zone_t *zone = ksp->ks_private; 17610209230bSgjelinek zone_kstat_t *zk = ksp->ks_data; 17620209230bSgjelinek 17630209230bSgjelinek if (rw == KSTAT_WRITE) 17640209230bSgjelinek return (EACCES); 17650209230bSgjelinek 17660209230bSgjelinek zk->zk_usage.value.ui64 = zone->zone_max_swap; 17670209230bSgjelinek zk->zk_value.value.ui64 = zone->zone_max_swap_ctl; 17680209230bSgjelinek return (0); 17690209230bSgjelinek } 17700209230bSgjelinek 1771ff19e029SMenno Lageman static kstat_t * 1772ff19e029SMenno Lageman zone_kstat_create_common(zone_t *zone, char *name, 1773ff19e029SMenno Lageman int (*updatefunc) (kstat_t *, int)) 17740209230bSgjelinek { 17750209230bSgjelinek kstat_t *ksp; 17760209230bSgjelinek zone_kstat_t *zk; 17770209230bSgjelinek 1778ff19e029SMenno Lageman ksp = rctl_kstat_create_zone(zone, name, KSTAT_TYPE_NAMED, 17790209230bSgjelinek sizeof (zone_kstat_t) / sizeof (kstat_named_t), 17800209230bSgjelinek KSTAT_FLAG_VIRTUAL); 17810209230bSgjelinek 17820209230bSgjelinek if (ksp == NULL) 1783ff19e029SMenno Lageman return (NULL); 17840209230bSgjelinek 17850209230bSgjelinek zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 17860209230bSgjelinek ksp->ks_data_size += strlen(zone->zone_name) + 1; 17870209230bSgjelinek kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 17880209230bSgjelinek kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 17890209230bSgjelinek kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 17900209230bSgjelinek kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 1791ff19e029SMenno Lageman ksp->ks_update = updatefunc; 17920209230bSgjelinek ksp->ks_private = zone; 17930209230bSgjelinek kstat_install(ksp); 1794ff19e029SMenno Lageman return (ksp); 1795ff19e029SMenno Lageman } 17960209230bSgjelinek 1797ff19e029SMenno Lageman static void 1798ff19e029SMenno Lageman zone_kstat_create(zone_t *zone) 1799ff19e029SMenno Lageman { 1800ff19e029SMenno Lageman zone->zone_lockedmem_kstat = zone_kstat_create_common(zone, 1801ff19e029SMenno Lageman "lockedmem", zone_lockedmem_kstat_update); 1802ff19e029SMenno Lageman zone->zone_swapresv_kstat = zone_kstat_create_common(zone, 1803ff19e029SMenno Lageman "swapresv", zone_swapresv_kstat_update); 1804ff19e029SMenno Lageman zone->zone_nprocs_kstat = zone_kstat_create_common(zone, 1805ff19e029SMenno Lageman "nprocs", zone_nprocs_kstat_update); 1806ff19e029SMenno Lageman } 18070209230bSgjelinek 1808ff19e029SMenno Lageman static void 1809ff19e029SMenno Lageman zone_kstat_delete_common(kstat_t **pkstat) 1810ff19e029SMenno Lageman { 1811ff19e029SMenno Lageman void *data; 18120209230bSgjelinek 1813ff19e029SMenno Lageman if (*pkstat != NULL) { 1814ff19e029SMenno Lageman data = (*pkstat)->ks_data; 1815ff19e029SMenno Lageman kstat_delete(*pkstat); 1816ff19e029SMenno Lageman kmem_free(data, sizeof (zone_kstat_t)); 1817ff19e029SMenno Lageman *pkstat = NULL; 1818ff19e029SMenno Lageman } 18190209230bSgjelinek } 18200209230bSgjelinek 18210209230bSgjelinek static void 18220209230bSgjelinek zone_kstat_delete(zone_t *zone) 18230209230bSgjelinek { 1824ff19e029SMenno Lageman zone_kstat_delete_common(&zone->zone_lockedmem_kstat); 1825ff19e029SMenno Lageman zone_kstat_delete_common(&zone->zone_swapresv_kstat); 1826ff19e029SMenno Lageman zone_kstat_delete_common(&zone->zone_nprocs_kstat); 18270209230bSgjelinek } 18280209230bSgjelinek 18297c478bd9Sstevel@tonic-gate /* 18307c478bd9Sstevel@tonic-gate * Called very early on in boot to initialize the ZSD list so that 18317c478bd9Sstevel@tonic-gate * zone_key_create() can be called before zone_init(). It also initializes 18327c478bd9Sstevel@tonic-gate * portions of zone0 which may be used before zone_init() is called. The 18337c478bd9Sstevel@tonic-gate * variable "global_zone" will be set when zone0 is fully initialized by 18347c478bd9Sstevel@tonic-gate * zone_init(). 18357c478bd9Sstevel@tonic-gate */ 18367c478bd9Sstevel@tonic-gate void 18377c478bd9Sstevel@tonic-gate zone_zsd_init(void) 18387c478bd9Sstevel@tonic-gate { 18397c478bd9Sstevel@tonic-gate mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 18407c478bd9Sstevel@tonic-gate mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 18417c478bd9Sstevel@tonic-gate list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 18427c478bd9Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 18437c478bd9Sstevel@tonic-gate list_create(&zone_active, sizeof (zone_t), 18447c478bd9Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 18457c478bd9Sstevel@tonic-gate list_create(&zone_deathrow, sizeof (zone_t), 18467c478bd9Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 18477c478bd9Sstevel@tonic-gate 18487c478bd9Sstevel@tonic-gate mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 18497c478bd9Sstevel@tonic-gate mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 18500209230bSgjelinek mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 18517c478bd9Sstevel@tonic-gate zone0.zone_shares = 1; 18520209230bSgjelinek zone0.zone_nlwps = 0; 18537c478bd9Sstevel@tonic-gate zone0.zone_nlwps_ctl = INT_MAX; 1854ff19e029SMenno Lageman zone0.zone_nprocs = 0; 1855ff19e029SMenno Lageman zone0.zone_nprocs_ctl = INT_MAX; 18560209230bSgjelinek zone0.zone_locked_mem = 0; 18570209230bSgjelinek zone0.zone_locked_mem_ctl = UINT64_MAX; 18580209230bSgjelinek ASSERT(zone0.zone_max_swap == 0); 18590209230bSgjelinek zone0.zone_max_swap_ctl = UINT64_MAX; 18600fbb751dSJohn Levon zone0.zone_max_lofi = 0; 18610fbb751dSJohn Levon zone0.zone_max_lofi_ctl = UINT64_MAX; 1862824c205fSml93401 zone0.zone_shmmax = 0; 1863824c205fSml93401 zone0.zone_ipc.ipcq_shmmni = 0; 1864824c205fSml93401 zone0.zone_ipc.ipcq_semmni = 0; 1865824c205fSml93401 zone0.zone_ipc.ipcq_msgmni = 0; 18667c478bd9Sstevel@tonic-gate zone0.zone_name = GLOBAL_ZONENAME; 18677c478bd9Sstevel@tonic-gate zone0.zone_nodename = utsname.nodename; 18687c478bd9Sstevel@tonic-gate zone0.zone_domain = srpc_domain; 18695679c89fSjv227347 zone0.zone_hostid = HW_INVALID_HOSTID; 18700fbb751dSJohn Levon zone0.zone_fs_allowed = NULL; 18717c478bd9Sstevel@tonic-gate zone0.zone_ref = 1; 18727c478bd9Sstevel@tonic-gate zone0.zone_id = GLOBAL_ZONEID; 18737c478bd9Sstevel@tonic-gate zone0.zone_status = ZONE_IS_RUNNING; 18747c478bd9Sstevel@tonic-gate zone0.zone_rootpath = "/"; 18757c478bd9Sstevel@tonic-gate zone0.zone_rootpathlen = 2; 18767c478bd9Sstevel@tonic-gate zone0.zone_psetid = ZONE_PS_INVAL; 18777c478bd9Sstevel@tonic-gate zone0.zone_ncpus = 0; 18787c478bd9Sstevel@tonic-gate zone0.zone_ncpus_online = 0; 18797c478bd9Sstevel@tonic-gate zone0.zone_proc_initpid = 1; 18803f2f09c1Sdp zone0.zone_initname = initname; 18810209230bSgjelinek zone0.zone_lockedmem_kstat = NULL; 18820209230bSgjelinek zone0.zone_swapresv_kstat = NULL; 1883ff19e029SMenno Lageman zone0.zone_nprocs_kstat = NULL; 18847c478bd9Sstevel@tonic-gate list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 18857c478bd9Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 18867c478bd9Sstevel@tonic-gate list_insert_head(&zone_active, &zone0); 18877c478bd9Sstevel@tonic-gate 18887c478bd9Sstevel@tonic-gate /* 18897c478bd9Sstevel@tonic-gate * The root filesystem is not mounted yet, so zone_rootvp cannot be set 18907c478bd9Sstevel@tonic-gate * to anything meaningful. It is assigned to be 'rootdir' in 18917c478bd9Sstevel@tonic-gate * vfs_mountroot(). 18927c478bd9Sstevel@tonic-gate */ 18937c478bd9Sstevel@tonic-gate zone0.zone_rootvp = NULL; 18947c478bd9Sstevel@tonic-gate zone0.zone_vfslist = NULL; 18953f2f09c1Sdp zone0.zone_bootargs = initargs; 18967c478bd9Sstevel@tonic-gate zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 18977c478bd9Sstevel@tonic-gate /* 18987c478bd9Sstevel@tonic-gate * The global zone has all privileges 18997c478bd9Sstevel@tonic-gate */ 19007c478bd9Sstevel@tonic-gate priv_fillset(zone0.zone_privset); 19017c478bd9Sstevel@tonic-gate /* 19027c478bd9Sstevel@tonic-gate * Add p0 to the global zone 19037c478bd9Sstevel@tonic-gate */ 19047c478bd9Sstevel@tonic-gate zone0.zone_zsched = &p0; 19057c478bd9Sstevel@tonic-gate p0.p_zone = &zone0; 19067c478bd9Sstevel@tonic-gate } 19077c478bd9Sstevel@tonic-gate 19087c478bd9Sstevel@tonic-gate /* 190945916cd2Sjpk * Compute a hash value based on the contents of the label and the DOI. The 191045916cd2Sjpk * hash algorithm is somewhat arbitrary, but is based on the observation that 191145916cd2Sjpk * humans will likely pick labels that differ by amounts that work out to be 191245916cd2Sjpk * multiples of the number of hash chains, and thus stirring in some primes 191345916cd2Sjpk * should help. 191445916cd2Sjpk */ 191545916cd2Sjpk static uint_t 191645916cd2Sjpk hash_bylabel(void *hdata, mod_hash_key_t key) 191745916cd2Sjpk { 191845916cd2Sjpk const ts_label_t *lab = (ts_label_t *)key; 191945916cd2Sjpk const uint32_t *up, *ue; 192045916cd2Sjpk uint_t hash; 192145916cd2Sjpk int i; 192245916cd2Sjpk 192345916cd2Sjpk _NOTE(ARGUNUSED(hdata)); 192445916cd2Sjpk 192545916cd2Sjpk hash = lab->tsl_doi + (lab->tsl_doi << 1); 192645916cd2Sjpk /* we depend on alignment of label, but not representation */ 192745916cd2Sjpk up = (const uint32_t *)&lab->tsl_label; 192845916cd2Sjpk ue = up + sizeof (lab->tsl_label) / sizeof (*up); 192945916cd2Sjpk i = 1; 193045916cd2Sjpk while (up < ue) { 193145916cd2Sjpk /* using 2^n + 1, 1 <= n <= 16 as source of many primes */ 193245916cd2Sjpk hash += *up + (*up << ((i % 16) + 1)); 193345916cd2Sjpk up++; 193445916cd2Sjpk i++; 193545916cd2Sjpk } 193645916cd2Sjpk return (hash); 193745916cd2Sjpk } 193845916cd2Sjpk 193945916cd2Sjpk /* 194045916cd2Sjpk * All that mod_hash cares about here is zero (equal) versus non-zero (not 194145916cd2Sjpk * equal). This may need to be changed if less than / greater than is ever 194245916cd2Sjpk * needed. 194345916cd2Sjpk */ 194445916cd2Sjpk static int 194545916cd2Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 194645916cd2Sjpk { 194745916cd2Sjpk ts_label_t *lab1 = (ts_label_t *)key1; 194845916cd2Sjpk ts_label_t *lab2 = (ts_label_t *)key2; 194945916cd2Sjpk 195045916cd2Sjpk return (label_equal(lab1, lab2) ? 0 : 1); 195145916cd2Sjpk } 195245916cd2Sjpk 195345916cd2Sjpk /* 19547c478bd9Sstevel@tonic-gate * Called by main() to initialize the zones framework. 19557c478bd9Sstevel@tonic-gate */ 19567c478bd9Sstevel@tonic-gate void 19577c478bd9Sstevel@tonic-gate zone_init(void) 19587c478bd9Sstevel@tonic-gate { 19597c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rde; 19607c478bd9Sstevel@tonic-gate rctl_val_t *dval; 19617c478bd9Sstevel@tonic-gate rctl_set_t *set; 19627c478bd9Sstevel@tonic-gate rctl_alloc_gp_t *gp; 19637c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 1964cf8f45c7Sdstaff int res; 19657c478bd9Sstevel@tonic-gate 19667c478bd9Sstevel@tonic-gate ASSERT(curproc == &p0); 19677c478bd9Sstevel@tonic-gate 19687c478bd9Sstevel@tonic-gate /* 19697c478bd9Sstevel@tonic-gate * Create ID space for zone IDs. ID 0 is reserved for the 19707c478bd9Sstevel@tonic-gate * global zone. 19717c478bd9Sstevel@tonic-gate */ 19727c478bd9Sstevel@tonic-gate zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 19737c478bd9Sstevel@tonic-gate 19747c478bd9Sstevel@tonic-gate /* 19757c478bd9Sstevel@tonic-gate * Initialize generic zone resource controls, if any. 19767c478bd9Sstevel@tonic-gate */ 19777c478bd9Sstevel@tonic-gate rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 19787c478bd9Sstevel@tonic-gate RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 197919f92332Sml93401 RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER, 1980c97ad5cdSakolb FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops); 1981c97ad5cdSakolb 1982c97ad5cdSakolb rc_zone_cpu_cap = rctl_register("zone.cpu-cap", 1983c97ad5cdSakolb RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS | 1984c97ad5cdSakolb RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER | 1985c97ad5cdSakolb RCTL_GLOBAL_INFINITE, 1986c97ad5cdSakolb MAXCAP, MAXCAP, &zone_cpu_cap_ops); 19877c478bd9Sstevel@tonic-gate 19887c478bd9Sstevel@tonic-gate rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 19897c478bd9Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 19907c478bd9Sstevel@tonic-gate INT_MAX, INT_MAX, &zone_lwps_ops); 1991ff19e029SMenno Lageman 1992ff19e029SMenno Lageman rc_zone_nprocs = rctl_register("zone.max-processes", RCENTITY_ZONE, 1993ff19e029SMenno Lageman RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 1994ff19e029SMenno Lageman INT_MAX, INT_MAX, &zone_procs_ops); 1995ff19e029SMenno Lageman 19967c478bd9Sstevel@tonic-gate /* 1997824c205fSml93401 * System V IPC resource controls 1998824c205fSml93401 */ 1999824c205fSml93401 rc_zone_msgmni = rctl_register("zone.max-msg-ids", 2000824c205fSml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 2001824c205fSml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops); 2002824c205fSml93401 2003824c205fSml93401 rc_zone_semmni = rctl_register("zone.max-sem-ids", 2004824c205fSml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 2005824c205fSml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops); 2006824c205fSml93401 2007824c205fSml93401 rc_zone_shmmni = rctl_register("zone.max-shm-ids", 2008824c205fSml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 2009824c205fSml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops); 2010824c205fSml93401 2011824c205fSml93401 rc_zone_shmmax = rctl_register("zone.max-shm-memory", 2012824c205fSml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 2013824c205fSml93401 RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops); 2014824c205fSml93401 2015824c205fSml93401 /* 20167c478bd9Sstevel@tonic-gate * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 20177c478bd9Sstevel@tonic-gate * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 20187c478bd9Sstevel@tonic-gate */ 20197c478bd9Sstevel@tonic-gate dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 20207c478bd9Sstevel@tonic-gate bzero(dval, sizeof (rctl_val_t)); 20217c478bd9Sstevel@tonic-gate dval->rcv_value = 1; 20227c478bd9Sstevel@tonic-gate dval->rcv_privilege = RCPRIV_PRIVILEGED; 20237c478bd9Sstevel@tonic-gate dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 20247c478bd9Sstevel@tonic-gate dval->rcv_action_recip_pid = -1; 20257c478bd9Sstevel@tonic-gate 20267c478bd9Sstevel@tonic-gate rde = rctl_dict_lookup("zone.cpu-shares"); 20277c478bd9Sstevel@tonic-gate (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 20287c478bd9Sstevel@tonic-gate 2029c6939658Ssl108498 rc_zone_locked_mem = rctl_register("zone.max-locked-memory", 2030c6939658Ssl108498 RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 2031c6939658Ssl108498 RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 2032c6939658Ssl108498 &zone_locked_mem_ops); 20330209230bSgjelinek 20340209230bSgjelinek rc_zone_max_swap = rctl_register("zone.max-swap", 20350209230bSgjelinek RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 20360209230bSgjelinek RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 20370209230bSgjelinek &zone_max_swap_ops); 20380209230bSgjelinek 20390fbb751dSJohn Levon rc_zone_max_lofi = rctl_register("zone.max-lofi", 20400fbb751dSJohn Levon RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | 20410fbb751dSJohn Levon RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 20420fbb751dSJohn Levon &zone_max_lofi_ops); 20430fbb751dSJohn Levon 20447c478bd9Sstevel@tonic-gate /* 20457c478bd9Sstevel@tonic-gate * Initialize the ``global zone''. 20467c478bd9Sstevel@tonic-gate */ 20477c478bd9Sstevel@tonic-gate set = rctl_set_create(); 20487c478bd9Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 20497c478bd9Sstevel@tonic-gate mutex_enter(&p0.p_lock); 20507c478bd9Sstevel@tonic-gate e.rcep_p.zone = &zone0; 20517c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 20527c478bd9Sstevel@tonic-gate zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 20537c478bd9Sstevel@tonic-gate gp); 20547c478bd9Sstevel@tonic-gate 20557c478bd9Sstevel@tonic-gate zone0.zone_nlwps = p0.p_lwpcnt; 2056ff19e029SMenno Lageman zone0.zone_nprocs = 1; 20577c478bd9Sstevel@tonic-gate zone0.zone_ntasks = 1; 20587c478bd9Sstevel@tonic-gate mutex_exit(&p0.p_lock); 20599acbbeafSnn35248 zone0.zone_restart_init = B_TRUE; 20609acbbeafSnn35248 zone0.zone_brand = &native_brand; 20617c478bd9Sstevel@tonic-gate rctl_prealloc_destroy(gp); 20627c478bd9Sstevel@tonic-gate /* 20630209230bSgjelinek * pool_default hasn't been initialized yet, so we let pool_init() 20640209230bSgjelinek * take care of making sure the global zone is in the default pool. 20657c478bd9Sstevel@tonic-gate */ 206645916cd2Sjpk 206745916cd2Sjpk /* 20680209230bSgjelinek * Initialize global zone kstats 20690209230bSgjelinek */ 20700209230bSgjelinek zone_kstat_create(&zone0); 20710209230bSgjelinek 20720209230bSgjelinek /* 207345916cd2Sjpk * Initialize zone label. 207445916cd2Sjpk * mlp are initialized when tnzonecfg is loaded. 207545916cd2Sjpk */ 207645916cd2Sjpk zone0.zone_slabel = l_admin_low; 207745916cd2Sjpk rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 207845916cd2Sjpk label_hold(l_admin_low); 207945916cd2Sjpk 2080835ee219SRobert Harris /* 2081835ee219SRobert Harris * Initialise the lock for the database structure used by mntfs. 2082835ee219SRobert Harris */ 2083835ee219SRobert Harris rw_init(&zone0.zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL); 2084835ee219SRobert Harris 20857c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 20867c478bd9Sstevel@tonic-gate zone_uniqid(&zone0); 20877c478bd9Sstevel@tonic-gate ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 208845916cd2Sjpk 20897c478bd9Sstevel@tonic-gate zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 20907c478bd9Sstevel@tonic-gate mod_hash_null_valdtor); 20917c478bd9Sstevel@tonic-gate zonehashbyname = mod_hash_create_strhash("zone_by_name", 20927c478bd9Sstevel@tonic-gate zone_hash_size, mod_hash_null_valdtor); 209345916cd2Sjpk /* 209445916cd2Sjpk * maintain zonehashbylabel only for labeled systems 209545916cd2Sjpk */ 209645916cd2Sjpk if (is_system_labeled()) 209745916cd2Sjpk zonehashbylabel = mod_hash_create_extended("zone_by_label", 209845916cd2Sjpk zone_hash_size, mod_hash_null_keydtor, 209945916cd2Sjpk mod_hash_null_valdtor, hash_bylabel, NULL, 210045916cd2Sjpk hash_labelkey_cmp, KM_SLEEP); 21017c478bd9Sstevel@tonic-gate zonecount = 1; 21027c478bd9Sstevel@tonic-gate 21037c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 21047c478bd9Sstevel@tonic-gate (mod_hash_val_t)&zone0); 21057c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 21067c478bd9Sstevel@tonic-gate (mod_hash_val_t)&zone0); 210748451833Scarlsonj if (is_system_labeled()) { 210848451833Scarlsonj zone0.zone_flags |= ZF_HASHED_LABEL; 210945916cd2Sjpk (void) mod_hash_insert(zonehashbylabel, 211045916cd2Sjpk (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0); 211148451833Scarlsonj } 211245916cd2Sjpk mutex_exit(&zonehash_lock); 211345916cd2Sjpk 21147c478bd9Sstevel@tonic-gate /* 21157c478bd9Sstevel@tonic-gate * We avoid setting zone_kcred until now, since kcred is initialized 21167c478bd9Sstevel@tonic-gate * sometime after zone_zsd_init() and before zone_init(). 21177c478bd9Sstevel@tonic-gate */ 21187c478bd9Sstevel@tonic-gate zone0.zone_kcred = kcred; 21197c478bd9Sstevel@tonic-gate /* 21207c478bd9Sstevel@tonic-gate * The global zone is fully initialized (except for zone_rootvp which 21217c478bd9Sstevel@tonic-gate * will be set when the root filesystem is mounted). 21227c478bd9Sstevel@tonic-gate */ 21237c478bd9Sstevel@tonic-gate global_zone = &zone0; 2124cf8f45c7Sdstaff 2125cf8f45c7Sdstaff /* 2126cf8f45c7Sdstaff * Setup an event channel to send zone status change notifications on 2127cf8f45c7Sdstaff */ 2128cf8f45c7Sdstaff res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan, 2129cf8f45c7Sdstaff EVCH_CREAT); 2130cf8f45c7Sdstaff 2131cf8f45c7Sdstaff if (res) 2132cf8f45c7Sdstaff panic("Sysevent_evc_bind failed during zone setup.\n"); 21330209230bSgjelinek 21347c478bd9Sstevel@tonic-gate } 21357c478bd9Sstevel@tonic-gate 21367c478bd9Sstevel@tonic-gate static void 21377c478bd9Sstevel@tonic-gate zone_free(zone_t *zone) 21387c478bd9Sstevel@tonic-gate { 21397c478bd9Sstevel@tonic-gate ASSERT(zone != global_zone); 21407c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ntasks == 0); 21417c478bd9Sstevel@tonic-gate ASSERT(zone->zone_nlwps == 0); 2142ff19e029SMenno Lageman ASSERT(zone->zone_nprocs == 0); 21437c478bd9Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 0); 21447c478bd9Sstevel@tonic-gate ASSERT(zone->zone_kcred == NULL); 21457c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 21467c478bd9Sstevel@tonic-gate zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 21477c478bd9Sstevel@tonic-gate 2148c97ad5cdSakolb /* 2149c97ad5cdSakolb * Remove any zone caps. 2150c97ad5cdSakolb */ 2151c97ad5cdSakolb cpucaps_zone_remove(zone); 2152c97ad5cdSakolb 2153c97ad5cdSakolb ASSERT(zone->zone_cpucap == NULL); 2154c97ad5cdSakolb 21557c478bd9Sstevel@tonic-gate /* remove from deathrow list */ 21567c478bd9Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_DEAD) { 21577c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 21587c478bd9Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 21597c478bd9Sstevel@tonic-gate list_remove(&zone_deathrow, zone); 21607c478bd9Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 21617c478bd9Sstevel@tonic-gate } 21627c478bd9Sstevel@tonic-gate 21637c478bd9Sstevel@tonic-gate zone_free_zsd(zone); 2164fa9e4066Sahrens zone_free_datasets(zone); 21652b24ab6bSSebastien Roy list_destroy(&zone->zone_dl_list); 21667c478bd9Sstevel@tonic-gate 21677c478bd9Sstevel@tonic-gate if (zone->zone_rootvp != NULL) 21687c478bd9Sstevel@tonic-gate VN_RELE(zone->zone_rootvp); 21697c478bd9Sstevel@tonic-gate if (zone->zone_rootpath) 21707c478bd9Sstevel@tonic-gate kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 21717c478bd9Sstevel@tonic-gate if (zone->zone_name != NULL) 21727c478bd9Sstevel@tonic-gate kmem_free(zone->zone_name, ZONENAME_MAX); 217345916cd2Sjpk if (zone->zone_slabel != NULL) 217445916cd2Sjpk label_rele(zone->zone_slabel); 21757c478bd9Sstevel@tonic-gate if (zone->zone_nodename != NULL) 21767c478bd9Sstevel@tonic-gate kmem_free(zone->zone_nodename, _SYS_NMLN); 21777c478bd9Sstevel@tonic-gate if (zone->zone_domain != NULL) 21787c478bd9Sstevel@tonic-gate kmem_free(zone->zone_domain, _SYS_NMLN); 21797c478bd9Sstevel@tonic-gate if (zone->zone_privset != NULL) 21807c478bd9Sstevel@tonic-gate kmem_free(zone->zone_privset, sizeof (priv_set_t)); 21817c478bd9Sstevel@tonic-gate if (zone->zone_rctls != NULL) 21827c478bd9Sstevel@tonic-gate rctl_set_free(zone->zone_rctls); 21837c478bd9Sstevel@tonic-gate if (zone->zone_bootargs != NULL) 21840fbb751dSJohn Levon strfree(zone->zone_bootargs); 21853f2f09c1Sdp if (zone->zone_initname != NULL) 21860fbb751dSJohn Levon strfree(zone->zone_initname); 21870fbb751dSJohn Levon if (zone->zone_fs_allowed != NULL) 21880fbb751dSJohn Levon strfree(zone->zone_fs_allowed); 2189134a1f4eSCasper H.S. Dik if (zone->zone_pfexecd != NULL) 2190134a1f4eSCasper H.S. Dik klpd_freelist(&zone->zone_pfexecd); 21917c478bd9Sstevel@tonic-gate id_free(zoneid_space, zone->zone_id); 21927c478bd9Sstevel@tonic-gate mutex_destroy(&zone->zone_lock); 21937c478bd9Sstevel@tonic-gate cv_destroy(&zone->zone_cv); 219445916cd2Sjpk rw_destroy(&zone->zone_mlps.mlpl_rwlock); 2195835ee219SRobert Harris rw_destroy(&zone->zone_mntfs_db_lock); 21967c478bd9Sstevel@tonic-gate kmem_free(zone, sizeof (zone_t)); 21977c478bd9Sstevel@tonic-gate } 21987c478bd9Sstevel@tonic-gate 21997c478bd9Sstevel@tonic-gate /* 22007c478bd9Sstevel@tonic-gate * See block comment at the top of this file for information about zone 22017c478bd9Sstevel@tonic-gate * status values. 22027c478bd9Sstevel@tonic-gate */ 22037c478bd9Sstevel@tonic-gate /* 22047c478bd9Sstevel@tonic-gate * Convenience function for setting zone status. 22057c478bd9Sstevel@tonic-gate */ 22067c478bd9Sstevel@tonic-gate static void 22077c478bd9Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status) 22087c478bd9Sstevel@tonic-gate { 2209cf8f45c7Sdstaff 2210cf8f45c7Sdstaff nvlist_t *nvl = NULL; 22117c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zone_status_lock)); 22127c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 22137c478bd9Sstevel@tonic-gate status >= zone_status_get(zone)); 2214cf8f45c7Sdstaff 2215cf8f45c7Sdstaff if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) || 2216cf8f45c7Sdstaff nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) || 2217cf8f45c7Sdstaff nvlist_add_string(nvl, ZONE_CB_NEWSTATE, 2218cf8f45c7Sdstaff zone_status_table[status]) || 2219cf8f45c7Sdstaff nvlist_add_string(nvl, ZONE_CB_OLDSTATE, 2220cf8f45c7Sdstaff zone_status_table[zone->zone_status]) || 2221cf8f45c7Sdstaff nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) || 2222cf8f45c7Sdstaff nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) || 2223cf8f45c7Sdstaff sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS, 22243f2f09c1Sdp ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) { 2225cf8f45c7Sdstaff #ifdef DEBUG 2226cf8f45c7Sdstaff (void) printf( 2227cf8f45c7Sdstaff "Failed to allocate and send zone state change event.\n"); 2228cf8f45c7Sdstaff #endif 2229cf8f45c7Sdstaff } 2230cf8f45c7Sdstaff nvlist_free(nvl); 2231cf8f45c7Sdstaff 22327c478bd9Sstevel@tonic-gate zone->zone_status = status; 2233cf8f45c7Sdstaff 22347c478bd9Sstevel@tonic-gate cv_broadcast(&zone->zone_cv); 22357c478bd9Sstevel@tonic-gate } 22367c478bd9Sstevel@tonic-gate 22377c478bd9Sstevel@tonic-gate /* 22387c478bd9Sstevel@tonic-gate * Public function to retrieve the zone status. The zone status may 22397c478bd9Sstevel@tonic-gate * change after it is retrieved. 22407c478bd9Sstevel@tonic-gate */ 22417c478bd9Sstevel@tonic-gate zone_status_t 22427c478bd9Sstevel@tonic-gate zone_status_get(zone_t *zone) 22437c478bd9Sstevel@tonic-gate { 22447c478bd9Sstevel@tonic-gate return (zone->zone_status); 22457c478bd9Sstevel@tonic-gate } 22467c478bd9Sstevel@tonic-gate 22477c478bd9Sstevel@tonic-gate static int 22487c478bd9Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 22497c478bd9Sstevel@tonic-gate { 22500fbb751dSJohn Levon char *buf = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP); 22513f2f09c1Sdp int err = 0; 22527c478bd9Sstevel@tonic-gate 22533f2f09c1Sdp ASSERT(zone != global_zone); 22540fbb751dSJohn Levon if ((err = copyinstr(zone_bootargs, buf, BOOTARGS_MAX, NULL)) != 0) 22553f2f09c1Sdp goto done; /* EFAULT or ENAMETOOLONG */ 22563f2f09c1Sdp 22573f2f09c1Sdp if (zone->zone_bootargs != NULL) 22580fbb751dSJohn Levon strfree(zone->zone_bootargs); 22593f2f09c1Sdp 22600fbb751dSJohn Levon zone->zone_bootargs = strdup(buf); 22613f2f09c1Sdp 22623f2f09c1Sdp done: 22630fbb751dSJohn Levon kmem_free(buf, BOOTARGS_MAX); 22643f2f09c1Sdp return (err); 22657c478bd9Sstevel@tonic-gate } 22667c478bd9Sstevel@tonic-gate 22673f2f09c1Sdp static int 226859f2ff5cSedp zone_set_brand(zone_t *zone, const char *brand) 226959f2ff5cSedp { 227059f2ff5cSedp struct brand_attr *attrp; 227159f2ff5cSedp brand_t *bp; 227259f2ff5cSedp 227359f2ff5cSedp attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP); 227459f2ff5cSedp if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) { 227559f2ff5cSedp kmem_free(attrp, sizeof (struct brand_attr)); 227659f2ff5cSedp return (EFAULT); 227759f2ff5cSedp } 227859f2ff5cSedp 227959f2ff5cSedp bp = brand_register_zone(attrp); 228059f2ff5cSedp kmem_free(attrp, sizeof (struct brand_attr)); 228159f2ff5cSedp if (bp == NULL) 228259f2ff5cSedp return (EINVAL); 228359f2ff5cSedp 228459f2ff5cSedp /* 228559f2ff5cSedp * This is the only place where a zone can change it's brand. 228659f2ff5cSedp * We already need to hold zone_status_lock to check the zone 228759f2ff5cSedp * status, so we'll just use that lock to serialize zone 228859f2ff5cSedp * branding requests as well. 228959f2ff5cSedp */ 229059f2ff5cSedp mutex_enter(&zone_status_lock); 229159f2ff5cSedp 229259f2ff5cSedp /* Re-Branding is not allowed and the zone can't be booted yet */ 229359f2ff5cSedp if ((ZONE_IS_BRANDED(zone)) || 229459f2ff5cSedp (zone_status_get(zone) >= ZONE_IS_BOOTING)) { 229559f2ff5cSedp mutex_exit(&zone_status_lock); 229659f2ff5cSedp brand_unregister_zone(bp); 229759f2ff5cSedp return (EINVAL); 229859f2ff5cSedp } 229959f2ff5cSedp 2300319378d9Seh208807 /* set up the brand specific data */ 230159f2ff5cSedp zone->zone_brand = bp; 2302319378d9Seh208807 ZBROP(zone)->b_init_brand_data(zone); 2303319378d9Seh208807 230459f2ff5cSedp mutex_exit(&zone_status_lock); 230559f2ff5cSedp return (0); 230659f2ff5cSedp } 230759f2ff5cSedp 230859f2ff5cSedp static int 23090fbb751dSJohn Levon zone_set_fs_allowed(zone_t *zone, const char *zone_fs_allowed) 23100fbb751dSJohn Levon { 23110fbb751dSJohn Levon char *buf = kmem_zalloc(ZONE_FS_ALLOWED_MAX, KM_SLEEP); 23120fbb751dSJohn Levon int err = 0; 23130fbb751dSJohn Levon 23140fbb751dSJohn Levon ASSERT(zone != global_zone); 23150fbb751dSJohn Levon if ((err = copyinstr(zone_fs_allowed, buf, 23160fbb751dSJohn Levon ZONE_FS_ALLOWED_MAX, NULL)) != 0) 23170fbb751dSJohn Levon goto done; 23180fbb751dSJohn Levon 23190fbb751dSJohn Levon if (zone->zone_fs_allowed != NULL) 23200fbb751dSJohn Levon strfree(zone->zone_fs_allowed); 23210fbb751dSJohn Levon 23220fbb751dSJohn Levon zone->zone_fs_allowed = strdup(buf); 23230fbb751dSJohn Levon 23240fbb751dSJohn Levon done: 23250fbb751dSJohn Levon kmem_free(buf, ZONE_FS_ALLOWED_MAX); 23260fbb751dSJohn Levon return (err); 23270fbb751dSJohn Levon } 23280fbb751dSJohn Levon 23290fbb751dSJohn Levon static int 23303f2f09c1Sdp zone_set_initname(zone_t *zone, const char *zone_initname) 23313f2f09c1Sdp { 23323f2f09c1Sdp char initname[INITNAME_SZ]; 23333f2f09c1Sdp size_t len; 23343f2f09c1Sdp int err = 0; 23353f2f09c1Sdp 23363f2f09c1Sdp ASSERT(zone != global_zone); 23373f2f09c1Sdp if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0) 23383f2f09c1Sdp return (err); /* EFAULT or ENAMETOOLONG */ 23393f2f09c1Sdp 23403f2f09c1Sdp if (zone->zone_initname != NULL) 23410fbb751dSJohn Levon strfree(zone->zone_initname); 23423f2f09c1Sdp 23433f2f09c1Sdp zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP); 23443f2f09c1Sdp (void) strcpy(zone->zone_initname, initname); 23457c478bd9Sstevel@tonic-gate return (0); 23467c478bd9Sstevel@tonic-gate } 23477c478bd9Sstevel@tonic-gate 23480209230bSgjelinek static int 23490209230bSgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap) 23500209230bSgjelinek { 23510209230bSgjelinek uint64_t mcap; 23520209230bSgjelinek int err = 0; 23530209230bSgjelinek 23540209230bSgjelinek if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0) 23550209230bSgjelinek zone->zone_phys_mcap = mcap; 23560209230bSgjelinek 23570209230bSgjelinek return (err); 23580209230bSgjelinek } 23590209230bSgjelinek 23600209230bSgjelinek static int 23610209230bSgjelinek zone_set_sched_class(zone_t *zone, const char *new_class) 23620209230bSgjelinek { 23630209230bSgjelinek char sched_class[PC_CLNMSZ]; 23640209230bSgjelinek id_t classid; 23650209230bSgjelinek int err; 23660209230bSgjelinek 23670209230bSgjelinek ASSERT(zone != global_zone); 23680209230bSgjelinek if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0) 23690209230bSgjelinek return (err); /* EFAULT or ENAMETOOLONG */ 23700209230bSgjelinek 237135a5a358SJonathan Adams if (getcid(sched_class, &classid) != 0 || CLASS_KERNEL(classid)) 23720209230bSgjelinek return (set_errno(EINVAL)); 23730209230bSgjelinek zone->zone_defaultcid = classid; 23740209230bSgjelinek ASSERT(zone->zone_defaultcid > 0 && 23750209230bSgjelinek zone->zone_defaultcid < loaded_classes); 23760209230bSgjelinek 23770209230bSgjelinek return (0); 23780209230bSgjelinek } 23790209230bSgjelinek 23807c478bd9Sstevel@tonic-gate /* 23817c478bd9Sstevel@tonic-gate * Block indefinitely waiting for (zone_status >= status) 23827c478bd9Sstevel@tonic-gate */ 23837c478bd9Sstevel@tonic-gate void 23847c478bd9Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status) 23857c478bd9Sstevel@tonic-gate { 23867c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 23877c478bd9Sstevel@tonic-gate 23887c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 23897c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 23907c478bd9Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 23917c478bd9Sstevel@tonic-gate } 23927c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 23937c478bd9Sstevel@tonic-gate } 23947c478bd9Sstevel@tonic-gate 23957c478bd9Sstevel@tonic-gate /* 23967c478bd9Sstevel@tonic-gate * Private CPR-safe version of zone_status_wait(). 23977c478bd9Sstevel@tonic-gate */ 23987c478bd9Sstevel@tonic-gate static void 23997c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 24007c478bd9Sstevel@tonic-gate { 24017c478bd9Sstevel@tonic-gate callb_cpr_t cprinfo; 24027c478bd9Sstevel@tonic-gate 24037c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 24047c478bd9Sstevel@tonic-gate 24057c478bd9Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 24067c478bd9Sstevel@tonic-gate str); 24077c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24087c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 24097c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 24107c478bd9Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 24117c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 24127c478bd9Sstevel@tonic-gate } 24137c478bd9Sstevel@tonic-gate /* 24147c478bd9Sstevel@tonic-gate * zone_status_lock is implicitly released by the following. 24157c478bd9Sstevel@tonic-gate */ 24167c478bd9Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 24177c478bd9Sstevel@tonic-gate } 24187c478bd9Sstevel@tonic-gate 24197c478bd9Sstevel@tonic-gate /* 24207c478bd9Sstevel@tonic-gate * Block until zone enters requested state or signal is received. Return (0) 24217c478bd9Sstevel@tonic-gate * if signaled, non-zero otherwise. 24227c478bd9Sstevel@tonic-gate */ 24237c478bd9Sstevel@tonic-gate int 24247c478bd9Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status) 24257c478bd9Sstevel@tonic-gate { 24267c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 24277c478bd9Sstevel@tonic-gate 24287c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24297c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 24307c478bd9Sstevel@tonic-gate if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 24317c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 24327c478bd9Sstevel@tonic-gate return (0); 24337c478bd9Sstevel@tonic-gate } 24347c478bd9Sstevel@tonic-gate } 24357c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 24367c478bd9Sstevel@tonic-gate return (1); 24377c478bd9Sstevel@tonic-gate } 24387c478bd9Sstevel@tonic-gate 24397c478bd9Sstevel@tonic-gate /* 24407c478bd9Sstevel@tonic-gate * Block until the zone enters the requested state or the timeout expires, 24417c478bd9Sstevel@tonic-gate * whichever happens first. Return (-1) if operation timed out, time remaining 24427c478bd9Sstevel@tonic-gate * otherwise. 24437c478bd9Sstevel@tonic-gate */ 24447c478bd9Sstevel@tonic-gate clock_t 24457c478bd9Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 24467c478bd9Sstevel@tonic-gate { 24477c478bd9Sstevel@tonic-gate clock_t timeleft = 0; 24487c478bd9Sstevel@tonic-gate 24497c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 24507c478bd9Sstevel@tonic-gate 24517c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24527c478bd9Sstevel@tonic-gate while (zone->zone_status < status && timeleft != -1) { 24537c478bd9Sstevel@tonic-gate timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 24547c478bd9Sstevel@tonic-gate } 24557c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 24567c478bd9Sstevel@tonic-gate return (timeleft); 24577c478bd9Sstevel@tonic-gate } 24587c478bd9Sstevel@tonic-gate 24597c478bd9Sstevel@tonic-gate /* 24607c478bd9Sstevel@tonic-gate * Block until the zone enters the requested state, the current process is 24617c478bd9Sstevel@tonic-gate * signaled, or the timeout expires, whichever happens first. Return (-1) if 24627c478bd9Sstevel@tonic-gate * operation timed out, 0 if signaled, time remaining otherwise. 24637c478bd9Sstevel@tonic-gate */ 24647c478bd9Sstevel@tonic-gate clock_t 24657c478bd9Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 24667c478bd9Sstevel@tonic-gate { 2467d3d50737SRafael Vanoni clock_t timeleft = tim - ddi_get_lbolt(); 24687c478bd9Sstevel@tonic-gate 24697c478bd9Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 24707c478bd9Sstevel@tonic-gate 24717c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24727c478bd9Sstevel@tonic-gate while (zone->zone_status < status) { 24737c478bd9Sstevel@tonic-gate timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 24747c478bd9Sstevel@tonic-gate tim); 24757c478bd9Sstevel@tonic-gate if (timeleft <= 0) 24767c478bd9Sstevel@tonic-gate break; 24777c478bd9Sstevel@tonic-gate } 24787c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 24797c478bd9Sstevel@tonic-gate return (timeleft); 24807c478bd9Sstevel@tonic-gate } 24817c478bd9Sstevel@tonic-gate 24827c478bd9Sstevel@tonic-gate /* 24837c478bd9Sstevel@tonic-gate * Zones have two reference counts: one for references from credential 24847c478bd9Sstevel@tonic-gate * structures (zone_cred_ref), and one (zone_ref) for everything else. 24857c478bd9Sstevel@tonic-gate * This is so we can allow a zone to be rebooted while there are still 24867c478bd9Sstevel@tonic-gate * outstanding cred references, since certain drivers cache dblks (which 24877c478bd9Sstevel@tonic-gate * implicitly results in cached creds). We wait for zone_ref to drop to 24887c478bd9Sstevel@tonic-gate * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 24897c478bd9Sstevel@tonic-gate * later freed when the zone_cred_ref drops to 0, though nothing other 24907c478bd9Sstevel@tonic-gate * than the zone id and privilege set should be accessed once the zone 24917c478bd9Sstevel@tonic-gate * is "dead". 24927c478bd9Sstevel@tonic-gate * 24937c478bd9Sstevel@tonic-gate * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 24947c478bd9Sstevel@tonic-gate * to force halt/reboot to block waiting for the zone_cred_ref to drop 24957c478bd9Sstevel@tonic-gate * to 0. This can be useful to flush out other sources of cached creds 24967c478bd9Sstevel@tonic-gate * that may be less innocuous than the driver case. 24977c478bd9Sstevel@tonic-gate */ 24987c478bd9Sstevel@tonic-gate 24997c478bd9Sstevel@tonic-gate int zone_wait_for_cred = 0; 25007c478bd9Sstevel@tonic-gate 25017c478bd9Sstevel@tonic-gate static void 25027c478bd9Sstevel@tonic-gate zone_hold_locked(zone_t *z) 25037c478bd9Sstevel@tonic-gate { 25047c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&z->zone_lock)); 25057c478bd9Sstevel@tonic-gate z->zone_ref++; 25067c478bd9Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 25077c478bd9Sstevel@tonic-gate } 25087c478bd9Sstevel@tonic-gate 25097c478bd9Sstevel@tonic-gate void 25107c478bd9Sstevel@tonic-gate zone_hold(zone_t *z) 25117c478bd9Sstevel@tonic-gate { 25127c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 25137c478bd9Sstevel@tonic-gate zone_hold_locked(z); 25147c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 25157c478bd9Sstevel@tonic-gate } 25167c478bd9Sstevel@tonic-gate 25177c478bd9Sstevel@tonic-gate /* 25187c478bd9Sstevel@tonic-gate * If the non-cred ref count drops to 1 and either the cred ref count 25197c478bd9Sstevel@tonic-gate * is 0 or we aren't waiting for cred references, the zone is ready to 25207c478bd9Sstevel@tonic-gate * be destroyed. 25217c478bd9Sstevel@tonic-gate */ 25227c478bd9Sstevel@tonic-gate #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 25237c478bd9Sstevel@tonic-gate (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 25247c478bd9Sstevel@tonic-gate 25257c478bd9Sstevel@tonic-gate void 25267c478bd9Sstevel@tonic-gate zone_rele(zone_t *z) 25277c478bd9Sstevel@tonic-gate { 25287c478bd9Sstevel@tonic-gate boolean_t wakeup; 25297c478bd9Sstevel@tonic-gate 25307c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 25317c478bd9Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 25327c478bd9Sstevel@tonic-gate z->zone_ref--; 25337c478bd9Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 25347c478bd9Sstevel@tonic-gate /* no more refs, free the structure */ 25357c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 25367c478bd9Sstevel@tonic-gate zone_free(z); 25377c478bd9Sstevel@tonic-gate return; 25387c478bd9Sstevel@tonic-gate } 25397c478bd9Sstevel@tonic-gate /* signal zone_destroy so the zone can finish halting */ 25407c478bd9Sstevel@tonic-gate wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 25417c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 25427c478bd9Sstevel@tonic-gate 25437c478bd9Sstevel@tonic-gate if (wakeup) { 25447c478bd9Sstevel@tonic-gate /* 25457c478bd9Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 25467c478bd9Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 25477c478bd9Sstevel@tonic-gate */ 25487c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 25497c478bd9Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 25507c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 25517c478bd9Sstevel@tonic-gate } 25527c478bd9Sstevel@tonic-gate } 25537c478bd9Sstevel@tonic-gate 25547c478bd9Sstevel@tonic-gate void 25557c478bd9Sstevel@tonic-gate zone_cred_hold(zone_t *z) 25567c478bd9Sstevel@tonic-gate { 25577c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 25587c478bd9Sstevel@tonic-gate z->zone_cred_ref++; 25597c478bd9Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 25607c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 25617c478bd9Sstevel@tonic-gate } 25627c478bd9Sstevel@tonic-gate 25637c478bd9Sstevel@tonic-gate void 25647c478bd9Sstevel@tonic-gate zone_cred_rele(zone_t *z) 25657c478bd9Sstevel@tonic-gate { 25667c478bd9Sstevel@tonic-gate boolean_t wakeup; 25677c478bd9Sstevel@tonic-gate 25687c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 25697c478bd9Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 25707c478bd9Sstevel@tonic-gate z->zone_cred_ref--; 25717c478bd9Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 25727c478bd9Sstevel@tonic-gate /* no more refs, free the structure */ 25737c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 25747c478bd9Sstevel@tonic-gate zone_free(z); 25757c478bd9Sstevel@tonic-gate return; 25767c478bd9Sstevel@tonic-gate } 25777c478bd9Sstevel@tonic-gate /* 25787c478bd9Sstevel@tonic-gate * If zone_destroy is waiting for the cred references to drain 25797c478bd9Sstevel@tonic-gate * out, and they have, signal it. 25807c478bd9Sstevel@tonic-gate */ 25817c478bd9Sstevel@tonic-gate wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 25827c478bd9Sstevel@tonic-gate zone_status_get(z) >= ZONE_IS_DEAD); 25837c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 25847c478bd9Sstevel@tonic-gate 25857c478bd9Sstevel@tonic-gate if (wakeup) { 25867c478bd9Sstevel@tonic-gate /* 25877c478bd9Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 25887c478bd9Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 25897c478bd9Sstevel@tonic-gate */ 25907c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 25917c478bd9Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 25927c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 25937c478bd9Sstevel@tonic-gate } 25947c478bd9Sstevel@tonic-gate } 25957c478bd9Sstevel@tonic-gate 25967c478bd9Sstevel@tonic-gate void 25977c478bd9Sstevel@tonic-gate zone_task_hold(zone_t *z) 25987c478bd9Sstevel@tonic-gate { 25997c478bd9Sstevel@tonic-gate mutex_enter(&z->zone_lock); 26007c478bd9Sstevel@tonic-gate z->zone_ntasks++; 26017c478bd9Sstevel@tonic-gate ASSERT(z->zone_ntasks != 0); 26027c478bd9Sstevel@tonic-gate mutex_exit(&z->zone_lock); 26037c478bd9Sstevel@tonic-gate } 26047c478bd9Sstevel@tonic-gate 26057c478bd9Sstevel@tonic-gate void 26067c478bd9Sstevel@tonic-gate zone_task_rele(zone_t *zone) 26077c478bd9Sstevel@tonic-gate { 26087c478bd9Sstevel@tonic-gate uint_t refcnt; 26097c478bd9Sstevel@tonic-gate 26107c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 26117c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ntasks != 0); 26127c478bd9Sstevel@tonic-gate refcnt = --zone->zone_ntasks; 26137c478bd9Sstevel@tonic-gate if (refcnt > 1) { /* Common case */ 26147c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 26157c478bd9Sstevel@tonic-gate return; 26167c478bd9Sstevel@tonic-gate } 26177c478bd9Sstevel@tonic-gate zone_hold_locked(zone); /* so we can use the zone_t later */ 26187c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 26197c478bd9Sstevel@tonic-gate if (refcnt == 1) { 26207c478bd9Sstevel@tonic-gate /* 26217c478bd9Sstevel@tonic-gate * See if the zone is shutting down. 26227c478bd9Sstevel@tonic-gate */ 26237c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 26247c478bd9Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 26257c478bd9Sstevel@tonic-gate goto out; 26267c478bd9Sstevel@tonic-gate } 26277c478bd9Sstevel@tonic-gate 26287c478bd9Sstevel@tonic-gate /* 26297c478bd9Sstevel@tonic-gate * Make sure the ntasks didn't change since we 26307c478bd9Sstevel@tonic-gate * dropped zone_lock. 26317c478bd9Sstevel@tonic-gate */ 26327c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 26337c478bd9Sstevel@tonic-gate if (refcnt != zone->zone_ntasks) { 26347c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 26357c478bd9Sstevel@tonic-gate goto out; 26367c478bd9Sstevel@tonic-gate } 26377c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 26387c478bd9Sstevel@tonic-gate 26397c478bd9Sstevel@tonic-gate /* 26407c478bd9Sstevel@tonic-gate * No more user processes in the zone. The zone is empty. 26417c478bd9Sstevel@tonic-gate */ 26427c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 26437c478bd9Sstevel@tonic-gate goto out; 26447c478bd9Sstevel@tonic-gate } 26457c478bd9Sstevel@tonic-gate 26467c478bd9Sstevel@tonic-gate ASSERT(refcnt == 0); 26477c478bd9Sstevel@tonic-gate /* 26487c478bd9Sstevel@tonic-gate * zsched has exited; the zone is dead. 26497c478bd9Sstevel@tonic-gate */ 26507c478bd9Sstevel@tonic-gate zone->zone_zsched = NULL; /* paranoia */ 26517c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 26527c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DEAD); 26537c478bd9Sstevel@tonic-gate out: 26547c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 26557c478bd9Sstevel@tonic-gate zone_rele(zone); 26567c478bd9Sstevel@tonic-gate } 26577c478bd9Sstevel@tonic-gate 26587c478bd9Sstevel@tonic-gate zoneid_t 26597c478bd9Sstevel@tonic-gate getzoneid(void) 26607c478bd9Sstevel@tonic-gate { 26617c478bd9Sstevel@tonic-gate return (curproc->p_zone->zone_id); 26627c478bd9Sstevel@tonic-gate } 26637c478bd9Sstevel@tonic-gate 26647c478bd9Sstevel@tonic-gate /* 26657c478bd9Sstevel@tonic-gate * Internal versions of zone_find_by_*(). These don't zone_hold() or 26667c478bd9Sstevel@tonic-gate * check the validity of a zone's state. 26677c478bd9Sstevel@tonic-gate */ 26687c478bd9Sstevel@tonic-gate static zone_t * 26697c478bd9Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid) 26707c478bd9Sstevel@tonic-gate { 26717c478bd9Sstevel@tonic-gate mod_hash_val_t hv; 26727c478bd9Sstevel@tonic-gate zone_t *zone = NULL; 26737c478bd9Sstevel@tonic-gate 26747c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 26757c478bd9Sstevel@tonic-gate 26767c478bd9Sstevel@tonic-gate if (mod_hash_find(zonehashbyid, 26777c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 26787c478bd9Sstevel@tonic-gate zone = (zone_t *)hv; 26797c478bd9Sstevel@tonic-gate return (zone); 26807c478bd9Sstevel@tonic-gate } 26817c478bd9Sstevel@tonic-gate 26827c478bd9Sstevel@tonic-gate static zone_t * 268345916cd2Sjpk zone_find_all_by_label(const ts_label_t *label) 268445916cd2Sjpk { 268545916cd2Sjpk mod_hash_val_t hv; 268645916cd2Sjpk zone_t *zone = NULL; 268745916cd2Sjpk 268845916cd2Sjpk ASSERT(MUTEX_HELD(&zonehash_lock)); 268945916cd2Sjpk 269045916cd2Sjpk /* 269145916cd2Sjpk * zonehashbylabel is not maintained for unlabeled systems 269245916cd2Sjpk */ 269345916cd2Sjpk if (!is_system_labeled()) 269445916cd2Sjpk return (NULL); 269545916cd2Sjpk if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0) 269645916cd2Sjpk zone = (zone_t *)hv; 269745916cd2Sjpk return (zone); 269845916cd2Sjpk } 269945916cd2Sjpk 270045916cd2Sjpk static zone_t * 27017c478bd9Sstevel@tonic-gate zone_find_all_by_name(char *name) 27027c478bd9Sstevel@tonic-gate { 27037c478bd9Sstevel@tonic-gate mod_hash_val_t hv; 27047c478bd9Sstevel@tonic-gate zone_t *zone = NULL; 27057c478bd9Sstevel@tonic-gate 27067c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 27077c478bd9Sstevel@tonic-gate 27087c478bd9Sstevel@tonic-gate if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 27097c478bd9Sstevel@tonic-gate zone = (zone_t *)hv; 27107c478bd9Sstevel@tonic-gate return (zone); 27117c478bd9Sstevel@tonic-gate } 27127c478bd9Sstevel@tonic-gate 27137c478bd9Sstevel@tonic-gate /* 27147c478bd9Sstevel@tonic-gate * Public interface for looking up a zone by zoneid. Only returns the zone if 27157c478bd9Sstevel@tonic-gate * it is fully initialized, and has not yet begun the zone_destroy() sequence. 27167c478bd9Sstevel@tonic-gate * Caller must call zone_rele() once it is done with the zone. 27177c478bd9Sstevel@tonic-gate * 27187c478bd9Sstevel@tonic-gate * The zone may begin the zone_destroy() sequence immediately after this 27197c478bd9Sstevel@tonic-gate * function returns, but may be safely used until zone_rele() is called. 27207c478bd9Sstevel@tonic-gate */ 27217c478bd9Sstevel@tonic-gate zone_t * 27227c478bd9Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid) 27237c478bd9Sstevel@tonic-gate { 27247c478bd9Sstevel@tonic-gate zone_t *zone; 27257c478bd9Sstevel@tonic-gate zone_status_t status; 27267c478bd9Sstevel@tonic-gate 27277c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 27287c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 27297c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 27307c478bd9Sstevel@tonic-gate return (NULL); 27317c478bd9Sstevel@tonic-gate } 27327c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 27337c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 27347c478bd9Sstevel@tonic-gate /* 27357c478bd9Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 27367c478bd9Sstevel@tonic-gate */ 27377c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 27387c478bd9Sstevel@tonic-gate return (NULL); 27397c478bd9Sstevel@tonic-gate } 27407c478bd9Sstevel@tonic-gate zone_hold(zone); 27417c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 27427c478bd9Sstevel@tonic-gate return (zone); 27437c478bd9Sstevel@tonic-gate } 27447c478bd9Sstevel@tonic-gate 27457c478bd9Sstevel@tonic-gate /* 274645916cd2Sjpk * Similar to zone_find_by_id, but using zone label as the key. 274745916cd2Sjpk */ 274845916cd2Sjpk zone_t * 274945916cd2Sjpk zone_find_by_label(const ts_label_t *label) 275045916cd2Sjpk { 275145916cd2Sjpk zone_t *zone; 275242bc57c4Srica zone_status_t status; 275345916cd2Sjpk 275445916cd2Sjpk mutex_enter(&zonehash_lock); 275545916cd2Sjpk if ((zone = zone_find_all_by_label(label)) == NULL) { 275645916cd2Sjpk mutex_exit(&zonehash_lock); 275745916cd2Sjpk return (NULL); 275845916cd2Sjpk } 275942bc57c4Srica 276042bc57c4Srica status = zone_status_get(zone); 276142bc57c4Srica if (status > ZONE_IS_DOWN) { 276245916cd2Sjpk /* 276345916cd2Sjpk * For all practical purposes the zone doesn't exist. 276445916cd2Sjpk */ 276542bc57c4Srica mutex_exit(&zonehash_lock); 276642bc57c4Srica return (NULL); 276745916cd2Sjpk } 276842bc57c4Srica zone_hold(zone); 276945916cd2Sjpk mutex_exit(&zonehash_lock); 277045916cd2Sjpk return (zone); 277145916cd2Sjpk } 277245916cd2Sjpk 277345916cd2Sjpk /* 27747c478bd9Sstevel@tonic-gate * Similar to zone_find_by_id, but using zone name as the key. 27757c478bd9Sstevel@tonic-gate */ 27767c478bd9Sstevel@tonic-gate zone_t * 27777c478bd9Sstevel@tonic-gate zone_find_by_name(char *name) 27787c478bd9Sstevel@tonic-gate { 27797c478bd9Sstevel@tonic-gate zone_t *zone; 27807c478bd9Sstevel@tonic-gate zone_status_t status; 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 27837c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_name(name)) == NULL) { 27847c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 27857c478bd9Sstevel@tonic-gate return (NULL); 27867c478bd9Sstevel@tonic-gate } 27877c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 27887c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 27897c478bd9Sstevel@tonic-gate /* 27907c478bd9Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 27917c478bd9Sstevel@tonic-gate */ 27927c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 27937c478bd9Sstevel@tonic-gate return (NULL); 27947c478bd9Sstevel@tonic-gate } 27957c478bd9Sstevel@tonic-gate zone_hold(zone); 27967c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 27977c478bd9Sstevel@tonic-gate return (zone); 27987c478bd9Sstevel@tonic-gate } 27997c478bd9Sstevel@tonic-gate 28007c478bd9Sstevel@tonic-gate /* 28017c478bd9Sstevel@tonic-gate * Similar to zone_find_by_id(), using the path as a key. For instance, 28027c478bd9Sstevel@tonic-gate * if there is a zone "foo" rooted at /foo/root, and the path argument 28037c478bd9Sstevel@tonic-gate * is "/foo/root/proc", it will return the held zone_t corresponding to 28047c478bd9Sstevel@tonic-gate * zone "foo". 28057c478bd9Sstevel@tonic-gate * 28067c478bd9Sstevel@tonic-gate * zone_find_by_path() always returns a non-NULL value, since at the 28077c478bd9Sstevel@tonic-gate * very least every path will be contained in the global zone. 28087c478bd9Sstevel@tonic-gate * 28097c478bd9Sstevel@tonic-gate * As with the other zone_find_by_*() functions, the caller is 28107c478bd9Sstevel@tonic-gate * responsible for zone_rele()ing the return value of this function. 28117c478bd9Sstevel@tonic-gate */ 28127c478bd9Sstevel@tonic-gate zone_t * 28137c478bd9Sstevel@tonic-gate zone_find_by_path(const char *path) 28147c478bd9Sstevel@tonic-gate { 28157c478bd9Sstevel@tonic-gate zone_t *zone; 28167c478bd9Sstevel@tonic-gate zone_t *zret = NULL; 28177c478bd9Sstevel@tonic-gate zone_status_t status; 28187c478bd9Sstevel@tonic-gate 28197c478bd9Sstevel@tonic-gate if (path == NULL) { 28207c478bd9Sstevel@tonic-gate /* 28217c478bd9Sstevel@tonic-gate * Call from rootconf(). 28227c478bd9Sstevel@tonic-gate */ 28237c478bd9Sstevel@tonic-gate zone_hold(global_zone); 28247c478bd9Sstevel@tonic-gate return (global_zone); 28257c478bd9Sstevel@tonic-gate } 28267c478bd9Sstevel@tonic-gate ASSERT(*path == '/'); 28277c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 28287c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 28297c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 28307c478bd9Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(path, zone)) 28317c478bd9Sstevel@tonic-gate zret = zone; 28327c478bd9Sstevel@tonic-gate } 28337c478bd9Sstevel@tonic-gate ASSERT(zret != NULL); 28347c478bd9Sstevel@tonic-gate status = zone_status_get(zret); 28357c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 28367c478bd9Sstevel@tonic-gate /* 28377c478bd9Sstevel@tonic-gate * Zone practically doesn't exist. 28387c478bd9Sstevel@tonic-gate */ 28397c478bd9Sstevel@tonic-gate zret = global_zone; 28407c478bd9Sstevel@tonic-gate } 28417c478bd9Sstevel@tonic-gate zone_hold(zret); 28427c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 28437c478bd9Sstevel@tonic-gate return (zret); 28447c478bd9Sstevel@tonic-gate } 28457c478bd9Sstevel@tonic-gate 28467c478bd9Sstevel@tonic-gate /* 28477c478bd9Sstevel@tonic-gate * Get the number of cpus visible to this zone. The system-wide global 28487c478bd9Sstevel@tonic-gate * 'ncpus' is returned if pools are disabled, the caller is in the 28497c478bd9Sstevel@tonic-gate * global zone, or a NULL zone argument is passed in. 28507c478bd9Sstevel@tonic-gate */ 28517c478bd9Sstevel@tonic-gate int 28527c478bd9Sstevel@tonic-gate zone_ncpus_get(zone_t *zone) 28537c478bd9Sstevel@tonic-gate { 28547c478bd9Sstevel@tonic-gate int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 28557c478bd9Sstevel@tonic-gate 28567c478bd9Sstevel@tonic-gate return (myncpus != 0 ? myncpus : ncpus); 28577c478bd9Sstevel@tonic-gate } 28587c478bd9Sstevel@tonic-gate 28597c478bd9Sstevel@tonic-gate /* 28607c478bd9Sstevel@tonic-gate * Get the number of online cpus visible to this zone. The system-wide 28617c478bd9Sstevel@tonic-gate * global 'ncpus_online' is returned if pools are disabled, the caller 28627c478bd9Sstevel@tonic-gate * is in the global zone, or a NULL zone argument is passed in. 28637c478bd9Sstevel@tonic-gate */ 28647c478bd9Sstevel@tonic-gate int 28657c478bd9Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone) 28667c478bd9Sstevel@tonic-gate { 28677c478bd9Sstevel@tonic-gate int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 28687c478bd9Sstevel@tonic-gate 28697c478bd9Sstevel@tonic-gate return (myncpus_online != 0 ? myncpus_online : ncpus_online); 28707c478bd9Sstevel@tonic-gate } 28717c478bd9Sstevel@tonic-gate 28727c478bd9Sstevel@tonic-gate /* 28737c478bd9Sstevel@tonic-gate * Return the pool to which the zone is currently bound. 28747c478bd9Sstevel@tonic-gate */ 28757c478bd9Sstevel@tonic-gate pool_t * 28767c478bd9Sstevel@tonic-gate zone_pool_get(zone_t *zone) 28777c478bd9Sstevel@tonic-gate { 28787c478bd9Sstevel@tonic-gate ASSERT(pool_lock_held()); 28797c478bd9Sstevel@tonic-gate 28807c478bd9Sstevel@tonic-gate return (zone->zone_pool); 28817c478bd9Sstevel@tonic-gate } 28827c478bd9Sstevel@tonic-gate 28837c478bd9Sstevel@tonic-gate /* 28847c478bd9Sstevel@tonic-gate * Set the zone's pool pointer and update the zone's visibility to match 28857c478bd9Sstevel@tonic-gate * the resources in the new pool. 28867c478bd9Sstevel@tonic-gate */ 28877c478bd9Sstevel@tonic-gate void 28887c478bd9Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool) 28897c478bd9Sstevel@tonic-gate { 28907c478bd9Sstevel@tonic-gate ASSERT(pool_lock_held()); 28917c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 28927c478bd9Sstevel@tonic-gate 28937c478bd9Sstevel@tonic-gate zone->zone_pool = pool; 28947c478bd9Sstevel@tonic-gate zone_pset_set(zone, pool->pool_pset->pset_id); 28957c478bd9Sstevel@tonic-gate } 28967c478bd9Sstevel@tonic-gate 28977c478bd9Sstevel@tonic-gate /* 28987c478bd9Sstevel@tonic-gate * Return the cached value of the id of the processor set to which the 28997c478bd9Sstevel@tonic-gate * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 29007c478bd9Sstevel@tonic-gate * facility is disabled. 29017c478bd9Sstevel@tonic-gate */ 29027c478bd9Sstevel@tonic-gate psetid_t 29037c478bd9Sstevel@tonic-gate zone_pset_get(zone_t *zone) 29047c478bd9Sstevel@tonic-gate { 29057c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 29067c478bd9Sstevel@tonic-gate 29077c478bd9Sstevel@tonic-gate return (zone->zone_psetid); 29087c478bd9Sstevel@tonic-gate } 29097c478bd9Sstevel@tonic-gate 29107c478bd9Sstevel@tonic-gate /* 29117c478bd9Sstevel@tonic-gate * Set the cached value of the id of the processor set to which the zone 29127c478bd9Sstevel@tonic-gate * is currently bound. Also update the zone's visibility to match the 29137c478bd9Sstevel@tonic-gate * resources in the new processor set. 29147c478bd9Sstevel@tonic-gate */ 29157c478bd9Sstevel@tonic-gate void 29167c478bd9Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid) 29177c478bd9Sstevel@tonic-gate { 29187c478bd9Sstevel@tonic-gate psetid_t oldpsetid; 29197c478bd9Sstevel@tonic-gate 29207c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 29217c478bd9Sstevel@tonic-gate oldpsetid = zone_pset_get(zone); 29227c478bd9Sstevel@tonic-gate 29237c478bd9Sstevel@tonic-gate if (oldpsetid == newpsetid) 29247c478bd9Sstevel@tonic-gate return; 29257c478bd9Sstevel@tonic-gate /* 29267c478bd9Sstevel@tonic-gate * Global zone sees all. 29277c478bd9Sstevel@tonic-gate */ 29287c478bd9Sstevel@tonic-gate if (zone != global_zone) { 29297c478bd9Sstevel@tonic-gate zone->zone_psetid = newpsetid; 29307c478bd9Sstevel@tonic-gate if (newpsetid != ZONE_PS_INVAL) 29317c478bd9Sstevel@tonic-gate pool_pset_visibility_add(newpsetid, zone); 29327c478bd9Sstevel@tonic-gate if (oldpsetid != ZONE_PS_INVAL) 29337c478bd9Sstevel@tonic-gate pool_pset_visibility_remove(oldpsetid, zone); 29347c478bd9Sstevel@tonic-gate } 29357c478bd9Sstevel@tonic-gate /* 29367c478bd9Sstevel@tonic-gate * Disabling pools, so we should start using the global values 29377c478bd9Sstevel@tonic-gate * for ncpus and ncpus_online. 29387c478bd9Sstevel@tonic-gate */ 29397c478bd9Sstevel@tonic-gate if (newpsetid == ZONE_PS_INVAL) { 29407c478bd9Sstevel@tonic-gate zone->zone_ncpus = 0; 29417c478bd9Sstevel@tonic-gate zone->zone_ncpus_online = 0; 29427c478bd9Sstevel@tonic-gate } 29437c478bd9Sstevel@tonic-gate } 29447c478bd9Sstevel@tonic-gate 29457c478bd9Sstevel@tonic-gate /* 29467c478bd9Sstevel@tonic-gate * Walk the list of active zones and issue the provided callback for 29477c478bd9Sstevel@tonic-gate * each of them. 29487c478bd9Sstevel@tonic-gate * 29497c478bd9Sstevel@tonic-gate * Caller must not be holding any locks that may be acquired under 29507c478bd9Sstevel@tonic-gate * zonehash_lock. See comment at the beginning of the file for a list of 29517c478bd9Sstevel@tonic-gate * common locks and their interactions with zones. 29527c478bd9Sstevel@tonic-gate */ 29537c478bd9Sstevel@tonic-gate int 29547c478bd9Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data) 29557c478bd9Sstevel@tonic-gate { 29567c478bd9Sstevel@tonic-gate zone_t *zone; 29577c478bd9Sstevel@tonic-gate int ret = 0; 29587c478bd9Sstevel@tonic-gate zone_status_t status; 29597c478bd9Sstevel@tonic-gate 29607c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 29617c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 29627c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 29637c478bd9Sstevel@tonic-gate /* 29647c478bd9Sstevel@tonic-gate * Skip zones that shouldn't be externally visible. 29657c478bd9Sstevel@tonic-gate */ 29667c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 29677c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 29687c478bd9Sstevel@tonic-gate continue; 29697c478bd9Sstevel@tonic-gate /* 29707c478bd9Sstevel@tonic-gate * Bail immediately if any callback invocation returns a 29717c478bd9Sstevel@tonic-gate * non-zero value. 29727c478bd9Sstevel@tonic-gate */ 29737c478bd9Sstevel@tonic-gate ret = (*cb)(zone, data); 29747c478bd9Sstevel@tonic-gate if (ret != 0) 29757c478bd9Sstevel@tonic-gate break; 29767c478bd9Sstevel@tonic-gate } 29777c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 29787c478bd9Sstevel@tonic-gate return (ret); 29797c478bd9Sstevel@tonic-gate } 29807c478bd9Sstevel@tonic-gate 29817c478bd9Sstevel@tonic-gate static int 29827c478bd9Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath) 29837c478bd9Sstevel@tonic-gate { 29847c478bd9Sstevel@tonic-gate vnode_t *vp; 29857c478bd9Sstevel@tonic-gate int trycount; 29867c478bd9Sstevel@tonic-gate int error = 0; 29877c478bd9Sstevel@tonic-gate char *path; 29887c478bd9Sstevel@tonic-gate struct pathname upn, pn; 29897c478bd9Sstevel@tonic-gate size_t pathlen; 29907c478bd9Sstevel@tonic-gate 29917c478bd9Sstevel@tonic-gate if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 29927c478bd9Sstevel@tonic-gate return (error); 29937c478bd9Sstevel@tonic-gate 29947c478bd9Sstevel@tonic-gate pn_alloc(&pn); 29957c478bd9Sstevel@tonic-gate 29967c478bd9Sstevel@tonic-gate /* prevent infinite loop */ 29977c478bd9Sstevel@tonic-gate trycount = 10; 29987c478bd9Sstevel@tonic-gate for (;;) { 29997c478bd9Sstevel@tonic-gate if (--trycount <= 0) { 30007c478bd9Sstevel@tonic-gate error = ESTALE; 30017c478bd9Sstevel@tonic-gate goto out; 30027c478bd9Sstevel@tonic-gate } 30037c478bd9Sstevel@tonic-gate 30047c478bd9Sstevel@tonic-gate if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 30057c478bd9Sstevel@tonic-gate /* 30067c478bd9Sstevel@tonic-gate * VOP_ACCESS() may cover 'vp' with a new 30077c478bd9Sstevel@tonic-gate * filesystem, if 'vp' is an autoFS vnode. 30087c478bd9Sstevel@tonic-gate * Get the new 'vp' if so. 30097c478bd9Sstevel@tonic-gate */ 3010da6c28aaSamw if ((error = 3011da6c28aaSamw VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) == 0 && 301225d2dc23Seh208807 (!vn_ismntpt(vp) || 30137c478bd9Sstevel@tonic-gate (error = traverse(&vp)) == 0)) { 30147c478bd9Sstevel@tonic-gate pathlen = pn.pn_pathlen + 2; 30157c478bd9Sstevel@tonic-gate path = kmem_alloc(pathlen, KM_SLEEP); 30167c478bd9Sstevel@tonic-gate (void) strncpy(path, pn.pn_path, 30177c478bd9Sstevel@tonic-gate pn.pn_pathlen + 1); 30187c478bd9Sstevel@tonic-gate path[pathlen - 2] = '/'; 30197c478bd9Sstevel@tonic-gate path[pathlen - 1] = '\0'; 30207c478bd9Sstevel@tonic-gate pn_free(&pn); 30217c478bd9Sstevel@tonic-gate pn_free(&upn); 30227c478bd9Sstevel@tonic-gate 30237c478bd9Sstevel@tonic-gate /* Success! */ 30247c478bd9Sstevel@tonic-gate break; 30257c478bd9Sstevel@tonic-gate } 30267c478bd9Sstevel@tonic-gate VN_RELE(vp); 30277c478bd9Sstevel@tonic-gate } 30287c478bd9Sstevel@tonic-gate if (error != ESTALE) 30297c478bd9Sstevel@tonic-gate goto out; 30307c478bd9Sstevel@tonic-gate } 30317c478bd9Sstevel@tonic-gate 30327c478bd9Sstevel@tonic-gate ASSERT(error == 0); 30337c478bd9Sstevel@tonic-gate zone->zone_rootvp = vp; /* we hold a reference to vp */ 30347c478bd9Sstevel@tonic-gate zone->zone_rootpath = path; 30357c478bd9Sstevel@tonic-gate zone->zone_rootpathlen = pathlen; 303648451833Scarlsonj if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0) 303748451833Scarlsonj zone->zone_flags |= ZF_IS_SCRATCH; 30387c478bd9Sstevel@tonic-gate return (0); 30397c478bd9Sstevel@tonic-gate 30407c478bd9Sstevel@tonic-gate out: 30417c478bd9Sstevel@tonic-gate pn_free(&pn); 30427c478bd9Sstevel@tonic-gate pn_free(&upn); 30437c478bd9Sstevel@tonic-gate return (error); 30447c478bd9Sstevel@tonic-gate } 30457c478bd9Sstevel@tonic-gate 30467c478bd9Sstevel@tonic-gate #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 30477c478bd9Sstevel@tonic-gate ((c) >= 'a' && (c) <= 'z') || \ 30487c478bd9Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'Z')) 30497c478bd9Sstevel@tonic-gate 30507c478bd9Sstevel@tonic-gate static int 30517c478bd9Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname) 30527c478bd9Sstevel@tonic-gate { 30537c478bd9Sstevel@tonic-gate char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 30547c478bd9Sstevel@tonic-gate size_t len; 30557c478bd9Sstevel@tonic-gate int i, err; 30567c478bd9Sstevel@tonic-gate 30577c478bd9Sstevel@tonic-gate if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 30587c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 30597c478bd9Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 30607c478bd9Sstevel@tonic-gate } 30617c478bd9Sstevel@tonic-gate 30627c478bd9Sstevel@tonic-gate /* must be less than ZONENAME_MAX */ 30637c478bd9Sstevel@tonic-gate if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 30647c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 30657c478bd9Sstevel@tonic-gate return (EINVAL); 30667c478bd9Sstevel@tonic-gate } 30677c478bd9Sstevel@tonic-gate 30687c478bd9Sstevel@tonic-gate /* 30697c478bd9Sstevel@tonic-gate * Name must start with an alphanumeric and must contain only 30707c478bd9Sstevel@tonic-gate * alphanumerics, '-', '_' and '.'. 30717c478bd9Sstevel@tonic-gate */ 30727c478bd9Sstevel@tonic-gate if (!isalnum(kname[0])) { 30737c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 30747c478bd9Sstevel@tonic-gate return (EINVAL); 30757c478bd9Sstevel@tonic-gate } 30767c478bd9Sstevel@tonic-gate for (i = 1; i < len - 1; i++) { 30777c478bd9Sstevel@tonic-gate if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 30787c478bd9Sstevel@tonic-gate kname[i] != '.') { 30797c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 30807c478bd9Sstevel@tonic-gate return (EINVAL); 30817c478bd9Sstevel@tonic-gate } 30827c478bd9Sstevel@tonic-gate } 30837c478bd9Sstevel@tonic-gate 30847c478bd9Sstevel@tonic-gate zone->zone_name = kname; 30857c478bd9Sstevel@tonic-gate return (0); 30867c478bd9Sstevel@tonic-gate } 30877c478bd9Sstevel@tonic-gate 30887c478bd9Sstevel@tonic-gate /* 30895679c89fSjv227347 * Gets the 32-bit hostid of the specified zone as an unsigned int. If 'zonep' 30905679c89fSjv227347 * is NULL or it points to a zone with no hostid emulation, then the machine's 30915679c89fSjv227347 * hostid (i.e., the global zone's hostid) is returned. This function returns 30925679c89fSjv227347 * zero if neither the zone nor the host machine (global zone) have hostids. It 30935679c89fSjv227347 * returns HW_INVALID_HOSTID if the function attempts to return the machine's 30945679c89fSjv227347 * hostid and the machine's hostid is invalid. 30955679c89fSjv227347 */ 30965679c89fSjv227347 uint32_t 30975679c89fSjv227347 zone_get_hostid(zone_t *zonep) 30985679c89fSjv227347 { 30995679c89fSjv227347 unsigned long machine_hostid; 31005679c89fSjv227347 31015679c89fSjv227347 if (zonep == NULL || zonep->zone_hostid == HW_INVALID_HOSTID) { 31025679c89fSjv227347 if (ddi_strtoul(hw_serial, NULL, 10, &machine_hostid) != 0) 31035679c89fSjv227347 return (HW_INVALID_HOSTID); 31045679c89fSjv227347 return ((uint32_t)machine_hostid); 31055679c89fSjv227347 } 31065679c89fSjv227347 return (zonep->zone_hostid); 31075679c89fSjv227347 } 31085679c89fSjv227347 31095679c89fSjv227347 /* 31107c478bd9Sstevel@tonic-gate * Similar to thread_create(), but makes sure the thread is in the appropriate 31117c478bd9Sstevel@tonic-gate * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 31127c478bd9Sstevel@tonic-gate */ 31137c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 31147c478bd9Sstevel@tonic-gate kthread_t * 31157c478bd9Sstevel@tonic-gate zthread_create( 31167c478bd9Sstevel@tonic-gate caddr_t stk, 31177c478bd9Sstevel@tonic-gate size_t stksize, 31187c478bd9Sstevel@tonic-gate void (*proc)(), 31197c478bd9Sstevel@tonic-gate void *arg, 31207c478bd9Sstevel@tonic-gate size_t len, 31217c478bd9Sstevel@tonic-gate pri_t pri) 31227c478bd9Sstevel@tonic-gate { 31237c478bd9Sstevel@tonic-gate kthread_t *t; 31247c478bd9Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 31257c478bd9Sstevel@tonic-gate proc_t *pp = zone->zone_zsched; 31267c478bd9Sstevel@tonic-gate 31277c478bd9Sstevel@tonic-gate zone_hold(zone); /* Reference to be dropped when thread exits */ 31287c478bd9Sstevel@tonic-gate 31297c478bd9Sstevel@tonic-gate /* 31307c478bd9Sstevel@tonic-gate * No-one should be trying to create threads if the zone is shutting 31317c478bd9Sstevel@tonic-gate * down and there aren't any kernel threads around. See comment 31327c478bd9Sstevel@tonic-gate * in zthread_exit(). 31337c478bd9Sstevel@tonic-gate */ 31347c478bd9Sstevel@tonic-gate ASSERT(!(zone->zone_kthreads == NULL && 31357c478bd9Sstevel@tonic-gate zone_status_get(zone) >= ZONE_IS_EMPTY)); 31367c478bd9Sstevel@tonic-gate /* 31377c478bd9Sstevel@tonic-gate * Create a thread, but don't let it run until we've finished setting 31387c478bd9Sstevel@tonic-gate * things up. 31397c478bd9Sstevel@tonic-gate */ 31407c478bd9Sstevel@tonic-gate t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 31417c478bd9Sstevel@tonic-gate ASSERT(t->t_forw == NULL); 31427c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 31437c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 31447c478bd9Sstevel@tonic-gate t->t_forw = t->t_back = t; 31457c478bd9Sstevel@tonic-gate } else { 31467c478bd9Sstevel@tonic-gate kthread_t *tx = zone->zone_kthreads; 31477c478bd9Sstevel@tonic-gate 31487c478bd9Sstevel@tonic-gate t->t_forw = tx; 31497c478bd9Sstevel@tonic-gate t->t_back = tx->t_back; 31507c478bd9Sstevel@tonic-gate tx->t_back->t_forw = t; 31517c478bd9Sstevel@tonic-gate tx->t_back = t; 31527c478bd9Sstevel@tonic-gate } 31537c478bd9Sstevel@tonic-gate zone->zone_kthreads = t; 31547c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31557c478bd9Sstevel@tonic-gate 31567c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 31577c478bd9Sstevel@tonic-gate t->t_proc_flag |= TP_ZTHREAD; 31587c478bd9Sstevel@tonic-gate project_rele(t->t_proj); 31597c478bd9Sstevel@tonic-gate t->t_proj = project_hold(pp->p_task->tk_proj); 31607c478bd9Sstevel@tonic-gate 31617c478bd9Sstevel@tonic-gate /* 31627c478bd9Sstevel@tonic-gate * Setup complete, let it run. 31637c478bd9Sstevel@tonic-gate */ 31647c478bd9Sstevel@tonic-gate thread_lock(t); 31657c478bd9Sstevel@tonic-gate t->t_schedflag |= TS_ALLSTART; 31667c478bd9Sstevel@tonic-gate setrun_locked(t); 31677c478bd9Sstevel@tonic-gate thread_unlock(t); 31687c478bd9Sstevel@tonic-gate 31697c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 31707c478bd9Sstevel@tonic-gate 31717c478bd9Sstevel@tonic-gate return (t); 31727c478bd9Sstevel@tonic-gate } 31737c478bd9Sstevel@tonic-gate 31747c478bd9Sstevel@tonic-gate /* 31757c478bd9Sstevel@tonic-gate * Similar to thread_exit(). Must be called by threads created via 31767c478bd9Sstevel@tonic-gate * zthread_exit(). 31777c478bd9Sstevel@tonic-gate */ 31787c478bd9Sstevel@tonic-gate void 31797c478bd9Sstevel@tonic-gate zthread_exit(void) 31807c478bd9Sstevel@tonic-gate { 31817c478bd9Sstevel@tonic-gate kthread_t *t = curthread; 31827c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 31837c478bd9Sstevel@tonic-gate zone_t *zone = pp->p_zone; 31847c478bd9Sstevel@tonic-gate 31857c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 31867c478bd9Sstevel@tonic-gate 31877c478bd9Sstevel@tonic-gate /* 31887c478bd9Sstevel@tonic-gate * Reparent to p0 31897c478bd9Sstevel@tonic-gate */ 3190b4b07f87Sjosephb kpreempt_disable(); 31917c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 31927c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_ZTHREAD; 31937c478bd9Sstevel@tonic-gate t->t_procp = &p0; 31947c478bd9Sstevel@tonic-gate hat_thread_exit(t); 31957c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 3196b4b07f87Sjosephb kpreempt_enable(); 31977c478bd9Sstevel@tonic-gate 31987c478bd9Sstevel@tonic-gate if (t->t_back == t) { 31997c478bd9Sstevel@tonic-gate ASSERT(t->t_forw == t); 32007c478bd9Sstevel@tonic-gate /* 32017c478bd9Sstevel@tonic-gate * If the zone is empty, once the thread count 32027c478bd9Sstevel@tonic-gate * goes to zero no further kernel threads can be 32037c478bd9Sstevel@tonic-gate * created. This is because if the creator is a process 32047c478bd9Sstevel@tonic-gate * in the zone, then it must have exited before the zone 32057c478bd9Sstevel@tonic-gate * state could be set to ZONE_IS_EMPTY. 32067c478bd9Sstevel@tonic-gate * Otherwise, if the creator is a kernel thread in the 32077c478bd9Sstevel@tonic-gate * zone, the thread count is non-zero. 32087c478bd9Sstevel@tonic-gate * 32097c478bd9Sstevel@tonic-gate * This really means that non-zone kernel threads should 32107c478bd9Sstevel@tonic-gate * not create zone kernel threads. 32117c478bd9Sstevel@tonic-gate */ 32127c478bd9Sstevel@tonic-gate zone->zone_kthreads = NULL; 32137c478bd9Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_EMPTY) { 32147c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 3215c97ad5cdSakolb /* 3216c97ad5cdSakolb * Remove any CPU caps on this zone. 3217c97ad5cdSakolb */ 3218c97ad5cdSakolb cpucaps_zone_remove(zone); 32197c478bd9Sstevel@tonic-gate } 32207c478bd9Sstevel@tonic-gate } else { 32217c478bd9Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 32227c478bd9Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 32237c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == t) 32247c478bd9Sstevel@tonic-gate zone->zone_kthreads = t->t_forw; 32257c478bd9Sstevel@tonic-gate } 32267c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 32277c478bd9Sstevel@tonic-gate zone_rele(zone); 32287c478bd9Sstevel@tonic-gate thread_exit(); 32297c478bd9Sstevel@tonic-gate /* NOTREACHED */ 32307c478bd9Sstevel@tonic-gate } 32317c478bd9Sstevel@tonic-gate 32327c478bd9Sstevel@tonic-gate static void 32337c478bd9Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 32347c478bd9Sstevel@tonic-gate { 32357c478bd9Sstevel@tonic-gate vnode_t *oldvp; 32367c478bd9Sstevel@tonic-gate 32377c478bd9Sstevel@tonic-gate /* we're going to hold a reference here to the directory */ 32387c478bd9Sstevel@tonic-gate VN_HOLD(vp); 32397c478bd9Sstevel@tonic-gate 3240005d3febSMarek Pospisil /* update abs cwd/root path see c2/audit.c */ 3241005d3febSMarek Pospisil if (AU_AUDITING()) 32427c478bd9Sstevel@tonic-gate audit_chdirec(vp, vpp); 32437c478bd9Sstevel@tonic-gate 32447c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 32457c478bd9Sstevel@tonic-gate oldvp = *vpp; 32467c478bd9Sstevel@tonic-gate *vpp = vp; 32477c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 32487c478bd9Sstevel@tonic-gate if (oldvp != NULL) 32497c478bd9Sstevel@tonic-gate VN_RELE(oldvp); 32507c478bd9Sstevel@tonic-gate } 32517c478bd9Sstevel@tonic-gate 32527c478bd9Sstevel@tonic-gate /* 32537c478bd9Sstevel@tonic-gate * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 32547c478bd9Sstevel@tonic-gate */ 32557c478bd9Sstevel@tonic-gate static int 32567c478bd9Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 32577c478bd9Sstevel@tonic-gate { 32587c478bd9Sstevel@tonic-gate nvpair_t *nvp = NULL; 32597c478bd9Sstevel@tonic-gate boolean_t priv_set = B_FALSE; 32607c478bd9Sstevel@tonic-gate boolean_t limit_set = B_FALSE; 32617c478bd9Sstevel@tonic-gate boolean_t action_set = B_FALSE; 32627c478bd9Sstevel@tonic-gate 32637c478bd9Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 32647c478bd9Sstevel@tonic-gate const char *name; 32657c478bd9Sstevel@tonic-gate uint64_t ui64; 32667c478bd9Sstevel@tonic-gate 32677c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 32687c478bd9Sstevel@tonic-gate if (nvpair_type(nvp) != DATA_TYPE_UINT64) 32697c478bd9Sstevel@tonic-gate return (EINVAL); 32707c478bd9Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &ui64); 32717c478bd9Sstevel@tonic-gate if (strcmp(name, "privilege") == 0) { 32727c478bd9Sstevel@tonic-gate /* 32737c478bd9Sstevel@tonic-gate * Currently only privileged values are allowed, but 32747c478bd9Sstevel@tonic-gate * this may change in the future. 32757c478bd9Sstevel@tonic-gate */ 32767c478bd9Sstevel@tonic-gate if (ui64 != RCPRIV_PRIVILEGED) 32777c478bd9Sstevel@tonic-gate return (EINVAL); 32787c478bd9Sstevel@tonic-gate rv->rcv_privilege = ui64; 32797c478bd9Sstevel@tonic-gate priv_set = B_TRUE; 32807c478bd9Sstevel@tonic-gate } else if (strcmp(name, "limit") == 0) { 32817c478bd9Sstevel@tonic-gate rv->rcv_value = ui64; 32827c478bd9Sstevel@tonic-gate limit_set = B_TRUE; 32837c478bd9Sstevel@tonic-gate } else if (strcmp(name, "action") == 0) { 32847c478bd9Sstevel@tonic-gate if (ui64 != RCTL_LOCAL_NOACTION && 32857c478bd9Sstevel@tonic-gate ui64 != RCTL_LOCAL_DENY) 32867c478bd9Sstevel@tonic-gate return (EINVAL); 32877c478bd9Sstevel@tonic-gate rv->rcv_flagaction = ui64; 32887c478bd9Sstevel@tonic-gate action_set = B_TRUE; 32897c478bd9Sstevel@tonic-gate } else { 32907c478bd9Sstevel@tonic-gate return (EINVAL); 32917c478bd9Sstevel@tonic-gate } 32927c478bd9Sstevel@tonic-gate } 32937c478bd9Sstevel@tonic-gate 32947c478bd9Sstevel@tonic-gate if (!(priv_set && limit_set && action_set)) 32957c478bd9Sstevel@tonic-gate return (EINVAL); 32967c478bd9Sstevel@tonic-gate rv->rcv_action_signal = 0; 32977c478bd9Sstevel@tonic-gate rv->rcv_action_recipient = NULL; 32987c478bd9Sstevel@tonic-gate rv->rcv_action_recip_pid = -1; 32997c478bd9Sstevel@tonic-gate rv->rcv_firing_time = 0; 33007c478bd9Sstevel@tonic-gate 33017c478bd9Sstevel@tonic-gate return (0); 33027c478bd9Sstevel@tonic-gate } 33037c478bd9Sstevel@tonic-gate 33043f2f09c1Sdp /* 33053f2f09c1Sdp * Non-global zone version of start_init. 33063f2f09c1Sdp */ 33077c478bd9Sstevel@tonic-gate void 33083f2f09c1Sdp zone_start_init(void) 33097c478bd9Sstevel@tonic-gate { 33107c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 33119acbbeafSnn35248 zone_t *z = p->p_zone; 33123f2f09c1Sdp 33133f2f09c1Sdp ASSERT(!INGLOBALZONE(curproc)); 33147c478bd9Sstevel@tonic-gate 33157c478bd9Sstevel@tonic-gate /* 33169acbbeafSnn35248 * For all purposes (ZONE_ATTR_INITPID and restart_init), 33179acbbeafSnn35248 * storing just the pid of init is sufficient. 33189acbbeafSnn35248 */ 33199acbbeafSnn35248 z->zone_proc_initpid = p->p_pid; 33209acbbeafSnn35248 33219acbbeafSnn35248 /* 33223f2f09c1Sdp * We maintain zone_boot_err so that we can return the cause of the 33233f2f09c1Sdp * failure back to the caller of the zone_boot syscall. 33247c478bd9Sstevel@tonic-gate */ 33253f2f09c1Sdp p->p_zone->zone_boot_err = start_init_common(); 33267c478bd9Sstevel@tonic-gate 33278f983ab3Sjv227347 /* 33288f983ab3Sjv227347 * We will prevent booting zones from becoming running zones if the 33298f983ab3Sjv227347 * global zone is shutting down. 33308f983ab3Sjv227347 */ 33317c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 33328f983ab3Sjv227347 if (z->zone_boot_err != 0 || zone_status_get(global_zone) >= 33338f983ab3Sjv227347 ZONE_IS_SHUTTING_DOWN) { 33347c478bd9Sstevel@tonic-gate /* 33357c478bd9Sstevel@tonic-gate * Make sure we are still in the booting state-- we could have 33367c478bd9Sstevel@tonic-gate * raced and already be shutting down, or even further along. 33377c478bd9Sstevel@tonic-gate */ 3338c97ad5cdSakolb if (zone_status_get(z) == ZONE_IS_BOOTING) { 33399acbbeafSnn35248 zone_status_set(z, ZONE_IS_SHUTTING_DOWN); 3340c97ad5cdSakolb } 33417c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 33427c478bd9Sstevel@tonic-gate /* It's gone bad, dispose of the process */ 33439acbbeafSnn35248 if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) { 334497eda132Sraf mutex_enter(&p->p_lock); 334597eda132Sraf ASSERT(p->p_flag & SEXITLWPS); 33467c478bd9Sstevel@tonic-gate lwp_exit(); 33477c478bd9Sstevel@tonic-gate } 33487c478bd9Sstevel@tonic-gate } else { 33499acbbeafSnn35248 if (zone_status_get(z) == ZONE_IS_BOOTING) 33509acbbeafSnn35248 zone_status_set(z, ZONE_IS_RUNNING); 33517c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 33527c478bd9Sstevel@tonic-gate /* cause the process to return to userland. */ 33537c478bd9Sstevel@tonic-gate lwp_rtt(); 33547c478bd9Sstevel@tonic-gate } 33557c478bd9Sstevel@tonic-gate } 33567c478bd9Sstevel@tonic-gate 33577c478bd9Sstevel@tonic-gate struct zsched_arg { 33587c478bd9Sstevel@tonic-gate zone_t *zone; 33597c478bd9Sstevel@tonic-gate nvlist_t *nvlist; 33607c478bd9Sstevel@tonic-gate }; 33617c478bd9Sstevel@tonic-gate 33627c478bd9Sstevel@tonic-gate /* 33637c478bd9Sstevel@tonic-gate * Per-zone "sched" workalike. The similarity to "sched" doesn't have 33647c478bd9Sstevel@tonic-gate * anything to do with scheduling, but rather with the fact that 33657c478bd9Sstevel@tonic-gate * per-zone kernel threads are parented to zsched, just like regular 33667c478bd9Sstevel@tonic-gate * kernel threads are parented to sched (p0). 33677c478bd9Sstevel@tonic-gate * 33687c478bd9Sstevel@tonic-gate * zsched is also responsible for launching init for the zone. 33697c478bd9Sstevel@tonic-gate */ 33707c478bd9Sstevel@tonic-gate static void 33717c478bd9Sstevel@tonic-gate zsched(void *arg) 33727c478bd9Sstevel@tonic-gate { 33737c478bd9Sstevel@tonic-gate struct zsched_arg *za = arg; 33747c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 33757c478bd9Sstevel@tonic-gate proc_t *initp = proc_init; 33767c478bd9Sstevel@tonic-gate zone_t *zone = za->zone; 33777c478bd9Sstevel@tonic-gate cred_t *cr, *oldcred; 33787c478bd9Sstevel@tonic-gate rctl_set_t *set; 33797c478bd9Sstevel@tonic-gate rctl_alloc_gp_t *gp; 33807c478bd9Sstevel@tonic-gate contract_t *ct = NULL; 33817c478bd9Sstevel@tonic-gate task_t *tk, *oldtk; 33827c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 33837c478bd9Sstevel@tonic-gate kproject_t *pj; 33847c478bd9Sstevel@tonic-gate 33857c478bd9Sstevel@tonic-gate nvlist_t *nvl = za->nvlist; 33867c478bd9Sstevel@tonic-gate nvpair_t *nvp = NULL; 33877c478bd9Sstevel@tonic-gate 3388ae115bc7Smrj bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched")); 3389ae115bc7Smrj bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched")); 3390ae115bc7Smrj PTOU(pp)->u_argc = 0; 3391ae115bc7Smrj PTOU(pp)->u_argv = NULL; 3392ae115bc7Smrj PTOU(pp)->u_envp = NULL; 33937c478bd9Sstevel@tonic-gate closeall(P_FINFO(pp)); 33947c478bd9Sstevel@tonic-gate 33957c478bd9Sstevel@tonic-gate /* 33967c478bd9Sstevel@tonic-gate * We are this zone's "zsched" process. As the zone isn't generally 33977c478bd9Sstevel@tonic-gate * visible yet we don't need to grab any locks before initializing its 33987c478bd9Sstevel@tonic-gate * zone_proc pointer. 33997c478bd9Sstevel@tonic-gate */ 34007c478bd9Sstevel@tonic-gate zone_hold(zone); /* this hold is released by zone_destroy() */ 34017c478bd9Sstevel@tonic-gate zone->zone_zsched = pp; 34027c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 34037c478bd9Sstevel@tonic-gate pp->p_zone = zone; 34047c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 34057c478bd9Sstevel@tonic-gate 34067c478bd9Sstevel@tonic-gate /* 34077c478bd9Sstevel@tonic-gate * Disassociate process from its 'parent'; parent ourselves to init 34087c478bd9Sstevel@tonic-gate * (pid 1) and change other values as needed. 34097c478bd9Sstevel@tonic-gate */ 34107c478bd9Sstevel@tonic-gate sess_create(); 34117c478bd9Sstevel@tonic-gate 34127c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 34137c478bd9Sstevel@tonic-gate proc_detach(pp); 34147c478bd9Sstevel@tonic-gate pp->p_ppid = 1; 34157c478bd9Sstevel@tonic-gate pp->p_flag |= SZONETOP; 34167c478bd9Sstevel@tonic-gate pp->p_ancpid = 1; 34177c478bd9Sstevel@tonic-gate pp->p_parent = initp; 34187c478bd9Sstevel@tonic-gate pp->p_psibling = NULL; 34197c478bd9Sstevel@tonic-gate if (initp->p_child) 34207c478bd9Sstevel@tonic-gate initp->p_child->p_psibling = pp; 34217c478bd9Sstevel@tonic-gate pp->p_sibling = initp->p_child; 34227c478bd9Sstevel@tonic-gate initp->p_child = pp; 34237c478bd9Sstevel@tonic-gate 34247c478bd9Sstevel@tonic-gate /* Decrement what newproc() incremented. */ 34257c478bd9Sstevel@tonic-gate upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 34267c478bd9Sstevel@tonic-gate /* 34277c478bd9Sstevel@tonic-gate * Our credentials are about to become kcred-like, so we don't care 34287c478bd9Sstevel@tonic-gate * about the caller's ruid. 34297c478bd9Sstevel@tonic-gate */ 34307c478bd9Sstevel@tonic-gate upcount_inc(crgetruid(kcred), zone->zone_id); 34317c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 34327c478bd9Sstevel@tonic-gate 34337c478bd9Sstevel@tonic-gate /* 3434ff19e029SMenno Lageman * getting out of global zone, so decrement lwp and process counts 34357c478bd9Sstevel@tonic-gate */ 34367c478bd9Sstevel@tonic-gate pj = pp->p_task->tk_proj; 34377c478bd9Sstevel@tonic-gate mutex_enter(&global_zone->zone_nlwps_lock); 34387c478bd9Sstevel@tonic-gate pj->kpj_nlwps -= pp->p_lwpcnt; 34397c478bd9Sstevel@tonic-gate global_zone->zone_nlwps -= pp->p_lwpcnt; 3440ff19e029SMenno Lageman pj->kpj_nprocs--; 3441ff19e029SMenno Lageman global_zone->zone_nprocs--; 34427c478bd9Sstevel@tonic-gate mutex_exit(&global_zone->zone_nlwps_lock); 34437c478bd9Sstevel@tonic-gate 34447c478bd9Sstevel@tonic-gate /* 3445c6939658Ssl108498 * Decrement locked memory counts on old zone and project. 3446c6939658Ssl108498 */ 34470209230bSgjelinek mutex_enter(&global_zone->zone_mem_lock); 3448c6939658Ssl108498 global_zone->zone_locked_mem -= pp->p_locked_mem; 3449c6939658Ssl108498 pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 34500209230bSgjelinek mutex_exit(&global_zone->zone_mem_lock); 3451c6939658Ssl108498 3452c6939658Ssl108498 /* 34537c478bd9Sstevel@tonic-gate * Create and join a new task in project '0' of this zone. 34547c478bd9Sstevel@tonic-gate * 34557c478bd9Sstevel@tonic-gate * We don't need to call holdlwps() since we know we're the only lwp in 34567c478bd9Sstevel@tonic-gate * this process. 34577c478bd9Sstevel@tonic-gate * 34587c478bd9Sstevel@tonic-gate * task_join() returns with p_lock held. 34597c478bd9Sstevel@tonic-gate */ 34607c478bd9Sstevel@tonic-gate tk = task_create(0, zone); 34617c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 34627c478bd9Sstevel@tonic-gate oldtk = task_join(tk, 0); 3463c6939658Ssl108498 3464c6939658Ssl108498 pj = pp->p_task->tk_proj; 3465c6939658Ssl108498 34660209230bSgjelinek mutex_enter(&zone->zone_mem_lock); 3467c6939658Ssl108498 zone->zone_locked_mem += pp->p_locked_mem; 3468c6939658Ssl108498 pj->kpj_data.kpd_locked_mem += pp->p_locked_mem; 34690209230bSgjelinek mutex_exit(&zone->zone_mem_lock); 34707c478bd9Sstevel@tonic-gate 34717c478bd9Sstevel@tonic-gate /* 3472ff19e029SMenno Lageman * add lwp and process counts to zsched's zone, and increment 3473ff19e029SMenno Lageman * project's task and process count due to the task created in 3474ff19e029SMenno Lageman * the above task_create. 34757c478bd9Sstevel@tonic-gate */ 34767c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 34777c478bd9Sstevel@tonic-gate pj->kpj_nlwps += pp->p_lwpcnt; 34787c478bd9Sstevel@tonic-gate pj->kpj_ntasks += 1; 34797c478bd9Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 3480ff19e029SMenno Lageman pj->kpj_nprocs++; 3481ff19e029SMenno Lageman zone->zone_nprocs++; 34827c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 34837c478bd9Sstevel@tonic-gate 3484c6939658Ssl108498 mutex_exit(&curproc->p_lock); 3485c6939658Ssl108498 mutex_exit(&cpu_lock); 3486c6939658Ssl108498 task_rele(oldtk); 3487c6939658Ssl108498 34887c478bd9Sstevel@tonic-gate /* 34897c478bd9Sstevel@tonic-gate * The process was created by a process in the global zone, hence the 34907c478bd9Sstevel@tonic-gate * credentials are wrong. We might as well have kcred-ish credentials. 34917c478bd9Sstevel@tonic-gate */ 34927c478bd9Sstevel@tonic-gate cr = zone->zone_kcred; 34937c478bd9Sstevel@tonic-gate crhold(cr); 34947c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 34957c478bd9Sstevel@tonic-gate oldcred = pp->p_cred; 34967c478bd9Sstevel@tonic-gate pp->p_cred = cr; 34977c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 34987c478bd9Sstevel@tonic-gate crfree(oldcred); 34997c478bd9Sstevel@tonic-gate 35007c478bd9Sstevel@tonic-gate /* 35017c478bd9Sstevel@tonic-gate * Hold credentials again (for thread) 35027c478bd9Sstevel@tonic-gate */ 35037c478bd9Sstevel@tonic-gate crhold(cr); 35047c478bd9Sstevel@tonic-gate 35057c478bd9Sstevel@tonic-gate /* 35067c478bd9Sstevel@tonic-gate * p_lwpcnt can't change since this is a kernel process. 35077c478bd9Sstevel@tonic-gate */ 35087c478bd9Sstevel@tonic-gate crset(pp, cr); 35097c478bd9Sstevel@tonic-gate 35107c478bd9Sstevel@tonic-gate /* 35117c478bd9Sstevel@tonic-gate * Chroot 35127c478bd9Sstevel@tonic-gate */ 35137c478bd9Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 35147c478bd9Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 35157c478bd9Sstevel@tonic-gate 35167c478bd9Sstevel@tonic-gate /* 35177c478bd9Sstevel@tonic-gate * Initialize zone's rctl set. 35187c478bd9Sstevel@tonic-gate */ 35197c478bd9Sstevel@tonic-gate set = rctl_set_create(); 35207c478bd9Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 35217c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 35227c478bd9Sstevel@tonic-gate e.rcep_p.zone = zone; 35237c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 35247c478bd9Sstevel@tonic-gate zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 35257c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 35267c478bd9Sstevel@tonic-gate rctl_prealloc_destroy(gp); 35277c478bd9Sstevel@tonic-gate 35287c478bd9Sstevel@tonic-gate /* 35297c478bd9Sstevel@tonic-gate * Apply the rctls passed in to zone_create(). This is basically a list 35307c478bd9Sstevel@tonic-gate * assignment: all of the old values are removed and the new ones 35317c478bd9Sstevel@tonic-gate * inserted. That is, if an empty list is passed in, all values are 35327c478bd9Sstevel@tonic-gate * removed. 35337c478bd9Sstevel@tonic-gate */ 35347c478bd9Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 35357c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rde; 35367c478bd9Sstevel@tonic-gate rctl_hndl_t hndl; 35377c478bd9Sstevel@tonic-gate char *name; 35387c478bd9Sstevel@tonic-gate nvlist_t **nvlarray; 35397c478bd9Sstevel@tonic-gate uint_t i, nelem; 35407c478bd9Sstevel@tonic-gate int error; /* For ASSERT()s */ 35417c478bd9Sstevel@tonic-gate 35427c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 35437c478bd9Sstevel@tonic-gate hndl = rctl_hndl_lookup(name); 35447c478bd9Sstevel@tonic-gate ASSERT(hndl != -1); 35457c478bd9Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 35467c478bd9Sstevel@tonic-gate ASSERT(rde != NULL); 35477c478bd9Sstevel@tonic-gate 35487c478bd9Sstevel@tonic-gate for (; /* ever */; ) { 35497c478bd9Sstevel@tonic-gate rctl_val_t oval; 35507c478bd9Sstevel@tonic-gate 35517c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 35527c478bd9Sstevel@tonic-gate error = rctl_local_get(hndl, NULL, &oval, pp); 35537c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 35547c478bd9Sstevel@tonic-gate ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 35557c478bd9Sstevel@tonic-gate ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 35567c478bd9Sstevel@tonic-gate if (oval.rcv_privilege == RCPRIV_SYSTEM) 35577c478bd9Sstevel@tonic-gate break; 35587c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 35597c478bd9Sstevel@tonic-gate error = rctl_local_delete(hndl, &oval, pp); 35607c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 35617c478bd9Sstevel@tonic-gate ASSERT(error == 0); 35627c478bd9Sstevel@tonic-gate } 35637c478bd9Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 35647c478bd9Sstevel@tonic-gate ASSERT(error == 0); 35657c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 35667c478bd9Sstevel@tonic-gate rctl_val_t *nvalp; 35677c478bd9Sstevel@tonic-gate 35687c478bd9Sstevel@tonic-gate nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 35697c478bd9Sstevel@tonic-gate error = nvlist2rctlval(nvlarray[i], nvalp); 35707c478bd9Sstevel@tonic-gate ASSERT(error == 0); 35717c478bd9Sstevel@tonic-gate /* 35727c478bd9Sstevel@tonic-gate * rctl_local_insert can fail if the value being 35737c478bd9Sstevel@tonic-gate * inserted is a duplicate; this is OK. 35747c478bd9Sstevel@tonic-gate */ 35757c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 35767c478bd9Sstevel@tonic-gate if (rctl_local_insert(hndl, nvalp, pp) != 0) 35777c478bd9Sstevel@tonic-gate kmem_cache_free(rctl_val_cache, nvalp); 35787c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 35797c478bd9Sstevel@tonic-gate } 35807c478bd9Sstevel@tonic-gate } 35817c478bd9Sstevel@tonic-gate /* 35827c478bd9Sstevel@tonic-gate * Tell the world that we're done setting up. 35837c478bd9Sstevel@tonic-gate * 3584bd41d0a8Snordmark * At this point we want to set the zone status to ZONE_IS_INITIALIZED 35857c478bd9Sstevel@tonic-gate * and atomically set the zone's processor set visibility. Once 35867c478bd9Sstevel@tonic-gate * we drop pool_lock() this zone will automatically get updated 35877c478bd9Sstevel@tonic-gate * to reflect any future changes to the pools configuration. 3588bd41d0a8Snordmark * 3589bd41d0a8Snordmark * Note that after we drop the locks below (zonehash_lock in 3590bd41d0a8Snordmark * particular) other operations such as a zone_getattr call can 3591bd41d0a8Snordmark * now proceed and observe the zone. That is the reason for doing a 3592bd41d0a8Snordmark * state transition to the INITIALIZED state. 35937c478bd9Sstevel@tonic-gate */ 35947c478bd9Sstevel@tonic-gate pool_lock(); 35957c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 35967c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 35977c478bd9Sstevel@tonic-gate zone_uniqid(zone); 35987c478bd9Sstevel@tonic-gate zone_zsd_configure(zone); 35997c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) 36007c478bd9Sstevel@tonic-gate zone_pset_set(zone, pool_default->pool_pset->pset_id); 36017c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 36027c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 3603bd41d0a8Snordmark zone_status_set(zone, ZONE_IS_INITIALIZED); 36047c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 36057c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36067c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 36077c478bd9Sstevel@tonic-gate pool_unlock(); 36087c478bd9Sstevel@tonic-gate 3609bd41d0a8Snordmark /* Now call the create callback for this key */ 3610bd41d0a8Snordmark zsd_apply_all_keys(zsd_apply_create, zone); 3611bd41d0a8Snordmark 3612bd41d0a8Snordmark /* The callbacks are complete. Mark ZONE_IS_READY */ 3613bd41d0a8Snordmark mutex_enter(&zone_status_lock); 3614bd41d0a8Snordmark ASSERT(zone_status_get(zone) == ZONE_IS_INITIALIZED); 3615bd41d0a8Snordmark zone_status_set(zone, ZONE_IS_READY); 3616bd41d0a8Snordmark mutex_exit(&zone_status_lock); 3617bd41d0a8Snordmark 36187c478bd9Sstevel@tonic-gate /* 36197c478bd9Sstevel@tonic-gate * Once we see the zone transition to the ZONE_IS_BOOTING state, 36207c478bd9Sstevel@tonic-gate * we launch init, and set the state to running. 36217c478bd9Sstevel@tonic-gate */ 36227c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 36237c478bd9Sstevel@tonic-gate 36247c478bd9Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_BOOTING) { 36257c478bd9Sstevel@tonic-gate id_t cid; 36267c478bd9Sstevel@tonic-gate 36277c478bd9Sstevel@tonic-gate /* 36287c478bd9Sstevel@tonic-gate * Ok, this is a little complicated. We need to grab the 36297c478bd9Sstevel@tonic-gate * zone's pool's scheduling class ID; note that by now, we 36307c478bd9Sstevel@tonic-gate * are already bound to a pool if we need to be (zoneadmd 36317c478bd9Sstevel@tonic-gate * will have done that to us while we're in the READY 36327c478bd9Sstevel@tonic-gate * state). *But* the scheduling class for the zone's 'init' 36337c478bd9Sstevel@tonic-gate * must be explicitly passed to newproc, which doesn't 36347c478bd9Sstevel@tonic-gate * respect pool bindings. 36357c478bd9Sstevel@tonic-gate * 36367c478bd9Sstevel@tonic-gate * We hold the pool_lock across the call to newproc() to 36377c478bd9Sstevel@tonic-gate * close the obvious race: the pool's scheduling class 36387c478bd9Sstevel@tonic-gate * could change before we manage to create the LWP with 36397c478bd9Sstevel@tonic-gate * classid 'cid'. 36407c478bd9Sstevel@tonic-gate */ 36417c478bd9Sstevel@tonic-gate pool_lock(); 36420209230bSgjelinek if (zone->zone_defaultcid > 0) 36430209230bSgjelinek cid = zone->zone_defaultcid; 36440209230bSgjelinek else 36457c478bd9Sstevel@tonic-gate cid = pool_get_class(zone->zone_pool); 36467c478bd9Sstevel@tonic-gate if (cid == -1) 36477c478bd9Sstevel@tonic-gate cid = defaultcid; 36487c478bd9Sstevel@tonic-gate 36497c478bd9Sstevel@tonic-gate /* 36507c478bd9Sstevel@tonic-gate * If this fails, zone_boot will ultimately fail. The 36517c478bd9Sstevel@tonic-gate * state of the zone will be set to SHUTTING_DOWN-- userland 36527c478bd9Sstevel@tonic-gate * will have to tear down the zone, and fail, or try again. 36537c478bd9Sstevel@tonic-gate */ 36543f2f09c1Sdp if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid, 365535a5a358SJonathan Adams minclsyspri - 1, &ct, 0)) != 0) { 36567c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 36577c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 36587c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 36597c478bd9Sstevel@tonic-gate } 36607c478bd9Sstevel@tonic-gate pool_unlock(); 36617c478bd9Sstevel@tonic-gate } 36627c478bd9Sstevel@tonic-gate 36637c478bd9Sstevel@tonic-gate /* 36647c478bd9Sstevel@tonic-gate * Wait for zone_destroy() to be called. This is what we spend 36657c478bd9Sstevel@tonic-gate * most of our life doing. 36667c478bd9Sstevel@tonic-gate */ 36677c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 36687c478bd9Sstevel@tonic-gate 36697c478bd9Sstevel@tonic-gate if (ct) 36707c478bd9Sstevel@tonic-gate /* 36717c478bd9Sstevel@tonic-gate * At this point the process contract should be empty. 36727c478bd9Sstevel@tonic-gate * (Though if it isn't, it's not the end of the world.) 36737c478bd9Sstevel@tonic-gate */ 36747c478bd9Sstevel@tonic-gate VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 36757c478bd9Sstevel@tonic-gate 36767c478bd9Sstevel@tonic-gate /* 36777c478bd9Sstevel@tonic-gate * Allow kcred to be freed when all referring processes 36787c478bd9Sstevel@tonic-gate * (including this one) go away. We can't just do this in 36797c478bd9Sstevel@tonic-gate * zone_free because we need to wait for the zone_cred_ref to 36807c478bd9Sstevel@tonic-gate * drop to 0 before calling zone_free, and the existence of 36817c478bd9Sstevel@tonic-gate * zone_kcred will prevent that. Thus, we call crfree here to 36827c478bd9Sstevel@tonic-gate * balance the crdup in zone_create. The crhold calls earlier 36837c478bd9Sstevel@tonic-gate * in zsched will be dropped when the thread and process exit. 36847c478bd9Sstevel@tonic-gate */ 36857c478bd9Sstevel@tonic-gate crfree(zone->zone_kcred); 36867c478bd9Sstevel@tonic-gate zone->zone_kcred = NULL; 36877c478bd9Sstevel@tonic-gate 36887c478bd9Sstevel@tonic-gate exit(CLD_EXITED, 0); 36897c478bd9Sstevel@tonic-gate } 36907c478bd9Sstevel@tonic-gate 36917c478bd9Sstevel@tonic-gate /* 36927c478bd9Sstevel@tonic-gate * Helper function to determine if there are any submounts of the 36937c478bd9Sstevel@tonic-gate * provided path. Used to make sure the zone doesn't "inherit" any 36947c478bd9Sstevel@tonic-gate * mounts from before it is created. 36957c478bd9Sstevel@tonic-gate */ 36967c478bd9Sstevel@tonic-gate static uint_t 36977c478bd9Sstevel@tonic-gate zone_mount_count(const char *rootpath) 36987c478bd9Sstevel@tonic-gate { 36997c478bd9Sstevel@tonic-gate vfs_t *vfsp; 37007c478bd9Sstevel@tonic-gate uint_t count = 0; 37017c478bd9Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 37027c478bd9Sstevel@tonic-gate 37037c478bd9Sstevel@tonic-gate /* 37047c478bd9Sstevel@tonic-gate * Holding zonehash_lock prevents race conditions with 37057c478bd9Sstevel@tonic-gate * vfs_list_add()/vfs_list_remove() since we serialize with 37067c478bd9Sstevel@tonic-gate * zone_find_by_path(). 37077c478bd9Sstevel@tonic-gate */ 37087c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 37097c478bd9Sstevel@tonic-gate /* 37107c478bd9Sstevel@tonic-gate * The rootpath must end with a '/' 37117c478bd9Sstevel@tonic-gate */ 37127c478bd9Sstevel@tonic-gate ASSERT(rootpath[rootpathlen - 1] == '/'); 37137c478bd9Sstevel@tonic-gate 37147c478bd9Sstevel@tonic-gate /* 37157c478bd9Sstevel@tonic-gate * This intentionally does not count the rootpath itself if that 37167c478bd9Sstevel@tonic-gate * happens to be a mount point. 37177c478bd9Sstevel@tonic-gate */ 37187c478bd9Sstevel@tonic-gate vfs_list_read_lock(); 37197c478bd9Sstevel@tonic-gate vfsp = rootvfs; 37207c478bd9Sstevel@tonic-gate do { 37217c478bd9Sstevel@tonic-gate if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 37227c478bd9Sstevel@tonic-gate rootpathlen) == 0) 37237c478bd9Sstevel@tonic-gate count++; 37247c478bd9Sstevel@tonic-gate vfsp = vfsp->vfs_next; 37257c478bd9Sstevel@tonic-gate } while (vfsp != rootvfs); 37267c478bd9Sstevel@tonic-gate vfs_list_unlock(); 37277c478bd9Sstevel@tonic-gate return (count); 37287c478bd9Sstevel@tonic-gate } 37297c478bd9Sstevel@tonic-gate 37307c478bd9Sstevel@tonic-gate /* 37317c478bd9Sstevel@tonic-gate * Helper function to make sure that a zone created on 'rootpath' 37327c478bd9Sstevel@tonic-gate * wouldn't end up containing other zones' rootpaths. 37337c478bd9Sstevel@tonic-gate */ 37347c478bd9Sstevel@tonic-gate static boolean_t 37357c478bd9Sstevel@tonic-gate zone_is_nested(const char *rootpath) 37367c478bd9Sstevel@tonic-gate { 37377c478bd9Sstevel@tonic-gate zone_t *zone; 37387c478bd9Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 37397c478bd9Sstevel@tonic-gate size_t len; 37407c478bd9Sstevel@tonic-gate 37417c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 37427c478bd9Sstevel@tonic-gate 37432bb88978SDhanaraj M /* 37442bb88978SDhanaraj M * zone_set_root() appended '/' and '\0' at the end of rootpath 37452bb88978SDhanaraj M */ 37462bb88978SDhanaraj M if ((rootpathlen <= 3) && (rootpath[0] == '/') && 37472bb88978SDhanaraj M (rootpath[1] == '/') && (rootpath[2] == '\0')) 37482bb88978SDhanaraj M return (B_TRUE); 37492bb88978SDhanaraj M 37507c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 37517c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 37527c478bd9Sstevel@tonic-gate if (zone == global_zone) 37537c478bd9Sstevel@tonic-gate continue; 37547c478bd9Sstevel@tonic-gate len = strlen(zone->zone_rootpath); 37557c478bd9Sstevel@tonic-gate if (strncmp(rootpath, zone->zone_rootpath, 37567c478bd9Sstevel@tonic-gate MIN(rootpathlen, len)) == 0) 37577c478bd9Sstevel@tonic-gate return (B_TRUE); 37587c478bd9Sstevel@tonic-gate } 37597c478bd9Sstevel@tonic-gate return (B_FALSE); 37607c478bd9Sstevel@tonic-gate } 37617c478bd9Sstevel@tonic-gate 37627c478bd9Sstevel@tonic-gate static int 3763821c4a97Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs, 3764821c4a97Sdp size_t zone_privssz) 37657c478bd9Sstevel@tonic-gate { 37667c478bd9Sstevel@tonic-gate priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 37677c478bd9Sstevel@tonic-gate 3768821c4a97Sdp if (zone_privssz < sizeof (priv_set_t)) 3769821c4a97Sdp return (set_errno(ENOMEM)); 3770821c4a97Sdp 37717c478bd9Sstevel@tonic-gate if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 37727c478bd9Sstevel@tonic-gate kmem_free(privs, sizeof (priv_set_t)); 37737c478bd9Sstevel@tonic-gate return (EFAULT); 37747c478bd9Sstevel@tonic-gate } 37757c478bd9Sstevel@tonic-gate 37767c478bd9Sstevel@tonic-gate zone->zone_privset = privs; 37777c478bd9Sstevel@tonic-gate return (0); 37787c478bd9Sstevel@tonic-gate } 37797c478bd9Sstevel@tonic-gate 37807c478bd9Sstevel@tonic-gate /* 37817c478bd9Sstevel@tonic-gate * We make creative use of nvlists to pass in rctls from userland. The list is 37827c478bd9Sstevel@tonic-gate * a list of the following structures: 37837c478bd9Sstevel@tonic-gate * 37847c478bd9Sstevel@tonic-gate * (name = rctl_name, value = nvpair_list_array) 37857c478bd9Sstevel@tonic-gate * 37867c478bd9Sstevel@tonic-gate * Where each element of the nvpair_list_array is of the form: 37877c478bd9Sstevel@tonic-gate * 37887c478bd9Sstevel@tonic-gate * [(name = "privilege", value = RCPRIV_PRIVILEGED), 37897c478bd9Sstevel@tonic-gate * (name = "limit", value = uint64_t), 37907c478bd9Sstevel@tonic-gate * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 37917c478bd9Sstevel@tonic-gate */ 37927c478bd9Sstevel@tonic-gate static int 37937c478bd9Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 37947c478bd9Sstevel@tonic-gate { 37957c478bd9Sstevel@tonic-gate nvpair_t *nvp = NULL; 37967c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL; 37977c478bd9Sstevel@tonic-gate char *kbuf; 37987c478bd9Sstevel@tonic-gate int error; 37997c478bd9Sstevel@tonic-gate rctl_val_t rv; 38007c478bd9Sstevel@tonic-gate 38017c478bd9Sstevel@tonic-gate *nvlp = NULL; 38027c478bd9Sstevel@tonic-gate 38037c478bd9Sstevel@tonic-gate if (buflen == 0) 38047c478bd9Sstevel@tonic-gate return (0); 38057c478bd9Sstevel@tonic-gate 38067c478bd9Sstevel@tonic-gate if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 38077c478bd9Sstevel@tonic-gate return (ENOMEM); 38087c478bd9Sstevel@tonic-gate if (copyin(ubuf, kbuf, buflen)) { 38097c478bd9Sstevel@tonic-gate error = EFAULT; 38107c478bd9Sstevel@tonic-gate goto out; 38117c478bd9Sstevel@tonic-gate } 38127c478bd9Sstevel@tonic-gate if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 38137c478bd9Sstevel@tonic-gate /* 38147c478bd9Sstevel@tonic-gate * nvl may have been allocated/free'd, but the value set to 38157c478bd9Sstevel@tonic-gate * non-NULL, so we reset it here. 38167c478bd9Sstevel@tonic-gate */ 38177c478bd9Sstevel@tonic-gate nvl = NULL; 38187c478bd9Sstevel@tonic-gate error = EINVAL; 38197c478bd9Sstevel@tonic-gate goto out; 38207c478bd9Sstevel@tonic-gate } 38217c478bd9Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 38227c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rde; 38237c478bd9Sstevel@tonic-gate rctl_hndl_t hndl; 38247c478bd9Sstevel@tonic-gate nvlist_t **nvlarray; 38257c478bd9Sstevel@tonic-gate uint_t i, nelem; 38267c478bd9Sstevel@tonic-gate char *name; 38277c478bd9Sstevel@tonic-gate 38287c478bd9Sstevel@tonic-gate error = EINVAL; 38297c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 38307c478bd9Sstevel@tonic-gate if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 38317c478bd9Sstevel@tonic-gate != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 38327c478bd9Sstevel@tonic-gate goto out; 38337c478bd9Sstevel@tonic-gate } 38347c478bd9Sstevel@tonic-gate if ((hndl = rctl_hndl_lookup(name)) == -1) { 38357c478bd9Sstevel@tonic-gate goto out; 38367c478bd9Sstevel@tonic-gate } 38377c478bd9Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 38387c478bd9Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 38397c478bd9Sstevel@tonic-gate ASSERT(error == 0); 38407c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 38417c478bd9Sstevel@tonic-gate if (error = nvlist2rctlval(nvlarray[i], &rv)) 38427c478bd9Sstevel@tonic-gate goto out; 38437c478bd9Sstevel@tonic-gate } 38447c478bd9Sstevel@tonic-gate if (rctl_invalid_value(rde, &rv)) { 38457c478bd9Sstevel@tonic-gate error = EINVAL; 38467c478bd9Sstevel@tonic-gate goto out; 38477c478bd9Sstevel@tonic-gate } 38487c478bd9Sstevel@tonic-gate } 38497c478bd9Sstevel@tonic-gate error = 0; 38507c478bd9Sstevel@tonic-gate *nvlp = nvl; 38517c478bd9Sstevel@tonic-gate out: 38527c478bd9Sstevel@tonic-gate kmem_free(kbuf, buflen); 38537c478bd9Sstevel@tonic-gate if (error && nvl != NULL) 38547c478bd9Sstevel@tonic-gate nvlist_free(nvl); 38557c478bd9Sstevel@tonic-gate return (error); 38567c478bd9Sstevel@tonic-gate } 38577c478bd9Sstevel@tonic-gate 38587c478bd9Sstevel@tonic-gate int 38597c478bd9Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) { 38607c478bd9Sstevel@tonic-gate if (er_out != NULL) { 38617c478bd9Sstevel@tonic-gate if (copyout(&er_ext, er_out, sizeof (int))) { 38627c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 38637c478bd9Sstevel@tonic-gate } 38647c478bd9Sstevel@tonic-gate } 38657c478bd9Sstevel@tonic-gate return (set_errno(er_error)); 38667c478bd9Sstevel@tonic-gate } 38677c478bd9Sstevel@tonic-gate 386845916cd2Sjpk static int 386945916cd2Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi) 387045916cd2Sjpk { 387145916cd2Sjpk ts_label_t *tsl; 387245916cd2Sjpk bslabel_t blab; 387345916cd2Sjpk 387445916cd2Sjpk /* Get label from user */ 387545916cd2Sjpk if (copyin(lab, &blab, sizeof (blab)) != 0) 387645916cd2Sjpk return (EFAULT); 387745916cd2Sjpk tsl = labelalloc(&blab, doi, KM_NOSLEEP); 387845916cd2Sjpk if (tsl == NULL) 387945916cd2Sjpk return (ENOMEM); 388045916cd2Sjpk 388145916cd2Sjpk zone->zone_slabel = tsl; 388245916cd2Sjpk return (0); 388345916cd2Sjpk } 388445916cd2Sjpk 38857c478bd9Sstevel@tonic-gate /* 3886fa9e4066Sahrens * Parses a comma-separated list of ZFS datasets into a per-zone dictionary. 3887fa9e4066Sahrens */ 3888fa9e4066Sahrens static int 3889fa9e4066Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen) 3890fa9e4066Sahrens { 3891fa9e4066Sahrens char *kbuf; 3892fa9e4066Sahrens char *dataset, *next; 3893fa9e4066Sahrens zone_dataset_t *zd; 3894fa9e4066Sahrens size_t len; 3895fa9e4066Sahrens 3896fa9e4066Sahrens if (ubuf == NULL || buflen == 0) 3897fa9e4066Sahrens return (0); 3898fa9e4066Sahrens 3899fa9e4066Sahrens if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 3900fa9e4066Sahrens return (ENOMEM); 3901fa9e4066Sahrens 3902fa9e4066Sahrens if (copyin(ubuf, kbuf, buflen) != 0) { 3903fa9e4066Sahrens kmem_free(kbuf, buflen); 3904fa9e4066Sahrens return (EFAULT); 3905fa9e4066Sahrens } 3906fa9e4066Sahrens 3907fa9e4066Sahrens dataset = next = kbuf; 3908fa9e4066Sahrens for (;;) { 3909fa9e4066Sahrens zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP); 3910fa9e4066Sahrens 3911fa9e4066Sahrens next = strchr(dataset, ','); 3912fa9e4066Sahrens 3913fa9e4066Sahrens if (next == NULL) 3914fa9e4066Sahrens len = strlen(dataset); 3915fa9e4066Sahrens else 3916fa9e4066Sahrens len = next - dataset; 3917fa9e4066Sahrens 3918fa9e4066Sahrens zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP); 3919fa9e4066Sahrens bcopy(dataset, zd->zd_dataset, len); 3920fa9e4066Sahrens zd->zd_dataset[len] = '\0'; 3921fa9e4066Sahrens 3922fa9e4066Sahrens list_insert_head(&zone->zone_datasets, zd); 3923fa9e4066Sahrens 3924fa9e4066Sahrens if (next == NULL) 3925fa9e4066Sahrens break; 3926fa9e4066Sahrens 3927fa9e4066Sahrens dataset = next + 1; 3928fa9e4066Sahrens } 3929fa9e4066Sahrens 3930fa9e4066Sahrens kmem_free(kbuf, buflen); 3931fa9e4066Sahrens return (0); 3932fa9e4066Sahrens } 3933fa9e4066Sahrens 3934fa9e4066Sahrens /* 39357c478bd9Sstevel@tonic-gate * System call to create/initialize a new zone named 'zone_name', rooted 39367c478bd9Sstevel@tonic-gate * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 393745916cd2Sjpk * and initialized with the zone-wide rctls described in 'rctlbuf', and 393845916cd2Sjpk * with labeling set by 'match', 'doi', and 'label'. 39397c478bd9Sstevel@tonic-gate * 39407c478bd9Sstevel@tonic-gate * If extended error is non-null, we may use it to return more detailed 39417c478bd9Sstevel@tonic-gate * error information. 39427c478bd9Sstevel@tonic-gate */ 39437c478bd9Sstevel@tonic-gate static zoneid_t 39447c478bd9Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root, 3945821c4a97Sdp const priv_set_t *zone_privs, size_t zone_privssz, 3946821c4a97Sdp caddr_t rctlbuf, size_t rctlbufsz, 394745916cd2Sjpk caddr_t zfsbuf, size_t zfsbufsz, int *extended_error, 3948f4b3ec61Sdh155122 int match, uint32_t doi, const bslabel_t *label, 3949f4b3ec61Sdh155122 int flags) 39507c478bd9Sstevel@tonic-gate { 39517c478bd9Sstevel@tonic-gate struct zsched_arg zarg; 39527c478bd9Sstevel@tonic-gate nvlist_t *rctls = NULL; 39537c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 39547c478bd9Sstevel@tonic-gate zone_t *zone, *ztmp; 39557c478bd9Sstevel@tonic-gate zoneid_t zoneid; 39567c478bd9Sstevel@tonic-gate int error; 39577c478bd9Sstevel@tonic-gate int error2 = 0; 39587c478bd9Sstevel@tonic-gate char *str; 39597c478bd9Sstevel@tonic-gate cred_t *zkcr; 396048451833Scarlsonj boolean_t insert_label_hash; 39617c478bd9Sstevel@tonic-gate 39627c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 39637c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 39647c478bd9Sstevel@tonic-gate 39657c478bd9Sstevel@tonic-gate /* can't boot zone from within chroot environment */ 39667c478bd9Sstevel@tonic-gate if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 39677c478bd9Sstevel@tonic-gate return (zone_create_error(ENOTSUP, ZE_CHROOTED, 39687c478bd9Sstevel@tonic-gate extended_error)); 39697c478bd9Sstevel@tonic-gate 39707c478bd9Sstevel@tonic-gate zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 39717c478bd9Sstevel@tonic-gate zoneid = zone->zone_id = id_alloc(zoneid_space); 39727c478bd9Sstevel@tonic-gate zone->zone_status = ZONE_IS_UNINITIALIZED; 39737c478bd9Sstevel@tonic-gate zone->zone_pool = pool_default; 39747c478bd9Sstevel@tonic-gate zone->zone_pool_mod = gethrtime(); 39757c478bd9Sstevel@tonic-gate zone->zone_psetid = ZONE_PS_INVAL; 39767c478bd9Sstevel@tonic-gate zone->zone_ncpus = 0; 39777c478bd9Sstevel@tonic-gate zone->zone_ncpus_online = 0; 39789acbbeafSnn35248 zone->zone_restart_init = B_TRUE; 39799acbbeafSnn35248 zone->zone_brand = &native_brand; 39809acbbeafSnn35248 zone->zone_initname = NULL; 39817c478bd9Sstevel@tonic-gate mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 39827c478bd9Sstevel@tonic-gate mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 39830209230bSgjelinek mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 39847c478bd9Sstevel@tonic-gate cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 39857c478bd9Sstevel@tonic-gate list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 39867c478bd9Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 3987fa9e4066Sahrens list_create(&zone->zone_datasets, sizeof (zone_dataset_t), 3988fa9e4066Sahrens offsetof(zone_dataset_t, zd_linkage)); 39892b24ab6bSSebastien Roy list_create(&zone->zone_dl_list, sizeof (zone_dl_t), 39902b24ab6bSSebastien Roy offsetof(zone_dl_t, zdl_linkage)); 399145916cd2Sjpk rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 3992835ee219SRobert Harris rw_init(&zone->zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL); 39937c478bd9Sstevel@tonic-gate 3994f4b3ec61Sdh155122 if (flags & ZCF_NET_EXCL) { 3995f4b3ec61Sdh155122 zone->zone_flags |= ZF_NET_EXCL; 3996f4b3ec61Sdh155122 } 3997f4b3ec61Sdh155122 39987c478bd9Sstevel@tonic-gate if ((error = zone_set_name(zone, zone_name)) != 0) { 39997c478bd9Sstevel@tonic-gate zone_free(zone); 40007c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 40017c478bd9Sstevel@tonic-gate } 40027c478bd9Sstevel@tonic-gate 40037c478bd9Sstevel@tonic-gate if ((error = zone_set_root(zone, zone_root)) != 0) { 40047c478bd9Sstevel@tonic-gate zone_free(zone); 40057c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 40067c478bd9Sstevel@tonic-gate } 4007821c4a97Sdp if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) { 40087c478bd9Sstevel@tonic-gate zone_free(zone); 40097c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 40107c478bd9Sstevel@tonic-gate } 40117c478bd9Sstevel@tonic-gate 40127c478bd9Sstevel@tonic-gate /* initialize node name to be the same as zone name */ 40137c478bd9Sstevel@tonic-gate zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 40147c478bd9Sstevel@tonic-gate (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 40157c478bd9Sstevel@tonic-gate zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 40167c478bd9Sstevel@tonic-gate 40177c478bd9Sstevel@tonic-gate zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 40187c478bd9Sstevel@tonic-gate zone->zone_domain[0] = '\0'; 40195679c89fSjv227347 zone->zone_hostid = HW_INVALID_HOSTID; 40207c478bd9Sstevel@tonic-gate zone->zone_shares = 1; 4021824c205fSml93401 zone->zone_shmmax = 0; 4022824c205fSml93401 zone->zone_ipc.ipcq_shmmni = 0; 4023824c205fSml93401 zone->zone_ipc.ipcq_semmni = 0; 4024824c205fSml93401 zone->zone_ipc.ipcq_msgmni = 0; 40257c478bd9Sstevel@tonic-gate zone->zone_bootargs = NULL; 40260fbb751dSJohn Levon zone->zone_fs_allowed = NULL; 40273f2f09c1Sdp zone->zone_initname = 40283f2f09c1Sdp kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP); 40293f2f09c1Sdp (void) strcpy(zone->zone_initname, zone_default_initname); 40300209230bSgjelinek zone->zone_nlwps = 0; 40310209230bSgjelinek zone->zone_nlwps_ctl = INT_MAX; 4032ff19e029SMenno Lageman zone->zone_nprocs = 0; 4033ff19e029SMenno Lageman zone->zone_nprocs_ctl = INT_MAX; 4034c6939658Ssl108498 zone->zone_locked_mem = 0; 4035c6939658Ssl108498 zone->zone_locked_mem_ctl = UINT64_MAX; 40360209230bSgjelinek zone->zone_max_swap = 0; 40370209230bSgjelinek zone->zone_max_swap_ctl = UINT64_MAX; 40380fbb751dSJohn Levon zone->zone_max_lofi = 0; 40390fbb751dSJohn Levon zone->zone_max_lofi_ctl = UINT64_MAX; 40400209230bSgjelinek zone0.zone_lockedmem_kstat = NULL; 40410209230bSgjelinek zone0.zone_swapresv_kstat = NULL; 40427c478bd9Sstevel@tonic-gate 40437c478bd9Sstevel@tonic-gate /* 40447c478bd9Sstevel@tonic-gate * Zsched initializes the rctls. 40457c478bd9Sstevel@tonic-gate */ 40467c478bd9Sstevel@tonic-gate zone->zone_rctls = NULL; 40477c478bd9Sstevel@tonic-gate 40487c478bd9Sstevel@tonic-gate if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 40497c478bd9Sstevel@tonic-gate zone_free(zone); 40507c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 40517c478bd9Sstevel@tonic-gate } 40527c478bd9Sstevel@tonic-gate 4053fa9e4066Sahrens if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) { 4054fa9e4066Sahrens zone_free(zone); 4055fa9e4066Sahrens return (set_errno(error)); 4056fa9e4066Sahrens } 4057fa9e4066Sahrens 40587c478bd9Sstevel@tonic-gate /* 405945916cd2Sjpk * Read in the trusted system parameters: 406045916cd2Sjpk * match flag and sensitivity label. 406145916cd2Sjpk */ 406245916cd2Sjpk zone->zone_match = match; 406348451833Scarlsonj if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 40647e6639c2Skp158701 /* Fail if requested to set doi to anything but system's doi */ 40657e6639c2Skp158701 if (doi != 0 && doi != default_doi) { 40667e6639c2Skp158701 zone_free(zone); 40677e6639c2Skp158701 return (set_errno(EINVAL)); 40687e6639c2Skp158701 } 40697e6639c2Skp158701 /* Always apply system's doi to the zone */ 40707e6639c2Skp158701 error = zone_set_label(zone, label, default_doi); 407145916cd2Sjpk if (error != 0) { 407245916cd2Sjpk zone_free(zone); 407345916cd2Sjpk return (set_errno(error)); 407445916cd2Sjpk } 407548451833Scarlsonj insert_label_hash = B_TRUE; 407645916cd2Sjpk } else { 407745916cd2Sjpk /* all zones get an admin_low label if system is not labeled */ 407845916cd2Sjpk zone->zone_slabel = l_admin_low; 407945916cd2Sjpk label_hold(l_admin_low); 408048451833Scarlsonj insert_label_hash = B_FALSE; 408145916cd2Sjpk } 408245916cd2Sjpk 408345916cd2Sjpk /* 40847c478bd9Sstevel@tonic-gate * Stop all lwps since that's what normally happens as part of fork(). 40857c478bd9Sstevel@tonic-gate * This needs to happen before we grab any locks to avoid deadlock 40867c478bd9Sstevel@tonic-gate * (another lwp in the process could be waiting for the held lock). 40877c478bd9Sstevel@tonic-gate */ 40887c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 40897c478bd9Sstevel@tonic-gate zone_free(zone); 40907c478bd9Sstevel@tonic-gate if (rctls) 40917c478bd9Sstevel@tonic-gate nvlist_free(rctls); 40927c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 40937c478bd9Sstevel@tonic-gate } 40947c478bd9Sstevel@tonic-gate 40957c478bd9Sstevel@tonic-gate if (block_mounts() == 0) { 40967c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 40977c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 40987c478bd9Sstevel@tonic-gate continuelwps(pp); 40997c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 41007c478bd9Sstevel@tonic-gate zone_free(zone); 41017c478bd9Sstevel@tonic-gate if (rctls) 41027c478bd9Sstevel@tonic-gate nvlist_free(rctls); 41037c478bd9Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 41047c478bd9Sstevel@tonic-gate } 41057c478bd9Sstevel@tonic-gate 41067c478bd9Sstevel@tonic-gate /* 41077c478bd9Sstevel@tonic-gate * Set up credential for kernel access. After this, any errors 41087c478bd9Sstevel@tonic-gate * should go through the dance in errout rather than calling 41097c478bd9Sstevel@tonic-gate * zone_free directly. 41107c478bd9Sstevel@tonic-gate */ 41117c478bd9Sstevel@tonic-gate zone->zone_kcred = crdup(kcred); 41127c478bd9Sstevel@tonic-gate crsetzone(zone->zone_kcred, zone); 41137c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 41147c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 41157c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 41167c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 41177c478bd9Sstevel@tonic-gate 41187c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 41197c478bd9Sstevel@tonic-gate /* 41207c478bd9Sstevel@tonic-gate * Make sure zone doesn't already exist. 412145916cd2Sjpk * 412245916cd2Sjpk * If the system and zone are labeled, 412345916cd2Sjpk * make sure no other zone exists that has the same label. 41247c478bd9Sstevel@tonic-gate */ 412545916cd2Sjpk if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL || 412648451833Scarlsonj (insert_label_hash && 412745916cd2Sjpk (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) { 41287c478bd9Sstevel@tonic-gate zone_status_t status; 41297c478bd9Sstevel@tonic-gate 41307c478bd9Sstevel@tonic-gate status = zone_status_get(ztmp); 41317c478bd9Sstevel@tonic-gate if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 41327c478bd9Sstevel@tonic-gate error = EEXIST; 41337c478bd9Sstevel@tonic-gate else 41347c478bd9Sstevel@tonic-gate error = EBUSY; 41358c55461bSton 41368c55461bSton if (insert_label_hash) 41378c55461bSton error2 = ZE_LABELINUSE; 41388c55461bSton 41397c478bd9Sstevel@tonic-gate goto errout; 41407c478bd9Sstevel@tonic-gate } 41417c478bd9Sstevel@tonic-gate 41427c478bd9Sstevel@tonic-gate /* 41437c478bd9Sstevel@tonic-gate * Don't allow zone creations which would cause one zone's rootpath to 41447c478bd9Sstevel@tonic-gate * be accessible from that of another (non-global) zone. 41457c478bd9Sstevel@tonic-gate */ 41467c478bd9Sstevel@tonic-gate if (zone_is_nested(zone->zone_rootpath)) { 41477c478bd9Sstevel@tonic-gate error = EBUSY; 41487c478bd9Sstevel@tonic-gate goto errout; 41497c478bd9Sstevel@tonic-gate } 41507c478bd9Sstevel@tonic-gate 41517c478bd9Sstevel@tonic-gate ASSERT(zonecount != 0); /* check for leaks */ 41527c478bd9Sstevel@tonic-gate if (zonecount + 1 > maxzones) { 41537c478bd9Sstevel@tonic-gate error = ENOMEM; 41547c478bd9Sstevel@tonic-gate goto errout; 41557c478bd9Sstevel@tonic-gate } 41567c478bd9Sstevel@tonic-gate 41577c478bd9Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 41587c478bd9Sstevel@tonic-gate error = EBUSY; 41597c478bd9Sstevel@tonic-gate error2 = ZE_AREMOUNTS; 41607c478bd9Sstevel@tonic-gate goto errout; 41617c478bd9Sstevel@tonic-gate } 41627c478bd9Sstevel@tonic-gate 41637c478bd9Sstevel@tonic-gate /* 41647c478bd9Sstevel@tonic-gate * Zone is still incomplete, but we need to drop all locks while 41657c478bd9Sstevel@tonic-gate * zsched() initializes this zone's kernel process. We 41667c478bd9Sstevel@tonic-gate * optimistically add the zone to the hashtable and associated 41677c478bd9Sstevel@tonic-gate * lists so a parallel zone_create() doesn't try to create the 41687c478bd9Sstevel@tonic-gate * same zone. 41697c478bd9Sstevel@tonic-gate */ 41707c478bd9Sstevel@tonic-gate zonecount++; 41717c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, 41727c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id, 41737c478bd9Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 41747c478bd9Sstevel@tonic-gate str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 41757c478bd9Sstevel@tonic-gate (void) strcpy(str, zone->zone_name); 41767c478bd9Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 41777c478bd9Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 417848451833Scarlsonj if (insert_label_hash) { 417945916cd2Sjpk (void) mod_hash_insert(zonehashbylabel, 418045916cd2Sjpk (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone); 418148451833Scarlsonj zone->zone_flags |= ZF_HASHED_LABEL; 418245916cd2Sjpk } 418345916cd2Sjpk 41847c478bd9Sstevel@tonic-gate /* 41857c478bd9Sstevel@tonic-gate * Insert into active list. At this point there are no 'hold's 41867c478bd9Sstevel@tonic-gate * on the zone, but everyone else knows not to use it, so we can 41877c478bd9Sstevel@tonic-gate * continue to use it. zsched() will do a zone_hold() if the 41887c478bd9Sstevel@tonic-gate * newproc() is successful. 41897c478bd9Sstevel@tonic-gate */ 41907c478bd9Sstevel@tonic-gate list_insert_tail(&zone_active, zone); 41917c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 41927c478bd9Sstevel@tonic-gate 41937c478bd9Sstevel@tonic-gate zarg.zone = zone; 41947c478bd9Sstevel@tonic-gate zarg.nvlist = rctls; 41957c478bd9Sstevel@tonic-gate /* 41967c478bd9Sstevel@tonic-gate * The process, task, and project rctls are probably wrong; 41977c478bd9Sstevel@tonic-gate * we need an interface to get the default values of all rctls, 41987c478bd9Sstevel@tonic-gate * and initialize zsched appropriately. I'm not sure that that 41997c478bd9Sstevel@tonic-gate * makes much of a difference, though. 42007c478bd9Sstevel@tonic-gate */ 420135a5a358SJonathan Adams error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL, 0); 420235a5a358SJonathan Adams if (error != 0) { 42037c478bd9Sstevel@tonic-gate /* 42047c478bd9Sstevel@tonic-gate * We need to undo all globally visible state. 42057c478bd9Sstevel@tonic-gate */ 42067c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 42077c478bd9Sstevel@tonic-gate list_remove(&zone_active, zone); 420848451833Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) { 420945916cd2Sjpk ASSERT(zone->zone_slabel != NULL); 421045916cd2Sjpk (void) mod_hash_destroy(zonehashbylabel, 421145916cd2Sjpk (mod_hash_key_t)zone->zone_slabel); 421245916cd2Sjpk } 42137c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 42147c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_name); 42157c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 42167c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 42177c478bd9Sstevel@tonic-gate ASSERT(zonecount > 1); 42187c478bd9Sstevel@tonic-gate zonecount--; 42197c478bd9Sstevel@tonic-gate goto errout; 42207c478bd9Sstevel@tonic-gate } 42217c478bd9Sstevel@tonic-gate 42227c478bd9Sstevel@tonic-gate /* 42237c478bd9Sstevel@tonic-gate * Zone creation can't fail from now on. 42247c478bd9Sstevel@tonic-gate */ 42257c478bd9Sstevel@tonic-gate 42267c478bd9Sstevel@tonic-gate /* 42270209230bSgjelinek * Create zone kstats 42280209230bSgjelinek */ 42290209230bSgjelinek zone_kstat_create(zone); 42300209230bSgjelinek 42310209230bSgjelinek /* 42327c478bd9Sstevel@tonic-gate * Let the other lwps continue. 42337c478bd9Sstevel@tonic-gate */ 42347c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 42357c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 42367c478bd9Sstevel@tonic-gate continuelwps(pp); 42377c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 42387c478bd9Sstevel@tonic-gate 42397c478bd9Sstevel@tonic-gate /* 42407c478bd9Sstevel@tonic-gate * Wait for zsched to finish initializing the zone. 42417c478bd9Sstevel@tonic-gate */ 42427c478bd9Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_READY); 42437c478bd9Sstevel@tonic-gate /* 42447c478bd9Sstevel@tonic-gate * The zone is fully visible, so we can let mounts progress. 42457c478bd9Sstevel@tonic-gate */ 42467c478bd9Sstevel@tonic-gate resume_mounts(); 42477c478bd9Sstevel@tonic-gate if (rctls) 42487c478bd9Sstevel@tonic-gate nvlist_free(rctls); 42497c478bd9Sstevel@tonic-gate 42507c478bd9Sstevel@tonic-gate return (zoneid); 42517c478bd9Sstevel@tonic-gate 42527c478bd9Sstevel@tonic-gate errout: 42537c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42547c478bd9Sstevel@tonic-gate /* 42557c478bd9Sstevel@tonic-gate * Let the other lwps continue. 42567c478bd9Sstevel@tonic-gate */ 42577c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 42587c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 42597c478bd9Sstevel@tonic-gate continuelwps(pp); 42607c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 42617c478bd9Sstevel@tonic-gate 42627c478bd9Sstevel@tonic-gate resume_mounts(); 42637c478bd9Sstevel@tonic-gate if (rctls) 42647c478bd9Sstevel@tonic-gate nvlist_free(rctls); 42657c478bd9Sstevel@tonic-gate /* 42667c478bd9Sstevel@tonic-gate * There is currently one reference to the zone, a cred_ref from 42677c478bd9Sstevel@tonic-gate * zone_kcred. To free the zone, we call crfree, which will call 42687c478bd9Sstevel@tonic-gate * zone_cred_rele, which will call zone_free. 42697c478bd9Sstevel@tonic-gate */ 42707c478bd9Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 42717c478bd9Sstevel@tonic-gate ASSERT(zone->zone_kcred->cr_ref == 1); 42727c478bd9Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 42737c478bd9Sstevel@tonic-gate zkcr = zone->zone_kcred; 42747c478bd9Sstevel@tonic-gate zone->zone_kcred = NULL; 42757c478bd9Sstevel@tonic-gate crfree(zkcr); /* triggers call to zone_free */ 42767c478bd9Sstevel@tonic-gate return (zone_create_error(error, error2, extended_error)); 42777c478bd9Sstevel@tonic-gate } 42787c478bd9Sstevel@tonic-gate 42797c478bd9Sstevel@tonic-gate /* 42807c478bd9Sstevel@tonic-gate * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 42813f2f09c1Sdp * the heavy lifting. initname is the path to the program to launch 42823f2f09c1Sdp * at the "top" of the zone; if this is NULL, we use the system default, 42833f2f09c1Sdp * which is stored at zone_default_initname. 42847c478bd9Sstevel@tonic-gate */ 42857c478bd9Sstevel@tonic-gate static int 42863f2f09c1Sdp zone_boot(zoneid_t zoneid) 42877c478bd9Sstevel@tonic-gate { 42887c478bd9Sstevel@tonic-gate int err; 42897c478bd9Sstevel@tonic-gate zone_t *zone; 42907c478bd9Sstevel@tonic-gate 42917c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 42927c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 42937c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 42947c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 42957c478bd9Sstevel@tonic-gate 42967c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 42977c478bd9Sstevel@tonic-gate /* 42987c478bd9Sstevel@tonic-gate * Look for zone under hash lock to prevent races with calls to 42997c478bd9Sstevel@tonic-gate * zone_shutdown, zone_destroy, etc. 43007c478bd9Sstevel@tonic-gate */ 43017c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 43027c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43037c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 43047c478bd9Sstevel@tonic-gate } 43057c478bd9Sstevel@tonic-gate 43067c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 43077c478bd9Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_READY) { 43087c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 43097c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43107c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 43117c478bd9Sstevel@tonic-gate } 43127c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_BOOTING); 43137c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 43147c478bd9Sstevel@tonic-gate 43157c478bd9Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 43167c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43177c478bd9Sstevel@tonic-gate 43187c478bd9Sstevel@tonic-gate if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 43197c478bd9Sstevel@tonic-gate zone_rele(zone); 43207c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 43217c478bd9Sstevel@tonic-gate } 43227c478bd9Sstevel@tonic-gate 43237c478bd9Sstevel@tonic-gate /* 43247c478bd9Sstevel@tonic-gate * Boot (starting init) might have failed, in which case the zone 43257c478bd9Sstevel@tonic-gate * will go to the SHUTTING_DOWN state; an appropriate errno will 43267c478bd9Sstevel@tonic-gate * be placed in zone->zone_boot_err, and so we return that. 43277c478bd9Sstevel@tonic-gate */ 43287c478bd9Sstevel@tonic-gate err = zone->zone_boot_err; 43297c478bd9Sstevel@tonic-gate zone_rele(zone); 43307c478bd9Sstevel@tonic-gate return (err ? set_errno(err) : 0); 43317c478bd9Sstevel@tonic-gate } 43327c478bd9Sstevel@tonic-gate 43337c478bd9Sstevel@tonic-gate /* 43347c478bd9Sstevel@tonic-gate * Kills all user processes in the zone, waiting for them all to exit 43357c478bd9Sstevel@tonic-gate * before returning. 43367c478bd9Sstevel@tonic-gate */ 43377c478bd9Sstevel@tonic-gate static int 43387c478bd9Sstevel@tonic-gate zone_empty(zone_t *zone) 43397c478bd9Sstevel@tonic-gate { 43407c478bd9Sstevel@tonic-gate int waitstatus; 43417c478bd9Sstevel@tonic-gate 43427c478bd9Sstevel@tonic-gate /* 43437c478bd9Sstevel@tonic-gate * We need to drop zonehash_lock before killing all 43447c478bd9Sstevel@tonic-gate * processes, otherwise we'll deadlock with zone_find_* 43457c478bd9Sstevel@tonic-gate * which can be called from the exit path. 43467c478bd9Sstevel@tonic-gate */ 43477c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 4348d3d50737SRafael Vanoni while ((waitstatus = zone_status_timedwait_sig(zone, 4349d3d50737SRafael Vanoni ddi_get_lbolt() + hz, ZONE_IS_EMPTY)) == -1) { 43507c478bd9Sstevel@tonic-gate killall(zone->zone_id); 43517c478bd9Sstevel@tonic-gate } 43527c478bd9Sstevel@tonic-gate /* 43537c478bd9Sstevel@tonic-gate * return EINTR if we were signaled 43547c478bd9Sstevel@tonic-gate */ 43557c478bd9Sstevel@tonic-gate if (waitstatus == 0) 43567c478bd9Sstevel@tonic-gate return (EINTR); 43577c478bd9Sstevel@tonic-gate return (0); 43587c478bd9Sstevel@tonic-gate } 43597c478bd9Sstevel@tonic-gate 43607c478bd9Sstevel@tonic-gate /* 436145916cd2Sjpk * This function implements the policy for zone visibility. 436245916cd2Sjpk * 436345916cd2Sjpk * In standard Solaris, a non-global zone can only see itself. 436445916cd2Sjpk * 436545916cd2Sjpk * In Trusted Extensions, a labeled zone can lookup any zone whose label 436645916cd2Sjpk * it dominates. For this test, the label of the global zone is treated as 436745916cd2Sjpk * admin_high so it is special-cased instead of being checked for dominance. 436845916cd2Sjpk * 436945916cd2Sjpk * Returns true if zone attributes are viewable, false otherwise. 437045916cd2Sjpk */ 437145916cd2Sjpk static boolean_t 437245916cd2Sjpk zone_list_access(zone_t *zone) 437345916cd2Sjpk { 437445916cd2Sjpk 437545916cd2Sjpk if (curproc->p_zone == global_zone || 437645916cd2Sjpk curproc->p_zone == zone) { 437745916cd2Sjpk return (B_TRUE); 437848451833Scarlsonj } else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 437945916cd2Sjpk bslabel_t *curproc_label; 438045916cd2Sjpk bslabel_t *zone_label; 438145916cd2Sjpk 438245916cd2Sjpk curproc_label = label2bslabel(curproc->p_zone->zone_slabel); 438345916cd2Sjpk zone_label = label2bslabel(zone->zone_slabel); 438445916cd2Sjpk 438545916cd2Sjpk if (zone->zone_id != GLOBAL_ZONEID && 438645916cd2Sjpk bldominates(curproc_label, zone_label)) { 438745916cd2Sjpk return (B_TRUE); 438845916cd2Sjpk } else { 438945916cd2Sjpk return (B_FALSE); 439045916cd2Sjpk } 439145916cd2Sjpk } else { 439245916cd2Sjpk return (B_FALSE); 439345916cd2Sjpk } 439445916cd2Sjpk } 439545916cd2Sjpk 439645916cd2Sjpk /* 43977c478bd9Sstevel@tonic-gate * Systemcall to start the zone's halt sequence. By the time this 43987c478bd9Sstevel@tonic-gate * function successfully returns, all user processes and kernel threads 43997c478bd9Sstevel@tonic-gate * executing in it will have exited, ZSD shutdown callbacks executed, 44007c478bd9Sstevel@tonic-gate * and the zone status set to ZONE_IS_DOWN. 44017c478bd9Sstevel@tonic-gate * 44027c478bd9Sstevel@tonic-gate * It is possible that the call will interrupt itself if the caller is the 44037c478bd9Sstevel@tonic-gate * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 44047c478bd9Sstevel@tonic-gate */ 44057c478bd9Sstevel@tonic-gate static int 44067c478bd9Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 44077c478bd9Sstevel@tonic-gate { 44087c478bd9Sstevel@tonic-gate int error; 44097c478bd9Sstevel@tonic-gate zone_t *zone; 44107c478bd9Sstevel@tonic-gate zone_status_t status; 44117c478bd9Sstevel@tonic-gate 44127c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 44137c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 44147c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 44157c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 44167c478bd9Sstevel@tonic-gate 44177c478bd9Sstevel@tonic-gate /* 44187c478bd9Sstevel@tonic-gate * Block mounts so that VFS_MOUNT() can get an accurate view of 44197c478bd9Sstevel@tonic-gate * the zone's status with regards to ZONE_IS_SHUTTING down. 44207c478bd9Sstevel@tonic-gate * 44217c478bd9Sstevel@tonic-gate * e.g. NFS can fail the mount if it determines that the zone 44227c478bd9Sstevel@tonic-gate * has already begun the shutdown sequence. 44237c478bd9Sstevel@tonic-gate */ 44247c478bd9Sstevel@tonic-gate if (block_mounts() == 0) 44257c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 44267c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 44277c478bd9Sstevel@tonic-gate /* 44287c478bd9Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 44297c478bd9Sstevel@tonic-gate * calls to zone_shutdown and zone_destroy. 44307c478bd9Sstevel@tonic-gate */ 44317c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 44327c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44337c478bd9Sstevel@tonic-gate resume_mounts(); 44347c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 44357c478bd9Sstevel@tonic-gate } 44367c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 44377c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 44387c478bd9Sstevel@tonic-gate /* 44397c478bd9Sstevel@tonic-gate * Fail if the zone isn't fully initialized yet. 44407c478bd9Sstevel@tonic-gate */ 44417c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY) { 44427c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 44437c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44447c478bd9Sstevel@tonic-gate resume_mounts(); 44457c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 44467c478bd9Sstevel@tonic-gate } 44477c478bd9Sstevel@tonic-gate /* 44487c478bd9Sstevel@tonic-gate * If conditions required for zone_shutdown() to return have been met, 44497c478bd9Sstevel@tonic-gate * return success. 44507c478bd9Sstevel@tonic-gate */ 44517c478bd9Sstevel@tonic-gate if (status >= ZONE_IS_DOWN) { 44527c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 44537c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44547c478bd9Sstevel@tonic-gate resume_mounts(); 44557c478bd9Sstevel@tonic-gate return (0); 44567c478bd9Sstevel@tonic-gate } 44577c478bd9Sstevel@tonic-gate /* 44587c478bd9Sstevel@tonic-gate * If zone_shutdown() hasn't been called before, go through the motions. 44597c478bd9Sstevel@tonic-gate * If it has, there's nothing to do but wait for the kernel threads to 44607c478bd9Sstevel@tonic-gate * drain. 44617c478bd9Sstevel@tonic-gate */ 44627c478bd9Sstevel@tonic-gate if (status < ZONE_IS_EMPTY) { 44637c478bd9Sstevel@tonic-gate uint_t ntasks; 44647c478bd9Sstevel@tonic-gate 44657c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 44667c478bd9Sstevel@tonic-gate if ((ntasks = zone->zone_ntasks) != 1) { 44677c478bd9Sstevel@tonic-gate /* 44687c478bd9Sstevel@tonic-gate * There's still stuff running. 44697c478bd9Sstevel@tonic-gate */ 44707c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 44717c478bd9Sstevel@tonic-gate } 44727c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 44737c478bd9Sstevel@tonic-gate if (ntasks == 1) { 44747c478bd9Sstevel@tonic-gate /* 44757c478bd9Sstevel@tonic-gate * The only way to create another task is through 44767c478bd9Sstevel@tonic-gate * zone_enter(), which will block until we drop 44777c478bd9Sstevel@tonic-gate * zonehash_lock. The zone is empty. 44787c478bd9Sstevel@tonic-gate */ 44797c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 44807c478bd9Sstevel@tonic-gate /* 44817c478bd9Sstevel@tonic-gate * Skip ahead to ZONE_IS_DOWN 44827c478bd9Sstevel@tonic-gate */ 44837c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 44847c478bd9Sstevel@tonic-gate } else { 44857c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 44867c478bd9Sstevel@tonic-gate } 44877c478bd9Sstevel@tonic-gate } 44887c478bd9Sstevel@tonic-gate } 44897c478bd9Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 44907c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 44917c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44927c478bd9Sstevel@tonic-gate resume_mounts(); 44937c478bd9Sstevel@tonic-gate 44947c478bd9Sstevel@tonic-gate if (error = zone_empty(zone)) { 44957c478bd9Sstevel@tonic-gate zone_rele(zone); 44967c478bd9Sstevel@tonic-gate return (set_errno(error)); 44977c478bd9Sstevel@tonic-gate } 44987c478bd9Sstevel@tonic-gate /* 44997c478bd9Sstevel@tonic-gate * After the zone status goes to ZONE_IS_DOWN this zone will no 45007c478bd9Sstevel@tonic-gate * longer be notified of changes to the pools configuration, so 45017c478bd9Sstevel@tonic-gate * in order to not end up with a stale pool pointer, we point 45027c478bd9Sstevel@tonic-gate * ourselves at the default pool and remove all resource 45037c478bd9Sstevel@tonic-gate * visibility. This is especially important as the zone_t may 45047c478bd9Sstevel@tonic-gate * languish on the deathrow for a very long time waiting for 45057c478bd9Sstevel@tonic-gate * cred's to drain out. 45067c478bd9Sstevel@tonic-gate * 45077c478bd9Sstevel@tonic-gate * This rebinding of the zone can happen multiple times 45087c478bd9Sstevel@tonic-gate * (presumably due to interrupted or parallel systemcalls) 45097c478bd9Sstevel@tonic-gate * without any adverse effects. 45107c478bd9Sstevel@tonic-gate */ 45117c478bd9Sstevel@tonic-gate if (pool_lock_intr() != 0) { 45127c478bd9Sstevel@tonic-gate zone_rele(zone); 45137c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 45147c478bd9Sstevel@tonic-gate } 45157c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 45167c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 45177c478bd9Sstevel@tonic-gate zone_pool_set(zone, pool_default); 45187c478bd9Sstevel@tonic-gate /* 45197c478bd9Sstevel@tonic-gate * The zone no longer needs to be able to see any cpus. 45207c478bd9Sstevel@tonic-gate */ 45217c478bd9Sstevel@tonic-gate zone_pset_set(zone, ZONE_PS_INVAL); 45227c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 45237c478bd9Sstevel@tonic-gate } 45247c478bd9Sstevel@tonic-gate pool_unlock(); 45257c478bd9Sstevel@tonic-gate 45267c478bd9Sstevel@tonic-gate /* 45277c478bd9Sstevel@tonic-gate * ZSD shutdown callbacks can be executed multiple times, hence 45287c478bd9Sstevel@tonic-gate * it is safe to not be holding any locks across this call. 45297c478bd9Sstevel@tonic-gate */ 45307c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 45317c478bd9Sstevel@tonic-gate 45327c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 45337c478bd9Sstevel@tonic-gate if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 45347c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 45357c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 45367c478bd9Sstevel@tonic-gate 45377c478bd9Sstevel@tonic-gate /* 45387c478bd9Sstevel@tonic-gate * Wait for kernel threads to drain. 45397c478bd9Sstevel@tonic-gate */ 45407c478bd9Sstevel@tonic-gate if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 45417c478bd9Sstevel@tonic-gate zone_rele(zone); 45427c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 45437c478bd9Sstevel@tonic-gate } 45449acbbeafSnn35248 454552978630Ssl108498 /* 454652978630Ssl108498 * Zone can be become down/destroyable even if the above wait 454752978630Ssl108498 * returns EINTR, so any code added here may never execute. 454852978630Ssl108498 * (i.e. don't add code here) 454952978630Ssl108498 */ 45509acbbeafSnn35248 45517c478bd9Sstevel@tonic-gate zone_rele(zone); 45527c478bd9Sstevel@tonic-gate return (0); 45537c478bd9Sstevel@tonic-gate } 45547c478bd9Sstevel@tonic-gate 45557c478bd9Sstevel@tonic-gate /* 45567c478bd9Sstevel@tonic-gate * Systemcall entry point to finalize the zone halt process. The caller 4557824c205fSml93401 * must have already successfully called zone_shutdown(). 45587c478bd9Sstevel@tonic-gate * 45597c478bd9Sstevel@tonic-gate * Upon successful completion, the zone will have been fully destroyed: 45607c478bd9Sstevel@tonic-gate * zsched will have exited, destructor callbacks executed, and the zone 45617c478bd9Sstevel@tonic-gate * removed from the list of active zones. 45627c478bd9Sstevel@tonic-gate */ 45637c478bd9Sstevel@tonic-gate static int 45647c478bd9Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 45657c478bd9Sstevel@tonic-gate { 45667c478bd9Sstevel@tonic-gate uint64_t uniqid; 45677c478bd9Sstevel@tonic-gate zone_t *zone; 45687c478bd9Sstevel@tonic-gate zone_status_t status; 45697c478bd9Sstevel@tonic-gate 45707c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 45717c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 45727c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 45737c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 45747c478bd9Sstevel@tonic-gate 45757c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 45767c478bd9Sstevel@tonic-gate /* 45777c478bd9Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 45787c478bd9Sstevel@tonic-gate * calls to zone_destroy. 45797c478bd9Sstevel@tonic-gate */ 45807c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 45817c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45827c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 45837c478bd9Sstevel@tonic-gate } 45847c478bd9Sstevel@tonic-gate 45857c478bd9Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 45867c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45877c478bd9Sstevel@tonic-gate return (set_errno(EBUSY)); 45887c478bd9Sstevel@tonic-gate } 45897c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 45907c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 45917c478bd9Sstevel@tonic-gate if (status < ZONE_IS_DOWN) { 45927c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 45937c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45947c478bd9Sstevel@tonic-gate return (set_errno(EBUSY)); 45957c478bd9Sstevel@tonic-gate } else if (status == ZONE_IS_DOWN) { 45967c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 45977c478bd9Sstevel@tonic-gate } 45987c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 45997c478bd9Sstevel@tonic-gate zone_hold(zone); 46007c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 46017c478bd9Sstevel@tonic-gate 46027c478bd9Sstevel@tonic-gate /* 46037c478bd9Sstevel@tonic-gate * wait for zsched to exit 46047c478bd9Sstevel@tonic-gate */ 46057c478bd9Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_DEAD); 46067c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_DESTROY); 4607f4b3ec61Sdh155122 zone->zone_netstack = NULL; 46087c478bd9Sstevel@tonic-gate uniqid = zone->zone_uniqid; 46097c478bd9Sstevel@tonic-gate zone_rele(zone); 46107c478bd9Sstevel@tonic-gate zone = NULL; /* potentially free'd */ 46117c478bd9Sstevel@tonic-gate 46127c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 46137c478bd9Sstevel@tonic-gate for (; /* ever */; ) { 46147c478bd9Sstevel@tonic-gate boolean_t unref; 46157c478bd9Sstevel@tonic-gate 46167c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL || 46177c478bd9Sstevel@tonic-gate zone->zone_uniqid != uniqid) { 46187c478bd9Sstevel@tonic-gate /* 46197c478bd9Sstevel@tonic-gate * The zone has gone away. Necessary conditions 46207c478bd9Sstevel@tonic-gate * are met, so we return success. 46217c478bd9Sstevel@tonic-gate */ 46227c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 46237c478bd9Sstevel@tonic-gate return (0); 46247c478bd9Sstevel@tonic-gate } 46257c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 46267c478bd9Sstevel@tonic-gate unref = ZONE_IS_UNREF(zone); 46277c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 46287c478bd9Sstevel@tonic-gate if (unref) { 46297c478bd9Sstevel@tonic-gate /* 46307c478bd9Sstevel@tonic-gate * There is only one reference to the zone -- that 46317c478bd9Sstevel@tonic-gate * added when the zone was added to the hashtables -- 46327c478bd9Sstevel@tonic-gate * and things will remain this way until we drop 46337c478bd9Sstevel@tonic-gate * zonehash_lock... we can go ahead and cleanup the 46347c478bd9Sstevel@tonic-gate * zone. 46357c478bd9Sstevel@tonic-gate */ 46367c478bd9Sstevel@tonic-gate break; 46377c478bd9Sstevel@tonic-gate } 46387c478bd9Sstevel@tonic-gate 46397c478bd9Sstevel@tonic-gate if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 46407c478bd9Sstevel@tonic-gate /* Signaled */ 46417c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 46427c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 46437c478bd9Sstevel@tonic-gate } 46447c478bd9Sstevel@tonic-gate 46457c478bd9Sstevel@tonic-gate } 46467c478bd9Sstevel@tonic-gate 4647c97ad5cdSakolb /* 4648c97ad5cdSakolb * Remove CPU cap for this zone now since we're not going to 4649c97ad5cdSakolb * fail below this point. 4650c97ad5cdSakolb */ 4651c97ad5cdSakolb cpucaps_zone_remove(zone); 4652c97ad5cdSakolb 4653c97ad5cdSakolb /* Get rid of the zone's kstats */ 46540209230bSgjelinek zone_kstat_delete(zone); 46550209230bSgjelinek 4656134a1f4eSCasper H.S. Dik /* remove the pfexecd doors */ 4657134a1f4eSCasper H.S. Dik if (zone->zone_pfexecd != NULL) { 4658134a1f4eSCasper H.S. Dik klpd_freelist(&zone->zone_pfexecd); 4659134a1f4eSCasper H.S. Dik zone->zone_pfexecd = NULL; 4660134a1f4eSCasper H.S. Dik } 4661134a1f4eSCasper H.S. Dik 4662319378d9Seh208807 /* free brand specific data */ 4663319378d9Seh208807 if (ZONE_IS_BRANDED(zone)) 4664319378d9Seh208807 ZBROP(zone)->b_free_brand_data(zone); 4665319378d9Seh208807 466652978630Ssl108498 /* Say goodbye to brand framework. */ 466752978630Ssl108498 brand_unregister_zone(zone->zone_brand); 466852978630Ssl108498 46697c478bd9Sstevel@tonic-gate /* 46707c478bd9Sstevel@tonic-gate * It is now safe to let the zone be recreated; remove it from the 46717c478bd9Sstevel@tonic-gate * lists. The memory will not be freed until the last cred 46727c478bd9Sstevel@tonic-gate * reference goes away. 46737c478bd9Sstevel@tonic-gate */ 46747c478bd9Sstevel@tonic-gate ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 46757c478bd9Sstevel@tonic-gate zonecount--; 46767c478bd9Sstevel@tonic-gate /* remove from active list and hash tables */ 46777c478bd9Sstevel@tonic-gate list_remove(&zone_active, zone); 46787c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 46797c478bd9Sstevel@tonic-gate (mod_hash_key_t)zone->zone_name); 46807c478bd9Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 46817c478bd9Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 468248451833Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) 468345916cd2Sjpk (void) mod_hash_destroy(zonehashbylabel, 468445916cd2Sjpk (mod_hash_key_t)zone->zone_slabel); 46857c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 46867c478bd9Sstevel@tonic-gate 4687108322fbScarlsonj /* 4688108322fbScarlsonj * Release the root vnode; we're not using it anymore. Nor should any 4689108322fbScarlsonj * other thread that might access it exist. 4690108322fbScarlsonj */ 4691108322fbScarlsonj if (zone->zone_rootvp != NULL) { 4692108322fbScarlsonj VN_RELE(zone->zone_rootvp); 4693108322fbScarlsonj zone->zone_rootvp = NULL; 4694108322fbScarlsonj } 4695108322fbScarlsonj 46967c478bd9Sstevel@tonic-gate /* add to deathrow list */ 46977c478bd9Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 46987c478bd9Sstevel@tonic-gate list_insert_tail(&zone_deathrow, zone); 46997c478bd9Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 47007c478bd9Sstevel@tonic-gate 47017c478bd9Sstevel@tonic-gate /* 47027c478bd9Sstevel@tonic-gate * Drop last reference (which was added by zsched()), this will 47037c478bd9Sstevel@tonic-gate * free the zone unless there are outstanding cred references. 47047c478bd9Sstevel@tonic-gate */ 47057c478bd9Sstevel@tonic-gate zone_rele(zone); 47067c478bd9Sstevel@tonic-gate return (0); 47077c478bd9Sstevel@tonic-gate } 47087c478bd9Sstevel@tonic-gate 47097c478bd9Sstevel@tonic-gate /* 47107c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_getattr(2). 47117c478bd9Sstevel@tonic-gate */ 47127c478bd9Sstevel@tonic-gate static ssize_t 47137c478bd9Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 47147c478bd9Sstevel@tonic-gate { 47157c478bd9Sstevel@tonic-gate size_t size; 47167c478bd9Sstevel@tonic-gate int error = 0, err; 47177c478bd9Sstevel@tonic-gate zone_t *zone; 47187c478bd9Sstevel@tonic-gate char *zonepath; 47193f2f09c1Sdp char *outstr; 47207c478bd9Sstevel@tonic-gate zone_status_t zone_status; 47217c478bd9Sstevel@tonic-gate pid_t initpid; 4722c97ad5cdSakolb boolean_t global = (curzone == global_zone); 4723c97ad5cdSakolb boolean_t inzone = (curzone->zone_id == zoneid); 4724f4b3ec61Sdh155122 ushort_t flags; 4725*550b6e40SSowmini Varadhan zone_net_data_t *zbuf; 47267c478bd9Sstevel@tonic-gate 47277c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 47287c478bd9Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 47297c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 47307c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 47317c478bd9Sstevel@tonic-gate } 47327c478bd9Sstevel@tonic-gate zone_status = zone_status_get(zone); 4733bd41d0a8Snordmark if (zone_status < ZONE_IS_INITIALIZED) { 47347c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 47357c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 47367c478bd9Sstevel@tonic-gate } 47377c478bd9Sstevel@tonic-gate zone_hold(zone); 47387c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 47397c478bd9Sstevel@tonic-gate 47407c478bd9Sstevel@tonic-gate /* 474145916cd2Sjpk * If not in the global zone, don't show information about other zones, 474245916cd2Sjpk * unless the system is labeled and the local zone's label dominates 474345916cd2Sjpk * the other zone. 47447c478bd9Sstevel@tonic-gate */ 474545916cd2Sjpk if (!zone_list_access(zone)) { 47467c478bd9Sstevel@tonic-gate zone_rele(zone); 47477c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 47487c478bd9Sstevel@tonic-gate } 47497c478bd9Sstevel@tonic-gate 47507c478bd9Sstevel@tonic-gate switch (attr) { 47517c478bd9Sstevel@tonic-gate case ZONE_ATTR_ROOT: 47527c478bd9Sstevel@tonic-gate if (global) { 47537c478bd9Sstevel@tonic-gate /* 47547c478bd9Sstevel@tonic-gate * Copy the path to trim the trailing "/" (except for 47557c478bd9Sstevel@tonic-gate * the global zone). 47567c478bd9Sstevel@tonic-gate */ 47577c478bd9Sstevel@tonic-gate if (zone != global_zone) 47587c478bd9Sstevel@tonic-gate size = zone->zone_rootpathlen - 1; 47597c478bd9Sstevel@tonic-gate else 47607c478bd9Sstevel@tonic-gate size = zone->zone_rootpathlen; 47617c478bd9Sstevel@tonic-gate zonepath = kmem_alloc(size, KM_SLEEP); 47627c478bd9Sstevel@tonic-gate bcopy(zone->zone_rootpath, zonepath, size); 47637c478bd9Sstevel@tonic-gate zonepath[size - 1] = '\0'; 47647c478bd9Sstevel@tonic-gate } else { 4765c97ad5cdSakolb if (inzone || !is_system_labeled()) { 47667c478bd9Sstevel@tonic-gate /* 476745916cd2Sjpk * Caller is not in the global zone. 476845916cd2Sjpk * if the query is on the current zone 476945916cd2Sjpk * or the system is not labeled, 477045916cd2Sjpk * just return faked-up path for current zone. 47717c478bd9Sstevel@tonic-gate */ 47727c478bd9Sstevel@tonic-gate zonepath = "/"; 47737c478bd9Sstevel@tonic-gate size = 2; 477445916cd2Sjpk } else { 477545916cd2Sjpk /* 477645916cd2Sjpk * Return related path for current zone. 477745916cd2Sjpk */ 477845916cd2Sjpk int prefix_len = strlen(zone_prefix); 477945916cd2Sjpk int zname_len = strlen(zone->zone_name); 478045916cd2Sjpk 478145916cd2Sjpk size = prefix_len + zname_len + 1; 478245916cd2Sjpk zonepath = kmem_alloc(size, KM_SLEEP); 478345916cd2Sjpk bcopy(zone_prefix, zonepath, prefix_len); 478445916cd2Sjpk bcopy(zone->zone_name, zonepath + 478545916cd2Sjpk prefix_len, zname_len); 478645916cd2Sjpk zonepath[size - 1] = '\0'; 478745916cd2Sjpk } 47887c478bd9Sstevel@tonic-gate } 47897c478bd9Sstevel@tonic-gate if (bufsize > size) 47907c478bd9Sstevel@tonic-gate bufsize = size; 47917c478bd9Sstevel@tonic-gate if (buf != NULL) { 47927c478bd9Sstevel@tonic-gate err = copyoutstr(zonepath, buf, bufsize, NULL); 47937c478bd9Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 47947c478bd9Sstevel@tonic-gate error = EFAULT; 47957c478bd9Sstevel@tonic-gate } 4796c97ad5cdSakolb if (global || (is_system_labeled() && !inzone)) 47977c478bd9Sstevel@tonic-gate kmem_free(zonepath, size); 47987c478bd9Sstevel@tonic-gate break; 47997c478bd9Sstevel@tonic-gate 48007c478bd9Sstevel@tonic-gate case ZONE_ATTR_NAME: 48017c478bd9Sstevel@tonic-gate size = strlen(zone->zone_name) + 1; 48027c478bd9Sstevel@tonic-gate if (bufsize > size) 48037c478bd9Sstevel@tonic-gate bufsize = size; 48047c478bd9Sstevel@tonic-gate if (buf != NULL) { 48057c478bd9Sstevel@tonic-gate err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 48067c478bd9Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 48077c478bd9Sstevel@tonic-gate error = EFAULT; 48087c478bd9Sstevel@tonic-gate } 48097c478bd9Sstevel@tonic-gate break; 48107c478bd9Sstevel@tonic-gate 48117c478bd9Sstevel@tonic-gate case ZONE_ATTR_STATUS: 48127c478bd9Sstevel@tonic-gate /* 48137c478bd9Sstevel@tonic-gate * Since we're not holding zonehash_lock, the zone status 48147c478bd9Sstevel@tonic-gate * may be anything; leave it up to userland to sort it out. 48157c478bd9Sstevel@tonic-gate */ 48167c478bd9Sstevel@tonic-gate size = sizeof (zone_status); 48177c478bd9Sstevel@tonic-gate if (bufsize > size) 48187c478bd9Sstevel@tonic-gate bufsize = size; 48197c478bd9Sstevel@tonic-gate zone_status = zone_status_get(zone); 48207c478bd9Sstevel@tonic-gate if (buf != NULL && 48217c478bd9Sstevel@tonic-gate copyout(&zone_status, buf, bufsize) != 0) 48227c478bd9Sstevel@tonic-gate error = EFAULT; 48237c478bd9Sstevel@tonic-gate break; 4824f4b3ec61Sdh155122 case ZONE_ATTR_FLAGS: 4825f4b3ec61Sdh155122 size = sizeof (zone->zone_flags); 4826f4b3ec61Sdh155122 if (bufsize > size) 4827f4b3ec61Sdh155122 bufsize = size; 4828f4b3ec61Sdh155122 flags = zone->zone_flags; 4829f4b3ec61Sdh155122 if (buf != NULL && 4830f4b3ec61Sdh155122 copyout(&flags, buf, bufsize) != 0) 4831f4b3ec61Sdh155122 error = EFAULT; 4832f4b3ec61Sdh155122 break; 48337c478bd9Sstevel@tonic-gate case ZONE_ATTR_PRIVSET: 48347c478bd9Sstevel@tonic-gate size = sizeof (priv_set_t); 48357c478bd9Sstevel@tonic-gate if (bufsize > size) 48367c478bd9Sstevel@tonic-gate bufsize = size; 48377c478bd9Sstevel@tonic-gate if (buf != NULL && 48387c478bd9Sstevel@tonic-gate copyout(zone->zone_privset, buf, bufsize) != 0) 48397c478bd9Sstevel@tonic-gate error = EFAULT; 48407c478bd9Sstevel@tonic-gate break; 48417c478bd9Sstevel@tonic-gate case ZONE_ATTR_UNIQID: 48427c478bd9Sstevel@tonic-gate size = sizeof (zone->zone_uniqid); 48437c478bd9Sstevel@tonic-gate if (bufsize > size) 48447c478bd9Sstevel@tonic-gate bufsize = size; 48457c478bd9Sstevel@tonic-gate if (buf != NULL && 48467c478bd9Sstevel@tonic-gate copyout(&zone->zone_uniqid, buf, bufsize) != 0) 48477c478bd9Sstevel@tonic-gate error = EFAULT; 48487c478bd9Sstevel@tonic-gate break; 48497c478bd9Sstevel@tonic-gate case ZONE_ATTR_POOLID: 48507c478bd9Sstevel@tonic-gate { 48517c478bd9Sstevel@tonic-gate pool_t *pool; 48527c478bd9Sstevel@tonic-gate poolid_t poolid; 48537c478bd9Sstevel@tonic-gate 48547c478bd9Sstevel@tonic-gate if (pool_lock_intr() != 0) { 48557c478bd9Sstevel@tonic-gate error = EINTR; 48567c478bd9Sstevel@tonic-gate break; 48577c478bd9Sstevel@tonic-gate } 48587c478bd9Sstevel@tonic-gate pool = zone_pool_get(zone); 48597c478bd9Sstevel@tonic-gate poolid = pool->pool_id; 48607c478bd9Sstevel@tonic-gate pool_unlock(); 48617c478bd9Sstevel@tonic-gate size = sizeof (poolid); 48627c478bd9Sstevel@tonic-gate if (bufsize > size) 48637c478bd9Sstevel@tonic-gate bufsize = size; 48647c478bd9Sstevel@tonic-gate if (buf != NULL && copyout(&poolid, buf, size) != 0) 48657c478bd9Sstevel@tonic-gate error = EFAULT; 48667c478bd9Sstevel@tonic-gate } 48677c478bd9Sstevel@tonic-gate break; 486845916cd2Sjpk case ZONE_ATTR_SLBL: 486945916cd2Sjpk size = sizeof (bslabel_t); 487045916cd2Sjpk if (bufsize > size) 487145916cd2Sjpk bufsize = size; 487245916cd2Sjpk if (zone->zone_slabel == NULL) 487345916cd2Sjpk error = EINVAL; 487445916cd2Sjpk else if (buf != NULL && 487545916cd2Sjpk copyout(label2bslabel(zone->zone_slabel), buf, 487645916cd2Sjpk bufsize) != 0) 487745916cd2Sjpk error = EFAULT; 487845916cd2Sjpk break; 48797c478bd9Sstevel@tonic-gate case ZONE_ATTR_INITPID: 48807c478bd9Sstevel@tonic-gate size = sizeof (initpid); 48817c478bd9Sstevel@tonic-gate if (bufsize > size) 48827c478bd9Sstevel@tonic-gate bufsize = size; 48837c478bd9Sstevel@tonic-gate initpid = zone->zone_proc_initpid; 48847c478bd9Sstevel@tonic-gate if (initpid == -1) { 48857c478bd9Sstevel@tonic-gate error = ESRCH; 48867c478bd9Sstevel@tonic-gate break; 48877c478bd9Sstevel@tonic-gate } 48887c478bd9Sstevel@tonic-gate if (buf != NULL && 48897c478bd9Sstevel@tonic-gate copyout(&initpid, buf, bufsize) != 0) 48907c478bd9Sstevel@tonic-gate error = EFAULT; 48917c478bd9Sstevel@tonic-gate break; 48929acbbeafSnn35248 case ZONE_ATTR_BRAND: 48939acbbeafSnn35248 size = strlen(zone->zone_brand->b_name) + 1; 48949acbbeafSnn35248 48959acbbeafSnn35248 if (bufsize > size) 48969acbbeafSnn35248 bufsize = size; 48979acbbeafSnn35248 if (buf != NULL) { 48989acbbeafSnn35248 err = copyoutstr(zone->zone_brand->b_name, buf, 48999acbbeafSnn35248 bufsize, NULL); 49009acbbeafSnn35248 if (err != 0 && err != ENAMETOOLONG) 49019acbbeafSnn35248 error = EFAULT; 49029acbbeafSnn35248 } 49039acbbeafSnn35248 break; 49043f2f09c1Sdp case ZONE_ATTR_INITNAME: 49053f2f09c1Sdp size = strlen(zone->zone_initname) + 1; 49063f2f09c1Sdp if (bufsize > size) 49073f2f09c1Sdp bufsize = size; 49083f2f09c1Sdp if (buf != NULL) { 49093f2f09c1Sdp err = copyoutstr(zone->zone_initname, buf, bufsize, 49103f2f09c1Sdp NULL); 49113f2f09c1Sdp if (err != 0 && err != ENAMETOOLONG) 49123f2f09c1Sdp error = EFAULT; 49133f2f09c1Sdp } 49143f2f09c1Sdp break; 49153f2f09c1Sdp case ZONE_ATTR_BOOTARGS: 49163f2f09c1Sdp if (zone->zone_bootargs == NULL) 49173f2f09c1Sdp outstr = ""; 49183f2f09c1Sdp else 49193f2f09c1Sdp outstr = zone->zone_bootargs; 49203f2f09c1Sdp size = strlen(outstr) + 1; 49213f2f09c1Sdp if (bufsize > size) 49223f2f09c1Sdp bufsize = size; 49233f2f09c1Sdp if (buf != NULL) { 49243f2f09c1Sdp err = copyoutstr(outstr, buf, bufsize, NULL); 49253f2f09c1Sdp if (err != 0 && err != ENAMETOOLONG) 49263f2f09c1Sdp error = EFAULT; 49273f2f09c1Sdp } 49283f2f09c1Sdp break; 49290209230bSgjelinek case ZONE_ATTR_PHYS_MCAP: 49300209230bSgjelinek size = sizeof (zone->zone_phys_mcap); 49310209230bSgjelinek if (bufsize > size) 49320209230bSgjelinek bufsize = size; 49330209230bSgjelinek if (buf != NULL && 49340209230bSgjelinek copyout(&zone->zone_phys_mcap, buf, bufsize) != 0) 49350209230bSgjelinek error = EFAULT; 49360209230bSgjelinek break; 49370209230bSgjelinek case ZONE_ATTR_SCHED_CLASS: 49380209230bSgjelinek mutex_enter(&class_lock); 49390209230bSgjelinek 49400209230bSgjelinek if (zone->zone_defaultcid >= loaded_classes) 49410209230bSgjelinek outstr = ""; 49420209230bSgjelinek else 49430209230bSgjelinek outstr = sclass[zone->zone_defaultcid].cl_name; 49440209230bSgjelinek size = strlen(outstr) + 1; 49450209230bSgjelinek if (bufsize > size) 49460209230bSgjelinek bufsize = size; 49470209230bSgjelinek if (buf != NULL) { 49480209230bSgjelinek err = copyoutstr(outstr, buf, bufsize, NULL); 49490209230bSgjelinek if (err != 0 && err != ENAMETOOLONG) 49500209230bSgjelinek error = EFAULT; 49510209230bSgjelinek } 49520209230bSgjelinek 49530209230bSgjelinek mutex_exit(&class_lock); 49540209230bSgjelinek break; 49555679c89fSjv227347 case ZONE_ATTR_HOSTID: 49565679c89fSjv227347 if (zone->zone_hostid != HW_INVALID_HOSTID && 49575679c89fSjv227347 bufsize == sizeof (zone->zone_hostid)) { 49585679c89fSjv227347 size = sizeof (zone->zone_hostid); 49595679c89fSjv227347 if (buf != NULL && copyout(&zone->zone_hostid, buf, 49605679c89fSjv227347 bufsize) != 0) 49615679c89fSjv227347 error = EFAULT; 49625679c89fSjv227347 } else { 49635679c89fSjv227347 error = EINVAL; 49645679c89fSjv227347 } 49655679c89fSjv227347 break; 49660fbb751dSJohn Levon case ZONE_ATTR_FS_ALLOWED: 49670fbb751dSJohn Levon if (zone->zone_fs_allowed == NULL) 49680fbb751dSJohn Levon outstr = ""; 49690fbb751dSJohn Levon else 49700fbb751dSJohn Levon outstr = zone->zone_fs_allowed; 49710fbb751dSJohn Levon size = strlen(outstr) + 1; 49720fbb751dSJohn Levon if (bufsize > size) 49730fbb751dSJohn Levon bufsize = size; 49740fbb751dSJohn Levon if (buf != NULL) { 49750fbb751dSJohn Levon err = copyoutstr(outstr, buf, bufsize, NULL); 49760fbb751dSJohn Levon if (err != 0 && err != ENAMETOOLONG) 49770fbb751dSJohn Levon error = EFAULT; 49780fbb751dSJohn Levon } 49790fbb751dSJohn Levon break; 4980*550b6e40SSowmini Varadhan case ZONE_ATTR_NETWORK: 4981*550b6e40SSowmini Varadhan zbuf = kmem_alloc(bufsize, KM_SLEEP); 4982*550b6e40SSowmini Varadhan if (copyin(buf, zbuf, bufsize) != 0) { 4983*550b6e40SSowmini Varadhan error = EFAULT; 4984*550b6e40SSowmini Varadhan } else { 4985*550b6e40SSowmini Varadhan error = zone_get_network(zoneid, zbuf); 4986*550b6e40SSowmini Varadhan if (error == 0 && copyout(zbuf, buf, bufsize) != 0) 4987*550b6e40SSowmini Varadhan error = EFAULT; 4988*550b6e40SSowmini Varadhan } 4989*550b6e40SSowmini Varadhan kmem_free(zbuf, bufsize); 4990*550b6e40SSowmini Varadhan break; 49917c478bd9Sstevel@tonic-gate default: 49929acbbeafSnn35248 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) { 49939acbbeafSnn35248 size = bufsize; 49949acbbeafSnn35248 error = ZBROP(zone)->b_getattr(zone, attr, buf, &size); 49959acbbeafSnn35248 } else { 49967c478bd9Sstevel@tonic-gate error = EINVAL; 49977c478bd9Sstevel@tonic-gate } 49989acbbeafSnn35248 } 49997c478bd9Sstevel@tonic-gate zone_rele(zone); 50007c478bd9Sstevel@tonic-gate 50017c478bd9Sstevel@tonic-gate if (error) 50027c478bd9Sstevel@tonic-gate return (set_errno(error)); 50037c478bd9Sstevel@tonic-gate return ((ssize_t)size); 50047c478bd9Sstevel@tonic-gate } 50057c478bd9Sstevel@tonic-gate 50067c478bd9Sstevel@tonic-gate /* 50073f2f09c1Sdp * Systemcall entry point for zone_setattr(2). 50083f2f09c1Sdp */ 50093f2f09c1Sdp /*ARGSUSED*/ 50103f2f09c1Sdp static int 50113f2f09c1Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 50123f2f09c1Sdp { 50133f2f09c1Sdp zone_t *zone; 50143f2f09c1Sdp zone_status_t zone_status; 50153f2f09c1Sdp int err; 5016*550b6e40SSowmini Varadhan zone_net_data_t *zbuf; 50173f2f09c1Sdp 50183f2f09c1Sdp if (secpolicy_zone_config(CRED()) != 0) 50193f2f09c1Sdp return (set_errno(EPERM)); 50203f2f09c1Sdp 50213f2f09c1Sdp /* 50220209230bSgjelinek * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the 50230209230bSgjelinek * global zone. 50243f2f09c1Sdp */ 50250209230bSgjelinek if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) { 50263f2f09c1Sdp return (set_errno(EINVAL)); 50273f2f09c1Sdp } 50283f2f09c1Sdp 50293f2f09c1Sdp mutex_enter(&zonehash_lock); 50303f2f09c1Sdp if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 50313f2f09c1Sdp mutex_exit(&zonehash_lock); 50323f2f09c1Sdp return (set_errno(EINVAL)); 50333f2f09c1Sdp } 50343f2f09c1Sdp zone_hold(zone); 50353f2f09c1Sdp mutex_exit(&zonehash_lock); 50363f2f09c1Sdp 50370209230bSgjelinek /* 50380209230bSgjelinek * At present most attributes can only be set on non-running, 50390209230bSgjelinek * non-global zones. 50400209230bSgjelinek */ 50413f2f09c1Sdp zone_status = zone_status_get(zone); 50420209230bSgjelinek if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY) 50433f2f09c1Sdp goto done; 50443f2f09c1Sdp 50453f2f09c1Sdp switch (attr) { 50463f2f09c1Sdp case ZONE_ATTR_INITNAME: 50473f2f09c1Sdp err = zone_set_initname(zone, (const char *)buf); 50483f2f09c1Sdp break; 50493f2f09c1Sdp case ZONE_ATTR_BOOTARGS: 50503f2f09c1Sdp err = zone_set_bootargs(zone, (const char *)buf); 50513f2f09c1Sdp break; 50529acbbeafSnn35248 case ZONE_ATTR_BRAND: 505359f2ff5cSedp err = zone_set_brand(zone, (const char *)buf); 50549acbbeafSnn35248 break; 50550fbb751dSJohn Levon case ZONE_ATTR_FS_ALLOWED: 50560fbb751dSJohn Levon err = zone_set_fs_allowed(zone, (const char *)buf); 50570fbb751dSJohn Levon break; 50580209230bSgjelinek case ZONE_ATTR_PHYS_MCAP: 50590209230bSgjelinek err = zone_set_phys_mcap(zone, (const uint64_t *)buf); 50600209230bSgjelinek break; 50610209230bSgjelinek case ZONE_ATTR_SCHED_CLASS: 50620209230bSgjelinek err = zone_set_sched_class(zone, (const char *)buf); 50630209230bSgjelinek break; 50645679c89fSjv227347 case ZONE_ATTR_HOSTID: 50655679c89fSjv227347 if (bufsize == sizeof (zone->zone_hostid)) { 50665679c89fSjv227347 if (copyin(buf, &zone->zone_hostid, bufsize) == 0) 50675679c89fSjv227347 err = 0; 50685679c89fSjv227347 else 50695679c89fSjv227347 err = EFAULT; 50705679c89fSjv227347 } else { 50715679c89fSjv227347 err = EINVAL; 50725679c89fSjv227347 } 50735679c89fSjv227347 break; 5074*550b6e40SSowmini Varadhan case ZONE_ATTR_NETWORK: 5075*550b6e40SSowmini Varadhan if (bufsize > (PIPE_BUF + sizeof (zone_net_data_t))) { 5076*550b6e40SSowmini Varadhan err = EINVAL; 5077*550b6e40SSowmini Varadhan goto done; 5078*550b6e40SSowmini Varadhan } 5079*550b6e40SSowmini Varadhan zbuf = kmem_alloc(bufsize, KM_SLEEP); 5080*550b6e40SSowmini Varadhan if (copyin(buf, zbuf, bufsize) != 0) { 5081*550b6e40SSowmini Varadhan err = EFAULT; 5082*550b6e40SSowmini Varadhan goto done; 5083*550b6e40SSowmini Varadhan } 5084*550b6e40SSowmini Varadhan err = zone_set_network(zoneid, zbuf); 5085*550b6e40SSowmini Varadhan kmem_free(zbuf, bufsize); 5086*550b6e40SSowmini Varadhan break; 50873f2f09c1Sdp default: 50889acbbeafSnn35248 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) 50899acbbeafSnn35248 err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize); 50909acbbeafSnn35248 else 50913f2f09c1Sdp err = EINVAL; 50923f2f09c1Sdp } 50933f2f09c1Sdp 50943f2f09c1Sdp done: 50953f2f09c1Sdp zone_rele(zone); 50963f2f09c1Sdp return (err != 0 ? set_errno(err) : 0); 50973f2f09c1Sdp } 50983f2f09c1Sdp 50993f2f09c1Sdp /* 51007c478bd9Sstevel@tonic-gate * Return zero if the process has at least one vnode mapped in to its 51017c478bd9Sstevel@tonic-gate * address space which shouldn't be allowed to change zones. 51020209230bSgjelinek * 51030209230bSgjelinek * Also return zero if the process has any shared mappings which reserve 51040209230bSgjelinek * swap. This is because the counting for zone.max-swap does not allow swap 5105da6c28aaSamw * reservation to be shared between zones. zone swap reservation is counted 51060209230bSgjelinek * on zone->zone_max_swap. 51077c478bd9Sstevel@tonic-gate */ 51087c478bd9Sstevel@tonic-gate static int 51097c478bd9Sstevel@tonic-gate as_can_change_zones(void) 51107c478bd9Sstevel@tonic-gate { 51117c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 51127c478bd9Sstevel@tonic-gate struct seg *seg; 51137c478bd9Sstevel@tonic-gate struct as *as = pp->p_as; 51147c478bd9Sstevel@tonic-gate vnode_t *vp; 51157c478bd9Sstevel@tonic-gate int allow = 1; 51167c478bd9Sstevel@tonic-gate 51177c478bd9Sstevel@tonic-gate ASSERT(pp->p_as != &kas); 51180209230bSgjelinek AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 51197c478bd9Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 51200209230bSgjelinek 51210209230bSgjelinek /* 51220209230bSgjelinek * Cannot enter zone with shared anon memory which 51230209230bSgjelinek * reserves swap. See comment above. 51240209230bSgjelinek */ 51250209230bSgjelinek if (seg_can_change_zones(seg) == B_FALSE) { 51260209230bSgjelinek allow = 0; 51270209230bSgjelinek break; 51280209230bSgjelinek } 51297c478bd9Sstevel@tonic-gate /* 51307c478bd9Sstevel@tonic-gate * if we can't get a backing vnode for this segment then skip 51317c478bd9Sstevel@tonic-gate * it. 51327c478bd9Sstevel@tonic-gate */ 51337c478bd9Sstevel@tonic-gate vp = NULL; 51347c478bd9Sstevel@tonic-gate if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 51357c478bd9Sstevel@tonic-gate continue; 51367c478bd9Sstevel@tonic-gate if (!vn_can_change_zones(vp)) { /* bail on first match */ 51377c478bd9Sstevel@tonic-gate allow = 0; 51387c478bd9Sstevel@tonic-gate break; 51397c478bd9Sstevel@tonic-gate } 51407c478bd9Sstevel@tonic-gate } 51410209230bSgjelinek AS_LOCK_EXIT(as, &as->a_lock); 51427c478bd9Sstevel@tonic-gate return (allow); 51437c478bd9Sstevel@tonic-gate } 51447c478bd9Sstevel@tonic-gate 51457c478bd9Sstevel@tonic-gate /* 51460209230bSgjelinek * Count swap reserved by curproc's address space 51470209230bSgjelinek */ 51480209230bSgjelinek static size_t 51490209230bSgjelinek as_swresv(void) 51500209230bSgjelinek { 51510209230bSgjelinek proc_t *pp = curproc; 51520209230bSgjelinek struct seg *seg; 51530209230bSgjelinek struct as *as = pp->p_as; 51540209230bSgjelinek size_t swap = 0; 51550209230bSgjelinek 51560209230bSgjelinek ASSERT(pp->p_as != &kas); 51570209230bSgjelinek ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 51580209230bSgjelinek for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) 51590209230bSgjelinek swap += seg_swresv(seg); 51600209230bSgjelinek 51610209230bSgjelinek return (swap); 51620209230bSgjelinek } 51630209230bSgjelinek 51640209230bSgjelinek /* 51657c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_enter(). 51667c478bd9Sstevel@tonic-gate * 51677c478bd9Sstevel@tonic-gate * The current process is injected into said zone. In the process 51687c478bd9Sstevel@tonic-gate * it will change its project membership, privileges, rootdir/cwd, 51697c478bd9Sstevel@tonic-gate * zone-wide rctls, and pool association to match those of the zone. 51707c478bd9Sstevel@tonic-gate * 51717c478bd9Sstevel@tonic-gate * The first zone_enter() called while the zone is in the ZONE_IS_READY 51727c478bd9Sstevel@tonic-gate * state will transition it to ZONE_IS_RUNNING. Processes may only 51737c478bd9Sstevel@tonic-gate * enter a zone that is "ready" or "running". 51747c478bd9Sstevel@tonic-gate */ 51757c478bd9Sstevel@tonic-gate static int 51767c478bd9Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 51777c478bd9Sstevel@tonic-gate { 51787c478bd9Sstevel@tonic-gate zone_t *zone; 51797c478bd9Sstevel@tonic-gate vnode_t *vp; 51807c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 51817c478bd9Sstevel@tonic-gate contract_t *ct; 51827c478bd9Sstevel@tonic-gate cont_process_t *ctp; 51837c478bd9Sstevel@tonic-gate task_t *tk, *oldtk; 51847c478bd9Sstevel@tonic-gate kproject_t *zone_proj0; 51857c478bd9Sstevel@tonic-gate cred_t *cr, *newcr; 51867c478bd9Sstevel@tonic-gate pool_t *oldpool, *newpool; 51877c478bd9Sstevel@tonic-gate sess_t *sp; 51887c478bd9Sstevel@tonic-gate uid_t uid; 51897c478bd9Sstevel@tonic-gate zone_status_t status; 51907c478bd9Sstevel@tonic-gate int err = 0; 51917c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 51920209230bSgjelinek size_t swap; 5193c97ad5cdSakolb kthread_id_t t; 51947c478bd9Sstevel@tonic-gate 51957c478bd9Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 51967c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 51977c478bd9Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 51987c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 51997c478bd9Sstevel@tonic-gate 52007c478bd9Sstevel@tonic-gate /* 52017c478bd9Sstevel@tonic-gate * Stop all lwps so we don't need to hold a lock to look at 52027c478bd9Sstevel@tonic-gate * curproc->p_zone. This needs to happen before we grab any 52037c478bd9Sstevel@tonic-gate * locks to avoid deadlock (another lwp in the process could 52047c478bd9Sstevel@tonic-gate * be waiting for the held lock). 52057c478bd9Sstevel@tonic-gate */ 52067c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 52077c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 52087c478bd9Sstevel@tonic-gate 52097c478bd9Sstevel@tonic-gate /* 52107c478bd9Sstevel@tonic-gate * Make sure we're not changing zones with files open or mapped in 52117c478bd9Sstevel@tonic-gate * to our address space which shouldn't be changing zones. 52127c478bd9Sstevel@tonic-gate */ 52137c478bd9Sstevel@tonic-gate if (!files_can_change_zones()) { 52147c478bd9Sstevel@tonic-gate err = EBADF; 52157c478bd9Sstevel@tonic-gate goto out; 52167c478bd9Sstevel@tonic-gate } 52177c478bd9Sstevel@tonic-gate if (!as_can_change_zones()) { 52187c478bd9Sstevel@tonic-gate err = EFAULT; 52197c478bd9Sstevel@tonic-gate goto out; 52207c478bd9Sstevel@tonic-gate } 52217c478bd9Sstevel@tonic-gate 52227c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 52237c478bd9Sstevel@tonic-gate if (pp->p_zone != global_zone) { 52247c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 52257c478bd9Sstevel@tonic-gate err = EINVAL; 52267c478bd9Sstevel@tonic-gate goto out; 52277c478bd9Sstevel@tonic-gate } 52287c478bd9Sstevel@tonic-gate 52297c478bd9Sstevel@tonic-gate zone = zone_find_all_by_id(zoneid); 52307c478bd9Sstevel@tonic-gate if (zone == NULL) { 52317c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 52327c478bd9Sstevel@tonic-gate err = EINVAL; 52337c478bd9Sstevel@tonic-gate goto out; 52347c478bd9Sstevel@tonic-gate } 52357c478bd9Sstevel@tonic-gate 52367c478bd9Sstevel@tonic-gate /* 52377c478bd9Sstevel@tonic-gate * To prevent processes in a zone from holding contracts on 52387c478bd9Sstevel@tonic-gate * extrazonal resources, and to avoid process contract 52397c478bd9Sstevel@tonic-gate * memberships which span zones, contract holders and processes 52407c478bd9Sstevel@tonic-gate * which aren't the sole members of their encapsulating process 52417c478bd9Sstevel@tonic-gate * contracts are not allowed to zone_enter. 52427c478bd9Sstevel@tonic-gate */ 52437c478bd9Sstevel@tonic-gate ctp = pp->p_ct_process; 52447c478bd9Sstevel@tonic-gate ct = &ctp->conp_contract; 52457c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 52467c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 52477c478bd9Sstevel@tonic-gate if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 52487c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 52497c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 52507c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 52517c478bd9Sstevel@tonic-gate err = EINVAL; 52527c478bd9Sstevel@tonic-gate goto out; 52537c478bd9Sstevel@tonic-gate } 52547c478bd9Sstevel@tonic-gate 52557c478bd9Sstevel@tonic-gate /* 52567c478bd9Sstevel@tonic-gate * Moreover, we don't allow processes whose encapsulating 52577c478bd9Sstevel@tonic-gate * process contracts have inherited extrazonal contracts. 52587c478bd9Sstevel@tonic-gate * While it would be easier to eliminate all process contracts 52597c478bd9Sstevel@tonic-gate * with inherited contracts, we need to be able to give a 52607c478bd9Sstevel@tonic-gate * restarted init (or other zone-penetrating process) its 52617c478bd9Sstevel@tonic-gate * predecessor's contracts. 52627c478bd9Sstevel@tonic-gate */ 52637c478bd9Sstevel@tonic-gate if (ctp->conp_ninherited != 0) { 52647c478bd9Sstevel@tonic-gate contract_t *next; 52657c478bd9Sstevel@tonic-gate for (next = list_head(&ctp->conp_inherited); next; 52667c478bd9Sstevel@tonic-gate next = list_next(&ctp->conp_inherited, next)) { 52677c478bd9Sstevel@tonic-gate if (contract_getzuniqid(next) != zone->zone_uniqid) { 52687c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 52697c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 52707c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 52717c478bd9Sstevel@tonic-gate err = EINVAL; 52727c478bd9Sstevel@tonic-gate goto out; 52737c478bd9Sstevel@tonic-gate } 52747c478bd9Sstevel@tonic-gate } 52757c478bd9Sstevel@tonic-gate } 52767b209c2cSacruz 52777c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 52787c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 52797c478bd9Sstevel@tonic-gate 52807c478bd9Sstevel@tonic-gate status = zone_status_get(zone); 52817c478bd9Sstevel@tonic-gate if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 52827c478bd9Sstevel@tonic-gate /* 52837c478bd9Sstevel@tonic-gate * Can't join 52847c478bd9Sstevel@tonic-gate */ 52857c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 52867c478bd9Sstevel@tonic-gate err = EINVAL; 52877c478bd9Sstevel@tonic-gate goto out; 52887c478bd9Sstevel@tonic-gate } 52897c478bd9Sstevel@tonic-gate 52907c478bd9Sstevel@tonic-gate /* 52917c478bd9Sstevel@tonic-gate * Make sure new priv set is within the permitted set for caller 52927c478bd9Sstevel@tonic-gate */ 52937c478bd9Sstevel@tonic-gate if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 52947c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 52957c478bd9Sstevel@tonic-gate err = EPERM; 52967c478bd9Sstevel@tonic-gate goto out; 52977c478bd9Sstevel@tonic-gate } 52987c478bd9Sstevel@tonic-gate /* 52997c478bd9Sstevel@tonic-gate * We want to momentarily drop zonehash_lock while we optimistically 53007c478bd9Sstevel@tonic-gate * bind curproc to the pool it should be running in. This is safe 53017c478bd9Sstevel@tonic-gate * since the zone can't disappear (we have a hold on it). 53027c478bd9Sstevel@tonic-gate */ 53037c478bd9Sstevel@tonic-gate zone_hold(zone); 53047c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 53057c478bd9Sstevel@tonic-gate 53067c478bd9Sstevel@tonic-gate /* 53077c478bd9Sstevel@tonic-gate * Grab pool_lock to keep the pools configuration from changing 53087c478bd9Sstevel@tonic-gate * and to stop ourselves from getting rebound to another pool 53097c478bd9Sstevel@tonic-gate * until we join the zone. 53107c478bd9Sstevel@tonic-gate */ 53117c478bd9Sstevel@tonic-gate if (pool_lock_intr() != 0) { 53127c478bd9Sstevel@tonic-gate zone_rele(zone); 53137c478bd9Sstevel@tonic-gate err = EINTR; 53147c478bd9Sstevel@tonic-gate goto out; 53157c478bd9Sstevel@tonic-gate } 53167c478bd9Sstevel@tonic-gate ASSERT(secpolicy_pool(CRED()) == 0); 53177c478bd9Sstevel@tonic-gate /* 53187c478bd9Sstevel@tonic-gate * Bind ourselves to the pool currently associated with the zone. 53197c478bd9Sstevel@tonic-gate */ 53207c478bd9Sstevel@tonic-gate oldpool = curproc->p_pool; 53217c478bd9Sstevel@tonic-gate newpool = zone_pool_get(zone); 53227c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED && newpool != oldpool && 53237c478bd9Sstevel@tonic-gate (err = pool_do_bind(newpool, P_PID, P_MYID, 53247c478bd9Sstevel@tonic-gate POOL_BIND_ALL)) != 0) { 53257c478bd9Sstevel@tonic-gate pool_unlock(); 53267c478bd9Sstevel@tonic-gate zone_rele(zone); 53277c478bd9Sstevel@tonic-gate goto out; 53287c478bd9Sstevel@tonic-gate } 53297c478bd9Sstevel@tonic-gate 53307c478bd9Sstevel@tonic-gate /* 53317c478bd9Sstevel@tonic-gate * Grab cpu_lock now; we'll need it later when we call 53327c478bd9Sstevel@tonic-gate * task_join(). 53337c478bd9Sstevel@tonic-gate */ 53347c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 53357c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 53367c478bd9Sstevel@tonic-gate /* 53377c478bd9Sstevel@tonic-gate * Make sure the zone hasn't moved on since we dropped zonehash_lock. 53387c478bd9Sstevel@tonic-gate */ 53397c478bd9Sstevel@tonic-gate if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 53407c478bd9Sstevel@tonic-gate /* 53417c478bd9Sstevel@tonic-gate * Can't join anymore. 53427c478bd9Sstevel@tonic-gate */ 53437c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 53447c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 53457c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED && 53467c478bd9Sstevel@tonic-gate newpool != oldpool) 53477c478bd9Sstevel@tonic-gate (void) pool_do_bind(oldpool, P_PID, P_MYID, 53487c478bd9Sstevel@tonic-gate POOL_BIND_ALL); 53497c478bd9Sstevel@tonic-gate pool_unlock(); 53507c478bd9Sstevel@tonic-gate zone_rele(zone); 53517c478bd9Sstevel@tonic-gate err = EINVAL; 53527c478bd9Sstevel@tonic-gate goto out; 53537c478bd9Sstevel@tonic-gate } 53547c478bd9Sstevel@tonic-gate 53550209230bSgjelinek /* 53560209230bSgjelinek * a_lock must be held while transfering locked memory and swap 53570209230bSgjelinek * reservation from the global zone to the non global zone because 53580209230bSgjelinek * asynchronous faults on the processes' address space can lock 53590209230bSgjelinek * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE 53600209230bSgjelinek * segments respectively. 53610209230bSgjelinek */ 53620209230bSgjelinek AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER); 53630209230bSgjelinek swap = as_swresv(); 53647c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 53657c478bd9Sstevel@tonic-gate zone_proj0 = zone->zone_zsched->p_task->tk_proj; 53667c478bd9Sstevel@tonic-gate /* verify that we do not exceed and task or lwp limits */ 53677c478bd9Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 53687c478bd9Sstevel@tonic-gate /* add new lwps to zone and zone's proj0 */ 53697c478bd9Sstevel@tonic-gate zone_proj0->kpj_nlwps += pp->p_lwpcnt; 53707c478bd9Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 53717c478bd9Sstevel@tonic-gate /* add 1 task to zone's proj0 */ 53727c478bd9Sstevel@tonic-gate zone_proj0->kpj_ntasks += 1; 5373ff19e029SMenno Lageman 5374ff19e029SMenno Lageman zone_proj0->kpj_nprocs++; 5375ff19e029SMenno Lageman zone->zone_nprocs++; 53767c478bd9Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 53777c478bd9Sstevel@tonic-gate 53780209230bSgjelinek mutex_enter(&zone->zone_mem_lock); 5379c6939658Ssl108498 zone->zone_locked_mem += pp->p_locked_mem; 5380c6939658Ssl108498 zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem; 53810209230bSgjelinek zone->zone_max_swap += swap; 53820209230bSgjelinek mutex_exit(&zone->zone_mem_lock); 5383c6939658Ssl108498 5384c1a9a9c3Skrishna mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock)); 5385c1a9a9c3Skrishna zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem; 5386c1a9a9c3Skrishna mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock)); 5387c1a9a9c3Skrishna 5388ff19e029SMenno Lageman /* remove lwps and process from proc's old zone and old project */ 53897c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_zone->zone_nlwps_lock); 53907c478bd9Sstevel@tonic-gate pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 53917c478bd9Sstevel@tonic-gate pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 5392ff19e029SMenno Lageman pp->p_task->tk_proj->kpj_nprocs--; 5393ff19e029SMenno Lageman pp->p_zone->zone_nprocs--; 53947c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_zone->zone_nlwps_lock); 53957c478bd9Sstevel@tonic-gate 53960209230bSgjelinek mutex_enter(&pp->p_zone->zone_mem_lock); 5397c6939658Ssl108498 pp->p_zone->zone_locked_mem -= pp->p_locked_mem; 5398c6939658Ssl108498 pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 53990209230bSgjelinek pp->p_zone->zone_max_swap -= swap; 54000209230bSgjelinek mutex_exit(&pp->p_zone->zone_mem_lock); 5401c6939658Ssl108498 5402c1a9a9c3Skrishna mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 5403c1a9a9c3Skrishna pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem; 5404c1a9a9c3Skrishna mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 5405c1a9a9c3Skrishna 5406bb5ca623SVamsi Nagineni pp->p_flag |= SZONETOP; 5407bb5ca623SVamsi Nagineni pp->p_zone = zone; 5408c6939658Ssl108498 mutex_exit(&pp->p_lock); 54090209230bSgjelinek AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock); 5410c6939658Ssl108498 54117c478bd9Sstevel@tonic-gate /* 54127c478bd9Sstevel@tonic-gate * Joining the zone cannot fail from now on. 54137c478bd9Sstevel@tonic-gate * 54147c478bd9Sstevel@tonic-gate * This means that a lot of the following code can be commonized and 54157c478bd9Sstevel@tonic-gate * shared with zsched(). 54167c478bd9Sstevel@tonic-gate */ 54177c478bd9Sstevel@tonic-gate 54187c478bd9Sstevel@tonic-gate /* 54197b209c2cSacruz * If the process contract fmri was inherited, we need to 54207b209c2cSacruz * flag this so that any contract status will not leak 54217b209c2cSacruz * extra zone information, svc_fmri in this case 54227b209c2cSacruz */ 54237b209c2cSacruz if (ctp->conp_svc_ctid != ct->ct_id) { 54247b209c2cSacruz mutex_enter(&ct->ct_lock); 54257b209c2cSacruz ctp->conp_svc_zone_enter = ct->ct_id; 54267b209c2cSacruz mutex_exit(&ct->ct_lock); 54277b209c2cSacruz } 54287b209c2cSacruz 54297b209c2cSacruz /* 54307c478bd9Sstevel@tonic-gate * Reset the encapsulating process contract's zone. 54317c478bd9Sstevel@tonic-gate */ 54327c478bd9Sstevel@tonic-gate ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 54337c478bd9Sstevel@tonic-gate contract_setzuniqid(ct, zone->zone_uniqid); 54347c478bd9Sstevel@tonic-gate 54357c478bd9Sstevel@tonic-gate /* 54367c478bd9Sstevel@tonic-gate * Create a new task and associate the process with the project keyed 54377c478bd9Sstevel@tonic-gate * by (projid,zoneid). 54387c478bd9Sstevel@tonic-gate * 54397c478bd9Sstevel@tonic-gate * We might as well be in project 0; the global zone's projid doesn't 54407c478bd9Sstevel@tonic-gate * make much sense in a zone anyhow. 54417c478bd9Sstevel@tonic-gate * 54427c478bd9Sstevel@tonic-gate * This also increments zone_ntasks, and returns with p_lock held. 54437c478bd9Sstevel@tonic-gate */ 54447c478bd9Sstevel@tonic-gate tk = task_create(0, zone); 54457c478bd9Sstevel@tonic-gate oldtk = task_join(tk, 0); 54467c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 54477c478bd9Sstevel@tonic-gate 54487c478bd9Sstevel@tonic-gate /* 54497c478bd9Sstevel@tonic-gate * call RCTLOP_SET functions on this proc 54507c478bd9Sstevel@tonic-gate */ 54517c478bd9Sstevel@tonic-gate e.rcep_p.zone = zone; 54527c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 54537c478bd9Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 54547c478bd9Sstevel@tonic-gate RCD_CALLBACK); 54557c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 54567c478bd9Sstevel@tonic-gate 54577c478bd9Sstevel@tonic-gate /* 54587c478bd9Sstevel@tonic-gate * We don't need to hold any of zsched's locks here; not only do we know 54597c478bd9Sstevel@tonic-gate * the process and zone aren't going away, we know its session isn't 54607c478bd9Sstevel@tonic-gate * changing either. 54617c478bd9Sstevel@tonic-gate * 54627c478bd9Sstevel@tonic-gate * By joining zsched's session here, we mimic the behavior in the 54637c478bd9Sstevel@tonic-gate * global zone of init's sid being the pid of sched. We extend this 54647c478bd9Sstevel@tonic-gate * to all zlogin-like zone_enter()'ing processes as well. 54657c478bd9Sstevel@tonic-gate */ 54667c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 54677c478bd9Sstevel@tonic-gate sp = zone->zone_zsched->p_sessp; 54689acbbeafSnn35248 sess_hold(zone->zone_zsched); 54697c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 54707c478bd9Sstevel@tonic-gate pgexit(pp); 54719acbbeafSnn35248 sess_rele(pp->p_sessp, B_TRUE); 54727c478bd9Sstevel@tonic-gate pp->p_sessp = sp; 54737c478bd9Sstevel@tonic-gate pgjoin(pp, zone->zone_zsched->p_pidp); 54740209230bSgjelinek 54750209230bSgjelinek /* 5476c97ad5cdSakolb * If any threads are scheduled to be placed on zone wait queue they 5477c97ad5cdSakolb * should abandon the idea since the wait queue is changing. 5478c97ad5cdSakolb * We need to be holding pidlock & p_lock to do this. 5479c97ad5cdSakolb */ 5480c97ad5cdSakolb if ((t = pp->p_tlist) != NULL) { 5481c97ad5cdSakolb do { 5482c97ad5cdSakolb thread_lock(t); 5483c97ad5cdSakolb /* 5484c97ad5cdSakolb * Kick this thread so that he doesn't sit 5485c97ad5cdSakolb * on a wrong wait queue. 5486c97ad5cdSakolb */ 5487c97ad5cdSakolb if (ISWAITING(t)) 5488c97ad5cdSakolb setrun_locked(t); 5489c97ad5cdSakolb 5490c97ad5cdSakolb if (t->t_schedflag & TS_ANYWAITQ) 5491c97ad5cdSakolb t->t_schedflag &= ~ TS_ANYWAITQ; 5492c97ad5cdSakolb 5493c97ad5cdSakolb thread_unlock(t); 5494c97ad5cdSakolb } while ((t = t->t_forw) != pp->p_tlist); 5495c97ad5cdSakolb } 5496c97ad5cdSakolb 5497c97ad5cdSakolb /* 54980209230bSgjelinek * If there is a default scheduling class for the zone and it is not 54990209230bSgjelinek * the class we are currently in, change all of the threads in the 55000209230bSgjelinek * process to the new class. We need to be holding pidlock & p_lock 55010209230bSgjelinek * when we call parmsset so this is a good place to do it. 55020209230bSgjelinek */ 55030209230bSgjelinek if (zone->zone_defaultcid > 0 && 55040209230bSgjelinek zone->zone_defaultcid != curthread->t_cid) { 55050209230bSgjelinek pcparms_t pcparms; 55060209230bSgjelinek 55070209230bSgjelinek pcparms.pc_cid = zone->zone_defaultcid; 55080209230bSgjelinek pcparms.pc_clparms[0] = 0; 55090209230bSgjelinek 55100209230bSgjelinek /* 55110209230bSgjelinek * If setting the class fails, we still want to enter the zone. 55120209230bSgjelinek */ 55130209230bSgjelinek if ((t = pp->p_tlist) != NULL) { 55140209230bSgjelinek do { 55150209230bSgjelinek (void) parmsset(&pcparms, t); 55160209230bSgjelinek } while ((t = t->t_forw) != pp->p_tlist); 55170209230bSgjelinek } 55180209230bSgjelinek } 55190209230bSgjelinek 55207c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 55217c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 55227c478bd9Sstevel@tonic-gate 55237c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 55247c478bd9Sstevel@tonic-gate /* 55257c478bd9Sstevel@tonic-gate * We're firmly in the zone; let pools progress. 55267c478bd9Sstevel@tonic-gate */ 55277c478bd9Sstevel@tonic-gate pool_unlock(); 55287c478bd9Sstevel@tonic-gate task_rele(oldtk); 55297c478bd9Sstevel@tonic-gate /* 55307c478bd9Sstevel@tonic-gate * We don't need to retain a hold on the zone since we already 55317c478bd9Sstevel@tonic-gate * incremented zone_ntasks, so the zone isn't going anywhere. 55327c478bd9Sstevel@tonic-gate */ 55337c478bd9Sstevel@tonic-gate zone_rele(zone); 55347c478bd9Sstevel@tonic-gate 55357c478bd9Sstevel@tonic-gate /* 55367c478bd9Sstevel@tonic-gate * Chroot 55377c478bd9Sstevel@tonic-gate */ 55387c478bd9Sstevel@tonic-gate vp = zone->zone_rootvp; 55397c478bd9Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 55407c478bd9Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 55417c478bd9Sstevel@tonic-gate 55427c478bd9Sstevel@tonic-gate /* 55437c478bd9Sstevel@tonic-gate * Change process credentials 55447c478bd9Sstevel@tonic-gate */ 55457c478bd9Sstevel@tonic-gate newcr = cralloc(); 55467c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 55477c478bd9Sstevel@tonic-gate cr = pp->p_cred; 55487c478bd9Sstevel@tonic-gate crcopy_to(cr, newcr); 55497c478bd9Sstevel@tonic-gate crsetzone(newcr, zone); 55507c478bd9Sstevel@tonic-gate pp->p_cred = newcr; 55517c478bd9Sstevel@tonic-gate 55527c478bd9Sstevel@tonic-gate /* 55537c478bd9Sstevel@tonic-gate * Restrict all process privilege sets to zone limit 55547c478bd9Sstevel@tonic-gate */ 55557c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 55567c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 55577c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 55587c478bd9Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 55597c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 55607c478bd9Sstevel@tonic-gate crset(pp, newcr); 55617c478bd9Sstevel@tonic-gate 55627c478bd9Sstevel@tonic-gate /* 55637c478bd9Sstevel@tonic-gate * Adjust upcount to reflect zone entry. 55647c478bd9Sstevel@tonic-gate */ 55657c478bd9Sstevel@tonic-gate uid = crgetruid(newcr); 55667c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 55677c478bd9Sstevel@tonic-gate upcount_dec(uid, GLOBAL_ZONEID); 55687c478bd9Sstevel@tonic-gate upcount_inc(uid, zoneid); 55697c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 55707c478bd9Sstevel@tonic-gate 55717c478bd9Sstevel@tonic-gate /* 55727c478bd9Sstevel@tonic-gate * Set up core file path and content. 55737c478bd9Sstevel@tonic-gate */ 55747c478bd9Sstevel@tonic-gate set_core_defaults(); 55757c478bd9Sstevel@tonic-gate 55767c478bd9Sstevel@tonic-gate out: 55777c478bd9Sstevel@tonic-gate /* 55787c478bd9Sstevel@tonic-gate * Let the other lwps continue. 55797c478bd9Sstevel@tonic-gate */ 55807c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 55817c478bd9Sstevel@tonic-gate if (curthread != pp->p_agenttp) 55827c478bd9Sstevel@tonic-gate continuelwps(pp); 55837c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 55847c478bd9Sstevel@tonic-gate 55857c478bd9Sstevel@tonic-gate return (err != 0 ? set_errno(err) : 0); 55867c478bd9Sstevel@tonic-gate } 55877c478bd9Sstevel@tonic-gate 55887c478bd9Sstevel@tonic-gate /* 55897c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_list(2). 55907c478bd9Sstevel@tonic-gate * 55917c478bd9Sstevel@tonic-gate * Processes running in a (non-global) zone only see themselves. 559245916cd2Sjpk * On labeled systems, they see all zones whose label they dominate. 55937c478bd9Sstevel@tonic-gate */ 55947c478bd9Sstevel@tonic-gate static int 55957c478bd9Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones) 55967c478bd9Sstevel@tonic-gate { 55977c478bd9Sstevel@tonic-gate zoneid_t *zoneids; 559848451833Scarlsonj zone_t *zone, *myzone; 55997c478bd9Sstevel@tonic-gate uint_t user_nzones, real_nzones; 560045916cd2Sjpk uint_t domi_nzones; 560145916cd2Sjpk int error; 56027c478bd9Sstevel@tonic-gate 56037c478bd9Sstevel@tonic-gate if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 56047c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 56057c478bd9Sstevel@tonic-gate 560648451833Scarlsonj myzone = curproc->p_zone; 560748451833Scarlsonj if (myzone != global_zone) { 560845916cd2Sjpk bslabel_t *mybslab; 560945916cd2Sjpk 561045916cd2Sjpk if (!is_system_labeled()) { 56117c478bd9Sstevel@tonic-gate /* just return current zone */ 561245916cd2Sjpk real_nzones = domi_nzones = 1; 56137c478bd9Sstevel@tonic-gate zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 561448451833Scarlsonj zoneids[0] = myzone->zone_id; 56157c478bd9Sstevel@tonic-gate } else { 561645916cd2Sjpk /* return all zones that are dominated */ 56177c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 56187c478bd9Sstevel@tonic-gate real_nzones = zonecount; 561945916cd2Sjpk domi_nzones = 0; 562045916cd2Sjpk if (real_nzones > 0) { 562145916cd2Sjpk zoneids = kmem_alloc(real_nzones * 562245916cd2Sjpk sizeof (zoneid_t), KM_SLEEP); 562348451833Scarlsonj mybslab = label2bslabel(myzone->zone_slabel); 562445916cd2Sjpk for (zone = list_head(&zone_active); 562545916cd2Sjpk zone != NULL; 562645916cd2Sjpk zone = list_next(&zone_active, zone)) { 562745916cd2Sjpk if (zone->zone_id == GLOBAL_ZONEID) 562845916cd2Sjpk continue; 562948451833Scarlsonj if (zone != myzone && 563048451833Scarlsonj (zone->zone_flags & ZF_IS_SCRATCH)) 563148451833Scarlsonj continue; 563248451833Scarlsonj /* 563348451833Scarlsonj * Note that a label always dominates 563448451833Scarlsonj * itself, so myzone is always included 563548451833Scarlsonj * in the list. 563648451833Scarlsonj */ 563745916cd2Sjpk if (bldominates(mybslab, 563845916cd2Sjpk label2bslabel(zone->zone_slabel))) { 563945916cd2Sjpk zoneids[domi_nzones++] = 564045916cd2Sjpk zone->zone_id; 564145916cd2Sjpk } 564245916cd2Sjpk } 564345916cd2Sjpk } 564445916cd2Sjpk mutex_exit(&zonehash_lock); 564545916cd2Sjpk } 564645916cd2Sjpk } else { 564745916cd2Sjpk mutex_enter(&zonehash_lock); 564845916cd2Sjpk real_nzones = zonecount; 564945916cd2Sjpk domi_nzones = 0; 565045916cd2Sjpk if (real_nzones > 0) { 56517c478bd9Sstevel@tonic-gate zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 56527c478bd9Sstevel@tonic-gate KM_SLEEP); 56537c478bd9Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 56547c478bd9Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 565545916cd2Sjpk zoneids[domi_nzones++] = zone->zone_id; 565645916cd2Sjpk ASSERT(domi_nzones == real_nzones); 56577c478bd9Sstevel@tonic-gate } 56587c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 56597c478bd9Sstevel@tonic-gate } 56607c478bd9Sstevel@tonic-gate 566145916cd2Sjpk /* 566245916cd2Sjpk * If user has allocated space for fewer entries than we found, then 566345916cd2Sjpk * return only up to his limit. Either way, tell him exactly how many 566445916cd2Sjpk * we found. 566545916cd2Sjpk */ 566645916cd2Sjpk if (domi_nzones < user_nzones) 566745916cd2Sjpk user_nzones = domi_nzones; 566845916cd2Sjpk error = 0; 566945916cd2Sjpk if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) { 56707c478bd9Sstevel@tonic-gate error = EFAULT; 567145916cd2Sjpk } else if (zoneidlist != NULL && user_nzones != 0) { 56727c478bd9Sstevel@tonic-gate if (copyout(zoneids, zoneidlist, 56737c478bd9Sstevel@tonic-gate user_nzones * sizeof (zoneid_t)) != 0) 56747c478bd9Sstevel@tonic-gate error = EFAULT; 56757c478bd9Sstevel@tonic-gate } 56767c478bd9Sstevel@tonic-gate 567745916cd2Sjpk if (real_nzones > 0) 56787c478bd9Sstevel@tonic-gate kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 56797c478bd9Sstevel@tonic-gate 568045916cd2Sjpk if (error != 0) 56817c478bd9Sstevel@tonic-gate return (set_errno(error)); 56827c478bd9Sstevel@tonic-gate else 56837c478bd9Sstevel@tonic-gate return (0); 56847c478bd9Sstevel@tonic-gate } 56857c478bd9Sstevel@tonic-gate 56867c478bd9Sstevel@tonic-gate /* 56877c478bd9Sstevel@tonic-gate * Systemcall entry point for zone_lookup(2). 56887c478bd9Sstevel@tonic-gate * 568945916cd2Sjpk * Non-global zones are only able to see themselves and (on labeled systems) 569045916cd2Sjpk * the zones they dominate. 56917c478bd9Sstevel@tonic-gate */ 56927c478bd9Sstevel@tonic-gate static zoneid_t 56937c478bd9Sstevel@tonic-gate zone_lookup(const char *zone_name) 56947c478bd9Sstevel@tonic-gate { 56957c478bd9Sstevel@tonic-gate char *kname; 56967c478bd9Sstevel@tonic-gate zone_t *zone; 56977c478bd9Sstevel@tonic-gate zoneid_t zoneid; 56987c478bd9Sstevel@tonic-gate int err; 56997c478bd9Sstevel@tonic-gate 57007c478bd9Sstevel@tonic-gate if (zone_name == NULL) { 57017c478bd9Sstevel@tonic-gate /* return caller's zone id */ 57027c478bd9Sstevel@tonic-gate return (getzoneid()); 57037c478bd9Sstevel@tonic-gate } 57047c478bd9Sstevel@tonic-gate 57057c478bd9Sstevel@tonic-gate kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 57067c478bd9Sstevel@tonic-gate if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 57077c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 57087c478bd9Sstevel@tonic-gate return (set_errno(err)); 57097c478bd9Sstevel@tonic-gate } 57107c478bd9Sstevel@tonic-gate 57117c478bd9Sstevel@tonic-gate mutex_enter(&zonehash_lock); 57127c478bd9Sstevel@tonic-gate zone = zone_find_all_by_name(kname); 57137c478bd9Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 571445916cd2Sjpk /* 571545916cd2Sjpk * In a non-global zone, can only lookup global and own name. 571645916cd2Sjpk * In Trusted Extensions zone label dominance rules apply. 571745916cd2Sjpk */ 571845916cd2Sjpk if (zone == NULL || 571945916cd2Sjpk zone_status_get(zone) < ZONE_IS_READY || 572045916cd2Sjpk !zone_list_access(zone)) { 57217c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 57227c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 572345916cd2Sjpk } else { 57247c478bd9Sstevel@tonic-gate zoneid = zone->zone_id; 57257c478bd9Sstevel@tonic-gate mutex_exit(&zonehash_lock); 57267c478bd9Sstevel@tonic-gate return (zoneid); 57277c478bd9Sstevel@tonic-gate } 572845916cd2Sjpk } 57297c478bd9Sstevel@tonic-gate 5730821c4a97Sdp static int 5731821c4a97Sdp zone_version(int *version_arg) 5732821c4a97Sdp { 5733821c4a97Sdp int version = ZONE_SYSCALL_API_VERSION; 5734821c4a97Sdp 5735821c4a97Sdp if (copyout(&version, version_arg, sizeof (int)) != 0) 5736821c4a97Sdp return (set_errno(EFAULT)); 5737821c4a97Sdp return (0); 5738821c4a97Sdp } 5739821c4a97Sdp 57407c478bd9Sstevel@tonic-gate /* ARGSUSED */ 57417c478bd9Sstevel@tonic-gate long 5742fa9e4066Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4) 57437c478bd9Sstevel@tonic-gate { 57447c478bd9Sstevel@tonic-gate zone_def zs; 57452b24ab6bSSebastien Roy int err; 57467c478bd9Sstevel@tonic-gate 57477c478bd9Sstevel@tonic-gate switch (cmd) { 57487c478bd9Sstevel@tonic-gate case ZONE_CREATE: 57497c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 57507c478bd9Sstevel@tonic-gate if (copyin(arg1, &zs, sizeof (zone_def))) { 57517c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 57527c478bd9Sstevel@tonic-gate } 57537c478bd9Sstevel@tonic-gate } else { 57547c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 57557c478bd9Sstevel@tonic-gate zone_def32 zs32; 57567c478bd9Sstevel@tonic-gate 57577c478bd9Sstevel@tonic-gate if (copyin(arg1, &zs32, sizeof (zone_def32))) { 57587c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 57597c478bd9Sstevel@tonic-gate } 57607c478bd9Sstevel@tonic-gate zs.zone_name = 57617c478bd9Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_name; 57627c478bd9Sstevel@tonic-gate zs.zone_root = 57637c478bd9Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_root; 57647c478bd9Sstevel@tonic-gate zs.zone_privs = 57657c478bd9Sstevel@tonic-gate (const struct priv_set *) 57667c478bd9Sstevel@tonic-gate (unsigned long)zs32.zone_privs; 57676f70df68Sdp zs.zone_privssz = zs32.zone_privssz; 57687c478bd9Sstevel@tonic-gate zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 57697c478bd9Sstevel@tonic-gate zs.rctlbufsz = zs32.rctlbufsz; 5770fa9e4066Sahrens zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf; 5771fa9e4066Sahrens zs.zfsbufsz = zs32.zfsbufsz; 57727c478bd9Sstevel@tonic-gate zs.extended_error = 57737c478bd9Sstevel@tonic-gate (int *)(unsigned long)zs32.extended_error; 577445916cd2Sjpk zs.match = zs32.match; 577545916cd2Sjpk zs.doi = zs32.doi; 577645916cd2Sjpk zs.label = (const bslabel_t *)(uintptr_t)zs32.label; 5777f4b3ec61Sdh155122 zs.flags = zs32.flags; 57787c478bd9Sstevel@tonic-gate #else 57797c478bd9Sstevel@tonic-gate panic("get_udatamodel() returned bogus result\n"); 57807c478bd9Sstevel@tonic-gate #endif 57817c478bd9Sstevel@tonic-gate } 57827c478bd9Sstevel@tonic-gate 57837c478bd9Sstevel@tonic-gate return (zone_create(zs.zone_name, zs.zone_root, 5784821c4a97Sdp zs.zone_privs, zs.zone_privssz, 5785821c4a97Sdp (caddr_t)zs.rctlbuf, zs.rctlbufsz, 5786fa9e4066Sahrens (caddr_t)zs.zfsbuf, zs.zfsbufsz, 578745916cd2Sjpk zs.extended_error, zs.match, zs.doi, 5788f4b3ec61Sdh155122 zs.label, zs.flags)); 57897c478bd9Sstevel@tonic-gate case ZONE_BOOT: 57903f2f09c1Sdp return (zone_boot((zoneid_t)(uintptr_t)arg1)); 57917c478bd9Sstevel@tonic-gate case ZONE_DESTROY: 57927c478bd9Sstevel@tonic-gate return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 57937c478bd9Sstevel@tonic-gate case ZONE_GETATTR: 57947c478bd9Sstevel@tonic-gate return (zone_getattr((zoneid_t)(uintptr_t)arg1, 57957c478bd9Sstevel@tonic-gate (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 57963f2f09c1Sdp case ZONE_SETATTR: 57973f2f09c1Sdp return (zone_setattr((zoneid_t)(uintptr_t)arg1, 57983f2f09c1Sdp (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 57997c478bd9Sstevel@tonic-gate case ZONE_ENTER: 58007c478bd9Sstevel@tonic-gate return (zone_enter((zoneid_t)(uintptr_t)arg1)); 58017c478bd9Sstevel@tonic-gate case ZONE_LIST: 58027c478bd9Sstevel@tonic-gate return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 58037c478bd9Sstevel@tonic-gate case ZONE_SHUTDOWN: 58047c478bd9Sstevel@tonic-gate return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 58057c478bd9Sstevel@tonic-gate case ZONE_LOOKUP: 58067c478bd9Sstevel@tonic-gate return (zone_lookup((const char *)arg1)); 5807821c4a97Sdp case ZONE_VERSION: 5808821c4a97Sdp return (zone_version((int *)arg1)); 5809f4b3ec61Sdh155122 case ZONE_ADD_DATALINK: 5810f4b3ec61Sdh155122 return (zone_add_datalink((zoneid_t)(uintptr_t)arg1, 58112b24ab6bSSebastien Roy (datalink_id_t)(uintptr_t)arg2)); 5812f4b3ec61Sdh155122 case ZONE_DEL_DATALINK: 5813f4b3ec61Sdh155122 return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1, 58142b24ab6bSSebastien Roy (datalink_id_t)(uintptr_t)arg2)); 58152b24ab6bSSebastien Roy case ZONE_CHECK_DATALINK: { 58162b24ab6bSSebastien Roy zoneid_t zoneid; 58172b24ab6bSSebastien Roy boolean_t need_copyout; 58182b24ab6bSSebastien Roy 58192b24ab6bSSebastien Roy if (copyin(arg1, &zoneid, sizeof (zoneid)) != 0) 58202b24ab6bSSebastien Roy return (EFAULT); 58212b24ab6bSSebastien Roy need_copyout = (zoneid == ALL_ZONES); 58222b24ab6bSSebastien Roy err = zone_check_datalink(&zoneid, 58232b24ab6bSSebastien Roy (datalink_id_t)(uintptr_t)arg2); 58242b24ab6bSSebastien Roy if (err == 0 && need_copyout) { 58252b24ab6bSSebastien Roy if (copyout(&zoneid, arg1, sizeof (zoneid)) != 0) 58262b24ab6bSSebastien Roy err = EFAULT; 58272b24ab6bSSebastien Roy } 58282b24ab6bSSebastien Roy return (err == 0 ? 0 : set_errno(err)); 58292b24ab6bSSebastien Roy } 5830f4b3ec61Sdh155122 case ZONE_LIST_DATALINK: 5831f4b3ec61Sdh155122 return (zone_list_datalink((zoneid_t)(uintptr_t)arg1, 58322b24ab6bSSebastien Roy (int *)arg2, (datalink_id_t *)(uintptr_t)arg3)); 58337c478bd9Sstevel@tonic-gate default: 58347c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 58357c478bd9Sstevel@tonic-gate } 58367c478bd9Sstevel@tonic-gate } 58377c478bd9Sstevel@tonic-gate 58387c478bd9Sstevel@tonic-gate struct zarg { 58397c478bd9Sstevel@tonic-gate zone_t *zone; 58407c478bd9Sstevel@tonic-gate zone_cmd_arg_t arg; 58417c478bd9Sstevel@tonic-gate }; 58427c478bd9Sstevel@tonic-gate 58437c478bd9Sstevel@tonic-gate static int 58447c478bd9Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp) 58457c478bd9Sstevel@tonic-gate { 58467c478bd9Sstevel@tonic-gate char *buf; 58477c478bd9Sstevel@tonic-gate size_t buflen; 58487c478bd9Sstevel@tonic-gate int error; 58497c478bd9Sstevel@tonic-gate 58507c478bd9Sstevel@tonic-gate buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 58517c478bd9Sstevel@tonic-gate buf = kmem_alloc(buflen, KM_SLEEP); 58527c478bd9Sstevel@tonic-gate (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 58537c478bd9Sstevel@tonic-gate error = door_ki_open(buf, doorp); 58547c478bd9Sstevel@tonic-gate kmem_free(buf, buflen); 58557c478bd9Sstevel@tonic-gate return (error); 58567c478bd9Sstevel@tonic-gate } 58577c478bd9Sstevel@tonic-gate 58587c478bd9Sstevel@tonic-gate static void 58597c478bd9Sstevel@tonic-gate zone_release_door(door_handle_t *doorp) 58607c478bd9Sstevel@tonic-gate { 58617c478bd9Sstevel@tonic-gate door_ki_rele(*doorp); 58627c478bd9Sstevel@tonic-gate *doorp = NULL; 58637c478bd9Sstevel@tonic-gate } 58647c478bd9Sstevel@tonic-gate 58657c478bd9Sstevel@tonic-gate static void 58667c478bd9Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp) 58677c478bd9Sstevel@tonic-gate { 58687c478bd9Sstevel@tonic-gate door_handle_t door = NULL; 58697c478bd9Sstevel@tonic-gate door_arg_t darg, save_arg; 58707c478bd9Sstevel@tonic-gate char *zone_name; 58717c478bd9Sstevel@tonic-gate size_t zone_namelen; 58727c478bd9Sstevel@tonic-gate zoneid_t zoneid; 58737c478bd9Sstevel@tonic-gate zone_t *zone; 58747c478bd9Sstevel@tonic-gate zone_cmd_arg_t arg; 58757c478bd9Sstevel@tonic-gate uint64_t uniqid; 58767c478bd9Sstevel@tonic-gate size_t size; 58777c478bd9Sstevel@tonic-gate int error; 58787c478bd9Sstevel@tonic-gate int retry; 58797c478bd9Sstevel@tonic-gate 58807c478bd9Sstevel@tonic-gate zone = zargp->zone; 58817c478bd9Sstevel@tonic-gate arg = zargp->arg; 58827c478bd9Sstevel@tonic-gate kmem_free(zargp, sizeof (*zargp)); 58837c478bd9Sstevel@tonic-gate 58847c478bd9Sstevel@tonic-gate zone_namelen = strlen(zone->zone_name) + 1; 58857c478bd9Sstevel@tonic-gate zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 58867c478bd9Sstevel@tonic-gate bcopy(zone->zone_name, zone_name, zone_namelen); 58877c478bd9Sstevel@tonic-gate zoneid = zone->zone_id; 58887c478bd9Sstevel@tonic-gate uniqid = zone->zone_uniqid; 58897c478bd9Sstevel@tonic-gate /* 58907c478bd9Sstevel@tonic-gate * zoneadmd may be down, but at least we can empty out the zone. 58917c478bd9Sstevel@tonic-gate * We can ignore the return value of zone_empty() since we're called 58927c478bd9Sstevel@tonic-gate * from a kernel thread and know we won't be delivered any signals. 58937c478bd9Sstevel@tonic-gate */ 58947c478bd9Sstevel@tonic-gate ASSERT(curproc == &p0); 58957c478bd9Sstevel@tonic-gate (void) zone_empty(zone); 58967c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 58977c478bd9Sstevel@tonic-gate zone_rele(zone); 58987c478bd9Sstevel@tonic-gate 58997c478bd9Sstevel@tonic-gate size = sizeof (arg); 59007c478bd9Sstevel@tonic-gate darg.rbuf = (char *)&arg; 59017c478bd9Sstevel@tonic-gate darg.data_ptr = (char *)&arg; 59027c478bd9Sstevel@tonic-gate darg.rsize = size; 59037c478bd9Sstevel@tonic-gate darg.data_size = size; 59047c478bd9Sstevel@tonic-gate darg.desc_ptr = NULL; 59057c478bd9Sstevel@tonic-gate darg.desc_num = 0; 59067c478bd9Sstevel@tonic-gate 59077c478bd9Sstevel@tonic-gate save_arg = darg; 59087c478bd9Sstevel@tonic-gate /* 59097c478bd9Sstevel@tonic-gate * Since we're not holding a reference to the zone, any number of 59107c478bd9Sstevel@tonic-gate * things can go wrong, including the zone disappearing before we get a 59117c478bd9Sstevel@tonic-gate * chance to talk to zoneadmd. 59127c478bd9Sstevel@tonic-gate */ 59137c478bd9Sstevel@tonic-gate for (retry = 0; /* forever */; retry++) { 59147c478bd9Sstevel@tonic-gate if (door == NULL && 59157c478bd9Sstevel@tonic-gate (error = zone_lookup_door(zone_name, &door)) != 0) { 59167c478bd9Sstevel@tonic-gate goto next; 59177c478bd9Sstevel@tonic-gate } 59187c478bd9Sstevel@tonic-gate ASSERT(door != NULL); 59197c478bd9Sstevel@tonic-gate 5920323a81d9Sjwadams if ((error = door_ki_upcall_limited(door, &darg, NULL, 5921323a81d9Sjwadams SIZE_MAX, 0)) == 0) { 59227c478bd9Sstevel@tonic-gate break; 59237c478bd9Sstevel@tonic-gate } 59247c478bd9Sstevel@tonic-gate switch (error) { 59257c478bd9Sstevel@tonic-gate case EINTR: 59267c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 59277c478bd9Sstevel@tonic-gate case EAGAIN: /* process may be forking */ 59287c478bd9Sstevel@tonic-gate /* 59297c478bd9Sstevel@tonic-gate * Back off for a bit 59307c478bd9Sstevel@tonic-gate */ 59317c478bd9Sstevel@tonic-gate break; 59327c478bd9Sstevel@tonic-gate case EBADF: 59337c478bd9Sstevel@tonic-gate zone_release_door(&door); 59347c478bd9Sstevel@tonic-gate if (zone_lookup_door(zone_name, &door) != 0) { 59357c478bd9Sstevel@tonic-gate /* 59367c478bd9Sstevel@tonic-gate * zoneadmd may be dead, but it may come back to 59377c478bd9Sstevel@tonic-gate * life later. 59387c478bd9Sstevel@tonic-gate */ 59397c478bd9Sstevel@tonic-gate break; 59407c478bd9Sstevel@tonic-gate } 59417c478bd9Sstevel@tonic-gate break; 59427c478bd9Sstevel@tonic-gate default: 59437c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 59447c478bd9Sstevel@tonic-gate "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 59457c478bd9Sstevel@tonic-gate error); 59467c478bd9Sstevel@tonic-gate goto out; 59477c478bd9Sstevel@tonic-gate } 59487c478bd9Sstevel@tonic-gate next: 59497c478bd9Sstevel@tonic-gate /* 59507c478bd9Sstevel@tonic-gate * If this isn't the same zone_t that we originally had in mind, 59517c478bd9Sstevel@tonic-gate * then this is the same as if two kadmin requests come in at 59527c478bd9Sstevel@tonic-gate * the same time: the first one wins. This means we lose, so we 59537c478bd9Sstevel@tonic-gate * bail. 59547c478bd9Sstevel@tonic-gate */ 59557c478bd9Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) { 59567c478bd9Sstevel@tonic-gate /* 59577c478bd9Sstevel@tonic-gate * Problem is solved. 59587c478bd9Sstevel@tonic-gate */ 59597c478bd9Sstevel@tonic-gate break; 59607c478bd9Sstevel@tonic-gate } 59617c478bd9Sstevel@tonic-gate if (zone->zone_uniqid != uniqid) { 59627c478bd9Sstevel@tonic-gate /* 59637c478bd9Sstevel@tonic-gate * zoneid recycled 59647c478bd9Sstevel@tonic-gate */ 59657c478bd9Sstevel@tonic-gate zone_rele(zone); 59667c478bd9Sstevel@tonic-gate break; 59677c478bd9Sstevel@tonic-gate } 59687c478bd9Sstevel@tonic-gate /* 59697c478bd9Sstevel@tonic-gate * We could zone_status_timedwait(), but there doesn't seem to 59707c478bd9Sstevel@tonic-gate * be much point in doing that (plus, it would mean that 59717c478bd9Sstevel@tonic-gate * zone_free() isn't called until this thread exits). 59727c478bd9Sstevel@tonic-gate */ 59737c478bd9Sstevel@tonic-gate zone_rele(zone); 59747c478bd9Sstevel@tonic-gate delay(hz); 59757c478bd9Sstevel@tonic-gate darg = save_arg; 59767c478bd9Sstevel@tonic-gate } 59777c478bd9Sstevel@tonic-gate out: 59787c478bd9Sstevel@tonic-gate if (door != NULL) { 59797c478bd9Sstevel@tonic-gate zone_release_door(&door); 59807c478bd9Sstevel@tonic-gate } 59817c478bd9Sstevel@tonic-gate kmem_free(zone_name, zone_namelen); 59827c478bd9Sstevel@tonic-gate thread_exit(); 59837c478bd9Sstevel@tonic-gate } 59847c478bd9Sstevel@tonic-gate 59857c478bd9Sstevel@tonic-gate /* 59863f2f09c1Sdp * Entry point for uadmin() to tell the zone to go away or reboot. Analog to 59873f2f09c1Sdp * kadmin(). The caller is a process in the zone. 59887c478bd9Sstevel@tonic-gate * 59897c478bd9Sstevel@tonic-gate * In order to shutdown the zone, we will hand off control to zoneadmd 59907c478bd9Sstevel@tonic-gate * (running in the global zone) via a door. We do a half-hearted job at 59917c478bd9Sstevel@tonic-gate * killing all processes in the zone, create a kernel thread to contact 59927c478bd9Sstevel@tonic-gate * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 59937c478bd9Sstevel@tonic-gate * a form of generation number used to let zoneadmd (as well as 59947c478bd9Sstevel@tonic-gate * zone_destroy()) know exactly which zone they're re talking about. 59957c478bd9Sstevel@tonic-gate */ 59967c478bd9Sstevel@tonic-gate int 59973f2f09c1Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp) 59987c478bd9Sstevel@tonic-gate { 59997c478bd9Sstevel@tonic-gate struct zarg *zargp; 60007c478bd9Sstevel@tonic-gate zone_cmd_t zcmd; 60017c478bd9Sstevel@tonic-gate zone_t *zone; 60027c478bd9Sstevel@tonic-gate 60037c478bd9Sstevel@tonic-gate zone = curproc->p_zone; 60047c478bd9Sstevel@tonic-gate ASSERT(getzoneid() != GLOBAL_ZONEID); 60057c478bd9Sstevel@tonic-gate 60067c478bd9Sstevel@tonic-gate switch (cmd) { 60077c478bd9Sstevel@tonic-gate case A_SHUTDOWN: 60087c478bd9Sstevel@tonic-gate switch (fcn) { 60097c478bd9Sstevel@tonic-gate case AD_HALT: 60107c478bd9Sstevel@tonic-gate case AD_POWEROFF: 60117c478bd9Sstevel@tonic-gate zcmd = Z_HALT; 60127c478bd9Sstevel@tonic-gate break; 60137c478bd9Sstevel@tonic-gate case AD_BOOT: 60147c478bd9Sstevel@tonic-gate zcmd = Z_REBOOT; 60157c478bd9Sstevel@tonic-gate break; 60167c478bd9Sstevel@tonic-gate case AD_IBOOT: 60177c478bd9Sstevel@tonic-gate case AD_SBOOT: 60187c478bd9Sstevel@tonic-gate case AD_SIBOOT: 60197c478bd9Sstevel@tonic-gate case AD_NOSYNC: 60207c478bd9Sstevel@tonic-gate return (ENOTSUP); 60217c478bd9Sstevel@tonic-gate default: 60227c478bd9Sstevel@tonic-gate return (EINVAL); 60237c478bd9Sstevel@tonic-gate } 60247c478bd9Sstevel@tonic-gate break; 60257c478bd9Sstevel@tonic-gate case A_REBOOT: 60267c478bd9Sstevel@tonic-gate zcmd = Z_REBOOT; 60277c478bd9Sstevel@tonic-gate break; 60287c478bd9Sstevel@tonic-gate case A_FTRACE: 60297c478bd9Sstevel@tonic-gate case A_REMOUNT: 60307c478bd9Sstevel@tonic-gate case A_FREEZE: 60317c478bd9Sstevel@tonic-gate case A_DUMP: 6032753a6d45SSherry Moore case A_CONFIG: 60337c478bd9Sstevel@tonic-gate return (ENOTSUP); 60347c478bd9Sstevel@tonic-gate default: 60357c478bd9Sstevel@tonic-gate ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 60367c478bd9Sstevel@tonic-gate return (EINVAL); 60377c478bd9Sstevel@tonic-gate } 60387c478bd9Sstevel@tonic-gate 60397c478bd9Sstevel@tonic-gate if (secpolicy_zone_admin(credp, B_FALSE)) 60407c478bd9Sstevel@tonic-gate return (EPERM); 60417c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 60423f2f09c1Sdp 60437c478bd9Sstevel@tonic-gate /* 60447c478bd9Sstevel@tonic-gate * zone_status can't be ZONE_IS_EMPTY or higher since curproc 60457c478bd9Sstevel@tonic-gate * is in the zone. 60467c478bd9Sstevel@tonic-gate */ 60477c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 60487c478bd9Sstevel@tonic-gate if (zone_status_get(zone) > ZONE_IS_RUNNING) { 60497c478bd9Sstevel@tonic-gate /* 60507c478bd9Sstevel@tonic-gate * This zone is already on its way down. 60517c478bd9Sstevel@tonic-gate */ 60527c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 60537c478bd9Sstevel@tonic-gate return (0); 60547c478bd9Sstevel@tonic-gate } 60557c478bd9Sstevel@tonic-gate /* 60567c478bd9Sstevel@tonic-gate * Prevent future zone_enter()s 60577c478bd9Sstevel@tonic-gate */ 60587c478bd9Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 60597c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 60607c478bd9Sstevel@tonic-gate 60617c478bd9Sstevel@tonic-gate /* 60627c478bd9Sstevel@tonic-gate * Kill everyone now and call zoneadmd later. 60637c478bd9Sstevel@tonic-gate * zone_ki_call_zoneadmd() will do a more thorough job of this 60647c478bd9Sstevel@tonic-gate * later. 60657c478bd9Sstevel@tonic-gate */ 60667c478bd9Sstevel@tonic-gate killall(zone->zone_id); 60677c478bd9Sstevel@tonic-gate /* 60687c478bd9Sstevel@tonic-gate * Now, create the thread to contact zoneadmd and do the rest of the 60697c478bd9Sstevel@tonic-gate * work. This thread can't be created in our zone otherwise 60707c478bd9Sstevel@tonic-gate * zone_destroy() would deadlock. 60717c478bd9Sstevel@tonic-gate */ 60723f2f09c1Sdp zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP); 60737c478bd9Sstevel@tonic-gate zargp->arg.cmd = zcmd; 60747c478bd9Sstevel@tonic-gate zargp->arg.uniqid = zone->zone_uniqid; 60753f2f09c1Sdp zargp->zone = zone; 60767c478bd9Sstevel@tonic-gate (void) strcpy(zargp->arg.locale, "C"); 60773f2f09c1Sdp /* mdep was already copied in for us by uadmin */ 60783f2f09c1Sdp if (mdep != NULL) 60793f2f09c1Sdp (void) strlcpy(zargp->arg.bootbuf, mdep, 60803f2f09c1Sdp sizeof (zargp->arg.bootbuf)); 60813f2f09c1Sdp zone_hold(zone); 60827c478bd9Sstevel@tonic-gate 60837c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 60847c478bd9Sstevel@tonic-gate TS_RUN, minclsyspri); 60857c478bd9Sstevel@tonic-gate exit(CLD_EXITED, 0); 60867c478bd9Sstevel@tonic-gate 60877c478bd9Sstevel@tonic-gate return (EINVAL); 60887c478bd9Sstevel@tonic-gate } 60897c478bd9Sstevel@tonic-gate 60907c478bd9Sstevel@tonic-gate /* 60917c478bd9Sstevel@tonic-gate * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 60927c478bd9Sstevel@tonic-gate * status to ZONE_IS_SHUTTING_DOWN. 60938f983ab3Sjv227347 * 60948f983ab3Sjv227347 * This function also shuts down all running zones to ensure that they won't 60958f983ab3Sjv227347 * fork new processes. 60967c478bd9Sstevel@tonic-gate */ 60977c478bd9Sstevel@tonic-gate void 60987c478bd9Sstevel@tonic-gate zone_shutdown_global(void) 60997c478bd9Sstevel@tonic-gate { 61008f983ab3Sjv227347 zone_t *current_zonep; 61017c478bd9Sstevel@tonic-gate 61028f983ab3Sjv227347 ASSERT(INGLOBALZONE(curproc)); 61038f983ab3Sjv227347 mutex_enter(&zonehash_lock); 61047c478bd9Sstevel@tonic-gate mutex_enter(&zone_status_lock); 61058f983ab3Sjv227347 61068f983ab3Sjv227347 /* Modify the global zone's status first. */ 61077c478bd9Sstevel@tonic-gate ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 61087c478bd9Sstevel@tonic-gate zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 61098f983ab3Sjv227347 61108f983ab3Sjv227347 /* 61118f983ab3Sjv227347 * Now change the states of all running zones to ZONE_IS_SHUTTING_DOWN. 61128f983ab3Sjv227347 * We don't mark all zones with ZONE_IS_SHUTTING_DOWN because doing so 61138f983ab3Sjv227347 * could cause assertions to fail (e.g., assertions about a zone's 61148f983ab3Sjv227347 * state during initialization, readying, or booting) or produce races. 61158f983ab3Sjv227347 * We'll let threads continue to initialize and ready new zones: they'll 61168f983ab3Sjv227347 * fail to boot the new zones when they see that the global zone is 61178f983ab3Sjv227347 * shutting down. 61188f983ab3Sjv227347 */ 61198f983ab3Sjv227347 for (current_zonep = list_head(&zone_active); current_zonep != NULL; 61208f983ab3Sjv227347 current_zonep = list_next(&zone_active, current_zonep)) { 61218f983ab3Sjv227347 if (zone_status_get(current_zonep) == ZONE_IS_RUNNING) 61228f983ab3Sjv227347 zone_status_set(current_zonep, ZONE_IS_SHUTTING_DOWN); 61238f983ab3Sjv227347 } 61247c478bd9Sstevel@tonic-gate mutex_exit(&zone_status_lock); 61258f983ab3Sjv227347 mutex_exit(&zonehash_lock); 61267c478bd9Sstevel@tonic-gate } 6127fa9e4066Sahrens 6128fa9e4066Sahrens /* 6129fa9e4066Sahrens * Returns true if the named dataset is visible in the current zone. 6130fa9e4066Sahrens * The 'write' parameter is set to 1 if the dataset is also writable. 6131fa9e4066Sahrens */ 6132fa9e4066Sahrens int 6133fa9e4066Sahrens zone_dataset_visible(const char *dataset, int *write) 6134fa9e4066Sahrens { 6135658a1dc7SSanjeev Bagewadi static int zfstype = -1; 6136fa9e4066Sahrens zone_dataset_t *zd; 6137fa9e4066Sahrens size_t len; 6138fa9e4066Sahrens zone_t *zone = curproc->p_zone; 6139658a1dc7SSanjeev Bagewadi const char *name = NULL; 6140658a1dc7SSanjeev Bagewadi vfs_t *vfsp = NULL; 6141fa9e4066Sahrens 6142fa9e4066Sahrens if (dataset[0] == '\0') 6143fa9e4066Sahrens return (0); 6144fa9e4066Sahrens 6145fa9e4066Sahrens /* 6146fa9e4066Sahrens * Walk the list once, looking for datasets which match exactly, or 6147fa9e4066Sahrens * specify a dataset underneath an exported dataset. If found, return 6148fa9e4066Sahrens * true and note that it is writable. 6149fa9e4066Sahrens */ 6150fa9e4066Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 6151fa9e4066Sahrens zd = list_next(&zone->zone_datasets, zd)) { 6152fa9e4066Sahrens 6153fa9e4066Sahrens len = strlen(zd->zd_dataset); 6154fa9e4066Sahrens if (strlen(dataset) >= len && 6155fa9e4066Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 615695c9592aSmaybee (dataset[len] == '\0' || dataset[len] == '/' || 615795c9592aSmaybee dataset[len] == '@')) { 6158fa9e4066Sahrens if (write) 6159fa9e4066Sahrens *write = 1; 6160fa9e4066Sahrens return (1); 6161fa9e4066Sahrens } 6162fa9e4066Sahrens } 6163fa9e4066Sahrens 6164fa9e4066Sahrens /* 6165fa9e4066Sahrens * Walk the list a second time, searching for datasets which are parents 6166fa9e4066Sahrens * of exported datasets. These should be visible, but read-only. 6167fa9e4066Sahrens * 6168fa9e4066Sahrens * Note that we also have to support forms such as 'pool/dataset/', with 6169fa9e4066Sahrens * a trailing slash. 6170fa9e4066Sahrens */ 6171fa9e4066Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 6172fa9e4066Sahrens zd = list_next(&zone->zone_datasets, zd)) { 6173fa9e4066Sahrens 6174fa9e4066Sahrens len = strlen(dataset); 6175fa9e4066Sahrens if (dataset[len - 1] == '/') 6176fa9e4066Sahrens len--; /* Ignore trailing slash */ 6177fa9e4066Sahrens if (len < strlen(zd->zd_dataset) && 6178fa9e4066Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 6179fa9e4066Sahrens zd->zd_dataset[len] == '/') { 6180fa9e4066Sahrens if (write) 6181fa9e4066Sahrens *write = 0; 6182fa9e4066Sahrens return (1); 6183fa9e4066Sahrens } 6184fa9e4066Sahrens } 6185fa9e4066Sahrens 6186658a1dc7SSanjeev Bagewadi /* 6187658a1dc7SSanjeev Bagewadi * We reach here if the given dataset is not found in the zone_dataset 6188658a1dc7SSanjeev Bagewadi * list. Check if this dataset was added as a filesystem (ie. "add fs") 6189658a1dc7SSanjeev Bagewadi * instead of delegation. For this we search for the dataset in the 6190658a1dc7SSanjeev Bagewadi * zone_vfslist of this zone. If found, return true and note that it is 6191658a1dc7SSanjeev Bagewadi * not writable. 6192658a1dc7SSanjeev Bagewadi */ 6193658a1dc7SSanjeev Bagewadi 6194658a1dc7SSanjeev Bagewadi /* 6195658a1dc7SSanjeev Bagewadi * Initialize zfstype if it is not initialized yet. 6196658a1dc7SSanjeev Bagewadi */ 6197658a1dc7SSanjeev Bagewadi if (zfstype == -1) { 6198658a1dc7SSanjeev Bagewadi struct vfssw *vswp = vfs_getvfssw("zfs"); 6199658a1dc7SSanjeev Bagewadi zfstype = vswp - vfssw; 6200658a1dc7SSanjeev Bagewadi vfs_unrefvfssw(vswp); 6201658a1dc7SSanjeev Bagewadi } 6202658a1dc7SSanjeev Bagewadi 6203658a1dc7SSanjeev Bagewadi vfs_list_read_lock(); 6204658a1dc7SSanjeev Bagewadi vfsp = zone->zone_vfslist; 6205658a1dc7SSanjeev Bagewadi do { 6206658a1dc7SSanjeev Bagewadi ASSERT(vfsp); 6207658a1dc7SSanjeev Bagewadi if (vfsp->vfs_fstype == zfstype) { 6208658a1dc7SSanjeev Bagewadi name = refstr_value(vfsp->vfs_resource); 6209658a1dc7SSanjeev Bagewadi 6210658a1dc7SSanjeev Bagewadi /* 6211658a1dc7SSanjeev Bagewadi * Check if we have an exact match. 6212658a1dc7SSanjeev Bagewadi */ 6213658a1dc7SSanjeev Bagewadi if (strcmp(dataset, name) == 0) { 6214658a1dc7SSanjeev Bagewadi vfs_list_unlock(); 6215658a1dc7SSanjeev Bagewadi if (write) 6216658a1dc7SSanjeev Bagewadi *write = 0; 6217658a1dc7SSanjeev Bagewadi return (1); 6218658a1dc7SSanjeev Bagewadi } 6219658a1dc7SSanjeev Bagewadi /* 6220658a1dc7SSanjeev Bagewadi * We need to check if we are looking for parents of 6221658a1dc7SSanjeev Bagewadi * a dataset. These should be visible, but read-only. 6222658a1dc7SSanjeev Bagewadi */ 6223658a1dc7SSanjeev Bagewadi len = strlen(dataset); 6224658a1dc7SSanjeev Bagewadi if (dataset[len - 1] == '/') 6225658a1dc7SSanjeev Bagewadi len--; 6226658a1dc7SSanjeev Bagewadi 6227658a1dc7SSanjeev Bagewadi if (len < strlen(name) && 6228658a1dc7SSanjeev Bagewadi bcmp(dataset, name, len) == 0 && name[len] == '/') { 6229658a1dc7SSanjeev Bagewadi vfs_list_unlock(); 6230658a1dc7SSanjeev Bagewadi if (write) 6231658a1dc7SSanjeev Bagewadi *write = 0; 6232658a1dc7SSanjeev Bagewadi return (1); 6233658a1dc7SSanjeev Bagewadi } 6234658a1dc7SSanjeev Bagewadi } 6235658a1dc7SSanjeev Bagewadi vfsp = vfsp->vfs_zone_next; 6236658a1dc7SSanjeev Bagewadi } while (vfsp != zone->zone_vfslist); 6237658a1dc7SSanjeev Bagewadi 6238658a1dc7SSanjeev Bagewadi vfs_list_unlock(); 6239fa9e4066Sahrens return (0); 6240fa9e4066Sahrens } 624145916cd2Sjpk 624245916cd2Sjpk /* 624345916cd2Sjpk * zone_find_by_any_path() - 624445916cd2Sjpk * 624545916cd2Sjpk * kernel-private routine similar to zone_find_by_path(), but which 624645916cd2Sjpk * effectively compares against zone paths rather than zonerootpath 624745916cd2Sjpk * (i.e., the last component of zonerootpaths, which should be "root/", 624845916cd2Sjpk * are not compared.) This is done in order to accurately identify all 624945916cd2Sjpk * paths, whether zone-visible or not, including those which are parallel 625045916cd2Sjpk * to /root/, such as /dev/, /home/, etc... 625145916cd2Sjpk * 625245916cd2Sjpk * If the specified path does not fall under any zone path then global 625345916cd2Sjpk * zone is returned. 625445916cd2Sjpk * 625545916cd2Sjpk * The treat_abs parameter indicates whether the path should be treated as 625645916cd2Sjpk * an absolute path although it does not begin with "/". (This supports 625745916cd2Sjpk * nfs mount syntax such as host:any/path.) 625845916cd2Sjpk * 625945916cd2Sjpk * The caller is responsible for zone_rele of the returned zone. 626045916cd2Sjpk */ 626145916cd2Sjpk zone_t * 626245916cd2Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs) 626345916cd2Sjpk { 626445916cd2Sjpk zone_t *zone; 626545916cd2Sjpk int path_offset = 0; 626645916cd2Sjpk 626745916cd2Sjpk if (path == NULL) { 626845916cd2Sjpk zone_hold(global_zone); 626945916cd2Sjpk return (global_zone); 627045916cd2Sjpk } 627145916cd2Sjpk 627245916cd2Sjpk if (*path != '/') { 627345916cd2Sjpk ASSERT(treat_abs); 627445916cd2Sjpk path_offset = 1; 627545916cd2Sjpk } 627645916cd2Sjpk 627745916cd2Sjpk mutex_enter(&zonehash_lock); 627845916cd2Sjpk for (zone = list_head(&zone_active); zone != NULL; 627945916cd2Sjpk zone = list_next(&zone_active, zone)) { 628045916cd2Sjpk char *c; 628145916cd2Sjpk size_t pathlen; 62820f95d722Smp46848 char *rootpath_start; 628345916cd2Sjpk 628445916cd2Sjpk if (zone == global_zone) /* skip global zone */ 628545916cd2Sjpk continue; 628645916cd2Sjpk 628745916cd2Sjpk /* scan backwards to find start of last component */ 628845916cd2Sjpk c = zone->zone_rootpath + zone->zone_rootpathlen - 2; 628945916cd2Sjpk do { 629045916cd2Sjpk c--; 629145916cd2Sjpk } while (*c != '/'); 629245916cd2Sjpk 62930f95d722Smp46848 pathlen = c - zone->zone_rootpath + 1 - path_offset; 62940f95d722Smp46848 rootpath_start = (zone->zone_rootpath + path_offset); 62950f95d722Smp46848 if (strncmp(path, rootpath_start, pathlen) == 0) 629645916cd2Sjpk break; 629745916cd2Sjpk } 629845916cd2Sjpk if (zone == NULL) 629945916cd2Sjpk zone = global_zone; 630045916cd2Sjpk zone_hold(zone); 630145916cd2Sjpk mutex_exit(&zonehash_lock); 630245916cd2Sjpk return (zone); 630345916cd2Sjpk } 6304f4b3ec61Sdh155122 6305f4b3ec61Sdh155122 /* 63062b24ab6bSSebastien Roy * Finds a zone_dl_t with the given linkid in the given zone. Returns the 63072b24ab6bSSebastien Roy * zone_dl_t pointer if found, and NULL otherwise. 6308f4b3ec61Sdh155122 */ 63092b24ab6bSSebastien Roy static zone_dl_t * 63102b24ab6bSSebastien Roy zone_find_dl(zone_t *zone, datalink_id_t linkid) 6311f4b3ec61Sdh155122 { 63122b24ab6bSSebastien Roy zone_dl_t *zdl; 6313f4b3ec61Sdh155122 63142b24ab6bSSebastien Roy ASSERT(mutex_owned(&zone->zone_lock)); 63152b24ab6bSSebastien Roy for (zdl = list_head(&zone->zone_dl_list); zdl != NULL; 63162b24ab6bSSebastien Roy zdl = list_next(&zone->zone_dl_list, zdl)) { 63172b24ab6bSSebastien Roy if (zdl->zdl_id == linkid) 6318f4b3ec61Sdh155122 break; 6319f4b3ec61Sdh155122 } 63202b24ab6bSSebastien Roy return (zdl); 6321f4b3ec61Sdh155122 } 63222b24ab6bSSebastien Roy 63232b24ab6bSSebastien Roy static boolean_t 63242b24ab6bSSebastien Roy zone_dl_exists(zone_t *zone, datalink_id_t linkid) 63252b24ab6bSSebastien Roy { 63262b24ab6bSSebastien Roy boolean_t exists; 63272b24ab6bSSebastien Roy 63282b24ab6bSSebastien Roy mutex_enter(&zone->zone_lock); 63292b24ab6bSSebastien Roy exists = (zone_find_dl(zone, linkid) != NULL); 6330f4b3ec61Sdh155122 mutex_exit(&zone->zone_lock); 63312b24ab6bSSebastien Roy return (exists); 6332f4b3ec61Sdh155122 } 6333f4b3ec61Sdh155122 6334f4b3ec61Sdh155122 /* 63352b24ab6bSSebastien Roy * Add an data link name for the zone. 6336f4b3ec61Sdh155122 */ 6337f4b3ec61Sdh155122 static int 63382b24ab6bSSebastien Roy zone_add_datalink(zoneid_t zoneid, datalink_id_t linkid) 6339f4b3ec61Sdh155122 { 63402b24ab6bSSebastien Roy zone_dl_t *zdl; 6341f4b3ec61Sdh155122 zone_t *zone; 6342f4b3ec61Sdh155122 zone_t *thiszone; 6343f4b3ec61Sdh155122 63442b24ab6bSSebastien Roy if ((thiszone = zone_find_by_id(zoneid)) == NULL) 6345f4b3ec61Sdh155122 return (set_errno(ENXIO)); 6346f4b3ec61Sdh155122 63472b24ab6bSSebastien Roy /* Verify that the datalink ID doesn't already belong to a zone. */ 6348f4b3ec61Sdh155122 mutex_enter(&zonehash_lock); 6349f4b3ec61Sdh155122 for (zone = list_head(&zone_active); zone != NULL; 6350f4b3ec61Sdh155122 zone = list_next(&zone_active, zone)) { 63512b24ab6bSSebastien Roy if (zone_dl_exists(zone, linkid)) { 6352f4b3ec61Sdh155122 mutex_exit(&zonehash_lock); 6353f4b3ec61Sdh155122 zone_rele(thiszone); 63542b24ab6bSSebastien Roy return (set_errno((zone == thiszone) ? EEXIST : EPERM)); 6355f4b3ec61Sdh155122 } 6356f4b3ec61Sdh155122 } 63572b24ab6bSSebastien Roy 63582b24ab6bSSebastien Roy zdl = kmem_zalloc(sizeof (*zdl), KM_SLEEP); 63592b24ab6bSSebastien Roy zdl->zdl_id = linkid; 6360*550b6e40SSowmini Varadhan zdl->zdl_net = NULL; 6361f4b3ec61Sdh155122 mutex_enter(&thiszone->zone_lock); 63622b24ab6bSSebastien Roy list_insert_head(&thiszone->zone_dl_list, zdl); 6363f4b3ec61Sdh155122 mutex_exit(&thiszone->zone_lock); 6364f4b3ec61Sdh155122 mutex_exit(&zonehash_lock); 6365f4b3ec61Sdh155122 zone_rele(thiszone); 6366f4b3ec61Sdh155122 return (0); 6367f4b3ec61Sdh155122 } 6368f4b3ec61Sdh155122 6369f4b3ec61Sdh155122 static int 63702b24ab6bSSebastien Roy zone_remove_datalink(zoneid_t zoneid, datalink_id_t linkid) 6371f4b3ec61Sdh155122 { 63722b24ab6bSSebastien Roy zone_dl_t *zdl; 6373f4b3ec61Sdh155122 zone_t *zone; 6374f4b3ec61Sdh155122 int err = 0; 6375f4b3ec61Sdh155122 63762b24ab6bSSebastien Roy if ((zone = zone_find_by_id(zoneid)) == NULL) 63772b24ab6bSSebastien Roy return (set_errno(EINVAL)); 6378f4b3ec61Sdh155122 63792b24ab6bSSebastien Roy mutex_enter(&zone->zone_lock); 63802b24ab6bSSebastien Roy if ((zdl = zone_find_dl(zone, linkid)) == NULL) { 63812b24ab6bSSebastien Roy err = ENXIO; 63822b24ab6bSSebastien Roy } else { 63832b24ab6bSSebastien Roy list_remove(&zone->zone_dl_list, zdl); 6384*550b6e40SSowmini Varadhan if (zdl->zdl_net != NULL) 6385*550b6e40SSowmini Varadhan nvlist_free(zdl->zdl_net); 63862b24ab6bSSebastien Roy kmem_free(zdl, sizeof (zone_dl_t)); 63872b24ab6bSSebastien Roy } 63882b24ab6bSSebastien Roy mutex_exit(&zone->zone_lock); 63892b24ab6bSSebastien Roy zone_rele(zone); 63902b24ab6bSSebastien Roy return (err == 0 ? 0 : set_errno(err)); 63912b24ab6bSSebastien Roy } 6392f4b3ec61Sdh155122 6393f4b3ec61Sdh155122 /* 63942b24ab6bSSebastien Roy * Using the zoneidp as ALL_ZONES, we can lookup which zone has been assigned 63952b24ab6bSSebastien Roy * the linkid. Otherwise we just check if the specified zoneidp has been 63962b24ab6bSSebastien Roy * assigned the supplied linkid. 6397f4b3ec61Sdh155122 */ 63982b24ab6bSSebastien Roy int 63992b24ab6bSSebastien Roy zone_check_datalink(zoneid_t *zoneidp, datalink_id_t linkid) 64002b24ab6bSSebastien Roy { 64012b24ab6bSSebastien Roy zone_t *zone; 64022b24ab6bSSebastien Roy int err = ENXIO; 64032b24ab6bSSebastien Roy 64042b24ab6bSSebastien Roy if (*zoneidp != ALL_ZONES) { 64052b24ab6bSSebastien Roy if ((zone = zone_find_by_id(*zoneidp)) != NULL) { 64062b24ab6bSSebastien Roy if (zone_dl_exists(zone, linkid)) 64072b24ab6bSSebastien Roy err = 0; 64082b24ab6bSSebastien Roy zone_rele(zone); 64092b24ab6bSSebastien Roy } 64102b24ab6bSSebastien Roy return (err); 64112b24ab6bSSebastien Roy } 64122b24ab6bSSebastien Roy 6413f4b3ec61Sdh155122 mutex_enter(&zonehash_lock); 6414f4b3ec61Sdh155122 for (zone = list_head(&zone_active); zone != NULL; 6415f4b3ec61Sdh155122 zone = list_next(&zone_active, zone)) { 64162b24ab6bSSebastien Roy if (zone_dl_exists(zone, linkid)) { 64172b24ab6bSSebastien Roy *zoneidp = zone->zone_id; 64182b24ab6bSSebastien Roy err = 0; 64192b24ab6bSSebastien Roy break; 6420f4b3ec61Sdh155122 } 6421f4b3ec61Sdh155122 } 6422f4b3ec61Sdh155122 mutex_exit(&zonehash_lock); 64232b24ab6bSSebastien Roy return (err); 6424f4b3ec61Sdh155122 } 6425f4b3ec61Sdh155122 6426f4b3ec61Sdh155122 /* 64272b24ab6bSSebastien Roy * Get the list of datalink IDs assigned to a zone. 64282b24ab6bSSebastien Roy * 64292b24ab6bSSebastien Roy * On input, *nump is the number of datalink IDs that can fit in the supplied 64302b24ab6bSSebastien Roy * idarray. Upon return, *nump is either set to the number of datalink IDs 64312b24ab6bSSebastien Roy * that were placed in the array if the array was large enough, or to the 64322b24ab6bSSebastien Roy * number of datalink IDs that the function needs to place in the array if the 64332b24ab6bSSebastien Roy * array is too small. 6434f4b3ec61Sdh155122 */ 6435f4b3ec61Sdh155122 static int 64362b24ab6bSSebastien Roy zone_list_datalink(zoneid_t zoneid, int *nump, datalink_id_t *idarray) 6437f4b3ec61Sdh155122 { 64382b24ab6bSSebastien Roy uint_t num, dlcount; 6439f4b3ec61Sdh155122 zone_t *zone; 64402b24ab6bSSebastien Roy zone_dl_t *zdl; 64412b24ab6bSSebastien Roy datalink_id_t *idptr = idarray; 6442f4b3ec61Sdh155122 6443f4b3ec61Sdh155122 if (copyin(nump, &dlcount, sizeof (dlcount)) != 0) 6444f4b3ec61Sdh155122 return (set_errno(EFAULT)); 64452b24ab6bSSebastien Roy if ((zone = zone_find_by_id(zoneid)) == NULL) 6446f4b3ec61Sdh155122 return (set_errno(ENXIO)); 6447f4b3ec61Sdh155122 6448f4b3ec61Sdh155122 num = 0; 6449f4b3ec61Sdh155122 mutex_enter(&zone->zone_lock); 64502b24ab6bSSebastien Roy for (zdl = list_head(&zone->zone_dl_list); zdl != NULL; 64512b24ab6bSSebastien Roy zdl = list_next(&zone->zone_dl_list, zdl)) { 6452f4b3ec61Sdh155122 /* 64532b24ab6bSSebastien Roy * If the list is bigger than what the caller supplied, just 64542b24ab6bSSebastien Roy * count, don't do copyout. 6455f4b3ec61Sdh155122 */ 6456f4b3ec61Sdh155122 if (++num > dlcount) 6457f4b3ec61Sdh155122 continue; 64582b24ab6bSSebastien Roy if (copyout(&zdl->zdl_id, idptr, sizeof (*idptr)) != 0) { 6459f4b3ec61Sdh155122 mutex_exit(&zone->zone_lock); 6460f4b3ec61Sdh155122 zone_rele(zone); 6461f4b3ec61Sdh155122 return (set_errno(EFAULT)); 6462f4b3ec61Sdh155122 } 64632b24ab6bSSebastien Roy idptr++; 6464f4b3ec61Sdh155122 } 6465f4b3ec61Sdh155122 mutex_exit(&zone->zone_lock); 6466f4b3ec61Sdh155122 zone_rele(zone); 6467f4b3ec61Sdh155122 6468f4b3ec61Sdh155122 /* Increased or decreased, caller should be notified. */ 6469f4b3ec61Sdh155122 if (num != dlcount) { 64702b24ab6bSSebastien Roy if (copyout(&num, nump, sizeof (num)) != 0) 6471f4b3ec61Sdh155122 return (set_errno(EFAULT)); 6472f4b3ec61Sdh155122 } 6473f4b3ec61Sdh155122 return (0); 6474f4b3ec61Sdh155122 } 6475f4b3ec61Sdh155122 6476f4b3ec61Sdh155122 /* 6477f4b3ec61Sdh155122 * Public interface for looking up a zone by zoneid. It's a customized version 6478bd41d0a8Snordmark * for netstack_zone_create(). It can only be called from the zsd create 6479bd41d0a8Snordmark * callbacks, since it doesn't have reference on the zone structure hence if 6480bd41d0a8Snordmark * it is called elsewhere the zone could disappear after the zonehash_lock 6481bd41d0a8Snordmark * is dropped. 6482bd41d0a8Snordmark * 6483bd41d0a8Snordmark * Furthermore it 6484bd41d0a8Snordmark * 1. Doesn't check the status of the zone. 6485bd41d0a8Snordmark * 2. It will be called even before zone_init is called, in that case the 6486f4b3ec61Sdh155122 * address of zone0 is returned directly, and netstack_zone_create() 6487f4b3ec61Sdh155122 * will only assign a value to zone0.zone_netstack, won't break anything. 6488bd41d0a8Snordmark * 3. Returns without the zone being held. 6489f4b3ec61Sdh155122 */ 6490f4b3ec61Sdh155122 zone_t * 6491f4b3ec61Sdh155122 zone_find_by_id_nolock(zoneid_t zoneid) 6492f4b3ec61Sdh155122 { 6493bd41d0a8Snordmark zone_t *zone; 6494f4b3ec61Sdh155122 6495bd41d0a8Snordmark mutex_enter(&zonehash_lock); 6496f4b3ec61Sdh155122 if (zonehashbyid == NULL) 6497bd41d0a8Snordmark zone = &zone0; 6498f4b3ec61Sdh155122 else 6499bd41d0a8Snordmark zone = zone_find_all_by_id(zoneid); 6500bd41d0a8Snordmark mutex_exit(&zonehash_lock); 6501bd41d0a8Snordmark return (zone); 6502f4b3ec61Sdh155122 } 6503d62bc4baSyz147064 6504d62bc4baSyz147064 /* 6505d62bc4baSyz147064 * Walk the datalinks for a given zone 6506d62bc4baSyz147064 */ 6507d62bc4baSyz147064 int 65082b24ab6bSSebastien Roy zone_datalink_walk(zoneid_t zoneid, int (*cb)(datalink_id_t, void *), 65092b24ab6bSSebastien Roy void *data) 6510d62bc4baSyz147064 { 6511d62bc4baSyz147064 zone_t *zone; 65122b24ab6bSSebastien Roy zone_dl_t *zdl; 65132b24ab6bSSebastien Roy datalink_id_t *idarray; 65142b24ab6bSSebastien Roy uint_t idcount = 0; 65152b24ab6bSSebastien Roy int i, ret = 0; 6516d62bc4baSyz147064 6517d62bc4baSyz147064 if ((zone = zone_find_by_id(zoneid)) == NULL) 6518d62bc4baSyz147064 return (ENOENT); 6519d62bc4baSyz147064 65202b24ab6bSSebastien Roy /* 65212b24ab6bSSebastien Roy * We first build an array of linkid's so that we can walk these and 65222b24ab6bSSebastien Roy * execute the callback with the zone_lock dropped. 65232b24ab6bSSebastien Roy */ 6524d62bc4baSyz147064 mutex_enter(&zone->zone_lock); 65252b24ab6bSSebastien Roy for (zdl = list_head(&zone->zone_dl_list); zdl != NULL; 65262b24ab6bSSebastien Roy zdl = list_next(&zone->zone_dl_list, zdl)) { 65272b24ab6bSSebastien Roy idcount++; 6528d62bc4baSyz147064 } 65292b24ab6bSSebastien Roy 65302b24ab6bSSebastien Roy if (idcount == 0) { 6531d62bc4baSyz147064 mutex_exit(&zone->zone_lock); 6532d62bc4baSyz147064 zone_rele(zone); 65332b24ab6bSSebastien Roy return (0); 65342b24ab6bSSebastien Roy } 65352b24ab6bSSebastien Roy 65362b24ab6bSSebastien Roy idarray = kmem_alloc(sizeof (datalink_id_t) * idcount, KM_NOSLEEP); 65372b24ab6bSSebastien Roy if (idarray == NULL) { 65382b24ab6bSSebastien Roy mutex_exit(&zone->zone_lock); 65392b24ab6bSSebastien Roy zone_rele(zone); 65402b24ab6bSSebastien Roy return (ENOMEM); 65412b24ab6bSSebastien Roy } 65422b24ab6bSSebastien Roy 65432b24ab6bSSebastien Roy for (i = 0, zdl = list_head(&zone->zone_dl_list); zdl != NULL; 65442b24ab6bSSebastien Roy i++, zdl = list_next(&zone->zone_dl_list, zdl)) { 65452b24ab6bSSebastien Roy idarray[i] = zdl->zdl_id; 65462b24ab6bSSebastien Roy } 65472b24ab6bSSebastien Roy 65482b24ab6bSSebastien Roy mutex_exit(&zone->zone_lock); 65492b24ab6bSSebastien Roy 65502b24ab6bSSebastien Roy for (i = 0; i < idcount && ret == 0; i++) { 65512b24ab6bSSebastien Roy if ((ret = (*cb)(idarray[i], data)) != 0) 65522b24ab6bSSebastien Roy break; 65532b24ab6bSSebastien Roy } 65542b24ab6bSSebastien Roy 65552b24ab6bSSebastien Roy zone_rele(zone); 65562b24ab6bSSebastien Roy kmem_free(idarray, sizeof (datalink_id_t) * idcount); 6557d62bc4baSyz147064 return (ret); 6558d62bc4baSyz147064 } 6559*550b6e40SSowmini Varadhan 6560*550b6e40SSowmini Varadhan static char * 6561*550b6e40SSowmini Varadhan zone_net_type2name(int type) 6562*550b6e40SSowmini Varadhan { 6563*550b6e40SSowmini Varadhan switch (type) { 6564*550b6e40SSowmini Varadhan case ZONE_NETWORK_ADDRESS: 6565*550b6e40SSowmini Varadhan return (ZONE_NET_ADDRNAME); 6566*550b6e40SSowmini Varadhan case ZONE_NETWORK_DEFROUTER: 6567*550b6e40SSowmini Varadhan return (ZONE_NET_RTRNAME); 6568*550b6e40SSowmini Varadhan default: 6569*550b6e40SSowmini Varadhan return (NULL); 6570*550b6e40SSowmini Varadhan } 6571*550b6e40SSowmini Varadhan } 6572*550b6e40SSowmini Varadhan 6573*550b6e40SSowmini Varadhan static int 6574*550b6e40SSowmini Varadhan zone_set_network(zoneid_t zoneid, zone_net_data_t *znbuf) 6575*550b6e40SSowmini Varadhan { 6576*550b6e40SSowmini Varadhan zone_t *zone; 6577*550b6e40SSowmini Varadhan zone_dl_t *zdl; 6578*550b6e40SSowmini Varadhan nvlist_t *nvl; 6579*550b6e40SSowmini Varadhan int err = 0; 6580*550b6e40SSowmini Varadhan uint8_t *new = NULL; 6581*550b6e40SSowmini Varadhan char *nvname; 6582*550b6e40SSowmini Varadhan int bufsize; 6583*550b6e40SSowmini Varadhan datalink_id_t linkid = znbuf->zn_linkid; 6584*550b6e40SSowmini Varadhan 6585*550b6e40SSowmini Varadhan if (secpolicy_zone_config(CRED()) != 0) 6586*550b6e40SSowmini Varadhan return (set_errno(EPERM)); 6587*550b6e40SSowmini Varadhan 6588*550b6e40SSowmini Varadhan if (zoneid == GLOBAL_ZONEID) 6589*550b6e40SSowmini Varadhan return (set_errno(EINVAL)); 6590*550b6e40SSowmini Varadhan 6591*550b6e40SSowmini Varadhan nvname = zone_net_type2name(znbuf->zn_type); 6592*550b6e40SSowmini Varadhan bufsize = znbuf->zn_len; 6593*550b6e40SSowmini Varadhan new = znbuf->zn_val; 6594*550b6e40SSowmini Varadhan if (nvname == NULL) 6595*550b6e40SSowmini Varadhan return (set_errno(EINVAL)); 6596*550b6e40SSowmini Varadhan 6597*550b6e40SSowmini Varadhan if ((zone = zone_find_by_id(zoneid)) == NULL) { 6598*550b6e40SSowmini Varadhan return (set_errno(EINVAL)); 6599*550b6e40SSowmini Varadhan } 6600*550b6e40SSowmini Varadhan 6601*550b6e40SSowmini Varadhan mutex_enter(&zone->zone_lock); 6602*550b6e40SSowmini Varadhan if ((zdl = zone_find_dl(zone, linkid)) == NULL) { 6603*550b6e40SSowmini Varadhan err = ENXIO; 6604*550b6e40SSowmini Varadhan goto done; 6605*550b6e40SSowmini Varadhan } 6606*550b6e40SSowmini Varadhan if ((nvl = zdl->zdl_net) == NULL) { 6607*550b6e40SSowmini Varadhan if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) { 6608*550b6e40SSowmini Varadhan err = ENOMEM; 6609*550b6e40SSowmini Varadhan goto done; 6610*550b6e40SSowmini Varadhan } else { 6611*550b6e40SSowmini Varadhan zdl->zdl_net = nvl; 6612*550b6e40SSowmini Varadhan } 6613*550b6e40SSowmini Varadhan } 6614*550b6e40SSowmini Varadhan if (nvlist_exists(nvl, nvname)) { 6615*550b6e40SSowmini Varadhan err = EINVAL; 6616*550b6e40SSowmini Varadhan goto done; 6617*550b6e40SSowmini Varadhan } 6618*550b6e40SSowmini Varadhan err = nvlist_add_uint8_array(nvl, nvname, new, bufsize); 6619*550b6e40SSowmini Varadhan ASSERT(err == 0); 6620*550b6e40SSowmini Varadhan done: 6621*550b6e40SSowmini Varadhan mutex_exit(&zone->zone_lock); 6622*550b6e40SSowmini Varadhan zone_rele(zone); 6623*550b6e40SSowmini Varadhan if (err != 0) 6624*550b6e40SSowmini Varadhan return (set_errno(err)); 6625*550b6e40SSowmini Varadhan else 6626*550b6e40SSowmini Varadhan return (0); 6627*550b6e40SSowmini Varadhan } 6628*550b6e40SSowmini Varadhan 6629*550b6e40SSowmini Varadhan static int 6630*550b6e40SSowmini Varadhan zone_get_network(zoneid_t zoneid, zone_net_data_t *znbuf) 6631*550b6e40SSowmini Varadhan { 6632*550b6e40SSowmini Varadhan zone_t *zone; 6633*550b6e40SSowmini Varadhan zone_dl_t *zdl; 6634*550b6e40SSowmini Varadhan nvlist_t *nvl; 6635*550b6e40SSowmini Varadhan uint8_t *ptr; 6636*550b6e40SSowmini Varadhan uint_t psize; 6637*550b6e40SSowmini Varadhan int err = 0; 6638*550b6e40SSowmini Varadhan char *nvname; 6639*550b6e40SSowmini Varadhan int bufsize; 6640*550b6e40SSowmini Varadhan void *buf; 6641*550b6e40SSowmini Varadhan datalink_id_t linkid = znbuf->zn_linkid; 6642*550b6e40SSowmini Varadhan 6643*550b6e40SSowmini Varadhan if (zoneid == GLOBAL_ZONEID) 6644*550b6e40SSowmini Varadhan return (set_errno(EINVAL)); 6645*550b6e40SSowmini Varadhan 6646*550b6e40SSowmini Varadhan nvname = zone_net_type2name(znbuf->zn_type); 6647*550b6e40SSowmini Varadhan bufsize = znbuf->zn_len; 6648*550b6e40SSowmini Varadhan buf = znbuf->zn_val; 6649*550b6e40SSowmini Varadhan 6650*550b6e40SSowmini Varadhan if (nvname == NULL) 6651*550b6e40SSowmini Varadhan return (set_errno(EINVAL)); 6652*550b6e40SSowmini Varadhan if ((zone = zone_find_by_id(zoneid)) == NULL) 6653*550b6e40SSowmini Varadhan return (set_errno(EINVAL)); 6654*550b6e40SSowmini Varadhan 6655*550b6e40SSowmini Varadhan mutex_enter(&zone->zone_lock); 6656*550b6e40SSowmini Varadhan if ((zdl = zone_find_dl(zone, linkid)) == NULL) { 6657*550b6e40SSowmini Varadhan err = ENXIO; 6658*550b6e40SSowmini Varadhan goto done; 6659*550b6e40SSowmini Varadhan } 6660*550b6e40SSowmini Varadhan if ((nvl = zdl->zdl_net) == NULL || !nvlist_exists(nvl, nvname)) { 6661*550b6e40SSowmini Varadhan err = ENOENT; 6662*550b6e40SSowmini Varadhan goto done; 6663*550b6e40SSowmini Varadhan } 6664*550b6e40SSowmini Varadhan err = nvlist_lookup_uint8_array(nvl, nvname, &ptr, &psize); 6665*550b6e40SSowmini Varadhan ASSERT(err == 0); 6666*550b6e40SSowmini Varadhan 6667*550b6e40SSowmini Varadhan if (psize > bufsize) { 6668*550b6e40SSowmini Varadhan err = ENOBUFS; 6669*550b6e40SSowmini Varadhan goto done; 6670*550b6e40SSowmini Varadhan } 6671*550b6e40SSowmini Varadhan znbuf->zn_len = psize; 6672*550b6e40SSowmini Varadhan bcopy(ptr, buf, psize); 6673*550b6e40SSowmini Varadhan done: 6674*550b6e40SSowmini Varadhan mutex_exit(&zone->zone_lock); 6675*550b6e40SSowmini Varadhan zone_rele(zone); 6676*550b6e40SSowmini Varadhan if (err != 0) 6677*550b6e40SSowmini Varadhan return (set_errno(err)); 6678*550b6e40SSowmini Varadhan else 6679*550b6e40SSowmini Varadhan return (0); 6680*550b6e40SSowmini Varadhan } 6681