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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22390Sraf 230Sstevel@tonic-gate /* 24*1409Sdp * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * Zones 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * A zone is a named collection of processes, namespace constraints, 340Sstevel@tonic-gate * and other system resources which comprise a secure and manageable 350Sstevel@tonic-gate * application containment facility. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * Zones (represented by the reference counted zone_t) are tracked in 380Sstevel@tonic-gate * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs 390Sstevel@tonic-gate * (zoneid_t) are used to track zone association. Zone IDs are 400Sstevel@tonic-gate * dynamically generated when the zone is created; if a persistent 410Sstevel@tonic-gate * identifier is needed (core files, accounting logs, audit trail, 420Sstevel@tonic-gate * etc.), the zone name should be used. 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * 450Sstevel@tonic-gate * Global Zone: 460Sstevel@tonic-gate * 470Sstevel@tonic-gate * The global zone (zoneid 0) is automatically associated with all 480Sstevel@tonic-gate * system resources that have not been bound to a user-created zone. 490Sstevel@tonic-gate * This means that even systems where zones are not in active use 500Sstevel@tonic-gate * have a global zone, and all processes, mounts, etc. are 510Sstevel@tonic-gate * associated with that zone. The global zone is generally 520Sstevel@tonic-gate * unconstrained in terms of privileges and access, though the usual 530Sstevel@tonic-gate * credential and privilege based restrictions apply. 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * 560Sstevel@tonic-gate * Zone States: 570Sstevel@tonic-gate * 580Sstevel@tonic-gate * The states in which a zone may be in and the transitions are as 590Sstevel@tonic-gate * follows: 600Sstevel@tonic-gate * 610Sstevel@tonic-gate * ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially 620Sstevel@tonic-gate * initialized zone is added to the list of active zones on the system but 630Sstevel@tonic-gate * isn't accessible. 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * ZONE_IS_READY: zsched (the kernel dummy process for a zone) is 660Sstevel@tonic-gate * ready. The zone is made visible after the ZSD constructor callbacks are 670Sstevel@tonic-gate * executed. A zone remains in this state until it transitions into 680Sstevel@tonic-gate * the ZONE_IS_BOOTING state as a result of a call to zone_boot(). 690Sstevel@tonic-gate * 700Sstevel@tonic-gate * ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start 710Sstevel@tonic-gate * init. Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN 720Sstevel@tonic-gate * state. 730Sstevel@tonic-gate * 740Sstevel@tonic-gate * ZONE_IS_RUNNING: The zone is open for business: zsched has 750Sstevel@tonic-gate * successfully started init. A zone remains in this state until 760Sstevel@tonic-gate * zone_shutdown() is called. 770Sstevel@tonic-gate * 780Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is 790Sstevel@tonic-gate * killing all processes running in the zone. The zone remains 800Sstevel@tonic-gate * in this state until there are no more user processes running in the zone. 810Sstevel@tonic-gate * zone_create(), zone_enter(), and zone_destroy() on this zone will fail. 820Sstevel@tonic-gate * Since zone_shutdown() is restartable, it may be called successfully 830Sstevel@tonic-gate * multiple times for the same zone_t. Setting of the zone's state to 840Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check 850Sstevel@tonic-gate * the zone's status without worrying about it being a moving target. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * ZONE_IS_EMPTY: zone_shutdown() has been called, and there 880Sstevel@tonic-gate * are no more user processes in the zone. The zone remains in this 890Sstevel@tonic-gate * state until there are no more kernel threads associated with the 900Sstevel@tonic-gate * zone. zone_create(), zone_enter(), and zone_destroy() on this zone will 910Sstevel@tonic-gate * fail. 920Sstevel@tonic-gate * 930Sstevel@tonic-gate * ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone 940Sstevel@tonic-gate * have exited. zone_shutdown() returns. Henceforth it is not possible to 950Sstevel@tonic-gate * join the zone or create kernel threads therein. 960Sstevel@tonic-gate * 970Sstevel@tonic-gate * ZONE_IS_DYING: zone_destroy() has been called on the zone; zone 980Sstevel@tonic-gate * remains in this state until zsched exits. Calls to zone_find_by_*() 990Sstevel@tonic-gate * return NULL from now on. 1000Sstevel@tonic-gate * 1010Sstevel@tonic-gate * ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0). There are no 1020Sstevel@tonic-gate * processes or threads doing work on behalf of the zone. The zone is 1030Sstevel@tonic-gate * removed from the list of active zones. zone_destroy() returns, and 1040Sstevel@tonic-gate * the zone can be recreated. 1050Sstevel@tonic-gate * 1060Sstevel@tonic-gate * ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor 1070Sstevel@tonic-gate * callbacks are executed, and all memory associated with the zone is 1080Sstevel@tonic-gate * freed. 1090Sstevel@tonic-gate * 1100Sstevel@tonic-gate * Threads can wait for the zone to enter a requested state by using 1110Sstevel@tonic-gate * zone_status_wait() or zone_status_timedwait() with the desired 1120Sstevel@tonic-gate * state passed in as an argument. Zone state transitions are 1130Sstevel@tonic-gate * uni-directional; it is not possible to move back to an earlier state. 1140Sstevel@tonic-gate * 1150Sstevel@tonic-gate * 1160Sstevel@tonic-gate * Zone-Specific Data: 1170Sstevel@tonic-gate * 1180Sstevel@tonic-gate * Subsystems needing to maintain zone-specific data can store that 1190Sstevel@tonic-gate * data using the ZSD mechanism. This provides a zone-specific data 1200Sstevel@tonic-gate * store, similar to thread-specific data (see pthread_getspecific(3C) 1210Sstevel@tonic-gate * or the TSD code in uts/common/disp/thread.c. Also, ZSD can be used 1220Sstevel@tonic-gate * to register callbacks to be invoked when a zone is created, shut 1230Sstevel@tonic-gate * down, or destroyed. This can be used to initialize zone-specific 1240Sstevel@tonic-gate * data for new zones and to clean up when zones go away. 1250Sstevel@tonic-gate * 1260Sstevel@tonic-gate * 1270Sstevel@tonic-gate * Data Structures: 1280Sstevel@tonic-gate * 1290Sstevel@tonic-gate * The per-zone structure (zone_t) is reference counted, and freed 1300Sstevel@tonic-gate * when all references are released. zone_hold and zone_rele can be 1310Sstevel@tonic-gate * used to adjust the reference count. In addition, reference counts 1320Sstevel@tonic-gate * associated with the cred_t structure are tracked separately using 1330Sstevel@tonic-gate * zone_cred_hold and zone_cred_rele. 1340Sstevel@tonic-gate * 1350Sstevel@tonic-gate * Pointers to active zone_t's are stored in two hash tables; one 1360Sstevel@tonic-gate * for searching by id, the other for searching by name. Lookups 1370Sstevel@tonic-gate * can be performed on either basis, using zone_find_by_id and 1380Sstevel@tonic-gate * zone_find_by_name. Both return zone_t pointers with the zone 1390Sstevel@tonic-gate * held, so zone_rele should be called when the pointer is no longer 1400Sstevel@tonic-gate * needed. Zones can also be searched by path; zone_find_by_path 1410Sstevel@tonic-gate * returns the zone with which a path name is associated (global 1420Sstevel@tonic-gate * zone if the path is not within some other zone's file system 1430Sstevel@tonic-gate * hierarchy). This currently requires iterating through each zone, 1440Sstevel@tonic-gate * so it is slower than an id or name search via a hash table. 1450Sstevel@tonic-gate * 1460Sstevel@tonic-gate * 1470Sstevel@tonic-gate * Locking: 1480Sstevel@tonic-gate * 1490Sstevel@tonic-gate * zonehash_lock: This is a top-level global lock used to protect the 1500Sstevel@tonic-gate * zone hash tables and lists. Zones cannot be created or destroyed 1510Sstevel@tonic-gate * while this lock is held. 1520Sstevel@tonic-gate * zone_status_lock: This is a global lock protecting zone state. 1530Sstevel@tonic-gate * Zones cannot change state while this lock is held. It also 1540Sstevel@tonic-gate * protects the list of kernel threads associated with a zone. 1550Sstevel@tonic-gate * zone_lock: This is a per-zone lock used to protect several fields of 1560Sstevel@tonic-gate * the zone_t (see <sys/zone.h> for details). In addition, holding 1570Sstevel@tonic-gate * this lock means that the zone cannot go away. 1580Sstevel@tonic-gate * zsd_key_lock: This is a global lock protecting the key state for ZSD. 1590Sstevel@tonic-gate * zone_deathrow_lock: This is a global lock protecting the "deathrow" 1600Sstevel@tonic-gate * list (a list of zones in the ZONE_IS_DEAD state). 1610Sstevel@tonic-gate * 1620Sstevel@tonic-gate * Ordering requirements: 1630Sstevel@tonic-gate * pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock --> 1640Sstevel@tonic-gate * zone_lock --> zsd_key_lock --> pidlock --> p_lock 1650Sstevel@tonic-gate * 1660Sstevel@tonic-gate * Blocking memory allocations are permitted while holding any of the 1670Sstevel@tonic-gate * zone locks. 1680Sstevel@tonic-gate * 1690Sstevel@tonic-gate * 1700Sstevel@tonic-gate * System Call Interface: 1710Sstevel@tonic-gate * 1720Sstevel@tonic-gate * The zone subsystem can be managed and queried from user level with 1730Sstevel@tonic-gate * the following system calls (all subcodes of the primary "zone" 1740Sstevel@tonic-gate * system call): 1750Sstevel@tonic-gate * - zone_create: creates a zone with selected attributes (name, 176789Sahrens * root path, privileges, resource controls, ZFS datasets) 1770Sstevel@tonic-gate * - zone_enter: allows the current process to enter a zone 1780Sstevel@tonic-gate * - zone_getattr: reports attributes of a zone 1790Sstevel@tonic-gate * - zone_list: lists all zones active in the system 1800Sstevel@tonic-gate * - zone_lookup: looks up zone id based on name 1810Sstevel@tonic-gate * - zone_shutdown: initiates shutdown process (see states above) 1820Sstevel@tonic-gate * - zone_destroy: completes shutdown process (see states above) 1830Sstevel@tonic-gate * 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate #include <sys/priv_impl.h> 1870Sstevel@tonic-gate #include <sys/cred.h> 1880Sstevel@tonic-gate #include <c2/audit.h> 1890Sstevel@tonic-gate #include <sys/ddi.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> 1940Sstevel@tonic-gate #include <sys/pathname.h> 1950Sstevel@tonic-gate #include <sys/proc.h> 1960Sstevel@tonic-gate #include <sys/project.h> 1971166Sdstaff #include <sys/sysevent.h> 1980Sstevel@tonic-gate #include <sys/task.h> 1990Sstevel@tonic-gate #include <sys/systm.h> 2000Sstevel@tonic-gate #include <sys/types.h> 2010Sstevel@tonic-gate #include <sys/utsname.h> 2020Sstevel@tonic-gate #include <sys/vnode.h> 2030Sstevel@tonic-gate #include <sys/vfs.h> 2040Sstevel@tonic-gate #include <sys/systeminfo.h> 2050Sstevel@tonic-gate #include <sys/policy.h> 2060Sstevel@tonic-gate #include <sys/cred_impl.h> 2070Sstevel@tonic-gate #include <sys/contract_impl.h> 2080Sstevel@tonic-gate #include <sys/contract/process_impl.h> 2090Sstevel@tonic-gate #include <sys/class.h> 2100Sstevel@tonic-gate #include <sys/pool.h> 2110Sstevel@tonic-gate #include <sys/pool_pset.h> 2120Sstevel@tonic-gate #include <sys/pset.h> 2130Sstevel@tonic-gate #include <sys/log.h> 2140Sstevel@tonic-gate #include <sys/sysmacros.h> 2150Sstevel@tonic-gate #include <sys/callb.h> 2160Sstevel@tonic-gate #include <sys/vmparam.h> 2170Sstevel@tonic-gate #include <sys/corectl.h> 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate #include <sys/door.h> 2200Sstevel@tonic-gate #include <sys/cpuvar.h> 2210Sstevel@tonic-gate #include <sys/fs/snode.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> 2270Sstevel@tonic-gate #include <sys/nvpair.h> 2280Sstevel@tonic-gate #include <sys/rctl.h> 2290Sstevel@tonic-gate #include <sys/fss.h> 2300Sstevel@tonic-gate #include <sys/zone.h> 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* 2330Sstevel@tonic-gate * cv used to signal that all references to the zone have been released. This 2340Sstevel@tonic-gate * needs to be global since there may be multiple waiters, and the first to 2350Sstevel@tonic-gate * wake up will free the zone_t, hence we cannot use zone->zone_cv. 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate static kcondvar_t zone_destroy_cv; 2380Sstevel@tonic-gate /* 2390Sstevel@tonic-gate * Lock used to serialize access to zone_cv. This could have been per-zone, 2400Sstevel@tonic-gate * but then we'd need another lock for zone_destroy_cv, and why bother? 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate static kmutex_t zone_status_lock; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* 2450Sstevel@tonic-gate * ZSD-related global variables. 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate static kmutex_t zsd_key_lock; /* protects the following two */ 2480Sstevel@tonic-gate /* 2490Sstevel@tonic-gate * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate static zone_key_t zsd_keyval = 0; 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * Global list of registered keys. We use this when a new zone is created. 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate static list_t zsd_registered_keys; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate int zone_hash_size = 256; 2580Sstevel@tonic-gate static mod_hash_t *zonehashbyname, *zonehashbyid; 2590Sstevel@tonic-gate static kmutex_t zonehash_lock; 2600Sstevel@tonic-gate static uint_t zonecount; 2610Sstevel@tonic-gate static id_space_t *zoneid_space; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 2650Sstevel@tonic-gate * kernel proper runs, and which manages all other zones. 2660Sstevel@tonic-gate * 2670Sstevel@tonic-gate * Although not declared as static, the variable "zone0" should not be used 2680Sstevel@tonic-gate * except for by code that needs to reference the global zone early on in boot, 2690Sstevel@tonic-gate * before it is fully initialized. All other consumers should use 2700Sstevel@tonic-gate * 'global_zone'. 2710Sstevel@tonic-gate */ 2720Sstevel@tonic-gate zone_t zone0; 2730Sstevel@tonic-gate zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate /* 2760Sstevel@tonic-gate * List of active zones, protected by zonehash_lock. 2770Sstevel@tonic-gate */ 2780Sstevel@tonic-gate static list_t zone_active; 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* 2810Sstevel@tonic-gate * List of destroyed zones that still have outstanding cred references. 2820Sstevel@tonic-gate * Used for debugging. Uses a separate lock to avoid lock ordering 2830Sstevel@tonic-gate * problems in zone_free. 2840Sstevel@tonic-gate */ 2850Sstevel@tonic-gate static list_t zone_deathrow; 2860Sstevel@tonic-gate static kmutex_t zone_deathrow_lock; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */ 2890Sstevel@tonic-gate uint_t maxzones = 8192; 2900Sstevel@tonic-gate 2911166Sdstaff /* Event channel to sent zone state change notifications */ 2921166Sdstaff evchan_t *zone_event_chan; 2931166Sdstaff 2941166Sdstaff /* 2951166Sdstaff * This table holds the mapping from kernel zone states to 2961166Sdstaff * states visible in the state notification API. 2971166Sdstaff * The idea is that we only expose "obvious" states and 2981166Sdstaff * do not expose states which are just implementation details. 2991166Sdstaff */ 3001166Sdstaff const char *zone_status_table[] = { 3011166Sdstaff ZONE_EVENT_UNINITIALIZED, /* uninitialized */ 3021166Sdstaff ZONE_EVENT_READY, /* ready */ 3031166Sdstaff ZONE_EVENT_READY, /* booting */ 3041166Sdstaff ZONE_EVENT_RUNNING, /* running */ 3051166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* shutting_down */ 3061166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* empty */ 3071166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* down */ 3081166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* dying */ 3091166Sdstaff ZONE_EVENT_UNINITIALIZED, /* dead */ 3101166Sdstaff }; 3111166Sdstaff 3120Sstevel@tonic-gate /* 3130Sstevel@tonic-gate * This isn't static so lint doesn't complain. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares; 3160Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps; 3170Sstevel@tonic-gate /* 3180Sstevel@tonic-gate * Synchronization primitives used to synchronize between mounts and zone 3190Sstevel@tonic-gate * creation/destruction. 3200Sstevel@tonic-gate */ 3210Sstevel@tonic-gate static int mounts_in_progress; 3220Sstevel@tonic-gate static kcondvar_t mount_cv; 3230Sstevel@tonic-gate static kmutex_t mount_lock; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate const char * const zone_initname = "/sbin/init"; 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid); 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate /* 330813Sdp * Bump this number when you alter the zone syscall interfaces; this is 331813Sdp * because we need to have support for previous API versions in libc 332813Sdp * to support patching; libc calls into the kernel to determine this number. 333813Sdp * 334813Sdp * Version 1 of the API is the version originally shipped with Solaris 10 335813Sdp * Version 2 alters the zone_create system call in order to support more 336813Sdp * arguments by moving the args into a structure; and to do better 337813Sdp * error reporting when zone_create() fails. 338813Sdp * Version 3 alters the zone_create system call in order to support the 339813Sdp * import of ZFS datasets to zones. 340813Sdp */ 341813Sdp static const int ZONE_SYSCALL_API_VERSION = 3; 342813Sdp 343813Sdp /* 3440Sstevel@tonic-gate * Certain filesystems (such as NFS and autofs) need to know which zone 3450Sstevel@tonic-gate * the mount is being placed in. Because of this, we need to be able to 3460Sstevel@tonic-gate * ensure that a zone isn't in the process of being created such that 3470Sstevel@tonic-gate * nfs_mount() thinks it is in the global zone, while by the time it 3480Sstevel@tonic-gate * gets added the list of mounted zones, it ends up on zoneA's mount 3490Sstevel@tonic-gate * list. 3500Sstevel@tonic-gate * 3510Sstevel@tonic-gate * The following functions: block_mounts()/resume_mounts() and 3520Sstevel@tonic-gate * mount_in_progress()/mount_completed() are used by zones and the VFS 3530Sstevel@tonic-gate * layer (respectively) to synchronize zone creation and new mounts. 3540Sstevel@tonic-gate * 3550Sstevel@tonic-gate * The semantics are like a reader-reader lock such that there may 3560Sstevel@tonic-gate * either be multiple mounts (or zone creations, if that weren't 3570Sstevel@tonic-gate * serialized by zonehash_lock) in progress at the same time, but not 3580Sstevel@tonic-gate * both. 3590Sstevel@tonic-gate * 3600Sstevel@tonic-gate * We use cv's so the user can ctrl-C out of the operation if it's 3610Sstevel@tonic-gate * taking too long. 3620Sstevel@tonic-gate * 3630Sstevel@tonic-gate * The semantics are such that there is unfair bias towards the 3640Sstevel@tonic-gate * "current" operation. This means that zone creations may starve if 3650Sstevel@tonic-gate * there is a rapid succession of new mounts coming in to the system, or 3660Sstevel@tonic-gate * there is a remote possibility that zones will be created at such a 3670Sstevel@tonic-gate * rate that new mounts will not be able to proceed. 3680Sstevel@tonic-gate */ 3690Sstevel@tonic-gate /* 3700Sstevel@tonic-gate * Prevent new mounts from progressing to the point of calling 3710Sstevel@tonic-gate * VFS_MOUNT(). If there are already mounts in this "region", wait for 3720Sstevel@tonic-gate * them to complete. 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate static int 3750Sstevel@tonic-gate block_mounts(void) 3760Sstevel@tonic-gate { 3770Sstevel@tonic-gate int retval = 0; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate /* 3800Sstevel@tonic-gate * Since it may block for a long time, block_mounts() shouldn't be 3810Sstevel@tonic-gate * called with zonehash_lock held. 3820Sstevel@tonic-gate */ 3830Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 3840Sstevel@tonic-gate mutex_enter(&mount_lock); 3850Sstevel@tonic-gate while (mounts_in_progress > 0) { 3860Sstevel@tonic-gate if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 3870Sstevel@tonic-gate goto signaled; 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate /* 3900Sstevel@tonic-gate * A negative value of mounts_in_progress indicates that mounts 3910Sstevel@tonic-gate * have been blocked by (-mounts_in_progress) different callers. 3920Sstevel@tonic-gate */ 3930Sstevel@tonic-gate mounts_in_progress--; 3940Sstevel@tonic-gate retval = 1; 3950Sstevel@tonic-gate signaled: 3960Sstevel@tonic-gate mutex_exit(&mount_lock); 3970Sstevel@tonic-gate return (retval); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate /* 4010Sstevel@tonic-gate * The VFS layer may progress with new mounts as far as we're concerned. 4020Sstevel@tonic-gate * Allow them to progress if we were the last obstacle. 4030Sstevel@tonic-gate */ 4040Sstevel@tonic-gate static void 4050Sstevel@tonic-gate resume_mounts(void) 4060Sstevel@tonic-gate { 4070Sstevel@tonic-gate mutex_enter(&mount_lock); 4080Sstevel@tonic-gate if (++mounts_in_progress == 0) 4090Sstevel@tonic-gate cv_broadcast(&mount_cv); 4100Sstevel@tonic-gate mutex_exit(&mount_lock); 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate /* 4140Sstevel@tonic-gate * The VFS layer is busy with a mount; zones should wait until all 4150Sstevel@tonic-gate * mounts are completed to progress. 4160Sstevel@tonic-gate */ 4170Sstevel@tonic-gate void 4180Sstevel@tonic-gate mount_in_progress(void) 4190Sstevel@tonic-gate { 4200Sstevel@tonic-gate mutex_enter(&mount_lock); 4210Sstevel@tonic-gate while (mounts_in_progress < 0) 4220Sstevel@tonic-gate cv_wait(&mount_cv, &mount_lock); 4230Sstevel@tonic-gate mounts_in_progress++; 4240Sstevel@tonic-gate mutex_exit(&mount_lock); 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate /* 4280Sstevel@tonic-gate * VFS is done with one mount; wake up any waiting block_mounts() 4290Sstevel@tonic-gate * callers if this is the last mount. 4300Sstevel@tonic-gate */ 4310Sstevel@tonic-gate void 4320Sstevel@tonic-gate mount_completed(void) 4330Sstevel@tonic-gate { 4340Sstevel@tonic-gate mutex_enter(&mount_lock); 4350Sstevel@tonic-gate if (--mounts_in_progress == 0) 4360Sstevel@tonic-gate cv_broadcast(&mount_cv); 4370Sstevel@tonic-gate mutex_exit(&mount_lock); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * ZSD routines. 4420Sstevel@tonic-gate * 4430Sstevel@tonic-gate * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 4440Sstevel@tonic-gate * defined by the pthread_key_create() and related interfaces. 4450Sstevel@tonic-gate * 4460Sstevel@tonic-gate * Kernel subsystems may register one or more data items and/or 4470Sstevel@tonic-gate * callbacks to be executed when a zone is created, shutdown, or 4480Sstevel@tonic-gate * destroyed. 4490Sstevel@tonic-gate * 4500Sstevel@tonic-gate * Unlike the thread counterpart, destructor callbacks will be executed 4510Sstevel@tonic-gate * even if the data pointer is NULL and/or there are no constructor 4520Sstevel@tonic-gate * callbacks, so it is the responsibility of such callbacks to check for 4530Sstevel@tonic-gate * NULL data values if necessary. 4540Sstevel@tonic-gate * 4550Sstevel@tonic-gate * The locking strategy and overall picture is as follows: 4560Sstevel@tonic-gate * 4570Sstevel@tonic-gate * When someone calls zone_key_create(), a template ZSD entry is added to the 4580Sstevel@tonic-gate * global list "zsd_registered_keys", protected by zsd_key_lock. The 4590Sstevel@tonic-gate * constructor callback is called immediately on all existing zones, and a 4600Sstevel@tonic-gate * copy of the ZSD entry added to the per-zone zone_zsd list (protected by 4610Sstevel@tonic-gate * zone_lock). As this operation requires the list of zones, the list of 4620Sstevel@tonic-gate * registered keys, and the per-zone list of ZSD entries to remain constant 4630Sstevel@tonic-gate * throughout the entire operation, it must grab zonehash_lock, zone_lock for 4640Sstevel@tonic-gate * all existing zones, and zsd_key_lock, in that order. Similar locking is 4650Sstevel@tonic-gate * needed when zone_key_delete() is called. It is thus sufficient to hold 4660Sstevel@tonic-gate * zsd_key_lock *or* zone_lock to prevent additions to or removals from the 4670Sstevel@tonic-gate * per-zone zone_zsd list. 4680Sstevel@tonic-gate * 4690Sstevel@tonic-gate * Note that this implementation does not make a copy of the ZSD entry if a 4700Sstevel@tonic-gate * constructor callback is not provided. A zone_getspecific() on such an 4710Sstevel@tonic-gate * uninitialized ZSD entry will return NULL. 4720Sstevel@tonic-gate * 4730Sstevel@tonic-gate * When new zones are created constructor callbacks for all registered ZSD 4740Sstevel@tonic-gate * entries will be called. 4750Sstevel@tonic-gate * 4760Sstevel@tonic-gate * The framework does not provide any locking around zone_getspecific() and 4770Sstevel@tonic-gate * zone_setspecific() apart from that needed for internal consistency, so 4780Sstevel@tonic-gate * callers interested in atomic "test-and-set" semantics will need to provide 4790Sstevel@tonic-gate * their own locking. 4800Sstevel@tonic-gate */ 4810Sstevel@tonic-gate void 4820Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 4830Sstevel@tonic-gate void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 4840Sstevel@tonic-gate { 4850Sstevel@tonic-gate struct zsd_entry *zsdp; 4860Sstevel@tonic-gate struct zsd_entry *t; 4870Sstevel@tonic-gate struct zone *zone; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP); 4900Sstevel@tonic-gate zsdp->zsd_data = NULL; 4910Sstevel@tonic-gate zsdp->zsd_create = create; 4920Sstevel@tonic-gate zsdp->zsd_shutdown = shutdown; 4930Sstevel@tonic-gate zsdp->zsd_destroy = destroy; 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* stop the world */ 4960Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 4970Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 4980Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5010Sstevel@tonic-gate *keyp = zsdp->zsd_key = ++zsd_keyval; 5020Sstevel@tonic-gate ASSERT(zsd_keyval != 0); 5030Sstevel@tonic-gate list_insert_tail(&zsd_registered_keys, zsdp); 5040Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate if (create != NULL) { 5070Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5080Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 5090Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 5100Sstevel@tonic-gate t->zsd_key = *keyp; 5110Sstevel@tonic-gate t->zsd_data = (*create)(zone->zone_id); 5120Sstevel@tonic-gate t->zsd_create = create; 5130Sstevel@tonic-gate t->zsd_shutdown = shutdown; 5140Sstevel@tonic-gate t->zsd_destroy = destroy; 5150Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5190Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5200Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 5210Sstevel@tonic-gate mutex_exit(&zonehash_lock); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* 5250Sstevel@tonic-gate * Helper function to find the zsd_entry associated with the key in the 5260Sstevel@tonic-gate * given list. 5270Sstevel@tonic-gate */ 5280Sstevel@tonic-gate static struct zsd_entry * 5290Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key) 5300Sstevel@tonic-gate { 5310Sstevel@tonic-gate struct zsd_entry *zsd; 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 5340Sstevel@tonic-gate if (zsd->zsd_key == key) { 5350Sstevel@tonic-gate /* 5360Sstevel@tonic-gate * Move to head of list to keep list in MRU order. 5370Sstevel@tonic-gate */ 5380Sstevel@tonic-gate if (zsd != list_head(l)) { 5390Sstevel@tonic-gate list_remove(l, zsd); 5400Sstevel@tonic-gate list_insert_head(l, zsd); 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate return (zsd); 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate } 5450Sstevel@tonic-gate return (NULL); 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate /* 5490Sstevel@tonic-gate * Function called when a module is being unloaded, or otherwise wishes 5500Sstevel@tonic-gate * to unregister its ZSD key and callbacks. 5510Sstevel@tonic-gate */ 5520Sstevel@tonic-gate int 5530Sstevel@tonic-gate zone_key_delete(zone_key_t key) 5540Sstevel@tonic-gate { 5550Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 5560Sstevel@tonic-gate zone_t *zone; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* Zone create/delete waits for us */ 5590Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5600Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5610Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5640Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 5650Sstevel@tonic-gate if (zsdp == NULL) 5660Sstevel@tonic-gate goto notfound; 5670Sstevel@tonic-gate list_remove(&zsd_registered_keys, zsdp); 5680Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5710Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 5720Sstevel@tonic-gate struct zsd_entry *del; 5730Sstevel@tonic-gate void *data; 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate if (!(zone->zone_flags & ZF_DESTROYED)) { 5760Sstevel@tonic-gate del = zsd_find(&zone->zone_zsd, key); 5770Sstevel@tonic-gate if (del != NULL) { 5780Sstevel@tonic-gate data = del->zsd_data; 5790Sstevel@tonic-gate ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 5800Sstevel@tonic-gate ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 5810Sstevel@tonic-gate list_remove(&zone->zone_zsd, del); 5820Sstevel@tonic-gate kmem_free(del, sizeof (*del)); 5830Sstevel@tonic-gate } else { 5840Sstevel@tonic-gate data = NULL; 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate if (zsdp->zsd_shutdown) 5870Sstevel@tonic-gate zsdp->zsd_shutdown(zone->zone_id, data); 5880Sstevel@tonic-gate if (zsdp->zsd_destroy) 5890Sstevel@tonic-gate zsdp->zsd_destroy(zone->zone_id, data); 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate mutex_exit(&zonehash_lock); 5940Sstevel@tonic-gate kmem_free(zsdp, sizeof (*zsdp)); 5950Sstevel@tonic-gate return (0); 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate notfound: 5980Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5990Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 6000Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 6010Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6020Sstevel@tonic-gate mutex_exit(&zonehash_lock); 6030Sstevel@tonic-gate return (-1); 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate /* 6070Sstevel@tonic-gate * ZSD counterpart of pthread_setspecific(). 6080Sstevel@tonic-gate */ 6090Sstevel@tonic-gate int 6100Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 6110Sstevel@tonic-gate { 6120Sstevel@tonic-gate struct zsd_entry *t; 6130Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6160Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6170Sstevel@tonic-gate if (t != NULL) { 6180Sstevel@tonic-gate /* 6190Sstevel@tonic-gate * Replace old value with new 6200Sstevel@tonic-gate */ 6210Sstevel@tonic-gate t->zsd_data = (void *)data; 6220Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6230Sstevel@tonic-gate return (0); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate /* 6260Sstevel@tonic-gate * If there was no previous value, go through the list of registered 6270Sstevel@tonic-gate * keys. 6280Sstevel@tonic-gate * 6290Sstevel@tonic-gate * We avoid grabbing zsd_key_lock until we are sure we need it; this is 6300Sstevel@tonic-gate * necessary for shutdown callbacks to be able to execute without fear 6310Sstevel@tonic-gate * of deadlock. 6320Sstevel@tonic-gate */ 6330Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 6340Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 6350Sstevel@tonic-gate if (zsdp == NULL) { /* Key was not registered */ 6360Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6370Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6380Sstevel@tonic-gate return (-1); 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * Add a zsd_entry to this zone, using the template we just retrieved 6430Sstevel@tonic-gate * to initialize the constructor and destructor(s). 6440Sstevel@tonic-gate */ 6450Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 6460Sstevel@tonic-gate t->zsd_key = key; 6470Sstevel@tonic-gate t->zsd_data = (void *)data; 6480Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 6490Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 6500Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 6510Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 6520Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6530Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6540Sstevel@tonic-gate return (0); 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate /* 6580Sstevel@tonic-gate * ZSD counterpart of pthread_getspecific(). 6590Sstevel@tonic-gate */ 6600Sstevel@tonic-gate void * 6610Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone) 6620Sstevel@tonic-gate { 6630Sstevel@tonic-gate struct zsd_entry *t; 6640Sstevel@tonic-gate void *data; 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6670Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6680Sstevel@tonic-gate data = (t == NULL ? NULL : t->zsd_data); 6690Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6700Sstevel@tonic-gate return (data); 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * Function used to initialize a zone's list of ZSD callbacks and data 6750Sstevel@tonic-gate * when the zone is being created. The callbacks are initialized from 6760Sstevel@tonic-gate * the template list (zsd_registered_keys), and the constructor 6770Sstevel@tonic-gate * callback executed (if one exists). 6780Sstevel@tonic-gate * 6790Sstevel@tonic-gate * This is called before the zone is made publicly available, hence no 6800Sstevel@tonic-gate * need to grab zone_lock. 6810Sstevel@tonic-gate * 6820Sstevel@tonic-gate * Although we grab and release zsd_key_lock, new entries cannot be 6830Sstevel@tonic-gate * added to or removed from the zsd_registered_keys list until we 6840Sstevel@tonic-gate * release zonehash_lock, so there isn't a window for a 6850Sstevel@tonic-gate * zone_key_create() to come in after we've dropped zsd_key_lock but 6860Sstevel@tonic-gate * before the zone is added to the zone list, such that the constructor 6870Sstevel@tonic-gate * callbacks aren't executed for the new zone. 6880Sstevel@tonic-gate */ 6890Sstevel@tonic-gate static void 6900Sstevel@tonic-gate zone_zsd_configure(zone_t *zone) 6910Sstevel@tonic-gate { 6920Sstevel@tonic-gate struct zsd_entry *zsdp; 6930Sstevel@tonic-gate struct zsd_entry *t; 6940Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 6970Sstevel@tonic-gate ASSERT(list_head(&zone->zone_zsd) == NULL); 6980Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 6990Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7000Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7010Sstevel@tonic-gate if (zsdp->zsd_create != NULL) { 7020Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 7030Sstevel@tonic-gate t->zsd_key = zsdp->zsd_key; 7040Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 7050Sstevel@tonic-gate t->zsd_data = (*t->zsd_create)(zoneid); 7060Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 7070Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 7080Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate /* 7170Sstevel@tonic-gate * Helper function to execute shutdown or destructor callbacks. 7180Sstevel@tonic-gate */ 7190Sstevel@tonic-gate static void 7200Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 7210Sstevel@tonic-gate { 7220Sstevel@tonic-gate struct zsd_entry *zsdp; 7230Sstevel@tonic-gate struct zsd_entry *t; 7240Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 7270Sstevel@tonic-gate ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 7280Sstevel@tonic-gate ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 7310Sstevel@tonic-gate if (ct == ZSD_DESTROY) { 7320Sstevel@tonic-gate if (zone->zone_flags & ZF_DESTROYED) { 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * Make sure destructors are only called once. 7350Sstevel@tonic-gate */ 7360Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7370Sstevel@tonic-gate return; 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate zone->zone_flags |= ZF_DESTROYED; 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate /* 7440Sstevel@tonic-gate * Both zsd_key_lock and zone_lock need to be held in order to add or 7450Sstevel@tonic-gate * remove a ZSD key, (either globally as part of 7460Sstevel@tonic-gate * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is 7470Sstevel@tonic-gate * possible through zone_setspecific()), so it's sufficient to hold 7480Sstevel@tonic-gate * zsd_key_lock here. 7490Sstevel@tonic-gate * 7500Sstevel@tonic-gate * This is a good thing, since we don't want to recursively try to grab 7510Sstevel@tonic-gate * zone_lock if a callback attempts to do something like a crfree() or 7520Sstevel@tonic-gate * zone_rele(). 7530Sstevel@tonic-gate */ 7540Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7550Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7560Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7570Sstevel@tonic-gate zone_key_t key = zsdp->zsd_key; 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate /* Skip if no callbacks registered */ 7600Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL) 7610Sstevel@tonic-gate continue; 7620Sstevel@tonic-gate if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL) 7630Sstevel@tonic-gate continue; 7640Sstevel@tonic-gate /* 7650Sstevel@tonic-gate * Call the callback with the zone-specific data if we can find 7660Sstevel@tonic-gate * any, otherwise with NULL. 7670Sstevel@tonic-gate */ 7680Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 7690Sstevel@tonic-gate if (t != NULL) { 7700Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 7710Sstevel@tonic-gate t->zsd_shutdown(zoneid, t->zsd_data); 7720Sstevel@tonic-gate } else { 7730Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 7740Sstevel@tonic-gate t->zsd_destroy(zoneid, t->zsd_data); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate } else { 7770Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 7780Sstevel@tonic-gate zsdp->zsd_shutdown(zoneid, NULL); 7790Sstevel@tonic-gate } else { 7800Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 7810Sstevel@tonic-gate zsdp->zsd_destroy(zoneid, NULL); 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate /* 7890Sstevel@tonic-gate * Called when the zone is going away; free ZSD-related memory, and 7900Sstevel@tonic-gate * destroy the zone_zsd list. 7910Sstevel@tonic-gate */ 7920Sstevel@tonic-gate static void 7930Sstevel@tonic-gate zone_free_zsd(zone_t *zone) 7940Sstevel@tonic-gate { 7950Sstevel@tonic-gate struct zsd_entry *t, *next; 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate /* 7980Sstevel@tonic-gate * Free all the zsd_entry's we had on this zone. 7990Sstevel@tonic-gate */ 8000Sstevel@tonic-gate for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 8010Sstevel@tonic-gate next = list_next(&zone->zone_zsd, t); 8020Sstevel@tonic-gate list_remove(&zone->zone_zsd, t); 8030Sstevel@tonic-gate kmem_free(t, sizeof (*t)); 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate list_destroy(&zone->zone_zsd); 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate /* 809789Sahrens * Frees memory associated with the zone dataset list. 810789Sahrens */ 811789Sahrens static void 812789Sahrens zone_free_datasets(zone_t *zone) 813789Sahrens { 814789Sahrens zone_dataset_t *t, *next; 815789Sahrens 816789Sahrens for (t = list_head(&zone->zone_datasets); t != NULL; t = next) { 817789Sahrens next = list_next(&zone->zone_datasets, t); 818789Sahrens list_remove(&zone->zone_datasets, t); 819789Sahrens kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1); 820789Sahrens kmem_free(t, sizeof (*t)); 821789Sahrens } 822789Sahrens list_destroy(&zone->zone_datasets); 823789Sahrens } 824789Sahrens 825789Sahrens /* 8260Sstevel@tonic-gate * zone.cpu-shares resource control support. 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate /*ARGSUSED*/ 8290Sstevel@tonic-gate static rctl_qty_t 8300Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 8310Sstevel@tonic-gate { 8320Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8330Sstevel@tonic-gate return (p->p_zone->zone_shares); 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate /*ARGSUSED*/ 8370Sstevel@tonic-gate static int 8380Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 8390Sstevel@tonic-gate rctl_qty_t nv) 8400Sstevel@tonic-gate { 8410Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8420Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 8430Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 8440Sstevel@tonic-gate return (0); 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate e->rcep_p.zone->zone_shares = nv; 8470Sstevel@tonic-gate return (0); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = { 8510Sstevel@tonic-gate rcop_no_action, 8520Sstevel@tonic-gate zone_cpu_shares_usage, 8530Sstevel@tonic-gate zone_cpu_shares_set, 8540Sstevel@tonic-gate rcop_no_test 8550Sstevel@tonic-gate }; 8560Sstevel@tonic-gate 8570Sstevel@tonic-gate /*ARGSUSED*/ 8580Sstevel@tonic-gate static rctl_qty_t 8590Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p) 8600Sstevel@tonic-gate { 8610Sstevel@tonic-gate rctl_qty_t nlwps; 8620Sstevel@tonic-gate zone_t *zone = p->p_zone; 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 8670Sstevel@tonic-gate nlwps = zone->zone_nlwps; 8680Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate return (nlwps); 8710Sstevel@tonic-gate } 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate /*ARGSUSED*/ 8740Sstevel@tonic-gate static int 8750Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 8760Sstevel@tonic-gate rctl_qty_t incr, uint_t flags) 8770Sstevel@tonic-gate { 8780Sstevel@tonic-gate rctl_qty_t nlwps; 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8810Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 8820Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 8830Sstevel@tonic-gate return (0); 8840Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 8850Sstevel@tonic-gate nlwps = e->rcep_p.zone->zone_nlwps; 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate if (nlwps + incr > rcntl->rcv_value) 8880Sstevel@tonic-gate return (1); 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate return (0); 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate /*ARGSUSED*/ 8940Sstevel@tonic-gate static int 8950Sstevel@tonic-gate zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) { 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8980Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 8990Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 9000Sstevel@tonic-gate return (0); 9010Sstevel@tonic-gate e->rcep_p.zone->zone_nlwps_ctl = nv; 9020Sstevel@tonic-gate return (0); 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = { 9060Sstevel@tonic-gate rcop_no_action, 9070Sstevel@tonic-gate zone_lwps_usage, 9080Sstevel@tonic-gate zone_lwps_set, 9090Sstevel@tonic-gate zone_lwps_test, 9100Sstevel@tonic-gate }; 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate /* 9130Sstevel@tonic-gate * Helper function to brand the zone with a unique ID. 9140Sstevel@tonic-gate */ 9150Sstevel@tonic-gate static void 9160Sstevel@tonic-gate zone_uniqid(zone_t *zone) 9170Sstevel@tonic-gate { 9180Sstevel@tonic-gate static uint64_t uniqid = 0; 9190Sstevel@tonic-gate 9200Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 9210Sstevel@tonic-gate zone->zone_uniqid = uniqid++; 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate /* 9250Sstevel@tonic-gate * Returns a held pointer to the "kcred" for the specified zone. 9260Sstevel@tonic-gate */ 9270Sstevel@tonic-gate struct cred * 9280Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid) 9290Sstevel@tonic-gate { 9300Sstevel@tonic-gate zone_t *zone; 9310Sstevel@tonic-gate cred_t *cr; 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) 9340Sstevel@tonic-gate return (NULL); 9350Sstevel@tonic-gate cr = zone->zone_kcred; 9360Sstevel@tonic-gate crhold(cr); 9370Sstevel@tonic-gate zone_rele(zone); 9380Sstevel@tonic-gate return (cr); 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate /* 9420Sstevel@tonic-gate * Called very early on in boot to initialize the ZSD list so that 9430Sstevel@tonic-gate * zone_key_create() can be called before zone_init(). It also initializes 9440Sstevel@tonic-gate * portions of zone0 which may be used before zone_init() is called. The 9450Sstevel@tonic-gate * variable "global_zone" will be set when zone0 is fully initialized by 9460Sstevel@tonic-gate * zone_init(). 9470Sstevel@tonic-gate */ 9480Sstevel@tonic-gate void 9490Sstevel@tonic-gate zone_zsd_init(void) 9500Sstevel@tonic-gate { 9510Sstevel@tonic-gate mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 9520Sstevel@tonic-gate mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 9530Sstevel@tonic-gate list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 9540Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 9550Sstevel@tonic-gate list_create(&zone_active, sizeof (zone_t), 9560Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 9570Sstevel@tonic-gate list_create(&zone_deathrow, sizeof (zone_t), 9580Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 9610Sstevel@tonic-gate mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 9620Sstevel@tonic-gate zone0.zone_shares = 1; 9630Sstevel@tonic-gate zone0.zone_nlwps_ctl = INT_MAX; 9640Sstevel@tonic-gate zone0.zone_name = GLOBAL_ZONENAME; 9650Sstevel@tonic-gate zone0.zone_nodename = utsname.nodename; 9660Sstevel@tonic-gate zone0.zone_domain = srpc_domain; 9670Sstevel@tonic-gate zone0.zone_ref = 1; 9680Sstevel@tonic-gate zone0.zone_id = GLOBAL_ZONEID; 9690Sstevel@tonic-gate zone0.zone_status = ZONE_IS_RUNNING; 9700Sstevel@tonic-gate zone0.zone_rootpath = "/"; 9710Sstevel@tonic-gate zone0.zone_rootpathlen = 2; 9720Sstevel@tonic-gate zone0.zone_psetid = ZONE_PS_INVAL; 9730Sstevel@tonic-gate zone0.zone_ncpus = 0; 9740Sstevel@tonic-gate zone0.zone_ncpus_online = 0; 9750Sstevel@tonic-gate zone0.zone_proc_initpid = 1; 9760Sstevel@tonic-gate list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 9770Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 9780Sstevel@tonic-gate list_insert_head(&zone_active, &zone0); 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate /* 9810Sstevel@tonic-gate * The root filesystem is not mounted yet, so zone_rootvp cannot be set 9820Sstevel@tonic-gate * to anything meaningful. It is assigned to be 'rootdir' in 9830Sstevel@tonic-gate * vfs_mountroot(). 9840Sstevel@tonic-gate */ 9850Sstevel@tonic-gate zone0.zone_rootvp = NULL; 9860Sstevel@tonic-gate zone0.zone_vfslist = NULL; 9870Sstevel@tonic-gate zone0.zone_bootargs = NULL; 9880Sstevel@tonic-gate zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 9890Sstevel@tonic-gate /* 9900Sstevel@tonic-gate * The global zone has all privileges 9910Sstevel@tonic-gate */ 9920Sstevel@tonic-gate priv_fillset(zone0.zone_privset); 9930Sstevel@tonic-gate /* 9940Sstevel@tonic-gate * Add p0 to the global zone 9950Sstevel@tonic-gate */ 9960Sstevel@tonic-gate zone0.zone_zsched = &p0; 9970Sstevel@tonic-gate p0.p_zone = &zone0; 9980Sstevel@tonic-gate } 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate /* 10010Sstevel@tonic-gate * Called by main() to initialize the zones framework. 10020Sstevel@tonic-gate */ 10030Sstevel@tonic-gate void 10040Sstevel@tonic-gate zone_init(void) 10050Sstevel@tonic-gate { 10060Sstevel@tonic-gate rctl_dict_entry_t *rde; 10070Sstevel@tonic-gate rctl_val_t *dval; 10080Sstevel@tonic-gate rctl_set_t *set; 10090Sstevel@tonic-gate rctl_alloc_gp_t *gp; 10100Sstevel@tonic-gate rctl_entity_p_t e; 10111166Sdstaff int res; 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate ASSERT(curproc == &p0); 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate /* 10160Sstevel@tonic-gate * Create ID space for zone IDs. ID 0 is reserved for the 10170Sstevel@tonic-gate * global zone. 10180Sstevel@tonic-gate */ 10190Sstevel@tonic-gate zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate /* 10220Sstevel@tonic-gate * Initialize generic zone resource controls, if any. 10230Sstevel@tonic-gate */ 10240Sstevel@tonic-gate rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 10250Sstevel@tonic-gate RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 10260Sstevel@tonic-gate RCTL_GLOBAL_NOBASIC | 10270Sstevel@tonic-gate RCTL_GLOBAL_COUNT, FSS_MAXSHARES, FSS_MAXSHARES, 10280Sstevel@tonic-gate &zone_cpu_shares_ops); 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 10310Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 10320Sstevel@tonic-gate INT_MAX, INT_MAX, &zone_lwps_ops); 10330Sstevel@tonic-gate /* 10340Sstevel@tonic-gate * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 10350Sstevel@tonic-gate * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 10360Sstevel@tonic-gate */ 10370Sstevel@tonic-gate dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 10380Sstevel@tonic-gate bzero(dval, sizeof (rctl_val_t)); 10390Sstevel@tonic-gate dval->rcv_value = 1; 10400Sstevel@tonic-gate dval->rcv_privilege = RCPRIV_PRIVILEGED; 10410Sstevel@tonic-gate dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 10420Sstevel@tonic-gate dval->rcv_action_recip_pid = -1; 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate rde = rctl_dict_lookup("zone.cpu-shares"); 10450Sstevel@tonic-gate (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate /* 10480Sstevel@tonic-gate * Initialize the ``global zone''. 10490Sstevel@tonic-gate */ 10500Sstevel@tonic-gate set = rctl_set_create(); 10510Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 10520Sstevel@tonic-gate mutex_enter(&p0.p_lock); 10530Sstevel@tonic-gate e.rcep_p.zone = &zone0; 10540Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 10550Sstevel@tonic-gate zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 10560Sstevel@tonic-gate gp); 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate zone0.zone_nlwps = p0.p_lwpcnt; 10590Sstevel@tonic-gate zone0.zone_ntasks = 1; 10600Sstevel@tonic-gate mutex_exit(&p0.p_lock); 10610Sstevel@tonic-gate rctl_prealloc_destroy(gp); 10620Sstevel@tonic-gate /* 10630Sstevel@tonic-gate * pool_default hasn't been initialized yet, so we let pool_init() take 10640Sstevel@tonic-gate * care of making the global zone is in the default pool. 10650Sstevel@tonic-gate */ 10660Sstevel@tonic-gate mutex_enter(&zonehash_lock); 10670Sstevel@tonic-gate zone_uniqid(&zone0); 10680Sstevel@tonic-gate ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 10690Sstevel@tonic-gate mutex_exit(&zonehash_lock); 10700Sstevel@tonic-gate zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 10710Sstevel@tonic-gate mod_hash_null_valdtor); 10720Sstevel@tonic-gate zonehashbyname = mod_hash_create_strhash("zone_by_name", 10730Sstevel@tonic-gate zone_hash_size, mod_hash_null_valdtor); 10740Sstevel@tonic-gate zonecount = 1; 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 10770Sstevel@tonic-gate (mod_hash_val_t)&zone0); 10780Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 10790Sstevel@tonic-gate (mod_hash_val_t)&zone0); 10800Sstevel@tonic-gate /* 10810Sstevel@tonic-gate * We avoid setting zone_kcred until now, since kcred is initialized 10820Sstevel@tonic-gate * sometime after zone_zsd_init() and before zone_init(). 10830Sstevel@tonic-gate */ 10840Sstevel@tonic-gate zone0.zone_kcred = kcred; 10850Sstevel@tonic-gate /* 10860Sstevel@tonic-gate * The global zone is fully initialized (except for zone_rootvp which 10870Sstevel@tonic-gate * will be set when the root filesystem is mounted). 10880Sstevel@tonic-gate */ 10890Sstevel@tonic-gate global_zone = &zone0; 10901166Sdstaff 10911166Sdstaff /* 10921166Sdstaff * Setup an event channel to send zone status change notifications on 10931166Sdstaff */ 10941166Sdstaff res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan, 10951166Sdstaff EVCH_CREAT); 10961166Sdstaff 10971166Sdstaff if (res) 10981166Sdstaff panic("Sysevent_evc_bind failed during zone setup.\n"); 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate static void 11020Sstevel@tonic-gate zone_free(zone_t *zone) 11030Sstevel@tonic-gate { 11040Sstevel@tonic-gate ASSERT(zone != global_zone); 11050Sstevel@tonic-gate ASSERT(zone->zone_ntasks == 0); 11060Sstevel@tonic-gate ASSERT(zone->zone_nlwps == 0); 11070Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 0); 11080Sstevel@tonic-gate ASSERT(zone->zone_kcred == NULL); 11090Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 11100Sstevel@tonic-gate zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate /* remove from deathrow list */ 11130Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_DEAD) { 11140Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 11150Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 11160Sstevel@tonic-gate list_remove(&zone_deathrow, zone); 11170Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 11180Sstevel@tonic-gate } 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate zone_free_zsd(zone); 1121789Sahrens zone_free_datasets(zone); 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate if (zone->zone_rootvp != NULL) 11240Sstevel@tonic-gate VN_RELE(zone->zone_rootvp); 11250Sstevel@tonic-gate if (zone->zone_rootpath) 11260Sstevel@tonic-gate kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 11270Sstevel@tonic-gate if (zone->zone_name != NULL) 11280Sstevel@tonic-gate kmem_free(zone->zone_name, ZONENAME_MAX); 11290Sstevel@tonic-gate if (zone->zone_nodename != NULL) 11300Sstevel@tonic-gate kmem_free(zone->zone_nodename, _SYS_NMLN); 11310Sstevel@tonic-gate if (zone->zone_domain != NULL) 11320Sstevel@tonic-gate kmem_free(zone->zone_domain, _SYS_NMLN); 11330Sstevel@tonic-gate if (zone->zone_privset != NULL) 11340Sstevel@tonic-gate kmem_free(zone->zone_privset, sizeof (priv_set_t)); 11350Sstevel@tonic-gate if (zone->zone_rctls != NULL) 11360Sstevel@tonic-gate rctl_set_free(zone->zone_rctls); 11370Sstevel@tonic-gate if (zone->zone_bootargs != NULL) 11380Sstevel@tonic-gate kmem_free(zone->zone_bootargs, ZONEBOOTARGS_MAX); 11390Sstevel@tonic-gate id_free(zoneid_space, zone->zone_id); 11400Sstevel@tonic-gate mutex_destroy(&zone->zone_lock); 11410Sstevel@tonic-gate cv_destroy(&zone->zone_cv); 11420Sstevel@tonic-gate kmem_free(zone, sizeof (zone_t)); 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate /* 11460Sstevel@tonic-gate * See block comment at the top of this file for information about zone 11470Sstevel@tonic-gate * status values. 11480Sstevel@tonic-gate */ 11490Sstevel@tonic-gate /* 11500Sstevel@tonic-gate * Convenience function for setting zone status. 11510Sstevel@tonic-gate */ 11520Sstevel@tonic-gate static void 11530Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status) 11540Sstevel@tonic-gate { 11551166Sdstaff 11561166Sdstaff nvlist_t *nvl = NULL; 11570Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zone_status_lock)); 11580Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 11590Sstevel@tonic-gate status >= zone_status_get(zone)); 11601166Sdstaff 11611166Sdstaff if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) || 11621166Sdstaff nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) || 11631166Sdstaff nvlist_add_string(nvl, ZONE_CB_NEWSTATE, 11641166Sdstaff zone_status_table[status]) || 11651166Sdstaff nvlist_add_string(nvl, ZONE_CB_OLDSTATE, 11661166Sdstaff zone_status_table[zone->zone_status]) || 11671166Sdstaff nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) || 11681166Sdstaff nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) || 11691166Sdstaff sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS, 11701166Sdstaff ZONE_EVENT_STATUS_SUBCLASS, 11711166Sdstaff "sun.com", "kernel", nvl, EVCH_SLEEP)) { 11721166Sdstaff #ifdef DEBUG 11731166Sdstaff (void) printf( 11741166Sdstaff "Failed to allocate and send zone state change event.\n"); 11751166Sdstaff #endif 11761166Sdstaff } 11771166Sdstaff nvlist_free(nvl); 11781166Sdstaff 11790Sstevel@tonic-gate zone->zone_status = status; 11801166Sdstaff 11810Sstevel@tonic-gate cv_broadcast(&zone->zone_cv); 11820Sstevel@tonic-gate } 11830Sstevel@tonic-gate 11840Sstevel@tonic-gate /* 11850Sstevel@tonic-gate * Public function to retrieve the zone status. The zone status may 11860Sstevel@tonic-gate * change after it is retrieved. 11870Sstevel@tonic-gate */ 11880Sstevel@tonic-gate zone_status_t 11890Sstevel@tonic-gate zone_status_get(zone_t *zone) 11900Sstevel@tonic-gate { 11910Sstevel@tonic-gate return (zone->zone_status); 11920Sstevel@tonic-gate } 11930Sstevel@tonic-gate 11940Sstevel@tonic-gate static int 11950Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 11960Sstevel@tonic-gate { 11970Sstevel@tonic-gate char *bootargs = kmem_zalloc(ZONEBOOTARGS_MAX, KM_SLEEP); 11980Sstevel@tonic-gate size_t len; 11990Sstevel@tonic-gate int err; 12000Sstevel@tonic-gate 12010Sstevel@tonic-gate err = copyinstr(zone_bootargs, bootargs, ZONEBOOTARGS_MAX - 1, &len); 12020Sstevel@tonic-gate if (err != 0) { 12030Sstevel@tonic-gate kmem_free(bootargs, ZONEBOOTARGS_MAX); 12040Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 12050Sstevel@tonic-gate } 12060Sstevel@tonic-gate bootargs[len] = '\0'; 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate ASSERT(zone->zone_bootargs == NULL); 12090Sstevel@tonic-gate zone->zone_bootargs = bootargs; 12100Sstevel@tonic-gate return (0); 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate 12130Sstevel@tonic-gate /* 12140Sstevel@tonic-gate * Block indefinitely waiting for (zone_status >= status) 12150Sstevel@tonic-gate */ 12160Sstevel@tonic-gate void 12170Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status) 12180Sstevel@tonic-gate { 12190Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 12200Sstevel@tonic-gate 12210Sstevel@tonic-gate mutex_enter(&zone_status_lock); 12220Sstevel@tonic-gate while (zone->zone_status < status) { 12230Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate mutex_exit(&zone_status_lock); 12260Sstevel@tonic-gate } 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate /* 12290Sstevel@tonic-gate * Private CPR-safe version of zone_status_wait(). 12300Sstevel@tonic-gate */ 12310Sstevel@tonic-gate static void 12320Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 12330Sstevel@tonic-gate { 12340Sstevel@tonic-gate callb_cpr_t cprinfo; 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 12390Sstevel@tonic-gate str); 12400Sstevel@tonic-gate mutex_enter(&zone_status_lock); 12410Sstevel@tonic-gate while (zone->zone_status < status) { 12420Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 12430Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 12440Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 12450Sstevel@tonic-gate } 12460Sstevel@tonic-gate /* 12470Sstevel@tonic-gate * zone_status_lock is implicitly released by the following. 12480Sstevel@tonic-gate */ 12490Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate /* 12530Sstevel@tonic-gate * Block until zone enters requested state or signal is received. Return (0) 12540Sstevel@tonic-gate * if signaled, non-zero otherwise. 12550Sstevel@tonic-gate */ 12560Sstevel@tonic-gate int 12570Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status) 12580Sstevel@tonic-gate { 12590Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate mutex_enter(&zone_status_lock); 12620Sstevel@tonic-gate while (zone->zone_status < status) { 12630Sstevel@tonic-gate if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 12640Sstevel@tonic-gate mutex_exit(&zone_status_lock); 12650Sstevel@tonic-gate return (0); 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate } 12680Sstevel@tonic-gate mutex_exit(&zone_status_lock); 12690Sstevel@tonic-gate return (1); 12700Sstevel@tonic-gate } 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate /* 12730Sstevel@tonic-gate * Block until the zone enters the requested state or the timeout expires, 12740Sstevel@tonic-gate * whichever happens first. Return (-1) if operation timed out, time remaining 12750Sstevel@tonic-gate * otherwise. 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate clock_t 12780Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 12790Sstevel@tonic-gate { 12800Sstevel@tonic-gate clock_t timeleft = 0; 12810Sstevel@tonic-gate 12820Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 12830Sstevel@tonic-gate 12840Sstevel@tonic-gate mutex_enter(&zone_status_lock); 12850Sstevel@tonic-gate while (zone->zone_status < status && timeleft != -1) { 12860Sstevel@tonic-gate timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate mutex_exit(&zone_status_lock); 12890Sstevel@tonic-gate return (timeleft); 12900Sstevel@tonic-gate } 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate /* 12930Sstevel@tonic-gate * Block until the zone enters the requested state, the current process is 12940Sstevel@tonic-gate * signaled, or the timeout expires, whichever happens first. Return (-1) if 12950Sstevel@tonic-gate * operation timed out, 0 if signaled, time remaining otherwise. 12960Sstevel@tonic-gate */ 12970Sstevel@tonic-gate clock_t 12980Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 12990Sstevel@tonic-gate { 13000Sstevel@tonic-gate clock_t timeleft = tim - lbolt; 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate mutex_enter(&zone_status_lock); 13050Sstevel@tonic-gate while (zone->zone_status < status) { 13060Sstevel@tonic-gate timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 13070Sstevel@tonic-gate tim); 13080Sstevel@tonic-gate if (timeleft <= 0) 13090Sstevel@tonic-gate break; 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate mutex_exit(&zone_status_lock); 13120Sstevel@tonic-gate return (timeleft); 13130Sstevel@tonic-gate } 13140Sstevel@tonic-gate 13150Sstevel@tonic-gate /* 13160Sstevel@tonic-gate * Zones have two reference counts: one for references from credential 13170Sstevel@tonic-gate * structures (zone_cred_ref), and one (zone_ref) for everything else. 13180Sstevel@tonic-gate * This is so we can allow a zone to be rebooted while there are still 13190Sstevel@tonic-gate * outstanding cred references, since certain drivers cache dblks (which 13200Sstevel@tonic-gate * implicitly results in cached creds). We wait for zone_ref to drop to 13210Sstevel@tonic-gate * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 13220Sstevel@tonic-gate * later freed when the zone_cred_ref drops to 0, though nothing other 13230Sstevel@tonic-gate * than the zone id and privilege set should be accessed once the zone 13240Sstevel@tonic-gate * is "dead". 13250Sstevel@tonic-gate * 13260Sstevel@tonic-gate * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 13270Sstevel@tonic-gate * to force halt/reboot to block waiting for the zone_cred_ref to drop 13280Sstevel@tonic-gate * to 0. This can be useful to flush out other sources of cached creds 13290Sstevel@tonic-gate * that may be less innocuous than the driver case. 13300Sstevel@tonic-gate */ 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate int zone_wait_for_cred = 0; 13330Sstevel@tonic-gate 13340Sstevel@tonic-gate static void 13350Sstevel@tonic-gate zone_hold_locked(zone_t *z) 13360Sstevel@tonic-gate { 13370Sstevel@tonic-gate ASSERT(MUTEX_HELD(&z->zone_lock)); 13380Sstevel@tonic-gate z->zone_ref++; 13390Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 13400Sstevel@tonic-gate } 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate void 13430Sstevel@tonic-gate zone_hold(zone_t *z) 13440Sstevel@tonic-gate { 13450Sstevel@tonic-gate mutex_enter(&z->zone_lock); 13460Sstevel@tonic-gate zone_hold_locked(z); 13470Sstevel@tonic-gate mutex_exit(&z->zone_lock); 13480Sstevel@tonic-gate } 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate /* 13510Sstevel@tonic-gate * If the non-cred ref count drops to 1 and either the cred ref count 13520Sstevel@tonic-gate * is 0 or we aren't waiting for cred references, the zone is ready to 13530Sstevel@tonic-gate * be destroyed. 13540Sstevel@tonic-gate */ 13550Sstevel@tonic-gate #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 13560Sstevel@tonic-gate (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate void 13590Sstevel@tonic-gate zone_rele(zone_t *z) 13600Sstevel@tonic-gate { 13610Sstevel@tonic-gate boolean_t wakeup; 13620Sstevel@tonic-gate 13630Sstevel@tonic-gate mutex_enter(&z->zone_lock); 13640Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 13650Sstevel@tonic-gate z->zone_ref--; 13660Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 13670Sstevel@tonic-gate /* no more refs, free the structure */ 13680Sstevel@tonic-gate mutex_exit(&z->zone_lock); 13690Sstevel@tonic-gate zone_free(z); 13700Sstevel@tonic-gate return; 13710Sstevel@tonic-gate } 13720Sstevel@tonic-gate /* signal zone_destroy so the zone can finish halting */ 13730Sstevel@tonic-gate wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 13740Sstevel@tonic-gate mutex_exit(&z->zone_lock); 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate if (wakeup) { 13770Sstevel@tonic-gate /* 13780Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 13790Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 13800Sstevel@tonic-gate */ 13810Sstevel@tonic-gate mutex_enter(&zonehash_lock); 13820Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 13830Sstevel@tonic-gate mutex_exit(&zonehash_lock); 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate void 13880Sstevel@tonic-gate zone_cred_hold(zone_t *z) 13890Sstevel@tonic-gate { 13900Sstevel@tonic-gate mutex_enter(&z->zone_lock); 13910Sstevel@tonic-gate z->zone_cred_ref++; 13920Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 13930Sstevel@tonic-gate mutex_exit(&z->zone_lock); 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate void 13970Sstevel@tonic-gate zone_cred_rele(zone_t *z) 13980Sstevel@tonic-gate { 13990Sstevel@tonic-gate boolean_t wakeup; 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate mutex_enter(&z->zone_lock); 14020Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 14030Sstevel@tonic-gate z->zone_cred_ref--; 14040Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 14050Sstevel@tonic-gate /* no more refs, free the structure */ 14060Sstevel@tonic-gate mutex_exit(&z->zone_lock); 14070Sstevel@tonic-gate zone_free(z); 14080Sstevel@tonic-gate return; 14090Sstevel@tonic-gate } 14100Sstevel@tonic-gate /* 14110Sstevel@tonic-gate * If zone_destroy is waiting for the cred references to drain 14120Sstevel@tonic-gate * out, and they have, signal it. 14130Sstevel@tonic-gate */ 14140Sstevel@tonic-gate wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 14150Sstevel@tonic-gate zone_status_get(z) >= ZONE_IS_DEAD); 14160Sstevel@tonic-gate mutex_exit(&z->zone_lock); 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate if (wakeup) { 14190Sstevel@tonic-gate /* 14200Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 14210Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 14220Sstevel@tonic-gate */ 14230Sstevel@tonic-gate mutex_enter(&zonehash_lock); 14240Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 14250Sstevel@tonic-gate mutex_exit(&zonehash_lock); 14260Sstevel@tonic-gate } 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate void 14300Sstevel@tonic-gate zone_task_hold(zone_t *z) 14310Sstevel@tonic-gate { 14320Sstevel@tonic-gate mutex_enter(&z->zone_lock); 14330Sstevel@tonic-gate z->zone_ntasks++; 14340Sstevel@tonic-gate ASSERT(z->zone_ntasks != 0); 14350Sstevel@tonic-gate mutex_exit(&z->zone_lock); 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate void 14390Sstevel@tonic-gate zone_task_rele(zone_t *zone) 14400Sstevel@tonic-gate { 14410Sstevel@tonic-gate uint_t refcnt; 14420Sstevel@tonic-gate 14430Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 14440Sstevel@tonic-gate ASSERT(zone->zone_ntasks != 0); 14450Sstevel@tonic-gate refcnt = --zone->zone_ntasks; 14460Sstevel@tonic-gate if (refcnt > 1) { /* Common case */ 14470Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 14480Sstevel@tonic-gate return; 14490Sstevel@tonic-gate } 14500Sstevel@tonic-gate zone_hold_locked(zone); /* so we can use the zone_t later */ 14510Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 14520Sstevel@tonic-gate if (refcnt == 1) { 14530Sstevel@tonic-gate /* 14540Sstevel@tonic-gate * See if the zone is shutting down. 14550Sstevel@tonic-gate */ 14560Sstevel@tonic-gate mutex_enter(&zone_status_lock); 14570Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 14580Sstevel@tonic-gate goto out; 14590Sstevel@tonic-gate } 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate /* 14620Sstevel@tonic-gate * Make sure the ntasks didn't change since we 14630Sstevel@tonic-gate * dropped zone_lock. 14640Sstevel@tonic-gate */ 14650Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 14660Sstevel@tonic-gate if (refcnt != zone->zone_ntasks) { 14670Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 14680Sstevel@tonic-gate goto out; 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate /* 14730Sstevel@tonic-gate * No more user processes in the zone. The zone is empty. 14740Sstevel@tonic-gate */ 14750Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 14760Sstevel@tonic-gate goto out; 14770Sstevel@tonic-gate } 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate ASSERT(refcnt == 0); 14800Sstevel@tonic-gate /* 14810Sstevel@tonic-gate * zsched has exited; the zone is dead. 14820Sstevel@tonic-gate */ 14830Sstevel@tonic-gate zone->zone_zsched = NULL; /* paranoia */ 14840Sstevel@tonic-gate mutex_enter(&zone_status_lock); 14850Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DEAD); 14860Sstevel@tonic-gate out: 14870Sstevel@tonic-gate mutex_exit(&zone_status_lock); 14880Sstevel@tonic-gate zone_rele(zone); 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate zoneid_t 14920Sstevel@tonic-gate getzoneid(void) 14930Sstevel@tonic-gate { 14940Sstevel@tonic-gate return (curproc->p_zone->zone_id); 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate /* 14980Sstevel@tonic-gate * Internal versions of zone_find_by_*(). These don't zone_hold() or 14990Sstevel@tonic-gate * check the validity of a zone's state. 15000Sstevel@tonic-gate */ 15010Sstevel@tonic-gate static zone_t * 15020Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid) 15030Sstevel@tonic-gate { 15040Sstevel@tonic-gate mod_hash_val_t hv; 15050Sstevel@tonic-gate zone_t *zone = NULL; 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate if (mod_hash_find(zonehashbyid, 15100Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 15110Sstevel@tonic-gate zone = (zone_t *)hv; 15120Sstevel@tonic-gate return (zone); 15130Sstevel@tonic-gate } 15140Sstevel@tonic-gate 15150Sstevel@tonic-gate static zone_t * 15160Sstevel@tonic-gate zone_find_all_by_name(char *name) 15170Sstevel@tonic-gate { 15180Sstevel@tonic-gate mod_hash_val_t hv; 15190Sstevel@tonic-gate zone_t *zone = NULL; 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 15220Sstevel@tonic-gate 15230Sstevel@tonic-gate if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 15240Sstevel@tonic-gate zone = (zone_t *)hv; 15250Sstevel@tonic-gate return (zone); 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate /* 15290Sstevel@tonic-gate * Public interface for looking up a zone by zoneid. Only returns the zone if 15300Sstevel@tonic-gate * it is fully initialized, and has not yet begun the zone_destroy() sequence. 15310Sstevel@tonic-gate * Caller must call zone_rele() once it is done with the zone. 15320Sstevel@tonic-gate * 15330Sstevel@tonic-gate * The zone may begin the zone_destroy() sequence immediately after this 15340Sstevel@tonic-gate * function returns, but may be safely used until zone_rele() is called. 15350Sstevel@tonic-gate */ 15360Sstevel@tonic-gate zone_t * 15370Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid) 15380Sstevel@tonic-gate { 15390Sstevel@tonic-gate zone_t *zone; 15400Sstevel@tonic-gate zone_status_t status; 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate mutex_enter(&zonehash_lock); 15430Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 15440Sstevel@tonic-gate mutex_exit(&zonehash_lock); 15450Sstevel@tonic-gate return (NULL); 15460Sstevel@tonic-gate } 15470Sstevel@tonic-gate status = zone_status_get(zone); 15480Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 15490Sstevel@tonic-gate /* 15500Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 15510Sstevel@tonic-gate */ 15520Sstevel@tonic-gate mutex_exit(&zonehash_lock); 15530Sstevel@tonic-gate return (NULL); 15540Sstevel@tonic-gate } 15550Sstevel@tonic-gate zone_hold(zone); 15560Sstevel@tonic-gate mutex_exit(&zonehash_lock); 15570Sstevel@tonic-gate return (zone); 15580Sstevel@tonic-gate } 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate /* 15610Sstevel@tonic-gate * Similar to zone_find_by_id, but using zone name as the key. 15620Sstevel@tonic-gate */ 15630Sstevel@tonic-gate zone_t * 15640Sstevel@tonic-gate zone_find_by_name(char *name) 15650Sstevel@tonic-gate { 15660Sstevel@tonic-gate zone_t *zone; 15670Sstevel@tonic-gate zone_status_t status; 15680Sstevel@tonic-gate 15690Sstevel@tonic-gate mutex_enter(&zonehash_lock); 15700Sstevel@tonic-gate if ((zone = zone_find_all_by_name(name)) == NULL) { 15710Sstevel@tonic-gate mutex_exit(&zonehash_lock); 15720Sstevel@tonic-gate return (NULL); 15730Sstevel@tonic-gate } 15740Sstevel@tonic-gate status = zone_status_get(zone); 15750Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 15760Sstevel@tonic-gate /* 15770Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 15780Sstevel@tonic-gate */ 15790Sstevel@tonic-gate mutex_exit(&zonehash_lock); 15800Sstevel@tonic-gate return (NULL); 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate zone_hold(zone); 15830Sstevel@tonic-gate mutex_exit(&zonehash_lock); 15840Sstevel@tonic-gate return (zone); 15850Sstevel@tonic-gate } 15860Sstevel@tonic-gate 15870Sstevel@tonic-gate /* 15880Sstevel@tonic-gate * Similar to zone_find_by_id(), using the path as a key. For instance, 15890Sstevel@tonic-gate * if there is a zone "foo" rooted at /foo/root, and the path argument 15900Sstevel@tonic-gate * is "/foo/root/proc", it will return the held zone_t corresponding to 15910Sstevel@tonic-gate * zone "foo". 15920Sstevel@tonic-gate * 15930Sstevel@tonic-gate * zone_find_by_path() always returns a non-NULL value, since at the 15940Sstevel@tonic-gate * very least every path will be contained in the global zone. 15950Sstevel@tonic-gate * 15960Sstevel@tonic-gate * As with the other zone_find_by_*() functions, the caller is 15970Sstevel@tonic-gate * responsible for zone_rele()ing the return value of this function. 15980Sstevel@tonic-gate */ 15990Sstevel@tonic-gate zone_t * 16000Sstevel@tonic-gate zone_find_by_path(const char *path) 16010Sstevel@tonic-gate { 16020Sstevel@tonic-gate zone_t *zone; 16030Sstevel@tonic-gate zone_t *zret = NULL; 16040Sstevel@tonic-gate zone_status_t status; 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate if (path == NULL) { 16070Sstevel@tonic-gate /* 16080Sstevel@tonic-gate * Call from rootconf(). 16090Sstevel@tonic-gate */ 16100Sstevel@tonic-gate zone_hold(global_zone); 16110Sstevel@tonic-gate return (global_zone); 16120Sstevel@tonic-gate } 16130Sstevel@tonic-gate ASSERT(*path == '/'); 16140Sstevel@tonic-gate mutex_enter(&zonehash_lock); 16150Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 16160Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 16170Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(path, zone)) 16180Sstevel@tonic-gate zret = zone; 16190Sstevel@tonic-gate } 16200Sstevel@tonic-gate ASSERT(zret != NULL); 16210Sstevel@tonic-gate status = zone_status_get(zret); 16220Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 16230Sstevel@tonic-gate /* 16240Sstevel@tonic-gate * Zone practically doesn't exist. 16250Sstevel@tonic-gate */ 16260Sstevel@tonic-gate zret = global_zone; 16270Sstevel@tonic-gate } 16280Sstevel@tonic-gate zone_hold(zret); 16290Sstevel@tonic-gate mutex_exit(&zonehash_lock); 16300Sstevel@tonic-gate return (zret); 16310Sstevel@tonic-gate } 16320Sstevel@tonic-gate 16330Sstevel@tonic-gate /* 16340Sstevel@tonic-gate * Get the number of cpus visible to this zone. The system-wide global 16350Sstevel@tonic-gate * 'ncpus' is returned if pools are disabled, the caller is in the 16360Sstevel@tonic-gate * global zone, or a NULL zone argument is passed in. 16370Sstevel@tonic-gate */ 16380Sstevel@tonic-gate int 16390Sstevel@tonic-gate zone_ncpus_get(zone_t *zone) 16400Sstevel@tonic-gate { 16410Sstevel@tonic-gate int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 16420Sstevel@tonic-gate 16430Sstevel@tonic-gate return (myncpus != 0 ? myncpus : ncpus); 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate 16460Sstevel@tonic-gate /* 16470Sstevel@tonic-gate * Get the number of online cpus visible to this zone. The system-wide 16480Sstevel@tonic-gate * global 'ncpus_online' is returned if pools are disabled, the caller 16490Sstevel@tonic-gate * is in the global zone, or a NULL zone argument is passed in. 16500Sstevel@tonic-gate */ 16510Sstevel@tonic-gate int 16520Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone) 16530Sstevel@tonic-gate { 16540Sstevel@tonic-gate int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 16550Sstevel@tonic-gate 16560Sstevel@tonic-gate return (myncpus_online != 0 ? myncpus_online : ncpus_online); 16570Sstevel@tonic-gate } 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate /* 16600Sstevel@tonic-gate * Return the pool to which the zone is currently bound. 16610Sstevel@tonic-gate */ 16620Sstevel@tonic-gate pool_t * 16630Sstevel@tonic-gate zone_pool_get(zone_t *zone) 16640Sstevel@tonic-gate { 16650Sstevel@tonic-gate ASSERT(pool_lock_held()); 16660Sstevel@tonic-gate 16670Sstevel@tonic-gate return (zone->zone_pool); 16680Sstevel@tonic-gate } 16690Sstevel@tonic-gate 16700Sstevel@tonic-gate /* 16710Sstevel@tonic-gate * Set the zone's pool pointer and update the zone's visibility to match 16720Sstevel@tonic-gate * the resources in the new pool. 16730Sstevel@tonic-gate */ 16740Sstevel@tonic-gate void 16750Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool) 16760Sstevel@tonic-gate { 16770Sstevel@tonic-gate ASSERT(pool_lock_held()); 16780Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 16790Sstevel@tonic-gate 16800Sstevel@tonic-gate zone->zone_pool = pool; 16810Sstevel@tonic-gate zone_pset_set(zone, pool->pool_pset->pset_id); 16820Sstevel@tonic-gate } 16830Sstevel@tonic-gate 16840Sstevel@tonic-gate /* 16850Sstevel@tonic-gate * Return the cached value of the id of the processor set to which the 16860Sstevel@tonic-gate * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 16870Sstevel@tonic-gate * facility is disabled. 16880Sstevel@tonic-gate */ 16890Sstevel@tonic-gate psetid_t 16900Sstevel@tonic-gate zone_pset_get(zone_t *zone) 16910Sstevel@tonic-gate { 16920Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 16930Sstevel@tonic-gate 16940Sstevel@tonic-gate return (zone->zone_psetid); 16950Sstevel@tonic-gate } 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate /* 16980Sstevel@tonic-gate * Set the cached value of the id of the processor set to which the zone 16990Sstevel@tonic-gate * is currently bound. Also update the zone's visibility to match the 17000Sstevel@tonic-gate * resources in the new processor set. 17010Sstevel@tonic-gate */ 17020Sstevel@tonic-gate void 17030Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid) 17040Sstevel@tonic-gate { 17050Sstevel@tonic-gate psetid_t oldpsetid; 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 17080Sstevel@tonic-gate oldpsetid = zone_pset_get(zone); 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate if (oldpsetid == newpsetid) 17110Sstevel@tonic-gate return; 17120Sstevel@tonic-gate /* 17130Sstevel@tonic-gate * Global zone sees all. 17140Sstevel@tonic-gate */ 17150Sstevel@tonic-gate if (zone != global_zone) { 17160Sstevel@tonic-gate zone->zone_psetid = newpsetid; 17170Sstevel@tonic-gate if (newpsetid != ZONE_PS_INVAL) 17180Sstevel@tonic-gate pool_pset_visibility_add(newpsetid, zone); 17190Sstevel@tonic-gate if (oldpsetid != ZONE_PS_INVAL) 17200Sstevel@tonic-gate pool_pset_visibility_remove(oldpsetid, zone); 17210Sstevel@tonic-gate } 17220Sstevel@tonic-gate /* 17230Sstevel@tonic-gate * Disabling pools, so we should start using the global values 17240Sstevel@tonic-gate * for ncpus and ncpus_online. 17250Sstevel@tonic-gate */ 17260Sstevel@tonic-gate if (newpsetid == ZONE_PS_INVAL) { 17270Sstevel@tonic-gate zone->zone_ncpus = 0; 17280Sstevel@tonic-gate zone->zone_ncpus_online = 0; 17290Sstevel@tonic-gate } 17300Sstevel@tonic-gate } 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate /* 17330Sstevel@tonic-gate * Walk the list of active zones and issue the provided callback for 17340Sstevel@tonic-gate * each of them. 17350Sstevel@tonic-gate * 17360Sstevel@tonic-gate * Caller must not be holding any locks that may be acquired under 17370Sstevel@tonic-gate * zonehash_lock. See comment at the beginning of the file for a list of 17380Sstevel@tonic-gate * common locks and their interactions with zones. 17390Sstevel@tonic-gate */ 17400Sstevel@tonic-gate int 17410Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data) 17420Sstevel@tonic-gate { 17430Sstevel@tonic-gate zone_t *zone; 17440Sstevel@tonic-gate int ret = 0; 17450Sstevel@tonic-gate zone_status_t status; 17460Sstevel@tonic-gate 17470Sstevel@tonic-gate mutex_enter(&zonehash_lock); 17480Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 17490Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 17500Sstevel@tonic-gate /* 17510Sstevel@tonic-gate * Skip zones that shouldn't be externally visible. 17520Sstevel@tonic-gate */ 17530Sstevel@tonic-gate status = zone_status_get(zone); 17540Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 17550Sstevel@tonic-gate continue; 17560Sstevel@tonic-gate /* 17570Sstevel@tonic-gate * Bail immediately if any callback invocation returns a 17580Sstevel@tonic-gate * non-zero value. 17590Sstevel@tonic-gate */ 17600Sstevel@tonic-gate ret = (*cb)(zone, data); 17610Sstevel@tonic-gate if (ret != 0) 17620Sstevel@tonic-gate break; 17630Sstevel@tonic-gate } 17640Sstevel@tonic-gate mutex_exit(&zonehash_lock); 17650Sstevel@tonic-gate return (ret); 17660Sstevel@tonic-gate } 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate static int 17690Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath) 17700Sstevel@tonic-gate { 17710Sstevel@tonic-gate vnode_t *vp; 17720Sstevel@tonic-gate int trycount; 17730Sstevel@tonic-gate int error = 0; 17740Sstevel@tonic-gate char *path; 17750Sstevel@tonic-gate struct pathname upn, pn; 17760Sstevel@tonic-gate size_t pathlen; 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 17790Sstevel@tonic-gate return (error); 17800Sstevel@tonic-gate 17810Sstevel@tonic-gate pn_alloc(&pn); 17820Sstevel@tonic-gate 17830Sstevel@tonic-gate /* prevent infinite loop */ 17840Sstevel@tonic-gate trycount = 10; 17850Sstevel@tonic-gate for (;;) { 17860Sstevel@tonic-gate if (--trycount <= 0) { 17870Sstevel@tonic-gate error = ESTALE; 17880Sstevel@tonic-gate goto out; 17890Sstevel@tonic-gate } 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 17920Sstevel@tonic-gate /* 17930Sstevel@tonic-gate * VOP_ACCESS() may cover 'vp' with a new 17940Sstevel@tonic-gate * filesystem, if 'vp' is an autoFS vnode. 17950Sstevel@tonic-gate * Get the new 'vp' if so. 17960Sstevel@tonic-gate */ 17970Sstevel@tonic-gate if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 && 17980Sstevel@tonic-gate (vp->v_vfsmountedhere == NULL || 17990Sstevel@tonic-gate (error = traverse(&vp)) == 0)) { 18000Sstevel@tonic-gate pathlen = pn.pn_pathlen + 2; 18010Sstevel@tonic-gate path = kmem_alloc(pathlen, KM_SLEEP); 18020Sstevel@tonic-gate (void) strncpy(path, pn.pn_path, 18030Sstevel@tonic-gate pn.pn_pathlen + 1); 18040Sstevel@tonic-gate path[pathlen - 2] = '/'; 18050Sstevel@tonic-gate path[pathlen - 1] = '\0'; 18060Sstevel@tonic-gate pn_free(&pn); 18070Sstevel@tonic-gate pn_free(&upn); 18080Sstevel@tonic-gate 18090Sstevel@tonic-gate /* Success! */ 18100Sstevel@tonic-gate break; 18110Sstevel@tonic-gate } 18120Sstevel@tonic-gate VN_RELE(vp); 18130Sstevel@tonic-gate } 18140Sstevel@tonic-gate if (error != ESTALE) 18150Sstevel@tonic-gate goto out; 18160Sstevel@tonic-gate } 18170Sstevel@tonic-gate 18180Sstevel@tonic-gate ASSERT(error == 0); 18190Sstevel@tonic-gate zone->zone_rootvp = vp; /* we hold a reference to vp */ 18200Sstevel@tonic-gate zone->zone_rootpath = path; 18210Sstevel@tonic-gate zone->zone_rootpathlen = pathlen; 18220Sstevel@tonic-gate return (0); 18230Sstevel@tonic-gate 18240Sstevel@tonic-gate out: 18250Sstevel@tonic-gate pn_free(&pn); 18260Sstevel@tonic-gate pn_free(&upn); 18270Sstevel@tonic-gate return (error); 18280Sstevel@tonic-gate } 18290Sstevel@tonic-gate 18300Sstevel@tonic-gate #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 18310Sstevel@tonic-gate ((c) >= 'a' && (c) <= 'z') || \ 18320Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'Z')) 18330Sstevel@tonic-gate 18340Sstevel@tonic-gate static int 18350Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname) 18360Sstevel@tonic-gate { 18370Sstevel@tonic-gate char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 18380Sstevel@tonic-gate size_t len; 18390Sstevel@tonic-gate int i, err; 18400Sstevel@tonic-gate 18410Sstevel@tonic-gate if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 18420Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 18430Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 18440Sstevel@tonic-gate } 18450Sstevel@tonic-gate 18460Sstevel@tonic-gate /* must be less than ZONENAME_MAX */ 18470Sstevel@tonic-gate if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 18480Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 18490Sstevel@tonic-gate return (EINVAL); 18500Sstevel@tonic-gate } 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate /* 18530Sstevel@tonic-gate * Name must start with an alphanumeric and must contain only 18540Sstevel@tonic-gate * alphanumerics, '-', '_' and '.'. 18550Sstevel@tonic-gate */ 18560Sstevel@tonic-gate if (!isalnum(kname[0])) { 18570Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 18580Sstevel@tonic-gate return (EINVAL); 18590Sstevel@tonic-gate } 18600Sstevel@tonic-gate for (i = 1; i < len - 1; i++) { 18610Sstevel@tonic-gate if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 18620Sstevel@tonic-gate kname[i] != '.') { 18630Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 18640Sstevel@tonic-gate return (EINVAL); 18650Sstevel@tonic-gate } 18660Sstevel@tonic-gate } 18670Sstevel@tonic-gate 18680Sstevel@tonic-gate zone->zone_name = kname; 18690Sstevel@tonic-gate return (0); 18700Sstevel@tonic-gate } 18710Sstevel@tonic-gate 18720Sstevel@tonic-gate /* 18730Sstevel@tonic-gate * Similar to thread_create(), but makes sure the thread is in the appropriate 18740Sstevel@tonic-gate * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 18750Sstevel@tonic-gate */ 18760Sstevel@tonic-gate /*ARGSUSED*/ 18770Sstevel@tonic-gate kthread_t * 18780Sstevel@tonic-gate zthread_create( 18790Sstevel@tonic-gate caddr_t stk, 18800Sstevel@tonic-gate size_t stksize, 18810Sstevel@tonic-gate void (*proc)(), 18820Sstevel@tonic-gate void *arg, 18830Sstevel@tonic-gate size_t len, 18840Sstevel@tonic-gate pri_t pri) 18850Sstevel@tonic-gate { 18860Sstevel@tonic-gate kthread_t *t; 18870Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 18880Sstevel@tonic-gate proc_t *pp = zone->zone_zsched; 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate zone_hold(zone); /* Reference to be dropped when thread exits */ 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate /* 18930Sstevel@tonic-gate * No-one should be trying to create threads if the zone is shutting 18940Sstevel@tonic-gate * down and there aren't any kernel threads around. See comment 18950Sstevel@tonic-gate * in zthread_exit(). 18960Sstevel@tonic-gate */ 18970Sstevel@tonic-gate ASSERT(!(zone->zone_kthreads == NULL && 18980Sstevel@tonic-gate zone_status_get(zone) >= ZONE_IS_EMPTY)); 18990Sstevel@tonic-gate /* 19000Sstevel@tonic-gate * Create a thread, but don't let it run until we've finished setting 19010Sstevel@tonic-gate * things up. 19020Sstevel@tonic-gate */ 19030Sstevel@tonic-gate t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 19040Sstevel@tonic-gate ASSERT(t->t_forw == NULL); 19050Sstevel@tonic-gate mutex_enter(&zone_status_lock); 19060Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 19070Sstevel@tonic-gate t->t_forw = t->t_back = t; 19080Sstevel@tonic-gate } else { 19090Sstevel@tonic-gate kthread_t *tx = zone->zone_kthreads; 19100Sstevel@tonic-gate 19110Sstevel@tonic-gate t->t_forw = tx; 19120Sstevel@tonic-gate t->t_back = tx->t_back; 19130Sstevel@tonic-gate tx->t_back->t_forw = t; 19140Sstevel@tonic-gate tx->t_back = t; 19150Sstevel@tonic-gate } 19160Sstevel@tonic-gate zone->zone_kthreads = t; 19170Sstevel@tonic-gate mutex_exit(&zone_status_lock); 19180Sstevel@tonic-gate 19190Sstevel@tonic-gate mutex_enter(&pp->p_lock); 19200Sstevel@tonic-gate t->t_proc_flag |= TP_ZTHREAD; 19210Sstevel@tonic-gate project_rele(t->t_proj); 19220Sstevel@tonic-gate t->t_proj = project_hold(pp->p_task->tk_proj); 19230Sstevel@tonic-gate 19240Sstevel@tonic-gate /* 19250Sstevel@tonic-gate * Setup complete, let it run. 19260Sstevel@tonic-gate */ 19270Sstevel@tonic-gate thread_lock(t); 19280Sstevel@tonic-gate t->t_schedflag |= TS_ALLSTART; 19290Sstevel@tonic-gate setrun_locked(t); 19300Sstevel@tonic-gate thread_unlock(t); 19310Sstevel@tonic-gate 19320Sstevel@tonic-gate mutex_exit(&pp->p_lock); 19330Sstevel@tonic-gate 19340Sstevel@tonic-gate return (t); 19350Sstevel@tonic-gate } 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate /* 19380Sstevel@tonic-gate * Similar to thread_exit(). Must be called by threads created via 19390Sstevel@tonic-gate * zthread_exit(). 19400Sstevel@tonic-gate */ 19410Sstevel@tonic-gate void 19420Sstevel@tonic-gate zthread_exit(void) 19430Sstevel@tonic-gate { 19440Sstevel@tonic-gate kthread_t *t = curthread; 19450Sstevel@tonic-gate proc_t *pp = curproc; 19460Sstevel@tonic-gate zone_t *zone = pp->p_zone; 19470Sstevel@tonic-gate 19480Sstevel@tonic-gate mutex_enter(&zone_status_lock); 19490Sstevel@tonic-gate 19500Sstevel@tonic-gate /* 19510Sstevel@tonic-gate * Reparent to p0 19520Sstevel@tonic-gate */ 19531075Sjosephb kpreempt_disable(); 19540Sstevel@tonic-gate mutex_enter(&pp->p_lock); 19550Sstevel@tonic-gate t->t_proc_flag &= ~TP_ZTHREAD; 19560Sstevel@tonic-gate t->t_procp = &p0; 19570Sstevel@tonic-gate hat_thread_exit(t); 19580Sstevel@tonic-gate mutex_exit(&pp->p_lock); 19591075Sjosephb kpreempt_enable(); 19600Sstevel@tonic-gate 19610Sstevel@tonic-gate if (t->t_back == t) { 19620Sstevel@tonic-gate ASSERT(t->t_forw == t); 19630Sstevel@tonic-gate /* 19640Sstevel@tonic-gate * If the zone is empty, once the thread count 19650Sstevel@tonic-gate * goes to zero no further kernel threads can be 19660Sstevel@tonic-gate * created. This is because if the creator is a process 19670Sstevel@tonic-gate * in the zone, then it must have exited before the zone 19680Sstevel@tonic-gate * state could be set to ZONE_IS_EMPTY. 19690Sstevel@tonic-gate * Otherwise, if the creator is a kernel thread in the 19700Sstevel@tonic-gate * zone, the thread count is non-zero. 19710Sstevel@tonic-gate * 19720Sstevel@tonic-gate * This really means that non-zone kernel threads should 19730Sstevel@tonic-gate * not create zone kernel threads. 19740Sstevel@tonic-gate */ 19750Sstevel@tonic-gate zone->zone_kthreads = NULL; 19760Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_EMPTY) { 19770Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 19780Sstevel@tonic-gate } 19790Sstevel@tonic-gate } else { 19800Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 19810Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 19820Sstevel@tonic-gate if (zone->zone_kthreads == t) 19830Sstevel@tonic-gate zone->zone_kthreads = t->t_forw; 19840Sstevel@tonic-gate } 19850Sstevel@tonic-gate mutex_exit(&zone_status_lock); 19860Sstevel@tonic-gate zone_rele(zone); 19870Sstevel@tonic-gate thread_exit(); 19880Sstevel@tonic-gate /* NOTREACHED */ 19890Sstevel@tonic-gate } 19900Sstevel@tonic-gate 19910Sstevel@tonic-gate static void 19920Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 19930Sstevel@tonic-gate { 19940Sstevel@tonic-gate vnode_t *oldvp; 19950Sstevel@tonic-gate 19960Sstevel@tonic-gate /* we're going to hold a reference here to the directory */ 19970Sstevel@tonic-gate VN_HOLD(vp); 19980Sstevel@tonic-gate 19990Sstevel@tonic-gate #ifdef C2_AUDIT 20000Sstevel@tonic-gate if (audit_active) /* update abs cwd/root path see c2audit.c */ 20010Sstevel@tonic-gate audit_chdirec(vp, vpp); 20020Sstevel@tonic-gate #endif 20030Sstevel@tonic-gate 20040Sstevel@tonic-gate mutex_enter(&pp->p_lock); 20050Sstevel@tonic-gate oldvp = *vpp; 20060Sstevel@tonic-gate *vpp = vp; 20070Sstevel@tonic-gate mutex_exit(&pp->p_lock); 20080Sstevel@tonic-gate if (oldvp != NULL) 20090Sstevel@tonic-gate VN_RELE(oldvp); 20100Sstevel@tonic-gate } 20110Sstevel@tonic-gate 20120Sstevel@tonic-gate /* 20130Sstevel@tonic-gate * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 20140Sstevel@tonic-gate */ 20150Sstevel@tonic-gate static int 20160Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 20170Sstevel@tonic-gate { 20180Sstevel@tonic-gate nvpair_t *nvp = NULL; 20190Sstevel@tonic-gate boolean_t priv_set = B_FALSE; 20200Sstevel@tonic-gate boolean_t limit_set = B_FALSE; 20210Sstevel@tonic-gate boolean_t action_set = B_FALSE; 20220Sstevel@tonic-gate 20230Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 20240Sstevel@tonic-gate const char *name; 20250Sstevel@tonic-gate uint64_t ui64; 20260Sstevel@tonic-gate 20270Sstevel@tonic-gate name = nvpair_name(nvp); 20280Sstevel@tonic-gate if (nvpair_type(nvp) != DATA_TYPE_UINT64) 20290Sstevel@tonic-gate return (EINVAL); 20300Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &ui64); 20310Sstevel@tonic-gate if (strcmp(name, "privilege") == 0) { 20320Sstevel@tonic-gate /* 20330Sstevel@tonic-gate * Currently only privileged values are allowed, but 20340Sstevel@tonic-gate * this may change in the future. 20350Sstevel@tonic-gate */ 20360Sstevel@tonic-gate if (ui64 != RCPRIV_PRIVILEGED) 20370Sstevel@tonic-gate return (EINVAL); 20380Sstevel@tonic-gate rv->rcv_privilege = ui64; 20390Sstevel@tonic-gate priv_set = B_TRUE; 20400Sstevel@tonic-gate } else if (strcmp(name, "limit") == 0) { 20410Sstevel@tonic-gate rv->rcv_value = ui64; 20420Sstevel@tonic-gate limit_set = B_TRUE; 20430Sstevel@tonic-gate } else if (strcmp(name, "action") == 0) { 20440Sstevel@tonic-gate if (ui64 != RCTL_LOCAL_NOACTION && 20450Sstevel@tonic-gate ui64 != RCTL_LOCAL_DENY) 20460Sstevel@tonic-gate return (EINVAL); 20470Sstevel@tonic-gate rv->rcv_flagaction = ui64; 20480Sstevel@tonic-gate action_set = B_TRUE; 20490Sstevel@tonic-gate } else { 20500Sstevel@tonic-gate return (EINVAL); 20510Sstevel@tonic-gate } 20520Sstevel@tonic-gate } 20530Sstevel@tonic-gate 20540Sstevel@tonic-gate if (!(priv_set && limit_set && action_set)) 20550Sstevel@tonic-gate return (EINVAL); 20560Sstevel@tonic-gate rv->rcv_action_signal = 0; 20570Sstevel@tonic-gate rv->rcv_action_recipient = NULL; 20580Sstevel@tonic-gate rv->rcv_action_recip_pid = -1; 20590Sstevel@tonic-gate rv->rcv_firing_time = 0; 20600Sstevel@tonic-gate 20610Sstevel@tonic-gate return (0); 20620Sstevel@tonic-gate } 20630Sstevel@tonic-gate 20640Sstevel@tonic-gate void 20650Sstevel@tonic-gate zone_icode(void) 20660Sstevel@tonic-gate { 20670Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 20680Sstevel@tonic-gate struct core_globals *cg; 20690Sstevel@tonic-gate 20700Sstevel@tonic-gate /* 20710Sstevel@tonic-gate * For all purposes (ZONE_ATTR_INITPID and restart_init), 20720Sstevel@tonic-gate * storing just the pid of init is sufficient. 20730Sstevel@tonic-gate */ 20740Sstevel@tonic-gate p->p_zone->zone_proc_initpid = p->p_pid; 20750Sstevel@tonic-gate 20760Sstevel@tonic-gate /* 20770Sstevel@tonic-gate * Allocate user address space and stack segment 20780Sstevel@tonic-gate */ 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0; 20810Sstevel@tonic-gate p->p_usrstack = (caddr_t)USRSTACK32; 20820Sstevel@tonic-gate p->p_model = DATAMODEL_ILP32; 20830Sstevel@tonic-gate p->p_stkprot = PROT_ZFOD & ~PROT_EXEC; 20840Sstevel@tonic-gate p->p_datprot = PROT_ZFOD & ~PROT_EXEC; 20850Sstevel@tonic-gate p->p_stk_ctl = INT32_MAX; 20860Sstevel@tonic-gate 20870Sstevel@tonic-gate p->p_as = as_alloc(); 20880Sstevel@tonic-gate p->p_as->a_userlimit = (caddr_t)USERLIMIT32; 20890Sstevel@tonic-gate (void) hat_setup(p->p_as->a_hat, HAT_INIT); 20900Sstevel@tonic-gate 20910Sstevel@tonic-gate cg = zone_getspecific(core_zone_key, p->p_zone); 20920Sstevel@tonic-gate ASSERT(cg != NULL); 20930Sstevel@tonic-gate corectl_path_hold(cg->core_default_path); 20940Sstevel@tonic-gate corectl_content_hold(cg->core_default_content); 20950Sstevel@tonic-gate p->p_corefile = cg->core_default_path; 20960Sstevel@tonic-gate p->p_content = cg->core_default_content; 20970Sstevel@tonic-gate 20980Sstevel@tonic-gate init_mstate(curthread, LMS_SYSTEM); 20990Sstevel@tonic-gate 21000Sstevel@tonic-gate p->p_zone->zone_boot_err = exec_init(zone_initname, 0, 21010Sstevel@tonic-gate p->p_zone->zone_bootargs); 21020Sstevel@tonic-gate 21030Sstevel@tonic-gate mutex_enter(&zone_status_lock); 21040Sstevel@tonic-gate if (p->p_zone->zone_boot_err != 0) { 21050Sstevel@tonic-gate /* 21060Sstevel@tonic-gate * Make sure we are still in the booting state-- we could have 21070Sstevel@tonic-gate * raced and already be shutting down, or even further along. 21080Sstevel@tonic-gate */ 21090Sstevel@tonic-gate if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING) 21100Sstevel@tonic-gate zone_status_set(p->p_zone, ZONE_IS_SHUTTING_DOWN); 21110Sstevel@tonic-gate mutex_exit(&zone_status_lock); 21120Sstevel@tonic-gate /* It's gone bad, dispose of the process */ 21130Sstevel@tonic-gate if (proc_exit(CLD_EXITED, p->p_zone->zone_boot_err) != 0) { 2114390Sraf mutex_enter(&p->p_lock); 2115390Sraf ASSERT(p->p_flag & SEXITLWPS); 21160Sstevel@tonic-gate lwp_exit(); 21170Sstevel@tonic-gate } 21180Sstevel@tonic-gate } else { 21190Sstevel@tonic-gate if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING) 21200Sstevel@tonic-gate zone_status_set(p->p_zone, ZONE_IS_RUNNING); 21210Sstevel@tonic-gate mutex_exit(&zone_status_lock); 21220Sstevel@tonic-gate /* cause the process to return to userland. */ 21230Sstevel@tonic-gate lwp_rtt(); 21240Sstevel@tonic-gate } 21250Sstevel@tonic-gate } 21260Sstevel@tonic-gate 21270Sstevel@tonic-gate struct zsched_arg { 21280Sstevel@tonic-gate zone_t *zone; 21290Sstevel@tonic-gate nvlist_t *nvlist; 21300Sstevel@tonic-gate }; 21310Sstevel@tonic-gate 21320Sstevel@tonic-gate /* 21330Sstevel@tonic-gate * Per-zone "sched" workalike. The similarity to "sched" doesn't have 21340Sstevel@tonic-gate * anything to do with scheduling, but rather with the fact that 21350Sstevel@tonic-gate * per-zone kernel threads are parented to zsched, just like regular 21360Sstevel@tonic-gate * kernel threads are parented to sched (p0). 21370Sstevel@tonic-gate * 21380Sstevel@tonic-gate * zsched is also responsible for launching init for the zone. 21390Sstevel@tonic-gate */ 21400Sstevel@tonic-gate static void 21410Sstevel@tonic-gate zsched(void *arg) 21420Sstevel@tonic-gate { 21430Sstevel@tonic-gate struct zsched_arg *za = arg; 21440Sstevel@tonic-gate proc_t *pp = curproc; 21450Sstevel@tonic-gate proc_t *initp = proc_init; 21460Sstevel@tonic-gate zone_t *zone = za->zone; 21470Sstevel@tonic-gate cred_t *cr, *oldcred; 21480Sstevel@tonic-gate rctl_set_t *set; 21490Sstevel@tonic-gate rctl_alloc_gp_t *gp; 21500Sstevel@tonic-gate contract_t *ct = NULL; 21510Sstevel@tonic-gate task_t *tk, *oldtk; 21520Sstevel@tonic-gate rctl_entity_p_t e; 21530Sstevel@tonic-gate kproject_t *pj; 21540Sstevel@tonic-gate 21550Sstevel@tonic-gate nvlist_t *nvl = za->nvlist; 21560Sstevel@tonic-gate nvpair_t *nvp = NULL; 21570Sstevel@tonic-gate 21580Sstevel@tonic-gate bcopy("zsched", u.u_psargs, sizeof ("zsched")); 21590Sstevel@tonic-gate bcopy("zsched", u.u_comm, sizeof ("zsched")); 21600Sstevel@tonic-gate u.u_argc = 0; 21610Sstevel@tonic-gate u.u_argv = NULL; 21620Sstevel@tonic-gate u.u_envp = NULL; 21630Sstevel@tonic-gate closeall(P_FINFO(pp)); 21640Sstevel@tonic-gate 21650Sstevel@tonic-gate /* 21660Sstevel@tonic-gate * We are this zone's "zsched" process. As the zone isn't generally 21670Sstevel@tonic-gate * visible yet we don't need to grab any locks before initializing its 21680Sstevel@tonic-gate * zone_proc pointer. 21690Sstevel@tonic-gate */ 21700Sstevel@tonic-gate zone_hold(zone); /* this hold is released by zone_destroy() */ 21710Sstevel@tonic-gate zone->zone_zsched = pp; 21720Sstevel@tonic-gate mutex_enter(&pp->p_lock); 21730Sstevel@tonic-gate pp->p_zone = zone; 21740Sstevel@tonic-gate mutex_exit(&pp->p_lock); 21750Sstevel@tonic-gate 21760Sstevel@tonic-gate /* 21770Sstevel@tonic-gate * Disassociate process from its 'parent'; parent ourselves to init 21780Sstevel@tonic-gate * (pid 1) and change other values as needed. 21790Sstevel@tonic-gate */ 21800Sstevel@tonic-gate sess_create(); 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate mutex_enter(&pidlock); 21830Sstevel@tonic-gate proc_detach(pp); 21840Sstevel@tonic-gate pp->p_ppid = 1; 21850Sstevel@tonic-gate pp->p_flag |= SZONETOP; 21860Sstevel@tonic-gate pp->p_ancpid = 1; 21870Sstevel@tonic-gate pp->p_parent = initp; 21880Sstevel@tonic-gate pp->p_psibling = NULL; 21890Sstevel@tonic-gate if (initp->p_child) 21900Sstevel@tonic-gate initp->p_child->p_psibling = pp; 21910Sstevel@tonic-gate pp->p_sibling = initp->p_child; 21920Sstevel@tonic-gate initp->p_child = pp; 21930Sstevel@tonic-gate 21940Sstevel@tonic-gate /* Decrement what newproc() incremented. */ 21950Sstevel@tonic-gate upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 21960Sstevel@tonic-gate /* 21970Sstevel@tonic-gate * Our credentials are about to become kcred-like, so we don't care 21980Sstevel@tonic-gate * about the caller's ruid. 21990Sstevel@tonic-gate */ 22000Sstevel@tonic-gate upcount_inc(crgetruid(kcred), zone->zone_id); 22010Sstevel@tonic-gate mutex_exit(&pidlock); 22020Sstevel@tonic-gate 22030Sstevel@tonic-gate /* 22040Sstevel@tonic-gate * getting out of global zone, so decrement lwp counts 22050Sstevel@tonic-gate */ 22060Sstevel@tonic-gate pj = pp->p_task->tk_proj; 22070Sstevel@tonic-gate mutex_enter(&global_zone->zone_nlwps_lock); 22080Sstevel@tonic-gate pj->kpj_nlwps -= pp->p_lwpcnt; 22090Sstevel@tonic-gate global_zone->zone_nlwps -= pp->p_lwpcnt; 22100Sstevel@tonic-gate mutex_exit(&global_zone->zone_nlwps_lock); 22110Sstevel@tonic-gate 22120Sstevel@tonic-gate /* 22130Sstevel@tonic-gate * Create and join a new task in project '0' of this zone. 22140Sstevel@tonic-gate * 22150Sstevel@tonic-gate * We don't need to call holdlwps() since we know we're the only lwp in 22160Sstevel@tonic-gate * this process. 22170Sstevel@tonic-gate * 22180Sstevel@tonic-gate * task_join() returns with p_lock held. 22190Sstevel@tonic-gate */ 22200Sstevel@tonic-gate tk = task_create(0, zone); 22210Sstevel@tonic-gate mutex_enter(&cpu_lock); 22220Sstevel@tonic-gate oldtk = task_join(tk, 0); 22230Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 22240Sstevel@tonic-gate mutex_exit(&cpu_lock); 22250Sstevel@tonic-gate task_rele(oldtk); 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate /* 22280Sstevel@tonic-gate * add lwp counts to zsched's zone, and increment project's task count 22290Sstevel@tonic-gate * due to the task created in the above tasksys_settaskid 22300Sstevel@tonic-gate */ 22310Sstevel@tonic-gate pj = pp->p_task->tk_proj; 22320Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 22330Sstevel@tonic-gate pj->kpj_nlwps += pp->p_lwpcnt; 22340Sstevel@tonic-gate pj->kpj_ntasks += 1; 22350Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 22360Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 22370Sstevel@tonic-gate 22380Sstevel@tonic-gate /* 22390Sstevel@tonic-gate * The process was created by a process in the global zone, hence the 22400Sstevel@tonic-gate * credentials are wrong. We might as well have kcred-ish credentials. 22410Sstevel@tonic-gate */ 22420Sstevel@tonic-gate cr = zone->zone_kcred; 22430Sstevel@tonic-gate crhold(cr); 22440Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 22450Sstevel@tonic-gate oldcred = pp->p_cred; 22460Sstevel@tonic-gate pp->p_cred = cr; 22470Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 22480Sstevel@tonic-gate crfree(oldcred); 22490Sstevel@tonic-gate 22500Sstevel@tonic-gate /* 22510Sstevel@tonic-gate * Hold credentials again (for thread) 22520Sstevel@tonic-gate */ 22530Sstevel@tonic-gate crhold(cr); 22540Sstevel@tonic-gate 22550Sstevel@tonic-gate /* 22560Sstevel@tonic-gate * p_lwpcnt can't change since this is a kernel process. 22570Sstevel@tonic-gate */ 22580Sstevel@tonic-gate crset(pp, cr); 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate /* 22610Sstevel@tonic-gate * Chroot 22620Sstevel@tonic-gate */ 22630Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 22640Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 22650Sstevel@tonic-gate 22660Sstevel@tonic-gate /* 22670Sstevel@tonic-gate * Initialize zone's rctl set. 22680Sstevel@tonic-gate */ 22690Sstevel@tonic-gate set = rctl_set_create(); 22700Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 22710Sstevel@tonic-gate mutex_enter(&pp->p_lock); 22720Sstevel@tonic-gate e.rcep_p.zone = zone; 22730Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 22740Sstevel@tonic-gate zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 22750Sstevel@tonic-gate mutex_exit(&pp->p_lock); 22760Sstevel@tonic-gate rctl_prealloc_destroy(gp); 22770Sstevel@tonic-gate 22780Sstevel@tonic-gate /* 22790Sstevel@tonic-gate * Apply the rctls passed in to zone_create(). This is basically a list 22800Sstevel@tonic-gate * assignment: all of the old values are removed and the new ones 22810Sstevel@tonic-gate * inserted. That is, if an empty list is passed in, all values are 22820Sstevel@tonic-gate * removed. 22830Sstevel@tonic-gate */ 22840Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 22850Sstevel@tonic-gate rctl_dict_entry_t *rde; 22860Sstevel@tonic-gate rctl_hndl_t hndl; 22870Sstevel@tonic-gate char *name; 22880Sstevel@tonic-gate nvlist_t **nvlarray; 22890Sstevel@tonic-gate uint_t i, nelem; 22900Sstevel@tonic-gate int error; /* For ASSERT()s */ 22910Sstevel@tonic-gate 22920Sstevel@tonic-gate name = nvpair_name(nvp); 22930Sstevel@tonic-gate hndl = rctl_hndl_lookup(name); 22940Sstevel@tonic-gate ASSERT(hndl != -1); 22950Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 22960Sstevel@tonic-gate ASSERT(rde != NULL); 22970Sstevel@tonic-gate 22980Sstevel@tonic-gate for (; /* ever */; ) { 22990Sstevel@tonic-gate rctl_val_t oval; 23000Sstevel@tonic-gate 23010Sstevel@tonic-gate mutex_enter(&pp->p_lock); 23020Sstevel@tonic-gate error = rctl_local_get(hndl, NULL, &oval, pp); 23030Sstevel@tonic-gate mutex_exit(&pp->p_lock); 23040Sstevel@tonic-gate ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 23050Sstevel@tonic-gate ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 23060Sstevel@tonic-gate if (oval.rcv_privilege == RCPRIV_SYSTEM) 23070Sstevel@tonic-gate break; 23080Sstevel@tonic-gate mutex_enter(&pp->p_lock); 23090Sstevel@tonic-gate error = rctl_local_delete(hndl, &oval, pp); 23100Sstevel@tonic-gate mutex_exit(&pp->p_lock); 23110Sstevel@tonic-gate ASSERT(error == 0); 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 23140Sstevel@tonic-gate ASSERT(error == 0); 23150Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 23160Sstevel@tonic-gate rctl_val_t *nvalp; 23170Sstevel@tonic-gate 23180Sstevel@tonic-gate nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 23190Sstevel@tonic-gate error = nvlist2rctlval(nvlarray[i], nvalp); 23200Sstevel@tonic-gate ASSERT(error == 0); 23210Sstevel@tonic-gate /* 23220Sstevel@tonic-gate * rctl_local_insert can fail if the value being 23230Sstevel@tonic-gate * inserted is a duplicate; this is OK. 23240Sstevel@tonic-gate */ 23250Sstevel@tonic-gate mutex_enter(&pp->p_lock); 23260Sstevel@tonic-gate if (rctl_local_insert(hndl, nvalp, pp) != 0) 23270Sstevel@tonic-gate kmem_cache_free(rctl_val_cache, nvalp); 23280Sstevel@tonic-gate mutex_exit(&pp->p_lock); 23290Sstevel@tonic-gate } 23300Sstevel@tonic-gate } 23310Sstevel@tonic-gate /* 23320Sstevel@tonic-gate * Tell the world that we're done setting up. 23330Sstevel@tonic-gate * 23340Sstevel@tonic-gate * At this point we want to set the zone status to ZONE_IS_READY 23350Sstevel@tonic-gate * and atomically set the zone's processor set visibility. Once 23360Sstevel@tonic-gate * we drop pool_lock() this zone will automatically get updated 23370Sstevel@tonic-gate * to reflect any future changes to the pools configuration. 23380Sstevel@tonic-gate */ 23390Sstevel@tonic-gate pool_lock(); 23400Sstevel@tonic-gate mutex_enter(&cpu_lock); 23410Sstevel@tonic-gate mutex_enter(&zonehash_lock); 23420Sstevel@tonic-gate zone_uniqid(zone); 23430Sstevel@tonic-gate zone_zsd_configure(zone); 23440Sstevel@tonic-gate if (pool_state == POOL_ENABLED) 23450Sstevel@tonic-gate zone_pset_set(zone, pool_default->pool_pset->pset_id); 23460Sstevel@tonic-gate mutex_enter(&zone_status_lock); 23470Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 23480Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_READY); 23490Sstevel@tonic-gate mutex_exit(&zone_status_lock); 23500Sstevel@tonic-gate mutex_exit(&zonehash_lock); 23510Sstevel@tonic-gate mutex_exit(&cpu_lock); 23520Sstevel@tonic-gate pool_unlock(); 23530Sstevel@tonic-gate 23540Sstevel@tonic-gate /* 23550Sstevel@tonic-gate * Once we see the zone transition to the ZONE_IS_BOOTING state, 23560Sstevel@tonic-gate * we launch init, and set the state to running. 23570Sstevel@tonic-gate */ 23580Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 23590Sstevel@tonic-gate 23600Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_BOOTING) { 23610Sstevel@tonic-gate id_t cid; 23620Sstevel@tonic-gate 23630Sstevel@tonic-gate /* 23640Sstevel@tonic-gate * Ok, this is a little complicated. We need to grab the 23650Sstevel@tonic-gate * zone's pool's scheduling class ID; note that by now, we 23660Sstevel@tonic-gate * are already bound to a pool if we need to be (zoneadmd 23670Sstevel@tonic-gate * will have done that to us while we're in the READY 23680Sstevel@tonic-gate * state). *But* the scheduling class for the zone's 'init' 23690Sstevel@tonic-gate * must be explicitly passed to newproc, which doesn't 23700Sstevel@tonic-gate * respect pool bindings. 23710Sstevel@tonic-gate * 23720Sstevel@tonic-gate * We hold the pool_lock across the call to newproc() to 23730Sstevel@tonic-gate * close the obvious race: the pool's scheduling class 23740Sstevel@tonic-gate * could change before we manage to create the LWP with 23750Sstevel@tonic-gate * classid 'cid'. 23760Sstevel@tonic-gate */ 23770Sstevel@tonic-gate pool_lock(); 23780Sstevel@tonic-gate cid = pool_get_class(zone->zone_pool); 23790Sstevel@tonic-gate if (cid == -1) 23800Sstevel@tonic-gate cid = defaultcid; 23810Sstevel@tonic-gate 23820Sstevel@tonic-gate /* 23830Sstevel@tonic-gate * If this fails, zone_boot will ultimately fail. The 23840Sstevel@tonic-gate * state of the zone will be set to SHUTTING_DOWN-- userland 23850Sstevel@tonic-gate * will have to tear down the zone, and fail, or try again. 23860Sstevel@tonic-gate */ 23870Sstevel@tonic-gate if ((zone->zone_boot_err = newproc(zone_icode, NULL, cid, 23880Sstevel@tonic-gate minclsyspri - 1, &ct)) != 0) { 23890Sstevel@tonic-gate mutex_enter(&zone_status_lock); 23900Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 23910Sstevel@tonic-gate mutex_exit(&zone_status_lock); 23920Sstevel@tonic-gate } 23930Sstevel@tonic-gate pool_unlock(); 23940Sstevel@tonic-gate } 23950Sstevel@tonic-gate 23960Sstevel@tonic-gate /* 23970Sstevel@tonic-gate * Wait for zone_destroy() to be called. This is what we spend 23980Sstevel@tonic-gate * most of our life doing. 23990Sstevel@tonic-gate */ 24000Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 24010Sstevel@tonic-gate 24020Sstevel@tonic-gate if (ct) 24030Sstevel@tonic-gate /* 24040Sstevel@tonic-gate * At this point the process contract should be empty. 24050Sstevel@tonic-gate * (Though if it isn't, it's not the end of the world.) 24060Sstevel@tonic-gate */ 24070Sstevel@tonic-gate VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 24080Sstevel@tonic-gate 24090Sstevel@tonic-gate /* 24100Sstevel@tonic-gate * Allow kcred to be freed when all referring processes 24110Sstevel@tonic-gate * (including this one) go away. We can't just do this in 24120Sstevel@tonic-gate * zone_free because we need to wait for the zone_cred_ref to 24130Sstevel@tonic-gate * drop to 0 before calling zone_free, and the existence of 24140Sstevel@tonic-gate * zone_kcred will prevent that. Thus, we call crfree here to 24150Sstevel@tonic-gate * balance the crdup in zone_create. The crhold calls earlier 24160Sstevel@tonic-gate * in zsched will be dropped when the thread and process exit. 24170Sstevel@tonic-gate */ 24180Sstevel@tonic-gate crfree(zone->zone_kcred); 24190Sstevel@tonic-gate zone->zone_kcred = NULL; 24200Sstevel@tonic-gate 24210Sstevel@tonic-gate exit(CLD_EXITED, 0); 24220Sstevel@tonic-gate } 24230Sstevel@tonic-gate 24240Sstevel@tonic-gate /* 24250Sstevel@tonic-gate * Helper function to determine if there are any submounts of the 24260Sstevel@tonic-gate * provided path. Used to make sure the zone doesn't "inherit" any 24270Sstevel@tonic-gate * mounts from before it is created. 24280Sstevel@tonic-gate */ 24290Sstevel@tonic-gate static uint_t 24300Sstevel@tonic-gate zone_mount_count(const char *rootpath) 24310Sstevel@tonic-gate { 24320Sstevel@tonic-gate vfs_t *vfsp; 24330Sstevel@tonic-gate uint_t count = 0; 24340Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 24350Sstevel@tonic-gate 24360Sstevel@tonic-gate /* 24370Sstevel@tonic-gate * Holding zonehash_lock prevents race conditions with 24380Sstevel@tonic-gate * vfs_list_add()/vfs_list_remove() since we serialize with 24390Sstevel@tonic-gate * zone_find_by_path(). 24400Sstevel@tonic-gate */ 24410Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 24420Sstevel@tonic-gate /* 24430Sstevel@tonic-gate * The rootpath must end with a '/' 24440Sstevel@tonic-gate */ 24450Sstevel@tonic-gate ASSERT(rootpath[rootpathlen - 1] == '/'); 24460Sstevel@tonic-gate 24470Sstevel@tonic-gate /* 24480Sstevel@tonic-gate * This intentionally does not count the rootpath itself if that 24490Sstevel@tonic-gate * happens to be a mount point. 24500Sstevel@tonic-gate */ 24510Sstevel@tonic-gate vfs_list_read_lock(); 24520Sstevel@tonic-gate vfsp = rootvfs; 24530Sstevel@tonic-gate do { 24540Sstevel@tonic-gate if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 24550Sstevel@tonic-gate rootpathlen) == 0) 24560Sstevel@tonic-gate count++; 24570Sstevel@tonic-gate vfsp = vfsp->vfs_next; 24580Sstevel@tonic-gate } while (vfsp != rootvfs); 24590Sstevel@tonic-gate vfs_list_unlock(); 24600Sstevel@tonic-gate return (count); 24610Sstevel@tonic-gate } 24620Sstevel@tonic-gate 24630Sstevel@tonic-gate /* 24640Sstevel@tonic-gate * Helper function to make sure that a zone created on 'rootpath' 24650Sstevel@tonic-gate * wouldn't end up containing other zones' rootpaths. 24660Sstevel@tonic-gate */ 24670Sstevel@tonic-gate static boolean_t 24680Sstevel@tonic-gate zone_is_nested(const char *rootpath) 24690Sstevel@tonic-gate { 24700Sstevel@tonic-gate zone_t *zone; 24710Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 24720Sstevel@tonic-gate size_t len; 24730Sstevel@tonic-gate 24740Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 24750Sstevel@tonic-gate 24760Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 24770Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 24780Sstevel@tonic-gate if (zone == global_zone) 24790Sstevel@tonic-gate continue; 24800Sstevel@tonic-gate len = strlen(zone->zone_rootpath); 24810Sstevel@tonic-gate if (strncmp(rootpath, zone->zone_rootpath, 24820Sstevel@tonic-gate MIN(rootpathlen, len)) == 0) 24830Sstevel@tonic-gate return (B_TRUE); 24840Sstevel@tonic-gate } 24850Sstevel@tonic-gate return (B_FALSE); 24860Sstevel@tonic-gate } 24870Sstevel@tonic-gate 24880Sstevel@tonic-gate static int 2489813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs, 2490813Sdp size_t zone_privssz) 24910Sstevel@tonic-gate { 24920Sstevel@tonic-gate priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 24930Sstevel@tonic-gate 2494813Sdp if (zone_privssz < sizeof (priv_set_t)) 2495813Sdp return (set_errno(ENOMEM)); 2496813Sdp 24970Sstevel@tonic-gate if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 24980Sstevel@tonic-gate kmem_free(privs, sizeof (priv_set_t)); 24990Sstevel@tonic-gate return (EFAULT); 25000Sstevel@tonic-gate } 25010Sstevel@tonic-gate 25020Sstevel@tonic-gate zone->zone_privset = privs; 25030Sstevel@tonic-gate return (0); 25040Sstevel@tonic-gate } 25050Sstevel@tonic-gate 25060Sstevel@tonic-gate /* 25070Sstevel@tonic-gate * We make creative use of nvlists to pass in rctls from userland. The list is 25080Sstevel@tonic-gate * a list of the following structures: 25090Sstevel@tonic-gate * 25100Sstevel@tonic-gate * (name = rctl_name, value = nvpair_list_array) 25110Sstevel@tonic-gate * 25120Sstevel@tonic-gate * Where each element of the nvpair_list_array is of the form: 25130Sstevel@tonic-gate * 25140Sstevel@tonic-gate * [(name = "privilege", value = RCPRIV_PRIVILEGED), 25150Sstevel@tonic-gate * (name = "limit", value = uint64_t), 25160Sstevel@tonic-gate * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 25170Sstevel@tonic-gate */ 25180Sstevel@tonic-gate static int 25190Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 25200Sstevel@tonic-gate { 25210Sstevel@tonic-gate nvpair_t *nvp = NULL; 25220Sstevel@tonic-gate nvlist_t *nvl = NULL; 25230Sstevel@tonic-gate char *kbuf; 25240Sstevel@tonic-gate int error; 25250Sstevel@tonic-gate rctl_val_t rv; 25260Sstevel@tonic-gate 25270Sstevel@tonic-gate *nvlp = NULL; 25280Sstevel@tonic-gate 25290Sstevel@tonic-gate if (buflen == 0) 25300Sstevel@tonic-gate return (0); 25310Sstevel@tonic-gate 25320Sstevel@tonic-gate if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 25330Sstevel@tonic-gate return (ENOMEM); 25340Sstevel@tonic-gate if (copyin(ubuf, kbuf, buflen)) { 25350Sstevel@tonic-gate error = EFAULT; 25360Sstevel@tonic-gate goto out; 25370Sstevel@tonic-gate } 25380Sstevel@tonic-gate if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 25390Sstevel@tonic-gate /* 25400Sstevel@tonic-gate * nvl may have been allocated/free'd, but the value set to 25410Sstevel@tonic-gate * non-NULL, so we reset it here. 25420Sstevel@tonic-gate */ 25430Sstevel@tonic-gate nvl = NULL; 25440Sstevel@tonic-gate error = EINVAL; 25450Sstevel@tonic-gate goto out; 25460Sstevel@tonic-gate } 25470Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 25480Sstevel@tonic-gate rctl_dict_entry_t *rde; 25490Sstevel@tonic-gate rctl_hndl_t hndl; 25500Sstevel@tonic-gate nvlist_t **nvlarray; 25510Sstevel@tonic-gate uint_t i, nelem; 25520Sstevel@tonic-gate char *name; 25530Sstevel@tonic-gate 25540Sstevel@tonic-gate error = EINVAL; 25550Sstevel@tonic-gate name = nvpair_name(nvp); 25560Sstevel@tonic-gate if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 25570Sstevel@tonic-gate != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 25580Sstevel@tonic-gate goto out; 25590Sstevel@tonic-gate } 25600Sstevel@tonic-gate if ((hndl = rctl_hndl_lookup(name)) == -1) { 25610Sstevel@tonic-gate goto out; 25620Sstevel@tonic-gate } 25630Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 25640Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 25650Sstevel@tonic-gate ASSERT(error == 0); 25660Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 25670Sstevel@tonic-gate if (error = nvlist2rctlval(nvlarray[i], &rv)) 25680Sstevel@tonic-gate goto out; 25690Sstevel@tonic-gate } 25700Sstevel@tonic-gate if (rctl_invalid_value(rde, &rv)) { 25710Sstevel@tonic-gate error = EINVAL; 25720Sstevel@tonic-gate goto out; 25730Sstevel@tonic-gate } 25740Sstevel@tonic-gate } 25750Sstevel@tonic-gate error = 0; 25760Sstevel@tonic-gate *nvlp = nvl; 25770Sstevel@tonic-gate out: 25780Sstevel@tonic-gate kmem_free(kbuf, buflen); 25790Sstevel@tonic-gate if (error && nvl != NULL) 25800Sstevel@tonic-gate nvlist_free(nvl); 25810Sstevel@tonic-gate return (error); 25820Sstevel@tonic-gate } 25830Sstevel@tonic-gate 25840Sstevel@tonic-gate int 25850Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) { 25860Sstevel@tonic-gate if (er_out != NULL) { 25870Sstevel@tonic-gate if (copyout(&er_ext, er_out, sizeof (int))) { 25880Sstevel@tonic-gate return (set_errno(EFAULT)); 25890Sstevel@tonic-gate } 25900Sstevel@tonic-gate } 25910Sstevel@tonic-gate return (set_errno(er_error)); 25920Sstevel@tonic-gate } 25930Sstevel@tonic-gate 25940Sstevel@tonic-gate /* 2595789Sahrens * Parses a comma-separated list of ZFS datasets into a per-zone dictionary. 2596789Sahrens */ 2597789Sahrens static int 2598789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen) 2599789Sahrens { 2600789Sahrens char *kbuf; 2601789Sahrens char *dataset, *next; 2602789Sahrens zone_dataset_t *zd; 2603789Sahrens size_t len; 2604789Sahrens 2605789Sahrens if (ubuf == NULL || buflen == 0) 2606789Sahrens return (0); 2607789Sahrens 2608789Sahrens if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 2609789Sahrens return (ENOMEM); 2610789Sahrens 2611789Sahrens if (copyin(ubuf, kbuf, buflen) != 0) { 2612789Sahrens kmem_free(kbuf, buflen); 2613789Sahrens return (EFAULT); 2614789Sahrens } 2615789Sahrens 2616789Sahrens dataset = next = kbuf; 2617789Sahrens for (;;) { 2618789Sahrens zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP); 2619789Sahrens 2620789Sahrens next = strchr(dataset, ','); 2621789Sahrens 2622789Sahrens if (next == NULL) 2623789Sahrens len = strlen(dataset); 2624789Sahrens else 2625789Sahrens len = next - dataset; 2626789Sahrens 2627789Sahrens zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP); 2628789Sahrens bcopy(dataset, zd->zd_dataset, len); 2629789Sahrens zd->zd_dataset[len] = '\0'; 2630789Sahrens 2631789Sahrens list_insert_head(&zone->zone_datasets, zd); 2632789Sahrens 2633789Sahrens if (next == NULL) 2634789Sahrens break; 2635789Sahrens 2636789Sahrens dataset = next + 1; 2637789Sahrens } 2638789Sahrens 2639789Sahrens kmem_free(kbuf, buflen); 2640789Sahrens return (0); 2641789Sahrens } 2642789Sahrens 2643789Sahrens /* 26440Sstevel@tonic-gate * System call to create/initialize a new zone named 'zone_name', rooted 26450Sstevel@tonic-gate * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 26460Sstevel@tonic-gate * and initialized with the zone-wide rctls described in 'rctlbuf'. 26470Sstevel@tonic-gate * 26480Sstevel@tonic-gate * If extended error is non-null, we may use it to return more detailed 26490Sstevel@tonic-gate * error information. 26500Sstevel@tonic-gate */ 26510Sstevel@tonic-gate static zoneid_t 26520Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root, 2653813Sdp const priv_set_t *zone_privs, size_t zone_privssz, 2654813Sdp caddr_t rctlbuf, size_t rctlbufsz, 2655789Sahrens caddr_t zfsbuf, size_t zfsbufsz, int *extended_error) 26560Sstevel@tonic-gate { 26570Sstevel@tonic-gate struct zsched_arg zarg; 26580Sstevel@tonic-gate nvlist_t *rctls = NULL; 26590Sstevel@tonic-gate proc_t *pp = curproc; 26600Sstevel@tonic-gate zone_t *zone, *ztmp; 26610Sstevel@tonic-gate zoneid_t zoneid; 26620Sstevel@tonic-gate int error; 26630Sstevel@tonic-gate int error2 = 0; 26640Sstevel@tonic-gate char *str; 26650Sstevel@tonic-gate cred_t *zkcr; 26660Sstevel@tonic-gate 26670Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 26680Sstevel@tonic-gate return (set_errno(EPERM)); 26690Sstevel@tonic-gate 26700Sstevel@tonic-gate /* can't boot zone from within chroot environment */ 26710Sstevel@tonic-gate if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 26720Sstevel@tonic-gate return (zone_create_error(ENOTSUP, ZE_CHROOTED, 2673813Sdp extended_error)); 26740Sstevel@tonic-gate 26750Sstevel@tonic-gate zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 26760Sstevel@tonic-gate zoneid = zone->zone_id = id_alloc(zoneid_space); 26770Sstevel@tonic-gate zone->zone_status = ZONE_IS_UNINITIALIZED; 26780Sstevel@tonic-gate zone->zone_pool = pool_default; 26790Sstevel@tonic-gate zone->zone_pool_mod = gethrtime(); 26800Sstevel@tonic-gate zone->zone_psetid = ZONE_PS_INVAL; 26810Sstevel@tonic-gate zone->zone_ncpus = 0; 26820Sstevel@tonic-gate zone->zone_ncpus_online = 0; 26830Sstevel@tonic-gate mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 26840Sstevel@tonic-gate mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 26850Sstevel@tonic-gate cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 26860Sstevel@tonic-gate list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 26870Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 2688789Sahrens list_create(&zone->zone_datasets, sizeof (zone_dataset_t), 2689789Sahrens offsetof(zone_dataset_t, zd_linkage)); 26900Sstevel@tonic-gate 26910Sstevel@tonic-gate if ((error = zone_set_name(zone, zone_name)) != 0) { 26920Sstevel@tonic-gate zone_free(zone); 26930Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 26940Sstevel@tonic-gate } 26950Sstevel@tonic-gate 26960Sstevel@tonic-gate if ((error = zone_set_root(zone, zone_root)) != 0) { 26970Sstevel@tonic-gate zone_free(zone); 26980Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 26990Sstevel@tonic-gate } 2700813Sdp if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) { 27010Sstevel@tonic-gate zone_free(zone); 27020Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 27030Sstevel@tonic-gate } 27040Sstevel@tonic-gate 27050Sstevel@tonic-gate /* initialize node name to be the same as zone name */ 27060Sstevel@tonic-gate zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 27070Sstevel@tonic-gate (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 27080Sstevel@tonic-gate zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 27090Sstevel@tonic-gate 27100Sstevel@tonic-gate zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 27110Sstevel@tonic-gate zone->zone_domain[0] = '\0'; 27120Sstevel@tonic-gate zone->zone_shares = 1; 27130Sstevel@tonic-gate zone->zone_bootargs = NULL; 27140Sstevel@tonic-gate 27150Sstevel@tonic-gate /* 27160Sstevel@tonic-gate * Zsched initializes the rctls. 27170Sstevel@tonic-gate */ 27180Sstevel@tonic-gate zone->zone_rctls = NULL; 27190Sstevel@tonic-gate 27200Sstevel@tonic-gate if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 27210Sstevel@tonic-gate zone_free(zone); 27220Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 27230Sstevel@tonic-gate } 27240Sstevel@tonic-gate 2725789Sahrens if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) { 2726789Sahrens zone_free(zone); 2727789Sahrens return (set_errno(error)); 2728789Sahrens } 2729789Sahrens 27300Sstevel@tonic-gate /* 27310Sstevel@tonic-gate * Stop all lwps since that's what normally happens as part of fork(). 27320Sstevel@tonic-gate * This needs to happen before we grab any locks to avoid deadlock 27330Sstevel@tonic-gate * (another lwp in the process could be waiting for the held lock). 27340Sstevel@tonic-gate */ 27350Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 27360Sstevel@tonic-gate zone_free(zone); 27370Sstevel@tonic-gate if (rctls) 27380Sstevel@tonic-gate nvlist_free(rctls); 27390Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 27400Sstevel@tonic-gate } 27410Sstevel@tonic-gate 27420Sstevel@tonic-gate if (block_mounts() == 0) { 27430Sstevel@tonic-gate mutex_enter(&pp->p_lock); 27440Sstevel@tonic-gate if (curthread != pp->p_agenttp) 27450Sstevel@tonic-gate continuelwps(pp); 27460Sstevel@tonic-gate mutex_exit(&pp->p_lock); 27470Sstevel@tonic-gate zone_free(zone); 27480Sstevel@tonic-gate if (rctls) 27490Sstevel@tonic-gate nvlist_free(rctls); 27500Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 27510Sstevel@tonic-gate } 27520Sstevel@tonic-gate 27530Sstevel@tonic-gate /* 27540Sstevel@tonic-gate * Set up credential for kernel access. After this, any errors 27550Sstevel@tonic-gate * should go through the dance in errout rather than calling 27560Sstevel@tonic-gate * zone_free directly. 27570Sstevel@tonic-gate */ 27580Sstevel@tonic-gate zone->zone_kcred = crdup(kcred); 27590Sstevel@tonic-gate crsetzone(zone->zone_kcred, zone); 27600Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 27610Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 27620Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 27630Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 27640Sstevel@tonic-gate 27650Sstevel@tonic-gate mutex_enter(&zonehash_lock); 27660Sstevel@tonic-gate /* 27670Sstevel@tonic-gate * Make sure zone doesn't already exist. 27680Sstevel@tonic-gate */ 27690Sstevel@tonic-gate if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL) { 27700Sstevel@tonic-gate zone_status_t status; 27710Sstevel@tonic-gate 27720Sstevel@tonic-gate status = zone_status_get(ztmp); 27730Sstevel@tonic-gate if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 27740Sstevel@tonic-gate error = EEXIST; 27750Sstevel@tonic-gate else 27760Sstevel@tonic-gate error = EBUSY; 27770Sstevel@tonic-gate goto errout; 27780Sstevel@tonic-gate } 27790Sstevel@tonic-gate 27800Sstevel@tonic-gate /* 27810Sstevel@tonic-gate * Don't allow zone creations which would cause one zone's rootpath to 27820Sstevel@tonic-gate * be accessible from that of another (non-global) zone. 27830Sstevel@tonic-gate */ 27840Sstevel@tonic-gate if (zone_is_nested(zone->zone_rootpath)) { 27850Sstevel@tonic-gate error = EBUSY; 27860Sstevel@tonic-gate goto errout; 27870Sstevel@tonic-gate } 27880Sstevel@tonic-gate 27890Sstevel@tonic-gate ASSERT(zonecount != 0); /* check for leaks */ 27900Sstevel@tonic-gate if (zonecount + 1 > maxzones) { 27910Sstevel@tonic-gate error = ENOMEM; 27920Sstevel@tonic-gate goto errout; 27930Sstevel@tonic-gate } 27940Sstevel@tonic-gate 27950Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 27960Sstevel@tonic-gate error = EBUSY; 27970Sstevel@tonic-gate error2 = ZE_AREMOUNTS; 27980Sstevel@tonic-gate goto errout; 27990Sstevel@tonic-gate } 28000Sstevel@tonic-gate 28010Sstevel@tonic-gate /* 28020Sstevel@tonic-gate * Zone is still incomplete, but we need to drop all locks while 28030Sstevel@tonic-gate * zsched() initializes this zone's kernel process. We 28040Sstevel@tonic-gate * optimistically add the zone to the hashtable and associated 28050Sstevel@tonic-gate * lists so a parallel zone_create() doesn't try to create the 28060Sstevel@tonic-gate * same zone. 28070Sstevel@tonic-gate */ 28080Sstevel@tonic-gate zonecount++; 28090Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, 28100Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id, 28110Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 28120Sstevel@tonic-gate str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 28130Sstevel@tonic-gate (void) strcpy(str, zone->zone_name); 28140Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 28150Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 28160Sstevel@tonic-gate /* 28170Sstevel@tonic-gate * Insert into active list. At this point there are no 'hold's 28180Sstevel@tonic-gate * on the zone, but everyone else knows not to use it, so we can 28190Sstevel@tonic-gate * continue to use it. zsched() will do a zone_hold() if the 28200Sstevel@tonic-gate * newproc() is successful. 28210Sstevel@tonic-gate */ 28220Sstevel@tonic-gate list_insert_tail(&zone_active, zone); 28230Sstevel@tonic-gate mutex_exit(&zonehash_lock); 28240Sstevel@tonic-gate 28250Sstevel@tonic-gate zarg.zone = zone; 28260Sstevel@tonic-gate zarg.nvlist = rctls; 28270Sstevel@tonic-gate /* 28280Sstevel@tonic-gate * The process, task, and project rctls are probably wrong; 28290Sstevel@tonic-gate * we need an interface to get the default values of all rctls, 28300Sstevel@tonic-gate * and initialize zsched appropriately. I'm not sure that that 28310Sstevel@tonic-gate * makes much of a difference, though. 28320Sstevel@tonic-gate */ 28330Sstevel@tonic-gate if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) { 28340Sstevel@tonic-gate /* 28350Sstevel@tonic-gate * We need to undo all globally visible state. 28360Sstevel@tonic-gate */ 28370Sstevel@tonic-gate mutex_enter(&zonehash_lock); 28380Sstevel@tonic-gate list_remove(&zone_active, zone); 28390Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 28400Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_name); 28410Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 28420Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 28430Sstevel@tonic-gate ASSERT(zonecount > 1); 28440Sstevel@tonic-gate zonecount--; 28450Sstevel@tonic-gate goto errout; 28460Sstevel@tonic-gate } 28470Sstevel@tonic-gate 28480Sstevel@tonic-gate /* 28490Sstevel@tonic-gate * Zone creation can't fail from now on. 28500Sstevel@tonic-gate */ 28510Sstevel@tonic-gate 28520Sstevel@tonic-gate /* 28530Sstevel@tonic-gate * Let the other lwps continue. 28540Sstevel@tonic-gate */ 28550Sstevel@tonic-gate mutex_enter(&pp->p_lock); 28560Sstevel@tonic-gate if (curthread != pp->p_agenttp) 28570Sstevel@tonic-gate continuelwps(pp); 28580Sstevel@tonic-gate mutex_exit(&pp->p_lock); 28590Sstevel@tonic-gate 28600Sstevel@tonic-gate /* 28610Sstevel@tonic-gate * Wait for zsched to finish initializing the zone. 28620Sstevel@tonic-gate */ 28630Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_READY); 28640Sstevel@tonic-gate /* 28650Sstevel@tonic-gate * The zone is fully visible, so we can let mounts progress. 28660Sstevel@tonic-gate */ 28670Sstevel@tonic-gate resume_mounts(); 28680Sstevel@tonic-gate if (rctls) 28690Sstevel@tonic-gate nvlist_free(rctls); 28700Sstevel@tonic-gate 28710Sstevel@tonic-gate return (zoneid); 28720Sstevel@tonic-gate 28730Sstevel@tonic-gate errout: 28740Sstevel@tonic-gate mutex_exit(&zonehash_lock); 28750Sstevel@tonic-gate /* 28760Sstevel@tonic-gate * Let the other lwps continue. 28770Sstevel@tonic-gate */ 28780Sstevel@tonic-gate mutex_enter(&pp->p_lock); 28790Sstevel@tonic-gate if (curthread != pp->p_agenttp) 28800Sstevel@tonic-gate continuelwps(pp); 28810Sstevel@tonic-gate mutex_exit(&pp->p_lock); 28820Sstevel@tonic-gate 28830Sstevel@tonic-gate resume_mounts(); 28840Sstevel@tonic-gate if (rctls) 28850Sstevel@tonic-gate nvlist_free(rctls); 28860Sstevel@tonic-gate /* 28870Sstevel@tonic-gate * There is currently one reference to the zone, a cred_ref from 28880Sstevel@tonic-gate * zone_kcred. To free the zone, we call crfree, which will call 28890Sstevel@tonic-gate * zone_cred_rele, which will call zone_free. 28900Sstevel@tonic-gate */ 28910Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 28920Sstevel@tonic-gate ASSERT(zone->zone_kcred->cr_ref == 1); 28930Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 28940Sstevel@tonic-gate zkcr = zone->zone_kcred; 28950Sstevel@tonic-gate zone->zone_kcred = NULL; 28960Sstevel@tonic-gate crfree(zkcr); /* triggers call to zone_free */ 28970Sstevel@tonic-gate return (zone_create_error(error, error2, extended_error)); 28980Sstevel@tonic-gate } 28990Sstevel@tonic-gate 29000Sstevel@tonic-gate /* 29010Sstevel@tonic-gate * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 29020Sstevel@tonic-gate * the heavy lifting. 29030Sstevel@tonic-gate */ 29040Sstevel@tonic-gate static int 29050Sstevel@tonic-gate zone_boot(zoneid_t zoneid, const char *bootargs) 29060Sstevel@tonic-gate { 29070Sstevel@tonic-gate int err; 29080Sstevel@tonic-gate zone_t *zone; 29090Sstevel@tonic-gate 29100Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 29110Sstevel@tonic-gate return (set_errno(EPERM)); 29120Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 29130Sstevel@tonic-gate return (set_errno(EINVAL)); 29140Sstevel@tonic-gate 29150Sstevel@tonic-gate mutex_enter(&zonehash_lock); 29160Sstevel@tonic-gate /* 29170Sstevel@tonic-gate * Look for zone under hash lock to prevent races with calls to 29180Sstevel@tonic-gate * zone_shutdown, zone_destroy, etc. 29190Sstevel@tonic-gate */ 29200Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 29210Sstevel@tonic-gate mutex_exit(&zonehash_lock); 29220Sstevel@tonic-gate return (set_errno(EINVAL)); 29230Sstevel@tonic-gate } 29240Sstevel@tonic-gate 29250Sstevel@tonic-gate if ((err = zone_set_bootargs(zone, bootargs)) != 0) { 29260Sstevel@tonic-gate mutex_exit(&zonehash_lock); 29270Sstevel@tonic-gate return (set_errno(err)); 29280Sstevel@tonic-gate } 29290Sstevel@tonic-gate 29300Sstevel@tonic-gate mutex_enter(&zone_status_lock); 29310Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_READY) { 29320Sstevel@tonic-gate mutex_exit(&zone_status_lock); 29330Sstevel@tonic-gate mutex_exit(&zonehash_lock); 29340Sstevel@tonic-gate return (set_errno(EINVAL)); 29350Sstevel@tonic-gate } 29360Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_BOOTING); 29370Sstevel@tonic-gate mutex_exit(&zone_status_lock); 29380Sstevel@tonic-gate 29390Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 29400Sstevel@tonic-gate mutex_exit(&zonehash_lock); 29410Sstevel@tonic-gate 29420Sstevel@tonic-gate if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 29430Sstevel@tonic-gate zone_rele(zone); 29440Sstevel@tonic-gate return (set_errno(EINTR)); 29450Sstevel@tonic-gate } 29460Sstevel@tonic-gate 29470Sstevel@tonic-gate /* 29480Sstevel@tonic-gate * Boot (starting init) might have failed, in which case the zone 29490Sstevel@tonic-gate * will go to the SHUTTING_DOWN state; an appropriate errno will 29500Sstevel@tonic-gate * be placed in zone->zone_boot_err, and so we return that. 29510Sstevel@tonic-gate */ 29520Sstevel@tonic-gate err = zone->zone_boot_err; 29530Sstevel@tonic-gate zone_rele(zone); 29540Sstevel@tonic-gate return (err ? set_errno(err) : 0); 29550Sstevel@tonic-gate } 29560Sstevel@tonic-gate 29570Sstevel@tonic-gate /* 29580Sstevel@tonic-gate * Kills all user processes in the zone, waiting for them all to exit 29590Sstevel@tonic-gate * before returning. 29600Sstevel@tonic-gate */ 29610Sstevel@tonic-gate static int 29620Sstevel@tonic-gate zone_empty(zone_t *zone) 29630Sstevel@tonic-gate { 29640Sstevel@tonic-gate int waitstatus; 29650Sstevel@tonic-gate 29660Sstevel@tonic-gate /* 29670Sstevel@tonic-gate * We need to drop zonehash_lock before killing all 29680Sstevel@tonic-gate * processes, otherwise we'll deadlock with zone_find_* 29690Sstevel@tonic-gate * which can be called from the exit path. 29700Sstevel@tonic-gate */ 29710Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 29720Sstevel@tonic-gate while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz, 29730Sstevel@tonic-gate ZONE_IS_EMPTY)) == -1) { 29740Sstevel@tonic-gate killall(zone->zone_id); 29750Sstevel@tonic-gate } 29760Sstevel@tonic-gate /* 29770Sstevel@tonic-gate * return EINTR if we were signaled 29780Sstevel@tonic-gate */ 29790Sstevel@tonic-gate if (waitstatus == 0) 29800Sstevel@tonic-gate return (EINTR); 29810Sstevel@tonic-gate return (0); 29820Sstevel@tonic-gate } 29830Sstevel@tonic-gate 29840Sstevel@tonic-gate /* 29850Sstevel@tonic-gate * Systemcall to start the zone's halt sequence. By the time this 29860Sstevel@tonic-gate * function successfully returns, all user processes and kernel threads 29870Sstevel@tonic-gate * executing in it will have exited, ZSD shutdown callbacks executed, 29880Sstevel@tonic-gate * and the zone status set to ZONE_IS_DOWN. 29890Sstevel@tonic-gate * 29900Sstevel@tonic-gate * It is possible that the call will interrupt itself if the caller is the 29910Sstevel@tonic-gate * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 29920Sstevel@tonic-gate */ 29930Sstevel@tonic-gate static int 29940Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 29950Sstevel@tonic-gate { 29960Sstevel@tonic-gate int error; 29970Sstevel@tonic-gate zone_t *zone; 29980Sstevel@tonic-gate zone_status_t status; 29990Sstevel@tonic-gate 30000Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 30010Sstevel@tonic-gate return (set_errno(EPERM)); 30020Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 30030Sstevel@tonic-gate return (set_errno(EINVAL)); 30040Sstevel@tonic-gate 30050Sstevel@tonic-gate /* 30060Sstevel@tonic-gate * Block mounts so that VFS_MOUNT() can get an accurate view of 30070Sstevel@tonic-gate * the zone's status with regards to ZONE_IS_SHUTTING down. 30080Sstevel@tonic-gate * 30090Sstevel@tonic-gate * e.g. NFS can fail the mount if it determines that the zone 30100Sstevel@tonic-gate * has already begun the shutdown sequence. 30110Sstevel@tonic-gate */ 30120Sstevel@tonic-gate if (block_mounts() == 0) 30130Sstevel@tonic-gate return (set_errno(EINTR)); 30140Sstevel@tonic-gate mutex_enter(&zonehash_lock); 30150Sstevel@tonic-gate /* 30160Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 30170Sstevel@tonic-gate * calls to zone_shutdown and zone_destroy. 30180Sstevel@tonic-gate */ 30190Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 30200Sstevel@tonic-gate mutex_exit(&zonehash_lock); 30210Sstevel@tonic-gate resume_mounts(); 30220Sstevel@tonic-gate return (set_errno(EINVAL)); 30230Sstevel@tonic-gate } 30240Sstevel@tonic-gate mutex_enter(&zone_status_lock); 30250Sstevel@tonic-gate status = zone_status_get(zone); 30260Sstevel@tonic-gate /* 30270Sstevel@tonic-gate * Fail if the zone isn't fully initialized yet. 30280Sstevel@tonic-gate */ 30290Sstevel@tonic-gate if (status < ZONE_IS_READY) { 30300Sstevel@tonic-gate mutex_exit(&zone_status_lock); 30310Sstevel@tonic-gate mutex_exit(&zonehash_lock); 30320Sstevel@tonic-gate resume_mounts(); 30330Sstevel@tonic-gate return (set_errno(EINVAL)); 30340Sstevel@tonic-gate } 30350Sstevel@tonic-gate /* 30360Sstevel@tonic-gate * If conditions required for zone_shutdown() to return have been met, 30370Sstevel@tonic-gate * return success. 30380Sstevel@tonic-gate */ 30390Sstevel@tonic-gate if (status >= ZONE_IS_DOWN) { 30400Sstevel@tonic-gate mutex_exit(&zone_status_lock); 30410Sstevel@tonic-gate mutex_exit(&zonehash_lock); 30420Sstevel@tonic-gate resume_mounts(); 30430Sstevel@tonic-gate return (0); 30440Sstevel@tonic-gate } 30450Sstevel@tonic-gate /* 30460Sstevel@tonic-gate * If zone_shutdown() hasn't been called before, go through the motions. 30470Sstevel@tonic-gate * If it has, there's nothing to do but wait for the kernel threads to 30480Sstevel@tonic-gate * drain. 30490Sstevel@tonic-gate */ 30500Sstevel@tonic-gate if (status < ZONE_IS_EMPTY) { 30510Sstevel@tonic-gate uint_t ntasks; 30520Sstevel@tonic-gate 30530Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 30540Sstevel@tonic-gate if ((ntasks = zone->zone_ntasks) != 1) { 30550Sstevel@tonic-gate /* 30560Sstevel@tonic-gate * There's still stuff running. 30570Sstevel@tonic-gate */ 30580Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 30590Sstevel@tonic-gate } 30600Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 30610Sstevel@tonic-gate if (ntasks == 1) { 30620Sstevel@tonic-gate /* 30630Sstevel@tonic-gate * The only way to create another task is through 30640Sstevel@tonic-gate * zone_enter(), which will block until we drop 30650Sstevel@tonic-gate * zonehash_lock. The zone is empty. 30660Sstevel@tonic-gate */ 30670Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 30680Sstevel@tonic-gate /* 30690Sstevel@tonic-gate * Skip ahead to ZONE_IS_DOWN 30700Sstevel@tonic-gate */ 30710Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 30720Sstevel@tonic-gate } else { 30730Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 30740Sstevel@tonic-gate } 30750Sstevel@tonic-gate } 30760Sstevel@tonic-gate } 30770Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 30780Sstevel@tonic-gate mutex_exit(&zone_status_lock); 30790Sstevel@tonic-gate mutex_exit(&zonehash_lock); 30800Sstevel@tonic-gate resume_mounts(); 30810Sstevel@tonic-gate 30820Sstevel@tonic-gate if (error = zone_empty(zone)) { 30830Sstevel@tonic-gate zone_rele(zone); 30840Sstevel@tonic-gate return (set_errno(error)); 30850Sstevel@tonic-gate } 30860Sstevel@tonic-gate /* 30870Sstevel@tonic-gate * After the zone status goes to ZONE_IS_DOWN this zone will no 30880Sstevel@tonic-gate * longer be notified of changes to the pools configuration, so 30890Sstevel@tonic-gate * in order to not end up with a stale pool pointer, we point 30900Sstevel@tonic-gate * ourselves at the default pool and remove all resource 30910Sstevel@tonic-gate * visibility. This is especially important as the zone_t may 30920Sstevel@tonic-gate * languish on the deathrow for a very long time waiting for 30930Sstevel@tonic-gate * cred's to drain out. 30940Sstevel@tonic-gate * 30950Sstevel@tonic-gate * This rebinding of the zone can happen multiple times 30960Sstevel@tonic-gate * (presumably due to interrupted or parallel systemcalls) 30970Sstevel@tonic-gate * without any adverse effects. 30980Sstevel@tonic-gate */ 30990Sstevel@tonic-gate if (pool_lock_intr() != 0) { 31000Sstevel@tonic-gate zone_rele(zone); 31010Sstevel@tonic-gate return (set_errno(EINTR)); 31020Sstevel@tonic-gate } 31030Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 31040Sstevel@tonic-gate mutex_enter(&cpu_lock); 31050Sstevel@tonic-gate zone_pool_set(zone, pool_default); 31060Sstevel@tonic-gate /* 31070Sstevel@tonic-gate * The zone no longer needs to be able to see any cpus. 31080Sstevel@tonic-gate */ 31090Sstevel@tonic-gate zone_pset_set(zone, ZONE_PS_INVAL); 31100Sstevel@tonic-gate mutex_exit(&cpu_lock); 31110Sstevel@tonic-gate } 31120Sstevel@tonic-gate pool_unlock(); 31130Sstevel@tonic-gate 31140Sstevel@tonic-gate /* 31150Sstevel@tonic-gate * ZSD shutdown callbacks can be executed multiple times, hence 31160Sstevel@tonic-gate * it is safe to not be holding any locks across this call. 31170Sstevel@tonic-gate */ 31180Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 31190Sstevel@tonic-gate 31200Sstevel@tonic-gate mutex_enter(&zone_status_lock); 31210Sstevel@tonic-gate if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 31220Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 31230Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31240Sstevel@tonic-gate 31250Sstevel@tonic-gate /* 31260Sstevel@tonic-gate * Wait for kernel threads to drain. 31270Sstevel@tonic-gate */ 31280Sstevel@tonic-gate if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 31290Sstevel@tonic-gate zone_rele(zone); 31300Sstevel@tonic-gate return (set_errno(EINTR)); 31310Sstevel@tonic-gate } 31320Sstevel@tonic-gate zone_rele(zone); 31330Sstevel@tonic-gate return (0); 31340Sstevel@tonic-gate } 31350Sstevel@tonic-gate 31360Sstevel@tonic-gate /* 31370Sstevel@tonic-gate * Systemcall entry point to finalize the zone halt process. The caller 31380Sstevel@tonic-gate * must have already successfully callefd zone_shutdown(). 31390Sstevel@tonic-gate * 31400Sstevel@tonic-gate * Upon successful completion, the zone will have been fully destroyed: 31410Sstevel@tonic-gate * zsched will have exited, destructor callbacks executed, and the zone 31420Sstevel@tonic-gate * removed from the list of active zones. 31430Sstevel@tonic-gate */ 31440Sstevel@tonic-gate static int 31450Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 31460Sstevel@tonic-gate { 31470Sstevel@tonic-gate uint64_t uniqid; 31480Sstevel@tonic-gate zone_t *zone; 31490Sstevel@tonic-gate zone_status_t status; 31500Sstevel@tonic-gate 31510Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 31520Sstevel@tonic-gate return (set_errno(EPERM)); 31530Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 31540Sstevel@tonic-gate return (set_errno(EINVAL)); 31550Sstevel@tonic-gate 31560Sstevel@tonic-gate mutex_enter(&zonehash_lock); 31570Sstevel@tonic-gate /* 31580Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 31590Sstevel@tonic-gate * calls to zone_destroy. 31600Sstevel@tonic-gate */ 31610Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 31620Sstevel@tonic-gate mutex_exit(&zonehash_lock); 31630Sstevel@tonic-gate return (set_errno(EINVAL)); 31640Sstevel@tonic-gate } 31650Sstevel@tonic-gate 31660Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 31670Sstevel@tonic-gate mutex_exit(&zonehash_lock); 31680Sstevel@tonic-gate return (set_errno(EBUSY)); 31690Sstevel@tonic-gate } 31700Sstevel@tonic-gate mutex_enter(&zone_status_lock); 31710Sstevel@tonic-gate status = zone_status_get(zone); 31720Sstevel@tonic-gate if (status < ZONE_IS_DOWN) { 31730Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31740Sstevel@tonic-gate mutex_exit(&zonehash_lock); 31750Sstevel@tonic-gate return (set_errno(EBUSY)); 31760Sstevel@tonic-gate } else if (status == ZONE_IS_DOWN) { 31770Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 31780Sstevel@tonic-gate } 31790Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31800Sstevel@tonic-gate zone_hold(zone); 31810Sstevel@tonic-gate mutex_exit(&zonehash_lock); 31820Sstevel@tonic-gate 31830Sstevel@tonic-gate /* 31840Sstevel@tonic-gate * wait for zsched to exit 31850Sstevel@tonic-gate */ 31860Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_DEAD); 31870Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_DESTROY); 31880Sstevel@tonic-gate uniqid = zone->zone_uniqid; 31890Sstevel@tonic-gate zone_rele(zone); 31900Sstevel@tonic-gate zone = NULL; /* potentially free'd */ 31910Sstevel@tonic-gate 31920Sstevel@tonic-gate mutex_enter(&zonehash_lock); 31930Sstevel@tonic-gate for (; /* ever */; ) { 31940Sstevel@tonic-gate boolean_t unref; 31950Sstevel@tonic-gate 31960Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL || 31970Sstevel@tonic-gate zone->zone_uniqid != uniqid) { 31980Sstevel@tonic-gate /* 31990Sstevel@tonic-gate * The zone has gone away. Necessary conditions 32000Sstevel@tonic-gate * are met, so we return success. 32010Sstevel@tonic-gate */ 32020Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32030Sstevel@tonic-gate return (0); 32040Sstevel@tonic-gate } 32050Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 32060Sstevel@tonic-gate unref = ZONE_IS_UNREF(zone); 32070Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 32080Sstevel@tonic-gate if (unref) { 32090Sstevel@tonic-gate /* 32100Sstevel@tonic-gate * There is only one reference to the zone -- that 32110Sstevel@tonic-gate * added when the zone was added to the hashtables -- 32120Sstevel@tonic-gate * and things will remain this way until we drop 32130Sstevel@tonic-gate * zonehash_lock... we can go ahead and cleanup the 32140Sstevel@tonic-gate * zone. 32150Sstevel@tonic-gate */ 32160Sstevel@tonic-gate break; 32170Sstevel@tonic-gate } 32180Sstevel@tonic-gate 32190Sstevel@tonic-gate if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 32200Sstevel@tonic-gate /* Signaled */ 32210Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32220Sstevel@tonic-gate return (set_errno(EINTR)); 32230Sstevel@tonic-gate } 32240Sstevel@tonic-gate 32250Sstevel@tonic-gate } 32260Sstevel@tonic-gate 32270Sstevel@tonic-gate /* 32280Sstevel@tonic-gate * It is now safe to let the zone be recreated; remove it from the 32290Sstevel@tonic-gate * lists. The memory will not be freed until the last cred 32300Sstevel@tonic-gate * reference goes away. 32310Sstevel@tonic-gate */ 32320Sstevel@tonic-gate ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 32330Sstevel@tonic-gate zonecount--; 32340Sstevel@tonic-gate /* remove from active list and hash tables */ 32350Sstevel@tonic-gate list_remove(&zone_active, zone); 32360Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 32370Sstevel@tonic-gate (mod_hash_key_t)zone->zone_name); 32380Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 32390Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 32400Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32410Sstevel@tonic-gate 3242766Scarlsonj /* 3243766Scarlsonj * Release the root vnode; we're not using it anymore. Nor should any 3244766Scarlsonj * other thread that might access it exist. 3245766Scarlsonj */ 3246766Scarlsonj if (zone->zone_rootvp != NULL) { 3247766Scarlsonj VN_RELE(zone->zone_rootvp); 3248766Scarlsonj zone->zone_rootvp = NULL; 3249766Scarlsonj } 3250766Scarlsonj 32510Sstevel@tonic-gate /* add to deathrow list */ 32520Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 32530Sstevel@tonic-gate list_insert_tail(&zone_deathrow, zone); 32540Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 32550Sstevel@tonic-gate 32560Sstevel@tonic-gate /* 32570Sstevel@tonic-gate * Drop last reference (which was added by zsched()), this will 32580Sstevel@tonic-gate * free the zone unless there are outstanding cred references. 32590Sstevel@tonic-gate */ 32600Sstevel@tonic-gate zone_rele(zone); 32610Sstevel@tonic-gate return (0); 32620Sstevel@tonic-gate } 32630Sstevel@tonic-gate 32640Sstevel@tonic-gate /* 32650Sstevel@tonic-gate * Systemcall entry point for zone_getattr(2). 32660Sstevel@tonic-gate */ 32670Sstevel@tonic-gate static ssize_t 32680Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 32690Sstevel@tonic-gate { 32700Sstevel@tonic-gate size_t size; 32710Sstevel@tonic-gate int error = 0, err; 32720Sstevel@tonic-gate zone_t *zone; 32730Sstevel@tonic-gate char *zonepath; 32740Sstevel@tonic-gate zone_status_t zone_status; 32750Sstevel@tonic-gate pid_t initpid; 32760Sstevel@tonic-gate boolean_t global = (curproc->p_zone == global_zone); 32770Sstevel@tonic-gate 32780Sstevel@tonic-gate mutex_enter(&zonehash_lock); 32790Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 32800Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32810Sstevel@tonic-gate return (set_errno(EINVAL)); 32820Sstevel@tonic-gate } 32830Sstevel@tonic-gate zone_status = zone_status_get(zone); 32840Sstevel@tonic-gate if (zone_status < ZONE_IS_READY) { 32850Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32860Sstevel@tonic-gate return (set_errno(EINVAL)); 32870Sstevel@tonic-gate } 32880Sstevel@tonic-gate zone_hold(zone); 32890Sstevel@tonic-gate mutex_exit(&zonehash_lock); 32900Sstevel@tonic-gate 32910Sstevel@tonic-gate /* 32920Sstevel@tonic-gate * If not in the global zone, don't show information about other zones. 32930Sstevel@tonic-gate */ 32940Sstevel@tonic-gate if (!global && curproc->p_zone != zone) { 32950Sstevel@tonic-gate zone_rele(zone); 32960Sstevel@tonic-gate return (set_errno(EINVAL)); 32970Sstevel@tonic-gate } 32980Sstevel@tonic-gate 32990Sstevel@tonic-gate switch (attr) { 33000Sstevel@tonic-gate case ZONE_ATTR_ROOT: 33010Sstevel@tonic-gate if (global) { 33020Sstevel@tonic-gate /* 33030Sstevel@tonic-gate * Copy the path to trim the trailing "/" (except for 33040Sstevel@tonic-gate * the global zone). 33050Sstevel@tonic-gate */ 33060Sstevel@tonic-gate if (zone != global_zone) 33070Sstevel@tonic-gate size = zone->zone_rootpathlen - 1; 33080Sstevel@tonic-gate else 33090Sstevel@tonic-gate size = zone->zone_rootpathlen; 33100Sstevel@tonic-gate zonepath = kmem_alloc(size, KM_SLEEP); 33110Sstevel@tonic-gate bcopy(zone->zone_rootpath, zonepath, size); 33120Sstevel@tonic-gate zonepath[size - 1] = '\0'; 33130Sstevel@tonic-gate } else { 33140Sstevel@tonic-gate /* 33150Sstevel@tonic-gate * Caller is not in the global zone, just return 33160Sstevel@tonic-gate * faked-up path for current zone. 33170Sstevel@tonic-gate */ 33180Sstevel@tonic-gate zonepath = "/"; 33190Sstevel@tonic-gate size = 2; 33200Sstevel@tonic-gate } 33210Sstevel@tonic-gate if (bufsize > size) 33220Sstevel@tonic-gate bufsize = size; 33230Sstevel@tonic-gate if (buf != NULL) { 33240Sstevel@tonic-gate err = copyoutstr(zonepath, buf, bufsize, NULL); 33250Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 33260Sstevel@tonic-gate error = EFAULT; 33270Sstevel@tonic-gate } 33280Sstevel@tonic-gate if (global) 33290Sstevel@tonic-gate kmem_free(zonepath, size); 33300Sstevel@tonic-gate break; 33310Sstevel@tonic-gate 33320Sstevel@tonic-gate case ZONE_ATTR_NAME: 33330Sstevel@tonic-gate size = strlen(zone->zone_name) + 1; 33340Sstevel@tonic-gate if (bufsize > size) 33350Sstevel@tonic-gate bufsize = size; 33360Sstevel@tonic-gate if (buf != NULL) { 33370Sstevel@tonic-gate err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 33380Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 33390Sstevel@tonic-gate error = EFAULT; 33400Sstevel@tonic-gate } 33410Sstevel@tonic-gate break; 33420Sstevel@tonic-gate 33430Sstevel@tonic-gate case ZONE_ATTR_STATUS: 33440Sstevel@tonic-gate /* 33450Sstevel@tonic-gate * Since we're not holding zonehash_lock, the zone status 33460Sstevel@tonic-gate * may be anything; leave it up to userland to sort it out. 33470Sstevel@tonic-gate */ 33480Sstevel@tonic-gate size = sizeof (zone_status); 33490Sstevel@tonic-gate if (bufsize > size) 33500Sstevel@tonic-gate bufsize = size; 33510Sstevel@tonic-gate zone_status = zone_status_get(zone); 33520Sstevel@tonic-gate if (buf != NULL && 33530Sstevel@tonic-gate copyout(&zone_status, buf, bufsize) != 0) 33540Sstevel@tonic-gate error = EFAULT; 33550Sstevel@tonic-gate break; 33560Sstevel@tonic-gate case ZONE_ATTR_PRIVSET: 33570Sstevel@tonic-gate size = sizeof (priv_set_t); 33580Sstevel@tonic-gate if (bufsize > size) 33590Sstevel@tonic-gate bufsize = size; 33600Sstevel@tonic-gate if (buf != NULL && 33610Sstevel@tonic-gate copyout(zone->zone_privset, buf, bufsize) != 0) 33620Sstevel@tonic-gate error = EFAULT; 33630Sstevel@tonic-gate break; 33640Sstevel@tonic-gate case ZONE_ATTR_UNIQID: 33650Sstevel@tonic-gate size = sizeof (zone->zone_uniqid); 33660Sstevel@tonic-gate if (bufsize > size) 33670Sstevel@tonic-gate bufsize = size; 33680Sstevel@tonic-gate if (buf != NULL && 33690Sstevel@tonic-gate copyout(&zone->zone_uniqid, buf, bufsize) != 0) 33700Sstevel@tonic-gate error = EFAULT; 33710Sstevel@tonic-gate break; 33720Sstevel@tonic-gate case ZONE_ATTR_POOLID: 33730Sstevel@tonic-gate { 33740Sstevel@tonic-gate pool_t *pool; 33750Sstevel@tonic-gate poolid_t poolid; 33760Sstevel@tonic-gate 33770Sstevel@tonic-gate if (pool_lock_intr() != 0) { 33780Sstevel@tonic-gate error = EINTR; 33790Sstevel@tonic-gate break; 33800Sstevel@tonic-gate } 33810Sstevel@tonic-gate pool = zone_pool_get(zone); 33820Sstevel@tonic-gate poolid = pool->pool_id; 33830Sstevel@tonic-gate pool_unlock(); 33840Sstevel@tonic-gate size = sizeof (poolid); 33850Sstevel@tonic-gate if (bufsize > size) 33860Sstevel@tonic-gate bufsize = size; 33870Sstevel@tonic-gate if (buf != NULL && copyout(&poolid, buf, size) != 0) 33880Sstevel@tonic-gate error = EFAULT; 33890Sstevel@tonic-gate } 33900Sstevel@tonic-gate break; 33910Sstevel@tonic-gate case ZONE_ATTR_INITPID: 33920Sstevel@tonic-gate size = sizeof (initpid); 33930Sstevel@tonic-gate if (bufsize > size) 33940Sstevel@tonic-gate bufsize = size; 33950Sstevel@tonic-gate initpid = zone->zone_proc_initpid; 33960Sstevel@tonic-gate if (initpid == -1) { 33970Sstevel@tonic-gate error = ESRCH; 33980Sstevel@tonic-gate break; 33990Sstevel@tonic-gate } 34000Sstevel@tonic-gate if (buf != NULL && 34010Sstevel@tonic-gate copyout(&initpid, buf, bufsize) != 0) 34020Sstevel@tonic-gate error = EFAULT; 34030Sstevel@tonic-gate break; 34040Sstevel@tonic-gate default: 34050Sstevel@tonic-gate error = EINVAL; 34060Sstevel@tonic-gate } 34070Sstevel@tonic-gate zone_rele(zone); 34080Sstevel@tonic-gate 34090Sstevel@tonic-gate if (error) 34100Sstevel@tonic-gate return (set_errno(error)); 34110Sstevel@tonic-gate return ((ssize_t)size); 34120Sstevel@tonic-gate } 34130Sstevel@tonic-gate 34140Sstevel@tonic-gate /* 34150Sstevel@tonic-gate * Return zero if the process has at least one vnode mapped in to its 34160Sstevel@tonic-gate * address space which shouldn't be allowed to change zones. 34170Sstevel@tonic-gate */ 34180Sstevel@tonic-gate static int 34190Sstevel@tonic-gate as_can_change_zones(void) 34200Sstevel@tonic-gate { 34210Sstevel@tonic-gate proc_t *pp = curproc; 34220Sstevel@tonic-gate struct seg *seg; 34230Sstevel@tonic-gate struct as *as = pp->p_as; 34240Sstevel@tonic-gate vnode_t *vp; 34250Sstevel@tonic-gate int allow = 1; 34260Sstevel@tonic-gate 34270Sstevel@tonic-gate ASSERT(pp->p_as != &kas); 34280Sstevel@tonic-gate AS_LOCK_ENTER(&as, &as->a_lock, RW_READER); 34290Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 34300Sstevel@tonic-gate /* 34310Sstevel@tonic-gate * if we can't get a backing vnode for this segment then skip 34320Sstevel@tonic-gate * it. 34330Sstevel@tonic-gate */ 34340Sstevel@tonic-gate vp = NULL; 34350Sstevel@tonic-gate if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 34360Sstevel@tonic-gate continue; 34370Sstevel@tonic-gate if (!vn_can_change_zones(vp)) { /* bail on first match */ 34380Sstevel@tonic-gate allow = 0; 34390Sstevel@tonic-gate break; 34400Sstevel@tonic-gate } 34410Sstevel@tonic-gate } 34420Sstevel@tonic-gate AS_LOCK_EXIT(&as, &as->a_lock); 34430Sstevel@tonic-gate return (allow); 34440Sstevel@tonic-gate } 34450Sstevel@tonic-gate 34460Sstevel@tonic-gate /* 34470Sstevel@tonic-gate * Systemcall entry point for zone_enter(). 34480Sstevel@tonic-gate * 34490Sstevel@tonic-gate * The current process is injected into said zone. In the process 34500Sstevel@tonic-gate * it will change its project membership, privileges, rootdir/cwd, 34510Sstevel@tonic-gate * zone-wide rctls, and pool association to match those of the zone. 34520Sstevel@tonic-gate * 34530Sstevel@tonic-gate * The first zone_enter() called while the zone is in the ZONE_IS_READY 34540Sstevel@tonic-gate * state will transition it to ZONE_IS_RUNNING. Processes may only 34550Sstevel@tonic-gate * enter a zone that is "ready" or "running". 34560Sstevel@tonic-gate */ 34570Sstevel@tonic-gate static int 34580Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 34590Sstevel@tonic-gate { 34600Sstevel@tonic-gate zone_t *zone; 34610Sstevel@tonic-gate vnode_t *vp; 34620Sstevel@tonic-gate proc_t *pp = curproc; 34630Sstevel@tonic-gate contract_t *ct; 34640Sstevel@tonic-gate cont_process_t *ctp; 34650Sstevel@tonic-gate task_t *tk, *oldtk; 34660Sstevel@tonic-gate kproject_t *zone_proj0; 34670Sstevel@tonic-gate cred_t *cr, *newcr; 34680Sstevel@tonic-gate pool_t *oldpool, *newpool; 34690Sstevel@tonic-gate sess_t *sp; 34700Sstevel@tonic-gate uid_t uid; 34710Sstevel@tonic-gate zone_status_t status; 34720Sstevel@tonic-gate int err = 0; 34730Sstevel@tonic-gate rctl_entity_p_t e; 34740Sstevel@tonic-gate 34750Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 34760Sstevel@tonic-gate return (set_errno(EPERM)); 34770Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 34780Sstevel@tonic-gate return (set_errno(EINVAL)); 34790Sstevel@tonic-gate 34800Sstevel@tonic-gate /* 34810Sstevel@tonic-gate * Stop all lwps so we don't need to hold a lock to look at 34820Sstevel@tonic-gate * curproc->p_zone. This needs to happen before we grab any 34830Sstevel@tonic-gate * locks to avoid deadlock (another lwp in the process could 34840Sstevel@tonic-gate * be waiting for the held lock). 34850Sstevel@tonic-gate */ 34860Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 34870Sstevel@tonic-gate return (set_errno(EINTR)); 34880Sstevel@tonic-gate 34890Sstevel@tonic-gate /* 34900Sstevel@tonic-gate * Make sure we're not changing zones with files open or mapped in 34910Sstevel@tonic-gate * to our address space which shouldn't be changing zones. 34920Sstevel@tonic-gate */ 34930Sstevel@tonic-gate if (!files_can_change_zones()) { 34940Sstevel@tonic-gate err = EBADF; 34950Sstevel@tonic-gate goto out; 34960Sstevel@tonic-gate } 34970Sstevel@tonic-gate if (!as_can_change_zones()) { 34980Sstevel@tonic-gate err = EFAULT; 34990Sstevel@tonic-gate goto out; 35000Sstevel@tonic-gate } 35010Sstevel@tonic-gate 35020Sstevel@tonic-gate mutex_enter(&zonehash_lock); 35030Sstevel@tonic-gate if (pp->p_zone != global_zone) { 35040Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35050Sstevel@tonic-gate err = EINVAL; 35060Sstevel@tonic-gate goto out; 35070Sstevel@tonic-gate } 35080Sstevel@tonic-gate 35090Sstevel@tonic-gate zone = zone_find_all_by_id(zoneid); 35100Sstevel@tonic-gate if (zone == NULL) { 35110Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35120Sstevel@tonic-gate err = EINVAL; 35130Sstevel@tonic-gate goto out; 35140Sstevel@tonic-gate } 35150Sstevel@tonic-gate 35160Sstevel@tonic-gate /* 35170Sstevel@tonic-gate * To prevent processes in a zone from holding contracts on 35180Sstevel@tonic-gate * extrazonal resources, and to avoid process contract 35190Sstevel@tonic-gate * memberships which span zones, contract holders and processes 35200Sstevel@tonic-gate * which aren't the sole members of their encapsulating process 35210Sstevel@tonic-gate * contracts are not allowed to zone_enter. 35220Sstevel@tonic-gate */ 35230Sstevel@tonic-gate ctp = pp->p_ct_process; 35240Sstevel@tonic-gate ct = &ctp->conp_contract; 35250Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 35260Sstevel@tonic-gate mutex_enter(&pp->p_lock); 35270Sstevel@tonic-gate if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 35280Sstevel@tonic-gate mutex_exit(&pp->p_lock); 35290Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 35300Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35310Sstevel@tonic-gate pool_unlock(); 35320Sstevel@tonic-gate err = EINVAL; 35330Sstevel@tonic-gate goto out; 35340Sstevel@tonic-gate } 35350Sstevel@tonic-gate 35360Sstevel@tonic-gate /* 35370Sstevel@tonic-gate * Moreover, we don't allow processes whose encapsulating 35380Sstevel@tonic-gate * process contracts have inherited extrazonal contracts. 35390Sstevel@tonic-gate * While it would be easier to eliminate all process contracts 35400Sstevel@tonic-gate * with inherited contracts, we need to be able to give a 35410Sstevel@tonic-gate * restarted init (or other zone-penetrating process) its 35420Sstevel@tonic-gate * predecessor's contracts. 35430Sstevel@tonic-gate */ 35440Sstevel@tonic-gate if (ctp->conp_ninherited != 0) { 35450Sstevel@tonic-gate contract_t *next; 35460Sstevel@tonic-gate for (next = list_head(&ctp->conp_inherited); next; 35470Sstevel@tonic-gate next = list_next(&ctp->conp_inherited, next)) { 35480Sstevel@tonic-gate if (contract_getzuniqid(next) != zone->zone_uniqid) { 35490Sstevel@tonic-gate mutex_exit(&pp->p_lock); 35500Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 35510Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35520Sstevel@tonic-gate pool_unlock(); 35530Sstevel@tonic-gate err = EINVAL; 35540Sstevel@tonic-gate goto out; 35550Sstevel@tonic-gate } 35560Sstevel@tonic-gate } 35570Sstevel@tonic-gate } 35580Sstevel@tonic-gate mutex_exit(&pp->p_lock); 35590Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 35600Sstevel@tonic-gate 35610Sstevel@tonic-gate status = zone_status_get(zone); 35620Sstevel@tonic-gate if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 35630Sstevel@tonic-gate /* 35640Sstevel@tonic-gate * Can't join 35650Sstevel@tonic-gate */ 35660Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35670Sstevel@tonic-gate err = EINVAL; 35680Sstevel@tonic-gate goto out; 35690Sstevel@tonic-gate } 35700Sstevel@tonic-gate 35710Sstevel@tonic-gate /* 35720Sstevel@tonic-gate * Make sure new priv set is within the permitted set for caller 35730Sstevel@tonic-gate */ 35740Sstevel@tonic-gate if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 35750Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35760Sstevel@tonic-gate err = EPERM; 35770Sstevel@tonic-gate goto out; 35780Sstevel@tonic-gate } 35790Sstevel@tonic-gate /* 35800Sstevel@tonic-gate * We want to momentarily drop zonehash_lock while we optimistically 35810Sstevel@tonic-gate * bind curproc to the pool it should be running in. This is safe 35820Sstevel@tonic-gate * since the zone can't disappear (we have a hold on it). 35830Sstevel@tonic-gate */ 35840Sstevel@tonic-gate zone_hold(zone); 35850Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35860Sstevel@tonic-gate 35870Sstevel@tonic-gate /* 35880Sstevel@tonic-gate * Grab pool_lock to keep the pools configuration from changing 35890Sstevel@tonic-gate * and to stop ourselves from getting rebound to another pool 35900Sstevel@tonic-gate * until we join the zone. 35910Sstevel@tonic-gate */ 35920Sstevel@tonic-gate if (pool_lock_intr() != 0) { 35930Sstevel@tonic-gate zone_rele(zone); 35940Sstevel@tonic-gate err = EINTR; 35950Sstevel@tonic-gate goto out; 35960Sstevel@tonic-gate } 35970Sstevel@tonic-gate ASSERT(secpolicy_pool(CRED()) == 0); 35980Sstevel@tonic-gate /* 35990Sstevel@tonic-gate * Bind ourselves to the pool currently associated with the zone. 36000Sstevel@tonic-gate */ 36010Sstevel@tonic-gate oldpool = curproc->p_pool; 36020Sstevel@tonic-gate newpool = zone_pool_get(zone); 36030Sstevel@tonic-gate if (pool_state == POOL_ENABLED && newpool != oldpool && 36040Sstevel@tonic-gate (err = pool_do_bind(newpool, P_PID, P_MYID, 36050Sstevel@tonic-gate POOL_BIND_ALL)) != 0) { 36060Sstevel@tonic-gate pool_unlock(); 36070Sstevel@tonic-gate zone_rele(zone); 36080Sstevel@tonic-gate goto out; 36090Sstevel@tonic-gate } 36100Sstevel@tonic-gate 36110Sstevel@tonic-gate /* 36120Sstevel@tonic-gate * Grab cpu_lock now; we'll need it later when we call 36130Sstevel@tonic-gate * task_join(). 36140Sstevel@tonic-gate */ 36150Sstevel@tonic-gate mutex_enter(&cpu_lock); 36160Sstevel@tonic-gate mutex_enter(&zonehash_lock); 36170Sstevel@tonic-gate /* 36180Sstevel@tonic-gate * Make sure the zone hasn't moved on since we dropped zonehash_lock. 36190Sstevel@tonic-gate */ 36200Sstevel@tonic-gate if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 36210Sstevel@tonic-gate /* 36220Sstevel@tonic-gate * Can't join anymore. 36230Sstevel@tonic-gate */ 36240Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36250Sstevel@tonic-gate mutex_exit(&cpu_lock); 36260Sstevel@tonic-gate if (pool_state == POOL_ENABLED && 36270Sstevel@tonic-gate newpool != oldpool) 36280Sstevel@tonic-gate (void) pool_do_bind(oldpool, P_PID, P_MYID, 36290Sstevel@tonic-gate POOL_BIND_ALL); 36300Sstevel@tonic-gate pool_unlock(); 36310Sstevel@tonic-gate zone_rele(zone); 36320Sstevel@tonic-gate err = EINVAL; 36330Sstevel@tonic-gate goto out; 36340Sstevel@tonic-gate } 36350Sstevel@tonic-gate 36360Sstevel@tonic-gate mutex_enter(&pp->p_lock); 36370Sstevel@tonic-gate zone_proj0 = zone->zone_zsched->p_task->tk_proj; 36380Sstevel@tonic-gate /* verify that we do not exceed and task or lwp limits */ 36390Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 36400Sstevel@tonic-gate /* add new lwps to zone and zone's proj0 */ 36410Sstevel@tonic-gate zone_proj0->kpj_nlwps += pp->p_lwpcnt; 36420Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 36430Sstevel@tonic-gate /* add 1 task to zone's proj0 */ 36440Sstevel@tonic-gate zone_proj0->kpj_ntasks += 1; 36450Sstevel@tonic-gate mutex_exit(&pp->p_lock); 36460Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 36470Sstevel@tonic-gate 36480Sstevel@tonic-gate /* remove lwps from proc's old zone and old project */ 36490Sstevel@tonic-gate mutex_enter(&pp->p_zone->zone_nlwps_lock); 36500Sstevel@tonic-gate pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 36510Sstevel@tonic-gate pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 36520Sstevel@tonic-gate mutex_exit(&pp->p_zone->zone_nlwps_lock); 36530Sstevel@tonic-gate 36540Sstevel@tonic-gate /* 36550Sstevel@tonic-gate * Joining the zone cannot fail from now on. 36560Sstevel@tonic-gate * 36570Sstevel@tonic-gate * This means that a lot of the following code can be commonized and 36580Sstevel@tonic-gate * shared with zsched(). 36590Sstevel@tonic-gate */ 36600Sstevel@tonic-gate 36610Sstevel@tonic-gate /* 36620Sstevel@tonic-gate * Reset the encapsulating process contract's zone. 36630Sstevel@tonic-gate */ 36640Sstevel@tonic-gate ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 36650Sstevel@tonic-gate contract_setzuniqid(ct, zone->zone_uniqid); 36660Sstevel@tonic-gate 36670Sstevel@tonic-gate /* 36680Sstevel@tonic-gate * Create a new task and associate the process with the project keyed 36690Sstevel@tonic-gate * by (projid,zoneid). 36700Sstevel@tonic-gate * 36710Sstevel@tonic-gate * We might as well be in project 0; the global zone's projid doesn't 36720Sstevel@tonic-gate * make much sense in a zone anyhow. 36730Sstevel@tonic-gate * 36740Sstevel@tonic-gate * This also increments zone_ntasks, and returns with p_lock held. 36750Sstevel@tonic-gate */ 36760Sstevel@tonic-gate tk = task_create(0, zone); 36770Sstevel@tonic-gate oldtk = task_join(tk, 0); 36780Sstevel@tonic-gate mutex_exit(&cpu_lock); 36790Sstevel@tonic-gate 36800Sstevel@tonic-gate pp->p_flag |= SZONETOP; 36810Sstevel@tonic-gate pp->p_zone = zone; 36820Sstevel@tonic-gate 36830Sstevel@tonic-gate /* 36840Sstevel@tonic-gate * call RCTLOP_SET functions on this proc 36850Sstevel@tonic-gate */ 36860Sstevel@tonic-gate e.rcep_p.zone = zone; 36870Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 36880Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 36890Sstevel@tonic-gate RCD_CALLBACK); 36900Sstevel@tonic-gate mutex_exit(&pp->p_lock); 36910Sstevel@tonic-gate 36920Sstevel@tonic-gate /* 36930Sstevel@tonic-gate * We don't need to hold any of zsched's locks here; not only do we know 36940Sstevel@tonic-gate * the process and zone aren't going away, we know its session isn't 36950Sstevel@tonic-gate * changing either. 36960Sstevel@tonic-gate * 36970Sstevel@tonic-gate * By joining zsched's session here, we mimic the behavior in the 36980Sstevel@tonic-gate * global zone of init's sid being the pid of sched. We extend this 36990Sstevel@tonic-gate * to all zlogin-like zone_enter()'ing processes as well. 37000Sstevel@tonic-gate */ 37010Sstevel@tonic-gate mutex_enter(&pidlock); 37020Sstevel@tonic-gate sp = zone->zone_zsched->p_sessp; 37030Sstevel@tonic-gate SESS_HOLD(sp); 37040Sstevel@tonic-gate mutex_enter(&pp->p_lock); 37050Sstevel@tonic-gate pgexit(pp); 37060Sstevel@tonic-gate SESS_RELE(pp->p_sessp); 37070Sstevel@tonic-gate pp->p_sessp = sp; 37080Sstevel@tonic-gate pgjoin(pp, zone->zone_zsched->p_pidp); 37090Sstevel@tonic-gate mutex_exit(&pp->p_lock); 37100Sstevel@tonic-gate mutex_exit(&pidlock); 37110Sstevel@tonic-gate 37120Sstevel@tonic-gate mutex_exit(&zonehash_lock); 37130Sstevel@tonic-gate /* 37140Sstevel@tonic-gate * We're firmly in the zone; let pools progress. 37150Sstevel@tonic-gate */ 37160Sstevel@tonic-gate pool_unlock(); 37170Sstevel@tonic-gate task_rele(oldtk); 37180Sstevel@tonic-gate /* 37190Sstevel@tonic-gate * We don't need to retain a hold on the zone since we already 37200Sstevel@tonic-gate * incremented zone_ntasks, so the zone isn't going anywhere. 37210Sstevel@tonic-gate */ 37220Sstevel@tonic-gate zone_rele(zone); 37230Sstevel@tonic-gate 37240Sstevel@tonic-gate /* 37250Sstevel@tonic-gate * Chroot 37260Sstevel@tonic-gate */ 37270Sstevel@tonic-gate vp = zone->zone_rootvp; 37280Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 37290Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 37300Sstevel@tonic-gate 37310Sstevel@tonic-gate /* 37320Sstevel@tonic-gate * Change process credentials 37330Sstevel@tonic-gate */ 37340Sstevel@tonic-gate newcr = cralloc(); 37350Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 37360Sstevel@tonic-gate cr = pp->p_cred; 37370Sstevel@tonic-gate crcopy_to(cr, newcr); 37380Sstevel@tonic-gate crsetzone(newcr, zone); 37390Sstevel@tonic-gate pp->p_cred = newcr; 37400Sstevel@tonic-gate 37410Sstevel@tonic-gate /* 37420Sstevel@tonic-gate * Restrict all process privilege sets to zone limit 37430Sstevel@tonic-gate */ 37440Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 37450Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 37460Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 37470Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 37480Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 37490Sstevel@tonic-gate crset(pp, newcr); 37500Sstevel@tonic-gate 37510Sstevel@tonic-gate /* 37520Sstevel@tonic-gate * Adjust upcount to reflect zone entry. 37530Sstevel@tonic-gate */ 37540Sstevel@tonic-gate uid = crgetruid(newcr); 37550Sstevel@tonic-gate mutex_enter(&pidlock); 37560Sstevel@tonic-gate upcount_dec(uid, GLOBAL_ZONEID); 37570Sstevel@tonic-gate upcount_inc(uid, zoneid); 37580Sstevel@tonic-gate mutex_exit(&pidlock); 37590Sstevel@tonic-gate 37600Sstevel@tonic-gate /* 37610Sstevel@tonic-gate * Set up core file path and content. 37620Sstevel@tonic-gate */ 37630Sstevel@tonic-gate set_core_defaults(); 37640Sstevel@tonic-gate 37650Sstevel@tonic-gate out: 37660Sstevel@tonic-gate /* 37670Sstevel@tonic-gate * Let the other lwps continue. 37680Sstevel@tonic-gate */ 37690Sstevel@tonic-gate mutex_enter(&pp->p_lock); 37700Sstevel@tonic-gate if (curthread != pp->p_agenttp) 37710Sstevel@tonic-gate continuelwps(pp); 37720Sstevel@tonic-gate mutex_exit(&pp->p_lock); 37730Sstevel@tonic-gate 37740Sstevel@tonic-gate return (err != 0 ? set_errno(err) : 0); 37750Sstevel@tonic-gate } 37760Sstevel@tonic-gate 37770Sstevel@tonic-gate /* 37780Sstevel@tonic-gate * Systemcall entry point for zone_list(2). 37790Sstevel@tonic-gate * 37800Sstevel@tonic-gate * Processes running in a (non-global) zone only see themselves. 37810Sstevel@tonic-gate */ 37820Sstevel@tonic-gate static int 37830Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones) 37840Sstevel@tonic-gate { 37850Sstevel@tonic-gate zoneid_t *zoneids; 37860Sstevel@tonic-gate zone_t *zone; 37870Sstevel@tonic-gate uint_t user_nzones, real_nzones; 37880Sstevel@tonic-gate int error = 0; 37890Sstevel@tonic-gate uint_t i; 37900Sstevel@tonic-gate 37910Sstevel@tonic-gate if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 37920Sstevel@tonic-gate return (set_errno(EFAULT)); 37930Sstevel@tonic-gate 37940Sstevel@tonic-gate if (curproc->p_zone != global_zone) { 37950Sstevel@tonic-gate /* just return current zone */ 37960Sstevel@tonic-gate real_nzones = 1; 37970Sstevel@tonic-gate zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 37980Sstevel@tonic-gate zoneids[0] = curproc->p_zone->zone_id; 37990Sstevel@tonic-gate } else { 38000Sstevel@tonic-gate mutex_enter(&zonehash_lock); 38010Sstevel@tonic-gate real_nzones = zonecount; 38020Sstevel@tonic-gate if (real_nzones) { 38030Sstevel@tonic-gate zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 38040Sstevel@tonic-gate KM_SLEEP); 38050Sstevel@tonic-gate i = 0; 38060Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 38070Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 38080Sstevel@tonic-gate zoneids[i++] = zone->zone_id; 38090Sstevel@tonic-gate ASSERT(i == real_nzones); 38100Sstevel@tonic-gate } 38110Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38120Sstevel@tonic-gate } 38130Sstevel@tonic-gate 38140Sstevel@tonic-gate if (user_nzones > real_nzones) 38150Sstevel@tonic-gate user_nzones = real_nzones; 38160Sstevel@tonic-gate 38170Sstevel@tonic-gate if (copyout(&real_nzones, numzones, sizeof (uint_t)) != 0) 38180Sstevel@tonic-gate error = EFAULT; 38190Sstevel@tonic-gate else if (zoneidlist != NULL && user_nzones != 0) { 38200Sstevel@tonic-gate if (copyout(zoneids, zoneidlist, 38210Sstevel@tonic-gate user_nzones * sizeof (zoneid_t)) != 0) 38220Sstevel@tonic-gate error = EFAULT; 38230Sstevel@tonic-gate } 38240Sstevel@tonic-gate 38250Sstevel@tonic-gate if (real_nzones) 38260Sstevel@tonic-gate kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 38270Sstevel@tonic-gate 38280Sstevel@tonic-gate if (error) 38290Sstevel@tonic-gate return (set_errno(error)); 38300Sstevel@tonic-gate else 38310Sstevel@tonic-gate return (0); 38320Sstevel@tonic-gate } 38330Sstevel@tonic-gate 38340Sstevel@tonic-gate /* 38350Sstevel@tonic-gate * Systemcall entry point for zone_lookup(2). 38360Sstevel@tonic-gate * 38370Sstevel@tonic-gate * Non-global zones are only able to see themselves. 38380Sstevel@tonic-gate */ 38390Sstevel@tonic-gate static zoneid_t 38400Sstevel@tonic-gate zone_lookup(const char *zone_name) 38410Sstevel@tonic-gate { 38420Sstevel@tonic-gate char *kname; 38430Sstevel@tonic-gate zone_t *zone; 38440Sstevel@tonic-gate zoneid_t zoneid; 38450Sstevel@tonic-gate int err; 38460Sstevel@tonic-gate 38470Sstevel@tonic-gate if (zone_name == NULL) { 38480Sstevel@tonic-gate /* return caller's zone id */ 38490Sstevel@tonic-gate return (getzoneid()); 38500Sstevel@tonic-gate } 38510Sstevel@tonic-gate 38520Sstevel@tonic-gate kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 38530Sstevel@tonic-gate if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 38540Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 38550Sstevel@tonic-gate return (set_errno(err)); 38560Sstevel@tonic-gate } 38570Sstevel@tonic-gate 38580Sstevel@tonic-gate mutex_enter(&zonehash_lock); 38590Sstevel@tonic-gate zone = zone_find_all_by_name(kname); 38600Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 38610Sstevel@tonic-gate if (zone == NULL || zone_status_get(zone) < ZONE_IS_READY || 38620Sstevel@tonic-gate (curproc->p_zone != global_zone && curproc->p_zone != zone)) { 38630Sstevel@tonic-gate /* in non-global zone, can only lookup own name */ 38640Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38650Sstevel@tonic-gate return (set_errno(EINVAL)); 38660Sstevel@tonic-gate } 38670Sstevel@tonic-gate zoneid = zone->zone_id; 38680Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38690Sstevel@tonic-gate return (zoneid); 38700Sstevel@tonic-gate } 38710Sstevel@tonic-gate 3872813Sdp static int 3873813Sdp zone_version(int *version_arg) 3874813Sdp { 3875813Sdp int version = ZONE_SYSCALL_API_VERSION; 3876813Sdp 3877813Sdp if (copyout(&version, version_arg, sizeof (int)) != 0) 3878813Sdp return (set_errno(EFAULT)); 3879813Sdp return (0); 3880813Sdp } 3881813Sdp 38820Sstevel@tonic-gate /* ARGSUSED */ 38830Sstevel@tonic-gate long 3884789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4) 38850Sstevel@tonic-gate { 38860Sstevel@tonic-gate zone_def zs; 38870Sstevel@tonic-gate 38880Sstevel@tonic-gate switch (cmd) { 38890Sstevel@tonic-gate case ZONE_CREATE: 38900Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 38910Sstevel@tonic-gate if (copyin(arg1, &zs, sizeof (zone_def))) { 38920Sstevel@tonic-gate return (set_errno(EFAULT)); 38930Sstevel@tonic-gate } 38940Sstevel@tonic-gate } else { 38950Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 38960Sstevel@tonic-gate zone_def32 zs32; 38970Sstevel@tonic-gate 38980Sstevel@tonic-gate if (copyin(arg1, &zs32, sizeof (zone_def32))) { 38990Sstevel@tonic-gate return (set_errno(EFAULT)); 39000Sstevel@tonic-gate } 39010Sstevel@tonic-gate zs.zone_name = 39020Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_name; 39030Sstevel@tonic-gate zs.zone_root = 39040Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_root; 39050Sstevel@tonic-gate zs.zone_privs = 39060Sstevel@tonic-gate (const struct priv_set *) 39070Sstevel@tonic-gate (unsigned long)zs32.zone_privs; 3908*1409Sdp zs.zone_privssz = zs32.zone_privssz; 39090Sstevel@tonic-gate zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 39100Sstevel@tonic-gate zs.rctlbufsz = zs32.rctlbufsz; 3911789Sahrens zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf; 3912789Sahrens zs.zfsbufsz = zs32.zfsbufsz; 39130Sstevel@tonic-gate zs.extended_error = 39140Sstevel@tonic-gate (int *)(unsigned long)zs32.extended_error; 39150Sstevel@tonic-gate #else 39160Sstevel@tonic-gate panic("get_udatamodel() returned bogus result\n"); 39170Sstevel@tonic-gate #endif 39180Sstevel@tonic-gate } 39190Sstevel@tonic-gate 39200Sstevel@tonic-gate return (zone_create(zs.zone_name, zs.zone_root, 3921813Sdp zs.zone_privs, zs.zone_privssz, 3922813Sdp (caddr_t)zs.rctlbuf, zs.rctlbufsz, 3923813Sdp (caddr_t)zs.zfsbuf, zs.zfsbufsz, 3924813Sdp zs.extended_error)); 39250Sstevel@tonic-gate case ZONE_BOOT: 39260Sstevel@tonic-gate return (zone_boot((zoneid_t)(uintptr_t)arg1, 39270Sstevel@tonic-gate (const char *)arg2)); 39280Sstevel@tonic-gate case ZONE_DESTROY: 39290Sstevel@tonic-gate return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 39300Sstevel@tonic-gate case ZONE_GETATTR: 39310Sstevel@tonic-gate return (zone_getattr((zoneid_t)(uintptr_t)arg1, 39320Sstevel@tonic-gate (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 39330Sstevel@tonic-gate case ZONE_ENTER: 39340Sstevel@tonic-gate return (zone_enter((zoneid_t)(uintptr_t)arg1)); 39350Sstevel@tonic-gate case ZONE_LIST: 39360Sstevel@tonic-gate return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 39370Sstevel@tonic-gate case ZONE_SHUTDOWN: 39380Sstevel@tonic-gate return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 39390Sstevel@tonic-gate case ZONE_LOOKUP: 39400Sstevel@tonic-gate return (zone_lookup((const char *)arg1)); 3941813Sdp case ZONE_VERSION: 3942813Sdp return (zone_version((int *)arg1)); 39430Sstevel@tonic-gate default: 39440Sstevel@tonic-gate return (set_errno(EINVAL)); 39450Sstevel@tonic-gate } 39460Sstevel@tonic-gate } 39470Sstevel@tonic-gate 39480Sstevel@tonic-gate struct zarg { 39490Sstevel@tonic-gate zone_t *zone; 39500Sstevel@tonic-gate zone_cmd_arg_t arg; 39510Sstevel@tonic-gate }; 39520Sstevel@tonic-gate 39530Sstevel@tonic-gate static int 39540Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp) 39550Sstevel@tonic-gate { 39560Sstevel@tonic-gate char *buf; 39570Sstevel@tonic-gate size_t buflen; 39580Sstevel@tonic-gate int error; 39590Sstevel@tonic-gate 39600Sstevel@tonic-gate buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 39610Sstevel@tonic-gate buf = kmem_alloc(buflen, KM_SLEEP); 39620Sstevel@tonic-gate (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 39630Sstevel@tonic-gate error = door_ki_open(buf, doorp); 39640Sstevel@tonic-gate kmem_free(buf, buflen); 39650Sstevel@tonic-gate return (error); 39660Sstevel@tonic-gate } 39670Sstevel@tonic-gate 39680Sstevel@tonic-gate static void 39690Sstevel@tonic-gate zone_release_door(door_handle_t *doorp) 39700Sstevel@tonic-gate { 39710Sstevel@tonic-gate door_ki_rele(*doorp); 39720Sstevel@tonic-gate *doorp = NULL; 39730Sstevel@tonic-gate } 39740Sstevel@tonic-gate 39750Sstevel@tonic-gate static void 39760Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp) 39770Sstevel@tonic-gate { 39780Sstevel@tonic-gate door_handle_t door = NULL; 39790Sstevel@tonic-gate door_arg_t darg, save_arg; 39800Sstevel@tonic-gate char *zone_name; 39810Sstevel@tonic-gate size_t zone_namelen; 39820Sstevel@tonic-gate zoneid_t zoneid; 39830Sstevel@tonic-gate zone_t *zone; 39840Sstevel@tonic-gate zone_cmd_arg_t arg; 39850Sstevel@tonic-gate uint64_t uniqid; 39860Sstevel@tonic-gate size_t size; 39870Sstevel@tonic-gate int error; 39880Sstevel@tonic-gate int retry; 39890Sstevel@tonic-gate 39900Sstevel@tonic-gate zone = zargp->zone; 39910Sstevel@tonic-gate arg = zargp->arg; 39920Sstevel@tonic-gate kmem_free(zargp, sizeof (*zargp)); 39930Sstevel@tonic-gate 39940Sstevel@tonic-gate zone_namelen = strlen(zone->zone_name) + 1; 39950Sstevel@tonic-gate zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 39960Sstevel@tonic-gate bcopy(zone->zone_name, zone_name, zone_namelen); 39970Sstevel@tonic-gate zoneid = zone->zone_id; 39980Sstevel@tonic-gate uniqid = zone->zone_uniqid; 39990Sstevel@tonic-gate /* 40000Sstevel@tonic-gate * zoneadmd may be down, but at least we can empty out the zone. 40010Sstevel@tonic-gate * We can ignore the return value of zone_empty() since we're called 40020Sstevel@tonic-gate * from a kernel thread and know we won't be delivered any signals. 40030Sstevel@tonic-gate */ 40040Sstevel@tonic-gate ASSERT(curproc == &p0); 40050Sstevel@tonic-gate (void) zone_empty(zone); 40060Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 40070Sstevel@tonic-gate zone_rele(zone); 40080Sstevel@tonic-gate 40090Sstevel@tonic-gate size = sizeof (arg); 40100Sstevel@tonic-gate darg.rbuf = (char *)&arg; 40110Sstevel@tonic-gate darg.data_ptr = (char *)&arg; 40120Sstevel@tonic-gate darg.rsize = size; 40130Sstevel@tonic-gate darg.data_size = size; 40140Sstevel@tonic-gate darg.desc_ptr = NULL; 40150Sstevel@tonic-gate darg.desc_num = 0; 40160Sstevel@tonic-gate 40170Sstevel@tonic-gate save_arg = darg; 40180Sstevel@tonic-gate /* 40190Sstevel@tonic-gate * Since we're not holding a reference to the zone, any number of 40200Sstevel@tonic-gate * things can go wrong, including the zone disappearing before we get a 40210Sstevel@tonic-gate * chance to talk to zoneadmd. 40220Sstevel@tonic-gate */ 40230Sstevel@tonic-gate for (retry = 0; /* forever */; retry++) { 40240Sstevel@tonic-gate if (door == NULL && 40250Sstevel@tonic-gate (error = zone_lookup_door(zone_name, &door)) != 0) { 40260Sstevel@tonic-gate goto next; 40270Sstevel@tonic-gate } 40280Sstevel@tonic-gate ASSERT(door != NULL); 40290Sstevel@tonic-gate 40300Sstevel@tonic-gate if ((error = door_ki_upcall(door, &darg)) == 0) { 40310Sstevel@tonic-gate break; 40320Sstevel@tonic-gate } 40330Sstevel@tonic-gate switch (error) { 40340Sstevel@tonic-gate case EINTR: 40350Sstevel@tonic-gate /* FALLTHROUGH */ 40360Sstevel@tonic-gate case EAGAIN: /* process may be forking */ 40370Sstevel@tonic-gate /* 40380Sstevel@tonic-gate * Back off for a bit 40390Sstevel@tonic-gate */ 40400Sstevel@tonic-gate break; 40410Sstevel@tonic-gate case EBADF: 40420Sstevel@tonic-gate zone_release_door(&door); 40430Sstevel@tonic-gate if (zone_lookup_door(zone_name, &door) != 0) { 40440Sstevel@tonic-gate /* 40450Sstevel@tonic-gate * zoneadmd may be dead, but it may come back to 40460Sstevel@tonic-gate * life later. 40470Sstevel@tonic-gate */ 40480Sstevel@tonic-gate break; 40490Sstevel@tonic-gate } 40500Sstevel@tonic-gate break; 40510Sstevel@tonic-gate default: 40520Sstevel@tonic-gate cmn_err(CE_WARN, 40530Sstevel@tonic-gate "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 40540Sstevel@tonic-gate error); 40550Sstevel@tonic-gate goto out; 40560Sstevel@tonic-gate } 40570Sstevel@tonic-gate next: 40580Sstevel@tonic-gate /* 40590Sstevel@tonic-gate * If this isn't the same zone_t that we originally had in mind, 40600Sstevel@tonic-gate * then this is the same as if two kadmin requests come in at 40610Sstevel@tonic-gate * the same time: the first one wins. This means we lose, so we 40620Sstevel@tonic-gate * bail. 40630Sstevel@tonic-gate */ 40640Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) { 40650Sstevel@tonic-gate /* 40660Sstevel@tonic-gate * Problem is solved. 40670Sstevel@tonic-gate */ 40680Sstevel@tonic-gate break; 40690Sstevel@tonic-gate } 40700Sstevel@tonic-gate if (zone->zone_uniqid != uniqid) { 40710Sstevel@tonic-gate /* 40720Sstevel@tonic-gate * zoneid recycled 40730Sstevel@tonic-gate */ 40740Sstevel@tonic-gate zone_rele(zone); 40750Sstevel@tonic-gate break; 40760Sstevel@tonic-gate } 40770Sstevel@tonic-gate /* 40780Sstevel@tonic-gate * We could zone_status_timedwait(), but there doesn't seem to 40790Sstevel@tonic-gate * be much point in doing that (plus, it would mean that 40800Sstevel@tonic-gate * zone_free() isn't called until this thread exits). 40810Sstevel@tonic-gate */ 40820Sstevel@tonic-gate zone_rele(zone); 40830Sstevel@tonic-gate delay(hz); 40840Sstevel@tonic-gate darg = save_arg; 40850Sstevel@tonic-gate } 40860Sstevel@tonic-gate out: 40870Sstevel@tonic-gate if (door != NULL) { 40880Sstevel@tonic-gate zone_release_door(&door); 40890Sstevel@tonic-gate } 40900Sstevel@tonic-gate kmem_free(zone_name, zone_namelen); 40910Sstevel@tonic-gate thread_exit(); 40920Sstevel@tonic-gate } 40930Sstevel@tonic-gate 40940Sstevel@tonic-gate /* 40950Sstevel@tonic-gate * Entry point for uadmin() to tell the zone to go away or reboot. The caller 40960Sstevel@tonic-gate * is a process in the zone to be modified. 40970Sstevel@tonic-gate * 40980Sstevel@tonic-gate * In order to shutdown the zone, we will hand off control to zoneadmd 40990Sstevel@tonic-gate * (running in the global zone) via a door. We do a half-hearted job at 41000Sstevel@tonic-gate * killing all processes in the zone, create a kernel thread to contact 41010Sstevel@tonic-gate * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 41020Sstevel@tonic-gate * a form of generation number used to let zoneadmd (as well as 41030Sstevel@tonic-gate * zone_destroy()) know exactly which zone they're re talking about. 41040Sstevel@tonic-gate */ 41050Sstevel@tonic-gate int 41060Sstevel@tonic-gate zone_uadmin(int cmd, int fcn, cred_t *credp) 41070Sstevel@tonic-gate { 41080Sstevel@tonic-gate struct zarg *zargp; 41090Sstevel@tonic-gate zone_cmd_t zcmd; 41100Sstevel@tonic-gate zone_t *zone; 41110Sstevel@tonic-gate 41120Sstevel@tonic-gate zone = curproc->p_zone; 41130Sstevel@tonic-gate ASSERT(getzoneid() != GLOBAL_ZONEID); 41140Sstevel@tonic-gate 41150Sstevel@tonic-gate switch (cmd) { 41160Sstevel@tonic-gate case A_SHUTDOWN: 41170Sstevel@tonic-gate switch (fcn) { 41180Sstevel@tonic-gate case AD_HALT: 41190Sstevel@tonic-gate case AD_POWEROFF: 41200Sstevel@tonic-gate zcmd = Z_HALT; 41210Sstevel@tonic-gate break; 41220Sstevel@tonic-gate case AD_BOOT: 41230Sstevel@tonic-gate zcmd = Z_REBOOT; 41240Sstevel@tonic-gate break; 41250Sstevel@tonic-gate case AD_IBOOT: 41260Sstevel@tonic-gate case AD_SBOOT: 41270Sstevel@tonic-gate case AD_SIBOOT: 41280Sstevel@tonic-gate case AD_NOSYNC: 41290Sstevel@tonic-gate return (ENOTSUP); 41300Sstevel@tonic-gate default: 41310Sstevel@tonic-gate return (EINVAL); 41320Sstevel@tonic-gate } 41330Sstevel@tonic-gate break; 41340Sstevel@tonic-gate case A_REBOOT: 41350Sstevel@tonic-gate zcmd = Z_REBOOT; 41360Sstevel@tonic-gate break; 41370Sstevel@tonic-gate case A_FTRACE: 41380Sstevel@tonic-gate case A_REMOUNT: 41390Sstevel@tonic-gate case A_FREEZE: 41400Sstevel@tonic-gate case A_DUMP: 41410Sstevel@tonic-gate return (ENOTSUP); 41420Sstevel@tonic-gate default: 41430Sstevel@tonic-gate ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 41440Sstevel@tonic-gate return (EINVAL); 41450Sstevel@tonic-gate } 41460Sstevel@tonic-gate 41470Sstevel@tonic-gate if (secpolicy_zone_admin(credp, B_FALSE)) 41480Sstevel@tonic-gate return (EPERM); 41490Sstevel@tonic-gate mutex_enter(&zone_status_lock); 41500Sstevel@tonic-gate /* 41510Sstevel@tonic-gate * zone_status can't be ZONE_IS_EMPTY or higher since curproc 41520Sstevel@tonic-gate * is in the zone. 41530Sstevel@tonic-gate */ 41540Sstevel@tonic-gate ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 41550Sstevel@tonic-gate if (zone_status_get(zone) > ZONE_IS_RUNNING) { 41560Sstevel@tonic-gate /* 41570Sstevel@tonic-gate * This zone is already on its way down. 41580Sstevel@tonic-gate */ 41590Sstevel@tonic-gate mutex_exit(&zone_status_lock); 41600Sstevel@tonic-gate return (0); 41610Sstevel@tonic-gate } 41620Sstevel@tonic-gate /* 41630Sstevel@tonic-gate * Prevent future zone_enter()s 41640Sstevel@tonic-gate */ 41650Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 41660Sstevel@tonic-gate mutex_exit(&zone_status_lock); 41670Sstevel@tonic-gate 41680Sstevel@tonic-gate /* 41690Sstevel@tonic-gate * Kill everyone now and call zoneadmd later. 41700Sstevel@tonic-gate * zone_ki_call_zoneadmd() will do a more thorough job of this 41710Sstevel@tonic-gate * later. 41720Sstevel@tonic-gate */ 41730Sstevel@tonic-gate killall(zone->zone_id); 41740Sstevel@tonic-gate /* 41750Sstevel@tonic-gate * Now, create the thread to contact zoneadmd and do the rest of the 41760Sstevel@tonic-gate * work. This thread can't be created in our zone otherwise 41770Sstevel@tonic-gate * zone_destroy() would deadlock. 41780Sstevel@tonic-gate */ 41790Sstevel@tonic-gate zargp = kmem_alloc(sizeof (*zargp), KM_SLEEP); 41800Sstevel@tonic-gate zargp->arg.cmd = zcmd; 41810Sstevel@tonic-gate zargp->arg.uniqid = zone->zone_uniqid; 41820Sstevel@tonic-gate (void) strcpy(zargp->arg.locale, "C"); 41830Sstevel@tonic-gate zone_hold(zargp->zone = zone); 41840Sstevel@tonic-gate 41850Sstevel@tonic-gate (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 41860Sstevel@tonic-gate TS_RUN, minclsyspri); 41870Sstevel@tonic-gate exit(CLD_EXITED, 0); 41880Sstevel@tonic-gate 41890Sstevel@tonic-gate return (EINVAL); 41900Sstevel@tonic-gate } 41910Sstevel@tonic-gate 41920Sstevel@tonic-gate /* 41930Sstevel@tonic-gate * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 41940Sstevel@tonic-gate * status to ZONE_IS_SHUTTING_DOWN. 41950Sstevel@tonic-gate */ 41960Sstevel@tonic-gate void 41970Sstevel@tonic-gate zone_shutdown_global(void) 41980Sstevel@tonic-gate { 41990Sstevel@tonic-gate ASSERT(curproc->p_zone == global_zone); 42000Sstevel@tonic-gate 42010Sstevel@tonic-gate mutex_enter(&zone_status_lock); 42020Sstevel@tonic-gate ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 42030Sstevel@tonic-gate zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 42040Sstevel@tonic-gate mutex_exit(&zone_status_lock); 42050Sstevel@tonic-gate } 4206789Sahrens 4207789Sahrens /* 4208789Sahrens * Returns true if the named dataset is visible in the current zone. 4209789Sahrens * The 'write' parameter is set to 1 if the dataset is also writable. 4210789Sahrens */ 4211789Sahrens int 4212789Sahrens zone_dataset_visible(const char *dataset, int *write) 4213789Sahrens { 4214789Sahrens zone_dataset_t *zd; 4215789Sahrens size_t len; 4216789Sahrens zone_t *zone = curproc->p_zone; 4217789Sahrens 4218789Sahrens if (dataset[0] == '\0') 4219789Sahrens return (0); 4220789Sahrens 4221789Sahrens /* 4222789Sahrens * Walk the list once, looking for datasets which match exactly, or 4223789Sahrens * specify a dataset underneath an exported dataset. If found, return 4224789Sahrens * true and note that it is writable. 4225789Sahrens */ 4226789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 4227789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 4228789Sahrens 4229789Sahrens len = strlen(zd->zd_dataset); 4230789Sahrens if (strlen(dataset) >= len && 4231789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 4232816Smaybee (dataset[len] == '\0' || dataset[len] == '/' || 4233816Smaybee dataset[len] == '@')) { 4234789Sahrens if (write) 4235789Sahrens *write = 1; 4236789Sahrens return (1); 4237789Sahrens } 4238789Sahrens } 4239789Sahrens 4240789Sahrens /* 4241789Sahrens * Walk the list a second time, searching for datasets which are parents 4242789Sahrens * of exported datasets. These should be visible, but read-only. 4243789Sahrens * 4244789Sahrens * Note that we also have to support forms such as 'pool/dataset/', with 4245789Sahrens * a trailing slash. 4246789Sahrens */ 4247789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 4248789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 4249789Sahrens 4250789Sahrens len = strlen(dataset); 4251789Sahrens if (dataset[len - 1] == '/') 4252789Sahrens len--; /* Ignore trailing slash */ 4253789Sahrens if (len < strlen(zd->zd_dataset) && 4254789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 4255789Sahrens zd->zd_dataset[len] == '/') { 4256789Sahrens if (write) 4257789Sahrens *write = 0; 4258789Sahrens return (1); 4259789Sahrens } 4260789Sahrens } 4261789Sahrens 4262789Sahrens return (0); 4263789Sahrens } 4264