10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51676Sjpk * Common Development and Distribution License (the "License"). 61676Sjpk * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21390Sraf 220Sstevel@tonic-gate /* 231409Sdp * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * Zones 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * A zone is a named collection of processes, namespace constraints, 330Sstevel@tonic-gate * and other system resources which comprise a secure and manageable 340Sstevel@tonic-gate * application containment facility. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Zones (represented by the reference counted zone_t) are tracked in 370Sstevel@tonic-gate * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs 380Sstevel@tonic-gate * (zoneid_t) are used to track zone association. Zone IDs are 390Sstevel@tonic-gate * dynamically generated when the zone is created; if a persistent 400Sstevel@tonic-gate * identifier is needed (core files, accounting logs, audit trail, 410Sstevel@tonic-gate * etc.), the zone name should be used. 420Sstevel@tonic-gate * 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * Global Zone: 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * The global zone (zoneid 0) is automatically associated with all 470Sstevel@tonic-gate * system resources that have not been bound to a user-created zone. 480Sstevel@tonic-gate * This means that even systems where zones are not in active use 490Sstevel@tonic-gate * have a global zone, and all processes, mounts, etc. are 500Sstevel@tonic-gate * associated with that zone. The global zone is generally 510Sstevel@tonic-gate * unconstrained in terms of privileges and access, though the usual 520Sstevel@tonic-gate * credential and privilege based restrictions apply. 530Sstevel@tonic-gate * 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * Zone States: 560Sstevel@tonic-gate * 570Sstevel@tonic-gate * The states in which a zone may be in and the transitions are as 580Sstevel@tonic-gate * follows: 590Sstevel@tonic-gate * 600Sstevel@tonic-gate * ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially 610Sstevel@tonic-gate * initialized zone is added to the list of active zones on the system but 620Sstevel@tonic-gate * isn't accessible. 630Sstevel@tonic-gate * 640Sstevel@tonic-gate * ZONE_IS_READY: zsched (the kernel dummy process for a zone) is 650Sstevel@tonic-gate * ready. The zone is made visible after the ZSD constructor callbacks are 660Sstevel@tonic-gate * executed. A zone remains in this state until it transitions into 670Sstevel@tonic-gate * the ZONE_IS_BOOTING state as a result of a call to zone_boot(). 680Sstevel@tonic-gate * 690Sstevel@tonic-gate * ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start 700Sstevel@tonic-gate * init. Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN 710Sstevel@tonic-gate * state. 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * ZONE_IS_RUNNING: The zone is open for business: zsched has 740Sstevel@tonic-gate * successfully started init. A zone remains in this state until 750Sstevel@tonic-gate * zone_shutdown() is called. 760Sstevel@tonic-gate * 770Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is 780Sstevel@tonic-gate * killing all processes running in the zone. The zone remains 790Sstevel@tonic-gate * in this state until there are no more user processes running in the zone. 800Sstevel@tonic-gate * zone_create(), zone_enter(), and zone_destroy() on this zone will fail. 810Sstevel@tonic-gate * Since zone_shutdown() is restartable, it may be called successfully 820Sstevel@tonic-gate * multiple times for the same zone_t. Setting of the zone's state to 830Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check 840Sstevel@tonic-gate * the zone's status without worrying about it being a moving target. 850Sstevel@tonic-gate * 860Sstevel@tonic-gate * ZONE_IS_EMPTY: zone_shutdown() has been called, and there 870Sstevel@tonic-gate * are no more user processes in the zone. The zone remains in this 880Sstevel@tonic-gate * state until there are no more kernel threads associated with the 890Sstevel@tonic-gate * zone. zone_create(), zone_enter(), and zone_destroy() on this zone will 900Sstevel@tonic-gate * fail. 910Sstevel@tonic-gate * 920Sstevel@tonic-gate * ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone 930Sstevel@tonic-gate * have exited. zone_shutdown() returns. Henceforth it is not possible to 940Sstevel@tonic-gate * join the zone or create kernel threads therein. 950Sstevel@tonic-gate * 960Sstevel@tonic-gate * ZONE_IS_DYING: zone_destroy() has been called on the zone; zone 970Sstevel@tonic-gate * remains in this state until zsched exits. Calls to zone_find_by_*() 980Sstevel@tonic-gate * return NULL from now on. 990Sstevel@tonic-gate * 1000Sstevel@tonic-gate * ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0). There are no 1010Sstevel@tonic-gate * processes or threads doing work on behalf of the zone. The zone is 1020Sstevel@tonic-gate * removed from the list of active zones. zone_destroy() returns, and 1030Sstevel@tonic-gate * the zone can be recreated. 1040Sstevel@tonic-gate * 1050Sstevel@tonic-gate * ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor 1060Sstevel@tonic-gate * callbacks are executed, and all memory associated with the zone is 1070Sstevel@tonic-gate * freed. 1080Sstevel@tonic-gate * 1090Sstevel@tonic-gate * Threads can wait for the zone to enter a requested state by using 1100Sstevel@tonic-gate * zone_status_wait() or zone_status_timedwait() with the desired 1110Sstevel@tonic-gate * state passed in as an argument. Zone state transitions are 1120Sstevel@tonic-gate * uni-directional; it is not possible to move back to an earlier state. 1130Sstevel@tonic-gate * 1140Sstevel@tonic-gate * 1150Sstevel@tonic-gate * Zone-Specific Data: 1160Sstevel@tonic-gate * 1170Sstevel@tonic-gate * Subsystems needing to maintain zone-specific data can store that 1180Sstevel@tonic-gate * data using the ZSD mechanism. This provides a zone-specific data 1190Sstevel@tonic-gate * store, similar to thread-specific data (see pthread_getspecific(3C) 1200Sstevel@tonic-gate * or the TSD code in uts/common/disp/thread.c. Also, ZSD can be used 1210Sstevel@tonic-gate * to register callbacks to be invoked when a zone is created, shut 1220Sstevel@tonic-gate * down, or destroyed. This can be used to initialize zone-specific 1230Sstevel@tonic-gate * data for new zones and to clean up when zones go away. 1240Sstevel@tonic-gate * 1250Sstevel@tonic-gate * 1260Sstevel@tonic-gate * Data Structures: 1270Sstevel@tonic-gate * 1280Sstevel@tonic-gate * The per-zone structure (zone_t) is reference counted, and freed 1290Sstevel@tonic-gate * when all references are released. zone_hold and zone_rele can be 1300Sstevel@tonic-gate * used to adjust the reference count. In addition, reference counts 1310Sstevel@tonic-gate * associated with the cred_t structure are tracked separately using 1320Sstevel@tonic-gate * zone_cred_hold and zone_cred_rele. 1330Sstevel@tonic-gate * 1340Sstevel@tonic-gate * Pointers to active zone_t's are stored in two hash tables; one 1350Sstevel@tonic-gate * for searching by id, the other for searching by name. Lookups 1360Sstevel@tonic-gate * can be performed on either basis, using zone_find_by_id and 1370Sstevel@tonic-gate * zone_find_by_name. Both return zone_t pointers with the zone 1380Sstevel@tonic-gate * held, so zone_rele should be called when the pointer is no longer 1390Sstevel@tonic-gate * needed. Zones can also be searched by path; zone_find_by_path 1400Sstevel@tonic-gate * returns the zone with which a path name is associated (global 1410Sstevel@tonic-gate * zone if the path is not within some other zone's file system 1420Sstevel@tonic-gate * hierarchy). This currently requires iterating through each zone, 1430Sstevel@tonic-gate * so it is slower than an id or name search via a hash table. 1440Sstevel@tonic-gate * 1450Sstevel@tonic-gate * 1460Sstevel@tonic-gate * Locking: 1470Sstevel@tonic-gate * 1480Sstevel@tonic-gate * zonehash_lock: This is a top-level global lock used to protect the 1490Sstevel@tonic-gate * zone hash tables and lists. Zones cannot be created or destroyed 1500Sstevel@tonic-gate * while this lock is held. 1510Sstevel@tonic-gate * zone_status_lock: This is a global lock protecting zone state. 1520Sstevel@tonic-gate * Zones cannot change state while this lock is held. It also 1530Sstevel@tonic-gate * protects the list of kernel threads associated with a zone. 1540Sstevel@tonic-gate * zone_lock: This is a per-zone lock used to protect several fields of 1550Sstevel@tonic-gate * the zone_t (see <sys/zone.h> for details). In addition, holding 1560Sstevel@tonic-gate * this lock means that the zone cannot go away. 1570Sstevel@tonic-gate * zsd_key_lock: This is a global lock protecting the key state for ZSD. 1580Sstevel@tonic-gate * zone_deathrow_lock: This is a global lock protecting the "deathrow" 1590Sstevel@tonic-gate * list (a list of zones in the ZONE_IS_DEAD state). 1600Sstevel@tonic-gate * 1610Sstevel@tonic-gate * Ordering requirements: 1620Sstevel@tonic-gate * pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock --> 1630Sstevel@tonic-gate * zone_lock --> zsd_key_lock --> pidlock --> p_lock 1640Sstevel@tonic-gate * 1650Sstevel@tonic-gate * Blocking memory allocations are permitted while holding any of the 1660Sstevel@tonic-gate * zone locks. 1670Sstevel@tonic-gate * 1680Sstevel@tonic-gate * 1690Sstevel@tonic-gate * System Call Interface: 1700Sstevel@tonic-gate * 1710Sstevel@tonic-gate * The zone subsystem can be managed and queried from user level with 1720Sstevel@tonic-gate * the following system calls (all subcodes of the primary "zone" 1730Sstevel@tonic-gate * system call): 1740Sstevel@tonic-gate * - zone_create: creates a zone with selected attributes (name, 175789Sahrens * root path, privileges, resource controls, ZFS datasets) 1760Sstevel@tonic-gate * - zone_enter: allows the current process to enter a zone 1770Sstevel@tonic-gate * - zone_getattr: reports attributes of a zone 178*2267Sdp * - zone_setattr: set attributes of a zone 179*2267Sdp * - zone_boot: set 'init' running for the zone 1800Sstevel@tonic-gate * - zone_list: lists all zones active in the system 1810Sstevel@tonic-gate * - zone_lookup: looks up zone id based on name 1820Sstevel@tonic-gate * - zone_shutdown: initiates shutdown process (see states above) 1830Sstevel@tonic-gate * - zone_destroy: completes shutdown process (see states above) 1840Sstevel@tonic-gate * 1850Sstevel@tonic-gate */ 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate #include <sys/priv_impl.h> 1880Sstevel@tonic-gate #include <sys/cred.h> 1890Sstevel@tonic-gate #include <c2/audit.h> 1900Sstevel@tonic-gate #include <sys/debug.h> 1910Sstevel@tonic-gate #include <sys/file.h> 1920Sstevel@tonic-gate #include <sys/kmem.h> 1930Sstevel@tonic-gate #include <sys/mutex.h> 1941676Sjpk #include <sys/note.h> 1950Sstevel@tonic-gate #include <sys/pathname.h> 1960Sstevel@tonic-gate #include <sys/proc.h> 1970Sstevel@tonic-gate #include <sys/project.h> 1981166Sdstaff #include <sys/sysevent.h> 1990Sstevel@tonic-gate #include <sys/task.h> 2000Sstevel@tonic-gate #include <sys/systm.h> 2010Sstevel@tonic-gate #include <sys/types.h> 2020Sstevel@tonic-gate #include <sys/utsname.h> 2030Sstevel@tonic-gate #include <sys/vnode.h> 2040Sstevel@tonic-gate #include <sys/vfs.h> 2050Sstevel@tonic-gate #include <sys/systeminfo.h> 2060Sstevel@tonic-gate #include <sys/policy.h> 2070Sstevel@tonic-gate #include <sys/cred_impl.h> 2080Sstevel@tonic-gate #include <sys/contract_impl.h> 2090Sstevel@tonic-gate #include <sys/contract/process_impl.h> 2100Sstevel@tonic-gate #include <sys/class.h> 2110Sstevel@tonic-gate #include <sys/pool.h> 2120Sstevel@tonic-gate #include <sys/pool_pset.h> 2130Sstevel@tonic-gate #include <sys/pset.h> 2140Sstevel@tonic-gate #include <sys/sysmacros.h> 2150Sstevel@tonic-gate #include <sys/callb.h> 2160Sstevel@tonic-gate #include <sys/vmparam.h> 2170Sstevel@tonic-gate #include <sys/corectl.h> 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate #include <sys/door.h> 2200Sstevel@tonic-gate #include <sys/cpuvar.h> 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate #include <sys/uadmin.h> 2230Sstevel@tonic-gate #include <sys/session.h> 2240Sstevel@tonic-gate #include <sys/cmn_err.h> 2250Sstevel@tonic-gate #include <sys/modhash.h> 226*2267Sdp #include <sys/sunddi.h> 2270Sstevel@tonic-gate #include <sys/nvpair.h> 2280Sstevel@tonic-gate #include <sys/rctl.h> 2290Sstevel@tonic-gate #include <sys/fss.h> 2300Sstevel@tonic-gate #include <sys/zone.h> 2311676Sjpk #include <sys/tsol/label.h> 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate /* 2340Sstevel@tonic-gate * cv used to signal that all references to the zone have been released. This 2350Sstevel@tonic-gate * needs to be global since there may be multiple waiters, and the first to 2360Sstevel@tonic-gate * wake up will free the zone_t, hence we cannot use zone->zone_cv. 2370Sstevel@tonic-gate */ 2380Sstevel@tonic-gate static kcondvar_t zone_destroy_cv; 2390Sstevel@tonic-gate /* 2400Sstevel@tonic-gate * Lock used to serialize access to zone_cv. This could have been per-zone, 2410Sstevel@tonic-gate * but then we'd need another lock for zone_destroy_cv, and why bother? 2420Sstevel@tonic-gate */ 2430Sstevel@tonic-gate static kmutex_t zone_status_lock; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * ZSD-related global variables. 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate static kmutex_t zsd_key_lock; /* protects the following two */ 2490Sstevel@tonic-gate /* 2500Sstevel@tonic-gate * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate static zone_key_t zsd_keyval = 0; 2530Sstevel@tonic-gate /* 2540Sstevel@tonic-gate * Global list of registered keys. We use this when a new zone is created. 2550Sstevel@tonic-gate */ 2560Sstevel@tonic-gate static list_t zsd_registered_keys; 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate int zone_hash_size = 256; 2591676Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel; 2600Sstevel@tonic-gate static kmutex_t zonehash_lock; 2610Sstevel@tonic-gate static uint_t zonecount; 2620Sstevel@tonic-gate static id_space_t *zoneid_space; 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate /* 2650Sstevel@tonic-gate * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 2660Sstevel@tonic-gate * kernel proper runs, and which manages all other zones. 2670Sstevel@tonic-gate * 2680Sstevel@tonic-gate * Although not declared as static, the variable "zone0" should not be used 2690Sstevel@tonic-gate * except for by code that needs to reference the global zone early on in boot, 2700Sstevel@tonic-gate * before it is fully initialized. All other consumers should use 2710Sstevel@tonic-gate * 'global_zone'. 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate zone_t zone0; 2740Sstevel@tonic-gate zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * List of active zones, protected by zonehash_lock. 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate static list_t zone_active; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate /* 2820Sstevel@tonic-gate * List of destroyed zones that still have outstanding cred references. 2830Sstevel@tonic-gate * Used for debugging. Uses a separate lock to avoid lock ordering 2840Sstevel@tonic-gate * problems in zone_free. 2850Sstevel@tonic-gate */ 2860Sstevel@tonic-gate static list_t zone_deathrow; 2870Sstevel@tonic-gate static kmutex_t zone_deathrow_lock; 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */ 2900Sstevel@tonic-gate uint_t maxzones = 8192; 2910Sstevel@tonic-gate 2921166Sdstaff /* Event channel to sent zone state change notifications */ 2931166Sdstaff evchan_t *zone_event_chan; 2941166Sdstaff 2951166Sdstaff /* 2961166Sdstaff * This table holds the mapping from kernel zone states to 2971166Sdstaff * states visible in the state notification API. 2981166Sdstaff * The idea is that we only expose "obvious" states and 2991166Sdstaff * do not expose states which are just implementation details. 3001166Sdstaff */ 3011166Sdstaff const char *zone_status_table[] = { 3021166Sdstaff ZONE_EVENT_UNINITIALIZED, /* uninitialized */ 3031166Sdstaff ZONE_EVENT_READY, /* ready */ 3041166Sdstaff ZONE_EVENT_READY, /* booting */ 3051166Sdstaff ZONE_EVENT_RUNNING, /* running */ 3061166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* shutting_down */ 3071166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* empty */ 3081166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* down */ 3091166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* dying */ 3101166Sdstaff ZONE_EVENT_UNINITIALIZED, /* dead */ 3111166Sdstaff }; 3121166Sdstaff 3130Sstevel@tonic-gate /* 3140Sstevel@tonic-gate * This isn't static so lint doesn't complain. 3150Sstevel@tonic-gate */ 3160Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares; 3170Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps; 3180Sstevel@tonic-gate /* 3190Sstevel@tonic-gate * Synchronization primitives used to synchronize between mounts and zone 3200Sstevel@tonic-gate * creation/destruction. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate static int mounts_in_progress; 3230Sstevel@tonic-gate static kcondvar_t mount_cv; 3240Sstevel@tonic-gate static kmutex_t mount_lock; 3250Sstevel@tonic-gate 326*2267Sdp const char * const zone_default_initname = "/sbin/init"; 3271676Sjpk static char * const zone_prefix = "/zone/"; 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid); 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate /* 332813Sdp * Bump this number when you alter the zone syscall interfaces; this is 333813Sdp * because we need to have support for previous API versions in libc 334813Sdp * to support patching; libc calls into the kernel to determine this number. 335813Sdp * 336813Sdp * Version 1 of the API is the version originally shipped with Solaris 10 337813Sdp * Version 2 alters the zone_create system call in order to support more 338813Sdp * arguments by moving the args into a structure; and to do better 339813Sdp * error reporting when zone_create() fails. 340813Sdp * Version 3 alters the zone_create system call in order to support the 341813Sdp * import of ZFS datasets to zones. 3421676Sjpk * Version 4 alters the zone_create system call in order to support 3431676Sjpk * Trusted Extensions. 344*2267Sdp * Version 5 alters the zone_boot system call, and converts its old 345*2267Sdp * bootargs parameter to be set by the zone_setattr API instead. 346813Sdp */ 347*2267Sdp static const int ZONE_SYSCALL_API_VERSION = 5; 348813Sdp 349813Sdp /* 3500Sstevel@tonic-gate * Certain filesystems (such as NFS and autofs) need to know which zone 3510Sstevel@tonic-gate * the mount is being placed in. Because of this, we need to be able to 3520Sstevel@tonic-gate * ensure that a zone isn't in the process of being created such that 3530Sstevel@tonic-gate * nfs_mount() thinks it is in the global zone, while by the time it 3540Sstevel@tonic-gate * gets added the list of mounted zones, it ends up on zoneA's mount 3550Sstevel@tonic-gate * list. 3560Sstevel@tonic-gate * 3570Sstevel@tonic-gate * The following functions: block_mounts()/resume_mounts() and 3580Sstevel@tonic-gate * mount_in_progress()/mount_completed() are used by zones and the VFS 3590Sstevel@tonic-gate * layer (respectively) to synchronize zone creation and new mounts. 3600Sstevel@tonic-gate * 3610Sstevel@tonic-gate * The semantics are like a reader-reader lock such that there may 3620Sstevel@tonic-gate * either be multiple mounts (or zone creations, if that weren't 3630Sstevel@tonic-gate * serialized by zonehash_lock) in progress at the same time, but not 3640Sstevel@tonic-gate * both. 3650Sstevel@tonic-gate * 3660Sstevel@tonic-gate * We use cv's so the user can ctrl-C out of the operation if it's 3670Sstevel@tonic-gate * taking too long. 3680Sstevel@tonic-gate * 3690Sstevel@tonic-gate * The semantics are such that there is unfair bias towards the 3700Sstevel@tonic-gate * "current" operation. This means that zone creations may starve if 3710Sstevel@tonic-gate * there is a rapid succession of new mounts coming in to the system, or 3720Sstevel@tonic-gate * there is a remote possibility that zones will be created at such a 3730Sstevel@tonic-gate * rate that new mounts will not be able to proceed. 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate /* 3760Sstevel@tonic-gate * Prevent new mounts from progressing to the point of calling 3770Sstevel@tonic-gate * VFS_MOUNT(). If there are already mounts in this "region", wait for 3780Sstevel@tonic-gate * them to complete. 3790Sstevel@tonic-gate */ 3800Sstevel@tonic-gate static int 3810Sstevel@tonic-gate block_mounts(void) 3820Sstevel@tonic-gate { 3830Sstevel@tonic-gate int retval = 0; 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate /* 3860Sstevel@tonic-gate * Since it may block for a long time, block_mounts() shouldn't be 3870Sstevel@tonic-gate * called with zonehash_lock held. 3880Sstevel@tonic-gate */ 3890Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 3900Sstevel@tonic-gate mutex_enter(&mount_lock); 3910Sstevel@tonic-gate while (mounts_in_progress > 0) { 3920Sstevel@tonic-gate if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 3930Sstevel@tonic-gate goto signaled; 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate /* 3960Sstevel@tonic-gate * A negative value of mounts_in_progress indicates that mounts 3970Sstevel@tonic-gate * have been blocked by (-mounts_in_progress) different callers. 3980Sstevel@tonic-gate */ 3990Sstevel@tonic-gate mounts_in_progress--; 4000Sstevel@tonic-gate retval = 1; 4010Sstevel@tonic-gate signaled: 4020Sstevel@tonic-gate mutex_exit(&mount_lock); 4030Sstevel@tonic-gate return (retval); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate /* 4070Sstevel@tonic-gate * The VFS layer may progress with new mounts as far as we're concerned. 4080Sstevel@tonic-gate * Allow them to progress if we were the last obstacle. 4090Sstevel@tonic-gate */ 4100Sstevel@tonic-gate static void 4110Sstevel@tonic-gate resume_mounts(void) 4120Sstevel@tonic-gate { 4130Sstevel@tonic-gate mutex_enter(&mount_lock); 4140Sstevel@tonic-gate if (++mounts_in_progress == 0) 4150Sstevel@tonic-gate cv_broadcast(&mount_cv); 4160Sstevel@tonic-gate mutex_exit(&mount_lock); 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * The VFS layer is busy with a mount; zones should wait until all 4210Sstevel@tonic-gate * mounts are completed to progress. 4220Sstevel@tonic-gate */ 4230Sstevel@tonic-gate void 4240Sstevel@tonic-gate mount_in_progress(void) 4250Sstevel@tonic-gate { 4260Sstevel@tonic-gate mutex_enter(&mount_lock); 4270Sstevel@tonic-gate while (mounts_in_progress < 0) 4280Sstevel@tonic-gate cv_wait(&mount_cv, &mount_lock); 4290Sstevel@tonic-gate mounts_in_progress++; 4300Sstevel@tonic-gate mutex_exit(&mount_lock); 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * VFS is done with one mount; wake up any waiting block_mounts() 4350Sstevel@tonic-gate * callers if this is the last mount. 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate void 4380Sstevel@tonic-gate mount_completed(void) 4390Sstevel@tonic-gate { 4400Sstevel@tonic-gate mutex_enter(&mount_lock); 4410Sstevel@tonic-gate if (--mounts_in_progress == 0) 4420Sstevel@tonic-gate cv_broadcast(&mount_cv); 4430Sstevel@tonic-gate mutex_exit(&mount_lock); 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * ZSD routines. 4480Sstevel@tonic-gate * 4490Sstevel@tonic-gate * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 4500Sstevel@tonic-gate * defined by the pthread_key_create() and related interfaces. 4510Sstevel@tonic-gate * 4520Sstevel@tonic-gate * Kernel subsystems may register one or more data items and/or 4530Sstevel@tonic-gate * callbacks to be executed when a zone is created, shutdown, or 4540Sstevel@tonic-gate * destroyed. 4550Sstevel@tonic-gate * 4560Sstevel@tonic-gate * Unlike the thread counterpart, destructor callbacks will be executed 4570Sstevel@tonic-gate * even if the data pointer is NULL and/or there are no constructor 4580Sstevel@tonic-gate * callbacks, so it is the responsibility of such callbacks to check for 4590Sstevel@tonic-gate * NULL data values if necessary. 4600Sstevel@tonic-gate * 4610Sstevel@tonic-gate * The locking strategy and overall picture is as follows: 4620Sstevel@tonic-gate * 4630Sstevel@tonic-gate * When someone calls zone_key_create(), a template ZSD entry is added to the 4640Sstevel@tonic-gate * global list "zsd_registered_keys", protected by zsd_key_lock. The 4650Sstevel@tonic-gate * constructor callback is called immediately on all existing zones, and a 4660Sstevel@tonic-gate * copy of the ZSD entry added to the per-zone zone_zsd list (protected by 4670Sstevel@tonic-gate * zone_lock). As this operation requires the list of zones, the list of 4680Sstevel@tonic-gate * registered keys, and the per-zone list of ZSD entries to remain constant 4690Sstevel@tonic-gate * throughout the entire operation, it must grab zonehash_lock, zone_lock for 4700Sstevel@tonic-gate * all existing zones, and zsd_key_lock, in that order. Similar locking is 4710Sstevel@tonic-gate * needed when zone_key_delete() is called. It is thus sufficient to hold 4720Sstevel@tonic-gate * zsd_key_lock *or* zone_lock to prevent additions to or removals from the 4730Sstevel@tonic-gate * per-zone zone_zsd list. 4740Sstevel@tonic-gate * 4750Sstevel@tonic-gate * Note that this implementation does not make a copy of the ZSD entry if a 4760Sstevel@tonic-gate * constructor callback is not provided. A zone_getspecific() on such an 4770Sstevel@tonic-gate * uninitialized ZSD entry will return NULL. 4780Sstevel@tonic-gate * 4790Sstevel@tonic-gate * When new zones are created constructor callbacks for all registered ZSD 4800Sstevel@tonic-gate * entries will be called. 4810Sstevel@tonic-gate * 4820Sstevel@tonic-gate * The framework does not provide any locking around zone_getspecific() and 4830Sstevel@tonic-gate * zone_setspecific() apart from that needed for internal consistency, so 4840Sstevel@tonic-gate * callers interested in atomic "test-and-set" semantics will need to provide 4850Sstevel@tonic-gate * their own locking. 4860Sstevel@tonic-gate */ 4870Sstevel@tonic-gate void 4880Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 4890Sstevel@tonic-gate void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 4900Sstevel@tonic-gate { 4910Sstevel@tonic-gate struct zsd_entry *zsdp; 4920Sstevel@tonic-gate struct zsd_entry *t; 4930Sstevel@tonic-gate struct zone *zone; 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP); 4960Sstevel@tonic-gate zsdp->zsd_data = NULL; 4970Sstevel@tonic-gate zsdp->zsd_create = create; 4980Sstevel@tonic-gate zsdp->zsd_shutdown = shutdown; 4990Sstevel@tonic-gate zsdp->zsd_destroy = destroy; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* stop the world */ 5020Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5030Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5040Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5070Sstevel@tonic-gate *keyp = zsdp->zsd_key = ++zsd_keyval; 5080Sstevel@tonic-gate ASSERT(zsd_keyval != 0); 5090Sstevel@tonic-gate list_insert_tail(&zsd_registered_keys, zsdp); 5100Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate if (create != NULL) { 5130Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5140Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 5150Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 5160Sstevel@tonic-gate t->zsd_key = *keyp; 5170Sstevel@tonic-gate t->zsd_data = (*create)(zone->zone_id); 5180Sstevel@tonic-gate t->zsd_create = create; 5190Sstevel@tonic-gate t->zsd_shutdown = shutdown; 5200Sstevel@tonic-gate t->zsd_destroy = destroy; 5210Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5250Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5260Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 5270Sstevel@tonic-gate mutex_exit(&zonehash_lock); 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate /* 5310Sstevel@tonic-gate * Helper function to find the zsd_entry associated with the key in the 5320Sstevel@tonic-gate * given list. 5330Sstevel@tonic-gate */ 5340Sstevel@tonic-gate static struct zsd_entry * 5350Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key) 5360Sstevel@tonic-gate { 5370Sstevel@tonic-gate struct zsd_entry *zsd; 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 5400Sstevel@tonic-gate if (zsd->zsd_key == key) { 5410Sstevel@tonic-gate /* 5420Sstevel@tonic-gate * Move to head of list to keep list in MRU order. 5430Sstevel@tonic-gate */ 5440Sstevel@tonic-gate if (zsd != list_head(l)) { 5450Sstevel@tonic-gate list_remove(l, zsd); 5460Sstevel@tonic-gate list_insert_head(l, zsd); 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate return (zsd); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate return (NULL); 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate /* 5550Sstevel@tonic-gate * Function called when a module is being unloaded, or otherwise wishes 5560Sstevel@tonic-gate * to unregister its ZSD key and callbacks. 5570Sstevel@tonic-gate */ 5580Sstevel@tonic-gate int 5590Sstevel@tonic-gate zone_key_delete(zone_key_t key) 5600Sstevel@tonic-gate { 5610Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 5620Sstevel@tonic-gate zone_t *zone; 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* Zone create/delete waits for us */ 5650Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5660Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5670Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5700Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 5710Sstevel@tonic-gate if (zsdp == NULL) 5720Sstevel@tonic-gate goto notfound; 5730Sstevel@tonic-gate list_remove(&zsd_registered_keys, zsdp); 5740Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5770Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 5780Sstevel@tonic-gate struct zsd_entry *del; 5790Sstevel@tonic-gate void *data; 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate if (!(zone->zone_flags & ZF_DESTROYED)) { 5820Sstevel@tonic-gate del = zsd_find(&zone->zone_zsd, key); 5830Sstevel@tonic-gate if (del != NULL) { 5840Sstevel@tonic-gate data = del->zsd_data; 5850Sstevel@tonic-gate ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 5860Sstevel@tonic-gate ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 5870Sstevel@tonic-gate list_remove(&zone->zone_zsd, del); 5880Sstevel@tonic-gate kmem_free(del, sizeof (*del)); 5890Sstevel@tonic-gate } else { 5900Sstevel@tonic-gate data = NULL; 5910Sstevel@tonic-gate } 5920Sstevel@tonic-gate if (zsdp->zsd_shutdown) 5930Sstevel@tonic-gate zsdp->zsd_shutdown(zone->zone_id, data); 5940Sstevel@tonic-gate if (zsdp->zsd_destroy) 5950Sstevel@tonic-gate zsdp->zsd_destroy(zone->zone_id, data); 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate mutex_exit(&zonehash_lock); 6000Sstevel@tonic-gate kmem_free(zsdp, sizeof (*zsdp)); 6010Sstevel@tonic-gate return (0); 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate notfound: 6040Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6050Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 6060Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 6070Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6080Sstevel@tonic-gate mutex_exit(&zonehash_lock); 6090Sstevel@tonic-gate return (-1); 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate /* 6130Sstevel@tonic-gate * ZSD counterpart of pthread_setspecific(). 6140Sstevel@tonic-gate */ 6150Sstevel@tonic-gate int 6160Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 6170Sstevel@tonic-gate { 6180Sstevel@tonic-gate struct zsd_entry *t; 6190Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6220Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6230Sstevel@tonic-gate if (t != NULL) { 6240Sstevel@tonic-gate /* 6250Sstevel@tonic-gate * Replace old value with new 6260Sstevel@tonic-gate */ 6270Sstevel@tonic-gate t->zsd_data = (void *)data; 6280Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6290Sstevel@tonic-gate return (0); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate /* 6320Sstevel@tonic-gate * If there was no previous value, go through the list of registered 6330Sstevel@tonic-gate * keys. 6340Sstevel@tonic-gate * 6350Sstevel@tonic-gate * We avoid grabbing zsd_key_lock until we are sure we need it; this is 6360Sstevel@tonic-gate * necessary for shutdown callbacks to be able to execute without fear 6370Sstevel@tonic-gate * of deadlock. 6380Sstevel@tonic-gate */ 6390Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 6400Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 6410Sstevel@tonic-gate if (zsdp == NULL) { /* Key was not registered */ 6420Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6430Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6440Sstevel@tonic-gate return (-1); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* 6480Sstevel@tonic-gate * Add a zsd_entry to this zone, using the template we just retrieved 6490Sstevel@tonic-gate * to initialize the constructor and destructor(s). 6500Sstevel@tonic-gate */ 6510Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 6520Sstevel@tonic-gate t->zsd_key = key; 6530Sstevel@tonic-gate t->zsd_data = (void *)data; 6540Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 6550Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 6560Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 6570Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 6580Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6590Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6600Sstevel@tonic-gate return (0); 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /* 6640Sstevel@tonic-gate * ZSD counterpart of pthread_getspecific(). 6650Sstevel@tonic-gate */ 6660Sstevel@tonic-gate void * 6670Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone) 6680Sstevel@tonic-gate { 6690Sstevel@tonic-gate struct zsd_entry *t; 6700Sstevel@tonic-gate void *data; 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6730Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6740Sstevel@tonic-gate data = (t == NULL ? NULL : t->zsd_data); 6750Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6760Sstevel@tonic-gate return (data); 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate /* 6800Sstevel@tonic-gate * Function used to initialize a zone's list of ZSD callbacks and data 6810Sstevel@tonic-gate * when the zone is being created. The callbacks are initialized from 6820Sstevel@tonic-gate * the template list (zsd_registered_keys), and the constructor 6830Sstevel@tonic-gate * callback executed (if one exists). 6840Sstevel@tonic-gate * 6850Sstevel@tonic-gate * This is called before the zone is made publicly available, hence no 6860Sstevel@tonic-gate * need to grab zone_lock. 6870Sstevel@tonic-gate * 6880Sstevel@tonic-gate * Although we grab and release zsd_key_lock, new entries cannot be 6890Sstevel@tonic-gate * added to or removed from the zsd_registered_keys list until we 6900Sstevel@tonic-gate * release zonehash_lock, so there isn't a window for a 6910Sstevel@tonic-gate * zone_key_create() to come in after we've dropped zsd_key_lock but 6920Sstevel@tonic-gate * before the zone is added to the zone list, such that the constructor 6930Sstevel@tonic-gate * callbacks aren't executed for the new zone. 6940Sstevel@tonic-gate */ 6950Sstevel@tonic-gate static void 6960Sstevel@tonic-gate zone_zsd_configure(zone_t *zone) 6970Sstevel@tonic-gate { 6980Sstevel@tonic-gate struct zsd_entry *zsdp; 6990Sstevel@tonic-gate struct zsd_entry *t; 7000Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 7030Sstevel@tonic-gate ASSERT(list_head(&zone->zone_zsd) == NULL); 7040Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7050Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7060Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7070Sstevel@tonic-gate if (zsdp->zsd_create != NULL) { 7080Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 7090Sstevel@tonic-gate t->zsd_key = zsdp->zsd_key; 7100Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 7110Sstevel@tonic-gate t->zsd_data = (*t->zsd_create)(zoneid); 7120Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 7130Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 7140Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate /* 7230Sstevel@tonic-gate * Helper function to execute shutdown or destructor callbacks. 7240Sstevel@tonic-gate */ 7250Sstevel@tonic-gate static void 7260Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 7270Sstevel@tonic-gate { 7280Sstevel@tonic-gate struct zsd_entry *zsdp; 7290Sstevel@tonic-gate struct zsd_entry *t; 7300Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 7330Sstevel@tonic-gate ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 7340Sstevel@tonic-gate ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 7370Sstevel@tonic-gate if (ct == ZSD_DESTROY) { 7380Sstevel@tonic-gate if (zone->zone_flags & ZF_DESTROYED) { 7390Sstevel@tonic-gate /* 7400Sstevel@tonic-gate * Make sure destructors are only called once. 7410Sstevel@tonic-gate */ 7420Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7430Sstevel@tonic-gate return; 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate zone->zone_flags |= ZF_DESTROYED; 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate /* 7500Sstevel@tonic-gate * Both zsd_key_lock and zone_lock need to be held in order to add or 7510Sstevel@tonic-gate * remove a ZSD key, (either globally as part of 7520Sstevel@tonic-gate * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is 7530Sstevel@tonic-gate * possible through zone_setspecific()), so it's sufficient to hold 7540Sstevel@tonic-gate * zsd_key_lock here. 7550Sstevel@tonic-gate * 7560Sstevel@tonic-gate * This is a good thing, since we don't want to recursively try to grab 7570Sstevel@tonic-gate * zone_lock if a callback attempts to do something like a crfree() or 7580Sstevel@tonic-gate * zone_rele(). 7590Sstevel@tonic-gate */ 7600Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7610Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7620Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7630Sstevel@tonic-gate zone_key_t key = zsdp->zsd_key; 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate /* Skip if no callbacks registered */ 7660Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL) 7670Sstevel@tonic-gate continue; 7680Sstevel@tonic-gate if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL) 7690Sstevel@tonic-gate continue; 7700Sstevel@tonic-gate /* 7710Sstevel@tonic-gate * Call the callback with the zone-specific data if we can find 7720Sstevel@tonic-gate * any, otherwise with NULL. 7730Sstevel@tonic-gate */ 7740Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 7750Sstevel@tonic-gate if (t != NULL) { 7760Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 7770Sstevel@tonic-gate t->zsd_shutdown(zoneid, t->zsd_data); 7780Sstevel@tonic-gate } else { 7790Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 7800Sstevel@tonic-gate t->zsd_destroy(zoneid, t->zsd_data); 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate } else { 7830Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 7840Sstevel@tonic-gate zsdp->zsd_shutdown(zoneid, NULL); 7850Sstevel@tonic-gate } else { 7860Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 7870Sstevel@tonic-gate zsdp->zsd_destroy(zoneid, NULL); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 7920Sstevel@tonic-gate } 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate /* 7950Sstevel@tonic-gate * Called when the zone is going away; free ZSD-related memory, and 7960Sstevel@tonic-gate * destroy the zone_zsd list. 7970Sstevel@tonic-gate */ 7980Sstevel@tonic-gate static void 7990Sstevel@tonic-gate zone_free_zsd(zone_t *zone) 8000Sstevel@tonic-gate { 8010Sstevel@tonic-gate struct zsd_entry *t, *next; 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate /* 8040Sstevel@tonic-gate * Free all the zsd_entry's we had on this zone. 8050Sstevel@tonic-gate */ 8060Sstevel@tonic-gate for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 8070Sstevel@tonic-gate next = list_next(&zone->zone_zsd, t); 8080Sstevel@tonic-gate list_remove(&zone->zone_zsd, t); 8090Sstevel@tonic-gate kmem_free(t, sizeof (*t)); 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate list_destroy(&zone->zone_zsd); 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate /* 815789Sahrens * Frees memory associated with the zone dataset list. 816789Sahrens */ 817789Sahrens static void 818789Sahrens zone_free_datasets(zone_t *zone) 819789Sahrens { 820789Sahrens zone_dataset_t *t, *next; 821789Sahrens 822789Sahrens for (t = list_head(&zone->zone_datasets); t != NULL; t = next) { 823789Sahrens next = list_next(&zone->zone_datasets, t); 824789Sahrens list_remove(&zone->zone_datasets, t); 825789Sahrens kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1); 826789Sahrens kmem_free(t, sizeof (*t)); 827789Sahrens } 828789Sahrens list_destroy(&zone->zone_datasets); 829789Sahrens } 830789Sahrens 831789Sahrens /* 8320Sstevel@tonic-gate * zone.cpu-shares resource control support. 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate /*ARGSUSED*/ 8350Sstevel@tonic-gate static rctl_qty_t 8360Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 8370Sstevel@tonic-gate { 8380Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8390Sstevel@tonic-gate return (p->p_zone->zone_shares); 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate /*ARGSUSED*/ 8430Sstevel@tonic-gate static int 8440Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 8450Sstevel@tonic-gate rctl_qty_t nv) 8460Sstevel@tonic-gate { 8470Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8480Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 8490Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 8500Sstevel@tonic-gate return (0); 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate e->rcep_p.zone->zone_shares = nv; 8530Sstevel@tonic-gate return (0); 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = { 8570Sstevel@tonic-gate rcop_no_action, 8580Sstevel@tonic-gate zone_cpu_shares_usage, 8590Sstevel@tonic-gate zone_cpu_shares_set, 8600Sstevel@tonic-gate rcop_no_test 8610Sstevel@tonic-gate }; 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate /*ARGSUSED*/ 8640Sstevel@tonic-gate static rctl_qty_t 8650Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p) 8660Sstevel@tonic-gate { 8670Sstevel@tonic-gate rctl_qty_t nlwps; 8680Sstevel@tonic-gate zone_t *zone = p->p_zone; 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 8730Sstevel@tonic-gate nlwps = zone->zone_nlwps; 8740Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate return (nlwps); 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate /*ARGSUSED*/ 8800Sstevel@tonic-gate static int 8810Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 8820Sstevel@tonic-gate rctl_qty_t incr, uint_t flags) 8830Sstevel@tonic-gate { 8840Sstevel@tonic-gate rctl_qty_t nlwps; 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8870Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 8880Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 8890Sstevel@tonic-gate return (0); 8900Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 8910Sstevel@tonic-gate nlwps = e->rcep_p.zone->zone_nlwps; 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate if (nlwps + incr > rcntl->rcv_value) 8940Sstevel@tonic-gate return (1); 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate return (0); 8970Sstevel@tonic-gate } 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate /*ARGSUSED*/ 9000Sstevel@tonic-gate static int 9010Sstevel@tonic-gate zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) { 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 9040Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 9050Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 9060Sstevel@tonic-gate return (0); 9070Sstevel@tonic-gate e->rcep_p.zone->zone_nlwps_ctl = nv; 9080Sstevel@tonic-gate return (0); 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = { 9120Sstevel@tonic-gate rcop_no_action, 9130Sstevel@tonic-gate zone_lwps_usage, 9140Sstevel@tonic-gate zone_lwps_set, 9150Sstevel@tonic-gate zone_lwps_test, 9160Sstevel@tonic-gate }; 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate /* 9190Sstevel@tonic-gate * Helper function to brand the zone with a unique ID. 9200Sstevel@tonic-gate */ 9210Sstevel@tonic-gate static void 9220Sstevel@tonic-gate zone_uniqid(zone_t *zone) 9230Sstevel@tonic-gate { 9240Sstevel@tonic-gate static uint64_t uniqid = 0; 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 9270Sstevel@tonic-gate zone->zone_uniqid = uniqid++; 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate /* 9310Sstevel@tonic-gate * Returns a held pointer to the "kcred" for the specified zone. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate struct cred * 9340Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid) 9350Sstevel@tonic-gate { 9360Sstevel@tonic-gate zone_t *zone; 9370Sstevel@tonic-gate cred_t *cr; 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) 9400Sstevel@tonic-gate return (NULL); 9410Sstevel@tonic-gate cr = zone->zone_kcred; 9420Sstevel@tonic-gate crhold(cr); 9430Sstevel@tonic-gate zone_rele(zone); 9440Sstevel@tonic-gate return (cr); 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate /* 9480Sstevel@tonic-gate * Called very early on in boot to initialize the ZSD list so that 9490Sstevel@tonic-gate * zone_key_create() can be called before zone_init(). It also initializes 9500Sstevel@tonic-gate * portions of zone0 which may be used before zone_init() is called. The 9510Sstevel@tonic-gate * variable "global_zone" will be set when zone0 is fully initialized by 9520Sstevel@tonic-gate * zone_init(). 9530Sstevel@tonic-gate */ 9540Sstevel@tonic-gate void 9550Sstevel@tonic-gate zone_zsd_init(void) 9560Sstevel@tonic-gate { 9570Sstevel@tonic-gate mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 9580Sstevel@tonic-gate mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 9590Sstevel@tonic-gate list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 9600Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 9610Sstevel@tonic-gate list_create(&zone_active, sizeof (zone_t), 9620Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 9630Sstevel@tonic-gate list_create(&zone_deathrow, sizeof (zone_t), 9640Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 9670Sstevel@tonic-gate mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 9680Sstevel@tonic-gate zone0.zone_shares = 1; 9690Sstevel@tonic-gate zone0.zone_nlwps_ctl = INT_MAX; 9700Sstevel@tonic-gate zone0.zone_name = GLOBAL_ZONENAME; 9710Sstevel@tonic-gate zone0.zone_nodename = utsname.nodename; 9720Sstevel@tonic-gate zone0.zone_domain = srpc_domain; 9730Sstevel@tonic-gate zone0.zone_ref = 1; 9740Sstevel@tonic-gate zone0.zone_id = GLOBAL_ZONEID; 9750Sstevel@tonic-gate zone0.zone_status = ZONE_IS_RUNNING; 9760Sstevel@tonic-gate zone0.zone_rootpath = "/"; 9770Sstevel@tonic-gate zone0.zone_rootpathlen = 2; 9780Sstevel@tonic-gate zone0.zone_psetid = ZONE_PS_INVAL; 9790Sstevel@tonic-gate zone0.zone_ncpus = 0; 9800Sstevel@tonic-gate zone0.zone_ncpus_online = 0; 9810Sstevel@tonic-gate zone0.zone_proc_initpid = 1; 982*2267Sdp zone0.zone_initname = initname; 9830Sstevel@tonic-gate list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 9840Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 9850Sstevel@tonic-gate list_insert_head(&zone_active, &zone0); 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate /* 9880Sstevel@tonic-gate * The root filesystem is not mounted yet, so zone_rootvp cannot be set 9890Sstevel@tonic-gate * to anything meaningful. It is assigned to be 'rootdir' in 9900Sstevel@tonic-gate * vfs_mountroot(). 9910Sstevel@tonic-gate */ 9920Sstevel@tonic-gate zone0.zone_rootvp = NULL; 9930Sstevel@tonic-gate zone0.zone_vfslist = NULL; 994*2267Sdp zone0.zone_bootargs = initargs; 9950Sstevel@tonic-gate zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 9960Sstevel@tonic-gate /* 9970Sstevel@tonic-gate * The global zone has all privileges 9980Sstevel@tonic-gate */ 9990Sstevel@tonic-gate priv_fillset(zone0.zone_privset); 10000Sstevel@tonic-gate /* 10010Sstevel@tonic-gate * Add p0 to the global zone 10020Sstevel@tonic-gate */ 10030Sstevel@tonic-gate zone0.zone_zsched = &p0; 10040Sstevel@tonic-gate p0.p_zone = &zone0; 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate /* 10081676Sjpk * Compute a hash value based on the contents of the label and the DOI. The 10091676Sjpk * hash algorithm is somewhat arbitrary, but is based on the observation that 10101676Sjpk * humans will likely pick labels that differ by amounts that work out to be 10111676Sjpk * multiples of the number of hash chains, and thus stirring in some primes 10121676Sjpk * should help. 10131676Sjpk */ 10141676Sjpk static uint_t 10151676Sjpk hash_bylabel(void *hdata, mod_hash_key_t key) 10161676Sjpk { 10171676Sjpk const ts_label_t *lab = (ts_label_t *)key; 10181676Sjpk const uint32_t *up, *ue; 10191676Sjpk uint_t hash; 10201676Sjpk int i; 10211676Sjpk 10221676Sjpk _NOTE(ARGUNUSED(hdata)); 10231676Sjpk 10241676Sjpk hash = lab->tsl_doi + (lab->tsl_doi << 1); 10251676Sjpk /* we depend on alignment of label, but not representation */ 10261676Sjpk up = (const uint32_t *)&lab->tsl_label; 10271676Sjpk ue = up + sizeof (lab->tsl_label) / sizeof (*up); 10281676Sjpk i = 1; 10291676Sjpk while (up < ue) { 10301676Sjpk /* using 2^n + 1, 1 <= n <= 16 as source of many primes */ 10311676Sjpk hash += *up + (*up << ((i % 16) + 1)); 10321676Sjpk up++; 10331676Sjpk i++; 10341676Sjpk } 10351676Sjpk return (hash); 10361676Sjpk } 10371676Sjpk 10381676Sjpk /* 10391676Sjpk * All that mod_hash cares about here is zero (equal) versus non-zero (not 10401676Sjpk * equal). This may need to be changed if less than / greater than is ever 10411676Sjpk * needed. 10421676Sjpk */ 10431676Sjpk static int 10441676Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 10451676Sjpk { 10461676Sjpk ts_label_t *lab1 = (ts_label_t *)key1; 10471676Sjpk ts_label_t *lab2 = (ts_label_t *)key2; 10481676Sjpk 10491676Sjpk return (label_equal(lab1, lab2) ? 0 : 1); 10501676Sjpk } 10511676Sjpk 10521676Sjpk /* 10530Sstevel@tonic-gate * Called by main() to initialize the zones framework. 10540Sstevel@tonic-gate */ 10550Sstevel@tonic-gate void 10560Sstevel@tonic-gate zone_init(void) 10570Sstevel@tonic-gate { 10580Sstevel@tonic-gate rctl_dict_entry_t *rde; 10590Sstevel@tonic-gate rctl_val_t *dval; 10600Sstevel@tonic-gate rctl_set_t *set; 10610Sstevel@tonic-gate rctl_alloc_gp_t *gp; 10620Sstevel@tonic-gate rctl_entity_p_t e; 10631166Sdstaff int res; 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate ASSERT(curproc == &p0); 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate /* 10680Sstevel@tonic-gate * Create ID space for zone IDs. ID 0 is reserved for the 10690Sstevel@tonic-gate * global zone. 10700Sstevel@tonic-gate */ 10710Sstevel@tonic-gate zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 10720Sstevel@tonic-gate 10730Sstevel@tonic-gate /* 10740Sstevel@tonic-gate * Initialize generic zone resource controls, if any. 10750Sstevel@tonic-gate */ 10760Sstevel@tonic-gate rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 10770Sstevel@tonic-gate RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 10781996Sml93401 RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER, 10791996Sml93401 FSS_MAXSHARES, FSS_MAXSHARES, 10800Sstevel@tonic-gate &zone_cpu_shares_ops); 10810Sstevel@tonic-gate 10820Sstevel@tonic-gate rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 10830Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 10840Sstevel@tonic-gate INT_MAX, INT_MAX, &zone_lwps_ops); 10850Sstevel@tonic-gate /* 10860Sstevel@tonic-gate * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 10870Sstevel@tonic-gate * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 10880Sstevel@tonic-gate */ 10890Sstevel@tonic-gate dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 10900Sstevel@tonic-gate bzero(dval, sizeof (rctl_val_t)); 10910Sstevel@tonic-gate dval->rcv_value = 1; 10920Sstevel@tonic-gate dval->rcv_privilege = RCPRIV_PRIVILEGED; 10930Sstevel@tonic-gate dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 10940Sstevel@tonic-gate dval->rcv_action_recip_pid = -1; 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate rde = rctl_dict_lookup("zone.cpu-shares"); 10970Sstevel@tonic-gate (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate /* 11000Sstevel@tonic-gate * Initialize the ``global zone''. 11010Sstevel@tonic-gate */ 11020Sstevel@tonic-gate set = rctl_set_create(); 11030Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 11040Sstevel@tonic-gate mutex_enter(&p0.p_lock); 11050Sstevel@tonic-gate e.rcep_p.zone = &zone0; 11060Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 11070Sstevel@tonic-gate zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 11080Sstevel@tonic-gate gp); 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate zone0.zone_nlwps = p0.p_lwpcnt; 11110Sstevel@tonic-gate zone0.zone_ntasks = 1; 11120Sstevel@tonic-gate mutex_exit(&p0.p_lock); 11130Sstevel@tonic-gate rctl_prealloc_destroy(gp); 11140Sstevel@tonic-gate /* 11150Sstevel@tonic-gate * pool_default hasn't been initialized yet, so we let pool_init() take 11160Sstevel@tonic-gate * care of making the global zone is in the default pool. 11170Sstevel@tonic-gate */ 11181676Sjpk 11191676Sjpk /* 11201676Sjpk * Initialize zone label. 11211676Sjpk * mlp are initialized when tnzonecfg is loaded. 11221676Sjpk */ 11231676Sjpk zone0.zone_slabel = l_admin_low; 11241676Sjpk rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 11251676Sjpk label_hold(l_admin_low); 11261676Sjpk 11270Sstevel@tonic-gate mutex_enter(&zonehash_lock); 11280Sstevel@tonic-gate zone_uniqid(&zone0); 11290Sstevel@tonic-gate ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 11301676Sjpk 11310Sstevel@tonic-gate zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 11320Sstevel@tonic-gate mod_hash_null_valdtor); 11330Sstevel@tonic-gate zonehashbyname = mod_hash_create_strhash("zone_by_name", 11340Sstevel@tonic-gate zone_hash_size, mod_hash_null_valdtor); 11351676Sjpk /* 11361676Sjpk * maintain zonehashbylabel only for labeled systems 11371676Sjpk */ 11381676Sjpk if (is_system_labeled()) 11391676Sjpk zonehashbylabel = mod_hash_create_extended("zone_by_label", 11401676Sjpk zone_hash_size, mod_hash_null_keydtor, 11411676Sjpk mod_hash_null_valdtor, hash_bylabel, NULL, 11421676Sjpk hash_labelkey_cmp, KM_SLEEP); 11430Sstevel@tonic-gate zonecount = 1; 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 11460Sstevel@tonic-gate (mod_hash_val_t)&zone0); 11470Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 11480Sstevel@tonic-gate (mod_hash_val_t)&zone0); 11491769Scarlsonj if (is_system_labeled()) { 11501769Scarlsonj zone0.zone_flags |= ZF_HASHED_LABEL; 11511676Sjpk (void) mod_hash_insert(zonehashbylabel, 11521676Sjpk (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0); 11531769Scarlsonj } 11541676Sjpk mutex_exit(&zonehash_lock); 11551676Sjpk 11560Sstevel@tonic-gate /* 11570Sstevel@tonic-gate * We avoid setting zone_kcred until now, since kcred is initialized 11580Sstevel@tonic-gate * sometime after zone_zsd_init() and before zone_init(). 11590Sstevel@tonic-gate */ 11600Sstevel@tonic-gate zone0.zone_kcred = kcred; 11610Sstevel@tonic-gate /* 11620Sstevel@tonic-gate * The global zone is fully initialized (except for zone_rootvp which 11630Sstevel@tonic-gate * will be set when the root filesystem is mounted). 11640Sstevel@tonic-gate */ 11650Sstevel@tonic-gate global_zone = &zone0; 11661166Sdstaff 11671166Sdstaff /* 11681166Sdstaff * Setup an event channel to send zone status change notifications on 11691166Sdstaff */ 11701166Sdstaff res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan, 11711166Sdstaff EVCH_CREAT); 11721166Sdstaff 11731166Sdstaff if (res) 11741166Sdstaff panic("Sysevent_evc_bind failed during zone setup.\n"); 11750Sstevel@tonic-gate } 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate static void 11780Sstevel@tonic-gate zone_free(zone_t *zone) 11790Sstevel@tonic-gate { 11800Sstevel@tonic-gate ASSERT(zone != global_zone); 11810Sstevel@tonic-gate ASSERT(zone->zone_ntasks == 0); 11820Sstevel@tonic-gate ASSERT(zone->zone_nlwps == 0); 11830Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 0); 11840Sstevel@tonic-gate ASSERT(zone->zone_kcred == NULL); 11850Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 11860Sstevel@tonic-gate zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 11870Sstevel@tonic-gate 11880Sstevel@tonic-gate /* remove from deathrow list */ 11890Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_DEAD) { 11900Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 11910Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 11920Sstevel@tonic-gate list_remove(&zone_deathrow, zone); 11930Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 11940Sstevel@tonic-gate } 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate zone_free_zsd(zone); 1197789Sahrens zone_free_datasets(zone); 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate if (zone->zone_rootvp != NULL) 12000Sstevel@tonic-gate VN_RELE(zone->zone_rootvp); 12010Sstevel@tonic-gate if (zone->zone_rootpath) 12020Sstevel@tonic-gate kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 12030Sstevel@tonic-gate if (zone->zone_name != NULL) 12040Sstevel@tonic-gate kmem_free(zone->zone_name, ZONENAME_MAX); 12051676Sjpk if (zone->zone_slabel != NULL) 12061676Sjpk label_rele(zone->zone_slabel); 12070Sstevel@tonic-gate if (zone->zone_nodename != NULL) 12080Sstevel@tonic-gate kmem_free(zone->zone_nodename, _SYS_NMLN); 12090Sstevel@tonic-gate if (zone->zone_domain != NULL) 12100Sstevel@tonic-gate kmem_free(zone->zone_domain, _SYS_NMLN); 12110Sstevel@tonic-gate if (zone->zone_privset != NULL) 12120Sstevel@tonic-gate kmem_free(zone->zone_privset, sizeof (priv_set_t)); 12130Sstevel@tonic-gate if (zone->zone_rctls != NULL) 12140Sstevel@tonic-gate rctl_set_free(zone->zone_rctls); 12150Sstevel@tonic-gate if (zone->zone_bootargs != NULL) 1216*2267Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 1217*2267Sdp if (zone->zone_initname != NULL) 1218*2267Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 12190Sstevel@tonic-gate id_free(zoneid_space, zone->zone_id); 12200Sstevel@tonic-gate mutex_destroy(&zone->zone_lock); 12210Sstevel@tonic-gate cv_destroy(&zone->zone_cv); 12221676Sjpk rw_destroy(&zone->zone_mlps.mlpl_rwlock); 12230Sstevel@tonic-gate kmem_free(zone, sizeof (zone_t)); 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate /* 12270Sstevel@tonic-gate * See block comment at the top of this file for information about zone 12280Sstevel@tonic-gate * status values. 12290Sstevel@tonic-gate */ 12300Sstevel@tonic-gate /* 12310Sstevel@tonic-gate * Convenience function for setting zone status. 12320Sstevel@tonic-gate */ 12330Sstevel@tonic-gate static void 12340Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status) 12350Sstevel@tonic-gate { 12361166Sdstaff 12371166Sdstaff nvlist_t *nvl = NULL; 12380Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zone_status_lock)); 12390Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 12400Sstevel@tonic-gate status >= zone_status_get(zone)); 12411166Sdstaff 12421166Sdstaff if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) || 12431166Sdstaff nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) || 12441166Sdstaff nvlist_add_string(nvl, ZONE_CB_NEWSTATE, 1245*2267Sdp zone_status_table[status]) || 12461166Sdstaff nvlist_add_string(nvl, ZONE_CB_OLDSTATE, 1247*2267Sdp zone_status_table[zone->zone_status]) || 12481166Sdstaff nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) || 12491166Sdstaff nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) || 12501166Sdstaff sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS, 1251*2267Sdp ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) { 12521166Sdstaff #ifdef DEBUG 12531166Sdstaff (void) printf( 12541166Sdstaff "Failed to allocate and send zone state change event.\n"); 12551166Sdstaff #endif 12561166Sdstaff } 12571166Sdstaff nvlist_free(nvl); 12581166Sdstaff 12590Sstevel@tonic-gate zone->zone_status = status; 12601166Sdstaff 12610Sstevel@tonic-gate cv_broadcast(&zone->zone_cv); 12620Sstevel@tonic-gate } 12630Sstevel@tonic-gate 12640Sstevel@tonic-gate /* 12650Sstevel@tonic-gate * Public function to retrieve the zone status. The zone status may 12660Sstevel@tonic-gate * change after it is retrieved. 12670Sstevel@tonic-gate */ 12680Sstevel@tonic-gate zone_status_t 12690Sstevel@tonic-gate zone_status_get(zone_t *zone) 12700Sstevel@tonic-gate { 12710Sstevel@tonic-gate return (zone->zone_status); 12720Sstevel@tonic-gate } 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate static int 12750Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 12760Sstevel@tonic-gate { 1277*2267Sdp char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP); 1278*2267Sdp int err = 0; 1279*2267Sdp 1280*2267Sdp ASSERT(zone != global_zone); 1281*2267Sdp if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0) 1282*2267Sdp goto done; /* EFAULT or ENAMETOOLONG */ 1283*2267Sdp 1284*2267Sdp if (zone->zone_bootargs != NULL) 1285*2267Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 1286*2267Sdp 1287*2267Sdp zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP); 1288*2267Sdp (void) strcpy(zone->zone_bootargs, bootargs); 1289*2267Sdp 1290*2267Sdp done: 1291*2267Sdp kmem_free(bootargs, BOOTARGS_MAX); 1292*2267Sdp return (err); 1293*2267Sdp } 1294*2267Sdp 1295*2267Sdp static int 1296*2267Sdp zone_set_initname(zone_t *zone, const char *zone_initname) 1297*2267Sdp { 1298*2267Sdp char initname[INITNAME_SZ]; 12990Sstevel@tonic-gate size_t len; 1300*2267Sdp int err = 0; 1301*2267Sdp 1302*2267Sdp ASSERT(zone != global_zone); 1303*2267Sdp if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0) 13040Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 1305*2267Sdp 1306*2267Sdp if (zone->zone_initname != NULL) 1307*2267Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 1308*2267Sdp 1309*2267Sdp zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP); 1310*2267Sdp (void) strcpy(zone->zone_initname, initname); 13110Sstevel@tonic-gate return (0); 13120Sstevel@tonic-gate } 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate /* 13150Sstevel@tonic-gate * Block indefinitely waiting for (zone_status >= status) 13160Sstevel@tonic-gate */ 13170Sstevel@tonic-gate void 13180Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status) 13190Sstevel@tonic-gate { 13200Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 13210Sstevel@tonic-gate 13220Sstevel@tonic-gate mutex_enter(&zone_status_lock); 13230Sstevel@tonic-gate while (zone->zone_status < status) { 13240Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 13250Sstevel@tonic-gate } 13260Sstevel@tonic-gate mutex_exit(&zone_status_lock); 13270Sstevel@tonic-gate } 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate /* 13300Sstevel@tonic-gate * Private CPR-safe version of zone_status_wait(). 13310Sstevel@tonic-gate */ 13320Sstevel@tonic-gate static void 13330Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 13340Sstevel@tonic-gate { 13350Sstevel@tonic-gate callb_cpr_t cprinfo; 13360Sstevel@tonic-gate 13370Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 13380Sstevel@tonic-gate 13390Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 13400Sstevel@tonic-gate str); 13410Sstevel@tonic-gate mutex_enter(&zone_status_lock); 13420Sstevel@tonic-gate while (zone->zone_status < status) { 13430Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 13440Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 13450Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 13460Sstevel@tonic-gate } 13470Sstevel@tonic-gate /* 13480Sstevel@tonic-gate * zone_status_lock is implicitly released by the following. 13490Sstevel@tonic-gate */ 13500Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate /* 13540Sstevel@tonic-gate * Block until zone enters requested state or signal is received. Return (0) 13550Sstevel@tonic-gate * if signaled, non-zero otherwise. 13560Sstevel@tonic-gate */ 13570Sstevel@tonic-gate int 13580Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status) 13590Sstevel@tonic-gate { 13600Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate mutex_enter(&zone_status_lock); 13630Sstevel@tonic-gate while (zone->zone_status < status) { 13640Sstevel@tonic-gate if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 13650Sstevel@tonic-gate mutex_exit(&zone_status_lock); 13660Sstevel@tonic-gate return (0); 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate } 13690Sstevel@tonic-gate mutex_exit(&zone_status_lock); 13700Sstevel@tonic-gate return (1); 13710Sstevel@tonic-gate } 13720Sstevel@tonic-gate 13730Sstevel@tonic-gate /* 13740Sstevel@tonic-gate * Block until the zone enters the requested state or the timeout expires, 13750Sstevel@tonic-gate * whichever happens first. Return (-1) if operation timed out, time remaining 13760Sstevel@tonic-gate * otherwise. 13770Sstevel@tonic-gate */ 13780Sstevel@tonic-gate clock_t 13790Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 13800Sstevel@tonic-gate { 13810Sstevel@tonic-gate clock_t timeleft = 0; 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate mutex_enter(&zone_status_lock); 13860Sstevel@tonic-gate while (zone->zone_status < status && timeleft != -1) { 13870Sstevel@tonic-gate timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 13880Sstevel@tonic-gate } 13890Sstevel@tonic-gate mutex_exit(&zone_status_lock); 13900Sstevel@tonic-gate return (timeleft); 13910Sstevel@tonic-gate } 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate /* 13940Sstevel@tonic-gate * Block until the zone enters the requested state, the current process is 13950Sstevel@tonic-gate * signaled, or the timeout expires, whichever happens first. Return (-1) if 13960Sstevel@tonic-gate * operation timed out, 0 if signaled, time remaining otherwise. 13970Sstevel@tonic-gate */ 13980Sstevel@tonic-gate clock_t 13990Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 14000Sstevel@tonic-gate { 14010Sstevel@tonic-gate clock_t timeleft = tim - lbolt; 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate mutex_enter(&zone_status_lock); 14060Sstevel@tonic-gate while (zone->zone_status < status) { 14070Sstevel@tonic-gate timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 14080Sstevel@tonic-gate tim); 14090Sstevel@tonic-gate if (timeleft <= 0) 14100Sstevel@tonic-gate break; 14110Sstevel@tonic-gate } 14120Sstevel@tonic-gate mutex_exit(&zone_status_lock); 14130Sstevel@tonic-gate return (timeleft); 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate 14160Sstevel@tonic-gate /* 14170Sstevel@tonic-gate * Zones have two reference counts: one for references from credential 14180Sstevel@tonic-gate * structures (zone_cred_ref), and one (zone_ref) for everything else. 14190Sstevel@tonic-gate * This is so we can allow a zone to be rebooted while there are still 14200Sstevel@tonic-gate * outstanding cred references, since certain drivers cache dblks (which 14210Sstevel@tonic-gate * implicitly results in cached creds). We wait for zone_ref to drop to 14220Sstevel@tonic-gate * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 14230Sstevel@tonic-gate * later freed when the zone_cred_ref drops to 0, though nothing other 14240Sstevel@tonic-gate * than the zone id and privilege set should be accessed once the zone 14250Sstevel@tonic-gate * is "dead". 14260Sstevel@tonic-gate * 14270Sstevel@tonic-gate * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 14280Sstevel@tonic-gate * to force halt/reboot to block waiting for the zone_cred_ref to drop 14290Sstevel@tonic-gate * to 0. This can be useful to flush out other sources of cached creds 14300Sstevel@tonic-gate * that may be less innocuous than the driver case. 14310Sstevel@tonic-gate */ 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate int zone_wait_for_cred = 0; 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate static void 14360Sstevel@tonic-gate zone_hold_locked(zone_t *z) 14370Sstevel@tonic-gate { 14380Sstevel@tonic-gate ASSERT(MUTEX_HELD(&z->zone_lock)); 14390Sstevel@tonic-gate z->zone_ref++; 14400Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 14410Sstevel@tonic-gate } 14420Sstevel@tonic-gate 14430Sstevel@tonic-gate void 14440Sstevel@tonic-gate zone_hold(zone_t *z) 14450Sstevel@tonic-gate { 14460Sstevel@tonic-gate mutex_enter(&z->zone_lock); 14470Sstevel@tonic-gate zone_hold_locked(z); 14480Sstevel@tonic-gate mutex_exit(&z->zone_lock); 14490Sstevel@tonic-gate } 14500Sstevel@tonic-gate 14510Sstevel@tonic-gate /* 14520Sstevel@tonic-gate * If the non-cred ref count drops to 1 and either the cred ref count 14530Sstevel@tonic-gate * is 0 or we aren't waiting for cred references, the zone is ready to 14540Sstevel@tonic-gate * be destroyed. 14550Sstevel@tonic-gate */ 14560Sstevel@tonic-gate #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 14570Sstevel@tonic-gate (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate void 14600Sstevel@tonic-gate zone_rele(zone_t *z) 14610Sstevel@tonic-gate { 14620Sstevel@tonic-gate boolean_t wakeup; 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate mutex_enter(&z->zone_lock); 14650Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 14660Sstevel@tonic-gate z->zone_ref--; 14670Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 14680Sstevel@tonic-gate /* no more refs, free the structure */ 14690Sstevel@tonic-gate mutex_exit(&z->zone_lock); 14700Sstevel@tonic-gate zone_free(z); 14710Sstevel@tonic-gate return; 14720Sstevel@tonic-gate } 14730Sstevel@tonic-gate /* signal zone_destroy so the zone can finish halting */ 14740Sstevel@tonic-gate wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 14750Sstevel@tonic-gate mutex_exit(&z->zone_lock); 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate if (wakeup) { 14780Sstevel@tonic-gate /* 14790Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 14800Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 14810Sstevel@tonic-gate */ 14820Sstevel@tonic-gate mutex_enter(&zonehash_lock); 14830Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 14840Sstevel@tonic-gate mutex_exit(&zonehash_lock); 14850Sstevel@tonic-gate } 14860Sstevel@tonic-gate } 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate void 14890Sstevel@tonic-gate zone_cred_hold(zone_t *z) 14900Sstevel@tonic-gate { 14910Sstevel@tonic-gate mutex_enter(&z->zone_lock); 14920Sstevel@tonic-gate z->zone_cred_ref++; 14930Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 14940Sstevel@tonic-gate mutex_exit(&z->zone_lock); 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate void 14980Sstevel@tonic-gate zone_cred_rele(zone_t *z) 14990Sstevel@tonic-gate { 15000Sstevel@tonic-gate boolean_t wakeup; 15010Sstevel@tonic-gate 15020Sstevel@tonic-gate mutex_enter(&z->zone_lock); 15030Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 15040Sstevel@tonic-gate z->zone_cred_ref--; 15050Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 15060Sstevel@tonic-gate /* no more refs, free the structure */ 15070Sstevel@tonic-gate mutex_exit(&z->zone_lock); 15080Sstevel@tonic-gate zone_free(z); 15090Sstevel@tonic-gate return; 15100Sstevel@tonic-gate } 15110Sstevel@tonic-gate /* 15120Sstevel@tonic-gate * If zone_destroy is waiting for the cred references to drain 15130Sstevel@tonic-gate * out, and they have, signal it. 15140Sstevel@tonic-gate */ 15150Sstevel@tonic-gate wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 15160Sstevel@tonic-gate zone_status_get(z) >= ZONE_IS_DEAD); 15170Sstevel@tonic-gate mutex_exit(&z->zone_lock); 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate if (wakeup) { 15200Sstevel@tonic-gate /* 15210Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 15220Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 15230Sstevel@tonic-gate */ 15240Sstevel@tonic-gate mutex_enter(&zonehash_lock); 15250Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 15260Sstevel@tonic-gate mutex_exit(&zonehash_lock); 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate } 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate void 15310Sstevel@tonic-gate zone_task_hold(zone_t *z) 15320Sstevel@tonic-gate { 15330Sstevel@tonic-gate mutex_enter(&z->zone_lock); 15340Sstevel@tonic-gate z->zone_ntasks++; 15350Sstevel@tonic-gate ASSERT(z->zone_ntasks != 0); 15360Sstevel@tonic-gate mutex_exit(&z->zone_lock); 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate void 15400Sstevel@tonic-gate zone_task_rele(zone_t *zone) 15410Sstevel@tonic-gate { 15420Sstevel@tonic-gate uint_t refcnt; 15430Sstevel@tonic-gate 15440Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 15450Sstevel@tonic-gate ASSERT(zone->zone_ntasks != 0); 15460Sstevel@tonic-gate refcnt = --zone->zone_ntasks; 15470Sstevel@tonic-gate if (refcnt > 1) { /* Common case */ 15480Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 15490Sstevel@tonic-gate return; 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate zone_hold_locked(zone); /* so we can use the zone_t later */ 15520Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 15530Sstevel@tonic-gate if (refcnt == 1) { 15540Sstevel@tonic-gate /* 15550Sstevel@tonic-gate * See if the zone is shutting down. 15560Sstevel@tonic-gate */ 15570Sstevel@tonic-gate mutex_enter(&zone_status_lock); 15580Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 15590Sstevel@tonic-gate goto out; 15600Sstevel@tonic-gate } 15610Sstevel@tonic-gate 15620Sstevel@tonic-gate /* 15630Sstevel@tonic-gate * Make sure the ntasks didn't change since we 15640Sstevel@tonic-gate * dropped zone_lock. 15650Sstevel@tonic-gate */ 15660Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 15670Sstevel@tonic-gate if (refcnt != zone->zone_ntasks) { 15680Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 15690Sstevel@tonic-gate goto out; 15700Sstevel@tonic-gate } 15710Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate /* 15740Sstevel@tonic-gate * No more user processes in the zone. The zone is empty. 15750Sstevel@tonic-gate */ 15760Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 15770Sstevel@tonic-gate goto out; 15780Sstevel@tonic-gate } 15790Sstevel@tonic-gate 15800Sstevel@tonic-gate ASSERT(refcnt == 0); 15810Sstevel@tonic-gate /* 15820Sstevel@tonic-gate * zsched has exited; the zone is dead. 15830Sstevel@tonic-gate */ 15840Sstevel@tonic-gate zone->zone_zsched = NULL; /* paranoia */ 15850Sstevel@tonic-gate mutex_enter(&zone_status_lock); 15860Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DEAD); 15870Sstevel@tonic-gate out: 15880Sstevel@tonic-gate mutex_exit(&zone_status_lock); 15890Sstevel@tonic-gate zone_rele(zone); 15900Sstevel@tonic-gate } 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate zoneid_t 15930Sstevel@tonic-gate getzoneid(void) 15940Sstevel@tonic-gate { 15950Sstevel@tonic-gate return (curproc->p_zone->zone_id); 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate /* 15990Sstevel@tonic-gate * Internal versions of zone_find_by_*(). These don't zone_hold() or 16000Sstevel@tonic-gate * check the validity of a zone's state. 16010Sstevel@tonic-gate */ 16020Sstevel@tonic-gate static zone_t * 16030Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid) 16040Sstevel@tonic-gate { 16050Sstevel@tonic-gate mod_hash_val_t hv; 16060Sstevel@tonic-gate zone_t *zone = NULL; 16070Sstevel@tonic-gate 16080Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate if (mod_hash_find(zonehashbyid, 16110Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 16120Sstevel@tonic-gate zone = (zone_t *)hv; 16130Sstevel@tonic-gate return (zone); 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate static zone_t * 16171676Sjpk zone_find_all_by_label(const ts_label_t *label) 16181676Sjpk { 16191676Sjpk mod_hash_val_t hv; 16201676Sjpk zone_t *zone = NULL; 16211676Sjpk 16221676Sjpk ASSERT(MUTEX_HELD(&zonehash_lock)); 16231676Sjpk 16241676Sjpk /* 16251676Sjpk * zonehashbylabel is not maintained for unlabeled systems 16261676Sjpk */ 16271676Sjpk if (!is_system_labeled()) 16281676Sjpk return (NULL); 16291676Sjpk if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0) 16301676Sjpk zone = (zone_t *)hv; 16311676Sjpk return (zone); 16321676Sjpk } 16331676Sjpk 16341676Sjpk static zone_t * 16350Sstevel@tonic-gate zone_find_all_by_name(char *name) 16360Sstevel@tonic-gate { 16370Sstevel@tonic-gate mod_hash_val_t hv; 16380Sstevel@tonic-gate zone_t *zone = NULL; 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 16430Sstevel@tonic-gate zone = (zone_t *)hv; 16440Sstevel@tonic-gate return (zone); 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate /* 16480Sstevel@tonic-gate * Public interface for looking up a zone by zoneid. Only returns the zone if 16490Sstevel@tonic-gate * it is fully initialized, and has not yet begun the zone_destroy() sequence. 16500Sstevel@tonic-gate * Caller must call zone_rele() once it is done with the zone. 16510Sstevel@tonic-gate * 16520Sstevel@tonic-gate * The zone may begin the zone_destroy() sequence immediately after this 16530Sstevel@tonic-gate * function returns, but may be safely used until zone_rele() is called. 16540Sstevel@tonic-gate */ 16550Sstevel@tonic-gate zone_t * 16560Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid) 16570Sstevel@tonic-gate { 16580Sstevel@tonic-gate zone_t *zone; 16590Sstevel@tonic-gate zone_status_t status; 16600Sstevel@tonic-gate 16610Sstevel@tonic-gate mutex_enter(&zonehash_lock); 16620Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 16630Sstevel@tonic-gate mutex_exit(&zonehash_lock); 16640Sstevel@tonic-gate return (NULL); 16650Sstevel@tonic-gate } 16660Sstevel@tonic-gate status = zone_status_get(zone); 16670Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 16680Sstevel@tonic-gate /* 16690Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 16700Sstevel@tonic-gate */ 16710Sstevel@tonic-gate mutex_exit(&zonehash_lock); 16720Sstevel@tonic-gate return (NULL); 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate zone_hold(zone); 16750Sstevel@tonic-gate mutex_exit(&zonehash_lock); 16760Sstevel@tonic-gate return (zone); 16770Sstevel@tonic-gate } 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate /* 16801676Sjpk * Similar to zone_find_by_id, but using zone label as the key. 16811676Sjpk */ 16821676Sjpk zone_t * 16831676Sjpk zone_find_by_label(const ts_label_t *label) 16841676Sjpk { 16851676Sjpk zone_t *zone; 16862110Srica zone_status_t status; 16871676Sjpk 16881676Sjpk mutex_enter(&zonehash_lock); 16891676Sjpk if ((zone = zone_find_all_by_label(label)) == NULL) { 16901676Sjpk mutex_exit(&zonehash_lock); 16911676Sjpk return (NULL); 16921676Sjpk } 16932110Srica 16942110Srica status = zone_status_get(zone); 16952110Srica if (status > ZONE_IS_DOWN) { 16961676Sjpk /* 16971676Sjpk * For all practical purposes the zone doesn't exist. 16981676Sjpk */ 16992110Srica mutex_exit(&zonehash_lock); 17002110Srica return (NULL); 17011676Sjpk } 17022110Srica zone_hold(zone); 17031676Sjpk mutex_exit(&zonehash_lock); 17041676Sjpk return (zone); 17051676Sjpk } 17061676Sjpk 17071676Sjpk /* 17080Sstevel@tonic-gate * Similar to zone_find_by_id, but using zone name as the key. 17090Sstevel@tonic-gate */ 17100Sstevel@tonic-gate zone_t * 17110Sstevel@tonic-gate zone_find_by_name(char *name) 17120Sstevel@tonic-gate { 17130Sstevel@tonic-gate zone_t *zone; 17140Sstevel@tonic-gate zone_status_t status; 17150Sstevel@tonic-gate 17160Sstevel@tonic-gate mutex_enter(&zonehash_lock); 17170Sstevel@tonic-gate if ((zone = zone_find_all_by_name(name)) == NULL) { 17180Sstevel@tonic-gate mutex_exit(&zonehash_lock); 17190Sstevel@tonic-gate return (NULL); 17200Sstevel@tonic-gate } 17210Sstevel@tonic-gate status = zone_status_get(zone); 17220Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 17230Sstevel@tonic-gate /* 17240Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 17250Sstevel@tonic-gate */ 17260Sstevel@tonic-gate mutex_exit(&zonehash_lock); 17270Sstevel@tonic-gate return (NULL); 17280Sstevel@tonic-gate } 17290Sstevel@tonic-gate zone_hold(zone); 17300Sstevel@tonic-gate mutex_exit(&zonehash_lock); 17310Sstevel@tonic-gate return (zone); 17320Sstevel@tonic-gate } 17330Sstevel@tonic-gate 17340Sstevel@tonic-gate /* 17350Sstevel@tonic-gate * Similar to zone_find_by_id(), using the path as a key. For instance, 17360Sstevel@tonic-gate * if there is a zone "foo" rooted at /foo/root, and the path argument 17370Sstevel@tonic-gate * is "/foo/root/proc", it will return the held zone_t corresponding to 17380Sstevel@tonic-gate * zone "foo". 17390Sstevel@tonic-gate * 17400Sstevel@tonic-gate * zone_find_by_path() always returns a non-NULL value, since at the 17410Sstevel@tonic-gate * very least every path will be contained in the global zone. 17420Sstevel@tonic-gate * 17430Sstevel@tonic-gate * As with the other zone_find_by_*() functions, the caller is 17440Sstevel@tonic-gate * responsible for zone_rele()ing the return value of this function. 17450Sstevel@tonic-gate */ 17460Sstevel@tonic-gate zone_t * 17470Sstevel@tonic-gate zone_find_by_path(const char *path) 17480Sstevel@tonic-gate { 17490Sstevel@tonic-gate zone_t *zone; 17500Sstevel@tonic-gate zone_t *zret = NULL; 17510Sstevel@tonic-gate zone_status_t status; 17520Sstevel@tonic-gate 17530Sstevel@tonic-gate if (path == NULL) { 17540Sstevel@tonic-gate /* 17550Sstevel@tonic-gate * Call from rootconf(). 17560Sstevel@tonic-gate */ 17570Sstevel@tonic-gate zone_hold(global_zone); 17580Sstevel@tonic-gate return (global_zone); 17590Sstevel@tonic-gate } 17600Sstevel@tonic-gate ASSERT(*path == '/'); 17610Sstevel@tonic-gate mutex_enter(&zonehash_lock); 17620Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 17630Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 17640Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(path, zone)) 17650Sstevel@tonic-gate zret = zone; 17660Sstevel@tonic-gate } 17670Sstevel@tonic-gate ASSERT(zret != NULL); 17680Sstevel@tonic-gate status = zone_status_get(zret); 17690Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 17700Sstevel@tonic-gate /* 17710Sstevel@tonic-gate * Zone practically doesn't exist. 17720Sstevel@tonic-gate */ 17730Sstevel@tonic-gate zret = global_zone; 17740Sstevel@tonic-gate } 17750Sstevel@tonic-gate zone_hold(zret); 17760Sstevel@tonic-gate mutex_exit(&zonehash_lock); 17770Sstevel@tonic-gate return (zret); 17780Sstevel@tonic-gate } 17790Sstevel@tonic-gate 17800Sstevel@tonic-gate /* 17810Sstevel@tonic-gate * Get the number of cpus visible to this zone. The system-wide global 17820Sstevel@tonic-gate * 'ncpus' is returned if pools are disabled, the caller is in the 17830Sstevel@tonic-gate * global zone, or a NULL zone argument is passed in. 17840Sstevel@tonic-gate */ 17850Sstevel@tonic-gate int 17860Sstevel@tonic-gate zone_ncpus_get(zone_t *zone) 17870Sstevel@tonic-gate { 17880Sstevel@tonic-gate int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 17890Sstevel@tonic-gate 17900Sstevel@tonic-gate return (myncpus != 0 ? myncpus : ncpus); 17910Sstevel@tonic-gate } 17920Sstevel@tonic-gate 17930Sstevel@tonic-gate /* 17940Sstevel@tonic-gate * Get the number of online cpus visible to this zone. The system-wide 17950Sstevel@tonic-gate * global 'ncpus_online' is returned if pools are disabled, the caller 17960Sstevel@tonic-gate * is in the global zone, or a NULL zone argument is passed in. 17970Sstevel@tonic-gate */ 17980Sstevel@tonic-gate int 17990Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone) 18000Sstevel@tonic-gate { 18010Sstevel@tonic-gate int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate return (myncpus_online != 0 ? myncpus_online : ncpus_online); 18040Sstevel@tonic-gate } 18050Sstevel@tonic-gate 18060Sstevel@tonic-gate /* 18070Sstevel@tonic-gate * Return the pool to which the zone is currently bound. 18080Sstevel@tonic-gate */ 18090Sstevel@tonic-gate pool_t * 18100Sstevel@tonic-gate zone_pool_get(zone_t *zone) 18110Sstevel@tonic-gate { 18120Sstevel@tonic-gate ASSERT(pool_lock_held()); 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate return (zone->zone_pool); 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate 18170Sstevel@tonic-gate /* 18180Sstevel@tonic-gate * Set the zone's pool pointer and update the zone's visibility to match 18190Sstevel@tonic-gate * the resources in the new pool. 18200Sstevel@tonic-gate */ 18210Sstevel@tonic-gate void 18220Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool) 18230Sstevel@tonic-gate { 18240Sstevel@tonic-gate ASSERT(pool_lock_held()); 18250Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 18260Sstevel@tonic-gate 18270Sstevel@tonic-gate zone->zone_pool = pool; 18280Sstevel@tonic-gate zone_pset_set(zone, pool->pool_pset->pset_id); 18290Sstevel@tonic-gate } 18300Sstevel@tonic-gate 18310Sstevel@tonic-gate /* 18320Sstevel@tonic-gate * Return the cached value of the id of the processor set to which the 18330Sstevel@tonic-gate * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 18340Sstevel@tonic-gate * facility is disabled. 18350Sstevel@tonic-gate */ 18360Sstevel@tonic-gate psetid_t 18370Sstevel@tonic-gate zone_pset_get(zone_t *zone) 18380Sstevel@tonic-gate { 18390Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 18400Sstevel@tonic-gate 18410Sstevel@tonic-gate return (zone->zone_psetid); 18420Sstevel@tonic-gate } 18430Sstevel@tonic-gate 18440Sstevel@tonic-gate /* 18450Sstevel@tonic-gate * Set the cached value of the id of the processor set to which the zone 18460Sstevel@tonic-gate * is currently bound. Also update the zone's visibility to match the 18470Sstevel@tonic-gate * resources in the new processor set. 18480Sstevel@tonic-gate */ 18490Sstevel@tonic-gate void 18500Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid) 18510Sstevel@tonic-gate { 18520Sstevel@tonic-gate psetid_t oldpsetid; 18530Sstevel@tonic-gate 18540Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 18550Sstevel@tonic-gate oldpsetid = zone_pset_get(zone); 18560Sstevel@tonic-gate 18570Sstevel@tonic-gate if (oldpsetid == newpsetid) 18580Sstevel@tonic-gate return; 18590Sstevel@tonic-gate /* 18600Sstevel@tonic-gate * Global zone sees all. 18610Sstevel@tonic-gate */ 18620Sstevel@tonic-gate if (zone != global_zone) { 18630Sstevel@tonic-gate zone->zone_psetid = newpsetid; 18640Sstevel@tonic-gate if (newpsetid != ZONE_PS_INVAL) 18650Sstevel@tonic-gate pool_pset_visibility_add(newpsetid, zone); 18660Sstevel@tonic-gate if (oldpsetid != ZONE_PS_INVAL) 18670Sstevel@tonic-gate pool_pset_visibility_remove(oldpsetid, zone); 18680Sstevel@tonic-gate } 18690Sstevel@tonic-gate /* 18700Sstevel@tonic-gate * Disabling pools, so we should start using the global values 18710Sstevel@tonic-gate * for ncpus and ncpus_online. 18720Sstevel@tonic-gate */ 18730Sstevel@tonic-gate if (newpsetid == ZONE_PS_INVAL) { 18740Sstevel@tonic-gate zone->zone_ncpus = 0; 18750Sstevel@tonic-gate zone->zone_ncpus_online = 0; 18760Sstevel@tonic-gate } 18770Sstevel@tonic-gate } 18780Sstevel@tonic-gate 18790Sstevel@tonic-gate /* 18800Sstevel@tonic-gate * Walk the list of active zones and issue the provided callback for 18810Sstevel@tonic-gate * each of them. 18820Sstevel@tonic-gate * 18830Sstevel@tonic-gate * Caller must not be holding any locks that may be acquired under 18840Sstevel@tonic-gate * zonehash_lock. See comment at the beginning of the file for a list of 18850Sstevel@tonic-gate * common locks and their interactions with zones. 18860Sstevel@tonic-gate */ 18870Sstevel@tonic-gate int 18880Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data) 18890Sstevel@tonic-gate { 18900Sstevel@tonic-gate zone_t *zone; 18910Sstevel@tonic-gate int ret = 0; 18920Sstevel@tonic-gate zone_status_t status; 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate mutex_enter(&zonehash_lock); 18950Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 18960Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 18970Sstevel@tonic-gate /* 18980Sstevel@tonic-gate * Skip zones that shouldn't be externally visible. 18990Sstevel@tonic-gate */ 19000Sstevel@tonic-gate status = zone_status_get(zone); 19010Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 19020Sstevel@tonic-gate continue; 19030Sstevel@tonic-gate /* 19040Sstevel@tonic-gate * Bail immediately if any callback invocation returns a 19050Sstevel@tonic-gate * non-zero value. 19060Sstevel@tonic-gate */ 19070Sstevel@tonic-gate ret = (*cb)(zone, data); 19080Sstevel@tonic-gate if (ret != 0) 19090Sstevel@tonic-gate break; 19100Sstevel@tonic-gate } 19110Sstevel@tonic-gate mutex_exit(&zonehash_lock); 19120Sstevel@tonic-gate return (ret); 19130Sstevel@tonic-gate } 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate static int 19160Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath) 19170Sstevel@tonic-gate { 19180Sstevel@tonic-gate vnode_t *vp; 19190Sstevel@tonic-gate int trycount; 19200Sstevel@tonic-gate int error = 0; 19210Sstevel@tonic-gate char *path; 19220Sstevel@tonic-gate struct pathname upn, pn; 19230Sstevel@tonic-gate size_t pathlen; 19240Sstevel@tonic-gate 19250Sstevel@tonic-gate if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 19260Sstevel@tonic-gate return (error); 19270Sstevel@tonic-gate 19280Sstevel@tonic-gate pn_alloc(&pn); 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate /* prevent infinite loop */ 19310Sstevel@tonic-gate trycount = 10; 19320Sstevel@tonic-gate for (;;) { 19330Sstevel@tonic-gate if (--trycount <= 0) { 19340Sstevel@tonic-gate error = ESTALE; 19350Sstevel@tonic-gate goto out; 19360Sstevel@tonic-gate } 19370Sstevel@tonic-gate 19380Sstevel@tonic-gate if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 19390Sstevel@tonic-gate /* 19400Sstevel@tonic-gate * VOP_ACCESS() may cover 'vp' with a new 19410Sstevel@tonic-gate * filesystem, if 'vp' is an autoFS vnode. 19420Sstevel@tonic-gate * Get the new 'vp' if so. 19430Sstevel@tonic-gate */ 19440Sstevel@tonic-gate if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 && 19450Sstevel@tonic-gate (vp->v_vfsmountedhere == NULL || 19460Sstevel@tonic-gate (error = traverse(&vp)) == 0)) { 19470Sstevel@tonic-gate pathlen = pn.pn_pathlen + 2; 19480Sstevel@tonic-gate path = kmem_alloc(pathlen, KM_SLEEP); 19490Sstevel@tonic-gate (void) strncpy(path, pn.pn_path, 19500Sstevel@tonic-gate pn.pn_pathlen + 1); 19510Sstevel@tonic-gate path[pathlen - 2] = '/'; 19520Sstevel@tonic-gate path[pathlen - 1] = '\0'; 19530Sstevel@tonic-gate pn_free(&pn); 19540Sstevel@tonic-gate pn_free(&upn); 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate /* Success! */ 19570Sstevel@tonic-gate break; 19580Sstevel@tonic-gate } 19590Sstevel@tonic-gate VN_RELE(vp); 19600Sstevel@tonic-gate } 19610Sstevel@tonic-gate if (error != ESTALE) 19620Sstevel@tonic-gate goto out; 19630Sstevel@tonic-gate } 19640Sstevel@tonic-gate 19650Sstevel@tonic-gate ASSERT(error == 0); 19660Sstevel@tonic-gate zone->zone_rootvp = vp; /* we hold a reference to vp */ 19670Sstevel@tonic-gate zone->zone_rootpath = path; 19680Sstevel@tonic-gate zone->zone_rootpathlen = pathlen; 19691769Scarlsonj if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0) 19701769Scarlsonj zone->zone_flags |= ZF_IS_SCRATCH; 19710Sstevel@tonic-gate return (0); 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate out: 19740Sstevel@tonic-gate pn_free(&pn); 19750Sstevel@tonic-gate pn_free(&upn); 19760Sstevel@tonic-gate return (error); 19770Sstevel@tonic-gate } 19780Sstevel@tonic-gate 19790Sstevel@tonic-gate #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 19800Sstevel@tonic-gate ((c) >= 'a' && (c) <= 'z') || \ 19810Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'Z')) 19820Sstevel@tonic-gate 19830Sstevel@tonic-gate static int 19840Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname) 19850Sstevel@tonic-gate { 19860Sstevel@tonic-gate char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 19870Sstevel@tonic-gate size_t len; 19880Sstevel@tonic-gate int i, err; 19890Sstevel@tonic-gate 19900Sstevel@tonic-gate if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 19910Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 19920Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 19930Sstevel@tonic-gate } 19940Sstevel@tonic-gate 19950Sstevel@tonic-gate /* must be less than ZONENAME_MAX */ 19960Sstevel@tonic-gate if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 19970Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 19980Sstevel@tonic-gate return (EINVAL); 19990Sstevel@tonic-gate } 20000Sstevel@tonic-gate 20010Sstevel@tonic-gate /* 20020Sstevel@tonic-gate * Name must start with an alphanumeric and must contain only 20030Sstevel@tonic-gate * alphanumerics, '-', '_' and '.'. 20040Sstevel@tonic-gate */ 20050Sstevel@tonic-gate if (!isalnum(kname[0])) { 20060Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 20070Sstevel@tonic-gate return (EINVAL); 20080Sstevel@tonic-gate } 20090Sstevel@tonic-gate for (i = 1; i < len - 1; i++) { 20100Sstevel@tonic-gate if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 20110Sstevel@tonic-gate kname[i] != '.') { 20120Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 20130Sstevel@tonic-gate return (EINVAL); 20140Sstevel@tonic-gate } 20150Sstevel@tonic-gate } 20160Sstevel@tonic-gate 20170Sstevel@tonic-gate zone->zone_name = kname; 20180Sstevel@tonic-gate return (0); 20190Sstevel@tonic-gate } 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate /* 20220Sstevel@tonic-gate * Similar to thread_create(), but makes sure the thread is in the appropriate 20230Sstevel@tonic-gate * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 20240Sstevel@tonic-gate */ 20250Sstevel@tonic-gate /*ARGSUSED*/ 20260Sstevel@tonic-gate kthread_t * 20270Sstevel@tonic-gate zthread_create( 20280Sstevel@tonic-gate caddr_t stk, 20290Sstevel@tonic-gate size_t stksize, 20300Sstevel@tonic-gate void (*proc)(), 20310Sstevel@tonic-gate void *arg, 20320Sstevel@tonic-gate size_t len, 20330Sstevel@tonic-gate pri_t pri) 20340Sstevel@tonic-gate { 20350Sstevel@tonic-gate kthread_t *t; 20360Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 20370Sstevel@tonic-gate proc_t *pp = zone->zone_zsched; 20380Sstevel@tonic-gate 20390Sstevel@tonic-gate zone_hold(zone); /* Reference to be dropped when thread exits */ 20400Sstevel@tonic-gate 20410Sstevel@tonic-gate /* 20420Sstevel@tonic-gate * No-one should be trying to create threads if the zone is shutting 20430Sstevel@tonic-gate * down and there aren't any kernel threads around. See comment 20440Sstevel@tonic-gate * in zthread_exit(). 20450Sstevel@tonic-gate */ 20460Sstevel@tonic-gate ASSERT(!(zone->zone_kthreads == NULL && 20470Sstevel@tonic-gate zone_status_get(zone) >= ZONE_IS_EMPTY)); 20480Sstevel@tonic-gate /* 20490Sstevel@tonic-gate * Create a thread, but don't let it run until we've finished setting 20500Sstevel@tonic-gate * things up. 20510Sstevel@tonic-gate */ 20520Sstevel@tonic-gate t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 20530Sstevel@tonic-gate ASSERT(t->t_forw == NULL); 20540Sstevel@tonic-gate mutex_enter(&zone_status_lock); 20550Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 20560Sstevel@tonic-gate t->t_forw = t->t_back = t; 20570Sstevel@tonic-gate } else { 20580Sstevel@tonic-gate kthread_t *tx = zone->zone_kthreads; 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate t->t_forw = tx; 20610Sstevel@tonic-gate t->t_back = tx->t_back; 20620Sstevel@tonic-gate tx->t_back->t_forw = t; 20630Sstevel@tonic-gate tx->t_back = t; 20640Sstevel@tonic-gate } 20650Sstevel@tonic-gate zone->zone_kthreads = t; 20660Sstevel@tonic-gate mutex_exit(&zone_status_lock); 20670Sstevel@tonic-gate 20680Sstevel@tonic-gate mutex_enter(&pp->p_lock); 20690Sstevel@tonic-gate t->t_proc_flag |= TP_ZTHREAD; 20700Sstevel@tonic-gate project_rele(t->t_proj); 20710Sstevel@tonic-gate t->t_proj = project_hold(pp->p_task->tk_proj); 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate /* 20740Sstevel@tonic-gate * Setup complete, let it run. 20750Sstevel@tonic-gate */ 20760Sstevel@tonic-gate thread_lock(t); 20770Sstevel@tonic-gate t->t_schedflag |= TS_ALLSTART; 20780Sstevel@tonic-gate setrun_locked(t); 20790Sstevel@tonic-gate thread_unlock(t); 20800Sstevel@tonic-gate 20810Sstevel@tonic-gate mutex_exit(&pp->p_lock); 20820Sstevel@tonic-gate 20830Sstevel@tonic-gate return (t); 20840Sstevel@tonic-gate } 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate /* 20870Sstevel@tonic-gate * Similar to thread_exit(). Must be called by threads created via 20880Sstevel@tonic-gate * zthread_exit(). 20890Sstevel@tonic-gate */ 20900Sstevel@tonic-gate void 20910Sstevel@tonic-gate zthread_exit(void) 20920Sstevel@tonic-gate { 20930Sstevel@tonic-gate kthread_t *t = curthread; 20940Sstevel@tonic-gate proc_t *pp = curproc; 20950Sstevel@tonic-gate zone_t *zone = pp->p_zone; 20960Sstevel@tonic-gate 20970Sstevel@tonic-gate mutex_enter(&zone_status_lock); 20980Sstevel@tonic-gate 20990Sstevel@tonic-gate /* 21000Sstevel@tonic-gate * Reparent to p0 21010Sstevel@tonic-gate */ 21021075Sjosephb kpreempt_disable(); 21030Sstevel@tonic-gate mutex_enter(&pp->p_lock); 21040Sstevel@tonic-gate t->t_proc_flag &= ~TP_ZTHREAD; 21050Sstevel@tonic-gate t->t_procp = &p0; 21060Sstevel@tonic-gate hat_thread_exit(t); 21070Sstevel@tonic-gate mutex_exit(&pp->p_lock); 21081075Sjosephb kpreempt_enable(); 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate if (t->t_back == t) { 21110Sstevel@tonic-gate ASSERT(t->t_forw == t); 21120Sstevel@tonic-gate /* 21130Sstevel@tonic-gate * If the zone is empty, once the thread count 21140Sstevel@tonic-gate * goes to zero no further kernel threads can be 21150Sstevel@tonic-gate * created. This is because if the creator is a process 21160Sstevel@tonic-gate * in the zone, then it must have exited before the zone 21170Sstevel@tonic-gate * state could be set to ZONE_IS_EMPTY. 21180Sstevel@tonic-gate * Otherwise, if the creator is a kernel thread in the 21190Sstevel@tonic-gate * zone, the thread count is non-zero. 21200Sstevel@tonic-gate * 21210Sstevel@tonic-gate * This really means that non-zone kernel threads should 21220Sstevel@tonic-gate * not create zone kernel threads. 21230Sstevel@tonic-gate */ 21240Sstevel@tonic-gate zone->zone_kthreads = NULL; 21250Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_EMPTY) { 21260Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 21270Sstevel@tonic-gate } 21280Sstevel@tonic-gate } else { 21290Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 21300Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 21310Sstevel@tonic-gate if (zone->zone_kthreads == t) 21320Sstevel@tonic-gate zone->zone_kthreads = t->t_forw; 21330Sstevel@tonic-gate } 21340Sstevel@tonic-gate mutex_exit(&zone_status_lock); 21350Sstevel@tonic-gate zone_rele(zone); 21360Sstevel@tonic-gate thread_exit(); 21370Sstevel@tonic-gate /* NOTREACHED */ 21380Sstevel@tonic-gate } 21390Sstevel@tonic-gate 21400Sstevel@tonic-gate static void 21410Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 21420Sstevel@tonic-gate { 21430Sstevel@tonic-gate vnode_t *oldvp; 21440Sstevel@tonic-gate 21450Sstevel@tonic-gate /* we're going to hold a reference here to the directory */ 21460Sstevel@tonic-gate VN_HOLD(vp); 21470Sstevel@tonic-gate 21480Sstevel@tonic-gate #ifdef C2_AUDIT 21490Sstevel@tonic-gate if (audit_active) /* update abs cwd/root path see c2audit.c */ 21500Sstevel@tonic-gate audit_chdirec(vp, vpp); 21510Sstevel@tonic-gate #endif 21520Sstevel@tonic-gate 21530Sstevel@tonic-gate mutex_enter(&pp->p_lock); 21540Sstevel@tonic-gate oldvp = *vpp; 21550Sstevel@tonic-gate *vpp = vp; 21560Sstevel@tonic-gate mutex_exit(&pp->p_lock); 21570Sstevel@tonic-gate if (oldvp != NULL) 21580Sstevel@tonic-gate VN_RELE(oldvp); 21590Sstevel@tonic-gate } 21600Sstevel@tonic-gate 21610Sstevel@tonic-gate /* 21620Sstevel@tonic-gate * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 21630Sstevel@tonic-gate */ 21640Sstevel@tonic-gate static int 21650Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 21660Sstevel@tonic-gate { 21670Sstevel@tonic-gate nvpair_t *nvp = NULL; 21680Sstevel@tonic-gate boolean_t priv_set = B_FALSE; 21690Sstevel@tonic-gate boolean_t limit_set = B_FALSE; 21700Sstevel@tonic-gate boolean_t action_set = B_FALSE; 21710Sstevel@tonic-gate 21720Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 21730Sstevel@tonic-gate const char *name; 21740Sstevel@tonic-gate uint64_t ui64; 21750Sstevel@tonic-gate 21760Sstevel@tonic-gate name = nvpair_name(nvp); 21770Sstevel@tonic-gate if (nvpair_type(nvp) != DATA_TYPE_UINT64) 21780Sstevel@tonic-gate return (EINVAL); 21790Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &ui64); 21800Sstevel@tonic-gate if (strcmp(name, "privilege") == 0) { 21810Sstevel@tonic-gate /* 21820Sstevel@tonic-gate * Currently only privileged values are allowed, but 21830Sstevel@tonic-gate * this may change in the future. 21840Sstevel@tonic-gate */ 21850Sstevel@tonic-gate if (ui64 != RCPRIV_PRIVILEGED) 21860Sstevel@tonic-gate return (EINVAL); 21870Sstevel@tonic-gate rv->rcv_privilege = ui64; 21880Sstevel@tonic-gate priv_set = B_TRUE; 21890Sstevel@tonic-gate } else if (strcmp(name, "limit") == 0) { 21900Sstevel@tonic-gate rv->rcv_value = ui64; 21910Sstevel@tonic-gate limit_set = B_TRUE; 21920Sstevel@tonic-gate } else if (strcmp(name, "action") == 0) { 21930Sstevel@tonic-gate if (ui64 != RCTL_LOCAL_NOACTION && 21940Sstevel@tonic-gate ui64 != RCTL_LOCAL_DENY) 21950Sstevel@tonic-gate return (EINVAL); 21960Sstevel@tonic-gate rv->rcv_flagaction = ui64; 21970Sstevel@tonic-gate action_set = B_TRUE; 21980Sstevel@tonic-gate } else { 21990Sstevel@tonic-gate return (EINVAL); 22000Sstevel@tonic-gate } 22010Sstevel@tonic-gate } 22020Sstevel@tonic-gate 22030Sstevel@tonic-gate if (!(priv_set && limit_set && action_set)) 22040Sstevel@tonic-gate return (EINVAL); 22050Sstevel@tonic-gate rv->rcv_action_signal = 0; 22060Sstevel@tonic-gate rv->rcv_action_recipient = NULL; 22070Sstevel@tonic-gate rv->rcv_action_recip_pid = -1; 22080Sstevel@tonic-gate rv->rcv_firing_time = 0; 22090Sstevel@tonic-gate 22100Sstevel@tonic-gate return (0); 22110Sstevel@tonic-gate } 22120Sstevel@tonic-gate 2213*2267Sdp /* 2214*2267Sdp * Non-global zone version of start_init. 2215*2267Sdp */ 22160Sstevel@tonic-gate void 2217*2267Sdp zone_start_init(void) 22180Sstevel@tonic-gate { 22190Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 2220*2267Sdp 2221*2267Sdp ASSERT(!INGLOBALZONE(curproc)); 22220Sstevel@tonic-gate 22230Sstevel@tonic-gate /* 2224*2267Sdp * We maintain zone_boot_err so that we can return the cause of the 2225*2267Sdp * failure back to the caller of the zone_boot syscall. 22260Sstevel@tonic-gate */ 2227*2267Sdp p->p_zone->zone_boot_err = start_init_common(); 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22300Sstevel@tonic-gate if (p->p_zone->zone_boot_err != 0) { 22310Sstevel@tonic-gate /* 22320Sstevel@tonic-gate * Make sure we are still in the booting state-- we could have 22330Sstevel@tonic-gate * raced and already be shutting down, or even further along. 22340Sstevel@tonic-gate */ 22350Sstevel@tonic-gate if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING) 22360Sstevel@tonic-gate zone_status_set(p->p_zone, ZONE_IS_SHUTTING_DOWN); 22370Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22380Sstevel@tonic-gate /* It's gone bad, dispose of the process */ 22390Sstevel@tonic-gate if (proc_exit(CLD_EXITED, p->p_zone->zone_boot_err) != 0) { 2240390Sraf mutex_enter(&p->p_lock); 2241390Sraf ASSERT(p->p_flag & SEXITLWPS); 22420Sstevel@tonic-gate lwp_exit(); 22430Sstevel@tonic-gate } 22440Sstevel@tonic-gate } else { 22450Sstevel@tonic-gate if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING) 22460Sstevel@tonic-gate zone_status_set(p->p_zone, ZONE_IS_RUNNING); 22470Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22480Sstevel@tonic-gate /* cause the process to return to userland. */ 22490Sstevel@tonic-gate lwp_rtt(); 22500Sstevel@tonic-gate } 22510Sstevel@tonic-gate } 22520Sstevel@tonic-gate 22530Sstevel@tonic-gate struct zsched_arg { 22540Sstevel@tonic-gate zone_t *zone; 22550Sstevel@tonic-gate nvlist_t *nvlist; 22560Sstevel@tonic-gate }; 22570Sstevel@tonic-gate 22580Sstevel@tonic-gate /* 22590Sstevel@tonic-gate * Per-zone "sched" workalike. The similarity to "sched" doesn't have 22600Sstevel@tonic-gate * anything to do with scheduling, but rather with the fact that 22610Sstevel@tonic-gate * per-zone kernel threads are parented to zsched, just like regular 22620Sstevel@tonic-gate * kernel threads are parented to sched (p0). 22630Sstevel@tonic-gate * 22640Sstevel@tonic-gate * zsched is also responsible for launching init for the zone. 22650Sstevel@tonic-gate */ 22660Sstevel@tonic-gate static void 22670Sstevel@tonic-gate zsched(void *arg) 22680Sstevel@tonic-gate { 22690Sstevel@tonic-gate struct zsched_arg *za = arg; 22700Sstevel@tonic-gate proc_t *pp = curproc; 22710Sstevel@tonic-gate proc_t *initp = proc_init; 22720Sstevel@tonic-gate zone_t *zone = za->zone; 22730Sstevel@tonic-gate cred_t *cr, *oldcred; 22740Sstevel@tonic-gate rctl_set_t *set; 22750Sstevel@tonic-gate rctl_alloc_gp_t *gp; 22760Sstevel@tonic-gate contract_t *ct = NULL; 22770Sstevel@tonic-gate task_t *tk, *oldtk; 22780Sstevel@tonic-gate rctl_entity_p_t e; 22790Sstevel@tonic-gate kproject_t *pj; 22800Sstevel@tonic-gate 22810Sstevel@tonic-gate nvlist_t *nvl = za->nvlist; 22820Sstevel@tonic-gate nvpair_t *nvp = NULL; 22830Sstevel@tonic-gate 22840Sstevel@tonic-gate bcopy("zsched", u.u_psargs, sizeof ("zsched")); 22850Sstevel@tonic-gate bcopy("zsched", u.u_comm, sizeof ("zsched")); 22860Sstevel@tonic-gate u.u_argc = 0; 22870Sstevel@tonic-gate u.u_argv = NULL; 22880Sstevel@tonic-gate u.u_envp = NULL; 22890Sstevel@tonic-gate closeall(P_FINFO(pp)); 22900Sstevel@tonic-gate 22910Sstevel@tonic-gate /* 22920Sstevel@tonic-gate * We are this zone's "zsched" process. As the zone isn't generally 22930Sstevel@tonic-gate * visible yet we don't need to grab any locks before initializing its 22940Sstevel@tonic-gate * zone_proc pointer. 22950Sstevel@tonic-gate */ 22960Sstevel@tonic-gate zone_hold(zone); /* this hold is released by zone_destroy() */ 22970Sstevel@tonic-gate zone->zone_zsched = pp; 22980Sstevel@tonic-gate mutex_enter(&pp->p_lock); 22990Sstevel@tonic-gate pp->p_zone = zone; 23000Sstevel@tonic-gate mutex_exit(&pp->p_lock); 23010Sstevel@tonic-gate 23020Sstevel@tonic-gate /* 23030Sstevel@tonic-gate * Disassociate process from its 'parent'; parent ourselves to init 23040Sstevel@tonic-gate * (pid 1) and change other values as needed. 23050Sstevel@tonic-gate */ 23060Sstevel@tonic-gate sess_create(); 23070Sstevel@tonic-gate 23080Sstevel@tonic-gate mutex_enter(&pidlock); 23090Sstevel@tonic-gate proc_detach(pp); 23100Sstevel@tonic-gate pp->p_ppid = 1; 23110Sstevel@tonic-gate pp->p_flag |= SZONETOP; 23120Sstevel@tonic-gate pp->p_ancpid = 1; 23130Sstevel@tonic-gate pp->p_parent = initp; 23140Sstevel@tonic-gate pp->p_psibling = NULL; 23150Sstevel@tonic-gate if (initp->p_child) 23160Sstevel@tonic-gate initp->p_child->p_psibling = pp; 23170Sstevel@tonic-gate pp->p_sibling = initp->p_child; 23180Sstevel@tonic-gate initp->p_child = pp; 23190Sstevel@tonic-gate 23200Sstevel@tonic-gate /* Decrement what newproc() incremented. */ 23210Sstevel@tonic-gate upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 23220Sstevel@tonic-gate /* 23230Sstevel@tonic-gate * Our credentials are about to become kcred-like, so we don't care 23240Sstevel@tonic-gate * about the caller's ruid. 23250Sstevel@tonic-gate */ 23260Sstevel@tonic-gate upcount_inc(crgetruid(kcred), zone->zone_id); 23270Sstevel@tonic-gate mutex_exit(&pidlock); 23280Sstevel@tonic-gate 23290Sstevel@tonic-gate /* 23300Sstevel@tonic-gate * getting out of global zone, so decrement lwp counts 23310Sstevel@tonic-gate */ 23320Sstevel@tonic-gate pj = pp->p_task->tk_proj; 23330Sstevel@tonic-gate mutex_enter(&global_zone->zone_nlwps_lock); 23340Sstevel@tonic-gate pj->kpj_nlwps -= pp->p_lwpcnt; 23350Sstevel@tonic-gate global_zone->zone_nlwps -= pp->p_lwpcnt; 23360Sstevel@tonic-gate mutex_exit(&global_zone->zone_nlwps_lock); 23370Sstevel@tonic-gate 23380Sstevel@tonic-gate /* 23390Sstevel@tonic-gate * Create and join a new task in project '0' of this zone. 23400Sstevel@tonic-gate * 23410Sstevel@tonic-gate * We don't need to call holdlwps() since we know we're the only lwp in 23420Sstevel@tonic-gate * this process. 23430Sstevel@tonic-gate * 23440Sstevel@tonic-gate * task_join() returns with p_lock held. 23450Sstevel@tonic-gate */ 23460Sstevel@tonic-gate tk = task_create(0, zone); 23470Sstevel@tonic-gate mutex_enter(&cpu_lock); 23480Sstevel@tonic-gate oldtk = task_join(tk, 0); 23490Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 23500Sstevel@tonic-gate mutex_exit(&cpu_lock); 23510Sstevel@tonic-gate task_rele(oldtk); 23520Sstevel@tonic-gate 23530Sstevel@tonic-gate /* 23540Sstevel@tonic-gate * add lwp counts to zsched's zone, and increment project's task count 23550Sstevel@tonic-gate * due to the task created in the above tasksys_settaskid 23560Sstevel@tonic-gate */ 23570Sstevel@tonic-gate pj = pp->p_task->tk_proj; 23580Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 23590Sstevel@tonic-gate pj->kpj_nlwps += pp->p_lwpcnt; 23600Sstevel@tonic-gate pj->kpj_ntasks += 1; 23610Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 23620Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 23630Sstevel@tonic-gate 23640Sstevel@tonic-gate /* 23650Sstevel@tonic-gate * The process was created by a process in the global zone, hence the 23660Sstevel@tonic-gate * credentials are wrong. We might as well have kcred-ish credentials. 23670Sstevel@tonic-gate */ 23680Sstevel@tonic-gate cr = zone->zone_kcred; 23690Sstevel@tonic-gate crhold(cr); 23700Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 23710Sstevel@tonic-gate oldcred = pp->p_cred; 23720Sstevel@tonic-gate pp->p_cred = cr; 23730Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 23740Sstevel@tonic-gate crfree(oldcred); 23750Sstevel@tonic-gate 23760Sstevel@tonic-gate /* 23770Sstevel@tonic-gate * Hold credentials again (for thread) 23780Sstevel@tonic-gate */ 23790Sstevel@tonic-gate crhold(cr); 23800Sstevel@tonic-gate 23810Sstevel@tonic-gate /* 23820Sstevel@tonic-gate * p_lwpcnt can't change since this is a kernel process. 23830Sstevel@tonic-gate */ 23840Sstevel@tonic-gate crset(pp, cr); 23850Sstevel@tonic-gate 23860Sstevel@tonic-gate /* 23870Sstevel@tonic-gate * Chroot 23880Sstevel@tonic-gate */ 23890Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 23900Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 23910Sstevel@tonic-gate 23920Sstevel@tonic-gate /* 23930Sstevel@tonic-gate * Initialize zone's rctl set. 23940Sstevel@tonic-gate */ 23950Sstevel@tonic-gate set = rctl_set_create(); 23960Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 23970Sstevel@tonic-gate mutex_enter(&pp->p_lock); 23980Sstevel@tonic-gate e.rcep_p.zone = zone; 23990Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 24000Sstevel@tonic-gate zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 24010Sstevel@tonic-gate mutex_exit(&pp->p_lock); 24020Sstevel@tonic-gate rctl_prealloc_destroy(gp); 24030Sstevel@tonic-gate 24040Sstevel@tonic-gate /* 24050Sstevel@tonic-gate * Apply the rctls passed in to zone_create(). This is basically a list 24060Sstevel@tonic-gate * assignment: all of the old values are removed and the new ones 24070Sstevel@tonic-gate * inserted. That is, if an empty list is passed in, all values are 24080Sstevel@tonic-gate * removed. 24090Sstevel@tonic-gate */ 24100Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 24110Sstevel@tonic-gate rctl_dict_entry_t *rde; 24120Sstevel@tonic-gate rctl_hndl_t hndl; 24130Sstevel@tonic-gate char *name; 24140Sstevel@tonic-gate nvlist_t **nvlarray; 24150Sstevel@tonic-gate uint_t i, nelem; 24160Sstevel@tonic-gate int error; /* For ASSERT()s */ 24170Sstevel@tonic-gate 24180Sstevel@tonic-gate name = nvpair_name(nvp); 24190Sstevel@tonic-gate hndl = rctl_hndl_lookup(name); 24200Sstevel@tonic-gate ASSERT(hndl != -1); 24210Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 24220Sstevel@tonic-gate ASSERT(rde != NULL); 24230Sstevel@tonic-gate 24240Sstevel@tonic-gate for (; /* ever */; ) { 24250Sstevel@tonic-gate rctl_val_t oval; 24260Sstevel@tonic-gate 24270Sstevel@tonic-gate mutex_enter(&pp->p_lock); 24280Sstevel@tonic-gate error = rctl_local_get(hndl, NULL, &oval, pp); 24290Sstevel@tonic-gate mutex_exit(&pp->p_lock); 24300Sstevel@tonic-gate ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 24310Sstevel@tonic-gate ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 24320Sstevel@tonic-gate if (oval.rcv_privilege == RCPRIV_SYSTEM) 24330Sstevel@tonic-gate break; 24340Sstevel@tonic-gate mutex_enter(&pp->p_lock); 24350Sstevel@tonic-gate error = rctl_local_delete(hndl, &oval, pp); 24360Sstevel@tonic-gate mutex_exit(&pp->p_lock); 24370Sstevel@tonic-gate ASSERT(error == 0); 24380Sstevel@tonic-gate } 24390Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 24400Sstevel@tonic-gate ASSERT(error == 0); 24410Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 24420Sstevel@tonic-gate rctl_val_t *nvalp; 24430Sstevel@tonic-gate 24440Sstevel@tonic-gate nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 24450Sstevel@tonic-gate error = nvlist2rctlval(nvlarray[i], nvalp); 24460Sstevel@tonic-gate ASSERT(error == 0); 24470Sstevel@tonic-gate /* 24480Sstevel@tonic-gate * rctl_local_insert can fail if the value being 24490Sstevel@tonic-gate * inserted is a duplicate; this is OK. 24500Sstevel@tonic-gate */ 24510Sstevel@tonic-gate mutex_enter(&pp->p_lock); 24520Sstevel@tonic-gate if (rctl_local_insert(hndl, nvalp, pp) != 0) 24530Sstevel@tonic-gate kmem_cache_free(rctl_val_cache, nvalp); 24540Sstevel@tonic-gate mutex_exit(&pp->p_lock); 24550Sstevel@tonic-gate } 24560Sstevel@tonic-gate } 24570Sstevel@tonic-gate /* 24580Sstevel@tonic-gate * Tell the world that we're done setting up. 24590Sstevel@tonic-gate * 24600Sstevel@tonic-gate * At this point we want to set the zone status to ZONE_IS_READY 24610Sstevel@tonic-gate * and atomically set the zone's processor set visibility. Once 24620Sstevel@tonic-gate * we drop pool_lock() this zone will automatically get updated 24630Sstevel@tonic-gate * to reflect any future changes to the pools configuration. 24640Sstevel@tonic-gate */ 24650Sstevel@tonic-gate pool_lock(); 24660Sstevel@tonic-gate mutex_enter(&cpu_lock); 24670Sstevel@tonic-gate mutex_enter(&zonehash_lock); 24680Sstevel@tonic-gate zone_uniqid(zone); 24690Sstevel@tonic-gate zone_zsd_configure(zone); 24700Sstevel@tonic-gate if (pool_state == POOL_ENABLED) 24710Sstevel@tonic-gate zone_pset_set(zone, pool_default->pool_pset->pset_id); 24720Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24730Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 24740Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_READY); 24750Sstevel@tonic-gate mutex_exit(&zone_status_lock); 24760Sstevel@tonic-gate mutex_exit(&zonehash_lock); 24770Sstevel@tonic-gate mutex_exit(&cpu_lock); 24780Sstevel@tonic-gate pool_unlock(); 24790Sstevel@tonic-gate 24800Sstevel@tonic-gate /* 24810Sstevel@tonic-gate * Once we see the zone transition to the ZONE_IS_BOOTING state, 24820Sstevel@tonic-gate * we launch init, and set the state to running. 24830Sstevel@tonic-gate */ 24840Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 24850Sstevel@tonic-gate 24860Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_BOOTING) { 24870Sstevel@tonic-gate id_t cid; 24880Sstevel@tonic-gate 24890Sstevel@tonic-gate /* 24900Sstevel@tonic-gate * Ok, this is a little complicated. We need to grab the 24910Sstevel@tonic-gate * zone's pool's scheduling class ID; note that by now, we 24920Sstevel@tonic-gate * are already bound to a pool if we need to be (zoneadmd 24930Sstevel@tonic-gate * will have done that to us while we're in the READY 24940Sstevel@tonic-gate * state). *But* the scheduling class for the zone's 'init' 24950Sstevel@tonic-gate * must be explicitly passed to newproc, which doesn't 24960Sstevel@tonic-gate * respect pool bindings. 24970Sstevel@tonic-gate * 24980Sstevel@tonic-gate * We hold the pool_lock across the call to newproc() to 24990Sstevel@tonic-gate * close the obvious race: the pool's scheduling class 25000Sstevel@tonic-gate * could change before we manage to create the LWP with 25010Sstevel@tonic-gate * classid 'cid'. 25020Sstevel@tonic-gate */ 25030Sstevel@tonic-gate pool_lock(); 25040Sstevel@tonic-gate cid = pool_get_class(zone->zone_pool); 25050Sstevel@tonic-gate if (cid == -1) 25060Sstevel@tonic-gate cid = defaultcid; 25070Sstevel@tonic-gate 25080Sstevel@tonic-gate /* 25090Sstevel@tonic-gate * If this fails, zone_boot will ultimately fail. The 25100Sstevel@tonic-gate * state of the zone will be set to SHUTTING_DOWN-- userland 25110Sstevel@tonic-gate * will have to tear down the zone, and fail, or try again. 25120Sstevel@tonic-gate */ 2513*2267Sdp if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid, 25140Sstevel@tonic-gate minclsyspri - 1, &ct)) != 0) { 25150Sstevel@tonic-gate mutex_enter(&zone_status_lock); 25160Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 25170Sstevel@tonic-gate mutex_exit(&zone_status_lock); 25180Sstevel@tonic-gate } 25190Sstevel@tonic-gate pool_unlock(); 25200Sstevel@tonic-gate } 25210Sstevel@tonic-gate 25220Sstevel@tonic-gate /* 25230Sstevel@tonic-gate * Wait for zone_destroy() to be called. This is what we spend 25240Sstevel@tonic-gate * most of our life doing. 25250Sstevel@tonic-gate */ 25260Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 25270Sstevel@tonic-gate 25280Sstevel@tonic-gate if (ct) 25290Sstevel@tonic-gate /* 25300Sstevel@tonic-gate * At this point the process contract should be empty. 25310Sstevel@tonic-gate * (Though if it isn't, it's not the end of the world.) 25320Sstevel@tonic-gate */ 25330Sstevel@tonic-gate VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 25340Sstevel@tonic-gate 25350Sstevel@tonic-gate /* 25360Sstevel@tonic-gate * Allow kcred to be freed when all referring processes 25370Sstevel@tonic-gate * (including this one) go away. We can't just do this in 25380Sstevel@tonic-gate * zone_free because we need to wait for the zone_cred_ref to 25390Sstevel@tonic-gate * drop to 0 before calling zone_free, and the existence of 25400Sstevel@tonic-gate * zone_kcred will prevent that. Thus, we call crfree here to 25410Sstevel@tonic-gate * balance the crdup in zone_create. The crhold calls earlier 25420Sstevel@tonic-gate * in zsched will be dropped when the thread and process exit. 25430Sstevel@tonic-gate */ 25440Sstevel@tonic-gate crfree(zone->zone_kcred); 25450Sstevel@tonic-gate zone->zone_kcred = NULL; 25460Sstevel@tonic-gate 25470Sstevel@tonic-gate exit(CLD_EXITED, 0); 25480Sstevel@tonic-gate } 25490Sstevel@tonic-gate 25500Sstevel@tonic-gate /* 25510Sstevel@tonic-gate * Helper function to determine if there are any submounts of the 25520Sstevel@tonic-gate * provided path. Used to make sure the zone doesn't "inherit" any 25530Sstevel@tonic-gate * mounts from before it is created. 25540Sstevel@tonic-gate */ 25550Sstevel@tonic-gate static uint_t 25560Sstevel@tonic-gate zone_mount_count(const char *rootpath) 25570Sstevel@tonic-gate { 25580Sstevel@tonic-gate vfs_t *vfsp; 25590Sstevel@tonic-gate uint_t count = 0; 25600Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 25610Sstevel@tonic-gate 25620Sstevel@tonic-gate /* 25630Sstevel@tonic-gate * Holding zonehash_lock prevents race conditions with 25640Sstevel@tonic-gate * vfs_list_add()/vfs_list_remove() since we serialize with 25650Sstevel@tonic-gate * zone_find_by_path(). 25660Sstevel@tonic-gate */ 25670Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 25680Sstevel@tonic-gate /* 25690Sstevel@tonic-gate * The rootpath must end with a '/' 25700Sstevel@tonic-gate */ 25710Sstevel@tonic-gate ASSERT(rootpath[rootpathlen - 1] == '/'); 25720Sstevel@tonic-gate 25730Sstevel@tonic-gate /* 25740Sstevel@tonic-gate * This intentionally does not count the rootpath itself if that 25750Sstevel@tonic-gate * happens to be a mount point. 25760Sstevel@tonic-gate */ 25770Sstevel@tonic-gate vfs_list_read_lock(); 25780Sstevel@tonic-gate vfsp = rootvfs; 25790Sstevel@tonic-gate do { 25800Sstevel@tonic-gate if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 25810Sstevel@tonic-gate rootpathlen) == 0) 25820Sstevel@tonic-gate count++; 25830Sstevel@tonic-gate vfsp = vfsp->vfs_next; 25840Sstevel@tonic-gate } while (vfsp != rootvfs); 25850Sstevel@tonic-gate vfs_list_unlock(); 25860Sstevel@tonic-gate return (count); 25870Sstevel@tonic-gate } 25880Sstevel@tonic-gate 25890Sstevel@tonic-gate /* 25900Sstevel@tonic-gate * Helper function to make sure that a zone created on 'rootpath' 25910Sstevel@tonic-gate * wouldn't end up containing other zones' rootpaths. 25920Sstevel@tonic-gate */ 25930Sstevel@tonic-gate static boolean_t 25940Sstevel@tonic-gate zone_is_nested(const char *rootpath) 25950Sstevel@tonic-gate { 25960Sstevel@tonic-gate zone_t *zone; 25970Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 25980Sstevel@tonic-gate size_t len; 25990Sstevel@tonic-gate 26000Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 26010Sstevel@tonic-gate 26020Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 26030Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 26040Sstevel@tonic-gate if (zone == global_zone) 26050Sstevel@tonic-gate continue; 26060Sstevel@tonic-gate len = strlen(zone->zone_rootpath); 26070Sstevel@tonic-gate if (strncmp(rootpath, zone->zone_rootpath, 26080Sstevel@tonic-gate MIN(rootpathlen, len)) == 0) 26090Sstevel@tonic-gate return (B_TRUE); 26100Sstevel@tonic-gate } 26110Sstevel@tonic-gate return (B_FALSE); 26120Sstevel@tonic-gate } 26130Sstevel@tonic-gate 26140Sstevel@tonic-gate static int 2615813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs, 2616813Sdp size_t zone_privssz) 26170Sstevel@tonic-gate { 26180Sstevel@tonic-gate priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 26190Sstevel@tonic-gate 2620813Sdp if (zone_privssz < sizeof (priv_set_t)) 2621813Sdp return (set_errno(ENOMEM)); 2622813Sdp 26230Sstevel@tonic-gate if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 26240Sstevel@tonic-gate kmem_free(privs, sizeof (priv_set_t)); 26250Sstevel@tonic-gate return (EFAULT); 26260Sstevel@tonic-gate } 26270Sstevel@tonic-gate 26280Sstevel@tonic-gate zone->zone_privset = privs; 26290Sstevel@tonic-gate return (0); 26300Sstevel@tonic-gate } 26310Sstevel@tonic-gate 26320Sstevel@tonic-gate /* 26330Sstevel@tonic-gate * We make creative use of nvlists to pass in rctls from userland. The list is 26340Sstevel@tonic-gate * a list of the following structures: 26350Sstevel@tonic-gate * 26360Sstevel@tonic-gate * (name = rctl_name, value = nvpair_list_array) 26370Sstevel@tonic-gate * 26380Sstevel@tonic-gate * Where each element of the nvpair_list_array is of the form: 26390Sstevel@tonic-gate * 26400Sstevel@tonic-gate * [(name = "privilege", value = RCPRIV_PRIVILEGED), 26410Sstevel@tonic-gate * (name = "limit", value = uint64_t), 26420Sstevel@tonic-gate * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 26430Sstevel@tonic-gate */ 26440Sstevel@tonic-gate static int 26450Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 26460Sstevel@tonic-gate { 26470Sstevel@tonic-gate nvpair_t *nvp = NULL; 26480Sstevel@tonic-gate nvlist_t *nvl = NULL; 26490Sstevel@tonic-gate char *kbuf; 26500Sstevel@tonic-gate int error; 26510Sstevel@tonic-gate rctl_val_t rv; 26520Sstevel@tonic-gate 26530Sstevel@tonic-gate *nvlp = NULL; 26540Sstevel@tonic-gate 26550Sstevel@tonic-gate if (buflen == 0) 26560Sstevel@tonic-gate return (0); 26570Sstevel@tonic-gate 26580Sstevel@tonic-gate if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 26590Sstevel@tonic-gate return (ENOMEM); 26600Sstevel@tonic-gate if (copyin(ubuf, kbuf, buflen)) { 26610Sstevel@tonic-gate error = EFAULT; 26620Sstevel@tonic-gate goto out; 26630Sstevel@tonic-gate } 26640Sstevel@tonic-gate if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 26650Sstevel@tonic-gate /* 26660Sstevel@tonic-gate * nvl may have been allocated/free'd, but the value set to 26670Sstevel@tonic-gate * non-NULL, so we reset it here. 26680Sstevel@tonic-gate */ 26690Sstevel@tonic-gate nvl = NULL; 26700Sstevel@tonic-gate error = EINVAL; 26710Sstevel@tonic-gate goto out; 26720Sstevel@tonic-gate } 26730Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 26740Sstevel@tonic-gate rctl_dict_entry_t *rde; 26750Sstevel@tonic-gate rctl_hndl_t hndl; 26760Sstevel@tonic-gate nvlist_t **nvlarray; 26770Sstevel@tonic-gate uint_t i, nelem; 26780Sstevel@tonic-gate char *name; 26790Sstevel@tonic-gate 26800Sstevel@tonic-gate error = EINVAL; 26810Sstevel@tonic-gate name = nvpair_name(nvp); 26820Sstevel@tonic-gate if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 26830Sstevel@tonic-gate != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 26840Sstevel@tonic-gate goto out; 26850Sstevel@tonic-gate } 26860Sstevel@tonic-gate if ((hndl = rctl_hndl_lookup(name)) == -1) { 26870Sstevel@tonic-gate goto out; 26880Sstevel@tonic-gate } 26890Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 26900Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 26910Sstevel@tonic-gate ASSERT(error == 0); 26920Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 26930Sstevel@tonic-gate if (error = nvlist2rctlval(nvlarray[i], &rv)) 26940Sstevel@tonic-gate goto out; 26950Sstevel@tonic-gate } 26960Sstevel@tonic-gate if (rctl_invalid_value(rde, &rv)) { 26970Sstevel@tonic-gate error = EINVAL; 26980Sstevel@tonic-gate goto out; 26990Sstevel@tonic-gate } 27000Sstevel@tonic-gate } 27010Sstevel@tonic-gate error = 0; 27020Sstevel@tonic-gate *nvlp = nvl; 27030Sstevel@tonic-gate out: 27040Sstevel@tonic-gate kmem_free(kbuf, buflen); 27050Sstevel@tonic-gate if (error && nvl != NULL) 27060Sstevel@tonic-gate nvlist_free(nvl); 27070Sstevel@tonic-gate return (error); 27080Sstevel@tonic-gate } 27090Sstevel@tonic-gate 27100Sstevel@tonic-gate int 27110Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) { 27120Sstevel@tonic-gate if (er_out != NULL) { 27130Sstevel@tonic-gate if (copyout(&er_ext, er_out, sizeof (int))) { 27140Sstevel@tonic-gate return (set_errno(EFAULT)); 27150Sstevel@tonic-gate } 27160Sstevel@tonic-gate } 27170Sstevel@tonic-gate return (set_errno(er_error)); 27180Sstevel@tonic-gate } 27190Sstevel@tonic-gate 27201676Sjpk static int 27211676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi) 27221676Sjpk { 27231676Sjpk ts_label_t *tsl; 27241676Sjpk bslabel_t blab; 27251676Sjpk 27261676Sjpk /* Get label from user */ 27271676Sjpk if (copyin(lab, &blab, sizeof (blab)) != 0) 27281676Sjpk return (EFAULT); 27291676Sjpk tsl = labelalloc(&blab, doi, KM_NOSLEEP); 27301676Sjpk if (tsl == NULL) 27311676Sjpk return (ENOMEM); 27321676Sjpk 27331676Sjpk zone->zone_slabel = tsl; 27341676Sjpk return (0); 27351676Sjpk } 27361676Sjpk 27370Sstevel@tonic-gate /* 2738789Sahrens * Parses a comma-separated list of ZFS datasets into a per-zone dictionary. 2739789Sahrens */ 2740789Sahrens static int 2741789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen) 2742789Sahrens { 2743789Sahrens char *kbuf; 2744789Sahrens char *dataset, *next; 2745789Sahrens zone_dataset_t *zd; 2746789Sahrens size_t len; 2747789Sahrens 2748789Sahrens if (ubuf == NULL || buflen == 0) 2749789Sahrens return (0); 2750789Sahrens 2751789Sahrens if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 2752789Sahrens return (ENOMEM); 2753789Sahrens 2754789Sahrens if (copyin(ubuf, kbuf, buflen) != 0) { 2755789Sahrens kmem_free(kbuf, buflen); 2756789Sahrens return (EFAULT); 2757789Sahrens } 2758789Sahrens 2759789Sahrens dataset = next = kbuf; 2760789Sahrens for (;;) { 2761789Sahrens zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP); 2762789Sahrens 2763789Sahrens next = strchr(dataset, ','); 2764789Sahrens 2765789Sahrens if (next == NULL) 2766789Sahrens len = strlen(dataset); 2767789Sahrens else 2768789Sahrens len = next - dataset; 2769789Sahrens 2770789Sahrens zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP); 2771789Sahrens bcopy(dataset, zd->zd_dataset, len); 2772789Sahrens zd->zd_dataset[len] = '\0'; 2773789Sahrens 2774789Sahrens list_insert_head(&zone->zone_datasets, zd); 2775789Sahrens 2776789Sahrens if (next == NULL) 2777789Sahrens break; 2778789Sahrens 2779789Sahrens dataset = next + 1; 2780789Sahrens } 2781789Sahrens 2782789Sahrens kmem_free(kbuf, buflen); 2783789Sahrens return (0); 2784789Sahrens } 2785789Sahrens 2786789Sahrens /* 27870Sstevel@tonic-gate * System call to create/initialize a new zone named 'zone_name', rooted 27880Sstevel@tonic-gate * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 27891676Sjpk * and initialized with the zone-wide rctls described in 'rctlbuf', and 27901676Sjpk * with labeling set by 'match', 'doi', and 'label'. 27910Sstevel@tonic-gate * 27920Sstevel@tonic-gate * If extended error is non-null, we may use it to return more detailed 27930Sstevel@tonic-gate * error information. 27940Sstevel@tonic-gate */ 27950Sstevel@tonic-gate static zoneid_t 27960Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root, 2797813Sdp const priv_set_t *zone_privs, size_t zone_privssz, 2798813Sdp caddr_t rctlbuf, size_t rctlbufsz, 27991676Sjpk caddr_t zfsbuf, size_t zfsbufsz, int *extended_error, 28001676Sjpk int match, uint32_t doi, const bslabel_t *label) 28010Sstevel@tonic-gate { 28020Sstevel@tonic-gate struct zsched_arg zarg; 28030Sstevel@tonic-gate nvlist_t *rctls = NULL; 28040Sstevel@tonic-gate proc_t *pp = curproc; 28050Sstevel@tonic-gate zone_t *zone, *ztmp; 28060Sstevel@tonic-gate zoneid_t zoneid; 28070Sstevel@tonic-gate int error; 28080Sstevel@tonic-gate int error2 = 0; 28090Sstevel@tonic-gate char *str; 28100Sstevel@tonic-gate cred_t *zkcr; 28111769Scarlsonj boolean_t insert_label_hash; 28120Sstevel@tonic-gate 28130Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 28140Sstevel@tonic-gate return (set_errno(EPERM)); 28150Sstevel@tonic-gate 28160Sstevel@tonic-gate /* can't boot zone from within chroot environment */ 28170Sstevel@tonic-gate if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 28180Sstevel@tonic-gate return (zone_create_error(ENOTSUP, ZE_CHROOTED, 2819813Sdp extended_error)); 28200Sstevel@tonic-gate 28210Sstevel@tonic-gate zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 28220Sstevel@tonic-gate zoneid = zone->zone_id = id_alloc(zoneid_space); 28230Sstevel@tonic-gate zone->zone_status = ZONE_IS_UNINITIALIZED; 28240Sstevel@tonic-gate zone->zone_pool = pool_default; 28250Sstevel@tonic-gate zone->zone_pool_mod = gethrtime(); 28260Sstevel@tonic-gate zone->zone_psetid = ZONE_PS_INVAL; 28270Sstevel@tonic-gate zone->zone_ncpus = 0; 28280Sstevel@tonic-gate zone->zone_ncpus_online = 0; 28290Sstevel@tonic-gate mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 28300Sstevel@tonic-gate mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 28310Sstevel@tonic-gate cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 28320Sstevel@tonic-gate list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 28330Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 2834789Sahrens list_create(&zone->zone_datasets, sizeof (zone_dataset_t), 2835789Sahrens offsetof(zone_dataset_t, zd_linkage)); 28361676Sjpk rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 28370Sstevel@tonic-gate 28380Sstevel@tonic-gate if ((error = zone_set_name(zone, zone_name)) != 0) { 28390Sstevel@tonic-gate zone_free(zone); 28400Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 28410Sstevel@tonic-gate } 28420Sstevel@tonic-gate 28430Sstevel@tonic-gate if ((error = zone_set_root(zone, zone_root)) != 0) { 28440Sstevel@tonic-gate zone_free(zone); 28450Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 28460Sstevel@tonic-gate } 2847813Sdp if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) { 28480Sstevel@tonic-gate zone_free(zone); 28490Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 28500Sstevel@tonic-gate } 28510Sstevel@tonic-gate 28520Sstevel@tonic-gate /* initialize node name to be the same as zone name */ 28530Sstevel@tonic-gate zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 28540Sstevel@tonic-gate (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 28550Sstevel@tonic-gate zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 28560Sstevel@tonic-gate 28570Sstevel@tonic-gate zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 28580Sstevel@tonic-gate zone->zone_domain[0] = '\0'; 28590Sstevel@tonic-gate zone->zone_shares = 1; 28600Sstevel@tonic-gate zone->zone_bootargs = NULL; 2861*2267Sdp zone->zone_initname = 2862*2267Sdp kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP); 2863*2267Sdp (void) strcpy(zone->zone_initname, zone_default_initname); 28640Sstevel@tonic-gate 28650Sstevel@tonic-gate /* 28660Sstevel@tonic-gate * Zsched initializes the rctls. 28670Sstevel@tonic-gate */ 28680Sstevel@tonic-gate zone->zone_rctls = NULL; 28690Sstevel@tonic-gate 28700Sstevel@tonic-gate if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 28710Sstevel@tonic-gate zone_free(zone); 28720Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 28730Sstevel@tonic-gate } 28740Sstevel@tonic-gate 2875789Sahrens if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) { 2876789Sahrens zone_free(zone); 2877789Sahrens return (set_errno(error)); 2878789Sahrens } 2879789Sahrens 28800Sstevel@tonic-gate /* 28811676Sjpk * Read in the trusted system parameters: 28821676Sjpk * match flag and sensitivity label. 28831676Sjpk */ 28841676Sjpk zone->zone_match = match; 28851769Scarlsonj if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 28861676Sjpk error = zone_set_label(zone, label, doi); 28871676Sjpk if (error != 0) { 28881676Sjpk zone_free(zone); 28891676Sjpk return (set_errno(error)); 28901676Sjpk } 28911769Scarlsonj insert_label_hash = B_TRUE; 28921676Sjpk } else { 28931676Sjpk /* all zones get an admin_low label if system is not labeled */ 28941676Sjpk zone->zone_slabel = l_admin_low; 28951676Sjpk label_hold(l_admin_low); 28961769Scarlsonj insert_label_hash = B_FALSE; 28971676Sjpk } 28981676Sjpk 28991676Sjpk /* 29000Sstevel@tonic-gate * Stop all lwps since that's what normally happens as part of fork(). 29010Sstevel@tonic-gate * This needs to happen before we grab any locks to avoid deadlock 29020Sstevel@tonic-gate * (another lwp in the process could be waiting for the held lock). 29030Sstevel@tonic-gate */ 29040Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 29050Sstevel@tonic-gate zone_free(zone); 29060Sstevel@tonic-gate if (rctls) 29070Sstevel@tonic-gate nvlist_free(rctls); 29080Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 29090Sstevel@tonic-gate } 29100Sstevel@tonic-gate 29110Sstevel@tonic-gate if (block_mounts() == 0) { 29120Sstevel@tonic-gate mutex_enter(&pp->p_lock); 29130Sstevel@tonic-gate if (curthread != pp->p_agenttp) 29140Sstevel@tonic-gate continuelwps(pp); 29150Sstevel@tonic-gate mutex_exit(&pp->p_lock); 29160Sstevel@tonic-gate zone_free(zone); 29170Sstevel@tonic-gate if (rctls) 29180Sstevel@tonic-gate nvlist_free(rctls); 29190Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 29200Sstevel@tonic-gate } 29210Sstevel@tonic-gate 29220Sstevel@tonic-gate /* 29230Sstevel@tonic-gate * Set up credential for kernel access. After this, any errors 29240Sstevel@tonic-gate * should go through the dance in errout rather than calling 29250Sstevel@tonic-gate * zone_free directly. 29260Sstevel@tonic-gate */ 29270Sstevel@tonic-gate zone->zone_kcred = crdup(kcred); 29280Sstevel@tonic-gate crsetzone(zone->zone_kcred, zone); 29290Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 29300Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 29310Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 29320Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 29330Sstevel@tonic-gate 29340Sstevel@tonic-gate mutex_enter(&zonehash_lock); 29350Sstevel@tonic-gate /* 29360Sstevel@tonic-gate * Make sure zone doesn't already exist. 29371676Sjpk * 29381676Sjpk * If the system and zone are labeled, 29391676Sjpk * make sure no other zone exists that has the same label. 29400Sstevel@tonic-gate */ 29411676Sjpk if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL || 29421769Scarlsonj (insert_label_hash && 29431676Sjpk (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) { 29440Sstevel@tonic-gate zone_status_t status; 29450Sstevel@tonic-gate 29460Sstevel@tonic-gate status = zone_status_get(ztmp); 29470Sstevel@tonic-gate if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 29480Sstevel@tonic-gate error = EEXIST; 29490Sstevel@tonic-gate else 29500Sstevel@tonic-gate error = EBUSY; 29510Sstevel@tonic-gate goto errout; 29520Sstevel@tonic-gate } 29530Sstevel@tonic-gate 29540Sstevel@tonic-gate /* 29550Sstevel@tonic-gate * Don't allow zone creations which would cause one zone's rootpath to 29560Sstevel@tonic-gate * be accessible from that of another (non-global) zone. 29570Sstevel@tonic-gate */ 29580Sstevel@tonic-gate if (zone_is_nested(zone->zone_rootpath)) { 29590Sstevel@tonic-gate error = EBUSY; 29600Sstevel@tonic-gate goto errout; 29610Sstevel@tonic-gate } 29620Sstevel@tonic-gate 29630Sstevel@tonic-gate ASSERT(zonecount != 0); /* check for leaks */ 29640Sstevel@tonic-gate if (zonecount + 1 > maxzones) { 29650Sstevel@tonic-gate error = ENOMEM; 29660Sstevel@tonic-gate goto errout; 29670Sstevel@tonic-gate } 29680Sstevel@tonic-gate 29690Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 29700Sstevel@tonic-gate error = EBUSY; 29710Sstevel@tonic-gate error2 = ZE_AREMOUNTS; 29720Sstevel@tonic-gate goto errout; 29730Sstevel@tonic-gate } 29740Sstevel@tonic-gate 29750Sstevel@tonic-gate /* 29760Sstevel@tonic-gate * Zone is still incomplete, but we need to drop all locks while 29770Sstevel@tonic-gate * zsched() initializes this zone's kernel process. We 29780Sstevel@tonic-gate * optimistically add the zone to the hashtable and associated 29790Sstevel@tonic-gate * lists so a parallel zone_create() doesn't try to create the 29800Sstevel@tonic-gate * same zone. 29810Sstevel@tonic-gate */ 29820Sstevel@tonic-gate zonecount++; 29830Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, 29840Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id, 29850Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 29860Sstevel@tonic-gate str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 29870Sstevel@tonic-gate (void) strcpy(str, zone->zone_name); 29880Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 29890Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 29901769Scarlsonj if (insert_label_hash) { 29911676Sjpk (void) mod_hash_insert(zonehashbylabel, 29921676Sjpk (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone); 29931769Scarlsonj zone->zone_flags |= ZF_HASHED_LABEL; 29941676Sjpk } 29951676Sjpk 29960Sstevel@tonic-gate /* 29970Sstevel@tonic-gate * Insert into active list. At this point there are no 'hold's 29980Sstevel@tonic-gate * on the zone, but everyone else knows not to use it, so we can 29990Sstevel@tonic-gate * continue to use it. zsched() will do a zone_hold() if the 30000Sstevel@tonic-gate * newproc() is successful. 30010Sstevel@tonic-gate */ 30020Sstevel@tonic-gate list_insert_tail(&zone_active, zone); 30030Sstevel@tonic-gate mutex_exit(&zonehash_lock); 30040Sstevel@tonic-gate 30050Sstevel@tonic-gate zarg.zone = zone; 30060Sstevel@tonic-gate zarg.nvlist = rctls; 30070Sstevel@tonic-gate /* 30080Sstevel@tonic-gate * The process, task, and project rctls are probably wrong; 30090Sstevel@tonic-gate * we need an interface to get the default values of all rctls, 30100Sstevel@tonic-gate * and initialize zsched appropriately. I'm not sure that that 30110Sstevel@tonic-gate * makes much of a difference, though. 30120Sstevel@tonic-gate */ 30130Sstevel@tonic-gate if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) { 30140Sstevel@tonic-gate /* 30150Sstevel@tonic-gate * We need to undo all globally visible state. 30160Sstevel@tonic-gate */ 30170Sstevel@tonic-gate mutex_enter(&zonehash_lock); 30180Sstevel@tonic-gate list_remove(&zone_active, zone); 30191769Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) { 30201676Sjpk ASSERT(zone->zone_slabel != NULL); 30211676Sjpk (void) mod_hash_destroy(zonehashbylabel, 30221676Sjpk (mod_hash_key_t)zone->zone_slabel); 30231676Sjpk } 30240Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 30250Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_name); 30260Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 30270Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 30280Sstevel@tonic-gate ASSERT(zonecount > 1); 30290Sstevel@tonic-gate zonecount--; 30300Sstevel@tonic-gate goto errout; 30310Sstevel@tonic-gate } 30320Sstevel@tonic-gate 30330Sstevel@tonic-gate /* 30340Sstevel@tonic-gate * Zone creation can't fail from now on. 30350Sstevel@tonic-gate */ 30360Sstevel@tonic-gate 30370Sstevel@tonic-gate /* 30380Sstevel@tonic-gate * Let the other lwps continue. 30390Sstevel@tonic-gate */ 30400Sstevel@tonic-gate mutex_enter(&pp->p_lock); 30410Sstevel@tonic-gate if (curthread != pp->p_agenttp) 30420Sstevel@tonic-gate continuelwps(pp); 30430Sstevel@tonic-gate mutex_exit(&pp->p_lock); 30440Sstevel@tonic-gate 30450Sstevel@tonic-gate /* 30460Sstevel@tonic-gate * Wait for zsched to finish initializing the zone. 30470Sstevel@tonic-gate */ 30480Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_READY); 30490Sstevel@tonic-gate /* 30500Sstevel@tonic-gate * The zone is fully visible, so we can let mounts progress. 30510Sstevel@tonic-gate */ 30520Sstevel@tonic-gate resume_mounts(); 30530Sstevel@tonic-gate if (rctls) 30540Sstevel@tonic-gate nvlist_free(rctls); 30550Sstevel@tonic-gate 30560Sstevel@tonic-gate return (zoneid); 30570Sstevel@tonic-gate 30580Sstevel@tonic-gate errout: 30590Sstevel@tonic-gate mutex_exit(&zonehash_lock); 30600Sstevel@tonic-gate /* 30610Sstevel@tonic-gate * Let the other lwps continue. 30620Sstevel@tonic-gate */ 30630Sstevel@tonic-gate mutex_enter(&pp->p_lock); 30640Sstevel@tonic-gate if (curthread != pp->p_agenttp) 30650Sstevel@tonic-gate continuelwps(pp); 30660Sstevel@tonic-gate mutex_exit(&pp->p_lock); 30670Sstevel@tonic-gate 30680Sstevel@tonic-gate resume_mounts(); 30690Sstevel@tonic-gate if (rctls) 30700Sstevel@tonic-gate nvlist_free(rctls); 30710Sstevel@tonic-gate /* 30720Sstevel@tonic-gate * There is currently one reference to the zone, a cred_ref from 30730Sstevel@tonic-gate * zone_kcred. To free the zone, we call crfree, which will call 30740Sstevel@tonic-gate * zone_cred_rele, which will call zone_free. 30750Sstevel@tonic-gate */ 30760Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 30770Sstevel@tonic-gate ASSERT(zone->zone_kcred->cr_ref == 1); 30780Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 30790Sstevel@tonic-gate zkcr = zone->zone_kcred; 30800Sstevel@tonic-gate zone->zone_kcred = NULL; 30810Sstevel@tonic-gate crfree(zkcr); /* triggers call to zone_free */ 30820Sstevel@tonic-gate return (zone_create_error(error, error2, extended_error)); 30830Sstevel@tonic-gate } 30840Sstevel@tonic-gate 30850Sstevel@tonic-gate /* 30860Sstevel@tonic-gate * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 3087*2267Sdp * the heavy lifting. initname is the path to the program to launch 3088*2267Sdp * at the "top" of the zone; if this is NULL, we use the system default, 3089*2267Sdp * which is stored at zone_default_initname. 30900Sstevel@tonic-gate */ 30910Sstevel@tonic-gate static int 3092*2267Sdp zone_boot(zoneid_t zoneid) 30930Sstevel@tonic-gate { 30940Sstevel@tonic-gate int err; 30950Sstevel@tonic-gate zone_t *zone; 30960Sstevel@tonic-gate 30970Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 30980Sstevel@tonic-gate return (set_errno(EPERM)); 30990Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 31000Sstevel@tonic-gate return (set_errno(EINVAL)); 31010Sstevel@tonic-gate 31020Sstevel@tonic-gate mutex_enter(&zonehash_lock); 31030Sstevel@tonic-gate /* 31040Sstevel@tonic-gate * Look for zone under hash lock to prevent races with calls to 31050Sstevel@tonic-gate * zone_shutdown, zone_destroy, etc. 31060Sstevel@tonic-gate */ 31070Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 31080Sstevel@tonic-gate mutex_exit(&zonehash_lock); 31090Sstevel@tonic-gate return (set_errno(EINVAL)); 31100Sstevel@tonic-gate } 31110Sstevel@tonic-gate 31120Sstevel@tonic-gate mutex_enter(&zone_status_lock); 31130Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_READY) { 31140Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31150Sstevel@tonic-gate mutex_exit(&zonehash_lock); 31160Sstevel@tonic-gate return (set_errno(EINVAL)); 31170Sstevel@tonic-gate } 31180Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_BOOTING); 31190Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31200Sstevel@tonic-gate 31210Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 31220Sstevel@tonic-gate mutex_exit(&zonehash_lock); 31230Sstevel@tonic-gate 31240Sstevel@tonic-gate if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 31250Sstevel@tonic-gate zone_rele(zone); 31260Sstevel@tonic-gate return (set_errno(EINTR)); 31270Sstevel@tonic-gate } 31280Sstevel@tonic-gate 31290Sstevel@tonic-gate /* 31300Sstevel@tonic-gate * Boot (starting init) might have failed, in which case the zone 31310Sstevel@tonic-gate * will go to the SHUTTING_DOWN state; an appropriate errno will 31320Sstevel@tonic-gate * be placed in zone->zone_boot_err, and so we return that. 31330Sstevel@tonic-gate */ 31340Sstevel@tonic-gate err = zone->zone_boot_err; 31350Sstevel@tonic-gate zone_rele(zone); 31360Sstevel@tonic-gate return (err ? set_errno(err) : 0); 31370Sstevel@tonic-gate } 31380Sstevel@tonic-gate 31390Sstevel@tonic-gate /* 31400Sstevel@tonic-gate * Kills all user processes in the zone, waiting for them all to exit 31410Sstevel@tonic-gate * before returning. 31420Sstevel@tonic-gate */ 31430Sstevel@tonic-gate static int 31440Sstevel@tonic-gate zone_empty(zone_t *zone) 31450Sstevel@tonic-gate { 31460Sstevel@tonic-gate int waitstatus; 31470Sstevel@tonic-gate 31480Sstevel@tonic-gate /* 31490Sstevel@tonic-gate * We need to drop zonehash_lock before killing all 31500Sstevel@tonic-gate * processes, otherwise we'll deadlock with zone_find_* 31510Sstevel@tonic-gate * which can be called from the exit path. 31520Sstevel@tonic-gate */ 31530Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 31540Sstevel@tonic-gate while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz, 31550Sstevel@tonic-gate ZONE_IS_EMPTY)) == -1) { 31560Sstevel@tonic-gate killall(zone->zone_id); 31570Sstevel@tonic-gate } 31580Sstevel@tonic-gate /* 31590Sstevel@tonic-gate * return EINTR if we were signaled 31600Sstevel@tonic-gate */ 31610Sstevel@tonic-gate if (waitstatus == 0) 31620Sstevel@tonic-gate return (EINTR); 31630Sstevel@tonic-gate return (0); 31640Sstevel@tonic-gate } 31650Sstevel@tonic-gate 31660Sstevel@tonic-gate /* 31671676Sjpk * This function implements the policy for zone visibility. 31681676Sjpk * 31691676Sjpk * In standard Solaris, a non-global zone can only see itself. 31701676Sjpk * 31711676Sjpk * In Trusted Extensions, a labeled zone can lookup any zone whose label 31721676Sjpk * it dominates. For this test, the label of the global zone is treated as 31731676Sjpk * admin_high so it is special-cased instead of being checked for dominance. 31741676Sjpk * 31751676Sjpk * Returns true if zone attributes are viewable, false otherwise. 31761676Sjpk */ 31771676Sjpk static boolean_t 31781676Sjpk zone_list_access(zone_t *zone) 31791676Sjpk { 31801676Sjpk 31811676Sjpk if (curproc->p_zone == global_zone || 31821676Sjpk curproc->p_zone == zone) { 31831676Sjpk return (B_TRUE); 31841769Scarlsonj } else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 31851676Sjpk bslabel_t *curproc_label; 31861676Sjpk bslabel_t *zone_label; 31871676Sjpk 31881676Sjpk curproc_label = label2bslabel(curproc->p_zone->zone_slabel); 31891676Sjpk zone_label = label2bslabel(zone->zone_slabel); 31901676Sjpk 31911676Sjpk if (zone->zone_id != GLOBAL_ZONEID && 31921676Sjpk bldominates(curproc_label, zone_label)) { 31931676Sjpk return (B_TRUE); 31941676Sjpk } else { 31951676Sjpk return (B_FALSE); 31961676Sjpk } 31971676Sjpk } else { 31981676Sjpk return (B_FALSE); 31991676Sjpk } 32001676Sjpk } 32011676Sjpk 32021676Sjpk /* 32030Sstevel@tonic-gate * Systemcall to start the zone's halt sequence. By the time this 32040Sstevel@tonic-gate * function successfully returns, all user processes and kernel threads 32050Sstevel@tonic-gate * executing in it will have exited, ZSD shutdown callbacks executed, 32060Sstevel@tonic-gate * and the zone status set to ZONE_IS_DOWN. 32070Sstevel@tonic-gate * 32080Sstevel@tonic-gate * It is possible that the call will interrupt itself if the caller is the 32090Sstevel@tonic-gate * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 32100Sstevel@tonic-gate */ 32110Sstevel@tonic-gate static int 32120Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 32130Sstevel@tonic-gate { 32140Sstevel@tonic-gate int error; 32150Sstevel@tonic-gate zone_t *zone; 32160Sstevel@tonic-gate zone_status_t status; 32170Sstevel@tonic-gate 32180Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 32190Sstevel@tonic-gate return (set_errno(EPERM)); 32200Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 32210Sstevel@tonic-gate return (set_errno(EINVAL)); 32220Sstevel@tonic-gate 32230Sstevel@tonic-gate /* 32240Sstevel@tonic-gate * Block mounts so that VFS_MOUNT() can get an accurate view of 32250Sstevel@tonic-gate * the zone's status with regards to ZONE_IS_SHUTTING down. 32260Sstevel@tonic-gate * 32270Sstevel@tonic-gate * e.g. NFS can fail the mount if it determines that the zone 32280Sstevel@tonic-gate * has already begun the shutdown sequence. 32290Sstevel@tonic-gate */ 32300Sstevel@tonic-gate if (block_mounts() == 0) 32310Sstevel@tonic-gate return (set_errno(EINTR)); 32320Sstevel@tonic-gate mutex_enter(&zonehash_lock); 32330Sstevel@tonic-gate /* 32340Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 32350Sstevel@tonic-gate * calls to zone_shutdown and zone_destroy. 32360Sstevel@tonic-gate */ 32370Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 32380Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32390Sstevel@tonic-gate resume_mounts(); 32400Sstevel@tonic-gate return (set_errno(EINVAL)); 32410Sstevel@tonic-gate } 32420Sstevel@tonic-gate mutex_enter(&zone_status_lock); 32430Sstevel@tonic-gate status = zone_status_get(zone); 32440Sstevel@tonic-gate /* 32450Sstevel@tonic-gate * Fail if the zone isn't fully initialized yet. 32460Sstevel@tonic-gate */ 32470Sstevel@tonic-gate if (status < ZONE_IS_READY) { 32480Sstevel@tonic-gate mutex_exit(&zone_status_lock); 32490Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32500Sstevel@tonic-gate resume_mounts(); 32510Sstevel@tonic-gate return (set_errno(EINVAL)); 32520Sstevel@tonic-gate } 32530Sstevel@tonic-gate /* 32540Sstevel@tonic-gate * If conditions required for zone_shutdown() to return have been met, 32550Sstevel@tonic-gate * return success. 32560Sstevel@tonic-gate */ 32570Sstevel@tonic-gate if (status >= ZONE_IS_DOWN) { 32580Sstevel@tonic-gate mutex_exit(&zone_status_lock); 32590Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32600Sstevel@tonic-gate resume_mounts(); 32610Sstevel@tonic-gate return (0); 32620Sstevel@tonic-gate } 32630Sstevel@tonic-gate /* 32640Sstevel@tonic-gate * If zone_shutdown() hasn't been called before, go through the motions. 32650Sstevel@tonic-gate * If it has, there's nothing to do but wait for the kernel threads to 32660Sstevel@tonic-gate * drain. 32670Sstevel@tonic-gate */ 32680Sstevel@tonic-gate if (status < ZONE_IS_EMPTY) { 32690Sstevel@tonic-gate uint_t ntasks; 32700Sstevel@tonic-gate 32710Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 32720Sstevel@tonic-gate if ((ntasks = zone->zone_ntasks) != 1) { 32730Sstevel@tonic-gate /* 32740Sstevel@tonic-gate * There's still stuff running. 32750Sstevel@tonic-gate */ 32760Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 32770Sstevel@tonic-gate } 32780Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 32790Sstevel@tonic-gate if (ntasks == 1) { 32800Sstevel@tonic-gate /* 32810Sstevel@tonic-gate * The only way to create another task is through 32820Sstevel@tonic-gate * zone_enter(), which will block until we drop 32830Sstevel@tonic-gate * zonehash_lock. The zone is empty. 32840Sstevel@tonic-gate */ 32850Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 32860Sstevel@tonic-gate /* 32870Sstevel@tonic-gate * Skip ahead to ZONE_IS_DOWN 32880Sstevel@tonic-gate */ 32890Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 32900Sstevel@tonic-gate } else { 32910Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 32920Sstevel@tonic-gate } 32930Sstevel@tonic-gate } 32940Sstevel@tonic-gate } 32950Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 32960Sstevel@tonic-gate mutex_exit(&zone_status_lock); 32970Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32980Sstevel@tonic-gate resume_mounts(); 32990Sstevel@tonic-gate 33000Sstevel@tonic-gate if (error = zone_empty(zone)) { 33010Sstevel@tonic-gate zone_rele(zone); 33020Sstevel@tonic-gate return (set_errno(error)); 33030Sstevel@tonic-gate } 33040Sstevel@tonic-gate /* 33050Sstevel@tonic-gate * After the zone status goes to ZONE_IS_DOWN this zone will no 33060Sstevel@tonic-gate * longer be notified of changes to the pools configuration, so 33070Sstevel@tonic-gate * in order to not end up with a stale pool pointer, we point 33080Sstevel@tonic-gate * ourselves at the default pool and remove all resource 33090Sstevel@tonic-gate * visibility. This is especially important as the zone_t may 33100Sstevel@tonic-gate * languish on the deathrow for a very long time waiting for 33110Sstevel@tonic-gate * cred's to drain out. 33120Sstevel@tonic-gate * 33130Sstevel@tonic-gate * This rebinding of the zone can happen multiple times 33140Sstevel@tonic-gate * (presumably due to interrupted or parallel systemcalls) 33150Sstevel@tonic-gate * without any adverse effects. 33160Sstevel@tonic-gate */ 33170Sstevel@tonic-gate if (pool_lock_intr() != 0) { 33180Sstevel@tonic-gate zone_rele(zone); 33190Sstevel@tonic-gate return (set_errno(EINTR)); 33200Sstevel@tonic-gate } 33210Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 33220Sstevel@tonic-gate mutex_enter(&cpu_lock); 33230Sstevel@tonic-gate zone_pool_set(zone, pool_default); 33240Sstevel@tonic-gate /* 33250Sstevel@tonic-gate * The zone no longer needs to be able to see any cpus. 33260Sstevel@tonic-gate */ 33270Sstevel@tonic-gate zone_pset_set(zone, ZONE_PS_INVAL); 33280Sstevel@tonic-gate mutex_exit(&cpu_lock); 33290Sstevel@tonic-gate } 33300Sstevel@tonic-gate pool_unlock(); 33310Sstevel@tonic-gate 33320Sstevel@tonic-gate /* 33330Sstevel@tonic-gate * ZSD shutdown callbacks can be executed multiple times, hence 33340Sstevel@tonic-gate * it is safe to not be holding any locks across this call. 33350Sstevel@tonic-gate */ 33360Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 33370Sstevel@tonic-gate 33380Sstevel@tonic-gate mutex_enter(&zone_status_lock); 33390Sstevel@tonic-gate if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 33400Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 33410Sstevel@tonic-gate mutex_exit(&zone_status_lock); 33420Sstevel@tonic-gate 33430Sstevel@tonic-gate /* 33440Sstevel@tonic-gate * Wait for kernel threads to drain. 33450Sstevel@tonic-gate */ 33460Sstevel@tonic-gate if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 33470Sstevel@tonic-gate zone_rele(zone); 33480Sstevel@tonic-gate return (set_errno(EINTR)); 33490Sstevel@tonic-gate } 33500Sstevel@tonic-gate zone_rele(zone); 33510Sstevel@tonic-gate return (0); 33520Sstevel@tonic-gate } 33530Sstevel@tonic-gate 33540Sstevel@tonic-gate /* 33550Sstevel@tonic-gate * Systemcall entry point to finalize the zone halt process. The caller 33560Sstevel@tonic-gate * must have already successfully callefd zone_shutdown(). 33570Sstevel@tonic-gate * 33580Sstevel@tonic-gate * Upon successful completion, the zone will have been fully destroyed: 33590Sstevel@tonic-gate * zsched will have exited, destructor callbacks executed, and the zone 33600Sstevel@tonic-gate * removed from the list of active zones. 33610Sstevel@tonic-gate */ 33620Sstevel@tonic-gate static int 33630Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 33640Sstevel@tonic-gate { 33650Sstevel@tonic-gate uint64_t uniqid; 33660Sstevel@tonic-gate zone_t *zone; 33670Sstevel@tonic-gate zone_status_t status; 33680Sstevel@tonic-gate 33690Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 33700Sstevel@tonic-gate return (set_errno(EPERM)); 33710Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 33720Sstevel@tonic-gate return (set_errno(EINVAL)); 33730Sstevel@tonic-gate 33740Sstevel@tonic-gate mutex_enter(&zonehash_lock); 33750Sstevel@tonic-gate /* 33760Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 33770Sstevel@tonic-gate * calls to zone_destroy. 33780Sstevel@tonic-gate */ 33790Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 33800Sstevel@tonic-gate mutex_exit(&zonehash_lock); 33810Sstevel@tonic-gate return (set_errno(EINVAL)); 33820Sstevel@tonic-gate } 33830Sstevel@tonic-gate 33840Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 33850Sstevel@tonic-gate mutex_exit(&zonehash_lock); 33860Sstevel@tonic-gate return (set_errno(EBUSY)); 33870Sstevel@tonic-gate } 33880Sstevel@tonic-gate mutex_enter(&zone_status_lock); 33890Sstevel@tonic-gate status = zone_status_get(zone); 33900Sstevel@tonic-gate if (status < ZONE_IS_DOWN) { 33910Sstevel@tonic-gate mutex_exit(&zone_status_lock); 33920Sstevel@tonic-gate mutex_exit(&zonehash_lock); 33930Sstevel@tonic-gate return (set_errno(EBUSY)); 33940Sstevel@tonic-gate } else if (status == ZONE_IS_DOWN) { 33950Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 33960Sstevel@tonic-gate } 33970Sstevel@tonic-gate mutex_exit(&zone_status_lock); 33980Sstevel@tonic-gate zone_hold(zone); 33990Sstevel@tonic-gate mutex_exit(&zonehash_lock); 34000Sstevel@tonic-gate 34010Sstevel@tonic-gate /* 34020Sstevel@tonic-gate * wait for zsched to exit 34030Sstevel@tonic-gate */ 34040Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_DEAD); 34050Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_DESTROY); 34060Sstevel@tonic-gate uniqid = zone->zone_uniqid; 34070Sstevel@tonic-gate zone_rele(zone); 34080Sstevel@tonic-gate zone = NULL; /* potentially free'd */ 34090Sstevel@tonic-gate 34100Sstevel@tonic-gate mutex_enter(&zonehash_lock); 34110Sstevel@tonic-gate for (; /* ever */; ) { 34120Sstevel@tonic-gate boolean_t unref; 34130Sstevel@tonic-gate 34140Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL || 34150Sstevel@tonic-gate zone->zone_uniqid != uniqid) { 34160Sstevel@tonic-gate /* 34170Sstevel@tonic-gate * The zone has gone away. Necessary conditions 34180Sstevel@tonic-gate * are met, so we return success. 34190Sstevel@tonic-gate */ 34200Sstevel@tonic-gate mutex_exit(&zonehash_lock); 34210Sstevel@tonic-gate return (0); 34220Sstevel@tonic-gate } 34230Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 34240Sstevel@tonic-gate unref = ZONE_IS_UNREF(zone); 34250Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 34260Sstevel@tonic-gate if (unref) { 34270Sstevel@tonic-gate /* 34280Sstevel@tonic-gate * There is only one reference to the zone -- that 34290Sstevel@tonic-gate * added when the zone was added to the hashtables -- 34300Sstevel@tonic-gate * and things will remain this way until we drop 34310Sstevel@tonic-gate * zonehash_lock... we can go ahead and cleanup the 34320Sstevel@tonic-gate * zone. 34330Sstevel@tonic-gate */ 34340Sstevel@tonic-gate break; 34350Sstevel@tonic-gate } 34360Sstevel@tonic-gate 34370Sstevel@tonic-gate if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 34380Sstevel@tonic-gate /* Signaled */ 34390Sstevel@tonic-gate mutex_exit(&zonehash_lock); 34400Sstevel@tonic-gate return (set_errno(EINTR)); 34410Sstevel@tonic-gate } 34420Sstevel@tonic-gate 34430Sstevel@tonic-gate } 34440Sstevel@tonic-gate 34450Sstevel@tonic-gate /* 34460Sstevel@tonic-gate * It is now safe to let the zone be recreated; remove it from the 34470Sstevel@tonic-gate * lists. The memory will not be freed until the last cred 34480Sstevel@tonic-gate * reference goes away. 34490Sstevel@tonic-gate */ 34500Sstevel@tonic-gate ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 34510Sstevel@tonic-gate zonecount--; 34520Sstevel@tonic-gate /* remove from active list and hash tables */ 34530Sstevel@tonic-gate list_remove(&zone_active, zone); 34540Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 34550Sstevel@tonic-gate (mod_hash_key_t)zone->zone_name); 34560Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 34570Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 34581769Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) 34591676Sjpk (void) mod_hash_destroy(zonehashbylabel, 34601676Sjpk (mod_hash_key_t)zone->zone_slabel); 34610Sstevel@tonic-gate mutex_exit(&zonehash_lock); 34620Sstevel@tonic-gate 3463766Scarlsonj /* 3464766Scarlsonj * Release the root vnode; we're not using it anymore. Nor should any 3465766Scarlsonj * other thread that might access it exist. 3466766Scarlsonj */ 3467766Scarlsonj if (zone->zone_rootvp != NULL) { 3468766Scarlsonj VN_RELE(zone->zone_rootvp); 3469766Scarlsonj zone->zone_rootvp = NULL; 3470766Scarlsonj } 3471766Scarlsonj 34720Sstevel@tonic-gate /* add to deathrow list */ 34730Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 34740Sstevel@tonic-gate list_insert_tail(&zone_deathrow, zone); 34750Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 34760Sstevel@tonic-gate 34770Sstevel@tonic-gate /* 34780Sstevel@tonic-gate * Drop last reference (which was added by zsched()), this will 34790Sstevel@tonic-gate * free the zone unless there are outstanding cred references. 34800Sstevel@tonic-gate */ 34810Sstevel@tonic-gate zone_rele(zone); 34820Sstevel@tonic-gate return (0); 34830Sstevel@tonic-gate } 34840Sstevel@tonic-gate 34850Sstevel@tonic-gate /* 34860Sstevel@tonic-gate * Systemcall entry point for zone_getattr(2). 34870Sstevel@tonic-gate */ 34880Sstevel@tonic-gate static ssize_t 34890Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 34900Sstevel@tonic-gate { 34910Sstevel@tonic-gate size_t size; 34920Sstevel@tonic-gate int error = 0, err; 34930Sstevel@tonic-gate zone_t *zone; 34940Sstevel@tonic-gate char *zonepath; 3495*2267Sdp char *outstr; 34960Sstevel@tonic-gate zone_status_t zone_status; 34970Sstevel@tonic-gate pid_t initpid; 34980Sstevel@tonic-gate boolean_t global = (curproc->p_zone == global_zone); 34991676Sjpk boolean_t curzone = (curproc->p_zone->zone_id == zoneid); 35000Sstevel@tonic-gate 35010Sstevel@tonic-gate mutex_enter(&zonehash_lock); 35020Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 35030Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35040Sstevel@tonic-gate return (set_errno(EINVAL)); 35050Sstevel@tonic-gate } 35060Sstevel@tonic-gate zone_status = zone_status_get(zone); 35070Sstevel@tonic-gate if (zone_status < ZONE_IS_READY) { 35080Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35090Sstevel@tonic-gate return (set_errno(EINVAL)); 35100Sstevel@tonic-gate } 35110Sstevel@tonic-gate zone_hold(zone); 35120Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35130Sstevel@tonic-gate 35140Sstevel@tonic-gate /* 35151676Sjpk * If not in the global zone, don't show information about other zones, 35161676Sjpk * unless the system is labeled and the local zone's label dominates 35171676Sjpk * the other zone. 35180Sstevel@tonic-gate */ 35191676Sjpk if (!zone_list_access(zone)) { 35200Sstevel@tonic-gate zone_rele(zone); 35210Sstevel@tonic-gate return (set_errno(EINVAL)); 35220Sstevel@tonic-gate } 35230Sstevel@tonic-gate 35240Sstevel@tonic-gate switch (attr) { 35250Sstevel@tonic-gate case ZONE_ATTR_ROOT: 35260Sstevel@tonic-gate if (global) { 35270Sstevel@tonic-gate /* 35280Sstevel@tonic-gate * Copy the path to trim the trailing "/" (except for 35290Sstevel@tonic-gate * the global zone). 35300Sstevel@tonic-gate */ 35310Sstevel@tonic-gate if (zone != global_zone) 35320Sstevel@tonic-gate size = zone->zone_rootpathlen - 1; 35330Sstevel@tonic-gate else 35340Sstevel@tonic-gate size = zone->zone_rootpathlen; 35350Sstevel@tonic-gate zonepath = kmem_alloc(size, KM_SLEEP); 35360Sstevel@tonic-gate bcopy(zone->zone_rootpath, zonepath, size); 35370Sstevel@tonic-gate zonepath[size - 1] = '\0'; 35380Sstevel@tonic-gate } else { 35391676Sjpk if (curzone || !is_system_labeled()) { 35401676Sjpk /* 35411676Sjpk * Caller is not in the global zone. 35421676Sjpk * if the query is on the current zone 35431676Sjpk * or the system is not labeled, 35441676Sjpk * just return faked-up path for current zone. 35451676Sjpk */ 35461676Sjpk zonepath = "/"; 35471676Sjpk size = 2; 35481676Sjpk } else { 35491676Sjpk /* 35501676Sjpk * Return related path for current zone. 35511676Sjpk */ 35521676Sjpk int prefix_len = strlen(zone_prefix); 35531676Sjpk int zname_len = strlen(zone->zone_name); 35541676Sjpk 35551676Sjpk size = prefix_len + zname_len + 1; 35561676Sjpk zonepath = kmem_alloc(size, KM_SLEEP); 35571676Sjpk bcopy(zone_prefix, zonepath, prefix_len); 35581676Sjpk bcopy(zone->zone_name, zonepath + 3559*2267Sdp prefix_len, zname_len); 35601676Sjpk zonepath[size - 1] = '\0'; 35611676Sjpk } 35620Sstevel@tonic-gate } 35630Sstevel@tonic-gate if (bufsize > size) 35640Sstevel@tonic-gate bufsize = size; 35650Sstevel@tonic-gate if (buf != NULL) { 35660Sstevel@tonic-gate err = copyoutstr(zonepath, buf, bufsize, NULL); 35670Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 35680Sstevel@tonic-gate error = EFAULT; 35690Sstevel@tonic-gate } 35701676Sjpk if (global || (is_system_labeled() && !curzone)) 35710Sstevel@tonic-gate kmem_free(zonepath, size); 35720Sstevel@tonic-gate break; 35730Sstevel@tonic-gate 35740Sstevel@tonic-gate case ZONE_ATTR_NAME: 35750Sstevel@tonic-gate size = strlen(zone->zone_name) + 1; 35760Sstevel@tonic-gate if (bufsize > size) 35770Sstevel@tonic-gate bufsize = size; 35780Sstevel@tonic-gate if (buf != NULL) { 35790Sstevel@tonic-gate err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 35800Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 35810Sstevel@tonic-gate error = EFAULT; 35820Sstevel@tonic-gate } 35830Sstevel@tonic-gate break; 35840Sstevel@tonic-gate 35850Sstevel@tonic-gate case ZONE_ATTR_STATUS: 35860Sstevel@tonic-gate /* 35870Sstevel@tonic-gate * Since we're not holding zonehash_lock, the zone status 35880Sstevel@tonic-gate * may be anything; leave it up to userland to sort it out. 35890Sstevel@tonic-gate */ 35900Sstevel@tonic-gate size = sizeof (zone_status); 35910Sstevel@tonic-gate if (bufsize > size) 35920Sstevel@tonic-gate bufsize = size; 35930Sstevel@tonic-gate zone_status = zone_status_get(zone); 35940Sstevel@tonic-gate if (buf != NULL && 35950Sstevel@tonic-gate copyout(&zone_status, buf, bufsize) != 0) 35960Sstevel@tonic-gate error = EFAULT; 35970Sstevel@tonic-gate break; 35980Sstevel@tonic-gate case ZONE_ATTR_PRIVSET: 35990Sstevel@tonic-gate size = sizeof (priv_set_t); 36000Sstevel@tonic-gate if (bufsize > size) 36010Sstevel@tonic-gate bufsize = size; 36020Sstevel@tonic-gate if (buf != NULL && 36030Sstevel@tonic-gate copyout(zone->zone_privset, buf, bufsize) != 0) 36040Sstevel@tonic-gate error = EFAULT; 36050Sstevel@tonic-gate break; 36060Sstevel@tonic-gate case ZONE_ATTR_UNIQID: 36070Sstevel@tonic-gate size = sizeof (zone->zone_uniqid); 36080Sstevel@tonic-gate if (bufsize > size) 36090Sstevel@tonic-gate bufsize = size; 36100Sstevel@tonic-gate if (buf != NULL && 36110Sstevel@tonic-gate copyout(&zone->zone_uniqid, buf, bufsize) != 0) 36120Sstevel@tonic-gate error = EFAULT; 36130Sstevel@tonic-gate break; 36140Sstevel@tonic-gate case ZONE_ATTR_POOLID: 36150Sstevel@tonic-gate { 36160Sstevel@tonic-gate pool_t *pool; 36170Sstevel@tonic-gate poolid_t poolid; 36180Sstevel@tonic-gate 36190Sstevel@tonic-gate if (pool_lock_intr() != 0) { 36200Sstevel@tonic-gate error = EINTR; 36210Sstevel@tonic-gate break; 36220Sstevel@tonic-gate } 36230Sstevel@tonic-gate pool = zone_pool_get(zone); 36240Sstevel@tonic-gate poolid = pool->pool_id; 36250Sstevel@tonic-gate pool_unlock(); 36260Sstevel@tonic-gate size = sizeof (poolid); 36270Sstevel@tonic-gate if (bufsize > size) 36280Sstevel@tonic-gate bufsize = size; 36290Sstevel@tonic-gate if (buf != NULL && copyout(&poolid, buf, size) != 0) 36300Sstevel@tonic-gate error = EFAULT; 36310Sstevel@tonic-gate } 36320Sstevel@tonic-gate break; 36331676Sjpk case ZONE_ATTR_SLBL: 36341676Sjpk size = sizeof (bslabel_t); 36351676Sjpk if (bufsize > size) 36361676Sjpk bufsize = size; 36371676Sjpk if (zone->zone_slabel == NULL) 36381676Sjpk error = EINVAL; 36391676Sjpk else if (buf != NULL && 36401676Sjpk copyout(label2bslabel(zone->zone_slabel), buf, 36411676Sjpk bufsize) != 0) 36421676Sjpk error = EFAULT; 36431676Sjpk break; 36440Sstevel@tonic-gate case ZONE_ATTR_INITPID: 36450Sstevel@tonic-gate size = sizeof (initpid); 36460Sstevel@tonic-gate if (bufsize > size) 36470Sstevel@tonic-gate bufsize = size; 36480Sstevel@tonic-gate initpid = zone->zone_proc_initpid; 36490Sstevel@tonic-gate if (initpid == -1) { 36500Sstevel@tonic-gate error = ESRCH; 36510Sstevel@tonic-gate break; 36520Sstevel@tonic-gate } 36530Sstevel@tonic-gate if (buf != NULL && 36540Sstevel@tonic-gate copyout(&initpid, buf, bufsize) != 0) 36550Sstevel@tonic-gate error = EFAULT; 36560Sstevel@tonic-gate break; 3657*2267Sdp case ZONE_ATTR_INITNAME: 3658*2267Sdp size = strlen(zone->zone_initname) + 1; 3659*2267Sdp if (bufsize > size) 3660*2267Sdp bufsize = size; 3661*2267Sdp if (buf != NULL) { 3662*2267Sdp err = copyoutstr(zone->zone_initname, buf, bufsize, 3663*2267Sdp NULL); 3664*2267Sdp if (err != 0 && err != ENAMETOOLONG) 3665*2267Sdp error = EFAULT; 3666*2267Sdp } 3667*2267Sdp break; 3668*2267Sdp case ZONE_ATTR_BOOTARGS: 3669*2267Sdp if (zone->zone_bootargs == NULL) 3670*2267Sdp outstr = ""; 3671*2267Sdp else 3672*2267Sdp outstr = zone->zone_bootargs; 3673*2267Sdp size = strlen(outstr) + 1; 3674*2267Sdp if (bufsize > size) 3675*2267Sdp bufsize = size; 3676*2267Sdp if (buf != NULL) { 3677*2267Sdp err = copyoutstr(outstr, buf, bufsize, NULL); 3678*2267Sdp if (err != 0 && err != ENAMETOOLONG) 3679*2267Sdp error = EFAULT; 3680*2267Sdp } 3681*2267Sdp break; 36820Sstevel@tonic-gate default: 36830Sstevel@tonic-gate error = EINVAL; 36840Sstevel@tonic-gate } 36850Sstevel@tonic-gate zone_rele(zone); 36860Sstevel@tonic-gate 36870Sstevel@tonic-gate if (error) 36880Sstevel@tonic-gate return (set_errno(error)); 36890Sstevel@tonic-gate return ((ssize_t)size); 36900Sstevel@tonic-gate } 36910Sstevel@tonic-gate 36920Sstevel@tonic-gate /* 3693*2267Sdp * Systemcall entry point for zone_setattr(2). 3694*2267Sdp */ 3695*2267Sdp /*ARGSUSED*/ 3696*2267Sdp static int 3697*2267Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 3698*2267Sdp { 3699*2267Sdp zone_t *zone; 3700*2267Sdp zone_status_t zone_status; 3701*2267Sdp int err; 3702*2267Sdp 3703*2267Sdp if (secpolicy_zone_config(CRED()) != 0) 3704*2267Sdp return (set_errno(EPERM)); 3705*2267Sdp 3706*2267Sdp /* 3707*2267Sdp * At present, attributes can only be set on non-running, 3708*2267Sdp * non-global zones. 3709*2267Sdp */ 3710*2267Sdp if (zoneid == GLOBAL_ZONEID) { 3711*2267Sdp return (set_errno(EINVAL)); 3712*2267Sdp } 3713*2267Sdp 3714*2267Sdp mutex_enter(&zonehash_lock); 3715*2267Sdp if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 3716*2267Sdp mutex_exit(&zonehash_lock); 3717*2267Sdp return (set_errno(EINVAL)); 3718*2267Sdp } 3719*2267Sdp zone_hold(zone); 3720*2267Sdp mutex_exit(&zonehash_lock); 3721*2267Sdp 3722*2267Sdp zone_status = zone_status_get(zone); 3723*2267Sdp if (zone_status > ZONE_IS_READY) 3724*2267Sdp goto done; 3725*2267Sdp 3726*2267Sdp switch (attr) { 3727*2267Sdp case ZONE_ATTR_INITNAME: 3728*2267Sdp err = zone_set_initname(zone, (const char *)buf); 3729*2267Sdp break; 3730*2267Sdp case ZONE_ATTR_BOOTARGS: 3731*2267Sdp err = zone_set_bootargs(zone, (const char *)buf); 3732*2267Sdp break; 3733*2267Sdp default: 3734*2267Sdp err = EINVAL; 3735*2267Sdp } 3736*2267Sdp 3737*2267Sdp done: 3738*2267Sdp zone_rele(zone); 3739*2267Sdp return (err != 0 ? set_errno(err) : 0); 3740*2267Sdp } 3741*2267Sdp 3742*2267Sdp /* 37430Sstevel@tonic-gate * Return zero if the process has at least one vnode mapped in to its 37440Sstevel@tonic-gate * address space which shouldn't be allowed to change zones. 37450Sstevel@tonic-gate */ 37460Sstevel@tonic-gate static int 37470Sstevel@tonic-gate as_can_change_zones(void) 37480Sstevel@tonic-gate { 37490Sstevel@tonic-gate proc_t *pp = curproc; 37500Sstevel@tonic-gate struct seg *seg; 37510Sstevel@tonic-gate struct as *as = pp->p_as; 37520Sstevel@tonic-gate vnode_t *vp; 37530Sstevel@tonic-gate int allow = 1; 37540Sstevel@tonic-gate 37550Sstevel@tonic-gate ASSERT(pp->p_as != &kas); 37560Sstevel@tonic-gate AS_LOCK_ENTER(&as, &as->a_lock, RW_READER); 37570Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 37580Sstevel@tonic-gate /* 37590Sstevel@tonic-gate * if we can't get a backing vnode for this segment then skip 37600Sstevel@tonic-gate * it. 37610Sstevel@tonic-gate */ 37620Sstevel@tonic-gate vp = NULL; 37630Sstevel@tonic-gate if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 37640Sstevel@tonic-gate continue; 37650Sstevel@tonic-gate if (!vn_can_change_zones(vp)) { /* bail on first match */ 37660Sstevel@tonic-gate allow = 0; 37670Sstevel@tonic-gate break; 37680Sstevel@tonic-gate } 37690Sstevel@tonic-gate } 37700Sstevel@tonic-gate AS_LOCK_EXIT(&as, &as->a_lock); 37710Sstevel@tonic-gate return (allow); 37720Sstevel@tonic-gate } 37730Sstevel@tonic-gate 37740Sstevel@tonic-gate /* 37750Sstevel@tonic-gate * Systemcall entry point for zone_enter(). 37760Sstevel@tonic-gate * 37770Sstevel@tonic-gate * The current process is injected into said zone. In the process 37780Sstevel@tonic-gate * it will change its project membership, privileges, rootdir/cwd, 37790Sstevel@tonic-gate * zone-wide rctls, and pool association to match those of the zone. 37800Sstevel@tonic-gate * 37810Sstevel@tonic-gate * The first zone_enter() called while the zone is in the ZONE_IS_READY 37820Sstevel@tonic-gate * state will transition it to ZONE_IS_RUNNING. Processes may only 37830Sstevel@tonic-gate * enter a zone that is "ready" or "running". 37840Sstevel@tonic-gate */ 37850Sstevel@tonic-gate static int 37860Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 37870Sstevel@tonic-gate { 37880Sstevel@tonic-gate zone_t *zone; 37890Sstevel@tonic-gate vnode_t *vp; 37900Sstevel@tonic-gate proc_t *pp = curproc; 37910Sstevel@tonic-gate contract_t *ct; 37920Sstevel@tonic-gate cont_process_t *ctp; 37930Sstevel@tonic-gate task_t *tk, *oldtk; 37940Sstevel@tonic-gate kproject_t *zone_proj0; 37950Sstevel@tonic-gate cred_t *cr, *newcr; 37960Sstevel@tonic-gate pool_t *oldpool, *newpool; 37970Sstevel@tonic-gate sess_t *sp; 37980Sstevel@tonic-gate uid_t uid; 37990Sstevel@tonic-gate zone_status_t status; 38000Sstevel@tonic-gate int err = 0; 38010Sstevel@tonic-gate rctl_entity_p_t e; 38020Sstevel@tonic-gate 38030Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 38040Sstevel@tonic-gate return (set_errno(EPERM)); 38050Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 38060Sstevel@tonic-gate return (set_errno(EINVAL)); 38070Sstevel@tonic-gate 38080Sstevel@tonic-gate /* 38090Sstevel@tonic-gate * Stop all lwps so we don't need to hold a lock to look at 38100Sstevel@tonic-gate * curproc->p_zone. This needs to happen before we grab any 38110Sstevel@tonic-gate * locks to avoid deadlock (another lwp in the process could 38120Sstevel@tonic-gate * be waiting for the held lock). 38130Sstevel@tonic-gate */ 38140Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 38150Sstevel@tonic-gate return (set_errno(EINTR)); 38160Sstevel@tonic-gate 38170Sstevel@tonic-gate /* 38180Sstevel@tonic-gate * Make sure we're not changing zones with files open or mapped in 38190Sstevel@tonic-gate * to our address space which shouldn't be changing zones. 38200Sstevel@tonic-gate */ 38210Sstevel@tonic-gate if (!files_can_change_zones()) { 38220Sstevel@tonic-gate err = EBADF; 38230Sstevel@tonic-gate goto out; 38240Sstevel@tonic-gate } 38250Sstevel@tonic-gate if (!as_can_change_zones()) { 38260Sstevel@tonic-gate err = EFAULT; 38270Sstevel@tonic-gate goto out; 38280Sstevel@tonic-gate } 38290Sstevel@tonic-gate 38300Sstevel@tonic-gate mutex_enter(&zonehash_lock); 38310Sstevel@tonic-gate if (pp->p_zone != global_zone) { 38320Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38330Sstevel@tonic-gate err = EINVAL; 38340Sstevel@tonic-gate goto out; 38350Sstevel@tonic-gate } 38360Sstevel@tonic-gate 38370Sstevel@tonic-gate zone = zone_find_all_by_id(zoneid); 38380Sstevel@tonic-gate if (zone == NULL) { 38390Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38400Sstevel@tonic-gate err = EINVAL; 38410Sstevel@tonic-gate goto out; 38420Sstevel@tonic-gate } 38430Sstevel@tonic-gate 38440Sstevel@tonic-gate /* 38450Sstevel@tonic-gate * To prevent processes in a zone from holding contracts on 38460Sstevel@tonic-gate * extrazonal resources, and to avoid process contract 38470Sstevel@tonic-gate * memberships which span zones, contract holders and processes 38480Sstevel@tonic-gate * which aren't the sole members of their encapsulating process 38490Sstevel@tonic-gate * contracts are not allowed to zone_enter. 38500Sstevel@tonic-gate */ 38510Sstevel@tonic-gate ctp = pp->p_ct_process; 38520Sstevel@tonic-gate ct = &ctp->conp_contract; 38530Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 38540Sstevel@tonic-gate mutex_enter(&pp->p_lock); 38550Sstevel@tonic-gate if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 38560Sstevel@tonic-gate mutex_exit(&pp->p_lock); 38570Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 38580Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38590Sstevel@tonic-gate pool_unlock(); 38600Sstevel@tonic-gate err = EINVAL; 38610Sstevel@tonic-gate goto out; 38620Sstevel@tonic-gate } 38630Sstevel@tonic-gate 38640Sstevel@tonic-gate /* 38650Sstevel@tonic-gate * Moreover, we don't allow processes whose encapsulating 38660Sstevel@tonic-gate * process contracts have inherited extrazonal contracts. 38670Sstevel@tonic-gate * While it would be easier to eliminate all process contracts 38680Sstevel@tonic-gate * with inherited contracts, we need to be able to give a 38690Sstevel@tonic-gate * restarted init (or other zone-penetrating process) its 38700Sstevel@tonic-gate * predecessor's contracts. 38710Sstevel@tonic-gate */ 38720Sstevel@tonic-gate if (ctp->conp_ninherited != 0) { 38730Sstevel@tonic-gate contract_t *next; 38740Sstevel@tonic-gate for (next = list_head(&ctp->conp_inherited); next; 38750Sstevel@tonic-gate next = list_next(&ctp->conp_inherited, next)) { 38760Sstevel@tonic-gate if (contract_getzuniqid(next) != zone->zone_uniqid) { 38770Sstevel@tonic-gate mutex_exit(&pp->p_lock); 38780Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 38790Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38800Sstevel@tonic-gate pool_unlock(); 38810Sstevel@tonic-gate err = EINVAL; 38820Sstevel@tonic-gate goto out; 38830Sstevel@tonic-gate } 38840Sstevel@tonic-gate } 38850Sstevel@tonic-gate } 38860Sstevel@tonic-gate mutex_exit(&pp->p_lock); 38870Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 38880Sstevel@tonic-gate 38890Sstevel@tonic-gate status = zone_status_get(zone); 38900Sstevel@tonic-gate if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 38910Sstevel@tonic-gate /* 38920Sstevel@tonic-gate * Can't join 38930Sstevel@tonic-gate */ 38940Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38950Sstevel@tonic-gate err = EINVAL; 38960Sstevel@tonic-gate goto out; 38970Sstevel@tonic-gate } 38980Sstevel@tonic-gate 38990Sstevel@tonic-gate /* 39000Sstevel@tonic-gate * Make sure new priv set is within the permitted set for caller 39010Sstevel@tonic-gate */ 39020Sstevel@tonic-gate if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 39030Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39040Sstevel@tonic-gate err = EPERM; 39050Sstevel@tonic-gate goto out; 39060Sstevel@tonic-gate } 39070Sstevel@tonic-gate /* 39080Sstevel@tonic-gate * We want to momentarily drop zonehash_lock while we optimistically 39090Sstevel@tonic-gate * bind curproc to the pool it should be running in. This is safe 39100Sstevel@tonic-gate * since the zone can't disappear (we have a hold on it). 39110Sstevel@tonic-gate */ 39120Sstevel@tonic-gate zone_hold(zone); 39130Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39140Sstevel@tonic-gate 39150Sstevel@tonic-gate /* 39160Sstevel@tonic-gate * Grab pool_lock to keep the pools configuration from changing 39170Sstevel@tonic-gate * and to stop ourselves from getting rebound to another pool 39180Sstevel@tonic-gate * until we join the zone. 39190Sstevel@tonic-gate */ 39200Sstevel@tonic-gate if (pool_lock_intr() != 0) { 39210Sstevel@tonic-gate zone_rele(zone); 39220Sstevel@tonic-gate err = EINTR; 39230Sstevel@tonic-gate goto out; 39240Sstevel@tonic-gate } 39250Sstevel@tonic-gate ASSERT(secpolicy_pool(CRED()) == 0); 39260Sstevel@tonic-gate /* 39270Sstevel@tonic-gate * Bind ourselves to the pool currently associated with the zone. 39280Sstevel@tonic-gate */ 39290Sstevel@tonic-gate oldpool = curproc->p_pool; 39300Sstevel@tonic-gate newpool = zone_pool_get(zone); 39310Sstevel@tonic-gate if (pool_state == POOL_ENABLED && newpool != oldpool && 39320Sstevel@tonic-gate (err = pool_do_bind(newpool, P_PID, P_MYID, 39330Sstevel@tonic-gate POOL_BIND_ALL)) != 0) { 39340Sstevel@tonic-gate pool_unlock(); 39350Sstevel@tonic-gate zone_rele(zone); 39360Sstevel@tonic-gate goto out; 39370Sstevel@tonic-gate } 39380Sstevel@tonic-gate 39390Sstevel@tonic-gate /* 39400Sstevel@tonic-gate * Grab cpu_lock now; we'll need it later when we call 39410Sstevel@tonic-gate * task_join(). 39420Sstevel@tonic-gate */ 39430Sstevel@tonic-gate mutex_enter(&cpu_lock); 39440Sstevel@tonic-gate mutex_enter(&zonehash_lock); 39450Sstevel@tonic-gate /* 39460Sstevel@tonic-gate * Make sure the zone hasn't moved on since we dropped zonehash_lock. 39470Sstevel@tonic-gate */ 39480Sstevel@tonic-gate if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 39490Sstevel@tonic-gate /* 39500Sstevel@tonic-gate * Can't join anymore. 39510Sstevel@tonic-gate */ 39520Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39530Sstevel@tonic-gate mutex_exit(&cpu_lock); 39540Sstevel@tonic-gate if (pool_state == POOL_ENABLED && 39550Sstevel@tonic-gate newpool != oldpool) 39560Sstevel@tonic-gate (void) pool_do_bind(oldpool, P_PID, P_MYID, 39570Sstevel@tonic-gate POOL_BIND_ALL); 39580Sstevel@tonic-gate pool_unlock(); 39590Sstevel@tonic-gate zone_rele(zone); 39600Sstevel@tonic-gate err = EINVAL; 39610Sstevel@tonic-gate goto out; 39620Sstevel@tonic-gate } 39630Sstevel@tonic-gate 39640Sstevel@tonic-gate mutex_enter(&pp->p_lock); 39650Sstevel@tonic-gate zone_proj0 = zone->zone_zsched->p_task->tk_proj; 39660Sstevel@tonic-gate /* verify that we do not exceed and task or lwp limits */ 39670Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 39680Sstevel@tonic-gate /* add new lwps to zone and zone's proj0 */ 39690Sstevel@tonic-gate zone_proj0->kpj_nlwps += pp->p_lwpcnt; 39700Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 39710Sstevel@tonic-gate /* add 1 task to zone's proj0 */ 39720Sstevel@tonic-gate zone_proj0->kpj_ntasks += 1; 39730Sstevel@tonic-gate mutex_exit(&pp->p_lock); 39740Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 39750Sstevel@tonic-gate 39760Sstevel@tonic-gate /* remove lwps from proc's old zone and old project */ 39770Sstevel@tonic-gate mutex_enter(&pp->p_zone->zone_nlwps_lock); 39780Sstevel@tonic-gate pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 39790Sstevel@tonic-gate pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 39800Sstevel@tonic-gate mutex_exit(&pp->p_zone->zone_nlwps_lock); 39810Sstevel@tonic-gate 39820Sstevel@tonic-gate /* 39830Sstevel@tonic-gate * Joining the zone cannot fail from now on. 39840Sstevel@tonic-gate * 39850Sstevel@tonic-gate * This means that a lot of the following code can be commonized and 39860Sstevel@tonic-gate * shared with zsched(). 39870Sstevel@tonic-gate */ 39880Sstevel@tonic-gate 39890Sstevel@tonic-gate /* 39900Sstevel@tonic-gate * Reset the encapsulating process contract's zone. 39910Sstevel@tonic-gate */ 39920Sstevel@tonic-gate ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 39930Sstevel@tonic-gate contract_setzuniqid(ct, zone->zone_uniqid); 39940Sstevel@tonic-gate 39950Sstevel@tonic-gate /* 39960Sstevel@tonic-gate * Create a new task and associate the process with the project keyed 39970Sstevel@tonic-gate * by (projid,zoneid). 39980Sstevel@tonic-gate * 39990Sstevel@tonic-gate * We might as well be in project 0; the global zone's projid doesn't 40000Sstevel@tonic-gate * make much sense in a zone anyhow. 40010Sstevel@tonic-gate * 40020Sstevel@tonic-gate * This also increments zone_ntasks, and returns with p_lock held. 40030Sstevel@tonic-gate */ 40040Sstevel@tonic-gate tk = task_create(0, zone); 40050Sstevel@tonic-gate oldtk = task_join(tk, 0); 40060Sstevel@tonic-gate mutex_exit(&cpu_lock); 40070Sstevel@tonic-gate 40080Sstevel@tonic-gate pp->p_flag |= SZONETOP; 40090Sstevel@tonic-gate pp->p_zone = zone; 40100Sstevel@tonic-gate 40110Sstevel@tonic-gate /* 40120Sstevel@tonic-gate * call RCTLOP_SET functions on this proc 40130Sstevel@tonic-gate */ 40140Sstevel@tonic-gate e.rcep_p.zone = zone; 40150Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 40160Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 40170Sstevel@tonic-gate RCD_CALLBACK); 40180Sstevel@tonic-gate mutex_exit(&pp->p_lock); 40190Sstevel@tonic-gate 40200Sstevel@tonic-gate /* 40210Sstevel@tonic-gate * We don't need to hold any of zsched's locks here; not only do we know 40220Sstevel@tonic-gate * the process and zone aren't going away, we know its session isn't 40230Sstevel@tonic-gate * changing either. 40240Sstevel@tonic-gate * 40250Sstevel@tonic-gate * By joining zsched's session here, we mimic the behavior in the 40260Sstevel@tonic-gate * global zone of init's sid being the pid of sched. We extend this 40270Sstevel@tonic-gate * to all zlogin-like zone_enter()'ing processes as well. 40280Sstevel@tonic-gate */ 40290Sstevel@tonic-gate mutex_enter(&pidlock); 40300Sstevel@tonic-gate sp = zone->zone_zsched->p_sessp; 40310Sstevel@tonic-gate SESS_HOLD(sp); 40320Sstevel@tonic-gate mutex_enter(&pp->p_lock); 40330Sstevel@tonic-gate pgexit(pp); 40340Sstevel@tonic-gate SESS_RELE(pp->p_sessp); 40350Sstevel@tonic-gate pp->p_sessp = sp; 40360Sstevel@tonic-gate pgjoin(pp, zone->zone_zsched->p_pidp); 40370Sstevel@tonic-gate mutex_exit(&pp->p_lock); 40380Sstevel@tonic-gate mutex_exit(&pidlock); 40390Sstevel@tonic-gate 40400Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40410Sstevel@tonic-gate /* 40420Sstevel@tonic-gate * We're firmly in the zone; let pools progress. 40430Sstevel@tonic-gate */ 40440Sstevel@tonic-gate pool_unlock(); 40450Sstevel@tonic-gate task_rele(oldtk); 40460Sstevel@tonic-gate /* 40470Sstevel@tonic-gate * We don't need to retain a hold on the zone since we already 40480Sstevel@tonic-gate * incremented zone_ntasks, so the zone isn't going anywhere. 40490Sstevel@tonic-gate */ 40500Sstevel@tonic-gate zone_rele(zone); 40510Sstevel@tonic-gate 40520Sstevel@tonic-gate /* 40530Sstevel@tonic-gate * Chroot 40540Sstevel@tonic-gate */ 40550Sstevel@tonic-gate vp = zone->zone_rootvp; 40560Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 40570Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 40580Sstevel@tonic-gate 40590Sstevel@tonic-gate /* 40600Sstevel@tonic-gate * Change process credentials 40610Sstevel@tonic-gate */ 40620Sstevel@tonic-gate newcr = cralloc(); 40630Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 40640Sstevel@tonic-gate cr = pp->p_cred; 40650Sstevel@tonic-gate crcopy_to(cr, newcr); 40660Sstevel@tonic-gate crsetzone(newcr, zone); 40670Sstevel@tonic-gate pp->p_cred = newcr; 40680Sstevel@tonic-gate 40690Sstevel@tonic-gate /* 40700Sstevel@tonic-gate * Restrict all process privilege sets to zone limit 40710Sstevel@tonic-gate */ 40720Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 40730Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 40740Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 40750Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 40760Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 40770Sstevel@tonic-gate crset(pp, newcr); 40780Sstevel@tonic-gate 40790Sstevel@tonic-gate /* 40800Sstevel@tonic-gate * Adjust upcount to reflect zone entry. 40810Sstevel@tonic-gate */ 40820Sstevel@tonic-gate uid = crgetruid(newcr); 40830Sstevel@tonic-gate mutex_enter(&pidlock); 40840Sstevel@tonic-gate upcount_dec(uid, GLOBAL_ZONEID); 40850Sstevel@tonic-gate upcount_inc(uid, zoneid); 40860Sstevel@tonic-gate mutex_exit(&pidlock); 40870Sstevel@tonic-gate 40880Sstevel@tonic-gate /* 40890Sstevel@tonic-gate * Set up core file path and content. 40900Sstevel@tonic-gate */ 40910Sstevel@tonic-gate set_core_defaults(); 40920Sstevel@tonic-gate 40930Sstevel@tonic-gate out: 40940Sstevel@tonic-gate /* 40950Sstevel@tonic-gate * Let the other lwps continue. 40960Sstevel@tonic-gate */ 40970Sstevel@tonic-gate mutex_enter(&pp->p_lock); 40980Sstevel@tonic-gate if (curthread != pp->p_agenttp) 40990Sstevel@tonic-gate continuelwps(pp); 41000Sstevel@tonic-gate mutex_exit(&pp->p_lock); 41010Sstevel@tonic-gate 41020Sstevel@tonic-gate return (err != 0 ? set_errno(err) : 0); 41030Sstevel@tonic-gate } 41040Sstevel@tonic-gate 41050Sstevel@tonic-gate /* 41060Sstevel@tonic-gate * Systemcall entry point for zone_list(2). 41070Sstevel@tonic-gate * 41080Sstevel@tonic-gate * Processes running in a (non-global) zone only see themselves. 41091676Sjpk * On labeled systems, they see all zones whose label they dominate. 41100Sstevel@tonic-gate */ 41110Sstevel@tonic-gate static int 41120Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones) 41130Sstevel@tonic-gate { 41140Sstevel@tonic-gate zoneid_t *zoneids; 41151769Scarlsonj zone_t *zone, *myzone; 41160Sstevel@tonic-gate uint_t user_nzones, real_nzones; 41171676Sjpk uint_t domi_nzones; 41181676Sjpk int error; 41190Sstevel@tonic-gate 41200Sstevel@tonic-gate if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 41210Sstevel@tonic-gate return (set_errno(EFAULT)); 41220Sstevel@tonic-gate 41231769Scarlsonj myzone = curproc->p_zone; 41241769Scarlsonj if (myzone != global_zone) { 41251676Sjpk bslabel_t *mybslab; 41261676Sjpk 41271676Sjpk if (!is_system_labeled()) { 41281676Sjpk /* just return current zone */ 41291676Sjpk real_nzones = domi_nzones = 1; 41301676Sjpk zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 41311769Scarlsonj zoneids[0] = myzone->zone_id; 41321676Sjpk } else { 41331676Sjpk /* return all zones that are dominated */ 41341676Sjpk mutex_enter(&zonehash_lock); 41351676Sjpk real_nzones = zonecount; 41361676Sjpk domi_nzones = 0; 41371676Sjpk if (real_nzones > 0) { 41381676Sjpk zoneids = kmem_alloc(real_nzones * 41391676Sjpk sizeof (zoneid_t), KM_SLEEP); 41401769Scarlsonj mybslab = label2bslabel(myzone->zone_slabel); 41411676Sjpk for (zone = list_head(&zone_active); 41421676Sjpk zone != NULL; 41431676Sjpk zone = list_next(&zone_active, zone)) { 41441676Sjpk if (zone->zone_id == GLOBAL_ZONEID) 41451676Sjpk continue; 41461769Scarlsonj if (zone != myzone && 41471769Scarlsonj (zone->zone_flags & ZF_IS_SCRATCH)) 41481769Scarlsonj continue; 41491769Scarlsonj /* 41501769Scarlsonj * Note that a label always dominates 41511769Scarlsonj * itself, so myzone is always included 41521769Scarlsonj * in the list. 41531769Scarlsonj */ 41541676Sjpk if (bldominates(mybslab, 41551676Sjpk label2bslabel(zone->zone_slabel))) { 41561676Sjpk zoneids[domi_nzones++] = 41571676Sjpk zone->zone_id; 41581676Sjpk } 41591676Sjpk } 41601676Sjpk } 41611676Sjpk mutex_exit(&zonehash_lock); 41621676Sjpk } 41630Sstevel@tonic-gate } else { 41640Sstevel@tonic-gate mutex_enter(&zonehash_lock); 41650Sstevel@tonic-gate real_nzones = zonecount; 41661676Sjpk domi_nzones = 0; 41671676Sjpk if (real_nzones > 0) { 41680Sstevel@tonic-gate zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 41690Sstevel@tonic-gate KM_SLEEP); 41700Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 41710Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 41721676Sjpk zoneids[domi_nzones++] = zone->zone_id; 41731676Sjpk ASSERT(domi_nzones == real_nzones); 41740Sstevel@tonic-gate } 41750Sstevel@tonic-gate mutex_exit(&zonehash_lock); 41760Sstevel@tonic-gate } 41770Sstevel@tonic-gate 41781676Sjpk /* 41791676Sjpk * If user has allocated space for fewer entries than we found, then 41801676Sjpk * return only up to his limit. Either way, tell him exactly how many 41811676Sjpk * we found. 41821676Sjpk */ 41831676Sjpk if (domi_nzones < user_nzones) 41841676Sjpk user_nzones = domi_nzones; 41851676Sjpk error = 0; 41861676Sjpk if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) { 41870Sstevel@tonic-gate error = EFAULT; 41881676Sjpk } else if (zoneidlist != NULL && user_nzones != 0) { 41890Sstevel@tonic-gate if (copyout(zoneids, zoneidlist, 41900Sstevel@tonic-gate user_nzones * sizeof (zoneid_t)) != 0) 41910Sstevel@tonic-gate error = EFAULT; 41920Sstevel@tonic-gate } 41930Sstevel@tonic-gate 41941676Sjpk if (real_nzones > 0) 41950Sstevel@tonic-gate kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 41960Sstevel@tonic-gate 41971676Sjpk if (error != 0) 41980Sstevel@tonic-gate return (set_errno(error)); 41990Sstevel@tonic-gate else 42000Sstevel@tonic-gate return (0); 42010Sstevel@tonic-gate } 42020Sstevel@tonic-gate 42030Sstevel@tonic-gate /* 42040Sstevel@tonic-gate * Systemcall entry point for zone_lookup(2). 42050Sstevel@tonic-gate * 42061676Sjpk * Non-global zones are only able to see themselves and (on labeled systems) 42071676Sjpk * the zones they dominate. 42080Sstevel@tonic-gate */ 42090Sstevel@tonic-gate static zoneid_t 42100Sstevel@tonic-gate zone_lookup(const char *zone_name) 42110Sstevel@tonic-gate { 42120Sstevel@tonic-gate char *kname; 42130Sstevel@tonic-gate zone_t *zone; 42140Sstevel@tonic-gate zoneid_t zoneid; 42150Sstevel@tonic-gate int err; 42160Sstevel@tonic-gate 42170Sstevel@tonic-gate if (zone_name == NULL) { 42180Sstevel@tonic-gate /* return caller's zone id */ 42190Sstevel@tonic-gate return (getzoneid()); 42200Sstevel@tonic-gate } 42210Sstevel@tonic-gate 42220Sstevel@tonic-gate kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 42230Sstevel@tonic-gate if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 42240Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 42250Sstevel@tonic-gate return (set_errno(err)); 42260Sstevel@tonic-gate } 42270Sstevel@tonic-gate 42280Sstevel@tonic-gate mutex_enter(&zonehash_lock); 42290Sstevel@tonic-gate zone = zone_find_all_by_name(kname); 42300Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 42311676Sjpk /* 42321676Sjpk * In a non-global zone, can only lookup global and own name. 42331676Sjpk * In Trusted Extensions zone label dominance rules apply. 42341676Sjpk */ 42351676Sjpk if (zone == NULL || 42361676Sjpk zone_status_get(zone) < ZONE_IS_READY || 42371676Sjpk !zone_list_access(zone)) { 42380Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42390Sstevel@tonic-gate return (set_errno(EINVAL)); 42401676Sjpk } else { 42411676Sjpk zoneid = zone->zone_id; 42421676Sjpk mutex_exit(&zonehash_lock); 42431676Sjpk return (zoneid); 42440Sstevel@tonic-gate } 42450Sstevel@tonic-gate } 42460Sstevel@tonic-gate 4247813Sdp static int 4248813Sdp zone_version(int *version_arg) 4249813Sdp { 4250813Sdp int version = ZONE_SYSCALL_API_VERSION; 4251813Sdp 4252813Sdp if (copyout(&version, version_arg, sizeof (int)) != 0) 4253813Sdp return (set_errno(EFAULT)); 4254813Sdp return (0); 4255813Sdp } 4256813Sdp 42570Sstevel@tonic-gate /* ARGSUSED */ 42580Sstevel@tonic-gate long 4259789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4) 42600Sstevel@tonic-gate { 42610Sstevel@tonic-gate zone_def zs; 42620Sstevel@tonic-gate 42630Sstevel@tonic-gate switch (cmd) { 42640Sstevel@tonic-gate case ZONE_CREATE: 42650Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 42660Sstevel@tonic-gate if (copyin(arg1, &zs, sizeof (zone_def))) { 42670Sstevel@tonic-gate return (set_errno(EFAULT)); 42680Sstevel@tonic-gate } 42690Sstevel@tonic-gate } else { 42700Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 42710Sstevel@tonic-gate zone_def32 zs32; 42720Sstevel@tonic-gate 42730Sstevel@tonic-gate if (copyin(arg1, &zs32, sizeof (zone_def32))) { 42740Sstevel@tonic-gate return (set_errno(EFAULT)); 42750Sstevel@tonic-gate } 42760Sstevel@tonic-gate zs.zone_name = 42770Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_name; 42780Sstevel@tonic-gate zs.zone_root = 42790Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_root; 42800Sstevel@tonic-gate zs.zone_privs = 42810Sstevel@tonic-gate (const struct priv_set *) 42820Sstevel@tonic-gate (unsigned long)zs32.zone_privs; 42831409Sdp zs.zone_privssz = zs32.zone_privssz; 42840Sstevel@tonic-gate zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 42850Sstevel@tonic-gate zs.rctlbufsz = zs32.rctlbufsz; 4286789Sahrens zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf; 4287789Sahrens zs.zfsbufsz = zs32.zfsbufsz; 42880Sstevel@tonic-gate zs.extended_error = 42890Sstevel@tonic-gate (int *)(unsigned long)zs32.extended_error; 42901676Sjpk zs.match = zs32.match; 42911676Sjpk zs.doi = zs32.doi; 42921676Sjpk zs.label = (const bslabel_t *)(uintptr_t)zs32.label; 42930Sstevel@tonic-gate #else 42940Sstevel@tonic-gate panic("get_udatamodel() returned bogus result\n"); 42950Sstevel@tonic-gate #endif 42960Sstevel@tonic-gate } 42970Sstevel@tonic-gate 42980Sstevel@tonic-gate return (zone_create(zs.zone_name, zs.zone_root, 4299813Sdp zs.zone_privs, zs.zone_privssz, 4300813Sdp (caddr_t)zs.rctlbuf, zs.rctlbufsz, 4301813Sdp (caddr_t)zs.zfsbuf, zs.zfsbufsz, 43021676Sjpk zs.extended_error, zs.match, zs.doi, 43031676Sjpk zs.label)); 43040Sstevel@tonic-gate case ZONE_BOOT: 4305*2267Sdp return (zone_boot((zoneid_t)(uintptr_t)arg1)); 43060Sstevel@tonic-gate case ZONE_DESTROY: 43070Sstevel@tonic-gate return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 43080Sstevel@tonic-gate case ZONE_GETATTR: 43090Sstevel@tonic-gate return (zone_getattr((zoneid_t)(uintptr_t)arg1, 43100Sstevel@tonic-gate (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 4311*2267Sdp case ZONE_SETATTR: 4312*2267Sdp return (zone_setattr((zoneid_t)(uintptr_t)arg1, 4313*2267Sdp (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 43140Sstevel@tonic-gate case ZONE_ENTER: 43150Sstevel@tonic-gate return (zone_enter((zoneid_t)(uintptr_t)arg1)); 43160Sstevel@tonic-gate case ZONE_LIST: 43170Sstevel@tonic-gate return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 43180Sstevel@tonic-gate case ZONE_SHUTDOWN: 43190Sstevel@tonic-gate return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 43200Sstevel@tonic-gate case ZONE_LOOKUP: 43210Sstevel@tonic-gate return (zone_lookup((const char *)arg1)); 4322813Sdp case ZONE_VERSION: 4323813Sdp return (zone_version((int *)arg1)); 43240Sstevel@tonic-gate default: 43250Sstevel@tonic-gate return (set_errno(EINVAL)); 43260Sstevel@tonic-gate } 43270Sstevel@tonic-gate } 43280Sstevel@tonic-gate 43290Sstevel@tonic-gate struct zarg { 43300Sstevel@tonic-gate zone_t *zone; 43310Sstevel@tonic-gate zone_cmd_arg_t arg; 43320Sstevel@tonic-gate }; 43330Sstevel@tonic-gate 43340Sstevel@tonic-gate static int 43350Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp) 43360Sstevel@tonic-gate { 43370Sstevel@tonic-gate char *buf; 43380Sstevel@tonic-gate size_t buflen; 43390Sstevel@tonic-gate int error; 43400Sstevel@tonic-gate 43410Sstevel@tonic-gate buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 43420Sstevel@tonic-gate buf = kmem_alloc(buflen, KM_SLEEP); 43430Sstevel@tonic-gate (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 43440Sstevel@tonic-gate error = door_ki_open(buf, doorp); 43450Sstevel@tonic-gate kmem_free(buf, buflen); 43460Sstevel@tonic-gate return (error); 43470Sstevel@tonic-gate } 43480Sstevel@tonic-gate 43490Sstevel@tonic-gate static void 43500Sstevel@tonic-gate zone_release_door(door_handle_t *doorp) 43510Sstevel@tonic-gate { 43520Sstevel@tonic-gate door_ki_rele(*doorp); 43530Sstevel@tonic-gate *doorp = NULL; 43540Sstevel@tonic-gate } 43550Sstevel@tonic-gate 43560Sstevel@tonic-gate static void 43570Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp) 43580Sstevel@tonic-gate { 43590Sstevel@tonic-gate door_handle_t door = NULL; 43600Sstevel@tonic-gate door_arg_t darg, save_arg; 43610Sstevel@tonic-gate char *zone_name; 43620Sstevel@tonic-gate size_t zone_namelen; 43630Sstevel@tonic-gate zoneid_t zoneid; 43640Sstevel@tonic-gate zone_t *zone; 43650Sstevel@tonic-gate zone_cmd_arg_t arg; 43660Sstevel@tonic-gate uint64_t uniqid; 43670Sstevel@tonic-gate size_t size; 43680Sstevel@tonic-gate int error; 43690Sstevel@tonic-gate int retry; 43700Sstevel@tonic-gate 43710Sstevel@tonic-gate zone = zargp->zone; 43720Sstevel@tonic-gate arg = zargp->arg; 43730Sstevel@tonic-gate kmem_free(zargp, sizeof (*zargp)); 43740Sstevel@tonic-gate 43750Sstevel@tonic-gate zone_namelen = strlen(zone->zone_name) + 1; 43760Sstevel@tonic-gate zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 43770Sstevel@tonic-gate bcopy(zone->zone_name, zone_name, zone_namelen); 43780Sstevel@tonic-gate zoneid = zone->zone_id; 43790Sstevel@tonic-gate uniqid = zone->zone_uniqid; 43800Sstevel@tonic-gate /* 43810Sstevel@tonic-gate * zoneadmd may be down, but at least we can empty out the zone. 43820Sstevel@tonic-gate * We can ignore the return value of zone_empty() since we're called 43830Sstevel@tonic-gate * from a kernel thread and know we won't be delivered any signals. 43840Sstevel@tonic-gate */ 43850Sstevel@tonic-gate ASSERT(curproc == &p0); 43860Sstevel@tonic-gate (void) zone_empty(zone); 43870Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 43880Sstevel@tonic-gate zone_rele(zone); 43890Sstevel@tonic-gate 43900Sstevel@tonic-gate size = sizeof (arg); 43910Sstevel@tonic-gate darg.rbuf = (char *)&arg; 43920Sstevel@tonic-gate darg.data_ptr = (char *)&arg; 43930Sstevel@tonic-gate darg.rsize = size; 43940Sstevel@tonic-gate darg.data_size = size; 43950Sstevel@tonic-gate darg.desc_ptr = NULL; 43960Sstevel@tonic-gate darg.desc_num = 0; 43970Sstevel@tonic-gate 43980Sstevel@tonic-gate save_arg = darg; 43990Sstevel@tonic-gate /* 44000Sstevel@tonic-gate * Since we're not holding a reference to the zone, any number of 44010Sstevel@tonic-gate * things can go wrong, including the zone disappearing before we get a 44020Sstevel@tonic-gate * chance to talk to zoneadmd. 44030Sstevel@tonic-gate */ 44040Sstevel@tonic-gate for (retry = 0; /* forever */; retry++) { 44050Sstevel@tonic-gate if (door == NULL && 44060Sstevel@tonic-gate (error = zone_lookup_door(zone_name, &door)) != 0) { 44070Sstevel@tonic-gate goto next; 44080Sstevel@tonic-gate } 44090Sstevel@tonic-gate ASSERT(door != NULL); 44100Sstevel@tonic-gate 44110Sstevel@tonic-gate if ((error = door_ki_upcall(door, &darg)) == 0) { 44120Sstevel@tonic-gate break; 44130Sstevel@tonic-gate } 44140Sstevel@tonic-gate switch (error) { 44150Sstevel@tonic-gate case EINTR: 44160Sstevel@tonic-gate /* FALLTHROUGH */ 44170Sstevel@tonic-gate case EAGAIN: /* process may be forking */ 44180Sstevel@tonic-gate /* 44190Sstevel@tonic-gate * Back off for a bit 44200Sstevel@tonic-gate */ 44210Sstevel@tonic-gate break; 44220Sstevel@tonic-gate case EBADF: 44230Sstevel@tonic-gate zone_release_door(&door); 44240Sstevel@tonic-gate if (zone_lookup_door(zone_name, &door) != 0) { 44250Sstevel@tonic-gate /* 44260Sstevel@tonic-gate * zoneadmd may be dead, but it may come back to 44270Sstevel@tonic-gate * life later. 44280Sstevel@tonic-gate */ 44290Sstevel@tonic-gate break; 44300Sstevel@tonic-gate } 44310Sstevel@tonic-gate break; 44320Sstevel@tonic-gate default: 44330Sstevel@tonic-gate cmn_err(CE_WARN, 44340Sstevel@tonic-gate "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 44350Sstevel@tonic-gate error); 44360Sstevel@tonic-gate goto out; 44370Sstevel@tonic-gate } 44380Sstevel@tonic-gate next: 44390Sstevel@tonic-gate /* 44400Sstevel@tonic-gate * If this isn't the same zone_t that we originally had in mind, 44410Sstevel@tonic-gate * then this is the same as if two kadmin requests come in at 44420Sstevel@tonic-gate * the same time: the first one wins. This means we lose, so we 44430Sstevel@tonic-gate * bail. 44440Sstevel@tonic-gate */ 44450Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) { 44460Sstevel@tonic-gate /* 44470Sstevel@tonic-gate * Problem is solved. 44480Sstevel@tonic-gate */ 44490Sstevel@tonic-gate break; 44500Sstevel@tonic-gate } 44510Sstevel@tonic-gate if (zone->zone_uniqid != uniqid) { 44520Sstevel@tonic-gate /* 44530Sstevel@tonic-gate * zoneid recycled 44540Sstevel@tonic-gate */ 44550Sstevel@tonic-gate zone_rele(zone); 44560Sstevel@tonic-gate break; 44570Sstevel@tonic-gate } 44580Sstevel@tonic-gate /* 44590Sstevel@tonic-gate * We could zone_status_timedwait(), but there doesn't seem to 44600Sstevel@tonic-gate * be much point in doing that (plus, it would mean that 44610Sstevel@tonic-gate * zone_free() isn't called until this thread exits). 44620Sstevel@tonic-gate */ 44630Sstevel@tonic-gate zone_rele(zone); 44640Sstevel@tonic-gate delay(hz); 44650Sstevel@tonic-gate darg = save_arg; 44660Sstevel@tonic-gate } 44670Sstevel@tonic-gate out: 44680Sstevel@tonic-gate if (door != NULL) { 44690Sstevel@tonic-gate zone_release_door(&door); 44700Sstevel@tonic-gate } 44710Sstevel@tonic-gate kmem_free(zone_name, zone_namelen); 44720Sstevel@tonic-gate thread_exit(); 44730Sstevel@tonic-gate } 44740Sstevel@tonic-gate 44750Sstevel@tonic-gate /* 4476*2267Sdp * Entry point for uadmin() to tell the zone to go away or reboot. Analog to 4477*2267Sdp * kadmin(). The caller is a process in the zone. 44780Sstevel@tonic-gate * 44790Sstevel@tonic-gate * In order to shutdown the zone, we will hand off control to zoneadmd 44800Sstevel@tonic-gate * (running in the global zone) via a door. We do a half-hearted job at 44810Sstevel@tonic-gate * killing all processes in the zone, create a kernel thread to contact 44820Sstevel@tonic-gate * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 44830Sstevel@tonic-gate * a form of generation number used to let zoneadmd (as well as 44840Sstevel@tonic-gate * zone_destroy()) know exactly which zone they're re talking about. 44850Sstevel@tonic-gate */ 44860Sstevel@tonic-gate int 4487*2267Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp) 44880Sstevel@tonic-gate { 44890Sstevel@tonic-gate struct zarg *zargp; 44900Sstevel@tonic-gate zone_cmd_t zcmd; 44910Sstevel@tonic-gate zone_t *zone; 44920Sstevel@tonic-gate 44930Sstevel@tonic-gate zone = curproc->p_zone; 44940Sstevel@tonic-gate ASSERT(getzoneid() != GLOBAL_ZONEID); 44950Sstevel@tonic-gate 44960Sstevel@tonic-gate switch (cmd) { 44970Sstevel@tonic-gate case A_SHUTDOWN: 44980Sstevel@tonic-gate switch (fcn) { 44990Sstevel@tonic-gate case AD_HALT: 45000Sstevel@tonic-gate case AD_POWEROFF: 45010Sstevel@tonic-gate zcmd = Z_HALT; 45020Sstevel@tonic-gate break; 45030Sstevel@tonic-gate case AD_BOOT: 45040Sstevel@tonic-gate zcmd = Z_REBOOT; 45050Sstevel@tonic-gate break; 45060Sstevel@tonic-gate case AD_IBOOT: 45070Sstevel@tonic-gate case AD_SBOOT: 45080Sstevel@tonic-gate case AD_SIBOOT: 45090Sstevel@tonic-gate case AD_NOSYNC: 45100Sstevel@tonic-gate return (ENOTSUP); 45110Sstevel@tonic-gate default: 45120Sstevel@tonic-gate return (EINVAL); 45130Sstevel@tonic-gate } 45140Sstevel@tonic-gate break; 45150Sstevel@tonic-gate case A_REBOOT: 45160Sstevel@tonic-gate zcmd = Z_REBOOT; 45170Sstevel@tonic-gate break; 45180Sstevel@tonic-gate case A_FTRACE: 45190Sstevel@tonic-gate case A_REMOUNT: 45200Sstevel@tonic-gate case A_FREEZE: 45210Sstevel@tonic-gate case A_DUMP: 45220Sstevel@tonic-gate return (ENOTSUP); 45230Sstevel@tonic-gate default: 45240Sstevel@tonic-gate ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 45250Sstevel@tonic-gate return (EINVAL); 45260Sstevel@tonic-gate } 45270Sstevel@tonic-gate 45280Sstevel@tonic-gate if (secpolicy_zone_admin(credp, B_FALSE)) 45290Sstevel@tonic-gate return (EPERM); 45300Sstevel@tonic-gate mutex_enter(&zone_status_lock); 4531*2267Sdp 45320Sstevel@tonic-gate /* 45330Sstevel@tonic-gate * zone_status can't be ZONE_IS_EMPTY or higher since curproc 45340Sstevel@tonic-gate * is in the zone. 45350Sstevel@tonic-gate */ 45360Sstevel@tonic-gate ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 45370Sstevel@tonic-gate if (zone_status_get(zone) > ZONE_IS_RUNNING) { 45380Sstevel@tonic-gate /* 45390Sstevel@tonic-gate * This zone is already on its way down. 45400Sstevel@tonic-gate */ 45410Sstevel@tonic-gate mutex_exit(&zone_status_lock); 45420Sstevel@tonic-gate return (0); 45430Sstevel@tonic-gate } 45440Sstevel@tonic-gate /* 45450Sstevel@tonic-gate * Prevent future zone_enter()s 45460Sstevel@tonic-gate */ 45470Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 45480Sstevel@tonic-gate mutex_exit(&zone_status_lock); 45490Sstevel@tonic-gate 45500Sstevel@tonic-gate /* 45510Sstevel@tonic-gate * Kill everyone now and call zoneadmd later. 45520Sstevel@tonic-gate * zone_ki_call_zoneadmd() will do a more thorough job of this 45530Sstevel@tonic-gate * later. 45540Sstevel@tonic-gate */ 45550Sstevel@tonic-gate killall(zone->zone_id); 45560Sstevel@tonic-gate /* 45570Sstevel@tonic-gate * Now, create the thread to contact zoneadmd and do the rest of the 45580Sstevel@tonic-gate * work. This thread can't be created in our zone otherwise 45590Sstevel@tonic-gate * zone_destroy() would deadlock. 45600Sstevel@tonic-gate */ 4561*2267Sdp zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP); 45620Sstevel@tonic-gate zargp->arg.cmd = zcmd; 45630Sstevel@tonic-gate zargp->arg.uniqid = zone->zone_uniqid; 4564*2267Sdp zargp->zone = zone; 45650Sstevel@tonic-gate (void) strcpy(zargp->arg.locale, "C"); 4566*2267Sdp /* mdep was already copied in for us by uadmin */ 4567*2267Sdp if (mdep != NULL) 4568*2267Sdp (void) strlcpy(zargp->arg.bootbuf, mdep, 4569*2267Sdp sizeof (zargp->arg.bootbuf)); 4570*2267Sdp zone_hold(zone); 45710Sstevel@tonic-gate 45720Sstevel@tonic-gate (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 45730Sstevel@tonic-gate TS_RUN, minclsyspri); 45740Sstevel@tonic-gate exit(CLD_EXITED, 0); 45750Sstevel@tonic-gate 45760Sstevel@tonic-gate return (EINVAL); 45770Sstevel@tonic-gate } 45780Sstevel@tonic-gate 45790Sstevel@tonic-gate /* 45800Sstevel@tonic-gate * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 45810Sstevel@tonic-gate * status to ZONE_IS_SHUTTING_DOWN. 45820Sstevel@tonic-gate */ 45830Sstevel@tonic-gate void 45840Sstevel@tonic-gate zone_shutdown_global(void) 45850Sstevel@tonic-gate { 45860Sstevel@tonic-gate ASSERT(curproc->p_zone == global_zone); 45870Sstevel@tonic-gate 45880Sstevel@tonic-gate mutex_enter(&zone_status_lock); 45890Sstevel@tonic-gate ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 45900Sstevel@tonic-gate zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 45910Sstevel@tonic-gate mutex_exit(&zone_status_lock); 45920Sstevel@tonic-gate } 4593789Sahrens 4594789Sahrens /* 4595789Sahrens * Returns true if the named dataset is visible in the current zone. 4596789Sahrens * The 'write' parameter is set to 1 if the dataset is also writable. 4597789Sahrens */ 4598789Sahrens int 4599789Sahrens zone_dataset_visible(const char *dataset, int *write) 4600789Sahrens { 4601789Sahrens zone_dataset_t *zd; 4602789Sahrens size_t len; 4603789Sahrens zone_t *zone = curproc->p_zone; 4604789Sahrens 4605789Sahrens if (dataset[0] == '\0') 4606789Sahrens return (0); 4607789Sahrens 4608789Sahrens /* 4609789Sahrens * Walk the list once, looking for datasets which match exactly, or 4610789Sahrens * specify a dataset underneath an exported dataset. If found, return 4611789Sahrens * true and note that it is writable. 4612789Sahrens */ 4613789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 4614789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 4615789Sahrens 4616789Sahrens len = strlen(zd->zd_dataset); 4617789Sahrens if (strlen(dataset) >= len && 4618789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 4619816Smaybee (dataset[len] == '\0' || dataset[len] == '/' || 4620816Smaybee dataset[len] == '@')) { 4621789Sahrens if (write) 4622789Sahrens *write = 1; 4623789Sahrens return (1); 4624789Sahrens } 4625789Sahrens } 4626789Sahrens 4627789Sahrens /* 4628789Sahrens * Walk the list a second time, searching for datasets which are parents 4629789Sahrens * of exported datasets. These should be visible, but read-only. 4630789Sahrens * 4631789Sahrens * Note that we also have to support forms such as 'pool/dataset/', with 4632789Sahrens * a trailing slash. 4633789Sahrens */ 4634789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 4635789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 4636789Sahrens 4637789Sahrens len = strlen(dataset); 4638789Sahrens if (dataset[len - 1] == '/') 4639789Sahrens len--; /* Ignore trailing slash */ 4640789Sahrens if (len < strlen(zd->zd_dataset) && 4641789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 4642789Sahrens zd->zd_dataset[len] == '/') { 4643789Sahrens if (write) 4644789Sahrens *write = 0; 4645789Sahrens return (1); 4646789Sahrens } 4647789Sahrens } 4648789Sahrens 4649789Sahrens return (0); 4650789Sahrens } 46511676Sjpk 46521676Sjpk /* 46531676Sjpk * zone_find_by_any_path() - 46541676Sjpk * 46551676Sjpk * kernel-private routine similar to zone_find_by_path(), but which 46561676Sjpk * effectively compares against zone paths rather than zonerootpath 46571676Sjpk * (i.e., the last component of zonerootpaths, which should be "root/", 46581676Sjpk * are not compared.) This is done in order to accurately identify all 46591676Sjpk * paths, whether zone-visible or not, including those which are parallel 46601676Sjpk * to /root/, such as /dev/, /home/, etc... 46611676Sjpk * 46621676Sjpk * If the specified path does not fall under any zone path then global 46631676Sjpk * zone is returned. 46641676Sjpk * 46651676Sjpk * The treat_abs parameter indicates whether the path should be treated as 46661676Sjpk * an absolute path although it does not begin with "/". (This supports 46671676Sjpk * nfs mount syntax such as host:any/path.) 46681676Sjpk * 46691676Sjpk * The caller is responsible for zone_rele of the returned zone. 46701676Sjpk */ 46711676Sjpk zone_t * 46721676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs) 46731676Sjpk { 46741676Sjpk zone_t *zone; 46751676Sjpk int path_offset = 0; 46761676Sjpk 46771676Sjpk if (path == NULL) { 46781676Sjpk zone_hold(global_zone); 46791676Sjpk return (global_zone); 46801676Sjpk } 46811676Sjpk 46821676Sjpk if (*path != '/') { 46831676Sjpk ASSERT(treat_abs); 46841676Sjpk path_offset = 1; 46851676Sjpk } 46861676Sjpk 46871676Sjpk mutex_enter(&zonehash_lock); 46881676Sjpk for (zone = list_head(&zone_active); zone != NULL; 46891676Sjpk zone = list_next(&zone_active, zone)) { 46901676Sjpk char *c; 46911676Sjpk size_t pathlen; 46921876Smp46848 char *rootpath_start; 46931676Sjpk 46941676Sjpk if (zone == global_zone) /* skip global zone */ 46951676Sjpk continue; 46961676Sjpk 46971676Sjpk /* scan backwards to find start of last component */ 46981676Sjpk c = zone->zone_rootpath + zone->zone_rootpathlen - 2; 46991676Sjpk do { 47001676Sjpk c--; 47011676Sjpk } while (*c != '/'); 47021676Sjpk 47031876Smp46848 pathlen = c - zone->zone_rootpath + 1 - path_offset; 47041876Smp46848 rootpath_start = (zone->zone_rootpath + path_offset); 47051876Smp46848 if (strncmp(path, rootpath_start, pathlen) == 0) 47061676Sjpk break; 47071676Sjpk } 47081676Sjpk if (zone == NULL) 47091676Sjpk zone = global_zone; 47101676Sjpk zone_hold(zone); 47111676Sjpk mutex_exit(&zonehash_lock); 47121676Sjpk return (zone); 47131676Sjpk } 4714