10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51676Sjpk * Common Development and Distribution License (the "License"). 61676Sjpk * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21390Sraf 220Sstevel@tonic-gate /* 231409Sdp * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * Zones 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * A zone is a named collection of processes, namespace constraints, 330Sstevel@tonic-gate * and other system resources which comprise a secure and manageable 340Sstevel@tonic-gate * application containment facility. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Zones (represented by the reference counted zone_t) are tracked in 370Sstevel@tonic-gate * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs 380Sstevel@tonic-gate * (zoneid_t) are used to track zone association. Zone IDs are 390Sstevel@tonic-gate * dynamically generated when the zone is created; if a persistent 400Sstevel@tonic-gate * identifier is needed (core files, accounting logs, audit trail, 410Sstevel@tonic-gate * etc.), the zone name should be used. 420Sstevel@tonic-gate * 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * Global Zone: 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * The global zone (zoneid 0) is automatically associated with all 470Sstevel@tonic-gate * system resources that have not been bound to a user-created zone. 480Sstevel@tonic-gate * This means that even systems where zones are not in active use 490Sstevel@tonic-gate * have a global zone, and all processes, mounts, etc. are 500Sstevel@tonic-gate * associated with that zone. The global zone is generally 510Sstevel@tonic-gate * unconstrained in terms of privileges and access, though the usual 520Sstevel@tonic-gate * credential and privilege based restrictions apply. 530Sstevel@tonic-gate * 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * Zone States: 560Sstevel@tonic-gate * 570Sstevel@tonic-gate * The states in which a zone may be in and the transitions are as 580Sstevel@tonic-gate * follows: 590Sstevel@tonic-gate * 600Sstevel@tonic-gate * ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially 610Sstevel@tonic-gate * initialized zone is added to the list of active zones on the system but 620Sstevel@tonic-gate * isn't accessible. 630Sstevel@tonic-gate * 640Sstevel@tonic-gate * ZONE_IS_READY: zsched (the kernel dummy process for a zone) is 650Sstevel@tonic-gate * ready. The zone is made visible after the ZSD constructor callbacks are 660Sstevel@tonic-gate * executed. A zone remains in this state until it transitions into 670Sstevel@tonic-gate * the ZONE_IS_BOOTING state as a result of a call to zone_boot(). 680Sstevel@tonic-gate * 690Sstevel@tonic-gate * ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start 700Sstevel@tonic-gate * init. Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN 710Sstevel@tonic-gate * state. 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * ZONE_IS_RUNNING: The zone is open for business: zsched has 740Sstevel@tonic-gate * successfully started init. A zone remains in this state until 750Sstevel@tonic-gate * zone_shutdown() is called. 760Sstevel@tonic-gate * 770Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is 780Sstevel@tonic-gate * killing all processes running in the zone. The zone remains 790Sstevel@tonic-gate * in this state until there are no more user processes running in the zone. 800Sstevel@tonic-gate * zone_create(), zone_enter(), and zone_destroy() on this zone will fail. 810Sstevel@tonic-gate * Since zone_shutdown() is restartable, it may be called successfully 820Sstevel@tonic-gate * multiple times for the same zone_t. Setting of the zone's state to 830Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check 840Sstevel@tonic-gate * the zone's status without worrying about it being a moving target. 850Sstevel@tonic-gate * 860Sstevel@tonic-gate * ZONE_IS_EMPTY: zone_shutdown() has been called, and there 870Sstevel@tonic-gate * are no more user processes in the zone. The zone remains in this 880Sstevel@tonic-gate * state until there are no more kernel threads associated with the 890Sstevel@tonic-gate * zone. zone_create(), zone_enter(), and zone_destroy() on this zone will 900Sstevel@tonic-gate * fail. 910Sstevel@tonic-gate * 920Sstevel@tonic-gate * ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone 930Sstevel@tonic-gate * have exited. zone_shutdown() returns. Henceforth it is not possible to 940Sstevel@tonic-gate * join the zone or create kernel threads therein. 950Sstevel@tonic-gate * 960Sstevel@tonic-gate * ZONE_IS_DYING: zone_destroy() has been called on the zone; zone 970Sstevel@tonic-gate * remains in this state until zsched exits. Calls to zone_find_by_*() 980Sstevel@tonic-gate * return NULL from now on. 990Sstevel@tonic-gate * 1000Sstevel@tonic-gate * ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0). There are no 1010Sstevel@tonic-gate * processes or threads doing work on behalf of the zone. The zone is 1020Sstevel@tonic-gate * removed from the list of active zones. zone_destroy() returns, and 1030Sstevel@tonic-gate * the zone can be recreated. 1040Sstevel@tonic-gate * 1050Sstevel@tonic-gate * ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor 1060Sstevel@tonic-gate * callbacks are executed, and all memory associated with the zone is 1070Sstevel@tonic-gate * freed. 1080Sstevel@tonic-gate * 1090Sstevel@tonic-gate * Threads can wait for the zone to enter a requested state by using 1100Sstevel@tonic-gate * zone_status_wait() or zone_status_timedwait() with the desired 1110Sstevel@tonic-gate * state passed in as an argument. Zone state transitions are 1120Sstevel@tonic-gate * uni-directional; it is not possible to move back to an earlier state. 1130Sstevel@tonic-gate * 1140Sstevel@tonic-gate * 1150Sstevel@tonic-gate * Zone-Specific Data: 1160Sstevel@tonic-gate * 1170Sstevel@tonic-gate * Subsystems needing to maintain zone-specific data can store that 1180Sstevel@tonic-gate * data using the ZSD mechanism. This provides a zone-specific data 1190Sstevel@tonic-gate * store, similar to thread-specific data (see pthread_getspecific(3C) 1200Sstevel@tonic-gate * or the TSD code in uts/common/disp/thread.c. Also, ZSD can be used 1210Sstevel@tonic-gate * to register callbacks to be invoked when a zone is created, shut 1220Sstevel@tonic-gate * down, or destroyed. This can be used to initialize zone-specific 1230Sstevel@tonic-gate * data for new zones and to clean up when zones go away. 1240Sstevel@tonic-gate * 1250Sstevel@tonic-gate * 1260Sstevel@tonic-gate * Data Structures: 1270Sstevel@tonic-gate * 1280Sstevel@tonic-gate * The per-zone structure (zone_t) is reference counted, and freed 1290Sstevel@tonic-gate * when all references are released. zone_hold and zone_rele can be 1300Sstevel@tonic-gate * used to adjust the reference count. In addition, reference counts 1310Sstevel@tonic-gate * associated with the cred_t structure are tracked separately using 1320Sstevel@tonic-gate * zone_cred_hold and zone_cred_rele. 1330Sstevel@tonic-gate * 1340Sstevel@tonic-gate * Pointers to active zone_t's are stored in two hash tables; one 1350Sstevel@tonic-gate * for searching by id, the other for searching by name. Lookups 1360Sstevel@tonic-gate * can be performed on either basis, using zone_find_by_id and 1370Sstevel@tonic-gate * zone_find_by_name. Both return zone_t pointers with the zone 1380Sstevel@tonic-gate * held, so zone_rele should be called when the pointer is no longer 1390Sstevel@tonic-gate * needed. Zones can also be searched by path; zone_find_by_path 1400Sstevel@tonic-gate * returns the zone with which a path name is associated (global 1410Sstevel@tonic-gate * zone if the path is not within some other zone's file system 1420Sstevel@tonic-gate * hierarchy). This currently requires iterating through each zone, 1430Sstevel@tonic-gate * so it is slower than an id or name search via a hash table. 1440Sstevel@tonic-gate * 1450Sstevel@tonic-gate * 1460Sstevel@tonic-gate * Locking: 1470Sstevel@tonic-gate * 1480Sstevel@tonic-gate * zonehash_lock: This is a top-level global lock used to protect the 1490Sstevel@tonic-gate * zone hash tables and lists. Zones cannot be created or destroyed 1500Sstevel@tonic-gate * while this lock is held. 1510Sstevel@tonic-gate * zone_status_lock: This is a global lock protecting zone state. 1520Sstevel@tonic-gate * Zones cannot change state while this lock is held. It also 1530Sstevel@tonic-gate * protects the list of kernel threads associated with a zone. 1540Sstevel@tonic-gate * zone_lock: This is a per-zone lock used to protect several fields of 1550Sstevel@tonic-gate * the zone_t (see <sys/zone.h> for details). In addition, holding 1560Sstevel@tonic-gate * this lock means that the zone cannot go away. 157*3247Sgjelinek * zone_nlwps_lock: This is a per-zone lock used to protect the fields 158*3247Sgjelinek * related to the zone.max-lwps rctl. 159*3247Sgjelinek * zone_mem_lock: This is a per-zone lock used to protect the fields 160*3247Sgjelinek * related to the zone.max-locked-memory and zone.max-swap rctls. 1610Sstevel@tonic-gate * zsd_key_lock: This is a global lock protecting the key state for ZSD. 1620Sstevel@tonic-gate * zone_deathrow_lock: This is a global lock protecting the "deathrow" 1630Sstevel@tonic-gate * list (a list of zones in the ZONE_IS_DEAD state). 1640Sstevel@tonic-gate * 1650Sstevel@tonic-gate * Ordering requirements: 1660Sstevel@tonic-gate * pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock --> 1670Sstevel@tonic-gate * zone_lock --> zsd_key_lock --> pidlock --> p_lock 1680Sstevel@tonic-gate * 169*3247Sgjelinek * When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is: 170*3247Sgjelinek * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 171*3247Sgjelinek * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 172*3247Sgjelinek * 1730Sstevel@tonic-gate * Blocking memory allocations are permitted while holding any of the 1740Sstevel@tonic-gate * zone locks. 1750Sstevel@tonic-gate * 1760Sstevel@tonic-gate * 1770Sstevel@tonic-gate * System Call Interface: 1780Sstevel@tonic-gate * 1790Sstevel@tonic-gate * The zone subsystem can be managed and queried from user level with 1800Sstevel@tonic-gate * the following system calls (all subcodes of the primary "zone" 1810Sstevel@tonic-gate * system call): 1820Sstevel@tonic-gate * - zone_create: creates a zone with selected attributes (name, 183789Sahrens * root path, privileges, resource controls, ZFS datasets) 1840Sstevel@tonic-gate * - zone_enter: allows the current process to enter a zone 1850Sstevel@tonic-gate * - zone_getattr: reports attributes of a zone 1862267Sdp * - zone_setattr: set attributes of a zone 1872267Sdp * - zone_boot: set 'init' running for the zone 1880Sstevel@tonic-gate * - zone_list: lists all zones active in the system 1890Sstevel@tonic-gate * - zone_lookup: looks up zone id based on name 1900Sstevel@tonic-gate * - zone_shutdown: initiates shutdown process (see states above) 1910Sstevel@tonic-gate * - zone_destroy: completes shutdown process (see states above) 1920Sstevel@tonic-gate * 1930Sstevel@tonic-gate */ 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate #include <sys/priv_impl.h> 1960Sstevel@tonic-gate #include <sys/cred.h> 1970Sstevel@tonic-gate #include <c2/audit.h> 1980Sstevel@tonic-gate #include <sys/debug.h> 1990Sstevel@tonic-gate #include <sys/file.h> 2000Sstevel@tonic-gate #include <sys/kmem.h> 201*3247Sgjelinek #include <sys/kstat.h> 2020Sstevel@tonic-gate #include <sys/mutex.h> 2031676Sjpk #include <sys/note.h> 2040Sstevel@tonic-gate #include <sys/pathname.h> 2050Sstevel@tonic-gate #include <sys/proc.h> 2060Sstevel@tonic-gate #include <sys/project.h> 2071166Sdstaff #include <sys/sysevent.h> 2080Sstevel@tonic-gate #include <sys/task.h> 2090Sstevel@tonic-gate #include <sys/systm.h> 2100Sstevel@tonic-gate #include <sys/types.h> 2110Sstevel@tonic-gate #include <sys/utsname.h> 2120Sstevel@tonic-gate #include <sys/vnode.h> 2130Sstevel@tonic-gate #include <sys/vfs.h> 2140Sstevel@tonic-gate #include <sys/systeminfo.h> 2150Sstevel@tonic-gate #include <sys/policy.h> 2160Sstevel@tonic-gate #include <sys/cred_impl.h> 2170Sstevel@tonic-gate #include <sys/contract_impl.h> 2180Sstevel@tonic-gate #include <sys/contract/process_impl.h> 2190Sstevel@tonic-gate #include <sys/class.h> 2200Sstevel@tonic-gate #include <sys/pool.h> 2210Sstevel@tonic-gate #include <sys/pool_pset.h> 2220Sstevel@tonic-gate #include <sys/pset.h> 2230Sstevel@tonic-gate #include <sys/sysmacros.h> 2240Sstevel@tonic-gate #include <sys/callb.h> 2250Sstevel@tonic-gate #include <sys/vmparam.h> 2260Sstevel@tonic-gate #include <sys/corectl.h> 2272677Sml93401 #include <sys/ipc_impl.h> 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate #include <sys/door.h> 2300Sstevel@tonic-gate #include <sys/cpuvar.h> 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate #include <sys/uadmin.h> 2330Sstevel@tonic-gate #include <sys/session.h> 2340Sstevel@tonic-gate #include <sys/cmn_err.h> 2350Sstevel@tonic-gate #include <sys/modhash.h> 2362267Sdp #include <sys/sunddi.h> 2370Sstevel@tonic-gate #include <sys/nvpair.h> 2380Sstevel@tonic-gate #include <sys/rctl.h> 2390Sstevel@tonic-gate #include <sys/fss.h> 2402712Snn35248 #include <sys/brand.h> 2410Sstevel@tonic-gate #include <sys/zone.h> 2421676Sjpk #include <sys/tsol/label.h> 2430Sstevel@tonic-gate 244*3247Sgjelinek #include <vm/seg.h> 245*3247Sgjelinek 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * cv used to signal that all references to the zone have been released. This 2480Sstevel@tonic-gate * needs to be global since there may be multiple waiters, and the first to 2490Sstevel@tonic-gate * wake up will free the zone_t, hence we cannot use zone->zone_cv. 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate static kcondvar_t zone_destroy_cv; 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * Lock used to serialize access to zone_cv. This could have been per-zone, 2540Sstevel@tonic-gate * but then we'd need another lock for zone_destroy_cv, and why bother? 2550Sstevel@tonic-gate */ 2560Sstevel@tonic-gate static kmutex_t zone_status_lock; 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * ZSD-related global variables. 2600Sstevel@tonic-gate */ 2610Sstevel@tonic-gate static kmutex_t zsd_key_lock; /* protects the following two */ 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 2640Sstevel@tonic-gate */ 2650Sstevel@tonic-gate static zone_key_t zsd_keyval = 0; 2660Sstevel@tonic-gate /* 2670Sstevel@tonic-gate * Global list of registered keys. We use this when a new zone is created. 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate static list_t zsd_registered_keys; 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate int zone_hash_size = 256; 2721676Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel; 2730Sstevel@tonic-gate static kmutex_t zonehash_lock; 2740Sstevel@tonic-gate static uint_t zonecount; 2750Sstevel@tonic-gate static id_space_t *zoneid_space; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 2790Sstevel@tonic-gate * kernel proper runs, and which manages all other zones. 2800Sstevel@tonic-gate * 2810Sstevel@tonic-gate * Although not declared as static, the variable "zone0" should not be used 2820Sstevel@tonic-gate * except for by code that needs to reference the global zone early on in boot, 2830Sstevel@tonic-gate * before it is fully initialized. All other consumers should use 2840Sstevel@tonic-gate * 'global_zone'. 2850Sstevel@tonic-gate */ 2860Sstevel@tonic-gate zone_t zone0; 2870Sstevel@tonic-gate zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate /* 2900Sstevel@tonic-gate * List of active zones, protected by zonehash_lock. 2910Sstevel@tonic-gate */ 2920Sstevel@tonic-gate static list_t zone_active; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * List of destroyed zones that still have outstanding cred references. 2960Sstevel@tonic-gate * Used for debugging. Uses a separate lock to avoid lock ordering 2970Sstevel@tonic-gate * problems in zone_free. 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate static list_t zone_deathrow; 3000Sstevel@tonic-gate static kmutex_t zone_deathrow_lock; 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */ 3030Sstevel@tonic-gate uint_t maxzones = 8192; 3040Sstevel@tonic-gate 3051166Sdstaff /* Event channel to sent zone state change notifications */ 3061166Sdstaff evchan_t *zone_event_chan; 3071166Sdstaff 3081166Sdstaff /* 3091166Sdstaff * This table holds the mapping from kernel zone states to 3101166Sdstaff * states visible in the state notification API. 3111166Sdstaff * The idea is that we only expose "obvious" states and 3121166Sdstaff * do not expose states which are just implementation details. 3131166Sdstaff */ 3141166Sdstaff const char *zone_status_table[] = { 3151166Sdstaff ZONE_EVENT_UNINITIALIZED, /* uninitialized */ 3161166Sdstaff ZONE_EVENT_READY, /* ready */ 3171166Sdstaff ZONE_EVENT_READY, /* booting */ 3181166Sdstaff ZONE_EVENT_RUNNING, /* running */ 3191166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* shutting_down */ 3201166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* empty */ 3211166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* down */ 3221166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* dying */ 3231166Sdstaff ZONE_EVENT_UNINITIALIZED, /* dead */ 3241166Sdstaff }; 3251166Sdstaff 3260Sstevel@tonic-gate /* 3270Sstevel@tonic-gate * This isn't static so lint doesn't complain. 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares; 3302768Ssl108498 rctl_hndl_t rc_zone_locked_mem; 331*3247Sgjelinek rctl_hndl_t rc_zone_max_swap; 3320Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps; 3332677Sml93401 rctl_hndl_t rc_zone_shmmax; 3342677Sml93401 rctl_hndl_t rc_zone_shmmni; 3352677Sml93401 rctl_hndl_t rc_zone_semmni; 3362677Sml93401 rctl_hndl_t rc_zone_msgmni; 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * Synchronization primitives used to synchronize between mounts and zone 3390Sstevel@tonic-gate * creation/destruction. 3400Sstevel@tonic-gate */ 3410Sstevel@tonic-gate static int mounts_in_progress; 3420Sstevel@tonic-gate static kcondvar_t mount_cv; 3430Sstevel@tonic-gate static kmutex_t mount_lock; 3440Sstevel@tonic-gate 3452267Sdp const char * const zone_default_initname = "/sbin/init"; 3461676Sjpk static char * const zone_prefix = "/zone/"; 3470Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid); 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate /* 350813Sdp * Bump this number when you alter the zone syscall interfaces; this is 351813Sdp * because we need to have support for previous API versions in libc 352813Sdp * to support patching; libc calls into the kernel to determine this number. 353813Sdp * 354813Sdp * Version 1 of the API is the version originally shipped with Solaris 10 355813Sdp * Version 2 alters the zone_create system call in order to support more 356813Sdp * arguments by moving the args into a structure; and to do better 357813Sdp * error reporting when zone_create() fails. 358813Sdp * Version 3 alters the zone_create system call in order to support the 359813Sdp * import of ZFS datasets to zones. 3601676Sjpk * Version 4 alters the zone_create system call in order to support 3611676Sjpk * Trusted Extensions. 3622267Sdp * Version 5 alters the zone_boot system call, and converts its old 3632267Sdp * bootargs parameter to be set by the zone_setattr API instead. 364813Sdp */ 3652267Sdp static const int ZONE_SYSCALL_API_VERSION = 5; 366813Sdp 367813Sdp /* 3680Sstevel@tonic-gate * Certain filesystems (such as NFS and autofs) need to know which zone 3690Sstevel@tonic-gate * the mount is being placed in. Because of this, we need to be able to 3700Sstevel@tonic-gate * ensure that a zone isn't in the process of being created such that 3710Sstevel@tonic-gate * nfs_mount() thinks it is in the global zone, while by the time it 3720Sstevel@tonic-gate * gets added the list of mounted zones, it ends up on zoneA's mount 3730Sstevel@tonic-gate * list. 3740Sstevel@tonic-gate * 3750Sstevel@tonic-gate * The following functions: block_mounts()/resume_mounts() and 3760Sstevel@tonic-gate * mount_in_progress()/mount_completed() are used by zones and the VFS 3770Sstevel@tonic-gate * layer (respectively) to synchronize zone creation and new mounts. 3780Sstevel@tonic-gate * 3790Sstevel@tonic-gate * The semantics are like a reader-reader lock such that there may 3800Sstevel@tonic-gate * either be multiple mounts (or zone creations, if that weren't 3810Sstevel@tonic-gate * serialized by zonehash_lock) in progress at the same time, but not 3820Sstevel@tonic-gate * both. 3830Sstevel@tonic-gate * 3840Sstevel@tonic-gate * We use cv's so the user can ctrl-C out of the operation if it's 3850Sstevel@tonic-gate * taking too long. 3860Sstevel@tonic-gate * 3870Sstevel@tonic-gate * The semantics are such that there is unfair bias towards the 3880Sstevel@tonic-gate * "current" operation. This means that zone creations may starve if 3890Sstevel@tonic-gate * there is a rapid succession of new mounts coming in to the system, or 3900Sstevel@tonic-gate * there is a remote possibility that zones will be created at such a 3910Sstevel@tonic-gate * rate that new mounts will not be able to proceed. 3920Sstevel@tonic-gate */ 3930Sstevel@tonic-gate /* 3940Sstevel@tonic-gate * Prevent new mounts from progressing to the point of calling 3950Sstevel@tonic-gate * VFS_MOUNT(). If there are already mounts in this "region", wait for 3960Sstevel@tonic-gate * them to complete. 3970Sstevel@tonic-gate */ 3980Sstevel@tonic-gate static int 3990Sstevel@tonic-gate block_mounts(void) 4000Sstevel@tonic-gate { 4010Sstevel@tonic-gate int retval = 0; 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate /* 4040Sstevel@tonic-gate * Since it may block for a long time, block_mounts() shouldn't be 4050Sstevel@tonic-gate * called with zonehash_lock held. 4060Sstevel@tonic-gate */ 4070Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 4080Sstevel@tonic-gate mutex_enter(&mount_lock); 4090Sstevel@tonic-gate while (mounts_in_progress > 0) { 4100Sstevel@tonic-gate if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 4110Sstevel@tonic-gate goto signaled; 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate /* 4140Sstevel@tonic-gate * A negative value of mounts_in_progress indicates that mounts 4150Sstevel@tonic-gate * have been blocked by (-mounts_in_progress) different callers. 4160Sstevel@tonic-gate */ 4170Sstevel@tonic-gate mounts_in_progress--; 4180Sstevel@tonic-gate retval = 1; 4190Sstevel@tonic-gate signaled: 4200Sstevel@tonic-gate mutex_exit(&mount_lock); 4210Sstevel@tonic-gate return (retval); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate /* 4250Sstevel@tonic-gate * The VFS layer may progress with new mounts as far as we're concerned. 4260Sstevel@tonic-gate * Allow them to progress if we were the last obstacle. 4270Sstevel@tonic-gate */ 4280Sstevel@tonic-gate static void 4290Sstevel@tonic-gate resume_mounts(void) 4300Sstevel@tonic-gate { 4310Sstevel@tonic-gate mutex_enter(&mount_lock); 4320Sstevel@tonic-gate if (++mounts_in_progress == 0) 4330Sstevel@tonic-gate cv_broadcast(&mount_cv); 4340Sstevel@tonic-gate mutex_exit(&mount_lock); 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate /* 4380Sstevel@tonic-gate * The VFS layer is busy with a mount; zones should wait until all 4390Sstevel@tonic-gate * mounts are completed to progress. 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate void 4420Sstevel@tonic-gate mount_in_progress(void) 4430Sstevel@tonic-gate { 4440Sstevel@tonic-gate mutex_enter(&mount_lock); 4450Sstevel@tonic-gate while (mounts_in_progress < 0) 4460Sstevel@tonic-gate cv_wait(&mount_cv, &mount_lock); 4470Sstevel@tonic-gate mounts_in_progress++; 4480Sstevel@tonic-gate mutex_exit(&mount_lock); 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* 4520Sstevel@tonic-gate * VFS is done with one mount; wake up any waiting block_mounts() 4530Sstevel@tonic-gate * callers if this is the last mount. 4540Sstevel@tonic-gate */ 4550Sstevel@tonic-gate void 4560Sstevel@tonic-gate mount_completed(void) 4570Sstevel@tonic-gate { 4580Sstevel@tonic-gate mutex_enter(&mount_lock); 4590Sstevel@tonic-gate if (--mounts_in_progress == 0) 4600Sstevel@tonic-gate cv_broadcast(&mount_cv); 4610Sstevel@tonic-gate mutex_exit(&mount_lock); 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate /* 4650Sstevel@tonic-gate * ZSD routines. 4660Sstevel@tonic-gate * 4670Sstevel@tonic-gate * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 4680Sstevel@tonic-gate * defined by the pthread_key_create() and related interfaces. 4690Sstevel@tonic-gate * 4700Sstevel@tonic-gate * Kernel subsystems may register one or more data items and/or 4710Sstevel@tonic-gate * callbacks to be executed when a zone is created, shutdown, or 4720Sstevel@tonic-gate * destroyed. 4730Sstevel@tonic-gate * 4740Sstevel@tonic-gate * Unlike the thread counterpart, destructor callbacks will be executed 4750Sstevel@tonic-gate * even if the data pointer is NULL and/or there are no constructor 4760Sstevel@tonic-gate * callbacks, so it is the responsibility of such callbacks to check for 4770Sstevel@tonic-gate * NULL data values if necessary. 4780Sstevel@tonic-gate * 4790Sstevel@tonic-gate * The locking strategy and overall picture is as follows: 4800Sstevel@tonic-gate * 4810Sstevel@tonic-gate * When someone calls zone_key_create(), a template ZSD entry is added to the 4820Sstevel@tonic-gate * global list "zsd_registered_keys", protected by zsd_key_lock. The 4830Sstevel@tonic-gate * constructor callback is called immediately on all existing zones, and a 4840Sstevel@tonic-gate * copy of the ZSD entry added to the per-zone zone_zsd list (protected by 4850Sstevel@tonic-gate * zone_lock). As this operation requires the list of zones, the list of 4860Sstevel@tonic-gate * registered keys, and the per-zone list of ZSD entries to remain constant 4870Sstevel@tonic-gate * throughout the entire operation, it must grab zonehash_lock, zone_lock for 4880Sstevel@tonic-gate * all existing zones, and zsd_key_lock, in that order. Similar locking is 4890Sstevel@tonic-gate * needed when zone_key_delete() is called. It is thus sufficient to hold 4900Sstevel@tonic-gate * zsd_key_lock *or* zone_lock to prevent additions to or removals from the 4910Sstevel@tonic-gate * per-zone zone_zsd list. 4920Sstevel@tonic-gate * 4930Sstevel@tonic-gate * Note that this implementation does not make a copy of the ZSD entry if a 4940Sstevel@tonic-gate * constructor callback is not provided. A zone_getspecific() on such an 4950Sstevel@tonic-gate * uninitialized ZSD entry will return NULL. 4960Sstevel@tonic-gate * 4970Sstevel@tonic-gate * When new zones are created constructor callbacks for all registered ZSD 4980Sstevel@tonic-gate * entries will be called. 4990Sstevel@tonic-gate * 5000Sstevel@tonic-gate * The framework does not provide any locking around zone_getspecific() and 5010Sstevel@tonic-gate * zone_setspecific() apart from that needed for internal consistency, so 5020Sstevel@tonic-gate * callers interested in atomic "test-and-set" semantics will need to provide 5030Sstevel@tonic-gate * their own locking. 5040Sstevel@tonic-gate */ 5050Sstevel@tonic-gate void 5060Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 5070Sstevel@tonic-gate void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 5080Sstevel@tonic-gate { 5090Sstevel@tonic-gate struct zsd_entry *zsdp; 5100Sstevel@tonic-gate struct zsd_entry *t; 5110Sstevel@tonic-gate struct zone *zone; 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP); 5140Sstevel@tonic-gate zsdp->zsd_data = NULL; 5150Sstevel@tonic-gate zsdp->zsd_create = create; 5160Sstevel@tonic-gate zsdp->zsd_shutdown = shutdown; 5170Sstevel@tonic-gate zsdp->zsd_destroy = destroy; 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* stop the world */ 5200Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5210Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5220Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5250Sstevel@tonic-gate *keyp = zsdp->zsd_key = ++zsd_keyval; 5260Sstevel@tonic-gate ASSERT(zsd_keyval != 0); 5270Sstevel@tonic-gate list_insert_tail(&zsd_registered_keys, zsdp); 5280Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate if (create != NULL) { 5310Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5320Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 5330Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 5340Sstevel@tonic-gate t->zsd_key = *keyp; 5350Sstevel@tonic-gate t->zsd_data = (*create)(zone->zone_id); 5360Sstevel@tonic-gate t->zsd_create = create; 5370Sstevel@tonic-gate t->zsd_shutdown = shutdown; 5380Sstevel@tonic-gate t->zsd_destroy = destroy; 5390Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5430Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5440Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 5450Sstevel@tonic-gate mutex_exit(&zonehash_lock); 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate /* 5490Sstevel@tonic-gate * Helper function to find the zsd_entry associated with the key in the 5500Sstevel@tonic-gate * given list. 5510Sstevel@tonic-gate */ 5520Sstevel@tonic-gate static struct zsd_entry * 5530Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key) 5540Sstevel@tonic-gate { 5550Sstevel@tonic-gate struct zsd_entry *zsd; 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 5580Sstevel@tonic-gate if (zsd->zsd_key == key) { 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * Move to head of list to keep list in MRU order. 5610Sstevel@tonic-gate */ 5620Sstevel@tonic-gate if (zsd != list_head(l)) { 5630Sstevel@tonic-gate list_remove(l, zsd); 5640Sstevel@tonic-gate list_insert_head(l, zsd); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate return (zsd); 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate return (NULL); 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * Function called when a module is being unloaded, or otherwise wishes 5740Sstevel@tonic-gate * to unregister its ZSD key and callbacks. 5750Sstevel@tonic-gate */ 5760Sstevel@tonic-gate int 5770Sstevel@tonic-gate zone_key_delete(zone_key_t key) 5780Sstevel@tonic-gate { 5790Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 5800Sstevel@tonic-gate zone_t *zone; 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* Zone create/delete waits for us */ 5830Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5840Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 5850Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 5880Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 5890Sstevel@tonic-gate if (zsdp == NULL) 5900Sstevel@tonic-gate goto notfound; 5910Sstevel@tonic-gate list_remove(&zsd_registered_keys, zsdp); 5920Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 5950Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 5960Sstevel@tonic-gate struct zsd_entry *del; 5970Sstevel@tonic-gate void *data; 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate if (!(zone->zone_flags & ZF_DESTROYED)) { 6000Sstevel@tonic-gate del = zsd_find(&zone->zone_zsd, key); 6010Sstevel@tonic-gate if (del != NULL) { 6020Sstevel@tonic-gate data = del->zsd_data; 6030Sstevel@tonic-gate ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 6040Sstevel@tonic-gate ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 6050Sstevel@tonic-gate list_remove(&zone->zone_zsd, del); 6060Sstevel@tonic-gate kmem_free(del, sizeof (*del)); 6070Sstevel@tonic-gate } else { 6080Sstevel@tonic-gate data = NULL; 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate if (zsdp->zsd_shutdown) 6110Sstevel@tonic-gate zsdp->zsd_shutdown(zone->zone_id, data); 6120Sstevel@tonic-gate if (zsdp->zsd_destroy) 6130Sstevel@tonic-gate zsdp->zsd_destroy(zone->zone_id, data); 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate mutex_exit(&zonehash_lock); 6180Sstevel@tonic-gate kmem_free(zsdp, sizeof (*zsdp)); 6190Sstevel@tonic-gate return (0); 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate notfound: 6220Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6230Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 6240Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 6250Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6260Sstevel@tonic-gate mutex_exit(&zonehash_lock); 6270Sstevel@tonic-gate return (-1); 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * ZSD counterpart of pthread_setspecific(). 6320Sstevel@tonic-gate */ 6330Sstevel@tonic-gate int 6340Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 6350Sstevel@tonic-gate { 6360Sstevel@tonic-gate struct zsd_entry *t; 6370Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6400Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6410Sstevel@tonic-gate if (t != NULL) { 6420Sstevel@tonic-gate /* 6430Sstevel@tonic-gate * Replace old value with new 6440Sstevel@tonic-gate */ 6450Sstevel@tonic-gate t->zsd_data = (void *)data; 6460Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6470Sstevel@tonic-gate return (0); 6480Sstevel@tonic-gate } 6490Sstevel@tonic-gate /* 6500Sstevel@tonic-gate * If there was no previous value, go through the list of registered 6510Sstevel@tonic-gate * keys. 6520Sstevel@tonic-gate * 6530Sstevel@tonic-gate * We avoid grabbing zsd_key_lock until we are sure we need it; this is 6540Sstevel@tonic-gate * necessary for shutdown callbacks to be able to execute without fear 6550Sstevel@tonic-gate * of deadlock. 6560Sstevel@tonic-gate */ 6570Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 6580Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 6590Sstevel@tonic-gate if (zsdp == NULL) { /* Key was not registered */ 6600Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6610Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6620Sstevel@tonic-gate return (-1); 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate /* 6660Sstevel@tonic-gate * Add a zsd_entry to this zone, using the template we just retrieved 6670Sstevel@tonic-gate * to initialize the constructor and destructor(s). 6680Sstevel@tonic-gate */ 6690Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 6700Sstevel@tonic-gate t->zsd_key = key; 6710Sstevel@tonic-gate t->zsd_data = (void *)data; 6720Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 6730Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 6740Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 6750Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 6760Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6770Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6780Sstevel@tonic-gate return (0); 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate /* 6820Sstevel@tonic-gate * ZSD counterpart of pthread_getspecific(). 6830Sstevel@tonic-gate */ 6840Sstevel@tonic-gate void * 6850Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone) 6860Sstevel@tonic-gate { 6870Sstevel@tonic-gate struct zsd_entry *t; 6880Sstevel@tonic-gate void *data; 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 6910Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 6920Sstevel@tonic-gate data = (t == NULL ? NULL : t->zsd_data); 6930Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 6940Sstevel@tonic-gate return (data); 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate /* 6980Sstevel@tonic-gate * Function used to initialize a zone's list of ZSD callbacks and data 6990Sstevel@tonic-gate * when the zone is being created. The callbacks are initialized from 7000Sstevel@tonic-gate * the template list (zsd_registered_keys), and the constructor 7010Sstevel@tonic-gate * callback executed (if one exists). 7020Sstevel@tonic-gate * 7030Sstevel@tonic-gate * This is called before the zone is made publicly available, hence no 7040Sstevel@tonic-gate * need to grab zone_lock. 7050Sstevel@tonic-gate * 7060Sstevel@tonic-gate * Although we grab and release zsd_key_lock, new entries cannot be 7070Sstevel@tonic-gate * added to or removed from the zsd_registered_keys list until we 7080Sstevel@tonic-gate * release zonehash_lock, so there isn't a window for a 7090Sstevel@tonic-gate * zone_key_create() to come in after we've dropped zsd_key_lock but 7100Sstevel@tonic-gate * before the zone is added to the zone list, such that the constructor 7110Sstevel@tonic-gate * callbacks aren't executed for the new zone. 7120Sstevel@tonic-gate */ 7130Sstevel@tonic-gate static void 7140Sstevel@tonic-gate zone_zsd_configure(zone_t *zone) 7150Sstevel@tonic-gate { 7160Sstevel@tonic-gate struct zsd_entry *zsdp; 7170Sstevel@tonic-gate struct zsd_entry *t; 7180Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 7210Sstevel@tonic-gate ASSERT(list_head(&zone->zone_zsd) == NULL); 7220Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7230Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7240Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7250Sstevel@tonic-gate if (zsdp->zsd_create != NULL) { 7260Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 7270Sstevel@tonic-gate t->zsd_key = zsdp->zsd_key; 7280Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 7290Sstevel@tonic-gate t->zsd_data = (*t->zsd_create)(zoneid); 7300Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 7310Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 7320Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate /* 7410Sstevel@tonic-gate * Helper function to execute shutdown or destructor callbacks. 7420Sstevel@tonic-gate */ 7430Sstevel@tonic-gate static void 7440Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 7450Sstevel@tonic-gate { 7460Sstevel@tonic-gate struct zsd_entry *zsdp; 7470Sstevel@tonic-gate struct zsd_entry *t; 7480Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 7510Sstevel@tonic-gate ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 7520Sstevel@tonic-gate ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 7550Sstevel@tonic-gate if (ct == ZSD_DESTROY) { 7560Sstevel@tonic-gate if (zone->zone_flags & ZF_DESTROYED) { 7570Sstevel@tonic-gate /* 7580Sstevel@tonic-gate * Make sure destructors are only called once. 7590Sstevel@tonic-gate */ 7600Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7610Sstevel@tonic-gate return; 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate zone->zone_flags |= ZF_DESTROYED; 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate /* 7680Sstevel@tonic-gate * Both zsd_key_lock and zone_lock need to be held in order to add or 7690Sstevel@tonic-gate * remove a ZSD key, (either globally as part of 7700Sstevel@tonic-gate * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is 7710Sstevel@tonic-gate * possible through zone_setspecific()), so it's sufficient to hold 7720Sstevel@tonic-gate * zsd_key_lock here. 7730Sstevel@tonic-gate * 7740Sstevel@tonic-gate * This is a good thing, since we don't want to recursively try to grab 7750Sstevel@tonic-gate * zone_lock if a callback attempts to do something like a crfree() or 7760Sstevel@tonic-gate * zone_rele(). 7770Sstevel@tonic-gate */ 7780Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 7790Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 7800Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 7810Sstevel@tonic-gate zone_key_t key = zsdp->zsd_key; 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate /* Skip if no callbacks registered */ 7840Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL) 7850Sstevel@tonic-gate continue; 7860Sstevel@tonic-gate if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL) 7870Sstevel@tonic-gate continue; 7880Sstevel@tonic-gate /* 7890Sstevel@tonic-gate * Call the callback with the zone-specific data if we can find 7900Sstevel@tonic-gate * any, otherwise with NULL. 7910Sstevel@tonic-gate */ 7920Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 7930Sstevel@tonic-gate if (t != NULL) { 7940Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 7950Sstevel@tonic-gate t->zsd_shutdown(zoneid, t->zsd_data); 7960Sstevel@tonic-gate } else { 7970Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 7980Sstevel@tonic-gate t->zsd_destroy(zoneid, t->zsd_data); 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate } else { 8010Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 8020Sstevel@tonic-gate zsdp->zsd_shutdown(zoneid, NULL); 8030Sstevel@tonic-gate } else { 8040Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 8050Sstevel@tonic-gate zsdp->zsd_destroy(zoneid, NULL); 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * Called when the zone is going away; free ZSD-related memory, and 8140Sstevel@tonic-gate * destroy the zone_zsd list. 8150Sstevel@tonic-gate */ 8160Sstevel@tonic-gate static void 8170Sstevel@tonic-gate zone_free_zsd(zone_t *zone) 8180Sstevel@tonic-gate { 8190Sstevel@tonic-gate struct zsd_entry *t, *next; 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate /* 8220Sstevel@tonic-gate * Free all the zsd_entry's we had on this zone. 8230Sstevel@tonic-gate */ 8240Sstevel@tonic-gate for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 8250Sstevel@tonic-gate next = list_next(&zone->zone_zsd, t); 8260Sstevel@tonic-gate list_remove(&zone->zone_zsd, t); 8270Sstevel@tonic-gate kmem_free(t, sizeof (*t)); 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate list_destroy(&zone->zone_zsd); 8300Sstevel@tonic-gate } 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate /* 833789Sahrens * Frees memory associated with the zone dataset list. 834789Sahrens */ 835789Sahrens static void 836789Sahrens zone_free_datasets(zone_t *zone) 837789Sahrens { 838789Sahrens zone_dataset_t *t, *next; 839789Sahrens 840789Sahrens for (t = list_head(&zone->zone_datasets); t != NULL; t = next) { 841789Sahrens next = list_next(&zone->zone_datasets, t); 842789Sahrens list_remove(&zone->zone_datasets, t); 843789Sahrens kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1); 844789Sahrens kmem_free(t, sizeof (*t)); 845789Sahrens } 846789Sahrens list_destroy(&zone->zone_datasets); 847789Sahrens } 848789Sahrens 849789Sahrens /* 8500Sstevel@tonic-gate * zone.cpu-shares resource control support. 8510Sstevel@tonic-gate */ 8520Sstevel@tonic-gate /*ARGSUSED*/ 8530Sstevel@tonic-gate static rctl_qty_t 8540Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 8550Sstevel@tonic-gate { 8560Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8570Sstevel@tonic-gate return (p->p_zone->zone_shares); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate /*ARGSUSED*/ 8610Sstevel@tonic-gate static int 8620Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 8630Sstevel@tonic-gate rctl_qty_t nv) 8640Sstevel@tonic-gate { 8650Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8660Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 8670Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 8680Sstevel@tonic-gate return (0); 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate e->rcep_p.zone->zone_shares = nv; 8710Sstevel@tonic-gate return (0); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = { 8750Sstevel@tonic-gate rcop_no_action, 8760Sstevel@tonic-gate zone_cpu_shares_usage, 8770Sstevel@tonic-gate zone_cpu_shares_set, 8780Sstevel@tonic-gate rcop_no_test 8790Sstevel@tonic-gate }; 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate /*ARGSUSED*/ 8820Sstevel@tonic-gate static rctl_qty_t 8830Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p) 8840Sstevel@tonic-gate { 8850Sstevel@tonic-gate rctl_qty_t nlwps; 8860Sstevel@tonic-gate zone_t *zone = p->p_zone; 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 8910Sstevel@tonic-gate nlwps = zone->zone_nlwps; 8920Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate return (nlwps); 8950Sstevel@tonic-gate } 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate /*ARGSUSED*/ 8980Sstevel@tonic-gate static int 8990Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 9000Sstevel@tonic-gate rctl_qty_t incr, uint_t flags) 9010Sstevel@tonic-gate { 9020Sstevel@tonic-gate rctl_qty_t nlwps; 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 9050Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 9060Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 9070Sstevel@tonic-gate return (0); 9080Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 9090Sstevel@tonic-gate nlwps = e->rcep_p.zone->zone_nlwps; 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate if (nlwps + incr > rcntl->rcv_value) 9120Sstevel@tonic-gate return (1); 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate return (0); 9150Sstevel@tonic-gate } 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate /*ARGSUSED*/ 9180Sstevel@tonic-gate static int 9192768Ssl108498 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) 9202768Ssl108498 { 9210Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 9220Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 9230Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 9240Sstevel@tonic-gate return (0); 9250Sstevel@tonic-gate e->rcep_p.zone->zone_nlwps_ctl = nv; 9260Sstevel@tonic-gate return (0); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = { 9300Sstevel@tonic-gate rcop_no_action, 9310Sstevel@tonic-gate zone_lwps_usage, 9320Sstevel@tonic-gate zone_lwps_set, 9330Sstevel@tonic-gate zone_lwps_test, 9340Sstevel@tonic-gate }; 9350Sstevel@tonic-gate 9362677Sml93401 /*ARGSUSED*/ 9372677Sml93401 static int 9382677Sml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 9392677Sml93401 rctl_qty_t incr, uint_t flags) 9402677Sml93401 { 9412677Sml93401 rctl_qty_t v; 9422677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 9432677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 9442677Sml93401 v = e->rcep_p.zone->zone_shmmax + incr; 9452677Sml93401 if (v > rval->rcv_value) 9462677Sml93401 return (1); 9472677Sml93401 return (0); 9482677Sml93401 } 9492677Sml93401 9502677Sml93401 static rctl_ops_t zone_shmmax_ops = { 9512677Sml93401 rcop_no_action, 9522677Sml93401 rcop_no_usage, 9532677Sml93401 rcop_no_set, 9542677Sml93401 zone_shmmax_test 9552677Sml93401 }; 9562677Sml93401 9572677Sml93401 /*ARGSUSED*/ 9582677Sml93401 static int 9592677Sml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 9602677Sml93401 rctl_qty_t incr, uint_t flags) 9612677Sml93401 { 9622677Sml93401 rctl_qty_t v; 9632677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 9642677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 9652677Sml93401 v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr; 9662677Sml93401 if (v > rval->rcv_value) 9672677Sml93401 return (1); 9682677Sml93401 return (0); 9692677Sml93401 } 9702677Sml93401 9712677Sml93401 static rctl_ops_t zone_shmmni_ops = { 9722677Sml93401 rcop_no_action, 9732677Sml93401 rcop_no_usage, 9742677Sml93401 rcop_no_set, 9752677Sml93401 zone_shmmni_test 9762677Sml93401 }; 9772677Sml93401 9782677Sml93401 /*ARGSUSED*/ 9792677Sml93401 static int 9802677Sml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 9812677Sml93401 rctl_qty_t incr, uint_t flags) 9822677Sml93401 { 9832677Sml93401 rctl_qty_t v; 9842677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 9852677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 9862677Sml93401 v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr; 9872677Sml93401 if (v > rval->rcv_value) 9882677Sml93401 return (1); 9892677Sml93401 return (0); 9902677Sml93401 } 9912677Sml93401 9922677Sml93401 static rctl_ops_t zone_semmni_ops = { 9932677Sml93401 rcop_no_action, 9942677Sml93401 rcop_no_usage, 9952677Sml93401 rcop_no_set, 9962677Sml93401 zone_semmni_test 9972677Sml93401 }; 9982677Sml93401 9992677Sml93401 /*ARGSUSED*/ 10002677Sml93401 static int 10012677Sml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 10022677Sml93401 rctl_qty_t incr, uint_t flags) 10032677Sml93401 { 10042677Sml93401 rctl_qty_t v; 10052677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 10062677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 10072677Sml93401 v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr; 10082677Sml93401 if (v > rval->rcv_value) 10092677Sml93401 return (1); 10102677Sml93401 return (0); 10112677Sml93401 } 10122677Sml93401 10132677Sml93401 static rctl_ops_t zone_msgmni_ops = { 10142677Sml93401 rcop_no_action, 10152677Sml93401 rcop_no_usage, 10162677Sml93401 rcop_no_set, 10172677Sml93401 zone_msgmni_test 10182677Sml93401 }; 10192677Sml93401 10202768Ssl108498 /*ARGSUSED*/ 10212768Ssl108498 static rctl_qty_t 10222768Ssl108498 zone_locked_mem_usage(rctl_t *rctl, struct proc *p) 10232768Ssl108498 { 10242768Ssl108498 rctl_qty_t q; 10252768Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 1026*3247Sgjelinek mutex_enter(&p->p_zone->zone_mem_lock); 10272768Ssl108498 q = p->p_zone->zone_locked_mem; 1028*3247Sgjelinek mutex_exit(&p->p_zone->zone_mem_lock); 10292768Ssl108498 return (q); 10302768Ssl108498 } 10312768Ssl108498 10322768Ssl108498 /*ARGSUSED*/ 10332768Ssl108498 static int 10342768Ssl108498 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 10352768Ssl108498 rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 10362768Ssl108498 { 10372768Ssl108498 rctl_qty_t q; 1038*3247Sgjelinek zone_t *z; 1039*3247Sgjelinek 1040*3247Sgjelinek z = e->rcep_p.zone; 10412768Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 1042*3247Sgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 1043*3247Sgjelinek q = z->zone_locked_mem; 10442768Ssl108498 if (q + incr > rcntl->rcv_value) 10452768Ssl108498 return (1); 10462768Ssl108498 return (0); 10472768Ssl108498 } 10482768Ssl108498 10492768Ssl108498 /*ARGSUSED*/ 10502768Ssl108498 static int 10512768Ssl108498 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 10522768Ssl108498 rctl_qty_t nv) 10532768Ssl108498 { 10542768Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 10552768Ssl108498 ASSERT(e->rcep_t == RCENTITY_ZONE); 10562768Ssl108498 if (e->rcep_p.zone == NULL) 10572768Ssl108498 return (0); 10582768Ssl108498 e->rcep_p.zone->zone_locked_mem_ctl = nv; 10592768Ssl108498 return (0); 10602768Ssl108498 } 10612768Ssl108498 10622768Ssl108498 static rctl_ops_t zone_locked_mem_ops = { 10632768Ssl108498 rcop_no_action, 10642768Ssl108498 zone_locked_mem_usage, 10652768Ssl108498 zone_locked_mem_set, 10662768Ssl108498 zone_locked_mem_test 10672768Ssl108498 }; 10682677Sml93401 1069*3247Sgjelinek /*ARGSUSED*/ 1070*3247Sgjelinek static rctl_qty_t 1071*3247Sgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p) 1072*3247Sgjelinek { 1073*3247Sgjelinek rctl_qty_t q; 1074*3247Sgjelinek zone_t *z = p->p_zone; 1075*3247Sgjelinek 1076*3247Sgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 1077*3247Sgjelinek mutex_enter(&z->zone_mem_lock); 1078*3247Sgjelinek q = z->zone_max_swap; 1079*3247Sgjelinek mutex_exit(&z->zone_mem_lock); 1080*3247Sgjelinek return (q); 1081*3247Sgjelinek } 1082*3247Sgjelinek 1083*3247Sgjelinek /*ARGSUSED*/ 1084*3247Sgjelinek static int 1085*3247Sgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 1086*3247Sgjelinek rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 1087*3247Sgjelinek { 1088*3247Sgjelinek rctl_qty_t q; 1089*3247Sgjelinek zone_t *z; 1090*3247Sgjelinek 1091*3247Sgjelinek z = e->rcep_p.zone; 1092*3247Sgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 1093*3247Sgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 1094*3247Sgjelinek q = z->zone_max_swap; 1095*3247Sgjelinek if (q + incr > rcntl->rcv_value) 1096*3247Sgjelinek return (1); 1097*3247Sgjelinek return (0); 1098*3247Sgjelinek } 1099*3247Sgjelinek 1100*3247Sgjelinek /*ARGSUSED*/ 1101*3247Sgjelinek static int 1102*3247Sgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1103*3247Sgjelinek rctl_qty_t nv) 1104*3247Sgjelinek { 1105*3247Sgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 1106*3247Sgjelinek ASSERT(e->rcep_t == RCENTITY_ZONE); 1107*3247Sgjelinek if (e->rcep_p.zone == NULL) 1108*3247Sgjelinek return (0); 1109*3247Sgjelinek e->rcep_p.zone->zone_max_swap_ctl = nv; 1110*3247Sgjelinek return (0); 1111*3247Sgjelinek } 1112*3247Sgjelinek 1113*3247Sgjelinek static rctl_ops_t zone_max_swap_ops = { 1114*3247Sgjelinek rcop_no_action, 1115*3247Sgjelinek zone_max_swap_usage, 1116*3247Sgjelinek zone_max_swap_set, 1117*3247Sgjelinek zone_max_swap_test 1118*3247Sgjelinek }; 1119*3247Sgjelinek 11200Sstevel@tonic-gate /* 11210Sstevel@tonic-gate * Helper function to brand the zone with a unique ID. 11220Sstevel@tonic-gate */ 11230Sstevel@tonic-gate static void 11240Sstevel@tonic-gate zone_uniqid(zone_t *zone) 11250Sstevel@tonic-gate { 11260Sstevel@tonic-gate static uint64_t uniqid = 0; 11270Sstevel@tonic-gate 11280Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 11290Sstevel@tonic-gate zone->zone_uniqid = uniqid++; 11300Sstevel@tonic-gate } 11310Sstevel@tonic-gate 11320Sstevel@tonic-gate /* 11330Sstevel@tonic-gate * Returns a held pointer to the "kcred" for the specified zone. 11340Sstevel@tonic-gate */ 11350Sstevel@tonic-gate struct cred * 11360Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid) 11370Sstevel@tonic-gate { 11380Sstevel@tonic-gate zone_t *zone; 11390Sstevel@tonic-gate cred_t *cr; 11400Sstevel@tonic-gate 11410Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) 11420Sstevel@tonic-gate return (NULL); 11430Sstevel@tonic-gate cr = zone->zone_kcred; 11440Sstevel@tonic-gate crhold(cr); 11450Sstevel@tonic-gate zone_rele(zone); 11460Sstevel@tonic-gate return (cr); 11470Sstevel@tonic-gate } 11480Sstevel@tonic-gate 1149*3247Sgjelinek static int 1150*3247Sgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw) 1151*3247Sgjelinek { 1152*3247Sgjelinek zone_t *zone = ksp->ks_private; 1153*3247Sgjelinek zone_kstat_t *zk = ksp->ks_data; 1154*3247Sgjelinek 1155*3247Sgjelinek if (rw == KSTAT_WRITE) 1156*3247Sgjelinek return (EACCES); 1157*3247Sgjelinek 1158*3247Sgjelinek zk->zk_usage.value.ui64 = zone->zone_locked_mem; 1159*3247Sgjelinek zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl; 1160*3247Sgjelinek return (0); 1161*3247Sgjelinek } 1162*3247Sgjelinek 1163*3247Sgjelinek static int 1164*3247Sgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw) 1165*3247Sgjelinek { 1166*3247Sgjelinek zone_t *zone = ksp->ks_private; 1167*3247Sgjelinek zone_kstat_t *zk = ksp->ks_data; 1168*3247Sgjelinek 1169*3247Sgjelinek if (rw == KSTAT_WRITE) 1170*3247Sgjelinek return (EACCES); 1171*3247Sgjelinek 1172*3247Sgjelinek zk->zk_usage.value.ui64 = zone->zone_max_swap; 1173*3247Sgjelinek zk->zk_value.value.ui64 = zone->zone_max_swap_ctl; 1174*3247Sgjelinek return (0); 1175*3247Sgjelinek } 1176*3247Sgjelinek 1177*3247Sgjelinek static void 1178*3247Sgjelinek zone_kstat_create(zone_t *zone) 1179*3247Sgjelinek { 1180*3247Sgjelinek kstat_t *ksp; 1181*3247Sgjelinek zone_kstat_t *zk; 1182*3247Sgjelinek 1183*3247Sgjelinek ksp = rctl_kstat_create_zone(zone, "lockedmem", KSTAT_TYPE_NAMED, 1184*3247Sgjelinek sizeof (zone_kstat_t) / sizeof (kstat_named_t), 1185*3247Sgjelinek KSTAT_FLAG_VIRTUAL); 1186*3247Sgjelinek 1187*3247Sgjelinek if (ksp == NULL) 1188*3247Sgjelinek return; 1189*3247Sgjelinek 1190*3247Sgjelinek zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 1191*3247Sgjelinek ksp->ks_data_size += strlen(zone->zone_name) + 1; 1192*3247Sgjelinek kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 1193*3247Sgjelinek kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 1194*3247Sgjelinek kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 1195*3247Sgjelinek kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 1196*3247Sgjelinek ksp->ks_update = zone_lockedmem_kstat_update; 1197*3247Sgjelinek ksp->ks_private = zone; 1198*3247Sgjelinek kstat_install(ksp); 1199*3247Sgjelinek 1200*3247Sgjelinek zone->zone_lockedmem_kstat = ksp; 1201*3247Sgjelinek 1202*3247Sgjelinek ksp = rctl_kstat_create_zone(zone, "swapresv", KSTAT_TYPE_NAMED, 1203*3247Sgjelinek sizeof (zone_kstat_t) / sizeof (kstat_named_t), 1204*3247Sgjelinek KSTAT_FLAG_VIRTUAL); 1205*3247Sgjelinek 1206*3247Sgjelinek if (ksp == NULL) 1207*3247Sgjelinek return; 1208*3247Sgjelinek 1209*3247Sgjelinek zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 1210*3247Sgjelinek ksp->ks_data_size += strlen(zone->zone_name) + 1; 1211*3247Sgjelinek kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 1212*3247Sgjelinek kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 1213*3247Sgjelinek kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 1214*3247Sgjelinek kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 1215*3247Sgjelinek ksp->ks_update = zone_swapresv_kstat_update; 1216*3247Sgjelinek ksp->ks_private = zone; 1217*3247Sgjelinek kstat_install(ksp); 1218*3247Sgjelinek 1219*3247Sgjelinek zone->zone_swapresv_kstat = ksp; 1220*3247Sgjelinek } 1221*3247Sgjelinek 1222*3247Sgjelinek static void 1223*3247Sgjelinek zone_kstat_delete(zone_t *zone) 1224*3247Sgjelinek { 1225*3247Sgjelinek void *data; 1226*3247Sgjelinek 1227*3247Sgjelinek if (zone->zone_lockedmem_kstat != NULL) { 1228*3247Sgjelinek data = zone->zone_lockedmem_kstat->ks_data; 1229*3247Sgjelinek kstat_delete(zone->zone_lockedmem_kstat); 1230*3247Sgjelinek kmem_free(data, sizeof (zone_kstat_t)); 1231*3247Sgjelinek } 1232*3247Sgjelinek if (zone->zone_swapresv_kstat != NULL) { 1233*3247Sgjelinek data = zone->zone_swapresv_kstat->ks_data; 1234*3247Sgjelinek kstat_delete(zone->zone_swapresv_kstat); 1235*3247Sgjelinek kmem_free(data, sizeof (zone_kstat_t)); 1236*3247Sgjelinek } 1237*3247Sgjelinek } 1238*3247Sgjelinek 12390Sstevel@tonic-gate /* 12400Sstevel@tonic-gate * Called very early on in boot to initialize the ZSD list so that 12410Sstevel@tonic-gate * zone_key_create() can be called before zone_init(). It also initializes 12420Sstevel@tonic-gate * portions of zone0 which may be used before zone_init() is called. The 12430Sstevel@tonic-gate * variable "global_zone" will be set when zone0 is fully initialized by 12440Sstevel@tonic-gate * zone_init(). 12450Sstevel@tonic-gate */ 12460Sstevel@tonic-gate void 12470Sstevel@tonic-gate zone_zsd_init(void) 12480Sstevel@tonic-gate { 12490Sstevel@tonic-gate mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 12500Sstevel@tonic-gate mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 12510Sstevel@tonic-gate list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 12520Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 12530Sstevel@tonic-gate list_create(&zone_active, sizeof (zone_t), 12540Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 12550Sstevel@tonic-gate list_create(&zone_deathrow, sizeof (zone_t), 12560Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 12570Sstevel@tonic-gate 12580Sstevel@tonic-gate mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 12590Sstevel@tonic-gate mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 1260*3247Sgjelinek mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 12610Sstevel@tonic-gate zone0.zone_shares = 1; 1262*3247Sgjelinek zone0.zone_nlwps = 0; 12630Sstevel@tonic-gate zone0.zone_nlwps_ctl = INT_MAX; 1264*3247Sgjelinek zone0.zone_locked_mem = 0; 1265*3247Sgjelinek zone0.zone_locked_mem_ctl = UINT64_MAX; 1266*3247Sgjelinek ASSERT(zone0.zone_max_swap == 0); 1267*3247Sgjelinek zone0.zone_max_swap_ctl = UINT64_MAX; 12682677Sml93401 zone0.zone_shmmax = 0; 12692677Sml93401 zone0.zone_ipc.ipcq_shmmni = 0; 12702677Sml93401 zone0.zone_ipc.ipcq_semmni = 0; 12712677Sml93401 zone0.zone_ipc.ipcq_msgmni = 0; 12720Sstevel@tonic-gate zone0.zone_name = GLOBAL_ZONENAME; 12730Sstevel@tonic-gate zone0.zone_nodename = utsname.nodename; 12740Sstevel@tonic-gate zone0.zone_domain = srpc_domain; 12750Sstevel@tonic-gate zone0.zone_ref = 1; 12760Sstevel@tonic-gate zone0.zone_id = GLOBAL_ZONEID; 12770Sstevel@tonic-gate zone0.zone_status = ZONE_IS_RUNNING; 12780Sstevel@tonic-gate zone0.zone_rootpath = "/"; 12790Sstevel@tonic-gate zone0.zone_rootpathlen = 2; 12800Sstevel@tonic-gate zone0.zone_psetid = ZONE_PS_INVAL; 12810Sstevel@tonic-gate zone0.zone_ncpus = 0; 12820Sstevel@tonic-gate zone0.zone_ncpus_online = 0; 12830Sstevel@tonic-gate zone0.zone_proc_initpid = 1; 12842267Sdp zone0.zone_initname = initname; 1285*3247Sgjelinek zone0.zone_lockedmem_kstat = NULL; 1286*3247Sgjelinek zone0.zone_swapresv_kstat = NULL; 12870Sstevel@tonic-gate list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 12880Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 12890Sstevel@tonic-gate list_insert_head(&zone_active, &zone0); 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate /* 12920Sstevel@tonic-gate * The root filesystem is not mounted yet, so zone_rootvp cannot be set 12930Sstevel@tonic-gate * to anything meaningful. It is assigned to be 'rootdir' in 12940Sstevel@tonic-gate * vfs_mountroot(). 12950Sstevel@tonic-gate */ 12960Sstevel@tonic-gate zone0.zone_rootvp = NULL; 12970Sstevel@tonic-gate zone0.zone_vfslist = NULL; 12982267Sdp zone0.zone_bootargs = initargs; 12990Sstevel@tonic-gate zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 13000Sstevel@tonic-gate /* 13010Sstevel@tonic-gate * The global zone has all privileges 13020Sstevel@tonic-gate */ 13030Sstevel@tonic-gate priv_fillset(zone0.zone_privset); 13040Sstevel@tonic-gate /* 13050Sstevel@tonic-gate * Add p0 to the global zone 13060Sstevel@tonic-gate */ 13070Sstevel@tonic-gate zone0.zone_zsched = &p0; 13080Sstevel@tonic-gate p0.p_zone = &zone0; 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate /* 13121676Sjpk * Compute a hash value based on the contents of the label and the DOI. The 13131676Sjpk * hash algorithm is somewhat arbitrary, but is based on the observation that 13141676Sjpk * humans will likely pick labels that differ by amounts that work out to be 13151676Sjpk * multiples of the number of hash chains, and thus stirring in some primes 13161676Sjpk * should help. 13171676Sjpk */ 13181676Sjpk static uint_t 13191676Sjpk hash_bylabel(void *hdata, mod_hash_key_t key) 13201676Sjpk { 13211676Sjpk const ts_label_t *lab = (ts_label_t *)key; 13221676Sjpk const uint32_t *up, *ue; 13231676Sjpk uint_t hash; 13241676Sjpk int i; 13251676Sjpk 13261676Sjpk _NOTE(ARGUNUSED(hdata)); 13271676Sjpk 13281676Sjpk hash = lab->tsl_doi + (lab->tsl_doi << 1); 13291676Sjpk /* we depend on alignment of label, but not representation */ 13301676Sjpk up = (const uint32_t *)&lab->tsl_label; 13311676Sjpk ue = up + sizeof (lab->tsl_label) / sizeof (*up); 13321676Sjpk i = 1; 13331676Sjpk while (up < ue) { 13341676Sjpk /* using 2^n + 1, 1 <= n <= 16 as source of many primes */ 13351676Sjpk hash += *up + (*up << ((i % 16) + 1)); 13361676Sjpk up++; 13371676Sjpk i++; 13381676Sjpk } 13391676Sjpk return (hash); 13401676Sjpk } 13411676Sjpk 13421676Sjpk /* 13431676Sjpk * All that mod_hash cares about here is zero (equal) versus non-zero (not 13441676Sjpk * equal). This may need to be changed if less than / greater than is ever 13451676Sjpk * needed. 13461676Sjpk */ 13471676Sjpk static int 13481676Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 13491676Sjpk { 13501676Sjpk ts_label_t *lab1 = (ts_label_t *)key1; 13511676Sjpk ts_label_t *lab2 = (ts_label_t *)key2; 13521676Sjpk 13531676Sjpk return (label_equal(lab1, lab2) ? 0 : 1); 13541676Sjpk } 13551676Sjpk 13561676Sjpk /* 13570Sstevel@tonic-gate * Called by main() to initialize the zones framework. 13580Sstevel@tonic-gate */ 13590Sstevel@tonic-gate void 13600Sstevel@tonic-gate zone_init(void) 13610Sstevel@tonic-gate { 13620Sstevel@tonic-gate rctl_dict_entry_t *rde; 13630Sstevel@tonic-gate rctl_val_t *dval; 13640Sstevel@tonic-gate rctl_set_t *set; 13650Sstevel@tonic-gate rctl_alloc_gp_t *gp; 13660Sstevel@tonic-gate rctl_entity_p_t e; 13671166Sdstaff int res; 13680Sstevel@tonic-gate 13690Sstevel@tonic-gate ASSERT(curproc == &p0); 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate /* 13720Sstevel@tonic-gate * Create ID space for zone IDs. ID 0 is reserved for the 13730Sstevel@tonic-gate * global zone. 13740Sstevel@tonic-gate */ 13750Sstevel@tonic-gate zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate /* 13780Sstevel@tonic-gate * Initialize generic zone resource controls, if any. 13790Sstevel@tonic-gate */ 13800Sstevel@tonic-gate rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 13810Sstevel@tonic-gate RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 13821996Sml93401 RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER, 13831996Sml93401 FSS_MAXSHARES, FSS_MAXSHARES, 13840Sstevel@tonic-gate &zone_cpu_shares_ops); 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 13870Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 13880Sstevel@tonic-gate INT_MAX, INT_MAX, &zone_lwps_ops); 13890Sstevel@tonic-gate /* 13902677Sml93401 * System V IPC resource controls 13912677Sml93401 */ 13922677Sml93401 rc_zone_msgmni = rctl_register("zone.max-msg-ids", 13932677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 13942677Sml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops); 13952677Sml93401 13962677Sml93401 rc_zone_semmni = rctl_register("zone.max-sem-ids", 13972677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 13982677Sml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops); 13992677Sml93401 14002677Sml93401 rc_zone_shmmni = rctl_register("zone.max-shm-ids", 14012677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 14022677Sml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops); 14032677Sml93401 14042677Sml93401 rc_zone_shmmax = rctl_register("zone.max-shm-memory", 14052677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 14062677Sml93401 RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops); 14072677Sml93401 14082677Sml93401 /* 14090Sstevel@tonic-gate * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 14100Sstevel@tonic-gate * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 14110Sstevel@tonic-gate */ 14120Sstevel@tonic-gate dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 14130Sstevel@tonic-gate bzero(dval, sizeof (rctl_val_t)); 14140Sstevel@tonic-gate dval->rcv_value = 1; 14150Sstevel@tonic-gate dval->rcv_privilege = RCPRIV_PRIVILEGED; 14160Sstevel@tonic-gate dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 14170Sstevel@tonic-gate dval->rcv_action_recip_pid = -1; 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate rde = rctl_dict_lookup("zone.cpu-shares"); 14200Sstevel@tonic-gate (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 14210Sstevel@tonic-gate 14222768Ssl108498 rc_zone_locked_mem = rctl_register("zone.max-locked-memory", 14232768Ssl108498 RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 14242768Ssl108498 RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 14252768Ssl108498 &zone_locked_mem_ops); 1426*3247Sgjelinek 1427*3247Sgjelinek rc_zone_max_swap = rctl_register("zone.max-swap", 1428*3247Sgjelinek RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 1429*3247Sgjelinek RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 1430*3247Sgjelinek &zone_max_swap_ops); 1431*3247Sgjelinek 14320Sstevel@tonic-gate /* 14330Sstevel@tonic-gate * Initialize the ``global zone''. 14340Sstevel@tonic-gate */ 14350Sstevel@tonic-gate set = rctl_set_create(); 14360Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 14370Sstevel@tonic-gate mutex_enter(&p0.p_lock); 14380Sstevel@tonic-gate e.rcep_p.zone = &zone0; 14390Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 14400Sstevel@tonic-gate zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 14410Sstevel@tonic-gate gp); 14420Sstevel@tonic-gate 14430Sstevel@tonic-gate zone0.zone_nlwps = p0.p_lwpcnt; 14440Sstevel@tonic-gate zone0.zone_ntasks = 1; 14450Sstevel@tonic-gate mutex_exit(&p0.p_lock); 14462712Snn35248 zone0.zone_restart_init = B_TRUE; 14472712Snn35248 zone0.zone_brand = &native_brand; 14480Sstevel@tonic-gate rctl_prealloc_destroy(gp); 14490Sstevel@tonic-gate /* 1450*3247Sgjelinek * pool_default hasn't been initialized yet, so we let pool_init() 1451*3247Sgjelinek * take care of making sure the global zone is in the default pool. 14520Sstevel@tonic-gate */ 14531676Sjpk 14541676Sjpk /* 1455*3247Sgjelinek * Initialize global zone kstats 1456*3247Sgjelinek */ 1457*3247Sgjelinek zone_kstat_create(&zone0); 1458*3247Sgjelinek 1459*3247Sgjelinek /* 14601676Sjpk * Initialize zone label. 14611676Sjpk * mlp are initialized when tnzonecfg is loaded. 14621676Sjpk */ 14631676Sjpk zone0.zone_slabel = l_admin_low; 14641676Sjpk rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 14651676Sjpk label_hold(l_admin_low); 14661676Sjpk 14670Sstevel@tonic-gate mutex_enter(&zonehash_lock); 14680Sstevel@tonic-gate zone_uniqid(&zone0); 14690Sstevel@tonic-gate ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 14701676Sjpk 14710Sstevel@tonic-gate zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 14720Sstevel@tonic-gate mod_hash_null_valdtor); 14730Sstevel@tonic-gate zonehashbyname = mod_hash_create_strhash("zone_by_name", 14740Sstevel@tonic-gate zone_hash_size, mod_hash_null_valdtor); 14751676Sjpk /* 14761676Sjpk * maintain zonehashbylabel only for labeled systems 14771676Sjpk */ 14781676Sjpk if (is_system_labeled()) 14791676Sjpk zonehashbylabel = mod_hash_create_extended("zone_by_label", 14801676Sjpk zone_hash_size, mod_hash_null_keydtor, 14811676Sjpk mod_hash_null_valdtor, hash_bylabel, NULL, 14821676Sjpk hash_labelkey_cmp, KM_SLEEP); 14830Sstevel@tonic-gate zonecount = 1; 14840Sstevel@tonic-gate 14850Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 14860Sstevel@tonic-gate (mod_hash_val_t)&zone0); 14870Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 14880Sstevel@tonic-gate (mod_hash_val_t)&zone0); 14891769Scarlsonj if (is_system_labeled()) { 14901769Scarlsonj zone0.zone_flags |= ZF_HASHED_LABEL; 14911676Sjpk (void) mod_hash_insert(zonehashbylabel, 14921676Sjpk (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0); 14931769Scarlsonj } 14941676Sjpk mutex_exit(&zonehash_lock); 14951676Sjpk 14960Sstevel@tonic-gate /* 14970Sstevel@tonic-gate * We avoid setting zone_kcred until now, since kcred is initialized 14980Sstevel@tonic-gate * sometime after zone_zsd_init() and before zone_init(). 14990Sstevel@tonic-gate */ 15000Sstevel@tonic-gate zone0.zone_kcred = kcred; 15010Sstevel@tonic-gate /* 15020Sstevel@tonic-gate * The global zone is fully initialized (except for zone_rootvp which 15030Sstevel@tonic-gate * will be set when the root filesystem is mounted). 15040Sstevel@tonic-gate */ 15050Sstevel@tonic-gate global_zone = &zone0; 15061166Sdstaff 15071166Sdstaff /* 15081166Sdstaff * Setup an event channel to send zone status change notifications on 15091166Sdstaff */ 15101166Sdstaff res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan, 15111166Sdstaff EVCH_CREAT); 15121166Sdstaff 15131166Sdstaff if (res) 15141166Sdstaff panic("Sysevent_evc_bind failed during zone setup.\n"); 1515*3247Sgjelinek 15160Sstevel@tonic-gate } 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate static void 15190Sstevel@tonic-gate zone_free(zone_t *zone) 15200Sstevel@tonic-gate { 15210Sstevel@tonic-gate ASSERT(zone != global_zone); 15220Sstevel@tonic-gate ASSERT(zone->zone_ntasks == 0); 15230Sstevel@tonic-gate ASSERT(zone->zone_nlwps == 0); 15240Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 0); 15250Sstevel@tonic-gate ASSERT(zone->zone_kcred == NULL); 15260Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 15270Sstevel@tonic-gate zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 15280Sstevel@tonic-gate 15290Sstevel@tonic-gate /* remove from deathrow list */ 15300Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_DEAD) { 15310Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 15320Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 15330Sstevel@tonic-gate list_remove(&zone_deathrow, zone); 15340Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 15350Sstevel@tonic-gate } 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate zone_free_zsd(zone); 1538789Sahrens zone_free_datasets(zone); 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate if (zone->zone_rootvp != NULL) 15410Sstevel@tonic-gate VN_RELE(zone->zone_rootvp); 15420Sstevel@tonic-gate if (zone->zone_rootpath) 15430Sstevel@tonic-gate kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 15440Sstevel@tonic-gate if (zone->zone_name != NULL) 15450Sstevel@tonic-gate kmem_free(zone->zone_name, ZONENAME_MAX); 15461676Sjpk if (zone->zone_slabel != NULL) 15471676Sjpk label_rele(zone->zone_slabel); 15480Sstevel@tonic-gate if (zone->zone_nodename != NULL) 15490Sstevel@tonic-gate kmem_free(zone->zone_nodename, _SYS_NMLN); 15500Sstevel@tonic-gate if (zone->zone_domain != NULL) 15510Sstevel@tonic-gate kmem_free(zone->zone_domain, _SYS_NMLN); 15520Sstevel@tonic-gate if (zone->zone_privset != NULL) 15530Sstevel@tonic-gate kmem_free(zone->zone_privset, sizeof (priv_set_t)); 15540Sstevel@tonic-gate if (zone->zone_rctls != NULL) 15550Sstevel@tonic-gate rctl_set_free(zone->zone_rctls); 15560Sstevel@tonic-gate if (zone->zone_bootargs != NULL) 15572267Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 15582267Sdp if (zone->zone_initname != NULL) 15592267Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 15600Sstevel@tonic-gate id_free(zoneid_space, zone->zone_id); 15610Sstevel@tonic-gate mutex_destroy(&zone->zone_lock); 15620Sstevel@tonic-gate cv_destroy(&zone->zone_cv); 15631676Sjpk rw_destroy(&zone->zone_mlps.mlpl_rwlock); 15640Sstevel@tonic-gate kmem_free(zone, sizeof (zone_t)); 15650Sstevel@tonic-gate } 15660Sstevel@tonic-gate 15670Sstevel@tonic-gate /* 15680Sstevel@tonic-gate * See block comment at the top of this file for information about zone 15690Sstevel@tonic-gate * status values. 15700Sstevel@tonic-gate */ 15710Sstevel@tonic-gate /* 15720Sstevel@tonic-gate * Convenience function for setting zone status. 15730Sstevel@tonic-gate */ 15740Sstevel@tonic-gate static void 15750Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status) 15760Sstevel@tonic-gate { 15771166Sdstaff 15781166Sdstaff nvlist_t *nvl = NULL; 15790Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zone_status_lock)); 15800Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 15810Sstevel@tonic-gate status >= zone_status_get(zone)); 15821166Sdstaff 15831166Sdstaff if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) || 15841166Sdstaff nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) || 15851166Sdstaff nvlist_add_string(nvl, ZONE_CB_NEWSTATE, 15862267Sdp zone_status_table[status]) || 15871166Sdstaff nvlist_add_string(nvl, ZONE_CB_OLDSTATE, 15882267Sdp zone_status_table[zone->zone_status]) || 15891166Sdstaff nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) || 15901166Sdstaff nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) || 15911166Sdstaff sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS, 15922267Sdp ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) { 15931166Sdstaff #ifdef DEBUG 15941166Sdstaff (void) printf( 15951166Sdstaff "Failed to allocate and send zone state change event.\n"); 15961166Sdstaff #endif 15971166Sdstaff } 15981166Sdstaff nvlist_free(nvl); 15991166Sdstaff 16000Sstevel@tonic-gate zone->zone_status = status; 16011166Sdstaff 16020Sstevel@tonic-gate cv_broadcast(&zone->zone_cv); 16030Sstevel@tonic-gate } 16040Sstevel@tonic-gate 16050Sstevel@tonic-gate /* 16060Sstevel@tonic-gate * Public function to retrieve the zone status. The zone status may 16070Sstevel@tonic-gate * change after it is retrieved. 16080Sstevel@tonic-gate */ 16090Sstevel@tonic-gate zone_status_t 16100Sstevel@tonic-gate zone_status_get(zone_t *zone) 16110Sstevel@tonic-gate { 16120Sstevel@tonic-gate return (zone->zone_status); 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate static int 16160Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 16170Sstevel@tonic-gate { 16182267Sdp char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP); 16192267Sdp int err = 0; 16202267Sdp 16212267Sdp ASSERT(zone != global_zone); 16222267Sdp if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0) 16232267Sdp goto done; /* EFAULT or ENAMETOOLONG */ 16242267Sdp 16252267Sdp if (zone->zone_bootargs != NULL) 16262267Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 16272267Sdp 16282267Sdp zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP); 16292267Sdp (void) strcpy(zone->zone_bootargs, bootargs); 16302267Sdp 16312267Sdp done: 16322267Sdp kmem_free(bootargs, BOOTARGS_MAX); 16332267Sdp return (err); 16342267Sdp } 16352267Sdp 16362267Sdp static int 16372267Sdp zone_set_initname(zone_t *zone, const char *zone_initname) 16382267Sdp { 16392267Sdp char initname[INITNAME_SZ]; 16400Sstevel@tonic-gate size_t len; 16412267Sdp int err = 0; 16422267Sdp 16432267Sdp ASSERT(zone != global_zone); 16442267Sdp if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0) 16450Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 16462267Sdp 16472267Sdp if (zone->zone_initname != NULL) 16482267Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 16492267Sdp 16502267Sdp zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP); 16512267Sdp (void) strcpy(zone->zone_initname, initname); 16520Sstevel@tonic-gate return (0); 16530Sstevel@tonic-gate } 16540Sstevel@tonic-gate 1655*3247Sgjelinek static int 1656*3247Sgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap) 1657*3247Sgjelinek { 1658*3247Sgjelinek uint64_t mcap; 1659*3247Sgjelinek int err = 0; 1660*3247Sgjelinek 1661*3247Sgjelinek if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0) 1662*3247Sgjelinek zone->zone_phys_mcap = mcap; 1663*3247Sgjelinek 1664*3247Sgjelinek return (err); 1665*3247Sgjelinek } 1666*3247Sgjelinek 1667*3247Sgjelinek static int 1668*3247Sgjelinek zone_set_sched_class(zone_t *zone, const char *new_class) 1669*3247Sgjelinek { 1670*3247Sgjelinek char sched_class[PC_CLNMSZ]; 1671*3247Sgjelinek id_t classid; 1672*3247Sgjelinek int err; 1673*3247Sgjelinek 1674*3247Sgjelinek ASSERT(zone != global_zone); 1675*3247Sgjelinek if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0) 1676*3247Sgjelinek return (err); /* EFAULT or ENAMETOOLONG */ 1677*3247Sgjelinek 1678*3247Sgjelinek if (getcid(sched_class, &classid) != 0 || classid == syscid) 1679*3247Sgjelinek return (set_errno(EINVAL)); 1680*3247Sgjelinek zone->zone_defaultcid = classid; 1681*3247Sgjelinek ASSERT(zone->zone_defaultcid > 0 && 1682*3247Sgjelinek zone->zone_defaultcid < loaded_classes); 1683*3247Sgjelinek 1684*3247Sgjelinek return (0); 1685*3247Sgjelinek } 1686*3247Sgjelinek 16870Sstevel@tonic-gate /* 16880Sstevel@tonic-gate * Block indefinitely waiting for (zone_status >= status) 16890Sstevel@tonic-gate */ 16900Sstevel@tonic-gate void 16910Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status) 16920Sstevel@tonic-gate { 16930Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate mutex_enter(&zone_status_lock); 16960Sstevel@tonic-gate while (zone->zone_status < status) { 16970Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 16980Sstevel@tonic-gate } 16990Sstevel@tonic-gate mutex_exit(&zone_status_lock); 17000Sstevel@tonic-gate } 17010Sstevel@tonic-gate 17020Sstevel@tonic-gate /* 17030Sstevel@tonic-gate * Private CPR-safe version of zone_status_wait(). 17040Sstevel@tonic-gate */ 17050Sstevel@tonic-gate static void 17060Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 17070Sstevel@tonic-gate { 17080Sstevel@tonic-gate callb_cpr_t cprinfo; 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 17130Sstevel@tonic-gate str); 17140Sstevel@tonic-gate mutex_enter(&zone_status_lock); 17150Sstevel@tonic-gate while (zone->zone_status < status) { 17160Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 17170Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 17180Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 17190Sstevel@tonic-gate } 17200Sstevel@tonic-gate /* 17210Sstevel@tonic-gate * zone_status_lock is implicitly released by the following. 17220Sstevel@tonic-gate */ 17230Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 17240Sstevel@tonic-gate } 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate /* 17270Sstevel@tonic-gate * Block until zone enters requested state or signal is received. Return (0) 17280Sstevel@tonic-gate * if signaled, non-zero otherwise. 17290Sstevel@tonic-gate */ 17300Sstevel@tonic-gate int 17310Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status) 17320Sstevel@tonic-gate { 17330Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 17340Sstevel@tonic-gate 17350Sstevel@tonic-gate mutex_enter(&zone_status_lock); 17360Sstevel@tonic-gate while (zone->zone_status < status) { 17370Sstevel@tonic-gate if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 17380Sstevel@tonic-gate mutex_exit(&zone_status_lock); 17390Sstevel@tonic-gate return (0); 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate } 17420Sstevel@tonic-gate mutex_exit(&zone_status_lock); 17430Sstevel@tonic-gate return (1); 17440Sstevel@tonic-gate } 17450Sstevel@tonic-gate 17460Sstevel@tonic-gate /* 17470Sstevel@tonic-gate * Block until the zone enters the requested state or the timeout expires, 17480Sstevel@tonic-gate * whichever happens first. Return (-1) if operation timed out, time remaining 17490Sstevel@tonic-gate * otherwise. 17500Sstevel@tonic-gate */ 17510Sstevel@tonic-gate clock_t 17520Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 17530Sstevel@tonic-gate { 17540Sstevel@tonic-gate clock_t timeleft = 0; 17550Sstevel@tonic-gate 17560Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 17570Sstevel@tonic-gate 17580Sstevel@tonic-gate mutex_enter(&zone_status_lock); 17590Sstevel@tonic-gate while (zone->zone_status < status && timeleft != -1) { 17600Sstevel@tonic-gate timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 17610Sstevel@tonic-gate } 17620Sstevel@tonic-gate mutex_exit(&zone_status_lock); 17630Sstevel@tonic-gate return (timeleft); 17640Sstevel@tonic-gate } 17650Sstevel@tonic-gate 17660Sstevel@tonic-gate /* 17670Sstevel@tonic-gate * Block until the zone enters the requested state, the current process is 17680Sstevel@tonic-gate * signaled, or the timeout expires, whichever happens first. Return (-1) if 17690Sstevel@tonic-gate * operation timed out, 0 if signaled, time remaining otherwise. 17700Sstevel@tonic-gate */ 17710Sstevel@tonic-gate clock_t 17720Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 17730Sstevel@tonic-gate { 17740Sstevel@tonic-gate clock_t timeleft = tim - lbolt; 17750Sstevel@tonic-gate 17760Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate mutex_enter(&zone_status_lock); 17790Sstevel@tonic-gate while (zone->zone_status < status) { 17800Sstevel@tonic-gate timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 17810Sstevel@tonic-gate tim); 17820Sstevel@tonic-gate if (timeleft <= 0) 17830Sstevel@tonic-gate break; 17840Sstevel@tonic-gate } 17850Sstevel@tonic-gate mutex_exit(&zone_status_lock); 17860Sstevel@tonic-gate return (timeleft); 17870Sstevel@tonic-gate } 17880Sstevel@tonic-gate 17890Sstevel@tonic-gate /* 17900Sstevel@tonic-gate * Zones have two reference counts: one for references from credential 17910Sstevel@tonic-gate * structures (zone_cred_ref), and one (zone_ref) for everything else. 17920Sstevel@tonic-gate * This is so we can allow a zone to be rebooted while there are still 17930Sstevel@tonic-gate * outstanding cred references, since certain drivers cache dblks (which 17940Sstevel@tonic-gate * implicitly results in cached creds). We wait for zone_ref to drop to 17950Sstevel@tonic-gate * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 17960Sstevel@tonic-gate * later freed when the zone_cred_ref drops to 0, though nothing other 17970Sstevel@tonic-gate * than the zone id and privilege set should be accessed once the zone 17980Sstevel@tonic-gate * is "dead". 17990Sstevel@tonic-gate * 18000Sstevel@tonic-gate * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 18010Sstevel@tonic-gate * to force halt/reboot to block waiting for the zone_cred_ref to drop 18020Sstevel@tonic-gate * to 0. This can be useful to flush out other sources of cached creds 18030Sstevel@tonic-gate * that may be less innocuous than the driver case. 18040Sstevel@tonic-gate */ 18050Sstevel@tonic-gate 18060Sstevel@tonic-gate int zone_wait_for_cred = 0; 18070Sstevel@tonic-gate 18080Sstevel@tonic-gate static void 18090Sstevel@tonic-gate zone_hold_locked(zone_t *z) 18100Sstevel@tonic-gate { 18110Sstevel@tonic-gate ASSERT(MUTEX_HELD(&z->zone_lock)); 18120Sstevel@tonic-gate z->zone_ref++; 18130Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 18140Sstevel@tonic-gate } 18150Sstevel@tonic-gate 18160Sstevel@tonic-gate void 18170Sstevel@tonic-gate zone_hold(zone_t *z) 18180Sstevel@tonic-gate { 18190Sstevel@tonic-gate mutex_enter(&z->zone_lock); 18200Sstevel@tonic-gate zone_hold_locked(z); 18210Sstevel@tonic-gate mutex_exit(&z->zone_lock); 18220Sstevel@tonic-gate } 18230Sstevel@tonic-gate 18240Sstevel@tonic-gate /* 18250Sstevel@tonic-gate * If the non-cred ref count drops to 1 and either the cred ref count 18260Sstevel@tonic-gate * is 0 or we aren't waiting for cred references, the zone is ready to 18270Sstevel@tonic-gate * be destroyed. 18280Sstevel@tonic-gate */ 18290Sstevel@tonic-gate #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 18300Sstevel@tonic-gate (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate void 18330Sstevel@tonic-gate zone_rele(zone_t *z) 18340Sstevel@tonic-gate { 18350Sstevel@tonic-gate boolean_t wakeup; 18360Sstevel@tonic-gate 18370Sstevel@tonic-gate mutex_enter(&z->zone_lock); 18380Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 18390Sstevel@tonic-gate z->zone_ref--; 18400Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 18410Sstevel@tonic-gate /* no more refs, free the structure */ 18420Sstevel@tonic-gate mutex_exit(&z->zone_lock); 18430Sstevel@tonic-gate zone_free(z); 18440Sstevel@tonic-gate return; 18450Sstevel@tonic-gate } 18460Sstevel@tonic-gate /* signal zone_destroy so the zone can finish halting */ 18470Sstevel@tonic-gate wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 18480Sstevel@tonic-gate mutex_exit(&z->zone_lock); 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate if (wakeup) { 18510Sstevel@tonic-gate /* 18520Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 18530Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 18540Sstevel@tonic-gate */ 18550Sstevel@tonic-gate mutex_enter(&zonehash_lock); 18560Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 18570Sstevel@tonic-gate mutex_exit(&zonehash_lock); 18580Sstevel@tonic-gate } 18590Sstevel@tonic-gate } 18600Sstevel@tonic-gate 18610Sstevel@tonic-gate void 18620Sstevel@tonic-gate zone_cred_hold(zone_t *z) 18630Sstevel@tonic-gate { 18640Sstevel@tonic-gate mutex_enter(&z->zone_lock); 18650Sstevel@tonic-gate z->zone_cred_ref++; 18660Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 18670Sstevel@tonic-gate mutex_exit(&z->zone_lock); 18680Sstevel@tonic-gate } 18690Sstevel@tonic-gate 18700Sstevel@tonic-gate void 18710Sstevel@tonic-gate zone_cred_rele(zone_t *z) 18720Sstevel@tonic-gate { 18730Sstevel@tonic-gate boolean_t wakeup; 18740Sstevel@tonic-gate 18750Sstevel@tonic-gate mutex_enter(&z->zone_lock); 18760Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 18770Sstevel@tonic-gate z->zone_cred_ref--; 18780Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 18790Sstevel@tonic-gate /* no more refs, free the structure */ 18800Sstevel@tonic-gate mutex_exit(&z->zone_lock); 18810Sstevel@tonic-gate zone_free(z); 18820Sstevel@tonic-gate return; 18830Sstevel@tonic-gate } 18840Sstevel@tonic-gate /* 18850Sstevel@tonic-gate * If zone_destroy is waiting for the cred references to drain 18860Sstevel@tonic-gate * out, and they have, signal it. 18870Sstevel@tonic-gate */ 18880Sstevel@tonic-gate wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 18890Sstevel@tonic-gate zone_status_get(z) >= ZONE_IS_DEAD); 18900Sstevel@tonic-gate mutex_exit(&z->zone_lock); 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate if (wakeup) { 18930Sstevel@tonic-gate /* 18940Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 18950Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 18960Sstevel@tonic-gate */ 18970Sstevel@tonic-gate mutex_enter(&zonehash_lock); 18980Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 18990Sstevel@tonic-gate mutex_exit(&zonehash_lock); 19000Sstevel@tonic-gate } 19010Sstevel@tonic-gate } 19020Sstevel@tonic-gate 19030Sstevel@tonic-gate void 19040Sstevel@tonic-gate zone_task_hold(zone_t *z) 19050Sstevel@tonic-gate { 19060Sstevel@tonic-gate mutex_enter(&z->zone_lock); 19070Sstevel@tonic-gate z->zone_ntasks++; 19080Sstevel@tonic-gate ASSERT(z->zone_ntasks != 0); 19090Sstevel@tonic-gate mutex_exit(&z->zone_lock); 19100Sstevel@tonic-gate } 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate void 19130Sstevel@tonic-gate zone_task_rele(zone_t *zone) 19140Sstevel@tonic-gate { 19150Sstevel@tonic-gate uint_t refcnt; 19160Sstevel@tonic-gate 19170Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 19180Sstevel@tonic-gate ASSERT(zone->zone_ntasks != 0); 19190Sstevel@tonic-gate refcnt = --zone->zone_ntasks; 19200Sstevel@tonic-gate if (refcnt > 1) { /* Common case */ 19210Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 19220Sstevel@tonic-gate return; 19230Sstevel@tonic-gate } 19240Sstevel@tonic-gate zone_hold_locked(zone); /* so we can use the zone_t later */ 19250Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 19260Sstevel@tonic-gate if (refcnt == 1) { 19270Sstevel@tonic-gate /* 19280Sstevel@tonic-gate * See if the zone is shutting down. 19290Sstevel@tonic-gate */ 19300Sstevel@tonic-gate mutex_enter(&zone_status_lock); 19310Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 19320Sstevel@tonic-gate goto out; 19330Sstevel@tonic-gate } 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate /* 19360Sstevel@tonic-gate * Make sure the ntasks didn't change since we 19370Sstevel@tonic-gate * dropped zone_lock. 19380Sstevel@tonic-gate */ 19390Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 19400Sstevel@tonic-gate if (refcnt != zone->zone_ntasks) { 19410Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 19420Sstevel@tonic-gate goto out; 19430Sstevel@tonic-gate } 19440Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 19450Sstevel@tonic-gate 19460Sstevel@tonic-gate /* 19470Sstevel@tonic-gate * No more user processes in the zone. The zone is empty. 19480Sstevel@tonic-gate */ 19490Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 19500Sstevel@tonic-gate goto out; 19510Sstevel@tonic-gate } 19520Sstevel@tonic-gate 19530Sstevel@tonic-gate ASSERT(refcnt == 0); 19540Sstevel@tonic-gate /* 19550Sstevel@tonic-gate * zsched has exited; the zone is dead. 19560Sstevel@tonic-gate */ 19570Sstevel@tonic-gate zone->zone_zsched = NULL; /* paranoia */ 19580Sstevel@tonic-gate mutex_enter(&zone_status_lock); 19590Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DEAD); 19600Sstevel@tonic-gate out: 19610Sstevel@tonic-gate mutex_exit(&zone_status_lock); 19620Sstevel@tonic-gate zone_rele(zone); 19630Sstevel@tonic-gate } 19640Sstevel@tonic-gate 19650Sstevel@tonic-gate zoneid_t 19660Sstevel@tonic-gate getzoneid(void) 19670Sstevel@tonic-gate { 19680Sstevel@tonic-gate return (curproc->p_zone->zone_id); 19690Sstevel@tonic-gate } 19700Sstevel@tonic-gate 19710Sstevel@tonic-gate /* 19720Sstevel@tonic-gate * Internal versions of zone_find_by_*(). These don't zone_hold() or 19730Sstevel@tonic-gate * check the validity of a zone's state. 19740Sstevel@tonic-gate */ 19750Sstevel@tonic-gate static zone_t * 19760Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid) 19770Sstevel@tonic-gate { 19780Sstevel@tonic-gate mod_hash_val_t hv; 19790Sstevel@tonic-gate zone_t *zone = NULL; 19800Sstevel@tonic-gate 19810Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 19820Sstevel@tonic-gate 19830Sstevel@tonic-gate if (mod_hash_find(zonehashbyid, 19840Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 19850Sstevel@tonic-gate zone = (zone_t *)hv; 19860Sstevel@tonic-gate return (zone); 19870Sstevel@tonic-gate } 19880Sstevel@tonic-gate 19890Sstevel@tonic-gate static zone_t * 19901676Sjpk zone_find_all_by_label(const ts_label_t *label) 19911676Sjpk { 19921676Sjpk mod_hash_val_t hv; 19931676Sjpk zone_t *zone = NULL; 19941676Sjpk 19951676Sjpk ASSERT(MUTEX_HELD(&zonehash_lock)); 19961676Sjpk 19971676Sjpk /* 19981676Sjpk * zonehashbylabel is not maintained for unlabeled systems 19991676Sjpk */ 20001676Sjpk if (!is_system_labeled()) 20011676Sjpk return (NULL); 20021676Sjpk if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0) 20031676Sjpk zone = (zone_t *)hv; 20041676Sjpk return (zone); 20051676Sjpk } 20061676Sjpk 20071676Sjpk static zone_t * 20080Sstevel@tonic-gate zone_find_all_by_name(char *name) 20090Sstevel@tonic-gate { 20100Sstevel@tonic-gate mod_hash_val_t hv; 20110Sstevel@tonic-gate zone_t *zone = NULL; 20120Sstevel@tonic-gate 20130Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 20140Sstevel@tonic-gate 20150Sstevel@tonic-gate if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 20160Sstevel@tonic-gate zone = (zone_t *)hv; 20170Sstevel@tonic-gate return (zone); 20180Sstevel@tonic-gate } 20190Sstevel@tonic-gate 20200Sstevel@tonic-gate /* 20210Sstevel@tonic-gate * Public interface for looking up a zone by zoneid. Only returns the zone if 20220Sstevel@tonic-gate * it is fully initialized, and has not yet begun the zone_destroy() sequence. 20230Sstevel@tonic-gate * Caller must call zone_rele() once it is done with the zone. 20240Sstevel@tonic-gate * 20250Sstevel@tonic-gate * The zone may begin the zone_destroy() sequence immediately after this 20260Sstevel@tonic-gate * function returns, but may be safely used until zone_rele() is called. 20270Sstevel@tonic-gate */ 20280Sstevel@tonic-gate zone_t * 20290Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid) 20300Sstevel@tonic-gate { 20310Sstevel@tonic-gate zone_t *zone; 20320Sstevel@tonic-gate zone_status_t status; 20330Sstevel@tonic-gate 20340Sstevel@tonic-gate mutex_enter(&zonehash_lock); 20350Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 20360Sstevel@tonic-gate mutex_exit(&zonehash_lock); 20370Sstevel@tonic-gate return (NULL); 20380Sstevel@tonic-gate } 20390Sstevel@tonic-gate status = zone_status_get(zone); 20400Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 20410Sstevel@tonic-gate /* 20420Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 20430Sstevel@tonic-gate */ 20440Sstevel@tonic-gate mutex_exit(&zonehash_lock); 20450Sstevel@tonic-gate return (NULL); 20460Sstevel@tonic-gate } 20470Sstevel@tonic-gate zone_hold(zone); 20480Sstevel@tonic-gate mutex_exit(&zonehash_lock); 20490Sstevel@tonic-gate return (zone); 20500Sstevel@tonic-gate } 20510Sstevel@tonic-gate 20520Sstevel@tonic-gate /* 20531676Sjpk * Similar to zone_find_by_id, but using zone label as the key. 20541676Sjpk */ 20551676Sjpk zone_t * 20561676Sjpk zone_find_by_label(const ts_label_t *label) 20571676Sjpk { 20581676Sjpk zone_t *zone; 20592110Srica zone_status_t status; 20601676Sjpk 20611676Sjpk mutex_enter(&zonehash_lock); 20621676Sjpk if ((zone = zone_find_all_by_label(label)) == NULL) { 20631676Sjpk mutex_exit(&zonehash_lock); 20641676Sjpk return (NULL); 20651676Sjpk } 20662110Srica 20672110Srica status = zone_status_get(zone); 20682110Srica if (status > ZONE_IS_DOWN) { 20691676Sjpk /* 20701676Sjpk * For all practical purposes the zone doesn't exist. 20711676Sjpk */ 20722110Srica mutex_exit(&zonehash_lock); 20732110Srica return (NULL); 20741676Sjpk } 20752110Srica zone_hold(zone); 20761676Sjpk mutex_exit(&zonehash_lock); 20771676Sjpk return (zone); 20781676Sjpk } 20791676Sjpk 20801676Sjpk /* 20810Sstevel@tonic-gate * Similar to zone_find_by_id, but using zone name as the key. 20820Sstevel@tonic-gate */ 20830Sstevel@tonic-gate zone_t * 20840Sstevel@tonic-gate zone_find_by_name(char *name) 20850Sstevel@tonic-gate { 20860Sstevel@tonic-gate zone_t *zone; 20870Sstevel@tonic-gate zone_status_t status; 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate mutex_enter(&zonehash_lock); 20900Sstevel@tonic-gate if ((zone = zone_find_all_by_name(name)) == NULL) { 20910Sstevel@tonic-gate mutex_exit(&zonehash_lock); 20920Sstevel@tonic-gate return (NULL); 20930Sstevel@tonic-gate } 20940Sstevel@tonic-gate status = zone_status_get(zone); 20950Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 20960Sstevel@tonic-gate /* 20970Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 20980Sstevel@tonic-gate */ 20990Sstevel@tonic-gate mutex_exit(&zonehash_lock); 21000Sstevel@tonic-gate return (NULL); 21010Sstevel@tonic-gate } 21020Sstevel@tonic-gate zone_hold(zone); 21030Sstevel@tonic-gate mutex_exit(&zonehash_lock); 21040Sstevel@tonic-gate return (zone); 21050Sstevel@tonic-gate } 21060Sstevel@tonic-gate 21070Sstevel@tonic-gate /* 21080Sstevel@tonic-gate * Similar to zone_find_by_id(), using the path as a key. For instance, 21090Sstevel@tonic-gate * if there is a zone "foo" rooted at /foo/root, and the path argument 21100Sstevel@tonic-gate * is "/foo/root/proc", it will return the held zone_t corresponding to 21110Sstevel@tonic-gate * zone "foo". 21120Sstevel@tonic-gate * 21130Sstevel@tonic-gate * zone_find_by_path() always returns a non-NULL value, since at the 21140Sstevel@tonic-gate * very least every path will be contained in the global zone. 21150Sstevel@tonic-gate * 21160Sstevel@tonic-gate * As with the other zone_find_by_*() functions, the caller is 21170Sstevel@tonic-gate * responsible for zone_rele()ing the return value of this function. 21180Sstevel@tonic-gate */ 21190Sstevel@tonic-gate zone_t * 21200Sstevel@tonic-gate zone_find_by_path(const char *path) 21210Sstevel@tonic-gate { 21220Sstevel@tonic-gate zone_t *zone; 21230Sstevel@tonic-gate zone_t *zret = NULL; 21240Sstevel@tonic-gate zone_status_t status; 21250Sstevel@tonic-gate 21260Sstevel@tonic-gate if (path == NULL) { 21270Sstevel@tonic-gate /* 21280Sstevel@tonic-gate * Call from rootconf(). 21290Sstevel@tonic-gate */ 21300Sstevel@tonic-gate zone_hold(global_zone); 21310Sstevel@tonic-gate return (global_zone); 21320Sstevel@tonic-gate } 21330Sstevel@tonic-gate ASSERT(*path == '/'); 21340Sstevel@tonic-gate mutex_enter(&zonehash_lock); 21350Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 21360Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 21370Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(path, zone)) 21380Sstevel@tonic-gate zret = zone; 21390Sstevel@tonic-gate } 21400Sstevel@tonic-gate ASSERT(zret != NULL); 21410Sstevel@tonic-gate status = zone_status_get(zret); 21420Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 21430Sstevel@tonic-gate /* 21440Sstevel@tonic-gate * Zone practically doesn't exist. 21450Sstevel@tonic-gate */ 21460Sstevel@tonic-gate zret = global_zone; 21470Sstevel@tonic-gate } 21480Sstevel@tonic-gate zone_hold(zret); 21490Sstevel@tonic-gate mutex_exit(&zonehash_lock); 21500Sstevel@tonic-gate return (zret); 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate 21530Sstevel@tonic-gate /* 21540Sstevel@tonic-gate * Get the number of cpus visible to this zone. The system-wide global 21550Sstevel@tonic-gate * 'ncpus' is returned if pools are disabled, the caller is in the 21560Sstevel@tonic-gate * global zone, or a NULL zone argument is passed in. 21570Sstevel@tonic-gate */ 21580Sstevel@tonic-gate int 21590Sstevel@tonic-gate zone_ncpus_get(zone_t *zone) 21600Sstevel@tonic-gate { 21610Sstevel@tonic-gate int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 21620Sstevel@tonic-gate 21630Sstevel@tonic-gate return (myncpus != 0 ? myncpus : ncpus); 21640Sstevel@tonic-gate } 21650Sstevel@tonic-gate 21660Sstevel@tonic-gate /* 21670Sstevel@tonic-gate * Get the number of online cpus visible to this zone. The system-wide 21680Sstevel@tonic-gate * global 'ncpus_online' is returned if pools are disabled, the caller 21690Sstevel@tonic-gate * is in the global zone, or a NULL zone argument is passed in. 21700Sstevel@tonic-gate */ 21710Sstevel@tonic-gate int 21720Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone) 21730Sstevel@tonic-gate { 21740Sstevel@tonic-gate int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 21750Sstevel@tonic-gate 21760Sstevel@tonic-gate return (myncpus_online != 0 ? myncpus_online : ncpus_online); 21770Sstevel@tonic-gate } 21780Sstevel@tonic-gate 21790Sstevel@tonic-gate /* 21800Sstevel@tonic-gate * Return the pool to which the zone is currently bound. 21810Sstevel@tonic-gate */ 21820Sstevel@tonic-gate pool_t * 21830Sstevel@tonic-gate zone_pool_get(zone_t *zone) 21840Sstevel@tonic-gate { 21850Sstevel@tonic-gate ASSERT(pool_lock_held()); 21860Sstevel@tonic-gate 21870Sstevel@tonic-gate return (zone->zone_pool); 21880Sstevel@tonic-gate } 21890Sstevel@tonic-gate 21900Sstevel@tonic-gate /* 21910Sstevel@tonic-gate * Set the zone's pool pointer and update the zone's visibility to match 21920Sstevel@tonic-gate * the resources in the new pool. 21930Sstevel@tonic-gate */ 21940Sstevel@tonic-gate void 21950Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool) 21960Sstevel@tonic-gate { 21970Sstevel@tonic-gate ASSERT(pool_lock_held()); 21980Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 21990Sstevel@tonic-gate 22000Sstevel@tonic-gate zone->zone_pool = pool; 22010Sstevel@tonic-gate zone_pset_set(zone, pool->pool_pset->pset_id); 22020Sstevel@tonic-gate } 22030Sstevel@tonic-gate 22040Sstevel@tonic-gate /* 22050Sstevel@tonic-gate * Return the cached value of the id of the processor set to which the 22060Sstevel@tonic-gate * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 22070Sstevel@tonic-gate * facility is disabled. 22080Sstevel@tonic-gate */ 22090Sstevel@tonic-gate psetid_t 22100Sstevel@tonic-gate zone_pset_get(zone_t *zone) 22110Sstevel@tonic-gate { 22120Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 22130Sstevel@tonic-gate 22140Sstevel@tonic-gate return (zone->zone_psetid); 22150Sstevel@tonic-gate } 22160Sstevel@tonic-gate 22170Sstevel@tonic-gate /* 22180Sstevel@tonic-gate * Set the cached value of the id of the processor set to which the zone 22190Sstevel@tonic-gate * is currently bound. Also update the zone's visibility to match the 22200Sstevel@tonic-gate * resources in the new processor set. 22210Sstevel@tonic-gate */ 22220Sstevel@tonic-gate void 22230Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid) 22240Sstevel@tonic-gate { 22250Sstevel@tonic-gate psetid_t oldpsetid; 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 22280Sstevel@tonic-gate oldpsetid = zone_pset_get(zone); 22290Sstevel@tonic-gate 22300Sstevel@tonic-gate if (oldpsetid == newpsetid) 22310Sstevel@tonic-gate return; 22320Sstevel@tonic-gate /* 22330Sstevel@tonic-gate * Global zone sees all. 22340Sstevel@tonic-gate */ 22350Sstevel@tonic-gate if (zone != global_zone) { 22360Sstevel@tonic-gate zone->zone_psetid = newpsetid; 22370Sstevel@tonic-gate if (newpsetid != ZONE_PS_INVAL) 22380Sstevel@tonic-gate pool_pset_visibility_add(newpsetid, zone); 22390Sstevel@tonic-gate if (oldpsetid != ZONE_PS_INVAL) 22400Sstevel@tonic-gate pool_pset_visibility_remove(oldpsetid, zone); 22410Sstevel@tonic-gate } 22420Sstevel@tonic-gate /* 22430Sstevel@tonic-gate * Disabling pools, so we should start using the global values 22440Sstevel@tonic-gate * for ncpus and ncpus_online. 22450Sstevel@tonic-gate */ 22460Sstevel@tonic-gate if (newpsetid == ZONE_PS_INVAL) { 22470Sstevel@tonic-gate zone->zone_ncpus = 0; 22480Sstevel@tonic-gate zone->zone_ncpus_online = 0; 22490Sstevel@tonic-gate } 22500Sstevel@tonic-gate } 22510Sstevel@tonic-gate 22520Sstevel@tonic-gate /* 22530Sstevel@tonic-gate * Walk the list of active zones and issue the provided callback for 22540Sstevel@tonic-gate * each of them. 22550Sstevel@tonic-gate * 22560Sstevel@tonic-gate * Caller must not be holding any locks that may be acquired under 22570Sstevel@tonic-gate * zonehash_lock. See comment at the beginning of the file for a list of 22580Sstevel@tonic-gate * common locks and their interactions with zones. 22590Sstevel@tonic-gate */ 22600Sstevel@tonic-gate int 22610Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data) 22620Sstevel@tonic-gate { 22630Sstevel@tonic-gate zone_t *zone; 22640Sstevel@tonic-gate int ret = 0; 22650Sstevel@tonic-gate zone_status_t status; 22660Sstevel@tonic-gate 22670Sstevel@tonic-gate mutex_enter(&zonehash_lock); 22680Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 22690Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 22700Sstevel@tonic-gate /* 22710Sstevel@tonic-gate * Skip zones that shouldn't be externally visible. 22720Sstevel@tonic-gate */ 22730Sstevel@tonic-gate status = zone_status_get(zone); 22740Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 22750Sstevel@tonic-gate continue; 22760Sstevel@tonic-gate /* 22770Sstevel@tonic-gate * Bail immediately if any callback invocation returns a 22780Sstevel@tonic-gate * non-zero value. 22790Sstevel@tonic-gate */ 22800Sstevel@tonic-gate ret = (*cb)(zone, data); 22810Sstevel@tonic-gate if (ret != 0) 22820Sstevel@tonic-gate break; 22830Sstevel@tonic-gate } 22840Sstevel@tonic-gate mutex_exit(&zonehash_lock); 22850Sstevel@tonic-gate return (ret); 22860Sstevel@tonic-gate } 22870Sstevel@tonic-gate 22880Sstevel@tonic-gate static int 22890Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath) 22900Sstevel@tonic-gate { 22910Sstevel@tonic-gate vnode_t *vp; 22920Sstevel@tonic-gate int trycount; 22930Sstevel@tonic-gate int error = 0; 22940Sstevel@tonic-gate char *path; 22950Sstevel@tonic-gate struct pathname upn, pn; 22960Sstevel@tonic-gate size_t pathlen; 22970Sstevel@tonic-gate 22980Sstevel@tonic-gate if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 22990Sstevel@tonic-gate return (error); 23000Sstevel@tonic-gate 23010Sstevel@tonic-gate pn_alloc(&pn); 23020Sstevel@tonic-gate 23030Sstevel@tonic-gate /* prevent infinite loop */ 23040Sstevel@tonic-gate trycount = 10; 23050Sstevel@tonic-gate for (;;) { 23060Sstevel@tonic-gate if (--trycount <= 0) { 23070Sstevel@tonic-gate error = ESTALE; 23080Sstevel@tonic-gate goto out; 23090Sstevel@tonic-gate } 23100Sstevel@tonic-gate 23110Sstevel@tonic-gate if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 23120Sstevel@tonic-gate /* 23130Sstevel@tonic-gate * VOP_ACCESS() may cover 'vp' with a new 23140Sstevel@tonic-gate * filesystem, if 'vp' is an autoFS vnode. 23150Sstevel@tonic-gate * Get the new 'vp' if so. 23160Sstevel@tonic-gate */ 23170Sstevel@tonic-gate if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 && 23180Sstevel@tonic-gate (vp->v_vfsmountedhere == NULL || 23190Sstevel@tonic-gate (error = traverse(&vp)) == 0)) { 23200Sstevel@tonic-gate pathlen = pn.pn_pathlen + 2; 23210Sstevel@tonic-gate path = kmem_alloc(pathlen, KM_SLEEP); 23220Sstevel@tonic-gate (void) strncpy(path, pn.pn_path, 23230Sstevel@tonic-gate pn.pn_pathlen + 1); 23240Sstevel@tonic-gate path[pathlen - 2] = '/'; 23250Sstevel@tonic-gate path[pathlen - 1] = '\0'; 23260Sstevel@tonic-gate pn_free(&pn); 23270Sstevel@tonic-gate pn_free(&upn); 23280Sstevel@tonic-gate 23290Sstevel@tonic-gate /* Success! */ 23300Sstevel@tonic-gate break; 23310Sstevel@tonic-gate } 23320Sstevel@tonic-gate VN_RELE(vp); 23330Sstevel@tonic-gate } 23340Sstevel@tonic-gate if (error != ESTALE) 23350Sstevel@tonic-gate goto out; 23360Sstevel@tonic-gate } 23370Sstevel@tonic-gate 23380Sstevel@tonic-gate ASSERT(error == 0); 23390Sstevel@tonic-gate zone->zone_rootvp = vp; /* we hold a reference to vp */ 23400Sstevel@tonic-gate zone->zone_rootpath = path; 23410Sstevel@tonic-gate zone->zone_rootpathlen = pathlen; 23421769Scarlsonj if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0) 23431769Scarlsonj zone->zone_flags |= ZF_IS_SCRATCH; 23440Sstevel@tonic-gate return (0); 23450Sstevel@tonic-gate 23460Sstevel@tonic-gate out: 23470Sstevel@tonic-gate pn_free(&pn); 23480Sstevel@tonic-gate pn_free(&upn); 23490Sstevel@tonic-gate return (error); 23500Sstevel@tonic-gate } 23510Sstevel@tonic-gate 23520Sstevel@tonic-gate #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 23530Sstevel@tonic-gate ((c) >= 'a' && (c) <= 'z') || \ 23540Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'Z')) 23550Sstevel@tonic-gate 23560Sstevel@tonic-gate static int 23570Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname) 23580Sstevel@tonic-gate { 23590Sstevel@tonic-gate char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 23600Sstevel@tonic-gate size_t len; 23610Sstevel@tonic-gate int i, err; 23620Sstevel@tonic-gate 23630Sstevel@tonic-gate if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 23640Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 23650Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 23660Sstevel@tonic-gate } 23670Sstevel@tonic-gate 23680Sstevel@tonic-gate /* must be less than ZONENAME_MAX */ 23690Sstevel@tonic-gate if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 23700Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 23710Sstevel@tonic-gate return (EINVAL); 23720Sstevel@tonic-gate } 23730Sstevel@tonic-gate 23740Sstevel@tonic-gate /* 23750Sstevel@tonic-gate * Name must start with an alphanumeric and must contain only 23760Sstevel@tonic-gate * alphanumerics, '-', '_' and '.'. 23770Sstevel@tonic-gate */ 23780Sstevel@tonic-gate if (!isalnum(kname[0])) { 23790Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 23800Sstevel@tonic-gate return (EINVAL); 23810Sstevel@tonic-gate } 23820Sstevel@tonic-gate for (i = 1; i < len - 1; i++) { 23830Sstevel@tonic-gate if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 23840Sstevel@tonic-gate kname[i] != '.') { 23850Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 23860Sstevel@tonic-gate return (EINVAL); 23870Sstevel@tonic-gate } 23880Sstevel@tonic-gate } 23890Sstevel@tonic-gate 23900Sstevel@tonic-gate zone->zone_name = kname; 23910Sstevel@tonic-gate return (0); 23920Sstevel@tonic-gate } 23930Sstevel@tonic-gate 23940Sstevel@tonic-gate /* 23950Sstevel@tonic-gate * Similar to thread_create(), but makes sure the thread is in the appropriate 23960Sstevel@tonic-gate * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 23970Sstevel@tonic-gate */ 23980Sstevel@tonic-gate /*ARGSUSED*/ 23990Sstevel@tonic-gate kthread_t * 24000Sstevel@tonic-gate zthread_create( 24010Sstevel@tonic-gate caddr_t stk, 24020Sstevel@tonic-gate size_t stksize, 24030Sstevel@tonic-gate void (*proc)(), 24040Sstevel@tonic-gate void *arg, 24050Sstevel@tonic-gate size_t len, 24060Sstevel@tonic-gate pri_t pri) 24070Sstevel@tonic-gate { 24080Sstevel@tonic-gate kthread_t *t; 24090Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 24100Sstevel@tonic-gate proc_t *pp = zone->zone_zsched; 24110Sstevel@tonic-gate 24120Sstevel@tonic-gate zone_hold(zone); /* Reference to be dropped when thread exits */ 24130Sstevel@tonic-gate 24140Sstevel@tonic-gate /* 24150Sstevel@tonic-gate * No-one should be trying to create threads if the zone is shutting 24160Sstevel@tonic-gate * down and there aren't any kernel threads around. See comment 24170Sstevel@tonic-gate * in zthread_exit(). 24180Sstevel@tonic-gate */ 24190Sstevel@tonic-gate ASSERT(!(zone->zone_kthreads == NULL && 24200Sstevel@tonic-gate zone_status_get(zone) >= ZONE_IS_EMPTY)); 24210Sstevel@tonic-gate /* 24220Sstevel@tonic-gate * Create a thread, but don't let it run until we've finished setting 24230Sstevel@tonic-gate * things up. 24240Sstevel@tonic-gate */ 24250Sstevel@tonic-gate t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 24260Sstevel@tonic-gate ASSERT(t->t_forw == NULL); 24270Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24280Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 24290Sstevel@tonic-gate t->t_forw = t->t_back = t; 24300Sstevel@tonic-gate } else { 24310Sstevel@tonic-gate kthread_t *tx = zone->zone_kthreads; 24320Sstevel@tonic-gate 24330Sstevel@tonic-gate t->t_forw = tx; 24340Sstevel@tonic-gate t->t_back = tx->t_back; 24350Sstevel@tonic-gate tx->t_back->t_forw = t; 24360Sstevel@tonic-gate tx->t_back = t; 24370Sstevel@tonic-gate } 24380Sstevel@tonic-gate zone->zone_kthreads = t; 24390Sstevel@tonic-gate mutex_exit(&zone_status_lock); 24400Sstevel@tonic-gate 24410Sstevel@tonic-gate mutex_enter(&pp->p_lock); 24420Sstevel@tonic-gate t->t_proc_flag |= TP_ZTHREAD; 24430Sstevel@tonic-gate project_rele(t->t_proj); 24440Sstevel@tonic-gate t->t_proj = project_hold(pp->p_task->tk_proj); 24450Sstevel@tonic-gate 24460Sstevel@tonic-gate /* 24470Sstevel@tonic-gate * Setup complete, let it run. 24480Sstevel@tonic-gate */ 24490Sstevel@tonic-gate thread_lock(t); 24500Sstevel@tonic-gate t->t_schedflag |= TS_ALLSTART; 24510Sstevel@tonic-gate setrun_locked(t); 24520Sstevel@tonic-gate thread_unlock(t); 24530Sstevel@tonic-gate 24540Sstevel@tonic-gate mutex_exit(&pp->p_lock); 24550Sstevel@tonic-gate 24560Sstevel@tonic-gate return (t); 24570Sstevel@tonic-gate } 24580Sstevel@tonic-gate 24590Sstevel@tonic-gate /* 24600Sstevel@tonic-gate * Similar to thread_exit(). Must be called by threads created via 24610Sstevel@tonic-gate * zthread_exit(). 24620Sstevel@tonic-gate */ 24630Sstevel@tonic-gate void 24640Sstevel@tonic-gate zthread_exit(void) 24650Sstevel@tonic-gate { 24660Sstevel@tonic-gate kthread_t *t = curthread; 24670Sstevel@tonic-gate proc_t *pp = curproc; 24680Sstevel@tonic-gate zone_t *zone = pp->p_zone; 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24710Sstevel@tonic-gate 24720Sstevel@tonic-gate /* 24730Sstevel@tonic-gate * Reparent to p0 24740Sstevel@tonic-gate */ 24751075Sjosephb kpreempt_disable(); 24760Sstevel@tonic-gate mutex_enter(&pp->p_lock); 24770Sstevel@tonic-gate t->t_proc_flag &= ~TP_ZTHREAD; 24780Sstevel@tonic-gate t->t_procp = &p0; 24790Sstevel@tonic-gate hat_thread_exit(t); 24800Sstevel@tonic-gate mutex_exit(&pp->p_lock); 24811075Sjosephb kpreempt_enable(); 24820Sstevel@tonic-gate 24830Sstevel@tonic-gate if (t->t_back == t) { 24840Sstevel@tonic-gate ASSERT(t->t_forw == t); 24850Sstevel@tonic-gate /* 24860Sstevel@tonic-gate * If the zone is empty, once the thread count 24870Sstevel@tonic-gate * goes to zero no further kernel threads can be 24880Sstevel@tonic-gate * created. This is because if the creator is a process 24890Sstevel@tonic-gate * in the zone, then it must have exited before the zone 24900Sstevel@tonic-gate * state could be set to ZONE_IS_EMPTY. 24910Sstevel@tonic-gate * Otherwise, if the creator is a kernel thread in the 24920Sstevel@tonic-gate * zone, the thread count is non-zero. 24930Sstevel@tonic-gate * 24940Sstevel@tonic-gate * This really means that non-zone kernel threads should 24950Sstevel@tonic-gate * not create zone kernel threads. 24960Sstevel@tonic-gate */ 24970Sstevel@tonic-gate zone->zone_kthreads = NULL; 24980Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_EMPTY) { 24990Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 25000Sstevel@tonic-gate } 25010Sstevel@tonic-gate } else { 25020Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 25030Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 25040Sstevel@tonic-gate if (zone->zone_kthreads == t) 25050Sstevel@tonic-gate zone->zone_kthreads = t->t_forw; 25060Sstevel@tonic-gate } 25070Sstevel@tonic-gate mutex_exit(&zone_status_lock); 25080Sstevel@tonic-gate zone_rele(zone); 25090Sstevel@tonic-gate thread_exit(); 25100Sstevel@tonic-gate /* NOTREACHED */ 25110Sstevel@tonic-gate } 25120Sstevel@tonic-gate 25130Sstevel@tonic-gate static void 25140Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 25150Sstevel@tonic-gate { 25160Sstevel@tonic-gate vnode_t *oldvp; 25170Sstevel@tonic-gate 25180Sstevel@tonic-gate /* we're going to hold a reference here to the directory */ 25190Sstevel@tonic-gate VN_HOLD(vp); 25200Sstevel@tonic-gate 25210Sstevel@tonic-gate #ifdef C2_AUDIT 25220Sstevel@tonic-gate if (audit_active) /* update abs cwd/root path see c2audit.c */ 25230Sstevel@tonic-gate audit_chdirec(vp, vpp); 25240Sstevel@tonic-gate #endif 25250Sstevel@tonic-gate 25260Sstevel@tonic-gate mutex_enter(&pp->p_lock); 25270Sstevel@tonic-gate oldvp = *vpp; 25280Sstevel@tonic-gate *vpp = vp; 25290Sstevel@tonic-gate mutex_exit(&pp->p_lock); 25300Sstevel@tonic-gate if (oldvp != NULL) 25310Sstevel@tonic-gate VN_RELE(oldvp); 25320Sstevel@tonic-gate } 25330Sstevel@tonic-gate 25340Sstevel@tonic-gate /* 25350Sstevel@tonic-gate * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 25360Sstevel@tonic-gate */ 25370Sstevel@tonic-gate static int 25380Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 25390Sstevel@tonic-gate { 25400Sstevel@tonic-gate nvpair_t *nvp = NULL; 25410Sstevel@tonic-gate boolean_t priv_set = B_FALSE; 25420Sstevel@tonic-gate boolean_t limit_set = B_FALSE; 25430Sstevel@tonic-gate boolean_t action_set = B_FALSE; 25440Sstevel@tonic-gate 25450Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 25460Sstevel@tonic-gate const char *name; 25470Sstevel@tonic-gate uint64_t ui64; 25480Sstevel@tonic-gate 25490Sstevel@tonic-gate name = nvpair_name(nvp); 25500Sstevel@tonic-gate if (nvpair_type(nvp) != DATA_TYPE_UINT64) 25510Sstevel@tonic-gate return (EINVAL); 25520Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &ui64); 25530Sstevel@tonic-gate if (strcmp(name, "privilege") == 0) { 25540Sstevel@tonic-gate /* 25550Sstevel@tonic-gate * Currently only privileged values are allowed, but 25560Sstevel@tonic-gate * this may change in the future. 25570Sstevel@tonic-gate */ 25580Sstevel@tonic-gate if (ui64 != RCPRIV_PRIVILEGED) 25590Sstevel@tonic-gate return (EINVAL); 25600Sstevel@tonic-gate rv->rcv_privilege = ui64; 25610Sstevel@tonic-gate priv_set = B_TRUE; 25620Sstevel@tonic-gate } else if (strcmp(name, "limit") == 0) { 25630Sstevel@tonic-gate rv->rcv_value = ui64; 25640Sstevel@tonic-gate limit_set = B_TRUE; 25650Sstevel@tonic-gate } else if (strcmp(name, "action") == 0) { 25660Sstevel@tonic-gate if (ui64 != RCTL_LOCAL_NOACTION && 25670Sstevel@tonic-gate ui64 != RCTL_LOCAL_DENY) 25680Sstevel@tonic-gate return (EINVAL); 25690Sstevel@tonic-gate rv->rcv_flagaction = ui64; 25700Sstevel@tonic-gate action_set = B_TRUE; 25710Sstevel@tonic-gate } else { 25720Sstevel@tonic-gate return (EINVAL); 25730Sstevel@tonic-gate } 25740Sstevel@tonic-gate } 25750Sstevel@tonic-gate 25760Sstevel@tonic-gate if (!(priv_set && limit_set && action_set)) 25770Sstevel@tonic-gate return (EINVAL); 25780Sstevel@tonic-gate rv->rcv_action_signal = 0; 25790Sstevel@tonic-gate rv->rcv_action_recipient = NULL; 25800Sstevel@tonic-gate rv->rcv_action_recip_pid = -1; 25810Sstevel@tonic-gate rv->rcv_firing_time = 0; 25820Sstevel@tonic-gate 25830Sstevel@tonic-gate return (0); 25840Sstevel@tonic-gate } 25850Sstevel@tonic-gate 25862267Sdp /* 25872267Sdp * Non-global zone version of start_init. 25882267Sdp */ 25890Sstevel@tonic-gate void 25902267Sdp zone_start_init(void) 25910Sstevel@tonic-gate { 25920Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 25932712Snn35248 zone_t *z = p->p_zone; 25942267Sdp 25952267Sdp ASSERT(!INGLOBALZONE(curproc)); 25960Sstevel@tonic-gate 25970Sstevel@tonic-gate /* 25982712Snn35248 * For all purposes (ZONE_ATTR_INITPID and restart_init), 25992712Snn35248 * storing just the pid of init is sufficient. 26002712Snn35248 */ 26012712Snn35248 z->zone_proc_initpid = p->p_pid; 26022712Snn35248 26032712Snn35248 /* 26042267Sdp * We maintain zone_boot_err so that we can return the cause of the 26052267Sdp * failure back to the caller of the zone_boot syscall. 26060Sstevel@tonic-gate */ 26072267Sdp p->p_zone->zone_boot_err = start_init_common(); 26080Sstevel@tonic-gate 26090Sstevel@tonic-gate mutex_enter(&zone_status_lock); 26102712Snn35248 if (z->zone_boot_err != 0) { 26110Sstevel@tonic-gate /* 26120Sstevel@tonic-gate * Make sure we are still in the booting state-- we could have 26130Sstevel@tonic-gate * raced and already be shutting down, or even further along. 26140Sstevel@tonic-gate */ 26152712Snn35248 if (zone_status_get(z) == ZONE_IS_BOOTING) 26162712Snn35248 zone_status_set(z, ZONE_IS_SHUTTING_DOWN); 26170Sstevel@tonic-gate mutex_exit(&zone_status_lock); 26180Sstevel@tonic-gate /* It's gone bad, dispose of the process */ 26192712Snn35248 if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) { 2620390Sraf mutex_enter(&p->p_lock); 2621390Sraf ASSERT(p->p_flag & SEXITLWPS); 26220Sstevel@tonic-gate lwp_exit(); 26230Sstevel@tonic-gate } 26240Sstevel@tonic-gate } else { 26252712Snn35248 if (zone_status_get(z) == ZONE_IS_BOOTING) 26262712Snn35248 zone_status_set(z, ZONE_IS_RUNNING); 26270Sstevel@tonic-gate mutex_exit(&zone_status_lock); 26280Sstevel@tonic-gate /* cause the process to return to userland. */ 26290Sstevel@tonic-gate lwp_rtt(); 26300Sstevel@tonic-gate } 26310Sstevel@tonic-gate } 26320Sstevel@tonic-gate 26330Sstevel@tonic-gate struct zsched_arg { 26340Sstevel@tonic-gate zone_t *zone; 26350Sstevel@tonic-gate nvlist_t *nvlist; 26360Sstevel@tonic-gate }; 26370Sstevel@tonic-gate 26380Sstevel@tonic-gate /* 26390Sstevel@tonic-gate * Per-zone "sched" workalike. The similarity to "sched" doesn't have 26400Sstevel@tonic-gate * anything to do with scheduling, but rather with the fact that 26410Sstevel@tonic-gate * per-zone kernel threads are parented to zsched, just like regular 26420Sstevel@tonic-gate * kernel threads are parented to sched (p0). 26430Sstevel@tonic-gate * 26440Sstevel@tonic-gate * zsched is also responsible for launching init for the zone. 26450Sstevel@tonic-gate */ 26460Sstevel@tonic-gate static void 26470Sstevel@tonic-gate zsched(void *arg) 26480Sstevel@tonic-gate { 26490Sstevel@tonic-gate struct zsched_arg *za = arg; 26500Sstevel@tonic-gate proc_t *pp = curproc; 26510Sstevel@tonic-gate proc_t *initp = proc_init; 26520Sstevel@tonic-gate zone_t *zone = za->zone; 26530Sstevel@tonic-gate cred_t *cr, *oldcred; 26540Sstevel@tonic-gate rctl_set_t *set; 26550Sstevel@tonic-gate rctl_alloc_gp_t *gp; 26560Sstevel@tonic-gate contract_t *ct = NULL; 26570Sstevel@tonic-gate task_t *tk, *oldtk; 26580Sstevel@tonic-gate rctl_entity_p_t e; 26590Sstevel@tonic-gate kproject_t *pj; 26600Sstevel@tonic-gate 26610Sstevel@tonic-gate nvlist_t *nvl = za->nvlist; 26620Sstevel@tonic-gate nvpair_t *nvp = NULL; 26630Sstevel@tonic-gate 26640Sstevel@tonic-gate bcopy("zsched", u.u_psargs, sizeof ("zsched")); 26650Sstevel@tonic-gate bcopy("zsched", u.u_comm, sizeof ("zsched")); 26660Sstevel@tonic-gate u.u_argc = 0; 26670Sstevel@tonic-gate u.u_argv = NULL; 26680Sstevel@tonic-gate u.u_envp = NULL; 26690Sstevel@tonic-gate closeall(P_FINFO(pp)); 26700Sstevel@tonic-gate 26710Sstevel@tonic-gate /* 26720Sstevel@tonic-gate * We are this zone's "zsched" process. As the zone isn't generally 26730Sstevel@tonic-gate * visible yet we don't need to grab any locks before initializing its 26740Sstevel@tonic-gate * zone_proc pointer. 26750Sstevel@tonic-gate */ 26760Sstevel@tonic-gate zone_hold(zone); /* this hold is released by zone_destroy() */ 26770Sstevel@tonic-gate zone->zone_zsched = pp; 26780Sstevel@tonic-gate mutex_enter(&pp->p_lock); 26790Sstevel@tonic-gate pp->p_zone = zone; 26800Sstevel@tonic-gate mutex_exit(&pp->p_lock); 26810Sstevel@tonic-gate 26820Sstevel@tonic-gate /* 26830Sstevel@tonic-gate * Disassociate process from its 'parent'; parent ourselves to init 26840Sstevel@tonic-gate * (pid 1) and change other values as needed. 26850Sstevel@tonic-gate */ 26860Sstevel@tonic-gate sess_create(); 26870Sstevel@tonic-gate 26880Sstevel@tonic-gate mutex_enter(&pidlock); 26890Sstevel@tonic-gate proc_detach(pp); 26900Sstevel@tonic-gate pp->p_ppid = 1; 26910Sstevel@tonic-gate pp->p_flag |= SZONETOP; 26920Sstevel@tonic-gate pp->p_ancpid = 1; 26930Sstevel@tonic-gate pp->p_parent = initp; 26940Sstevel@tonic-gate pp->p_psibling = NULL; 26950Sstevel@tonic-gate if (initp->p_child) 26960Sstevel@tonic-gate initp->p_child->p_psibling = pp; 26970Sstevel@tonic-gate pp->p_sibling = initp->p_child; 26980Sstevel@tonic-gate initp->p_child = pp; 26990Sstevel@tonic-gate 27000Sstevel@tonic-gate /* Decrement what newproc() incremented. */ 27010Sstevel@tonic-gate upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 27020Sstevel@tonic-gate /* 27030Sstevel@tonic-gate * Our credentials are about to become kcred-like, so we don't care 27040Sstevel@tonic-gate * about the caller's ruid. 27050Sstevel@tonic-gate */ 27060Sstevel@tonic-gate upcount_inc(crgetruid(kcred), zone->zone_id); 27070Sstevel@tonic-gate mutex_exit(&pidlock); 27080Sstevel@tonic-gate 27090Sstevel@tonic-gate /* 27100Sstevel@tonic-gate * getting out of global zone, so decrement lwp counts 27110Sstevel@tonic-gate */ 27120Sstevel@tonic-gate pj = pp->p_task->tk_proj; 27130Sstevel@tonic-gate mutex_enter(&global_zone->zone_nlwps_lock); 27140Sstevel@tonic-gate pj->kpj_nlwps -= pp->p_lwpcnt; 27150Sstevel@tonic-gate global_zone->zone_nlwps -= pp->p_lwpcnt; 27160Sstevel@tonic-gate mutex_exit(&global_zone->zone_nlwps_lock); 27170Sstevel@tonic-gate 27180Sstevel@tonic-gate /* 27192768Ssl108498 * Decrement locked memory counts on old zone and project. 27202768Ssl108498 */ 2721*3247Sgjelinek mutex_enter(&global_zone->zone_mem_lock); 27222768Ssl108498 global_zone->zone_locked_mem -= pp->p_locked_mem; 27232768Ssl108498 pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 2724*3247Sgjelinek mutex_exit(&global_zone->zone_mem_lock); 27252768Ssl108498 27262768Ssl108498 /* 27270Sstevel@tonic-gate * Create and join a new task in project '0' of this zone. 27280Sstevel@tonic-gate * 27290Sstevel@tonic-gate * We don't need to call holdlwps() since we know we're the only lwp in 27300Sstevel@tonic-gate * this process. 27310Sstevel@tonic-gate * 27320Sstevel@tonic-gate * task_join() returns with p_lock held. 27330Sstevel@tonic-gate */ 27340Sstevel@tonic-gate tk = task_create(0, zone); 27350Sstevel@tonic-gate mutex_enter(&cpu_lock); 27360Sstevel@tonic-gate oldtk = task_join(tk, 0); 27372768Ssl108498 27382768Ssl108498 pj = pp->p_task->tk_proj; 27392768Ssl108498 2740*3247Sgjelinek mutex_enter(&zone->zone_mem_lock); 27412768Ssl108498 zone->zone_locked_mem += pp->p_locked_mem; 27422768Ssl108498 pj->kpj_data.kpd_locked_mem += pp->p_locked_mem; 2743*3247Sgjelinek mutex_exit(&zone->zone_mem_lock); 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate /* 27460Sstevel@tonic-gate * add lwp counts to zsched's zone, and increment project's task count 27470Sstevel@tonic-gate * due to the task created in the above tasksys_settaskid 27480Sstevel@tonic-gate */ 27492768Ssl108498 27500Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 27510Sstevel@tonic-gate pj->kpj_nlwps += pp->p_lwpcnt; 27520Sstevel@tonic-gate pj->kpj_ntasks += 1; 27530Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 27540Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 27550Sstevel@tonic-gate 27562768Ssl108498 mutex_exit(&curproc->p_lock); 27572768Ssl108498 mutex_exit(&cpu_lock); 27582768Ssl108498 task_rele(oldtk); 27592768Ssl108498 27600Sstevel@tonic-gate /* 27610Sstevel@tonic-gate * The process was created by a process in the global zone, hence the 27620Sstevel@tonic-gate * credentials are wrong. We might as well have kcred-ish credentials. 27630Sstevel@tonic-gate */ 27640Sstevel@tonic-gate cr = zone->zone_kcred; 27650Sstevel@tonic-gate crhold(cr); 27660Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 27670Sstevel@tonic-gate oldcred = pp->p_cred; 27680Sstevel@tonic-gate pp->p_cred = cr; 27690Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 27700Sstevel@tonic-gate crfree(oldcred); 27710Sstevel@tonic-gate 27720Sstevel@tonic-gate /* 27730Sstevel@tonic-gate * Hold credentials again (for thread) 27740Sstevel@tonic-gate */ 27750Sstevel@tonic-gate crhold(cr); 27760Sstevel@tonic-gate 27770Sstevel@tonic-gate /* 27780Sstevel@tonic-gate * p_lwpcnt can't change since this is a kernel process. 27790Sstevel@tonic-gate */ 27800Sstevel@tonic-gate crset(pp, cr); 27810Sstevel@tonic-gate 27820Sstevel@tonic-gate /* 27830Sstevel@tonic-gate * Chroot 27840Sstevel@tonic-gate */ 27850Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 27860Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 27870Sstevel@tonic-gate 27880Sstevel@tonic-gate /* 27890Sstevel@tonic-gate * Initialize zone's rctl set. 27900Sstevel@tonic-gate */ 27910Sstevel@tonic-gate set = rctl_set_create(); 27920Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 27930Sstevel@tonic-gate mutex_enter(&pp->p_lock); 27940Sstevel@tonic-gate e.rcep_p.zone = zone; 27950Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 27960Sstevel@tonic-gate zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 27970Sstevel@tonic-gate mutex_exit(&pp->p_lock); 27980Sstevel@tonic-gate rctl_prealloc_destroy(gp); 27990Sstevel@tonic-gate 28000Sstevel@tonic-gate /* 28010Sstevel@tonic-gate * Apply the rctls passed in to zone_create(). This is basically a list 28020Sstevel@tonic-gate * assignment: all of the old values are removed and the new ones 28030Sstevel@tonic-gate * inserted. That is, if an empty list is passed in, all values are 28040Sstevel@tonic-gate * removed. 28050Sstevel@tonic-gate */ 28060Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 28070Sstevel@tonic-gate rctl_dict_entry_t *rde; 28080Sstevel@tonic-gate rctl_hndl_t hndl; 28090Sstevel@tonic-gate char *name; 28100Sstevel@tonic-gate nvlist_t **nvlarray; 28110Sstevel@tonic-gate uint_t i, nelem; 28120Sstevel@tonic-gate int error; /* For ASSERT()s */ 28130Sstevel@tonic-gate 28140Sstevel@tonic-gate name = nvpair_name(nvp); 28150Sstevel@tonic-gate hndl = rctl_hndl_lookup(name); 28160Sstevel@tonic-gate ASSERT(hndl != -1); 28170Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 28180Sstevel@tonic-gate ASSERT(rde != NULL); 28190Sstevel@tonic-gate 28200Sstevel@tonic-gate for (; /* ever */; ) { 28210Sstevel@tonic-gate rctl_val_t oval; 28220Sstevel@tonic-gate 28230Sstevel@tonic-gate mutex_enter(&pp->p_lock); 28240Sstevel@tonic-gate error = rctl_local_get(hndl, NULL, &oval, pp); 28250Sstevel@tonic-gate mutex_exit(&pp->p_lock); 28260Sstevel@tonic-gate ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 28270Sstevel@tonic-gate ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 28280Sstevel@tonic-gate if (oval.rcv_privilege == RCPRIV_SYSTEM) 28290Sstevel@tonic-gate break; 28300Sstevel@tonic-gate mutex_enter(&pp->p_lock); 28310Sstevel@tonic-gate error = rctl_local_delete(hndl, &oval, pp); 28320Sstevel@tonic-gate mutex_exit(&pp->p_lock); 28330Sstevel@tonic-gate ASSERT(error == 0); 28340Sstevel@tonic-gate } 28350Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 28360Sstevel@tonic-gate ASSERT(error == 0); 28370Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 28380Sstevel@tonic-gate rctl_val_t *nvalp; 28390Sstevel@tonic-gate 28400Sstevel@tonic-gate nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 28410Sstevel@tonic-gate error = nvlist2rctlval(nvlarray[i], nvalp); 28420Sstevel@tonic-gate ASSERT(error == 0); 28430Sstevel@tonic-gate /* 28440Sstevel@tonic-gate * rctl_local_insert can fail if the value being 28450Sstevel@tonic-gate * inserted is a duplicate; this is OK. 28460Sstevel@tonic-gate */ 28470Sstevel@tonic-gate mutex_enter(&pp->p_lock); 28480Sstevel@tonic-gate if (rctl_local_insert(hndl, nvalp, pp) != 0) 28490Sstevel@tonic-gate kmem_cache_free(rctl_val_cache, nvalp); 28500Sstevel@tonic-gate mutex_exit(&pp->p_lock); 28510Sstevel@tonic-gate } 28520Sstevel@tonic-gate } 28530Sstevel@tonic-gate /* 28540Sstevel@tonic-gate * Tell the world that we're done setting up. 28550Sstevel@tonic-gate * 28560Sstevel@tonic-gate * At this point we want to set the zone status to ZONE_IS_READY 28570Sstevel@tonic-gate * and atomically set the zone's processor set visibility. Once 28580Sstevel@tonic-gate * we drop pool_lock() this zone will automatically get updated 28590Sstevel@tonic-gate * to reflect any future changes to the pools configuration. 28600Sstevel@tonic-gate */ 28610Sstevel@tonic-gate pool_lock(); 28620Sstevel@tonic-gate mutex_enter(&cpu_lock); 28630Sstevel@tonic-gate mutex_enter(&zonehash_lock); 28640Sstevel@tonic-gate zone_uniqid(zone); 28650Sstevel@tonic-gate zone_zsd_configure(zone); 28660Sstevel@tonic-gate if (pool_state == POOL_ENABLED) 28670Sstevel@tonic-gate zone_pset_set(zone, pool_default->pool_pset->pset_id); 28680Sstevel@tonic-gate mutex_enter(&zone_status_lock); 28690Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 28700Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_READY); 28710Sstevel@tonic-gate mutex_exit(&zone_status_lock); 28720Sstevel@tonic-gate mutex_exit(&zonehash_lock); 28730Sstevel@tonic-gate mutex_exit(&cpu_lock); 28740Sstevel@tonic-gate pool_unlock(); 28750Sstevel@tonic-gate 28760Sstevel@tonic-gate /* 28770Sstevel@tonic-gate * Once we see the zone transition to the ZONE_IS_BOOTING state, 28780Sstevel@tonic-gate * we launch init, and set the state to running. 28790Sstevel@tonic-gate */ 28800Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 28810Sstevel@tonic-gate 28820Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_BOOTING) { 28830Sstevel@tonic-gate id_t cid; 28840Sstevel@tonic-gate 28850Sstevel@tonic-gate /* 28860Sstevel@tonic-gate * Ok, this is a little complicated. We need to grab the 28870Sstevel@tonic-gate * zone's pool's scheduling class ID; note that by now, we 28880Sstevel@tonic-gate * are already bound to a pool if we need to be (zoneadmd 28890Sstevel@tonic-gate * will have done that to us while we're in the READY 28900Sstevel@tonic-gate * state). *But* the scheduling class for the zone's 'init' 28910Sstevel@tonic-gate * must be explicitly passed to newproc, which doesn't 28920Sstevel@tonic-gate * respect pool bindings. 28930Sstevel@tonic-gate * 28940Sstevel@tonic-gate * We hold the pool_lock across the call to newproc() to 28950Sstevel@tonic-gate * close the obvious race: the pool's scheduling class 28960Sstevel@tonic-gate * could change before we manage to create the LWP with 28970Sstevel@tonic-gate * classid 'cid'. 28980Sstevel@tonic-gate */ 28990Sstevel@tonic-gate pool_lock(); 2900*3247Sgjelinek if (zone->zone_defaultcid > 0) 2901*3247Sgjelinek cid = zone->zone_defaultcid; 2902*3247Sgjelinek else 2903*3247Sgjelinek cid = pool_get_class(zone->zone_pool); 29040Sstevel@tonic-gate if (cid == -1) 29050Sstevel@tonic-gate cid = defaultcid; 29060Sstevel@tonic-gate 29070Sstevel@tonic-gate /* 29080Sstevel@tonic-gate * If this fails, zone_boot will ultimately fail. The 29090Sstevel@tonic-gate * state of the zone will be set to SHUTTING_DOWN-- userland 29100Sstevel@tonic-gate * will have to tear down the zone, and fail, or try again. 29110Sstevel@tonic-gate */ 29122267Sdp if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid, 29130Sstevel@tonic-gate minclsyspri - 1, &ct)) != 0) { 29140Sstevel@tonic-gate mutex_enter(&zone_status_lock); 29150Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 29160Sstevel@tonic-gate mutex_exit(&zone_status_lock); 29170Sstevel@tonic-gate } 29180Sstevel@tonic-gate pool_unlock(); 29190Sstevel@tonic-gate } 29200Sstevel@tonic-gate 29210Sstevel@tonic-gate /* 29220Sstevel@tonic-gate * Wait for zone_destroy() to be called. This is what we spend 29230Sstevel@tonic-gate * most of our life doing. 29240Sstevel@tonic-gate */ 29250Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 29260Sstevel@tonic-gate 29270Sstevel@tonic-gate if (ct) 29280Sstevel@tonic-gate /* 29290Sstevel@tonic-gate * At this point the process contract should be empty. 29300Sstevel@tonic-gate * (Though if it isn't, it's not the end of the world.) 29310Sstevel@tonic-gate */ 29320Sstevel@tonic-gate VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 29330Sstevel@tonic-gate 29340Sstevel@tonic-gate /* 29350Sstevel@tonic-gate * Allow kcred to be freed when all referring processes 29360Sstevel@tonic-gate * (including this one) go away. We can't just do this in 29370Sstevel@tonic-gate * zone_free because we need to wait for the zone_cred_ref to 29380Sstevel@tonic-gate * drop to 0 before calling zone_free, and the existence of 29390Sstevel@tonic-gate * zone_kcred will prevent that. Thus, we call crfree here to 29400Sstevel@tonic-gate * balance the crdup in zone_create. The crhold calls earlier 29410Sstevel@tonic-gate * in zsched will be dropped when the thread and process exit. 29420Sstevel@tonic-gate */ 29430Sstevel@tonic-gate crfree(zone->zone_kcred); 29440Sstevel@tonic-gate zone->zone_kcred = NULL; 29450Sstevel@tonic-gate 29460Sstevel@tonic-gate exit(CLD_EXITED, 0); 29470Sstevel@tonic-gate } 29480Sstevel@tonic-gate 29490Sstevel@tonic-gate /* 29500Sstevel@tonic-gate * Helper function to determine if there are any submounts of the 29510Sstevel@tonic-gate * provided path. Used to make sure the zone doesn't "inherit" any 29520Sstevel@tonic-gate * mounts from before it is created. 29530Sstevel@tonic-gate */ 29540Sstevel@tonic-gate static uint_t 29550Sstevel@tonic-gate zone_mount_count(const char *rootpath) 29560Sstevel@tonic-gate { 29570Sstevel@tonic-gate vfs_t *vfsp; 29580Sstevel@tonic-gate uint_t count = 0; 29590Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 29600Sstevel@tonic-gate 29610Sstevel@tonic-gate /* 29620Sstevel@tonic-gate * Holding zonehash_lock prevents race conditions with 29630Sstevel@tonic-gate * vfs_list_add()/vfs_list_remove() since we serialize with 29640Sstevel@tonic-gate * zone_find_by_path(). 29650Sstevel@tonic-gate */ 29660Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 29670Sstevel@tonic-gate /* 29680Sstevel@tonic-gate * The rootpath must end with a '/' 29690Sstevel@tonic-gate */ 29700Sstevel@tonic-gate ASSERT(rootpath[rootpathlen - 1] == '/'); 29710Sstevel@tonic-gate 29720Sstevel@tonic-gate /* 29730Sstevel@tonic-gate * This intentionally does not count the rootpath itself if that 29740Sstevel@tonic-gate * happens to be a mount point. 29750Sstevel@tonic-gate */ 29760Sstevel@tonic-gate vfs_list_read_lock(); 29770Sstevel@tonic-gate vfsp = rootvfs; 29780Sstevel@tonic-gate do { 29790Sstevel@tonic-gate if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 29800Sstevel@tonic-gate rootpathlen) == 0) 29810Sstevel@tonic-gate count++; 29820Sstevel@tonic-gate vfsp = vfsp->vfs_next; 29830Sstevel@tonic-gate } while (vfsp != rootvfs); 29840Sstevel@tonic-gate vfs_list_unlock(); 29850Sstevel@tonic-gate return (count); 29860Sstevel@tonic-gate } 29870Sstevel@tonic-gate 29880Sstevel@tonic-gate /* 29890Sstevel@tonic-gate * Helper function to make sure that a zone created on 'rootpath' 29900Sstevel@tonic-gate * wouldn't end up containing other zones' rootpaths. 29910Sstevel@tonic-gate */ 29920Sstevel@tonic-gate static boolean_t 29930Sstevel@tonic-gate zone_is_nested(const char *rootpath) 29940Sstevel@tonic-gate { 29950Sstevel@tonic-gate zone_t *zone; 29960Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 29970Sstevel@tonic-gate size_t len; 29980Sstevel@tonic-gate 29990Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 30000Sstevel@tonic-gate 30010Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 30020Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 30030Sstevel@tonic-gate if (zone == global_zone) 30040Sstevel@tonic-gate continue; 30050Sstevel@tonic-gate len = strlen(zone->zone_rootpath); 30060Sstevel@tonic-gate if (strncmp(rootpath, zone->zone_rootpath, 30070Sstevel@tonic-gate MIN(rootpathlen, len)) == 0) 30080Sstevel@tonic-gate return (B_TRUE); 30090Sstevel@tonic-gate } 30100Sstevel@tonic-gate return (B_FALSE); 30110Sstevel@tonic-gate } 30120Sstevel@tonic-gate 30130Sstevel@tonic-gate static int 3014813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs, 3015813Sdp size_t zone_privssz) 30160Sstevel@tonic-gate { 30170Sstevel@tonic-gate priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 30180Sstevel@tonic-gate 3019813Sdp if (zone_privssz < sizeof (priv_set_t)) 3020813Sdp return (set_errno(ENOMEM)); 3021813Sdp 30220Sstevel@tonic-gate if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 30230Sstevel@tonic-gate kmem_free(privs, sizeof (priv_set_t)); 30240Sstevel@tonic-gate return (EFAULT); 30250Sstevel@tonic-gate } 30260Sstevel@tonic-gate 30270Sstevel@tonic-gate zone->zone_privset = privs; 30280Sstevel@tonic-gate return (0); 30290Sstevel@tonic-gate } 30300Sstevel@tonic-gate 30310Sstevel@tonic-gate /* 30320Sstevel@tonic-gate * We make creative use of nvlists to pass in rctls from userland. The list is 30330Sstevel@tonic-gate * a list of the following structures: 30340Sstevel@tonic-gate * 30350Sstevel@tonic-gate * (name = rctl_name, value = nvpair_list_array) 30360Sstevel@tonic-gate * 30370Sstevel@tonic-gate * Where each element of the nvpair_list_array is of the form: 30380Sstevel@tonic-gate * 30390Sstevel@tonic-gate * [(name = "privilege", value = RCPRIV_PRIVILEGED), 30400Sstevel@tonic-gate * (name = "limit", value = uint64_t), 30410Sstevel@tonic-gate * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 30420Sstevel@tonic-gate */ 30430Sstevel@tonic-gate static int 30440Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 30450Sstevel@tonic-gate { 30460Sstevel@tonic-gate nvpair_t *nvp = NULL; 30470Sstevel@tonic-gate nvlist_t *nvl = NULL; 30480Sstevel@tonic-gate char *kbuf; 30490Sstevel@tonic-gate int error; 30500Sstevel@tonic-gate rctl_val_t rv; 30510Sstevel@tonic-gate 30520Sstevel@tonic-gate *nvlp = NULL; 30530Sstevel@tonic-gate 30540Sstevel@tonic-gate if (buflen == 0) 30550Sstevel@tonic-gate return (0); 30560Sstevel@tonic-gate 30570Sstevel@tonic-gate if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 30580Sstevel@tonic-gate return (ENOMEM); 30590Sstevel@tonic-gate if (copyin(ubuf, kbuf, buflen)) { 30600Sstevel@tonic-gate error = EFAULT; 30610Sstevel@tonic-gate goto out; 30620Sstevel@tonic-gate } 30630Sstevel@tonic-gate if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 30640Sstevel@tonic-gate /* 30650Sstevel@tonic-gate * nvl may have been allocated/free'd, but the value set to 30660Sstevel@tonic-gate * non-NULL, so we reset it here. 30670Sstevel@tonic-gate */ 30680Sstevel@tonic-gate nvl = NULL; 30690Sstevel@tonic-gate error = EINVAL; 30700Sstevel@tonic-gate goto out; 30710Sstevel@tonic-gate } 30720Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 30730Sstevel@tonic-gate rctl_dict_entry_t *rde; 30740Sstevel@tonic-gate rctl_hndl_t hndl; 30750Sstevel@tonic-gate nvlist_t **nvlarray; 30760Sstevel@tonic-gate uint_t i, nelem; 30770Sstevel@tonic-gate char *name; 30780Sstevel@tonic-gate 30790Sstevel@tonic-gate error = EINVAL; 30800Sstevel@tonic-gate name = nvpair_name(nvp); 30810Sstevel@tonic-gate if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 30820Sstevel@tonic-gate != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 30830Sstevel@tonic-gate goto out; 30840Sstevel@tonic-gate } 30850Sstevel@tonic-gate if ((hndl = rctl_hndl_lookup(name)) == -1) { 30860Sstevel@tonic-gate goto out; 30870Sstevel@tonic-gate } 30880Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 30890Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 30900Sstevel@tonic-gate ASSERT(error == 0); 30910Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 30920Sstevel@tonic-gate if (error = nvlist2rctlval(nvlarray[i], &rv)) 30930Sstevel@tonic-gate goto out; 30940Sstevel@tonic-gate } 30950Sstevel@tonic-gate if (rctl_invalid_value(rde, &rv)) { 30960Sstevel@tonic-gate error = EINVAL; 30970Sstevel@tonic-gate goto out; 30980Sstevel@tonic-gate } 30990Sstevel@tonic-gate } 31000Sstevel@tonic-gate error = 0; 31010Sstevel@tonic-gate *nvlp = nvl; 31020Sstevel@tonic-gate out: 31030Sstevel@tonic-gate kmem_free(kbuf, buflen); 31040Sstevel@tonic-gate if (error && nvl != NULL) 31050Sstevel@tonic-gate nvlist_free(nvl); 31060Sstevel@tonic-gate return (error); 31070Sstevel@tonic-gate } 31080Sstevel@tonic-gate 31090Sstevel@tonic-gate int 31100Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) { 31110Sstevel@tonic-gate if (er_out != NULL) { 31120Sstevel@tonic-gate if (copyout(&er_ext, er_out, sizeof (int))) { 31130Sstevel@tonic-gate return (set_errno(EFAULT)); 31140Sstevel@tonic-gate } 31150Sstevel@tonic-gate } 31160Sstevel@tonic-gate return (set_errno(er_error)); 31170Sstevel@tonic-gate } 31180Sstevel@tonic-gate 31191676Sjpk static int 31201676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi) 31211676Sjpk { 31221676Sjpk ts_label_t *tsl; 31231676Sjpk bslabel_t blab; 31241676Sjpk 31251676Sjpk /* Get label from user */ 31261676Sjpk if (copyin(lab, &blab, sizeof (blab)) != 0) 31271676Sjpk return (EFAULT); 31281676Sjpk tsl = labelalloc(&blab, doi, KM_NOSLEEP); 31291676Sjpk if (tsl == NULL) 31301676Sjpk return (ENOMEM); 31311676Sjpk 31321676Sjpk zone->zone_slabel = tsl; 31331676Sjpk return (0); 31341676Sjpk } 31351676Sjpk 31360Sstevel@tonic-gate /* 3137789Sahrens * Parses a comma-separated list of ZFS datasets into a per-zone dictionary. 3138789Sahrens */ 3139789Sahrens static int 3140789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen) 3141789Sahrens { 3142789Sahrens char *kbuf; 3143789Sahrens char *dataset, *next; 3144789Sahrens zone_dataset_t *zd; 3145789Sahrens size_t len; 3146789Sahrens 3147789Sahrens if (ubuf == NULL || buflen == 0) 3148789Sahrens return (0); 3149789Sahrens 3150789Sahrens if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 3151789Sahrens return (ENOMEM); 3152789Sahrens 3153789Sahrens if (copyin(ubuf, kbuf, buflen) != 0) { 3154789Sahrens kmem_free(kbuf, buflen); 3155789Sahrens return (EFAULT); 3156789Sahrens } 3157789Sahrens 3158789Sahrens dataset = next = kbuf; 3159789Sahrens for (;;) { 3160789Sahrens zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP); 3161789Sahrens 3162789Sahrens next = strchr(dataset, ','); 3163789Sahrens 3164789Sahrens if (next == NULL) 3165789Sahrens len = strlen(dataset); 3166789Sahrens else 3167789Sahrens len = next - dataset; 3168789Sahrens 3169789Sahrens zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP); 3170789Sahrens bcopy(dataset, zd->zd_dataset, len); 3171789Sahrens zd->zd_dataset[len] = '\0'; 3172789Sahrens 3173789Sahrens list_insert_head(&zone->zone_datasets, zd); 3174789Sahrens 3175789Sahrens if (next == NULL) 3176789Sahrens break; 3177789Sahrens 3178789Sahrens dataset = next + 1; 3179789Sahrens } 3180789Sahrens 3181789Sahrens kmem_free(kbuf, buflen); 3182789Sahrens return (0); 3183789Sahrens } 3184789Sahrens 3185789Sahrens /* 31860Sstevel@tonic-gate * System call to create/initialize a new zone named 'zone_name', rooted 31870Sstevel@tonic-gate * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 31881676Sjpk * and initialized with the zone-wide rctls described in 'rctlbuf', and 31891676Sjpk * with labeling set by 'match', 'doi', and 'label'. 31900Sstevel@tonic-gate * 31910Sstevel@tonic-gate * If extended error is non-null, we may use it to return more detailed 31920Sstevel@tonic-gate * error information. 31930Sstevel@tonic-gate */ 31940Sstevel@tonic-gate static zoneid_t 31950Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root, 3196813Sdp const priv_set_t *zone_privs, size_t zone_privssz, 3197813Sdp caddr_t rctlbuf, size_t rctlbufsz, 31981676Sjpk caddr_t zfsbuf, size_t zfsbufsz, int *extended_error, 31991676Sjpk int match, uint32_t doi, const bslabel_t *label) 32000Sstevel@tonic-gate { 32010Sstevel@tonic-gate struct zsched_arg zarg; 32020Sstevel@tonic-gate nvlist_t *rctls = NULL; 32030Sstevel@tonic-gate proc_t *pp = curproc; 32040Sstevel@tonic-gate zone_t *zone, *ztmp; 32050Sstevel@tonic-gate zoneid_t zoneid; 32060Sstevel@tonic-gate int error; 32070Sstevel@tonic-gate int error2 = 0; 32080Sstevel@tonic-gate char *str; 32090Sstevel@tonic-gate cred_t *zkcr; 32101769Scarlsonj boolean_t insert_label_hash; 32110Sstevel@tonic-gate 32120Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 32130Sstevel@tonic-gate return (set_errno(EPERM)); 32140Sstevel@tonic-gate 32150Sstevel@tonic-gate /* can't boot zone from within chroot environment */ 32160Sstevel@tonic-gate if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 32170Sstevel@tonic-gate return (zone_create_error(ENOTSUP, ZE_CHROOTED, 3218813Sdp extended_error)); 32190Sstevel@tonic-gate 32200Sstevel@tonic-gate zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 32210Sstevel@tonic-gate zoneid = zone->zone_id = id_alloc(zoneid_space); 32220Sstevel@tonic-gate zone->zone_status = ZONE_IS_UNINITIALIZED; 32230Sstevel@tonic-gate zone->zone_pool = pool_default; 32240Sstevel@tonic-gate zone->zone_pool_mod = gethrtime(); 32250Sstevel@tonic-gate zone->zone_psetid = ZONE_PS_INVAL; 32260Sstevel@tonic-gate zone->zone_ncpus = 0; 32270Sstevel@tonic-gate zone->zone_ncpus_online = 0; 32282712Snn35248 zone->zone_restart_init = B_TRUE; 32292712Snn35248 zone->zone_brand = &native_brand; 32302712Snn35248 zone->zone_initname = NULL; 32310Sstevel@tonic-gate mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 32320Sstevel@tonic-gate mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 3233*3247Sgjelinek mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 32340Sstevel@tonic-gate cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 32350Sstevel@tonic-gate list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 32360Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 3237789Sahrens list_create(&zone->zone_datasets, sizeof (zone_dataset_t), 3238789Sahrens offsetof(zone_dataset_t, zd_linkage)); 32391676Sjpk rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 32400Sstevel@tonic-gate 32410Sstevel@tonic-gate if ((error = zone_set_name(zone, zone_name)) != 0) { 32420Sstevel@tonic-gate zone_free(zone); 32430Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 32440Sstevel@tonic-gate } 32450Sstevel@tonic-gate 32460Sstevel@tonic-gate if ((error = zone_set_root(zone, zone_root)) != 0) { 32470Sstevel@tonic-gate zone_free(zone); 32480Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 32490Sstevel@tonic-gate } 3250813Sdp if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) { 32510Sstevel@tonic-gate zone_free(zone); 32520Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 32530Sstevel@tonic-gate } 32540Sstevel@tonic-gate 32550Sstevel@tonic-gate /* initialize node name to be the same as zone name */ 32560Sstevel@tonic-gate zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 32570Sstevel@tonic-gate (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 32580Sstevel@tonic-gate zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 32590Sstevel@tonic-gate 32600Sstevel@tonic-gate zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 32610Sstevel@tonic-gate zone->zone_domain[0] = '\0'; 32620Sstevel@tonic-gate zone->zone_shares = 1; 32632677Sml93401 zone->zone_shmmax = 0; 32642677Sml93401 zone->zone_ipc.ipcq_shmmni = 0; 32652677Sml93401 zone->zone_ipc.ipcq_semmni = 0; 32662677Sml93401 zone->zone_ipc.ipcq_msgmni = 0; 32670Sstevel@tonic-gate zone->zone_bootargs = NULL; 32682267Sdp zone->zone_initname = 32692267Sdp kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP); 32702267Sdp (void) strcpy(zone->zone_initname, zone_default_initname); 3271*3247Sgjelinek zone->zone_nlwps = 0; 3272*3247Sgjelinek zone->zone_nlwps_ctl = INT_MAX; 32732768Ssl108498 zone->zone_locked_mem = 0; 32742768Ssl108498 zone->zone_locked_mem_ctl = UINT64_MAX; 3275*3247Sgjelinek zone->zone_max_swap = 0; 3276*3247Sgjelinek zone->zone_max_swap_ctl = UINT64_MAX; 3277*3247Sgjelinek zone0.zone_lockedmem_kstat = NULL; 3278*3247Sgjelinek zone0.zone_swapresv_kstat = NULL; 32790Sstevel@tonic-gate 32800Sstevel@tonic-gate /* 32810Sstevel@tonic-gate * Zsched initializes the rctls. 32820Sstevel@tonic-gate */ 32830Sstevel@tonic-gate zone->zone_rctls = NULL; 32840Sstevel@tonic-gate 32850Sstevel@tonic-gate if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 32860Sstevel@tonic-gate zone_free(zone); 32870Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 32880Sstevel@tonic-gate } 32890Sstevel@tonic-gate 3290789Sahrens if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) { 3291789Sahrens zone_free(zone); 3292789Sahrens return (set_errno(error)); 3293789Sahrens } 3294789Sahrens 32950Sstevel@tonic-gate /* 32961676Sjpk * Read in the trusted system parameters: 32971676Sjpk * match flag and sensitivity label. 32981676Sjpk */ 32991676Sjpk zone->zone_match = match; 33001769Scarlsonj if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 33011676Sjpk error = zone_set_label(zone, label, doi); 33021676Sjpk if (error != 0) { 33031676Sjpk zone_free(zone); 33041676Sjpk return (set_errno(error)); 33051676Sjpk } 33061769Scarlsonj insert_label_hash = B_TRUE; 33071676Sjpk } else { 33081676Sjpk /* all zones get an admin_low label if system is not labeled */ 33091676Sjpk zone->zone_slabel = l_admin_low; 33101676Sjpk label_hold(l_admin_low); 33111769Scarlsonj insert_label_hash = B_FALSE; 33121676Sjpk } 33131676Sjpk 33141676Sjpk /* 33150Sstevel@tonic-gate * Stop all lwps since that's what normally happens as part of fork(). 33160Sstevel@tonic-gate * This needs to happen before we grab any locks to avoid deadlock 33170Sstevel@tonic-gate * (another lwp in the process could be waiting for the held lock). 33180Sstevel@tonic-gate */ 33190Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 33200Sstevel@tonic-gate zone_free(zone); 33210Sstevel@tonic-gate if (rctls) 33220Sstevel@tonic-gate nvlist_free(rctls); 33230Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 33240Sstevel@tonic-gate } 33250Sstevel@tonic-gate 33260Sstevel@tonic-gate if (block_mounts() == 0) { 33270Sstevel@tonic-gate mutex_enter(&pp->p_lock); 33280Sstevel@tonic-gate if (curthread != pp->p_agenttp) 33290Sstevel@tonic-gate continuelwps(pp); 33300Sstevel@tonic-gate mutex_exit(&pp->p_lock); 33310Sstevel@tonic-gate zone_free(zone); 33320Sstevel@tonic-gate if (rctls) 33330Sstevel@tonic-gate nvlist_free(rctls); 33340Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 33350Sstevel@tonic-gate } 33360Sstevel@tonic-gate 33370Sstevel@tonic-gate /* 33380Sstevel@tonic-gate * Set up credential for kernel access. After this, any errors 33390Sstevel@tonic-gate * should go through the dance in errout rather than calling 33400Sstevel@tonic-gate * zone_free directly. 33410Sstevel@tonic-gate */ 33420Sstevel@tonic-gate zone->zone_kcred = crdup(kcred); 33430Sstevel@tonic-gate crsetzone(zone->zone_kcred, zone); 33440Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 33450Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 33460Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 33470Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 33480Sstevel@tonic-gate 33490Sstevel@tonic-gate mutex_enter(&zonehash_lock); 33500Sstevel@tonic-gate /* 33510Sstevel@tonic-gate * Make sure zone doesn't already exist. 33521676Sjpk * 33531676Sjpk * If the system and zone are labeled, 33541676Sjpk * make sure no other zone exists that has the same label. 33550Sstevel@tonic-gate */ 33561676Sjpk if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL || 33571769Scarlsonj (insert_label_hash && 33581676Sjpk (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) { 33590Sstevel@tonic-gate zone_status_t status; 33600Sstevel@tonic-gate 33610Sstevel@tonic-gate status = zone_status_get(ztmp); 33620Sstevel@tonic-gate if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 33630Sstevel@tonic-gate error = EEXIST; 33640Sstevel@tonic-gate else 33650Sstevel@tonic-gate error = EBUSY; 33660Sstevel@tonic-gate goto errout; 33670Sstevel@tonic-gate } 33680Sstevel@tonic-gate 33690Sstevel@tonic-gate /* 33700Sstevel@tonic-gate * Don't allow zone creations which would cause one zone's rootpath to 33710Sstevel@tonic-gate * be accessible from that of another (non-global) zone. 33720Sstevel@tonic-gate */ 33730Sstevel@tonic-gate if (zone_is_nested(zone->zone_rootpath)) { 33740Sstevel@tonic-gate error = EBUSY; 33750Sstevel@tonic-gate goto errout; 33760Sstevel@tonic-gate } 33770Sstevel@tonic-gate 33780Sstevel@tonic-gate ASSERT(zonecount != 0); /* check for leaks */ 33790Sstevel@tonic-gate if (zonecount + 1 > maxzones) { 33800Sstevel@tonic-gate error = ENOMEM; 33810Sstevel@tonic-gate goto errout; 33820Sstevel@tonic-gate } 33830Sstevel@tonic-gate 33840Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 33850Sstevel@tonic-gate error = EBUSY; 33860Sstevel@tonic-gate error2 = ZE_AREMOUNTS; 33870Sstevel@tonic-gate goto errout; 33880Sstevel@tonic-gate } 33890Sstevel@tonic-gate 33900Sstevel@tonic-gate /* 33910Sstevel@tonic-gate * Zone is still incomplete, but we need to drop all locks while 33920Sstevel@tonic-gate * zsched() initializes this zone's kernel process. We 33930Sstevel@tonic-gate * optimistically add the zone to the hashtable and associated 33940Sstevel@tonic-gate * lists so a parallel zone_create() doesn't try to create the 33950Sstevel@tonic-gate * same zone. 33960Sstevel@tonic-gate */ 33970Sstevel@tonic-gate zonecount++; 33980Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, 33990Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id, 34000Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 34010Sstevel@tonic-gate str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 34020Sstevel@tonic-gate (void) strcpy(str, zone->zone_name); 34030Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 34040Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 34051769Scarlsonj if (insert_label_hash) { 34061676Sjpk (void) mod_hash_insert(zonehashbylabel, 34071676Sjpk (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone); 34081769Scarlsonj zone->zone_flags |= ZF_HASHED_LABEL; 34091676Sjpk } 34101676Sjpk 34110Sstevel@tonic-gate /* 34120Sstevel@tonic-gate * Insert into active list. At this point there are no 'hold's 34130Sstevel@tonic-gate * on the zone, but everyone else knows not to use it, so we can 34140Sstevel@tonic-gate * continue to use it. zsched() will do a zone_hold() if the 34150Sstevel@tonic-gate * newproc() is successful. 34160Sstevel@tonic-gate */ 34170Sstevel@tonic-gate list_insert_tail(&zone_active, zone); 34180Sstevel@tonic-gate mutex_exit(&zonehash_lock); 34190Sstevel@tonic-gate 34200Sstevel@tonic-gate zarg.zone = zone; 34210Sstevel@tonic-gate zarg.nvlist = rctls; 34220Sstevel@tonic-gate /* 34230Sstevel@tonic-gate * The process, task, and project rctls are probably wrong; 34240Sstevel@tonic-gate * we need an interface to get the default values of all rctls, 34250Sstevel@tonic-gate * and initialize zsched appropriately. I'm not sure that that 34260Sstevel@tonic-gate * makes much of a difference, though. 34270Sstevel@tonic-gate */ 34280Sstevel@tonic-gate if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) { 34290Sstevel@tonic-gate /* 34300Sstevel@tonic-gate * We need to undo all globally visible state. 34310Sstevel@tonic-gate */ 34320Sstevel@tonic-gate mutex_enter(&zonehash_lock); 34330Sstevel@tonic-gate list_remove(&zone_active, zone); 34341769Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) { 34351676Sjpk ASSERT(zone->zone_slabel != NULL); 34361676Sjpk (void) mod_hash_destroy(zonehashbylabel, 34371676Sjpk (mod_hash_key_t)zone->zone_slabel); 34381676Sjpk } 34390Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 34400Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_name); 34410Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 34420Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 34430Sstevel@tonic-gate ASSERT(zonecount > 1); 34440Sstevel@tonic-gate zonecount--; 34450Sstevel@tonic-gate goto errout; 34460Sstevel@tonic-gate } 34470Sstevel@tonic-gate 34480Sstevel@tonic-gate /* 34490Sstevel@tonic-gate * Zone creation can't fail from now on. 34500Sstevel@tonic-gate */ 34510Sstevel@tonic-gate 34520Sstevel@tonic-gate /* 3453*3247Sgjelinek * Create zone kstats 3454*3247Sgjelinek */ 3455*3247Sgjelinek zone_kstat_create(zone); 3456*3247Sgjelinek 3457*3247Sgjelinek /* 34580Sstevel@tonic-gate * Let the other lwps continue. 34590Sstevel@tonic-gate */ 34600Sstevel@tonic-gate mutex_enter(&pp->p_lock); 34610Sstevel@tonic-gate if (curthread != pp->p_agenttp) 34620Sstevel@tonic-gate continuelwps(pp); 34630Sstevel@tonic-gate mutex_exit(&pp->p_lock); 34640Sstevel@tonic-gate 34650Sstevel@tonic-gate /* 34660Sstevel@tonic-gate * Wait for zsched to finish initializing the zone. 34670Sstevel@tonic-gate */ 34680Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_READY); 34690Sstevel@tonic-gate /* 34700Sstevel@tonic-gate * The zone is fully visible, so we can let mounts progress. 34710Sstevel@tonic-gate */ 34720Sstevel@tonic-gate resume_mounts(); 34730Sstevel@tonic-gate if (rctls) 34740Sstevel@tonic-gate nvlist_free(rctls); 34750Sstevel@tonic-gate 34760Sstevel@tonic-gate return (zoneid); 34770Sstevel@tonic-gate 34780Sstevel@tonic-gate errout: 34790Sstevel@tonic-gate mutex_exit(&zonehash_lock); 34800Sstevel@tonic-gate /* 34810Sstevel@tonic-gate * Let the other lwps continue. 34820Sstevel@tonic-gate */ 34830Sstevel@tonic-gate mutex_enter(&pp->p_lock); 34840Sstevel@tonic-gate if (curthread != pp->p_agenttp) 34850Sstevel@tonic-gate continuelwps(pp); 34860Sstevel@tonic-gate mutex_exit(&pp->p_lock); 34870Sstevel@tonic-gate 34880Sstevel@tonic-gate resume_mounts(); 34890Sstevel@tonic-gate if (rctls) 34900Sstevel@tonic-gate nvlist_free(rctls); 34910Sstevel@tonic-gate /* 34920Sstevel@tonic-gate * There is currently one reference to the zone, a cred_ref from 34930Sstevel@tonic-gate * zone_kcred. To free the zone, we call crfree, which will call 34940Sstevel@tonic-gate * zone_cred_rele, which will call zone_free. 34950Sstevel@tonic-gate */ 34960Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 34970Sstevel@tonic-gate ASSERT(zone->zone_kcred->cr_ref == 1); 34980Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 34990Sstevel@tonic-gate zkcr = zone->zone_kcred; 35000Sstevel@tonic-gate zone->zone_kcred = NULL; 35010Sstevel@tonic-gate crfree(zkcr); /* triggers call to zone_free */ 35020Sstevel@tonic-gate return (zone_create_error(error, error2, extended_error)); 35030Sstevel@tonic-gate } 35040Sstevel@tonic-gate 35050Sstevel@tonic-gate /* 35060Sstevel@tonic-gate * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 35072267Sdp * the heavy lifting. initname is the path to the program to launch 35082267Sdp * at the "top" of the zone; if this is NULL, we use the system default, 35092267Sdp * which is stored at zone_default_initname. 35100Sstevel@tonic-gate */ 35110Sstevel@tonic-gate static int 35122267Sdp zone_boot(zoneid_t zoneid) 35130Sstevel@tonic-gate { 35140Sstevel@tonic-gate int err; 35150Sstevel@tonic-gate zone_t *zone; 35160Sstevel@tonic-gate 35170Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 35180Sstevel@tonic-gate return (set_errno(EPERM)); 35190Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 35200Sstevel@tonic-gate return (set_errno(EINVAL)); 35210Sstevel@tonic-gate 35220Sstevel@tonic-gate mutex_enter(&zonehash_lock); 35230Sstevel@tonic-gate /* 35240Sstevel@tonic-gate * Look for zone under hash lock to prevent races with calls to 35250Sstevel@tonic-gate * zone_shutdown, zone_destroy, etc. 35260Sstevel@tonic-gate */ 35270Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 35280Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35290Sstevel@tonic-gate return (set_errno(EINVAL)); 35300Sstevel@tonic-gate } 35310Sstevel@tonic-gate 35320Sstevel@tonic-gate mutex_enter(&zone_status_lock); 35330Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_READY) { 35340Sstevel@tonic-gate mutex_exit(&zone_status_lock); 35350Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35360Sstevel@tonic-gate return (set_errno(EINVAL)); 35370Sstevel@tonic-gate } 35380Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_BOOTING); 35390Sstevel@tonic-gate mutex_exit(&zone_status_lock); 35400Sstevel@tonic-gate 35410Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 35420Sstevel@tonic-gate mutex_exit(&zonehash_lock); 35430Sstevel@tonic-gate 35440Sstevel@tonic-gate if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 35450Sstevel@tonic-gate zone_rele(zone); 35460Sstevel@tonic-gate return (set_errno(EINTR)); 35470Sstevel@tonic-gate } 35480Sstevel@tonic-gate 35490Sstevel@tonic-gate /* 35500Sstevel@tonic-gate * Boot (starting init) might have failed, in which case the zone 35510Sstevel@tonic-gate * will go to the SHUTTING_DOWN state; an appropriate errno will 35520Sstevel@tonic-gate * be placed in zone->zone_boot_err, and so we return that. 35530Sstevel@tonic-gate */ 35540Sstevel@tonic-gate err = zone->zone_boot_err; 35550Sstevel@tonic-gate zone_rele(zone); 35560Sstevel@tonic-gate return (err ? set_errno(err) : 0); 35570Sstevel@tonic-gate } 35580Sstevel@tonic-gate 35590Sstevel@tonic-gate /* 35600Sstevel@tonic-gate * Kills all user processes in the zone, waiting for them all to exit 35610Sstevel@tonic-gate * before returning. 35620Sstevel@tonic-gate */ 35630Sstevel@tonic-gate static int 35640Sstevel@tonic-gate zone_empty(zone_t *zone) 35650Sstevel@tonic-gate { 35660Sstevel@tonic-gate int waitstatus; 35670Sstevel@tonic-gate 35680Sstevel@tonic-gate /* 35690Sstevel@tonic-gate * We need to drop zonehash_lock before killing all 35700Sstevel@tonic-gate * processes, otherwise we'll deadlock with zone_find_* 35710Sstevel@tonic-gate * which can be called from the exit path. 35720Sstevel@tonic-gate */ 35730Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 35740Sstevel@tonic-gate while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz, 35750Sstevel@tonic-gate ZONE_IS_EMPTY)) == -1) { 35760Sstevel@tonic-gate killall(zone->zone_id); 35770Sstevel@tonic-gate } 35780Sstevel@tonic-gate /* 35790Sstevel@tonic-gate * return EINTR if we were signaled 35800Sstevel@tonic-gate */ 35810Sstevel@tonic-gate if (waitstatus == 0) 35820Sstevel@tonic-gate return (EINTR); 35830Sstevel@tonic-gate return (0); 35840Sstevel@tonic-gate } 35850Sstevel@tonic-gate 35860Sstevel@tonic-gate /* 35871676Sjpk * This function implements the policy for zone visibility. 35881676Sjpk * 35891676Sjpk * In standard Solaris, a non-global zone can only see itself. 35901676Sjpk * 35911676Sjpk * In Trusted Extensions, a labeled zone can lookup any zone whose label 35921676Sjpk * it dominates. For this test, the label of the global zone is treated as 35931676Sjpk * admin_high so it is special-cased instead of being checked for dominance. 35941676Sjpk * 35951676Sjpk * Returns true if zone attributes are viewable, false otherwise. 35961676Sjpk */ 35971676Sjpk static boolean_t 35981676Sjpk zone_list_access(zone_t *zone) 35991676Sjpk { 36001676Sjpk 36011676Sjpk if (curproc->p_zone == global_zone || 36021676Sjpk curproc->p_zone == zone) { 36031676Sjpk return (B_TRUE); 36041769Scarlsonj } else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 36051676Sjpk bslabel_t *curproc_label; 36061676Sjpk bslabel_t *zone_label; 36071676Sjpk 36081676Sjpk curproc_label = label2bslabel(curproc->p_zone->zone_slabel); 36091676Sjpk zone_label = label2bslabel(zone->zone_slabel); 36101676Sjpk 36111676Sjpk if (zone->zone_id != GLOBAL_ZONEID && 36121676Sjpk bldominates(curproc_label, zone_label)) { 36131676Sjpk return (B_TRUE); 36141676Sjpk } else { 36151676Sjpk return (B_FALSE); 36161676Sjpk } 36171676Sjpk } else { 36181676Sjpk return (B_FALSE); 36191676Sjpk } 36201676Sjpk } 36211676Sjpk 36221676Sjpk /* 36230Sstevel@tonic-gate * Systemcall to start the zone's halt sequence. By the time this 36240Sstevel@tonic-gate * function successfully returns, all user processes and kernel threads 36250Sstevel@tonic-gate * executing in it will have exited, ZSD shutdown callbacks executed, 36260Sstevel@tonic-gate * and the zone status set to ZONE_IS_DOWN. 36270Sstevel@tonic-gate * 36280Sstevel@tonic-gate * It is possible that the call will interrupt itself if the caller is the 36290Sstevel@tonic-gate * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 36300Sstevel@tonic-gate */ 36310Sstevel@tonic-gate static int 36320Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 36330Sstevel@tonic-gate { 36340Sstevel@tonic-gate int error; 36350Sstevel@tonic-gate zone_t *zone; 36360Sstevel@tonic-gate zone_status_t status; 36370Sstevel@tonic-gate 36380Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 36390Sstevel@tonic-gate return (set_errno(EPERM)); 36400Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 36410Sstevel@tonic-gate return (set_errno(EINVAL)); 36420Sstevel@tonic-gate 36430Sstevel@tonic-gate /* 36440Sstevel@tonic-gate * Block mounts so that VFS_MOUNT() can get an accurate view of 36450Sstevel@tonic-gate * the zone's status with regards to ZONE_IS_SHUTTING down. 36460Sstevel@tonic-gate * 36470Sstevel@tonic-gate * e.g. NFS can fail the mount if it determines that the zone 36480Sstevel@tonic-gate * has already begun the shutdown sequence. 36490Sstevel@tonic-gate */ 36500Sstevel@tonic-gate if (block_mounts() == 0) 36510Sstevel@tonic-gate return (set_errno(EINTR)); 36520Sstevel@tonic-gate mutex_enter(&zonehash_lock); 36530Sstevel@tonic-gate /* 36540Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 36550Sstevel@tonic-gate * calls to zone_shutdown and zone_destroy. 36560Sstevel@tonic-gate */ 36570Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 36580Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36590Sstevel@tonic-gate resume_mounts(); 36600Sstevel@tonic-gate return (set_errno(EINVAL)); 36610Sstevel@tonic-gate } 36620Sstevel@tonic-gate mutex_enter(&zone_status_lock); 36630Sstevel@tonic-gate status = zone_status_get(zone); 36640Sstevel@tonic-gate /* 36650Sstevel@tonic-gate * Fail if the zone isn't fully initialized yet. 36660Sstevel@tonic-gate */ 36670Sstevel@tonic-gate if (status < ZONE_IS_READY) { 36680Sstevel@tonic-gate mutex_exit(&zone_status_lock); 36690Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36700Sstevel@tonic-gate resume_mounts(); 36710Sstevel@tonic-gate return (set_errno(EINVAL)); 36720Sstevel@tonic-gate } 36730Sstevel@tonic-gate /* 36740Sstevel@tonic-gate * If conditions required for zone_shutdown() to return have been met, 36750Sstevel@tonic-gate * return success. 36760Sstevel@tonic-gate */ 36770Sstevel@tonic-gate if (status >= ZONE_IS_DOWN) { 36780Sstevel@tonic-gate mutex_exit(&zone_status_lock); 36790Sstevel@tonic-gate mutex_exit(&zonehash_lock); 36800Sstevel@tonic-gate resume_mounts(); 36810Sstevel@tonic-gate return (0); 36820Sstevel@tonic-gate } 36830Sstevel@tonic-gate /* 36840Sstevel@tonic-gate * If zone_shutdown() hasn't been called before, go through the motions. 36850Sstevel@tonic-gate * If it has, there's nothing to do but wait for the kernel threads to 36860Sstevel@tonic-gate * drain. 36870Sstevel@tonic-gate */ 36880Sstevel@tonic-gate if (status < ZONE_IS_EMPTY) { 36890Sstevel@tonic-gate uint_t ntasks; 36900Sstevel@tonic-gate 36910Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 36920Sstevel@tonic-gate if ((ntasks = zone->zone_ntasks) != 1) { 36930Sstevel@tonic-gate /* 36940Sstevel@tonic-gate * There's still stuff running. 36950Sstevel@tonic-gate */ 36960Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 36970Sstevel@tonic-gate } 36980Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 36990Sstevel@tonic-gate if (ntasks == 1) { 37000Sstevel@tonic-gate /* 37010Sstevel@tonic-gate * The only way to create another task is through 37020Sstevel@tonic-gate * zone_enter(), which will block until we drop 37030Sstevel@tonic-gate * zonehash_lock. The zone is empty. 37040Sstevel@tonic-gate */ 37050Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 37060Sstevel@tonic-gate /* 37070Sstevel@tonic-gate * Skip ahead to ZONE_IS_DOWN 37080Sstevel@tonic-gate */ 37090Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 37100Sstevel@tonic-gate } else { 37110Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 37120Sstevel@tonic-gate } 37130Sstevel@tonic-gate } 37140Sstevel@tonic-gate } 37150Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 37160Sstevel@tonic-gate mutex_exit(&zone_status_lock); 37170Sstevel@tonic-gate mutex_exit(&zonehash_lock); 37180Sstevel@tonic-gate resume_mounts(); 37190Sstevel@tonic-gate 37200Sstevel@tonic-gate if (error = zone_empty(zone)) { 37210Sstevel@tonic-gate zone_rele(zone); 37220Sstevel@tonic-gate return (set_errno(error)); 37230Sstevel@tonic-gate } 37240Sstevel@tonic-gate /* 37250Sstevel@tonic-gate * After the zone status goes to ZONE_IS_DOWN this zone will no 37260Sstevel@tonic-gate * longer be notified of changes to the pools configuration, so 37270Sstevel@tonic-gate * in order to not end up with a stale pool pointer, we point 37280Sstevel@tonic-gate * ourselves at the default pool and remove all resource 37290Sstevel@tonic-gate * visibility. This is especially important as the zone_t may 37300Sstevel@tonic-gate * languish on the deathrow for a very long time waiting for 37310Sstevel@tonic-gate * cred's to drain out. 37320Sstevel@tonic-gate * 37330Sstevel@tonic-gate * This rebinding of the zone can happen multiple times 37340Sstevel@tonic-gate * (presumably due to interrupted or parallel systemcalls) 37350Sstevel@tonic-gate * without any adverse effects. 37360Sstevel@tonic-gate */ 37370Sstevel@tonic-gate if (pool_lock_intr() != 0) { 37380Sstevel@tonic-gate zone_rele(zone); 37390Sstevel@tonic-gate return (set_errno(EINTR)); 37400Sstevel@tonic-gate } 37410Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 37420Sstevel@tonic-gate mutex_enter(&cpu_lock); 37430Sstevel@tonic-gate zone_pool_set(zone, pool_default); 37440Sstevel@tonic-gate /* 37450Sstevel@tonic-gate * The zone no longer needs to be able to see any cpus. 37460Sstevel@tonic-gate */ 37470Sstevel@tonic-gate zone_pset_set(zone, ZONE_PS_INVAL); 37480Sstevel@tonic-gate mutex_exit(&cpu_lock); 37490Sstevel@tonic-gate } 37500Sstevel@tonic-gate pool_unlock(); 37510Sstevel@tonic-gate 37520Sstevel@tonic-gate /* 37530Sstevel@tonic-gate * ZSD shutdown callbacks can be executed multiple times, hence 37540Sstevel@tonic-gate * it is safe to not be holding any locks across this call. 37550Sstevel@tonic-gate */ 37560Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 37570Sstevel@tonic-gate 37580Sstevel@tonic-gate mutex_enter(&zone_status_lock); 37590Sstevel@tonic-gate if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 37600Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 37610Sstevel@tonic-gate mutex_exit(&zone_status_lock); 37620Sstevel@tonic-gate 37630Sstevel@tonic-gate /* 37640Sstevel@tonic-gate * Wait for kernel threads to drain. 37650Sstevel@tonic-gate */ 37660Sstevel@tonic-gate if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 37670Sstevel@tonic-gate zone_rele(zone); 37680Sstevel@tonic-gate return (set_errno(EINTR)); 37690Sstevel@tonic-gate } 37702712Snn35248 37712712Snn35248 brand_unregister_zone(zone->zone_brand); 37722712Snn35248 37730Sstevel@tonic-gate zone_rele(zone); 37740Sstevel@tonic-gate return (0); 37750Sstevel@tonic-gate } 37760Sstevel@tonic-gate 37770Sstevel@tonic-gate /* 37780Sstevel@tonic-gate * Systemcall entry point to finalize the zone halt process. The caller 37792677Sml93401 * must have already successfully called zone_shutdown(). 37800Sstevel@tonic-gate * 37810Sstevel@tonic-gate * Upon successful completion, the zone will have been fully destroyed: 37820Sstevel@tonic-gate * zsched will have exited, destructor callbacks executed, and the zone 37830Sstevel@tonic-gate * removed from the list of active zones. 37840Sstevel@tonic-gate */ 37850Sstevel@tonic-gate static int 37860Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 37870Sstevel@tonic-gate { 37880Sstevel@tonic-gate uint64_t uniqid; 37890Sstevel@tonic-gate zone_t *zone; 37900Sstevel@tonic-gate zone_status_t status; 37910Sstevel@tonic-gate 37920Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 37930Sstevel@tonic-gate return (set_errno(EPERM)); 37940Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 37950Sstevel@tonic-gate return (set_errno(EINVAL)); 37960Sstevel@tonic-gate 37970Sstevel@tonic-gate mutex_enter(&zonehash_lock); 37980Sstevel@tonic-gate /* 37990Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 38000Sstevel@tonic-gate * calls to zone_destroy. 38010Sstevel@tonic-gate */ 38020Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 38030Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38040Sstevel@tonic-gate return (set_errno(EINVAL)); 38050Sstevel@tonic-gate } 38060Sstevel@tonic-gate 38070Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 38080Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38090Sstevel@tonic-gate return (set_errno(EBUSY)); 38100Sstevel@tonic-gate } 38110Sstevel@tonic-gate mutex_enter(&zone_status_lock); 38120Sstevel@tonic-gate status = zone_status_get(zone); 38130Sstevel@tonic-gate if (status < ZONE_IS_DOWN) { 38140Sstevel@tonic-gate mutex_exit(&zone_status_lock); 38150Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38160Sstevel@tonic-gate return (set_errno(EBUSY)); 38170Sstevel@tonic-gate } else if (status == ZONE_IS_DOWN) { 38180Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 38190Sstevel@tonic-gate } 38200Sstevel@tonic-gate mutex_exit(&zone_status_lock); 38210Sstevel@tonic-gate zone_hold(zone); 38220Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38230Sstevel@tonic-gate 38240Sstevel@tonic-gate /* 38250Sstevel@tonic-gate * wait for zsched to exit 38260Sstevel@tonic-gate */ 38270Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_DEAD); 38280Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_DESTROY); 38290Sstevel@tonic-gate uniqid = zone->zone_uniqid; 38300Sstevel@tonic-gate zone_rele(zone); 38310Sstevel@tonic-gate zone = NULL; /* potentially free'd */ 38320Sstevel@tonic-gate 38330Sstevel@tonic-gate mutex_enter(&zonehash_lock); 38340Sstevel@tonic-gate for (; /* ever */; ) { 38350Sstevel@tonic-gate boolean_t unref; 38360Sstevel@tonic-gate 38370Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL || 38380Sstevel@tonic-gate zone->zone_uniqid != uniqid) { 38390Sstevel@tonic-gate /* 38400Sstevel@tonic-gate * The zone has gone away. Necessary conditions 38410Sstevel@tonic-gate * are met, so we return success. 38420Sstevel@tonic-gate */ 38430Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38440Sstevel@tonic-gate return (0); 38450Sstevel@tonic-gate } 38460Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 38470Sstevel@tonic-gate unref = ZONE_IS_UNREF(zone); 38480Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 38490Sstevel@tonic-gate if (unref) { 38500Sstevel@tonic-gate /* 38510Sstevel@tonic-gate * There is only one reference to the zone -- that 38520Sstevel@tonic-gate * added when the zone was added to the hashtables -- 38530Sstevel@tonic-gate * and things will remain this way until we drop 38540Sstevel@tonic-gate * zonehash_lock... we can go ahead and cleanup the 38550Sstevel@tonic-gate * zone. 38560Sstevel@tonic-gate */ 38570Sstevel@tonic-gate break; 38580Sstevel@tonic-gate } 38590Sstevel@tonic-gate 38600Sstevel@tonic-gate if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 38610Sstevel@tonic-gate /* Signaled */ 38620Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38630Sstevel@tonic-gate return (set_errno(EINTR)); 38640Sstevel@tonic-gate } 38650Sstevel@tonic-gate 38660Sstevel@tonic-gate } 38670Sstevel@tonic-gate 3868*3247Sgjelinek /* Get rid of the zone's kstats */ 3869*3247Sgjelinek zone_kstat_delete(zone); 3870*3247Sgjelinek 38710Sstevel@tonic-gate /* 38720Sstevel@tonic-gate * It is now safe to let the zone be recreated; remove it from the 38730Sstevel@tonic-gate * lists. The memory will not be freed until the last cred 38740Sstevel@tonic-gate * reference goes away. 38750Sstevel@tonic-gate */ 38760Sstevel@tonic-gate ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 38770Sstevel@tonic-gate zonecount--; 38780Sstevel@tonic-gate /* remove from active list and hash tables */ 38790Sstevel@tonic-gate list_remove(&zone_active, zone); 38800Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 38810Sstevel@tonic-gate (mod_hash_key_t)zone->zone_name); 38820Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 38830Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 38841769Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) 38851676Sjpk (void) mod_hash_destroy(zonehashbylabel, 38861676Sjpk (mod_hash_key_t)zone->zone_slabel); 38870Sstevel@tonic-gate mutex_exit(&zonehash_lock); 38880Sstevel@tonic-gate 3889766Scarlsonj /* 3890766Scarlsonj * Release the root vnode; we're not using it anymore. Nor should any 3891766Scarlsonj * other thread that might access it exist. 3892766Scarlsonj */ 3893766Scarlsonj if (zone->zone_rootvp != NULL) { 3894766Scarlsonj VN_RELE(zone->zone_rootvp); 3895766Scarlsonj zone->zone_rootvp = NULL; 3896766Scarlsonj } 3897766Scarlsonj 38980Sstevel@tonic-gate /* add to deathrow list */ 38990Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 39000Sstevel@tonic-gate list_insert_tail(&zone_deathrow, zone); 39010Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 39020Sstevel@tonic-gate 39030Sstevel@tonic-gate /* 39040Sstevel@tonic-gate * Drop last reference (which was added by zsched()), this will 39050Sstevel@tonic-gate * free the zone unless there are outstanding cred references. 39060Sstevel@tonic-gate */ 39070Sstevel@tonic-gate zone_rele(zone); 39080Sstevel@tonic-gate return (0); 39090Sstevel@tonic-gate } 39100Sstevel@tonic-gate 39110Sstevel@tonic-gate /* 39120Sstevel@tonic-gate * Systemcall entry point for zone_getattr(2). 39130Sstevel@tonic-gate */ 39140Sstevel@tonic-gate static ssize_t 39150Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 39160Sstevel@tonic-gate { 39170Sstevel@tonic-gate size_t size; 39180Sstevel@tonic-gate int error = 0, err; 39190Sstevel@tonic-gate zone_t *zone; 39200Sstevel@tonic-gate char *zonepath; 39212267Sdp char *outstr; 39220Sstevel@tonic-gate zone_status_t zone_status; 39230Sstevel@tonic-gate pid_t initpid; 39240Sstevel@tonic-gate boolean_t global = (curproc->p_zone == global_zone); 39251676Sjpk boolean_t curzone = (curproc->p_zone->zone_id == zoneid); 39260Sstevel@tonic-gate 39270Sstevel@tonic-gate mutex_enter(&zonehash_lock); 39280Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 39290Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39300Sstevel@tonic-gate return (set_errno(EINVAL)); 39310Sstevel@tonic-gate } 39320Sstevel@tonic-gate zone_status = zone_status_get(zone); 39330Sstevel@tonic-gate if (zone_status < ZONE_IS_READY) { 39340Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39350Sstevel@tonic-gate return (set_errno(EINVAL)); 39360Sstevel@tonic-gate } 39370Sstevel@tonic-gate zone_hold(zone); 39380Sstevel@tonic-gate mutex_exit(&zonehash_lock); 39390Sstevel@tonic-gate 39400Sstevel@tonic-gate /* 39411676Sjpk * If not in the global zone, don't show information about other zones, 39421676Sjpk * unless the system is labeled and the local zone's label dominates 39431676Sjpk * the other zone. 39440Sstevel@tonic-gate */ 39451676Sjpk if (!zone_list_access(zone)) { 39460Sstevel@tonic-gate zone_rele(zone); 39470Sstevel@tonic-gate return (set_errno(EINVAL)); 39480Sstevel@tonic-gate } 39490Sstevel@tonic-gate 39500Sstevel@tonic-gate switch (attr) { 39510Sstevel@tonic-gate case ZONE_ATTR_ROOT: 39520Sstevel@tonic-gate if (global) { 39530Sstevel@tonic-gate /* 39540Sstevel@tonic-gate * Copy the path to trim the trailing "/" (except for 39550Sstevel@tonic-gate * the global zone). 39560Sstevel@tonic-gate */ 39570Sstevel@tonic-gate if (zone != global_zone) 39580Sstevel@tonic-gate size = zone->zone_rootpathlen - 1; 39590Sstevel@tonic-gate else 39600Sstevel@tonic-gate size = zone->zone_rootpathlen; 39610Sstevel@tonic-gate zonepath = kmem_alloc(size, KM_SLEEP); 39620Sstevel@tonic-gate bcopy(zone->zone_rootpath, zonepath, size); 39630Sstevel@tonic-gate zonepath[size - 1] = '\0'; 39640Sstevel@tonic-gate } else { 39651676Sjpk if (curzone || !is_system_labeled()) { 39661676Sjpk /* 39671676Sjpk * Caller is not in the global zone. 39681676Sjpk * if the query is on the current zone 39691676Sjpk * or the system is not labeled, 39701676Sjpk * just return faked-up path for current zone. 39711676Sjpk */ 39721676Sjpk zonepath = "/"; 39731676Sjpk size = 2; 39741676Sjpk } else { 39751676Sjpk /* 39761676Sjpk * Return related path for current zone. 39771676Sjpk */ 39781676Sjpk int prefix_len = strlen(zone_prefix); 39791676Sjpk int zname_len = strlen(zone->zone_name); 39801676Sjpk 39811676Sjpk size = prefix_len + zname_len + 1; 39821676Sjpk zonepath = kmem_alloc(size, KM_SLEEP); 39831676Sjpk bcopy(zone_prefix, zonepath, prefix_len); 39841676Sjpk bcopy(zone->zone_name, zonepath + 39852267Sdp prefix_len, zname_len); 39861676Sjpk zonepath[size - 1] = '\0'; 39871676Sjpk } 39880Sstevel@tonic-gate } 39890Sstevel@tonic-gate if (bufsize > size) 39900Sstevel@tonic-gate bufsize = size; 39910Sstevel@tonic-gate if (buf != NULL) { 39920Sstevel@tonic-gate err = copyoutstr(zonepath, buf, bufsize, NULL); 39930Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 39940Sstevel@tonic-gate error = EFAULT; 39950Sstevel@tonic-gate } 39961676Sjpk if (global || (is_system_labeled() && !curzone)) 39970Sstevel@tonic-gate kmem_free(zonepath, size); 39980Sstevel@tonic-gate break; 39990Sstevel@tonic-gate 40000Sstevel@tonic-gate case ZONE_ATTR_NAME: 40010Sstevel@tonic-gate size = strlen(zone->zone_name) + 1; 40020Sstevel@tonic-gate if (bufsize > size) 40030Sstevel@tonic-gate bufsize = size; 40040Sstevel@tonic-gate if (buf != NULL) { 40050Sstevel@tonic-gate err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 40060Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 40070Sstevel@tonic-gate error = EFAULT; 40080Sstevel@tonic-gate } 40090Sstevel@tonic-gate break; 40100Sstevel@tonic-gate 40110Sstevel@tonic-gate case ZONE_ATTR_STATUS: 40120Sstevel@tonic-gate /* 40130Sstevel@tonic-gate * Since we're not holding zonehash_lock, the zone status 40140Sstevel@tonic-gate * may be anything; leave it up to userland to sort it out. 40150Sstevel@tonic-gate */ 40160Sstevel@tonic-gate size = sizeof (zone_status); 40170Sstevel@tonic-gate if (bufsize > size) 40180Sstevel@tonic-gate bufsize = size; 40190Sstevel@tonic-gate zone_status = zone_status_get(zone); 40200Sstevel@tonic-gate if (buf != NULL && 40210Sstevel@tonic-gate copyout(&zone_status, buf, bufsize) != 0) 40220Sstevel@tonic-gate error = EFAULT; 40230Sstevel@tonic-gate break; 40240Sstevel@tonic-gate case ZONE_ATTR_PRIVSET: 40250Sstevel@tonic-gate size = sizeof (priv_set_t); 40260Sstevel@tonic-gate if (bufsize > size) 40270Sstevel@tonic-gate bufsize = size; 40280Sstevel@tonic-gate if (buf != NULL && 40290Sstevel@tonic-gate copyout(zone->zone_privset, buf, bufsize) != 0) 40300Sstevel@tonic-gate error = EFAULT; 40310Sstevel@tonic-gate break; 40320Sstevel@tonic-gate case ZONE_ATTR_UNIQID: 40330Sstevel@tonic-gate size = sizeof (zone->zone_uniqid); 40340Sstevel@tonic-gate if (bufsize > size) 40350Sstevel@tonic-gate bufsize = size; 40360Sstevel@tonic-gate if (buf != NULL && 40370Sstevel@tonic-gate copyout(&zone->zone_uniqid, buf, bufsize) != 0) 40380Sstevel@tonic-gate error = EFAULT; 40390Sstevel@tonic-gate break; 40400Sstevel@tonic-gate case ZONE_ATTR_POOLID: 40410Sstevel@tonic-gate { 40420Sstevel@tonic-gate pool_t *pool; 40430Sstevel@tonic-gate poolid_t poolid; 40440Sstevel@tonic-gate 40450Sstevel@tonic-gate if (pool_lock_intr() != 0) { 40460Sstevel@tonic-gate error = EINTR; 40470Sstevel@tonic-gate break; 40480Sstevel@tonic-gate } 40490Sstevel@tonic-gate pool = zone_pool_get(zone); 40500Sstevel@tonic-gate poolid = pool->pool_id; 40510Sstevel@tonic-gate pool_unlock(); 40520Sstevel@tonic-gate size = sizeof (poolid); 40530Sstevel@tonic-gate if (bufsize > size) 40540Sstevel@tonic-gate bufsize = size; 40550Sstevel@tonic-gate if (buf != NULL && copyout(&poolid, buf, size) != 0) 40560Sstevel@tonic-gate error = EFAULT; 40570Sstevel@tonic-gate } 40580Sstevel@tonic-gate break; 40591676Sjpk case ZONE_ATTR_SLBL: 40601676Sjpk size = sizeof (bslabel_t); 40611676Sjpk if (bufsize > size) 40621676Sjpk bufsize = size; 40631676Sjpk if (zone->zone_slabel == NULL) 40641676Sjpk error = EINVAL; 40651676Sjpk else if (buf != NULL && 40661676Sjpk copyout(label2bslabel(zone->zone_slabel), buf, 40671676Sjpk bufsize) != 0) 40681676Sjpk error = EFAULT; 40691676Sjpk break; 40700Sstevel@tonic-gate case ZONE_ATTR_INITPID: 40710Sstevel@tonic-gate size = sizeof (initpid); 40720Sstevel@tonic-gate if (bufsize > size) 40730Sstevel@tonic-gate bufsize = size; 40740Sstevel@tonic-gate initpid = zone->zone_proc_initpid; 40750Sstevel@tonic-gate if (initpid == -1) { 40760Sstevel@tonic-gate error = ESRCH; 40770Sstevel@tonic-gate break; 40780Sstevel@tonic-gate } 40790Sstevel@tonic-gate if (buf != NULL && 40800Sstevel@tonic-gate copyout(&initpid, buf, bufsize) != 0) 40810Sstevel@tonic-gate error = EFAULT; 40820Sstevel@tonic-gate break; 40832712Snn35248 case ZONE_ATTR_BRAND: 40842712Snn35248 size = strlen(zone->zone_brand->b_name) + 1; 40852712Snn35248 40862712Snn35248 if (bufsize > size) 40872712Snn35248 bufsize = size; 40882712Snn35248 if (buf != NULL) { 40892712Snn35248 err = copyoutstr(zone->zone_brand->b_name, buf, 40902712Snn35248 bufsize, NULL); 40912712Snn35248 if (err != 0 && err != ENAMETOOLONG) 40922712Snn35248 error = EFAULT; 40932712Snn35248 } 40942712Snn35248 break; 40952267Sdp case ZONE_ATTR_INITNAME: 40962267Sdp size = strlen(zone->zone_initname) + 1; 40972267Sdp if (bufsize > size) 40982267Sdp bufsize = size; 40992267Sdp if (buf != NULL) { 41002267Sdp err = copyoutstr(zone->zone_initname, buf, bufsize, 41012267Sdp NULL); 41022267Sdp if (err != 0 && err != ENAMETOOLONG) 41032267Sdp error = EFAULT; 41042267Sdp } 41052267Sdp break; 41062267Sdp case ZONE_ATTR_BOOTARGS: 41072267Sdp if (zone->zone_bootargs == NULL) 41082267Sdp outstr = ""; 41092267Sdp else 41102267Sdp outstr = zone->zone_bootargs; 41112267Sdp size = strlen(outstr) + 1; 41122267Sdp if (bufsize > size) 41132267Sdp bufsize = size; 41142267Sdp if (buf != NULL) { 41152267Sdp err = copyoutstr(outstr, buf, bufsize, NULL); 41162267Sdp if (err != 0 && err != ENAMETOOLONG) 41172267Sdp error = EFAULT; 41182267Sdp } 41192267Sdp break; 4120*3247Sgjelinek case ZONE_ATTR_PHYS_MCAP: 4121*3247Sgjelinek size = sizeof (zone->zone_phys_mcap); 4122*3247Sgjelinek if (bufsize > size) 4123*3247Sgjelinek bufsize = size; 4124*3247Sgjelinek if (buf != NULL && 4125*3247Sgjelinek copyout(&zone->zone_phys_mcap, buf, bufsize) != 0) 4126*3247Sgjelinek error = EFAULT; 4127*3247Sgjelinek break; 4128*3247Sgjelinek case ZONE_ATTR_SCHED_CLASS: 4129*3247Sgjelinek mutex_enter(&class_lock); 4130*3247Sgjelinek 4131*3247Sgjelinek if (zone->zone_defaultcid >= loaded_classes) 4132*3247Sgjelinek outstr = ""; 4133*3247Sgjelinek else 4134*3247Sgjelinek outstr = sclass[zone->zone_defaultcid].cl_name; 4135*3247Sgjelinek size = strlen(outstr) + 1; 4136*3247Sgjelinek if (bufsize > size) 4137*3247Sgjelinek bufsize = size; 4138*3247Sgjelinek if (buf != NULL) { 4139*3247Sgjelinek err = copyoutstr(outstr, buf, bufsize, NULL); 4140*3247Sgjelinek if (err != 0 && err != ENAMETOOLONG) 4141*3247Sgjelinek error = EFAULT; 4142*3247Sgjelinek } 4143*3247Sgjelinek 4144*3247Sgjelinek mutex_exit(&class_lock); 4145*3247Sgjelinek break; 41460Sstevel@tonic-gate default: 41472712Snn35248 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) { 41482712Snn35248 size = bufsize; 41492712Snn35248 error = ZBROP(zone)->b_getattr(zone, attr, buf, &size); 41502712Snn35248 } else { 41512712Snn35248 error = EINVAL; 41522712Snn35248 } 41530Sstevel@tonic-gate } 41540Sstevel@tonic-gate zone_rele(zone); 41550Sstevel@tonic-gate 41560Sstevel@tonic-gate if (error) 41570Sstevel@tonic-gate return (set_errno(error)); 41580Sstevel@tonic-gate return ((ssize_t)size); 41590Sstevel@tonic-gate } 41600Sstevel@tonic-gate 41610Sstevel@tonic-gate /* 41622267Sdp * Systemcall entry point for zone_setattr(2). 41632267Sdp */ 41642267Sdp /*ARGSUSED*/ 41652267Sdp static int 41662267Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 41672267Sdp { 41682267Sdp zone_t *zone; 41692267Sdp zone_status_t zone_status; 41702712Snn35248 struct brand_attr *attrp; 41712267Sdp int err; 41722267Sdp 41732267Sdp if (secpolicy_zone_config(CRED()) != 0) 41742267Sdp return (set_errno(EPERM)); 41752267Sdp 41762267Sdp /* 4177*3247Sgjelinek * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the 4178*3247Sgjelinek * global zone. 41792267Sdp */ 4180*3247Sgjelinek if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) { 41812267Sdp return (set_errno(EINVAL)); 41822267Sdp } 41832267Sdp 41842267Sdp mutex_enter(&zonehash_lock); 41852267Sdp if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 41862267Sdp mutex_exit(&zonehash_lock); 41872267Sdp return (set_errno(EINVAL)); 41882267Sdp } 41892267Sdp zone_hold(zone); 41902267Sdp mutex_exit(&zonehash_lock); 41912267Sdp 4192*3247Sgjelinek /* 4193*3247Sgjelinek * At present most attributes can only be set on non-running, 4194*3247Sgjelinek * non-global zones. 4195*3247Sgjelinek */ 41962267Sdp zone_status = zone_status_get(zone); 4197*3247Sgjelinek if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY) 41982267Sdp goto done; 41992267Sdp 42002267Sdp switch (attr) { 42012267Sdp case ZONE_ATTR_INITNAME: 42022267Sdp err = zone_set_initname(zone, (const char *)buf); 42032267Sdp break; 42042267Sdp case ZONE_ATTR_BOOTARGS: 42052267Sdp err = zone_set_bootargs(zone, (const char *)buf); 42062267Sdp break; 42072712Snn35248 case ZONE_ATTR_BRAND: 42082712Snn35248 ASSERT(!ZONE_IS_BRANDED(zone)); 42092712Snn35248 err = 0; 42102712Snn35248 attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP); 42112712Snn35248 if ((buf == NULL) || 42122712Snn35248 (copyin(buf, attrp, sizeof (struct brand_attr)) != 0)) { 42132712Snn35248 kmem_free(attrp, sizeof (struct brand_attr)); 42142712Snn35248 err = EFAULT; 42152712Snn35248 break; 42162712Snn35248 } 42172712Snn35248 42182712Snn35248 if (is_system_labeled() && strncmp(attrp->ba_brandname, 42192712Snn35248 NATIVE_BRAND_NAME, MAXNAMELEN) != 0) { 42202712Snn35248 err = EPERM; 42212712Snn35248 break; 42222712Snn35248 } 42232712Snn35248 42242712Snn35248 zone->zone_brand = brand_register_zone(attrp); 42252712Snn35248 kmem_free(attrp, sizeof (struct brand_attr)); 42262712Snn35248 if (zone->zone_brand == NULL) 42272712Snn35248 err = EINVAL; 42282712Snn35248 break; 4229*3247Sgjelinek case ZONE_ATTR_PHYS_MCAP: 4230*3247Sgjelinek err = zone_set_phys_mcap(zone, (const uint64_t *)buf); 4231*3247Sgjelinek break; 4232*3247Sgjelinek case ZONE_ATTR_SCHED_CLASS: 4233*3247Sgjelinek err = zone_set_sched_class(zone, (const char *)buf); 4234*3247Sgjelinek break; 42352267Sdp default: 42362712Snn35248 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) 42372712Snn35248 err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize); 42382712Snn35248 else 42392712Snn35248 err = EINVAL; 42402267Sdp } 42412267Sdp 42422267Sdp done: 42432267Sdp zone_rele(zone); 42442267Sdp return (err != 0 ? set_errno(err) : 0); 42452267Sdp } 42462267Sdp 42472267Sdp /* 42480Sstevel@tonic-gate * Return zero if the process has at least one vnode mapped in to its 42490Sstevel@tonic-gate * address space which shouldn't be allowed to change zones. 4250*3247Sgjelinek * 4251*3247Sgjelinek * Also return zero if the process has any shared mappings which reserve 4252*3247Sgjelinek * swap. This is because the counting for zone.max-swap does not allow swap 4253*3247Sgjelinek * revervation to be shared between zones. zone swap reservation is counted 4254*3247Sgjelinek * on zone->zone_max_swap. 42550Sstevel@tonic-gate */ 42560Sstevel@tonic-gate static int 42570Sstevel@tonic-gate as_can_change_zones(void) 42580Sstevel@tonic-gate { 42590Sstevel@tonic-gate proc_t *pp = curproc; 42600Sstevel@tonic-gate struct seg *seg; 42610Sstevel@tonic-gate struct as *as = pp->p_as; 42620Sstevel@tonic-gate vnode_t *vp; 42630Sstevel@tonic-gate int allow = 1; 42640Sstevel@tonic-gate 42650Sstevel@tonic-gate ASSERT(pp->p_as != &kas); 4266*3247Sgjelinek AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 42670Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 4268*3247Sgjelinek 4269*3247Sgjelinek /* 4270*3247Sgjelinek * Cannot enter zone with shared anon memory which 4271*3247Sgjelinek * reserves swap. See comment above. 4272*3247Sgjelinek */ 4273*3247Sgjelinek if (seg_can_change_zones(seg) == B_FALSE) { 4274*3247Sgjelinek allow = 0; 4275*3247Sgjelinek break; 4276*3247Sgjelinek } 42770Sstevel@tonic-gate /* 42780Sstevel@tonic-gate * if we can't get a backing vnode for this segment then skip 42790Sstevel@tonic-gate * it. 42800Sstevel@tonic-gate */ 42810Sstevel@tonic-gate vp = NULL; 42820Sstevel@tonic-gate if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 42830Sstevel@tonic-gate continue; 42840Sstevel@tonic-gate if (!vn_can_change_zones(vp)) { /* bail on first match */ 42850Sstevel@tonic-gate allow = 0; 42860Sstevel@tonic-gate break; 42870Sstevel@tonic-gate } 42880Sstevel@tonic-gate } 4289*3247Sgjelinek AS_LOCK_EXIT(as, &as->a_lock); 42900Sstevel@tonic-gate return (allow); 42910Sstevel@tonic-gate } 42920Sstevel@tonic-gate 42930Sstevel@tonic-gate /* 4294*3247Sgjelinek * Count swap reserved by curproc's address space 4295*3247Sgjelinek */ 4296*3247Sgjelinek static size_t 4297*3247Sgjelinek as_swresv(void) 4298*3247Sgjelinek { 4299*3247Sgjelinek proc_t *pp = curproc; 4300*3247Sgjelinek struct seg *seg; 4301*3247Sgjelinek struct as *as = pp->p_as; 4302*3247Sgjelinek size_t swap = 0; 4303*3247Sgjelinek 4304*3247Sgjelinek ASSERT(pp->p_as != &kas); 4305*3247Sgjelinek ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 4306*3247Sgjelinek for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) 4307*3247Sgjelinek swap += seg_swresv(seg); 4308*3247Sgjelinek 4309*3247Sgjelinek return (swap); 4310*3247Sgjelinek } 4311*3247Sgjelinek 4312*3247Sgjelinek /* 43130Sstevel@tonic-gate * Systemcall entry point for zone_enter(). 43140Sstevel@tonic-gate * 43150Sstevel@tonic-gate * The current process is injected into said zone. In the process 43160Sstevel@tonic-gate * it will change its project membership, privileges, rootdir/cwd, 43170Sstevel@tonic-gate * zone-wide rctls, and pool association to match those of the zone. 43180Sstevel@tonic-gate * 43190Sstevel@tonic-gate * The first zone_enter() called while the zone is in the ZONE_IS_READY 43200Sstevel@tonic-gate * state will transition it to ZONE_IS_RUNNING. Processes may only 43210Sstevel@tonic-gate * enter a zone that is "ready" or "running". 43220Sstevel@tonic-gate */ 43230Sstevel@tonic-gate static int 43240Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 43250Sstevel@tonic-gate { 43260Sstevel@tonic-gate zone_t *zone; 43270Sstevel@tonic-gate vnode_t *vp; 43280Sstevel@tonic-gate proc_t *pp = curproc; 43290Sstevel@tonic-gate contract_t *ct; 43300Sstevel@tonic-gate cont_process_t *ctp; 43310Sstevel@tonic-gate task_t *tk, *oldtk; 43320Sstevel@tonic-gate kproject_t *zone_proj0; 43330Sstevel@tonic-gate cred_t *cr, *newcr; 43340Sstevel@tonic-gate pool_t *oldpool, *newpool; 43350Sstevel@tonic-gate sess_t *sp; 43360Sstevel@tonic-gate uid_t uid; 43370Sstevel@tonic-gate zone_status_t status; 43380Sstevel@tonic-gate int err = 0; 43390Sstevel@tonic-gate rctl_entity_p_t e; 4340*3247Sgjelinek size_t swap; 43410Sstevel@tonic-gate 43420Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 43430Sstevel@tonic-gate return (set_errno(EPERM)); 43440Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 43450Sstevel@tonic-gate return (set_errno(EINVAL)); 43460Sstevel@tonic-gate 43470Sstevel@tonic-gate /* 43480Sstevel@tonic-gate * Stop all lwps so we don't need to hold a lock to look at 43490Sstevel@tonic-gate * curproc->p_zone. This needs to happen before we grab any 43500Sstevel@tonic-gate * locks to avoid deadlock (another lwp in the process could 43510Sstevel@tonic-gate * be waiting for the held lock). 43520Sstevel@tonic-gate */ 43530Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 43540Sstevel@tonic-gate return (set_errno(EINTR)); 43550Sstevel@tonic-gate 43560Sstevel@tonic-gate /* 43570Sstevel@tonic-gate * Make sure we're not changing zones with files open or mapped in 43580Sstevel@tonic-gate * to our address space which shouldn't be changing zones. 43590Sstevel@tonic-gate */ 43600Sstevel@tonic-gate if (!files_can_change_zones()) { 43610Sstevel@tonic-gate err = EBADF; 43620Sstevel@tonic-gate goto out; 43630Sstevel@tonic-gate } 43640Sstevel@tonic-gate if (!as_can_change_zones()) { 43650Sstevel@tonic-gate err = EFAULT; 43660Sstevel@tonic-gate goto out; 43670Sstevel@tonic-gate } 43680Sstevel@tonic-gate 43690Sstevel@tonic-gate mutex_enter(&zonehash_lock); 43700Sstevel@tonic-gate if (pp->p_zone != global_zone) { 43710Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43720Sstevel@tonic-gate err = EINVAL; 43730Sstevel@tonic-gate goto out; 43740Sstevel@tonic-gate } 43750Sstevel@tonic-gate 43760Sstevel@tonic-gate zone = zone_find_all_by_id(zoneid); 43770Sstevel@tonic-gate if (zone == NULL) { 43780Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43790Sstevel@tonic-gate err = EINVAL; 43800Sstevel@tonic-gate goto out; 43810Sstevel@tonic-gate } 43820Sstevel@tonic-gate 43830Sstevel@tonic-gate /* 43840Sstevel@tonic-gate * To prevent processes in a zone from holding contracts on 43850Sstevel@tonic-gate * extrazonal resources, and to avoid process contract 43860Sstevel@tonic-gate * memberships which span zones, contract holders and processes 43870Sstevel@tonic-gate * which aren't the sole members of their encapsulating process 43880Sstevel@tonic-gate * contracts are not allowed to zone_enter. 43890Sstevel@tonic-gate */ 43900Sstevel@tonic-gate ctp = pp->p_ct_process; 43910Sstevel@tonic-gate ct = &ctp->conp_contract; 43920Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 43930Sstevel@tonic-gate mutex_enter(&pp->p_lock); 43940Sstevel@tonic-gate if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 43950Sstevel@tonic-gate mutex_exit(&pp->p_lock); 43960Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 43970Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43980Sstevel@tonic-gate pool_unlock(); 43990Sstevel@tonic-gate err = EINVAL; 44000Sstevel@tonic-gate goto out; 44010Sstevel@tonic-gate } 44020Sstevel@tonic-gate 44030Sstevel@tonic-gate /* 44040Sstevel@tonic-gate * Moreover, we don't allow processes whose encapsulating 44050Sstevel@tonic-gate * process contracts have inherited extrazonal contracts. 44060Sstevel@tonic-gate * While it would be easier to eliminate all process contracts 44070Sstevel@tonic-gate * with inherited contracts, we need to be able to give a 44080Sstevel@tonic-gate * restarted init (or other zone-penetrating process) its 44090Sstevel@tonic-gate * predecessor's contracts. 44100Sstevel@tonic-gate */ 44110Sstevel@tonic-gate if (ctp->conp_ninherited != 0) { 44120Sstevel@tonic-gate contract_t *next; 44130Sstevel@tonic-gate for (next = list_head(&ctp->conp_inherited); next; 44140Sstevel@tonic-gate next = list_next(&ctp->conp_inherited, next)) { 44150Sstevel@tonic-gate if (contract_getzuniqid(next) != zone->zone_uniqid) { 44160Sstevel@tonic-gate mutex_exit(&pp->p_lock); 44170Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 44180Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44190Sstevel@tonic-gate pool_unlock(); 44200Sstevel@tonic-gate err = EINVAL; 44210Sstevel@tonic-gate goto out; 44220Sstevel@tonic-gate } 44230Sstevel@tonic-gate } 44240Sstevel@tonic-gate } 44250Sstevel@tonic-gate mutex_exit(&pp->p_lock); 44260Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 44270Sstevel@tonic-gate 44280Sstevel@tonic-gate status = zone_status_get(zone); 44290Sstevel@tonic-gate if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 44300Sstevel@tonic-gate /* 44310Sstevel@tonic-gate * Can't join 44320Sstevel@tonic-gate */ 44330Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44340Sstevel@tonic-gate err = EINVAL; 44350Sstevel@tonic-gate goto out; 44360Sstevel@tonic-gate } 44370Sstevel@tonic-gate 44380Sstevel@tonic-gate /* 44390Sstevel@tonic-gate * Make sure new priv set is within the permitted set for caller 44400Sstevel@tonic-gate */ 44410Sstevel@tonic-gate if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 44420Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44430Sstevel@tonic-gate err = EPERM; 44440Sstevel@tonic-gate goto out; 44450Sstevel@tonic-gate } 44460Sstevel@tonic-gate /* 44470Sstevel@tonic-gate * We want to momentarily drop zonehash_lock while we optimistically 44480Sstevel@tonic-gate * bind curproc to the pool it should be running in. This is safe 44490Sstevel@tonic-gate * since the zone can't disappear (we have a hold on it). 44500Sstevel@tonic-gate */ 44510Sstevel@tonic-gate zone_hold(zone); 44520Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44530Sstevel@tonic-gate 44540Sstevel@tonic-gate /* 44550Sstevel@tonic-gate * Grab pool_lock to keep the pools configuration from changing 44560Sstevel@tonic-gate * and to stop ourselves from getting rebound to another pool 44570Sstevel@tonic-gate * until we join the zone. 44580Sstevel@tonic-gate */ 44590Sstevel@tonic-gate if (pool_lock_intr() != 0) { 44600Sstevel@tonic-gate zone_rele(zone); 44610Sstevel@tonic-gate err = EINTR; 44620Sstevel@tonic-gate goto out; 44630Sstevel@tonic-gate } 44640Sstevel@tonic-gate ASSERT(secpolicy_pool(CRED()) == 0); 44650Sstevel@tonic-gate /* 44660Sstevel@tonic-gate * Bind ourselves to the pool currently associated with the zone. 44670Sstevel@tonic-gate */ 44680Sstevel@tonic-gate oldpool = curproc->p_pool; 44690Sstevel@tonic-gate newpool = zone_pool_get(zone); 44700Sstevel@tonic-gate if (pool_state == POOL_ENABLED && newpool != oldpool && 44710Sstevel@tonic-gate (err = pool_do_bind(newpool, P_PID, P_MYID, 44720Sstevel@tonic-gate POOL_BIND_ALL)) != 0) { 44730Sstevel@tonic-gate pool_unlock(); 44740Sstevel@tonic-gate zone_rele(zone); 44750Sstevel@tonic-gate goto out; 44760Sstevel@tonic-gate } 44770Sstevel@tonic-gate 44780Sstevel@tonic-gate /* 44790Sstevel@tonic-gate * Grab cpu_lock now; we'll need it later when we call 44800Sstevel@tonic-gate * task_join(). 44810Sstevel@tonic-gate */ 44820Sstevel@tonic-gate mutex_enter(&cpu_lock); 44830Sstevel@tonic-gate mutex_enter(&zonehash_lock); 44840Sstevel@tonic-gate /* 44850Sstevel@tonic-gate * Make sure the zone hasn't moved on since we dropped zonehash_lock. 44860Sstevel@tonic-gate */ 44870Sstevel@tonic-gate if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 44880Sstevel@tonic-gate /* 44890Sstevel@tonic-gate * Can't join anymore. 44900Sstevel@tonic-gate */ 44910Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44920Sstevel@tonic-gate mutex_exit(&cpu_lock); 44930Sstevel@tonic-gate if (pool_state == POOL_ENABLED && 44940Sstevel@tonic-gate newpool != oldpool) 44950Sstevel@tonic-gate (void) pool_do_bind(oldpool, P_PID, P_MYID, 44960Sstevel@tonic-gate POOL_BIND_ALL); 44970Sstevel@tonic-gate pool_unlock(); 44980Sstevel@tonic-gate zone_rele(zone); 44990Sstevel@tonic-gate err = EINVAL; 45000Sstevel@tonic-gate goto out; 45010Sstevel@tonic-gate } 45020Sstevel@tonic-gate 4503*3247Sgjelinek /* 4504*3247Sgjelinek * a_lock must be held while transfering locked memory and swap 4505*3247Sgjelinek * reservation from the global zone to the non global zone because 4506*3247Sgjelinek * asynchronous faults on the processes' address space can lock 4507*3247Sgjelinek * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE 4508*3247Sgjelinek * segments respectively. 4509*3247Sgjelinek */ 4510*3247Sgjelinek AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER); 4511*3247Sgjelinek swap = as_swresv(); 45120Sstevel@tonic-gate mutex_enter(&pp->p_lock); 45130Sstevel@tonic-gate zone_proj0 = zone->zone_zsched->p_task->tk_proj; 45140Sstevel@tonic-gate /* verify that we do not exceed and task or lwp limits */ 45150Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 45160Sstevel@tonic-gate /* add new lwps to zone and zone's proj0 */ 45170Sstevel@tonic-gate zone_proj0->kpj_nlwps += pp->p_lwpcnt; 45180Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 45190Sstevel@tonic-gate /* add 1 task to zone's proj0 */ 45200Sstevel@tonic-gate zone_proj0->kpj_ntasks += 1; 45210Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 45220Sstevel@tonic-gate 4523*3247Sgjelinek mutex_enter(&zone->zone_mem_lock); 45242768Ssl108498 zone->zone_locked_mem += pp->p_locked_mem; 45252768Ssl108498 zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem; 4526*3247Sgjelinek zone->zone_max_swap += swap; 4527*3247Sgjelinek mutex_exit(&zone->zone_mem_lock); 45282768Ssl108498 45290Sstevel@tonic-gate /* remove lwps from proc's old zone and old project */ 45300Sstevel@tonic-gate mutex_enter(&pp->p_zone->zone_nlwps_lock); 45310Sstevel@tonic-gate pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 45320Sstevel@tonic-gate pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 45330Sstevel@tonic-gate mutex_exit(&pp->p_zone->zone_nlwps_lock); 45340Sstevel@tonic-gate 4535*3247Sgjelinek mutex_enter(&pp->p_zone->zone_mem_lock); 45362768Ssl108498 pp->p_zone->zone_locked_mem -= pp->p_locked_mem; 45372768Ssl108498 pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 4538*3247Sgjelinek pp->p_zone->zone_max_swap -= swap; 4539*3247Sgjelinek mutex_exit(&pp->p_zone->zone_mem_lock); 45402768Ssl108498 45412768Ssl108498 mutex_exit(&pp->p_lock); 4542*3247Sgjelinek AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock); 45432768Ssl108498 45440Sstevel@tonic-gate /* 45450Sstevel@tonic-gate * Joining the zone cannot fail from now on. 45460Sstevel@tonic-gate * 45470Sstevel@tonic-gate * This means that a lot of the following code can be commonized and 45480Sstevel@tonic-gate * shared with zsched(). 45490Sstevel@tonic-gate */ 45500Sstevel@tonic-gate 45510Sstevel@tonic-gate /* 45520Sstevel@tonic-gate * Reset the encapsulating process contract's zone. 45530Sstevel@tonic-gate */ 45540Sstevel@tonic-gate ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 45550Sstevel@tonic-gate contract_setzuniqid(ct, zone->zone_uniqid); 45560Sstevel@tonic-gate 45570Sstevel@tonic-gate /* 45580Sstevel@tonic-gate * Create a new task and associate the process with the project keyed 45590Sstevel@tonic-gate * by (projid,zoneid). 45600Sstevel@tonic-gate * 45610Sstevel@tonic-gate * We might as well be in project 0; the global zone's projid doesn't 45620Sstevel@tonic-gate * make much sense in a zone anyhow. 45630Sstevel@tonic-gate * 45640Sstevel@tonic-gate * This also increments zone_ntasks, and returns with p_lock held. 45650Sstevel@tonic-gate */ 45660Sstevel@tonic-gate tk = task_create(0, zone); 45670Sstevel@tonic-gate oldtk = task_join(tk, 0); 45680Sstevel@tonic-gate mutex_exit(&cpu_lock); 45690Sstevel@tonic-gate 45700Sstevel@tonic-gate pp->p_flag |= SZONETOP; 45710Sstevel@tonic-gate pp->p_zone = zone; 45720Sstevel@tonic-gate 45730Sstevel@tonic-gate /* 45740Sstevel@tonic-gate * call RCTLOP_SET functions on this proc 45750Sstevel@tonic-gate */ 45760Sstevel@tonic-gate e.rcep_p.zone = zone; 45770Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 45780Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 45790Sstevel@tonic-gate RCD_CALLBACK); 45800Sstevel@tonic-gate mutex_exit(&pp->p_lock); 45810Sstevel@tonic-gate 45820Sstevel@tonic-gate /* 45830Sstevel@tonic-gate * We don't need to hold any of zsched's locks here; not only do we know 45840Sstevel@tonic-gate * the process and zone aren't going away, we know its session isn't 45850Sstevel@tonic-gate * changing either. 45860Sstevel@tonic-gate * 45870Sstevel@tonic-gate * By joining zsched's session here, we mimic the behavior in the 45880Sstevel@tonic-gate * global zone of init's sid being the pid of sched. We extend this 45890Sstevel@tonic-gate * to all zlogin-like zone_enter()'ing processes as well. 45900Sstevel@tonic-gate */ 45910Sstevel@tonic-gate mutex_enter(&pidlock); 45920Sstevel@tonic-gate sp = zone->zone_zsched->p_sessp; 45932712Snn35248 sess_hold(zone->zone_zsched); 45940Sstevel@tonic-gate mutex_enter(&pp->p_lock); 45950Sstevel@tonic-gate pgexit(pp); 45962712Snn35248 sess_rele(pp->p_sessp, B_TRUE); 45970Sstevel@tonic-gate pp->p_sessp = sp; 45980Sstevel@tonic-gate pgjoin(pp, zone->zone_zsched->p_pidp); 4599*3247Sgjelinek 4600*3247Sgjelinek /* 4601*3247Sgjelinek * If there is a default scheduling class for the zone and it is not 4602*3247Sgjelinek * the class we are currently in, change all of the threads in the 4603*3247Sgjelinek * process to the new class. We need to be holding pidlock & p_lock 4604*3247Sgjelinek * when we call parmsset so this is a good place to do it. 4605*3247Sgjelinek */ 4606*3247Sgjelinek if (zone->zone_defaultcid > 0 && 4607*3247Sgjelinek zone->zone_defaultcid != curthread->t_cid) { 4608*3247Sgjelinek pcparms_t pcparms; 4609*3247Sgjelinek kthread_id_t t; 4610*3247Sgjelinek 4611*3247Sgjelinek pcparms.pc_cid = zone->zone_defaultcid; 4612*3247Sgjelinek pcparms.pc_clparms[0] = 0; 4613*3247Sgjelinek 4614*3247Sgjelinek /* 4615*3247Sgjelinek * If setting the class fails, we still want to enter the zone. 4616*3247Sgjelinek */ 4617*3247Sgjelinek if ((t = pp->p_tlist) != NULL) { 4618*3247Sgjelinek do { 4619*3247Sgjelinek (void) parmsset(&pcparms, t); 4620*3247Sgjelinek } while ((t = t->t_forw) != pp->p_tlist); 4621*3247Sgjelinek } 4622*3247Sgjelinek } 4623*3247Sgjelinek 46240Sstevel@tonic-gate mutex_exit(&pp->p_lock); 46250Sstevel@tonic-gate mutex_exit(&pidlock); 46260Sstevel@tonic-gate 46270Sstevel@tonic-gate mutex_exit(&zonehash_lock); 46280Sstevel@tonic-gate /* 46290Sstevel@tonic-gate * We're firmly in the zone; let pools progress. 46300Sstevel@tonic-gate */ 46310Sstevel@tonic-gate pool_unlock(); 46320Sstevel@tonic-gate task_rele(oldtk); 46330Sstevel@tonic-gate /* 46340Sstevel@tonic-gate * We don't need to retain a hold on the zone since we already 46350Sstevel@tonic-gate * incremented zone_ntasks, so the zone isn't going anywhere. 46360Sstevel@tonic-gate */ 46370Sstevel@tonic-gate zone_rele(zone); 46380Sstevel@tonic-gate 46390Sstevel@tonic-gate /* 46400Sstevel@tonic-gate * Chroot 46410Sstevel@tonic-gate */ 46420Sstevel@tonic-gate vp = zone->zone_rootvp; 46430Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 46440Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 46450Sstevel@tonic-gate 46460Sstevel@tonic-gate /* 46470Sstevel@tonic-gate * Change process credentials 46480Sstevel@tonic-gate */ 46490Sstevel@tonic-gate newcr = cralloc(); 46500Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 46510Sstevel@tonic-gate cr = pp->p_cred; 46520Sstevel@tonic-gate crcopy_to(cr, newcr); 46530Sstevel@tonic-gate crsetzone(newcr, zone); 46540Sstevel@tonic-gate pp->p_cred = newcr; 46550Sstevel@tonic-gate 46560Sstevel@tonic-gate /* 46570Sstevel@tonic-gate * Restrict all process privilege sets to zone limit 46580Sstevel@tonic-gate */ 46590Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 46600Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 46610Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 46620Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 46630Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 46640Sstevel@tonic-gate crset(pp, newcr); 46650Sstevel@tonic-gate 46660Sstevel@tonic-gate /* 46670Sstevel@tonic-gate * Adjust upcount to reflect zone entry. 46680Sstevel@tonic-gate */ 46690Sstevel@tonic-gate uid = crgetruid(newcr); 46700Sstevel@tonic-gate mutex_enter(&pidlock); 46710Sstevel@tonic-gate upcount_dec(uid, GLOBAL_ZONEID); 46720Sstevel@tonic-gate upcount_inc(uid, zoneid); 46730Sstevel@tonic-gate mutex_exit(&pidlock); 46740Sstevel@tonic-gate 46750Sstevel@tonic-gate /* 46760Sstevel@tonic-gate * Set up core file path and content. 46770Sstevel@tonic-gate */ 46780Sstevel@tonic-gate set_core_defaults(); 46790Sstevel@tonic-gate 46800Sstevel@tonic-gate out: 46810Sstevel@tonic-gate /* 46820Sstevel@tonic-gate * Let the other lwps continue. 46830Sstevel@tonic-gate */ 46840Sstevel@tonic-gate mutex_enter(&pp->p_lock); 46850Sstevel@tonic-gate if (curthread != pp->p_agenttp) 46860Sstevel@tonic-gate continuelwps(pp); 46870Sstevel@tonic-gate mutex_exit(&pp->p_lock); 46880Sstevel@tonic-gate 46890Sstevel@tonic-gate return (err != 0 ? set_errno(err) : 0); 46900Sstevel@tonic-gate } 46910Sstevel@tonic-gate 46920Sstevel@tonic-gate /* 46930Sstevel@tonic-gate * Systemcall entry point for zone_list(2). 46940Sstevel@tonic-gate * 46950Sstevel@tonic-gate * Processes running in a (non-global) zone only see themselves. 46961676Sjpk * On labeled systems, they see all zones whose label they dominate. 46970Sstevel@tonic-gate */ 46980Sstevel@tonic-gate static int 46990Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones) 47000Sstevel@tonic-gate { 47010Sstevel@tonic-gate zoneid_t *zoneids; 47021769Scarlsonj zone_t *zone, *myzone; 47030Sstevel@tonic-gate uint_t user_nzones, real_nzones; 47041676Sjpk uint_t domi_nzones; 47051676Sjpk int error; 47060Sstevel@tonic-gate 47070Sstevel@tonic-gate if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 47080Sstevel@tonic-gate return (set_errno(EFAULT)); 47090Sstevel@tonic-gate 47101769Scarlsonj myzone = curproc->p_zone; 47111769Scarlsonj if (myzone != global_zone) { 47121676Sjpk bslabel_t *mybslab; 47131676Sjpk 47141676Sjpk if (!is_system_labeled()) { 47151676Sjpk /* just return current zone */ 47161676Sjpk real_nzones = domi_nzones = 1; 47171676Sjpk zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 47181769Scarlsonj zoneids[0] = myzone->zone_id; 47191676Sjpk } else { 47201676Sjpk /* return all zones that are dominated */ 47211676Sjpk mutex_enter(&zonehash_lock); 47221676Sjpk real_nzones = zonecount; 47231676Sjpk domi_nzones = 0; 47241676Sjpk if (real_nzones > 0) { 47251676Sjpk zoneids = kmem_alloc(real_nzones * 47261676Sjpk sizeof (zoneid_t), KM_SLEEP); 47271769Scarlsonj mybslab = label2bslabel(myzone->zone_slabel); 47281676Sjpk for (zone = list_head(&zone_active); 47291676Sjpk zone != NULL; 47301676Sjpk zone = list_next(&zone_active, zone)) { 47311676Sjpk if (zone->zone_id == GLOBAL_ZONEID) 47321676Sjpk continue; 47331769Scarlsonj if (zone != myzone && 47341769Scarlsonj (zone->zone_flags & ZF_IS_SCRATCH)) 47351769Scarlsonj continue; 47361769Scarlsonj /* 47371769Scarlsonj * Note that a label always dominates 47381769Scarlsonj * itself, so myzone is always included 47391769Scarlsonj * in the list. 47401769Scarlsonj */ 47411676Sjpk if (bldominates(mybslab, 47421676Sjpk label2bslabel(zone->zone_slabel))) { 47431676Sjpk zoneids[domi_nzones++] = 47441676Sjpk zone->zone_id; 47451676Sjpk } 47461676Sjpk } 47471676Sjpk } 47481676Sjpk mutex_exit(&zonehash_lock); 47491676Sjpk } 47500Sstevel@tonic-gate } else { 47510Sstevel@tonic-gate mutex_enter(&zonehash_lock); 47520Sstevel@tonic-gate real_nzones = zonecount; 47531676Sjpk domi_nzones = 0; 47541676Sjpk if (real_nzones > 0) { 47550Sstevel@tonic-gate zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 47560Sstevel@tonic-gate KM_SLEEP); 47570Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 47580Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 47591676Sjpk zoneids[domi_nzones++] = zone->zone_id; 47601676Sjpk ASSERT(domi_nzones == real_nzones); 47610Sstevel@tonic-gate } 47620Sstevel@tonic-gate mutex_exit(&zonehash_lock); 47630Sstevel@tonic-gate } 47640Sstevel@tonic-gate 47651676Sjpk /* 47661676Sjpk * If user has allocated space for fewer entries than we found, then 47671676Sjpk * return only up to his limit. Either way, tell him exactly how many 47681676Sjpk * we found. 47691676Sjpk */ 47701676Sjpk if (domi_nzones < user_nzones) 47711676Sjpk user_nzones = domi_nzones; 47721676Sjpk error = 0; 47731676Sjpk if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) { 47740Sstevel@tonic-gate error = EFAULT; 47751676Sjpk } else if (zoneidlist != NULL && user_nzones != 0) { 47760Sstevel@tonic-gate if (copyout(zoneids, zoneidlist, 47770Sstevel@tonic-gate user_nzones * sizeof (zoneid_t)) != 0) 47780Sstevel@tonic-gate error = EFAULT; 47790Sstevel@tonic-gate } 47800Sstevel@tonic-gate 47811676Sjpk if (real_nzones > 0) 47820Sstevel@tonic-gate kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 47830Sstevel@tonic-gate 47841676Sjpk if (error != 0) 47850Sstevel@tonic-gate return (set_errno(error)); 47860Sstevel@tonic-gate else 47870Sstevel@tonic-gate return (0); 47880Sstevel@tonic-gate } 47890Sstevel@tonic-gate 47900Sstevel@tonic-gate /* 47910Sstevel@tonic-gate * Systemcall entry point for zone_lookup(2). 47920Sstevel@tonic-gate * 47931676Sjpk * Non-global zones are only able to see themselves and (on labeled systems) 47941676Sjpk * the zones they dominate. 47950Sstevel@tonic-gate */ 47960Sstevel@tonic-gate static zoneid_t 47970Sstevel@tonic-gate zone_lookup(const char *zone_name) 47980Sstevel@tonic-gate { 47990Sstevel@tonic-gate char *kname; 48000Sstevel@tonic-gate zone_t *zone; 48010Sstevel@tonic-gate zoneid_t zoneid; 48020Sstevel@tonic-gate int err; 48030Sstevel@tonic-gate 48040Sstevel@tonic-gate if (zone_name == NULL) { 48050Sstevel@tonic-gate /* return caller's zone id */ 48060Sstevel@tonic-gate return (getzoneid()); 48070Sstevel@tonic-gate } 48080Sstevel@tonic-gate 48090Sstevel@tonic-gate kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 48100Sstevel@tonic-gate if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 48110Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 48120Sstevel@tonic-gate return (set_errno(err)); 48130Sstevel@tonic-gate } 48140Sstevel@tonic-gate 48150Sstevel@tonic-gate mutex_enter(&zonehash_lock); 48160Sstevel@tonic-gate zone = zone_find_all_by_name(kname); 48170Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 48181676Sjpk /* 48191676Sjpk * In a non-global zone, can only lookup global and own name. 48201676Sjpk * In Trusted Extensions zone label dominance rules apply. 48211676Sjpk */ 48221676Sjpk if (zone == NULL || 48231676Sjpk zone_status_get(zone) < ZONE_IS_READY || 48241676Sjpk !zone_list_access(zone)) { 48250Sstevel@tonic-gate mutex_exit(&zonehash_lock); 48260Sstevel@tonic-gate return (set_errno(EINVAL)); 48271676Sjpk } else { 48281676Sjpk zoneid = zone->zone_id; 48291676Sjpk mutex_exit(&zonehash_lock); 48301676Sjpk return (zoneid); 48310Sstevel@tonic-gate } 48320Sstevel@tonic-gate } 48330Sstevel@tonic-gate 4834813Sdp static int 4835813Sdp zone_version(int *version_arg) 4836813Sdp { 4837813Sdp int version = ZONE_SYSCALL_API_VERSION; 4838813Sdp 4839813Sdp if (copyout(&version, version_arg, sizeof (int)) != 0) 4840813Sdp return (set_errno(EFAULT)); 4841813Sdp return (0); 4842813Sdp } 4843813Sdp 48440Sstevel@tonic-gate /* ARGSUSED */ 48450Sstevel@tonic-gate long 4846789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4) 48470Sstevel@tonic-gate { 48480Sstevel@tonic-gate zone_def zs; 48490Sstevel@tonic-gate 48500Sstevel@tonic-gate switch (cmd) { 48510Sstevel@tonic-gate case ZONE_CREATE: 48520Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 48530Sstevel@tonic-gate if (copyin(arg1, &zs, sizeof (zone_def))) { 48540Sstevel@tonic-gate return (set_errno(EFAULT)); 48550Sstevel@tonic-gate } 48560Sstevel@tonic-gate } else { 48570Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 48580Sstevel@tonic-gate zone_def32 zs32; 48590Sstevel@tonic-gate 48600Sstevel@tonic-gate if (copyin(arg1, &zs32, sizeof (zone_def32))) { 48610Sstevel@tonic-gate return (set_errno(EFAULT)); 48620Sstevel@tonic-gate } 48630Sstevel@tonic-gate zs.zone_name = 48640Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_name; 48650Sstevel@tonic-gate zs.zone_root = 48660Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_root; 48670Sstevel@tonic-gate zs.zone_privs = 48680Sstevel@tonic-gate (const struct priv_set *) 48690Sstevel@tonic-gate (unsigned long)zs32.zone_privs; 48701409Sdp zs.zone_privssz = zs32.zone_privssz; 48710Sstevel@tonic-gate zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 48720Sstevel@tonic-gate zs.rctlbufsz = zs32.rctlbufsz; 4873789Sahrens zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf; 4874789Sahrens zs.zfsbufsz = zs32.zfsbufsz; 48750Sstevel@tonic-gate zs.extended_error = 48760Sstevel@tonic-gate (int *)(unsigned long)zs32.extended_error; 48771676Sjpk zs.match = zs32.match; 48781676Sjpk zs.doi = zs32.doi; 48791676Sjpk zs.label = (const bslabel_t *)(uintptr_t)zs32.label; 48800Sstevel@tonic-gate #else 48810Sstevel@tonic-gate panic("get_udatamodel() returned bogus result\n"); 48820Sstevel@tonic-gate #endif 48830Sstevel@tonic-gate } 48840Sstevel@tonic-gate 48850Sstevel@tonic-gate return (zone_create(zs.zone_name, zs.zone_root, 4886813Sdp zs.zone_privs, zs.zone_privssz, 4887813Sdp (caddr_t)zs.rctlbuf, zs.rctlbufsz, 4888813Sdp (caddr_t)zs.zfsbuf, zs.zfsbufsz, 48891676Sjpk zs.extended_error, zs.match, zs.doi, 48901676Sjpk zs.label)); 48910Sstevel@tonic-gate case ZONE_BOOT: 48922267Sdp return (zone_boot((zoneid_t)(uintptr_t)arg1)); 48930Sstevel@tonic-gate case ZONE_DESTROY: 48940Sstevel@tonic-gate return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 48950Sstevel@tonic-gate case ZONE_GETATTR: 48960Sstevel@tonic-gate return (zone_getattr((zoneid_t)(uintptr_t)arg1, 48970Sstevel@tonic-gate (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 48982267Sdp case ZONE_SETATTR: 48992267Sdp return (zone_setattr((zoneid_t)(uintptr_t)arg1, 49002267Sdp (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 49010Sstevel@tonic-gate case ZONE_ENTER: 49020Sstevel@tonic-gate return (zone_enter((zoneid_t)(uintptr_t)arg1)); 49030Sstevel@tonic-gate case ZONE_LIST: 49040Sstevel@tonic-gate return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 49050Sstevel@tonic-gate case ZONE_SHUTDOWN: 49060Sstevel@tonic-gate return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 49070Sstevel@tonic-gate case ZONE_LOOKUP: 49080Sstevel@tonic-gate return (zone_lookup((const char *)arg1)); 4909813Sdp case ZONE_VERSION: 4910813Sdp return (zone_version((int *)arg1)); 49110Sstevel@tonic-gate default: 49120Sstevel@tonic-gate return (set_errno(EINVAL)); 49130Sstevel@tonic-gate } 49140Sstevel@tonic-gate } 49150Sstevel@tonic-gate 49160Sstevel@tonic-gate struct zarg { 49170Sstevel@tonic-gate zone_t *zone; 49180Sstevel@tonic-gate zone_cmd_arg_t arg; 49190Sstevel@tonic-gate }; 49200Sstevel@tonic-gate 49210Sstevel@tonic-gate static int 49220Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp) 49230Sstevel@tonic-gate { 49240Sstevel@tonic-gate char *buf; 49250Sstevel@tonic-gate size_t buflen; 49260Sstevel@tonic-gate int error; 49270Sstevel@tonic-gate 49280Sstevel@tonic-gate buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 49290Sstevel@tonic-gate buf = kmem_alloc(buflen, KM_SLEEP); 49300Sstevel@tonic-gate (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 49310Sstevel@tonic-gate error = door_ki_open(buf, doorp); 49320Sstevel@tonic-gate kmem_free(buf, buflen); 49330Sstevel@tonic-gate return (error); 49340Sstevel@tonic-gate } 49350Sstevel@tonic-gate 49360Sstevel@tonic-gate static void 49370Sstevel@tonic-gate zone_release_door(door_handle_t *doorp) 49380Sstevel@tonic-gate { 49390Sstevel@tonic-gate door_ki_rele(*doorp); 49400Sstevel@tonic-gate *doorp = NULL; 49410Sstevel@tonic-gate } 49420Sstevel@tonic-gate 49430Sstevel@tonic-gate static void 49440Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp) 49450Sstevel@tonic-gate { 49460Sstevel@tonic-gate door_handle_t door = NULL; 49470Sstevel@tonic-gate door_arg_t darg, save_arg; 49480Sstevel@tonic-gate char *zone_name; 49490Sstevel@tonic-gate size_t zone_namelen; 49500Sstevel@tonic-gate zoneid_t zoneid; 49510Sstevel@tonic-gate zone_t *zone; 49520Sstevel@tonic-gate zone_cmd_arg_t arg; 49530Sstevel@tonic-gate uint64_t uniqid; 49540Sstevel@tonic-gate size_t size; 49550Sstevel@tonic-gate int error; 49560Sstevel@tonic-gate int retry; 49570Sstevel@tonic-gate 49580Sstevel@tonic-gate zone = zargp->zone; 49590Sstevel@tonic-gate arg = zargp->arg; 49600Sstevel@tonic-gate kmem_free(zargp, sizeof (*zargp)); 49610Sstevel@tonic-gate 49620Sstevel@tonic-gate zone_namelen = strlen(zone->zone_name) + 1; 49630Sstevel@tonic-gate zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 49640Sstevel@tonic-gate bcopy(zone->zone_name, zone_name, zone_namelen); 49650Sstevel@tonic-gate zoneid = zone->zone_id; 49660Sstevel@tonic-gate uniqid = zone->zone_uniqid; 49670Sstevel@tonic-gate /* 49680Sstevel@tonic-gate * zoneadmd may be down, but at least we can empty out the zone. 49690Sstevel@tonic-gate * We can ignore the return value of zone_empty() since we're called 49700Sstevel@tonic-gate * from a kernel thread and know we won't be delivered any signals. 49710Sstevel@tonic-gate */ 49720Sstevel@tonic-gate ASSERT(curproc == &p0); 49730Sstevel@tonic-gate (void) zone_empty(zone); 49740Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 49750Sstevel@tonic-gate zone_rele(zone); 49760Sstevel@tonic-gate 49770Sstevel@tonic-gate size = sizeof (arg); 49780Sstevel@tonic-gate darg.rbuf = (char *)&arg; 49790Sstevel@tonic-gate darg.data_ptr = (char *)&arg; 49800Sstevel@tonic-gate darg.rsize = size; 49810Sstevel@tonic-gate darg.data_size = size; 49820Sstevel@tonic-gate darg.desc_ptr = NULL; 49830Sstevel@tonic-gate darg.desc_num = 0; 49840Sstevel@tonic-gate 49850Sstevel@tonic-gate save_arg = darg; 49860Sstevel@tonic-gate /* 49870Sstevel@tonic-gate * Since we're not holding a reference to the zone, any number of 49880Sstevel@tonic-gate * things can go wrong, including the zone disappearing before we get a 49890Sstevel@tonic-gate * chance to talk to zoneadmd. 49900Sstevel@tonic-gate */ 49910Sstevel@tonic-gate for (retry = 0; /* forever */; retry++) { 49920Sstevel@tonic-gate if (door == NULL && 49930Sstevel@tonic-gate (error = zone_lookup_door(zone_name, &door)) != 0) { 49940Sstevel@tonic-gate goto next; 49950Sstevel@tonic-gate } 49960Sstevel@tonic-gate ASSERT(door != NULL); 49970Sstevel@tonic-gate 49980Sstevel@tonic-gate if ((error = door_ki_upcall(door, &darg)) == 0) { 49990Sstevel@tonic-gate break; 50000Sstevel@tonic-gate } 50010Sstevel@tonic-gate switch (error) { 50020Sstevel@tonic-gate case EINTR: 50030Sstevel@tonic-gate /* FALLTHROUGH */ 50040Sstevel@tonic-gate case EAGAIN: /* process may be forking */ 50050Sstevel@tonic-gate /* 50060Sstevel@tonic-gate * Back off for a bit 50070Sstevel@tonic-gate */ 50080Sstevel@tonic-gate break; 50090Sstevel@tonic-gate case EBADF: 50100Sstevel@tonic-gate zone_release_door(&door); 50110Sstevel@tonic-gate if (zone_lookup_door(zone_name, &door) != 0) { 50120Sstevel@tonic-gate /* 50130Sstevel@tonic-gate * zoneadmd may be dead, but it may come back to 50140Sstevel@tonic-gate * life later. 50150Sstevel@tonic-gate */ 50160Sstevel@tonic-gate break; 50170Sstevel@tonic-gate } 50180Sstevel@tonic-gate break; 50190Sstevel@tonic-gate default: 50200Sstevel@tonic-gate cmn_err(CE_WARN, 50210Sstevel@tonic-gate "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 50220Sstevel@tonic-gate error); 50230Sstevel@tonic-gate goto out; 50240Sstevel@tonic-gate } 50250Sstevel@tonic-gate next: 50260Sstevel@tonic-gate /* 50270Sstevel@tonic-gate * If this isn't the same zone_t that we originally had in mind, 50280Sstevel@tonic-gate * then this is the same as if two kadmin requests come in at 50290Sstevel@tonic-gate * the same time: the first one wins. This means we lose, so we 50300Sstevel@tonic-gate * bail. 50310Sstevel@tonic-gate */ 50320Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) { 50330Sstevel@tonic-gate /* 50340Sstevel@tonic-gate * Problem is solved. 50350Sstevel@tonic-gate */ 50360Sstevel@tonic-gate break; 50370Sstevel@tonic-gate } 50380Sstevel@tonic-gate if (zone->zone_uniqid != uniqid) { 50390Sstevel@tonic-gate /* 50400Sstevel@tonic-gate * zoneid recycled 50410Sstevel@tonic-gate */ 50420Sstevel@tonic-gate zone_rele(zone); 50430Sstevel@tonic-gate break; 50440Sstevel@tonic-gate } 50450Sstevel@tonic-gate /* 50460Sstevel@tonic-gate * We could zone_status_timedwait(), but there doesn't seem to 50470Sstevel@tonic-gate * be much point in doing that (plus, it would mean that 50480Sstevel@tonic-gate * zone_free() isn't called until this thread exits). 50490Sstevel@tonic-gate */ 50500Sstevel@tonic-gate zone_rele(zone); 50510Sstevel@tonic-gate delay(hz); 50520Sstevel@tonic-gate darg = save_arg; 50530Sstevel@tonic-gate } 50540Sstevel@tonic-gate out: 50550Sstevel@tonic-gate if (door != NULL) { 50560Sstevel@tonic-gate zone_release_door(&door); 50570Sstevel@tonic-gate } 50580Sstevel@tonic-gate kmem_free(zone_name, zone_namelen); 50590Sstevel@tonic-gate thread_exit(); 50600Sstevel@tonic-gate } 50610Sstevel@tonic-gate 50620Sstevel@tonic-gate /* 50632267Sdp * Entry point for uadmin() to tell the zone to go away or reboot. Analog to 50642267Sdp * kadmin(). The caller is a process in the zone. 50650Sstevel@tonic-gate * 50660Sstevel@tonic-gate * In order to shutdown the zone, we will hand off control to zoneadmd 50670Sstevel@tonic-gate * (running in the global zone) via a door. We do a half-hearted job at 50680Sstevel@tonic-gate * killing all processes in the zone, create a kernel thread to contact 50690Sstevel@tonic-gate * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 50700Sstevel@tonic-gate * a form of generation number used to let zoneadmd (as well as 50710Sstevel@tonic-gate * zone_destroy()) know exactly which zone they're re talking about. 50720Sstevel@tonic-gate */ 50730Sstevel@tonic-gate int 50742267Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp) 50750Sstevel@tonic-gate { 50760Sstevel@tonic-gate struct zarg *zargp; 50770Sstevel@tonic-gate zone_cmd_t zcmd; 50780Sstevel@tonic-gate zone_t *zone; 50790Sstevel@tonic-gate 50800Sstevel@tonic-gate zone = curproc->p_zone; 50810Sstevel@tonic-gate ASSERT(getzoneid() != GLOBAL_ZONEID); 50820Sstevel@tonic-gate 50830Sstevel@tonic-gate switch (cmd) { 50840Sstevel@tonic-gate case A_SHUTDOWN: 50850Sstevel@tonic-gate switch (fcn) { 50860Sstevel@tonic-gate case AD_HALT: 50870Sstevel@tonic-gate case AD_POWEROFF: 50880Sstevel@tonic-gate zcmd = Z_HALT; 50890Sstevel@tonic-gate break; 50900Sstevel@tonic-gate case AD_BOOT: 50910Sstevel@tonic-gate zcmd = Z_REBOOT; 50920Sstevel@tonic-gate break; 50930Sstevel@tonic-gate case AD_IBOOT: 50940Sstevel@tonic-gate case AD_SBOOT: 50950Sstevel@tonic-gate case AD_SIBOOT: 50960Sstevel@tonic-gate case AD_NOSYNC: 50970Sstevel@tonic-gate return (ENOTSUP); 50980Sstevel@tonic-gate default: 50990Sstevel@tonic-gate return (EINVAL); 51000Sstevel@tonic-gate } 51010Sstevel@tonic-gate break; 51020Sstevel@tonic-gate case A_REBOOT: 51030Sstevel@tonic-gate zcmd = Z_REBOOT; 51040Sstevel@tonic-gate break; 51050Sstevel@tonic-gate case A_FTRACE: 51060Sstevel@tonic-gate case A_REMOUNT: 51070Sstevel@tonic-gate case A_FREEZE: 51080Sstevel@tonic-gate case A_DUMP: 51090Sstevel@tonic-gate return (ENOTSUP); 51100Sstevel@tonic-gate default: 51110Sstevel@tonic-gate ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 51120Sstevel@tonic-gate return (EINVAL); 51130Sstevel@tonic-gate } 51140Sstevel@tonic-gate 51150Sstevel@tonic-gate if (secpolicy_zone_admin(credp, B_FALSE)) 51160Sstevel@tonic-gate return (EPERM); 51170Sstevel@tonic-gate mutex_enter(&zone_status_lock); 51182267Sdp 51190Sstevel@tonic-gate /* 51200Sstevel@tonic-gate * zone_status can't be ZONE_IS_EMPTY or higher since curproc 51210Sstevel@tonic-gate * is in the zone. 51220Sstevel@tonic-gate */ 51230Sstevel@tonic-gate ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 51240Sstevel@tonic-gate if (zone_status_get(zone) > ZONE_IS_RUNNING) { 51250Sstevel@tonic-gate /* 51260Sstevel@tonic-gate * This zone is already on its way down. 51270Sstevel@tonic-gate */ 51280Sstevel@tonic-gate mutex_exit(&zone_status_lock); 51290Sstevel@tonic-gate return (0); 51300Sstevel@tonic-gate } 51310Sstevel@tonic-gate /* 51320Sstevel@tonic-gate * Prevent future zone_enter()s 51330Sstevel@tonic-gate */ 51340Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 51350Sstevel@tonic-gate mutex_exit(&zone_status_lock); 51360Sstevel@tonic-gate 51370Sstevel@tonic-gate /* 51380Sstevel@tonic-gate * Kill everyone now and call zoneadmd later. 51390Sstevel@tonic-gate * zone_ki_call_zoneadmd() will do a more thorough job of this 51400Sstevel@tonic-gate * later. 51410Sstevel@tonic-gate */ 51420Sstevel@tonic-gate killall(zone->zone_id); 51430Sstevel@tonic-gate /* 51440Sstevel@tonic-gate * Now, create the thread to contact zoneadmd and do the rest of the 51450Sstevel@tonic-gate * work. This thread can't be created in our zone otherwise 51460Sstevel@tonic-gate * zone_destroy() would deadlock. 51470Sstevel@tonic-gate */ 51482267Sdp zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP); 51490Sstevel@tonic-gate zargp->arg.cmd = zcmd; 51500Sstevel@tonic-gate zargp->arg.uniqid = zone->zone_uniqid; 51512267Sdp zargp->zone = zone; 51520Sstevel@tonic-gate (void) strcpy(zargp->arg.locale, "C"); 51532267Sdp /* mdep was already copied in for us by uadmin */ 51542267Sdp if (mdep != NULL) 51552267Sdp (void) strlcpy(zargp->arg.bootbuf, mdep, 51562267Sdp sizeof (zargp->arg.bootbuf)); 51572267Sdp zone_hold(zone); 51580Sstevel@tonic-gate 51590Sstevel@tonic-gate (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 51600Sstevel@tonic-gate TS_RUN, minclsyspri); 51610Sstevel@tonic-gate exit(CLD_EXITED, 0); 51620Sstevel@tonic-gate 51630Sstevel@tonic-gate return (EINVAL); 51640Sstevel@tonic-gate } 51650Sstevel@tonic-gate 51660Sstevel@tonic-gate /* 51670Sstevel@tonic-gate * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 51680Sstevel@tonic-gate * status to ZONE_IS_SHUTTING_DOWN. 51690Sstevel@tonic-gate */ 51700Sstevel@tonic-gate void 51710Sstevel@tonic-gate zone_shutdown_global(void) 51720Sstevel@tonic-gate { 51730Sstevel@tonic-gate ASSERT(curproc->p_zone == global_zone); 51740Sstevel@tonic-gate 51750Sstevel@tonic-gate mutex_enter(&zone_status_lock); 51760Sstevel@tonic-gate ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 51770Sstevel@tonic-gate zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 51780Sstevel@tonic-gate mutex_exit(&zone_status_lock); 51790Sstevel@tonic-gate } 5180789Sahrens 5181789Sahrens /* 5182789Sahrens * Returns true if the named dataset is visible in the current zone. 5183789Sahrens * The 'write' parameter is set to 1 if the dataset is also writable. 5184789Sahrens */ 5185789Sahrens int 5186789Sahrens zone_dataset_visible(const char *dataset, int *write) 5187789Sahrens { 5188789Sahrens zone_dataset_t *zd; 5189789Sahrens size_t len; 5190789Sahrens zone_t *zone = curproc->p_zone; 5191789Sahrens 5192789Sahrens if (dataset[0] == '\0') 5193789Sahrens return (0); 5194789Sahrens 5195789Sahrens /* 5196789Sahrens * Walk the list once, looking for datasets which match exactly, or 5197789Sahrens * specify a dataset underneath an exported dataset. If found, return 5198789Sahrens * true and note that it is writable. 5199789Sahrens */ 5200789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 5201789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 5202789Sahrens 5203789Sahrens len = strlen(zd->zd_dataset); 5204789Sahrens if (strlen(dataset) >= len && 5205789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 5206816Smaybee (dataset[len] == '\0' || dataset[len] == '/' || 5207816Smaybee dataset[len] == '@')) { 5208789Sahrens if (write) 5209789Sahrens *write = 1; 5210789Sahrens return (1); 5211789Sahrens } 5212789Sahrens } 5213789Sahrens 5214789Sahrens /* 5215789Sahrens * Walk the list a second time, searching for datasets which are parents 5216789Sahrens * of exported datasets. These should be visible, but read-only. 5217789Sahrens * 5218789Sahrens * Note that we also have to support forms such as 'pool/dataset/', with 5219789Sahrens * a trailing slash. 5220789Sahrens */ 5221789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 5222789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 5223789Sahrens 5224789Sahrens len = strlen(dataset); 5225789Sahrens if (dataset[len - 1] == '/') 5226789Sahrens len--; /* Ignore trailing slash */ 5227789Sahrens if (len < strlen(zd->zd_dataset) && 5228789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 5229789Sahrens zd->zd_dataset[len] == '/') { 5230789Sahrens if (write) 5231789Sahrens *write = 0; 5232789Sahrens return (1); 5233789Sahrens } 5234789Sahrens } 5235789Sahrens 5236789Sahrens return (0); 5237789Sahrens } 52381676Sjpk 52391676Sjpk /* 52401676Sjpk * zone_find_by_any_path() - 52411676Sjpk * 52421676Sjpk * kernel-private routine similar to zone_find_by_path(), but which 52431676Sjpk * effectively compares against zone paths rather than zonerootpath 52441676Sjpk * (i.e., the last component of zonerootpaths, which should be "root/", 52451676Sjpk * are not compared.) This is done in order to accurately identify all 52461676Sjpk * paths, whether zone-visible or not, including those which are parallel 52471676Sjpk * to /root/, such as /dev/, /home/, etc... 52481676Sjpk * 52491676Sjpk * If the specified path does not fall under any zone path then global 52501676Sjpk * zone is returned. 52511676Sjpk * 52521676Sjpk * The treat_abs parameter indicates whether the path should be treated as 52531676Sjpk * an absolute path although it does not begin with "/". (This supports 52541676Sjpk * nfs mount syntax such as host:any/path.) 52551676Sjpk * 52561676Sjpk * The caller is responsible for zone_rele of the returned zone. 52571676Sjpk */ 52581676Sjpk zone_t * 52591676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs) 52601676Sjpk { 52611676Sjpk zone_t *zone; 52621676Sjpk int path_offset = 0; 52631676Sjpk 52641676Sjpk if (path == NULL) { 52651676Sjpk zone_hold(global_zone); 52661676Sjpk return (global_zone); 52671676Sjpk } 52681676Sjpk 52691676Sjpk if (*path != '/') { 52701676Sjpk ASSERT(treat_abs); 52711676Sjpk path_offset = 1; 52721676Sjpk } 52731676Sjpk 52741676Sjpk mutex_enter(&zonehash_lock); 52751676Sjpk for (zone = list_head(&zone_active); zone != NULL; 52761676Sjpk zone = list_next(&zone_active, zone)) { 52771676Sjpk char *c; 52781676Sjpk size_t pathlen; 52791876Smp46848 char *rootpath_start; 52801676Sjpk 52811676Sjpk if (zone == global_zone) /* skip global zone */ 52821676Sjpk continue; 52831676Sjpk 52841676Sjpk /* scan backwards to find start of last component */ 52851676Sjpk c = zone->zone_rootpath + zone->zone_rootpathlen - 2; 52861676Sjpk do { 52871676Sjpk c--; 52881676Sjpk } while (*c != '/'); 52891676Sjpk 52901876Smp46848 pathlen = c - zone->zone_rootpath + 1 - path_offset; 52911876Smp46848 rootpath_start = (zone->zone_rootpath + path_offset); 52921876Smp46848 if (strncmp(path, rootpath_start, pathlen) == 0) 52931676Sjpk break; 52941676Sjpk } 52951676Sjpk if (zone == NULL) 52961676Sjpk zone = global_zone; 52971676Sjpk zone_hold(zone); 52981676Sjpk mutex_exit(&zonehash_lock); 52991676Sjpk return (zone); 53001676Sjpk } 5301