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 1782267Sdp * - zone_setattr: set attributes of a zone 1792267Sdp * - 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> 218*2677Sml93401 #include <sys/ipc_impl.h> 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate #include <sys/door.h> 2210Sstevel@tonic-gate #include <sys/cpuvar.h> 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate #include <sys/uadmin.h> 2240Sstevel@tonic-gate #include <sys/session.h> 2250Sstevel@tonic-gate #include <sys/cmn_err.h> 2260Sstevel@tonic-gate #include <sys/modhash.h> 2272267Sdp #include <sys/sunddi.h> 2280Sstevel@tonic-gate #include <sys/nvpair.h> 2290Sstevel@tonic-gate #include <sys/rctl.h> 2300Sstevel@tonic-gate #include <sys/fss.h> 2310Sstevel@tonic-gate #include <sys/zone.h> 2321676Sjpk #include <sys/tsol/label.h> 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* 2350Sstevel@tonic-gate * cv used to signal that all references to the zone have been released. This 2360Sstevel@tonic-gate * needs to be global since there may be multiple waiters, and the first to 2370Sstevel@tonic-gate * wake up will free the zone_t, hence we cannot use zone->zone_cv. 2380Sstevel@tonic-gate */ 2390Sstevel@tonic-gate static kcondvar_t zone_destroy_cv; 2400Sstevel@tonic-gate /* 2410Sstevel@tonic-gate * Lock used to serialize access to zone_cv. This could have been per-zone, 2420Sstevel@tonic-gate * but then we'd need another lock for zone_destroy_cv, and why bother? 2430Sstevel@tonic-gate */ 2440Sstevel@tonic-gate static kmutex_t zone_status_lock; 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * ZSD-related global variables. 2480Sstevel@tonic-gate */ 2490Sstevel@tonic-gate static kmutex_t zsd_key_lock; /* protects the following two */ 2500Sstevel@tonic-gate /* 2510Sstevel@tonic-gate * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 2520Sstevel@tonic-gate */ 2530Sstevel@tonic-gate static zone_key_t zsd_keyval = 0; 2540Sstevel@tonic-gate /* 2550Sstevel@tonic-gate * Global list of registered keys. We use this when a new zone is created. 2560Sstevel@tonic-gate */ 2570Sstevel@tonic-gate static list_t zsd_registered_keys; 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate int zone_hash_size = 256; 2601676Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel; 2610Sstevel@tonic-gate static kmutex_t zonehash_lock; 2620Sstevel@tonic-gate static uint_t zonecount; 2630Sstevel@tonic-gate static id_space_t *zoneid_space; 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate /* 2660Sstevel@tonic-gate * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 2670Sstevel@tonic-gate * kernel proper runs, and which manages all other zones. 2680Sstevel@tonic-gate * 2690Sstevel@tonic-gate * Although not declared as static, the variable "zone0" should not be used 2700Sstevel@tonic-gate * except for by code that needs to reference the global zone early on in boot, 2710Sstevel@tonic-gate * before it is fully initialized. All other consumers should use 2720Sstevel@tonic-gate * 'global_zone'. 2730Sstevel@tonic-gate */ 2740Sstevel@tonic-gate zone_t zone0; 2750Sstevel@tonic-gate zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * List of active zones, protected by zonehash_lock. 2790Sstevel@tonic-gate */ 2800Sstevel@tonic-gate static list_t zone_active; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate /* 2830Sstevel@tonic-gate * List of destroyed zones that still have outstanding cred references. 2840Sstevel@tonic-gate * Used for debugging. Uses a separate lock to avoid lock ordering 2850Sstevel@tonic-gate * problems in zone_free. 2860Sstevel@tonic-gate */ 2870Sstevel@tonic-gate static list_t zone_deathrow; 2880Sstevel@tonic-gate static kmutex_t zone_deathrow_lock; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */ 2910Sstevel@tonic-gate uint_t maxzones = 8192; 2920Sstevel@tonic-gate 2931166Sdstaff /* Event channel to sent zone state change notifications */ 2941166Sdstaff evchan_t *zone_event_chan; 2951166Sdstaff 2961166Sdstaff /* 2971166Sdstaff * This table holds the mapping from kernel zone states to 2981166Sdstaff * states visible in the state notification API. 2991166Sdstaff * The idea is that we only expose "obvious" states and 3001166Sdstaff * do not expose states which are just implementation details. 3011166Sdstaff */ 3021166Sdstaff const char *zone_status_table[] = { 3031166Sdstaff ZONE_EVENT_UNINITIALIZED, /* uninitialized */ 3041166Sdstaff ZONE_EVENT_READY, /* ready */ 3051166Sdstaff ZONE_EVENT_READY, /* booting */ 3061166Sdstaff ZONE_EVENT_RUNNING, /* running */ 3071166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* shutting_down */ 3081166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* empty */ 3091166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* down */ 3101166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* dying */ 3111166Sdstaff ZONE_EVENT_UNINITIALIZED, /* dead */ 3121166Sdstaff }; 3131166Sdstaff 3140Sstevel@tonic-gate /* 3150Sstevel@tonic-gate * This isn't static so lint doesn't complain. 3160Sstevel@tonic-gate */ 3170Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares; 3180Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps; 319*2677Sml93401 rctl_hndl_t rc_zone_shmmax; 320*2677Sml93401 rctl_hndl_t rc_zone_shmmni; 321*2677Sml93401 rctl_hndl_t rc_zone_semmni; 322*2677Sml93401 rctl_hndl_t rc_zone_msgmni; 3230Sstevel@tonic-gate /* 3240Sstevel@tonic-gate * Synchronization primitives used to synchronize between mounts and zone 3250Sstevel@tonic-gate * creation/destruction. 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate static int mounts_in_progress; 3280Sstevel@tonic-gate static kcondvar_t mount_cv; 3290Sstevel@tonic-gate static kmutex_t mount_lock; 3300Sstevel@tonic-gate 3312267Sdp const char * const zone_default_initname = "/sbin/init"; 3321676Sjpk static char * const zone_prefix = "/zone/"; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid); 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* 337813Sdp * Bump this number when you alter the zone syscall interfaces; this is 338813Sdp * because we need to have support for previous API versions in libc 339813Sdp * to support patching; libc calls into the kernel to determine this number. 340813Sdp * 341813Sdp * Version 1 of the API is the version originally shipped with Solaris 10 342813Sdp * Version 2 alters the zone_create system call in order to support more 343813Sdp * arguments by moving the args into a structure; and to do better 344813Sdp * error reporting when zone_create() fails. 345813Sdp * Version 3 alters the zone_create system call in order to support the 346813Sdp * import of ZFS datasets to zones. 3471676Sjpk * Version 4 alters the zone_create system call in order to support 3481676Sjpk * Trusted Extensions. 3492267Sdp * Version 5 alters the zone_boot system call, and converts its old 3502267Sdp * bootargs parameter to be set by the zone_setattr API instead. 351813Sdp */ 3522267Sdp static const int ZONE_SYSCALL_API_VERSION = 5; 353813Sdp 354813Sdp /* 3550Sstevel@tonic-gate * Certain filesystems (such as NFS and autofs) need to know which zone 3560Sstevel@tonic-gate * the mount is being placed in. Because of this, we need to be able to 3570Sstevel@tonic-gate * ensure that a zone isn't in the process of being created such that 3580Sstevel@tonic-gate * nfs_mount() thinks it is in the global zone, while by the time it 3590Sstevel@tonic-gate * gets added the list of mounted zones, it ends up on zoneA's mount 3600Sstevel@tonic-gate * list. 3610Sstevel@tonic-gate * 3620Sstevel@tonic-gate * The following functions: block_mounts()/resume_mounts() and 3630Sstevel@tonic-gate * mount_in_progress()/mount_completed() are used by zones and the VFS 3640Sstevel@tonic-gate * layer (respectively) to synchronize zone creation and new mounts. 3650Sstevel@tonic-gate * 3660Sstevel@tonic-gate * The semantics are like a reader-reader lock such that there may 3670Sstevel@tonic-gate * either be multiple mounts (or zone creations, if that weren't 3680Sstevel@tonic-gate * serialized by zonehash_lock) in progress at the same time, but not 3690Sstevel@tonic-gate * both. 3700Sstevel@tonic-gate * 3710Sstevel@tonic-gate * We use cv's so the user can ctrl-C out of the operation if it's 3720Sstevel@tonic-gate * taking too long. 3730Sstevel@tonic-gate * 3740Sstevel@tonic-gate * The semantics are such that there is unfair bias towards the 3750Sstevel@tonic-gate * "current" operation. This means that zone creations may starve if 3760Sstevel@tonic-gate * there is a rapid succession of new mounts coming in to the system, or 3770Sstevel@tonic-gate * there is a remote possibility that zones will be created at such a 3780Sstevel@tonic-gate * rate that new mounts will not be able to proceed. 3790Sstevel@tonic-gate */ 3800Sstevel@tonic-gate /* 3810Sstevel@tonic-gate * Prevent new mounts from progressing to the point of calling 3820Sstevel@tonic-gate * VFS_MOUNT(). If there are already mounts in this "region", wait for 3830Sstevel@tonic-gate * them to complete. 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate static int 3860Sstevel@tonic-gate block_mounts(void) 3870Sstevel@tonic-gate { 3880Sstevel@tonic-gate int retval = 0; 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate /* 3910Sstevel@tonic-gate * Since it may block for a long time, block_mounts() shouldn't be 3920Sstevel@tonic-gate * called with zonehash_lock held. 3930Sstevel@tonic-gate */ 3940Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 3950Sstevel@tonic-gate mutex_enter(&mount_lock); 3960Sstevel@tonic-gate while (mounts_in_progress > 0) { 3970Sstevel@tonic-gate if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 3980Sstevel@tonic-gate goto signaled; 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate /* 4010Sstevel@tonic-gate * A negative value of mounts_in_progress indicates that mounts 4020Sstevel@tonic-gate * have been blocked by (-mounts_in_progress) different callers. 4030Sstevel@tonic-gate */ 4040Sstevel@tonic-gate mounts_in_progress--; 4050Sstevel@tonic-gate retval = 1; 4060Sstevel@tonic-gate signaled: 4070Sstevel@tonic-gate mutex_exit(&mount_lock); 4080Sstevel@tonic-gate return (retval); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate /* 4120Sstevel@tonic-gate * The VFS layer may progress with new mounts as far as we're concerned. 4130Sstevel@tonic-gate * Allow them to progress if we were the last obstacle. 4140Sstevel@tonic-gate */ 4150Sstevel@tonic-gate static void 4160Sstevel@tonic-gate resume_mounts(void) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate mutex_enter(&mount_lock); 4190Sstevel@tonic-gate if (++mounts_in_progress == 0) 4200Sstevel@tonic-gate cv_broadcast(&mount_cv); 4210Sstevel@tonic-gate mutex_exit(&mount_lock); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate /* 4250Sstevel@tonic-gate * The VFS layer is busy with a mount; zones should wait until all 4260Sstevel@tonic-gate * mounts are completed to progress. 4270Sstevel@tonic-gate */ 4280Sstevel@tonic-gate void 4290Sstevel@tonic-gate mount_in_progress(void) 4300Sstevel@tonic-gate { 4310Sstevel@tonic-gate mutex_enter(&mount_lock); 4320Sstevel@tonic-gate while (mounts_in_progress < 0) 4330Sstevel@tonic-gate cv_wait(&mount_cv, &mount_lock); 4340Sstevel@tonic-gate mounts_in_progress++; 4350Sstevel@tonic-gate mutex_exit(&mount_lock); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* 4390Sstevel@tonic-gate * VFS is done with one mount; wake up any waiting block_mounts() 4400Sstevel@tonic-gate * callers if this is the last mount. 4410Sstevel@tonic-gate */ 4420Sstevel@tonic-gate void 4430Sstevel@tonic-gate mount_completed(void) 4440Sstevel@tonic-gate { 4450Sstevel@tonic-gate mutex_enter(&mount_lock); 4460Sstevel@tonic-gate if (--mounts_in_progress == 0) 4470Sstevel@tonic-gate cv_broadcast(&mount_cv); 4480Sstevel@tonic-gate mutex_exit(&mount_lock); 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* 4520Sstevel@tonic-gate * ZSD routines. 4530Sstevel@tonic-gate * 4540Sstevel@tonic-gate * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 4550Sstevel@tonic-gate * defined by the pthread_key_create() and related interfaces. 4560Sstevel@tonic-gate * 4570Sstevel@tonic-gate * Kernel subsystems may register one or more data items and/or 4580Sstevel@tonic-gate * callbacks to be executed when a zone is created, shutdown, or 4590Sstevel@tonic-gate * destroyed. 4600Sstevel@tonic-gate * 4610Sstevel@tonic-gate * Unlike the thread counterpart, destructor callbacks will be executed 4620Sstevel@tonic-gate * even if the data pointer is NULL and/or there are no constructor 4630Sstevel@tonic-gate * callbacks, so it is the responsibility of such callbacks to check for 4640Sstevel@tonic-gate * NULL data values if necessary. 4650Sstevel@tonic-gate * 4660Sstevel@tonic-gate * The locking strategy and overall picture is as follows: 4670Sstevel@tonic-gate * 4680Sstevel@tonic-gate * When someone calls zone_key_create(), a template ZSD entry is added to the 4690Sstevel@tonic-gate * global list "zsd_registered_keys", protected by zsd_key_lock. The 4700Sstevel@tonic-gate * constructor callback is called immediately on all existing zones, and a 4710Sstevel@tonic-gate * copy of the ZSD entry added to the per-zone zone_zsd list (protected by 4720Sstevel@tonic-gate * zone_lock). As this operation requires the list of zones, the list of 4730Sstevel@tonic-gate * registered keys, and the per-zone list of ZSD entries to remain constant 4740Sstevel@tonic-gate * throughout the entire operation, it must grab zonehash_lock, zone_lock for 4750Sstevel@tonic-gate * all existing zones, and zsd_key_lock, in that order. Similar locking is 4760Sstevel@tonic-gate * needed when zone_key_delete() is called. It is thus sufficient to hold 4770Sstevel@tonic-gate * zsd_key_lock *or* zone_lock to prevent additions to or removals from the 4780Sstevel@tonic-gate * per-zone zone_zsd list. 4790Sstevel@tonic-gate * 4800Sstevel@tonic-gate * Note that this implementation does not make a copy of the ZSD entry if a 4810Sstevel@tonic-gate * constructor callback is not provided. A zone_getspecific() on such an 4820Sstevel@tonic-gate * uninitialized ZSD entry will return NULL. 4830Sstevel@tonic-gate * 4840Sstevel@tonic-gate * When new zones are created constructor callbacks for all registered ZSD 4850Sstevel@tonic-gate * entries will be called. 4860Sstevel@tonic-gate * 4870Sstevel@tonic-gate * The framework does not provide any locking around zone_getspecific() and 4880Sstevel@tonic-gate * zone_setspecific() apart from that needed for internal consistency, so 4890Sstevel@tonic-gate * callers interested in atomic "test-and-set" semantics will need to provide 4900Sstevel@tonic-gate * their own locking. 4910Sstevel@tonic-gate */ 4920Sstevel@tonic-gate void 4930Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 4940Sstevel@tonic-gate void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 4950Sstevel@tonic-gate { 4960Sstevel@tonic-gate struct zsd_entry *zsdp; 4970Sstevel@tonic-gate struct zsd_entry *t; 4980Sstevel@tonic-gate struct zone *zone; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP); 5010Sstevel@tonic-gate zsdp->zsd_data = NULL; 5020Sstevel@tonic-gate zsdp->zsd_create = create; 5030Sstevel@tonic-gate zsdp->zsd_shutdown = shutdown; 5040Sstevel@tonic-gate zsdp->zsd_destroy = destroy; 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* stop the world */ 5070Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5080Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5090Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5120Sstevel@tonic-gate *keyp = zsdp->zsd_key = ++zsd_keyval; 5130Sstevel@tonic-gate ASSERT(zsd_keyval != 0); 5140Sstevel@tonic-gate list_insert_tail(&zsd_registered_keys, zsdp); 5150Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate if (create != NULL) { 5180Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5190Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 5200Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 5210Sstevel@tonic-gate t->zsd_key = *keyp; 5220Sstevel@tonic-gate t->zsd_data = (*create)(zone->zone_id); 5230Sstevel@tonic-gate t->zsd_create = create; 5240Sstevel@tonic-gate t->zsd_shutdown = shutdown; 5250Sstevel@tonic-gate t->zsd_destroy = destroy; 5260Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5300Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5310Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 5320Sstevel@tonic-gate mutex_exit(&zonehash_lock); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate /* 5360Sstevel@tonic-gate * Helper function to find the zsd_entry associated with the key in the 5370Sstevel@tonic-gate * given list. 5380Sstevel@tonic-gate */ 5390Sstevel@tonic-gate static struct zsd_entry * 5400Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key) 5410Sstevel@tonic-gate { 5420Sstevel@tonic-gate struct zsd_entry *zsd; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 5450Sstevel@tonic-gate if (zsd->zsd_key == key) { 5460Sstevel@tonic-gate /* 5470Sstevel@tonic-gate * Move to head of list to keep list in MRU order. 5480Sstevel@tonic-gate */ 5490Sstevel@tonic-gate if (zsd != list_head(l)) { 5500Sstevel@tonic-gate list_remove(l, zsd); 5510Sstevel@tonic-gate list_insert_head(l, zsd); 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate return (zsd); 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate return (NULL); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * Function called when a module is being unloaded, or otherwise wishes 5610Sstevel@tonic-gate * to unregister its ZSD key and callbacks. 5620Sstevel@tonic-gate */ 5630Sstevel@tonic-gate int 5640Sstevel@tonic-gate zone_key_delete(zone_key_t key) 5650Sstevel@tonic-gate { 5660Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 5670Sstevel@tonic-gate zone_t *zone; 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* Zone create/delete waits for us */ 5700Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5710Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5720Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5750Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 5760Sstevel@tonic-gate if (zsdp == NULL) 5770Sstevel@tonic-gate goto notfound; 5780Sstevel@tonic-gate list_remove(&zsd_registered_keys, zsdp); 5790Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5820Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 5830Sstevel@tonic-gate struct zsd_entry *del; 5840Sstevel@tonic-gate void *data; 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate if (!(zone->zone_flags & ZF_DESTROYED)) { 5870Sstevel@tonic-gate del = zsd_find(&zone->zone_zsd, key); 5880Sstevel@tonic-gate if (del != NULL) { 5890Sstevel@tonic-gate data = del->zsd_data; 5900Sstevel@tonic-gate ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 5910Sstevel@tonic-gate ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 5920Sstevel@tonic-gate list_remove(&zone->zone_zsd, del); 5930Sstevel@tonic-gate kmem_free(del, sizeof (*del)); 5940Sstevel@tonic-gate } else { 5950Sstevel@tonic-gate data = NULL; 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate if (zsdp->zsd_shutdown) 5980Sstevel@tonic-gate zsdp->zsd_shutdown(zone->zone_id, data); 5990Sstevel@tonic-gate if (zsdp->zsd_destroy) 6000Sstevel@tonic-gate zsdp->zsd_destroy(zone->zone_id, data); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate mutex_exit(&zonehash_lock); 6050Sstevel@tonic-gate kmem_free(zsdp, sizeof (*zsdp)); 6060Sstevel@tonic-gate return (0); 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate notfound: 6090Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6100Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 6110Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 6120Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6130Sstevel@tonic-gate mutex_exit(&zonehash_lock); 6140Sstevel@tonic-gate return (-1); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* 6180Sstevel@tonic-gate * ZSD counterpart of pthread_setspecific(). 6190Sstevel@tonic-gate */ 6200Sstevel@tonic-gate int 6210Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 6220Sstevel@tonic-gate { 6230Sstevel@tonic-gate struct zsd_entry *t; 6240Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6270Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6280Sstevel@tonic-gate if (t != NULL) { 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * Replace old value with new 6310Sstevel@tonic-gate */ 6320Sstevel@tonic-gate t->zsd_data = (void *)data; 6330Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6340Sstevel@tonic-gate return (0); 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate /* 6370Sstevel@tonic-gate * If there was no previous value, go through the list of registered 6380Sstevel@tonic-gate * keys. 6390Sstevel@tonic-gate * 6400Sstevel@tonic-gate * We avoid grabbing zsd_key_lock until we are sure we need it; this is 6410Sstevel@tonic-gate * necessary for shutdown callbacks to be able to execute without fear 6420Sstevel@tonic-gate * of deadlock. 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 6450Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 6460Sstevel@tonic-gate if (zsdp == NULL) { /* Key was not registered */ 6470Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6480Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6490Sstevel@tonic-gate return (-1); 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate /* 6530Sstevel@tonic-gate * Add a zsd_entry to this zone, using the template we just retrieved 6540Sstevel@tonic-gate * to initialize the constructor and destructor(s). 6550Sstevel@tonic-gate */ 6560Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 6570Sstevel@tonic-gate t->zsd_key = key; 6580Sstevel@tonic-gate t->zsd_data = (void *)data; 6590Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 6600Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 6610Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 6620Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 6630Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6640Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6650Sstevel@tonic-gate return (0); 6660Sstevel@tonic-gate } 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate /* 6690Sstevel@tonic-gate * ZSD counterpart of pthread_getspecific(). 6700Sstevel@tonic-gate */ 6710Sstevel@tonic-gate void * 6720Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone) 6730Sstevel@tonic-gate { 6740Sstevel@tonic-gate struct zsd_entry *t; 6750Sstevel@tonic-gate void *data; 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6780Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6790Sstevel@tonic-gate data = (t == NULL ? NULL : t->zsd_data); 6800Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6810Sstevel@tonic-gate return (data); 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate /* 6850Sstevel@tonic-gate * Function used to initialize a zone's list of ZSD callbacks and data 6860Sstevel@tonic-gate * when the zone is being created. The callbacks are initialized from 6870Sstevel@tonic-gate * the template list (zsd_registered_keys), and the constructor 6880Sstevel@tonic-gate * callback executed (if one exists). 6890Sstevel@tonic-gate * 6900Sstevel@tonic-gate * This is called before the zone is made publicly available, hence no 6910Sstevel@tonic-gate * need to grab zone_lock. 6920Sstevel@tonic-gate * 6930Sstevel@tonic-gate * Although we grab and release zsd_key_lock, new entries cannot be 6940Sstevel@tonic-gate * added to or removed from the zsd_registered_keys list until we 6950Sstevel@tonic-gate * release zonehash_lock, so there isn't a window for a 6960Sstevel@tonic-gate * zone_key_create() to come in after we've dropped zsd_key_lock but 6970Sstevel@tonic-gate * before the zone is added to the zone list, such that the constructor 6980Sstevel@tonic-gate * callbacks aren't executed for the new zone. 6990Sstevel@tonic-gate */ 7000Sstevel@tonic-gate static void 7010Sstevel@tonic-gate zone_zsd_configure(zone_t *zone) 7020Sstevel@tonic-gate { 7030Sstevel@tonic-gate struct zsd_entry *zsdp; 7040Sstevel@tonic-gate struct zsd_entry *t; 7050Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 7080Sstevel@tonic-gate ASSERT(list_head(&zone->zone_zsd) == NULL); 7090Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7100Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7110Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7120Sstevel@tonic-gate if (zsdp->zsd_create != NULL) { 7130Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 7140Sstevel@tonic-gate t->zsd_key = zsdp->zsd_key; 7150Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 7160Sstevel@tonic-gate t->zsd_data = (*t->zsd_create)(zoneid); 7170Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 7180Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 7190Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 7230Sstevel@tonic-gate } 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate /* 7280Sstevel@tonic-gate * Helper function to execute shutdown or destructor callbacks. 7290Sstevel@tonic-gate */ 7300Sstevel@tonic-gate static void 7310Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 7320Sstevel@tonic-gate { 7330Sstevel@tonic-gate struct zsd_entry *zsdp; 7340Sstevel@tonic-gate struct zsd_entry *t; 7350Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 7360Sstevel@tonic-gate 7370Sstevel@tonic-gate ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 7380Sstevel@tonic-gate ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 7390Sstevel@tonic-gate ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 7420Sstevel@tonic-gate if (ct == ZSD_DESTROY) { 7430Sstevel@tonic-gate if (zone->zone_flags & ZF_DESTROYED) { 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * Make sure destructors are only called once. 7460Sstevel@tonic-gate */ 7470Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7480Sstevel@tonic-gate return; 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate zone->zone_flags |= ZF_DESTROYED; 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate /* 7550Sstevel@tonic-gate * Both zsd_key_lock and zone_lock need to be held in order to add or 7560Sstevel@tonic-gate * remove a ZSD key, (either globally as part of 7570Sstevel@tonic-gate * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is 7580Sstevel@tonic-gate * possible through zone_setspecific()), so it's sufficient to hold 7590Sstevel@tonic-gate * zsd_key_lock here. 7600Sstevel@tonic-gate * 7610Sstevel@tonic-gate * This is a good thing, since we don't want to recursively try to grab 7620Sstevel@tonic-gate * zone_lock if a callback attempts to do something like a crfree() or 7630Sstevel@tonic-gate * zone_rele(). 7640Sstevel@tonic-gate */ 7650Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7660Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7670Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7680Sstevel@tonic-gate zone_key_t key = zsdp->zsd_key; 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate /* Skip if no callbacks registered */ 7710Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL) 7720Sstevel@tonic-gate continue; 7730Sstevel@tonic-gate if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL) 7740Sstevel@tonic-gate continue; 7750Sstevel@tonic-gate /* 7760Sstevel@tonic-gate * Call the callback with the zone-specific data if we can find 7770Sstevel@tonic-gate * any, otherwise with NULL. 7780Sstevel@tonic-gate */ 7790Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 7800Sstevel@tonic-gate if (t != NULL) { 7810Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 7820Sstevel@tonic-gate t->zsd_shutdown(zoneid, t->zsd_data); 7830Sstevel@tonic-gate } else { 7840Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 7850Sstevel@tonic-gate t->zsd_destroy(zoneid, t->zsd_data); 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate } else { 7880Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 7890Sstevel@tonic-gate zsdp->zsd_shutdown(zoneid, NULL); 7900Sstevel@tonic-gate } else { 7910Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 7920Sstevel@tonic-gate zsdp->zsd_destroy(zoneid, NULL); 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 7970Sstevel@tonic-gate } 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate /* 8000Sstevel@tonic-gate * Called when the zone is going away; free ZSD-related memory, and 8010Sstevel@tonic-gate * destroy the zone_zsd list. 8020Sstevel@tonic-gate */ 8030Sstevel@tonic-gate static void 8040Sstevel@tonic-gate zone_free_zsd(zone_t *zone) 8050Sstevel@tonic-gate { 8060Sstevel@tonic-gate struct zsd_entry *t, *next; 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate /* 8090Sstevel@tonic-gate * Free all the zsd_entry's we had on this zone. 8100Sstevel@tonic-gate */ 8110Sstevel@tonic-gate for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 8120Sstevel@tonic-gate next = list_next(&zone->zone_zsd, t); 8130Sstevel@tonic-gate list_remove(&zone->zone_zsd, t); 8140Sstevel@tonic-gate kmem_free(t, sizeof (*t)); 8150Sstevel@tonic-gate } 8160Sstevel@tonic-gate list_destroy(&zone->zone_zsd); 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate /* 820789Sahrens * Frees memory associated with the zone dataset list. 821789Sahrens */ 822789Sahrens static void 823789Sahrens zone_free_datasets(zone_t *zone) 824789Sahrens { 825789Sahrens zone_dataset_t *t, *next; 826789Sahrens 827789Sahrens for (t = list_head(&zone->zone_datasets); t != NULL; t = next) { 828789Sahrens next = list_next(&zone->zone_datasets, t); 829789Sahrens list_remove(&zone->zone_datasets, t); 830789Sahrens kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1); 831789Sahrens kmem_free(t, sizeof (*t)); 832789Sahrens } 833789Sahrens list_destroy(&zone->zone_datasets); 834789Sahrens } 835789Sahrens 836789Sahrens /* 8370Sstevel@tonic-gate * zone.cpu-shares resource control support. 8380Sstevel@tonic-gate */ 8390Sstevel@tonic-gate /*ARGSUSED*/ 8400Sstevel@tonic-gate static rctl_qty_t 8410Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 8420Sstevel@tonic-gate { 8430Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8440Sstevel@tonic-gate return (p->p_zone->zone_shares); 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate /*ARGSUSED*/ 8480Sstevel@tonic-gate static int 8490Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 8500Sstevel@tonic-gate rctl_qty_t nv) 8510Sstevel@tonic-gate { 8520Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8530Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 8540Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 8550Sstevel@tonic-gate return (0); 8560Sstevel@tonic-gate 8570Sstevel@tonic-gate e->rcep_p.zone->zone_shares = nv; 8580Sstevel@tonic-gate return (0); 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = { 8620Sstevel@tonic-gate rcop_no_action, 8630Sstevel@tonic-gate zone_cpu_shares_usage, 8640Sstevel@tonic-gate zone_cpu_shares_set, 8650Sstevel@tonic-gate rcop_no_test 8660Sstevel@tonic-gate }; 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate /*ARGSUSED*/ 8690Sstevel@tonic-gate static rctl_qty_t 8700Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p) 8710Sstevel@tonic-gate { 8720Sstevel@tonic-gate rctl_qty_t nlwps; 8730Sstevel@tonic-gate zone_t *zone = p->p_zone; 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 8780Sstevel@tonic-gate nlwps = zone->zone_nlwps; 8790Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate return (nlwps); 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate /*ARGSUSED*/ 8850Sstevel@tonic-gate static int 8860Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 8870Sstevel@tonic-gate rctl_qty_t incr, uint_t flags) 8880Sstevel@tonic-gate { 8890Sstevel@tonic-gate rctl_qty_t nlwps; 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8920Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 8930Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 8940Sstevel@tonic-gate return (0); 8950Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 8960Sstevel@tonic-gate nlwps = e->rcep_p.zone->zone_nlwps; 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate if (nlwps + incr > rcntl->rcv_value) 8990Sstevel@tonic-gate return (1); 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate return (0); 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate /*ARGSUSED*/ 9050Sstevel@tonic-gate static int 9060Sstevel@tonic-gate zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) { 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 9090Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 9100Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 9110Sstevel@tonic-gate return (0); 9120Sstevel@tonic-gate e->rcep_p.zone->zone_nlwps_ctl = nv; 9130Sstevel@tonic-gate return (0); 9140Sstevel@tonic-gate } 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = { 9170Sstevel@tonic-gate rcop_no_action, 9180Sstevel@tonic-gate zone_lwps_usage, 9190Sstevel@tonic-gate zone_lwps_set, 9200Sstevel@tonic-gate zone_lwps_test, 9210Sstevel@tonic-gate }; 9220Sstevel@tonic-gate 923*2677Sml93401 /*ARGSUSED*/ 924*2677Sml93401 static int 925*2677Sml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 926*2677Sml93401 rctl_qty_t incr, uint_t flags) 927*2677Sml93401 { 928*2677Sml93401 rctl_qty_t v; 929*2677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 930*2677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 931*2677Sml93401 v = e->rcep_p.zone->zone_shmmax + incr; 932*2677Sml93401 if (v > rval->rcv_value) 933*2677Sml93401 return (1); 934*2677Sml93401 return (0); 935*2677Sml93401 } 936*2677Sml93401 937*2677Sml93401 static rctl_ops_t zone_shmmax_ops = { 938*2677Sml93401 rcop_no_action, 939*2677Sml93401 rcop_no_usage, 940*2677Sml93401 rcop_no_set, 941*2677Sml93401 zone_shmmax_test 942*2677Sml93401 }; 943*2677Sml93401 944*2677Sml93401 /*ARGSUSED*/ 945*2677Sml93401 static int 946*2677Sml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 947*2677Sml93401 rctl_qty_t incr, uint_t flags) 948*2677Sml93401 { 949*2677Sml93401 rctl_qty_t v; 950*2677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 951*2677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 952*2677Sml93401 v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr; 953*2677Sml93401 if (v > rval->rcv_value) 954*2677Sml93401 return (1); 955*2677Sml93401 return (0); 956*2677Sml93401 } 957*2677Sml93401 958*2677Sml93401 static rctl_ops_t zone_shmmni_ops = { 959*2677Sml93401 rcop_no_action, 960*2677Sml93401 rcop_no_usage, 961*2677Sml93401 rcop_no_set, 962*2677Sml93401 zone_shmmni_test 963*2677Sml93401 }; 964*2677Sml93401 965*2677Sml93401 /*ARGSUSED*/ 966*2677Sml93401 static int 967*2677Sml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 968*2677Sml93401 rctl_qty_t incr, uint_t flags) 969*2677Sml93401 { 970*2677Sml93401 rctl_qty_t v; 971*2677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 972*2677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 973*2677Sml93401 v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr; 974*2677Sml93401 if (v > rval->rcv_value) 975*2677Sml93401 return (1); 976*2677Sml93401 return (0); 977*2677Sml93401 } 978*2677Sml93401 979*2677Sml93401 static rctl_ops_t zone_semmni_ops = { 980*2677Sml93401 rcop_no_action, 981*2677Sml93401 rcop_no_usage, 982*2677Sml93401 rcop_no_set, 983*2677Sml93401 zone_semmni_test 984*2677Sml93401 }; 985*2677Sml93401 986*2677Sml93401 /*ARGSUSED*/ 987*2677Sml93401 static int 988*2677Sml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 989*2677Sml93401 rctl_qty_t incr, uint_t flags) 990*2677Sml93401 { 991*2677Sml93401 rctl_qty_t v; 992*2677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 993*2677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 994*2677Sml93401 v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr; 995*2677Sml93401 if (v > rval->rcv_value) 996*2677Sml93401 return (1); 997*2677Sml93401 return (0); 998*2677Sml93401 } 999*2677Sml93401 1000*2677Sml93401 static rctl_ops_t zone_msgmni_ops = { 1001*2677Sml93401 rcop_no_action, 1002*2677Sml93401 rcop_no_usage, 1003*2677Sml93401 rcop_no_set, 1004*2677Sml93401 zone_msgmni_test 1005*2677Sml93401 }; 1006*2677Sml93401 1007*2677Sml93401 10080Sstevel@tonic-gate /* 10090Sstevel@tonic-gate * Helper function to brand the zone with a unique ID. 10100Sstevel@tonic-gate */ 10110Sstevel@tonic-gate static void 10120Sstevel@tonic-gate zone_uniqid(zone_t *zone) 10130Sstevel@tonic-gate { 10140Sstevel@tonic-gate static uint64_t uniqid = 0; 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 10170Sstevel@tonic-gate zone->zone_uniqid = uniqid++; 10180Sstevel@tonic-gate } 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate /* 10210Sstevel@tonic-gate * Returns a held pointer to the "kcred" for the specified zone. 10220Sstevel@tonic-gate */ 10230Sstevel@tonic-gate struct cred * 10240Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid) 10250Sstevel@tonic-gate { 10260Sstevel@tonic-gate zone_t *zone; 10270Sstevel@tonic-gate cred_t *cr; 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) 10300Sstevel@tonic-gate return (NULL); 10310Sstevel@tonic-gate cr = zone->zone_kcred; 10320Sstevel@tonic-gate crhold(cr); 10330Sstevel@tonic-gate zone_rele(zone); 10340Sstevel@tonic-gate return (cr); 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate /* 10380Sstevel@tonic-gate * Called very early on in boot to initialize the ZSD list so that 10390Sstevel@tonic-gate * zone_key_create() can be called before zone_init(). It also initializes 10400Sstevel@tonic-gate * portions of zone0 which may be used before zone_init() is called. The 10410Sstevel@tonic-gate * variable "global_zone" will be set when zone0 is fully initialized by 10420Sstevel@tonic-gate * zone_init(). 10430Sstevel@tonic-gate */ 10440Sstevel@tonic-gate void 10450Sstevel@tonic-gate zone_zsd_init(void) 10460Sstevel@tonic-gate { 10470Sstevel@tonic-gate mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 10480Sstevel@tonic-gate mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 10490Sstevel@tonic-gate list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 10500Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 10510Sstevel@tonic-gate list_create(&zone_active, sizeof (zone_t), 10520Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 10530Sstevel@tonic-gate list_create(&zone_deathrow, sizeof (zone_t), 10540Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 10570Sstevel@tonic-gate mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 10580Sstevel@tonic-gate zone0.zone_shares = 1; 10590Sstevel@tonic-gate zone0.zone_nlwps_ctl = INT_MAX; 1060*2677Sml93401 zone0.zone_shmmax = 0; 1061*2677Sml93401 zone0.zone_ipc.ipcq_shmmni = 0; 1062*2677Sml93401 zone0.zone_ipc.ipcq_semmni = 0; 1063*2677Sml93401 zone0.zone_ipc.ipcq_msgmni = 0; 10640Sstevel@tonic-gate zone0.zone_name = GLOBAL_ZONENAME; 10650Sstevel@tonic-gate zone0.zone_nodename = utsname.nodename; 10660Sstevel@tonic-gate zone0.zone_domain = srpc_domain; 10670Sstevel@tonic-gate zone0.zone_ref = 1; 10680Sstevel@tonic-gate zone0.zone_id = GLOBAL_ZONEID; 10690Sstevel@tonic-gate zone0.zone_status = ZONE_IS_RUNNING; 10700Sstevel@tonic-gate zone0.zone_rootpath = "/"; 10710Sstevel@tonic-gate zone0.zone_rootpathlen = 2; 10720Sstevel@tonic-gate zone0.zone_psetid = ZONE_PS_INVAL; 10730Sstevel@tonic-gate zone0.zone_ncpus = 0; 10740Sstevel@tonic-gate zone0.zone_ncpus_online = 0; 10750Sstevel@tonic-gate zone0.zone_proc_initpid = 1; 10762267Sdp zone0.zone_initname = initname; 10770Sstevel@tonic-gate list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 10780Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 10790Sstevel@tonic-gate list_insert_head(&zone_active, &zone0); 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate /* 10820Sstevel@tonic-gate * The root filesystem is not mounted yet, so zone_rootvp cannot be set 10830Sstevel@tonic-gate * to anything meaningful. It is assigned to be 'rootdir' in 10840Sstevel@tonic-gate * vfs_mountroot(). 10850Sstevel@tonic-gate */ 10860Sstevel@tonic-gate zone0.zone_rootvp = NULL; 10870Sstevel@tonic-gate zone0.zone_vfslist = NULL; 10882267Sdp zone0.zone_bootargs = initargs; 10890Sstevel@tonic-gate zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 10900Sstevel@tonic-gate /* 10910Sstevel@tonic-gate * The global zone has all privileges 10920Sstevel@tonic-gate */ 10930Sstevel@tonic-gate priv_fillset(zone0.zone_privset); 10940Sstevel@tonic-gate /* 10950Sstevel@tonic-gate * Add p0 to the global zone 10960Sstevel@tonic-gate */ 10970Sstevel@tonic-gate zone0.zone_zsched = &p0; 10980Sstevel@tonic-gate p0.p_zone = &zone0; 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate /* 11021676Sjpk * Compute a hash value based on the contents of the label and the DOI. The 11031676Sjpk * hash algorithm is somewhat arbitrary, but is based on the observation that 11041676Sjpk * humans will likely pick labels that differ by amounts that work out to be 11051676Sjpk * multiples of the number of hash chains, and thus stirring in some primes 11061676Sjpk * should help. 11071676Sjpk */ 11081676Sjpk static uint_t 11091676Sjpk hash_bylabel(void *hdata, mod_hash_key_t key) 11101676Sjpk { 11111676Sjpk const ts_label_t *lab = (ts_label_t *)key; 11121676Sjpk const uint32_t *up, *ue; 11131676Sjpk uint_t hash; 11141676Sjpk int i; 11151676Sjpk 11161676Sjpk _NOTE(ARGUNUSED(hdata)); 11171676Sjpk 11181676Sjpk hash = lab->tsl_doi + (lab->tsl_doi << 1); 11191676Sjpk /* we depend on alignment of label, but not representation */ 11201676Sjpk up = (const uint32_t *)&lab->tsl_label; 11211676Sjpk ue = up + sizeof (lab->tsl_label) / sizeof (*up); 11221676Sjpk i = 1; 11231676Sjpk while (up < ue) { 11241676Sjpk /* using 2^n + 1, 1 <= n <= 16 as source of many primes */ 11251676Sjpk hash += *up + (*up << ((i % 16) + 1)); 11261676Sjpk up++; 11271676Sjpk i++; 11281676Sjpk } 11291676Sjpk return (hash); 11301676Sjpk } 11311676Sjpk 11321676Sjpk /* 11331676Sjpk * All that mod_hash cares about here is zero (equal) versus non-zero (not 11341676Sjpk * equal). This may need to be changed if less than / greater than is ever 11351676Sjpk * needed. 11361676Sjpk */ 11371676Sjpk static int 11381676Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 11391676Sjpk { 11401676Sjpk ts_label_t *lab1 = (ts_label_t *)key1; 11411676Sjpk ts_label_t *lab2 = (ts_label_t *)key2; 11421676Sjpk 11431676Sjpk return (label_equal(lab1, lab2) ? 0 : 1); 11441676Sjpk } 11451676Sjpk 11461676Sjpk /* 11470Sstevel@tonic-gate * Called by main() to initialize the zones framework. 11480Sstevel@tonic-gate */ 11490Sstevel@tonic-gate void 11500Sstevel@tonic-gate zone_init(void) 11510Sstevel@tonic-gate { 11520Sstevel@tonic-gate rctl_dict_entry_t *rde; 11530Sstevel@tonic-gate rctl_val_t *dval; 11540Sstevel@tonic-gate rctl_set_t *set; 11550Sstevel@tonic-gate rctl_alloc_gp_t *gp; 11560Sstevel@tonic-gate rctl_entity_p_t e; 11571166Sdstaff int res; 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate ASSERT(curproc == &p0); 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate /* 11620Sstevel@tonic-gate * Create ID space for zone IDs. ID 0 is reserved for the 11630Sstevel@tonic-gate * global zone. 11640Sstevel@tonic-gate */ 11650Sstevel@tonic-gate zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate /* 11680Sstevel@tonic-gate * Initialize generic zone resource controls, if any. 11690Sstevel@tonic-gate */ 11700Sstevel@tonic-gate rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 11710Sstevel@tonic-gate RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 11721996Sml93401 RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER, 11731996Sml93401 FSS_MAXSHARES, FSS_MAXSHARES, 11740Sstevel@tonic-gate &zone_cpu_shares_ops); 11750Sstevel@tonic-gate 11760Sstevel@tonic-gate rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 11770Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 11780Sstevel@tonic-gate INT_MAX, INT_MAX, &zone_lwps_ops); 11790Sstevel@tonic-gate /* 1180*2677Sml93401 * System V IPC resource controls 1181*2677Sml93401 */ 1182*2677Sml93401 rc_zone_msgmni = rctl_register("zone.max-msg-ids", 1183*2677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1184*2677Sml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops); 1185*2677Sml93401 1186*2677Sml93401 rc_zone_semmni = rctl_register("zone.max-sem-ids", 1187*2677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1188*2677Sml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops); 1189*2677Sml93401 1190*2677Sml93401 rc_zone_shmmni = rctl_register("zone.max-shm-ids", 1191*2677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1192*2677Sml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops); 1193*2677Sml93401 1194*2677Sml93401 rc_zone_shmmax = rctl_register("zone.max-shm-memory", 1195*2677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1196*2677Sml93401 RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops); 1197*2677Sml93401 1198*2677Sml93401 /* 11990Sstevel@tonic-gate * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 12000Sstevel@tonic-gate * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 12010Sstevel@tonic-gate */ 12020Sstevel@tonic-gate dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 12030Sstevel@tonic-gate bzero(dval, sizeof (rctl_val_t)); 12040Sstevel@tonic-gate dval->rcv_value = 1; 12050Sstevel@tonic-gate dval->rcv_privilege = RCPRIV_PRIVILEGED; 12060Sstevel@tonic-gate dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 12070Sstevel@tonic-gate dval->rcv_action_recip_pid = -1; 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate rde = rctl_dict_lookup("zone.cpu-shares"); 12100Sstevel@tonic-gate (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 12110Sstevel@tonic-gate 12120Sstevel@tonic-gate /* 12130Sstevel@tonic-gate * Initialize the ``global zone''. 12140Sstevel@tonic-gate */ 12150Sstevel@tonic-gate set = rctl_set_create(); 12160Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 12170Sstevel@tonic-gate mutex_enter(&p0.p_lock); 12180Sstevel@tonic-gate e.rcep_p.zone = &zone0; 12190Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 12200Sstevel@tonic-gate zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 12210Sstevel@tonic-gate gp); 12220Sstevel@tonic-gate 12230Sstevel@tonic-gate zone0.zone_nlwps = p0.p_lwpcnt; 12240Sstevel@tonic-gate zone0.zone_ntasks = 1; 12250Sstevel@tonic-gate mutex_exit(&p0.p_lock); 12260Sstevel@tonic-gate rctl_prealloc_destroy(gp); 12270Sstevel@tonic-gate /* 12280Sstevel@tonic-gate * pool_default hasn't been initialized yet, so we let pool_init() take 12290Sstevel@tonic-gate * care of making the global zone is in the default pool. 12300Sstevel@tonic-gate */ 12311676Sjpk 12321676Sjpk /* 12331676Sjpk * Initialize zone label. 12341676Sjpk * mlp are initialized when tnzonecfg is loaded. 12351676Sjpk */ 12361676Sjpk zone0.zone_slabel = l_admin_low; 12371676Sjpk rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 12381676Sjpk label_hold(l_admin_low); 12391676Sjpk 12400Sstevel@tonic-gate mutex_enter(&zonehash_lock); 12410Sstevel@tonic-gate zone_uniqid(&zone0); 12420Sstevel@tonic-gate ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 12431676Sjpk 12440Sstevel@tonic-gate zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 12450Sstevel@tonic-gate mod_hash_null_valdtor); 12460Sstevel@tonic-gate zonehashbyname = mod_hash_create_strhash("zone_by_name", 12470Sstevel@tonic-gate zone_hash_size, mod_hash_null_valdtor); 12481676Sjpk /* 12491676Sjpk * maintain zonehashbylabel only for labeled systems 12501676Sjpk */ 12511676Sjpk if (is_system_labeled()) 12521676Sjpk zonehashbylabel = mod_hash_create_extended("zone_by_label", 12531676Sjpk zone_hash_size, mod_hash_null_keydtor, 12541676Sjpk mod_hash_null_valdtor, hash_bylabel, NULL, 12551676Sjpk hash_labelkey_cmp, KM_SLEEP); 12560Sstevel@tonic-gate zonecount = 1; 12570Sstevel@tonic-gate 12580Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 12590Sstevel@tonic-gate (mod_hash_val_t)&zone0); 12600Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 12610Sstevel@tonic-gate (mod_hash_val_t)&zone0); 12621769Scarlsonj if (is_system_labeled()) { 12631769Scarlsonj zone0.zone_flags |= ZF_HASHED_LABEL; 12641676Sjpk (void) mod_hash_insert(zonehashbylabel, 12651676Sjpk (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0); 12661769Scarlsonj } 12671676Sjpk mutex_exit(&zonehash_lock); 12681676Sjpk 12690Sstevel@tonic-gate /* 12700Sstevel@tonic-gate * We avoid setting zone_kcred until now, since kcred is initialized 12710Sstevel@tonic-gate * sometime after zone_zsd_init() and before zone_init(). 12720Sstevel@tonic-gate */ 12730Sstevel@tonic-gate zone0.zone_kcred = kcred; 12740Sstevel@tonic-gate /* 12750Sstevel@tonic-gate * The global zone is fully initialized (except for zone_rootvp which 12760Sstevel@tonic-gate * will be set when the root filesystem is mounted). 12770Sstevel@tonic-gate */ 12780Sstevel@tonic-gate global_zone = &zone0; 12791166Sdstaff 12801166Sdstaff /* 12811166Sdstaff * Setup an event channel to send zone status change notifications on 12821166Sdstaff */ 12831166Sdstaff res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan, 12841166Sdstaff EVCH_CREAT); 12851166Sdstaff 12861166Sdstaff if (res) 12871166Sdstaff panic("Sysevent_evc_bind failed during zone setup.\n"); 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate static void 12910Sstevel@tonic-gate zone_free(zone_t *zone) 12920Sstevel@tonic-gate { 12930Sstevel@tonic-gate ASSERT(zone != global_zone); 12940Sstevel@tonic-gate ASSERT(zone->zone_ntasks == 0); 12950Sstevel@tonic-gate ASSERT(zone->zone_nlwps == 0); 12960Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 0); 12970Sstevel@tonic-gate ASSERT(zone->zone_kcred == NULL); 12980Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 12990Sstevel@tonic-gate zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate /* remove from deathrow list */ 13020Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_DEAD) { 13030Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 13040Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 13050Sstevel@tonic-gate list_remove(&zone_deathrow, zone); 13060Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 13070Sstevel@tonic-gate } 13080Sstevel@tonic-gate 13090Sstevel@tonic-gate zone_free_zsd(zone); 1310789Sahrens zone_free_datasets(zone); 13110Sstevel@tonic-gate 13120Sstevel@tonic-gate if (zone->zone_rootvp != NULL) 13130Sstevel@tonic-gate VN_RELE(zone->zone_rootvp); 13140Sstevel@tonic-gate if (zone->zone_rootpath) 13150Sstevel@tonic-gate kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 13160Sstevel@tonic-gate if (zone->zone_name != NULL) 13170Sstevel@tonic-gate kmem_free(zone->zone_name, ZONENAME_MAX); 13181676Sjpk if (zone->zone_slabel != NULL) 13191676Sjpk label_rele(zone->zone_slabel); 13200Sstevel@tonic-gate if (zone->zone_nodename != NULL) 13210Sstevel@tonic-gate kmem_free(zone->zone_nodename, _SYS_NMLN); 13220Sstevel@tonic-gate if (zone->zone_domain != NULL) 13230Sstevel@tonic-gate kmem_free(zone->zone_domain, _SYS_NMLN); 13240Sstevel@tonic-gate if (zone->zone_privset != NULL) 13250Sstevel@tonic-gate kmem_free(zone->zone_privset, sizeof (priv_set_t)); 13260Sstevel@tonic-gate if (zone->zone_rctls != NULL) 13270Sstevel@tonic-gate rctl_set_free(zone->zone_rctls); 13280Sstevel@tonic-gate if (zone->zone_bootargs != NULL) 13292267Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 13302267Sdp if (zone->zone_initname != NULL) 13312267Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 13320Sstevel@tonic-gate id_free(zoneid_space, zone->zone_id); 13330Sstevel@tonic-gate mutex_destroy(&zone->zone_lock); 13340Sstevel@tonic-gate cv_destroy(&zone->zone_cv); 13351676Sjpk rw_destroy(&zone->zone_mlps.mlpl_rwlock); 13360Sstevel@tonic-gate kmem_free(zone, sizeof (zone_t)); 13370Sstevel@tonic-gate } 13380Sstevel@tonic-gate 13390Sstevel@tonic-gate /* 13400Sstevel@tonic-gate * See block comment at the top of this file for information about zone 13410Sstevel@tonic-gate * status values. 13420Sstevel@tonic-gate */ 13430Sstevel@tonic-gate /* 13440Sstevel@tonic-gate * Convenience function for setting zone status. 13450Sstevel@tonic-gate */ 13460Sstevel@tonic-gate static void 13470Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status) 13480Sstevel@tonic-gate { 13491166Sdstaff 13501166Sdstaff nvlist_t *nvl = NULL; 13510Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zone_status_lock)); 13520Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 13530Sstevel@tonic-gate status >= zone_status_get(zone)); 13541166Sdstaff 13551166Sdstaff if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) || 13561166Sdstaff nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) || 13571166Sdstaff nvlist_add_string(nvl, ZONE_CB_NEWSTATE, 13582267Sdp zone_status_table[status]) || 13591166Sdstaff nvlist_add_string(nvl, ZONE_CB_OLDSTATE, 13602267Sdp zone_status_table[zone->zone_status]) || 13611166Sdstaff nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) || 13621166Sdstaff nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) || 13631166Sdstaff sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS, 13642267Sdp ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) { 13651166Sdstaff #ifdef DEBUG 13661166Sdstaff (void) printf( 13671166Sdstaff "Failed to allocate and send zone state change event.\n"); 13681166Sdstaff #endif 13691166Sdstaff } 13701166Sdstaff nvlist_free(nvl); 13711166Sdstaff 13720Sstevel@tonic-gate zone->zone_status = status; 13731166Sdstaff 13740Sstevel@tonic-gate cv_broadcast(&zone->zone_cv); 13750Sstevel@tonic-gate } 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate /* 13780Sstevel@tonic-gate * Public function to retrieve the zone status. The zone status may 13790Sstevel@tonic-gate * change after it is retrieved. 13800Sstevel@tonic-gate */ 13810Sstevel@tonic-gate zone_status_t 13820Sstevel@tonic-gate zone_status_get(zone_t *zone) 13830Sstevel@tonic-gate { 13840Sstevel@tonic-gate return (zone->zone_status); 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate static int 13880Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 13890Sstevel@tonic-gate { 13902267Sdp char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP); 13912267Sdp int err = 0; 13922267Sdp 13932267Sdp ASSERT(zone != global_zone); 13942267Sdp if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0) 13952267Sdp goto done; /* EFAULT or ENAMETOOLONG */ 13962267Sdp 13972267Sdp if (zone->zone_bootargs != NULL) 13982267Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 13992267Sdp 14002267Sdp zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP); 14012267Sdp (void) strcpy(zone->zone_bootargs, bootargs); 14022267Sdp 14032267Sdp done: 14042267Sdp kmem_free(bootargs, BOOTARGS_MAX); 14052267Sdp return (err); 14062267Sdp } 14072267Sdp 14082267Sdp static int 14092267Sdp zone_set_initname(zone_t *zone, const char *zone_initname) 14102267Sdp { 14112267Sdp char initname[INITNAME_SZ]; 14120Sstevel@tonic-gate size_t len; 14132267Sdp int err = 0; 14142267Sdp 14152267Sdp ASSERT(zone != global_zone); 14162267Sdp if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0) 14170Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 14182267Sdp 14192267Sdp if (zone->zone_initname != NULL) 14202267Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 14212267Sdp 14222267Sdp zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP); 14232267Sdp (void) strcpy(zone->zone_initname, initname); 14240Sstevel@tonic-gate return (0); 14250Sstevel@tonic-gate } 14260Sstevel@tonic-gate 14270Sstevel@tonic-gate /* 14280Sstevel@tonic-gate * Block indefinitely waiting for (zone_status >= status) 14290Sstevel@tonic-gate */ 14300Sstevel@tonic-gate void 14310Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status) 14320Sstevel@tonic-gate { 14330Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate mutex_enter(&zone_status_lock); 14360Sstevel@tonic-gate while (zone->zone_status < status) { 14370Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 14380Sstevel@tonic-gate } 14390Sstevel@tonic-gate mutex_exit(&zone_status_lock); 14400Sstevel@tonic-gate } 14410Sstevel@tonic-gate 14420Sstevel@tonic-gate /* 14430Sstevel@tonic-gate * Private CPR-safe version of zone_status_wait(). 14440Sstevel@tonic-gate */ 14450Sstevel@tonic-gate static void 14460Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 14470Sstevel@tonic-gate { 14480Sstevel@tonic-gate callb_cpr_t cprinfo; 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 14530Sstevel@tonic-gate str); 14540Sstevel@tonic-gate mutex_enter(&zone_status_lock); 14550Sstevel@tonic-gate while (zone->zone_status < status) { 14560Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 14570Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 14580Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 14590Sstevel@tonic-gate } 14600Sstevel@tonic-gate /* 14610Sstevel@tonic-gate * zone_status_lock is implicitly released by the following. 14620Sstevel@tonic-gate */ 14630Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 14640Sstevel@tonic-gate } 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate /* 14670Sstevel@tonic-gate * Block until zone enters requested state or signal is received. Return (0) 14680Sstevel@tonic-gate * if signaled, non-zero otherwise. 14690Sstevel@tonic-gate */ 14700Sstevel@tonic-gate int 14710Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status) 14720Sstevel@tonic-gate { 14730Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 14740Sstevel@tonic-gate 14750Sstevel@tonic-gate mutex_enter(&zone_status_lock); 14760Sstevel@tonic-gate while (zone->zone_status < status) { 14770Sstevel@tonic-gate if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 14780Sstevel@tonic-gate mutex_exit(&zone_status_lock); 14790Sstevel@tonic-gate return (0); 14800Sstevel@tonic-gate } 14810Sstevel@tonic-gate } 14820Sstevel@tonic-gate mutex_exit(&zone_status_lock); 14830Sstevel@tonic-gate return (1); 14840Sstevel@tonic-gate } 14850Sstevel@tonic-gate 14860Sstevel@tonic-gate /* 14870Sstevel@tonic-gate * Block until the zone enters the requested state or the timeout expires, 14880Sstevel@tonic-gate * whichever happens first. Return (-1) if operation timed out, time remaining 14890Sstevel@tonic-gate * otherwise. 14900Sstevel@tonic-gate */ 14910Sstevel@tonic-gate clock_t 14920Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 14930Sstevel@tonic-gate { 14940Sstevel@tonic-gate clock_t timeleft = 0; 14950Sstevel@tonic-gate 14960Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 14970Sstevel@tonic-gate 14980Sstevel@tonic-gate mutex_enter(&zone_status_lock); 14990Sstevel@tonic-gate while (zone->zone_status < status && timeleft != -1) { 15000Sstevel@tonic-gate timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 15010Sstevel@tonic-gate } 15020Sstevel@tonic-gate mutex_exit(&zone_status_lock); 15030Sstevel@tonic-gate return (timeleft); 15040Sstevel@tonic-gate } 15050Sstevel@tonic-gate 15060Sstevel@tonic-gate /* 15070Sstevel@tonic-gate * Block until the zone enters the requested state, the current process is 15080Sstevel@tonic-gate * signaled, or the timeout expires, whichever happens first. Return (-1) if 15090Sstevel@tonic-gate * operation timed out, 0 if signaled, time remaining otherwise. 15100Sstevel@tonic-gate */ 15110Sstevel@tonic-gate clock_t 15120Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 15130Sstevel@tonic-gate { 15140Sstevel@tonic-gate clock_t timeleft = tim - lbolt; 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate mutex_enter(&zone_status_lock); 15190Sstevel@tonic-gate while (zone->zone_status < status) { 15200Sstevel@tonic-gate timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 15210Sstevel@tonic-gate tim); 15220Sstevel@tonic-gate if (timeleft <= 0) 15230Sstevel@tonic-gate break; 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate mutex_exit(&zone_status_lock); 15260Sstevel@tonic-gate return (timeleft); 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate 15290Sstevel@tonic-gate /* 15300Sstevel@tonic-gate * Zones have two reference counts: one for references from credential 15310Sstevel@tonic-gate * structures (zone_cred_ref), and one (zone_ref) for everything else. 15320Sstevel@tonic-gate * This is so we can allow a zone to be rebooted while there are still 15330Sstevel@tonic-gate * outstanding cred references, since certain drivers cache dblks (which 15340Sstevel@tonic-gate * implicitly results in cached creds). We wait for zone_ref to drop to 15350Sstevel@tonic-gate * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 15360Sstevel@tonic-gate * later freed when the zone_cred_ref drops to 0, though nothing other 15370Sstevel@tonic-gate * than the zone id and privilege set should be accessed once the zone 15380Sstevel@tonic-gate * is "dead". 15390Sstevel@tonic-gate * 15400Sstevel@tonic-gate * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 15410Sstevel@tonic-gate * to force halt/reboot to block waiting for the zone_cred_ref to drop 15420Sstevel@tonic-gate * to 0. This can be useful to flush out other sources of cached creds 15430Sstevel@tonic-gate * that may be less innocuous than the driver case. 15440Sstevel@tonic-gate */ 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate int zone_wait_for_cred = 0; 15470Sstevel@tonic-gate 15480Sstevel@tonic-gate static void 15490Sstevel@tonic-gate zone_hold_locked(zone_t *z) 15500Sstevel@tonic-gate { 15510Sstevel@tonic-gate ASSERT(MUTEX_HELD(&z->zone_lock)); 15520Sstevel@tonic-gate z->zone_ref++; 15530Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 15540Sstevel@tonic-gate } 15550Sstevel@tonic-gate 15560Sstevel@tonic-gate void 15570Sstevel@tonic-gate zone_hold(zone_t *z) 15580Sstevel@tonic-gate { 15590Sstevel@tonic-gate mutex_enter(&z->zone_lock); 15600Sstevel@tonic-gate zone_hold_locked(z); 15610Sstevel@tonic-gate mutex_exit(&z->zone_lock); 15620Sstevel@tonic-gate } 15630Sstevel@tonic-gate 15640Sstevel@tonic-gate /* 15650Sstevel@tonic-gate * If the non-cred ref count drops to 1 and either the cred ref count 15660Sstevel@tonic-gate * is 0 or we aren't waiting for cred references, the zone is ready to 15670Sstevel@tonic-gate * be destroyed. 15680Sstevel@tonic-gate */ 15690Sstevel@tonic-gate #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 15700Sstevel@tonic-gate (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate void 15730Sstevel@tonic-gate zone_rele(zone_t *z) 15740Sstevel@tonic-gate { 15750Sstevel@tonic-gate boolean_t wakeup; 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate mutex_enter(&z->zone_lock); 15780Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 15790Sstevel@tonic-gate z->zone_ref--; 15800Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 15810Sstevel@tonic-gate /* no more refs, free the structure */ 15820Sstevel@tonic-gate mutex_exit(&z->zone_lock); 15830Sstevel@tonic-gate zone_free(z); 15840Sstevel@tonic-gate return; 15850Sstevel@tonic-gate } 15860Sstevel@tonic-gate /* signal zone_destroy so the zone can finish halting */ 15870Sstevel@tonic-gate wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 15880Sstevel@tonic-gate mutex_exit(&z->zone_lock); 15890Sstevel@tonic-gate 15900Sstevel@tonic-gate if (wakeup) { 15910Sstevel@tonic-gate /* 15920Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 15930Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 15940Sstevel@tonic-gate */ 15950Sstevel@tonic-gate mutex_enter(&zonehash_lock); 15960Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 15970Sstevel@tonic-gate mutex_exit(&zonehash_lock); 15980Sstevel@tonic-gate } 15990Sstevel@tonic-gate } 16000Sstevel@tonic-gate 16010Sstevel@tonic-gate void 16020Sstevel@tonic-gate zone_cred_hold(zone_t *z) 16030Sstevel@tonic-gate { 16040Sstevel@tonic-gate mutex_enter(&z->zone_lock); 16050Sstevel@tonic-gate z->zone_cred_ref++; 16060Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 16070Sstevel@tonic-gate mutex_exit(&z->zone_lock); 16080Sstevel@tonic-gate } 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate void 16110Sstevel@tonic-gate zone_cred_rele(zone_t *z) 16120Sstevel@tonic-gate { 16130Sstevel@tonic-gate boolean_t wakeup; 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate mutex_enter(&z->zone_lock); 16160Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 16170Sstevel@tonic-gate z->zone_cred_ref--; 16180Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 16190Sstevel@tonic-gate /* no more refs, free the structure */ 16200Sstevel@tonic-gate mutex_exit(&z->zone_lock); 16210Sstevel@tonic-gate zone_free(z); 16220Sstevel@tonic-gate return; 16230Sstevel@tonic-gate } 16240Sstevel@tonic-gate /* 16250Sstevel@tonic-gate * If zone_destroy is waiting for the cred references to drain 16260Sstevel@tonic-gate * out, and they have, signal it. 16270Sstevel@tonic-gate */ 16280Sstevel@tonic-gate wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 16290Sstevel@tonic-gate zone_status_get(z) >= ZONE_IS_DEAD); 16300Sstevel@tonic-gate mutex_exit(&z->zone_lock); 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate if (wakeup) { 16330Sstevel@tonic-gate /* 16340Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 16350Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 16360Sstevel@tonic-gate */ 16370Sstevel@tonic-gate mutex_enter(&zonehash_lock); 16380Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 16390Sstevel@tonic-gate mutex_exit(&zonehash_lock); 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate } 16420Sstevel@tonic-gate 16430Sstevel@tonic-gate void 16440Sstevel@tonic-gate zone_task_hold(zone_t *z) 16450Sstevel@tonic-gate { 16460Sstevel@tonic-gate mutex_enter(&z->zone_lock); 16470Sstevel@tonic-gate z->zone_ntasks++; 16480Sstevel@tonic-gate ASSERT(z->zone_ntasks != 0); 16490Sstevel@tonic-gate mutex_exit(&z->zone_lock); 16500Sstevel@tonic-gate } 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate void 16530Sstevel@tonic-gate zone_task_rele(zone_t *zone) 16540Sstevel@tonic-gate { 16550Sstevel@tonic-gate uint_t refcnt; 16560Sstevel@tonic-gate 16570Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 16580Sstevel@tonic-gate ASSERT(zone->zone_ntasks != 0); 16590Sstevel@tonic-gate refcnt = --zone->zone_ntasks; 16600Sstevel@tonic-gate if (refcnt > 1) { /* Common case */ 16610Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 16620Sstevel@tonic-gate return; 16630Sstevel@tonic-gate } 16640Sstevel@tonic-gate zone_hold_locked(zone); /* so we can use the zone_t later */ 16650Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 16660Sstevel@tonic-gate if (refcnt == 1) { 16670Sstevel@tonic-gate /* 16680Sstevel@tonic-gate * See if the zone is shutting down. 16690Sstevel@tonic-gate */ 16700Sstevel@tonic-gate mutex_enter(&zone_status_lock); 16710Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 16720Sstevel@tonic-gate goto out; 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate /* 16760Sstevel@tonic-gate * Make sure the ntasks didn't change since we 16770Sstevel@tonic-gate * dropped zone_lock. 16780Sstevel@tonic-gate */ 16790Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 16800Sstevel@tonic-gate if (refcnt != zone->zone_ntasks) { 16810Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 16820Sstevel@tonic-gate goto out; 16830Sstevel@tonic-gate } 16840Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 16850Sstevel@tonic-gate 16860Sstevel@tonic-gate /* 16870Sstevel@tonic-gate * No more user processes in the zone. The zone is empty. 16880Sstevel@tonic-gate */ 16890Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 16900Sstevel@tonic-gate goto out; 16910Sstevel@tonic-gate } 16920Sstevel@tonic-gate 16930Sstevel@tonic-gate ASSERT(refcnt == 0); 16940Sstevel@tonic-gate /* 16950Sstevel@tonic-gate * zsched has exited; the zone is dead. 16960Sstevel@tonic-gate */ 16970Sstevel@tonic-gate zone->zone_zsched = NULL; /* paranoia */ 16980Sstevel@tonic-gate mutex_enter(&zone_status_lock); 16990Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DEAD); 17000Sstevel@tonic-gate out: 17010Sstevel@tonic-gate mutex_exit(&zone_status_lock); 17020Sstevel@tonic-gate zone_rele(zone); 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate zoneid_t 17060Sstevel@tonic-gate getzoneid(void) 17070Sstevel@tonic-gate { 17080Sstevel@tonic-gate return (curproc->p_zone->zone_id); 17090Sstevel@tonic-gate } 17100Sstevel@tonic-gate 17110Sstevel@tonic-gate /* 17120Sstevel@tonic-gate * Internal versions of zone_find_by_*(). These don't zone_hold() or 17130Sstevel@tonic-gate * check the validity of a zone's state. 17140Sstevel@tonic-gate */ 17150Sstevel@tonic-gate static zone_t * 17160Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid) 17170Sstevel@tonic-gate { 17180Sstevel@tonic-gate mod_hash_val_t hv; 17190Sstevel@tonic-gate zone_t *zone = NULL; 17200Sstevel@tonic-gate 17210Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 17220Sstevel@tonic-gate 17230Sstevel@tonic-gate if (mod_hash_find(zonehashbyid, 17240Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 17250Sstevel@tonic-gate zone = (zone_t *)hv; 17260Sstevel@tonic-gate return (zone); 17270Sstevel@tonic-gate } 17280Sstevel@tonic-gate 17290Sstevel@tonic-gate static zone_t * 17301676Sjpk zone_find_all_by_label(const ts_label_t *label) 17311676Sjpk { 17321676Sjpk mod_hash_val_t hv; 17331676Sjpk zone_t *zone = NULL; 17341676Sjpk 17351676Sjpk ASSERT(MUTEX_HELD(&zonehash_lock)); 17361676Sjpk 17371676Sjpk /* 17381676Sjpk * zonehashbylabel is not maintained for unlabeled systems 17391676Sjpk */ 17401676Sjpk if (!is_system_labeled()) 17411676Sjpk return (NULL); 17421676Sjpk if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0) 17431676Sjpk zone = (zone_t *)hv; 17441676Sjpk return (zone); 17451676Sjpk } 17461676Sjpk 17471676Sjpk static zone_t * 17480Sstevel@tonic-gate zone_find_all_by_name(char *name) 17490Sstevel@tonic-gate { 17500Sstevel@tonic-gate mod_hash_val_t hv; 17510Sstevel@tonic-gate zone_t *zone = NULL; 17520Sstevel@tonic-gate 17530Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 17540Sstevel@tonic-gate 17550Sstevel@tonic-gate if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 17560Sstevel@tonic-gate zone = (zone_t *)hv; 17570Sstevel@tonic-gate return (zone); 17580Sstevel@tonic-gate } 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate /* 17610Sstevel@tonic-gate * Public interface for looking up a zone by zoneid. Only returns the zone if 17620Sstevel@tonic-gate * it is fully initialized, and has not yet begun the zone_destroy() sequence. 17630Sstevel@tonic-gate * Caller must call zone_rele() once it is done with the zone. 17640Sstevel@tonic-gate * 17650Sstevel@tonic-gate * The zone may begin the zone_destroy() sequence immediately after this 17660Sstevel@tonic-gate * function returns, but may be safely used until zone_rele() is called. 17670Sstevel@tonic-gate */ 17680Sstevel@tonic-gate zone_t * 17690Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid) 17700Sstevel@tonic-gate { 17710Sstevel@tonic-gate zone_t *zone; 17720Sstevel@tonic-gate zone_status_t status; 17730Sstevel@tonic-gate 17740Sstevel@tonic-gate mutex_enter(&zonehash_lock); 17750Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 17760Sstevel@tonic-gate mutex_exit(&zonehash_lock); 17770Sstevel@tonic-gate return (NULL); 17780Sstevel@tonic-gate } 17790Sstevel@tonic-gate status = zone_status_get(zone); 17800Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 17810Sstevel@tonic-gate /* 17820Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 17830Sstevel@tonic-gate */ 17840Sstevel@tonic-gate mutex_exit(&zonehash_lock); 17850Sstevel@tonic-gate return (NULL); 17860Sstevel@tonic-gate } 17870Sstevel@tonic-gate zone_hold(zone); 17880Sstevel@tonic-gate mutex_exit(&zonehash_lock); 17890Sstevel@tonic-gate return (zone); 17900Sstevel@tonic-gate } 17910Sstevel@tonic-gate 17920Sstevel@tonic-gate /* 17931676Sjpk * Similar to zone_find_by_id, but using zone label as the key. 17941676Sjpk */ 17951676Sjpk zone_t * 17961676Sjpk zone_find_by_label(const ts_label_t *label) 17971676Sjpk { 17981676Sjpk zone_t *zone; 17992110Srica zone_status_t status; 18001676Sjpk 18011676Sjpk mutex_enter(&zonehash_lock); 18021676Sjpk if ((zone = zone_find_all_by_label(label)) == NULL) { 18031676Sjpk mutex_exit(&zonehash_lock); 18041676Sjpk return (NULL); 18051676Sjpk } 18062110Srica 18072110Srica status = zone_status_get(zone); 18082110Srica if (status > ZONE_IS_DOWN) { 18091676Sjpk /* 18101676Sjpk * For all practical purposes the zone doesn't exist. 18111676Sjpk */ 18122110Srica mutex_exit(&zonehash_lock); 18132110Srica return (NULL); 18141676Sjpk } 18152110Srica zone_hold(zone); 18161676Sjpk mutex_exit(&zonehash_lock); 18171676Sjpk return (zone); 18181676Sjpk } 18191676Sjpk 18201676Sjpk /* 18210Sstevel@tonic-gate * Similar to zone_find_by_id, but using zone name as the key. 18220Sstevel@tonic-gate */ 18230Sstevel@tonic-gate zone_t * 18240Sstevel@tonic-gate zone_find_by_name(char *name) 18250Sstevel@tonic-gate { 18260Sstevel@tonic-gate zone_t *zone; 18270Sstevel@tonic-gate zone_status_t status; 18280Sstevel@tonic-gate 18290Sstevel@tonic-gate mutex_enter(&zonehash_lock); 18300Sstevel@tonic-gate if ((zone = zone_find_all_by_name(name)) == NULL) { 18310Sstevel@tonic-gate mutex_exit(&zonehash_lock); 18320Sstevel@tonic-gate return (NULL); 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate status = zone_status_get(zone); 18350Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 18360Sstevel@tonic-gate /* 18370Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 18380Sstevel@tonic-gate */ 18390Sstevel@tonic-gate mutex_exit(&zonehash_lock); 18400Sstevel@tonic-gate return (NULL); 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate zone_hold(zone); 18430Sstevel@tonic-gate mutex_exit(&zonehash_lock); 18440Sstevel@tonic-gate return (zone); 18450Sstevel@tonic-gate } 18460Sstevel@tonic-gate 18470Sstevel@tonic-gate /* 18480Sstevel@tonic-gate * Similar to zone_find_by_id(), using the path as a key. For instance, 18490Sstevel@tonic-gate * if there is a zone "foo" rooted at /foo/root, and the path argument 18500Sstevel@tonic-gate * is "/foo/root/proc", it will return the held zone_t corresponding to 18510Sstevel@tonic-gate * zone "foo". 18520Sstevel@tonic-gate * 18530Sstevel@tonic-gate * zone_find_by_path() always returns a non-NULL value, since at the 18540Sstevel@tonic-gate * very least every path will be contained in the global zone. 18550Sstevel@tonic-gate * 18560Sstevel@tonic-gate * As with the other zone_find_by_*() functions, the caller is 18570Sstevel@tonic-gate * responsible for zone_rele()ing the return value of this function. 18580Sstevel@tonic-gate */ 18590Sstevel@tonic-gate zone_t * 18600Sstevel@tonic-gate zone_find_by_path(const char *path) 18610Sstevel@tonic-gate { 18620Sstevel@tonic-gate zone_t *zone; 18630Sstevel@tonic-gate zone_t *zret = NULL; 18640Sstevel@tonic-gate zone_status_t status; 18650Sstevel@tonic-gate 18660Sstevel@tonic-gate if (path == NULL) { 18670Sstevel@tonic-gate /* 18680Sstevel@tonic-gate * Call from rootconf(). 18690Sstevel@tonic-gate */ 18700Sstevel@tonic-gate zone_hold(global_zone); 18710Sstevel@tonic-gate return (global_zone); 18720Sstevel@tonic-gate } 18730Sstevel@tonic-gate ASSERT(*path == '/'); 18740Sstevel@tonic-gate mutex_enter(&zonehash_lock); 18750Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 18760Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 18770Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(path, zone)) 18780Sstevel@tonic-gate zret = zone; 18790Sstevel@tonic-gate } 18800Sstevel@tonic-gate ASSERT(zret != NULL); 18810Sstevel@tonic-gate status = zone_status_get(zret); 18820Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 18830Sstevel@tonic-gate /* 18840Sstevel@tonic-gate * Zone practically doesn't exist. 18850Sstevel@tonic-gate */ 18860Sstevel@tonic-gate zret = global_zone; 18870Sstevel@tonic-gate } 18880Sstevel@tonic-gate zone_hold(zret); 18890Sstevel@tonic-gate mutex_exit(&zonehash_lock); 18900Sstevel@tonic-gate return (zret); 18910Sstevel@tonic-gate } 18920Sstevel@tonic-gate 18930Sstevel@tonic-gate /* 18940Sstevel@tonic-gate * Get the number of cpus visible to this zone. The system-wide global 18950Sstevel@tonic-gate * 'ncpus' is returned if pools are disabled, the caller is in the 18960Sstevel@tonic-gate * global zone, or a NULL zone argument is passed in. 18970Sstevel@tonic-gate */ 18980Sstevel@tonic-gate int 18990Sstevel@tonic-gate zone_ncpus_get(zone_t *zone) 19000Sstevel@tonic-gate { 19010Sstevel@tonic-gate int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 19020Sstevel@tonic-gate 19030Sstevel@tonic-gate return (myncpus != 0 ? myncpus : ncpus); 19040Sstevel@tonic-gate } 19050Sstevel@tonic-gate 19060Sstevel@tonic-gate /* 19070Sstevel@tonic-gate * Get the number of online cpus visible to this zone. The system-wide 19080Sstevel@tonic-gate * global 'ncpus_online' is returned if pools are disabled, the caller 19090Sstevel@tonic-gate * is in the global zone, or a NULL zone argument is passed in. 19100Sstevel@tonic-gate */ 19110Sstevel@tonic-gate int 19120Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone) 19130Sstevel@tonic-gate { 19140Sstevel@tonic-gate int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 19150Sstevel@tonic-gate 19160Sstevel@tonic-gate return (myncpus_online != 0 ? myncpus_online : ncpus_online); 19170Sstevel@tonic-gate } 19180Sstevel@tonic-gate 19190Sstevel@tonic-gate /* 19200Sstevel@tonic-gate * Return the pool to which the zone is currently bound. 19210Sstevel@tonic-gate */ 19220Sstevel@tonic-gate pool_t * 19230Sstevel@tonic-gate zone_pool_get(zone_t *zone) 19240Sstevel@tonic-gate { 19250Sstevel@tonic-gate ASSERT(pool_lock_held()); 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate return (zone->zone_pool); 19280Sstevel@tonic-gate } 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate /* 19310Sstevel@tonic-gate * Set the zone's pool pointer and update the zone's visibility to match 19320Sstevel@tonic-gate * the resources in the new pool. 19330Sstevel@tonic-gate */ 19340Sstevel@tonic-gate void 19350Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool) 19360Sstevel@tonic-gate { 19370Sstevel@tonic-gate ASSERT(pool_lock_held()); 19380Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 19390Sstevel@tonic-gate 19400Sstevel@tonic-gate zone->zone_pool = pool; 19410Sstevel@tonic-gate zone_pset_set(zone, pool->pool_pset->pset_id); 19420Sstevel@tonic-gate } 19430Sstevel@tonic-gate 19440Sstevel@tonic-gate /* 19450Sstevel@tonic-gate * Return the cached value of the id of the processor set to which the 19460Sstevel@tonic-gate * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 19470Sstevel@tonic-gate * facility is disabled. 19480Sstevel@tonic-gate */ 19490Sstevel@tonic-gate psetid_t 19500Sstevel@tonic-gate zone_pset_get(zone_t *zone) 19510Sstevel@tonic-gate { 19520Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate return (zone->zone_psetid); 19550Sstevel@tonic-gate } 19560Sstevel@tonic-gate 19570Sstevel@tonic-gate /* 19580Sstevel@tonic-gate * Set the cached value of the id of the processor set to which the zone 19590Sstevel@tonic-gate * is currently bound. Also update the zone's visibility to match the 19600Sstevel@tonic-gate * resources in the new processor set. 19610Sstevel@tonic-gate */ 19620Sstevel@tonic-gate void 19630Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid) 19640Sstevel@tonic-gate { 19650Sstevel@tonic-gate psetid_t oldpsetid; 19660Sstevel@tonic-gate 19670Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 19680Sstevel@tonic-gate oldpsetid = zone_pset_get(zone); 19690Sstevel@tonic-gate 19700Sstevel@tonic-gate if (oldpsetid == newpsetid) 19710Sstevel@tonic-gate return; 19720Sstevel@tonic-gate /* 19730Sstevel@tonic-gate * Global zone sees all. 19740Sstevel@tonic-gate */ 19750Sstevel@tonic-gate if (zone != global_zone) { 19760Sstevel@tonic-gate zone->zone_psetid = newpsetid; 19770Sstevel@tonic-gate if (newpsetid != ZONE_PS_INVAL) 19780Sstevel@tonic-gate pool_pset_visibility_add(newpsetid, zone); 19790Sstevel@tonic-gate if (oldpsetid != ZONE_PS_INVAL) 19800Sstevel@tonic-gate pool_pset_visibility_remove(oldpsetid, zone); 19810Sstevel@tonic-gate } 19820Sstevel@tonic-gate /* 19830Sstevel@tonic-gate * Disabling pools, so we should start using the global values 19840Sstevel@tonic-gate * for ncpus and ncpus_online. 19850Sstevel@tonic-gate */ 19860Sstevel@tonic-gate if (newpsetid == ZONE_PS_INVAL) { 19870Sstevel@tonic-gate zone->zone_ncpus = 0; 19880Sstevel@tonic-gate zone->zone_ncpus_online = 0; 19890Sstevel@tonic-gate } 19900Sstevel@tonic-gate } 19910Sstevel@tonic-gate 19920Sstevel@tonic-gate /* 19930Sstevel@tonic-gate * Walk the list of active zones and issue the provided callback for 19940Sstevel@tonic-gate * each of them. 19950Sstevel@tonic-gate * 19960Sstevel@tonic-gate * Caller must not be holding any locks that may be acquired under 19970Sstevel@tonic-gate * zonehash_lock. See comment at the beginning of the file for a list of 19980Sstevel@tonic-gate * common locks and their interactions with zones. 19990Sstevel@tonic-gate */ 20000Sstevel@tonic-gate int 20010Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data) 20020Sstevel@tonic-gate { 20030Sstevel@tonic-gate zone_t *zone; 20040Sstevel@tonic-gate int ret = 0; 20050Sstevel@tonic-gate zone_status_t status; 20060Sstevel@tonic-gate 20070Sstevel@tonic-gate mutex_enter(&zonehash_lock); 20080Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 20090Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 20100Sstevel@tonic-gate /* 20110Sstevel@tonic-gate * Skip zones that shouldn't be externally visible. 20120Sstevel@tonic-gate */ 20130Sstevel@tonic-gate status = zone_status_get(zone); 20140Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 20150Sstevel@tonic-gate continue; 20160Sstevel@tonic-gate /* 20170Sstevel@tonic-gate * Bail immediately if any callback invocation returns a 20180Sstevel@tonic-gate * non-zero value. 20190Sstevel@tonic-gate */ 20200Sstevel@tonic-gate ret = (*cb)(zone, data); 20210Sstevel@tonic-gate if (ret != 0) 20220Sstevel@tonic-gate break; 20230Sstevel@tonic-gate } 20240Sstevel@tonic-gate mutex_exit(&zonehash_lock); 20250Sstevel@tonic-gate return (ret); 20260Sstevel@tonic-gate } 20270Sstevel@tonic-gate 20280Sstevel@tonic-gate static int 20290Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath) 20300Sstevel@tonic-gate { 20310Sstevel@tonic-gate vnode_t *vp; 20320Sstevel@tonic-gate int trycount; 20330Sstevel@tonic-gate int error = 0; 20340Sstevel@tonic-gate char *path; 20350Sstevel@tonic-gate struct pathname upn, pn; 20360Sstevel@tonic-gate size_t pathlen; 20370Sstevel@tonic-gate 20380Sstevel@tonic-gate if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 20390Sstevel@tonic-gate return (error); 20400Sstevel@tonic-gate 20410Sstevel@tonic-gate pn_alloc(&pn); 20420Sstevel@tonic-gate 20430Sstevel@tonic-gate /* prevent infinite loop */ 20440Sstevel@tonic-gate trycount = 10; 20450Sstevel@tonic-gate for (;;) { 20460Sstevel@tonic-gate if (--trycount <= 0) { 20470Sstevel@tonic-gate error = ESTALE; 20480Sstevel@tonic-gate goto out; 20490Sstevel@tonic-gate } 20500Sstevel@tonic-gate 20510Sstevel@tonic-gate if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 20520Sstevel@tonic-gate /* 20530Sstevel@tonic-gate * VOP_ACCESS() may cover 'vp' with a new 20540Sstevel@tonic-gate * filesystem, if 'vp' is an autoFS vnode. 20550Sstevel@tonic-gate * Get the new 'vp' if so. 20560Sstevel@tonic-gate */ 20570Sstevel@tonic-gate if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 && 20580Sstevel@tonic-gate (vp->v_vfsmountedhere == NULL || 20590Sstevel@tonic-gate (error = traverse(&vp)) == 0)) { 20600Sstevel@tonic-gate pathlen = pn.pn_pathlen + 2; 20610Sstevel@tonic-gate path = kmem_alloc(pathlen, KM_SLEEP); 20620Sstevel@tonic-gate (void) strncpy(path, pn.pn_path, 20630Sstevel@tonic-gate pn.pn_pathlen + 1); 20640Sstevel@tonic-gate path[pathlen - 2] = '/'; 20650Sstevel@tonic-gate path[pathlen - 1] = '\0'; 20660Sstevel@tonic-gate pn_free(&pn); 20670Sstevel@tonic-gate pn_free(&upn); 20680Sstevel@tonic-gate 20690Sstevel@tonic-gate /* Success! */ 20700Sstevel@tonic-gate break; 20710Sstevel@tonic-gate } 20720Sstevel@tonic-gate VN_RELE(vp); 20730Sstevel@tonic-gate } 20740Sstevel@tonic-gate if (error != ESTALE) 20750Sstevel@tonic-gate goto out; 20760Sstevel@tonic-gate } 20770Sstevel@tonic-gate 20780Sstevel@tonic-gate ASSERT(error == 0); 20790Sstevel@tonic-gate zone->zone_rootvp = vp; /* we hold a reference to vp */ 20800Sstevel@tonic-gate zone->zone_rootpath = path; 20810Sstevel@tonic-gate zone->zone_rootpathlen = pathlen; 20821769Scarlsonj if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0) 20831769Scarlsonj zone->zone_flags |= ZF_IS_SCRATCH; 20840Sstevel@tonic-gate return (0); 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate out: 20870Sstevel@tonic-gate pn_free(&pn); 20880Sstevel@tonic-gate pn_free(&upn); 20890Sstevel@tonic-gate return (error); 20900Sstevel@tonic-gate } 20910Sstevel@tonic-gate 20920Sstevel@tonic-gate #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 20930Sstevel@tonic-gate ((c) >= 'a' && (c) <= 'z') || \ 20940Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'Z')) 20950Sstevel@tonic-gate 20960Sstevel@tonic-gate static int 20970Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname) 20980Sstevel@tonic-gate { 20990Sstevel@tonic-gate char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 21000Sstevel@tonic-gate size_t len; 21010Sstevel@tonic-gate int i, err; 21020Sstevel@tonic-gate 21030Sstevel@tonic-gate if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 21040Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 21050Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 21060Sstevel@tonic-gate } 21070Sstevel@tonic-gate 21080Sstevel@tonic-gate /* must be less than ZONENAME_MAX */ 21090Sstevel@tonic-gate if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 21100Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 21110Sstevel@tonic-gate return (EINVAL); 21120Sstevel@tonic-gate } 21130Sstevel@tonic-gate 21140Sstevel@tonic-gate /* 21150Sstevel@tonic-gate * Name must start with an alphanumeric and must contain only 21160Sstevel@tonic-gate * alphanumerics, '-', '_' and '.'. 21170Sstevel@tonic-gate */ 21180Sstevel@tonic-gate if (!isalnum(kname[0])) { 21190Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 21200Sstevel@tonic-gate return (EINVAL); 21210Sstevel@tonic-gate } 21220Sstevel@tonic-gate for (i = 1; i < len - 1; i++) { 21230Sstevel@tonic-gate if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 21240Sstevel@tonic-gate kname[i] != '.') { 21250Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 21260Sstevel@tonic-gate return (EINVAL); 21270Sstevel@tonic-gate } 21280Sstevel@tonic-gate } 21290Sstevel@tonic-gate 21300Sstevel@tonic-gate zone->zone_name = kname; 21310Sstevel@tonic-gate return (0); 21320Sstevel@tonic-gate } 21330Sstevel@tonic-gate 21340Sstevel@tonic-gate /* 21350Sstevel@tonic-gate * Similar to thread_create(), but makes sure the thread is in the appropriate 21360Sstevel@tonic-gate * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 21370Sstevel@tonic-gate */ 21380Sstevel@tonic-gate /*ARGSUSED*/ 21390Sstevel@tonic-gate kthread_t * 21400Sstevel@tonic-gate zthread_create( 21410Sstevel@tonic-gate caddr_t stk, 21420Sstevel@tonic-gate size_t stksize, 21430Sstevel@tonic-gate void (*proc)(), 21440Sstevel@tonic-gate void *arg, 21450Sstevel@tonic-gate size_t len, 21460Sstevel@tonic-gate pri_t pri) 21470Sstevel@tonic-gate { 21480Sstevel@tonic-gate kthread_t *t; 21490Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 21500Sstevel@tonic-gate proc_t *pp = zone->zone_zsched; 21510Sstevel@tonic-gate 21520Sstevel@tonic-gate zone_hold(zone); /* Reference to be dropped when thread exits */ 21530Sstevel@tonic-gate 21540Sstevel@tonic-gate /* 21550Sstevel@tonic-gate * No-one should be trying to create threads if the zone is shutting 21560Sstevel@tonic-gate * down and there aren't any kernel threads around. See comment 21570Sstevel@tonic-gate * in zthread_exit(). 21580Sstevel@tonic-gate */ 21590Sstevel@tonic-gate ASSERT(!(zone->zone_kthreads == NULL && 21600Sstevel@tonic-gate zone_status_get(zone) >= ZONE_IS_EMPTY)); 21610Sstevel@tonic-gate /* 21620Sstevel@tonic-gate * Create a thread, but don't let it run until we've finished setting 21630Sstevel@tonic-gate * things up. 21640Sstevel@tonic-gate */ 21650Sstevel@tonic-gate t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 21660Sstevel@tonic-gate ASSERT(t->t_forw == NULL); 21670Sstevel@tonic-gate mutex_enter(&zone_status_lock); 21680Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 21690Sstevel@tonic-gate t->t_forw = t->t_back = t; 21700Sstevel@tonic-gate } else { 21710Sstevel@tonic-gate kthread_t *tx = zone->zone_kthreads; 21720Sstevel@tonic-gate 21730Sstevel@tonic-gate t->t_forw = tx; 21740Sstevel@tonic-gate t->t_back = tx->t_back; 21750Sstevel@tonic-gate tx->t_back->t_forw = t; 21760Sstevel@tonic-gate tx->t_back = t; 21770Sstevel@tonic-gate } 21780Sstevel@tonic-gate zone->zone_kthreads = t; 21790Sstevel@tonic-gate mutex_exit(&zone_status_lock); 21800Sstevel@tonic-gate 21810Sstevel@tonic-gate mutex_enter(&pp->p_lock); 21820Sstevel@tonic-gate t->t_proc_flag |= TP_ZTHREAD; 21830Sstevel@tonic-gate project_rele(t->t_proj); 21840Sstevel@tonic-gate t->t_proj = project_hold(pp->p_task->tk_proj); 21850Sstevel@tonic-gate 21860Sstevel@tonic-gate /* 21870Sstevel@tonic-gate * Setup complete, let it run. 21880Sstevel@tonic-gate */ 21890Sstevel@tonic-gate thread_lock(t); 21900Sstevel@tonic-gate t->t_schedflag |= TS_ALLSTART; 21910Sstevel@tonic-gate setrun_locked(t); 21920Sstevel@tonic-gate thread_unlock(t); 21930Sstevel@tonic-gate 21940Sstevel@tonic-gate mutex_exit(&pp->p_lock); 21950Sstevel@tonic-gate 21960Sstevel@tonic-gate return (t); 21970Sstevel@tonic-gate } 21980Sstevel@tonic-gate 21990Sstevel@tonic-gate /* 22000Sstevel@tonic-gate * Similar to thread_exit(). Must be called by threads created via 22010Sstevel@tonic-gate * zthread_exit(). 22020Sstevel@tonic-gate */ 22030Sstevel@tonic-gate void 22040Sstevel@tonic-gate zthread_exit(void) 22050Sstevel@tonic-gate { 22060Sstevel@tonic-gate kthread_t *t = curthread; 22070Sstevel@tonic-gate proc_t *pp = curproc; 22080Sstevel@tonic-gate zone_t *zone = pp->p_zone; 22090Sstevel@tonic-gate 22100Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22110Sstevel@tonic-gate 22120Sstevel@tonic-gate /* 22130Sstevel@tonic-gate * Reparent to p0 22140Sstevel@tonic-gate */ 22151075Sjosephb kpreempt_disable(); 22160Sstevel@tonic-gate mutex_enter(&pp->p_lock); 22170Sstevel@tonic-gate t->t_proc_flag &= ~TP_ZTHREAD; 22180Sstevel@tonic-gate t->t_procp = &p0; 22190Sstevel@tonic-gate hat_thread_exit(t); 22200Sstevel@tonic-gate mutex_exit(&pp->p_lock); 22211075Sjosephb kpreempt_enable(); 22220Sstevel@tonic-gate 22230Sstevel@tonic-gate if (t->t_back == t) { 22240Sstevel@tonic-gate ASSERT(t->t_forw == t); 22250Sstevel@tonic-gate /* 22260Sstevel@tonic-gate * If the zone is empty, once the thread count 22270Sstevel@tonic-gate * goes to zero no further kernel threads can be 22280Sstevel@tonic-gate * created. This is because if the creator is a process 22290Sstevel@tonic-gate * in the zone, then it must have exited before the zone 22300Sstevel@tonic-gate * state could be set to ZONE_IS_EMPTY. 22310Sstevel@tonic-gate * Otherwise, if the creator is a kernel thread in the 22320Sstevel@tonic-gate * zone, the thread count is non-zero. 22330Sstevel@tonic-gate * 22340Sstevel@tonic-gate * This really means that non-zone kernel threads should 22350Sstevel@tonic-gate * not create zone kernel threads. 22360Sstevel@tonic-gate */ 22370Sstevel@tonic-gate zone->zone_kthreads = NULL; 22380Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_EMPTY) { 22390Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 22400Sstevel@tonic-gate } 22410Sstevel@tonic-gate } else { 22420Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 22430Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 22440Sstevel@tonic-gate if (zone->zone_kthreads == t) 22450Sstevel@tonic-gate zone->zone_kthreads = t->t_forw; 22460Sstevel@tonic-gate } 22470Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22480Sstevel@tonic-gate zone_rele(zone); 22490Sstevel@tonic-gate thread_exit(); 22500Sstevel@tonic-gate /* NOTREACHED */ 22510Sstevel@tonic-gate } 22520Sstevel@tonic-gate 22530Sstevel@tonic-gate static void 22540Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 22550Sstevel@tonic-gate { 22560Sstevel@tonic-gate vnode_t *oldvp; 22570Sstevel@tonic-gate 22580Sstevel@tonic-gate /* we're going to hold a reference here to the directory */ 22590Sstevel@tonic-gate VN_HOLD(vp); 22600Sstevel@tonic-gate 22610Sstevel@tonic-gate #ifdef C2_AUDIT 22620Sstevel@tonic-gate if (audit_active) /* update abs cwd/root path see c2audit.c */ 22630Sstevel@tonic-gate audit_chdirec(vp, vpp); 22640Sstevel@tonic-gate #endif 22650Sstevel@tonic-gate 22660Sstevel@tonic-gate mutex_enter(&pp->p_lock); 22670Sstevel@tonic-gate oldvp = *vpp; 22680Sstevel@tonic-gate *vpp = vp; 22690Sstevel@tonic-gate mutex_exit(&pp->p_lock); 22700Sstevel@tonic-gate if (oldvp != NULL) 22710Sstevel@tonic-gate VN_RELE(oldvp); 22720Sstevel@tonic-gate } 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate /* 22750Sstevel@tonic-gate * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 22760Sstevel@tonic-gate */ 22770Sstevel@tonic-gate static int 22780Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 22790Sstevel@tonic-gate { 22800Sstevel@tonic-gate nvpair_t *nvp = NULL; 22810Sstevel@tonic-gate boolean_t priv_set = B_FALSE; 22820Sstevel@tonic-gate boolean_t limit_set = B_FALSE; 22830Sstevel@tonic-gate boolean_t action_set = B_FALSE; 22840Sstevel@tonic-gate 22850Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 22860Sstevel@tonic-gate const char *name; 22870Sstevel@tonic-gate uint64_t ui64; 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate name = nvpair_name(nvp); 22900Sstevel@tonic-gate if (nvpair_type(nvp) != DATA_TYPE_UINT64) 22910Sstevel@tonic-gate return (EINVAL); 22920Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &ui64); 22930Sstevel@tonic-gate if (strcmp(name, "privilege") == 0) { 22940Sstevel@tonic-gate /* 22950Sstevel@tonic-gate * Currently only privileged values are allowed, but 22960Sstevel@tonic-gate * this may change in the future. 22970Sstevel@tonic-gate */ 22980Sstevel@tonic-gate if (ui64 != RCPRIV_PRIVILEGED) 22990Sstevel@tonic-gate return (EINVAL); 23000Sstevel@tonic-gate rv->rcv_privilege = ui64; 23010Sstevel@tonic-gate priv_set = B_TRUE; 23020Sstevel@tonic-gate } else if (strcmp(name, "limit") == 0) { 23030Sstevel@tonic-gate rv->rcv_value = ui64; 23040Sstevel@tonic-gate limit_set = B_TRUE; 23050Sstevel@tonic-gate } else if (strcmp(name, "action") == 0) { 23060Sstevel@tonic-gate if (ui64 != RCTL_LOCAL_NOACTION && 23070Sstevel@tonic-gate ui64 != RCTL_LOCAL_DENY) 23080Sstevel@tonic-gate return (EINVAL); 23090Sstevel@tonic-gate rv->rcv_flagaction = ui64; 23100Sstevel@tonic-gate action_set = B_TRUE; 23110Sstevel@tonic-gate } else { 23120Sstevel@tonic-gate return (EINVAL); 23130Sstevel@tonic-gate } 23140Sstevel@tonic-gate } 23150Sstevel@tonic-gate 23160Sstevel@tonic-gate if (!(priv_set && limit_set && action_set)) 23170Sstevel@tonic-gate return (EINVAL); 23180Sstevel@tonic-gate rv->rcv_action_signal = 0; 23190Sstevel@tonic-gate rv->rcv_action_recipient = NULL; 23200Sstevel@tonic-gate rv->rcv_action_recip_pid = -1; 23210Sstevel@tonic-gate rv->rcv_firing_time = 0; 23220Sstevel@tonic-gate 23230Sstevel@tonic-gate return (0); 23240Sstevel@tonic-gate } 23250Sstevel@tonic-gate 23262267Sdp /* 23272267Sdp * Non-global zone version of start_init. 23282267Sdp */ 23290Sstevel@tonic-gate void 23302267Sdp zone_start_init(void) 23310Sstevel@tonic-gate { 23320Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 23332267Sdp 23342267Sdp ASSERT(!INGLOBALZONE(curproc)); 23350Sstevel@tonic-gate 23360Sstevel@tonic-gate /* 23372267Sdp * We maintain zone_boot_err so that we can return the cause of the 23382267Sdp * failure back to the caller of the zone_boot syscall. 23390Sstevel@tonic-gate */ 23402267Sdp p->p_zone->zone_boot_err = start_init_common(); 23410Sstevel@tonic-gate 23420Sstevel@tonic-gate mutex_enter(&zone_status_lock); 23430Sstevel@tonic-gate if (p->p_zone->zone_boot_err != 0) { 23440Sstevel@tonic-gate /* 23450Sstevel@tonic-gate * Make sure we are still in the booting state-- we could have 23460Sstevel@tonic-gate * raced and already be shutting down, or even further along. 23470Sstevel@tonic-gate */ 23480Sstevel@tonic-gate if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING) 23490Sstevel@tonic-gate zone_status_set(p->p_zone, ZONE_IS_SHUTTING_DOWN); 23500Sstevel@tonic-gate mutex_exit(&zone_status_lock); 23510Sstevel@tonic-gate /* It's gone bad, dispose of the process */ 23520Sstevel@tonic-gate if (proc_exit(CLD_EXITED, p->p_zone->zone_boot_err) != 0) { 2353390Sraf mutex_enter(&p->p_lock); 2354390Sraf ASSERT(p->p_flag & SEXITLWPS); 23550Sstevel@tonic-gate lwp_exit(); 23560Sstevel@tonic-gate } 23570Sstevel@tonic-gate } else { 23580Sstevel@tonic-gate if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING) 23590Sstevel@tonic-gate zone_status_set(p->p_zone, ZONE_IS_RUNNING); 23600Sstevel@tonic-gate mutex_exit(&zone_status_lock); 23610Sstevel@tonic-gate /* cause the process to return to userland. */ 23620Sstevel@tonic-gate lwp_rtt(); 23630Sstevel@tonic-gate } 23640Sstevel@tonic-gate } 23650Sstevel@tonic-gate 23660Sstevel@tonic-gate struct zsched_arg { 23670Sstevel@tonic-gate zone_t *zone; 23680Sstevel@tonic-gate nvlist_t *nvlist; 23690Sstevel@tonic-gate }; 23700Sstevel@tonic-gate 23710Sstevel@tonic-gate /* 23720Sstevel@tonic-gate * Per-zone "sched" workalike. The similarity to "sched" doesn't have 23730Sstevel@tonic-gate * anything to do with scheduling, but rather with the fact that 23740Sstevel@tonic-gate * per-zone kernel threads are parented to zsched, just like regular 23750Sstevel@tonic-gate * kernel threads are parented to sched (p0). 23760Sstevel@tonic-gate * 23770Sstevel@tonic-gate * zsched is also responsible for launching init for the zone. 23780Sstevel@tonic-gate */ 23790Sstevel@tonic-gate static void 23800Sstevel@tonic-gate zsched(void *arg) 23810Sstevel@tonic-gate { 23820Sstevel@tonic-gate struct zsched_arg *za = arg; 23830Sstevel@tonic-gate proc_t *pp = curproc; 23840Sstevel@tonic-gate proc_t *initp = proc_init; 23850Sstevel@tonic-gate zone_t *zone = za->zone; 23860Sstevel@tonic-gate cred_t *cr, *oldcred; 23870Sstevel@tonic-gate rctl_set_t *set; 23880Sstevel@tonic-gate rctl_alloc_gp_t *gp; 23890Sstevel@tonic-gate contract_t *ct = NULL; 23900Sstevel@tonic-gate task_t *tk, *oldtk; 23910Sstevel@tonic-gate rctl_entity_p_t e; 23920Sstevel@tonic-gate kproject_t *pj; 23930Sstevel@tonic-gate 23940Sstevel@tonic-gate nvlist_t *nvl = za->nvlist; 23950Sstevel@tonic-gate nvpair_t *nvp = NULL; 23960Sstevel@tonic-gate 23970Sstevel@tonic-gate bcopy("zsched", u.u_psargs, sizeof ("zsched")); 23980Sstevel@tonic-gate bcopy("zsched", u.u_comm, sizeof ("zsched")); 23990Sstevel@tonic-gate u.u_argc = 0; 24000Sstevel@tonic-gate u.u_argv = NULL; 24010Sstevel@tonic-gate u.u_envp = NULL; 24020Sstevel@tonic-gate closeall(P_FINFO(pp)); 24030Sstevel@tonic-gate 24040Sstevel@tonic-gate /* 24050Sstevel@tonic-gate * We are this zone's "zsched" process. As the zone isn't generally 24060Sstevel@tonic-gate * visible yet we don't need to grab any locks before initializing its 24070Sstevel@tonic-gate * zone_proc pointer. 24080Sstevel@tonic-gate */ 24090Sstevel@tonic-gate zone_hold(zone); /* this hold is released by zone_destroy() */ 24100Sstevel@tonic-gate zone->zone_zsched = pp; 24110Sstevel@tonic-gate mutex_enter(&pp->p_lock); 24120Sstevel@tonic-gate pp->p_zone = zone; 24130Sstevel@tonic-gate mutex_exit(&pp->p_lock); 24140Sstevel@tonic-gate 24150Sstevel@tonic-gate /* 24160Sstevel@tonic-gate * Disassociate process from its 'parent'; parent ourselves to init 24170Sstevel@tonic-gate * (pid 1) and change other values as needed. 24180Sstevel@tonic-gate */ 24190Sstevel@tonic-gate sess_create(); 24200Sstevel@tonic-gate 24210Sstevel@tonic-gate mutex_enter(&pidlock); 24220Sstevel@tonic-gate proc_detach(pp); 24230Sstevel@tonic-gate pp->p_ppid = 1; 24240Sstevel@tonic-gate pp->p_flag |= SZONETOP; 24250Sstevel@tonic-gate pp->p_ancpid = 1; 24260Sstevel@tonic-gate pp->p_parent = initp; 24270Sstevel@tonic-gate pp->p_psibling = NULL; 24280Sstevel@tonic-gate if (initp->p_child) 24290Sstevel@tonic-gate initp->p_child->p_psibling = pp; 24300Sstevel@tonic-gate pp->p_sibling = initp->p_child; 24310Sstevel@tonic-gate initp->p_child = pp; 24320Sstevel@tonic-gate 24330Sstevel@tonic-gate /* Decrement what newproc() incremented. */ 24340Sstevel@tonic-gate upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 24350Sstevel@tonic-gate /* 24360Sstevel@tonic-gate * Our credentials are about to become kcred-like, so we don't care 24370Sstevel@tonic-gate * about the caller's ruid. 24380Sstevel@tonic-gate */ 24390Sstevel@tonic-gate upcount_inc(crgetruid(kcred), zone->zone_id); 24400Sstevel@tonic-gate mutex_exit(&pidlock); 24410Sstevel@tonic-gate 24420Sstevel@tonic-gate /* 24430Sstevel@tonic-gate * getting out of global zone, so decrement lwp counts 24440Sstevel@tonic-gate */ 24450Sstevel@tonic-gate pj = pp->p_task->tk_proj; 24460Sstevel@tonic-gate mutex_enter(&global_zone->zone_nlwps_lock); 24470Sstevel@tonic-gate pj->kpj_nlwps -= pp->p_lwpcnt; 24480Sstevel@tonic-gate global_zone->zone_nlwps -= pp->p_lwpcnt; 24490Sstevel@tonic-gate mutex_exit(&global_zone->zone_nlwps_lock); 24500Sstevel@tonic-gate 24510Sstevel@tonic-gate /* 24520Sstevel@tonic-gate * Create and join a new task in project '0' of this zone. 24530Sstevel@tonic-gate * 24540Sstevel@tonic-gate * We don't need to call holdlwps() since we know we're the only lwp in 24550Sstevel@tonic-gate * this process. 24560Sstevel@tonic-gate * 24570Sstevel@tonic-gate * task_join() returns with p_lock held. 24580Sstevel@tonic-gate */ 24590Sstevel@tonic-gate tk = task_create(0, zone); 24600Sstevel@tonic-gate mutex_enter(&cpu_lock); 24610Sstevel@tonic-gate oldtk = task_join(tk, 0); 24620Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 24630Sstevel@tonic-gate mutex_exit(&cpu_lock); 24640Sstevel@tonic-gate task_rele(oldtk); 24650Sstevel@tonic-gate 24660Sstevel@tonic-gate /* 24670Sstevel@tonic-gate * add lwp counts to zsched's zone, and increment project's task count 24680Sstevel@tonic-gate * due to the task created in the above tasksys_settaskid 24690Sstevel@tonic-gate */ 24700Sstevel@tonic-gate pj = pp->p_task->tk_proj; 24710Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 24720Sstevel@tonic-gate pj->kpj_nlwps += pp->p_lwpcnt; 24730Sstevel@tonic-gate pj->kpj_ntasks += 1; 24740Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 24750Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 24760Sstevel@tonic-gate 24770Sstevel@tonic-gate /* 24780Sstevel@tonic-gate * The process was created by a process in the global zone, hence the 24790Sstevel@tonic-gate * credentials are wrong. We might as well have kcred-ish credentials. 24800Sstevel@tonic-gate */ 24810Sstevel@tonic-gate cr = zone->zone_kcred; 24820Sstevel@tonic-gate crhold(cr); 24830Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 24840Sstevel@tonic-gate oldcred = pp->p_cred; 24850Sstevel@tonic-gate pp->p_cred = cr; 24860Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 24870Sstevel@tonic-gate crfree(oldcred); 24880Sstevel@tonic-gate 24890Sstevel@tonic-gate /* 24900Sstevel@tonic-gate * Hold credentials again (for thread) 24910Sstevel@tonic-gate */ 24920Sstevel@tonic-gate crhold(cr); 24930Sstevel@tonic-gate 24940Sstevel@tonic-gate /* 24950Sstevel@tonic-gate * p_lwpcnt can't change since this is a kernel process. 24960Sstevel@tonic-gate */ 24970Sstevel@tonic-gate crset(pp, cr); 24980Sstevel@tonic-gate 24990Sstevel@tonic-gate /* 25000Sstevel@tonic-gate * Chroot 25010Sstevel@tonic-gate */ 25020Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 25030Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 25040Sstevel@tonic-gate 25050Sstevel@tonic-gate /* 25060Sstevel@tonic-gate * Initialize zone's rctl set. 25070Sstevel@tonic-gate */ 25080Sstevel@tonic-gate set = rctl_set_create(); 25090Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 25100Sstevel@tonic-gate mutex_enter(&pp->p_lock); 25110Sstevel@tonic-gate e.rcep_p.zone = zone; 25120Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 25130Sstevel@tonic-gate zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 25140Sstevel@tonic-gate mutex_exit(&pp->p_lock); 25150Sstevel@tonic-gate rctl_prealloc_destroy(gp); 25160Sstevel@tonic-gate 25170Sstevel@tonic-gate /* 25180Sstevel@tonic-gate * Apply the rctls passed in to zone_create(). This is basically a list 25190Sstevel@tonic-gate * assignment: all of the old values are removed and the new ones 25200Sstevel@tonic-gate * inserted. That is, if an empty list is passed in, all values are 25210Sstevel@tonic-gate * removed. 25220Sstevel@tonic-gate */ 25230Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 25240Sstevel@tonic-gate rctl_dict_entry_t *rde; 25250Sstevel@tonic-gate rctl_hndl_t hndl; 25260Sstevel@tonic-gate char *name; 25270Sstevel@tonic-gate nvlist_t **nvlarray; 25280Sstevel@tonic-gate uint_t i, nelem; 25290Sstevel@tonic-gate int error; /* For ASSERT()s */ 25300Sstevel@tonic-gate 25310Sstevel@tonic-gate name = nvpair_name(nvp); 25320Sstevel@tonic-gate hndl = rctl_hndl_lookup(name); 25330Sstevel@tonic-gate ASSERT(hndl != -1); 25340Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 25350Sstevel@tonic-gate ASSERT(rde != NULL); 25360Sstevel@tonic-gate 25370Sstevel@tonic-gate for (; /* ever */; ) { 25380Sstevel@tonic-gate rctl_val_t oval; 25390Sstevel@tonic-gate 25400Sstevel@tonic-gate mutex_enter(&pp->p_lock); 25410Sstevel@tonic-gate error = rctl_local_get(hndl, NULL, &oval, pp); 25420Sstevel@tonic-gate mutex_exit(&pp->p_lock); 25430Sstevel@tonic-gate ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 25440Sstevel@tonic-gate ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 25450Sstevel@tonic-gate if (oval.rcv_privilege == RCPRIV_SYSTEM) 25460Sstevel@tonic-gate break; 25470Sstevel@tonic-gate mutex_enter(&pp->p_lock); 25480Sstevel@tonic-gate error = rctl_local_delete(hndl, &oval, pp); 25490Sstevel@tonic-gate mutex_exit(&pp->p_lock); 25500Sstevel@tonic-gate ASSERT(error == 0); 25510Sstevel@tonic-gate } 25520Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 25530Sstevel@tonic-gate ASSERT(error == 0); 25540Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 25550Sstevel@tonic-gate rctl_val_t *nvalp; 25560Sstevel@tonic-gate 25570Sstevel@tonic-gate nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 25580Sstevel@tonic-gate error = nvlist2rctlval(nvlarray[i], nvalp); 25590Sstevel@tonic-gate ASSERT(error == 0); 25600Sstevel@tonic-gate /* 25610Sstevel@tonic-gate * rctl_local_insert can fail if the value being 25620Sstevel@tonic-gate * inserted is a duplicate; this is OK. 25630Sstevel@tonic-gate */ 25640Sstevel@tonic-gate mutex_enter(&pp->p_lock); 25650Sstevel@tonic-gate if (rctl_local_insert(hndl, nvalp, pp) != 0) 25660Sstevel@tonic-gate kmem_cache_free(rctl_val_cache, nvalp); 25670Sstevel@tonic-gate mutex_exit(&pp->p_lock); 25680Sstevel@tonic-gate } 25690Sstevel@tonic-gate } 25700Sstevel@tonic-gate /* 25710Sstevel@tonic-gate * Tell the world that we're done setting up. 25720Sstevel@tonic-gate * 25730Sstevel@tonic-gate * At this point we want to set the zone status to ZONE_IS_READY 25740Sstevel@tonic-gate * and atomically set the zone's processor set visibility. Once 25750Sstevel@tonic-gate * we drop pool_lock() this zone will automatically get updated 25760Sstevel@tonic-gate * to reflect any future changes to the pools configuration. 25770Sstevel@tonic-gate */ 25780Sstevel@tonic-gate pool_lock(); 25790Sstevel@tonic-gate mutex_enter(&cpu_lock); 25800Sstevel@tonic-gate mutex_enter(&zonehash_lock); 25810Sstevel@tonic-gate zone_uniqid(zone); 25820Sstevel@tonic-gate zone_zsd_configure(zone); 25830Sstevel@tonic-gate if (pool_state == POOL_ENABLED) 25840Sstevel@tonic-gate zone_pset_set(zone, pool_default->pool_pset->pset_id); 25850Sstevel@tonic-gate mutex_enter(&zone_status_lock); 25860Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 25870Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_READY); 25880Sstevel@tonic-gate mutex_exit(&zone_status_lock); 25890Sstevel@tonic-gate mutex_exit(&zonehash_lock); 25900Sstevel@tonic-gate mutex_exit(&cpu_lock); 25910Sstevel@tonic-gate pool_unlock(); 25920Sstevel@tonic-gate 25930Sstevel@tonic-gate /* 25940Sstevel@tonic-gate * Once we see the zone transition to the ZONE_IS_BOOTING state, 25950Sstevel@tonic-gate * we launch init, and set the state to running. 25960Sstevel@tonic-gate */ 25970Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 25980Sstevel@tonic-gate 25990Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_BOOTING) { 26000Sstevel@tonic-gate id_t cid; 26010Sstevel@tonic-gate 26020Sstevel@tonic-gate /* 26030Sstevel@tonic-gate * Ok, this is a little complicated. We need to grab the 26040Sstevel@tonic-gate * zone's pool's scheduling class ID; note that by now, we 26050Sstevel@tonic-gate * are already bound to a pool if we need to be (zoneadmd 26060Sstevel@tonic-gate * will have done that to us while we're in the READY 26070Sstevel@tonic-gate * state). *But* the scheduling class for the zone's 'init' 26080Sstevel@tonic-gate * must be explicitly passed to newproc, which doesn't 26090Sstevel@tonic-gate * respect pool bindings. 26100Sstevel@tonic-gate * 26110Sstevel@tonic-gate * We hold the pool_lock across the call to newproc() to 26120Sstevel@tonic-gate * close the obvious race: the pool's scheduling class 26130Sstevel@tonic-gate * could change before we manage to create the LWP with 26140Sstevel@tonic-gate * classid 'cid'. 26150Sstevel@tonic-gate */ 26160Sstevel@tonic-gate pool_lock(); 26170Sstevel@tonic-gate cid = pool_get_class(zone->zone_pool); 26180Sstevel@tonic-gate if (cid == -1) 26190Sstevel@tonic-gate cid = defaultcid; 26200Sstevel@tonic-gate 26210Sstevel@tonic-gate /* 26220Sstevel@tonic-gate * If this fails, zone_boot will ultimately fail. The 26230Sstevel@tonic-gate * state of the zone will be set to SHUTTING_DOWN-- userland 26240Sstevel@tonic-gate * will have to tear down the zone, and fail, or try again. 26250Sstevel@tonic-gate */ 26262267Sdp if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid, 26270Sstevel@tonic-gate minclsyspri - 1, &ct)) != 0) { 26280Sstevel@tonic-gate mutex_enter(&zone_status_lock); 26290Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 26300Sstevel@tonic-gate mutex_exit(&zone_status_lock); 26310Sstevel@tonic-gate } 26320Sstevel@tonic-gate pool_unlock(); 26330Sstevel@tonic-gate } 26340Sstevel@tonic-gate 26350Sstevel@tonic-gate /* 26360Sstevel@tonic-gate * Wait for zone_destroy() to be called. This is what we spend 26370Sstevel@tonic-gate * most of our life doing. 26380Sstevel@tonic-gate */ 26390Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 26400Sstevel@tonic-gate 26410Sstevel@tonic-gate if (ct) 26420Sstevel@tonic-gate /* 26430Sstevel@tonic-gate * At this point the process contract should be empty. 26440Sstevel@tonic-gate * (Though if it isn't, it's not the end of the world.) 26450Sstevel@tonic-gate */ 26460Sstevel@tonic-gate VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 26470Sstevel@tonic-gate 26480Sstevel@tonic-gate /* 26490Sstevel@tonic-gate * Allow kcred to be freed when all referring processes 26500Sstevel@tonic-gate * (including this one) go away. We can't just do this in 26510Sstevel@tonic-gate * zone_free because we need to wait for the zone_cred_ref to 26520Sstevel@tonic-gate * drop to 0 before calling zone_free, and the existence of 26530Sstevel@tonic-gate * zone_kcred will prevent that. Thus, we call crfree here to 26540Sstevel@tonic-gate * balance the crdup in zone_create. The crhold calls earlier 26550Sstevel@tonic-gate * in zsched will be dropped when the thread and process exit. 26560Sstevel@tonic-gate */ 26570Sstevel@tonic-gate crfree(zone->zone_kcred); 26580Sstevel@tonic-gate zone->zone_kcred = NULL; 26590Sstevel@tonic-gate 26600Sstevel@tonic-gate exit(CLD_EXITED, 0); 26610Sstevel@tonic-gate } 26620Sstevel@tonic-gate 26630Sstevel@tonic-gate /* 26640Sstevel@tonic-gate * Helper function to determine if there are any submounts of the 26650Sstevel@tonic-gate * provided path. Used to make sure the zone doesn't "inherit" any 26660Sstevel@tonic-gate * mounts from before it is created. 26670Sstevel@tonic-gate */ 26680Sstevel@tonic-gate static uint_t 26690Sstevel@tonic-gate zone_mount_count(const char *rootpath) 26700Sstevel@tonic-gate { 26710Sstevel@tonic-gate vfs_t *vfsp; 26720Sstevel@tonic-gate uint_t count = 0; 26730Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 26740Sstevel@tonic-gate 26750Sstevel@tonic-gate /* 26760Sstevel@tonic-gate * Holding zonehash_lock prevents race conditions with 26770Sstevel@tonic-gate * vfs_list_add()/vfs_list_remove() since we serialize with 26780Sstevel@tonic-gate * zone_find_by_path(). 26790Sstevel@tonic-gate */ 26800Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 26810Sstevel@tonic-gate /* 26820Sstevel@tonic-gate * The rootpath must end with a '/' 26830Sstevel@tonic-gate */ 26840Sstevel@tonic-gate ASSERT(rootpath[rootpathlen - 1] == '/'); 26850Sstevel@tonic-gate 26860Sstevel@tonic-gate /* 26870Sstevel@tonic-gate * This intentionally does not count the rootpath itself if that 26880Sstevel@tonic-gate * happens to be a mount point. 26890Sstevel@tonic-gate */ 26900Sstevel@tonic-gate vfs_list_read_lock(); 26910Sstevel@tonic-gate vfsp = rootvfs; 26920Sstevel@tonic-gate do { 26930Sstevel@tonic-gate if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 26940Sstevel@tonic-gate rootpathlen) == 0) 26950Sstevel@tonic-gate count++; 26960Sstevel@tonic-gate vfsp = vfsp->vfs_next; 26970Sstevel@tonic-gate } while (vfsp != rootvfs); 26980Sstevel@tonic-gate vfs_list_unlock(); 26990Sstevel@tonic-gate return (count); 27000Sstevel@tonic-gate } 27010Sstevel@tonic-gate 27020Sstevel@tonic-gate /* 27030Sstevel@tonic-gate * Helper function to make sure that a zone created on 'rootpath' 27040Sstevel@tonic-gate * wouldn't end up containing other zones' rootpaths. 27050Sstevel@tonic-gate */ 27060Sstevel@tonic-gate static boolean_t 27070Sstevel@tonic-gate zone_is_nested(const char *rootpath) 27080Sstevel@tonic-gate { 27090Sstevel@tonic-gate zone_t *zone; 27100Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 27110Sstevel@tonic-gate size_t len; 27120Sstevel@tonic-gate 27130Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 27140Sstevel@tonic-gate 27150Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 27160Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 27170Sstevel@tonic-gate if (zone == global_zone) 27180Sstevel@tonic-gate continue; 27190Sstevel@tonic-gate len = strlen(zone->zone_rootpath); 27200Sstevel@tonic-gate if (strncmp(rootpath, zone->zone_rootpath, 27210Sstevel@tonic-gate MIN(rootpathlen, len)) == 0) 27220Sstevel@tonic-gate return (B_TRUE); 27230Sstevel@tonic-gate } 27240Sstevel@tonic-gate return (B_FALSE); 27250Sstevel@tonic-gate } 27260Sstevel@tonic-gate 27270Sstevel@tonic-gate static int 2728813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs, 2729813Sdp size_t zone_privssz) 27300Sstevel@tonic-gate { 27310Sstevel@tonic-gate priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 27320Sstevel@tonic-gate 2733813Sdp if (zone_privssz < sizeof (priv_set_t)) 2734813Sdp return (set_errno(ENOMEM)); 2735813Sdp 27360Sstevel@tonic-gate if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 27370Sstevel@tonic-gate kmem_free(privs, sizeof (priv_set_t)); 27380Sstevel@tonic-gate return (EFAULT); 27390Sstevel@tonic-gate } 27400Sstevel@tonic-gate 27410Sstevel@tonic-gate zone->zone_privset = privs; 27420Sstevel@tonic-gate return (0); 27430Sstevel@tonic-gate } 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate /* 27460Sstevel@tonic-gate * We make creative use of nvlists to pass in rctls from userland. The list is 27470Sstevel@tonic-gate * a list of the following structures: 27480Sstevel@tonic-gate * 27490Sstevel@tonic-gate * (name = rctl_name, value = nvpair_list_array) 27500Sstevel@tonic-gate * 27510Sstevel@tonic-gate * Where each element of the nvpair_list_array is of the form: 27520Sstevel@tonic-gate * 27530Sstevel@tonic-gate * [(name = "privilege", value = RCPRIV_PRIVILEGED), 27540Sstevel@tonic-gate * (name = "limit", value = uint64_t), 27550Sstevel@tonic-gate * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 27560Sstevel@tonic-gate */ 27570Sstevel@tonic-gate static int 27580Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 27590Sstevel@tonic-gate { 27600Sstevel@tonic-gate nvpair_t *nvp = NULL; 27610Sstevel@tonic-gate nvlist_t *nvl = NULL; 27620Sstevel@tonic-gate char *kbuf; 27630Sstevel@tonic-gate int error; 27640Sstevel@tonic-gate rctl_val_t rv; 27650Sstevel@tonic-gate 27660Sstevel@tonic-gate *nvlp = NULL; 27670Sstevel@tonic-gate 27680Sstevel@tonic-gate if (buflen == 0) 27690Sstevel@tonic-gate return (0); 27700Sstevel@tonic-gate 27710Sstevel@tonic-gate if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 27720Sstevel@tonic-gate return (ENOMEM); 27730Sstevel@tonic-gate if (copyin(ubuf, kbuf, buflen)) { 27740Sstevel@tonic-gate error = EFAULT; 27750Sstevel@tonic-gate goto out; 27760Sstevel@tonic-gate } 27770Sstevel@tonic-gate if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 27780Sstevel@tonic-gate /* 27790Sstevel@tonic-gate * nvl may have been allocated/free'd, but the value set to 27800Sstevel@tonic-gate * non-NULL, so we reset it here. 27810Sstevel@tonic-gate */ 27820Sstevel@tonic-gate nvl = NULL; 27830Sstevel@tonic-gate error = EINVAL; 27840Sstevel@tonic-gate goto out; 27850Sstevel@tonic-gate } 27860Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 27870Sstevel@tonic-gate rctl_dict_entry_t *rde; 27880Sstevel@tonic-gate rctl_hndl_t hndl; 27890Sstevel@tonic-gate nvlist_t **nvlarray; 27900Sstevel@tonic-gate uint_t i, nelem; 27910Sstevel@tonic-gate char *name; 27920Sstevel@tonic-gate 27930Sstevel@tonic-gate error = EINVAL; 27940Sstevel@tonic-gate name = nvpair_name(nvp); 27950Sstevel@tonic-gate if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 27960Sstevel@tonic-gate != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 27970Sstevel@tonic-gate goto out; 27980Sstevel@tonic-gate } 27990Sstevel@tonic-gate if ((hndl = rctl_hndl_lookup(name)) == -1) { 28000Sstevel@tonic-gate goto out; 28010Sstevel@tonic-gate } 28020Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 28030Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 28040Sstevel@tonic-gate ASSERT(error == 0); 28050Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 28060Sstevel@tonic-gate if (error = nvlist2rctlval(nvlarray[i], &rv)) 28070Sstevel@tonic-gate goto out; 28080Sstevel@tonic-gate } 28090Sstevel@tonic-gate if (rctl_invalid_value(rde, &rv)) { 28100Sstevel@tonic-gate error = EINVAL; 28110Sstevel@tonic-gate goto out; 28120Sstevel@tonic-gate } 28130Sstevel@tonic-gate } 28140Sstevel@tonic-gate error = 0; 28150Sstevel@tonic-gate *nvlp = nvl; 28160Sstevel@tonic-gate out: 28170Sstevel@tonic-gate kmem_free(kbuf, buflen); 28180Sstevel@tonic-gate if (error && nvl != NULL) 28190Sstevel@tonic-gate nvlist_free(nvl); 28200Sstevel@tonic-gate return (error); 28210Sstevel@tonic-gate } 28220Sstevel@tonic-gate 28230Sstevel@tonic-gate int 28240Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) { 28250Sstevel@tonic-gate if (er_out != NULL) { 28260Sstevel@tonic-gate if (copyout(&er_ext, er_out, sizeof (int))) { 28270Sstevel@tonic-gate return (set_errno(EFAULT)); 28280Sstevel@tonic-gate } 28290Sstevel@tonic-gate } 28300Sstevel@tonic-gate return (set_errno(er_error)); 28310Sstevel@tonic-gate } 28320Sstevel@tonic-gate 28331676Sjpk static int 28341676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi) 28351676Sjpk { 28361676Sjpk ts_label_t *tsl; 28371676Sjpk bslabel_t blab; 28381676Sjpk 28391676Sjpk /* Get label from user */ 28401676Sjpk if (copyin(lab, &blab, sizeof (blab)) != 0) 28411676Sjpk return (EFAULT); 28421676Sjpk tsl = labelalloc(&blab, doi, KM_NOSLEEP); 28431676Sjpk if (tsl == NULL) 28441676Sjpk return (ENOMEM); 28451676Sjpk 28461676Sjpk zone->zone_slabel = tsl; 28471676Sjpk return (0); 28481676Sjpk } 28491676Sjpk 28500Sstevel@tonic-gate /* 2851789Sahrens * Parses a comma-separated list of ZFS datasets into a per-zone dictionary. 2852789Sahrens */ 2853789Sahrens static int 2854789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen) 2855789Sahrens { 2856789Sahrens char *kbuf; 2857789Sahrens char *dataset, *next; 2858789Sahrens zone_dataset_t *zd; 2859789Sahrens size_t len; 2860789Sahrens 2861789Sahrens if (ubuf == NULL || buflen == 0) 2862789Sahrens return (0); 2863789Sahrens 2864789Sahrens if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 2865789Sahrens return (ENOMEM); 2866789Sahrens 2867789Sahrens if (copyin(ubuf, kbuf, buflen) != 0) { 2868789Sahrens kmem_free(kbuf, buflen); 2869789Sahrens return (EFAULT); 2870789Sahrens } 2871789Sahrens 2872789Sahrens dataset = next = kbuf; 2873789Sahrens for (;;) { 2874789Sahrens zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP); 2875789Sahrens 2876789Sahrens next = strchr(dataset, ','); 2877789Sahrens 2878789Sahrens if (next == NULL) 2879789Sahrens len = strlen(dataset); 2880789Sahrens else 2881789Sahrens len = next - dataset; 2882789Sahrens 2883789Sahrens zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP); 2884789Sahrens bcopy(dataset, zd->zd_dataset, len); 2885789Sahrens zd->zd_dataset[len] = '\0'; 2886789Sahrens 2887789Sahrens list_insert_head(&zone->zone_datasets, zd); 2888789Sahrens 2889789Sahrens if (next == NULL) 2890789Sahrens break; 2891789Sahrens 2892789Sahrens dataset = next + 1; 2893789Sahrens } 2894789Sahrens 2895789Sahrens kmem_free(kbuf, buflen); 2896789Sahrens return (0); 2897789Sahrens } 2898789Sahrens 2899789Sahrens /* 29000Sstevel@tonic-gate * System call to create/initialize a new zone named 'zone_name', rooted 29010Sstevel@tonic-gate * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 29021676Sjpk * and initialized with the zone-wide rctls described in 'rctlbuf', and 29031676Sjpk * with labeling set by 'match', 'doi', and 'label'. 29040Sstevel@tonic-gate * 29050Sstevel@tonic-gate * If extended error is non-null, we may use it to return more detailed 29060Sstevel@tonic-gate * error information. 29070Sstevel@tonic-gate */ 29080Sstevel@tonic-gate static zoneid_t 29090Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root, 2910813Sdp const priv_set_t *zone_privs, size_t zone_privssz, 2911813Sdp caddr_t rctlbuf, size_t rctlbufsz, 29121676Sjpk caddr_t zfsbuf, size_t zfsbufsz, int *extended_error, 29131676Sjpk int match, uint32_t doi, const bslabel_t *label) 29140Sstevel@tonic-gate { 29150Sstevel@tonic-gate struct zsched_arg zarg; 29160Sstevel@tonic-gate nvlist_t *rctls = NULL; 29170Sstevel@tonic-gate proc_t *pp = curproc; 29180Sstevel@tonic-gate zone_t *zone, *ztmp; 29190Sstevel@tonic-gate zoneid_t zoneid; 29200Sstevel@tonic-gate int error; 29210Sstevel@tonic-gate int error2 = 0; 29220Sstevel@tonic-gate char *str; 29230Sstevel@tonic-gate cred_t *zkcr; 29241769Scarlsonj boolean_t insert_label_hash; 29250Sstevel@tonic-gate 29260Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 29270Sstevel@tonic-gate return (set_errno(EPERM)); 29280Sstevel@tonic-gate 29290Sstevel@tonic-gate /* can't boot zone from within chroot environment */ 29300Sstevel@tonic-gate if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 29310Sstevel@tonic-gate return (zone_create_error(ENOTSUP, ZE_CHROOTED, 2932813Sdp extended_error)); 29330Sstevel@tonic-gate 29340Sstevel@tonic-gate zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 29350Sstevel@tonic-gate zoneid = zone->zone_id = id_alloc(zoneid_space); 29360Sstevel@tonic-gate zone->zone_status = ZONE_IS_UNINITIALIZED; 29370Sstevel@tonic-gate zone->zone_pool = pool_default; 29380Sstevel@tonic-gate zone->zone_pool_mod = gethrtime(); 29390Sstevel@tonic-gate zone->zone_psetid = ZONE_PS_INVAL; 29400Sstevel@tonic-gate zone->zone_ncpus = 0; 29410Sstevel@tonic-gate zone->zone_ncpus_online = 0; 29420Sstevel@tonic-gate mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 29430Sstevel@tonic-gate mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 29440Sstevel@tonic-gate cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 29450Sstevel@tonic-gate list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 29460Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 2947789Sahrens list_create(&zone->zone_datasets, sizeof (zone_dataset_t), 2948789Sahrens offsetof(zone_dataset_t, zd_linkage)); 29491676Sjpk rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 29500Sstevel@tonic-gate 29510Sstevel@tonic-gate if ((error = zone_set_name(zone, zone_name)) != 0) { 29520Sstevel@tonic-gate zone_free(zone); 29530Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 29540Sstevel@tonic-gate } 29550Sstevel@tonic-gate 29560Sstevel@tonic-gate if ((error = zone_set_root(zone, zone_root)) != 0) { 29570Sstevel@tonic-gate zone_free(zone); 29580Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 29590Sstevel@tonic-gate } 2960813Sdp if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) { 29610Sstevel@tonic-gate zone_free(zone); 29620Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 29630Sstevel@tonic-gate } 29640Sstevel@tonic-gate 29650Sstevel@tonic-gate /* initialize node name to be the same as zone name */ 29660Sstevel@tonic-gate zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 29670Sstevel@tonic-gate (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 29680Sstevel@tonic-gate zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 29690Sstevel@tonic-gate 29700Sstevel@tonic-gate zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 29710Sstevel@tonic-gate zone->zone_domain[0] = '\0'; 29720Sstevel@tonic-gate zone->zone_shares = 1; 2973*2677Sml93401 zone->zone_shmmax = 0; 2974*2677Sml93401 zone->zone_ipc.ipcq_shmmni = 0; 2975*2677Sml93401 zone->zone_ipc.ipcq_semmni = 0; 2976*2677Sml93401 zone->zone_ipc.ipcq_msgmni = 0; 29770Sstevel@tonic-gate zone->zone_bootargs = NULL; 29782267Sdp zone->zone_initname = 29792267Sdp kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP); 29802267Sdp (void) strcpy(zone->zone_initname, zone_default_initname); 29810Sstevel@tonic-gate 29820Sstevel@tonic-gate /* 29830Sstevel@tonic-gate * Zsched initializes the rctls. 29840Sstevel@tonic-gate */ 29850Sstevel@tonic-gate zone->zone_rctls = NULL; 29860Sstevel@tonic-gate 29870Sstevel@tonic-gate if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 29880Sstevel@tonic-gate zone_free(zone); 29890Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 29900Sstevel@tonic-gate } 29910Sstevel@tonic-gate 2992789Sahrens if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) { 2993789Sahrens zone_free(zone); 2994789Sahrens return (set_errno(error)); 2995789Sahrens } 2996789Sahrens 29970Sstevel@tonic-gate /* 29981676Sjpk * Read in the trusted system parameters: 29991676Sjpk * match flag and sensitivity label. 30001676Sjpk */ 30011676Sjpk zone->zone_match = match; 30021769Scarlsonj if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 30031676Sjpk error = zone_set_label(zone, label, doi); 30041676Sjpk if (error != 0) { 30051676Sjpk zone_free(zone); 30061676Sjpk return (set_errno(error)); 30071676Sjpk } 30081769Scarlsonj insert_label_hash = B_TRUE; 30091676Sjpk } else { 30101676Sjpk /* all zones get an admin_low label if system is not labeled */ 30111676Sjpk zone->zone_slabel = l_admin_low; 30121676Sjpk label_hold(l_admin_low); 30131769Scarlsonj insert_label_hash = B_FALSE; 30141676Sjpk } 30151676Sjpk 30161676Sjpk /* 30170Sstevel@tonic-gate * Stop all lwps since that's what normally happens as part of fork(). 30180Sstevel@tonic-gate * This needs to happen before we grab any locks to avoid deadlock 30190Sstevel@tonic-gate * (another lwp in the process could be waiting for the held lock). 30200Sstevel@tonic-gate */ 30210Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 30220Sstevel@tonic-gate zone_free(zone); 30230Sstevel@tonic-gate if (rctls) 30240Sstevel@tonic-gate nvlist_free(rctls); 30250Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 30260Sstevel@tonic-gate } 30270Sstevel@tonic-gate 30280Sstevel@tonic-gate if (block_mounts() == 0) { 30290Sstevel@tonic-gate mutex_enter(&pp->p_lock); 30300Sstevel@tonic-gate if (curthread != pp->p_agenttp) 30310Sstevel@tonic-gate continuelwps(pp); 30320Sstevel@tonic-gate mutex_exit(&pp->p_lock); 30330Sstevel@tonic-gate zone_free(zone); 30340Sstevel@tonic-gate if (rctls) 30350Sstevel@tonic-gate nvlist_free(rctls); 30360Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 30370Sstevel@tonic-gate } 30380Sstevel@tonic-gate 30390Sstevel@tonic-gate /* 30400Sstevel@tonic-gate * Set up credential for kernel access. After this, any errors 30410Sstevel@tonic-gate * should go through the dance in errout rather than calling 30420Sstevel@tonic-gate * zone_free directly. 30430Sstevel@tonic-gate */ 30440Sstevel@tonic-gate zone->zone_kcred = crdup(kcred); 30450Sstevel@tonic-gate crsetzone(zone->zone_kcred, zone); 30460Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 30470Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 30480Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 30490Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 30500Sstevel@tonic-gate 30510Sstevel@tonic-gate mutex_enter(&zonehash_lock); 30520Sstevel@tonic-gate /* 30530Sstevel@tonic-gate * Make sure zone doesn't already exist. 30541676Sjpk * 30551676Sjpk * If the system and zone are labeled, 30561676Sjpk * make sure no other zone exists that has the same label. 30570Sstevel@tonic-gate */ 30581676Sjpk if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL || 30591769Scarlsonj (insert_label_hash && 30601676Sjpk (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) { 30610Sstevel@tonic-gate zone_status_t status; 30620Sstevel@tonic-gate 30630Sstevel@tonic-gate status = zone_status_get(ztmp); 30640Sstevel@tonic-gate if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 30650Sstevel@tonic-gate error = EEXIST; 30660Sstevel@tonic-gate else 30670Sstevel@tonic-gate error = EBUSY; 30680Sstevel@tonic-gate goto errout; 30690Sstevel@tonic-gate } 30700Sstevel@tonic-gate 30710Sstevel@tonic-gate /* 30720Sstevel@tonic-gate * Don't allow zone creations which would cause one zone's rootpath to 30730Sstevel@tonic-gate * be accessible from that of another (non-global) zone. 30740Sstevel@tonic-gate */ 30750Sstevel@tonic-gate if (zone_is_nested(zone->zone_rootpath)) { 30760Sstevel@tonic-gate error = EBUSY; 30770Sstevel@tonic-gate goto errout; 30780Sstevel@tonic-gate } 30790Sstevel@tonic-gate 30800Sstevel@tonic-gate ASSERT(zonecount != 0); /* check for leaks */ 30810Sstevel@tonic-gate if (zonecount + 1 > maxzones) { 30820Sstevel@tonic-gate error = ENOMEM; 30830Sstevel@tonic-gate goto errout; 30840Sstevel@tonic-gate } 30850Sstevel@tonic-gate 30860Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 30870Sstevel@tonic-gate error = EBUSY; 30880Sstevel@tonic-gate error2 = ZE_AREMOUNTS; 30890Sstevel@tonic-gate goto errout; 30900Sstevel@tonic-gate } 30910Sstevel@tonic-gate 30920Sstevel@tonic-gate /* 30930Sstevel@tonic-gate * Zone is still incomplete, but we need to drop all locks while 30940Sstevel@tonic-gate * zsched() initializes this zone's kernel process. We 30950Sstevel@tonic-gate * optimistically add the zone to the hashtable and associated 30960Sstevel@tonic-gate * lists so a parallel zone_create() doesn't try to create the 30970Sstevel@tonic-gate * same zone. 30980Sstevel@tonic-gate */ 30990Sstevel@tonic-gate zonecount++; 31000Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, 31010Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id, 31020Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 31030Sstevel@tonic-gate str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 31040Sstevel@tonic-gate (void) strcpy(str, zone->zone_name); 31050Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 31060Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 31071769Scarlsonj if (insert_label_hash) { 31081676Sjpk (void) mod_hash_insert(zonehashbylabel, 31091676Sjpk (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone); 31101769Scarlsonj zone->zone_flags |= ZF_HASHED_LABEL; 31111676Sjpk } 31121676Sjpk 31130Sstevel@tonic-gate /* 31140Sstevel@tonic-gate * Insert into active list. At this point there are no 'hold's 31150Sstevel@tonic-gate * on the zone, but everyone else knows not to use it, so we can 31160Sstevel@tonic-gate * continue to use it. zsched() will do a zone_hold() if the 31170Sstevel@tonic-gate * newproc() is successful. 31180Sstevel@tonic-gate */ 31190Sstevel@tonic-gate list_insert_tail(&zone_active, zone); 31200Sstevel@tonic-gate mutex_exit(&zonehash_lock); 31210Sstevel@tonic-gate 31220Sstevel@tonic-gate zarg.zone = zone; 31230Sstevel@tonic-gate zarg.nvlist = rctls; 31240Sstevel@tonic-gate /* 31250Sstevel@tonic-gate * The process, task, and project rctls are probably wrong; 31260Sstevel@tonic-gate * we need an interface to get the default values of all rctls, 31270Sstevel@tonic-gate * and initialize zsched appropriately. I'm not sure that that 31280Sstevel@tonic-gate * makes much of a difference, though. 31290Sstevel@tonic-gate */ 31300Sstevel@tonic-gate if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) { 31310Sstevel@tonic-gate /* 31320Sstevel@tonic-gate * We need to undo all globally visible state. 31330Sstevel@tonic-gate */ 31340Sstevel@tonic-gate mutex_enter(&zonehash_lock); 31350Sstevel@tonic-gate list_remove(&zone_active, zone); 31361769Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) { 31371676Sjpk ASSERT(zone->zone_slabel != NULL); 31381676Sjpk (void) mod_hash_destroy(zonehashbylabel, 31391676Sjpk (mod_hash_key_t)zone->zone_slabel); 31401676Sjpk } 31410Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 31420Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_name); 31430Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 31440Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 31450Sstevel@tonic-gate ASSERT(zonecount > 1); 31460Sstevel@tonic-gate zonecount--; 31470Sstevel@tonic-gate goto errout; 31480Sstevel@tonic-gate } 31490Sstevel@tonic-gate 31500Sstevel@tonic-gate /* 31510Sstevel@tonic-gate * Zone creation can't fail from now on. 31520Sstevel@tonic-gate */ 31530Sstevel@tonic-gate 31540Sstevel@tonic-gate /* 31550Sstevel@tonic-gate * Let the other lwps continue. 31560Sstevel@tonic-gate */ 31570Sstevel@tonic-gate mutex_enter(&pp->p_lock); 31580Sstevel@tonic-gate if (curthread != pp->p_agenttp) 31590Sstevel@tonic-gate continuelwps(pp); 31600Sstevel@tonic-gate mutex_exit(&pp->p_lock); 31610Sstevel@tonic-gate 31620Sstevel@tonic-gate /* 31630Sstevel@tonic-gate * Wait for zsched to finish initializing the zone. 31640Sstevel@tonic-gate */ 31650Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_READY); 31660Sstevel@tonic-gate /* 31670Sstevel@tonic-gate * The zone is fully visible, so we can let mounts progress. 31680Sstevel@tonic-gate */ 31690Sstevel@tonic-gate resume_mounts(); 31700Sstevel@tonic-gate if (rctls) 31710Sstevel@tonic-gate nvlist_free(rctls); 31720Sstevel@tonic-gate 31730Sstevel@tonic-gate return (zoneid); 31740Sstevel@tonic-gate 31750Sstevel@tonic-gate errout: 31760Sstevel@tonic-gate mutex_exit(&zonehash_lock); 31770Sstevel@tonic-gate /* 31780Sstevel@tonic-gate * Let the other lwps continue. 31790Sstevel@tonic-gate */ 31800Sstevel@tonic-gate mutex_enter(&pp->p_lock); 31810Sstevel@tonic-gate if (curthread != pp->p_agenttp) 31820Sstevel@tonic-gate continuelwps(pp); 31830Sstevel@tonic-gate mutex_exit(&pp->p_lock); 31840Sstevel@tonic-gate 31850Sstevel@tonic-gate resume_mounts(); 31860Sstevel@tonic-gate if (rctls) 31870Sstevel@tonic-gate nvlist_free(rctls); 31880Sstevel@tonic-gate /* 31890Sstevel@tonic-gate * There is currently one reference to the zone, a cred_ref from 31900Sstevel@tonic-gate * zone_kcred. To free the zone, we call crfree, which will call 31910Sstevel@tonic-gate * zone_cred_rele, which will call zone_free. 31920Sstevel@tonic-gate */ 31930Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 31940Sstevel@tonic-gate ASSERT(zone->zone_kcred->cr_ref == 1); 31950Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 31960Sstevel@tonic-gate zkcr = zone->zone_kcred; 31970Sstevel@tonic-gate zone->zone_kcred = NULL; 31980Sstevel@tonic-gate crfree(zkcr); /* triggers call to zone_free */ 31990Sstevel@tonic-gate return (zone_create_error(error, error2, extended_error)); 32000Sstevel@tonic-gate } 32010Sstevel@tonic-gate 32020Sstevel@tonic-gate /* 32030Sstevel@tonic-gate * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 32042267Sdp * the heavy lifting. initname is the path to the program to launch 32052267Sdp * at the "top" of the zone; if this is NULL, we use the system default, 32062267Sdp * which is stored at zone_default_initname. 32070Sstevel@tonic-gate */ 32080Sstevel@tonic-gate static int 32092267Sdp zone_boot(zoneid_t zoneid) 32100Sstevel@tonic-gate { 32110Sstevel@tonic-gate int err; 32120Sstevel@tonic-gate zone_t *zone; 32130Sstevel@tonic-gate 32140Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 32150Sstevel@tonic-gate return (set_errno(EPERM)); 32160Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 32170Sstevel@tonic-gate return (set_errno(EINVAL)); 32180Sstevel@tonic-gate 32190Sstevel@tonic-gate mutex_enter(&zonehash_lock); 32200Sstevel@tonic-gate /* 32210Sstevel@tonic-gate * Look for zone under hash lock to prevent races with calls to 32220Sstevel@tonic-gate * zone_shutdown, zone_destroy, etc. 32230Sstevel@tonic-gate */ 32240Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 32250Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32260Sstevel@tonic-gate return (set_errno(EINVAL)); 32270Sstevel@tonic-gate } 32280Sstevel@tonic-gate 32290Sstevel@tonic-gate mutex_enter(&zone_status_lock); 32300Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_READY) { 32310Sstevel@tonic-gate mutex_exit(&zone_status_lock); 32320Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32330Sstevel@tonic-gate return (set_errno(EINVAL)); 32340Sstevel@tonic-gate } 32350Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_BOOTING); 32360Sstevel@tonic-gate mutex_exit(&zone_status_lock); 32370Sstevel@tonic-gate 32380Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 32390Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32400Sstevel@tonic-gate 32410Sstevel@tonic-gate if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 32420Sstevel@tonic-gate zone_rele(zone); 32430Sstevel@tonic-gate return (set_errno(EINTR)); 32440Sstevel@tonic-gate } 32450Sstevel@tonic-gate 32460Sstevel@tonic-gate /* 32470Sstevel@tonic-gate * Boot (starting init) might have failed, in which case the zone 32480Sstevel@tonic-gate * will go to the SHUTTING_DOWN state; an appropriate errno will 32490Sstevel@tonic-gate * be placed in zone->zone_boot_err, and so we return that. 32500Sstevel@tonic-gate */ 32510Sstevel@tonic-gate err = zone->zone_boot_err; 32520Sstevel@tonic-gate zone_rele(zone); 32530Sstevel@tonic-gate return (err ? set_errno(err) : 0); 32540Sstevel@tonic-gate } 32550Sstevel@tonic-gate 32560Sstevel@tonic-gate /* 32570Sstevel@tonic-gate * Kills all user processes in the zone, waiting for them all to exit 32580Sstevel@tonic-gate * before returning. 32590Sstevel@tonic-gate */ 32600Sstevel@tonic-gate static int 32610Sstevel@tonic-gate zone_empty(zone_t *zone) 32620Sstevel@tonic-gate { 32630Sstevel@tonic-gate int waitstatus; 32640Sstevel@tonic-gate 32650Sstevel@tonic-gate /* 32660Sstevel@tonic-gate * We need to drop zonehash_lock before killing all 32670Sstevel@tonic-gate * processes, otherwise we'll deadlock with zone_find_* 32680Sstevel@tonic-gate * which can be called from the exit path. 32690Sstevel@tonic-gate */ 32700Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 32710Sstevel@tonic-gate while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz, 32720Sstevel@tonic-gate ZONE_IS_EMPTY)) == -1) { 32730Sstevel@tonic-gate killall(zone->zone_id); 32740Sstevel@tonic-gate } 32750Sstevel@tonic-gate /* 32760Sstevel@tonic-gate * return EINTR if we were signaled 32770Sstevel@tonic-gate */ 32780Sstevel@tonic-gate if (waitstatus == 0) 32790Sstevel@tonic-gate return (EINTR); 32800Sstevel@tonic-gate return (0); 32810Sstevel@tonic-gate } 32820Sstevel@tonic-gate 32830Sstevel@tonic-gate /* 32841676Sjpk * This function implements the policy for zone visibility. 32851676Sjpk * 32861676Sjpk * In standard Solaris, a non-global zone can only see itself. 32871676Sjpk * 32881676Sjpk * In Trusted Extensions, a labeled zone can lookup any zone whose label 32891676Sjpk * it dominates. For this test, the label of the global zone is treated as 32901676Sjpk * admin_high so it is special-cased instead of being checked for dominance. 32911676Sjpk * 32921676Sjpk * Returns true if zone attributes are viewable, false otherwise. 32931676Sjpk */ 32941676Sjpk static boolean_t 32951676Sjpk zone_list_access(zone_t *zone) 32961676Sjpk { 32971676Sjpk 32981676Sjpk if (curproc->p_zone == global_zone || 32991676Sjpk curproc->p_zone == zone) { 33001676Sjpk return (B_TRUE); 33011769Scarlsonj } else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 33021676Sjpk bslabel_t *curproc_label; 33031676Sjpk bslabel_t *zone_label; 33041676Sjpk 33051676Sjpk curproc_label = label2bslabel(curproc->p_zone->zone_slabel); 33061676Sjpk zone_label = label2bslabel(zone->zone_slabel); 33071676Sjpk 33081676Sjpk if (zone->zone_id != GLOBAL_ZONEID && 33091676Sjpk bldominates(curproc_label, zone_label)) { 33101676Sjpk return (B_TRUE); 33111676Sjpk } else { 33121676Sjpk return (B_FALSE); 33131676Sjpk } 33141676Sjpk } else { 33151676Sjpk return (B_FALSE); 33161676Sjpk } 33171676Sjpk } 33181676Sjpk 33191676Sjpk /* 33200Sstevel@tonic-gate * Systemcall to start the zone's halt sequence. By the time this 33210Sstevel@tonic-gate * function successfully returns, all user processes and kernel threads 33220Sstevel@tonic-gate * executing in it will have exited, ZSD shutdown callbacks executed, 33230Sstevel@tonic-gate * and the zone status set to ZONE_IS_DOWN. 33240Sstevel@tonic-gate * 33250Sstevel@tonic-gate * It is possible that the call will interrupt itself if the caller is the 33260Sstevel@tonic-gate * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 33270Sstevel@tonic-gate */ 33280Sstevel@tonic-gate static int 33290Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 33300Sstevel@tonic-gate { 33310Sstevel@tonic-gate int error; 33320Sstevel@tonic-gate zone_t *zone; 33330Sstevel@tonic-gate zone_status_t status; 33340Sstevel@tonic-gate 33350Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 33360Sstevel@tonic-gate return (set_errno(EPERM)); 33370Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 33380Sstevel@tonic-gate return (set_errno(EINVAL)); 33390Sstevel@tonic-gate 33400Sstevel@tonic-gate /* 33410Sstevel@tonic-gate * Block mounts so that VFS_MOUNT() can get an accurate view of 33420Sstevel@tonic-gate * the zone's status with regards to ZONE_IS_SHUTTING down. 33430Sstevel@tonic-gate * 33440Sstevel@tonic-gate * e.g. NFS can fail the mount if it determines that the zone 33450Sstevel@tonic-gate * has already begun the shutdown sequence. 33460Sstevel@tonic-gate */ 33470Sstevel@tonic-gate if (block_mounts() == 0) 33480Sstevel@tonic-gate return (set_errno(EINTR)); 33490Sstevel@tonic-gate mutex_enter(&zonehash_lock); 33500Sstevel@tonic-gate /* 33510Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 33520Sstevel@tonic-gate * calls to zone_shutdown and zone_destroy. 33530Sstevel@tonic-gate */ 33540Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 33550Sstevel@tonic-gate mutex_exit(&zonehash_lock); 33560Sstevel@tonic-gate resume_mounts(); 33570Sstevel@tonic-gate return (set_errno(EINVAL)); 33580Sstevel@tonic-gate } 33590Sstevel@tonic-gate mutex_enter(&zone_status_lock); 33600Sstevel@tonic-gate status = zone_status_get(zone); 33610Sstevel@tonic-gate /* 33620Sstevel@tonic-gate * Fail if the zone isn't fully initialized yet. 33630Sstevel@tonic-gate */ 33640Sstevel@tonic-gate if (status < ZONE_IS_READY) { 33650Sstevel@tonic-gate mutex_exit(&zone_status_lock); 33660Sstevel@tonic-gate mutex_exit(&zonehash_lock); 33670Sstevel@tonic-gate resume_mounts(); 33680Sstevel@tonic-gate return (set_errno(EINVAL)); 33690Sstevel@tonic-gate } 33700Sstevel@tonic-gate /* 33710Sstevel@tonic-gate * If conditions required for zone_shutdown() to return have been met, 33720Sstevel@tonic-gate * return success. 33730Sstevel@tonic-gate */ 33740Sstevel@tonic-gate if (status >= ZONE_IS_DOWN) { 33750Sstevel@tonic-gate mutex_exit(&zone_status_lock); 33760Sstevel@tonic-gate mutex_exit(&zonehash_lock); 33770Sstevel@tonic-gate resume_mounts(); 33780Sstevel@tonic-gate return (0); 33790Sstevel@tonic-gate } 33800Sstevel@tonic-gate /* 33810Sstevel@tonic-gate * If zone_shutdown() hasn't been called before, go through the motions. 33820Sstevel@tonic-gate * If it has, there's nothing to do but wait for the kernel threads to 33830Sstevel@tonic-gate * drain. 33840Sstevel@tonic-gate */ 33850Sstevel@tonic-gate if (status < ZONE_IS_EMPTY) { 33860Sstevel@tonic-gate uint_t ntasks; 33870Sstevel@tonic-gate 33880Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 33890Sstevel@tonic-gate if ((ntasks = zone->zone_ntasks) != 1) { 33900Sstevel@tonic-gate /* 33910Sstevel@tonic-gate * There's still stuff running. 33920Sstevel@tonic-gate */ 33930Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 33940Sstevel@tonic-gate } 33950Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 33960Sstevel@tonic-gate if (ntasks == 1) { 33970Sstevel@tonic-gate /* 33980Sstevel@tonic-gate * The only way to create another task is through 33990Sstevel@tonic-gate * zone_enter(), which will block until we drop 34000Sstevel@tonic-gate * zonehash_lock. The zone is empty. 34010Sstevel@tonic-gate */ 34020Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 34030Sstevel@tonic-gate /* 34040Sstevel@tonic-gate * Skip ahead to ZONE_IS_DOWN 34050Sstevel@tonic-gate */ 34060Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 34070Sstevel@tonic-gate } else { 34080Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 34090Sstevel@tonic-gate } 34100Sstevel@tonic-gate } 34110Sstevel@tonic-gate } 34120Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 34130Sstevel@tonic-gate mutex_exit(&zone_status_lock); 34140Sstevel@tonic-gate mutex_exit(&zonehash_lock); 34150Sstevel@tonic-gate resume_mounts(); 34160Sstevel@tonic-gate 34170Sstevel@tonic-gate if (error = zone_empty(zone)) { 34180Sstevel@tonic-gate zone_rele(zone); 34190Sstevel@tonic-gate return (set_errno(error)); 34200Sstevel@tonic-gate } 34210Sstevel@tonic-gate /* 34220Sstevel@tonic-gate * After the zone status goes to ZONE_IS_DOWN this zone will no 34230Sstevel@tonic-gate * longer be notified of changes to the pools configuration, so 34240Sstevel@tonic-gate * in order to not end up with a stale pool pointer, we point 34250Sstevel@tonic-gate * ourselves at the default pool and remove all resource 34260Sstevel@tonic-gate * visibility. This is especially important as the zone_t may 34270Sstevel@tonic-gate * languish on the deathrow for a very long time waiting for 34280Sstevel@tonic-gate * cred's to drain out. 34290Sstevel@tonic-gate * 34300Sstevel@tonic-gate * This rebinding of the zone can happen multiple times 34310Sstevel@tonic-gate * (presumably due to interrupted or parallel systemcalls) 34320Sstevel@tonic-gate * without any adverse effects. 34330Sstevel@tonic-gate */ 34340Sstevel@tonic-gate if (pool_lock_intr() != 0) { 34350Sstevel@tonic-gate zone_rele(zone); 34360Sstevel@tonic-gate return (set_errno(EINTR)); 34370Sstevel@tonic-gate } 34380Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 34390Sstevel@tonic-gate mutex_enter(&cpu_lock); 34400Sstevel@tonic-gate zone_pool_set(zone, pool_default); 34410Sstevel@tonic-gate /* 34420Sstevel@tonic-gate * The zone no longer needs to be able to see any cpus. 34430Sstevel@tonic-gate */ 34440Sstevel@tonic-gate zone_pset_set(zone, ZONE_PS_INVAL); 34450Sstevel@tonic-gate mutex_exit(&cpu_lock); 34460Sstevel@tonic-gate } 34470Sstevel@tonic-gate pool_unlock(); 34480Sstevel@tonic-gate 34490Sstevel@tonic-gate /* 34500Sstevel@tonic-gate * ZSD shutdown callbacks can be executed multiple times, hence 34510Sstevel@tonic-gate * it is safe to not be holding any locks across this call. 34520Sstevel@tonic-gate */ 34530Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 34540Sstevel@tonic-gate 34550Sstevel@tonic-gate mutex_enter(&zone_status_lock); 34560Sstevel@tonic-gate if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 34570Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 34580Sstevel@tonic-gate mutex_exit(&zone_status_lock); 34590Sstevel@tonic-gate 34600Sstevel@tonic-gate /* 34610Sstevel@tonic-gate * Wait for kernel threads to drain. 34620Sstevel@tonic-gate */ 34630Sstevel@tonic-gate if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 34640Sstevel@tonic-gate zone_rele(zone); 34650Sstevel@tonic-gate return (set_errno(EINTR)); 34660Sstevel@tonic-gate } 34670Sstevel@tonic-gate zone_rele(zone); 34680Sstevel@tonic-gate return (0); 34690Sstevel@tonic-gate } 34700Sstevel@tonic-gate 34710Sstevel@tonic-gate /* 34720Sstevel@tonic-gate * Systemcall entry point to finalize the zone halt process. The caller 3473*2677Sml93401 * must have already successfully called zone_shutdown(). 34740Sstevel@tonic-gate * 34750Sstevel@tonic-gate * Upon successful completion, the zone will have been fully destroyed: 34760Sstevel@tonic-gate * zsched will have exited, destructor callbacks executed, and the zone 34770Sstevel@tonic-gate * removed from the list of active zones. 34780Sstevel@tonic-gate */ 34790Sstevel@tonic-gate static int 34800Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 34810Sstevel@tonic-gate { 34820Sstevel@tonic-gate uint64_t uniqid; 34830Sstevel@tonic-gate zone_t *zone; 34840Sstevel@tonic-gate zone_status_t status; 34850Sstevel@tonic-gate 34860Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 34870Sstevel@tonic-gate return (set_errno(EPERM)); 34880Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 34890Sstevel@tonic-gate return (set_errno(EINVAL)); 34900Sstevel@tonic-gate 34910Sstevel@tonic-gate mutex_enter(&zonehash_lock); 34920Sstevel@tonic-gate /* 34930Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 34940Sstevel@tonic-gate * calls to zone_destroy. 34950Sstevel@tonic-gate */ 34960Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 34970Sstevel@tonic-gate mutex_exit(&zonehash_lock); 34980Sstevel@tonic-gate return (set_errno(EINVAL)); 34990Sstevel@tonic-gate } 35000Sstevel@tonic-gate 35010Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 35020Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35030Sstevel@tonic-gate return (set_errno(EBUSY)); 35040Sstevel@tonic-gate } 35050Sstevel@tonic-gate mutex_enter(&zone_status_lock); 35060Sstevel@tonic-gate status = zone_status_get(zone); 35070Sstevel@tonic-gate if (status < ZONE_IS_DOWN) { 35080Sstevel@tonic-gate mutex_exit(&zone_status_lock); 35090Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35100Sstevel@tonic-gate return (set_errno(EBUSY)); 35110Sstevel@tonic-gate } else if (status == ZONE_IS_DOWN) { 35120Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 35130Sstevel@tonic-gate } 35140Sstevel@tonic-gate mutex_exit(&zone_status_lock); 35150Sstevel@tonic-gate zone_hold(zone); 35160Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35170Sstevel@tonic-gate 35180Sstevel@tonic-gate /* 35190Sstevel@tonic-gate * wait for zsched to exit 35200Sstevel@tonic-gate */ 35210Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_DEAD); 35220Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_DESTROY); 35230Sstevel@tonic-gate uniqid = zone->zone_uniqid; 35240Sstevel@tonic-gate zone_rele(zone); 35250Sstevel@tonic-gate zone = NULL; /* potentially free'd */ 35260Sstevel@tonic-gate 35270Sstevel@tonic-gate mutex_enter(&zonehash_lock); 35280Sstevel@tonic-gate for (; /* ever */; ) { 35290Sstevel@tonic-gate boolean_t unref; 35300Sstevel@tonic-gate 35310Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL || 35320Sstevel@tonic-gate zone->zone_uniqid != uniqid) { 35330Sstevel@tonic-gate /* 35340Sstevel@tonic-gate * The zone has gone away. Necessary conditions 35350Sstevel@tonic-gate * are met, so we return success. 35360Sstevel@tonic-gate */ 35370Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35380Sstevel@tonic-gate return (0); 35390Sstevel@tonic-gate } 35400Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 35410Sstevel@tonic-gate unref = ZONE_IS_UNREF(zone); 35420Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 35430Sstevel@tonic-gate if (unref) { 35440Sstevel@tonic-gate /* 35450Sstevel@tonic-gate * There is only one reference to the zone -- that 35460Sstevel@tonic-gate * added when the zone was added to the hashtables -- 35470Sstevel@tonic-gate * and things will remain this way until we drop 35480Sstevel@tonic-gate * zonehash_lock... we can go ahead and cleanup the 35490Sstevel@tonic-gate * zone. 35500Sstevel@tonic-gate */ 35510Sstevel@tonic-gate break; 35520Sstevel@tonic-gate } 35530Sstevel@tonic-gate 35540Sstevel@tonic-gate if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 35550Sstevel@tonic-gate /* Signaled */ 35560Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35570Sstevel@tonic-gate return (set_errno(EINTR)); 35580Sstevel@tonic-gate } 35590Sstevel@tonic-gate 35600Sstevel@tonic-gate } 35610Sstevel@tonic-gate 35620Sstevel@tonic-gate /* 35630Sstevel@tonic-gate * It is now safe to let the zone be recreated; remove it from the 35640Sstevel@tonic-gate * lists. The memory will not be freed until the last cred 35650Sstevel@tonic-gate * reference goes away. 35660Sstevel@tonic-gate */ 35670Sstevel@tonic-gate ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 35680Sstevel@tonic-gate zonecount--; 35690Sstevel@tonic-gate /* remove from active list and hash tables */ 35700Sstevel@tonic-gate list_remove(&zone_active, zone); 35710Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 35720Sstevel@tonic-gate (mod_hash_key_t)zone->zone_name); 35730Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 35740Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 35751769Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) 35761676Sjpk (void) mod_hash_destroy(zonehashbylabel, 35771676Sjpk (mod_hash_key_t)zone->zone_slabel); 35780Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35790Sstevel@tonic-gate 3580766Scarlsonj /* 3581766Scarlsonj * Release the root vnode; we're not using it anymore. Nor should any 3582766Scarlsonj * other thread that might access it exist. 3583766Scarlsonj */ 3584766Scarlsonj if (zone->zone_rootvp != NULL) { 3585766Scarlsonj VN_RELE(zone->zone_rootvp); 3586766Scarlsonj zone->zone_rootvp = NULL; 3587766Scarlsonj } 3588766Scarlsonj 35890Sstevel@tonic-gate /* add to deathrow list */ 35900Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 35910Sstevel@tonic-gate list_insert_tail(&zone_deathrow, zone); 35920Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 35930Sstevel@tonic-gate 35940Sstevel@tonic-gate /* 35950Sstevel@tonic-gate * Drop last reference (which was added by zsched()), this will 35960Sstevel@tonic-gate * free the zone unless there are outstanding cred references. 35970Sstevel@tonic-gate */ 35980Sstevel@tonic-gate zone_rele(zone); 35990Sstevel@tonic-gate return (0); 36000Sstevel@tonic-gate } 36010Sstevel@tonic-gate 36020Sstevel@tonic-gate /* 36030Sstevel@tonic-gate * Systemcall entry point for zone_getattr(2). 36040Sstevel@tonic-gate */ 36050Sstevel@tonic-gate static ssize_t 36060Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 36070Sstevel@tonic-gate { 36080Sstevel@tonic-gate size_t size; 36090Sstevel@tonic-gate int error = 0, err; 36100Sstevel@tonic-gate zone_t *zone; 36110Sstevel@tonic-gate char *zonepath; 36122267Sdp char *outstr; 36130Sstevel@tonic-gate zone_status_t zone_status; 36140Sstevel@tonic-gate pid_t initpid; 36150Sstevel@tonic-gate boolean_t global = (curproc->p_zone == global_zone); 36161676Sjpk boolean_t curzone = (curproc->p_zone->zone_id == zoneid); 36170Sstevel@tonic-gate 36180Sstevel@tonic-gate mutex_enter(&zonehash_lock); 36190Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 36200Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36210Sstevel@tonic-gate return (set_errno(EINVAL)); 36220Sstevel@tonic-gate } 36230Sstevel@tonic-gate zone_status = zone_status_get(zone); 36240Sstevel@tonic-gate if (zone_status < ZONE_IS_READY) { 36250Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36260Sstevel@tonic-gate return (set_errno(EINVAL)); 36270Sstevel@tonic-gate } 36280Sstevel@tonic-gate zone_hold(zone); 36290Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36300Sstevel@tonic-gate 36310Sstevel@tonic-gate /* 36321676Sjpk * If not in the global zone, don't show information about other zones, 36331676Sjpk * unless the system is labeled and the local zone's label dominates 36341676Sjpk * the other zone. 36350Sstevel@tonic-gate */ 36361676Sjpk if (!zone_list_access(zone)) { 36370Sstevel@tonic-gate zone_rele(zone); 36380Sstevel@tonic-gate return (set_errno(EINVAL)); 36390Sstevel@tonic-gate } 36400Sstevel@tonic-gate 36410Sstevel@tonic-gate switch (attr) { 36420Sstevel@tonic-gate case ZONE_ATTR_ROOT: 36430Sstevel@tonic-gate if (global) { 36440Sstevel@tonic-gate /* 36450Sstevel@tonic-gate * Copy the path to trim the trailing "/" (except for 36460Sstevel@tonic-gate * the global zone). 36470Sstevel@tonic-gate */ 36480Sstevel@tonic-gate if (zone != global_zone) 36490Sstevel@tonic-gate size = zone->zone_rootpathlen - 1; 36500Sstevel@tonic-gate else 36510Sstevel@tonic-gate size = zone->zone_rootpathlen; 36520Sstevel@tonic-gate zonepath = kmem_alloc(size, KM_SLEEP); 36530Sstevel@tonic-gate bcopy(zone->zone_rootpath, zonepath, size); 36540Sstevel@tonic-gate zonepath[size - 1] = '\0'; 36550Sstevel@tonic-gate } else { 36561676Sjpk if (curzone || !is_system_labeled()) { 36571676Sjpk /* 36581676Sjpk * Caller is not in the global zone. 36591676Sjpk * if the query is on the current zone 36601676Sjpk * or the system is not labeled, 36611676Sjpk * just return faked-up path for current zone. 36621676Sjpk */ 36631676Sjpk zonepath = "/"; 36641676Sjpk size = 2; 36651676Sjpk } else { 36661676Sjpk /* 36671676Sjpk * Return related path for current zone. 36681676Sjpk */ 36691676Sjpk int prefix_len = strlen(zone_prefix); 36701676Sjpk int zname_len = strlen(zone->zone_name); 36711676Sjpk 36721676Sjpk size = prefix_len + zname_len + 1; 36731676Sjpk zonepath = kmem_alloc(size, KM_SLEEP); 36741676Sjpk bcopy(zone_prefix, zonepath, prefix_len); 36751676Sjpk bcopy(zone->zone_name, zonepath + 36762267Sdp prefix_len, zname_len); 36771676Sjpk zonepath[size - 1] = '\0'; 36781676Sjpk } 36790Sstevel@tonic-gate } 36800Sstevel@tonic-gate if (bufsize > size) 36810Sstevel@tonic-gate bufsize = size; 36820Sstevel@tonic-gate if (buf != NULL) { 36830Sstevel@tonic-gate err = copyoutstr(zonepath, buf, bufsize, NULL); 36840Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 36850Sstevel@tonic-gate error = EFAULT; 36860Sstevel@tonic-gate } 36871676Sjpk if (global || (is_system_labeled() && !curzone)) 36880Sstevel@tonic-gate kmem_free(zonepath, size); 36890Sstevel@tonic-gate break; 36900Sstevel@tonic-gate 36910Sstevel@tonic-gate case ZONE_ATTR_NAME: 36920Sstevel@tonic-gate size = strlen(zone->zone_name) + 1; 36930Sstevel@tonic-gate if (bufsize > size) 36940Sstevel@tonic-gate bufsize = size; 36950Sstevel@tonic-gate if (buf != NULL) { 36960Sstevel@tonic-gate err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 36970Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 36980Sstevel@tonic-gate error = EFAULT; 36990Sstevel@tonic-gate } 37000Sstevel@tonic-gate break; 37010Sstevel@tonic-gate 37020Sstevel@tonic-gate case ZONE_ATTR_STATUS: 37030Sstevel@tonic-gate /* 37040Sstevel@tonic-gate * Since we're not holding zonehash_lock, the zone status 37050Sstevel@tonic-gate * may be anything; leave it up to userland to sort it out. 37060Sstevel@tonic-gate */ 37070Sstevel@tonic-gate size = sizeof (zone_status); 37080Sstevel@tonic-gate if (bufsize > size) 37090Sstevel@tonic-gate bufsize = size; 37100Sstevel@tonic-gate zone_status = zone_status_get(zone); 37110Sstevel@tonic-gate if (buf != NULL && 37120Sstevel@tonic-gate copyout(&zone_status, buf, bufsize) != 0) 37130Sstevel@tonic-gate error = EFAULT; 37140Sstevel@tonic-gate break; 37150Sstevel@tonic-gate case ZONE_ATTR_PRIVSET: 37160Sstevel@tonic-gate size = sizeof (priv_set_t); 37170Sstevel@tonic-gate if (bufsize > size) 37180Sstevel@tonic-gate bufsize = size; 37190Sstevel@tonic-gate if (buf != NULL && 37200Sstevel@tonic-gate copyout(zone->zone_privset, buf, bufsize) != 0) 37210Sstevel@tonic-gate error = EFAULT; 37220Sstevel@tonic-gate break; 37230Sstevel@tonic-gate case ZONE_ATTR_UNIQID: 37240Sstevel@tonic-gate size = sizeof (zone->zone_uniqid); 37250Sstevel@tonic-gate if (bufsize > size) 37260Sstevel@tonic-gate bufsize = size; 37270Sstevel@tonic-gate if (buf != NULL && 37280Sstevel@tonic-gate copyout(&zone->zone_uniqid, buf, bufsize) != 0) 37290Sstevel@tonic-gate error = EFAULT; 37300Sstevel@tonic-gate break; 37310Sstevel@tonic-gate case ZONE_ATTR_POOLID: 37320Sstevel@tonic-gate { 37330Sstevel@tonic-gate pool_t *pool; 37340Sstevel@tonic-gate poolid_t poolid; 37350Sstevel@tonic-gate 37360Sstevel@tonic-gate if (pool_lock_intr() != 0) { 37370Sstevel@tonic-gate error = EINTR; 37380Sstevel@tonic-gate break; 37390Sstevel@tonic-gate } 37400Sstevel@tonic-gate pool = zone_pool_get(zone); 37410Sstevel@tonic-gate poolid = pool->pool_id; 37420Sstevel@tonic-gate pool_unlock(); 37430Sstevel@tonic-gate size = sizeof (poolid); 37440Sstevel@tonic-gate if (bufsize > size) 37450Sstevel@tonic-gate bufsize = size; 37460Sstevel@tonic-gate if (buf != NULL && copyout(&poolid, buf, size) != 0) 37470Sstevel@tonic-gate error = EFAULT; 37480Sstevel@tonic-gate } 37490Sstevel@tonic-gate break; 37501676Sjpk case ZONE_ATTR_SLBL: 37511676Sjpk size = sizeof (bslabel_t); 37521676Sjpk if (bufsize > size) 37531676Sjpk bufsize = size; 37541676Sjpk if (zone->zone_slabel == NULL) 37551676Sjpk error = EINVAL; 37561676Sjpk else if (buf != NULL && 37571676Sjpk copyout(label2bslabel(zone->zone_slabel), buf, 37581676Sjpk bufsize) != 0) 37591676Sjpk error = EFAULT; 37601676Sjpk break; 37610Sstevel@tonic-gate case ZONE_ATTR_INITPID: 37620Sstevel@tonic-gate size = sizeof (initpid); 37630Sstevel@tonic-gate if (bufsize > size) 37640Sstevel@tonic-gate bufsize = size; 37650Sstevel@tonic-gate initpid = zone->zone_proc_initpid; 37660Sstevel@tonic-gate if (initpid == -1) { 37670Sstevel@tonic-gate error = ESRCH; 37680Sstevel@tonic-gate break; 37690Sstevel@tonic-gate } 37700Sstevel@tonic-gate if (buf != NULL && 37710Sstevel@tonic-gate copyout(&initpid, buf, bufsize) != 0) 37720Sstevel@tonic-gate error = EFAULT; 37730Sstevel@tonic-gate break; 37742267Sdp case ZONE_ATTR_INITNAME: 37752267Sdp size = strlen(zone->zone_initname) + 1; 37762267Sdp if (bufsize > size) 37772267Sdp bufsize = size; 37782267Sdp if (buf != NULL) { 37792267Sdp err = copyoutstr(zone->zone_initname, buf, bufsize, 37802267Sdp NULL); 37812267Sdp if (err != 0 && err != ENAMETOOLONG) 37822267Sdp error = EFAULT; 37832267Sdp } 37842267Sdp break; 37852267Sdp case ZONE_ATTR_BOOTARGS: 37862267Sdp if (zone->zone_bootargs == NULL) 37872267Sdp outstr = ""; 37882267Sdp else 37892267Sdp outstr = zone->zone_bootargs; 37902267Sdp size = strlen(outstr) + 1; 37912267Sdp if (bufsize > size) 37922267Sdp bufsize = size; 37932267Sdp if (buf != NULL) { 37942267Sdp err = copyoutstr(outstr, buf, bufsize, NULL); 37952267Sdp if (err != 0 && err != ENAMETOOLONG) 37962267Sdp error = EFAULT; 37972267Sdp } 37982267Sdp break; 37990Sstevel@tonic-gate default: 38000Sstevel@tonic-gate error = EINVAL; 38010Sstevel@tonic-gate } 38020Sstevel@tonic-gate zone_rele(zone); 38030Sstevel@tonic-gate 38040Sstevel@tonic-gate if (error) 38050Sstevel@tonic-gate return (set_errno(error)); 38060Sstevel@tonic-gate return ((ssize_t)size); 38070Sstevel@tonic-gate } 38080Sstevel@tonic-gate 38090Sstevel@tonic-gate /* 38102267Sdp * Systemcall entry point for zone_setattr(2). 38112267Sdp */ 38122267Sdp /*ARGSUSED*/ 38132267Sdp static int 38142267Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 38152267Sdp { 38162267Sdp zone_t *zone; 38172267Sdp zone_status_t zone_status; 38182267Sdp int err; 38192267Sdp 38202267Sdp if (secpolicy_zone_config(CRED()) != 0) 38212267Sdp return (set_errno(EPERM)); 38222267Sdp 38232267Sdp /* 38242267Sdp * At present, attributes can only be set on non-running, 38252267Sdp * non-global zones. 38262267Sdp */ 38272267Sdp if (zoneid == GLOBAL_ZONEID) { 38282267Sdp return (set_errno(EINVAL)); 38292267Sdp } 38302267Sdp 38312267Sdp mutex_enter(&zonehash_lock); 38322267Sdp if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 38332267Sdp mutex_exit(&zonehash_lock); 38342267Sdp return (set_errno(EINVAL)); 38352267Sdp } 38362267Sdp zone_hold(zone); 38372267Sdp mutex_exit(&zonehash_lock); 38382267Sdp 38392267Sdp zone_status = zone_status_get(zone); 38402267Sdp if (zone_status > ZONE_IS_READY) 38412267Sdp goto done; 38422267Sdp 38432267Sdp switch (attr) { 38442267Sdp case ZONE_ATTR_INITNAME: 38452267Sdp err = zone_set_initname(zone, (const char *)buf); 38462267Sdp break; 38472267Sdp case ZONE_ATTR_BOOTARGS: 38482267Sdp err = zone_set_bootargs(zone, (const char *)buf); 38492267Sdp break; 38502267Sdp default: 38512267Sdp err = EINVAL; 38522267Sdp } 38532267Sdp 38542267Sdp done: 38552267Sdp zone_rele(zone); 38562267Sdp return (err != 0 ? set_errno(err) : 0); 38572267Sdp } 38582267Sdp 38592267Sdp /* 38600Sstevel@tonic-gate * Return zero if the process has at least one vnode mapped in to its 38610Sstevel@tonic-gate * address space which shouldn't be allowed to change zones. 38620Sstevel@tonic-gate */ 38630Sstevel@tonic-gate static int 38640Sstevel@tonic-gate as_can_change_zones(void) 38650Sstevel@tonic-gate { 38660Sstevel@tonic-gate proc_t *pp = curproc; 38670Sstevel@tonic-gate struct seg *seg; 38680Sstevel@tonic-gate struct as *as = pp->p_as; 38690Sstevel@tonic-gate vnode_t *vp; 38700Sstevel@tonic-gate int allow = 1; 38710Sstevel@tonic-gate 38720Sstevel@tonic-gate ASSERT(pp->p_as != &kas); 38730Sstevel@tonic-gate AS_LOCK_ENTER(&as, &as->a_lock, RW_READER); 38740Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 38750Sstevel@tonic-gate /* 38760Sstevel@tonic-gate * if we can't get a backing vnode for this segment then skip 38770Sstevel@tonic-gate * it. 38780Sstevel@tonic-gate */ 38790Sstevel@tonic-gate vp = NULL; 38800Sstevel@tonic-gate if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 38810Sstevel@tonic-gate continue; 38820Sstevel@tonic-gate if (!vn_can_change_zones(vp)) { /* bail on first match */ 38830Sstevel@tonic-gate allow = 0; 38840Sstevel@tonic-gate break; 38850Sstevel@tonic-gate } 38860Sstevel@tonic-gate } 38870Sstevel@tonic-gate AS_LOCK_EXIT(&as, &as->a_lock); 38880Sstevel@tonic-gate return (allow); 38890Sstevel@tonic-gate } 38900Sstevel@tonic-gate 38910Sstevel@tonic-gate /* 38920Sstevel@tonic-gate * Systemcall entry point for zone_enter(). 38930Sstevel@tonic-gate * 38940Sstevel@tonic-gate * The current process is injected into said zone. In the process 38950Sstevel@tonic-gate * it will change its project membership, privileges, rootdir/cwd, 38960Sstevel@tonic-gate * zone-wide rctls, and pool association to match those of the zone. 38970Sstevel@tonic-gate * 38980Sstevel@tonic-gate * The first zone_enter() called while the zone is in the ZONE_IS_READY 38990Sstevel@tonic-gate * state will transition it to ZONE_IS_RUNNING. Processes may only 39000Sstevel@tonic-gate * enter a zone that is "ready" or "running". 39010Sstevel@tonic-gate */ 39020Sstevel@tonic-gate static int 39030Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 39040Sstevel@tonic-gate { 39050Sstevel@tonic-gate zone_t *zone; 39060Sstevel@tonic-gate vnode_t *vp; 39070Sstevel@tonic-gate proc_t *pp = curproc; 39080Sstevel@tonic-gate contract_t *ct; 39090Sstevel@tonic-gate cont_process_t *ctp; 39100Sstevel@tonic-gate task_t *tk, *oldtk; 39110Sstevel@tonic-gate kproject_t *zone_proj0; 39120Sstevel@tonic-gate cred_t *cr, *newcr; 39130Sstevel@tonic-gate pool_t *oldpool, *newpool; 39140Sstevel@tonic-gate sess_t *sp; 39150Sstevel@tonic-gate uid_t uid; 39160Sstevel@tonic-gate zone_status_t status; 39170Sstevel@tonic-gate int err = 0; 39180Sstevel@tonic-gate rctl_entity_p_t e; 39190Sstevel@tonic-gate 39200Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 39210Sstevel@tonic-gate return (set_errno(EPERM)); 39220Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 39230Sstevel@tonic-gate return (set_errno(EINVAL)); 39240Sstevel@tonic-gate 39250Sstevel@tonic-gate /* 39260Sstevel@tonic-gate * Stop all lwps so we don't need to hold a lock to look at 39270Sstevel@tonic-gate * curproc->p_zone. This needs to happen before we grab any 39280Sstevel@tonic-gate * locks to avoid deadlock (another lwp in the process could 39290Sstevel@tonic-gate * be waiting for the held lock). 39300Sstevel@tonic-gate */ 39310Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 39320Sstevel@tonic-gate return (set_errno(EINTR)); 39330Sstevel@tonic-gate 39340Sstevel@tonic-gate /* 39350Sstevel@tonic-gate * Make sure we're not changing zones with files open or mapped in 39360Sstevel@tonic-gate * to our address space which shouldn't be changing zones. 39370Sstevel@tonic-gate */ 39380Sstevel@tonic-gate if (!files_can_change_zones()) { 39390Sstevel@tonic-gate err = EBADF; 39400Sstevel@tonic-gate goto out; 39410Sstevel@tonic-gate } 39420Sstevel@tonic-gate if (!as_can_change_zones()) { 39430Sstevel@tonic-gate err = EFAULT; 39440Sstevel@tonic-gate goto out; 39450Sstevel@tonic-gate } 39460Sstevel@tonic-gate 39470Sstevel@tonic-gate mutex_enter(&zonehash_lock); 39480Sstevel@tonic-gate if (pp->p_zone != global_zone) { 39490Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39500Sstevel@tonic-gate err = EINVAL; 39510Sstevel@tonic-gate goto out; 39520Sstevel@tonic-gate } 39530Sstevel@tonic-gate 39540Sstevel@tonic-gate zone = zone_find_all_by_id(zoneid); 39550Sstevel@tonic-gate if (zone == NULL) { 39560Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39570Sstevel@tonic-gate err = EINVAL; 39580Sstevel@tonic-gate goto out; 39590Sstevel@tonic-gate } 39600Sstevel@tonic-gate 39610Sstevel@tonic-gate /* 39620Sstevel@tonic-gate * To prevent processes in a zone from holding contracts on 39630Sstevel@tonic-gate * extrazonal resources, and to avoid process contract 39640Sstevel@tonic-gate * memberships which span zones, contract holders and processes 39650Sstevel@tonic-gate * which aren't the sole members of their encapsulating process 39660Sstevel@tonic-gate * contracts are not allowed to zone_enter. 39670Sstevel@tonic-gate */ 39680Sstevel@tonic-gate ctp = pp->p_ct_process; 39690Sstevel@tonic-gate ct = &ctp->conp_contract; 39700Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 39710Sstevel@tonic-gate mutex_enter(&pp->p_lock); 39720Sstevel@tonic-gate if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 39730Sstevel@tonic-gate mutex_exit(&pp->p_lock); 39740Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 39750Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39760Sstevel@tonic-gate pool_unlock(); 39770Sstevel@tonic-gate err = EINVAL; 39780Sstevel@tonic-gate goto out; 39790Sstevel@tonic-gate } 39800Sstevel@tonic-gate 39810Sstevel@tonic-gate /* 39820Sstevel@tonic-gate * Moreover, we don't allow processes whose encapsulating 39830Sstevel@tonic-gate * process contracts have inherited extrazonal contracts. 39840Sstevel@tonic-gate * While it would be easier to eliminate all process contracts 39850Sstevel@tonic-gate * with inherited contracts, we need to be able to give a 39860Sstevel@tonic-gate * restarted init (or other zone-penetrating process) its 39870Sstevel@tonic-gate * predecessor's contracts. 39880Sstevel@tonic-gate */ 39890Sstevel@tonic-gate if (ctp->conp_ninherited != 0) { 39900Sstevel@tonic-gate contract_t *next; 39910Sstevel@tonic-gate for (next = list_head(&ctp->conp_inherited); next; 39920Sstevel@tonic-gate next = list_next(&ctp->conp_inherited, next)) { 39930Sstevel@tonic-gate if (contract_getzuniqid(next) != zone->zone_uniqid) { 39940Sstevel@tonic-gate mutex_exit(&pp->p_lock); 39950Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 39960Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39970Sstevel@tonic-gate pool_unlock(); 39980Sstevel@tonic-gate err = EINVAL; 39990Sstevel@tonic-gate goto out; 40000Sstevel@tonic-gate } 40010Sstevel@tonic-gate } 40020Sstevel@tonic-gate } 40030Sstevel@tonic-gate mutex_exit(&pp->p_lock); 40040Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 40050Sstevel@tonic-gate 40060Sstevel@tonic-gate status = zone_status_get(zone); 40070Sstevel@tonic-gate if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 40080Sstevel@tonic-gate /* 40090Sstevel@tonic-gate * Can't join 40100Sstevel@tonic-gate */ 40110Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40120Sstevel@tonic-gate err = EINVAL; 40130Sstevel@tonic-gate goto out; 40140Sstevel@tonic-gate } 40150Sstevel@tonic-gate 40160Sstevel@tonic-gate /* 40170Sstevel@tonic-gate * Make sure new priv set is within the permitted set for caller 40180Sstevel@tonic-gate */ 40190Sstevel@tonic-gate if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 40200Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40210Sstevel@tonic-gate err = EPERM; 40220Sstevel@tonic-gate goto out; 40230Sstevel@tonic-gate } 40240Sstevel@tonic-gate /* 40250Sstevel@tonic-gate * We want to momentarily drop zonehash_lock while we optimistically 40260Sstevel@tonic-gate * bind curproc to the pool it should be running in. This is safe 40270Sstevel@tonic-gate * since the zone can't disappear (we have a hold on it). 40280Sstevel@tonic-gate */ 40290Sstevel@tonic-gate zone_hold(zone); 40300Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40310Sstevel@tonic-gate 40320Sstevel@tonic-gate /* 40330Sstevel@tonic-gate * Grab pool_lock to keep the pools configuration from changing 40340Sstevel@tonic-gate * and to stop ourselves from getting rebound to another pool 40350Sstevel@tonic-gate * until we join the zone. 40360Sstevel@tonic-gate */ 40370Sstevel@tonic-gate if (pool_lock_intr() != 0) { 40380Sstevel@tonic-gate zone_rele(zone); 40390Sstevel@tonic-gate err = EINTR; 40400Sstevel@tonic-gate goto out; 40410Sstevel@tonic-gate } 40420Sstevel@tonic-gate ASSERT(secpolicy_pool(CRED()) == 0); 40430Sstevel@tonic-gate /* 40440Sstevel@tonic-gate * Bind ourselves to the pool currently associated with the zone. 40450Sstevel@tonic-gate */ 40460Sstevel@tonic-gate oldpool = curproc->p_pool; 40470Sstevel@tonic-gate newpool = zone_pool_get(zone); 40480Sstevel@tonic-gate if (pool_state == POOL_ENABLED && newpool != oldpool && 40490Sstevel@tonic-gate (err = pool_do_bind(newpool, P_PID, P_MYID, 40500Sstevel@tonic-gate POOL_BIND_ALL)) != 0) { 40510Sstevel@tonic-gate pool_unlock(); 40520Sstevel@tonic-gate zone_rele(zone); 40530Sstevel@tonic-gate goto out; 40540Sstevel@tonic-gate } 40550Sstevel@tonic-gate 40560Sstevel@tonic-gate /* 40570Sstevel@tonic-gate * Grab cpu_lock now; we'll need it later when we call 40580Sstevel@tonic-gate * task_join(). 40590Sstevel@tonic-gate */ 40600Sstevel@tonic-gate mutex_enter(&cpu_lock); 40610Sstevel@tonic-gate mutex_enter(&zonehash_lock); 40620Sstevel@tonic-gate /* 40630Sstevel@tonic-gate * Make sure the zone hasn't moved on since we dropped zonehash_lock. 40640Sstevel@tonic-gate */ 40650Sstevel@tonic-gate if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 40660Sstevel@tonic-gate /* 40670Sstevel@tonic-gate * Can't join anymore. 40680Sstevel@tonic-gate */ 40690Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40700Sstevel@tonic-gate mutex_exit(&cpu_lock); 40710Sstevel@tonic-gate if (pool_state == POOL_ENABLED && 40720Sstevel@tonic-gate newpool != oldpool) 40730Sstevel@tonic-gate (void) pool_do_bind(oldpool, P_PID, P_MYID, 40740Sstevel@tonic-gate POOL_BIND_ALL); 40750Sstevel@tonic-gate pool_unlock(); 40760Sstevel@tonic-gate zone_rele(zone); 40770Sstevel@tonic-gate err = EINVAL; 40780Sstevel@tonic-gate goto out; 40790Sstevel@tonic-gate } 40800Sstevel@tonic-gate 40810Sstevel@tonic-gate mutex_enter(&pp->p_lock); 40820Sstevel@tonic-gate zone_proj0 = zone->zone_zsched->p_task->tk_proj; 40830Sstevel@tonic-gate /* verify that we do not exceed and task or lwp limits */ 40840Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 40850Sstevel@tonic-gate /* add new lwps to zone and zone's proj0 */ 40860Sstevel@tonic-gate zone_proj0->kpj_nlwps += pp->p_lwpcnt; 40870Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 40880Sstevel@tonic-gate /* add 1 task to zone's proj0 */ 40890Sstevel@tonic-gate zone_proj0->kpj_ntasks += 1; 40900Sstevel@tonic-gate mutex_exit(&pp->p_lock); 40910Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 40920Sstevel@tonic-gate 40930Sstevel@tonic-gate /* remove lwps from proc's old zone and old project */ 40940Sstevel@tonic-gate mutex_enter(&pp->p_zone->zone_nlwps_lock); 40950Sstevel@tonic-gate pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 40960Sstevel@tonic-gate pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 40970Sstevel@tonic-gate mutex_exit(&pp->p_zone->zone_nlwps_lock); 40980Sstevel@tonic-gate 40990Sstevel@tonic-gate /* 41000Sstevel@tonic-gate * Joining the zone cannot fail from now on. 41010Sstevel@tonic-gate * 41020Sstevel@tonic-gate * This means that a lot of the following code can be commonized and 41030Sstevel@tonic-gate * shared with zsched(). 41040Sstevel@tonic-gate */ 41050Sstevel@tonic-gate 41060Sstevel@tonic-gate /* 41070Sstevel@tonic-gate * Reset the encapsulating process contract's zone. 41080Sstevel@tonic-gate */ 41090Sstevel@tonic-gate ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 41100Sstevel@tonic-gate contract_setzuniqid(ct, zone->zone_uniqid); 41110Sstevel@tonic-gate 41120Sstevel@tonic-gate /* 41130Sstevel@tonic-gate * Create a new task and associate the process with the project keyed 41140Sstevel@tonic-gate * by (projid,zoneid). 41150Sstevel@tonic-gate * 41160Sstevel@tonic-gate * We might as well be in project 0; the global zone's projid doesn't 41170Sstevel@tonic-gate * make much sense in a zone anyhow. 41180Sstevel@tonic-gate * 41190Sstevel@tonic-gate * This also increments zone_ntasks, and returns with p_lock held. 41200Sstevel@tonic-gate */ 41210Sstevel@tonic-gate tk = task_create(0, zone); 41220Sstevel@tonic-gate oldtk = task_join(tk, 0); 41230Sstevel@tonic-gate mutex_exit(&cpu_lock); 41240Sstevel@tonic-gate 41250Sstevel@tonic-gate pp->p_flag |= SZONETOP; 41260Sstevel@tonic-gate pp->p_zone = zone; 41270Sstevel@tonic-gate 41280Sstevel@tonic-gate /* 41290Sstevel@tonic-gate * call RCTLOP_SET functions on this proc 41300Sstevel@tonic-gate */ 41310Sstevel@tonic-gate e.rcep_p.zone = zone; 41320Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 41330Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 41340Sstevel@tonic-gate RCD_CALLBACK); 41350Sstevel@tonic-gate mutex_exit(&pp->p_lock); 41360Sstevel@tonic-gate 41370Sstevel@tonic-gate /* 41380Sstevel@tonic-gate * We don't need to hold any of zsched's locks here; not only do we know 41390Sstevel@tonic-gate * the process and zone aren't going away, we know its session isn't 41400Sstevel@tonic-gate * changing either. 41410Sstevel@tonic-gate * 41420Sstevel@tonic-gate * By joining zsched's session here, we mimic the behavior in the 41430Sstevel@tonic-gate * global zone of init's sid being the pid of sched. We extend this 41440Sstevel@tonic-gate * to all zlogin-like zone_enter()'ing processes as well. 41450Sstevel@tonic-gate */ 41460Sstevel@tonic-gate mutex_enter(&pidlock); 41470Sstevel@tonic-gate sp = zone->zone_zsched->p_sessp; 41480Sstevel@tonic-gate SESS_HOLD(sp); 41490Sstevel@tonic-gate mutex_enter(&pp->p_lock); 41500Sstevel@tonic-gate pgexit(pp); 41510Sstevel@tonic-gate SESS_RELE(pp->p_sessp); 41520Sstevel@tonic-gate pp->p_sessp = sp; 41530Sstevel@tonic-gate pgjoin(pp, zone->zone_zsched->p_pidp); 41540Sstevel@tonic-gate mutex_exit(&pp->p_lock); 41550Sstevel@tonic-gate mutex_exit(&pidlock); 41560Sstevel@tonic-gate 41570Sstevel@tonic-gate mutex_exit(&zonehash_lock); 41580Sstevel@tonic-gate /* 41590Sstevel@tonic-gate * We're firmly in the zone; let pools progress. 41600Sstevel@tonic-gate */ 41610Sstevel@tonic-gate pool_unlock(); 41620Sstevel@tonic-gate task_rele(oldtk); 41630Sstevel@tonic-gate /* 41640Sstevel@tonic-gate * We don't need to retain a hold on the zone since we already 41650Sstevel@tonic-gate * incremented zone_ntasks, so the zone isn't going anywhere. 41660Sstevel@tonic-gate */ 41670Sstevel@tonic-gate zone_rele(zone); 41680Sstevel@tonic-gate 41690Sstevel@tonic-gate /* 41700Sstevel@tonic-gate * Chroot 41710Sstevel@tonic-gate */ 41720Sstevel@tonic-gate vp = zone->zone_rootvp; 41730Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 41740Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 41750Sstevel@tonic-gate 41760Sstevel@tonic-gate /* 41770Sstevel@tonic-gate * Change process credentials 41780Sstevel@tonic-gate */ 41790Sstevel@tonic-gate newcr = cralloc(); 41800Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 41810Sstevel@tonic-gate cr = pp->p_cred; 41820Sstevel@tonic-gate crcopy_to(cr, newcr); 41830Sstevel@tonic-gate crsetzone(newcr, zone); 41840Sstevel@tonic-gate pp->p_cred = newcr; 41850Sstevel@tonic-gate 41860Sstevel@tonic-gate /* 41870Sstevel@tonic-gate * Restrict all process privilege sets to zone limit 41880Sstevel@tonic-gate */ 41890Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 41900Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 41910Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 41920Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 41930Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 41940Sstevel@tonic-gate crset(pp, newcr); 41950Sstevel@tonic-gate 41960Sstevel@tonic-gate /* 41970Sstevel@tonic-gate * Adjust upcount to reflect zone entry. 41980Sstevel@tonic-gate */ 41990Sstevel@tonic-gate uid = crgetruid(newcr); 42000Sstevel@tonic-gate mutex_enter(&pidlock); 42010Sstevel@tonic-gate upcount_dec(uid, GLOBAL_ZONEID); 42020Sstevel@tonic-gate upcount_inc(uid, zoneid); 42030Sstevel@tonic-gate mutex_exit(&pidlock); 42040Sstevel@tonic-gate 42050Sstevel@tonic-gate /* 42060Sstevel@tonic-gate * Set up core file path and content. 42070Sstevel@tonic-gate */ 42080Sstevel@tonic-gate set_core_defaults(); 42090Sstevel@tonic-gate 42100Sstevel@tonic-gate out: 42110Sstevel@tonic-gate /* 42120Sstevel@tonic-gate * Let the other lwps continue. 42130Sstevel@tonic-gate */ 42140Sstevel@tonic-gate mutex_enter(&pp->p_lock); 42150Sstevel@tonic-gate if (curthread != pp->p_agenttp) 42160Sstevel@tonic-gate continuelwps(pp); 42170Sstevel@tonic-gate mutex_exit(&pp->p_lock); 42180Sstevel@tonic-gate 42190Sstevel@tonic-gate return (err != 0 ? set_errno(err) : 0); 42200Sstevel@tonic-gate } 42210Sstevel@tonic-gate 42220Sstevel@tonic-gate /* 42230Sstevel@tonic-gate * Systemcall entry point for zone_list(2). 42240Sstevel@tonic-gate * 42250Sstevel@tonic-gate * Processes running in a (non-global) zone only see themselves. 42261676Sjpk * On labeled systems, they see all zones whose label they dominate. 42270Sstevel@tonic-gate */ 42280Sstevel@tonic-gate static int 42290Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones) 42300Sstevel@tonic-gate { 42310Sstevel@tonic-gate zoneid_t *zoneids; 42321769Scarlsonj zone_t *zone, *myzone; 42330Sstevel@tonic-gate uint_t user_nzones, real_nzones; 42341676Sjpk uint_t domi_nzones; 42351676Sjpk int error; 42360Sstevel@tonic-gate 42370Sstevel@tonic-gate if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 42380Sstevel@tonic-gate return (set_errno(EFAULT)); 42390Sstevel@tonic-gate 42401769Scarlsonj myzone = curproc->p_zone; 42411769Scarlsonj if (myzone != global_zone) { 42421676Sjpk bslabel_t *mybslab; 42431676Sjpk 42441676Sjpk if (!is_system_labeled()) { 42451676Sjpk /* just return current zone */ 42461676Sjpk real_nzones = domi_nzones = 1; 42471676Sjpk zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 42481769Scarlsonj zoneids[0] = myzone->zone_id; 42491676Sjpk } else { 42501676Sjpk /* return all zones that are dominated */ 42511676Sjpk mutex_enter(&zonehash_lock); 42521676Sjpk real_nzones = zonecount; 42531676Sjpk domi_nzones = 0; 42541676Sjpk if (real_nzones > 0) { 42551676Sjpk zoneids = kmem_alloc(real_nzones * 42561676Sjpk sizeof (zoneid_t), KM_SLEEP); 42571769Scarlsonj mybslab = label2bslabel(myzone->zone_slabel); 42581676Sjpk for (zone = list_head(&zone_active); 42591676Sjpk zone != NULL; 42601676Sjpk zone = list_next(&zone_active, zone)) { 42611676Sjpk if (zone->zone_id == GLOBAL_ZONEID) 42621676Sjpk continue; 42631769Scarlsonj if (zone != myzone && 42641769Scarlsonj (zone->zone_flags & ZF_IS_SCRATCH)) 42651769Scarlsonj continue; 42661769Scarlsonj /* 42671769Scarlsonj * Note that a label always dominates 42681769Scarlsonj * itself, so myzone is always included 42691769Scarlsonj * in the list. 42701769Scarlsonj */ 42711676Sjpk if (bldominates(mybslab, 42721676Sjpk label2bslabel(zone->zone_slabel))) { 42731676Sjpk zoneids[domi_nzones++] = 42741676Sjpk zone->zone_id; 42751676Sjpk } 42761676Sjpk } 42771676Sjpk } 42781676Sjpk mutex_exit(&zonehash_lock); 42791676Sjpk } 42800Sstevel@tonic-gate } else { 42810Sstevel@tonic-gate mutex_enter(&zonehash_lock); 42820Sstevel@tonic-gate real_nzones = zonecount; 42831676Sjpk domi_nzones = 0; 42841676Sjpk if (real_nzones > 0) { 42850Sstevel@tonic-gate zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 42860Sstevel@tonic-gate KM_SLEEP); 42870Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 42880Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 42891676Sjpk zoneids[domi_nzones++] = zone->zone_id; 42901676Sjpk ASSERT(domi_nzones == real_nzones); 42910Sstevel@tonic-gate } 42920Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42930Sstevel@tonic-gate } 42940Sstevel@tonic-gate 42951676Sjpk /* 42961676Sjpk * If user has allocated space for fewer entries than we found, then 42971676Sjpk * return only up to his limit. Either way, tell him exactly how many 42981676Sjpk * we found. 42991676Sjpk */ 43001676Sjpk if (domi_nzones < user_nzones) 43011676Sjpk user_nzones = domi_nzones; 43021676Sjpk error = 0; 43031676Sjpk if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) { 43040Sstevel@tonic-gate error = EFAULT; 43051676Sjpk } else if (zoneidlist != NULL && user_nzones != 0) { 43060Sstevel@tonic-gate if (copyout(zoneids, zoneidlist, 43070Sstevel@tonic-gate user_nzones * sizeof (zoneid_t)) != 0) 43080Sstevel@tonic-gate error = EFAULT; 43090Sstevel@tonic-gate } 43100Sstevel@tonic-gate 43111676Sjpk if (real_nzones > 0) 43120Sstevel@tonic-gate kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 43130Sstevel@tonic-gate 43141676Sjpk if (error != 0) 43150Sstevel@tonic-gate return (set_errno(error)); 43160Sstevel@tonic-gate else 43170Sstevel@tonic-gate return (0); 43180Sstevel@tonic-gate } 43190Sstevel@tonic-gate 43200Sstevel@tonic-gate /* 43210Sstevel@tonic-gate * Systemcall entry point for zone_lookup(2). 43220Sstevel@tonic-gate * 43231676Sjpk * Non-global zones are only able to see themselves and (on labeled systems) 43241676Sjpk * the zones they dominate. 43250Sstevel@tonic-gate */ 43260Sstevel@tonic-gate static zoneid_t 43270Sstevel@tonic-gate zone_lookup(const char *zone_name) 43280Sstevel@tonic-gate { 43290Sstevel@tonic-gate char *kname; 43300Sstevel@tonic-gate zone_t *zone; 43310Sstevel@tonic-gate zoneid_t zoneid; 43320Sstevel@tonic-gate int err; 43330Sstevel@tonic-gate 43340Sstevel@tonic-gate if (zone_name == NULL) { 43350Sstevel@tonic-gate /* return caller's zone id */ 43360Sstevel@tonic-gate return (getzoneid()); 43370Sstevel@tonic-gate } 43380Sstevel@tonic-gate 43390Sstevel@tonic-gate kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 43400Sstevel@tonic-gate if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 43410Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 43420Sstevel@tonic-gate return (set_errno(err)); 43430Sstevel@tonic-gate } 43440Sstevel@tonic-gate 43450Sstevel@tonic-gate mutex_enter(&zonehash_lock); 43460Sstevel@tonic-gate zone = zone_find_all_by_name(kname); 43470Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 43481676Sjpk /* 43491676Sjpk * In a non-global zone, can only lookup global and own name. 43501676Sjpk * In Trusted Extensions zone label dominance rules apply. 43511676Sjpk */ 43521676Sjpk if (zone == NULL || 43531676Sjpk zone_status_get(zone) < ZONE_IS_READY || 43541676Sjpk !zone_list_access(zone)) { 43550Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43560Sstevel@tonic-gate return (set_errno(EINVAL)); 43571676Sjpk } else { 43581676Sjpk zoneid = zone->zone_id; 43591676Sjpk mutex_exit(&zonehash_lock); 43601676Sjpk return (zoneid); 43610Sstevel@tonic-gate } 43620Sstevel@tonic-gate } 43630Sstevel@tonic-gate 4364813Sdp static int 4365813Sdp zone_version(int *version_arg) 4366813Sdp { 4367813Sdp int version = ZONE_SYSCALL_API_VERSION; 4368813Sdp 4369813Sdp if (copyout(&version, version_arg, sizeof (int)) != 0) 4370813Sdp return (set_errno(EFAULT)); 4371813Sdp return (0); 4372813Sdp } 4373813Sdp 43740Sstevel@tonic-gate /* ARGSUSED */ 43750Sstevel@tonic-gate long 4376789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4) 43770Sstevel@tonic-gate { 43780Sstevel@tonic-gate zone_def zs; 43790Sstevel@tonic-gate 43800Sstevel@tonic-gate switch (cmd) { 43810Sstevel@tonic-gate case ZONE_CREATE: 43820Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 43830Sstevel@tonic-gate if (copyin(arg1, &zs, sizeof (zone_def))) { 43840Sstevel@tonic-gate return (set_errno(EFAULT)); 43850Sstevel@tonic-gate } 43860Sstevel@tonic-gate } else { 43870Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 43880Sstevel@tonic-gate zone_def32 zs32; 43890Sstevel@tonic-gate 43900Sstevel@tonic-gate if (copyin(arg1, &zs32, sizeof (zone_def32))) { 43910Sstevel@tonic-gate return (set_errno(EFAULT)); 43920Sstevel@tonic-gate } 43930Sstevel@tonic-gate zs.zone_name = 43940Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_name; 43950Sstevel@tonic-gate zs.zone_root = 43960Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_root; 43970Sstevel@tonic-gate zs.zone_privs = 43980Sstevel@tonic-gate (const struct priv_set *) 43990Sstevel@tonic-gate (unsigned long)zs32.zone_privs; 44001409Sdp zs.zone_privssz = zs32.zone_privssz; 44010Sstevel@tonic-gate zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 44020Sstevel@tonic-gate zs.rctlbufsz = zs32.rctlbufsz; 4403789Sahrens zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf; 4404789Sahrens zs.zfsbufsz = zs32.zfsbufsz; 44050Sstevel@tonic-gate zs.extended_error = 44060Sstevel@tonic-gate (int *)(unsigned long)zs32.extended_error; 44071676Sjpk zs.match = zs32.match; 44081676Sjpk zs.doi = zs32.doi; 44091676Sjpk zs.label = (const bslabel_t *)(uintptr_t)zs32.label; 44100Sstevel@tonic-gate #else 44110Sstevel@tonic-gate panic("get_udatamodel() returned bogus result\n"); 44120Sstevel@tonic-gate #endif 44130Sstevel@tonic-gate } 44140Sstevel@tonic-gate 44150Sstevel@tonic-gate return (zone_create(zs.zone_name, zs.zone_root, 4416813Sdp zs.zone_privs, zs.zone_privssz, 4417813Sdp (caddr_t)zs.rctlbuf, zs.rctlbufsz, 4418813Sdp (caddr_t)zs.zfsbuf, zs.zfsbufsz, 44191676Sjpk zs.extended_error, zs.match, zs.doi, 44201676Sjpk zs.label)); 44210Sstevel@tonic-gate case ZONE_BOOT: 44222267Sdp return (zone_boot((zoneid_t)(uintptr_t)arg1)); 44230Sstevel@tonic-gate case ZONE_DESTROY: 44240Sstevel@tonic-gate return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 44250Sstevel@tonic-gate case ZONE_GETATTR: 44260Sstevel@tonic-gate return (zone_getattr((zoneid_t)(uintptr_t)arg1, 44270Sstevel@tonic-gate (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 44282267Sdp case ZONE_SETATTR: 44292267Sdp return (zone_setattr((zoneid_t)(uintptr_t)arg1, 44302267Sdp (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 44310Sstevel@tonic-gate case ZONE_ENTER: 44320Sstevel@tonic-gate return (zone_enter((zoneid_t)(uintptr_t)arg1)); 44330Sstevel@tonic-gate case ZONE_LIST: 44340Sstevel@tonic-gate return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 44350Sstevel@tonic-gate case ZONE_SHUTDOWN: 44360Sstevel@tonic-gate return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 44370Sstevel@tonic-gate case ZONE_LOOKUP: 44380Sstevel@tonic-gate return (zone_lookup((const char *)arg1)); 4439813Sdp case ZONE_VERSION: 4440813Sdp return (zone_version((int *)arg1)); 44410Sstevel@tonic-gate default: 44420Sstevel@tonic-gate return (set_errno(EINVAL)); 44430Sstevel@tonic-gate } 44440Sstevel@tonic-gate } 44450Sstevel@tonic-gate 44460Sstevel@tonic-gate struct zarg { 44470Sstevel@tonic-gate zone_t *zone; 44480Sstevel@tonic-gate zone_cmd_arg_t arg; 44490Sstevel@tonic-gate }; 44500Sstevel@tonic-gate 44510Sstevel@tonic-gate static int 44520Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp) 44530Sstevel@tonic-gate { 44540Sstevel@tonic-gate char *buf; 44550Sstevel@tonic-gate size_t buflen; 44560Sstevel@tonic-gate int error; 44570Sstevel@tonic-gate 44580Sstevel@tonic-gate buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 44590Sstevel@tonic-gate buf = kmem_alloc(buflen, KM_SLEEP); 44600Sstevel@tonic-gate (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 44610Sstevel@tonic-gate error = door_ki_open(buf, doorp); 44620Sstevel@tonic-gate kmem_free(buf, buflen); 44630Sstevel@tonic-gate return (error); 44640Sstevel@tonic-gate } 44650Sstevel@tonic-gate 44660Sstevel@tonic-gate static void 44670Sstevel@tonic-gate zone_release_door(door_handle_t *doorp) 44680Sstevel@tonic-gate { 44690Sstevel@tonic-gate door_ki_rele(*doorp); 44700Sstevel@tonic-gate *doorp = NULL; 44710Sstevel@tonic-gate } 44720Sstevel@tonic-gate 44730Sstevel@tonic-gate static void 44740Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp) 44750Sstevel@tonic-gate { 44760Sstevel@tonic-gate door_handle_t door = NULL; 44770Sstevel@tonic-gate door_arg_t darg, save_arg; 44780Sstevel@tonic-gate char *zone_name; 44790Sstevel@tonic-gate size_t zone_namelen; 44800Sstevel@tonic-gate zoneid_t zoneid; 44810Sstevel@tonic-gate zone_t *zone; 44820Sstevel@tonic-gate zone_cmd_arg_t arg; 44830Sstevel@tonic-gate uint64_t uniqid; 44840Sstevel@tonic-gate size_t size; 44850Sstevel@tonic-gate int error; 44860Sstevel@tonic-gate int retry; 44870Sstevel@tonic-gate 44880Sstevel@tonic-gate zone = zargp->zone; 44890Sstevel@tonic-gate arg = zargp->arg; 44900Sstevel@tonic-gate kmem_free(zargp, sizeof (*zargp)); 44910Sstevel@tonic-gate 44920Sstevel@tonic-gate zone_namelen = strlen(zone->zone_name) + 1; 44930Sstevel@tonic-gate zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 44940Sstevel@tonic-gate bcopy(zone->zone_name, zone_name, zone_namelen); 44950Sstevel@tonic-gate zoneid = zone->zone_id; 44960Sstevel@tonic-gate uniqid = zone->zone_uniqid; 44970Sstevel@tonic-gate /* 44980Sstevel@tonic-gate * zoneadmd may be down, but at least we can empty out the zone. 44990Sstevel@tonic-gate * We can ignore the return value of zone_empty() since we're called 45000Sstevel@tonic-gate * from a kernel thread and know we won't be delivered any signals. 45010Sstevel@tonic-gate */ 45020Sstevel@tonic-gate ASSERT(curproc == &p0); 45030Sstevel@tonic-gate (void) zone_empty(zone); 45040Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 45050Sstevel@tonic-gate zone_rele(zone); 45060Sstevel@tonic-gate 45070Sstevel@tonic-gate size = sizeof (arg); 45080Sstevel@tonic-gate darg.rbuf = (char *)&arg; 45090Sstevel@tonic-gate darg.data_ptr = (char *)&arg; 45100Sstevel@tonic-gate darg.rsize = size; 45110Sstevel@tonic-gate darg.data_size = size; 45120Sstevel@tonic-gate darg.desc_ptr = NULL; 45130Sstevel@tonic-gate darg.desc_num = 0; 45140Sstevel@tonic-gate 45150Sstevel@tonic-gate save_arg = darg; 45160Sstevel@tonic-gate /* 45170Sstevel@tonic-gate * Since we're not holding a reference to the zone, any number of 45180Sstevel@tonic-gate * things can go wrong, including the zone disappearing before we get a 45190Sstevel@tonic-gate * chance to talk to zoneadmd. 45200Sstevel@tonic-gate */ 45210Sstevel@tonic-gate for (retry = 0; /* forever */; retry++) { 45220Sstevel@tonic-gate if (door == NULL && 45230Sstevel@tonic-gate (error = zone_lookup_door(zone_name, &door)) != 0) { 45240Sstevel@tonic-gate goto next; 45250Sstevel@tonic-gate } 45260Sstevel@tonic-gate ASSERT(door != NULL); 45270Sstevel@tonic-gate 45280Sstevel@tonic-gate if ((error = door_ki_upcall(door, &darg)) == 0) { 45290Sstevel@tonic-gate break; 45300Sstevel@tonic-gate } 45310Sstevel@tonic-gate switch (error) { 45320Sstevel@tonic-gate case EINTR: 45330Sstevel@tonic-gate /* FALLTHROUGH */ 45340Sstevel@tonic-gate case EAGAIN: /* process may be forking */ 45350Sstevel@tonic-gate /* 45360Sstevel@tonic-gate * Back off for a bit 45370Sstevel@tonic-gate */ 45380Sstevel@tonic-gate break; 45390Sstevel@tonic-gate case EBADF: 45400Sstevel@tonic-gate zone_release_door(&door); 45410Sstevel@tonic-gate if (zone_lookup_door(zone_name, &door) != 0) { 45420Sstevel@tonic-gate /* 45430Sstevel@tonic-gate * zoneadmd may be dead, but it may come back to 45440Sstevel@tonic-gate * life later. 45450Sstevel@tonic-gate */ 45460Sstevel@tonic-gate break; 45470Sstevel@tonic-gate } 45480Sstevel@tonic-gate break; 45490Sstevel@tonic-gate default: 45500Sstevel@tonic-gate cmn_err(CE_WARN, 45510Sstevel@tonic-gate "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 45520Sstevel@tonic-gate error); 45530Sstevel@tonic-gate goto out; 45540Sstevel@tonic-gate } 45550Sstevel@tonic-gate next: 45560Sstevel@tonic-gate /* 45570Sstevel@tonic-gate * If this isn't the same zone_t that we originally had in mind, 45580Sstevel@tonic-gate * then this is the same as if two kadmin requests come in at 45590Sstevel@tonic-gate * the same time: the first one wins. This means we lose, so we 45600Sstevel@tonic-gate * bail. 45610Sstevel@tonic-gate */ 45620Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) { 45630Sstevel@tonic-gate /* 45640Sstevel@tonic-gate * Problem is solved. 45650Sstevel@tonic-gate */ 45660Sstevel@tonic-gate break; 45670Sstevel@tonic-gate } 45680Sstevel@tonic-gate if (zone->zone_uniqid != uniqid) { 45690Sstevel@tonic-gate /* 45700Sstevel@tonic-gate * zoneid recycled 45710Sstevel@tonic-gate */ 45720Sstevel@tonic-gate zone_rele(zone); 45730Sstevel@tonic-gate break; 45740Sstevel@tonic-gate } 45750Sstevel@tonic-gate /* 45760Sstevel@tonic-gate * We could zone_status_timedwait(), but there doesn't seem to 45770Sstevel@tonic-gate * be much point in doing that (plus, it would mean that 45780Sstevel@tonic-gate * zone_free() isn't called until this thread exits). 45790Sstevel@tonic-gate */ 45800Sstevel@tonic-gate zone_rele(zone); 45810Sstevel@tonic-gate delay(hz); 45820Sstevel@tonic-gate darg = save_arg; 45830Sstevel@tonic-gate } 45840Sstevel@tonic-gate out: 45850Sstevel@tonic-gate if (door != NULL) { 45860Sstevel@tonic-gate zone_release_door(&door); 45870Sstevel@tonic-gate } 45880Sstevel@tonic-gate kmem_free(zone_name, zone_namelen); 45890Sstevel@tonic-gate thread_exit(); 45900Sstevel@tonic-gate } 45910Sstevel@tonic-gate 45920Sstevel@tonic-gate /* 45932267Sdp * Entry point for uadmin() to tell the zone to go away or reboot. Analog to 45942267Sdp * kadmin(). The caller is a process in the zone. 45950Sstevel@tonic-gate * 45960Sstevel@tonic-gate * In order to shutdown the zone, we will hand off control to zoneadmd 45970Sstevel@tonic-gate * (running in the global zone) via a door. We do a half-hearted job at 45980Sstevel@tonic-gate * killing all processes in the zone, create a kernel thread to contact 45990Sstevel@tonic-gate * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 46000Sstevel@tonic-gate * a form of generation number used to let zoneadmd (as well as 46010Sstevel@tonic-gate * zone_destroy()) know exactly which zone they're re talking about. 46020Sstevel@tonic-gate */ 46030Sstevel@tonic-gate int 46042267Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp) 46050Sstevel@tonic-gate { 46060Sstevel@tonic-gate struct zarg *zargp; 46070Sstevel@tonic-gate zone_cmd_t zcmd; 46080Sstevel@tonic-gate zone_t *zone; 46090Sstevel@tonic-gate 46100Sstevel@tonic-gate zone = curproc->p_zone; 46110Sstevel@tonic-gate ASSERT(getzoneid() != GLOBAL_ZONEID); 46120Sstevel@tonic-gate 46130Sstevel@tonic-gate switch (cmd) { 46140Sstevel@tonic-gate case A_SHUTDOWN: 46150Sstevel@tonic-gate switch (fcn) { 46160Sstevel@tonic-gate case AD_HALT: 46170Sstevel@tonic-gate case AD_POWEROFF: 46180Sstevel@tonic-gate zcmd = Z_HALT; 46190Sstevel@tonic-gate break; 46200Sstevel@tonic-gate case AD_BOOT: 46210Sstevel@tonic-gate zcmd = Z_REBOOT; 46220Sstevel@tonic-gate break; 46230Sstevel@tonic-gate case AD_IBOOT: 46240Sstevel@tonic-gate case AD_SBOOT: 46250Sstevel@tonic-gate case AD_SIBOOT: 46260Sstevel@tonic-gate case AD_NOSYNC: 46270Sstevel@tonic-gate return (ENOTSUP); 46280Sstevel@tonic-gate default: 46290Sstevel@tonic-gate return (EINVAL); 46300Sstevel@tonic-gate } 46310Sstevel@tonic-gate break; 46320Sstevel@tonic-gate case A_REBOOT: 46330Sstevel@tonic-gate zcmd = Z_REBOOT; 46340Sstevel@tonic-gate break; 46350Sstevel@tonic-gate case A_FTRACE: 46360Sstevel@tonic-gate case A_REMOUNT: 46370Sstevel@tonic-gate case A_FREEZE: 46380Sstevel@tonic-gate case A_DUMP: 46390Sstevel@tonic-gate return (ENOTSUP); 46400Sstevel@tonic-gate default: 46410Sstevel@tonic-gate ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 46420Sstevel@tonic-gate return (EINVAL); 46430Sstevel@tonic-gate } 46440Sstevel@tonic-gate 46450Sstevel@tonic-gate if (secpolicy_zone_admin(credp, B_FALSE)) 46460Sstevel@tonic-gate return (EPERM); 46470Sstevel@tonic-gate mutex_enter(&zone_status_lock); 46482267Sdp 46490Sstevel@tonic-gate /* 46500Sstevel@tonic-gate * zone_status can't be ZONE_IS_EMPTY or higher since curproc 46510Sstevel@tonic-gate * is in the zone. 46520Sstevel@tonic-gate */ 46530Sstevel@tonic-gate ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 46540Sstevel@tonic-gate if (zone_status_get(zone) > ZONE_IS_RUNNING) { 46550Sstevel@tonic-gate /* 46560Sstevel@tonic-gate * This zone is already on its way down. 46570Sstevel@tonic-gate */ 46580Sstevel@tonic-gate mutex_exit(&zone_status_lock); 46590Sstevel@tonic-gate return (0); 46600Sstevel@tonic-gate } 46610Sstevel@tonic-gate /* 46620Sstevel@tonic-gate * Prevent future zone_enter()s 46630Sstevel@tonic-gate */ 46640Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 46650Sstevel@tonic-gate mutex_exit(&zone_status_lock); 46660Sstevel@tonic-gate 46670Sstevel@tonic-gate /* 46680Sstevel@tonic-gate * Kill everyone now and call zoneadmd later. 46690Sstevel@tonic-gate * zone_ki_call_zoneadmd() will do a more thorough job of this 46700Sstevel@tonic-gate * later. 46710Sstevel@tonic-gate */ 46720Sstevel@tonic-gate killall(zone->zone_id); 46730Sstevel@tonic-gate /* 46740Sstevel@tonic-gate * Now, create the thread to contact zoneadmd and do the rest of the 46750Sstevel@tonic-gate * work. This thread can't be created in our zone otherwise 46760Sstevel@tonic-gate * zone_destroy() would deadlock. 46770Sstevel@tonic-gate */ 46782267Sdp zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP); 46790Sstevel@tonic-gate zargp->arg.cmd = zcmd; 46800Sstevel@tonic-gate zargp->arg.uniqid = zone->zone_uniqid; 46812267Sdp zargp->zone = zone; 46820Sstevel@tonic-gate (void) strcpy(zargp->arg.locale, "C"); 46832267Sdp /* mdep was already copied in for us by uadmin */ 46842267Sdp if (mdep != NULL) 46852267Sdp (void) strlcpy(zargp->arg.bootbuf, mdep, 46862267Sdp sizeof (zargp->arg.bootbuf)); 46872267Sdp zone_hold(zone); 46880Sstevel@tonic-gate 46890Sstevel@tonic-gate (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 46900Sstevel@tonic-gate TS_RUN, minclsyspri); 46910Sstevel@tonic-gate exit(CLD_EXITED, 0); 46920Sstevel@tonic-gate 46930Sstevel@tonic-gate return (EINVAL); 46940Sstevel@tonic-gate } 46950Sstevel@tonic-gate 46960Sstevel@tonic-gate /* 46970Sstevel@tonic-gate * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 46980Sstevel@tonic-gate * status to ZONE_IS_SHUTTING_DOWN. 46990Sstevel@tonic-gate */ 47000Sstevel@tonic-gate void 47010Sstevel@tonic-gate zone_shutdown_global(void) 47020Sstevel@tonic-gate { 47030Sstevel@tonic-gate ASSERT(curproc->p_zone == global_zone); 47040Sstevel@tonic-gate 47050Sstevel@tonic-gate mutex_enter(&zone_status_lock); 47060Sstevel@tonic-gate ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 47070Sstevel@tonic-gate zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 47080Sstevel@tonic-gate mutex_exit(&zone_status_lock); 47090Sstevel@tonic-gate } 4710789Sahrens 4711789Sahrens /* 4712789Sahrens * Returns true if the named dataset is visible in the current zone. 4713789Sahrens * The 'write' parameter is set to 1 if the dataset is also writable. 4714789Sahrens */ 4715789Sahrens int 4716789Sahrens zone_dataset_visible(const char *dataset, int *write) 4717789Sahrens { 4718789Sahrens zone_dataset_t *zd; 4719789Sahrens size_t len; 4720789Sahrens zone_t *zone = curproc->p_zone; 4721789Sahrens 4722789Sahrens if (dataset[0] == '\0') 4723789Sahrens return (0); 4724789Sahrens 4725789Sahrens /* 4726789Sahrens * Walk the list once, looking for datasets which match exactly, or 4727789Sahrens * specify a dataset underneath an exported dataset. If found, return 4728789Sahrens * true and note that it is writable. 4729789Sahrens */ 4730789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 4731789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 4732789Sahrens 4733789Sahrens len = strlen(zd->zd_dataset); 4734789Sahrens if (strlen(dataset) >= len && 4735789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 4736816Smaybee (dataset[len] == '\0' || dataset[len] == '/' || 4737816Smaybee dataset[len] == '@')) { 4738789Sahrens if (write) 4739789Sahrens *write = 1; 4740789Sahrens return (1); 4741789Sahrens } 4742789Sahrens } 4743789Sahrens 4744789Sahrens /* 4745789Sahrens * Walk the list a second time, searching for datasets which are parents 4746789Sahrens * of exported datasets. These should be visible, but read-only. 4747789Sahrens * 4748789Sahrens * Note that we also have to support forms such as 'pool/dataset/', with 4749789Sahrens * a trailing slash. 4750789Sahrens */ 4751789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 4752789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 4753789Sahrens 4754789Sahrens len = strlen(dataset); 4755789Sahrens if (dataset[len - 1] == '/') 4756789Sahrens len--; /* Ignore trailing slash */ 4757789Sahrens if (len < strlen(zd->zd_dataset) && 4758789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 4759789Sahrens zd->zd_dataset[len] == '/') { 4760789Sahrens if (write) 4761789Sahrens *write = 0; 4762789Sahrens return (1); 4763789Sahrens } 4764789Sahrens } 4765789Sahrens 4766789Sahrens return (0); 4767789Sahrens } 47681676Sjpk 47691676Sjpk /* 47701676Sjpk * zone_find_by_any_path() - 47711676Sjpk * 47721676Sjpk * kernel-private routine similar to zone_find_by_path(), but which 47731676Sjpk * effectively compares against zone paths rather than zonerootpath 47741676Sjpk * (i.e., the last component of zonerootpaths, which should be "root/", 47751676Sjpk * are not compared.) This is done in order to accurately identify all 47761676Sjpk * paths, whether zone-visible or not, including those which are parallel 47771676Sjpk * to /root/, such as /dev/, /home/, etc... 47781676Sjpk * 47791676Sjpk * If the specified path does not fall under any zone path then global 47801676Sjpk * zone is returned. 47811676Sjpk * 47821676Sjpk * The treat_abs parameter indicates whether the path should be treated as 47831676Sjpk * an absolute path although it does not begin with "/". (This supports 47841676Sjpk * nfs mount syntax such as host:any/path.) 47851676Sjpk * 47861676Sjpk * The caller is responsible for zone_rele of the returned zone. 47871676Sjpk */ 47881676Sjpk zone_t * 47891676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs) 47901676Sjpk { 47911676Sjpk zone_t *zone; 47921676Sjpk int path_offset = 0; 47931676Sjpk 47941676Sjpk if (path == NULL) { 47951676Sjpk zone_hold(global_zone); 47961676Sjpk return (global_zone); 47971676Sjpk } 47981676Sjpk 47991676Sjpk if (*path != '/') { 48001676Sjpk ASSERT(treat_abs); 48011676Sjpk path_offset = 1; 48021676Sjpk } 48031676Sjpk 48041676Sjpk mutex_enter(&zonehash_lock); 48051676Sjpk for (zone = list_head(&zone_active); zone != NULL; 48061676Sjpk zone = list_next(&zone_active, zone)) { 48071676Sjpk char *c; 48081676Sjpk size_t pathlen; 48091876Smp46848 char *rootpath_start; 48101676Sjpk 48111676Sjpk if (zone == global_zone) /* skip global zone */ 48121676Sjpk continue; 48131676Sjpk 48141676Sjpk /* scan backwards to find start of last component */ 48151676Sjpk c = zone->zone_rootpath + zone->zone_rootpathlen - 2; 48161676Sjpk do { 48171676Sjpk c--; 48181676Sjpk } while (*c != '/'); 48191676Sjpk 48201876Smp46848 pathlen = c - zone->zone_rootpath + 1 - path_offset; 48211876Smp46848 rootpath_start = (zone->zone_rootpath + path_offset); 48221876Smp46848 if (strncmp(path, rootpath_start, pathlen) == 0) 48231676Sjpk break; 48241676Sjpk } 48251676Sjpk if (zone == NULL) 48261676Sjpk zone = global_zone; 48271676Sjpk zone_hold(zone); 48281676Sjpk mutex_exit(&zonehash_lock); 48291676Sjpk return (zone); 48301676Sjpk } 4831