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 /* 238662SJordan.Vaughan@Sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * Zones 290Sstevel@tonic-gate * 300Sstevel@tonic-gate * A zone is a named collection of processes, namespace constraints, 310Sstevel@tonic-gate * and other system resources which comprise a secure and manageable 320Sstevel@tonic-gate * application containment facility. 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * Zones (represented by the reference counted zone_t) are tracked in 350Sstevel@tonic-gate * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs 360Sstevel@tonic-gate * (zoneid_t) are used to track zone association. Zone IDs are 370Sstevel@tonic-gate * dynamically generated when the zone is created; if a persistent 380Sstevel@tonic-gate * identifier is needed (core files, accounting logs, audit trail, 390Sstevel@tonic-gate * etc.), the zone name should be used. 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * 420Sstevel@tonic-gate * Global Zone: 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * The global zone (zoneid 0) is automatically associated with all 450Sstevel@tonic-gate * system resources that have not been bound to a user-created zone. 460Sstevel@tonic-gate * This means that even systems where zones are not in active use 470Sstevel@tonic-gate * have a global zone, and all processes, mounts, etc. are 480Sstevel@tonic-gate * associated with that zone. The global zone is generally 490Sstevel@tonic-gate * unconstrained in terms of privileges and access, though the usual 500Sstevel@tonic-gate * credential and privilege based restrictions apply. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * Zone States: 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * The states in which a zone may be in and the transitions are as 560Sstevel@tonic-gate * follows: 570Sstevel@tonic-gate * 580Sstevel@tonic-gate * ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially 590Sstevel@tonic-gate * initialized zone is added to the list of active zones on the system but 600Sstevel@tonic-gate * isn't accessible. 610Sstevel@tonic-gate * 625880Snordmark * ZONE_IS_INITIALIZED: Initialization complete except the ZSD callbacks are 635880Snordmark * not yet completed. Not possible to enter the zone, but attributes can 645880Snordmark * be retrieved. 655880Snordmark * 660Sstevel@tonic-gate * ZONE_IS_READY: zsched (the kernel dummy process for a zone) is 670Sstevel@tonic-gate * ready. The zone is made visible after the ZSD constructor callbacks are 680Sstevel@tonic-gate * executed. A zone remains in this state until it transitions into 690Sstevel@tonic-gate * the ZONE_IS_BOOTING state as a result of a call to zone_boot(). 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start 720Sstevel@tonic-gate * init. Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN 730Sstevel@tonic-gate * state. 740Sstevel@tonic-gate * 750Sstevel@tonic-gate * ZONE_IS_RUNNING: The zone is open for business: zsched has 760Sstevel@tonic-gate * successfully started init. A zone remains in this state until 770Sstevel@tonic-gate * zone_shutdown() is called. 780Sstevel@tonic-gate * 790Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is 800Sstevel@tonic-gate * killing all processes running in the zone. The zone remains 810Sstevel@tonic-gate * in this state until there are no more user processes running in the zone. 820Sstevel@tonic-gate * zone_create(), zone_enter(), and zone_destroy() on this zone will fail. 830Sstevel@tonic-gate * Since zone_shutdown() is restartable, it may be called successfully 840Sstevel@tonic-gate * multiple times for the same zone_t. Setting of the zone's state to 850Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check 860Sstevel@tonic-gate * the zone's status without worrying about it being a moving target. 870Sstevel@tonic-gate * 880Sstevel@tonic-gate * ZONE_IS_EMPTY: zone_shutdown() has been called, and there 890Sstevel@tonic-gate * are no more user processes in the zone. The zone remains in this 900Sstevel@tonic-gate * state until there are no more kernel threads associated with the 910Sstevel@tonic-gate * zone. zone_create(), zone_enter(), and zone_destroy() on this zone will 920Sstevel@tonic-gate * fail. 930Sstevel@tonic-gate * 940Sstevel@tonic-gate * ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone 950Sstevel@tonic-gate * have exited. zone_shutdown() returns. Henceforth it is not possible to 960Sstevel@tonic-gate * join the zone or create kernel threads therein. 970Sstevel@tonic-gate * 980Sstevel@tonic-gate * ZONE_IS_DYING: zone_destroy() has been called on the zone; zone 990Sstevel@tonic-gate * remains in this state until zsched exits. Calls to zone_find_by_*() 1000Sstevel@tonic-gate * return NULL from now on. 1010Sstevel@tonic-gate * 1020Sstevel@tonic-gate * ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0). There are no 1030Sstevel@tonic-gate * processes or threads doing work on behalf of the zone. The zone is 1040Sstevel@tonic-gate * removed from the list of active zones. zone_destroy() returns, and 1050Sstevel@tonic-gate * the zone can be recreated. 1060Sstevel@tonic-gate * 1070Sstevel@tonic-gate * ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor 1080Sstevel@tonic-gate * callbacks are executed, and all memory associated with the zone is 1090Sstevel@tonic-gate * freed. 1100Sstevel@tonic-gate * 1110Sstevel@tonic-gate * Threads can wait for the zone to enter a requested state by using 1120Sstevel@tonic-gate * zone_status_wait() or zone_status_timedwait() with the desired 1130Sstevel@tonic-gate * state passed in as an argument. Zone state transitions are 1140Sstevel@tonic-gate * uni-directional; it is not possible to move back to an earlier state. 1150Sstevel@tonic-gate * 1160Sstevel@tonic-gate * 1170Sstevel@tonic-gate * Zone-Specific Data: 1180Sstevel@tonic-gate * 1190Sstevel@tonic-gate * Subsystems needing to maintain zone-specific data can store that 1200Sstevel@tonic-gate * data using the ZSD mechanism. This provides a zone-specific data 1210Sstevel@tonic-gate * store, similar to thread-specific data (see pthread_getspecific(3C) 1220Sstevel@tonic-gate * or the TSD code in uts/common/disp/thread.c. Also, ZSD can be used 1230Sstevel@tonic-gate * to register callbacks to be invoked when a zone is created, shut 1240Sstevel@tonic-gate * down, or destroyed. This can be used to initialize zone-specific 1250Sstevel@tonic-gate * data for new zones and to clean up when zones go away. 1260Sstevel@tonic-gate * 1270Sstevel@tonic-gate * 1280Sstevel@tonic-gate * Data Structures: 1290Sstevel@tonic-gate * 1300Sstevel@tonic-gate * The per-zone structure (zone_t) is reference counted, and freed 1310Sstevel@tonic-gate * when all references are released. zone_hold and zone_rele can be 1320Sstevel@tonic-gate * used to adjust the reference count. In addition, reference counts 1330Sstevel@tonic-gate * associated with the cred_t structure are tracked separately using 1340Sstevel@tonic-gate * zone_cred_hold and zone_cred_rele. 1350Sstevel@tonic-gate * 1360Sstevel@tonic-gate * Pointers to active zone_t's are stored in two hash tables; one 1370Sstevel@tonic-gate * for searching by id, the other for searching by name. Lookups 1380Sstevel@tonic-gate * can be performed on either basis, using zone_find_by_id and 1390Sstevel@tonic-gate * zone_find_by_name. Both return zone_t pointers with the zone 1400Sstevel@tonic-gate * held, so zone_rele should be called when the pointer is no longer 1410Sstevel@tonic-gate * needed. Zones can also be searched by path; zone_find_by_path 1420Sstevel@tonic-gate * returns the zone with which a path name is associated (global 1430Sstevel@tonic-gate * zone if the path is not within some other zone's file system 1440Sstevel@tonic-gate * hierarchy). This currently requires iterating through each zone, 1450Sstevel@tonic-gate * so it is slower than an id or name search via a hash table. 1460Sstevel@tonic-gate * 1470Sstevel@tonic-gate * 1480Sstevel@tonic-gate * Locking: 1490Sstevel@tonic-gate * 1500Sstevel@tonic-gate * zonehash_lock: This is a top-level global lock used to protect the 1510Sstevel@tonic-gate * zone hash tables and lists. Zones cannot be created or destroyed 1520Sstevel@tonic-gate * while this lock is held. 1530Sstevel@tonic-gate * zone_status_lock: This is a global lock protecting zone state. 1540Sstevel@tonic-gate * Zones cannot change state while this lock is held. It also 1550Sstevel@tonic-gate * protects the list of kernel threads associated with a zone. 1560Sstevel@tonic-gate * zone_lock: This is a per-zone lock used to protect several fields of 1570Sstevel@tonic-gate * the zone_t (see <sys/zone.h> for details). In addition, holding 1580Sstevel@tonic-gate * this lock means that the zone cannot go away. 1593247Sgjelinek * zone_nlwps_lock: This is a per-zone lock used to protect the fields 1603247Sgjelinek * related to the zone.max-lwps rctl. 1613247Sgjelinek * zone_mem_lock: This is a per-zone lock used to protect the fields 1623247Sgjelinek * related to the zone.max-locked-memory and zone.max-swap rctls. 1630Sstevel@tonic-gate * zsd_key_lock: This is a global lock protecting the key state for ZSD. 1640Sstevel@tonic-gate * zone_deathrow_lock: This is a global lock protecting the "deathrow" 1650Sstevel@tonic-gate * list (a list of zones in the ZONE_IS_DEAD state). 1660Sstevel@tonic-gate * 1670Sstevel@tonic-gate * Ordering requirements: 1680Sstevel@tonic-gate * pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock --> 1690Sstevel@tonic-gate * zone_lock --> zsd_key_lock --> pidlock --> p_lock 1700Sstevel@tonic-gate * 1713247Sgjelinek * When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is: 1723247Sgjelinek * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 1733247Sgjelinek * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 1743247Sgjelinek * 1750Sstevel@tonic-gate * Blocking memory allocations are permitted while holding any of the 1760Sstevel@tonic-gate * zone locks. 1770Sstevel@tonic-gate * 1780Sstevel@tonic-gate * 1790Sstevel@tonic-gate * System Call Interface: 1800Sstevel@tonic-gate * 1810Sstevel@tonic-gate * The zone subsystem can be managed and queried from user level with 1820Sstevel@tonic-gate * the following system calls (all subcodes of the primary "zone" 1830Sstevel@tonic-gate * system call): 1840Sstevel@tonic-gate * - zone_create: creates a zone with selected attributes (name, 185789Sahrens * root path, privileges, resource controls, ZFS datasets) 1860Sstevel@tonic-gate * - zone_enter: allows the current process to enter a zone 1870Sstevel@tonic-gate * - zone_getattr: reports attributes of a zone 1882267Sdp * - zone_setattr: set attributes of a zone 1892267Sdp * - zone_boot: set 'init' running for the zone 1900Sstevel@tonic-gate * - zone_list: lists all zones active in the system 1910Sstevel@tonic-gate * - zone_lookup: looks up zone id based on name 1920Sstevel@tonic-gate * - zone_shutdown: initiates shutdown process (see states above) 1930Sstevel@tonic-gate * - zone_destroy: completes shutdown process (see states above) 1940Sstevel@tonic-gate * 1950Sstevel@tonic-gate */ 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate #include <sys/priv_impl.h> 1980Sstevel@tonic-gate #include <sys/cred.h> 1990Sstevel@tonic-gate #include <c2/audit.h> 2000Sstevel@tonic-gate #include <sys/debug.h> 2010Sstevel@tonic-gate #include <sys/file.h> 2020Sstevel@tonic-gate #include <sys/kmem.h> 2033247Sgjelinek #include <sys/kstat.h> 2040Sstevel@tonic-gate #include <sys/mutex.h> 2051676Sjpk #include <sys/note.h> 2060Sstevel@tonic-gate #include <sys/pathname.h> 2070Sstevel@tonic-gate #include <sys/proc.h> 2080Sstevel@tonic-gate #include <sys/project.h> 2091166Sdstaff #include <sys/sysevent.h> 2100Sstevel@tonic-gate #include <sys/task.h> 2110Sstevel@tonic-gate #include <sys/systm.h> 2120Sstevel@tonic-gate #include <sys/types.h> 2130Sstevel@tonic-gate #include <sys/utsname.h> 2140Sstevel@tonic-gate #include <sys/vnode.h> 2150Sstevel@tonic-gate #include <sys/vfs.h> 2160Sstevel@tonic-gate #include <sys/systeminfo.h> 2170Sstevel@tonic-gate #include <sys/policy.h> 2180Sstevel@tonic-gate #include <sys/cred_impl.h> 2190Sstevel@tonic-gate #include <sys/contract_impl.h> 2200Sstevel@tonic-gate #include <sys/contract/process_impl.h> 2210Sstevel@tonic-gate #include <sys/class.h> 2220Sstevel@tonic-gate #include <sys/pool.h> 2230Sstevel@tonic-gate #include <sys/pool_pset.h> 2240Sstevel@tonic-gate #include <sys/pset.h> 2250Sstevel@tonic-gate #include <sys/sysmacros.h> 2260Sstevel@tonic-gate #include <sys/callb.h> 2270Sstevel@tonic-gate #include <sys/vmparam.h> 2280Sstevel@tonic-gate #include <sys/corectl.h> 2292677Sml93401 #include <sys/ipc_impl.h> 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate #include <sys/door.h> 2320Sstevel@tonic-gate #include <sys/cpuvar.h> 2335880Snordmark #include <sys/sdt.h> 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate #include <sys/uadmin.h> 2360Sstevel@tonic-gate #include <sys/session.h> 2370Sstevel@tonic-gate #include <sys/cmn_err.h> 2380Sstevel@tonic-gate #include <sys/modhash.h> 2392267Sdp #include <sys/sunddi.h> 2400Sstevel@tonic-gate #include <sys/nvpair.h> 2410Sstevel@tonic-gate #include <sys/rctl.h> 2420Sstevel@tonic-gate #include <sys/fss.h> 2432712Snn35248 #include <sys/brand.h> 2440Sstevel@tonic-gate #include <sys/zone.h> 2453448Sdh155122 #include <net/if.h> 2463792Sakolb #include <sys/cpucaps.h> 2473247Sgjelinek #include <vm/seg.h> 24810616SSebastien.Roy@Sun.COM #include <sys/mac.h> 24910616SSebastien.Roy@Sun.COM 25010616SSebastien.Roy@Sun.COM /* List of data link IDs which are accessible from the zone */ 25110616SSebastien.Roy@Sun.COM typedef struct zone_dl { 25210616SSebastien.Roy@Sun.COM datalink_id_t zdl_id; 25310616SSebastien.Roy@Sun.COM list_node_t zdl_linkage; 25410616SSebastien.Roy@Sun.COM } zone_dl_t; 2553247Sgjelinek 2560Sstevel@tonic-gate /* 2570Sstevel@tonic-gate * cv used to signal that all references to the zone have been released. This 2580Sstevel@tonic-gate * needs to be global since there may be multiple waiters, and the first to 2590Sstevel@tonic-gate * wake up will free the zone_t, hence we cannot use zone->zone_cv. 2600Sstevel@tonic-gate */ 2610Sstevel@tonic-gate static kcondvar_t zone_destroy_cv; 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * Lock used to serialize access to zone_cv. This could have been per-zone, 2640Sstevel@tonic-gate * but then we'd need another lock for zone_destroy_cv, and why bother? 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate static kmutex_t zone_status_lock; 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* 2690Sstevel@tonic-gate * ZSD-related global variables. 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate static kmutex_t zsd_key_lock; /* protects the following two */ 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate static zone_key_t zsd_keyval = 0; 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * Global list of registered keys. We use this when a new zone is created. 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate static list_t zsd_registered_keys; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate int zone_hash_size = 256; 2821676Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel; 2830Sstevel@tonic-gate static kmutex_t zonehash_lock; 2840Sstevel@tonic-gate static uint_t zonecount; 2850Sstevel@tonic-gate static id_space_t *zoneid_space; 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate /* 2880Sstevel@tonic-gate * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 2890Sstevel@tonic-gate * kernel proper runs, and which manages all other zones. 2900Sstevel@tonic-gate * 2910Sstevel@tonic-gate * Although not declared as static, the variable "zone0" should not be used 2920Sstevel@tonic-gate * except for by code that needs to reference the global zone early on in boot, 2930Sstevel@tonic-gate * before it is fully initialized. All other consumers should use 2940Sstevel@tonic-gate * 'global_zone'. 2950Sstevel@tonic-gate */ 2960Sstevel@tonic-gate zone_t zone0; 2970Sstevel@tonic-gate zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate /* 3000Sstevel@tonic-gate * List of active zones, protected by zonehash_lock. 3010Sstevel@tonic-gate */ 3020Sstevel@tonic-gate static list_t zone_active; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate /* 3050Sstevel@tonic-gate * List of destroyed zones that still have outstanding cred references. 3060Sstevel@tonic-gate * Used for debugging. Uses a separate lock to avoid lock ordering 3070Sstevel@tonic-gate * problems in zone_free. 3080Sstevel@tonic-gate */ 3090Sstevel@tonic-gate static list_t zone_deathrow; 3100Sstevel@tonic-gate static kmutex_t zone_deathrow_lock; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */ 3130Sstevel@tonic-gate uint_t maxzones = 8192; 3140Sstevel@tonic-gate 3151166Sdstaff /* Event channel to sent zone state change notifications */ 3161166Sdstaff evchan_t *zone_event_chan; 3171166Sdstaff 3181166Sdstaff /* 3191166Sdstaff * This table holds the mapping from kernel zone states to 3201166Sdstaff * states visible in the state notification API. 3211166Sdstaff * The idea is that we only expose "obvious" states and 3221166Sdstaff * do not expose states which are just implementation details. 3231166Sdstaff */ 3241166Sdstaff const char *zone_status_table[] = { 3251166Sdstaff ZONE_EVENT_UNINITIALIZED, /* uninitialized */ 3265880Snordmark ZONE_EVENT_INITIALIZED, /* initialized */ 3271166Sdstaff ZONE_EVENT_READY, /* ready */ 3281166Sdstaff ZONE_EVENT_READY, /* booting */ 3291166Sdstaff ZONE_EVENT_RUNNING, /* running */ 3301166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* shutting_down */ 3311166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* empty */ 3321166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* down */ 3331166Sdstaff ZONE_EVENT_SHUTTING_DOWN, /* dying */ 3341166Sdstaff ZONE_EVENT_UNINITIALIZED, /* dead */ 3351166Sdstaff }; 3361166Sdstaff 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * This isn't static so lint doesn't complain. 3390Sstevel@tonic-gate */ 3400Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares; 3412768Ssl108498 rctl_hndl_t rc_zone_locked_mem; 3423247Sgjelinek rctl_hndl_t rc_zone_max_swap; 3433792Sakolb rctl_hndl_t rc_zone_cpu_cap; 3440Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps; 3452677Sml93401 rctl_hndl_t rc_zone_shmmax; 3462677Sml93401 rctl_hndl_t rc_zone_shmmni; 3472677Sml93401 rctl_hndl_t rc_zone_semmni; 3482677Sml93401 rctl_hndl_t rc_zone_msgmni; 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * Synchronization primitives used to synchronize between mounts and zone 3510Sstevel@tonic-gate * creation/destruction. 3520Sstevel@tonic-gate */ 3530Sstevel@tonic-gate static int mounts_in_progress; 3540Sstevel@tonic-gate static kcondvar_t mount_cv; 3550Sstevel@tonic-gate static kmutex_t mount_lock; 3560Sstevel@tonic-gate 3572267Sdp const char * const zone_default_initname = "/sbin/init"; 3581676Sjpk static char * const zone_prefix = "/zone/"; 3590Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid); 36010616SSebastien.Roy@Sun.COM static int zone_add_datalink(zoneid_t, datalink_id_t); 36110616SSebastien.Roy@Sun.COM static int zone_remove_datalink(zoneid_t, datalink_id_t); 36210616SSebastien.Roy@Sun.COM static int zone_list_datalink(zoneid_t, int *, datalink_id_t *); 3630Sstevel@tonic-gate 3645880Snordmark typedef boolean_t zsd_applyfn_t(kmutex_t *, boolean_t, zone_t *, zone_key_t); 3655880Snordmark 3665880Snordmark static void zsd_apply_all_zones(zsd_applyfn_t *, zone_key_t); 3675880Snordmark static void zsd_apply_all_keys(zsd_applyfn_t *, zone_t *); 3685880Snordmark static boolean_t zsd_apply_create(kmutex_t *, boolean_t, zone_t *, zone_key_t); 3695880Snordmark static boolean_t zsd_apply_shutdown(kmutex_t *, boolean_t, zone_t *, 3705880Snordmark zone_key_t); 3715880Snordmark static boolean_t zsd_apply_destroy(kmutex_t *, boolean_t, zone_t *, zone_key_t); 3725880Snordmark static boolean_t zsd_wait_for_creator(zone_t *, struct zsd_entry *, 3735880Snordmark kmutex_t *); 3745880Snordmark static boolean_t zsd_wait_for_inprogress(zone_t *, struct zsd_entry *, 3755880Snordmark kmutex_t *); 3765880Snordmark 3770Sstevel@tonic-gate /* 378813Sdp * Bump this number when you alter the zone syscall interfaces; this is 379813Sdp * because we need to have support for previous API versions in libc 380813Sdp * to support patching; libc calls into the kernel to determine this number. 381813Sdp * 382813Sdp * Version 1 of the API is the version originally shipped with Solaris 10 383813Sdp * Version 2 alters the zone_create system call in order to support more 384813Sdp * arguments by moving the args into a structure; and to do better 385813Sdp * error reporting when zone_create() fails. 386813Sdp * Version 3 alters the zone_create system call in order to support the 387813Sdp * import of ZFS datasets to zones. 3881676Sjpk * Version 4 alters the zone_create system call in order to support 3891676Sjpk * Trusted Extensions. 3902267Sdp * Version 5 alters the zone_boot system call, and converts its old 3912267Sdp * bootargs parameter to be set by the zone_setattr API instead. 3923448Sdh155122 * Version 6 adds the flag argument to zone_create. 393813Sdp */ 3943448Sdh155122 static const int ZONE_SYSCALL_API_VERSION = 6; 395813Sdp 396813Sdp /* 3970Sstevel@tonic-gate * Certain filesystems (such as NFS and autofs) need to know which zone 3980Sstevel@tonic-gate * the mount is being placed in. Because of this, we need to be able to 3990Sstevel@tonic-gate * ensure that a zone isn't in the process of being created such that 4000Sstevel@tonic-gate * nfs_mount() thinks it is in the global zone, while by the time it 4010Sstevel@tonic-gate * gets added the list of mounted zones, it ends up on zoneA's mount 4020Sstevel@tonic-gate * list. 4030Sstevel@tonic-gate * 4040Sstevel@tonic-gate * The following functions: block_mounts()/resume_mounts() and 4050Sstevel@tonic-gate * mount_in_progress()/mount_completed() are used by zones and the VFS 4060Sstevel@tonic-gate * layer (respectively) to synchronize zone creation and new mounts. 4070Sstevel@tonic-gate * 4080Sstevel@tonic-gate * The semantics are like a reader-reader lock such that there may 4090Sstevel@tonic-gate * either be multiple mounts (or zone creations, if that weren't 4100Sstevel@tonic-gate * serialized by zonehash_lock) in progress at the same time, but not 4110Sstevel@tonic-gate * both. 4120Sstevel@tonic-gate * 4130Sstevel@tonic-gate * We use cv's so the user can ctrl-C out of the operation if it's 4140Sstevel@tonic-gate * taking too long. 4150Sstevel@tonic-gate * 4160Sstevel@tonic-gate * The semantics are such that there is unfair bias towards the 4170Sstevel@tonic-gate * "current" operation. This means that zone creations may starve if 4180Sstevel@tonic-gate * there is a rapid succession of new mounts coming in to the system, or 4190Sstevel@tonic-gate * there is a remote possibility that zones will be created at such a 4200Sstevel@tonic-gate * rate that new mounts will not be able to proceed. 4210Sstevel@tonic-gate */ 4220Sstevel@tonic-gate /* 4230Sstevel@tonic-gate * Prevent new mounts from progressing to the point of calling 4240Sstevel@tonic-gate * VFS_MOUNT(). If there are already mounts in this "region", wait for 4250Sstevel@tonic-gate * them to complete. 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate static int 4280Sstevel@tonic-gate block_mounts(void) 4290Sstevel@tonic-gate { 4300Sstevel@tonic-gate int retval = 0; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate /* 4330Sstevel@tonic-gate * Since it may block for a long time, block_mounts() shouldn't be 4340Sstevel@tonic-gate * called with zonehash_lock held. 4350Sstevel@tonic-gate */ 4360Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 4370Sstevel@tonic-gate mutex_enter(&mount_lock); 4380Sstevel@tonic-gate while (mounts_in_progress > 0) { 4390Sstevel@tonic-gate if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 4400Sstevel@tonic-gate goto signaled; 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate /* 4430Sstevel@tonic-gate * A negative value of mounts_in_progress indicates that mounts 4440Sstevel@tonic-gate * have been blocked by (-mounts_in_progress) different callers. 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate mounts_in_progress--; 4470Sstevel@tonic-gate retval = 1; 4480Sstevel@tonic-gate signaled: 4490Sstevel@tonic-gate mutex_exit(&mount_lock); 4500Sstevel@tonic-gate return (retval); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate /* 4540Sstevel@tonic-gate * The VFS layer may progress with new mounts as far as we're concerned. 4550Sstevel@tonic-gate * Allow them to progress if we were the last obstacle. 4560Sstevel@tonic-gate */ 4570Sstevel@tonic-gate static void 4580Sstevel@tonic-gate resume_mounts(void) 4590Sstevel@tonic-gate { 4600Sstevel@tonic-gate mutex_enter(&mount_lock); 4610Sstevel@tonic-gate if (++mounts_in_progress == 0) 4620Sstevel@tonic-gate cv_broadcast(&mount_cv); 4630Sstevel@tonic-gate mutex_exit(&mount_lock); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * The VFS layer is busy with a mount; zones should wait until all 4680Sstevel@tonic-gate * mounts are completed to progress. 4690Sstevel@tonic-gate */ 4700Sstevel@tonic-gate void 4710Sstevel@tonic-gate mount_in_progress(void) 4720Sstevel@tonic-gate { 4730Sstevel@tonic-gate mutex_enter(&mount_lock); 4740Sstevel@tonic-gate while (mounts_in_progress < 0) 4750Sstevel@tonic-gate cv_wait(&mount_cv, &mount_lock); 4760Sstevel@tonic-gate mounts_in_progress++; 4770Sstevel@tonic-gate mutex_exit(&mount_lock); 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * VFS is done with one mount; wake up any waiting block_mounts() 4820Sstevel@tonic-gate * callers if this is the last mount. 4830Sstevel@tonic-gate */ 4840Sstevel@tonic-gate void 4850Sstevel@tonic-gate mount_completed(void) 4860Sstevel@tonic-gate { 4870Sstevel@tonic-gate mutex_enter(&mount_lock); 4880Sstevel@tonic-gate if (--mounts_in_progress == 0) 4890Sstevel@tonic-gate cv_broadcast(&mount_cv); 4900Sstevel@tonic-gate mutex_exit(&mount_lock); 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate /* 4940Sstevel@tonic-gate * ZSD routines. 4950Sstevel@tonic-gate * 4960Sstevel@tonic-gate * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 4970Sstevel@tonic-gate * defined by the pthread_key_create() and related interfaces. 4980Sstevel@tonic-gate * 4990Sstevel@tonic-gate * Kernel subsystems may register one or more data items and/or 5000Sstevel@tonic-gate * callbacks to be executed when a zone is created, shutdown, or 5010Sstevel@tonic-gate * destroyed. 5020Sstevel@tonic-gate * 5030Sstevel@tonic-gate * Unlike the thread counterpart, destructor callbacks will be executed 5040Sstevel@tonic-gate * even if the data pointer is NULL and/or there are no constructor 5050Sstevel@tonic-gate * callbacks, so it is the responsibility of such callbacks to check for 5060Sstevel@tonic-gate * NULL data values if necessary. 5070Sstevel@tonic-gate * 5080Sstevel@tonic-gate * The locking strategy and overall picture is as follows: 5090Sstevel@tonic-gate * 5100Sstevel@tonic-gate * When someone calls zone_key_create(), a template ZSD entry is added to the 5115880Snordmark * global list "zsd_registered_keys", protected by zsd_key_lock. While 5125880Snordmark * holding that lock all the existing zones are marked as 5135880Snordmark * ZSD_CREATE_NEEDED and a copy of the ZSD entry added to the per-zone 5145880Snordmark * zone_zsd list (protected by zone_lock). The global list is updated first 5155880Snordmark * (under zone_key_lock) to make sure that newly created zones use the 5165880Snordmark * most recent list of keys. Then under zonehash_lock we walk the zones 5175880Snordmark * and mark them. Similar locking is used in zone_key_delete(). 5180Sstevel@tonic-gate * 5195880Snordmark * The actual create, shutdown, and destroy callbacks are done without 5205880Snordmark * holding any lock. And zsd_flags are used to ensure that the operations 5215880Snordmark * completed so that when zone_key_create (and zone_create) is done, as well as 5225880Snordmark * zone_key_delete (and zone_destroy) is done, all the necessary callbacks 5235880Snordmark * are completed. 5240Sstevel@tonic-gate * 5250Sstevel@tonic-gate * When new zones are created constructor callbacks for all registered ZSD 5265880Snordmark * entries will be called. That also uses the above two phases of marking 5275880Snordmark * what needs to be done, and then running the callbacks without holding 5285880Snordmark * any locks. 5290Sstevel@tonic-gate * 5300Sstevel@tonic-gate * The framework does not provide any locking around zone_getspecific() and 5310Sstevel@tonic-gate * zone_setspecific() apart from that needed for internal consistency, so 5320Sstevel@tonic-gate * callers interested in atomic "test-and-set" semantics will need to provide 5330Sstevel@tonic-gate * their own locking. 5340Sstevel@tonic-gate */ 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /* 5370Sstevel@tonic-gate * Helper function to find the zsd_entry associated with the key in the 5380Sstevel@tonic-gate * given list. 5390Sstevel@tonic-gate */ 5400Sstevel@tonic-gate static struct zsd_entry * 5410Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key) 5420Sstevel@tonic-gate { 5430Sstevel@tonic-gate struct zsd_entry *zsd; 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 5460Sstevel@tonic-gate if (zsd->zsd_key == key) { 5475880Snordmark return (zsd); 5485880Snordmark } 5495880Snordmark } 5505880Snordmark return (NULL); 5515880Snordmark } 5525880Snordmark 5535880Snordmark /* 5545880Snordmark * Helper function to find the zsd_entry associated with the key in the 5555880Snordmark * given list. Move it to the front of the list. 5565880Snordmark */ 5575880Snordmark static struct zsd_entry * 5585880Snordmark zsd_find_mru(list_t *l, zone_key_t key) 5595880Snordmark { 5605880Snordmark struct zsd_entry *zsd; 5615880Snordmark 5625880Snordmark for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 5635880Snordmark if (zsd->zsd_key == key) { 5640Sstevel@tonic-gate /* 5650Sstevel@tonic-gate * Move to head of list to keep list in MRU order. 5660Sstevel@tonic-gate */ 5670Sstevel@tonic-gate if (zsd != list_head(l)) { 5680Sstevel@tonic-gate list_remove(l, zsd); 5690Sstevel@tonic-gate list_insert_head(l, zsd); 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate return (zsd); 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate return (NULL); 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate 5775880Snordmark void 5785880Snordmark zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 5795880Snordmark void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 5805880Snordmark { 5815880Snordmark struct zsd_entry *zsdp; 5825880Snordmark struct zsd_entry *t; 5835880Snordmark struct zone *zone; 5845880Snordmark zone_key_t key; 5855880Snordmark 5865880Snordmark zsdp = kmem_zalloc(sizeof (*zsdp), KM_SLEEP); 5875880Snordmark zsdp->zsd_data = NULL; 5885880Snordmark zsdp->zsd_create = create; 5895880Snordmark zsdp->zsd_shutdown = shutdown; 5905880Snordmark zsdp->zsd_destroy = destroy; 5915880Snordmark 5925880Snordmark /* 5935880Snordmark * Insert in global list of callbacks. Makes future zone creations 5945880Snordmark * see it. 5955880Snordmark */ 5965880Snordmark mutex_enter(&zsd_key_lock); 59710865SPramod.Batni@Sun.COM key = zsdp->zsd_key = ++zsd_keyval; 5985880Snordmark ASSERT(zsd_keyval != 0); 5995880Snordmark list_insert_tail(&zsd_registered_keys, zsdp); 6005880Snordmark mutex_exit(&zsd_key_lock); 6015880Snordmark 6025880Snordmark /* 6035880Snordmark * Insert for all existing zones and mark them as needing 6045880Snordmark * a create callback. 6055880Snordmark */ 6065880Snordmark mutex_enter(&zonehash_lock); /* stop the world */ 6075880Snordmark for (zone = list_head(&zone_active); zone != NULL; 6085880Snordmark zone = list_next(&zone_active, zone)) { 6095880Snordmark zone_status_t status; 6105880Snordmark 6115880Snordmark mutex_enter(&zone->zone_lock); 6125880Snordmark 6135880Snordmark /* Skip zones that are on the way down or not yet up */ 6145880Snordmark status = zone_status_get(zone); 6155880Snordmark if (status >= ZONE_IS_DOWN || 6165880Snordmark status == ZONE_IS_UNINITIALIZED) { 6175880Snordmark mutex_exit(&zone->zone_lock); 6185880Snordmark continue; 6195880Snordmark } 6205880Snordmark 6215880Snordmark t = zsd_find_mru(&zone->zone_zsd, key); 6225880Snordmark if (t != NULL) { 6235880Snordmark /* 6245880Snordmark * A zsd_configure already inserted it after 6255880Snordmark * we dropped zsd_key_lock above. 6265880Snordmark */ 6275880Snordmark mutex_exit(&zone->zone_lock); 6285880Snordmark continue; 6295880Snordmark } 6305880Snordmark t = kmem_zalloc(sizeof (*t), KM_SLEEP); 6315880Snordmark t->zsd_key = key; 6325880Snordmark t->zsd_create = create; 6335880Snordmark t->zsd_shutdown = shutdown; 6345880Snordmark t->zsd_destroy = destroy; 6355880Snordmark if (create != NULL) { 6365880Snordmark t->zsd_flags = ZSD_CREATE_NEEDED; 6375880Snordmark DTRACE_PROBE2(zsd__create__needed, 6385880Snordmark zone_t *, zone, zone_key_t, key); 6395880Snordmark } 6405880Snordmark list_insert_tail(&zone->zone_zsd, t); 6415880Snordmark mutex_exit(&zone->zone_lock); 6425880Snordmark } 6435880Snordmark mutex_exit(&zonehash_lock); 6445880Snordmark 6455880Snordmark if (create != NULL) { 6465880Snordmark /* Now call the create callback for this key */ 6475880Snordmark zsd_apply_all_zones(zsd_apply_create, key); 6485880Snordmark } 64910865SPramod.Batni@Sun.COM /* 65010910SRobert.Harris@Sun.COM * It is safe for consumers to use the key now, make it 65110910SRobert.Harris@Sun.COM * globally visible. Specifically zone_getspecific() will 65210910SRobert.Harris@Sun.COM * always successfully return the zone specific data associated 65310910SRobert.Harris@Sun.COM * with the key. 65410910SRobert.Harris@Sun.COM */ 65510865SPramod.Batni@Sun.COM *keyp = key; 65610865SPramod.Batni@Sun.COM 6575880Snordmark } 6585880Snordmark 6590Sstevel@tonic-gate /* 6600Sstevel@tonic-gate * Function called when a module is being unloaded, or otherwise wishes 6610Sstevel@tonic-gate * to unregister its ZSD key and callbacks. 6625880Snordmark * 6635880Snordmark * Remove from the global list and determine the functions that need to 6645880Snordmark * be called under a global lock. Then call the functions without 6655880Snordmark * holding any locks. Finally free up the zone_zsd entries. (The apply 6665880Snordmark * functions need to access the zone_zsd entries to find zsd_data etc.) 6670Sstevel@tonic-gate */ 6680Sstevel@tonic-gate int 6690Sstevel@tonic-gate zone_key_delete(zone_key_t key) 6700Sstevel@tonic-gate { 6710Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 6720Sstevel@tonic-gate zone_t *zone; 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 6755880Snordmark zsdp = zsd_find_mru(&zsd_registered_keys, key); 6765880Snordmark if (zsdp == NULL) { 6775880Snordmark mutex_exit(&zsd_key_lock); 6785880Snordmark return (-1); 6795880Snordmark } 6800Sstevel@tonic-gate list_remove(&zsd_registered_keys, zsdp); 6810Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 6820Sstevel@tonic-gate 6835880Snordmark mutex_enter(&zonehash_lock); 6840Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 6850Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 6860Sstevel@tonic-gate struct zsd_entry *del; 6875880Snordmark 6885880Snordmark mutex_enter(&zone->zone_lock); 6895880Snordmark del = zsd_find_mru(&zone->zone_zsd, key); 6905880Snordmark if (del == NULL) { 6915880Snordmark /* 6925880Snordmark * Somebody else got here first e.g the zone going 6935880Snordmark * away. 6945880Snordmark */ 6955880Snordmark mutex_exit(&zone->zone_lock); 6965880Snordmark continue; 6975880Snordmark } 6985880Snordmark ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 6995880Snordmark ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 7005880Snordmark if (del->zsd_shutdown != NULL && 7015880Snordmark (del->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) { 7025880Snordmark del->zsd_flags |= ZSD_SHUTDOWN_NEEDED; 7035880Snordmark DTRACE_PROBE2(zsd__shutdown__needed, 7045880Snordmark zone_t *, zone, zone_key_t, key); 7055880Snordmark } 7065880Snordmark if (del->zsd_destroy != NULL && 7075880Snordmark (del->zsd_flags & ZSD_DESTROY_ALL) == 0) { 7085880Snordmark del->zsd_flags |= ZSD_DESTROY_NEEDED; 7095880Snordmark DTRACE_PROBE2(zsd__destroy__needed, 7105880Snordmark zone_t *, zone, zone_key_t, key); 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate mutex_exit(&zonehash_lock); 7150Sstevel@tonic-gate kmem_free(zsdp, sizeof (*zsdp)); 7165880Snordmark 7175880Snordmark /* Now call the shutdown and destroy callback for this key */ 7185880Snordmark zsd_apply_all_zones(zsd_apply_shutdown, key); 7195880Snordmark zsd_apply_all_zones(zsd_apply_destroy, key); 7205880Snordmark 7215880Snordmark /* Now we can free up the zsdp structures in each zone */ 7225880Snordmark mutex_enter(&zonehash_lock); 7230Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 7245880Snordmark zone = list_next(&zone_active, zone)) { 7255880Snordmark struct zsd_entry *del; 7265880Snordmark 7275880Snordmark mutex_enter(&zone->zone_lock); 7285880Snordmark del = zsd_find(&zone->zone_zsd, key); 7295880Snordmark if (del != NULL) { 7305880Snordmark list_remove(&zone->zone_zsd, del); 7315880Snordmark ASSERT(!(del->zsd_flags & ZSD_ALL_INPROGRESS)); 7325880Snordmark kmem_free(del, sizeof (*del)); 7335880Snordmark } 7340Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7355880Snordmark } 7360Sstevel@tonic-gate mutex_exit(&zonehash_lock); 7375880Snordmark 7385880Snordmark return (0); 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate /* 7420Sstevel@tonic-gate * ZSD counterpart of pthread_setspecific(). 7435880Snordmark * 7445880Snordmark * Since all zsd callbacks, including those with no create function, 7455880Snordmark * have an entry in zone_zsd, if the key is registered it is part of 7465880Snordmark * the zone_zsd list. 7475880Snordmark * Return an error if the key wasn't registerd. 7480Sstevel@tonic-gate */ 7490Sstevel@tonic-gate int 7500Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 7510Sstevel@tonic-gate { 7520Sstevel@tonic-gate struct zsd_entry *t; 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 7555880Snordmark t = zsd_find_mru(&zone->zone_zsd, key); 7560Sstevel@tonic-gate if (t != NULL) { 7570Sstevel@tonic-gate /* 7580Sstevel@tonic-gate * Replace old value with new 7590Sstevel@tonic-gate */ 7600Sstevel@tonic-gate t->zsd_data = (void *)data; 7610Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7620Sstevel@tonic-gate return (0); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7655880Snordmark return (-1); 7660Sstevel@tonic-gate } 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate /* 7690Sstevel@tonic-gate * ZSD counterpart of pthread_getspecific(). 7700Sstevel@tonic-gate */ 7710Sstevel@tonic-gate void * 7720Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone) 7730Sstevel@tonic-gate { 7740Sstevel@tonic-gate struct zsd_entry *t; 7750Sstevel@tonic-gate void *data; 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 7785880Snordmark t = zsd_find_mru(&zone->zone_zsd, key); 7790Sstevel@tonic-gate data = (t == NULL ? NULL : t->zsd_data); 7800Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 7810Sstevel@tonic-gate return (data); 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate /* 7850Sstevel@tonic-gate * Function used to initialize a zone's list of ZSD callbacks and data 7860Sstevel@tonic-gate * when the zone is being created. The callbacks are initialized from 7875880Snordmark * the template list (zsd_registered_keys). The constructor callback is 7885880Snordmark * executed later (once the zone exists and with locks dropped). 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate static void 7910Sstevel@tonic-gate zone_zsd_configure(zone_t *zone) 7920Sstevel@tonic-gate { 7930Sstevel@tonic-gate struct zsd_entry *zsdp; 7940Sstevel@tonic-gate struct zsd_entry *t; 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 7970Sstevel@tonic-gate ASSERT(list_head(&zone->zone_zsd) == NULL); 7985880Snordmark mutex_enter(&zone->zone_lock); 7990Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 8000Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 8010Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 8025880Snordmark /* 8035880Snordmark * Since this zone is ZONE_IS_UNCONFIGURED, zone_key_create 8045880Snordmark * should not have added anything to it. 8055880Snordmark */ 8065880Snordmark ASSERT(zsd_find(&zone->zone_zsd, zsdp->zsd_key) == NULL); 8075880Snordmark 8085880Snordmark t = kmem_zalloc(sizeof (*t), KM_SLEEP); 8095880Snordmark t->zsd_key = zsdp->zsd_key; 8105880Snordmark t->zsd_create = zsdp->zsd_create; 8115880Snordmark t->zsd_shutdown = zsdp->zsd_shutdown; 8125880Snordmark t->zsd_destroy = zsdp->zsd_destroy; 8130Sstevel@tonic-gate if (zsdp->zsd_create != NULL) { 8145880Snordmark t->zsd_flags = ZSD_CREATE_NEEDED; 8155880Snordmark DTRACE_PROBE2(zsd__create__needed, 8165880Snordmark zone_t *, zone, zone_key_t, zsdp->zsd_key); 8170Sstevel@tonic-gate } 8185880Snordmark list_insert_tail(&zone->zone_zsd, t); 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 8215880Snordmark mutex_exit(&zone->zone_lock); 8220Sstevel@tonic-gate } 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate /* 8270Sstevel@tonic-gate * Helper function to execute shutdown or destructor callbacks. 8280Sstevel@tonic-gate */ 8290Sstevel@tonic-gate static void 8300Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 8310Sstevel@tonic-gate { 8320Sstevel@tonic-gate struct zsd_entry *t; 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 8350Sstevel@tonic-gate ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 8360Sstevel@tonic-gate ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 8370Sstevel@tonic-gate 8385880Snordmark /* 8395880Snordmark * Run the callback solely based on what is registered for the zone 8405880Snordmark * in zone_zsd. The global list can change independently of this 8415880Snordmark * as keys are registered and unregistered and we don't register new 8425880Snordmark * callbacks for a zone that is in the process of going away. 8435880Snordmark */ 8440Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 8455880Snordmark for (t = list_head(&zone->zone_zsd); t != NULL; 8465880Snordmark t = list_next(&zone->zone_zsd, t)) { 8475880Snordmark zone_key_t key = t->zsd_key; 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate /* Skip if no callbacks registered */ 8505880Snordmark 8515880Snordmark if (ct == ZSD_SHUTDOWN) { 8525880Snordmark if (t->zsd_shutdown != NULL && 8535880Snordmark (t->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) { 8545880Snordmark t->zsd_flags |= ZSD_SHUTDOWN_NEEDED; 8555880Snordmark DTRACE_PROBE2(zsd__shutdown__needed, 8565880Snordmark zone_t *, zone, zone_key_t, key); 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate } else { 8595880Snordmark if (t->zsd_destroy != NULL && 8605880Snordmark (t->zsd_flags & ZSD_DESTROY_ALL) == 0) { 8615880Snordmark t->zsd_flags |= ZSD_DESTROY_NEEDED; 8625880Snordmark DTRACE_PROBE2(zsd__destroy__needed, 8635880Snordmark zone_t *, zone, zone_key_t, key); 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate } 8675880Snordmark mutex_exit(&zone->zone_lock); 8685880Snordmark 8695880Snordmark /* Now call the shutdown and destroy callback for this key */ 8705880Snordmark zsd_apply_all_keys(zsd_apply_shutdown, zone); 8715880Snordmark zsd_apply_all_keys(zsd_apply_destroy, zone); 8725880Snordmark 8730Sstevel@tonic-gate } 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate /* 8760Sstevel@tonic-gate * Called when the zone is going away; free ZSD-related memory, and 8770Sstevel@tonic-gate * destroy the zone_zsd list. 8780Sstevel@tonic-gate */ 8790Sstevel@tonic-gate static void 8800Sstevel@tonic-gate zone_free_zsd(zone_t *zone) 8810Sstevel@tonic-gate { 8820Sstevel@tonic-gate struct zsd_entry *t, *next; 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate /* 8850Sstevel@tonic-gate * Free all the zsd_entry's we had on this zone. 8860Sstevel@tonic-gate */ 8875880Snordmark mutex_enter(&zone->zone_lock); 8880Sstevel@tonic-gate for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 8890Sstevel@tonic-gate next = list_next(&zone->zone_zsd, t); 8900Sstevel@tonic-gate list_remove(&zone->zone_zsd, t); 8915880Snordmark ASSERT(!(t->zsd_flags & ZSD_ALL_INPROGRESS)); 8920Sstevel@tonic-gate kmem_free(t, sizeof (*t)); 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate list_destroy(&zone->zone_zsd); 8955880Snordmark mutex_exit(&zone->zone_lock); 8965880Snordmark 8975880Snordmark } 8985880Snordmark 8995880Snordmark /* 9005880Snordmark * Apply a function to all zones for particular key value. 9015880Snordmark * 9025880Snordmark * The applyfn has to drop zonehash_lock if it does some work, and 9035880Snordmark * then reacquire it before it returns. 9045880Snordmark * When the lock is dropped we don't follow list_next even 9055880Snordmark * if it is possible to do so without any hazards. This is 9065880Snordmark * because we want the design to allow for the list of zones 9075880Snordmark * to change in any arbitrary way during the time the 9085880Snordmark * lock was dropped. 9095880Snordmark * 9105880Snordmark * It is safe to restart the loop at list_head since the applyfn 9115880Snordmark * changes the zsd_flags as it does work, so a subsequent 9125880Snordmark * pass through will have no effect in applyfn, hence the loop will terminate 9135880Snordmark * in at worst O(N^2). 9145880Snordmark */ 9155880Snordmark static void 9165880Snordmark zsd_apply_all_zones(zsd_applyfn_t *applyfn, zone_key_t key) 9175880Snordmark { 9185880Snordmark zone_t *zone; 9195880Snordmark 9205880Snordmark mutex_enter(&zonehash_lock); 9215880Snordmark zone = list_head(&zone_active); 9225880Snordmark while (zone != NULL) { 9235880Snordmark if ((applyfn)(&zonehash_lock, B_FALSE, zone, key)) { 9245880Snordmark /* Lock dropped - restart at head */ 9255880Snordmark zone = list_head(&zone_active); 9265880Snordmark } else { 9275880Snordmark zone = list_next(&zone_active, zone); 9285880Snordmark } 9295880Snordmark } 9305880Snordmark mutex_exit(&zonehash_lock); 9315880Snordmark } 9325880Snordmark 9335880Snordmark /* 9345880Snordmark * Apply a function to all keys for a particular zone. 9355880Snordmark * 9365880Snordmark * The applyfn has to drop zonehash_lock if it does some work, and 9375880Snordmark * then reacquire it before it returns. 9385880Snordmark * When the lock is dropped we don't follow list_next even 9395880Snordmark * if it is possible to do so without any hazards. This is 9405880Snordmark * because we want the design to allow for the list of zsd callbacks 9415880Snordmark * to change in any arbitrary way during the time the 9425880Snordmark * lock was dropped. 9435880Snordmark * 9445880Snordmark * It is safe to restart the loop at list_head since the applyfn 9455880Snordmark * changes the zsd_flags as it does work, so a subsequent 9465880Snordmark * pass through will have no effect in applyfn, hence the loop will terminate 9475880Snordmark * in at worst O(N^2). 9485880Snordmark */ 9495880Snordmark static void 9505880Snordmark zsd_apply_all_keys(zsd_applyfn_t *applyfn, zone_t *zone) 9515880Snordmark { 9525880Snordmark struct zsd_entry *t; 9535880Snordmark 9545880Snordmark mutex_enter(&zone->zone_lock); 9555880Snordmark t = list_head(&zone->zone_zsd); 9565880Snordmark while (t != NULL) { 9575880Snordmark if ((applyfn)(NULL, B_TRUE, zone, t->zsd_key)) { 9585880Snordmark /* Lock dropped - restart at head */ 9595880Snordmark t = list_head(&zone->zone_zsd); 9605880Snordmark } else { 9615880Snordmark t = list_next(&zone->zone_zsd, t); 9625880Snordmark } 9635880Snordmark } 9645880Snordmark mutex_exit(&zone->zone_lock); 9655880Snordmark } 9665880Snordmark 9675880Snordmark /* 9685880Snordmark * Call the create function for the zone and key if CREATE_NEEDED 9695880Snordmark * is set. 9705880Snordmark * If some other thread gets here first and sets CREATE_INPROGRESS, then 9715880Snordmark * we wait for that thread to complete so that we can ensure that 9725880Snordmark * all the callbacks are done when we've looped over all zones/keys. 9735880Snordmark * 9745880Snordmark * When we call the create function, we drop the global held by the 9755880Snordmark * caller, and return true to tell the caller it needs to re-evalute the 9765880Snordmark * state. 9775880Snordmark * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 9785880Snordmark * remains held on exit. 9795880Snordmark */ 9805880Snordmark static boolean_t 9815880Snordmark zsd_apply_create(kmutex_t *lockp, boolean_t zone_lock_held, 9825880Snordmark zone_t *zone, zone_key_t key) 9835880Snordmark { 9845880Snordmark void *result; 9855880Snordmark struct zsd_entry *t; 9865880Snordmark boolean_t dropped; 9875880Snordmark 9885880Snordmark if (lockp != NULL) { 9895880Snordmark ASSERT(MUTEX_HELD(lockp)); 9905880Snordmark } 9915880Snordmark if (zone_lock_held) { 9925880Snordmark ASSERT(MUTEX_HELD(&zone->zone_lock)); 9935880Snordmark } else { 9945880Snordmark mutex_enter(&zone->zone_lock); 9955880Snordmark } 9965880Snordmark 9975880Snordmark t = zsd_find(&zone->zone_zsd, key); 9985880Snordmark if (t == NULL) { 9995880Snordmark /* 10005880Snordmark * Somebody else got here first e.g the zone going 10015880Snordmark * away. 10025880Snordmark */ 10035880Snordmark if (!zone_lock_held) 10045880Snordmark mutex_exit(&zone->zone_lock); 10055880Snordmark return (B_FALSE); 10065880Snordmark } 10075880Snordmark dropped = B_FALSE; 10085880Snordmark if (zsd_wait_for_inprogress(zone, t, lockp)) 10095880Snordmark dropped = B_TRUE; 10105880Snordmark 10115880Snordmark if (t->zsd_flags & ZSD_CREATE_NEEDED) { 10125880Snordmark t->zsd_flags &= ~ZSD_CREATE_NEEDED; 10135880Snordmark t->zsd_flags |= ZSD_CREATE_INPROGRESS; 10145880Snordmark DTRACE_PROBE2(zsd__create__inprogress, 10155880Snordmark zone_t *, zone, zone_key_t, key); 10165880Snordmark mutex_exit(&zone->zone_lock); 10175880Snordmark if (lockp != NULL) 10185880Snordmark mutex_exit(lockp); 10195880Snordmark 10205880Snordmark dropped = B_TRUE; 10215880Snordmark ASSERT(t->zsd_create != NULL); 10225880Snordmark DTRACE_PROBE2(zsd__create__start, 10235880Snordmark zone_t *, zone, zone_key_t, key); 10245880Snordmark 10255880Snordmark result = (*t->zsd_create)(zone->zone_id); 10265880Snordmark 10275880Snordmark DTRACE_PROBE2(zsd__create__end, 10285880Snordmark zone_t *, zone, voidn *, result); 10295880Snordmark 10305880Snordmark ASSERT(result != NULL); 10315880Snordmark if (lockp != NULL) 10325880Snordmark mutex_enter(lockp); 10335880Snordmark mutex_enter(&zone->zone_lock); 10345880Snordmark t->zsd_data = result; 10355880Snordmark t->zsd_flags &= ~ZSD_CREATE_INPROGRESS; 10365880Snordmark t->zsd_flags |= ZSD_CREATE_COMPLETED; 10375880Snordmark cv_broadcast(&t->zsd_cv); 10385880Snordmark DTRACE_PROBE2(zsd__create__completed, 10395880Snordmark zone_t *, zone, zone_key_t, key); 10405880Snordmark } 10415880Snordmark if (!zone_lock_held) 10425880Snordmark mutex_exit(&zone->zone_lock); 10435880Snordmark return (dropped); 10445880Snordmark } 10455880Snordmark 10465880Snordmark /* 10475880Snordmark * Call the shutdown function for the zone and key if SHUTDOWN_NEEDED 10485880Snordmark * is set. 10495880Snordmark * If some other thread gets here first and sets *_INPROGRESS, then 10505880Snordmark * we wait for that thread to complete so that we can ensure that 10515880Snordmark * all the callbacks are done when we've looped over all zones/keys. 10525880Snordmark * 10535880Snordmark * When we call the shutdown function, we drop the global held by the 10545880Snordmark * caller, and return true to tell the caller it needs to re-evalute the 10555880Snordmark * state. 10565880Snordmark * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 10575880Snordmark * remains held on exit. 10585880Snordmark */ 10595880Snordmark static boolean_t 10605880Snordmark zsd_apply_shutdown(kmutex_t *lockp, boolean_t zone_lock_held, 10615880Snordmark zone_t *zone, zone_key_t key) 10625880Snordmark { 10635880Snordmark struct zsd_entry *t; 10645880Snordmark void *data; 10655880Snordmark boolean_t dropped; 10665880Snordmark 10675880Snordmark if (lockp != NULL) { 10685880Snordmark ASSERT(MUTEX_HELD(lockp)); 10695880Snordmark } 10705880Snordmark if (zone_lock_held) { 10715880Snordmark ASSERT(MUTEX_HELD(&zone->zone_lock)); 10725880Snordmark } else { 10735880Snordmark mutex_enter(&zone->zone_lock); 10745880Snordmark } 10755880Snordmark 10765880Snordmark t = zsd_find(&zone->zone_zsd, key); 10775880Snordmark if (t == NULL) { 10785880Snordmark /* 10795880Snordmark * Somebody else got here first e.g the zone going 10805880Snordmark * away. 10815880Snordmark */ 10825880Snordmark if (!zone_lock_held) 10835880Snordmark mutex_exit(&zone->zone_lock); 10845880Snordmark return (B_FALSE); 10855880Snordmark } 10865880Snordmark dropped = B_FALSE; 10875880Snordmark if (zsd_wait_for_creator(zone, t, lockp)) 10885880Snordmark dropped = B_TRUE; 10895880Snordmark 10905880Snordmark if (zsd_wait_for_inprogress(zone, t, lockp)) 10915880Snordmark dropped = B_TRUE; 10925880Snordmark 10935880Snordmark if (t->zsd_flags & ZSD_SHUTDOWN_NEEDED) { 10945880Snordmark t->zsd_flags &= ~ZSD_SHUTDOWN_NEEDED; 10955880Snordmark t->zsd_flags |= ZSD_SHUTDOWN_INPROGRESS; 10965880Snordmark DTRACE_PROBE2(zsd__shutdown__inprogress, 10975880Snordmark zone_t *, zone, zone_key_t, key); 10985880Snordmark mutex_exit(&zone->zone_lock); 10995880Snordmark if (lockp != NULL) 11005880Snordmark mutex_exit(lockp); 11015880Snordmark dropped = B_TRUE; 11025880Snordmark 11035880Snordmark ASSERT(t->zsd_shutdown != NULL); 11045880Snordmark data = t->zsd_data; 11055880Snordmark 11065880Snordmark DTRACE_PROBE2(zsd__shutdown__start, 11075880Snordmark zone_t *, zone, zone_key_t, key); 11085880Snordmark 11095880Snordmark (t->zsd_shutdown)(zone->zone_id, data); 11105880Snordmark DTRACE_PROBE2(zsd__shutdown__end, 11115880Snordmark zone_t *, zone, zone_key_t, key); 11125880Snordmark 11135880Snordmark if (lockp != NULL) 11145880Snordmark mutex_enter(lockp); 11155880Snordmark mutex_enter(&zone->zone_lock); 11165880Snordmark t->zsd_flags &= ~ZSD_SHUTDOWN_INPROGRESS; 11175880Snordmark t->zsd_flags |= ZSD_SHUTDOWN_COMPLETED; 11185880Snordmark cv_broadcast(&t->zsd_cv); 11195880Snordmark DTRACE_PROBE2(zsd__shutdown__completed, 11205880Snordmark zone_t *, zone, zone_key_t, key); 11215880Snordmark } 11225880Snordmark if (!zone_lock_held) 11235880Snordmark mutex_exit(&zone->zone_lock); 11245880Snordmark return (dropped); 11255880Snordmark } 11265880Snordmark 11275880Snordmark /* 11285880Snordmark * Call the destroy function for the zone and key if DESTROY_NEEDED 11295880Snordmark * is set. 11305880Snordmark * If some other thread gets here first and sets *_INPROGRESS, then 11315880Snordmark * we wait for that thread to complete so that we can ensure that 11325880Snordmark * all the callbacks are done when we've looped over all zones/keys. 11335880Snordmark * 11345880Snordmark * When we call the destroy function, we drop the global held by the 11355880Snordmark * caller, and return true to tell the caller it needs to re-evalute the 11365880Snordmark * state. 11375880Snordmark * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 11385880Snordmark * remains held on exit. 11395880Snordmark */ 11405880Snordmark static boolean_t 11415880Snordmark zsd_apply_destroy(kmutex_t *lockp, boolean_t zone_lock_held, 11425880Snordmark zone_t *zone, zone_key_t key) 11435880Snordmark { 11445880Snordmark struct zsd_entry *t; 11455880Snordmark void *data; 11465880Snordmark boolean_t dropped; 11475880Snordmark 11485880Snordmark if (lockp != NULL) { 11495880Snordmark ASSERT(MUTEX_HELD(lockp)); 11505880Snordmark } 11515880Snordmark if (zone_lock_held) { 11525880Snordmark ASSERT(MUTEX_HELD(&zone->zone_lock)); 11535880Snordmark } else { 11545880Snordmark mutex_enter(&zone->zone_lock); 11555880Snordmark } 11565880Snordmark 11575880Snordmark t = zsd_find(&zone->zone_zsd, key); 11585880Snordmark if (t == NULL) { 11595880Snordmark /* 11605880Snordmark * Somebody else got here first e.g the zone going 11615880Snordmark * away. 11625880Snordmark */ 11635880Snordmark if (!zone_lock_held) 11645880Snordmark mutex_exit(&zone->zone_lock); 11655880Snordmark return (B_FALSE); 11665880Snordmark } 11675880Snordmark dropped = B_FALSE; 11685880Snordmark if (zsd_wait_for_creator(zone, t, lockp)) 11695880Snordmark dropped = B_TRUE; 11705880Snordmark 11715880Snordmark if (zsd_wait_for_inprogress(zone, t, lockp)) 11725880Snordmark dropped = B_TRUE; 11735880Snordmark 11745880Snordmark if (t->zsd_flags & ZSD_DESTROY_NEEDED) { 11755880Snordmark t->zsd_flags &= ~ZSD_DESTROY_NEEDED; 11765880Snordmark t->zsd_flags |= ZSD_DESTROY_INPROGRESS; 11775880Snordmark DTRACE_PROBE2(zsd__destroy__inprogress, 11785880Snordmark zone_t *, zone, zone_key_t, key); 11795880Snordmark mutex_exit(&zone->zone_lock); 11805880Snordmark if (lockp != NULL) 11815880Snordmark mutex_exit(lockp); 11825880Snordmark dropped = B_TRUE; 11835880Snordmark 11845880Snordmark ASSERT(t->zsd_destroy != NULL); 11855880Snordmark data = t->zsd_data; 11865880Snordmark DTRACE_PROBE2(zsd__destroy__start, 11875880Snordmark zone_t *, zone, zone_key_t, key); 11885880Snordmark 11895880Snordmark (t->zsd_destroy)(zone->zone_id, data); 11905880Snordmark DTRACE_PROBE2(zsd__destroy__end, 11915880Snordmark zone_t *, zone, zone_key_t, key); 11925880Snordmark 11935880Snordmark if (lockp != NULL) 11945880Snordmark mutex_enter(lockp); 11955880Snordmark mutex_enter(&zone->zone_lock); 11965880Snordmark t->zsd_data = NULL; 11975880Snordmark t->zsd_flags &= ~ZSD_DESTROY_INPROGRESS; 11985880Snordmark t->zsd_flags |= ZSD_DESTROY_COMPLETED; 11995880Snordmark cv_broadcast(&t->zsd_cv); 12005880Snordmark DTRACE_PROBE2(zsd__destroy__completed, 12015880Snordmark zone_t *, zone, zone_key_t, key); 12025880Snordmark } 12035880Snordmark if (!zone_lock_held) 12045880Snordmark mutex_exit(&zone->zone_lock); 12055880Snordmark return (dropped); 12065880Snordmark } 12075880Snordmark 12085880Snordmark /* 12095880Snordmark * Wait for any CREATE_NEEDED flag to be cleared. 12105880Snordmark * Returns true if lockp was temporarily dropped while waiting. 12115880Snordmark */ 12125880Snordmark static boolean_t 12135880Snordmark zsd_wait_for_creator(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp) 12145880Snordmark { 12155880Snordmark boolean_t dropped = B_FALSE; 12165880Snordmark 12175880Snordmark while (t->zsd_flags & ZSD_CREATE_NEEDED) { 12185880Snordmark DTRACE_PROBE2(zsd__wait__for__creator, 12195880Snordmark zone_t *, zone, struct zsd_entry *, t); 12205880Snordmark if (lockp != NULL) { 12215880Snordmark dropped = B_TRUE; 12225880Snordmark mutex_exit(lockp); 12235880Snordmark } 12245880Snordmark cv_wait(&t->zsd_cv, &zone->zone_lock); 12255880Snordmark if (lockp != NULL) { 12265880Snordmark /* First drop zone_lock to preserve order */ 12275880Snordmark mutex_exit(&zone->zone_lock); 12285880Snordmark mutex_enter(lockp); 12295880Snordmark mutex_enter(&zone->zone_lock); 12305880Snordmark } 12315880Snordmark } 12325880Snordmark return (dropped); 12335880Snordmark } 12345880Snordmark 12355880Snordmark /* 12365880Snordmark * Wait for any INPROGRESS flag to be cleared. 12375880Snordmark * Returns true if lockp was temporarily dropped while waiting. 12385880Snordmark */ 12395880Snordmark static boolean_t 12405880Snordmark zsd_wait_for_inprogress(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp) 12415880Snordmark { 12425880Snordmark boolean_t dropped = B_FALSE; 12435880Snordmark 12445880Snordmark while (t->zsd_flags & ZSD_ALL_INPROGRESS) { 12455880Snordmark DTRACE_PROBE2(zsd__wait__for__inprogress, 12465880Snordmark zone_t *, zone, struct zsd_entry *, t); 12475880Snordmark if (lockp != NULL) { 12485880Snordmark dropped = B_TRUE; 12495880Snordmark mutex_exit(lockp); 12505880Snordmark } 12515880Snordmark cv_wait(&t->zsd_cv, &zone->zone_lock); 12525880Snordmark if (lockp != NULL) { 12535880Snordmark /* First drop zone_lock to preserve order */ 12545880Snordmark mutex_exit(&zone->zone_lock); 12555880Snordmark mutex_enter(lockp); 12565880Snordmark mutex_enter(&zone->zone_lock); 12575880Snordmark } 12585880Snordmark } 12595880Snordmark return (dropped); 12600Sstevel@tonic-gate } 12610Sstevel@tonic-gate 12620Sstevel@tonic-gate /* 1263789Sahrens * Frees memory associated with the zone dataset list. 1264789Sahrens */ 1265789Sahrens static void 1266789Sahrens zone_free_datasets(zone_t *zone) 1267789Sahrens { 1268789Sahrens zone_dataset_t *t, *next; 1269789Sahrens 1270789Sahrens for (t = list_head(&zone->zone_datasets); t != NULL; t = next) { 1271789Sahrens next = list_next(&zone->zone_datasets, t); 1272789Sahrens list_remove(&zone->zone_datasets, t); 1273789Sahrens kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1); 1274789Sahrens kmem_free(t, sizeof (*t)); 1275789Sahrens } 1276789Sahrens list_destroy(&zone->zone_datasets); 1277789Sahrens } 1278789Sahrens 1279789Sahrens /* 12800Sstevel@tonic-gate * zone.cpu-shares resource control support. 12810Sstevel@tonic-gate */ 12820Sstevel@tonic-gate /*ARGSUSED*/ 12830Sstevel@tonic-gate static rctl_qty_t 12840Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 12850Sstevel@tonic-gate { 12860Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 12870Sstevel@tonic-gate return (p->p_zone->zone_shares); 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate /*ARGSUSED*/ 12910Sstevel@tonic-gate static int 12920Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 12930Sstevel@tonic-gate rctl_qty_t nv) 12940Sstevel@tonic-gate { 12950Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 12960Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 12970Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 12980Sstevel@tonic-gate return (0); 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate e->rcep_p.zone->zone_shares = nv; 13010Sstevel@tonic-gate return (0); 13020Sstevel@tonic-gate } 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = { 13050Sstevel@tonic-gate rcop_no_action, 13060Sstevel@tonic-gate zone_cpu_shares_usage, 13070Sstevel@tonic-gate zone_cpu_shares_set, 13080Sstevel@tonic-gate rcop_no_test 13090Sstevel@tonic-gate }; 13100Sstevel@tonic-gate 13113792Sakolb /* 13123792Sakolb * zone.cpu-cap resource control support. 13133792Sakolb */ 13143792Sakolb /*ARGSUSED*/ 13153792Sakolb static rctl_qty_t 13163792Sakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p) 13173792Sakolb { 13183792Sakolb ASSERT(MUTEX_HELD(&p->p_lock)); 13193792Sakolb return (cpucaps_zone_get(p->p_zone)); 13203792Sakolb } 13213792Sakolb 13223792Sakolb /*ARGSUSED*/ 13233792Sakolb static int 13243792Sakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 13253792Sakolb rctl_qty_t nv) 13263792Sakolb { 13273792Sakolb zone_t *zone = e->rcep_p.zone; 13283792Sakolb 13293792Sakolb ASSERT(MUTEX_HELD(&p->p_lock)); 13303792Sakolb ASSERT(e->rcep_t == RCENTITY_ZONE); 13313792Sakolb 13323792Sakolb if (zone == NULL) 13333792Sakolb return (0); 13343792Sakolb 13353792Sakolb /* 13363792Sakolb * set cap to the new value. 13373792Sakolb */ 13383792Sakolb return (cpucaps_zone_set(zone, nv)); 13393792Sakolb } 13403792Sakolb 13413792Sakolb static rctl_ops_t zone_cpu_cap_ops = { 13423792Sakolb rcop_no_action, 13433792Sakolb zone_cpu_cap_get, 13443792Sakolb zone_cpu_cap_set, 13453792Sakolb rcop_no_test 13463792Sakolb }; 13473792Sakolb 13480Sstevel@tonic-gate /*ARGSUSED*/ 13490Sstevel@tonic-gate static rctl_qty_t 13500Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p) 13510Sstevel@tonic-gate { 13520Sstevel@tonic-gate rctl_qty_t nlwps; 13530Sstevel@tonic-gate zone_t *zone = p->p_zone; 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13560Sstevel@tonic-gate 13570Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 13580Sstevel@tonic-gate nlwps = zone->zone_nlwps; 13590Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 13600Sstevel@tonic-gate 13610Sstevel@tonic-gate return (nlwps); 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate /*ARGSUSED*/ 13650Sstevel@tonic-gate static int 13660Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 13670Sstevel@tonic-gate rctl_qty_t incr, uint_t flags) 13680Sstevel@tonic-gate { 13690Sstevel@tonic-gate rctl_qty_t nlwps; 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13720Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 13730Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 13740Sstevel@tonic-gate return (0); 13750Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 13760Sstevel@tonic-gate nlwps = e->rcep_p.zone->zone_nlwps; 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate if (nlwps + incr > rcntl->rcv_value) 13790Sstevel@tonic-gate return (1); 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate return (0); 13820Sstevel@tonic-gate } 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate /*ARGSUSED*/ 13850Sstevel@tonic-gate static int 13862768Ssl108498 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) 13872768Ssl108498 { 13880Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13890Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 13900Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 13910Sstevel@tonic-gate return (0); 13920Sstevel@tonic-gate e->rcep_p.zone->zone_nlwps_ctl = nv; 13930Sstevel@tonic-gate return (0); 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = { 13970Sstevel@tonic-gate rcop_no_action, 13980Sstevel@tonic-gate zone_lwps_usage, 13990Sstevel@tonic-gate zone_lwps_set, 14000Sstevel@tonic-gate zone_lwps_test, 14010Sstevel@tonic-gate }; 14020Sstevel@tonic-gate 14032677Sml93401 /*ARGSUSED*/ 14042677Sml93401 static int 14052677Sml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 14062677Sml93401 rctl_qty_t incr, uint_t flags) 14072677Sml93401 { 14082677Sml93401 rctl_qty_t v; 14092677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 14102677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 14112677Sml93401 v = e->rcep_p.zone->zone_shmmax + incr; 14122677Sml93401 if (v > rval->rcv_value) 14132677Sml93401 return (1); 14142677Sml93401 return (0); 14152677Sml93401 } 14162677Sml93401 14172677Sml93401 static rctl_ops_t zone_shmmax_ops = { 14182677Sml93401 rcop_no_action, 14192677Sml93401 rcop_no_usage, 14202677Sml93401 rcop_no_set, 14212677Sml93401 zone_shmmax_test 14222677Sml93401 }; 14232677Sml93401 14242677Sml93401 /*ARGSUSED*/ 14252677Sml93401 static int 14262677Sml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 14272677Sml93401 rctl_qty_t incr, uint_t flags) 14282677Sml93401 { 14292677Sml93401 rctl_qty_t v; 14302677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 14312677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 14322677Sml93401 v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr; 14332677Sml93401 if (v > rval->rcv_value) 14342677Sml93401 return (1); 14352677Sml93401 return (0); 14362677Sml93401 } 14372677Sml93401 14382677Sml93401 static rctl_ops_t zone_shmmni_ops = { 14392677Sml93401 rcop_no_action, 14402677Sml93401 rcop_no_usage, 14412677Sml93401 rcop_no_set, 14422677Sml93401 zone_shmmni_test 14432677Sml93401 }; 14442677Sml93401 14452677Sml93401 /*ARGSUSED*/ 14462677Sml93401 static int 14472677Sml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 14482677Sml93401 rctl_qty_t incr, uint_t flags) 14492677Sml93401 { 14502677Sml93401 rctl_qty_t v; 14512677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 14522677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 14532677Sml93401 v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr; 14542677Sml93401 if (v > rval->rcv_value) 14552677Sml93401 return (1); 14562677Sml93401 return (0); 14572677Sml93401 } 14582677Sml93401 14592677Sml93401 static rctl_ops_t zone_semmni_ops = { 14602677Sml93401 rcop_no_action, 14612677Sml93401 rcop_no_usage, 14622677Sml93401 rcop_no_set, 14632677Sml93401 zone_semmni_test 14642677Sml93401 }; 14652677Sml93401 14662677Sml93401 /*ARGSUSED*/ 14672677Sml93401 static int 14682677Sml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 14692677Sml93401 rctl_qty_t incr, uint_t flags) 14702677Sml93401 { 14712677Sml93401 rctl_qty_t v; 14722677Sml93401 ASSERT(MUTEX_HELD(&p->p_lock)); 14732677Sml93401 ASSERT(e->rcep_t == RCENTITY_ZONE); 14742677Sml93401 v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr; 14752677Sml93401 if (v > rval->rcv_value) 14762677Sml93401 return (1); 14772677Sml93401 return (0); 14782677Sml93401 } 14792677Sml93401 14802677Sml93401 static rctl_ops_t zone_msgmni_ops = { 14812677Sml93401 rcop_no_action, 14822677Sml93401 rcop_no_usage, 14832677Sml93401 rcop_no_set, 14842677Sml93401 zone_msgmni_test 14852677Sml93401 }; 14862677Sml93401 14872768Ssl108498 /*ARGSUSED*/ 14882768Ssl108498 static rctl_qty_t 14892768Ssl108498 zone_locked_mem_usage(rctl_t *rctl, struct proc *p) 14902768Ssl108498 { 14912768Ssl108498 rctl_qty_t q; 14922768Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 14933247Sgjelinek mutex_enter(&p->p_zone->zone_mem_lock); 14942768Ssl108498 q = p->p_zone->zone_locked_mem; 14953247Sgjelinek mutex_exit(&p->p_zone->zone_mem_lock); 14962768Ssl108498 return (q); 14972768Ssl108498 } 14982768Ssl108498 14992768Ssl108498 /*ARGSUSED*/ 15002768Ssl108498 static int 15012768Ssl108498 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 15022768Ssl108498 rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 15032768Ssl108498 { 15042768Ssl108498 rctl_qty_t q; 15053247Sgjelinek zone_t *z; 15063247Sgjelinek 15073247Sgjelinek z = e->rcep_p.zone; 15082768Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 15093247Sgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 15103247Sgjelinek q = z->zone_locked_mem; 15112768Ssl108498 if (q + incr > rcntl->rcv_value) 15122768Ssl108498 return (1); 15132768Ssl108498 return (0); 15142768Ssl108498 } 15152768Ssl108498 15162768Ssl108498 /*ARGSUSED*/ 15172768Ssl108498 static int 15182768Ssl108498 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 15192768Ssl108498 rctl_qty_t nv) 15202768Ssl108498 { 15212768Ssl108498 ASSERT(MUTEX_HELD(&p->p_lock)); 15222768Ssl108498 ASSERT(e->rcep_t == RCENTITY_ZONE); 15232768Ssl108498 if (e->rcep_p.zone == NULL) 15242768Ssl108498 return (0); 15252768Ssl108498 e->rcep_p.zone->zone_locked_mem_ctl = nv; 15262768Ssl108498 return (0); 15272768Ssl108498 } 15282768Ssl108498 15292768Ssl108498 static rctl_ops_t zone_locked_mem_ops = { 15302768Ssl108498 rcop_no_action, 15312768Ssl108498 zone_locked_mem_usage, 15322768Ssl108498 zone_locked_mem_set, 15332768Ssl108498 zone_locked_mem_test 15342768Ssl108498 }; 15352677Sml93401 15363247Sgjelinek /*ARGSUSED*/ 15373247Sgjelinek static rctl_qty_t 15383247Sgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p) 15393247Sgjelinek { 15403247Sgjelinek rctl_qty_t q; 15413247Sgjelinek zone_t *z = p->p_zone; 15423247Sgjelinek 15433247Sgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 15443247Sgjelinek mutex_enter(&z->zone_mem_lock); 15453247Sgjelinek q = z->zone_max_swap; 15463247Sgjelinek mutex_exit(&z->zone_mem_lock); 15473247Sgjelinek return (q); 15483247Sgjelinek } 15493247Sgjelinek 15503247Sgjelinek /*ARGSUSED*/ 15513247Sgjelinek static int 15523247Sgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 15533247Sgjelinek rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 15543247Sgjelinek { 15553247Sgjelinek rctl_qty_t q; 15563247Sgjelinek zone_t *z; 15573247Sgjelinek 15583247Sgjelinek z = e->rcep_p.zone; 15593247Sgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 15603247Sgjelinek ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 15613247Sgjelinek q = z->zone_max_swap; 15623247Sgjelinek if (q + incr > rcntl->rcv_value) 15633247Sgjelinek return (1); 15643247Sgjelinek return (0); 15653247Sgjelinek } 15663247Sgjelinek 15673247Sgjelinek /*ARGSUSED*/ 15683247Sgjelinek static int 15693247Sgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 15703247Sgjelinek rctl_qty_t nv) 15713247Sgjelinek { 15723247Sgjelinek ASSERT(MUTEX_HELD(&p->p_lock)); 15733247Sgjelinek ASSERT(e->rcep_t == RCENTITY_ZONE); 15743247Sgjelinek if (e->rcep_p.zone == NULL) 15753247Sgjelinek return (0); 15763247Sgjelinek e->rcep_p.zone->zone_max_swap_ctl = nv; 15773247Sgjelinek return (0); 15783247Sgjelinek } 15793247Sgjelinek 15803247Sgjelinek static rctl_ops_t zone_max_swap_ops = { 15813247Sgjelinek rcop_no_action, 15823247Sgjelinek zone_max_swap_usage, 15833247Sgjelinek zone_max_swap_set, 15843247Sgjelinek zone_max_swap_test 15853247Sgjelinek }; 15863247Sgjelinek 15870Sstevel@tonic-gate /* 15880Sstevel@tonic-gate * Helper function to brand the zone with a unique ID. 15890Sstevel@tonic-gate */ 15900Sstevel@tonic-gate static void 15910Sstevel@tonic-gate zone_uniqid(zone_t *zone) 15920Sstevel@tonic-gate { 15930Sstevel@tonic-gate static uint64_t uniqid = 0; 15940Sstevel@tonic-gate 15950Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 15960Sstevel@tonic-gate zone->zone_uniqid = uniqid++; 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate 15990Sstevel@tonic-gate /* 16000Sstevel@tonic-gate * Returns a held pointer to the "kcred" for the specified zone. 16010Sstevel@tonic-gate */ 16020Sstevel@tonic-gate struct cred * 16030Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid) 16040Sstevel@tonic-gate { 16050Sstevel@tonic-gate zone_t *zone; 16060Sstevel@tonic-gate cred_t *cr; 16070Sstevel@tonic-gate 16080Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) 16090Sstevel@tonic-gate return (NULL); 16100Sstevel@tonic-gate cr = zone->zone_kcred; 16110Sstevel@tonic-gate crhold(cr); 16120Sstevel@tonic-gate zone_rele(zone); 16130Sstevel@tonic-gate return (cr); 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate 16163247Sgjelinek static int 16173247Sgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw) 16183247Sgjelinek { 16193247Sgjelinek zone_t *zone = ksp->ks_private; 16203247Sgjelinek zone_kstat_t *zk = ksp->ks_data; 16213247Sgjelinek 16223247Sgjelinek if (rw == KSTAT_WRITE) 16233247Sgjelinek return (EACCES); 16243247Sgjelinek 16253247Sgjelinek zk->zk_usage.value.ui64 = zone->zone_locked_mem; 16263247Sgjelinek zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl; 16273247Sgjelinek return (0); 16283247Sgjelinek } 16293247Sgjelinek 16303247Sgjelinek static int 16313247Sgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw) 16323247Sgjelinek { 16333247Sgjelinek zone_t *zone = ksp->ks_private; 16343247Sgjelinek zone_kstat_t *zk = ksp->ks_data; 16353247Sgjelinek 16363247Sgjelinek if (rw == KSTAT_WRITE) 16373247Sgjelinek return (EACCES); 16383247Sgjelinek 16393247Sgjelinek zk->zk_usage.value.ui64 = zone->zone_max_swap; 16403247Sgjelinek zk->zk_value.value.ui64 = zone->zone_max_swap_ctl; 16413247Sgjelinek return (0); 16423247Sgjelinek } 16433247Sgjelinek 16443247Sgjelinek static void 16453247Sgjelinek zone_kstat_create(zone_t *zone) 16463247Sgjelinek { 16473247Sgjelinek kstat_t *ksp; 16483247Sgjelinek zone_kstat_t *zk; 16493247Sgjelinek 16503247Sgjelinek ksp = rctl_kstat_create_zone(zone, "lockedmem", KSTAT_TYPE_NAMED, 16513247Sgjelinek sizeof (zone_kstat_t) / sizeof (kstat_named_t), 16523247Sgjelinek KSTAT_FLAG_VIRTUAL); 16533247Sgjelinek 16543247Sgjelinek if (ksp == NULL) 16553247Sgjelinek return; 16563247Sgjelinek 16573247Sgjelinek zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 16583247Sgjelinek ksp->ks_data_size += strlen(zone->zone_name) + 1; 16593247Sgjelinek kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 16603247Sgjelinek kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 16613247Sgjelinek kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 16623247Sgjelinek kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 16633247Sgjelinek ksp->ks_update = zone_lockedmem_kstat_update; 16643247Sgjelinek ksp->ks_private = zone; 16653247Sgjelinek kstat_install(ksp); 16663247Sgjelinek 16673247Sgjelinek zone->zone_lockedmem_kstat = ksp; 16683247Sgjelinek 16693247Sgjelinek ksp = rctl_kstat_create_zone(zone, "swapresv", KSTAT_TYPE_NAMED, 16703247Sgjelinek sizeof (zone_kstat_t) / sizeof (kstat_named_t), 16713247Sgjelinek KSTAT_FLAG_VIRTUAL); 16723247Sgjelinek 16733247Sgjelinek if (ksp == NULL) 16743247Sgjelinek return; 16753247Sgjelinek 16763247Sgjelinek zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 16773247Sgjelinek ksp->ks_data_size += strlen(zone->zone_name) + 1; 16783247Sgjelinek kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 16793247Sgjelinek kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 16803247Sgjelinek kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 16813247Sgjelinek kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 16823247Sgjelinek ksp->ks_update = zone_swapresv_kstat_update; 16833247Sgjelinek ksp->ks_private = zone; 16843247Sgjelinek kstat_install(ksp); 16853247Sgjelinek 16863247Sgjelinek zone->zone_swapresv_kstat = ksp; 16873247Sgjelinek } 16883247Sgjelinek 16893247Sgjelinek static void 16903247Sgjelinek zone_kstat_delete(zone_t *zone) 16913247Sgjelinek { 16923247Sgjelinek void *data; 16933247Sgjelinek 16943247Sgjelinek if (zone->zone_lockedmem_kstat != NULL) { 16953247Sgjelinek data = zone->zone_lockedmem_kstat->ks_data; 16963247Sgjelinek kstat_delete(zone->zone_lockedmem_kstat); 16973247Sgjelinek kmem_free(data, sizeof (zone_kstat_t)); 16983247Sgjelinek } 16993247Sgjelinek if (zone->zone_swapresv_kstat != NULL) { 17003247Sgjelinek data = zone->zone_swapresv_kstat->ks_data; 17013247Sgjelinek kstat_delete(zone->zone_swapresv_kstat); 17023247Sgjelinek kmem_free(data, sizeof (zone_kstat_t)); 17033247Sgjelinek } 17043247Sgjelinek } 17053247Sgjelinek 17060Sstevel@tonic-gate /* 17070Sstevel@tonic-gate * Called very early on in boot to initialize the ZSD list so that 17080Sstevel@tonic-gate * zone_key_create() can be called before zone_init(). It also initializes 17090Sstevel@tonic-gate * portions of zone0 which may be used before zone_init() is called. The 17100Sstevel@tonic-gate * variable "global_zone" will be set when zone0 is fully initialized by 17110Sstevel@tonic-gate * zone_init(). 17120Sstevel@tonic-gate */ 17130Sstevel@tonic-gate void 17140Sstevel@tonic-gate zone_zsd_init(void) 17150Sstevel@tonic-gate { 17160Sstevel@tonic-gate mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 17170Sstevel@tonic-gate mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 17180Sstevel@tonic-gate list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 17190Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 17200Sstevel@tonic-gate list_create(&zone_active, sizeof (zone_t), 17210Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 17220Sstevel@tonic-gate list_create(&zone_deathrow, sizeof (zone_t), 17230Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 17240Sstevel@tonic-gate 17250Sstevel@tonic-gate mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 17260Sstevel@tonic-gate mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 17273247Sgjelinek mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 17280Sstevel@tonic-gate zone0.zone_shares = 1; 17293247Sgjelinek zone0.zone_nlwps = 0; 17300Sstevel@tonic-gate zone0.zone_nlwps_ctl = INT_MAX; 17313247Sgjelinek zone0.zone_locked_mem = 0; 17323247Sgjelinek zone0.zone_locked_mem_ctl = UINT64_MAX; 17333247Sgjelinek ASSERT(zone0.zone_max_swap == 0); 17343247Sgjelinek zone0.zone_max_swap_ctl = UINT64_MAX; 17352677Sml93401 zone0.zone_shmmax = 0; 17362677Sml93401 zone0.zone_ipc.ipcq_shmmni = 0; 17372677Sml93401 zone0.zone_ipc.ipcq_semmni = 0; 17382677Sml93401 zone0.zone_ipc.ipcq_msgmni = 0; 17390Sstevel@tonic-gate zone0.zone_name = GLOBAL_ZONENAME; 17400Sstevel@tonic-gate zone0.zone_nodename = utsname.nodename; 17410Sstevel@tonic-gate zone0.zone_domain = srpc_domain; 17428662SJordan.Vaughan@Sun.com zone0.zone_hostid = HW_INVALID_HOSTID; 17430Sstevel@tonic-gate zone0.zone_ref = 1; 17440Sstevel@tonic-gate zone0.zone_id = GLOBAL_ZONEID; 17450Sstevel@tonic-gate zone0.zone_status = ZONE_IS_RUNNING; 17460Sstevel@tonic-gate zone0.zone_rootpath = "/"; 17470Sstevel@tonic-gate zone0.zone_rootpathlen = 2; 17480Sstevel@tonic-gate zone0.zone_psetid = ZONE_PS_INVAL; 17490Sstevel@tonic-gate zone0.zone_ncpus = 0; 17500Sstevel@tonic-gate zone0.zone_ncpus_online = 0; 17510Sstevel@tonic-gate zone0.zone_proc_initpid = 1; 17522267Sdp zone0.zone_initname = initname; 17533247Sgjelinek zone0.zone_lockedmem_kstat = NULL; 17543247Sgjelinek zone0.zone_swapresv_kstat = NULL; 17550Sstevel@tonic-gate list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 17560Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 17570Sstevel@tonic-gate list_insert_head(&zone_active, &zone0); 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate /* 17600Sstevel@tonic-gate * The root filesystem is not mounted yet, so zone_rootvp cannot be set 17610Sstevel@tonic-gate * to anything meaningful. It is assigned to be 'rootdir' in 17620Sstevel@tonic-gate * vfs_mountroot(). 17630Sstevel@tonic-gate */ 17640Sstevel@tonic-gate zone0.zone_rootvp = NULL; 17650Sstevel@tonic-gate zone0.zone_vfslist = NULL; 17662267Sdp zone0.zone_bootargs = initargs; 17670Sstevel@tonic-gate zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 17680Sstevel@tonic-gate /* 17690Sstevel@tonic-gate * The global zone has all privileges 17700Sstevel@tonic-gate */ 17710Sstevel@tonic-gate priv_fillset(zone0.zone_privset); 17720Sstevel@tonic-gate /* 17730Sstevel@tonic-gate * Add p0 to the global zone 17740Sstevel@tonic-gate */ 17750Sstevel@tonic-gate zone0.zone_zsched = &p0; 17760Sstevel@tonic-gate p0.p_zone = &zone0; 17770Sstevel@tonic-gate } 17780Sstevel@tonic-gate 17790Sstevel@tonic-gate /* 17801676Sjpk * Compute a hash value based on the contents of the label and the DOI. The 17811676Sjpk * hash algorithm is somewhat arbitrary, but is based on the observation that 17821676Sjpk * humans will likely pick labels that differ by amounts that work out to be 17831676Sjpk * multiples of the number of hash chains, and thus stirring in some primes 17841676Sjpk * should help. 17851676Sjpk */ 17861676Sjpk static uint_t 17871676Sjpk hash_bylabel(void *hdata, mod_hash_key_t key) 17881676Sjpk { 17891676Sjpk const ts_label_t *lab = (ts_label_t *)key; 17901676Sjpk const uint32_t *up, *ue; 17911676Sjpk uint_t hash; 17921676Sjpk int i; 17931676Sjpk 17941676Sjpk _NOTE(ARGUNUSED(hdata)); 17951676Sjpk 17961676Sjpk hash = lab->tsl_doi + (lab->tsl_doi << 1); 17971676Sjpk /* we depend on alignment of label, but not representation */ 17981676Sjpk up = (const uint32_t *)&lab->tsl_label; 17991676Sjpk ue = up + sizeof (lab->tsl_label) / sizeof (*up); 18001676Sjpk i = 1; 18011676Sjpk while (up < ue) { 18021676Sjpk /* using 2^n + 1, 1 <= n <= 16 as source of many primes */ 18031676Sjpk hash += *up + (*up << ((i % 16) + 1)); 18041676Sjpk up++; 18051676Sjpk i++; 18061676Sjpk } 18071676Sjpk return (hash); 18081676Sjpk } 18091676Sjpk 18101676Sjpk /* 18111676Sjpk * All that mod_hash cares about here is zero (equal) versus non-zero (not 18121676Sjpk * equal). This may need to be changed if less than / greater than is ever 18131676Sjpk * needed. 18141676Sjpk */ 18151676Sjpk static int 18161676Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 18171676Sjpk { 18181676Sjpk ts_label_t *lab1 = (ts_label_t *)key1; 18191676Sjpk ts_label_t *lab2 = (ts_label_t *)key2; 18201676Sjpk 18211676Sjpk return (label_equal(lab1, lab2) ? 0 : 1); 18221676Sjpk } 18231676Sjpk 18241676Sjpk /* 18250Sstevel@tonic-gate * Called by main() to initialize the zones framework. 18260Sstevel@tonic-gate */ 18270Sstevel@tonic-gate void 18280Sstevel@tonic-gate zone_init(void) 18290Sstevel@tonic-gate { 18300Sstevel@tonic-gate rctl_dict_entry_t *rde; 18310Sstevel@tonic-gate rctl_val_t *dval; 18320Sstevel@tonic-gate rctl_set_t *set; 18330Sstevel@tonic-gate rctl_alloc_gp_t *gp; 18340Sstevel@tonic-gate rctl_entity_p_t e; 18351166Sdstaff int res; 18360Sstevel@tonic-gate 18370Sstevel@tonic-gate ASSERT(curproc == &p0); 18380Sstevel@tonic-gate 18390Sstevel@tonic-gate /* 18400Sstevel@tonic-gate * Create ID space for zone IDs. ID 0 is reserved for the 18410Sstevel@tonic-gate * global zone. 18420Sstevel@tonic-gate */ 18430Sstevel@tonic-gate zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 18440Sstevel@tonic-gate 18450Sstevel@tonic-gate /* 18460Sstevel@tonic-gate * Initialize generic zone resource controls, if any. 18470Sstevel@tonic-gate */ 18480Sstevel@tonic-gate rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 18490Sstevel@tonic-gate RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 18501996Sml93401 RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER, 18513792Sakolb FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops); 18523792Sakolb 18533792Sakolb rc_zone_cpu_cap = rctl_register("zone.cpu-cap", 18543792Sakolb RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS | 18553792Sakolb RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER | 18563792Sakolb RCTL_GLOBAL_INFINITE, 18573792Sakolb MAXCAP, MAXCAP, &zone_cpu_cap_ops); 18580Sstevel@tonic-gate 18590Sstevel@tonic-gate rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 18600Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 18610Sstevel@tonic-gate INT_MAX, INT_MAX, &zone_lwps_ops); 18620Sstevel@tonic-gate /* 18632677Sml93401 * System V IPC resource controls 18642677Sml93401 */ 18652677Sml93401 rc_zone_msgmni = rctl_register("zone.max-msg-ids", 18662677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 18672677Sml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops); 18682677Sml93401 18692677Sml93401 rc_zone_semmni = rctl_register("zone.max-sem-ids", 18702677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 18712677Sml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops); 18722677Sml93401 18732677Sml93401 rc_zone_shmmni = rctl_register("zone.max-shm-ids", 18742677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 18752677Sml93401 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops); 18762677Sml93401 18772677Sml93401 rc_zone_shmmax = rctl_register("zone.max-shm-memory", 18782677Sml93401 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 18792677Sml93401 RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops); 18802677Sml93401 18812677Sml93401 /* 18820Sstevel@tonic-gate * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 18830Sstevel@tonic-gate * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 18840Sstevel@tonic-gate */ 18850Sstevel@tonic-gate dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 18860Sstevel@tonic-gate bzero(dval, sizeof (rctl_val_t)); 18870Sstevel@tonic-gate dval->rcv_value = 1; 18880Sstevel@tonic-gate dval->rcv_privilege = RCPRIV_PRIVILEGED; 18890Sstevel@tonic-gate dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 18900Sstevel@tonic-gate dval->rcv_action_recip_pid = -1; 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate rde = rctl_dict_lookup("zone.cpu-shares"); 18930Sstevel@tonic-gate (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 18940Sstevel@tonic-gate 18952768Ssl108498 rc_zone_locked_mem = rctl_register("zone.max-locked-memory", 18962768Ssl108498 RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 18972768Ssl108498 RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 18982768Ssl108498 &zone_locked_mem_ops); 18993247Sgjelinek 19003247Sgjelinek rc_zone_max_swap = rctl_register("zone.max-swap", 19013247Sgjelinek RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 19023247Sgjelinek RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 19033247Sgjelinek &zone_max_swap_ops); 19043247Sgjelinek 19050Sstevel@tonic-gate /* 19060Sstevel@tonic-gate * Initialize the ``global zone''. 19070Sstevel@tonic-gate */ 19080Sstevel@tonic-gate set = rctl_set_create(); 19090Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 19100Sstevel@tonic-gate mutex_enter(&p0.p_lock); 19110Sstevel@tonic-gate e.rcep_p.zone = &zone0; 19120Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 19130Sstevel@tonic-gate zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 19140Sstevel@tonic-gate gp); 19150Sstevel@tonic-gate 19160Sstevel@tonic-gate zone0.zone_nlwps = p0.p_lwpcnt; 19170Sstevel@tonic-gate zone0.zone_ntasks = 1; 19180Sstevel@tonic-gate mutex_exit(&p0.p_lock); 19192712Snn35248 zone0.zone_restart_init = B_TRUE; 19202712Snn35248 zone0.zone_brand = &native_brand; 19210Sstevel@tonic-gate rctl_prealloc_destroy(gp); 19220Sstevel@tonic-gate /* 19233247Sgjelinek * pool_default hasn't been initialized yet, so we let pool_init() 19243247Sgjelinek * take care of making sure the global zone is in the default pool. 19250Sstevel@tonic-gate */ 19261676Sjpk 19271676Sjpk /* 19283247Sgjelinek * Initialize global zone kstats 19293247Sgjelinek */ 19303247Sgjelinek zone_kstat_create(&zone0); 19313247Sgjelinek 19323247Sgjelinek /* 19331676Sjpk * Initialize zone label. 19341676Sjpk * mlp are initialized when tnzonecfg is loaded. 19351676Sjpk */ 19361676Sjpk zone0.zone_slabel = l_admin_low; 19371676Sjpk rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 19381676Sjpk label_hold(l_admin_low); 19391676Sjpk 194010910SRobert.Harris@Sun.COM /* 194110910SRobert.Harris@Sun.COM * Initialise the lock for the database structure used by mntfs. 194210910SRobert.Harris@Sun.COM */ 194310910SRobert.Harris@Sun.COM rw_init(&zone0.zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL); 194410910SRobert.Harris@Sun.COM 19450Sstevel@tonic-gate mutex_enter(&zonehash_lock); 19460Sstevel@tonic-gate zone_uniqid(&zone0); 19470Sstevel@tonic-gate ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 19481676Sjpk 19490Sstevel@tonic-gate zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 19500Sstevel@tonic-gate mod_hash_null_valdtor); 19510Sstevel@tonic-gate zonehashbyname = mod_hash_create_strhash("zone_by_name", 19520Sstevel@tonic-gate zone_hash_size, mod_hash_null_valdtor); 19531676Sjpk /* 19541676Sjpk * maintain zonehashbylabel only for labeled systems 19551676Sjpk */ 19561676Sjpk if (is_system_labeled()) 19571676Sjpk zonehashbylabel = mod_hash_create_extended("zone_by_label", 19581676Sjpk zone_hash_size, mod_hash_null_keydtor, 19591676Sjpk mod_hash_null_valdtor, hash_bylabel, NULL, 19601676Sjpk hash_labelkey_cmp, KM_SLEEP); 19610Sstevel@tonic-gate zonecount = 1; 19620Sstevel@tonic-gate 19630Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 19640Sstevel@tonic-gate (mod_hash_val_t)&zone0); 19650Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 19660Sstevel@tonic-gate (mod_hash_val_t)&zone0); 19671769Scarlsonj if (is_system_labeled()) { 19681769Scarlsonj zone0.zone_flags |= ZF_HASHED_LABEL; 19691676Sjpk (void) mod_hash_insert(zonehashbylabel, 19701676Sjpk (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0); 19711769Scarlsonj } 19721676Sjpk mutex_exit(&zonehash_lock); 19731676Sjpk 19740Sstevel@tonic-gate /* 19750Sstevel@tonic-gate * We avoid setting zone_kcred until now, since kcred is initialized 19760Sstevel@tonic-gate * sometime after zone_zsd_init() and before zone_init(). 19770Sstevel@tonic-gate */ 19780Sstevel@tonic-gate zone0.zone_kcred = kcred; 19790Sstevel@tonic-gate /* 19800Sstevel@tonic-gate * The global zone is fully initialized (except for zone_rootvp which 19810Sstevel@tonic-gate * will be set when the root filesystem is mounted). 19820Sstevel@tonic-gate */ 19830Sstevel@tonic-gate global_zone = &zone0; 19841166Sdstaff 19851166Sdstaff /* 19861166Sdstaff * Setup an event channel to send zone status change notifications on 19871166Sdstaff */ 19881166Sdstaff res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan, 19891166Sdstaff EVCH_CREAT); 19901166Sdstaff 19911166Sdstaff if (res) 19921166Sdstaff panic("Sysevent_evc_bind failed during zone setup.\n"); 19933247Sgjelinek 19940Sstevel@tonic-gate } 19950Sstevel@tonic-gate 19960Sstevel@tonic-gate static void 19970Sstevel@tonic-gate zone_free(zone_t *zone) 19980Sstevel@tonic-gate { 19990Sstevel@tonic-gate ASSERT(zone != global_zone); 20000Sstevel@tonic-gate ASSERT(zone->zone_ntasks == 0); 20010Sstevel@tonic-gate ASSERT(zone->zone_nlwps == 0); 20020Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 0); 20030Sstevel@tonic-gate ASSERT(zone->zone_kcred == NULL); 20040Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 20050Sstevel@tonic-gate zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 20060Sstevel@tonic-gate 20073792Sakolb /* 20083792Sakolb * Remove any zone caps. 20093792Sakolb */ 20103792Sakolb cpucaps_zone_remove(zone); 20113792Sakolb 20123792Sakolb ASSERT(zone->zone_cpucap == NULL); 20133792Sakolb 20140Sstevel@tonic-gate /* remove from deathrow list */ 20150Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_DEAD) { 20160Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 20170Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 20180Sstevel@tonic-gate list_remove(&zone_deathrow, zone); 20190Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 20200Sstevel@tonic-gate } 20210Sstevel@tonic-gate 20220Sstevel@tonic-gate zone_free_zsd(zone); 2023789Sahrens zone_free_datasets(zone); 202410616SSebastien.Roy@Sun.COM list_destroy(&zone->zone_dl_list); 20250Sstevel@tonic-gate 20260Sstevel@tonic-gate if (zone->zone_rootvp != NULL) 20270Sstevel@tonic-gate VN_RELE(zone->zone_rootvp); 20280Sstevel@tonic-gate if (zone->zone_rootpath) 20290Sstevel@tonic-gate kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 20300Sstevel@tonic-gate if (zone->zone_name != NULL) 20310Sstevel@tonic-gate kmem_free(zone->zone_name, ZONENAME_MAX); 20321676Sjpk if (zone->zone_slabel != NULL) 20331676Sjpk label_rele(zone->zone_slabel); 20340Sstevel@tonic-gate if (zone->zone_nodename != NULL) 20350Sstevel@tonic-gate kmem_free(zone->zone_nodename, _SYS_NMLN); 20360Sstevel@tonic-gate if (zone->zone_domain != NULL) 20370Sstevel@tonic-gate kmem_free(zone->zone_domain, _SYS_NMLN); 20380Sstevel@tonic-gate if (zone->zone_privset != NULL) 20390Sstevel@tonic-gate kmem_free(zone->zone_privset, sizeof (priv_set_t)); 20400Sstevel@tonic-gate if (zone->zone_rctls != NULL) 20410Sstevel@tonic-gate rctl_set_free(zone->zone_rctls); 20420Sstevel@tonic-gate if (zone->zone_bootargs != NULL) 20432267Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 20442267Sdp if (zone->zone_initname != NULL) 20452267Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 20460Sstevel@tonic-gate id_free(zoneid_space, zone->zone_id); 20470Sstevel@tonic-gate mutex_destroy(&zone->zone_lock); 20480Sstevel@tonic-gate cv_destroy(&zone->zone_cv); 20491676Sjpk rw_destroy(&zone->zone_mlps.mlpl_rwlock); 205010910SRobert.Harris@Sun.COM rw_destroy(&zone->zone_mntfs_db_lock); 20510Sstevel@tonic-gate kmem_free(zone, sizeof (zone_t)); 20520Sstevel@tonic-gate } 20530Sstevel@tonic-gate 20540Sstevel@tonic-gate /* 20550Sstevel@tonic-gate * See block comment at the top of this file for information about zone 20560Sstevel@tonic-gate * status values. 20570Sstevel@tonic-gate */ 20580Sstevel@tonic-gate /* 20590Sstevel@tonic-gate * Convenience function for setting zone status. 20600Sstevel@tonic-gate */ 20610Sstevel@tonic-gate static void 20620Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status) 20630Sstevel@tonic-gate { 20641166Sdstaff 20651166Sdstaff nvlist_t *nvl = NULL; 20660Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zone_status_lock)); 20670Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 20680Sstevel@tonic-gate status >= zone_status_get(zone)); 20691166Sdstaff 20701166Sdstaff if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) || 20711166Sdstaff nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) || 20721166Sdstaff nvlist_add_string(nvl, ZONE_CB_NEWSTATE, 20732267Sdp zone_status_table[status]) || 20741166Sdstaff nvlist_add_string(nvl, ZONE_CB_OLDSTATE, 20752267Sdp zone_status_table[zone->zone_status]) || 20761166Sdstaff nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) || 20771166Sdstaff nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) || 20781166Sdstaff sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS, 20792267Sdp ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) { 20801166Sdstaff #ifdef DEBUG 20811166Sdstaff (void) printf( 20821166Sdstaff "Failed to allocate and send zone state change event.\n"); 20831166Sdstaff #endif 20841166Sdstaff } 20851166Sdstaff nvlist_free(nvl); 20861166Sdstaff 20870Sstevel@tonic-gate zone->zone_status = status; 20881166Sdstaff 20890Sstevel@tonic-gate cv_broadcast(&zone->zone_cv); 20900Sstevel@tonic-gate } 20910Sstevel@tonic-gate 20920Sstevel@tonic-gate /* 20930Sstevel@tonic-gate * Public function to retrieve the zone status. The zone status may 20940Sstevel@tonic-gate * change after it is retrieved. 20950Sstevel@tonic-gate */ 20960Sstevel@tonic-gate zone_status_t 20970Sstevel@tonic-gate zone_status_get(zone_t *zone) 20980Sstevel@tonic-gate { 20990Sstevel@tonic-gate return (zone->zone_status); 21000Sstevel@tonic-gate } 21010Sstevel@tonic-gate 21020Sstevel@tonic-gate static int 21030Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 21040Sstevel@tonic-gate { 21052267Sdp char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP); 21062267Sdp int err = 0; 21072267Sdp 21082267Sdp ASSERT(zone != global_zone); 21092267Sdp if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0) 21102267Sdp goto done; /* EFAULT or ENAMETOOLONG */ 21112267Sdp 21122267Sdp if (zone->zone_bootargs != NULL) 21132267Sdp kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 21142267Sdp 21152267Sdp zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP); 21162267Sdp (void) strcpy(zone->zone_bootargs, bootargs); 21172267Sdp 21182267Sdp done: 21192267Sdp kmem_free(bootargs, BOOTARGS_MAX); 21202267Sdp return (err); 21212267Sdp } 21222267Sdp 21232267Sdp static int 21244141Sedp zone_set_brand(zone_t *zone, const char *brand) 21254141Sedp { 21264141Sedp struct brand_attr *attrp; 21274141Sedp brand_t *bp; 21284141Sedp 21294141Sedp attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP); 21304141Sedp if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) { 21314141Sedp kmem_free(attrp, sizeof (struct brand_attr)); 21324141Sedp return (EFAULT); 21334141Sedp } 21344141Sedp 21354141Sedp bp = brand_register_zone(attrp); 21364141Sedp kmem_free(attrp, sizeof (struct brand_attr)); 21374141Sedp if (bp == NULL) 21384141Sedp return (EINVAL); 21394141Sedp 21404141Sedp /* 21414141Sedp * This is the only place where a zone can change it's brand. 21424141Sedp * We already need to hold zone_status_lock to check the zone 21434141Sedp * status, so we'll just use that lock to serialize zone 21444141Sedp * branding requests as well. 21454141Sedp */ 21464141Sedp mutex_enter(&zone_status_lock); 21474141Sedp 21484141Sedp /* Re-Branding is not allowed and the zone can't be booted yet */ 21494141Sedp if ((ZONE_IS_BRANDED(zone)) || 21504141Sedp (zone_status_get(zone) >= ZONE_IS_BOOTING)) { 21514141Sedp mutex_exit(&zone_status_lock); 21524141Sedp brand_unregister_zone(bp); 21534141Sedp return (EINVAL); 21544141Sedp } 21554141Sedp 21564888Seh208807 /* set up the brand specific data */ 21574141Sedp zone->zone_brand = bp; 21584888Seh208807 ZBROP(zone)->b_init_brand_data(zone); 21594888Seh208807 21604141Sedp mutex_exit(&zone_status_lock); 21614141Sedp return (0); 21624141Sedp } 21634141Sedp 21644141Sedp static int 21652267Sdp zone_set_initname(zone_t *zone, const char *zone_initname) 21662267Sdp { 21672267Sdp char initname[INITNAME_SZ]; 21680Sstevel@tonic-gate size_t len; 21692267Sdp int err = 0; 21702267Sdp 21712267Sdp ASSERT(zone != global_zone); 21722267Sdp if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0) 21730Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 21742267Sdp 21752267Sdp if (zone->zone_initname != NULL) 21762267Sdp kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 21772267Sdp 21782267Sdp zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP); 21792267Sdp (void) strcpy(zone->zone_initname, initname); 21800Sstevel@tonic-gate return (0); 21810Sstevel@tonic-gate } 21820Sstevel@tonic-gate 21833247Sgjelinek static int 21843247Sgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap) 21853247Sgjelinek { 21863247Sgjelinek uint64_t mcap; 21873247Sgjelinek int err = 0; 21883247Sgjelinek 21893247Sgjelinek if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0) 21903247Sgjelinek zone->zone_phys_mcap = mcap; 21913247Sgjelinek 21923247Sgjelinek return (err); 21933247Sgjelinek } 21943247Sgjelinek 21953247Sgjelinek static int 21963247Sgjelinek zone_set_sched_class(zone_t *zone, const char *new_class) 21973247Sgjelinek { 21983247Sgjelinek char sched_class[PC_CLNMSZ]; 21993247Sgjelinek id_t classid; 22003247Sgjelinek int err; 22013247Sgjelinek 22023247Sgjelinek ASSERT(zone != global_zone); 22033247Sgjelinek if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0) 22043247Sgjelinek return (err); /* EFAULT or ENAMETOOLONG */ 22053247Sgjelinek 2206*11173SJonathan.Adams@Sun.COM if (getcid(sched_class, &classid) != 0 || CLASS_KERNEL(classid)) 22073247Sgjelinek return (set_errno(EINVAL)); 22083247Sgjelinek zone->zone_defaultcid = classid; 22093247Sgjelinek ASSERT(zone->zone_defaultcid > 0 && 22103247Sgjelinek zone->zone_defaultcid < loaded_classes); 22113247Sgjelinek 22123247Sgjelinek return (0); 22133247Sgjelinek } 22143247Sgjelinek 22150Sstevel@tonic-gate /* 22160Sstevel@tonic-gate * Block indefinitely waiting for (zone_status >= status) 22170Sstevel@tonic-gate */ 22180Sstevel@tonic-gate void 22190Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status) 22200Sstevel@tonic-gate { 22210Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 22220Sstevel@tonic-gate 22230Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22240Sstevel@tonic-gate while (zone->zone_status < status) { 22250Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 22260Sstevel@tonic-gate } 22270Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22280Sstevel@tonic-gate } 22290Sstevel@tonic-gate 22300Sstevel@tonic-gate /* 22310Sstevel@tonic-gate * Private CPR-safe version of zone_status_wait(). 22320Sstevel@tonic-gate */ 22330Sstevel@tonic-gate static void 22340Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 22350Sstevel@tonic-gate { 22360Sstevel@tonic-gate callb_cpr_t cprinfo; 22370Sstevel@tonic-gate 22380Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 22390Sstevel@tonic-gate 22400Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 22410Sstevel@tonic-gate str); 22420Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22430Sstevel@tonic-gate while (zone->zone_status < status) { 22440Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 22450Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 22460Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 22470Sstevel@tonic-gate } 22480Sstevel@tonic-gate /* 22490Sstevel@tonic-gate * zone_status_lock is implicitly released by the following. 22500Sstevel@tonic-gate */ 22510Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 22520Sstevel@tonic-gate } 22530Sstevel@tonic-gate 22540Sstevel@tonic-gate /* 22550Sstevel@tonic-gate * Block until zone enters requested state or signal is received. Return (0) 22560Sstevel@tonic-gate * if signaled, non-zero otherwise. 22570Sstevel@tonic-gate */ 22580Sstevel@tonic-gate int 22590Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status) 22600Sstevel@tonic-gate { 22610Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22640Sstevel@tonic-gate while (zone->zone_status < status) { 22650Sstevel@tonic-gate if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 22660Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22670Sstevel@tonic-gate return (0); 22680Sstevel@tonic-gate } 22690Sstevel@tonic-gate } 22700Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22710Sstevel@tonic-gate return (1); 22720Sstevel@tonic-gate } 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate /* 22750Sstevel@tonic-gate * Block until the zone enters the requested state or the timeout expires, 22760Sstevel@tonic-gate * whichever happens first. Return (-1) if operation timed out, time remaining 22770Sstevel@tonic-gate * otherwise. 22780Sstevel@tonic-gate */ 22790Sstevel@tonic-gate clock_t 22800Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 22810Sstevel@tonic-gate { 22820Sstevel@tonic-gate clock_t timeleft = 0; 22830Sstevel@tonic-gate 22840Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate mutex_enter(&zone_status_lock); 22870Sstevel@tonic-gate while (zone->zone_status < status && timeleft != -1) { 22880Sstevel@tonic-gate timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 22890Sstevel@tonic-gate } 22900Sstevel@tonic-gate mutex_exit(&zone_status_lock); 22910Sstevel@tonic-gate return (timeleft); 22920Sstevel@tonic-gate } 22930Sstevel@tonic-gate 22940Sstevel@tonic-gate /* 22950Sstevel@tonic-gate * Block until the zone enters the requested state, the current process is 22960Sstevel@tonic-gate * signaled, or the timeout expires, whichever happens first. Return (-1) if 22970Sstevel@tonic-gate * operation timed out, 0 if signaled, time remaining otherwise. 22980Sstevel@tonic-gate */ 22990Sstevel@tonic-gate clock_t 23000Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 23010Sstevel@tonic-gate { 230211066Srafael.vanoni@sun.com clock_t timeleft = tim - ddi_get_lbolt(); 23030Sstevel@tonic-gate 23040Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate mutex_enter(&zone_status_lock); 23070Sstevel@tonic-gate while (zone->zone_status < status) { 23080Sstevel@tonic-gate timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 23090Sstevel@tonic-gate tim); 23100Sstevel@tonic-gate if (timeleft <= 0) 23110Sstevel@tonic-gate break; 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate mutex_exit(&zone_status_lock); 23140Sstevel@tonic-gate return (timeleft); 23150Sstevel@tonic-gate } 23160Sstevel@tonic-gate 23170Sstevel@tonic-gate /* 23180Sstevel@tonic-gate * Zones have two reference counts: one for references from credential 23190Sstevel@tonic-gate * structures (zone_cred_ref), and one (zone_ref) for everything else. 23200Sstevel@tonic-gate * This is so we can allow a zone to be rebooted while there are still 23210Sstevel@tonic-gate * outstanding cred references, since certain drivers cache dblks (which 23220Sstevel@tonic-gate * implicitly results in cached creds). We wait for zone_ref to drop to 23230Sstevel@tonic-gate * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 23240Sstevel@tonic-gate * later freed when the zone_cred_ref drops to 0, though nothing other 23250Sstevel@tonic-gate * than the zone id and privilege set should be accessed once the zone 23260Sstevel@tonic-gate * is "dead". 23270Sstevel@tonic-gate * 23280Sstevel@tonic-gate * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 23290Sstevel@tonic-gate * to force halt/reboot to block waiting for the zone_cred_ref to drop 23300Sstevel@tonic-gate * to 0. This can be useful to flush out other sources of cached creds 23310Sstevel@tonic-gate * that may be less innocuous than the driver case. 23320Sstevel@tonic-gate */ 23330Sstevel@tonic-gate 23340Sstevel@tonic-gate int zone_wait_for_cred = 0; 23350Sstevel@tonic-gate 23360Sstevel@tonic-gate static void 23370Sstevel@tonic-gate zone_hold_locked(zone_t *z) 23380Sstevel@tonic-gate { 23390Sstevel@tonic-gate ASSERT(MUTEX_HELD(&z->zone_lock)); 23400Sstevel@tonic-gate z->zone_ref++; 23410Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 23420Sstevel@tonic-gate } 23430Sstevel@tonic-gate 23440Sstevel@tonic-gate void 23450Sstevel@tonic-gate zone_hold(zone_t *z) 23460Sstevel@tonic-gate { 23470Sstevel@tonic-gate mutex_enter(&z->zone_lock); 23480Sstevel@tonic-gate zone_hold_locked(z); 23490Sstevel@tonic-gate mutex_exit(&z->zone_lock); 23500Sstevel@tonic-gate } 23510Sstevel@tonic-gate 23520Sstevel@tonic-gate /* 23530Sstevel@tonic-gate * If the non-cred ref count drops to 1 and either the cred ref count 23540Sstevel@tonic-gate * is 0 or we aren't waiting for cred references, the zone is ready to 23550Sstevel@tonic-gate * be destroyed. 23560Sstevel@tonic-gate */ 23570Sstevel@tonic-gate #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 23580Sstevel@tonic-gate (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 23590Sstevel@tonic-gate 23600Sstevel@tonic-gate void 23610Sstevel@tonic-gate zone_rele(zone_t *z) 23620Sstevel@tonic-gate { 23630Sstevel@tonic-gate boolean_t wakeup; 23640Sstevel@tonic-gate 23650Sstevel@tonic-gate mutex_enter(&z->zone_lock); 23660Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 23670Sstevel@tonic-gate z->zone_ref--; 23680Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 23690Sstevel@tonic-gate /* no more refs, free the structure */ 23700Sstevel@tonic-gate mutex_exit(&z->zone_lock); 23710Sstevel@tonic-gate zone_free(z); 23720Sstevel@tonic-gate return; 23730Sstevel@tonic-gate } 23740Sstevel@tonic-gate /* signal zone_destroy so the zone can finish halting */ 23750Sstevel@tonic-gate wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 23760Sstevel@tonic-gate mutex_exit(&z->zone_lock); 23770Sstevel@tonic-gate 23780Sstevel@tonic-gate if (wakeup) { 23790Sstevel@tonic-gate /* 23800Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 23810Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 23820Sstevel@tonic-gate */ 23830Sstevel@tonic-gate mutex_enter(&zonehash_lock); 23840Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 23850Sstevel@tonic-gate mutex_exit(&zonehash_lock); 23860Sstevel@tonic-gate } 23870Sstevel@tonic-gate } 23880Sstevel@tonic-gate 23890Sstevel@tonic-gate void 23900Sstevel@tonic-gate zone_cred_hold(zone_t *z) 23910Sstevel@tonic-gate { 23920Sstevel@tonic-gate mutex_enter(&z->zone_lock); 23930Sstevel@tonic-gate z->zone_cred_ref++; 23940Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 23950Sstevel@tonic-gate mutex_exit(&z->zone_lock); 23960Sstevel@tonic-gate } 23970Sstevel@tonic-gate 23980Sstevel@tonic-gate void 23990Sstevel@tonic-gate zone_cred_rele(zone_t *z) 24000Sstevel@tonic-gate { 24010Sstevel@tonic-gate boolean_t wakeup; 24020Sstevel@tonic-gate 24030Sstevel@tonic-gate mutex_enter(&z->zone_lock); 24040Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 24050Sstevel@tonic-gate z->zone_cred_ref--; 24060Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 24070Sstevel@tonic-gate /* no more refs, free the structure */ 24080Sstevel@tonic-gate mutex_exit(&z->zone_lock); 24090Sstevel@tonic-gate zone_free(z); 24100Sstevel@tonic-gate return; 24110Sstevel@tonic-gate } 24120Sstevel@tonic-gate /* 24130Sstevel@tonic-gate * If zone_destroy is waiting for the cred references to drain 24140Sstevel@tonic-gate * out, and they have, signal it. 24150Sstevel@tonic-gate */ 24160Sstevel@tonic-gate wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 24170Sstevel@tonic-gate zone_status_get(z) >= ZONE_IS_DEAD); 24180Sstevel@tonic-gate mutex_exit(&z->zone_lock); 24190Sstevel@tonic-gate 24200Sstevel@tonic-gate if (wakeup) { 24210Sstevel@tonic-gate /* 24220Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 24230Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 24240Sstevel@tonic-gate */ 24250Sstevel@tonic-gate mutex_enter(&zonehash_lock); 24260Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 24270Sstevel@tonic-gate mutex_exit(&zonehash_lock); 24280Sstevel@tonic-gate } 24290Sstevel@tonic-gate } 24300Sstevel@tonic-gate 24310Sstevel@tonic-gate void 24320Sstevel@tonic-gate zone_task_hold(zone_t *z) 24330Sstevel@tonic-gate { 24340Sstevel@tonic-gate mutex_enter(&z->zone_lock); 24350Sstevel@tonic-gate z->zone_ntasks++; 24360Sstevel@tonic-gate ASSERT(z->zone_ntasks != 0); 24370Sstevel@tonic-gate mutex_exit(&z->zone_lock); 24380Sstevel@tonic-gate } 24390Sstevel@tonic-gate 24400Sstevel@tonic-gate void 24410Sstevel@tonic-gate zone_task_rele(zone_t *zone) 24420Sstevel@tonic-gate { 24430Sstevel@tonic-gate uint_t refcnt; 24440Sstevel@tonic-gate 24450Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 24460Sstevel@tonic-gate ASSERT(zone->zone_ntasks != 0); 24470Sstevel@tonic-gate refcnt = --zone->zone_ntasks; 24480Sstevel@tonic-gate if (refcnt > 1) { /* Common case */ 24490Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 24500Sstevel@tonic-gate return; 24510Sstevel@tonic-gate } 24520Sstevel@tonic-gate zone_hold_locked(zone); /* so we can use the zone_t later */ 24530Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 24540Sstevel@tonic-gate if (refcnt == 1) { 24550Sstevel@tonic-gate /* 24560Sstevel@tonic-gate * See if the zone is shutting down. 24570Sstevel@tonic-gate */ 24580Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24590Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 24600Sstevel@tonic-gate goto out; 24610Sstevel@tonic-gate } 24620Sstevel@tonic-gate 24630Sstevel@tonic-gate /* 24640Sstevel@tonic-gate * Make sure the ntasks didn't change since we 24650Sstevel@tonic-gate * dropped zone_lock. 24660Sstevel@tonic-gate */ 24670Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 24680Sstevel@tonic-gate if (refcnt != zone->zone_ntasks) { 24690Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 24700Sstevel@tonic-gate goto out; 24710Sstevel@tonic-gate } 24720Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 24730Sstevel@tonic-gate 24740Sstevel@tonic-gate /* 24750Sstevel@tonic-gate * No more user processes in the zone. The zone is empty. 24760Sstevel@tonic-gate */ 24770Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 24780Sstevel@tonic-gate goto out; 24790Sstevel@tonic-gate } 24800Sstevel@tonic-gate 24810Sstevel@tonic-gate ASSERT(refcnt == 0); 24820Sstevel@tonic-gate /* 24830Sstevel@tonic-gate * zsched has exited; the zone is dead. 24840Sstevel@tonic-gate */ 24850Sstevel@tonic-gate zone->zone_zsched = NULL; /* paranoia */ 24860Sstevel@tonic-gate mutex_enter(&zone_status_lock); 24870Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DEAD); 24880Sstevel@tonic-gate out: 24890Sstevel@tonic-gate mutex_exit(&zone_status_lock); 24900Sstevel@tonic-gate zone_rele(zone); 24910Sstevel@tonic-gate } 24920Sstevel@tonic-gate 24930Sstevel@tonic-gate zoneid_t 24940Sstevel@tonic-gate getzoneid(void) 24950Sstevel@tonic-gate { 24960Sstevel@tonic-gate return (curproc->p_zone->zone_id); 24970Sstevel@tonic-gate } 24980Sstevel@tonic-gate 24990Sstevel@tonic-gate /* 25000Sstevel@tonic-gate * Internal versions of zone_find_by_*(). These don't zone_hold() or 25010Sstevel@tonic-gate * check the validity of a zone's state. 25020Sstevel@tonic-gate */ 25030Sstevel@tonic-gate static zone_t * 25040Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid) 25050Sstevel@tonic-gate { 25060Sstevel@tonic-gate mod_hash_val_t hv; 25070Sstevel@tonic-gate zone_t *zone = NULL; 25080Sstevel@tonic-gate 25090Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 25100Sstevel@tonic-gate 25110Sstevel@tonic-gate if (mod_hash_find(zonehashbyid, 25120Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 25130Sstevel@tonic-gate zone = (zone_t *)hv; 25140Sstevel@tonic-gate return (zone); 25150Sstevel@tonic-gate } 25160Sstevel@tonic-gate 25170Sstevel@tonic-gate static zone_t * 25181676Sjpk zone_find_all_by_label(const ts_label_t *label) 25191676Sjpk { 25201676Sjpk mod_hash_val_t hv; 25211676Sjpk zone_t *zone = NULL; 25221676Sjpk 25231676Sjpk ASSERT(MUTEX_HELD(&zonehash_lock)); 25241676Sjpk 25251676Sjpk /* 25261676Sjpk * zonehashbylabel is not maintained for unlabeled systems 25271676Sjpk */ 25281676Sjpk if (!is_system_labeled()) 25291676Sjpk return (NULL); 25301676Sjpk if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0) 25311676Sjpk zone = (zone_t *)hv; 25321676Sjpk return (zone); 25331676Sjpk } 25341676Sjpk 25351676Sjpk static zone_t * 25360Sstevel@tonic-gate zone_find_all_by_name(char *name) 25370Sstevel@tonic-gate { 25380Sstevel@tonic-gate mod_hash_val_t hv; 25390Sstevel@tonic-gate zone_t *zone = NULL; 25400Sstevel@tonic-gate 25410Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 25420Sstevel@tonic-gate 25430Sstevel@tonic-gate if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 25440Sstevel@tonic-gate zone = (zone_t *)hv; 25450Sstevel@tonic-gate return (zone); 25460Sstevel@tonic-gate } 25470Sstevel@tonic-gate 25480Sstevel@tonic-gate /* 25490Sstevel@tonic-gate * Public interface for looking up a zone by zoneid. Only returns the zone if 25500Sstevel@tonic-gate * it is fully initialized, and has not yet begun the zone_destroy() sequence. 25510Sstevel@tonic-gate * Caller must call zone_rele() once it is done with the zone. 25520Sstevel@tonic-gate * 25530Sstevel@tonic-gate * The zone may begin the zone_destroy() sequence immediately after this 25540Sstevel@tonic-gate * function returns, but may be safely used until zone_rele() is called. 25550Sstevel@tonic-gate */ 25560Sstevel@tonic-gate zone_t * 25570Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid) 25580Sstevel@tonic-gate { 25590Sstevel@tonic-gate zone_t *zone; 25600Sstevel@tonic-gate zone_status_t status; 25610Sstevel@tonic-gate 25620Sstevel@tonic-gate mutex_enter(&zonehash_lock); 25630Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 25640Sstevel@tonic-gate mutex_exit(&zonehash_lock); 25650Sstevel@tonic-gate return (NULL); 25660Sstevel@tonic-gate } 25670Sstevel@tonic-gate status = zone_status_get(zone); 25680Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 25690Sstevel@tonic-gate /* 25700Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 25710Sstevel@tonic-gate */ 25720Sstevel@tonic-gate mutex_exit(&zonehash_lock); 25730Sstevel@tonic-gate return (NULL); 25740Sstevel@tonic-gate } 25750Sstevel@tonic-gate zone_hold(zone); 25760Sstevel@tonic-gate mutex_exit(&zonehash_lock); 25770Sstevel@tonic-gate return (zone); 25780Sstevel@tonic-gate } 25790Sstevel@tonic-gate 25800Sstevel@tonic-gate /* 25811676Sjpk * Similar to zone_find_by_id, but using zone label as the key. 25821676Sjpk */ 25831676Sjpk zone_t * 25841676Sjpk zone_find_by_label(const ts_label_t *label) 25851676Sjpk { 25861676Sjpk zone_t *zone; 25872110Srica zone_status_t status; 25881676Sjpk 25891676Sjpk mutex_enter(&zonehash_lock); 25901676Sjpk if ((zone = zone_find_all_by_label(label)) == NULL) { 25911676Sjpk mutex_exit(&zonehash_lock); 25921676Sjpk return (NULL); 25931676Sjpk } 25942110Srica 25952110Srica status = zone_status_get(zone); 25962110Srica if (status > ZONE_IS_DOWN) { 25971676Sjpk /* 25981676Sjpk * For all practical purposes the zone doesn't exist. 25991676Sjpk */ 26002110Srica mutex_exit(&zonehash_lock); 26012110Srica return (NULL); 26021676Sjpk } 26032110Srica zone_hold(zone); 26041676Sjpk mutex_exit(&zonehash_lock); 26051676Sjpk return (zone); 26061676Sjpk } 26071676Sjpk 26081676Sjpk /* 26090Sstevel@tonic-gate * Similar to zone_find_by_id, but using zone name as the key. 26100Sstevel@tonic-gate */ 26110Sstevel@tonic-gate zone_t * 26120Sstevel@tonic-gate zone_find_by_name(char *name) 26130Sstevel@tonic-gate { 26140Sstevel@tonic-gate zone_t *zone; 26150Sstevel@tonic-gate zone_status_t status; 26160Sstevel@tonic-gate 26170Sstevel@tonic-gate mutex_enter(&zonehash_lock); 26180Sstevel@tonic-gate if ((zone = zone_find_all_by_name(name)) == NULL) { 26190Sstevel@tonic-gate mutex_exit(&zonehash_lock); 26200Sstevel@tonic-gate return (NULL); 26210Sstevel@tonic-gate } 26220Sstevel@tonic-gate status = zone_status_get(zone); 26230Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 26240Sstevel@tonic-gate /* 26250Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 26260Sstevel@tonic-gate */ 26270Sstevel@tonic-gate mutex_exit(&zonehash_lock); 26280Sstevel@tonic-gate return (NULL); 26290Sstevel@tonic-gate } 26300Sstevel@tonic-gate zone_hold(zone); 26310Sstevel@tonic-gate mutex_exit(&zonehash_lock); 26320Sstevel@tonic-gate return (zone); 26330Sstevel@tonic-gate } 26340Sstevel@tonic-gate 26350Sstevel@tonic-gate /* 26360Sstevel@tonic-gate * Similar to zone_find_by_id(), using the path as a key. For instance, 26370Sstevel@tonic-gate * if there is a zone "foo" rooted at /foo/root, and the path argument 26380Sstevel@tonic-gate * is "/foo/root/proc", it will return the held zone_t corresponding to 26390Sstevel@tonic-gate * zone "foo". 26400Sstevel@tonic-gate * 26410Sstevel@tonic-gate * zone_find_by_path() always returns a non-NULL value, since at the 26420Sstevel@tonic-gate * very least every path will be contained in the global zone. 26430Sstevel@tonic-gate * 26440Sstevel@tonic-gate * As with the other zone_find_by_*() functions, the caller is 26450Sstevel@tonic-gate * responsible for zone_rele()ing the return value of this function. 26460Sstevel@tonic-gate */ 26470Sstevel@tonic-gate zone_t * 26480Sstevel@tonic-gate zone_find_by_path(const char *path) 26490Sstevel@tonic-gate { 26500Sstevel@tonic-gate zone_t *zone; 26510Sstevel@tonic-gate zone_t *zret = NULL; 26520Sstevel@tonic-gate zone_status_t status; 26530Sstevel@tonic-gate 26540Sstevel@tonic-gate if (path == NULL) { 26550Sstevel@tonic-gate /* 26560Sstevel@tonic-gate * Call from rootconf(). 26570Sstevel@tonic-gate */ 26580Sstevel@tonic-gate zone_hold(global_zone); 26590Sstevel@tonic-gate return (global_zone); 26600Sstevel@tonic-gate } 26610Sstevel@tonic-gate ASSERT(*path == '/'); 26620Sstevel@tonic-gate mutex_enter(&zonehash_lock); 26630Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 26640Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 26650Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(path, zone)) 26660Sstevel@tonic-gate zret = zone; 26670Sstevel@tonic-gate } 26680Sstevel@tonic-gate ASSERT(zret != NULL); 26690Sstevel@tonic-gate status = zone_status_get(zret); 26700Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 26710Sstevel@tonic-gate /* 26720Sstevel@tonic-gate * Zone practically doesn't exist. 26730Sstevel@tonic-gate */ 26740Sstevel@tonic-gate zret = global_zone; 26750Sstevel@tonic-gate } 26760Sstevel@tonic-gate zone_hold(zret); 26770Sstevel@tonic-gate mutex_exit(&zonehash_lock); 26780Sstevel@tonic-gate return (zret); 26790Sstevel@tonic-gate } 26800Sstevel@tonic-gate 26810Sstevel@tonic-gate /* 26820Sstevel@tonic-gate * Get the number of cpus visible to this zone. The system-wide global 26830Sstevel@tonic-gate * 'ncpus' is returned if pools are disabled, the caller is in the 26840Sstevel@tonic-gate * global zone, or a NULL zone argument is passed in. 26850Sstevel@tonic-gate */ 26860Sstevel@tonic-gate int 26870Sstevel@tonic-gate zone_ncpus_get(zone_t *zone) 26880Sstevel@tonic-gate { 26890Sstevel@tonic-gate int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 26900Sstevel@tonic-gate 26910Sstevel@tonic-gate return (myncpus != 0 ? myncpus : ncpus); 26920Sstevel@tonic-gate } 26930Sstevel@tonic-gate 26940Sstevel@tonic-gate /* 26950Sstevel@tonic-gate * Get the number of online cpus visible to this zone. The system-wide 26960Sstevel@tonic-gate * global 'ncpus_online' is returned if pools are disabled, the caller 26970Sstevel@tonic-gate * is in the global zone, or a NULL zone argument is passed in. 26980Sstevel@tonic-gate */ 26990Sstevel@tonic-gate int 27000Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone) 27010Sstevel@tonic-gate { 27020Sstevel@tonic-gate int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 27030Sstevel@tonic-gate 27040Sstevel@tonic-gate return (myncpus_online != 0 ? myncpus_online : ncpus_online); 27050Sstevel@tonic-gate } 27060Sstevel@tonic-gate 27070Sstevel@tonic-gate /* 27080Sstevel@tonic-gate * Return the pool to which the zone is currently bound. 27090Sstevel@tonic-gate */ 27100Sstevel@tonic-gate pool_t * 27110Sstevel@tonic-gate zone_pool_get(zone_t *zone) 27120Sstevel@tonic-gate { 27130Sstevel@tonic-gate ASSERT(pool_lock_held()); 27140Sstevel@tonic-gate 27150Sstevel@tonic-gate return (zone->zone_pool); 27160Sstevel@tonic-gate } 27170Sstevel@tonic-gate 27180Sstevel@tonic-gate /* 27190Sstevel@tonic-gate * Set the zone's pool pointer and update the zone's visibility to match 27200Sstevel@tonic-gate * the resources in the new pool. 27210Sstevel@tonic-gate */ 27220Sstevel@tonic-gate void 27230Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool) 27240Sstevel@tonic-gate { 27250Sstevel@tonic-gate ASSERT(pool_lock_held()); 27260Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 27270Sstevel@tonic-gate 27280Sstevel@tonic-gate zone->zone_pool = pool; 27290Sstevel@tonic-gate zone_pset_set(zone, pool->pool_pset->pset_id); 27300Sstevel@tonic-gate } 27310Sstevel@tonic-gate 27320Sstevel@tonic-gate /* 27330Sstevel@tonic-gate * Return the cached value of the id of the processor set to which the 27340Sstevel@tonic-gate * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 27350Sstevel@tonic-gate * facility is disabled. 27360Sstevel@tonic-gate */ 27370Sstevel@tonic-gate psetid_t 27380Sstevel@tonic-gate zone_pset_get(zone_t *zone) 27390Sstevel@tonic-gate { 27400Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 27410Sstevel@tonic-gate 27420Sstevel@tonic-gate return (zone->zone_psetid); 27430Sstevel@tonic-gate } 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate /* 27460Sstevel@tonic-gate * Set the cached value of the id of the processor set to which the zone 27470Sstevel@tonic-gate * is currently bound. Also update the zone's visibility to match the 27480Sstevel@tonic-gate * resources in the new processor set. 27490Sstevel@tonic-gate */ 27500Sstevel@tonic-gate void 27510Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid) 27520Sstevel@tonic-gate { 27530Sstevel@tonic-gate psetid_t oldpsetid; 27540Sstevel@tonic-gate 27550Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 27560Sstevel@tonic-gate oldpsetid = zone_pset_get(zone); 27570Sstevel@tonic-gate 27580Sstevel@tonic-gate if (oldpsetid == newpsetid) 27590Sstevel@tonic-gate return; 27600Sstevel@tonic-gate /* 27610Sstevel@tonic-gate * Global zone sees all. 27620Sstevel@tonic-gate */ 27630Sstevel@tonic-gate if (zone != global_zone) { 27640Sstevel@tonic-gate zone->zone_psetid = newpsetid; 27650Sstevel@tonic-gate if (newpsetid != ZONE_PS_INVAL) 27660Sstevel@tonic-gate pool_pset_visibility_add(newpsetid, zone); 27670Sstevel@tonic-gate if (oldpsetid != ZONE_PS_INVAL) 27680Sstevel@tonic-gate pool_pset_visibility_remove(oldpsetid, zone); 27690Sstevel@tonic-gate } 27700Sstevel@tonic-gate /* 27710Sstevel@tonic-gate * Disabling pools, so we should start using the global values 27720Sstevel@tonic-gate * for ncpus and ncpus_online. 27730Sstevel@tonic-gate */ 27740Sstevel@tonic-gate if (newpsetid == ZONE_PS_INVAL) { 27750Sstevel@tonic-gate zone->zone_ncpus = 0; 27760Sstevel@tonic-gate zone->zone_ncpus_online = 0; 27770Sstevel@tonic-gate } 27780Sstevel@tonic-gate } 27790Sstevel@tonic-gate 27800Sstevel@tonic-gate /* 27810Sstevel@tonic-gate * Walk the list of active zones and issue the provided callback for 27820Sstevel@tonic-gate * each of them. 27830Sstevel@tonic-gate * 27840Sstevel@tonic-gate * Caller must not be holding any locks that may be acquired under 27850Sstevel@tonic-gate * zonehash_lock. See comment at the beginning of the file for a list of 27860Sstevel@tonic-gate * common locks and their interactions with zones. 27870Sstevel@tonic-gate */ 27880Sstevel@tonic-gate int 27890Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data) 27900Sstevel@tonic-gate { 27910Sstevel@tonic-gate zone_t *zone; 27920Sstevel@tonic-gate int ret = 0; 27930Sstevel@tonic-gate zone_status_t status; 27940Sstevel@tonic-gate 27950Sstevel@tonic-gate mutex_enter(&zonehash_lock); 27960Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 27970Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 27980Sstevel@tonic-gate /* 27990Sstevel@tonic-gate * Skip zones that shouldn't be externally visible. 28000Sstevel@tonic-gate */ 28010Sstevel@tonic-gate status = zone_status_get(zone); 28020Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 28030Sstevel@tonic-gate continue; 28040Sstevel@tonic-gate /* 28050Sstevel@tonic-gate * Bail immediately if any callback invocation returns a 28060Sstevel@tonic-gate * non-zero value. 28070Sstevel@tonic-gate */ 28080Sstevel@tonic-gate ret = (*cb)(zone, data); 28090Sstevel@tonic-gate if (ret != 0) 28100Sstevel@tonic-gate break; 28110Sstevel@tonic-gate } 28120Sstevel@tonic-gate mutex_exit(&zonehash_lock); 28130Sstevel@tonic-gate return (ret); 28140Sstevel@tonic-gate } 28150Sstevel@tonic-gate 28160Sstevel@tonic-gate static int 28170Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath) 28180Sstevel@tonic-gate { 28190Sstevel@tonic-gate vnode_t *vp; 28200Sstevel@tonic-gate int trycount; 28210Sstevel@tonic-gate int error = 0; 28220Sstevel@tonic-gate char *path; 28230Sstevel@tonic-gate struct pathname upn, pn; 28240Sstevel@tonic-gate size_t pathlen; 28250Sstevel@tonic-gate 28260Sstevel@tonic-gate if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 28270Sstevel@tonic-gate return (error); 28280Sstevel@tonic-gate 28290Sstevel@tonic-gate pn_alloc(&pn); 28300Sstevel@tonic-gate 28310Sstevel@tonic-gate /* prevent infinite loop */ 28320Sstevel@tonic-gate trycount = 10; 28330Sstevel@tonic-gate for (;;) { 28340Sstevel@tonic-gate if (--trycount <= 0) { 28350Sstevel@tonic-gate error = ESTALE; 28360Sstevel@tonic-gate goto out; 28370Sstevel@tonic-gate } 28380Sstevel@tonic-gate 28390Sstevel@tonic-gate if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 28400Sstevel@tonic-gate /* 28410Sstevel@tonic-gate * VOP_ACCESS() may cover 'vp' with a new 28420Sstevel@tonic-gate * filesystem, if 'vp' is an autoFS vnode. 28430Sstevel@tonic-gate * Get the new 'vp' if so. 28440Sstevel@tonic-gate */ 28455331Samw if ((error = 28465331Samw VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) == 0 && 28474417Seh208807 (!vn_ismntpt(vp) || 28480Sstevel@tonic-gate (error = traverse(&vp)) == 0)) { 28490Sstevel@tonic-gate pathlen = pn.pn_pathlen + 2; 28500Sstevel@tonic-gate path = kmem_alloc(pathlen, KM_SLEEP); 28510Sstevel@tonic-gate (void) strncpy(path, pn.pn_path, 28520Sstevel@tonic-gate pn.pn_pathlen + 1); 28530Sstevel@tonic-gate path[pathlen - 2] = '/'; 28540Sstevel@tonic-gate path[pathlen - 1] = '\0'; 28550Sstevel@tonic-gate pn_free(&pn); 28560Sstevel@tonic-gate pn_free(&upn); 28570Sstevel@tonic-gate 28580Sstevel@tonic-gate /* Success! */ 28590Sstevel@tonic-gate break; 28600Sstevel@tonic-gate } 28610Sstevel@tonic-gate VN_RELE(vp); 28620Sstevel@tonic-gate } 28630Sstevel@tonic-gate if (error != ESTALE) 28640Sstevel@tonic-gate goto out; 28650Sstevel@tonic-gate } 28660Sstevel@tonic-gate 28670Sstevel@tonic-gate ASSERT(error == 0); 28680Sstevel@tonic-gate zone->zone_rootvp = vp; /* we hold a reference to vp */ 28690Sstevel@tonic-gate zone->zone_rootpath = path; 28700Sstevel@tonic-gate zone->zone_rootpathlen = pathlen; 28711769Scarlsonj if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0) 28721769Scarlsonj zone->zone_flags |= ZF_IS_SCRATCH; 28730Sstevel@tonic-gate return (0); 28740Sstevel@tonic-gate 28750Sstevel@tonic-gate out: 28760Sstevel@tonic-gate pn_free(&pn); 28770Sstevel@tonic-gate pn_free(&upn); 28780Sstevel@tonic-gate return (error); 28790Sstevel@tonic-gate } 28800Sstevel@tonic-gate 28810Sstevel@tonic-gate #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 28820Sstevel@tonic-gate ((c) >= 'a' && (c) <= 'z') || \ 28830Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'Z')) 28840Sstevel@tonic-gate 28850Sstevel@tonic-gate static int 28860Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname) 28870Sstevel@tonic-gate { 28880Sstevel@tonic-gate char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 28890Sstevel@tonic-gate size_t len; 28900Sstevel@tonic-gate int i, err; 28910Sstevel@tonic-gate 28920Sstevel@tonic-gate if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 28930Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 28940Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 28950Sstevel@tonic-gate } 28960Sstevel@tonic-gate 28970Sstevel@tonic-gate /* must be less than ZONENAME_MAX */ 28980Sstevel@tonic-gate if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 28990Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 29000Sstevel@tonic-gate return (EINVAL); 29010Sstevel@tonic-gate } 29020Sstevel@tonic-gate 29030Sstevel@tonic-gate /* 29040Sstevel@tonic-gate * Name must start with an alphanumeric and must contain only 29050Sstevel@tonic-gate * alphanumerics, '-', '_' and '.'. 29060Sstevel@tonic-gate */ 29070Sstevel@tonic-gate if (!isalnum(kname[0])) { 29080Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 29090Sstevel@tonic-gate return (EINVAL); 29100Sstevel@tonic-gate } 29110Sstevel@tonic-gate for (i = 1; i < len - 1; i++) { 29120Sstevel@tonic-gate if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 29130Sstevel@tonic-gate kname[i] != '.') { 29140Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 29150Sstevel@tonic-gate return (EINVAL); 29160Sstevel@tonic-gate } 29170Sstevel@tonic-gate } 29180Sstevel@tonic-gate 29190Sstevel@tonic-gate zone->zone_name = kname; 29200Sstevel@tonic-gate return (0); 29210Sstevel@tonic-gate } 29220Sstevel@tonic-gate 29230Sstevel@tonic-gate /* 29248662SJordan.Vaughan@Sun.com * Gets the 32-bit hostid of the specified zone as an unsigned int. If 'zonep' 29258662SJordan.Vaughan@Sun.com * is NULL or it points to a zone with no hostid emulation, then the machine's 29268662SJordan.Vaughan@Sun.com * hostid (i.e., the global zone's hostid) is returned. This function returns 29278662SJordan.Vaughan@Sun.com * zero if neither the zone nor the host machine (global zone) have hostids. It 29288662SJordan.Vaughan@Sun.com * returns HW_INVALID_HOSTID if the function attempts to return the machine's 29298662SJordan.Vaughan@Sun.com * hostid and the machine's hostid is invalid. 29308662SJordan.Vaughan@Sun.com */ 29318662SJordan.Vaughan@Sun.com uint32_t 29328662SJordan.Vaughan@Sun.com zone_get_hostid(zone_t *zonep) 29338662SJordan.Vaughan@Sun.com { 29348662SJordan.Vaughan@Sun.com unsigned long machine_hostid; 29358662SJordan.Vaughan@Sun.com 29368662SJordan.Vaughan@Sun.com if (zonep == NULL || zonep->zone_hostid == HW_INVALID_HOSTID) { 29378662SJordan.Vaughan@Sun.com if (ddi_strtoul(hw_serial, NULL, 10, &machine_hostid) != 0) 29388662SJordan.Vaughan@Sun.com return (HW_INVALID_HOSTID); 29398662SJordan.Vaughan@Sun.com return ((uint32_t)machine_hostid); 29408662SJordan.Vaughan@Sun.com } 29418662SJordan.Vaughan@Sun.com return (zonep->zone_hostid); 29428662SJordan.Vaughan@Sun.com } 29438662SJordan.Vaughan@Sun.com 29448662SJordan.Vaughan@Sun.com /* 29450Sstevel@tonic-gate * Similar to thread_create(), but makes sure the thread is in the appropriate 29460Sstevel@tonic-gate * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 29470Sstevel@tonic-gate */ 29480Sstevel@tonic-gate /*ARGSUSED*/ 29490Sstevel@tonic-gate kthread_t * 29500Sstevel@tonic-gate zthread_create( 29510Sstevel@tonic-gate caddr_t stk, 29520Sstevel@tonic-gate size_t stksize, 29530Sstevel@tonic-gate void (*proc)(), 29540Sstevel@tonic-gate void *arg, 29550Sstevel@tonic-gate size_t len, 29560Sstevel@tonic-gate pri_t pri) 29570Sstevel@tonic-gate { 29580Sstevel@tonic-gate kthread_t *t; 29590Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 29600Sstevel@tonic-gate proc_t *pp = zone->zone_zsched; 29610Sstevel@tonic-gate 29620Sstevel@tonic-gate zone_hold(zone); /* Reference to be dropped when thread exits */ 29630Sstevel@tonic-gate 29640Sstevel@tonic-gate /* 29650Sstevel@tonic-gate * No-one should be trying to create threads if the zone is shutting 29660Sstevel@tonic-gate * down and there aren't any kernel threads around. See comment 29670Sstevel@tonic-gate * in zthread_exit(). 29680Sstevel@tonic-gate */ 29690Sstevel@tonic-gate ASSERT(!(zone->zone_kthreads == NULL && 29700Sstevel@tonic-gate zone_status_get(zone) >= ZONE_IS_EMPTY)); 29710Sstevel@tonic-gate /* 29720Sstevel@tonic-gate * Create a thread, but don't let it run until we've finished setting 29730Sstevel@tonic-gate * things up. 29740Sstevel@tonic-gate */ 29750Sstevel@tonic-gate t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 29760Sstevel@tonic-gate ASSERT(t->t_forw == NULL); 29770Sstevel@tonic-gate mutex_enter(&zone_status_lock); 29780Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 29790Sstevel@tonic-gate t->t_forw = t->t_back = t; 29800Sstevel@tonic-gate } else { 29810Sstevel@tonic-gate kthread_t *tx = zone->zone_kthreads; 29820Sstevel@tonic-gate 29830Sstevel@tonic-gate t->t_forw = tx; 29840Sstevel@tonic-gate t->t_back = tx->t_back; 29850Sstevel@tonic-gate tx->t_back->t_forw = t; 29860Sstevel@tonic-gate tx->t_back = t; 29870Sstevel@tonic-gate } 29880Sstevel@tonic-gate zone->zone_kthreads = t; 29890Sstevel@tonic-gate mutex_exit(&zone_status_lock); 29900Sstevel@tonic-gate 29910Sstevel@tonic-gate mutex_enter(&pp->p_lock); 29920Sstevel@tonic-gate t->t_proc_flag |= TP_ZTHREAD; 29930Sstevel@tonic-gate project_rele(t->t_proj); 29940Sstevel@tonic-gate t->t_proj = project_hold(pp->p_task->tk_proj); 29950Sstevel@tonic-gate 29960Sstevel@tonic-gate /* 29970Sstevel@tonic-gate * Setup complete, let it run. 29980Sstevel@tonic-gate */ 29990Sstevel@tonic-gate thread_lock(t); 30000Sstevel@tonic-gate t->t_schedflag |= TS_ALLSTART; 30010Sstevel@tonic-gate setrun_locked(t); 30020Sstevel@tonic-gate thread_unlock(t); 30030Sstevel@tonic-gate 30040Sstevel@tonic-gate mutex_exit(&pp->p_lock); 30050Sstevel@tonic-gate 30060Sstevel@tonic-gate return (t); 30070Sstevel@tonic-gate } 30080Sstevel@tonic-gate 30090Sstevel@tonic-gate /* 30100Sstevel@tonic-gate * Similar to thread_exit(). Must be called by threads created via 30110Sstevel@tonic-gate * zthread_exit(). 30120Sstevel@tonic-gate */ 30130Sstevel@tonic-gate void 30140Sstevel@tonic-gate zthread_exit(void) 30150Sstevel@tonic-gate { 30160Sstevel@tonic-gate kthread_t *t = curthread; 30170Sstevel@tonic-gate proc_t *pp = curproc; 30180Sstevel@tonic-gate zone_t *zone = pp->p_zone; 30190Sstevel@tonic-gate 30200Sstevel@tonic-gate mutex_enter(&zone_status_lock); 30210Sstevel@tonic-gate 30220Sstevel@tonic-gate /* 30230Sstevel@tonic-gate * Reparent to p0 30240Sstevel@tonic-gate */ 30251075Sjosephb kpreempt_disable(); 30260Sstevel@tonic-gate mutex_enter(&pp->p_lock); 30270Sstevel@tonic-gate t->t_proc_flag &= ~TP_ZTHREAD; 30280Sstevel@tonic-gate t->t_procp = &p0; 30290Sstevel@tonic-gate hat_thread_exit(t); 30300Sstevel@tonic-gate mutex_exit(&pp->p_lock); 30311075Sjosephb kpreempt_enable(); 30320Sstevel@tonic-gate 30330Sstevel@tonic-gate if (t->t_back == t) { 30340Sstevel@tonic-gate ASSERT(t->t_forw == t); 30350Sstevel@tonic-gate /* 30360Sstevel@tonic-gate * If the zone is empty, once the thread count 30370Sstevel@tonic-gate * goes to zero no further kernel threads can be 30380Sstevel@tonic-gate * created. This is because if the creator is a process 30390Sstevel@tonic-gate * in the zone, then it must have exited before the zone 30400Sstevel@tonic-gate * state could be set to ZONE_IS_EMPTY. 30410Sstevel@tonic-gate * Otherwise, if the creator is a kernel thread in the 30420Sstevel@tonic-gate * zone, the thread count is non-zero. 30430Sstevel@tonic-gate * 30440Sstevel@tonic-gate * This really means that non-zone kernel threads should 30450Sstevel@tonic-gate * not create zone kernel threads. 30460Sstevel@tonic-gate */ 30470Sstevel@tonic-gate zone->zone_kthreads = NULL; 30480Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_EMPTY) { 30490Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 30503792Sakolb /* 30513792Sakolb * Remove any CPU caps on this zone. 30523792Sakolb */ 30533792Sakolb cpucaps_zone_remove(zone); 30540Sstevel@tonic-gate } 30550Sstevel@tonic-gate } else { 30560Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 30570Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 30580Sstevel@tonic-gate if (zone->zone_kthreads == t) 30590Sstevel@tonic-gate zone->zone_kthreads = t->t_forw; 30600Sstevel@tonic-gate } 30610Sstevel@tonic-gate mutex_exit(&zone_status_lock); 30620Sstevel@tonic-gate zone_rele(zone); 30630Sstevel@tonic-gate thread_exit(); 30640Sstevel@tonic-gate /* NOTREACHED */ 30650Sstevel@tonic-gate } 30660Sstevel@tonic-gate 30670Sstevel@tonic-gate static void 30680Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 30690Sstevel@tonic-gate { 30700Sstevel@tonic-gate vnode_t *oldvp; 30710Sstevel@tonic-gate 30720Sstevel@tonic-gate /* we're going to hold a reference here to the directory */ 30730Sstevel@tonic-gate VN_HOLD(vp); 30740Sstevel@tonic-gate 30750Sstevel@tonic-gate if (audit_active) /* update abs cwd/root path see c2audit.c */ 30760Sstevel@tonic-gate audit_chdirec(vp, vpp); 30770Sstevel@tonic-gate 30780Sstevel@tonic-gate mutex_enter(&pp->p_lock); 30790Sstevel@tonic-gate oldvp = *vpp; 30800Sstevel@tonic-gate *vpp = vp; 30810Sstevel@tonic-gate mutex_exit(&pp->p_lock); 30820Sstevel@tonic-gate if (oldvp != NULL) 30830Sstevel@tonic-gate VN_RELE(oldvp); 30840Sstevel@tonic-gate } 30850Sstevel@tonic-gate 30860Sstevel@tonic-gate /* 30870Sstevel@tonic-gate * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 30880Sstevel@tonic-gate */ 30890Sstevel@tonic-gate static int 30900Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 30910Sstevel@tonic-gate { 30920Sstevel@tonic-gate nvpair_t *nvp = NULL; 30930Sstevel@tonic-gate boolean_t priv_set = B_FALSE; 30940Sstevel@tonic-gate boolean_t limit_set = B_FALSE; 30950Sstevel@tonic-gate boolean_t action_set = B_FALSE; 30960Sstevel@tonic-gate 30970Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 30980Sstevel@tonic-gate const char *name; 30990Sstevel@tonic-gate uint64_t ui64; 31000Sstevel@tonic-gate 31010Sstevel@tonic-gate name = nvpair_name(nvp); 31020Sstevel@tonic-gate if (nvpair_type(nvp) != DATA_TYPE_UINT64) 31030Sstevel@tonic-gate return (EINVAL); 31040Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &ui64); 31050Sstevel@tonic-gate if (strcmp(name, "privilege") == 0) { 31060Sstevel@tonic-gate /* 31070Sstevel@tonic-gate * Currently only privileged values are allowed, but 31080Sstevel@tonic-gate * this may change in the future. 31090Sstevel@tonic-gate */ 31100Sstevel@tonic-gate if (ui64 != RCPRIV_PRIVILEGED) 31110Sstevel@tonic-gate return (EINVAL); 31120Sstevel@tonic-gate rv->rcv_privilege = ui64; 31130Sstevel@tonic-gate priv_set = B_TRUE; 31140Sstevel@tonic-gate } else if (strcmp(name, "limit") == 0) { 31150Sstevel@tonic-gate rv->rcv_value = ui64; 31160Sstevel@tonic-gate limit_set = B_TRUE; 31170Sstevel@tonic-gate } else if (strcmp(name, "action") == 0) { 31180Sstevel@tonic-gate if (ui64 != RCTL_LOCAL_NOACTION && 31190Sstevel@tonic-gate ui64 != RCTL_LOCAL_DENY) 31200Sstevel@tonic-gate return (EINVAL); 31210Sstevel@tonic-gate rv->rcv_flagaction = ui64; 31220Sstevel@tonic-gate action_set = B_TRUE; 31230Sstevel@tonic-gate } else { 31240Sstevel@tonic-gate return (EINVAL); 31250Sstevel@tonic-gate } 31260Sstevel@tonic-gate } 31270Sstevel@tonic-gate 31280Sstevel@tonic-gate if (!(priv_set && limit_set && action_set)) 31290Sstevel@tonic-gate return (EINVAL); 31300Sstevel@tonic-gate rv->rcv_action_signal = 0; 31310Sstevel@tonic-gate rv->rcv_action_recipient = NULL; 31320Sstevel@tonic-gate rv->rcv_action_recip_pid = -1; 31330Sstevel@tonic-gate rv->rcv_firing_time = 0; 31340Sstevel@tonic-gate 31350Sstevel@tonic-gate return (0); 31360Sstevel@tonic-gate } 31370Sstevel@tonic-gate 31382267Sdp /* 31392267Sdp * Non-global zone version of start_init. 31402267Sdp */ 31410Sstevel@tonic-gate void 31422267Sdp zone_start_init(void) 31430Sstevel@tonic-gate { 31440Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 31452712Snn35248 zone_t *z = p->p_zone; 31462267Sdp 31472267Sdp ASSERT(!INGLOBALZONE(curproc)); 31480Sstevel@tonic-gate 31490Sstevel@tonic-gate /* 31502712Snn35248 * For all purposes (ZONE_ATTR_INITPID and restart_init), 31512712Snn35248 * storing just the pid of init is sufficient. 31522712Snn35248 */ 31532712Snn35248 z->zone_proc_initpid = p->p_pid; 31542712Snn35248 31552712Snn35248 /* 31562267Sdp * We maintain zone_boot_err so that we can return the cause of the 31572267Sdp * failure back to the caller of the zone_boot syscall. 31580Sstevel@tonic-gate */ 31592267Sdp p->p_zone->zone_boot_err = start_init_common(); 31600Sstevel@tonic-gate 31618364SJordan.Vaughan@Sun.com /* 31628364SJordan.Vaughan@Sun.com * We will prevent booting zones from becoming running zones if the 31638364SJordan.Vaughan@Sun.com * global zone is shutting down. 31648364SJordan.Vaughan@Sun.com */ 31650Sstevel@tonic-gate mutex_enter(&zone_status_lock); 31668364SJordan.Vaughan@Sun.com if (z->zone_boot_err != 0 || zone_status_get(global_zone) >= 31678364SJordan.Vaughan@Sun.com ZONE_IS_SHUTTING_DOWN) { 31680Sstevel@tonic-gate /* 31690Sstevel@tonic-gate * Make sure we are still in the booting state-- we could have 31700Sstevel@tonic-gate * raced and already be shutting down, or even further along. 31710Sstevel@tonic-gate */ 31723792Sakolb if (zone_status_get(z) == ZONE_IS_BOOTING) { 31732712Snn35248 zone_status_set(z, ZONE_IS_SHUTTING_DOWN); 31743792Sakolb } 31750Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31760Sstevel@tonic-gate /* It's gone bad, dispose of the process */ 31772712Snn35248 if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) { 3178390Sraf mutex_enter(&p->p_lock); 3179390Sraf ASSERT(p->p_flag & SEXITLWPS); 31800Sstevel@tonic-gate lwp_exit(); 31810Sstevel@tonic-gate } 31820Sstevel@tonic-gate } else { 31832712Snn35248 if (zone_status_get(z) == ZONE_IS_BOOTING) 31842712Snn35248 zone_status_set(z, ZONE_IS_RUNNING); 31850Sstevel@tonic-gate mutex_exit(&zone_status_lock); 31860Sstevel@tonic-gate /* cause the process to return to userland. */ 31870Sstevel@tonic-gate lwp_rtt(); 31880Sstevel@tonic-gate } 31890Sstevel@tonic-gate } 31900Sstevel@tonic-gate 31910Sstevel@tonic-gate struct zsched_arg { 31920Sstevel@tonic-gate zone_t *zone; 31930Sstevel@tonic-gate nvlist_t *nvlist; 31940Sstevel@tonic-gate }; 31950Sstevel@tonic-gate 31960Sstevel@tonic-gate /* 31970Sstevel@tonic-gate * Per-zone "sched" workalike. The similarity to "sched" doesn't have 31980Sstevel@tonic-gate * anything to do with scheduling, but rather with the fact that 31990Sstevel@tonic-gate * per-zone kernel threads are parented to zsched, just like regular 32000Sstevel@tonic-gate * kernel threads are parented to sched (p0). 32010Sstevel@tonic-gate * 32020Sstevel@tonic-gate * zsched is also responsible for launching init for the zone. 32030Sstevel@tonic-gate */ 32040Sstevel@tonic-gate static void 32050Sstevel@tonic-gate zsched(void *arg) 32060Sstevel@tonic-gate { 32070Sstevel@tonic-gate struct zsched_arg *za = arg; 32080Sstevel@tonic-gate proc_t *pp = curproc; 32090Sstevel@tonic-gate proc_t *initp = proc_init; 32100Sstevel@tonic-gate zone_t *zone = za->zone; 32110Sstevel@tonic-gate cred_t *cr, *oldcred; 32120Sstevel@tonic-gate rctl_set_t *set; 32130Sstevel@tonic-gate rctl_alloc_gp_t *gp; 32140Sstevel@tonic-gate contract_t *ct = NULL; 32150Sstevel@tonic-gate task_t *tk, *oldtk; 32160Sstevel@tonic-gate rctl_entity_p_t e; 32170Sstevel@tonic-gate kproject_t *pj; 32180Sstevel@tonic-gate 32190Sstevel@tonic-gate nvlist_t *nvl = za->nvlist; 32200Sstevel@tonic-gate nvpair_t *nvp = NULL; 32210Sstevel@tonic-gate 32223446Smrj bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched")); 32233446Smrj bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched")); 32243446Smrj PTOU(pp)->u_argc = 0; 32253446Smrj PTOU(pp)->u_argv = NULL; 32263446Smrj PTOU(pp)->u_envp = NULL; 32270Sstevel@tonic-gate closeall(P_FINFO(pp)); 32280Sstevel@tonic-gate 32290Sstevel@tonic-gate /* 32300Sstevel@tonic-gate * We are this zone's "zsched" process. As the zone isn't generally 32310Sstevel@tonic-gate * visible yet we don't need to grab any locks before initializing its 32320Sstevel@tonic-gate * zone_proc pointer. 32330Sstevel@tonic-gate */ 32340Sstevel@tonic-gate zone_hold(zone); /* this hold is released by zone_destroy() */ 32350Sstevel@tonic-gate zone->zone_zsched = pp; 32360Sstevel@tonic-gate mutex_enter(&pp->p_lock); 32370Sstevel@tonic-gate pp->p_zone = zone; 32380Sstevel@tonic-gate mutex_exit(&pp->p_lock); 32390Sstevel@tonic-gate 32400Sstevel@tonic-gate /* 32410Sstevel@tonic-gate * Disassociate process from its 'parent'; parent ourselves to init 32420Sstevel@tonic-gate * (pid 1) and change other values as needed. 32430Sstevel@tonic-gate */ 32440Sstevel@tonic-gate sess_create(); 32450Sstevel@tonic-gate 32460Sstevel@tonic-gate mutex_enter(&pidlock); 32470Sstevel@tonic-gate proc_detach(pp); 32480Sstevel@tonic-gate pp->p_ppid = 1; 32490Sstevel@tonic-gate pp->p_flag |= SZONETOP; 32500Sstevel@tonic-gate pp->p_ancpid = 1; 32510Sstevel@tonic-gate pp->p_parent = initp; 32520Sstevel@tonic-gate pp->p_psibling = NULL; 32530Sstevel@tonic-gate if (initp->p_child) 32540Sstevel@tonic-gate initp->p_child->p_psibling = pp; 32550Sstevel@tonic-gate pp->p_sibling = initp->p_child; 32560Sstevel@tonic-gate initp->p_child = pp; 32570Sstevel@tonic-gate 32580Sstevel@tonic-gate /* Decrement what newproc() incremented. */ 32590Sstevel@tonic-gate upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 32600Sstevel@tonic-gate /* 32610Sstevel@tonic-gate * Our credentials are about to become kcred-like, so we don't care 32620Sstevel@tonic-gate * about the caller's ruid. 32630Sstevel@tonic-gate */ 32640Sstevel@tonic-gate upcount_inc(crgetruid(kcred), zone->zone_id); 32650Sstevel@tonic-gate mutex_exit(&pidlock); 32660Sstevel@tonic-gate 32670Sstevel@tonic-gate /* 32680Sstevel@tonic-gate * getting out of global zone, so decrement lwp counts 32690Sstevel@tonic-gate */ 32700Sstevel@tonic-gate pj = pp->p_task->tk_proj; 32710Sstevel@tonic-gate mutex_enter(&global_zone->zone_nlwps_lock); 32720Sstevel@tonic-gate pj->kpj_nlwps -= pp->p_lwpcnt; 32730Sstevel@tonic-gate global_zone->zone_nlwps -= pp->p_lwpcnt; 32740Sstevel@tonic-gate mutex_exit(&global_zone->zone_nlwps_lock); 32750Sstevel@tonic-gate 32760Sstevel@tonic-gate /* 32772768Ssl108498 * Decrement locked memory counts on old zone and project. 32782768Ssl108498 */ 32793247Sgjelinek mutex_enter(&global_zone->zone_mem_lock); 32802768Ssl108498 global_zone->zone_locked_mem -= pp->p_locked_mem; 32812768Ssl108498 pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 32823247Sgjelinek mutex_exit(&global_zone->zone_mem_lock); 32832768Ssl108498 32842768Ssl108498 /* 32850Sstevel@tonic-gate * Create and join a new task in project '0' of this zone. 32860Sstevel@tonic-gate * 32870Sstevel@tonic-gate * We don't need to call holdlwps() since we know we're the only lwp in 32880Sstevel@tonic-gate * this process. 32890Sstevel@tonic-gate * 32900Sstevel@tonic-gate * task_join() returns with p_lock held. 32910Sstevel@tonic-gate */ 32920Sstevel@tonic-gate tk = task_create(0, zone); 32930Sstevel@tonic-gate mutex_enter(&cpu_lock); 32940Sstevel@tonic-gate oldtk = task_join(tk, 0); 32952768Ssl108498 32962768Ssl108498 pj = pp->p_task->tk_proj; 32972768Ssl108498 32983247Sgjelinek mutex_enter(&zone->zone_mem_lock); 32992768Ssl108498 zone->zone_locked_mem += pp->p_locked_mem; 33002768Ssl108498 pj->kpj_data.kpd_locked_mem += pp->p_locked_mem; 33013247Sgjelinek mutex_exit(&zone->zone_mem_lock); 33020Sstevel@tonic-gate 33030Sstevel@tonic-gate /* 33040Sstevel@tonic-gate * add lwp counts to zsched's zone, and increment project's task count 33050Sstevel@tonic-gate * due to the task created in the above tasksys_settaskid 33060Sstevel@tonic-gate */ 33072768Ssl108498 33080Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 33090Sstevel@tonic-gate pj->kpj_nlwps += pp->p_lwpcnt; 33100Sstevel@tonic-gate pj->kpj_ntasks += 1; 33110Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 33120Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 33130Sstevel@tonic-gate 33142768Ssl108498 mutex_exit(&curproc->p_lock); 33152768Ssl108498 mutex_exit(&cpu_lock); 33162768Ssl108498 task_rele(oldtk); 33172768Ssl108498 33180Sstevel@tonic-gate /* 33190Sstevel@tonic-gate * The process was created by a process in the global zone, hence the 33200Sstevel@tonic-gate * credentials are wrong. We might as well have kcred-ish credentials. 33210Sstevel@tonic-gate */ 33220Sstevel@tonic-gate cr = zone->zone_kcred; 33230Sstevel@tonic-gate crhold(cr); 33240Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 33250Sstevel@tonic-gate oldcred = pp->p_cred; 33260Sstevel@tonic-gate pp->p_cred = cr; 33270Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 33280Sstevel@tonic-gate crfree(oldcred); 33290Sstevel@tonic-gate 33300Sstevel@tonic-gate /* 33310Sstevel@tonic-gate * Hold credentials again (for thread) 33320Sstevel@tonic-gate */ 33330Sstevel@tonic-gate crhold(cr); 33340Sstevel@tonic-gate 33350Sstevel@tonic-gate /* 33360Sstevel@tonic-gate * p_lwpcnt can't change since this is a kernel process. 33370Sstevel@tonic-gate */ 33380Sstevel@tonic-gate crset(pp, cr); 33390Sstevel@tonic-gate 33400Sstevel@tonic-gate /* 33410Sstevel@tonic-gate * Chroot 33420Sstevel@tonic-gate */ 33430Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 33440Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 33450Sstevel@tonic-gate 33460Sstevel@tonic-gate /* 33470Sstevel@tonic-gate * Initialize zone's rctl set. 33480Sstevel@tonic-gate */ 33490Sstevel@tonic-gate set = rctl_set_create(); 33500Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 33510Sstevel@tonic-gate mutex_enter(&pp->p_lock); 33520Sstevel@tonic-gate e.rcep_p.zone = zone; 33530Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 33540Sstevel@tonic-gate zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 33550Sstevel@tonic-gate mutex_exit(&pp->p_lock); 33560Sstevel@tonic-gate rctl_prealloc_destroy(gp); 33570Sstevel@tonic-gate 33580Sstevel@tonic-gate /* 33590Sstevel@tonic-gate * Apply the rctls passed in to zone_create(). This is basically a list 33600Sstevel@tonic-gate * assignment: all of the old values are removed and the new ones 33610Sstevel@tonic-gate * inserted. That is, if an empty list is passed in, all values are 33620Sstevel@tonic-gate * removed. 33630Sstevel@tonic-gate */ 33640Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 33650Sstevel@tonic-gate rctl_dict_entry_t *rde; 33660Sstevel@tonic-gate rctl_hndl_t hndl; 33670Sstevel@tonic-gate char *name; 33680Sstevel@tonic-gate nvlist_t **nvlarray; 33690Sstevel@tonic-gate uint_t i, nelem; 33700Sstevel@tonic-gate int error; /* For ASSERT()s */ 33710Sstevel@tonic-gate 33720Sstevel@tonic-gate name = nvpair_name(nvp); 33730Sstevel@tonic-gate hndl = rctl_hndl_lookup(name); 33740Sstevel@tonic-gate ASSERT(hndl != -1); 33750Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 33760Sstevel@tonic-gate ASSERT(rde != NULL); 33770Sstevel@tonic-gate 33780Sstevel@tonic-gate for (; /* ever */; ) { 33790Sstevel@tonic-gate rctl_val_t oval; 33800Sstevel@tonic-gate 33810Sstevel@tonic-gate mutex_enter(&pp->p_lock); 33820Sstevel@tonic-gate error = rctl_local_get(hndl, NULL, &oval, pp); 33830Sstevel@tonic-gate mutex_exit(&pp->p_lock); 33840Sstevel@tonic-gate ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 33850Sstevel@tonic-gate ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 33860Sstevel@tonic-gate if (oval.rcv_privilege == RCPRIV_SYSTEM) 33870Sstevel@tonic-gate break; 33880Sstevel@tonic-gate mutex_enter(&pp->p_lock); 33890Sstevel@tonic-gate error = rctl_local_delete(hndl, &oval, pp); 33900Sstevel@tonic-gate mutex_exit(&pp->p_lock); 33910Sstevel@tonic-gate ASSERT(error == 0); 33920Sstevel@tonic-gate } 33930Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 33940Sstevel@tonic-gate ASSERT(error == 0); 33950Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 33960Sstevel@tonic-gate rctl_val_t *nvalp; 33970Sstevel@tonic-gate 33980Sstevel@tonic-gate nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 33990Sstevel@tonic-gate error = nvlist2rctlval(nvlarray[i], nvalp); 34000Sstevel@tonic-gate ASSERT(error == 0); 34010Sstevel@tonic-gate /* 34020Sstevel@tonic-gate * rctl_local_insert can fail if the value being 34030Sstevel@tonic-gate * inserted is a duplicate; this is OK. 34040Sstevel@tonic-gate */ 34050Sstevel@tonic-gate mutex_enter(&pp->p_lock); 34060Sstevel@tonic-gate if (rctl_local_insert(hndl, nvalp, pp) != 0) 34070Sstevel@tonic-gate kmem_cache_free(rctl_val_cache, nvalp); 34080Sstevel@tonic-gate mutex_exit(&pp->p_lock); 34090Sstevel@tonic-gate } 34100Sstevel@tonic-gate } 34110Sstevel@tonic-gate /* 34120Sstevel@tonic-gate * Tell the world that we're done setting up. 34130Sstevel@tonic-gate * 34145880Snordmark * At this point we want to set the zone status to ZONE_IS_INITIALIZED 34150Sstevel@tonic-gate * and atomically set the zone's processor set visibility. Once 34160Sstevel@tonic-gate * we drop pool_lock() this zone will automatically get updated 34170Sstevel@tonic-gate * to reflect any future changes to the pools configuration. 34185880Snordmark * 34195880Snordmark * Note that after we drop the locks below (zonehash_lock in 34205880Snordmark * particular) other operations such as a zone_getattr call can 34215880Snordmark * now proceed and observe the zone. That is the reason for doing a 34225880Snordmark * state transition to the INITIALIZED state. 34230Sstevel@tonic-gate */ 34240Sstevel@tonic-gate pool_lock(); 34250Sstevel@tonic-gate mutex_enter(&cpu_lock); 34260Sstevel@tonic-gate mutex_enter(&zonehash_lock); 34270Sstevel@tonic-gate zone_uniqid(zone); 34280Sstevel@tonic-gate zone_zsd_configure(zone); 34290Sstevel@tonic-gate if (pool_state == POOL_ENABLED) 34300Sstevel@tonic-gate zone_pset_set(zone, pool_default->pool_pset->pset_id); 34310Sstevel@tonic-gate mutex_enter(&zone_status_lock); 34320Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 34335880Snordmark zone_status_set(zone, ZONE_IS_INITIALIZED); 34340Sstevel@tonic-gate mutex_exit(&zone_status_lock); 34350Sstevel@tonic-gate mutex_exit(&zonehash_lock); 34360Sstevel@tonic-gate mutex_exit(&cpu_lock); 34370Sstevel@tonic-gate pool_unlock(); 34380Sstevel@tonic-gate 34395880Snordmark /* Now call the create callback for this key */ 34405880Snordmark zsd_apply_all_keys(zsd_apply_create, zone); 34415880Snordmark 34425880Snordmark /* The callbacks are complete. Mark ZONE_IS_READY */ 34435880Snordmark mutex_enter(&zone_status_lock); 34445880Snordmark ASSERT(zone_status_get(zone) == ZONE_IS_INITIALIZED); 34455880Snordmark zone_status_set(zone, ZONE_IS_READY); 34465880Snordmark mutex_exit(&zone_status_lock); 34475880Snordmark 34480Sstevel@tonic-gate /* 34490Sstevel@tonic-gate * Once we see the zone transition to the ZONE_IS_BOOTING state, 34500Sstevel@tonic-gate * we launch init, and set the state to running. 34510Sstevel@tonic-gate */ 34520Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 34530Sstevel@tonic-gate 34540Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_BOOTING) { 34550Sstevel@tonic-gate id_t cid; 34560Sstevel@tonic-gate 34570Sstevel@tonic-gate /* 34580Sstevel@tonic-gate * Ok, this is a little complicated. We need to grab the 34590Sstevel@tonic-gate * zone's pool's scheduling class ID; note that by now, we 34600Sstevel@tonic-gate * are already bound to a pool if we need to be (zoneadmd 34610Sstevel@tonic-gate * will have done that to us while we're in the READY 34620Sstevel@tonic-gate * state). *But* the scheduling class for the zone's 'init' 34630Sstevel@tonic-gate * must be explicitly passed to newproc, which doesn't 34640Sstevel@tonic-gate * respect pool bindings. 34650Sstevel@tonic-gate * 34660Sstevel@tonic-gate * We hold the pool_lock across the call to newproc() to 34670Sstevel@tonic-gate * close the obvious race: the pool's scheduling class 34680Sstevel@tonic-gate * could change before we manage to create the LWP with 34690Sstevel@tonic-gate * classid 'cid'. 34700Sstevel@tonic-gate */ 34710Sstevel@tonic-gate pool_lock(); 34723247Sgjelinek if (zone->zone_defaultcid > 0) 34733247Sgjelinek cid = zone->zone_defaultcid; 34743247Sgjelinek else 34753247Sgjelinek cid = pool_get_class(zone->zone_pool); 34760Sstevel@tonic-gate if (cid == -1) 34770Sstevel@tonic-gate cid = defaultcid; 34780Sstevel@tonic-gate 34790Sstevel@tonic-gate /* 34800Sstevel@tonic-gate * If this fails, zone_boot will ultimately fail. The 34810Sstevel@tonic-gate * state of the zone will be set to SHUTTING_DOWN-- userland 34820Sstevel@tonic-gate * will have to tear down the zone, and fail, or try again. 34830Sstevel@tonic-gate */ 34842267Sdp if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid, 3485*11173SJonathan.Adams@Sun.COM minclsyspri - 1, &ct, 0)) != 0) { 34860Sstevel@tonic-gate mutex_enter(&zone_status_lock); 34870Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 34880Sstevel@tonic-gate mutex_exit(&zone_status_lock); 34890Sstevel@tonic-gate } 34900Sstevel@tonic-gate pool_unlock(); 34910Sstevel@tonic-gate } 34920Sstevel@tonic-gate 34930Sstevel@tonic-gate /* 34940Sstevel@tonic-gate * Wait for zone_destroy() to be called. This is what we spend 34950Sstevel@tonic-gate * most of our life doing. 34960Sstevel@tonic-gate */ 34970Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 34980Sstevel@tonic-gate 34990Sstevel@tonic-gate if (ct) 35000Sstevel@tonic-gate /* 35010Sstevel@tonic-gate * At this point the process contract should be empty. 35020Sstevel@tonic-gate * (Though if it isn't, it's not the end of the world.) 35030Sstevel@tonic-gate */ 35040Sstevel@tonic-gate VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 35050Sstevel@tonic-gate 35060Sstevel@tonic-gate /* 35070Sstevel@tonic-gate * Allow kcred to be freed when all referring processes 35080Sstevel@tonic-gate * (including this one) go away. We can't just do this in 35090Sstevel@tonic-gate * zone_free because we need to wait for the zone_cred_ref to 35100Sstevel@tonic-gate * drop to 0 before calling zone_free, and the existence of 35110Sstevel@tonic-gate * zone_kcred will prevent that. Thus, we call crfree here to 35120Sstevel@tonic-gate * balance the crdup in zone_create. The crhold calls earlier 35130Sstevel@tonic-gate * in zsched will be dropped when the thread and process exit. 35140Sstevel@tonic-gate */ 35150Sstevel@tonic-gate crfree(zone->zone_kcred); 35160Sstevel@tonic-gate zone->zone_kcred = NULL; 35170Sstevel@tonic-gate 35180Sstevel@tonic-gate exit(CLD_EXITED, 0); 35190Sstevel@tonic-gate } 35200Sstevel@tonic-gate 35210Sstevel@tonic-gate /* 35220Sstevel@tonic-gate * Helper function to determine if there are any submounts of the 35230Sstevel@tonic-gate * provided path. Used to make sure the zone doesn't "inherit" any 35240Sstevel@tonic-gate * mounts from before it is created. 35250Sstevel@tonic-gate */ 35260Sstevel@tonic-gate static uint_t 35270Sstevel@tonic-gate zone_mount_count(const char *rootpath) 35280Sstevel@tonic-gate { 35290Sstevel@tonic-gate vfs_t *vfsp; 35300Sstevel@tonic-gate uint_t count = 0; 35310Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 35320Sstevel@tonic-gate 35330Sstevel@tonic-gate /* 35340Sstevel@tonic-gate * Holding zonehash_lock prevents race conditions with 35350Sstevel@tonic-gate * vfs_list_add()/vfs_list_remove() since we serialize with 35360Sstevel@tonic-gate * zone_find_by_path(). 35370Sstevel@tonic-gate */ 35380Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 35390Sstevel@tonic-gate /* 35400Sstevel@tonic-gate * The rootpath must end with a '/' 35410Sstevel@tonic-gate */ 35420Sstevel@tonic-gate ASSERT(rootpath[rootpathlen - 1] == '/'); 35430Sstevel@tonic-gate 35440Sstevel@tonic-gate /* 35450Sstevel@tonic-gate * This intentionally does not count the rootpath itself if that 35460Sstevel@tonic-gate * happens to be a mount point. 35470Sstevel@tonic-gate */ 35480Sstevel@tonic-gate vfs_list_read_lock(); 35490Sstevel@tonic-gate vfsp = rootvfs; 35500Sstevel@tonic-gate do { 35510Sstevel@tonic-gate if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 35520Sstevel@tonic-gate rootpathlen) == 0) 35530Sstevel@tonic-gate count++; 35540Sstevel@tonic-gate vfsp = vfsp->vfs_next; 35550Sstevel@tonic-gate } while (vfsp != rootvfs); 35560Sstevel@tonic-gate vfs_list_unlock(); 35570Sstevel@tonic-gate return (count); 35580Sstevel@tonic-gate } 35590Sstevel@tonic-gate 35600Sstevel@tonic-gate /* 35610Sstevel@tonic-gate * Helper function to make sure that a zone created on 'rootpath' 35620Sstevel@tonic-gate * wouldn't end up containing other zones' rootpaths. 35630Sstevel@tonic-gate */ 35640Sstevel@tonic-gate static boolean_t 35650Sstevel@tonic-gate zone_is_nested(const char *rootpath) 35660Sstevel@tonic-gate { 35670Sstevel@tonic-gate zone_t *zone; 35680Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 35690Sstevel@tonic-gate size_t len; 35700Sstevel@tonic-gate 35710Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 35720Sstevel@tonic-gate 35738799SDhanaraj.M@Sun.COM /* 35748799SDhanaraj.M@Sun.COM * zone_set_root() appended '/' and '\0' at the end of rootpath 35758799SDhanaraj.M@Sun.COM */ 35768799SDhanaraj.M@Sun.COM if ((rootpathlen <= 3) && (rootpath[0] == '/') && 35778799SDhanaraj.M@Sun.COM (rootpath[1] == '/') && (rootpath[2] == '\0')) 35788799SDhanaraj.M@Sun.COM return (B_TRUE); 35798799SDhanaraj.M@Sun.COM 35800Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 35810Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 35820Sstevel@tonic-gate if (zone == global_zone) 35830Sstevel@tonic-gate continue; 35840Sstevel@tonic-gate len = strlen(zone->zone_rootpath); 35850Sstevel@tonic-gate if (strncmp(rootpath, zone->zone_rootpath, 35860Sstevel@tonic-gate MIN(rootpathlen, len)) == 0) 35870Sstevel@tonic-gate return (B_TRUE); 35880Sstevel@tonic-gate } 35890Sstevel@tonic-gate return (B_FALSE); 35900Sstevel@tonic-gate } 35910Sstevel@tonic-gate 35920Sstevel@tonic-gate static int 3593813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs, 3594813Sdp size_t zone_privssz) 35950Sstevel@tonic-gate { 35960Sstevel@tonic-gate priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 35970Sstevel@tonic-gate 3598813Sdp if (zone_privssz < sizeof (priv_set_t)) 3599813Sdp return (set_errno(ENOMEM)); 3600813Sdp 36010Sstevel@tonic-gate if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 36020Sstevel@tonic-gate kmem_free(privs, sizeof (priv_set_t)); 36030Sstevel@tonic-gate return (EFAULT); 36040Sstevel@tonic-gate } 36050Sstevel@tonic-gate 36060Sstevel@tonic-gate zone->zone_privset = privs; 36070Sstevel@tonic-gate return (0); 36080Sstevel@tonic-gate } 36090Sstevel@tonic-gate 36100Sstevel@tonic-gate /* 36110Sstevel@tonic-gate * We make creative use of nvlists to pass in rctls from userland. The list is 36120Sstevel@tonic-gate * a list of the following structures: 36130Sstevel@tonic-gate * 36140Sstevel@tonic-gate * (name = rctl_name, value = nvpair_list_array) 36150Sstevel@tonic-gate * 36160Sstevel@tonic-gate * Where each element of the nvpair_list_array is of the form: 36170Sstevel@tonic-gate * 36180Sstevel@tonic-gate * [(name = "privilege", value = RCPRIV_PRIVILEGED), 36190Sstevel@tonic-gate * (name = "limit", value = uint64_t), 36200Sstevel@tonic-gate * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 36210Sstevel@tonic-gate */ 36220Sstevel@tonic-gate static int 36230Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 36240Sstevel@tonic-gate { 36250Sstevel@tonic-gate nvpair_t *nvp = NULL; 36260Sstevel@tonic-gate nvlist_t *nvl = NULL; 36270Sstevel@tonic-gate char *kbuf; 36280Sstevel@tonic-gate int error; 36290Sstevel@tonic-gate rctl_val_t rv; 36300Sstevel@tonic-gate 36310Sstevel@tonic-gate *nvlp = NULL; 36320Sstevel@tonic-gate 36330Sstevel@tonic-gate if (buflen == 0) 36340Sstevel@tonic-gate return (0); 36350Sstevel@tonic-gate 36360Sstevel@tonic-gate if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 36370Sstevel@tonic-gate return (ENOMEM); 36380Sstevel@tonic-gate if (copyin(ubuf, kbuf, buflen)) { 36390Sstevel@tonic-gate error = EFAULT; 36400Sstevel@tonic-gate goto out; 36410Sstevel@tonic-gate } 36420Sstevel@tonic-gate if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 36430Sstevel@tonic-gate /* 36440Sstevel@tonic-gate * nvl may have been allocated/free'd, but the value set to 36450Sstevel@tonic-gate * non-NULL, so we reset it here. 36460Sstevel@tonic-gate */ 36470Sstevel@tonic-gate nvl = NULL; 36480Sstevel@tonic-gate error = EINVAL; 36490Sstevel@tonic-gate goto out; 36500Sstevel@tonic-gate } 36510Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 36520Sstevel@tonic-gate rctl_dict_entry_t *rde; 36530Sstevel@tonic-gate rctl_hndl_t hndl; 36540Sstevel@tonic-gate nvlist_t **nvlarray; 36550Sstevel@tonic-gate uint_t i, nelem; 36560Sstevel@tonic-gate char *name; 36570Sstevel@tonic-gate 36580Sstevel@tonic-gate error = EINVAL; 36590Sstevel@tonic-gate name = nvpair_name(nvp); 36600Sstevel@tonic-gate if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 36610Sstevel@tonic-gate != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 36620Sstevel@tonic-gate goto out; 36630Sstevel@tonic-gate } 36640Sstevel@tonic-gate if ((hndl = rctl_hndl_lookup(name)) == -1) { 36650Sstevel@tonic-gate goto out; 36660Sstevel@tonic-gate } 36670Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 36680Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 36690Sstevel@tonic-gate ASSERT(error == 0); 36700Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 36710Sstevel@tonic-gate if (error = nvlist2rctlval(nvlarray[i], &rv)) 36720Sstevel@tonic-gate goto out; 36730Sstevel@tonic-gate } 36740Sstevel@tonic-gate if (rctl_invalid_value(rde, &rv)) { 36750Sstevel@tonic-gate error = EINVAL; 36760Sstevel@tonic-gate goto out; 36770Sstevel@tonic-gate } 36780Sstevel@tonic-gate } 36790Sstevel@tonic-gate error = 0; 36800Sstevel@tonic-gate *nvlp = nvl; 36810Sstevel@tonic-gate out: 36820Sstevel@tonic-gate kmem_free(kbuf, buflen); 36830Sstevel@tonic-gate if (error && nvl != NULL) 36840Sstevel@tonic-gate nvlist_free(nvl); 36850Sstevel@tonic-gate return (error); 36860Sstevel@tonic-gate } 36870Sstevel@tonic-gate 36880Sstevel@tonic-gate int 36890Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) { 36900Sstevel@tonic-gate if (er_out != NULL) { 36910Sstevel@tonic-gate if (copyout(&er_ext, er_out, sizeof (int))) { 36920Sstevel@tonic-gate return (set_errno(EFAULT)); 36930Sstevel@tonic-gate } 36940Sstevel@tonic-gate } 36950Sstevel@tonic-gate return (set_errno(er_error)); 36960Sstevel@tonic-gate } 36970Sstevel@tonic-gate 36981676Sjpk static int 36991676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi) 37001676Sjpk { 37011676Sjpk ts_label_t *tsl; 37021676Sjpk bslabel_t blab; 37031676Sjpk 37041676Sjpk /* Get label from user */ 37051676Sjpk if (copyin(lab, &blab, sizeof (blab)) != 0) 37061676Sjpk return (EFAULT); 37071676Sjpk tsl = labelalloc(&blab, doi, KM_NOSLEEP); 37081676Sjpk if (tsl == NULL) 37091676Sjpk return (ENOMEM); 37101676Sjpk 37111676Sjpk zone->zone_slabel = tsl; 37121676Sjpk return (0); 37131676Sjpk } 37141676Sjpk 37150Sstevel@tonic-gate /* 3716789Sahrens * Parses a comma-separated list of ZFS datasets into a per-zone dictionary. 3717789Sahrens */ 3718789Sahrens static int 3719789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen) 3720789Sahrens { 3721789Sahrens char *kbuf; 3722789Sahrens char *dataset, *next; 3723789Sahrens zone_dataset_t *zd; 3724789Sahrens size_t len; 3725789Sahrens 3726789Sahrens if (ubuf == NULL || buflen == 0) 3727789Sahrens return (0); 3728789Sahrens 3729789Sahrens if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 3730789Sahrens return (ENOMEM); 3731789Sahrens 3732789Sahrens if (copyin(ubuf, kbuf, buflen) != 0) { 3733789Sahrens kmem_free(kbuf, buflen); 3734789Sahrens return (EFAULT); 3735789Sahrens } 3736789Sahrens 3737789Sahrens dataset = next = kbuf; 3738789Sahrens for (;;) { 3739789Sahrens zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP); 3740789Sahrens 3741789Sahrens next = strchr(dataset, ','); 3742789Sahrens 3743789Sahrens if (next == NULL) 3744789Sahrens len = strlen(dataset); 3745789Sahrens else 3746789Sahrens len = next - dataset; 3747789Sahrens 3748789Sahrens zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP); 3749789Sahrens bcopy(dataset, zd->zd_dataset, len); 3750789Sahrens zd->zd_dataset[len] = '\0'; 3751789Sahrens 3752789Sahrens list_insert_head(&zone->zone_datasets, zd); 3753789Sahrens 3754789Sahrens if (next == NULL) 3755789Sahrens break; 3756789Sahrens 3757789Sahrens dataset = next + 1; 3758789Sahrens } 3759789Sahrens 3760789Sahrens kmem_free(kbuf, buflen); 3761789Sahrens return (0); 3762789Sahrens } 3763789Sahrens 3764789Sahrens /* 37650Sstevel@tonic-gate * System call to create/initialize a new zone named 'zone_name', rooted 37660Sstevel@tonic-gate * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 37671676Sjpk * and initialized with the zone-wide rctls described in 'rctlbuf', and 37681676Sjpk * with labeling set by 'match', 'doi', and 'label'. 37690Sstevel@tonic-gate * 37700Sstevel@tonic-gate * If extended error is non-null, we may use it to return more detailed 37710Sstevel@tonic-gate * error information. 37720Sstevel@tonic-gate */ 37730Sstevel@tonic-gate static zoneid_t 37740Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root, 3775813Sdp const priv_set_t *zone_privs, size_t zone_privssz, 3776813Sdp caddr_t rctlbuf, size_t rctlbufsz, 37771676Sjpk caddr_t zfsbuf, size_t zfsbufsz, int *extended_error, 37783448Sdh155122 int match, uint32_t doi, const bslabel_t *label, 37793448Sdh155122 int flags) 37800Sstevel@tonic-gate { 37810Sstevel@tonic-gate struct zsched_arg zarg; 37820Sstevel@tonic-gate nvlist_t *rctls = NULL; 37830Sstevel@tonic-gate proc_t *pp = curproc; 37840Sstevel@tonic-gate zone_t *zone, *ztmp; 37850Sstevel@tonic-gate zoneid_t zoneid; 37860Sstevel@tonic-gate int error; 37870Sstevel@tonic-gate int error2 = 0; 37880Sstevel@tonic-gate char *str; 37890Sstevel@tonic-gate cred_t *zkcr; 37901769Scarlsonj boolean_t insert_label_hash; 37910Sstevel@tonic-gate 37920Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 37930Sstevel@tonic-gate return (set_errno(EPERM)); 37940Sstevel@tonic-gate 37950Sstevel@tonic-gate /* can't boot zone from within chroot environment */ 37960Sstevel@tonic-gate if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 37970Sstevel@tonic-gate return (zone_create_error(ENOTSUP, ZE_CHROOTED, 3798813Sdp extended_error)); 37990Sstevel@tonic-gate 38000Sstevel@tonic-gate zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 38010Sstevel@tonic-gate zoneid = zone->zone_id = id_alloc(zoneid_space); 38020Sstevel@tonic-gate zone->zone_status = ZONE_IS_UNINITIALIZED; 38030Sstevel@tonic-gate zone->zone_pool = pool_default; 38040Sstevel@tonic-gate zone->zone_pool_mod = gethrtime(); 38050Sstevel@tonic-gate zone->zone_psetid = ZONE_PS_INVAL; 38060Sstevel@tonic-gate zone->zone_ncpus = 0; 38070Sstevel@tonic-gate zone->zone_ncpus_online = 0; 38082712Snn35248 zone->zone_restart_init = B_TRUE; 38092712Snn35248 zone->zone_brand = &native_brand; 38102712Snn35248 zone->zone_initname = NULL; 38110Sstevel@tonic-gate mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 38120Sstevel@tonic-gate mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 38133247Sgjelinek mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 38140Sstevel@tonic-gate cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 38150Sstevel@tonic-gate list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 38160Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 3817789Sahrens list_create(&zone->zone_datasets, sizeof (zone_dataset_t), 3818789Sahrens offsetof(zone_dataset_t, zd_linkage)); 381910616SSebastien.Roy@Sun.COM list_create(&zone->zone_dl_list, sizeof (zone_dl_t), 382010616SSebastien.Roy@Sun.COM offsetof(zone_dl_t, zdl_linkage)); 38211676Sjpk rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 382210910SRobert.Harris@Sun.COM rw_init(&zone->zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL); 38230Sstevel@tonic-gate 38243448Sdh155122 if (flags & ZCF_NET_EXCL) { 38253448Sdh155122 zone->zone_flags |= ZF_NET_EXCL; 38263448Sdh155122 } 38273448Sdh155122 38280Sstevel@tonic-gate if ((error = zone_set_name(zone, zone_name)) != 0) { 38290Sstevel@tonic-gate zone_free(zone); 38300Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 38310Sstevel@tonic-gate } 38320Sstevel@tonic-gate 38330Sstevel@tonic-gate if ((error = zone_set_root(zone, zone_root)) != 0) { 38340Sstevel@tonic-gate zone_free(zone); 38350Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 38360Sstevel@tonic-gate } 3837813Sdp if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) { 38380Sstevel@tonic-gate zone_free(zone); 38390Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 38400Sstevel@tonic-gate } 38410Sstevel@tonic-gate 38420Sstevel@tonic-gate /* initialize node name to be the same as zone name */ 38430Sstevel@tonic-gate zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 38440Sstevel@tonic-gate (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 38450Sstevel@tonic-gate zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 38460Sstevel@tonic-gate 38470Sstevel@tonic-gate zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 38480Sstevel@tonic-gate zone->zone_domain[0] = '\0'; 38498662SJordan.Vaughan@Sun.com zone->zone_hostid = HW_INVALID_HOSTID; 38500Sstevel@tonic-gate zone->zone_shares = 1; 38512677Sml93401 zone->zone_shmmax = 0; 38522677Sml93401 zone->zone_ipc.ipcq_shmmni = 0; 38532677Sml93401 zone->zone_ipc.ipcq_semmni = 0; 38542677Sml93401 zone->zone_ipc.ipcq_msgmni = 0; 38550Sstevel@tonic-gate zone->zone_bootargs = NULL; 38562267Sdp zone->zone_initname = 38572267Sdp kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP); 38582267Sdp (void) strcpy(zone->zone_initname, zone_default_initname); 38593247Sgjelinek zone->zone_nlwps = 0; 38603247Sgjelinek zone->zone_nlwps_ctl = INT_MAX; 38612768Ssl108498 zone->zone_locked_mem = 0; 38622768Ssl108498 zone->zone_locked_mem_ctl = UINT64_MAX; 38633247Sgjelinek zone->zone_max_swap = 0; 38643247Sgjelinek zone->zone_max_swap_ctl = UINT64_MAX; 38653247Sgjelinek zone0.zone_lockedmem_kstat = NULL; 38663247Sgjelinek zone0.zone_swapresv_kstat = NULL; 38670Sstevel@tonic-gate 38680Sstevel@tonic-gate /* 38690Sstevel@tonic-gate * Zsched initializes the rctls. 38700Sstevel@tonic-gate */ 38710Sstevel@tonic-gate zone->zone_rctls = NULL; 38720Sstevel@tonic-gate 38730Sstevel@tonic-gate if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 38740Sstevel@tonic-gate zone_free(zone); 38750Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 38760Sstevel@tonic-gate } 38770Sstevel@tonic-gate 3878789Sahrens if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) { 3879789Sahrens zone_free(zone); 3880789Sahrens return (set_errno(error)); 3881789Sahrens } 3882789Sahrens 38830Sstevel@tonic-gate /* 38841676Sjpk * Read in the trusted system parameters: 38851676Sjpk * match flag and sensitivity label. 38861676Sjpk */ 38871676Sjpk zone->zone_match = match; 38881769Scarlsonj if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 38894462Skp158701 /* Fail if requested to set doi to anything but system's doi */ 38904462Skp158701 if (doi != 0 && doi != default_doi) { 38914462Skp158701 zone_free(zone); 38924462Skp158701 return (set_errno(EINVAL)); 38934462Skp158701 } 38944462Skp158701 /* Always apply system's doi to the zone */ 38954462Skp158701 error = zone_set_label(zone, label, default_doi); 38961676Sjpk if (error != 0) { 38971676Sjpk zone_free(zone); 38981676Sjpk return (set_errno(error)); 38991676Sjpk } 39001769Scarlsonj insert_label_hash = B_TRUE; 39011676Sjpk } else { 39021676Sjpk /* all zones get an admin_low label if system is not labeled */ 39031676Sjpk zone->zone_slabel = l_admin_low; 39041676Sjpk label_hold(l_admin_low); 39051769Scarlsonj insert_label_hash = B_FALSE; 39061676Sjpk } 39071676Sjpk 39081676Sjpk /* 39090Sstevel@tonic-gate * Stop all lwps since that's what normally happens as part of fork(). 39100Sstevel@tonic-gate * This needs to happen before we grab any locks to avoid deadlock 39110Sstevel@tonic-gate * (another lwp in the process could be waiting for the held lock). 39120Sstevel@tonic-gate */ 39130Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 39140Sstevel@tonic-gate zone_free(zone); 39150Sstevel@tonic-gate if (rctls) 39160Sstevel@tonic-gate nvlist_free(rctls); 39170Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 39180Sstevel@tonic-gate } 39190Sstevel@tonic-gate 39200Sstevel@tonic-gate if (block_mounts() == 0) { 39210Sstevel@tonic-gate mutex_enter(&pp->p_lock); 39220Sstevel@tonic-gate if (curthread != pp->p_agenttp) 39230Sstevel@tonic-gate continuelwps(pp); 39240Sstevel@tonic-gate mutex_exit(&pp->p_lock); 39250Sstevel@tonic-gate zone_free(zone); 39260Sstevel@tonic-gate if (rctls) 39270Sstevel@tonic-gate nvlist_free(rctls); 39280Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 39290Sstevel@tonic-gate } 39300Sstevel@tonic-gate 39310Sstevel@tonic-gate /* 39320Sstevel@tonic-gate * Set up credential for kernel access. After this, any errors 39330Sstevel@tonic-gate * should go through the dance in errout rather than calling 39340Sstevel@tonic-gate * zone_free directly. 39350Sstevel@tonic-gate */ 39360Sstevel@tonic-gate zone->zone_kcred = crdup(kcred); 39370Sstevel@tonic-gate crsetzone(zone->zone_kcred, zone); 39380Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 39390Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 39400Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 39410Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 39420Sstevel@tonic-gate 39430Sstevel@tonic-gate mutex_enter(&zonehash_lock); 39440Sstevel@tonic-gate /* 39450Sstevel@tonic-gate * Make sure zone doesn't already exist. 39461676Sjpk * 39471676Sjpk * If the system and zone are labeled, 39481676Sjpk * make sure no other zone exists that has the same label. 39490Sstevel@tonic-gate */ 39501676Sjpk if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL || 39511769Scarlsonj (insert_label_hash && 39521676Sjpk (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) { 39530Sstevel@tonic-gate zone_status_t status; 39540Sstevel@tonic-gate 39550Sstevel@tonic-gate status = zone_status_get(ztmp); 39560Sstevel@tonic-gate if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 39570Sstevel@tonic-gate error = EEXIST; 39580Sstevel@tonic-gate else 39590Sstevel@tonic-gate error = EBUSY; 39604791Ston 39614791Ston if (insert_label_hash) 39624791Ston error2 = ZE_LABELINUSE; 39634791Ston 39640Sstevel@tonic-gate goto errout; 39650Sstevel@tonic-gate } 39660Sstevel@tonic-gate 39670Sstevel@tonic-gate /* 39680Sstevel@tonic-gate * Don't allow zone creations which would cause one zone's rootpath to 39690Sstevel@tonic-gate * be accessible from that of another (non-global) zone. 39700Sstevel@tonic-gate */ 39710Sstevel@tonic-gate if (zone_is_nested(zone->zone_rootpath)) { 39720Sstevel@tonic-gate error = EBUSY; 39730Sstevel@tonic-gate goto errout; 39740Sstevel@tonic-gate } 39750Sstevel@tonic-gate 39760Sstevel@tonic-gate ASSERT(zonecount != 0); /* check for leaks */ 39770Sstevel@tonic-gate if (zonecount + 1 > maxzones) { 39780Sstevel@tonic-gate error = ENOMEM; 39790Sstevel@tonic-gate goto errout; 39800Sstevel@tonic-gate } 39810Sstevel@tonic-gate 39820Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 39830Sstevel@tonic-gate error = EBUSY; 39840Sstevel@tonic-gate error2 = ZE_AREMOUNTS; 39850Sstevel@tonic-gate goto errout; 39860Sstevel@tonic-gate } 39870Sstevel@tonic-gate 39880Sstevel@tonic-gate /* 39890Sstevel@tonic-gate * Zone is still incomplete, but we need to drop all locks while 39900Sstevel@tonic-gate * zsched() initializes this zone's kernel process. We 39910Sstevel@tonic-gate * optimistically add the zone to the hashtable and associated 39920Sstevel@tonic-gate * lists so a parallel zone_create() doesn't try to create the 39930Sstevel@tonic-gate * same zone. 39940Sstevel@tonic-gate */ 39950Sstevel@tonic-gate zonecount++; 39960Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, 39970Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id, 39980Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 39990Sstevel@tonic-gate str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 40000Sstevel@tonic-gate (void) strcpy(str, zone->zone_name); 40010Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 40020Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 40031769Scarlsonj if (insert_label_hash) { 40041676Sjpk (void) mod_hash_insert(zonehashbylabel, 40051676Sjpk (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone); 40061769Scarlsonj zone->zone_flags |= ZF_HASHED_LABEL; 40071676Sjpk } 40081676Sjpk 40090Sstevel@tonic-gate /* 40100Sstevel@tonic-gate * Insert into active list. At this point there are no 'hold's 40110Sstevel@tonic-gate * on the zone, but everyone else knows not to use it, so we can 40120Sstevel@tonic-gate * continue to use it. zsched() will do a zone_hold() if the 40130Sstevel@tonic-gate * newproc() is successful. 40140Sstevel@tonic-gate */ 40150Sstevel@tonic-gate list_insert_tail(&zone_active, zone); 40160Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40170Sstevel@tonic-gate 40180Sstevel@tonic-gate zarg.zone = zone; 40190Sstevel@tonic-gate zarg.nvlist = rctls; 40200Sstevel@tonic-gate /* 40210Sstevel@tonic-gate * The process, task, and project rctls are probably wrong; 40220Sstevel@tonic-gate * we need an interface to get the default values of all rctls, 40230Sstevel@tonic-gate * and initialize zsched appropriately. I'm not sure that that 40240Sstevel@tonic-gate * makes much of a difference, though. 40250Sstevel@tonic-gate */ 4026*11173SJonathan.Adams@Sun.COM error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL, 0); 4027*11173SJonathan.Adams@Sun.COM if (error != 0) { 40280Sstevel@tonic-gate /* 40290Sstevel@tonic-gate * We need to undo all globally visible state. 40300Sstevel@tonic-gate */ 40310Sstevel@tonic-gate mutex_enter(&zonehash_lock); 40320Sstevel@tonic-gate list_remove(&zone_active, zone); 40331769Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) { 40341676Sjpk ASSERT(zone->zone_slabel != NULL); 40351676Sjpk (void) mod_hash_destroy(zonehashbylabel, 40361676Sjpk (mod_hash_key_t)zone->zone_slabel); 40371676Sjpk } 40380Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 40390Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_name); 40400Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 40410Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 40420Sstevel@tonic-gate ASSERT(zonecount > 1); 40430Sstevel@tonic-gate zonecount--; 40440Sstevel@tonic-gate goto errout; 40450Sstevel@tonic-gate } 40460Sstevel@tonic-gate 40470Sstevel@tonic-gate /* 40480Sstevel@tonic-gate * Zone creation can't fail from now on. 40490Sstevel@tonic-gate */ 40500Sstevel@tonic-gate 40510Sstevel@tonic-gate /* 40523247Sgjelinek * Create zone kstats 40533247Sgjelinek */ 40543247Sgjelinek zone_kstat_create(zone); 40553247Sgjelinek 40563247Sgjelinek /* 40570Sstevel@tonic-gate * Let the other lwps continue. 40580Sstevel@tonic-gate */ 40590Sstevel@tonic-gate mutex_enter(&pp->p_lock); 40600Sstevel@tonic-gate if (curthread != pp->p_agenttp) 40610Sstevel@tonic-gate continuelwps(pp); 40620Sstevel@tonic-gate mutex_exit(&pp->p_lock); 40630Sstevel@tonic-gate 40640Sstevel@tonic-gate /* 40650Sstevel@tonic-gate * Wait for zsched to finish initializing the zone. 40660Sstevel@tonic-gate */ 40670Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_READY); 40680Sstevel@tonic-gate /* 40690Sstevel@tonic-gate * The zone is fully visible, so we can let mounts progress. 40700Sstevel@tonic-gate */ 40710Sstevel@tonic-gate resume_mounts(); 40720Sstevel@tonic-gate if (rctls) 40730Sstevel@tonic-gate nvlist_free(rctls); 40740Sstevel@tonic-gate 40750Sstevel@tonic-gate return (zoneid); 40760Sstevel@tonic-gate 40770Sstevel@tonic-gate errout: 40780Sstevel@tonic-gate mutex_exit(&zonehash_lock); 40790Sstevel@tonic-gate /* 40800Sstevel@tonic-gate * Let the other lwps continue. 40810Sstevel@tonic-gate */ 40820Sstevel@tonic-gate mutex_enter(&pp->p_lock); 40830Sstevel@tonic-gate if (curthread != pp->p_agenttp) 40840Sstevel@tonic-gate continuelwps(pp); 40850Sstevel@tonic-gate mutex_exit(&pp->p_lock); 40860Sstevel@tonic-gate 40870Sstevel@tonic-gate resume_mounts(); 40880Sstevel@tonic-gate if (rctls) 40890Sstevel@tonic-gate nvlist_free(rctls); 40900Sstevel@tonic-gate /* 40910Sstevel@tonic-gate * There is currently one reference to the zone, a cred_ref from 40920Sstevel@tonic-gate * zone_kcred. To free the zone, we call crfree, which will call 40930Sstevel@tonic-gate * zone_cred_rele, which will call zone_free. 40940Sstevel@tonic-gate */ 40950Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 40960Sstevel@tonic-gate ASSERT(zone->zone_kcred->cr_ref == 1); 40970Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 40980Sstevel@tonic-gate zkcr = zone->zone_kcred; 40990Sstevel@tonic-gate zone->zone_kcred = NULL; 41000Sstevel@tonic-gate crfree(zkcr); /* triggers call to zone_free */ 41010Sstevel@tonic-gate return (zone_create_error(error, error2, extended_error)); 41020Sstevel@tonic-gate } 41030Sstevel@tonic-gate 41040Sstevel@tonic-gate /* 41050Sstevel@tonic-gate * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 41062267Sdp * the heavy lifting. initname is the path to the program to launch 41072267Sdp * at the "top" of the zone; if this is NULL, we use the system default, 41082267Sdp * which is stored at zone_default_initname. 41090Sstevel@tonic-gate */ 41100Sstevel@tonic-gate static int 41112267Sdp zone_boot(zoneid_t zoneid) 41120Sstevel@tonic-gate { 41130Sstevel@tonic-gate int err; 41140Sstevel@tonic-gate zone_t *zone; 41150Sstevel@tonic-gate 41160Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 41170Sstevel@tonic-gate return (set_errno(EPERM)); 41180Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 41190Sstevel@tonic-gate return (set_errno(EINVAL)); 41200Sstevel@tonic-gate 41210Sstevel@tonic-gate mutex_enter(&zonehash_lock); 41220Sstevel@tonic-gate /* 41230Sstevel@tonic-gate * Look for zone under hash lock to prevent races with calls to 41240Sstevel@tonic-gate * zone_shutdown, zone_destroy, etc. 41250Sstevel@tonic-gate */ 41260Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 41270Sstevel@tonic-gate mutex_exit(&zonehash_lock); 41280Sstevel@tonic-gate return (set_errno(EINVAL)); 41290Sstevel@tonic-gate } 41300Sstevel@tonic-gate 41310Sstevel@tonic-gate mutex_enter(&zone_status_lock); 41320Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_READY) { 41330Sstevel@tonic-gate mutex_exit(&zone_status_lock); 41340Sstevel@tonic-gate mutex_exit(&zonehash_lock); 41350Sstevel@tonic-gate return (set_errno(EINVAL)); 41360Sstevel@tonic-gate } 41370Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_BOOTING); 41380Sstevel@tonic-gate mutex_exit(&zone_status_lock); 41390Sstevel@tonic-gate 41400Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 41410Sstevel@tonic-gate mutex_exit(&zonehash_lock); 41420Sstevel@tonic-gate 41430Sstevel@tonic-gate if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 41440Sstevel@tonic-gate zone_rele(zone); 41450Sstevel@tonic-gate return (set_errno(EINTR)); 41460Sstevel@tonic-gate } 41470Sstevel@tonic-gate 41480Sstevel@tonic-gate /* 41490Sstevel@tonic-gate * Boot (starting init) might have failed, in which case the zone 41500Sstevel@tonic-gate * will go to the SHUTTING_DOWN state; an appropriate errno will 41510Sstevel@tonic-gate * be placed in zone->zone_boot_err, and so we return that. 41520Sstevel@tonic-gate */ 41530Sstevel@tonic-gate err = zone->zone_boot_err; 41540Sstevel@tonic-gate zone_rele(zone); 41550Sstevel@tonic-gate return (err ? set_errno(err) : 0); 41560Sstevel@tonic-gate } 41570Sstevel@tonic-gate 41580Sstevel@tonic-gate /* 41590Sstevel@tonic-gate * Kills all user processes in the zone, waiting for them all to exit 41600Sstevel@tonic-gate * before returning. 41610Sstevel@tonic-gate */ 41620Sstevel@tonic-gate static int 41630Sstevel@tonic-gate zone_empty(zone_t *zone) 41640Sstevel@tonic-gate { 41650Sstevel@tonic-gate int waitstatus; 41660Sstevel@tonic-gate 41670Sstevel@tonic-gate /* 41680Sstevel@tonic-gate * We need to drop zonehash_lock before killing all 41690Sstevel@tonic-gate * processes, otherwise we'll deadlock with zone_find_* 41700Sstevel@tonic-gate * which can be called from the exit path. 41710Sstevel@tonic-gate */ 41720Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 417311066Srafael.vanoni@sun.com while ((waitstatus = zone_status_timedwait_sig(zone, 417411066Srafael.vanoni@sun.com ddi_get_lbolt() + hz, ZONE_IS_EMPTY)) == -1) { 41750Sstevel@tonic-gate killall(zone->zone_id); 41760Sstevel@tonic-gate } 41770Sstevel@tonic-gate /* 41780Sstevel@tonic-gate * return EINTR if we were signaled 41790Sstevel@tonic-gate */ 41800Sstevel@tonic-gate if (waitstatus == 0) 41810Sstevel@tonic-gate return (EINTR); 41820Sstevel@tonic-gate return (0); 41830Sstevel@tonic-gate } 41840Sstevel@tonic-gate 41850Sstevel@tonic-gate /* 41861676Sjpk * This function implements the policy for zone visibility. 41871676Sjpk * 41881676Sjpk * In standard Solaris, a non-global zone can only see itself. 41891676Sjpk * 41901676Sjpk * In Trusted Extensions, a labeled zone can lookup any zone whose label 41911676Sjpk * it dominates. For this test, the label of the global zone is treated as 41921676Sjpk * admin_high so it is special-cased instead of being checked for dominance. 41931676Sjpk * 41941676Sjpk * Returns true if zone attributes are viewable, false otherwise. 41951676Sjpk */ 41961676Sjpk static boolean_t 41971676Sjpk zone_list_access(zone_t *zone) 41981676Sjpk { 41991676Sjpk 42001676Sjpk if (curproc->p_zone == global_zone || 42011676Sjpk curproc->p_zone == zone) { 42021676Sjpk return (B_TRUE); 42031769Scarlsonj } else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 42041676Sjpk bslabel_t *curproc_label; 42051676Sjpk bslabel_t *zone_label; 42061676Sjpk 42071676Sjpk curproc_label = label2bslabel(curproc->p_zone->zone_slabel); 42081676Sjpk zone_label = label2bslabel(zone->zone_slabel); 42091676Sjpk 42101676Sjpk if (zone->zone_id != GLOBAL_ZONEID && 42111676Sjpk bldominates(curproc_label, zone_label)) { 42121676Sjpk return (B_TRUE); 42131676Sjpk } else { 42141676Sjpk return (B_FALSE); 42151676Sjpk } 42161676Sjpk } else { 42171676Sjpk return (B_FALSE); 42181676Sjpk } 42191676Sjpk } 42201676Sjpk 42211676Sjpk /* 42220Sstevel@tonic-gate * Systemcall to start the zone's halt sequence. By the time this 42230Sstevel@tonic-gate * function successfully returns, all user processes and kernel threads 42240Sstevel@tonic-gate * executing in it will have exited, ZSD shutdown callbacks executed, 42250Sstevel@tonic-gate * and the zone status set to ZONE_IS_DOWN. 42260Sstevel@tonic-gate * 42270Sstevel@tonic-gate * It is possible that the call will interrupt itself if the caller is the 42280Sstevel@tonic-gate * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 42290Sstevel@tonic-gate */ 42300Sstevel@tonic-gate static int 42310Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 42320Sstevel@tonic-gate { 42330Sstevel@tonic-gate int error; 42340Sstevel@tonic-gate zone_t *zone; 42350Sstevel@tonic-gate zone_status_t status; 42360Sstevel@tonic-gate 42370Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 42380Sstevel@tonic-gate return (set_errno(EPERM)); 42390Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 42400Sstevel@tonic-gate return (set_errno(EINVAL)); 42410Sstevel@tonic-gate 42420Sstevel@tonic-gate /* 42430Sstevel@tonic-gate * Block mounts so that VFS_MOUNT() can get an accurate view of 42440Sstevel@tonic-gate * the zone's status with regards to ZONE_IS_SHUTTING down. 42450Sstevel@tonic-gate * 42460Sstevel@tonic-gate * e.g. NFS can fail the mount if it determines that the zone 42470Sstevel@tonic-gate * has already begun the shutdown sequence. 42480Sstevel@tonic-gate */ 42490Sstevel@tonic-gate if (block_mounts() == 0) 42500Sstevel@tonic-gate return (set_errno(EINTR)); 42510Sstevel@tonic-gate mutex_enter(&zonehash_lock); 42520Sstevel@tonic-gate /* 42530Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 42540Sstevel@tonic-gate * calls to zone_shutdown and zone_destroy. 42550Sstevel@tonic-gate */ 42560Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 42570Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42580Sstevel@tonic-gate resume_mounts(); 42590Sstevel@tonic-gate return (set_errno(EINVAL)); 42600Sstevel@tonic-gate } 42610Sstevel@tonic-gate mutex_enter(&zone_status_lock); 42620Sstevel@tonic-gate status = zone_status_get(zone); 42630Sstevel@tonic-gate /* 42640Sstevel@tonic-gate * Fail if the zone isn't fully initialized yet. 42650Sstevel@tonic-gate */ 42660Sstevel@tonic-gate if (status < ZONE_IS_READY) { 42670Sstevel@tonic-gate mutex_exit(&zone_status_lock); 42680Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42690Sstevel@tonic-gate resume_mounts(); 42700Sstevel@tonic-gate return (set_errno(EINVAL)); 42710Sstevel@tonic-gate } 42720Sstevel@tonic-gate /* 42730Sstevel@tonic-gate * If conditions required for zone_shutdown() to return have been met, 42740Sstevel@tonic-gate * return success. 42750Sstevel@tonic-gate */ 42760Sstevel@tonic-gate if (status >= ZONE_IS_DOWN) { 42770Sstevel@tonic-gate mutex_exit(&zone_status_lock); 42780Sstevel@tonic-gate mutex_exit(&zonehash_lock); 42790Sstevel@tonic-gate resume_mounts(); 42800Sstevel@tonic-gate return (0); 42810Sstevel@tonic-gate } 42820Sstevel@tonic-gate /* 42830Sstevel@tonic-gate * If zone_shutdown() hasn't been called before, go through the motions. 42840Sstevel@tonic-gate * If it has, there's nothing to do but wait for the kernel threads to 42850Sstevel@tonic-gate * drain. 42860Sstevel@tonic-gate */ 42870Sstevel@tonic-gate if (status < ZONE_IS_EMPTY) { 42880Sstevel@tonic-gate uint_t ntasks; 42890Sstevel@tonic-gate 42900Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 42910Sstevel@tonic-gate if ((ntasks = zone->zone_ntasks) != 1) { 42920Sstevel@tonic-gate /* 42930Sstevel@tonic-gate * There's still stuff running. 42940Sstevel@tonic-gate */ 42950Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 42960Sstevel@tonic-gate } 42970Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 42980Sstevel@tonic-gate if (ntasks == 1) { 42990Sstevel@tonic-gate /* 43000Sstevel@tonic-gate * The only way to create another task is through 43010Sstevel@tonic-gate * zone_enter(), which will block until we drop 43020Sstevel@tonic-gate * zonehash_lock. The zone is empty. 43030Sstevel@tonic-gate */ 43040Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 43050Sstevel@tonic-gate /* 43060Sstevel@tonic-gate * Skip ahead to ZONE_IS_DOWN 43070Sstevel@tonic-gate */ 43080Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 43090Sstevel@tonic-gate } else { 43100Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 43110Sstevel@tonic-gate } 43120Sstevel@tonic-gate } 43130Sstevel@tonic-gate } 43140Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 43150Sstevel@tonic-gate mutex_exit(&zone_status_lock); 43160Sstevel@tonic-gate mutex_exit(&zonehash_lock); 43170Sstevel@tonic-gate resume_mounts(); 43180Sstevel@tonic-gate 43190Sstevel@tonic-gate if (error = zone_empty(zone)) { 43200Sstevel@tonic-gate zone_rele(zone); 43210Sstevel@tonic-gate return (set_errno(error)); 43220Sstevel@tonic-gate } 43230Sstevel@tonic-gate /* 43240Sstevel@tonic-gate * After the zone status goes to ZONE_IS_DOWN this zone will no 43250Sstevel@tonic-gate * longer be notified of changes to the pools configuration, so 43260Sstevel@tonic-gate * in order to not end up with a stale pool pointer, we point 43270Sstevel@tonic-gate * ourselves at the default pool and remove all resource 43280Sstevel@tonic-gate * visibility. This is especially important as the zone_t may 43290Sstevel@tonic-gate * languish on the deathrow for a very long time waiting for 43300Sstevel@tonic-gate * cred's to drain out. 43310Sstevel@tonic-gate * 43320Sstevel@tonic-gate * This rebinding of the zone can happen multiple times 43330Sstevel@tonic-gate * (presumably due to interrupted or parallel systemcalls) 43340Sstevel@tonic-gate * without any adverse effects. 43350Sstevel@tonic-gate */ 43360Sstevel@tonic-gate if (pool_lock_intr() != 0) { 43370Sstevel@tonic-gate zone_rele(zone); 43380Sstevel@tonic-gate return (set_errno(EINTR)); 43390Sstevel@tonic-gate } 43400Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 43410Sstevel@tonic-gate mutex_enter(&cpu_lock); 43420Sstevel@tonic-gate zone_pool_set(zone, pool_default); 43430Sstevel@tonic-gate /* 43440Sstevel@tonic-gate * The zone no longer needs to be able to see any cpus. 43450Sstevel@tonic-gate */ 43460Sstevel@tonic-gate zone_pset_set(zone, ZONE_PS_INVAL); 43470Sstevel@tonic-gate mutex_exit(&cpu_lock); 43480Sstevel@tonic-gate } 43490Sstevel@tonic-gate pool_unlock(); 43500Sstevel@tonic-gate 43510Sstevel@tonic-gate /* 43520Sstevel@tonic-gate * ZSD shutdown callbacks can be executed multiple times, hence 43530Sstevel@tonic-gate * it is safe to not be holding any locks across this call. 43540Sstevel@tonic-gate */ 43550Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 43560Sstevel@tonic-gate 43570Sstevel@tonic-gate mutex_enter(&zone_status_lock); 43580Sstevel@tonic-gate if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 43590Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 43600Sstevel@tonic-gate mutex_exit(&zone_status_lock); 43610Sstevel@tonic-gate 43620Sstevel@tonic-gate /* 43630Sstevel@tonic-gate * Wait for kernel threads to drain. 43640Sstevel@tonic-gate */ 43650Sstevel@tonic-gate if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 43660Sstevel@tonic-gate zone_rele(zone); 43670Sstevel@tonic-gate return (set_errno(EINTR)); 43680Sstevel@tonic-gate } 43692712Snn35248 43703671Ssl108498 /* 43713671Ssl108498 * Zone can be become down/destroyable even if the above wait 43723671Ssl108498 * returns EINTR, so any code added here may never execute. 43733671Ssl108498 * (i.e. don't add code here) 43743671Ssl108498 */ 43752712Snn35248 43760Sstevel@tonic-gate zone_rele(zone); 43770Sstevel@tonic-gate return (0); 43780Sstevel@tonic-gate } 43790Sstevel@tonic-gate 43800Sstevel@tonic-gate /* 43810Sstevel@tonic-gate * Systemcall entry point to finalize the zone halt process. The caller 43822677Sml93401 * must have already successfully called zone_shutdown(). 43830Sstevel@tonic-gate * 43840Sstevel@tonic-gate * Upon successful completion, the zone will have been fully destroyed: 43850Sstevel@tonic-gate * zsched will have exited, destructor callbacks executed, and the zone 43860Sstevel@tonic-gate * removed from the list of active zones. 43870Sstevel@tonic-gate */ 43880Sstevel@tonic-gate static int 43890Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 43900Sstevel@tonic-gate { 43910Sstevel@tonic-gate uint64_t uniqid; 43920Sstevel@tonic-gate zone_t *zone; 43930Sstevel@tonic-gate zone_status_t status; 43940Sstevel@tonic-gate 43950Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 43960Sstevel@tonic-gate return (set_errno(EPERM)); 43970Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 43980Sstevel@tonic-gate return (set_errno(EINVAL)); 43990Sstevel@tonic-gate 44000Sstevel@tonic-gate mutex_enter(&zonehash_lock); 44010Sstevel@tonic-gate /* 44020Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 44030Sstevel@tonic-gate * calls to zone_destroy. 44040Sstevel@tonic-gate */ 44050Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 44060Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44070Sstevel@tonic-gate return (set_errno(EINVAL)); 44080Sstevel@tonic-gate } 44090Sstevel@tonic-gate 44100Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 44110Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44120Sstevel@tonic-gate return (set_errno(EBUSY)); 44130Sstevel@tonic-gate } 44140Sstevel@tonic-gate mutex_enter(&zone_status_lock); 44150Sstevel@tonic-gate status = zone_status_get(zone); 44160Sstevel@tonic-gate if (status < ZONE_IS_DOWN) { 44170Sstevel@tonic-gate mutex_exit(&zone_status_lock); 44180Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44190Sstevel@tonic-gate return (set_errno(EBUSY)); 44200Sstevel@tonic-gate } else if (status == ZONE_IS_DOWN) { 44210Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 44220Sstevel@tonic-gate } 44230Sstevel@tonic-gate mutex_exit(&zone_status_lock); 44240Sstevel@tonic-gate zone_hold(zone); 44250Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44260Sstevel@tonic-gate 44270Sstevel@tonic-gate /* 44280Sstevel@tonic-gate * wait for zsched to exit 44290Sstevel@tonic-gate */ 44300Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_DEAD); 44310Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_DESTROY); 44323448Sdh155122 zone->zone_netstack = NULL; 44330Sstevel@tonic-gate uniqid = zone->zone_uniqid; 44340Sstevel@tonic-gate zone_rele(zone); 44350Sstevel@tonic-gate zone = NULL; /* potentially free'd */ 44360Sstevel@tonic-gate 44370Sstevel@tonic-gate mutex_enter(&zonehash_lock); 44380Sstevel@tonic-gate for (; /* ever */; ) { 44390Sstevel@tonic-gate boolean_t unref; 44400Sstevel@tonic-gate 44410Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL || 44420Sstevel@tonic-gate zone->zone_uniqid != uniqid) { 44430Sstevel@tonic-gate /* 44440Sstevel@tonic-gate * The zone has gone away. Necessary conditions 44450Sstevel@tonic-gate * are met, so we return success. 44460Sstevel@tonic-gate */ 44470Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44480Sstevel@tonic-gate return (0); 44490Sstevel@tonic-gate } 44500Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 44510Sstevel@tonic-gate unref = ZONE_IS_UNREF(zone); 44520Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 44530Sstevel@tonic-gate if (unref) { 44540Sstevel@tonic-gate /* 44550Sstevel@tonic-gate * There is only one reference to the zone -- that 44560Sstevel@tonic-gate * added when the zone was added to the hashtables -- 44570Sstevel@tonic-gate * and things will remain this way until we drop 44580Sstevel@tonic-gate * zonehash_lock... we can go ahead and cleanup the 44590Sstevel@tonic-gate * zone. 44600Sstevel@tonic-gate */ 44610Sstevel@tonic-gate break; 44620Sstevel@tonic-gate } 44630Sstevel@tonic-gate 44640Sstevel@tonic-gate if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 44650Sstevel@tonic-gate /* Signaled */ 44660Sstevel@tonic-gate mutex_exit(&zonehash_lock); 44670Sstevel@tonic-gate return (set_errno(EINTR)); 44680Sstevel@tonic-gate } 44690Sstevel@tonic-gate 44700Sstevel@tonic-gate } 44710Sstevel@tonic-gate 44723792Sakolb /* 44733792Sakolb * Remove CPU cap for this zone now since we're not going to 44743792Sakolb * fail below this point. 44753792Sakolb */ 44763792Sakolb cpucaps_zone_remove(zone); 44773792Sakolb 44783792Sakolb /* Get rid of the zone's kstats */ 44793247Sgjelinek zone_kstat_delete(zone); 44803247Sgjelinek 44814888Seh208807 /* free brand specific data */ 44824888Seh208807 if (ZONE_IS_BRANDED(zone)) 44834888Seh208807 ZBROP(zone)->b_free_brand_data(zone); 44844888Seh208807 44853671Ssl108498 /* Say goodbye to brand framework. */ 44863671Ssl108498 brand_unregister_zone(zone->zone_brand); 44873671Ssl108498 44880Sstevel@tonic-gate /* 44890Sstevel@tonic-gate * It is now safe to let the zone be recreated; remove it from the 44900Sstevel@tonic-gate * lists. The memory will not be freed until the last cred 44910Sstevel@tonic-gate * reference goes away. 44920Sstevel@tonic-gate */ 44930Sstevel@tonic-gate ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 44940Sstevel@tonic-gate zonecount--; 44950Sstevel@tonic-gate /* remove from active list and hash tables */ 44960Sstevel@tonic-gate list_remove(&zone_active, zone); 44970Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 44980Sstevel@tonic-gate (mod_hash_key_t)zone->zone_name); 44990Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 45000Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 45011769Scarlsonj if (zone->zone_flags & ZF_HASHED_LABEL) 45021676Sjpk (void) mod_hash_destroy(zonehashbylabel, 45031676Sjpk (mod_hash_key_t)zone->zone_slabel); 45040Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45050Sstevel@tonic-gate 4506766Scarlsonj /* 4507766Scarlsonj * Release the root vnode; we're not using it anymore. Nor should any 4508766Scarlsonj * other thread that might access it exist. 4509766Scarlsonj */ 4510766Scarlsonj if (zone->zone_rootvp != NULL) { 4511766Scarlsonj VN_RELE(zone->zone_rootvp); 4512766Scarlsonj zone->zone_rootvp = NULL; 4513766Scarlsonj } 4514766Scarlsonj 45150Sstevel@tonic-gate /* add to deathrow list */ 45160Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 45170Sstevel@tonic-gate list_insert_tail(&zone_deathrow, zone); 45180Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 45190Sstevel@tonic-gate 45200Sstevel@tonic-gate /* 45210Sstevel@tonic-gate * Drop last reference (which was added by zsched()), this will 45220Sstevel@tonic-gate * free the zone unless there are outstanding cred references. 45230Sstevel@tonic-gate */ 45240Sstevel@tonic-gate zone_rele(zone); 45250Sstevel@tonic-gate return (0); 45260Sstevel@tonic-gate } 45270Sstevel@tonic-gate 45280Sstevel@tonic-gate /* 45290Sstevel@tonic-gate * Systemcall entry point for zone_getattr(2). 45300Sstevel@tonic-gate */ 45310Sstevel@tonic-gate static ssize_t 45320Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 45330Sstevel@tonic-gate { 45340Sstevel@tonic-gate size_t size; 45350Sstevel@tonic-gate int error = 0, err; 45360Sstevel@tonic-gate zone_t *zone; 45370Sstevel@tonic-gate char *zonepath; 45382267Sdp char *outstr; 45390Sstevel@tonic-gate zone_status_t zone_status; 45400Sstevel@tonic-gate pid_t initpid; 45413792Sakolb boolean_t global = (curzone == global_zone); 45423792Sakolb boolean_t inzone = (curzone->zone_id == zoneid); 45433448Sdh155122 ushort_t flags; 45440Sstevel@tonic-gate 45450Sstevel@tonic-gate mutex_enter(&zonehash_lock); 45460Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 45470Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45480Sstevel@tonic-gate return (set_errno(EINVAL)); 45490Sstevel@tonic-gate } 45500Sstevel@tonic-gate zone_status = zone_status_get(zone); 45515880Snordmark if (zone_status < ZONE_IS_INITIALIZED) { 45520Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45530Sstevel@tonic-gate return (set_errno(EINVAL)); 45540Sstevel@tonic-gate } 45550Sstevel@tonic-gate zone_hold(zone); 45560Sstevel@tonic-gate mutex_exit(&zonehash_lock); 45570Sstevel@tonic-gate 45580Sstevel@tonic-gate /* 45591676Sjpk * If not in the global zone, don't show information about other zones, 45601676Sjpk * unless the system is labeled and the local zone's label dominates 45611676Sjpk * the other zone. 45620Sstevel@tonic-gate */ 45631676Sjpk if (!zone_list_access(zone)) { 45640Sstevel@tonic-gate zone_rele(zone); 45650Sstevel@tonic-gate return (set_errno(EINVAL)); 45660Sstevel@tonic-gate } 45670Sstevel@tonic-gate 45680Sstevel@tonic-gate switch (attr) { 45690Sstevel@tonic-gate case ZONE_ATTR_ROOT: 45700Sstevel@tonic-gate if (global) { 45710Sstevel@tonic-gate /* 45720Sstevel@tonic-gate * Copy the path to trim the trailing "/" (except for 45730Sstevel@tonic-gate * the global zone). 45740Sstevel@tonic-gate */ 45750Sstevel@tonic-gate if (zone != global_zone) 45760Sstevel@tonic-gate size = zone->zone_rootpathlen - 1; 45770Sstevel@tonic-gate else 45780Sstevel@tonic-gate size = zone->zone_rootpathlen; 45790Sstevel@tonic-gate zonepath = kmem_alloc(size, KM_SLEEP); 45800Sstevel@tonic-gate bcopy(zone->zone_rootpath, zonepath, size); 45810Sstevel@tonic-gate zonepath[size - 1] = '\0'; 45820Sstevel@tonic-gate } else { 45833792Sakolb if (inzone || !is_system_labeled()) { 45841676Sjpk /* 45851676Sjpk * Caller is not in the global zone. 45861676Sjpk * if the query is on the current zone 45871676Sjpk * or the system is not labeled, 45881676Sjpk * just return faked-up path for current zone. 45891676Sjpk */ 45901676Sjpk zonepath = "/"; 45911676Sjpk size = 2; 45921676Sjpk } else { 45931676Sjpk /* 45941676Sjpk * Return related path for current zone. 45951676Sjpk */ 45961676Sjpk int prefix_len = strlen(zone_prefix); 45971676Sjpk int zname_len = strlen(zone->zone_name); 45981676Sjpk 45991676Sjpk size = prefix_len + zname_len + 1; 46001676Sjpk zonepath = kmem_alloc(size, KM_SLEEP); 46011676Sjpk bcopy(zone_prefix, zonepath, prefix_len); 46021676Sjpk bcopy(zone->zone_name, zonepath + 46032267Sdp prefix_len, zname_len); 46041676Sjpk zonepath[size - 1] = '\0'; 46051676Sjpk } 46060Sstevel@tonic-gate } 46070Sstevel@tonic-gate if (bufsize > size) 46080Sstevel@tonic-gate bufsize = size; 46090Sstevel@tonic-gate if (buf != NULL) { 46100Sstevel@tonic-gate err = copyoutstr(zonepath, buf, bufsize, NULL); 46110Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 46120Sstevel@tonic-gate error = EFAULT; 46130Sstevel@tonic-gate } 46143792Sakolb if (global || (is_system_labeled() && !inzone)) 46150Sstevel@tonic-gate kmem_free(zonepath, size); 46160Sstevel@tonic-gate break; 46170Sstevel@tonic-gate 46180Sstevel@tonic-gate case ZONE_ATTR_NAME: 46190Sstevel@tonic-gate size = strlen(zone->zone_name) + 1; 46200Sstevel@tonic-gate if (bufsize > size) 46210Sstevel@tonic-gate bufsize = size; 46220Sstevel@tonic-gate if (buf != NULL) { 46230Sstevel@tonic-gate err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 46240Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 46250Sstevel@tonic-gate error = EFAULT; 46260Sstevel@tonic-gate } 46270Sstevel@tonic-gate break; 46280Sstevel@tonic-gate 46290Sstevel@tonic-gate case ZONE_ATTR_STATUS: 46300Sstevel@tonic-gate /* 46310Sstevel@tonic-gate * Since we're not holding zonehash_lock, the zone status 46320Sstevel@tonic-gate * may be anything; leave it up to userland to sort it out. 46330Sstevel@tonic-gate */ 46340Sstevel@tonic-gate size = sizeof (zone_status); 46350Sstevel@tonic-gate if (bufsize > size) 46360Sstevel@tonic-gate bufsize = size; 46370Sstevel@tonic-gate zone_status = zone_status_get(zone); 46380Sstevel@tonic-gate if (buf != NULL && 46390Sstevel@tonic-gate copyout(&zone_status, buf, bufsize) != 0) 46400Sstevel@tonic-gate error = EFAULT; 46410Sstevel@tonic-gate break; 46423448Sdh155122 case ZONE_ATTR_FLAGS: 46433448Sdh155122 size = sizeof (zone->zone_flags); 46443448Sdh155122 if (bufsize > size) 46453448Sdh155122 bufsize = size; 46463448Sdh155122 flags = zone->zone_flags; 46473448Sdh155122 if (buf != NULL && 46483448Sdh155122 copyout(&flags, buf, bufsize) != 0) 46493448Sdh155122 error = EFAULT; 46503448Sdh155122 break; 46510Sstevel@tonic-gate case ZONE_ATTR_PRIVSET: 46520Sstevel@tonic-gate size = sizeof (priv_set_t); 46530Sstevel@tonic-gate if (bufsize > size) 46540Sstevel@tonic-gate bufsize = size; 46550Sstevel@tonic-gate if (buf != NULL && 46560Sstevel@tonic-gate copyout(zone->zone_privset, buf, bufsize) != 0) 46570Sstevel@tonic-gate error = EFAULT; 46580Sstevel@tonic-gate break; 46590Sstevel@tonic-gate case ZONE_ATTR_UNIQID: 46600Sstevel@tonic-gate size = sizeof (zone->zone_uniqid); 46610Sstevel@tonic-gate if (bufsize > size) 46620Sstevel@tonic-gate bufsize = size; 46630Sstevel@tonic-gate if (buf != NULL && 46640Sstevel@tonic-gate copyout(&zone->zone_uniqid, buf, bufsize) != 0) 46650Sstevel@tonic-gate error = EFAULT; 46660Sstevel@tonic-gate break; 46670Sstevel@tonic-gate case ZONE_ATTR_POOLID: 46680Sstevel@tonic-gate { 46690Sstevel@tonic-gate pool_t *pool; 46700Sstevel@tonic-gate poolid_t poolid; 46710Sstevel@tonic-gate 46720Sstevel@tonic-gate if (pool_lock_intr() != 0) { 46730Sstevel@tonic-gate error = EINTR; 46740Sstevel@tonic-gate break; 46750Sstevel@tonic-gate } 46760Sstevel@tonic-gate pool = zone_pool_get(zone); 46770Sstevel@tonic-gate poolid = pool->pool_id; 46780Sstevel@tonic-gate pool_unlock(); 46790Sstevel@tonic-gate size = sizeof (poolid); 46800Sstevel@tonic-gate if (bufsize > size) 46810Sstevel@tonic-gate bufsize = size; 46820Sstevel@tonic-gate if (buf != NULL && copyout(&poolid, buf, size) != 0) 46830Sstevel@tonic-gate error = EFAULT; 46840Sstevel@tonic-gate } 46850Sstevel@tonic-gate break; 46861676Sjpk case ZONE_ATTR_SLBL: 46871676Sjpk size = sizeof (bslabel_t); 46881676Sjpk if (bufsize > size) 46891676Sjpk bufsize = size; 46901676Sjpk if (zone->zone_slabel == NULL) 46911676Sjpk error = EINVAL; 46921676Sjpk else if (buf != NULL && 46931676Sjpk copyout(label2bslabel(zone->zone_slabel), buf, 46941676Sjpk bufsize) != 0) 46951676Sjpk error = EFAULT; 46961676Sjpk break; 46970Sstevel@tonic-gate case ZONE_ATTR_INITPID: 46980Sstevel@tonic-gate size = sizeof (initpid); 46990Sstevel@tonic-gate if (bufsize > size) 47000Sstevel@tonic-gate bufsize = size; 47010Sstevel@tonic-gate initpid = zone->zone_proc_initpid; 47020Sstevel@tonic-gate if (initpid == -1) { 47030Sstevel@tonic-gate error = ESRCH; 47040Sstevel@tonic-gate break; 47050Sstevel@tonic-gate } 47060Sstevel@tonic-gate if (buf != NULL && 47070Sstevel@tonic-gate copyout(&initpid, buf, bufsize) != 0) 47080Sstevel@tonic-gate error = EFAULT; 47090Sstevel@tonic-gate break; 47102712Snn35248 case ZONE_ATTR_BRAND: 47112712Snn35248 size = strlen(zone->zone_brand->b_name) + 1; 47122712Snn35248 47132712Snn35248 if (bufsize > size) 47142712Snn35248 bufsize = size; 47152712Snn35248 if (buf != NULL) { 47162712Snn35248 err = copyoutstr(zone->zone_brand->b_name, buf, 47172712Snn35248 bufsize, NULL); 47182712Snn35248 if (err != 0 && err != ENAMETOOLONG) 47192712Snn35248 error = EFAULT; 47202712Snn35248 } 47212712Snn35248 break; 47222267Sdp case ZONE_ATTR_INITNAME: 47232267Sdp size = strlen(zone->zone_initname) + 1; 47242267Sdp if (bufsize > size) 47252267Sdp bufsize = size; 47262267Sdp if (buf != NULL) { 47272267Sdp err = copyoutstr(zone->zone_initname, buf, bufsize, 47282267Sdp NULL); 47292267Sdp if (err != 0 && err != ENAMETOOLONG) 47302267Sdp error = EFAULT; 47312267Sdp } 47322267Sdp break; 47332267Sdp case ZONE_ATTR_BOOTARGS: 47342267Sdp if (zone->zone_bootargs == NULL) 47352267Sdp outstr = ""; 47362267Sdp else 47372267Sdp outstr = zone->zone_bootargs; 47382267Sdp size = strlen(outstr) + 1; 47392267Sdp if (bufsize > size) 47402267Sdp bufsize = size; 47412267Sdp if (buf != NULL) { 47422267Sdp err = copyoutstr(outstr, buf, bufsize, NULL); 47432267Sdp if (err != 0 && err != ENAMETOOLONG) 47442267Sdp error = EFAULT; 47452267Sdp } 47462267Sdp break; 47473247Sgjelinek case ZONE_ATTR_PHYS_MCAP: 47483247Sgjelinek size = sizeof (zone->zone_phys_mcap); 47493247Sgjelinek if (bufsize > size) 47503247Sgjelinek bufsize = size; 47513247Sgjelinek if (buf != NULL && 47523247Sgjelinek copyout(&zone->zone_phys_mcap, buf, bufsize) != 0) 47533247Sgjelinek error = EFAULT; 47543247Sgjelinek break; 47553247Sgjelinek case ZONE_ATTR_SCHED_CLASS: 47563247Sgjelinek mutex_enter(&class_lock); 47573247Sgjelinek 47583247Sgjelinek if (zone->zone_defaultcid >= loaded_classes) 47593247Sgjelinek outstr = ""; 47603247Sgjelinek else 47613247Sgjelinek outstr = sclass[zone->zone_defaultcid].cl_name; 47623247Sgjelinek size = strlen(outstr) + 1; 47633247Sgjelinek if (bufsize > size) 47643247Sgjelinek bufsize = size; 47653247Sgjelinek if (buf != NULL) { 47663247Sgjelinek err = copyoutstr(outstr, buf, bufsize, NULL); 47673247Sgjelinek if (err != 0 && err != ENAMETOOLONG) 47683247Sgjelinek error = EFAULT; 47693247Sgjelinek } 47703247Sgjelinek 47713247Sgjelinek mutex_exit(&class_lock); 47723247Sgjelinek break; 47738662SJordan.Vaughan@Sun.com case ZONE_ATTR_HOSTID: 47748662SJordan.Vaughan@Sun.com if (zone->zone_hostid != HW_INVALID_HOSTID && 47758662SJordan.Vaughan@Sun.com bufsize == sizeof (zone->zone_hostid)) { 47768662SJordan.Vaughan@Sun.com size = sizeof (zone->zone_hostid); 47778662SJordan.Vaughan@Sun.com if (buf != NULL && copyout(&zone->zone_hostid, buf, 47788662SJordan.Vaughan@Sun.com bufsize) != 0) 47798662SJordan.Vaughan@Sun.com error = EFAULT; 47808662SJordan.Vaughan@Sun.com } else { 47818662SJordan.Vaughan@Sun.com error = EINVAL; 47828662SJordan.Vaughan@Sun.com } 47838662SJordan.Vaughan@Sun.com break; 47840Sstevel@tonic-gate default: 47852712Snn35248 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) { 47862712Snn35248 size = bufsize; 47872712Snn35248 error = ZBROP(zone)->b_getattr(zone, attr, buf, &size); 47882712Snn35248 } else { 47892712Snn35248 error = EINVAL; 47902712Snn35248 } 47910Sstevel@tonic-gate } 47920Sstevel@tonic-gate zone_rele(zone); 47930Sstevel@tonic-gate 47940Sstevel@tonic-gate if (error) 47950Sstevel@tonic-gate return (set_errno(error)); 47960Sstevel@tonic-gate return ((ssize_t)size); 47970Sstevel@tonic-gate } 47980Sstevel@tonic-gate 47990Sstevel@tonic-gate /* 48002267Sdp * Systemcall entry point for zone_setattr(2). 48012267Sdp */ 48022267Sdp /*ARGSUSED*/ 48032267Sdp static int 48042267Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 48052267Sdp { 48062267Sdp zone_t *zone; 48072267Sdp zone_status_t zone_status; 48082267Sdp int err; 48092267Sdp 48102267Sdp if (secpolicy_zone_config(CRED()) != 0) 48112267Sdp return (set_errno(EPERM)); 48122267Sdp 48132267Sdp /* 48143247Sgjelinek * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the 48153247Sgjelinek * global zone. 48162267Sdp */ 48173247Sgjelinek if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) { 48182267Sdp return (set_errno(EINVAL)); 48192267Sdp } 48202267Sdp 48212267Sdp mutex_enter(&zonehash_lock); 48222267Sdp if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 48232267Sdp mutex_exit(&zonehash_lock); 48242267Sdp return (set_errno(EINVAL)); 48252267Sdp } 48262267Sdp zone_hold(zone); 48272267Sdp mutex_exit(&zonehash_lock); 48282267Sdp 48293247Sgjelinek /* 48303247Sgjelinek * At present most attributes can only be set on non-running, 48313247Sgjelinek * non-global zones. 48323247Sgjelinek */ 48332267Sdp zone_status = zone_status_get(zone); 48343247Sgjelinek if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY) 48352267Sdp goto done; 48362267Sdp 48372267Sdp switch (attr) { 48382267Sdp case ZONE_ATTR_INITNAME: 48392267Sdp err = zone_set_initname(zone, (const char *)buf); 48402267Sdp break; 48412267Sdp case ZONE_ATTR_BOOTARGS: 48422267Sdp err = zone_set_bootargs(zone, (const char *)buf); 48432267Sdp break; 48442712Snn35248 case ZONE_ATTR_BRAND: 48454141Sedp err = zone_set_brand(zone, (const char *)buf); 48462712Snn35248 break; 48473247Sgjelinek case ZONE_ATTR_PHYS_MCAP: 48483247Sgjelinek err = zone_set_phys_mcap(zone, (const uint64_t *)buf); 48493247Sgjelinek break; 48503247Sgjelinek case ZONE_ATTR_SCHED_CLASS: 48513247Sgjelinek err = zone_set_sched_class(zone, (const char *)buf); 48523247Sgjelinek break; 48538662SJordan.Vaughan@Sun.com case ZONE_ATTR_HOSTID: 48548662SJordan.Vaughan@Sun.com if (bufsize == sizeof (zone->zone_hostid)) { 48558662SJordan.Vaughan@Sun.com if (copyin(buf, &zone->zone_hostid, bufsize) == 0) 48568662SJordan.Vaughan@Sun.com err = 0; 48578662SJordan.Vaughan@Sun.com else 48588662SJordan.Vaughan@Sun.com err = EFAULT; 48598662SJordan.Vaughan@Sun.com } else { 48608662SJordan.Vaughan@Sun.com err = EINVAL; 48618662SJordan.Vaughan@Sun.com } 48628662SJordan.Vaughan@Sun.com break; 48632267Sdp default: 48642712Snn35248 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) 48652712Snn35248 err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize); 48662712Snn35248 else 48672712Snn35248 err = EINVAL; 48682267Sdp } 48692267Sdp 48702267Sdp done: 48712267Sdp zone_rele(zone); 48722267Sdp return (err != 0 ? set_errno(err) : 0); 48732267Sdp } 48742267Sdp 48752267Sdp /* 48760Sstevel@tonic-gate * Return zero if the process has at least one vnode mapped in to its 48770Sstevel@tonic-gate * address space which shouldn't be allowed to change zones. 48783247Sgjelinek * 48793247Sgjelinek * Also return zero if the process has any shared mappings which reserve 48803247Sgjelinek * swap. This is because the counting for zone.max-swap does not allow swap 48815331Samw * reservation to be shared between zones. zone swap reservation is counted 48823247Sgjelinek * on zone->zone_max_swap. 48830Sstevel@tonic-gate */ 48840Sstevel@tonic-gate static int 48850Sstevel@tonic-gate as_can_change_zones(void) 48860Sstevel@tonic-gate { 48870Sstevel@tonic-gate proc_t *pp = curproc; 48880Sstevel@tonic-gate struct seg *seg; 48890Sstevel@tonic-gate struct as *as = pp->p_as; 48900Sstevel@tonic-gate vnode_t *vp; 48910Sstevel@tonic-gate int allow = 1; 48920Sstevel@tonic-gate 48930Sstevel@tonic-gate ASSERT(pp->p_as != &kas); 48943247Sgjelinek AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 48950Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 48963247Sgjelinek 48973247Sgjelinek /* 48983247Sgjelinek * Cannot enter zone with shared anon memory which 48993247Sgjelinek * reserves swap. See comment above. 49003247Sgjelinek */ 49013247Sgjelinek if (seg_can_change_zones(seg) == B_FALSE) { 49023247Sgjelinek allow = 0; 49033247Sgjelinek break; 49043247Sgjelinek } 49050Sstevel@tonic-gate /* 49060Sstevel@tonic-gate * if we can't get a backing vnode for this segment then skip 49070Sstevel@tonic-gate * it. 49080Sstevel@tonic-gate */ 49090Sstevel@tonic-gate vp = NULL; 49100Sstevel@tonic-gate if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 49110Sstevel@tonic-gate continue; 49120Sstevel@tonic-gate if (!vn_can_change_zones(vp)) { /* bail on first match */ 49130Sstevel@tonic-gate allow = 0; 49140Sstevel@tonic-gate break; 49150Sstevel@tonic-gate } 49160Sstevel@tonic-gate } 49173247Sgjelinek AS_LOCK_EXIT(as, &as->a_lock); 49180Sstevel@tonic-gate return (allow); 49190Sstevel@tonic-gate } 49200Sstevel@tonic-gate 49210Sstevel@tonic-gate /* 49223247Sgjelinek * Count swap reserved by curproc's address space 49233247Sgjelinek */ 49243247Sgjelinek static size_t 49253247Sgjelinek as_swresv(void) 49263247Sgjelinek { 49273247Sgjelinek proc_t *pp = curproc; 49283247Sgjelinek struct seg *seg; 49293247Sgjelinek struct as *as = pp->p_as; 49303247Sgjelinek size_t swap = 0; 49313247Sgjelinek 49323247Sgjelinek ASSERT(pp->p_as != &kas); 49333247Sgjelinek ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 49343247Sgjelinek for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) 49353247Sgjelinek swap += seg_swresv(seg); 49363247Sgjelinek 49373247Sgjelinek return (swap); 49383247Sgjelinek } 49393247Sgjelinek 49403247Sgjelinek /* 49410Sstevel@tonic-gate * Systemcall entry point for zone_enter(). 49420Sstevel@tonic-gate * 49430Sstevel@tonic-gate * The current process is injected into said zone. In the process 49440Sstevel@tonic-gate * it will change its project membership, privileges, rootdir/cwd, 49450Sstevel@tonic-gate * zone-wide rctls, and pool association to match those of the zone. 49460Sstevel@tonic-gate * 49470Sstevel@tonic-gate * The first zone_enter() called while the zone is in the ZONE_IS_READY 49480Sstevel@tonic-gate * state will transition it to ZONE_IS_RUNNING. Processes may only 49490Sstevel@tonic-gate * enter a zone that is "ready" or "running". 49500Sstevel@tonic-gate */ 49510Sstevel@tonic-gate static int 49520Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 49530Sstevel@tonic-gate { 49540Sstevel@tonic-gate zone_t *zone; 49550Sstevel@tonic-gate vnode_t *vp; 49560Sstevel@tonic-gate proc_t *pp = curproc; 49570Sstevel@tonic-gate contract_t *ct; 49580Sstevel@tonic-gate cont_process_t *ctp; 49590Sstevel@tonic-gate task_t *tk, *oldtk; 49600Sstevel@tonic-gate kproject_t *zone_proj0; 49610Sstevel@tonic-gate cred_t *cr, *newcr; 49620Sstevel@tonic-gate pool_t *oldpool, *newpool; 49630Sstevel@tonic-gate sess_t *sp; 49640Sstevel@tonic-gate uid_t uid; 49650Sstevel@tonic-gate zone_status_t status; 49660Sstevel@tonic-gate int err = 0; 49670Sstevel@tonic-gate rctl_entity_p_t e; 49683247Sgjelinek size_t swap; 49693792Sakolb kthread_id_t t; 49700Sstevel@tonic-gate 49710Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 49720Sstevel@tonic-gate return (set_errno(EPERM)); 49730Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 49740Sstevel@tonic-gate return (set_errno(EINVAL)); 49750Sstevel@tonic-gate 49760Sstevel@tonic-gate /* 49770Sstevel@tonic-gate * Stop all lwps so we don't need to hold a lock to look at 49780Sstevel@tonic-gate * curproc->p_zone. This needs to happen before we grab any 49790Sstevel@tonic-gate * locks to avoid deadlock (another lwp in the process could 49800Sstevel@tonic-gate * be waiting for the held lock). 49810Sstevel@tonic-gate */ 49820Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 49830Sstevel@tonic-gate return (set_errno(EINTR)); 49840Sstevel@tonic-gate 49850Sstevel@tonic-gate /* 49860Sstevel@tonic-gate * Make sure we're not changing zones with files open or mapped in 49870Sstevel@tonic-gate * to our address space which shouldn't be changing zones. 49880Sstevel@tonic-gate */ 49890Sstevel@tonic-gate if (!files_can_change_zones()) { 49900Sstevel@tonic-gate err = EBADF; 49910Sstevel@tonic-gate goto out; 49920Sstevel@tonic-gate } 49930Sstevel@tonic-gate if (!as_can_change_zones()) { 49940Sstevel@tonic-gate err = EFAULT; 49950Sstevel@tonic-gate goto out; 49960Sstevel@tonic-gate } 49970Sstevel@tonic-gate 49980Sstevel@tonic-gate mutex_enter(&zonehash_lock); 49990Sstevel@tonic-gate if (pp->p_zone != global_zone) { 50000Sstevel@tonic-gate mutex_exit(&zonehash_lock); 50010Sstevel@tonic-gate err = EINVAL; 50020Sstevel@tonic-gate goto out; 50030Sstevel@tonic-gate } 50040Sstevel@tonic-gate 50050Sstevel@tonic-gate zone = zone_find_all_by_id(zoneid); 50060Sstevel@tonic-gate if (zone == NULL) { 50070Sstevel@tonic-gate mutex_exit(&zonehash_lock); 50080Sstevel@tonic-gate err = EINVAL; 50090Sstevel@tonic-gate goto out; 50100Sstevel@tonic-gate } 50110Sstevel@tonic-gate 50120Sstevel@tonic-gate /* 50130Sstevel@tonic-gate * To prevent processes in a zone from holding contracts on 50140Sstevel@tonic-gate * extrazonal resources, and to avoid process contract 50150Sstevel@tonic-gate * memberships which span zones, contract holders and processes 50160Sstevel@tonic-gate * which aren't the sole members of their encapsulating process 50170Sstevel@tonic-gate * contracts are not allowed to zone_enter. 50180Sstevel@tonic-gate */ 50190Sstevel@tonic-gate ctp = pp->p_ct_process; 50200Sstevel@tonic-gate ct = &ctp->conp_contract; 50210Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 50220Sstevel@tonic-gate mutex_enter(&pp->p_lock); 50230Sstevel@tonic-gate if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 50240Sstevel@tonic-gate mutex_exit(&pp->p_lock); 50250Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 50260Sstevel@tonic-gate mutex_exit(&zonehash_lock); 50270Sstevel@tonic-gate err = EINVAL; 50280Sstevel@tonic-gate goto out; 50290Sstevel@tonic-gate } 50300Sstevel@tonic-gate 50310Sstevel@tonic-gate /* 50320Sstevel@tonic-gate * Moreover, we don't allow processes whose encapsulating 50330Sstevel@tonic-gate * process contracts have inherited extrazonal contracts. 50340Sstevel@tonic-gate * While it would be easier to eliminate all process contracts 50350Sstevel@tonic-gate * with inherited contracts, we need to be able to give a 50360Sstevel@tonic-gate * restarted init (or other zone-penetrating process) its 50370Sstevel@tonic-gate * predecessor's contracts. 50380Sstevel@tonic-gate */ 50390Sstevel@tonic-gate if (ctp->conp_ninherited != 0) { 50400Sstevel@tonic-gate contract_t *next; 50410Sstevel@tonic-gate for (next = list_head(&ctp->conp_inherited); next; 50420Sstevel@tonic-gate next = list_next(&ctp->conp_inherited, next)) { 50430Sstevel@tonic-gate if (contract_getzuniqid(next) != zone->zone_uniqid) { 50440Sstevel@tonic-gate mutex_exit(&pp->p_lock); 50450Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 50460Sstevel@tonic-gate mutex_exit(&zonehash_lock); 50470Sstevel@tonic-gate err = EINVAL; 50480Sstevel@tonic-gate goto out; 50490Sstevel@tonic-gate } 50500Sstevel@tonic-gate } 50510Sstevel@tonic-gate } 50526073Sacruz 50530Sstevel@tonic-gate mutex_exit(&pp->p_lock); 50540Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 50550Sstevel@tonic-gate 50560Sstevel@tonic-gate status = zone_status_get(zone); 50570Sstevel@tonic-gate if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 50580Sstevel@tonic-gate /* 50590Sstevel@tonic-gate * Can't join 50600Sstevel@tonic-gate */ 50610Sstevel@tonic-gate mutex_exit(&zonehash_lock); 50620Sstevel@tonic-gate err = EINVAL; 50630Sstevel@tonic-gate goto out; 50640Sstevel@tonic-gate } 50650Sstevel@tonic-gate 50660Sstevel@tonic-gate /* 50670Sstevel@tonic-gate * Make sure new priv set is within the permitted set for caller 50680Sstevel@tonic-gate */ 50690Sstevel@tonic-gate if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 50700Sstevel@tonic-gate mutex_exit(&zonehash_lock); 50710Sstevel@tonic-gate err = EPERM; 50720Sstevel@tonic-gate goto out; 50730Sstevel@tonic-gate } 50740Sstevel@tonic-gate /* 50750Sstevel@tonic-gate * We want to momentarily drop zonehash_lock while we optimistically 50760Sstevel@tonic-gate * bind curproc to the pool it should be running in. This is safe 50770Sstevel@tonic-gate * since the zone can't disappear (we have a hold on it). 50780Sstevel@tonic-gate */ 50790Sstevel@tonic-gate zone_hold(zone); 50800Sstevel@tonic-gate mutex_exit(&zonehash_lock); 50810Sstevel@tonic-gate 50820Sstevel@tonic-gate /* 50830Sstevel@tonic-gate * Grab pool_lock to keep the pools configuration from changing 50840Sstevel@tonic-gate * and to stop ourselves from getting rebound to another pool 50850Sstevel@tonic-gate * until we join the zone. 50860Sstevel@tonic-gate */ 50870Sstevel@tonic-gate if (pool_lock_intr() != 0) { 50880Sstevel@tonic-gate zone_rele(zone); 50890Sstevel@tonic-gate err = EINTR; 50900Sstevel@tonic-gate goto out; 50910Sstevel@tonic-gate } 50920Sstevel@tonic-gate ASSERT(secpolicy_pool(CRED()) == 0); 50930Sstevel@tonic-gate /* 50940Sstevel@tonic-gate * Bind ourselves to the pool currently associated with the zone. 50950Sstevel@tonic-gate */ 50960Sstevel@tonic-gate oldpool = curproc->p_pool; 50970Sstevel@tonic-gate newpool = zone_pool_get(zone); 50980Sstevel@tonic-gate if (pool_state == POOL_ENABLED && newpool != oldpool && 50990Sstevel@tonic-gate (err = pool_do_bind(newpool, P_PID, P_MYID, 51000Sstevel@tonic-gate POOL_BIND_ALL)) != 0) { 51010Sstevel@tonic-gate pool_unlock(); 51020Sstevel@tonic-gate zone_rele(zone); 51030Sstevel@tonic-gate goto out; 51040Sstevel@tonic-gate } 51050Sstevel@tonic-gate 51060Sstevel@tonic-gate /* 51070Sstevel@tonic-gate * Grab cpu_lock now; we'll need it later when we call 51080Sstevel@tonic-gate * task_join(). 51090Sstevel@tonic-gate */ 51100Sstevel@tonic-gate mutex_enter(&cpu_lock); 51110Sstevel@tonic-gate mutex_enter(&zonehash_lock); 51120Sstevel@tonic-gate /* 51130Sstevel@tonic-gate * Make sure the zone hasn't moved on since we dropped zonehash_lock. 51140Sstevel@tonic-gate */ 51150Sstevel@tonic-gate if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 51160Sstevel@tonic-gate /* 51170Sstevel@tonic-gate * Can't join anymore. 51180Sstevel@tonic-gate */ 51190Sstevel@tonic-gate mutex_exit(&zonehash_lock); 51200Sstevel@tonic-gate mutex_exit(&cpu_lock); 51210Sstevel@tonic-gate if (pool_state == POOL_ENABLED && 51220Sstevel@tonic-gate newpool != oldpool) 51230Sstevel@tonic-gate (void) pool_do_bind(oldpool, P_PID, P_MYID, 51240Sstevel@tonic-gate POOL_BIND_ALL); 51250Sstevel@tonic-gate pool_unlock(); 51260Sstevel@tonic-gate zone_rele(zone); 51270Sstevel@tonic-gate err = EINVAL; 51280Sstevel@tonic-gate goto out; 51290Sstevel@tonic-gate } 51300Sstevel@tonic-gate 51313247Sgjelinek /* 51323247Sgjelinek * a_lock must be held while transfering locked memory and swap 51333247Sgjelinek * reservation from the global zone to the non global zone because 51343247Sgjelinek * asynchronous faults on the processes' address space can lock 51353247Sgjelinek * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE 51363247Sgjelinek * segments respectively. 51373247Sgjelinek */ 51383247Sgjelinek AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER); 51393247Sgjelinek swap = as_swresv(); 51400Sstevel@tonic-gate mutex_enter(&pp->p_lock); 51410Sstevel@tonic-gate zone_proj0 = zone->zone_zsched->p_task->tk_proj; 51420Sstevel@tonic-gate /* verify that we do not exceed and task or lwp limits */ 51430Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 51440Sstevel@tonic-gate /* add new lwps to zone and zone's proj0 */ 51450Sstevel@tonic-gate zone_proj0->kpj_nlwps += pp->p_lwpcnt; 51460Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 51470Sstevel@tonic-gate /* add 1 task to zone's proj0 */ 51480Sstevel@tonic-gate zone_proj0->kpj_ntasks += 1; 51490Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 51500Sstevel@tonic-gate 51513247Sgjelinek mutex_enter(&zone->zone_mem_lock); 51522768Ssl108498 zone->zone_locked_mem += pp->p_locked_mem; 51532768Ssl108498 zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem; 51543247Sgjelinek zone->zone_max_swap += swap; 51553247Sgjelinek mutex_exit(&zone->zone_mem_lock); 51562768Ssl108498 51573916Skrishna mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock)); 51583916Skrishna zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem; 51593916Skrishna mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock)); 51603916Skrishna 51610Sstevel@tonic-gate /* remove lwps from proc's old zone and old project */ 51620Sstevel@tonic-gate mutex_enter(&pp->p_zone->zone_nlwps_lock); 51630Sstevel@tonic-gate pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 51640Sstevel@tonic-gate pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 51650Sstevel@tonic-gate mutex_exit(&pp->p_zone->zone_nlwps_lock); 51660Sstevel@tonic-gate 51673247Sgjelinek mutex_enter(&pp->p_zone->zone_mem_lock); 51682768Ssl108498 pp->p_zone->zone_locked_mem -= pp->p_locked_mem; 51692768Ssl108498 pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 51703247Sgjelinek pp->p_zone->zone_max_swap -= swap; 51713247Sgjelinek mutex_exit(&pp->p_zone->zone_mem_lock); 51722768Ssl108498 51733916Skrishna mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 51743916Skrishna pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem; 51753916Skrishna mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 51763916Skrishna 51779121SVamsi.Krishna@Sun.COM pp->p_flag |= SZONETOP; 51789121SVamsi.Krishna@Sun.COM pp->p_zone = zone; 51792768Ssl108498 mutex_exit(&pp->p_lock); 51803247Sgjelinek AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock); 51812768Ssl108498 51820Sstevel@tonic-gate /* 51830Sstevel@tonic-gate * Joining the zone cannot fail from now on. 51840Sstevel@tonic-gate * 51850Sstevel@tonic-gate * This means that a lot of the following code can be commonized and 51860Sstevel@tonic-gate * shared with zsched(). 51870Sstevel@tonic-gate */ 51880Sstevel@tonic-gate 51890Sstevel@tonic-gate /* 51906073Sacruz * If the process contract fmri was inherited, we need to 51916073Sacruz * flag this so that any contract status will not leak 51926073Sacruz * extra zone information, svc_fmri in this case 51936073Sacruz */ 51946073Sacruz if (ctp->conp_svc_ctid != ct->ct_id) { 51956073Sacruz mutex_enter(&ct->ct_lock); 51966073Sacruz ctp->conp_svc_zone_enter = ct->ct_id; 51976073Sacruz mutex_exit(&ct->ct_lock); 51986073Sacruz } 51996073Sacruz 52006073Sacruz /* 52010Sstevel@tonic-gate * Reset the encapsulating process contract's zone. 52020Sstevel@tonic-gate */ 52030Sstevel@tonic-gate ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 52040Sstevel@tonic-gate contract_setzuniqid(ct, zone->zone_uniqid); 52050Sstevel@tonic-gate 52060Sstevel@tonic-gate /* 52070Sstevel@tonic-gate * Create a new task and associate the process with the project keyed 52080Sstevel@tonic-gate * by (projid,zoneid). 52090Sstevel@tonic-gate * 52100Sstevel@tonic-gate * We might as well be in project 0; the global zone's projid doesn't 52110Sstevel@tonic-gate * make much sense in a zone anyhow. 52120Sstevel@tonic-gate * 52130Sstevel@tonic-gate * This also increments zone_ntasks, and returns with p_lock held. 52140Sstevel@tonic-gate */ 52150Sstevel@tonic-gate tk = task_create(0, zone); 52160Sstevel@tonic-gate oldtk = task_join(tk, 0); 52170Sstevel@tonic-gate mutex_exit(&cpu_lock); 52180Sstevel@tonic-gate 52190Sstevel@tonic-gate /* 52200Sstevel@tonic-gate * call RCTLOP_SET functions on this proc 52210Sstevel@tonic-gate */ 52220Sstevel@tonic-gate e.rcep_p.zone = zone; 52230Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 52240Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 52250Sstevel@tonic-gate RCD_CALLBACK); 52260Sstevel@tonic-gate mutex_exit(&pp->p_lock); 52270Sstevel@tonic-gate 52280Sstevel@tonic-gate /* 52290Sstevel@tonic-gate * We don't need to hold any of zsched's locks here; not only do we know 52300Sstevel@tonic-gate * the process and zone aren't going away, we know its session isn't 52310Sstevel@tonic-gate * changing either. 52320Sstevel@tonic-gate * 52330Sstevel@tonic-gate * By joining zsched's session here, we mimic the behavior in the 52340Sstevel@tonic-gate * global zone of init's sid being the pid of sched. We extend this 52350Sstevel@tonic-gate * to all zlogin-like zone_enter()'ing processes as well. 52360Sstevel@tonic-gate */ 52370Sstevel@tonic-gate mutex_enter(&pidlock); 52380Sstevel@tonic-gate sp = zone->zone_zsched->p_sessp; 52392712Snn35248 sess_hold(zone->zone_zsched); 52400Sstevel@tonic-gate mutex_enter(&pp->p_lock); 52410Sstevel@tonic-gate pgexit(pp); 52422712Snn35248 sess_rele(pp->p_sessp, B_TRUE); 52430Sstevel@tonic-gate pp->p_sessp = sp; 52440Sstevel@tonic-gate pgjoin(pp, zone->zone_zsched->p_pidp); 52453247Sgjelinek 52463247Sgjelinek /* 52473792Sakolb * If any threads are scheduled to be placed on zone wait queue they 52483792Sakolb * should abandon the idea since the wait queue is changing. 52493792Sakolb * We need to be holding pidlock & p_lock to do this. 52503792Sakolb */ 52513792Sakolb if ((t = pp->p_tlist) != NULL) { 52523792Sakolb do { 52533792Sakolb thread_lock(t); 52543792Sakolb /* 52553792Sakolb * Kick this thread so that he doesn't sit 52563792Sakolb * on a wrong wait queue. 52573792Sakolb */ 52583792Sakolb if (ISWAITING(t)) 52593792Sakolb setrun_locked(t); 52603792Sakolb 52613792Sakolb if (t->t_schedflag & TS_ANYWAITQ) 52623792Sakolb t->t_schedflag &= ~ TS_ANYWAITQ; 52633792Sakolb 52643792Sakolb thread_unlock(t); 52653792Sakolb } while ((t = t->t_forw) != pp->p_tlist); 52663792Sakolb } 52673792Sakolb 52683792Sakolb /* 52693247Sgjelinek * If there is a default scheduling class for the zone and it is not 52703247Sgjelinek * the class we are currently in, change all of the threads in the 52713247Sgjelinek * process to the new class. We need to be holding pidlock & p_lock 52723247Sgjelinek * when we call parmsset so this is a good place to do it. 52733247Sgjelinek */ 52743247Sgjelinek if (zone->zone_defaultcid > 0 && 52753247Sgjelinek zone->zone_defaultcid != curthread->t_cid) { 52763247Sgjelinek pcparms_t pcparms; 52773247Sgjelinek 52783247Sgjelinek pcparms.pc_cid = zone->zone_defaultcid; 52793247Sgjelinek pcparms.pc_clparms[0] = 0; 52803247Sgjelinek 52813247Sgjelinek /* 52823247Sgjelinek * If setting the class fails, we still want to enter the zone. 52833247Sgjelinek */ 52843247Sgjelinek if ((t = pp->p_tlist) != NULL) { 52853247Sgjelinek do { 52863247Sgjelinek (void) parmsset(&pcparms, t); 52873247Sgjelinek } while ((t = t->t_forw) != pp->p_tlist); 52883247Sgjelinek } 52893247Sgjelinek } 52903247Sgjelinek 52910Sstevel@tonic-gate mutex_exit(&pp->p_lock); 52920Sstevel@tonic-gate mutex_exit(&pidlock); 52930Sstevel@tonic-gate 52940Sstevel@tonic-gate mutex_exit(&zonehash_lock); 52950Sstevel@tonic-gate /* 52960Sstevel@tonic-gate * We're firmly in the zone; let pools progress. 52970Sstevel@tonic-gate */ 52980Sstevel@tonic-gate pool_unlock(); 52990Sstevel@tonic-gate task_rele(oldtk); 53000Sstevel@tonic-gate /* 53010Sstevel@tonic-gate * We don't need to retain a hold on the zone since we already 53020Sstevel@tonic-gate * incremented zone_ntasks, so the zone isn't going anywhere. 53030Sstevel@tonic-gate */ 53040Sstevel@tonic-gate zone_rele(zone); 53050Sstevel@tonic-gate 53060Sstevel@tonic-gate /* 53070Sstevel@tonic-gate * Chroot 53080Sstevel@tonic-gate */ 53090Sstevel@tonic-gate vp = zone->zone_rootvp; 53100Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 53110Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 53120Sstevel@tonic-gate 53130Sstevel@tonic-gate /* 53140Sstevel@tonic-gate * Change process credentials 53150Sstevel@tonic-gate */ 53160Sstevel@tonic-gate newcr = cralloc(); 53170Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 53180Sstevel@tonic-gate cr = pp->p_cred; 53190Sstevel@tonic-gate crcopy_to(cr, newcr); 53200Sstevel@tonic-gate crsetzone(newcr, zone); 53210Sstevel@tonic-gate pp->p_cred = newcr; 53220Sstevel@tonic-gate 53230Sstevel@tonic-gate /* 53240Sstevel@tonic-gate * Restrict all process privilege sets to zone limit 53250Sstevel@tonic-gate */ 53260Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 53270Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 53280Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 53290Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 53300Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 53310Sstevel@tonic-gate crset(pp, newcr); 53320Sstevel@tonic-gate 53330Sstevel@tonic-gate /* 53340Sstevel@tonic-gate * Adjust upcount to reflect zone entry. 53350Sstevel@tonic-gate */ 53360Sstevel@tonic-gate uid = crgetruid(newcr); 53370Sstevel@tonic-gate mutex_enter(&pidlock); 53380Sstevel@tonic-gate upcount_dec(uid, GLOBAL_ZONEID); 53390Sstevel@tonic-gate upcount_inc(uid, zoneid); 53400Sstevel@tonic-gate mutex_exit(&pidlock); 53410Sstevel@tonic-gate 53420Sstevel@tonic-gate /* 53430Sstevel@tonic-gate * Set up core file path and content. 53440Sstevel@tonic-gate */ 53450Sstevel@tonic-gate set_core_defaults(); 53460Sstevel@tonic-gate 53470Sstevel@tonic-gate out: 53480Sstevel@tonic-gate /* 53490Sstevel@tonic-gate * Let the other lwps continue. 53500Sstevel@tonic-gate */ 53510Sstevel@tonic-gate mutex_enter(&pp->p_lock); 53520Sstevel@tonic-gate if (curthread != pp->p_agenttp) 53530Sstevel@tonic-gate continuelwps(pp); 53540Sstevel@tonic-gate mutex_exit(&pp->p_lock); 53550Sstevel@tonic-gate 53560Sstevel@tonic-gate return (err != 0 ? set_errno(err) : 0); 53570Sstevel@tonic-gate } 53580Sstevel@tonic-gate 53590Sstevel@tonic-gate /* 53600Sstevel@tonic-gate * Systemcall entry point for zone_list(2). 53610Sstevel@tonic-gate * 53620Sstevel@tonic-gate * Processes running in a (non-global) zone only see themselves. 53631676Sjpk * On labeled systems, they see all zones whose label they dominate. 53640Sstevel@tonic-gate */ 53650Sstevel@tonic-gate static int 53660Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones) 53670Sstevel@tonic-gate { 53680Sstevel@tonic-gate zoneid_t *zoneids; 53691769Scarlsonj zone_t *zone, *myzone; 53700Sstevel@tonic-gate uint_t user_nzones, real_nzones; 53711676Sjpk uint_t domi_nzones; 53721676Sjpk int error; 53730Sstevel@tonic-gate 53740Sstevel@tonic-gate if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 53750Sstevel@tonic-gate return (set_errno(EFAULT)); 53760Sstevel@tonic-gate 53771769Scarlsonj myzone = curproc->p_zone; 53781769Scarlsonj if (myzone != global_zone) { 53791676Sjpk bslabel_t *mybslab; 53801676Sjpk 53811676Sjpk if (!is_system_labeled()) { 53821676Sjpk /* just return current zone */ 53831676Sjpk real_nzones = domi_nzones = 1; 53841676Sjpk zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 53851769Scarlsonj zoneids[0] = myzone->zone_id; 53861676Sjpk } else { 53871676Sjpk /* return all zones that are dominated */ 53881676Sjpk mutex_enter(&zonehash_lock); 53891676Sjpk real_nzones = zonecount; 53901676Sjpk domi_nzones = 0; 53911676Sjpk if (real_nzones > 0) { 53921676Sjpk zoneids = kmem_alloc(real_nzones * 53931676Sjpk sizeof (zoneid_t), KM_SLEEP); 53941769Scarlsonj mybslab = label2bslabel(myzone->zone_slabel); 53951676Sjpk for (zone = list_head(&zone_active); 53961676Sjpk zone != NULL; 53971676Sjpk zone = list_next(&zone_active, zone)) { 53981676Sjpk if (zone->zone_id == GLOBAL_ZONEID) 53991676Sjpk continue; 54001769Scarlsonj if (zone != myzone && 54011769Scarlsonj (zone->zone_flags & ZF_IS_SCRATCH)) 54021769Scarlsonj continue; 54031769Scarlsonj /* 54041769Scarlsonj * Note that a label always dominates 54051769Scarlsonj * itself, so myzone is always included 54061769Scarlsonj * in the list. 54071769Scarlsonj */ 54081676Sjpk if (bldominates(mybslab, 54091676Sjpk label2bslabel(zone->zone_slabel))) { 54101676Sjpk zoneids[domi_nzones++] = 54111676Sjpk zone->zone_id; 54121676Sjpk } 54131676Sjpk } 54141676Sjpk } 54151676Sjpk mutex_exit(&zonehash_lock); 54161676Sjpk } 54170Sstevel@tonic-gate } else { 54180Sstevel@tonic-gate mutex_enter(&zonehash_lock); 54190Sstevel@tonic-gate real_nzones = zonecount; 54201676Sjpk domi_nzones = 0; 54211676Sjpk if (real_nzones > 0) { 54220Sstevel@tonic-gate zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 54230Sstevel@tonic-gate KM_SLEEP); 54240Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 54250Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 54261676Sjpk zoneids[domi_nzones++] = zone->zone_id; 54271676Sjpk ASSERT(domi_nzones == real_nzones); 54280Sstevel@tonic-gate } 54290Sstevel@tonic-gate mutex_exit(&zonehash_lock); 54300Sstevel@tonic-gate } 54310Sstevel@tonic-gate 54321676Sjpk /* 54331676Sjpk * If user has allocated space for fewer entries than we found, then 54341676Sjpk * return only up to his limit. Either way, tell him exactly how many 54351676Sjpk * we found. 54361676Sjpk */ 54371676Sjpk if (domi_nzones < user_nzones) 54381676Sjpk user_nzones = domi_nzones; 54391676Sjpk error = 0; 54401676Sjpk if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) { 54410Sstevel@tonic-gate error = EFAULT; 54421676Sjpk } else if (zoneidlist != NULL && user_nzones != 0) { 54430Sstevel@tonic-gate if (copyout(zoneids, zoneidlist, 54440Sstevel@tonic-gate user_nzones * sizeof (zoneid_t)) != 0) 54450Sstevel@tonic-gate error = EFAULT; 54460Sstevel@tonic-gate } 54470Sstevel@tonic-gate 54481676Sjpk if (real_nzones > 0) 54490Sstevel@tonic-gate kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 54500Sstevel@tonic-gate 54511676Sjpk if (error != 0) 54520Sstevel@tonic-gate return (set_errno(error)); 54530Sstevel@tonic-gate else 54540Sstevel@tonic-gate return (0); 54550Sstevel@tonic-gate } 54560Sstevel@tonic-gate 54570Sstevel@tonic-gate /* 54580Sstevel@tonic-gate * Systemcall entry point for zone_lookup(2). 54590Sstevel@tonic-gate * 54601676Sjpk * Non-global zones are only able to see themselves and (on labeled systems) 54611676Sjpk * the zones they dominate. 54620Sstevel@tonic-gate */ 54630Sstevel@tonic-gate static zoneid_t 54640Sstevel@tonic-gate zone_lookup(const char *zone_name) 54650Sstevel@tonic-gate { 54660Sstevel@tonic-gate char *kname; 54670Sstevel@tonic-gate zone_t *zone; 54680Sstevel@tonic-gate zoneid_t zoneid; 54690Sstevel@tonic-gate int err; 54700Sstevel@tonic-gate 54710Sstevel@tonic-gate if (zone_name == NULL) { 54720Sstevel@tonic-gate /* return caller's zone id */ 54730Sstevel@tonic-gate return (getzoneid()); 54740Sstevel@tonic-gate } 54750Sstevel@tonic-gate 54760Sstevel@tonic-gate kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 54770Sstevel@tonic-gate if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 54780Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 54790Sstevel@tonic-gate return (set_errno(err)); 54800Sstevel@tonic-gate } 54810Sstevel@tonic-gate 54820Sstevel@tonic-gate mutex_enter(&zonehash_lock); 54830Sstevel@tonic-gate zone = zone_find_all_by_name(kname); 54840Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 54851676Sjpk /* 54861676Sjpk * In a non-global zone, can only lookup global and own name. 54871676Sjpk * In Trusted Extensions zone label dominance rules apply. 54881676Sjpk */ 54891676Sjpk if (zone == NULL || 54901676Sjpk zone_status_get(zone) < ZONE_IS_READY || 54911676Sjpk !zone_list_access(zone)) { 54920Sstevel@tonic-gate mutex_exit(&zonehash_lock); 54930Sstevel@tonic-gate return (set_errno(EINVAL)); 54941676Sjpk } else { 54951676Sjpk zoneid = zone->zone_id; 54961676Sjpk mutex_exit(&zonehash_lock); 54971676Sjpk return (zoneid); 54980Sstevel@tonic-gate } 54990Sstevel@tonic-gate } 55000Sstevel@tonic-gate 5501813Sdp static int 5502813Sdp zone_version(int *version_arg) 5503813Sdp { 5504813Sdp int version = ZONE_SYSCALL_API_VERSION; 5505813Sdp 5506813Sdp if (copyout(&version, version_arg, sizeof (int)) != 0) 5507813Sdp return (set_errno(EFAULT)); 5508813Sdp return (0); 5509813Sdp } 5510813Sdp 55110Sstevel@tonic-gate /* ARGSUSED */ 55120Sstevel@tonic-gate long 5513789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4) 55140Sstevel@tonic-gate { 55150Sstevel@tonic-gate zone_def zs; 551610616SSebastien.Roy@Sun.COM int err; 55170Sstevel@tonic-gate 55180Sstevel@tonic-gate switch (cmd) { 55190Sstevel@tonic-gate case ZONE_CREATE: 55200Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 55210Sstevel@tonic-gate if (copyin(arg1, &zs, sizeof (zone_def))) { 55220Sstevel@tonic-gate return (set_errno(EFAULT)); 55230Sstevel@tonic-gate } 55240Sstevel@tonic-gate } else { 55250Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 55260Sstevel@tonic-gate zone_def32 zs32; 55270Sstevel@tonic-gate 55280Sstevel@tonic-gate if (copyin(arg1, &zs32, sizeof (zone_def32))) { 55290Sstevel@tonic-gate return (set_errno(EFAULT)); 55300Sstevel@tonic-gate } 55310Sstevel@tonic-gate zs.zone_name = 55320Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_name; 55330Sstevel@tonic-gate zs.zone_root = 55340Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_root; 55350Sstevel@tonic-gate zs.zone_privs = 55360Sstevel@tonic-gate (const struct priv_set *) 55370Sstevel@tonic-gate (unsigned long)zs32.zone_privs; 55381409Sdp zs.zone_privssz = zs32.zone_privssz; 55390Sstevel@tonic-gate zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 55400Sstevel@tonic-gate zs.rctlbufsz = zs32.rctlbufsz; 5541789Sahrens zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf; 5542789Sahrens zs.zfsbufsz = zs32.zfsbufsz; 55430Sstevel@tonic-gate zs.extended_error = 55440Sstevel@tonic-gate (int *)(unsigned long)zs32.extended_error; 55451676Sjpk zs.match = zs32.match; 55461676Sjpk zs.doi = zs32.doi; 55471676Sjpk zs.label = (const bslabel_t *)(uintptr_t)zs32.label; 55483448Sdh155122 zs.flags = zs32.flags; 55490Sstevel@tonic-gate #else 55500Sstevel@tonic-gate panic("get_udatamodel() returned bogus result\n"); 55510Sstevel@tonic-gate #endif 55520Sstevel@tonic-gate } 55530Sstevel@tonic-gate 55540Sstevel@tonic-gate return (zone_create(zs.zone_name, zs.zone_root, 5555813Sdp zs.zone_privs, zs.zone_privssz, 5556813Sdp (caddr_t)zs.rctlbuf, zs.rctlbufsz, 5557813Sdp (caddr_t)zs.zfsbuf, zs.zfsbufsz, 55581676Sjpk zs.extended_error, zs.match, zs.doi, 55593448Sdh155122 zs.label, zs.flags)); 55600Sstevel@tonic-gate case ZONE_BOOT: 55612267Sdp return (zone_boot((zoneid_t)(uintptr_t)arg1)); 55620Sstevel@tonic-gate case ZONE_DESTROY: 55630Sstevel@tonic-gate return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 55640Sstevel@tonic-gate case ZONE_GETATTR: 55650Sstevel@tonic-gate return (zone_getattr((zoneid_t)(uintptr_t)arg1, 55660Sstevel@tonic-gate (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 55672267Sdp case ZONE_SETATTR: 55682267Sdp return (zone_setattr((zoneid_t)(uintptr_t)arg1, 55692267Sdp (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 55700Sstevel@tonic-gate case ZONE_ENTER: 55710Sstevel@tonic-gate return (zone_enter((zoneid_t)(uintptr_t)arg1)); 55720Sstevel@tonic-gate case ZONE_LIST: 55730Sstevel@tonic-gate return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 55740Sstevel@tonic-gate case ZONE_SHUTDOWN: 55750Sstevel@tonic-gate return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 55760Sstevel@tonic-gate case ZONE_LOOKUP: 55770Sstevel@tonic-gate return (zone_lookup((const char *)arg1)); 5578813Sdp case ZONE_VERSION: 5579813Sdp return (zone_version((int *)arg1)); 55803448Sdh155122 case ZONE_ADD_DATALINK: 55813448Sdh155122 return (zone_add_datalink((zoneid_t)(uintptr_t)arg1, 558210616SSebastien.Roy@Sun.COM (datalink_id_t)(uintptr_t)arg2)); 55833448Sdh155122 case ZONE_DEL_DATALINK: 55843448Sdh155122 return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1, 558510616SSebastien.Roy@Sun.COM (datalink_id_t)(uintptr_t)arg2)); 558610616SSebastien.Roy@Sun.COM case ZONE_CHECK_DATALINK: { 558710616SSebastien.Roy@Sun.COM zoneid_t zoneid; 558810616SSebastien.Roy@Sun.COM boolean_t need_copyout; 558910616SSebastien.Roy@Sun.COM 559010616SSebastien.Roy@Sun.COM if (copyin(arg1, &zoneid, sizeof (zoneid)) != 0) 559110616SSebastien.Roy@Sun.COM return (EFAULT); 559210616SSebastien.Roy@Sun.COM need_copyout = (zoneid == ALL_ZONES); 559310616SSebastien.Roy@Sun.COM err = zone_check_datalink(&zoneid, 559410616SSebastien.Roy@Sun.COM (datalink_id_t)(uintptr_t)arg2); 559510616SSebastien.Roy@Sun.COM if (err == 0 && need_copyout) { 559610616SSebastien.Roy@Sun.COM if (copyout(&zoneid, arg1, sizeof (zoneid)) != 0) 559710616SSebastien.Roy@Sun.COM err = EFAULT; 559810616SSebastien.Roy@Sun.COM } 559910616SSebastien.Roy@Sun.COM return (err == 0 ? 0 : set_errno(err)); 560010616SSebastien.Roy@Sun.COM } 56013448Sdh155122 case ZONE_LIST_DATALINK: 56023448Sdh155122 return (zone_list_datalink((zoneid_t)(uintptr_t)arg1, 560310616SSebastien.Roy@Sun.COM (int *)arg2, (datalink_id_t *)(uintptr_t)arg3)); 56040Sstevel@tonic-gate default: 56050Sstevel@tonic-gate return (set_errno(EINVAL)); 56060Sstevel@tonic-gate } 56070Sstevel@tonic-gate } 56080Sstevel@tonic-gate 56090Sstevel@tonic-gate struct zarg { 56100Sstevel@tonic-gate zone_t *zone; 56110Sstevel@tonic-gate zone_cmd_arg_t arg; 56120Sstevel@tonic-gate }; 56130Sstevel@tonic-gate 56140Sstevel@tonic-gate static int 56150Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp) 56160Sstevel@tonic-gate { 56170Sstevel@tonic-gate char *buf; 56180Sstevel@tonic-gate size_t buflen; 56190Sstevel@tonic-gate int error; 56200Sstevel@tonic-gate 56210Sstevel@tonic-gate buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 56220Sstevel@tonic-gate buf = kmem_alloc(buflen, KM_SLEEP); 56230Sstevel@tonic-gate (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 56240Sstevel@tonic-gate error = door_ki_open(buf, doorp); 56250Sstevel@tonic-gate kmem_free(buf, buflen); 56260Sstevel@tonic-gate return (error); 56270Sstevel@tonic-gate } 56280Sstevel@tonic-gate 56290Sstevel@tonic-gate static void 56300Sstevel@tonic-gate zone_release_door(door_handle_t *doorp) 56310Sstevel@tonic-gate { 56320Sstevel@tonic-gate door_ki_rele(*doorp); 56330Sstevel@tonic-gate *doorp = NULL; 56340Sstevel@tonic-gate } 56350Sstevel@tonic-gate 56360Sstevel@tonic-gate static void 56370Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp) 56380Sstevel@tonic-gate { 56390Sstevel@tonic-gate door_handle_t door = NULL; 56400Sstevel@tonic-gate door_arg_t darg, save_arg; 56410Sstevel@tonic-gate char *zone_name; 56420Sstevel@tonic-gate size_t zone_namelen; 56430Sstevel@tonic-gate zoneid_t zoneid; 56440Sstevel@tonic-gate zone_t *zone; 56450Sstevel@tonic-gate zone_cmd_arg_t arg; 56460Sstevel@tonic-gate uint64_t uniqid; 56470Sstevel@tonic-gate size_t size; 56480Sstevel@tonic-gate int error; 56490Sstevel@tonic-gate int retry; 56500Sstevel@tonic-gate 56510Sstevel@tonic-gate zone = zargp->zone; 56520Sstevel@tonic-gate arg = zargp->arg; 56530Sstevel@tonic-gate kmem_free(zargp, sizeof (*zargp)); 56540Sstevel@tonic-gate 56550Sstevel@tonic-gate zone_namelen = strlen(zone->zone_name) + 1; 56560Sstevel@tonic-gate zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 56570Sstevel@tonic-gate bcopy(zone->zone_name, zone_name, zone_namelen); 56580Sstevel@tonic-gate zoneid = zone->zone_id; 56590Sstevel@tonic-gate uniqid = zone->zone_uniqid; 56600Sstevel@tonic-gate /* 56610Sstevel@tonic-gate * zoneadmd may be down, but at least we can empty out the zone. 56620Sstevel@tonic-gate * We can ignore the return value of zone_empty() since we're called 56630Sstevel@tonic-gate * from a kernel thread and know we won't be delivered any signals. 56640Sstevel@tonic-gate */ 56650Sstevel@tonic-gate ASSERT(curproc == &p0); 56660Sstevel@tonic-gate (void) zone_empty(zone); 56670Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 56680Sstevel@tonic-gate zone_rele(zone); 56690Sstevel@tonic-gate 56700Sstevel@tonic-gate size = sizeof (arg); 56710Sstevel@tonic-gate darg.rbuf = (char *)&arg; 56720Sstevel@tonic-gate darg.data_ptr = (char *)&arg; 56730Sstevel@tonic-gate darg.rsize = size; 56740Sstevel@tonic-gate darg.data_size = size; 56750Sstevel@tonic-gate darg.desc_ptr = NULL; 56760Sstevel@tonic-gate darg.desc_num = 0; 56770Sstevel@tonic-gate 56780Sstevel@tonic-gate save_arg = darg; 56790Sstevel@tonic-gate /* 56800Sstevel@tonic-gate * Since we're not holding a reference to the zone, any number of 56810Sstevel@tonic-gate * things can go wrong, including the zone disappearing before we get a 56820Sstevel@tonic-gate * chance to talk to zoneadmd. 56830Sstevel@tonic-gate */ 56840Sstevel@tonic-gate for (retry = 0; /* forever */; retry++) { 56850Sstevel@tonic-gate if (door == NULL && 56860Sstevel@tonic-gate (error = zone_lookup_door(zone_name, &door)) != 0) { 56870Sstevel@tonic-gate goto next; 56880Sstevel@tonic-gate } 56890Sstevel@tonic-gate ASSERT(door != NULL); 56900Sstevel@tonic-gate 56916997Sjwadams if ((error = door_ki_upcall_limited(door, &darg, NULL, 56926997Sjwadams SIZE_MAX, 0)) == 0) { 56930Sstevel@tonic-gate break; 56940Sstevel@tonic-gate } 56950Sstevel@tonic-gate switch (error) { 56960Sstevel@tonic-gate case EINTR: 56970Sstevel@tonic-gate /* FALLTHROUGH */ 56980Sstevel@tonic-gate case EAGAIN: /* process may be forking */ 56990Sstevel@tonic-gate /* 57000Sstevel@tonic-gate * Back off for a bit 57010Sstevel@tonic-gate */ 57020Sstevel@tonic-gate break; 57030Sstevel@tonic-gate case EBADF: 57040Sstevel@tonic-gate zone_release_door(&door); 57050Sstevel@tonic-gate if (zone_lookup_door(zone_name, &door) != 0) { 57060Sstevel@tonic-gate /* 57070Sstevel@tonic-gate * zoneadmd may be dead, but it may come back to 57080Sstevel@tonic-gate * life later. 57090Sstevel@tonic-gate */ 57100Sstevel@tonic-gate break; 57110Sstevel@tonic-gate } 57120Sstevel@tonic-gate break; 57130Sstevel@tonic-gate default: 57140Sstevel@tonic-gate cmn_err(CE_WARN, 57150Sstevel@tonic-gate "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 57160Sstevel@tonic-gate error); 57170Sstevel@tonic-gate goto out; 57180Sstevel@tonic-gate } 57190Sstevel@tonic-gate next: 57200Sstevel@tonic-gate /* 57210Sstevel@tonic-gate * If this isn't the same zone_t that we originally had in mind, 57220Sstevel@tonic-gate * then this is the same as if two kadmin requests come in at 57230Sstevel@tonic-gate * the same time: the first one wins. This means we lose, so we 57240Sstevel@tonic-gate * bail. 57250Sstevel@tonic-gate */ 57260Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) { 57270Sstevel@tonic-gate /* 57280Sstevel@tonic-gate * Problem is solved. 57290Sstevel@tonic-gate */ 57300Sstevel@tonic-gate break; 57310Sstevel@tonic-gate } 57320Sstevel@tonic-gate if (zone->zone_uniqid != uniqid) { 57330Sstevel@tonic-gate /* 57340Sstevel@tonic-gate * zoneid recycled 57350Sstevel@tonic-gate */ 57360Sstevel@tonic-gate zone_rele(zone); 57370Sstevel@tonic-gate break; 57380Sstevel@tonic-gate } 57390Sstevel@tonic-gate /* 57400Sstevel@tonic-gate * We could zone_status_timedwait(), but there doesn't seem to 57410Sstevel@tonic-gate * be much point in doing that (plus, it would mean that 57420Sstevel@tonic-gate * zone_free() isn't called until this thread exits). 57430Sstevel@tonic-gate */ 57440Sstevel@tonic-gate zone_rele(zone); 57450Sstevel@tonic-gate delay(hz); 57460Sstevel@tonic-gate darg = save_arg; 57470Sstevel@tonic-gate } 57480Sstevel@tonic-gate out: 57490Sstevel@tonic-gate if (door != NULL) { 57500Sstevel@tonic-gate zone_release_door(&door); 57510Sstevel@tonic-gate } 57520Sstevel@tonic-gate kmem_free(zone_name, zone_namelen); 57530Sstevel@tonic-gate thread_exit(); 57540Sstevel@tonic-gate } 57550Sstevel@tonic-gate 57560Sstevel@tonic-gate /* 57572267Sdp * Entry point for uadmin() to tell the zone to go away or reboot. Analog to 57582267Sdp * kadmin(). The caller is a process in the zone. 57590Sstevel@tonic-gate * 57600Sstevel@tonic-gate * In order to shutdown the zone, we will hand off control to zoneadmd 57610Sstevel@tonic-gate * (running in the global zone) via a door. We do a half-hearted job at 57620Sstevel@tonic-gate * killing all processes in the zone, create a kernel thread to contact 57630Sstevel@tonic-gate * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 57640Sstevel@tonic-gate * a form of generation number used to let zoneadmd (as well as 57650Sstevel@tonic-gate * zone_destroy()) know exactly which zone they're re talking about. 57660Sstevel@tonic-gate */ 57670Sstevel@tonic-gate int 57682267Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp) 57690Sstevel@tonic-gate { 57700Sstevel@tonic-gate struct zarg *zargp; 57710Sstevel@tonic-gate zone_cmd_t zcmd; 57720Sstevel@tonic-gate zone_t *zone; 57730Sstevel@tonic-gate 57740Sstevel@tonic-gate zone = curproc->p_zone; 57750Sstevel@tonic-gate ASSERT(getzoneid() != GLOBAL_ZONEID); 57760Sstevel@tonic-gate 57770Sstevel@tonic-gate switch (cmd) { 57780Sstevel@tonic-gate case A_SHUTDOWN: 57790Sstevel@tonic-gate switch (fcn) { 57800Sstevel@tonic-gate case AD_HALT: 57810Sstevel@tonic-gate case AD_POWEROFF: 57820Sstevel@tonic-gate zcmd = Z_HALT; 57830Sstevel@tonic-gate break; 57840Sstevel@tonic-gate case AD_BOOT: 57850Sstevel@tonic-gate zcmd = Z_REBOOT; 57860Sstevel@tonic-gate break; 57870Sstevel@tonic-gate case AD_IBOOT: 57880Sstevel@tonic-gate case AD_SBOOT: 57890Sstevel@tonic-gate case AD_SIBOOT: 57900Sstevel@tonic-gate case AD_NOSYNC: 57910Sstevel@tonic-gate return (ENOTSUP); 57920Sstevel@tonic-gate default: 57930Sstevel@tonic-gate return (EINVAL); 57940Sstevel@tonic-gate } 57950Sstevel@tonic-gate break; 57960Sstevel@tonic-gate case A_REBOOT: 57970Sstevel@tonic-gate zcmd = Z_REBOOT; 57980Sstevel@tonic-gate break; 57990Sstevel@tonic-gate case A_FTRACE: 58000Sstevel@tonic-gate case A_REMOUNT: 58010Sstevel@tonic-gate case A_FREEZE: 58020Sstevel@tonic-gate case A_DUMP: 58039160SSherry.Moore@Sun.COM case A_CONFIG: 58040Sstevel@tonic-gate return (ENOTSUP); 58050Sstevel@tonic-gate default: 58060Sstevel@tonic-gate ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 58070Sstevel@tonic-gate return (EINVAL); 58080Sstevel@tonic-gate } 58090Sstevel@tonic-gate 58100Sstevel@tonic-gate if (secpolicy_zone_admin(credp, B_FALSE)) 58110Sstevel@tonic-gate return (EPERM); 58120Sstevel@tonic-gate mutex_enter(&zone_status_lock); 58132267Sdp 58140Sstevel@tonic-gate /* 58150Sstevel@tonic-gate * zone_status can't be ZONE_IS_EMPTY or higher since curproc 58160Sstevel@tonic-gate * is in the zone. 58170Sstevel@tonic-gate */ 58180Sstevel@tonic-gate ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 58190Sstevel@tonic-gate if (zone_status_get(zone) > ZONE_IS_RUNNING) { 58200Sstevel@tonic-gate /* 58210Sstevel@tonic-gate * This zone is already on its way down. 58220Sstevel@tonic-gate */ 58230Sstevel@tonic-gate mutex_exit(&zone_status_lock); 58240Sstevel@tonic-gate return (0); 58250Sstevel@tonic-gate } 58260Sstevel@tonic-gate /* 58270Sstevel@tonic-gate * Prevent future zone_enter()s 58280Sstevel@tonic-gate */ 58290Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 58300Sstevel@tonic-gate mutex_exit(&zone_status_lock); 58310Sstevel@tonic-gate 58320Sstevel@tonic-gate /* 58330Sstevel@tonic-gate * Kill everyone now and call zoneadmd later. 58340Sstevel@tonic-gate * zone_ki_call_zoneadmd() will do a more thorough job of this 58350Sstevel@tonic-gate * later. 58360Sstevel@tonic-gate */ 58370Sstevel@tonic-gate killall(zone->zone_id); 58380Sstevel@tonic-gate /* 58390Sstevel@tonic-gate * Now, create the thread to contact zoneadmd and do the rest of the 58400Sstevel@tonic-gate * work. This thread can't be created in our zone otherwise 58410Sstevel@tonic-gate * zone_destroy() would deadlock. 58420Sstevel@tonic-gate */ 58432267Sdp zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP); 58440Sstevel@tonic-gate zargp->arg.cmd = zcmd; 58450Sstevel@tonic-gate zargp->arg.uniqid = zone->zone_uniqid; 58462267Sdp zargp->zone = zone; 58470Sstevel@tonic-gate (void) strcpy(zargp->arg.locale, "C"); 58482267Sdp /* mdep was already copied in for us by uadmin */ 58492267Sdp if (mdep != NULL) 58502267Sdp (void) strlcpy(zargp->arg.bootbuf, mdep, 58512267Sdp sizeof (zargp->arg.bootbuf)); 58522267Sdp zone_hold(zone); 58530Sstevel@tonic-gate 58540Sstevel@tonic-gate (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 58550Sstevel@tonic-gate TS_RUN, minclsyspri); 58560Sstevel@tonic-gate exit(CLD_EXITED, 0); 58570Sstevel@tonic-gate 58580Sstevel@tonic-gate return (EINVAL); 58590Sstevel@tonic-gate } 58600Sstevel@tonic-gate 58610Sstevel@tonic-gate /* 58620Sstevel@tonic-gate * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 58630Sstevel@tonic-gate * status to ZONE_IS_SHUTTING_DOWN. 58648364SJordan.Vaughan@Sun.com * 58658364SJordan.Vaughan@Sun.com * This function also shuts down all running zones to ensure that they won't 58668364SJordan.Vaughan@Sun.com * fork new processes. 58670Sstevel@tonic-gate */ 58680Sstevel@tonic-gate void 58690Sstevel@tonic-gate zone_shutdown_global(void) 58700Sstevel@tonic-gate { 58718364SJordan.Vaughan@Sun.com zone_t *current_zonep; 58728364SJordan.Vaughan@Sun.com 58738364SJordan.Vaughan@Sun.com ASSERT(INGLOBALZONE(curproc)); 58748364SJordan.Vaughan@Sun.com mutex_enter(&zonehash_lock); 58750Sstevel@tonic-gate mutex_enter(&zone_status_lock); 58768364SJordan.Vaughan@Sun.com 58778364SJordan.Vaughan@Sun.com /* Modify the global zone's status first. */ 58780Sstevel@tonic-gate ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 58790Sstevel@tonic-gate zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 58808364SJordan.Vaughan@Sun.com 58818364SJordan.Vaughan@Sun.com /* 58828364SJordan.Vaughan@Sun.com * Now change the states of all running zones to ZONE_IS_SHUTTING_DOWN. 58838364SJordan.Vaughan@Sun.com * We don't mark all zones with ZONE_IS_SHUTTING_DOWN because doing so 58848364SJordan.Vaughan@Sun.com * could cause assertions to fail (e.g., assertions about a zone's 58858364SJordan.Vaughan@Sun.com * state during initialization, readying, or booting) or produce races. 58868364SJordan.Vaughan@Sun.com * We'll let threads continue to initialize and ready new zones: they'll 58878364SJordan.Vaughan@Sun.com * fail to boot the new zones when they see that the global zone is 58888364SJordan.Vaughan@Sun.com * shutting down. 58898364SJordan.Vaughan@Sun.com */ 58908364SJordan.Vaughan@Sun.com for (current_zonep = list_head(&zone_active); current_zonep != NULL; 58918364SJordan.Vaughan@Sun.com current_zonep = list_next(&zone_active, current_zonep)) { 58928364SJordan.Vaughan@Sun.com if (zone_status_get(current_zonep) == ZONE_IS_RUNNING) 58938364SJordan.Vaughan@Sun.com zone_status_set(current_zonep, ZONE_IS_SHUTTING_DOWN); 58948364SJordan.Vaughan@Sun.com } 58950Sstevel@tonic-gate mutex_exit(&zone_status_lock); 58968364SJordan.Vaughan@Sun.com mutex_exit(&zonehash_lock); 58970Sstevel@tonic-gate } 5898789Sahrens 5899789Sahrens /* 5900789Sahrens * Returns true if the named dataset is visible in the current zone. 5901789Sahrens * The 'write' parameter is set to 1 if the dataset is also writable. 5902789Sahrens */ 5903789Sahrens int 5904789Sahrens zone_dataset_visible(const char *dataset, int *write) 5905789Sahrens { 5906789Sahrens zone_dataset_t *zd; 5907789Sahrens size_t len; 5908789Sahrens zone_t *zone = curproc->p_zone; 5909789Sahrens 5910789Sahrens if (dataset[0] == '\0') 5911789Sahrens return (0); 5912789Sahrens 5913789Sahrens /* 5914789Sahrens * Walk the list once, looking for datasets which match exactly, or 5915789Sahrens * specify a dataset underneath an exported dataset. If found, return 5916789Sahrens * true and note that it is writable. 5917789Sahrens */ 5918789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 5919789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 5920789Sahrens 5921789Sahrens len = strlen(zd->zd_dataset); 5922789Sahrens if (strlen(dataset) >= len && 5923789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 5924816Smaybee (dataset[len] == '\0' || dataset[len] == '/' || 5925816Smaybee dataset[len] == '@')) { 5926789Sahrens if (write) 5927789Sahrens *write = 1; 5928789Sahrens return (1); 5929789Sahrens } 5930789Sahrens } 5931789Sahrens 5932789Sahrens /* 5933789Sahrens * Walk the list a second time, searching for datasets which are parents 5934789Sahrens * of exported datasets. These should be visible, but read-only. 5935789Sahrens * 5936789Sahrens * Note that we also have to support forms such as 'pool/dataset/', with 5937789Sahrens * a trailing slash. 5938789Sahrens */ 5939789Sahrens for (zd = list_head(&zone->zone_datasets); zd != NULL; 5940789Sahrens zd = list_next(&zone->zone_datasets, zd)) { 5941789Sahrens 5942789Sahrens len = strlen(dataset); 5943789Sahrens if (dataset[len - 1] == '/') 5944789Sahrens len--; /* Ignore trailing slash */ 5945789Sahrens if (len < strlen(zd->zd_dataset) && 5946789Sahrens bcmp(dataset, zd->zd_dataset, len) == 0 && 5947789Sahrens zd->zd_dataset[len] == '/') { 5948789Sahrens if (write) 5949789Sahrens *write = 0; 5950789Sahrens return (1); 5951789Sahrens } 5952789Sahrens } 5953789Sahrens 5954789Sahrens return (0); 5955789Sahrens } 59561676Sjpk 59571676Sjpk /* 59581676Sjpk * zone_find_by_any_path() - 59591676Sjpk * 59601676Sjpk * kernel-private routine similar to zone_find_by_path(), but which 59611676Sjpk * effectively compares against zone paths rather than zonerootpath 59621676Sjpk * (i.e., the last component of zonerootpaths, which should be "root/", 59631676Sjpk * are not compared.) This is done in order to accurately identify all 59641676Sjpk * paths, whether zone-visible or not, including those which are parallel 59651676Sjpk * to /root/, such as /dev/, /home/, etc... 59661676Sjpk * 59671676Sjpk * If the specified path does not fall under any zone path then global 59681676Sjpk * zone is returned. 59691676Sjpk * 59701676Sjpk * The treat_abs parameter indicates whether the path should be treated as 59711676Sjpk * an absolute path although it does not begin with "/". (This supports 59721676Sjpk * nfs mount syntax such as host:any/path.) 59731676Sjpk * 59741676Sjpk * The caller is responsible for zone_rele of the returned zone. 59751676Sjpk */ 59761676Sjpk zone_t * 59771676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs) 59781676Sjpk { 59791676Sjpk zone_t *zone; 59801676Sjpk int path_offset = 0; 59811676Sjpk 59821676Sjpk if (path == NULL) { 59831676Sjpk zone_hold(global_zone); 59841676Sjpk return (global_zone); 59851676Sjpk } 59861676Sjpk 59871676Sjpk if (*path != '/') { 59881676Sjpk ASSERT(treat_abs); 59891676Sjpk path_offset = 1; 59901676Sjpk } 59911676Sjpk 59921676Sjpk mutex_enter(&zonehash_lock); 59931676Sjpk for (zone = list_head(&zone_active); zone != NULL; 59941676Sjpk zone = list_next(&zone_active, zone)) { 59951676Sjpk char *c; 59961676Sjpk size_t pathlen; 59971876Smp46848 char *rootpath_start; 59981676Sjpk 59991676Sjpk if (zone == global_zone) /* skip global zone */ 60001676Sjpk continue; 60011676Sjpk 60021676Sjpk /* scan backwards to find start of last component */ 60031676Sjpk c = zone->zone_rootpath + zone->zone_rootpathlen - 2; 60041676Sjpk do { 60051676Sjpk c--; 60061676Sjpk } while (*c != '/'); 60071676Sjpk 60081876Smp46848 pathlen = c - zone->zone_rootpath + 1 - path_offset; 60091876Smp46848 rootpath_start = (zone->zone_rootpath + path_offset); 60101876Smp46848 if (strncmp(path, rootpath_start, pathlen) == 0) 60111676Sjpk break; 60121676Sjpk } 60131676Sjpk if (zone == NULL) 60141676Sjpk zone = global_zone; 60151676Sjpk zone_hold(zone); 60161676Sjpk mutex_exit(&zonehash_lock); 60171676Sjpk return (zone); 60181676Sjpk } 60193448Sdh155122 60203448Sdh155122 /* 602110616SSebastien.Roy@Sun.COM * Finds a zone_dl_t with the given linkid in the given zone. Returns the 602210616SSebastien.Roy@Sun.COM * zone_dl_t pointer if found, and NULL otherwise. 60233448Sdh155122 */ 602410616SSebastien.Roy@Sun.COM static zone_dl_t * 602510616SSebastien.Roy@Sun.COM zone_find_dl(zone_t *zone, datalink_id_t linkid) 602610616SSebastien.Roy@Sun.COM { 602710616SSebastien.Roy@Sun.COM zone_dl_t *zdl; 602810616SSebastien.Roy@Sun.COM 602910616SSebastien.Roy@Sun.COM ASSERT(mutex_owned(&zone->zone_lock)); 603010616SSebastien.Roy@Sun.COM for (zdl = list_head(&zone->zone_dl_list); zdl != NULL; 603110616SSebastien.Roy@Sun.COM zdl = list_next(&zone->zone_dl_list, zdl)) { 603210616SSebastien.Roy@Sun.COM if (zdl->zdl_id == linkid) 603310616SSebastien.Roy@Sun.COM break; 603410616SSebastien.Roy@Sun.COM } 603510616SSebastien.Roy@Sun.COM return (zdl); 603610616SSebastien.Roy@Sun.COM } 603710616SSebastien.Roy@Sun.COM 60383448Sdh155122 static boolean_t 603910616SSebastien.Roy@Sun.COM zone_dl_exists(zone_t *zone, datalink_id_t linkid) 60403448Sdh155122 { 604110616SSebastien.Roy@Sun.COM boolean_t exists; 60423448Sdh155122 60433448Sdh155122 mutex_enter(&zone->zone_lock); 604410616SSebastien.Roy@Sun.COM exists = (zone_find_dl(zone, linkid) != NULL); 60453448Sdh155122 mutex_exit(&zone->zone_lock); 604610616SSebastien.Roy@Sun.COM return (exists); 60473448Sdh155122 } 60483448Sdh155122 60493448Sdh155122 /* 605010616SSebastien.Roy@Sun.COM * Add an data link name for the zone. 60513448Sdh155122 */ 60523448Sdh155122 static int 605310616SSebastien.Roy@Sun.COM zone_add_datalink(zoneid_t zoneid, datalink_id_t linkid) 60543448Sdh155122 { 605510616SSebastien.Roy@Sun.COM zone_dl_t *zdl; 60563448Sdh155122 zone_t *zone; 60573448Sdh155122 zone_t *thiszone; 605810616SSebastien.Roy@Sun.COM 605910616SSebastien.Roy@Sun.COM if ((thiszone = zone_find_by_id(zoneid)) == NULL) 60603448Sdh155122 return (set_errno(ENXIO)); 606110616SSebastien.Roy@Sun.COM 606210616SSebastien.Roy@Sun.COM /* Verify that the datalink ID doesn't already belong to a zone. */ 60633448Sdh155122 mutex_enter(&zonehash_lock); 60643448Sdh155122 for (zone = list_head(&zone_active); zone != NULL; 60653448Sdh155122 zone = list_next(&zone_active, zone)) { 606610616SSebastien.Roy@Sun.COM if (zone_dl_exists(zone, linkid)) { 60673448Sdh155122 mutex_exit(&zonehash_lock); 60683448Sdh155122 zone_rele(thiszone); 606910616SSebastien.Roy@Sun.COM return (set_errno((zone == thiszone) ? EEXIST : EPERM)); 60703448Sdh155122 } 60713448Sdh155122 } 607210616SSebastien.Roy@Sun.COM 607310616SSebastien.Roy@Sun.COM zdl = kmem_zalloc(sizeof (*zdl), KM_SLEEP); 607410616SSebastien.Roy@Sun.COM zdl->zdl_id = linkid; 60753448Sdh155122 mutex_enter(&thiszone->zone_lock); 607610616SSebastien.Roy@Sun.COM list_insert_head(&thiszone->zone_dl_list, zdl); 60773448Sdh155122 mutex_exit(&thiszone->zone_lock); 60783448Sdh155122 mutex_exit(&zonehash_lock); 60793448Sdh155122 zone_rele(thiszone); 60803448Sdh155122 return (0); 60813448Sdh155122 } 60823448Sdh155122 60833448Sdh155122 static int 608410616SSebastien.Roy@Sun.COM zone_remove_datalink(zoneid_t zoneid, datalink_id_t linkid) 60853448Sdh155122 { 608610616SSebastien.Roy@Sun.COM zone_dl_t *zdl; 60873448Sdh155122 zone_t *zone; 608810616SSebastien.Roy@Sun.COM int err = 0; 608910616SSebastien.Roy@Sun.COM 609010616SSebastien.Roy@Sun.COM if ((zone = zone_find_by_id(zoneid)) == NULL) 60913448Sdh155122 return (set_errno(EINVAL)); 60923448Sdh155122 60933448Sdh155122 mutex_enter(&zone->zone_lock); 609410616SSebastien.Roy@Sun.COM if ((zdl = zone_find_dl(zone, linkid)) == NULL) { 609510616SSebastien.Roy@Sun.COM err = ENXIO; 609610616SSebastien.Roy@Sun.COM } else { 609710616SSebastien.Roy@Sun.COM list_remove(&zone->zone_dl_list, zdl); 609810616SSebastien.Roy@Sun.COM kmem_free(zdl, sizeof (zone_dl_t)); 60993448Sdh155122 } 61003448Sdh155122 mutex_exit(&zone->zone_lock); 61013448Sdh155122 zone_rele(zone); 610210616SSebastien.Roy@Sun.COM return (err == 0 ? 0 : set_errno(err)); 61033448Sdh155122 } 61043448Sdh155122 61053448Sdh155122 /* 610610616SSebastien.Roy@Sun.COM * Using the zoneidp as ALL_ZONES, we can lookup which zone has been assigned 610710616SSebastien.Roy@Sun.COM * the linkid. Otherwise we just check if the specified zoneidp has been 610810616SSebastien.Roy@Sun.COM * assigned the supplied linkid. 61093448Sdh155122 */ 611010616SSebastien.Roy@Sun.COM int 611110616SSebastien.Roy@Sun.COM zone_check_datalink(zoneid_t *zoneidp, datalink_id_t linkid) 61123448Sdh155122 { 61133448Sdh155122 zone_t *zone; 611410616SSebastien.Roy@Sun.COM int err = ENXIO; 611510616SSebastien.Roy@Sun.COM 611610616SSebastien.Roy@Sun.COM if (*zoneidp != ALL_ZONES) { 611710616SSebastien.Roy@Sun.COM if ((zone = zone_find_by_id(*zoneidp)) != NULL) { 611810616SSebastien.Roy@Sun.COM if (zone_dl_exists(zone, linkid)) 611910616SSebastien.Roy@Sun.COM err = 0; 612010616SSebastien.Roy@Sun.COM zone_rele(zone); 612110616SSebastien.Roy@Sun.COM } 612210616SSebastien.Roy@Sun.COM return (err); 612310616SSebastien.Roy@Sun.COM } 612410616SSebastien.Roy@Sun.COM 61253448Sdh155122 mutex_enter(&zonehash_lock); 61263448Sdh155122 for (zone = list_head(&zone_active); zone != NULL; 61273448Sdh155122 zone = list_next(&zone_active, zone)) { 612810616SSebastien.Roy@Sun.COM if (zone_dl_exists(zone, linkid)) { 612910616SSebastien.Roy@Sun.COM *zoneidp = zone->zone_id; 613010616SSebastien.Roy@Sun.COM err = 0; 613110616SSebastien.Roy@Sun.COM break; 61323448Sdh155122 } 61333448Sdh155122 } 61343448Sdh155122 mutex_exit(&zonehash_lock); 613510616SSebastien.Roy@Sun.COM return (err); 61363448Sdh155122 } 61373448Sdh155122 61383448Sdh155122 /* 613910616SSebastien.Roy@Sun.COM * Get the list of datalink IDs assigned to a zone. 614010616SSebastien.Roy@Sun.COM * 614110616SSebastien.Roy@Sun.COM * On input, *nump is the number of datalink IDs that can fit in the supplied 614210616SSebastien.Roy@Sun.COM * idarray. Upon return, *nump is either set to the number of datalink IDs 614310616SSebastien.Roy@Sun.COM * that were placed in the array if the array was large enough, or to the 614410616SSebastien.Roy@Sun.COM * number of datalink IDs that the function needs to place in the array if the 614510616SSebastien.Roy@Sun.COM * array is too small. 61463448Sdh155122 */ 61473448Sdh155122 static int 614810616SSebastien.Roy@Sun.COM zone_list_datalink(zoneid_t zoneid, int *nump, datalink_id_t *idarray) 61493448Sdh155122 { 615010616SSebastien.Roy@Sun.COM uint_t num, dlcount; 61513448Sdh155122 zone_t *zone; 615210616SSebastien.Roy@Sun.COM zone_dl_t *zdl; 615310616SSebastien.Roy@Sun.COM datalink_id_t *idptr = idarray; 61543448Sdh155122 61553448Sdh155122 if (copyin(nump, &dlcount, sizeof (dlcount)) != 0) 61563448Sdh155122 return (set_errno(EFAULT)); 615710616SSebastien.Roy@Sun.COM if ((zone = zone_find_by_id(zoneid)) == NULL) 61583448Sdh155122 return (set_errno(ENXIO)); 61593448Sdh155122 61603448Sdh155122 num = 0; 61613448Sdh155122 mutex_enter(&zone->zone_lock); 616210616SSebastien.Roy@Sun.COM for (zdl = list_head(&zone->zone_dl_list); zdl != NULL; 616310616SSebastien.Roy@Sun.COM zdl = list_next(&zone->zone_dl_list, zdl)) { 61643448Sdh155122 /* 616510616SSebastien.Roy@Sun.COM * If the list is bigger than what the caller supplied, just 616610616SSebastien.Roy@Sun.COM * count, don't do copyout. 61673448Sdh155122 */ 61683448Sdh155122 if (++num > dlcount) 61693448Sdh155122 continue; 617010616SSebastien.Roy@Sun.COM if (copyout(&zdl->zdl_id, idptr, sizeof (*idptr)) != 0) { 61713448Sdh155122 mutex_exit(&zone->zone_lock); 61723448Sdh155122 zone_rele(zone); 61733448Sdh155122 return (set_errno(EFAULT)); 61743448Sdh155122 } 617510616SSebastien.Roy@Sun.COM idptr++; 61763448Sdh155122 } 61773448Sdh155122 mutex_exit(&zone->zone_lock); 61783448Sdh155122 zone_rele(zone); 61793448Sdh155122 61803448Sdh155122 /* Increased or decreased, caller should be notified. */ 61813448Sdh155122 if (num != dlcount) { 618210616SSebastien.Roy@Sun.COM if (copyout(&num, nump, sizeof (num)) != 0) 61833448Sdh155122 return (set_errno(EFAULT)); 61843448Sdh155122 } 61853448Sdh155122 return (0); 61863448Sdh155122 } 61873448Sdh155122 61883448Sdh155122 /* 61893448Sdh155122 * Public interface for looking up a zone by zoneid. It's a customized version 61905880Snordmark * for netstack_zone_create(). It can only be called from the zsd create 61915880Snordmark * callbacks, since it doesn't have reference on the zone structure hence if 61925880Snordmark * it is called elsewhere the zone could disappear after the zonehash_lock 61935880Snordmark * is dropped. 61945880Snordmark * 61955880Snordmark * Furthermore it 61965880Snordmark * 1. Doesn't check the status of the zone. 61975880Snordmark * 2. It will be called even before zone_init is called, in that case the 61983448Sdh155122 * address of zone0 is returned directly, and netstack_zone_create() 61993448Sdh155122 * will only assign a value to zone0.zone_netstack, won't break anything. 62005880Snordmark * 3. Returns without the zone being held. 62013448Sdh155122 */ 62023448Sdh155122 zone_t * 62033448Sdh155122 zone_find_by_id_nolock(zoneid_t zoneid) 62043448Sdh155122 { 62055880Snordmark zone_t *zone; 62065880Snordmark 62075880Snordmark mutex_enter(&zonehash_lock); 62083448Sdh155122 if (zonehashbyid == NULL) 62095880Snordmark zone = &zone0; 62103448Sdh155122 else 62115880Snordmark zone = zone_find_all_by_id(zoneid); 62125880Snordmark mutex_exit(&zonehash_lock); 62135880Snordmark return (zone); 62143448Sdh155122 } 62155895Syz147064 62165895Syz147064 /* 62175895Syz147064 * Walk the datalinks for a given zone 62185895Syz147064 */ 62195895Syz147064 int 622010616SSebastien.Roy@Sun.COM zone_datalink_walk(zoneid_t zoneid, int (*cb)(datalink_id_t, void *), 622110616SSebastien.Roy@Sun.COM void *data) 62225895Syz147064 { 622310616SSebastien.Roy@Sun.COM zone_t *zone; 622410616SSebastien.Roy@Sun.COM zone_dl_t *zdl; 622510616SSebastien.Roy@Sun.COM datalink_id_t *idarray; 622610616SSebastien.Roy@Sun.COM uint_t idcount = 0; 622710616SSebastien.Roy@Sun.COM int i, ret = 0; 62285895Syz147064 62295895Syz147064 if ((zone = zone_find_by_id(zoneid)) == NULL) 62305895Syz147064 return (ENOENT); 62315895Syz147064 623210616SSebastien.Roy@Sun.COM /* 623310616SSebastien.Roy@Sun.COM * We first build an array of linkid's so that we can walk these and 623410616SSebastien.Roy@Sun.COM * execute the callback with the zone_lock dropped. 623510616SSebastien.Roy@Sun.COM */ 62365895Syz147064 mutex_enter(&zone->zone_lock); 623710616SSebastien.Roy@Sun.COM for (zdl = list_head(&zone->zone_dl_list); zdl != NULL; 623810616SSebastien.Roy@Sun.COM zdl = list_next(&zone->zone_dl_list, zdl)) { 623910616SSebastien.Roy@Sun.COM idcount++; 624010616SSebastien.Roy@Sun.COM } 624110616SSebastien.Roy@Sun.COM 624210616SSebastien.Roy@Sun.COM if (idcount == 0) { 624310616SSebastien.Roy@Sun.COM mutex_exit(&zone->zone_lock); 624410616SSebastien.Roy@Sun.COM zone_rele(zone); 624510616SSebastien.Roy@Sun.COM return (0); 624610616SSebastien.Roy@Sun.COM } 624710616SSebastien.Roy@Sun.COM 624810616SSebastien.Roy@Sun.COM idarray = kmem_alloc(sizeof (datalink_id_t) * idcount, KM_NOSLEEP); 624910616SSebastien.Roy@Sun.COM if (idarray == NULL) { 625010616SSebastien.Roy@Sun.COM mutex_exit(&zone->zone_lock); 625110616SSebastien.Roy@Sun.COM zone_rele(zone); 625210616SSebastien.Roy@Sun.COM return (ENOMEM); 625310616SSebastien.Roy@Sun.COM } 625410616SSebastien.Roy@Sun.COM 625510616SSebastien.Roy@Sun.COM for (i = 0, zdl = list_head(&zone->zone_dl_list); zdl != NULL; 625610616SSebastien.Roy@Sun.COM i++, zdl = list_next(&zone->zone_dl_list, zdl)) { 625710616SSebastien.Roy@Sun.COM idarray[i] = zdl->zdl_id; 625810616SSebastien.Roy@Sun.COM } 625910616SSebastien.Roy@Sun.COM 626010616SSebastien.Roy@Sun.COM mutex_exit(&zone->zone_lock); 626110616SSebastien.Roy@Sun.COM 626210616SSebastien.Roy@Sun.COM for (i = 0; i < idcount && ret == 0; i++) { 626310616SSebastien.Roy@Sun.COM if ((ret = (*cb)(idarray[i], data)) != 0) 62645895Syz147064 break; 62655895Syz147064 } 626610616SSebastien.Roy@Sun.COM 62675895Syz147064 zone_rele(zone); 626810616SSebastien.Roy@Sun.COM kmem_free(idarray, sizeof (datalink_id_t) * idcount); 62695895Syz147064 return (ret); 62705895Syz147064 } 6271