1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Zones 29 * 30 * A zone is a named collection of processes, namespace constraints, 31 * and other system resources which comprise a secure and manageable 32 * application containment facility. 33 * 34 * Zones (represented by the reference counted zone_t) are tracked in 35 * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs 36 * (zoneid_t) are used to track zone association. Zone IDs are 37 * dynamically generated when the zone is created; if a persistent 38 * identifier is needed (core files, accounting logs, audit trail, 39 * etc.), the zone name should be used. 40 * 41 * 42 * Global Zone: 43 * 44 * The global zone (zoneid 0) is automatically associated with all 45 * system resources that have not been bound to a user-created zone. 46 * This means that even systems where zones are not in active use 47 * have a global zone, and all processes, mounts, etc. are 48 * associated with that zone. The global zone is generally 49 * unconstrained in terms of privileges and access, though the usual 50 * credential and privilege based restrictions apply. 51 * 52 * 53 * Zone States: 54 * 55 * The states in which a zone may be in and the transitions are as 56 * follows: 57 * 58 * ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially 59 * initialized zone is added to the list of active zones on the system but 60 * isn't accessible. 61 * 62 * ZONE_IS_INITIALIZED: Initialization complete except the ZSD callbacks are 63 * not yet completed. Not possible to enter the zone, but attributes can 64 * be retrieved. 65 * 66 * ZONE_IS_READY: zsched (the kernel dummy process for a zone) is 67 * ready. The zone is made visible after the ZSD constructor callbacks are 68 * executed. A zone remains in this state until it transitions into 69 * the ZONE_IS_BOOTING state as a result of a call to zone_boot(). 70 * 71 * ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start 72 * init. Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN 73 * state. 74 * 75 * ZONE_IS_RUNNING: The zone is open for business: zsched has 76 * successfully started init. A zone remains in this state until 77 * zone_shutdown() is called. 78 * 79 * ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is 80 * killing all processes running in the zone. The zone remains 81 * in this state until there are no more user processes running in the zone. 82 * zone_create(), zone_enter(), and zone_destroy() on this zone will fail. 83 * Since zone_shutdown() is restartable, it may be called successfully 84 * multiple times for the same zone_t. Setting of the zone's state to 85 * ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check 86 * the zone's status without worrying about it being a moving target. 87 * 88 * ZONE_IS_EMPTY: zone_shutdown() has been called, and there 89 * are no more user processes in the zone. The zone remains in this 90 * state until there are no more kernel threads associated with the 91 * zone. zone_create(), zone_enter(), and zone_destroy() on this zone will 92 * fail. 93 * 94 * ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone 95 * have exited. zone_shutdown() returns. Henceforth it is not possible to 96 * join the zone or create kernel threads therein. 97 * 98 * ZONE_IS_DYING: zone_destroy() has been called on the zone; zone 99 * remains in this state until zsched exits. Calls to zone_find_by_*() 100 * return NULL from now on. 101 * 102 * ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0). There are no 103 * processes or threads doing work on behalf of the zone. The zone is 104 * removed from the list of active zones. zone_destroy() returns, and 105 * the zone can be recreated. 106 * 107 * ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor 108 * callbacks are executed, and all memory associated with the zone is 109 * freed. 110 * 111 * Threads can wait for the zone to enter a requested state by using 112 * zone_status_wait() or zone_status_timedwait() with the desired 113 * state passed in as an argument. Zone state transitions are 114 * uni-directional; it is not possible to move back to an earlier state. 115 * 116 * 117 * Zone-Specific Data: 118 * 119 * Subsystems needing to maintain zone-specific data can store that 120 * data using the ZSD mechanism. This provides a zone-specific data 121 * store, similar to thread-specific data (see pthread_getspecific(3C) 122 * or the TSD code in uts/common/disp/thread.c. Also, ZSD can be used 123 * to register callbacks to be invoked when a zone is created, shut 124 * down, or destroyed. This can be used to initialize zone-specific 125 * data for new zones and to clean up when zones go away. 126 * 127 * 128 * Data Structures: 129 * 130 * The per-zone structure (zone_t) is reference counted, and freed 131 * when all references are released. zone_hold and zone_rele can be 132 * used to adjust the reference count. In addition, reference counts 133 * associated with the cred_t structure are tracked separately using 134 * zone_cred_hold and zone_cred_rele. 135 * 136 * Pointers to active zone_t's are stored in two hash tables; one 137 * for searching by id, the other for searching by name. Lookups 138 * can be performed on either basis, using zone_find_by_id and 139 * zone_find_by_name. Both return zone_t pointers with the zone 140 * held, so zone_rele should be called when the pointer is no longer 141 * needed. Zones can also be searched by path; zone_find_by_path 142 * returns the zone with which a path name is associated (global 143 * zone if the path is not within some other zone's file system 144 * hierarchy). This currently requires iterating through each zone, 145 * so it is slower than an id or name search via a hash table. 146 * 147 * 148 * Locking: 149 * 150 * zonehash_lock: This is a top-level global lock used to protect the 151 * zone hash tables and lists. Zones cannot be created or destroyed 152 * while this lock is held. 153 * zone_status_lock: This is a global lock protecting zone state. 154 * Zones cannot change state while this lock is held. It also 155 * protects the list of kernel threads associated with a zone. 156 * zone_lock: This is a per-zone lock used to protect several fields of 157 * the zone_t (see <sys/zone.h> for details). In addition, holding 158 * this lock means that the zone cannot go away. 159 * zone_nlwps_lock: This is a per-zone lock used to protect the fields 160 * related to the zone.max-lwps rctl. 161 * zone_mem_lock: This is a per-zone lock used to protect the fields 162 * related to the zone.max-locked-memory and zone.max-swap rctls. 163 * zsd_key_lock: This is a global lock protecting the key state for ZSD. 164 * zone_deathrow_lock: This is a global lock protecting the "deathrow" 165 * list (a list of zones in the ZONE_IS_DEAD state). 166 * 167 * Ordering requirements: 168 * pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock --> 169 * zone_lock --> zsd_key_lock --> pidlock --> p_lock 170 * 171 * When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is: 172 * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 173 * zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock 174 * 175 * Blocking memory allocations are permitted while holding any of the 176 * zone locks. 177 * 178 * 179 * System Call Interface: 180 * 181 * The zone subsystem can be managed and queried from user level with 182 * the following system calls (all subcodes of the primary "zone" 183 * system call): 184 * - zone_create: creates a zone with selected attributes (name, 185 * root path, privileges, resource controls, ZFS datasets) 186 * - zone_enter: allows the current process to enter a zone 187 * - zone_getattr: reports attributes of a zone 188 * - zone_setattr: set attributes of a zone 189 * - zone_boot: set 'init' running for the zone 190 * - zone_list: lists all zones active in the system 191 * - zone_lookup: looks up zone id based on name 192 * - zone_shutdown: initiates shutdown process (see states above) 193 * - zone_destroy: completes shutdown process (see states above) 194 * 195 */ 196 197 #include <sys/priv_impl.h> 198 #include <sys/cred.h> 199 #include <c2/audit.h> 200 #include <sys/debug.h> 201 #include <sys/file.h> 202 #include <sys/kmem.h> 203 #include <sys/kstat.h> 204 #include <sys/mutex.h> 205 #include <sys/note.h> 206 #include <sys/pathname.h> 207 #include <sys/proc.h> 208 #include <sys/project.h> 209 #include <sys/sysevent.h> 210 #include <sys/task.h> 211 #include <sys/systm.h> 212 #include <sys/types.h> 213 #include <sys/utsname.h> 214 #include <sys/vnode.h> 215 #include <sys/vfs.h> 216 #include <sys/systeminfo.h> 217 #include <sys/policy.h> 218 #include <sys/cred_impl.h> 219 #include <sys/contract_impl.h> 220 #include <sys/contract/process_impl.h> 221 #include <sys/class.h> 222 #include <sys/pool.h> 223 #include <sys/pool_pset.h> 224 #include <sys/pset.h> 225 #include <sys/sysmacros.h> 226 #include <sys/callb.h> 227 #include <sys/vmparam.h> 228 #include <sys/corectl.h> 229 #include <sys/ipc_impl.h> 230 231 #include <sys/door.h> 232 #include <sys/cpuvar.h> 233 #include <sys/sdt.h> 234 235 #include <sys/uadmin.h> 236 #include <sys/session.h> 237 #include <sys/cmn_err.h> 238 #include <sys/modhash.h> 239 #include <sys/sunddi.h> 240 #include <sys/nvpair.h> 241 #include <sys/rctl.h> 242 #include <sys/fss.h> 243 #include <sys/brand.h> 244 #include <sys/zone.h> 245 #include <net/if.h> 246 #include <sys/cpucaps.h> 247 #include <vm/seg.h> 248 #include <sys/mac.h> 249 250 /* List of data link IDs which are accessible from the zone */ 251 typedef struct zone_dl { 252 datalink_id_t zdl_id; 253 list_node_t zdl_linkage; 254 } zone_dl_t; 255 256 /* 257 * cv used to signal that all references to the zone have been released. This 258 * needs to be global since there may be multiple waiters, and the first to 259 * wake up will free the zone_t, hence we cannot use zone->zone_cv. 260 */ 261 static kcondvar_t zone_destroy_cv; 262 /* 263 * Lock used to serialize access to zone_cv. This could have been per-zone, 264 * but then we'd need another lock for zone_destroy_cv, and why bother? 265 */ 266 static kmutex_t zone_status_lock; 267 268 /* 269 * ZSD-related global variables. 270 */ 271 static kmutex_t zsd_key_lock; /* protects the following two */ 272 /* 273 * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 274 */ 275 static zone_key_t zsd_keyval = 0; 276 /* 277 * Global list of registered keys. We use this when a new zone is created. 278 */ 279 static list_t zsd_registered_keys; 280 281 int zone_hash_size = 256; 282 static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel; 283 static kmutex_t zonehash_lock; 284 static uint_t zonecount; 285 static id_space_t *zoneid_space; 286 287 /* 288 * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 289 * kernel proper runs, and which manages all other zones. 290 * 291 * Although not declared as static, the variable "zone0" should not be used 292 * except for by code that needs to reference the global zone early on in boot, 293 * before it is fully initialized. All other consumers should use 294 * 'global_zone'. 295 */ 296 zone_t zone0; 297 zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 298 299 /* 300 * List of active zones, protected by zonehash_lock. 301 */ 302 static list_t zone_active; 303 304 /* 305 * List of destroyed zones that still have outstanding cred references. 306 * Used for debugging. Uses a separate lock to avoid lock ordering 307 * problems in zone_free. 308 */ 309 static list_t zone_deathrow; 310 static kmutex_t zone_deathrow_lock; 311 312 /* number of zones is limited by virtual interface limit in IP */ 313 uint_t maxzones = 8192; 314 315 /* Event channel to sent zone state change notifications */ 316 evchan_t *zone_event_chan; 317 318 /* 319 * This table holds the mapping from kernel zone states to 320 * states visible in the state notification API. 321 * The idea is that we only expose "obvious" states and 322 * do not expose states which are just implementation details. 323 */ 324 const char *zone_status_table[] = { 325 ZONE_EVENT_UNINITIALIZED, /* uninitialized */ 326 ZONE_EVENT_INITIALIZED, /* initialized */ 327 ZONE_EVENT_READY, /* ready */ 328 ZONE_EVENT_READY, /* booting */ 329 ZONE_EVENT_RUNNING, /* running */ 330 ZONE_EVENT_SHUTTING_DOWN, /* shutting_down */ 331 ZONE_EVENT_SHUTTING_DOWN, /* empty */ 332 ZONE_EVENT_SHUTTING_DOWN, /* down */ 333 ZONE_EVENT_SHUTTING_DOWN, /* dying */ 334 ZONE_EVENT_UNINITIALIZED, /* dead */ 335 }; 336 337 /* 338 * This isn't static so lint doesn't complain. 339 */ 340 rctl_hndl_t rc_zone_cpu_shares; 341 rctl_hndl_t rc_zone_locked_mem; 342 rctl_hndl_t rc_zone_max_swap; 343 rctl_hndl_t rc_zone_cpu_cap; 344 rctl_hndl_t rc_zone_nlwps; 345 rctl_hndl_t rc_zone_shmmax; 346 rctl_hndl_t rc_zone_shmmni; 347 rctl_hndl_t rc_zone_semmni; 348 rctl_hndl_t rc_zone_msgmni; 349 /* 350 * Synchronization primitives used to synchronize between mounts and zone 351 * creation/destruction. 352 */ 353 static int mounts_in_progress; 354 static kcondvar_t mount_cv; 355 static kmutex_t mount_lock; 356 357 const char * const zone_default_initname = "/sbin/init"; 358 static char * const zone_prefix = "/zone/"; 359 static int zone_shutdown(zoneid_t zoneid); 360 static int zone_add_datalink(zoneid_t, datalink_id_t); 361 static int zone_remove_datalink(zoneid_t, datalink_id_t); 362 static int zone_list_datalink(zoneid_t, int *, datalink_id_t *); 363 364 typedef boolean_t zsd_applyfn_t(kmutex_t *, boolean_t, zone_t *, zone_key_t); 365 366 static void zsd_apply_all_zones(zsd_applyfn_t *, zone_key_t); 367 static void zsd_apply_all_keys(zsd_applyfn_t *, zone_t *); 368 static boolean_t zsd_apply_create(kmutex_t *, boolean_t, zone_t *, zone_key_t); 369 static boolean_t zsd_apply_shutdown(kmutex_t *, boolean_t, zone_t *, 370 zone_key_t); 371 static boolean_t zsd_apply_destroy(kmutex_t *, boolean_t, zone_t *, zone_key_t); 372 static boolean_t zsd_wait_for_creator(zone_t *, struct zsd_entry *, 373 kmutex_t *); 374 static boolean_t zsd_wait_for_inprogress(zone_t *, struct zsd_entry *, 375 kmutex_t *); 376 377 /* 378 * Bump this number when you alter the zone syscall interfaces; this is 379 * because we need to have support for previous API versions in libc 380 * to support patching; libc calls into the kernel to determine this number. 381 * 382 * Version 1 of the API is the version originally shipped with Solaris 10 383 * Version 2 alters the zone_create system call in order to support more 384 * arguments by moving the args into a structure; and to do better 385 * error reporting when zone_create() fails. 386 * Version 3 alters the zone_create system call in order to support the 387 * import of ZFS datasets to zones. 388 * Version 4 alters the zone_create system call in order to support 389 * Trusted Extensions. 390 * Version 5 alters the zone_boot system call, and converts its old 391 * bootargs parameter to be set by the zone_setattr API instead. 392 * Version 6 adds the flag argument to zone_create. 393 */ 394 static const int ZONE_SYSCALL_API_VERSION = 6; 395 396 /* 397 * Certain filesystems (such as NFS and autofs) need to know which zone 398 * the mount is being placed in. Because of this, we need to be able to 399 * ensure that a zone isn't in the process of being created such that 400 * nfs_mount() thinks it is in the global zone, while by the time it 401 * gets added the list of mounted zones, it ends up on zoneA's mount 402 * list. 403 * 404 * The following functions: block_mounts()/resume_mounts() and 405 * mount_in_progress()/mount_completed() are used by zones and the VFS 406 * layer (respectively) to synchronize zone creation and new mounts. 407 * 408 * The semantics are like a reader-reader lock such that there may 409 * either be multiple mounts (or zone creations, if that weren't 410 * serialized by zonehash_lock) in progress at the same time, but not 411 * both. 412 * 413 * We use cv's so the user can ctrl-C out of the operation if it's 414 * taking too long. 415 * 416 * The semantics are such that there is unfair bias towards the 417 * "current" operation. This means that zone creations may starve if 418 * there is a rapid succession of new mounts coming in to the system, or 419 * there is a remote possibility that zones will be created at such a 420 * rate that new mounts will not be able to proceed. 421 */ 422 /* 423 * Prevent new mounts from progressing to the point of calling 424 * VFS_MOUNT(). If there are already mounts in this "region", wait for 425 * them to complete. 426 */ 427 static int 428 block_mounts(void) 429 { 430 int retval = 0; 431 432 /* 433 * Since it may block for a long time, block_mounts() shouldn't be 434 * called with zonehash_lock held. 435 */ 436 ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 437 mutex_enter(&mount_lock); 438 while (mounts_in_progress > 0) { 439 if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 440 goto signaled; 441 } 442 /* 443 * A negative value of mounts_in_progress indicates that mounts 444 * have been blocked by (-mounts_in_progress) different callers. 445 */ 446 mounts_in_progress--; 447 retval = 1; 448 signaled: 449 mutex_exit(&mount_lock); 450 return (retval); 451 } 452 453 /* 454 * The VFS layer may progress with new mounts as far as we're concerned. 455 * Allow them to progress if we were the last obstacle. 456 */ 457 static void 458 resume_mounts(void) 459 { 460 mutex_enter(&mount_lock); 461 if (++mounts_in_progress == 0) 462 cv_broadcast(&mount_cv); 463 mutex_exit(&mount_lock); 464 } 465 466 /* 467 * The VFS layer is busy with a mount; zones should wait until all 468 * mounts are completed to progress. 469 */ 470 void 471 mount_in_progress(void) 472 { 473 mutex_enter(&mount_lock); 474 while (mounts_in_progress < 0) 475 cv_wait(&mount_cv, &mount_lock); 476 mounts_in_progress++; 477 mutex_exit(&mount_lock); 478 } 479 480 /* 481 * VFS is done with one mount; wake up any waiting block_mounts() 482 * callers if this is the last mount. 483 */ 484 void 485 mount_completed(void) 486 { 487 mutex_enter(&mount_lock); 488 if (--mounts_in_progress == 0) 489 cv_broadcast(&mount_cv); 490 mutex_exit(&mount_lock); 491 } 492 493 /* 494 * ZSD routines. 495 * 496 * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 497 * defined by the pthread_key_create() and related interfaces. 498 * 499 * Kernel subsystems may register one or more data items and/or 500 * callbacks to be executed when a zone is created, shutdown, or 501 * destroyed. 502 * 503 * Unlike the thread counterpart, destructor callbacks will be executed 504 * even if the data pointer is NULL and/or there are no constructor 505 * callbacks, so it is the responsibility of such callbacks to check for 506 * NULL data values if necessary. 507 * 508 * The locking strategy and overall picture is as follows: 509 * 510 * When someone calls zone_key_create(), a template ZSD entry is added to the 511 * global list "zsd_registered_keys", protected by zsd_key_lock. While 512 * holding that lock all the existing zones are marked as 513 * ZSD_CREATE_NEEDED and a copy of the ZSD entry added to the per-zone 514 * zone_zsd list (protected by zone_lock). The global list is updated first 515 * (under zone_key_lock) to make sure that newly created zones use the 516 * most recent list of keys. Then under zonehash_lock we walk the zones 517 * and mark them. Similar locking is used in zone_key_delete(). 518 * 519 * The actual create, shutdown, and destroy callbacks are done without 520 * holding any lock. And zsd_flags are used to ensure that the operations 521 * completed so that when zone_key_create (and zone_create) is done, as well as 522 * zone_key_delete (and zone_destroy) is done, all the necessary callbacks 523 * are completed. 524 * 525 * When new zones are created constructor callbacks for all registered ZSD 526 * entries will be called. That also uses the above two phases of marking 527 * what needs to be done, and then running the callbacks without holding 528 * any locks. 529 * 530 * The framework does not provide any locking around zone_getspecific() and 531 * zone_setspecific() apart from that needed for internal consistency, so 532 * callers interested in atomic "test-and-set" semantics will need to provide 533 * their own locking. 534 */ 535 536 /* 537 * Helper function to find the zsd_entry associated with the key in the 538 * given list. 539 */ 540 static struct zsd_entry * 541 zsd_find(list_t *l, zone_key_t key) 542 { 543 struct zsd_entry *zsd; 544 545 for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 546 if (zsd->zsd_key == key) { 547 return (zsd); 548 } 549 } 550 return (NULL); 551 } 552 553 /* 554 * Helper function to find the zsd_entry associated with the key in the 555 * given list. Move it to the front of the list. 556 */ 557 static struct zsd_entry * 558 zsd_find_mru(list_t *l, zone_key_t key) 559 { 560 struct zsd_entry *zsd; 561 562 for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 563 if (zsd->zsd_key == key) { 564 /* 565 * Move to head of list to keep list in MRU order. 566 */ 567 if (zsd != list_head(l)) { 568 list_remove(l, zsd); 569 list_insert_head(l, zsd); 570 } 571 return (zsd); 572 } 573 } 574 return (NULL); 575 } 576 577 void 578 zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 579 void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 580 { 581 struct zsd_entry *zsdp; 582 struct zsd_entry *t; 583 struct zone *zone; 584 zone_key_t key; 585 586 zsdp = kmem_zalloc(sizeof (*zsdp), KM_SLEEP); 587 zsdp->zsd_data = NULL; 588 zsdp->zsd_create = create; 589 zsdp->zsd_shutdown = shutdown; 590 zsdp->zsd_destroy = destroy; 591 592 /* 593 * Insert in global list of callbacks. Makes future zone creations 594 * see it. 595 */ 596 mutex_enter(&zsd_key_lock); 597 key = zsdp->zsd_key = ++zsd_keyval; 598 ASSERT(zsd_keyval != 0); 599 list_insert_tail(&zsd_registered_keys, zsdp); 600 mutex_exit(&zsd_key_lock); 601 602 /* 603 * Insert for all existing zones and mark them as needing 604 * a create callback. 605 */ 606 mutex_enter(&zonehash_lock); /* stop the world */ 607 for (zone = list_head(&zone_active); zone != NULL; 608 zone = list_next(&zone_active, zone)) { 609 zone_status_t status; 610 611 mutex_enter(&zone->zone_lock); 612 613 /* Skip zones that are on the way down or not yet up */ 614 status = zone_status_get(zone); 615 if (status >= ZONE_IS_DOWN || 616 status == ZONE_IS_UNINITIALIZED) { 617 mutex_exit(&zone->zone_lock); 618 continue; 619 } 620 621 t = zsd_find_mru(&zone->zone_zsd, key); 622 if (t != NULL) { 623 /* 624 * A zsd_configure already inserted it after 625 * we dropped zsd_key_lock above. 626 */ 627 mutex_exit(&zone->zone_lock); 628 continue; 629 } 630 t = kmem_zalloc(sizeof (*t), KM_SLEEP); 631 t->zsd_key = key; 632 t->zsd_create = create; 633 t->zsd_shutdown = shutdown; 634 t->zsd_destroy = destroy; 635 if (create != NULL) { 636 t->zsd_flags = ZSD_CREATE_NEEDED; 637 DTRACE_PROBE2(zsd__create__needed, 638 zone_t *, zone, zone_key_t, key); 639 } 640 list_insert_tail(&zone->zone_zsd, t); 641 mutex_exit(&zone->zone_lock); 642 } 643 mutex_exit(&zonehash_lock); 644 645 if (create != NULL) { 646 /* Now call the create callback for this key */ 647 zsd_apply_all_zones(zsd_apply_create, key); 648 } 649 /* 650 * It is safe for consumers to use the key now, make it 651 * globally visible. Specifically zone_getspecific() will 652 * always successfully return the zone specific data associated 653 * with the key. 654 */ 655 *keyp = key; 656 657 } 658 659 /* 660 * Function called when a module is being unloaded, or otherwise wishes 661 * to unregister its ZSD key and callbacks. 662 * 663 * Remove from the global list and determine the functions that need to 664 * be called under a global lock. Then call the functions without 665 * holding any locks. Finally free up the zone_zsd entries. (The apply 666 * functions need to access the zone_zsd entries to find zsd_data etc.) 667 */ 668 int 669 zone_key_delete(zone_key_t key) 670 { 671 struct zsd_entry *zsdp = NULL; 672 zone_t *zone; 673 674 mutex_enter(&zsd_key_lock); 675 zsdp = zsd_find_mru(&zsd_registered_keys, key); 676 if (zsdp == NULL) { 677 mutex_exit(&zsd_key_lock); 678 return (-1); 679 } 680 list_remove(&zsd_registered_keys, zsdp); 681 mutex_exit(&zsd_key_lock); 682 683 mutex_enter(&zonehash_lock); 684 for (zone = list_head(&zone_active); zone != NULL; 685 zone = list_next(&zone_active, zone)) { 686 struct zsd_entry *del; 687 688 mutex_enter(&zone->zone_lock); 689 del = zsd_find_mru(&zone->zone_zsd, key); 690 if (del == NULL) { 691 /* 692 * Somebody else got here first e.g the zone going 693 * away. 694 */ 695 mutex_exit(&zone->zone_lock); 696 continue; 697 } 698 ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 699 ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 700 if (del->zsd_shutdown != NULL && 701 (del->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) { 702 del->zsd_flags |= ZSD_SHUTDOWN_NEEDED; 703 DTRACE_PROBE2(zsd__shutdown__needed, 704 zone_t *, zone, zone_key_t, key); 705 } 706 if (del->zsd_destroy != NULL && 707 (del->zsd_flags & ZSD_DESTROY_ALL) == 0) { 708 del->zsd_flags |= ZSD_DESTROY_NEEDED; 709 DTRACE_PROBE2(zsd__destroy__needed, 710 zone_t *, zone, zone_key_t, key); 711 } 712 mutex_exit(&zone->zone_lock); 713 } 714 mutex_exit(&zonehash_lock); 715 kmem_free(zsdp, sizeof (*zsdp)); 716 717 /* Now call the shutdown and destroy callback for this key */ 718 zsd_apply_all_zones(zsd_apply_shutdown, key); 719 zsd_apply_all_zones(zsd_apply_destroy, key); 720 721 /* Now we can free up the zsdp structures in each zone */ 722 mutex_enter(&zonehash_lock); 723 for (zone = list_head(&zone_active); zone != NULL; 724 zone = list_next(&zone_active, zone)) { 725 struct zsd_entry *del; 726 727 mutex_enter(&zone->zone_lock); 728 del = zsd_find(&zone->zone_zsd, key); 729 if (del != NULL) { 730 list_remove(&zone->zone_zsd, del); 731 ASSERT(!(del->zsd_flags & ZSD_ALL_INPROGRESS)); 732 kmem_free(del, sizeof (*del)); 733 } 734 mutex_exit(&zone->zone_lock); 735 } 736 mutex_exit(&zonehash_lock); 737 738 return (0); 739 } 740 741 /* 742 * ZSD counterpart of pthread_setspecific(). 743 * 744 * Since all zsd callbacks, including those with no create function, 745 * have an entry in zone_zsd, if the key is registered it is part of 746 * the zone_zsd list. 747 * Return an error if the key wasn't registerd. 748 */ 749 int 750 zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 751 { 752 struct zsd_entry *t; 753 754 mutex_enter(&zone->zone_lock); 755 t = zsd_find_mru(&zone->zone_zsd, key); 756 if (t != NULL) { 757 /* 758 * Replace old value with new 759 */ 760 t->zsd_data = (void *)data; 761 mutex_exit(&zone->zone_lock); 762 return (0); 763 } 764 mutex_exit(&zone->zone_lock); 765 return (-1); 766 } 767 768 /* 769 * ZSD counterpart of pthread_getspecific(). 770 */ 771 void * 772 zone_getspecific(zone_key_t key, zone_t *zone) 773 { 774 struct zsd_entry *t; 775 void *data; 776 777 mutex_enter(&zone->zone_lock); 778 t = zsd_find_mru(&zone->zone_zsd, key); 779 data = (t == NULL ? NULL : t->zsd_data); 780 mutex_exit(&zone->zone_lock); 781 return (data); 782 } 783 784 /* 785 * Function used to initialize a zone's list of ZSD callbacks and data 786 * when the zone is being created. The callbacks are initialized from 787 * the template list (zsd_registered_keys). The constructor callback is 788 * executed later (once the zone exists and with locks dropped). 789 */ 790 static void 791 zone_zsd_configure(zone_t *zone) 792 { 793 struct zsd_entry *zsdp; 794 struct zsd_entry *t; 795 796 ASSERT(MUTEX_HELD(&zonehash_lock)); 797 ASSERT(list_head(&zone->zone_zsd) == NULL); 798 mutex_enter(&zone->zone_lock); 799 mutex_enter(&zsd_key_lock); 800 for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 801 zsdp = list_next(&zsd_registered_keys, zsdp)) { 802 /* 803 * Since this zone is ZONE_IS_UNCONFIGURED, zone_key_create 804 * should not have added anything to it. 805 */ 806 ASSERT(zsd_find(&zone->zone_zsd, zsdp->zsd_key) == NULL); 807 808 t = kmem_zalloc(sizeof (*t), KM_SLEEP); 809 t->zsd_key = zsdp->zsd_key; 810 t->zsd_create = zsdp->zsd_create; 811 t->zsd_shutdown = zsdp->zsd_shutdown; 812 t->zsd_destroy = zsdp->zsd_destroy; 813 if (zsdp->zsd_create != NULL) { 814 t->zsd_flags = ZSD_CREATE_NEEDED; 815 DTRACE_PROBE2(zsd__create__needed, 816 zone_t *, zone, zone_key_t, zsdp->zsd_key); 817 } 818 list_insert_tail(&zone->zone_zsd, t); 819 } 820 mutex_exit(&zsd_key_lock); 821 mutex_exit(&zone->zone_lock); 822 } 823 824 enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 825 826 /* 827 * Helper function to execute shutdown or destructor callbacks. 828 */ 829 static void 830 zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 831 { 832 struct zsd_entry *t; 833 834 ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 835 ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 836 ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 837 838 /* 839 * Run the callback solely based on what is registered for the zone 840 * in zone_zsd. The global list can change independently of this 841 * as keys are registered and unregistered and we don't register new 842 * callbacks for a zone that is in the process of going away. 843 */ 844 mutex_enter(&zone->zone_lock); 845 for (t = list_head(&zone->zone_zsd); t != NULL; 846 t = list_next(&zone->zone_zsd, t)) { 847 zone_key_t key = t->zsd_key; 848 849 /* Skip if no callbacks registered */ 850 851 if (ct == ZSD_SHUTDOWN) { 852 if (t->zsd_shutdown != NULL && 853 (t->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) { 854 t->zsd_flags |= ZSD_SHUTDOWN_NEEDED; 855 DTRACE_PROBE2(zsd__shutdown__needed, 856 zone_t *, zone, zone_key_t, key); 857 } 858 } else { 859 if (t->zsd_destroy != NULL && 860 (t->zsd_flags & ZSD_DESTROY_ALL) == 0) { 861 t->zsd_flags |= ZSD_DESTROY_NEEDED; 862 DTRACE_PROBE2(zsd__destroy__needed, 863 zone_t *, zone, zone_key_t, key); 864 } 865 } 866 } 867 mutex_exit(&zone->zone_lock); 868 869 /* Now call the shutdown and destroy callback for this key */ 870 zsd_apply_all_keys(zsd_apply_shutdown, zone); 871 zsd_apply_all_keys(zsd_apply_destroy, zone); 872 873 } 874 875 /* 876 * Called when the zone is going away; free ZSD-related memory, and 877 * destroy the zone_zsd list. 878 */ 879 static void 880 zone_free_zsd(zone_t *zone) 881 { 882 struct zsd_entry *t, *next; 883 884 /* 885 * Free all the zsd_entry's we had on this zone. 886 */ 887 mutex_enter(&zone->zone_lock); 888 for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 889 next = list_next(&zone->zone_zsd, t); 890 list_remove(&zone->zone_zsd, t); 891 ASSERT(!(t->zsd_flags & ZSD_ALL_INPROGRESS)); 892 kmem_free(t, sizeof (*t)); 893 } 894 list_destroy(&zone->zone_zsd); 895 mutex_exit(&zone->zone_lock); 896 897 } 898 899 /* 900 * Apply a function to all zones for particular key value. 901 * 902 * The applyfn has to drop zonehash_lock if it does some work, and 903 * then reacquire it before it returns. 904 * When the lock is dropped we don't follow list_next even 905 * if it is possible to do so without any hazards. This is 906 * because we want the design to allow for the list of zones 907 * to change in any arbitrary way during the time the 908 * lock was dropped. 909 * 910 * It is safe to restart the loop at list_head since the applyfn 911 * changes the zsd_flags as it does work, so a subsequent 912 * pass through will have no effect in applyfn, hence the loop will terminate 913 * in at worst O(N^2). 914 */ 915 static void 916 zsd_apply_all_zones(zsd_applyfn_t *applyfn, zone_key_t key) 917 { 918 zone_t *zone; 919 920 mutex_enter(&zonehash_lock); 921 zone = list_head(&zone_active); 922 while (zone != NULL) { 923 if ((applyfn)(&zonehash_lock, B_FALSE, zone, key)) { 924 /* Lock dropped - restart at head */ 925 zone = list_head(&zone_active); 926 } else { 927 zone = list_next(&zone_active, zone); 928 } 929 } 930 mutex_exit(&zonehash_lock); 931 } 932 933 /* 934 * Apply a function to all keys for a particular zone. 935 * 936 * The applyfn has to drop zonehash_lock if it does some work, and 937 * then reacquire it before it returns. 938 * When the lock is dropped we don't follow list_next even 939 * if it is possible to do so without any hazards. This is 940 * because we want the design to allow for the list of zsd callbacks 941 * to change in any arbitrary way during the time the 942 * lock was dropped. 943 * 944 * It is safe to restart the loop at list_head since the applyfn 945 * changes the zsd_flags as it does work, so a subsequent 946 * pass through will have no effect in applyfn, hence the loop will terminate 947 * in at worst O(N^2). 948 */ 949 static void 950 zsd_apply_all_keys(zsd_applyfn_t *applyfn, zone_t *zone) 951 { 952 struct zsd_entry *t; 953 954 mutex_enter(&zone->zone_lock); 955 t = list_head(&zone->zone_zsd); 956 while (t != NULL) { 957 if ((applyfn)(NULL, B_TRUE, zone, t->zsd_key)) { 958 /* Lock dropped - restart at head */ 959 t = list_head(&zone->zone_zsd); 960 } else { 961 t = list_next(&zone->zone_zsd, t); 962 } 963 } 964 mutex_exit(&zone->zone_lock); 965 } 966 967 /* 968 * Call the create function for the zone and key if CREATE_NEEDED 969 * is set. 970 * If some other thread gets here first and sets CREATE_INPROGRESS, then 971 * we wait for that thread to complete so that we can ensure that 972 * all the callbacks are done when we've looped over all zones/keys. 973 * 974 * When we call the create function, we drop the global held by the 975 * caller, and return true to tell the caller it needs to re-evalute the 976 * state. 977 * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 978 * remains held on exit. 979 */ 980 static boolean_t 981 zsd_apply_create(kmutex_t *lockp, boolean_t zone_lock_held, 982 zone_t *zone, zone_key_t key) 983 { 984 void *result; 985 struct zsd_entry *t; 986 boolean_t dropped; 987 988 if (lockp != NULL) { 989 ASSERT(MUTEX_HELD(lockp)); 990 } 991 if (zone_lock_held) { 992 ASSERT(MUTEX_HELD(&zone->zone_lock)); 993 } else { 994 mutex_enter(&zone->zone_lock); 995 } 996 997 t = zsd_find(&zone->zone_zsd, key); 998 if (t == NULL) { 999 /* 1000 * Somebody else got here first e.g the zone going 1001 * away. 1002 */ 1003 if (!zone_lock_held) 1004 mutex_exit(&zone->zone_lock); 1005 return (B_FALSE); 1006 } 1007 dropped = B_FALSE; 1008 if (zsd_wait_for_inprogress(zone, t, lockp)) 1009 dropped = B_TRUE; 1010 1011 if (t->zsd_flags & ZSD_CREATE_NEEDED) { 1012 t->zsd_flags &= ~ZSD_CREATE_NEEDED; 1013 t->zsd_flags |= ZSD_CREATE_INPROGRESS; 1014 DTRACE_PROBE2(zsd__create__inprogress, 1015 zone_t *, zone, zone_key_t, key); 1016 mutex_exit(&zone->zone_lock); 1017 if (lockp != NULL) 1018 mutex_exit(lockp); 1019 1020 dropped = B_TRUE; 1021 ASSERT(t->zsd_create != NULL); 1022 DTRACE_PROBE2(zsd__create__start, 1023 zone_t *, zone, zone_key_t, key); 1024 1025 result = (*t->zsd_create)(zone->zone_id); 1026 1027 DTRACE_PROBE2(zsd__create__end, 1028 zone_t *, zone, voidn *, result); 1029 1030 ASSERT(result != NULL); 1031 if (lockp != NULL) 1032 mutex_enter(lockp); 1033 mutex_enter(&zone->zone_lock); 1034 t->zsd_data = result; 1035 t->zsd_flags &= ~ZSD_CREATE_INPROGRESS; 1036 t->zsd_flags |= ZSD_CREATE_COMPLETED; 1037 cv_broadcast(&t->zsd_cv); 1038 DTRACE_PROBE2(zsd__create__completed, 1039 zone_t *, zone, zone_key_t, key); 1040 } 1041 if (!zone_lock_held) 1042 mutex_exit(&zone->zone_lock); 1043 return (dropped); 1044 } 1045 1046 /* 1047 * Call the shutdown function for the zone and key if SHUTDOWN_NEEDED 1048 * is set. 1049 * If some other thread gets here first and sets *_INPROGRESS, then 1050 * we wait for that thread to complete so that we can ensure that 1051 * all the callbacks are done when we've looped over all zones/keys. 1052 * 1053 * When we call the shutdown function, we drop the global held by the 1054 * caller, and return true to tell the caller it needs to re-evalute the 1055 * state. 1056 * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 1057 * remains held on exit. 1058 */ 1059 static boolean_t 1060 zsd_apply_shutdown(kmutex_t *lockp, boolean_t zone_lock_held, 1061 zone_t *zone, zone_key_t key) 1062 { 1063 struct zsd_entry *t; 1064 void *data; 1065 boolean_t dropped; 1066 1067 if (lockp != NULL) { 1068 ASSERT(MUTEX_HELD(lockp)); 1069 } 1070 if (zone_lock_held) { 1071 ASSERT(MUTEX_HELD(&zone->zone_lock)); 1072 } else { 1073 mutex_enter(&zone->zone_lock); 1074 } 1075 1076 t = zsd_find(&zone->zone_zsd, key); 1077 if (t == NULL) { 1078 /* 1079 * Somebody else got here first e.g the zone going 1080 * away. 1081 */ 1082 if (!zone_lock_held) 1083 mutex_exit(&zone->zone_lock); 1084 return (B_FALSE); 1085 } 1086 dropped = B_FALSE; 1087 if (zsd_wait_for_creator(zone, t, lockp)) 1088 dropped = B_TRUE; 1089 1090 if (zsd_wait_for_inprogress(zone, t, lockp)) 1091 dropped = B_TRUE; 1092 1093 if (t->zsd_flags & ZSD_SHUTDOWN_NEEDED) { 1094 t->zsd_flags &= ~ZSD_SHUTDOWN_NEEDED; 1095 t->zsd_flags |= ZSD_SHUTDOWN_INPROGRESS; 1096 DTRACE_PROBE2(zsd__shutdown__inprogress, 1097 zone_t *, zone, zone_key_t, key); 1098 mutex_exit(&zone->zone_lock); 1099 if (lockp != NULL) 1100 mutex_exit(lockp); 1101 dropped = B_TRUE; 1102 1103 ASSERT(t->zsd_shutdown != NULL); 1104 data = t->zsd_data; 1105 1106 DTRACE_PROBE2(zsd__shutdown__start, 1107 zone_t *, zone, zone_key_t, key); 1108 1109 (t->zsd_shutdown)(zone->zone_id, data); 1110 DTRACE_PROBE2(zsd__shutdown__end, 1111 zone_t *, zone, zone_key_t, key); 1112 1113 if (lockp != NULL) 1114 mutex_enter(lockp); 1115 mutex_enter(&zone->zone_lock); 1116 t->zsd_flags &= ~ZSD_SHUTDOWN_INPROGRESS; 1117 t->zsd_flags |= ZSD_SHUTDOWN_COMPLETED; 1118 cv_broadcast(&t->zsd_cv); 1119 DTRACE_PROBE2(zsd__shutdown__completed, 1120 zone_t *, zone, zone_key_t, key); 1121 } 1122 if (!zone_lock_held) 1123 mutex_exit(&zone->zone_lock); 1124 return (dropped); 1125 } 1126 1127 /* 1128 * Call the destroy function for the zone and key if DESTROY_NEEDED 1129 * is set. 1130 * If some other thread gets here first and sets *_INPROGRESS, then 1131 * we wait for that thread to complete so that we can ensure that 1132 * all the callbacks are done when we've looped over all zones/keys. 1133 * 1134 * When we call the destroy function, we drop the global held by the 1135 * caller, and return true to tell the caller it needs to re-evalute the 1136 * state. 1137 * If the caller holds zone_lock then zone_lock_held is set, and zone_lock 1138 * remains held on exit. 1139 */ 1140 static boolean_t 1141 zsd_apply_destroy(kmutex_t *lockp, boolean_t zone_lock_held, 1142 zone_t *zone, zone_key_t key) 1143 { 1144 struct zsd_entry *t; 1145 void *data; 1146 boolean_t dropped; 1147 1148 if (lockp != NULL) { 1149 ASSERT(MUTEX_HELD(lockp)); 1150 } 1151 if (zone_lock_held) { 1152 ASSERT(MUTEX_HELD(&zone->zone_lock)); 1153 } else { 1154 mutex_enter(&zone->zone_lock); 1155 } 1156 1157 t = zsd_find(&zone->zone_zsd, key); 1158 if (t == NULL) { 1159 /* 1160 * Somebody else got here first e.g the zone going 1161 * away. 1162 */ 1163 if (!zone_lock_held) 1164 mutex_exit(&zone->zone_lock); 1165 return (B_FALSE); 1166 } 1167 dropped = B_FALSE; 1168 if (zsd_wait_for_creator(zone, t, lockp)) 1169 dropped = B_TRUE; 1170 1171 if (zsd_wait_for_inprogress(zone, t, lockp)) 1172 dropped = B_TRUE; 1173 1174 if (t->zsd_flags & ZSD_DESTROY_NEEDED) { 1175 t->zsd_flags &= ~ZSD_DESTROY_NEEDED; 1176 t->zsd_flags |= ZSD_DESTROY_INPROGRESS; 1177 DTRACE_PROBE2(zsd__destroy__inprogress, 1178 zone_t *, zone, zone_key_t, key); 1179 mutex_exit(&zone->zone_lock); 1180 if (lockp != NULL) 1181 mutex_exit(lockp); 1182 dropped = B_TRUE; 1183 1184 ASSERT(t->zsd_destroy != NULL); 1185 data = t->zsd_data; 1186 DTRACE_PROBE2(zsd__destroy__start, 1187 zone_t *, zone, zone_key_t, key); 1188 1189 (t->zsd_destroy)(zone->zone_id, data); 1190 DTRACE_PROBE2(zsd__destroy__end, 1191 zone_t *, zone, zone_key_t, key); 1192 1193 if (lockp != NULL) 1194 mutex_enter(lockp); 1195 mutex_enter(&zone->zone_lock); 1196 t->zsd_data = NULL; 1197 t->zsd_flags &= ~ZSD_DESTROY_INPROGRESS; 1198 t->zsd_flags |= ZSD_DESTROY_COMPLETED; 1199 cv_broadcast(&t->zsd_cv); 1200 DTRACE_PROBE2(zsd__destroy__completed, 1201 zone_t *, zone, zone_key_t, key); 1202 } 1203 if (!zone_lock_held) 1204 mutex_exit(&zone->zone_lock); 1205 return (dropped); 1206 } 1207 1208 /* 1209 * Wait for any CREATE_NEEDED flag to be cleared. 1210 * Returns true if lockp was temporarily dropped while waiting. 1211 */ 1212 static boolean_t 1213 zsd_wait_for_creator(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp) 1214 { 1215 boolean_t dropped = B_FALSE; 1216 1217 while (t->zsd_flags & ZSD_CREATE_NEEDED) { 1218 DTRACE_PROBE2(zsd__wait__for__creator, 1219 zone_t *, zone, struct zsd_entry *, t); 1220 if (lockp != NULL) { 1221 dropped = B_TRUE; 1222 mutex_exit(lockp); 1223 } 1224 cv_wait(&t->zsd_cv, &zone->zone_lock); 1225 if (lockp != NULL) { 1226 /* First drop zone_lock to preserve order */ 1227 mutex_exit(&zone->zone_lock); 1228 mutex_enter(lockp); 1229 mutex_enter(&zone->zone_lock); 1230 } 1231 } 1232 return (dropped); 1233 } 1234 1235 /* 1236 * Wait for any INPROGRESS flag to be cleared. 1237 * Returns true if lockp was temporarily dropped while waiting. 1238 */ 1239 static boolean_t 1240 zsd_wait_for_inprogress(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp) 1241 { 1242 boolean_t dropped = B_FALSE; 1243 1244 while (t->zsd_flags & ZSD_ALL_INPROGRESS) { 1245 DTRACE_PROBE2(zsd__wait__for__inprogress, 1246 zone_t *, zone, struct zsd_entry *, t); 1247 if (lockp != NULL) { 1248 dropped = B_TRUE; 1249 mutex_exit(lockp); 1250 } 1251 cv_wait(&t->zsd_cv, &zone->zone_lock); 1252 if (lockp != NULL) { 1253 /* First drop zone_lock to preserve order */ 1254 mutex_exit(&zone->zone_lock); 1255 mutex_enter(lockp); 1256 mutex_enter(&zone->zone_lock); 1257 } 1258 } 1259 return (dropped); 1260 } 1261 1262 /* 1263 * Frees memory associated with the zone dataset list. 1264 */ 1265 static void 1266 zone_free_datasets(zone_t *zone) 1267 { 1268 zone_dataset_t *t, *next; 1269 1270 for (t = list_head(&zone->zone_datasets); t != NULL; t = next) { 1271 next = list_next(&zone->zone_datasets, t); 1272 list_remove(&zone->zone_datasets, t); 1273 kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1); 1274 kmem_free(t, sizeof (*t)); 1275 } 1276 list_destroy(&zone->zone_datasets); 1277 } 1278 1279 /* 1280 * zone.cpu-shares resource control support. 1281 */ 1282 /*ARGSUSED*/ 1283 static rctl_qty_t 1284 zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 1285 { 1286 ASSERT(MUTEX_HELD(&p->p_lock)); 1287 return (p->p_zone->zone_shares); 1288 } 1289 1290 /*ARGSUSED*/ 1291 static int 1292 zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1293 rctl_qty_t nv) 1294 { 1295 ASSERT(MUTEX_HELD(&p->p_lock)); 1296 ASSERT(e->rcep_t == RCENTITY_ZONE); 1297 if (e->rcep_p.zone == NULL) 1298 return (0); 1299 1300 e->rcep_p.zone->zone_shares = nv; 1301 return (0); 1302 } 1303 1304 static rctl_ops_t zone_cpu_shares_ops = { 1305 rcop_no_action, 1306 zone_cpu_shares_usage, 1307 zone_cpu_shares_set, 1308 rcop_no_test 1309 }; 1310 1311 /* 1312 * zone.cpu-cap resource control support. 1313 */ 1314 /*ARGSUSED*/ 1315 static rctl_qty_t 1316 zone_cpu_cap_get(rctl_t *rctl, struct proc *p) 1317 { 1318 ASSERT(MUTEX_HELD(&p->p_lock)); 1319 return (cpucaps_zone_get(p->p_zone)); 1320 } 1321 1322 /*ARGSUSED*/ 1323 static int 1324 zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1325 rctl_qty_t nv) 1326 { 1327 zone_t *zone = e->rcep_p.zone; 1328 1329 ASSERT(MUTEX_HELD(&p->p_lock)); 1330 ASSERT(e->rcep_t == RCENTITY_ZONE); 1331 1332 if (zone == NULL) 1333 return (0); 1334 1335 /* 1336 * set cap to the new value. 1337 */ 1338 return (cpucaps_zone_set(zone, nv)); 1339 } 1340 1341 static rctl_ops_t zone_cpu_cap_ops = { 1342 rcop_no_action, 1343 zone_cpu_cap_get, 1344 zone_cpu_cap_set, 1345 rcop_no_test 1346 }; 1347 1348 /*ARGSUSED*/ 1349 static rctl_qty_t 1350 zone_lwps_usage(rctl_t *r, proc_t *p) 1351 { 1352 rctl_qty_t nlwps; 1353 zone_t *zone = p->p_zone; 1354 1355 ASSERT(MUTEX_HELD(&p->p_lock)); 1356 1357 mutex_enter(&zone->zone_nlwps_lock); 1358 nlwps = zone->zone_nlwps; 1359 mutex_exit(&zone->zone_nlwps_lock); 1360 1361 return (nlwps); 1362 } 1363 1364 /*ARGSUSED*/ 1365 static int 1366 zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 1367 rctl_qty_t incr, uint_t flags) 1368 { 1369 rctl_qty_t nlwps; 1370 1371 ASSERT(MUTEX_HELD(&p->p_lock)); 1372 ASSERT(e->rcep_t == RCENTITY_ZONE); 1373 if (e->rcep_p.zone == NULL) 1374 return (0); 1375 ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 1376 nlwps = e->rcep_p.zone->zone_nlwps; 1377 1378 if (nlwps + incr > rcntl->rcv_value) 1379 return (1); 1380 1381 return (0); 1382 } 1383 1384 /*ARGSUSED*/ 1385 static int 1386 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) 1387 { 1388 ASSERT(MUTEX_HELD(&p->p_lock)); 1389 ASSERT(e->rcep_t == RCENTITY_ZONE); 1390 if (e->rcep_p.zone == NULL) 1391 return (0); 1392 e->rcep_p.zone->zone_nlwps_ctl = nv; 1393 return (0); 1394 } 1395 1396 static rctl_ops_t zone_lwps_ops = { 1397 rcop_no_action, 1398 zone_lwps_usage, 1399 zone_lwps_set, 1400 zone_lwps_test, 1401 }; 1402 1403 /*ARGSUSED*/ 1404 static int 1405 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1406 rctl_qty_t incr, uint_t flags) 1407 { 1408 rctl_qty_t v; 1409 ASSERT(MUTEX_HELD(&p->p_lock)); 1410 ASSERT(e->rcep_t == RCENTITY_ZONE); 1411 v = e->rcep_p.zone->zone_shmmax + incr; 1412 if (v > rval->rcv_value) 1413 return (1); 1414 return (0); 1415 } 1416 1417 static rctl_ops_t zone_shmmax_ops = { 1418 rcop_no_action, 1419 rcop_no_usage, 1420 rcop_no_set, 1421 zone_shmmax_test 1422 }; 1423 1424 /*ARGSUSED*/ 1425 static int 1426 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1427 rctl_qty_t incr, uint_t flags) 1428 { 1429 rctl_qty_t v; 1430 ASSERT(MUTEX_HELD(&p->p_lock)); 1431 ASSERT(e->rcep_t == RCENTITY_ZONE); 1432 v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr; 1433 if (v > rval->rcv_value) 1434 return (1); 1435 return (0); 1436 } 1437 1438 static rctl_ops_t zone_shmmni_ops = { 1439 rcop_no_action, 1440 rcop_no_usage, 1441 rcop_no_set, 1442 zone_shmmni_test 1443 }; 1444 1445 /*ARGSUSED*/ 1446 static int 1447 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1448 rctl_qty_t incr, uint_t flags) 1449 { 1450 rctl_qty_t v; 1451 ASSERT(MUTEX_HELD(&p->p_lock)); 1452 ASSERT(e->rcep_t == RCENTITY_ZONE); 1453 v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr; 1454 if (v > rval->rcv_value) 1455 return (1); 1456 return (0); 1457 } 1458 1459 static rctl_ops_t zone_semmni_ops = { 1460 rcop_no_action, 1461 rcop_no_usage, 1462 rcop_no_set, 1463 zone_semmni_test 1464 }; 1465 1466 /*ARGSUSED*/ 1467 static int 1468 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval, 1469 rctl_qty_t incr, uint_t flags) 1470 { 1471 rctl_qty_t v; 1472 ASSERT(MUTEX_HELD(&p->p_lock)); 1473 ASSERT(e->rcep_t == RCENTITY_ZONE); 1474 v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr; 1475 if (v > rval->rcv_value) 1476 return (1); 1477 return (0); 1478 } 1479 1480 static rctl_ops_t zone_msgmni_ops = { 1481 rcop_no_action, 1482 rcop_no_usage, 1483 rcop_no_set, 1484 zone_msgmni_test 1485 }; 1486 1487 /*ARGSUSED*/ 1488 static rctl_qty_t 1489 zone_locked_mem_usage(rctl_t *rctl, struct proc *p) 1490 { 1491 rctl_qty_t q; 1492 ASSERT(MUTEX_HELD(&p->p_lock)); 1493 mutex_enter(&p->p_zone->zone_mem_lock); 1494 q = p->p_zone->zone_locked_mem; 1495 mutex_exit(&p->p_zone->zone_mem_lock); 1496 return (q); 1497 } 1498 1499 /*ARGSUSED*/ 1500 static int 1501 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 1502 rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 1503 { 1504 rctl_qty_t q; 1505 zone_t *z; 1506 1507 z = e->rcep_p.zone; 1508 ASSERT(MUTEX_HELD(&p->p_lock)); 1509 ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 1510 q = z->zone_locked_mem; 1511 if (q + incr > rcntl->rcv_value) 1512 return (1); 1513 return (0); 1514 } 1515 1516 /*ARGSUSED*/ 1517 static int 1518 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1519 rctl_qty_t nv) 1520 { 1521 ASSERT(MUTEX_HELD(&p->p_lock)); 1522 ASSERT(e->rcep_t == RCENTITY_ZONE); 1523 if (e->rcep_p.zone == NULL) 1524 return (0); 1525 e->rcep_p.zone->zone_locked_mem_ctl = nv; 1526 return (0); 1527 } 1528 1529 static rctl_ops_t zone_locked_mem_ops = { 1530 rcop_no_action, 1531 zone_locked_mem_usage, 1532 zone_locked_mem_set, 1533 zone_locked_mem_test 1534 }; 1535 1536 /*ARGSUSED*/ 1537 static rctl_qty_t 1538 zone_max_swap_usage(rctl_t *rctl, struct proc *p) 1539 { 1540 rctl_qty_t q; 1541 zone_t *z = p->p_zone; 1542 1543 ASSERT(MUTEX_HELD(&p->p_lock)); 1544 mutex_enter(&z->zone_mem_lock); 1545 q = z->zone_max_swap; 1546 mutex_exit(&z->zone_mem_lock); 1547 return (q); 1548 } 1549 1550 /*ARGSUSED*/ 1551 static int 1552 zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 1553 rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags) 1554 { 1555 rctl_qty_t q; 1556 zone_t *z; 1557 1558 z = e->rcep_p.zone; 1559 ASSERT(MUTEX_HELD(&p->p_lock)); 1560 ASSERT(MUTEX_HELD(&z->zone_mem_lock)); 1561 q = z->zone_max_swap; 1562 if (q + incr > rcntl->rcv_value) 1563 return (1); 1564 return (0); 1565 } 1566 1567 /*ARGSUSED*/ 1568 static int 1569 zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 1570 rctl_qty_t nv) 1571 { 1572 ASSERT(MUTEX_HELD(&p->p_lock)); 1573 ASSERT(e->rcep_t == RCENTITY_ZONE); 1574 if (e->rcep_p.zone == NULL) 1575 return (0); 1576 e->rcep_p.zone->zone_max_swap_ctl = nv; 1577 return (0); 1578 } 1579 1580 static rctl_ops_t zone_max_swap_ops = { 1581 rcop_no_action, 1582 zone_max_swap_usage, 1583 zone_max_swap_set, 1584 zone_max_swap_test 1585 }; 1586 1587 /* 1588 * Helper function to brand the zone with a unique ID. 1589 */ 1590 static void 1591 zone_uniqid(zone_t *zone) 1592 { 1593 static uint64_t uniqid = 0; 1594 1595 ASSERT(MUTEX_HELD(&zonehash_lock)); 1596 zone->zone_uniqid = uniqid++; 1597 } 1598 1599 /* 1600 * Returns a held pointer to the "kcred" for the specified zone. 1601 */ 1602 struct cred * 1603 zone_get_kcred(zoneid_t zoneid) 1604 { 1605 zone_t *zone; 1606 cred_t *cr; 1607 1608 if ((zone = zone_find_by_id(zoneid)) == NULL) 1609 return (NULL); 1610 cr = zone->zone_kcred; 1611 crhold(cr); 1612 zone_rele(zone); 1613 return (cr); 1614 } 1615 1616 static int 1617 zone_lockedmem_kstat_update(kstat_t *ksp, int rw) 1618 { 1619 zone_t *zone = ksp->ks_private; 1620 zone_kstat_t *zk = ksp->ks_data; 1621 1622 if (rw == KSTAT_WRITE) 1623 return (EACCES); 1624 1625 zk->zk_usage.value.ui64 = zone->zone_locked_mem; 1626 zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl; 1627 return (0); 1628 } 1629 1630 static int 1631 zone_swapresv_kstat_update(kstat_t *ksp, int rw) 1632 { 1633 zone_t *zone = ksp->ks_private; 1634 zone_kstat_t *zk = ksp->ks_data; 1635 1636 if (rw == KSTAT_WRITE) 1637 return (EACCES); 1638 1639 zk->zk_usage.value.ui64 = zone->zone_max_swap; 1640 zk->zk_value.value.ui64 = zone->zone_max_swap_ctl; 1641 return (0); 1642 } 1643 1644 static void 1645 zone_kstat_create(zone_t *zone) 1646 { 1647 kstat_t *ksp; 1648 zone_kstat_t *zk; 1649 1650 ksp = rctl_kstat_create_zone(zone, "lockedmem", KSTAT_TYPE_NAMED, 1651 sizeof (zone_kstat_t) / sizeof (kstat_named_t), 1652 KSTAT_FLAG_VIRTUAL); 1653 1654 if (ksp == NULL) 1655 return; 1656 1657 zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 1658 ksp->ks_data_size += strlen(zone->zone_name) + 1; 1659 kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 1660 kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 1661 kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 1662 kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 1663 ksp->ks_update = zone_lockedmem_kstat_update; 1664 ksp->ks_private = zone; 1665 kstat_install(ksp); 1666 1667 zone->zone_lockedmem_kstat = ksp; 1668 1669 ksp = rctl_kstat_create_zone(zone, "swapresv", KSTAT_TYPE_NAMED, 1670 sizeof (zone_kstat_t) / sizeof (kstat_named_t), 1671 KSTAT_FLAG_VIRTUAL); 1672 1673 if (ksp == NULL) 1674 return; 1675 1676 zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP); 1677 ksp->ks_data_size += strlen(zone->zone_name) + 1; 1678 kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING); 1679 kstat_named_setstr(&zk->zk_zonename, zone->zone_name); 1680 kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64); 1681 kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64); 1682 ksp->ks_update = zone_swapresv_kstat_update; 1683 ksp->ks_private = zone; 1684 kstat_install(ksp); 1685 1686 zone->zone_swapresv_kstat = ksp; 1687 } 1688 1689 static void 1690 zone_kstat_delete(zone_t *zone) 1691 { 1692 void *data; 1693 1694 if (zone->zone_lockedmem_kstat != NULL) { 1695 data = zone->zone_lockedmem_kstat->ks_data; 1696 kstat_delete(zone->zone_lockedmem_kstat); 1697 kmem_free(data, sizeof (zone_kstat_t)); 1698 } 1699 if (zone->zone_swapresv_kstat != NULL) { 1700 data = zone->zone_swapresv_kstat->ks_data; 1701 kstat_delete(zone->zone_swapresv_kstat); 1702 kmem_free(data, sizeof (zone_kstat_t)); 1703 } 1704 } 1705 1706 /* 1707 * Called very early on in boot to initialize the ZSD list so that 1708 * zone_key_create() can be called before zone_init(). It also initializes 1709 * portions of zone0 which may be used before zone_init() is called. The 1710 * variable "global_zone" will be set when zone0 is fully initialized by 1711 * zone_init(). 1712 */ 1713 void 1714 zone_zsd_init(void) 1715 { 1716 mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 1717 mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 1718 list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 1719 offsetof(struct zsd_entry, zsd_linkage)); 1720 list_create(&zone_active, sizeof (zone_t), 1721 offsetof(zone_t, zone_linkage)); 1722 list_create(&zone_deathrow, sizeof (zone_t), 1723 offsetof(zone_t, zone_linkage)); 1724 1725 mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 1726 mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 1727 mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 1728 zone0.zone_shares = 1; 1729 zone0.zone_nlwps = 0; 1730 zone0.zone_nlwps_ctl = INT_MAX; 1731 zone0.zone_locked_mem = 0; 1732 zone0.zone_locked_mem_ctl = UINT64_MAX; 1733 ASSERT(zone0.zone_max_swap == 0); 1734 zone0.zone_max_swap_ctl = UINT64_MAX; 1735 zone0.zone_shmmax = 0; 1736 zone0.zone_ipc.ipcq_shmmni = 0; 1737 zone0.zone_ipc.ipcq_semmni = 0; 1738 zone0.zone_ipc.ipcq_msgmni = 0; 1739 zone0.zone_name = GLOBAL_ZONENAME; 1740 zone0.zone_nodename = utsname.nodename; 1741 zone0.zone_domain = srpc_domain; 1742 zone0.zone_hostid = HW_INVALID_HOSTID; 1743 zone0.zone_ref = 1; 1744 zone0.zone_id = GLOBAL_ZONEID; 1745 zone0.zone_status = ZONE_IS_RUNNING; 1746 zone0.zone_rootpath = "/"; 1747 zone0.zone_rootpathlen = 2; 1748 zone0.zone_psetid = ZONE_PS_INVAL; 1749 zone0.zone_ncpus = 0; 1750 zone0.zone_ncpus_online = 0; 1751 zone0.zone_proc_initpid = 1; 1752 zone0.zone_initname = initname; 1753 zone0.zone_lockedmem_kstat = NULL; 1754 zone0.zone_swapresv_kstat = NULL; 1755 list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 1756 offsetof(struct zsd_entry, zsd_linkage)); 1757 list_insert_head(&zone_active, &zone0); 1758 1759 /* 1760 * The root filesystem is not mounted yet, so zone_rootvp cannot be set 1761 * to anything meaningful. It is assigned to be 'rootdir' in 1762 * vfs_mountroot(). 1763 */ 1764 zone0.zone_rootvp = NULL; 1765 zone0.zone_vfslist = NULL; 1766 zone0.zone_bootargs = initargs; 1767 zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 1768 /* 1769 * The global zone has all privileges 1770 */ 1771 priv_fillset(zone0.zone_privset); 1772 /* 1773 * Add p0 to the global zone 1774 */ 1775 zone0.zone_zsched = &p0; 1776 p0.p_zone = &zone0; 1777 } 1778 1779 /* 1780 * Compute a hash value based on the contents of the label and the DOI. The 1781 * hash algorithm is somewhat arbitrary, but is based on the observation that 1782 * humans will likely pick labels that differ by amounts that work out to be 1783 * multiples of the number of hash chains, and thus stirring in some primes 1784 * should help. 1785 */ 1786 static uint_t 1787 hash_bylabel(void *hdata, mod_hash_key_t key) 1788 { 1789 const ts_label_t *lab = (ts_label_t *)key; 1790 const uint32_t *up, *ue; 1791 uint_t hash; 1792 int i; 1793 1794 _NOTE(ARGUNUSED(hdata)); 1795 1796 hash = lab->tsl_doi + (lab->tsl_doi << 1); 1797 /* we depend on alignment of label, but not representation */ 1798 up = (const uint32_t *)&lab->tsl_label; 1799 ue = up + sizeof (lab->tsl_label) / sizeof (*up); 1800 i = 1; 1801 while (up < ue) { 1802 /* using 2^n + 1, 1 <= n <= 16 as source of many primes */ 1803 hash += *up + (*up << ((i % 16) + 1)); 1804 up++; 1805 i++; 1806 } 1807 return (hash); 1808 } 1809 1810 /* 1811 * All that mod_hash cares about here is zero (equal) versus non-zero (not 1812 * equal). This may need to be changed if less than / greater than is ever 1813 * needed. 1814 */ 1815 static int 1816 hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 1817 { 1818 ts_label_t *lab1 = (ts_label_t *)key1; 1819 ts_label_t *lab2 = (ts_label_t *)key2; 1820 1821 return (label_equal(lab1, lab2) ? 0 : 1); 1822 } 1823 1824 /* 1825 * Called by main() to initialize the zones framework. 1826 */ 1827 void 1828 zone_init(void) 1829 { 1830 rctl_dict_entry_t *rde; 1831 rctl_val_t *dval; 1832 rctl_set_t *set; 1833 rctl_alloc_gp_t *gp; 1834 rctl_entity_p_t e; 1835 int res; 1836 1837 ASSERT(curproc == &p0); 1838 1839 /* 1840 * Create ID space for zone IDs. ID 0 is reserved for the 1841 * global zone. 1842 */ 1843 zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 1844 1845 /* 1846 * Initialize generic zone resource controls, if any. 1847 */ 1848 rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 1849 RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 1850 RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER, 1851 FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops); 1852 1853 rc_zone_cpu_cap = rctl_register("zone.cpu-cap", 1854 RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS | 1855 RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER | 1856 RCTL_GLOBAL_INFINITE, 1857 MAXCAP, MAXCAP, &zone_cpu_cap_ops); 1858 1859 rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 1860 RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 1861 INT_MAX, INT_MAX, &zone_lwps_ops); 1862 /* 1863 * System V IPC resource controls 1864 */ 1865 rc_zone_msgmni = rctl_register("zone.max-msg-ids", 1866 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1867 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops); 1868 1869 rc_zone_semmni = rctl_register("zone.max-sem-ids", 1870 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1871 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops); 1872 1873 rc_zone_shmmni = rctl_register("zone.max-shm-ids", 1874 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1875 RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops); 1876 1877 rc_zone_shmmax = rctl_register("zone.max-shm-memory", 1878 RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | 1879 RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops); 1880 1881 /* 1882 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 1883 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 1884 */ 1885 dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 1886 bzero(dval, sizeof (rctl_val_t)); 1887 dval->rcv_value = 1; 1888 dval->rcv_privilege = RCPRIV_PRIVILEGED; 1889 dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 1890 dval->rcv_action_recip_pid = -1; 1891 1892 rde = rctl_dict_lookup("zone.cpu-shares"); 1893 (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 1894 1895 rc_zone_locked_mem = rctl_register("zone.max-locked-memory", 1896 RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 1897 RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 1898 &zone_locked_mem_ops); 1899 1900 rc_zone_max_swap = rctl_register("zone.max-swap", 1901 RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES | 1902 RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX, 1903 &zone_max_swap_ops); 1904 1905 /* 1906 * Initialize the ``global zone''. 1907 */ 1908 set = rctl_set_create(); 1909 gp = rctl_set_init_prealloc(RCENTITY_ZONE); 1910 mutex_enter(&p0.p_lock); 1911 e.rcep_p.zone = &zone0; 1912 e.rcep_t = RCENTITY_ZONE; 1913 zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 1914 gp); 1915 1916 zone0.zone_nlwps = p0.p_lwpcnt; 1917 zone0.zone_ntasks = 1; 1918 mutex_exit(&p0.p_lock); 1919 zone0.zone_restart_init = B_TRUE; 1920 zone0.zone_brand = &native_brand; 1921 rctl_prealloc_destroy(gp); 1922 /* 1923 * pool_default hasn't been initialized yet, so we let pool_init() 1924 * take care of making sure the global zone is in the default pool. 1925 */ 1926 1927 /* 1928 * Initialize global zone kstats 1929 */ 1930 zone_kstat_create(&zone0); 1931 1932 /* 1933 * Initialize zone label. 1934 * mlp are initialized when tnzonecfg is loaded. 1935 */ 1936 zone0.zone_slabel = l_admin_low; 1937 rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 1938 label_hold(l_admin_low); 1939 1940 mutex_enter(&zonehash_lock); 1941 zone_uniqid(&zone0); 1942 ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 1943 1944 zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 1945 mod_hash_null_valdtor); 1946 zonehashbyname = mod_hash_create_strhash("zone_by_name", 1947 zone_hash_size, mod_hash_null_valdtor); 1948 /* 1949 * maintain zonehashbylabel only for labeled systems 1950 */ 1951 if (is_system_labeled()) 1952 zonehashbylabel = mod_hash_create_extended("zone_by_label", 1953 zone_hash_size, mod_hash_null_keydtor, 1954 mod_hash_null_valdtor, hash_bylabel, NULL, 1955 hash_labelkey_cmp, KM_SLEEP); 1956 zonecount = 1; 1957 1958 (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 1959 (mod_hash_val_t)&zone0); 1960 (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 1961 (mod_hash_val_t)&zone0); 1962 if (is_system_labeled()) { 1963 zone0.zone_flags |= ZF_HASHED_LABEL; 1964 (void) mod_hash_insert(zonehashbylabel, 1965 (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0); 1966 } 1967 mutex_exit(&zonehash_lock); 1968 1969 /* 1970 * We avoid setting zone_kcred until now, since kcred is initialized 1971 * sometime after zone_zsd_init() and before zone_init(). 1972 */ 1973 zone0.zone_kcred = kcred; 1974 /* 1975 * The global zone is fully initialized (except for zone_rootvp which 1976 * will be set when the root filesystem is mounted). 1977 */ 1978 global_zone = &zone0; 1979 1980 /* 1981 * Setup an event channel to send zone status change notifications on 1982 */ 1983 res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan, 1984 EVCH_CREAT); 1985 1986 if (res) 1987 panic("Sysevent_evc_bind failed during zone setup.\n"); 1988 1989 } 1990 1991 static void 1992 zone_free(zone_t *zone) 1993 { 1994 ASSERT(zone != global_zone); 1995 ASSERT(zone->zone_ntasks == 0); 1996 ASSERT(zone->zone_nlwps == 0); 1997 ASSERT(zone->zone_cred_ref == 0); 1998 ASSERT(zone->zone_kcred == NULL); 1999 ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 2000 zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 2001 2002 /* 2003 * Remove any zone caps. 2004 */ 2005 cpucaps_zone_remove(zone); 2006 2007 ASSERT(zone->zone_cpucap == NULL); 2008 2009 /* remove from deathrow list */ 2010 if (zone_status_get(zone) == ZONE_IS_DEAD) { 2011 ASSERT(zone->zone_ref == 0); 2012 mutex_enter(&zone_deathrow_lock); 2013 list_remove(&zone_deathrow, zone); 2014 mutex_exit(&zone_deathrow_lock); 2015 } 2016 2017 zone_free_zsd(zone); 2018 zone_free_datasets(zone); 2019 list_destroy(&zone->zone_dl_list); 2020 2021 if (zone->zone_rootvp != NULL) 2022 VN_RELE(zone->zone_rootvp); 2023 if (zone->zone_rootpath) 2024 kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 2025 if (zone->zone_name != NULL) 2026 kmem_free(zone->zone_name, ZONENAME_MAX); 2027 if (zone->zone_slabel != NULL) 2028 label_rele(zone->zone_slabel); 2029 if (zone->zone_nodename != NULL) 2030 kmem_free(zone->zone_nodename, _SYS_NMLN); 2031 if (zone->zone_domain != NULL) 2032 kmem_free(zone->zone_domain, _SYS_NMLN); 2033 if (zone->zone_privset != NULL) 2034 kmem_free(zone->zone_privset, sizeof (priv_set_t)); 2035 if (zone->zone_rctls != NULL) 2036 rctl_set_free(zone->zone_rctls); 2037 if (zone->zone_bootargs != NULL) 2038 kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 2039 if (zone->zone_initname != NULL) 2040 kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 2041 id_free(zoneid_space, zone->zone_id); 2042 mutex_destroy(&zone->zone_lock); 2043 cv_destroy(&zone->zone_cv); 2044 rw_destroy(&zone->zone_mlps.mlpl_rwlock); 2045 kmem_free(zone, sizeof (zone_t)); 2046 } 2047 2048 /* 2049 * See block comment at the top of this file for information about zone 2050 * status values. 2051 */ 2052 /* 2053 * Convenience function for setting zone status. 2054 */ 2055 static void 2056 zone_status_set(zone_t *zone, zone_status_t status) 2057 { 2058 2059 nvlist_t *nvl = NULL; 2060 ASSERT(MUTEX_HELD(&zone_status_lock)); 2061 ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 2062 status >= zone_status_get(zone)); 2063 2064 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) || 2065 nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) || 2066 nvlist_add_string(nvl, ZONE_CB_NEWSTATE, 2067 zone_status_table[status]) || 2068 nvlist_add_string(nvl, ZONE_CB_OLDSTATE, 2069 zone_status_table[zone->zone_status]) || 2070 nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) || 2071 nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) || 2072 sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS, 2073 ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) { 2074 #ifdef DEBUG 2075 (void) printf( 2076 "Failed to allocate and send zone state change event.\n"); 2077 #endif 2078 } 2079 nvlist_free(nvl); 2080 2081 zone->zone_status = status; 2082 2083 cv_broadcast(&zone->zone_cv); 2084 } 2085 2086 /* 2087 * Public function to retrieve the zone status. The zone status may 2088 * change after it is retrieved. 2089 */ 2090 zone_status_t 2091 zone_status_get(zone_t *zone) 2092 { 2093 return (zone->zone_status); 2094 } 2095 2096 static int 2097 zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 2098 { 2099 char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP); 2100 int err = 0; 2101 2102 ASSERT(zone != global_zone); 2103 if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0) 2104 goto done; /* EFAULT or ENAMETOOLONG */ 2105 2106 if (zone->zone_bootargs != NULL) 2107 kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1); 2108 2109 zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP); 2110 (void) strcpy(zone->zone_bootargs, bootargs); 2111 2112 done: 2113 kmem_free(bootargs, BOOTARGS_MAX); 2114 return (err); 2115 } 2116 2117 static int 2118 zone_set_brand(zone_t *zone, const char *brand) 2119 { 2120 struct brand_attr *attrp; 2121 brand_t *bp; 2122 2123 attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP); 2124 if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) { 2125 kmem_free(attrp, sizeof (struct brand_attr)); 2126 return (EFAULT); 2127 } 2128 2129 bp = brand_register_zone(attrp); 2130 kmem_free(attrp, sizeof (struct brand_attr)); 2131 if (bp == NULL) 2132 return (EINVAL); 2133 2134 /* 2135 * This is the only place where a zone can change it's brand. 2136 * We already need to hold zone_status_lock to check the zone 2137 * status, so we'll just use that lock to serialize zone 2138 * branding requests as well. 2139 */ 2140 mutex_enter(&zone_status_lock); 2141 2142 /* Re-Branding is not allowed and the zone can't be booted yet */ 2143 if ((ZONE_IS_BRANDED(zone)) || 2144 (zone_status_get(zone) >= ZONE_IS_BOOTING)) { 2145 mutex_exit(&zone_status_lock); 2146 brand_unregister_zone(bp); 2147 return (EINVAL); 2148 } 2149 2150 /* set up the brand specific data */ 2151 zone->zone_brand = bp; 2152 ZBROP(zone)->b_init_brand_data(zone); 2153 2154 mutex_exit(&zone_status_lock); 2155 return (0); 2156 } 2157 2158 static int 2159 zone_set_initname(zone_t *zone, const char *zone_initname) 2160 { 2161 char initname[INITNAME_SZ]; 2162 size_t len; 2163 int err = 0; 2164 2165 ASSERT(zone != global_zone); 2166 if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0) 2167 return (err); /* EFAULT or ENAMETOOLONG */ 2168 2169 if (zone->zone_initname != NULL) 2170 kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1); 2171 2172 zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP); 2173 (void) strcpy(zone->zone_initname, initname); 2174 return (0); 2175 } 2176 2177 static int 2178 zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap) 2179 { 2180 uint64_t mcap; 2181 int err = 0; 2182 2183 if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0) 2184 zone->zone_phys_mcap = mcap; 2185 2186 return (err); 2187 } 2188 2189 static int 2190 zone_set_sched_class(zone_t *zone, const char *new_class) 2191 { 2192 char sched_class[PC_CLNMSZ]; 2193 id_t classid; 2194 int err; 2195 2196 ASSERT(zone != global_zone); 2197 if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0) 2198 return (err); /* EFAULT or ENAMETOOLONG */ 2199 2200 if (getcid(sched_class, &classid) != 0 || classid == syscid) 2201 return (set_errno(EINVAL)); 2202 zone->zone_defaultcid = classid; 2203 ASSERT(zone->zone_defaultcid > 0 && 2204 zone->zone_defaultcid < loaded_classes); 2205 2206 return (0); 2207 } 2208 2209 /* 2210 * Block indefinitely waiting for (zone_status >= status) 2211 */ 2212 void 2213 zone_status_wait(zone_t *zone, zone_status_t status) 2214 { 2215 ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 2216 2217 mutex_enter(&zone_status_lock); 2218 while (zone->zone_status < status) { 2219 cv_wait(&zone->zone_cv, &zone_status_lock); 2220 } 2221 mutex_exit(&zone_status_lock); 2222 } 2223 2224 /* 2225 * Private CPR-safe version of zone_status_wait(). 2226 */ 2227 static void 2228 zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 2229 { 2230 callb_cpr_t cprinfo; 2231 2232 ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 2233 2234 CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 2235 str); 2236 mutex_enter(&zone_status_lock); 2237 while (zone->zone_status < status) { 2238 CALLB_CPR_SAFE_BEGIN(&cprinfo); 2239 cv_wait(&zone->zone_cv, &zone_status_lock); 2240 CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 2241 } 2242 /* 2243 * zone_status_lock is implicitly released by the following. 2244 */ 2245 CALLB_CPR_EXIT(&cprinfo); 2246 } 2247 2248 /* 2249 * Block until zone enters requested state or signal is received. Return (0) 2250 * if signaled, non-zero otherwise. 2251 */ 2252 int 2253 zone_status_wait_sig(zone_t *zone, zone_status_t status) 2254 { 2255 ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 2256 2257 mutex_enter(&zone_status_lock); 2258 while (zone->zone_status < status) { 2259 if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 2260 mutex_exit(&zone_status_lock); 2261 return (0); 2262 } 2263 } 2264 mutex_exit(&zone_status_lock); 2265 return (1); 2266 } 2267 2268 /* 2269 * Block until the zone enters the requested state or the timeout expires, 2270 * whichever happens first. Return (-1) if operation timed out, time remaining 2271 * otherwise. 2272 */ 2273 clock_t 2274 zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 2275 { 2276 clock_t timeleft = 0; 2277 2278 ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 2279 2280 mutex_enter(&zone_status_lock); 2281 while (zone->zone_status < status && timeleft != -1) { 2282 timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 2283 } 2284 mutex_exit(&zone_status_lock); 2285 return (timeleft); 2286 } 2287 2288 /* 2289 * Block until the zone enters the requested state, the current process is 2290 * signaled, or the timeout expires, whichever happens first. Return (-1) if 2291 * operation timed out, 0 if signaled, time remaining otherwise. 2292 */ 2293 clock_t 2294 zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 2295 { 2296 clock_t timeleft = tim - lbolt; 2297 2298 ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 2299 2300 mutex_enter(&zone_status_lock); 2301 while (zone->zone_status < status) { 2302 timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 2303 tim); 2304 if (timeleft <= 0) 2305 break; 2306 } 2307 mutex_exit(&zone_status_lock); 2308 return (timeleft); 2309 } 2310 2311 /* 2312 * Zones have two reference counts: one for references from credential 2313 * structures (zone_cred_ref), and one (zone_ref) for everything else. 2314 * This is so we can allow a zone to be rebooted while there are still 2315 * outstanding cred references, since certain drivers cache dblks (which 2316 * implicitly results in cached creds). We wait for zone_ref to drop to 2317 * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 2318 * later freed when the zone_cred_ref drops to 0, though nothing other 2319 * than the zone id and privilege set should be accessed once the zone 2320 * is "dead". 2321 * 2322 * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 2323 * to force halt/reboot to block waiting for the zone_cred_ref to drop 2324 * to 0. This can be useful to flush out other sources of cached creds 2325 * that may be less innocuous than the driver case. 2326 */ 2327 2328 int zone_wait_for_cred = 0; 2329 2330 static void 2331 zone_hold_locked(zone_t *z) 2332 { 2333 ASSERT(MUTEX_HELD(&z->zone_lock)); 2334 z->zone_ref++; 2335 ASSERT(z->zone_ref != 0); 2336 } 2337 2338 void 2339 zone_hold(zone_t *z) 2340 { 2341 mutex_enter(&z->zone_lock); 2342 zone_hold_locked(z); 2343 mutex_exit(&z->zone_lock); 2344 } 2345 2346 /* 2347 * If the non-cred ref count drops to 1 and either the cred ref count 2348 * is 0 or we aren't waiting for cred references, the zone is ready to 2349 * be destroyed. 2350 */ 2351 #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 2352 (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 2353 2354 void 2355 zone_rele(zone_t *z) 2356 { 2357 boolean_t wakeup; 2358 2359 mutex_enter(&z->zone_lock); 2360 ASSERT(z->zone_ref != 0); 2361 z->zone_ref--; 2362 if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 2363 /* no more refs, free the structure */ 2364 mutex_exit(&z->zone_lock); 2365 zone_free(z); 2366 return; 2367 } 2368 /* signal zone_destroy so the zone can finish halting */ 2369 wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 2370 mutex_exit(&z->zone_lock); 2371 2372 if (wakeup) { 2373 /* 2374 * Grabbing zonehash_lock here effectively synchronizes with 2375 * zone_destroy() to avoid missed signals. 2376 */ 2377 mutex_enter(&zonehash_lock); 2378 cv_broadcast(&zone_destroy_cv); 2379 mutex_exit(&zonehash_lock); 2380 } 2381 } 2382 2383 void 2384 zone_cred_hold(zone_t *z) 2385 { 2386 mutex_enter(&z->zone_lock); 2387 z->zone_cred_ref++; 2388 ASSERT(z->zone_cred_ref != 0); 2389 mutex_exit(&z->zone_lock); 2390 } 2391 2392 void 2393 zone_cred_rele(zone_t *z) 2394 { 2395 boolean_t wakeup; 2396 2397 mutex_enter(&z->zone_lock); 2398 ASSERT(z->zone_cred_ref != 0); 2399 z->zone_cred_ref--; 2400 if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 2401 /* no more refs, free the structure */ 2402 mutex_exit(&z->zone_lock); 2403 zone_free(z); 2404 return; 2405 } 2406 /* 2407 * If zone_destroy is waiting for the cred references to drain 2408 * out, and they have, signal it. 2409 */ 2410 wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 2411 zone_status_get(z) >= ZONE_IS_DEAD); 2412 mutex_exit(&z->zone_lock); 2413 2414 if (wakeup) { 2415 /* 2416 * Grabbing zonehash_lock here effectively synchronizes with 2417 * zone_destroy() to avoid missed signals. 2418 */ 2419 mutex_enter(&zonehash_lock); 2420 cv_broadcast(&zone_destroy_cv); 2421 mutex_exit(&zonehash_lock); 2422 } 2423 } 2424 2425 void 2426 zone_task_hold(zone_t *z) 2427 { 2428 mutex_enter(&z->zone_lock); 2429 z->zone_ntasks++; 2430 ASSERT(z->zone_ntasks != 0); 2431 mutex_exit(&z->zone_lock); 2432 } 2433 2434 void 2435 zone_task_rele(zone_t *zone) 2436 { 2437 uint_t refcnt; 2438 2439 mutex_enter(&zone->zone_lock); 2440 ASSERT(zone->zone_ntasks != 0); 2441 refcnt = --zone->zone_ntasks; 2442 if (refcnt > 1) { /* Common case */ 2443 mutex_exit(&zone->zone_lock); 2444 return; 2445 } 2446 zone_hold_locked(zone); /* so we can use the zone_t later */ 2447 mutex_exit(&zone->zone_lock); 2448 if (refcnt == 1) { 2449 /* 2450 * See if the zone is shutting down. 2451 */ 2452 mutex_enter(&zone_status_lock); 2453 if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 2454 goto out; 2455 } 2456 2457 /* 2458 * Make sure the ntasks didn't change since we 2459 * dropped zone_lock. 2460 */ 2461 mutex_enter(&zone->zone_lock); 2462 if (refcnt != zone->zone_ntasks) { 2463 mutex_exit(&zone->zone_lock); 2464 goto out; 2465 } 2466 mutex_exit(&zone->zone_lock); 2467 2468 /* 2469 * No more user processes in the zone. The zone is empty. 2470 */ 2471 zone_status_set(zone, ZONE_IS_EMPTY); 2472 goto out; 2473 } 2474 2475 ASSERT(refcnt == 0); 2476 /* 2477 * zsched has exited; the zone is dead. 2478 */ 2479 zone->zone_zsched = NULL; /* paranoia */ 2480 mutex_enter(&zone_status_lock); 2481 zone_status_set(zone, ZONE_IS_DEAD); 2482 out: 2483 mutex_exit(&zone_status_lock); 2484 zone_rele(zone); 2485 } 2486 2487 zoneid_t 2488 getzoneid(void) 2489 { 2490 return (curproc->p_zone->zone_id); 2491 } 2492 2493 /* 2494 * Internal versions of zone_find_by_*(). These don't zone_hold() or 2495 * check the validity of a zone's state. 2496 */ 2497 static zone_t * 2498 zone_find_all_by_id(zoneid_t zoneid) 2499 { 2500 mod_hash_val_t hv; 2501 zone_t *zone = NULL; 2502 2503 ASSERT(MUTEX_HELD(&zonehash_lock)); 2504 2505 if (mod_hash_find(zonehashbyid, 2506 (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 2507 zone = (zone_t *)hv; 2508 return (zone); 2509 } 2510 2511 static zone_t * 2512 zone_find_all_by_label(const ts_label_t *label) 2513 { 2514 mod_hash_val_t hv; 2515 zone_t *zone = NULL; 2516 2517 ASSERT(MUTEX_HELD(&zonehash_lock)); 2518 2519 /* 2520 * zonehashbylabel is not maintained for unlabeled systems 2521 */ 2522 if (!is_system_labeled()) 2523 return (NULL); 2524 if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0) 2525 zone = (zone_t *)hv; 2526 return (zone); 2527 } 2528 2529 static zone_t * 2530 zone_find_all_by_name(char *name) 2531 { 2532 mod_hash_val_t hv; 2533 zone_t *zone = NULL; 2534 2535 ASSERT(MUTEX_HELD(&zonehash_lock)); 2536 2537 if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 2538 zone = (zone_t *)hv; 2539 return (zone); 2540 } 2541 2542 /* 2543 * Public interface for looking up a zone by zoneid. Only returns the zone if 2544 * it is fully initialized, and has not yet begun the zone_destroy() sequence. 2545 * Caller must call zone_rele() once it is done with the zone. 2546 * 2547 * The zone may begin the zone_destroy() sequence immediately after this 2548 * function returns, but may be safely used until zone_rele() is called. 2549 */ 2550 zone_t * 2551 zone_find_by_id(zoneid_t zoneid) 2552 { 2553 zone_t *zone; 2554 zone_status_t status; 2555 2556 mutex_enter(&zonehash_lock); 2557 if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 2558 mutex_exit(&zonehash_lock); 2559 return (NULL); 2560 } 2561 status = zone_status_get(zone); 2562 if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 2563 /* 2564 * For all practical purposes the zone doesn't exist. 2565 */ 2566 mutex_exit(&zonehash_lock); 2567 return (NULL); 2568 } 2569 zone_hold(zone); 2570 mutex_exit(&zonehash_lock); 2571 return (zone); 2572 } 2573 2574 /* 2575 * Similar to zone_find_by_id, but using zone label as the key. 2576 */ 2577 zone_t * 2578 zone_find_by_label(const ts_label_t *label) 2579 { 2580 zone_t *zone; 2581 zone_status_t status; 2582 2583 mutex_enter(&zonehash_lock); 2584 if ((zone = zone_find_all_by_label(label)) == NULL) { 2585 mutex_exit(&zonehash_lock); 2586 return (NULL); 2587 } 2588 2589 status = zone_status_get(zone); 2590 if (status > ZONE_IS_DOWN) { 2591 /* 2592 * For all practical purposes the zone doesn't exist. 2593 */ 2594 mutex_exit(&zonehash_lock); 2595 return (NULL); 2596 } 2597 zone_hold(zone); 2598 mutex_exit(&zonehash_lock); 2599 return (zone); 2600 } 2601 2602 /* 2603 * Similar to zone_find_by_id, but using zone name as the key. 2604 */ 2605 zone_t * 2606 zone_find_by_name(char *name) 2607 { 2608 zone_t *zone; 2609 zone_status_t status; 2610 2611 mutex_enter(&zonehash_lock); 2612 if ((zone = zone_find_all_by_name(name)) == NULL) { 2613 mutex_exit(&zonehash_lock); 2614 return (NULL); 2615 } 2616 status = zone_status_get(zone); 2617 if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 2618 /* 2619 * For all practical purposes the zone doesn't exist. 2620 */ 2621 mutex_exit(&zonehash_lock); 2622 return (NULL); 2623 } 2624 zone_hold(zone); 2625 mutex_exit(&zonehash_lock); 2626 return (zone); 2627 } 2628 2629 /* 2630 * Similar to zone_find_by_id(), using the path as a key. For instance, 2631 * if there is a zone "foo" rooted at /foo/root, and the path argument 2632 * is "/foo/root/proc", it will return the held zone_t corresponding to 2633 * zone "foo". 2634 * 2635 * zone_find_by_path() always returns a non-NULL value, since at the 2636 * very least every path will be contained in the global zone. 2637 * 2638 * As with the other zone_find_by_*() functions, the caller is 2639 * responsible for zone_rele()ing the return value of this function. 2640 */ 2641 zone_t * 2642 zone_find_by_path(const char *path) 2643 { 2644 zone_t *zone; 2645 zone_t *zret = NULL; 2646 zone_status_t status; 2647 2648 if (path == NULL) { 2649 /* 2650 * Call from rootconf(). 2651 */ 2652 zone_hold(global_zone); 2653 return (global_zone); 2654 } 2655 ASSERT(*path == '/'); 2656 mutex_enter(&zonehash_lock); 2657 for (zone = list_head(&zone_active); zone != NULL; 2658 zone = list_next(&zone_active, zone)) { 2659 if (ZONE_PATH_VISIBLE(path, zone)) 2660 zret = zone; 2661 } 2662 ASSERT(zret != NULL); 2663 status = zone_status_get(zret); 2664 if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 2665 /* 2666 * Zone practically doesn't exist. 2667 */ 2668 zret = global_zone; 2669 } 2670 zone_hold(zret); 2671 mutex_exit(&zonehash_lock); 2672 return (zret); 2673 } 2674 2675 /* 2676 * Get the number of cpus visible to this zone. The system-wide global 2677 * 'ncpus' is returned if pools are disabled, the caller is in the 2678 * global zone, or a NULL zone argument is passed in. 2679 */ 2680 int 2681 zone_ncpus_get(zone_t *zone) 2682 { 2683 int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 2684 2685 return (myncpus != 0 ? myncpus : ncpus); 2686 } 2687 2688 /* 2689 * Get the number of online cpus visible to this zone. The system-wide 2690 * global 'ncpus_online' is returned if pools are disabled, the caller 2691 * is in the global zone, or a NULL zone argument is passed in. 2692 */ 2693 int 2694 zone_ncpus_online_get(zone_t *zone) 2695 { 2696 int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 2697 2698 return (myncpus_online != 0 ? myncpus_online : ncpus_online); 2699 } 2700 2701 /* 2702 * Return the pool to which the zone is currently bound. 2703 */ 2704 pool_t * 2705 zone_pool_get(zone_t *zone) 2706 { 2707 ASSERT(pool_lock_held()); 2708 2709 return (zone->zone_pool); 2710 } 2711 2712 /* 2713 * Set the zone's pool pointer and update the zone's visibility to match 2714 * the resources in the new pool. 2715 */ 2716 void 2717 zone_pool_set(zone_t *zone, pool_t *pool) 2718 { 2719 ASSERT(pool_lock_held()); 2720 ASSERT(MUTEX_HELD(&cpu_lock)); 2721 2722 zone->zone_pool = pool; 2723 zone_pset_set(zone, pool->pool_pset->pset_id); 2724 } 2725 2726 /* 2727 * Return the cached value of the id of the processor set to which the 2728 * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 2729 * facility is disabled. 2730 */ 2731 psetid_t 2732 zone_pset_get(zone_t *zone) 2733 { 2734 ASSERT(MUTEX_HELD(&cpu_lock)); 2735 2736 return (zone->zone_psetid); 2737 } 2738 2739 /* 2740 * Set the cached value of the id of the processor set to which the zone 2741 * is currently bound. Also update the zone's visibility to match the 2742 * resources in the new processor set. 2743 */ 2744 void 2745 zone_pset_set(zone_t *zone, psetid_t newpsetid) 2746 { 2747 psetid_t oldpsetid; 2748 2749 ASSERT(MUTEX_HELD(&cpu_lock)); 2750 oldpsetid = zone_pset_get(zone); 2751 2752 if (oldpsetid == newpsetid) 2753 return; 2754 /* 2755 * Global zone sees all. 2756 */ 2757 if (zone != global_zone) { 2758 zone->zone_psetid = newpsetid; 2759 if (newpsetid != ZONE_PS_INVAL) 2760 pool_pset_visibility_add(newpsetid, zone); 2761 if (oldpsetid != ZONE_PS_INVAL) 2762 pool_pset_visibility_remove(oldpsetid, zone); 2763 } 2764 /* 2765 * Disabling pools, so we should start using the global values 2766 * for ncpus and ncpus_online. 2767 */ 2768 if (newpsetid == ZONE_PS_INVAL) { 2769 zone->zone_ncpus = 0; 2770 zone->zone_ncpus_online = 0; 2771 } 2772 } 2773 2774 /* 2775 * Walk the list of active zones and issue the provided callback for 2776 * each of them. 2777 * 2778 * Caller must not be holding any locks that may be acquired under 2779 * zonehash_lock. See comment at the beginning of the file for a list of 2780 * common locks and their interactions with zones. 2781 */ 2782 int 2783 zone_walk(int (*cb)(zone_t *, void *), void *data) 2784 { 2785 zone_t *zone; 2786 int ret = 0; 2787 zone_status_t status; 2788 2789 mutex_enter(&zonehash_lock); 2790 for (zone = list_head(&zone_active); zone != NULL; 2791 zone = list_next(&zone_active, zone)) { 2792 /* 2793 * Skip zones that shouldn't be externally visible. 2794 */ 2795 status = zone_status_get(zone); 2796 if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 2797 continue; 2798 /* 2799 * Bail immediately if any callback invocation returns a 2800 * non-zero value. 2801 */ 2802 ret = (*cb)(zone, data); 2803 if (ret != 0) 2804 break; 2805 } 2806 mutex_exit(&zonehash_lock); 2807 return (ret); 2808 } 2809 2810 static int 2811 zone_set_root(zone_t *zone, const char *upath) 2812 { 2813 vnode_t *vp; 2814 int trycount; 2815 int error = 0; 2816 char *path; 2817 struct pathname upn, pn; 2818 size_t pathlen; 2819 2820 if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 2821 return (error); 2822 2823 pn_alloc(&pn); 2824 2825 /* prevent infinite loop */ 2826 trycount = 10; 2827 for (;;) { 2828 if (--trycount <= 0) { 2829 error = ESTALE; 2830 goto out; 2831 } 2832 2833 if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 2834 /* 2835 * VOP_ACCESS() may cover 'vp' with a new 2836 * filesystem, if 'vp' is an autoFS vnode. 2837 * Get the new 'vp' if so. 2838 */ 2839 if ((error = 2840 VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) == 0 && 2841 (!vn_ismntpt(vp) || 2842 (error = traverse(&vp)) == 0)) { 2843 pathlen = pn.pn_pathlen + 2; 2844 path = kmem_alloc(pathlen, KM_SLEEP); 2845 (void) strncpy(path, pn.pn_path, 2846 pn.pn_pathlen + 1); 2847 path[pathlen - 2] = '/'; 2848 path[pathlen - 1] = '\0'; 2849 pn_free(&pn); 2850 pn_free(&upn); 2851 2852 /* Success! */ 2853 break; 2854 } 2855 VN_RELE(vp); 2856 } 2857 if (error != ESTALE) 2858 goto out; 2859 } 2860 2861 ASSERT(error == 0); 2862 zone->zone_rootvp = vp; /* we hold a reference to vp */ 2863 zone->zone_rootpath = path; 2864 zone->zone_rootpathlen = pathlen; 2865 if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0) 2866 zone->zone_flags |= ZF_IS_SCRATCH; 2867 return (0); 2868 2869 out: 2870 pn_free(&pn); 2871 pn_free(&upn); 2872 return (error); 2873 } 2874 2875 #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 2876 ((c) >= 'a' && (c) <= 'z') || \ 2877 ((c) >= 'A' && (c) <= 'Z')) 2878 2879 static int 2880 zone_set_name(zone_t *zone, const char *uname) 2881 { 2882 char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 2883 size_t len; 2884 int i, err; 2885 2886 if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 2887 kmem_free(kname, ZONENAME_MAX); 2888 return (err); /* EFAULT or ENAMETOOLONG */ 2889 } 2890 2891 /* must be less than ZONENAME_MAX */ 2892 if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 2893 kmem_free(kname, ZONENAME_MAX); 2894 return (EINVAL); 2895 } 2896 2897 /* 2898 * Name must start with an alphanumeric and must contain only 2899 * alphanumerics, '-', '_' and '.'. 2900 */ 2901 if (!isalnum(kname[0])) { 2902 kmem_free(kname, ZONENAME_MAX); 2903 return (EINVAL); 2904 } 2905 for (i = 1; i < len - 1; i++) { 2906 if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 2907 kname[i] != '.') { 2908 kmem_free(kname, ZONENAME_MAX); 2909 return (EINVAL); 2910 } 2911 } 2912 2913 zone->zone_name = kname; 2914 return (0); 2915 } 2916 2917 /* 2918 * Gets the 32-bit hostid of the specified zone as an unsigned int. If 'zonep' 2919 * is NULL or it points to a zone with no hostid emulation, then the machine's 2920 * hostid (i.e., the global zone's hostid) is returned. This function returns 2921 * zero if neither the zone nor the host machine (global zone) have hostids. It 2922 * returns HW_INVALID_HOSTID if the function attempts to return the machine's 2923 * hostid and the machine's hostid is invalid. 2924 */ 2925 uint32_t 2926 zone_get_hostid(zone_t *zonep) 2927 { 2928 unsigned long machine_hostid; 2929 2930 if (zonep == NULL || zonep->zone_hostid == HW_INVALID_HOSTID) { 2931 if (ddi_strtoul(hw_serial, NULL, 10, &machine_hostid) != 0) 2932 return (HW_INVALID_HOSTID); 2933 return ((uint32_t)machine_hostid); 2934 } 2935 return (zonep->zone_hostid); 2936 } 2937 2938 /* 2939 * Similar to thread_create(), but makes sure the thread is in the appropriate 2940 * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 2941 */ 2942 /*ARGSUSED*/ 2943 kthread_t * 2944 zthread_create( 2945 caddr_t stk, 2946 size_t stksize, 2947 void (*proc)(), 2948 void *arg, 2949 size_t len, 2950 pri_t pri) 2951 { 2952 kthread_t *t; 2953 zone_t *zone = curproc->p_zone; 2954 proc_t *pp = zone->zone_zsched; 2955 2956 zone_hold(zone); /* Reference to be dropped when thread exits */ 2957 2958 /* 2959 * No-one should be trying to create threads if the zone is shutting 2960 * down and there aren't any kernel threads around. See comment 2961 * in zthread_exit(). 2962 */ 2963 ASSERT(!(zone->zone_kthreads == NULL && 2964 zone_status_get(zone) >= ZONE_IS_EMPTY)); 2965 /* 2966 * Create a thread, but don't let it run until we've finished setting 2967 * things up. 2968 */ 2969 t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 2970 ASSERT(t->t_forw == NULL); 2971 mutex_enter(&zone_status_lock); 2972 if (zone->zone_kthreads == NULL) { 2973 t->t_forw = t->t_back = t; 2974 } else { 2975 kthread_t *tx = zone->zone_kthreads; 2976 2977 t->t_forw = tx; 2978 t->t_back = tx->t_back; 2979 tx->t_back->t_forw = t; 2980 tx->t_back = t; 2981 } 2982 zone->zone_kthreads = t; 2983 mutex_exit(&zone_status_lock); 2984 2985 mutex_enter(&pp->p_lock); 2986 t->t_proc_flag |= TP_ZTHREAD; 2987 project_rele(t->t_proj); 2988 t->t_proj = project_hold(pp->p_task->tk_proj); 2989 2990 /* 2991 * Setup complete, let it run. 2992 */ 2993 thread_lock(t); 2994 t->t_schedflag |= TS_ALLSTART; 2995 setrun_locked(t); 2996 thread_unlock(t); 2997 2998 mutex_exit(&pp->p_lock); 2999 3000 return (t); 3001 } 3002 3003 /* 3004 * Similar to thread_exit(). Must be called by threads created via 3005 * zthread_exit(). 3006 */ 3007 void 3008 zthread_exit(void) 3009 { 3010 kthread_t *t = curthread; 3011 proc_t *pp = curproc; 3012 zone_t *zone = pp->p_zone; 3013 3014 mutex_enter(&zone_status_lock); 3015 3016 /* 3017 * Reparent to p0 3018 */ 3019 kpreempt_disable(); 3020 mutex_enter(&pp->p_lock); 3021 t->t_proc_flag &= ~TP_ZTHREAD; 3022 t->t_procp = &p0; 3023 hat_thread_exit(t); 3024 mutex_exit(&pp->p_lock); 3025 kpreempt_enable(); 3026 3027 if (t->t_back == t) { 3028 ASSERT(t->t_forw == t); 3029 /* 3030 * If the zone is empty, once the thread count 3031 * goes to zero no further kernel threads can be 3032 * created. This is because if the creator is a process 3033 * in the zone, then it must have exited before the zone 3034 * state could be set to ZONE_IS_EMPTY. 3035 * Otherwise, if the creator is a kernel thread in the 3036 * zone, the thread count is non-zero. 3037 * 3038 * This really means that non-zone kernel threads should 3039 * not create zone kernel threads. 3040 */ 3041 zone->zone_kthreads = NULL; 3042 if (zone_status_get(zone) == ZONE_IS_EMPTY) { 3043 zone_status_set(zone, ZONE_IS_DOWN); 3044 /* 3045 * Remove any CPU caps on this zone. 3046 */ 3047 cpucaps_zone_remove(zone); 3048 } 3049 } else { 3050 t->t_forw->t_back = t->t_back; 3051 t->t_back->t_forw = t->t_forw; 3052 if (zone->zone_kthreads == t) 3053 zone->zone_kthreads = t->t_forw; 3054 } 3055 mutex_exit(&zone_status_lock); 3056 zone_rele(zone); 3057 thread_exit(); 3058 /* NOTREACHED */ 3059 } 3060 3061 static void 3062 zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 3063 { 3064 vnode_t *oldvp; 3065 3066 /* we're going to hold a reference here to the directory */ 3067 VN_HOLD(vp); 3068 3069 if (audit_active) /* update abs cwd/root path see c2audit.c */ 3070 audit_chdirec(vp, vpp); 3071 3072 mutex_enter(&pp->p_lock); 3073 oldvp = *vpp; 3074 *vpp = vp; 3075 mutex_exit(&pp->p_lock); 3076 if (oldvp != NULL) 3077 VN_RELE(oldvp); 3078 } 3079 3080 /* 3081 * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 3082 */ 3083 static int 3084 nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 3085 { 3086 nvpair_t *nvp = NULL; 3087 boolean_t priv_set = B_FALSE; 3088 boolean_t limit_set = B_FALSE; 3089 boolean_t action_set = B_FALSE; 3090 3091 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 3092 const char *name; 3093 uint64_t ui64; 3094 3095 name = nvpair_name(nvp); 3096 if (nvpair_type(nvp) != DATA_TYPE_UINT64) 3097 return (EINVAL); 3098 (void) nvpair_value_uint64(nvp, &ui64); 3099 if (strcmp(name, "privilege") == 0) { 3100 /* 3101 * Currently only privileged values are allowed, but 3102 * this may change in the future. 3103 */ 3104 if (ui64 != RCPRIV_PRIVILEGED) 3105 return (EINVAL); 3106 rv->rcv_privilege = ui64; 3107 priv_set = B_TRUE; 3108 } else if (strcmp(name, "limit") == 0) { 3109 rv->rcv_value = ui64; 3110 limit_set = B_TRUE; 3111 } else if (strcmp(name, "action") == 0) { 3112 if (ui64 != RCTL_LOCAL_NOACTION && 3113 ui64 != RCTL_LOCAL_DENY) 3114 return (EINVAL); 3115 rv->rcv_flagaction = ui64; 3116 action_set = B_TRUE; 3117 } else { 3118 return (EINVAL); 3119 } 3120 } 3121 3122 if (!(priv_set && limit_set && action_set)) 3123 return (EINVAL); 3124 rv->rcv_action_signal = 0; 3125 rv->rcv_action_recipient = NULL; 3126 rv->rcv_action_recip_pid = -1; 3127 rv->rcv_firing_time = 0; 3128 3129 return (0); 3130 } 3131 3132 /* 3133 * Non-global zone version of start_init. 3134 */ 3135 void 3136 zone_start_init(void) 3137 { 3138 proc_t *p = ttoproc(curthread); 3139 zone_t *z = p->p_zone; 3140 3141 ASSERT(!INGLOBALZONE(curproc)); 3142 3143 /* 3144 * For all purposes (ZONE_ATTR_INITPID and restart_init), 3145 * storing just the pid of init is sufficient. 3146 */ 3147 z->zone_proc_initpid = p->p_pid; 3148 3149 /* 3150 * We maintain zone_boot_err so that we can return the cause of the 3151 * failure back to the caller of the zone_boot syscall. 3152 */ 3153 p->p_zone->zone_boot_err = start_init_common(); 3154 3155 /* 3156 * We will prevent booting zones from becoming running zones if the 3157 * global zone is shutting down. 3158 */ 3159 mutex_enter(&zone_status_lock); 3160 if (z->zone_boot_err != 0 || zone_status_get(global_zone) >= 3161 ZONE_IS_SHUTTING_DOWN) { 3162 /* 3163 * Make sure we are still in the booting state-- we could have 3164 * raced and already be shutting down, or even further along. 3165 */ 3166 if (zone_status_get(z) == ZONE_IS_BOOTING) { 3167 zone_status_set(z, ZONE_IS_SHUTTING_DOWN); 3168 } 3169 mutex_exit(&zone_status_lock); 3170 /* It's gone bad, dispose of the process */ 3171 if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) { 3172 mutex_enter(&p->p_lock); 3173 ASSERT(p->p_flag & SEXITLWPS); 3174 lwp_exit(); 3175 } 3176 } else { 3177 if (zone_status_get(z) == ZONE_IS_BOOTING) 3178 zone_status_set(z, ZONE_IS_RUNNING); 3179 mutex_exit(&zone_status_lock); 3180 /* cause the process to return to userland. */ 3181 lwp_rtt(); 3182 } 3183 } 3184 3185 struct zsched_arg { 3186 zone_t *zone; 3187 nvlist_t *nvlist; 3188 }; 3189 3190 /* 3191 * Per-zone "sched" workalike. The similarity to "sched" doesn't have 3192 * anything to do with scheduling, but rather with the fact that 3193 * per-zone kernel threads are parented to zsched, just like regular 3194 * kernel threads are parented to sched (p0). 3195 * 3196 * zsched is also responsible for launching init for the zone. 3197 */ 3198 static void 3199 zsched(void *arg) 3200 { 3201 struct zsched_arg *za = arg; 3202 proc_t *pp = curproc; 3203 proc_t *initp = proc_init; 3204 zone_t *zone = za->zone; 3205 cred_t *cr, *oldcred; 3206 rctl_set_t *set; 3207 rctl_alloc_gp_t *gp; 3208 contract_t *ct = NULL; 3209 task_t *tk, *oldtk; 3210 rctl_entity_p_t e; 3211 kproject_t *pj; 3212 3213 nvlist_t *nvl = za->nvlist; 3214 nvpair_t *nvp = NULL; 3215 3216 bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched")); 3217 bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched")); 3218 PTOU(pp)->u_argc = 0; 3219 PTOU(pp)->u_argv = NULL; 3220 PTOU(pp)->u_envp = NULL; 3221 closeall(P_FINFO(pp)); 3222 3223 /* 3224 * We are this zone's "zsched" process. As the zone isn't generally 3225 * visible yet we don't need to grab any locks before initializing its 3226 * zone_proc pointer. 3227 */ 3228 zone_hold(zone); /* this hold is released by zone_destroy() */ 3229 zone->zone_zsched = pp; 3230 mutex_enter(&pp->p_lock); 3231 pp->p_zone = zone; 3232 mutex_exit(&pp->p_lock); 3233 3234 /* 3235 * Disassociate process from its 'parent'; parent ourselves to init 3236 * (pid 1) and change other values as needed. 3237 */ 3238 sess_create(); 3239 3240 mutex_enter(&pidlock); 3241 proc_detach(pp); 3242 pp->p_ppid = 1; 3243 pp->p_flag |= SZONETOP; 3244 pp->p_ancpid = 1; 3245 pp->p_parent = initp; 3246 pp->p_psibling = NULL; 3247 if (initp->p_child) 3248 initp->p_child->p_psibling = pp; 3249 pp->p_sibling = initp->p_child; 3250 initp->p_child = pp; 3251 3252 /* Decrement what newproc() incremented. */ 3253 upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 3254 /* 3255 * Our credentials are about to become kcred-like, so we don't care 3256 * about the caller's ruid. 3257 */ 3258 upcount_inc(crgetruid(kcred), zone->zone_id); 3259 mutex_exit(&pidlock); 3260 3261 /* 3262 * getting out of global zone, so decrement lwp counts 3263 */ 3264 pj = pp->p_task->tk_proj; 3265 mutex_enter(&global_zone->zone_nlwps_lock); 3266 pj->kpj_nlwps -= pp->p_lwpcnt; 3267 global_zone->zone_nlwps -= pp->p_lwpcnt; 3268 mutex_exit(&global_zone->zone_nlwps_lock); 3269 3270 /* 3271 * Decrement locked memory counts on old zone and project. 3272 */ 3273 mutex_enter(&global_zone->zone_mem_lock); 3274 global_zone->zone_locked_mem -= pp->p_locked_mem; 3275 pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 3276 mutex_exit(&global_zone->zone_mem_lock); 3277 3278 /* 3279 * Create and join a new task in project '0' of this zone. 3280 * 3281 * We don't need to call holdlwps() since we know we're the only lwp in 3282 * this process. 3283 * 3284 * task_join() returns with p_lock held. 3285 */ 3286 tk = task_create(0, zone); 3287 mutex_enter(&cpu_lock); 3288 oldtk = task_join(tk, 0); 3289 3290 pj = pp->p_task->tk_proj; 3291 3292 mutex_enter(&zone->zone_mem_lock); 3293 zone->zone_locked_mem += pp->p_locked_mem; 3294 pj->kpj_data.kpd_locked_mem += pp->p_locked_mem; 3295 mutex_exit(&zone->zone_mem_lock); 3296 3297 /* 3298 * add lwp counts to zsched's zone, and increment project's task count 3299 * due to the task created in the above tasksys_settaskid 3300 */ 3301 3302 mutex_enter(&zone->zone_nlwps_lock); 3303 pj->kpj_nlwps += pp->p_lwpcnt; 3304 pj->kpj_ntasks += 1; 3305 zone->zone_nlwps += pp->p_lwpcnt; 3306 mutex_exit(&zone->zone_nlwps_lock); 3307 3308 mutex_exit(&curproc->p_lock); 3309 mutex_exit(&cpu_lock); 3310 task_rele(oldtk); 3311 3312 /* 3313 * The process was created by a process in the global zone, hence the 3314 * credentials are wrong. We might as well have kcred-ish credentials. 3315 */ 3316 cr = zone->zone_kcred; 3317 crhold(cr); 3318 mutex_enter(&pp->p_crlock); 3319 oldcred = pp->p_cred; 3320 pp->p_cred = cr; 3321 mutex_exit(&pp->p_crlock); 3322 crfree(oldcred); 3323 3324 /* 3325 * Hold credentials again (for thread) 3326 */ 3327 crhold(cr); 3328 3329 /* 3330 * p_lwpcnt can't change since this is a kernel process. 3331 */ 3332 crset(pp, cr); 3333 3334 /* 3335 * Chroot 3336 */ 3337 zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 3338 zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 3339 3340 /* 3341 * Initialize zone's rctl set. 3342 */ 3343 set = rctl_set_create(); 3344 gp = rctl_set_init_prealloc(RCENTITY_ZONE); 3345 mutex_enter(&pp->p_lock); 3346 e.rcep_p.zone = zone; 3347 e.rcep_t = RCENTITY_ZONE; 3348 zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 3349 mutex_exit(&pp->p_lock); 3350 rctl_prealloc_destroy(gp); 3351 3352 /* 3353 * Apply the rctls passed in to zone_create(). This is basically a list 3354 * assignment: all of the old values are removed and the new ones 3355 * inserted. That is, if an empty list is passed in, all values are 3356 * removed. 3357 */ 3358 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 3359 rctl_dict_entry_t *rde; 3360 rctl_hndl_t hndl; 3361 char *name; 3362 nvlist_t **nvlarray; 3363 uint_t i, nelem; 3364 int error; /* For ASSERT()s */ 3365 3366 name = nvpair_name(nvp); 3367 hndl = rctl_hndl_lookup(name); 3368 ASSERT(hndl != -1); 3369 rde = rctl_dict_lookup_hndl(hndl); 3370 ASSERT(rde != NULL); 3371 3372 for (; /* ever */; ) { 3373 rctl_val_t oval; 3374 3375 mutex_enter(&pp->p_lock); 3376 error = rctl_local_get(hndl, NULL, &oval, pp); 3377 mutex_exit(&pp->p_lock); 3378 ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 3379 ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 3380 if (oval.rcv_privilege == RCPRIV_SYSTEM) 3381 break; 3382 mutex_enter(&pp->p_lock); 3383 error = rctl_local_delete(hndl, &oval, pp); 3384 mutex_exit(&pp->p_lock); 3385 ASSERT(error == 0); 3386 } 3387 error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 3388 ASSERT(error == 0); 3389 for (i = 0; i < nelem; i++) { 3390 rctl_val_t *nvalp; 3391 3392 nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 3393 error = nvlist2rctlval(nvlarray[i], nvalp); 3394 ASSERT(error == 0); 3395 /* 3396 * rctl_local_insert can fail if the value being 3397 * inserted is a duplicate; this is OK. 3398 */ 3399 mutex_enter(&pp->p_lock); 3400 if (rctl_local_insert(hndl, nvalp, pp) != 0) 3401 kmem_cache_free(rctl_val_cache, nvalp); 3402 mutex_exit(&pp->p_lock); 3403 } 3404 } 3405 /* 3406 * Tell the world that we're done setting up. 3407 * 3408 * At this point we want to set the zone status to ZONE_IS_INITIALIZED 3409 * and atomically set the zone's processor set visibility. Once 3410 * we drop pool_lock() this zone will automatically get updated 3411 * to reflect any future changes to the pools configuration. 3412 * 3413 * Note that after we drop the locks below (zonehash_lock in 3414 * particular) other operations such as a zone_getattr call can 3415 * now proceed and observe the zone. That is the reason for doing a 3416 * state transition to the INITIALIZED state. 3417 */ 3418 pool_lock(); 3419 mutex_enter(&cpu_lock); 3420 mutex_enter(&zonehash_lock); 3421 zone_uniqid(zone); 3422 zone_zsd_configure(zone); 3423 if (pool_state == POOL_ENABLED) 3424 zone_pset_set(zone, pool_default->pool_pset->pset_id); 3425 mutex_enter(&zone_status_lock); 3426 ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 3427 zone_status_set(zone, ZONE_IS_INITIALIZED); 3428 mutex_exit(&zone_status_lock); 3429 mutex_exit(&zonehash_lock); 3430 mutex_exit(&cpu_lock); 3431 pool_unlock(); 3432 3433 /* Now call the create callback for this key */ 3434 zsd_apply_all_keys(zsd_apply_create, zone); 3435 3436 /* The callbacks are complete. Mark ZONE_IS_READY */ 3437 mutex_enter(&zone_status_lock); 3438 ASSERT(zone_status_get(zone) == ZONE_IS_INITIALIZED); 3439 zone_status_set(zone, ZONE_IS_READY); 3440 mutex_exit(&zone_status_lock); 3441 3442 /* 3443 * Once we see the zone transition to the ZONE_IS_BOOTING state, 3444 * we launch init, and set the state to running. 3445 */ 3446 zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 3447 3448 if (zone_status_get(zone) == ZONE_IS_BOOTING) { 3449 id_t cid; 3450 3451 /* 3452 * Ok, this is a little complicated. We need to grab the 3453 * zone's pool's scheduling class ID; note that by now, we 3454 * are already bound to a pool if we need to be (zoneadmd 3455 * will have done that to us while we're in the READY 3456 * state). *But* the scheduling class for the zone's 'init' 3457 * must be explicitly passed to newproc, which doesn't 3458 * respect pool bindings. 3459 * 3460 * We hold the pool_lock across the call to newproc() to 3461 * close the obvious race: the pool's scheduling class 3462 * could change before we manage to create the LWP with 3463 * classid 'cid'. 3464 */ 3465 pool_lock(); 3466 if (zone->zone_defaultcid > 0) 3467 cid = zone->zone_defaultcid; 3468 else 3469 cid = pool_get_class(zone->zone_pool); 3470 if (cid == -1) 3471 cid = defaultcid; 3472 3473 /* 3474 * If this fails, zone_boot will ultimately fail. The 3475 * state of the zone will be set to SHUTTING_DOWN-- userland 3476 * will have to tear down the zone, and fail, or try again. 3477 */ 3478 if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid, 3479 minclsyspri - 1, &ct)) != 0) { 3480 mutex_enter(&zone_status_lock); 3481 zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 3482 mutex_exit(&zone_status_lock); 3483 } 3484 pool_unlock(); 3485 } 3486 3487 /* 3488 * Wait for zone_destroy() to be called. This is what we spend 3489 * most of our life doing. 3490 */ 3491 zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 3492 3493 if (ct) 3494 /* 3495 * At this point the process contract should be empty. 3496 * (Though if it isn't, it's not the end of the world.) 3497 */ 3498 VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 3499 3500 /* 3501 * Allow kcred to be freed when all referring processes 3502 * (including this one) go away. We can't just do this in 3503 * zone_free because we need to wait for the zone_cred_ref to 3504 * drop to 0 before calling zone_free, and the existence of 3505 * zone_kcred will prevent that. Thus, we call crfree here to 3506 * balance the crdup in zone_create. The crhold calls earlier 3507 * in zsched will be dropped when the thread and process exit. 3508 */ 3509 crfree(zone->zone_kcred); 3510 zone->zone_kcred = NULL; 3511 3512 exit(CLD_EXITED, 0); 3513 } 3514 3515 /* 3516 * Helper function to determine if there are any submounts of the 3517 * provided path. Used to make sure the zone doesn't "inherit" any 3518 * mounts from before it is created. 3519 */ 3520 static uint_t 3521 zone_mount_count(const char *rootpath) 3522 { 3523 vfs_t *vfsp; 3524 uint_t count = 0; 3525 size_t rootpathlen = strlen(rootpath); 3526 3527 /* 3528 * Holding zonehash_lock prevents race conditions with 3529 * vfs_list_add()/vfs_list_remove() since we serialize with 3530 * zone_find_by_path(). 3531 */ 3532 ASSERT(MUTEX_HELD(&zonehash_lock)); 3533 /* 3534 * The rootpath must end with a '/' 3535 */ 3536 ASSERT(rootpath[rootpathlen - 1] == '/'); 3537 3538 /* 3539 * This intentionally does not count the rootpath itself if that 3540 * happens to be a mount point. 3541 */ 3542 vfs_list_read_lock(); 3543 vfsp = rootvfs; 3544 do { 3545 if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 3546 rootpathlen) == 0) 3547 count++; 3548 vfsp = vfsp->vfs_next; 3549 } while (vfsp != rootvfs); 3550 vfs_list_unlock(); 3551 return (count); 3552 } 3553 3554 /* 3555 * Helper function to make sure that a zone created on 'rootpath' 3556 * wouldn't end up containing other zones' rootpaths. 3557 */ 3558 static boolean_t 3559 zone_is_nested(const char *rootpath) 3560 { 3561 zone_t *zone; 3562 size_t rootpathlen = strlen(rootpath); 3563 size_t len; 3564 3565 ASSERT(MUTEX_HELD(&zonehash_lock)); 3566 3567 /* 3568 * zone_set_root() appended '/' and '\0' at the end of rootpath 3569 */ 3570 if ((rootpathlen <= 3) && (rootpath[0] == '/') && 3571 (rootpath[1] == '/') && (rootpath[2] == '\0')) 3572 return (B_TRUE); 3573 3574 for (zone = list_head(&zone_active); zone != NULL; 3575 zone = list_next(&zone_active, zone)) { 3576 if (zone == global_zone) 3577 continue; 3578 len = strlen(zone->zone_rootpath); 3579 if (strncmp(rootpath, zone->zone_rootpath, 3580 MIN(rootpathlen, len)) == 0) 3581 return (B_TRUE); 3582 } 3583 return (B_FALSE); 3584 } 3585 3586 static int 3587 zone_set_privset(zone_t *zone, const priv_set_t *zone_privs, 3588 size_t zone_privssz) 3589 { 3590 priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 3591 3592 if (zone_privssz < sizeof (priv_set_t)) 3593 return (set_errno(ENOMEM)); 3594 3595 if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 3596 kmem_free(privs, sizeof (priv_set_t)); 3597 return (EFAULT); 3598 } 3599 3600 zone->zone_privset = privs; 3601 return (0); 3602 } 3603 3604 /* 3605 * We make creative use of nvlists to pass in rctls from userland. The list is 3606 * a list of the following structures: 3607 * 3608 * (name = rctl_name, value = nvpair_list_array) 3609 * 3610 * Where each element of the nvpair_list_array is of the form: 3611 * 3612 * [(name = "privilege", value = RCPRIV_PRIVILEGED), 3613 * (name = "limit", value = uint64_t), 3614 * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 3615 */ 3616 static int 3617 parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 3618 { 3619 nvpair_t *nvp = NULL; 3620 nvlist_t *nvl = NULL; 3621 char *kbuf; 3622 int error; 3623 rctl_val_t rv; 3624 3625 *nvlp = NULL; 3626 3627 if (buflen == 0) 3628 return (0); 3629 3630 if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 3631 return (ENOMEM); 3632 if (copyin(ubuf, kbuf, buflen)) { 3633 error = EFAULT; 3634 goto out; 3635 } 3636 if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 3637 /* 3638 * nvl may have been allocated/free'd, but the value set to 3639 * non-NULL, so we reset it here. 3640 */ 3641 nvl = NULL; 3642 error = EINVAL; 3643 goto out; 3644 } 3645 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 3646 rctl_dict_entry_t *rde; 3647 rctl_hndl_t hndl; 3648 nvlist_t **nvlarray; 3649 uint_t i, nelem; 3650 char *name; 3651 3652 error = EINVAL; 3653 name = nvpair_name(nvp); 3654 if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 3655 != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 3656 goto out; 3657 } 3658 if ((hndl = rctl_hndl_lookup(name)) == -1) { 3659 goto out; 3660 } 3661 rde = rctl_dict_lookup_hndl(hndl); 3662 error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 3663 ASSERT(error == 0); 3664 for (i = 0; i < nelem; i++) { 3665 if (error = nvlist2rctlval(nvlarray[i], &rv)) 3666 goto out; 3667 } 3668 if (rctl_invalid_value(rde, &rv)) { 3669 error = EINVAL; 3670 goto out; 3671 } 3672 } 3673 error = 0; 3674 *nvlp = nvl; 3675 out: 3676 kmem_free(kbuf, buflen); 3677 if (error && nvl != NULL) 3678 nvlist_free(nvl); 3679 return (error); 3680 } 3681 3682 int 3683 zone_create_error(int er_error, int er_ext, int *er_out) { 3684 if (er_out != NULL) { 3685 if (copyout(&er_ext, er_out, sizeof (int))) { 3686 return (set_errno(EFAULT)); 3687 } 3688 } 3689 return (set_errno(er_error)); 3690 } 3691 3692 static int 3693 zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi) 3694 { 3695 ts_label_t *tsl; 3696 bslabel_t blab; 3697 3698 /* Get label from user */ 3699 if (copyin(lab, &blab, sizeof (blab)) != 0) 3700 return (EFAULT); 3701 tsl = labelalloc(&blab, doi, KM_NOSLEEP); 3702 if (tsl == NULL) 3703 return (ENOMEM); 3704 3705 zone->zone_slabel = tsl; 3706 return (0); 3707 } 3708 3709 /* 3710 * Parses a comma-separated list of ZFS datasets into a per-zone dictionary. 3711 */ 3712 static int 3713 parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen) 3714 { 3715 char *kbuf; 3716 char *dataset, *next; 3717 zone_dataset_t *zd; 3718 size_t len; 3719 3720 if (ubuf == NULL || buflen == 0) 3721 return (0); 3722 3723 if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 3724 return (ENOMEM); 3725 3726 if (copyin(ubuf, kbuf, buflen) != 0) { 3727 kmem_free(kbuf, buflen); 3728 return (EFAULT); 3729 } 3730 3731 dataset = next = kbuf; 3732 for (;;) { 3733 zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP); 3734 3735 next = strchr(dataset, ','); 3736 3737 if (next == NULL) 3738 len = strlen(dataset); 3739 else 3740 len = next - dataset; 3741 3742 zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP); 3743 bcopy(dataset, zd->zd_dataset, len); 3744 zd->zd_dataset[len] = '\0'; 3745 3746 list_insert_head(&zone->zone_datasets, zd); 3747 3748 if (next == NULL) 3749 break; 3750 3751 dataset = next + 1; 3752 } 3753 3754 kmem_free(kbuf, buflen); 3755 return (0); 3756 } 3757 3758 /* 3759 * System call to create/initialize a new zone named 'zone_name', rooted 3760 * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 3761 * and initialized with the zone-wide rctls described in 'rctlbuf', and 3762 * with labeling set by 'match', 'doi', and 'label'. 3763 * 3764 * If extended error is non-null, we may use it to return more detailed 3765 * error information. 3766 */ 3767 static zoneid_t 3768 zone_create(const char *zone_name, const char *zone_root, 3769 const priv_set_t *zone_privs, size_t zone_privssz, 3770 caddr_t rctlbuf, size_t rctlbufsz, 3771 caddr_t zfsbuf, size_t zfsbufsz, int *extended_error, 3772 int match, uint32_t doi, const bslabel_t *label, 3773 int flags) 3774 { 3775 struct zsched_arg zarg; 3776 nvlist_t *rctls = NULL; 3777 proc_t *pp = curproc; 3778 zone_t *zone, *ztmp; 3779 zoneid_t zoneid; 3780 int error; 3781 int error2 = 0; 3782 char *str; 3783 cred_t *zkcr; 3784 boolean_t insert_label_hash; 3785 3786 if (secpolicy_zone_config(CRED()) != 0) 3787 return (set_errno(EPERM)); 3788 3789 /* can't boot zone from within chroot environment */ 3790 if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 3791 return (zone_create_error(ENOTSUP, ZE_CHROOTED, 3792 extended_error)); 3793 3794 zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 3795 zoneid = zone->zone_id = id_alloc(zoneid_space); 3796 zone->zone_status = ZONE_IS_UNINITIALIZED; 3797 zone->zone_pool = pool_default; 3798 zone->zone_pool_mod = gethrtime(); 3799 zone->zone_psetid = ZONE_PS_INVAL; 3800 zone->zone_ncpus = 0; 3801 zone->zone_ncpus_online = 0; 3802 zone->zone_restart_init = B_TRUE; 3803 zone->zone_brand = &native_brand; 3804 zone->zone_initname = NULL; 3805 mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 3806 mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 3807 mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL); 3808 cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 3809 list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 3810 offsetof(struct zsd_entry, zsd_linkage)); 3811 list_create(&zone->zone_datasets, sizeof (zone_dataset_t), 3812 offsetof(zone_dataset_t, zd_linkage)); 3813 list_create(&zone->zone_dl_list, sizeof (zone_dl_t), 3814 offsetof(zone_dl_t, zdl_linkage)); 3815 rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL); 3816 3817 if (flags & ZCF_NET_EXCL) { 3818 zone->zone_flags |= ZF_NET_EXCL; 3819 } 3820 3821 if ((error = zone_set_name(zone, zone_name)) != 0) { 3822 zone_free(zone); 3823 return (zone_create_error(error, 0, extended_error)); 3824 } 3825 3826 if ((error = zone_set_root(zone, zone_root)) != 0) { 3827 zone_free(zone); 3828 return (zone_create_error(error, 0, extended_error)); 3829 } 3830 if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) { 3831 zone_free(zone); 3832 return (zone_create_error(error, 0, extended_error)); 3833 } 3834 3835 /* initialize node name to be the same as zone name */ 3836 zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 3837 (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 3838 zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 3839 3840 zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 3841 zone->zone_domain[0] = '\0'; 3842 zone->zone_hostid = HW_INVALID_HOSTID; 3843 zone->zone_shares = 1; 3844 zone->zone_shmmax = 0; 3845 zone->zone_ipc.ipcq_shmmni = 0; 3846 zone->zone_ipc.ipcq_semmni = 0; 3847 zone->zone_ipc.ipcq_msgmni = 0; 3848 zone->zone_bootargs = NULL; 3849 zone->zone_initname = 3850 kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP); 3851 (void) strcpy(zone->zone_initname, zone_default_initname); 3852 zone->zone_nlwps = 0; 3853 zone->zone_nlwps_ctl = INT_MAX; 3854 zone->zone_locked_mem = 0; 3855 zone->zone_locked_mem_ctl = UINT64_MAX; 3856 zone->zone_max_swap = 0; 3857 zone->zone_max_swap_ctl = UINT64_MAX; 3858 zone0.zone_lockedmem_kstat = NULL; 3859 zone0.zone_swapresv_kstat = NULL; 3860 3861 /* 3862 * Zsched initializes the rctls. 3863 */ 3864 zone->zone_rctls = NULL; 3865 3866 if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 3867 zone_free(zone); 3868 return (zone_create_error(error, 0, extended_error)); 3869 } 3870 3871 if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) { 3872 zone_free(zone); 3873 return (set_errno(error)); 3874 } 3875 3876 /* 3877 * Read in the trusted system parameters: 3878 * match flag and sensitivity label. 3879 */ 3880 zone->zone_match = match; 3881 if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 3882 /* Fail if requested to set doi to anything but system's doi */ 3883 if (doi != 0 && doi != default_doi) { 3884 zone_free(zone); 3885 return (set_errno(EINVAL)); 3886 } 3887 /* Always apply system's doi to the zone */ 3888 error = zone_set_label(zone, label, default_doi); 3889 if (error != 0) { 3890 zone_free(zone); 3891 return (set_errno(error)); 3892 } 3893 insert_label_hash = B_TRUE; 3894 } else { 3895 /* all zones get an admin_low label if system is not labeled */ 3896 zone->zone_slabel = l_admin_low; 3897 label_hold(l_admin_low); 3898 insert_label_hash = B_FALSE; 3899 } 3900 3901 /* 3902 * Stop all lwps since that's what normally happens as part of fork(). 3903 * This needs to happen before we grab any locks to avoid deadlock 3904 * (another lwp in the process could be waiting for the held lock). 3905 */ 3906 if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 3907 zone_free(zone); 3908 if (rctls) 3909 nvlist_free(rctls); 3910 return (zone_create_error(error, 0, extended_error)); 3911 } 3912 3913 if (block_mounts() == 0) { 3914 mutex_enter(&pp->p_lock); 3915 if (curthread != pp->p_agenttp) 3916 continuelwps(pp); 3917 mutex_exit(&pp->p_lock); 3918 zone_free(zone); 3919 if (rctls) 3920 nvlist_free(rctls); 3921 return (zone_create_error(error, 0, extended_error)); 3922 } 3923 3924 /* 3925 * Set up credential for kernel access. After this, any errors 3926 * should go through the dance in errout rather than calling 3927 * zone_free directly. 3928 */ 3929 zone->zone_kcred = crdup(kcred); 3930 crsetzone(zone->zone_kcred, zone); 3931 priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 3932 priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 3933 priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 3934 priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 3935 3936 mutex_enter(&zonehash_lock); 3937 /* 3938 * Make sure zone doesn't already exist. 3939 * 3940 * If the system and zone are labeled, 3941 * make sure no other zone exists that has the same label. 3942 */ 3943 if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL || 3944 (insert_label_hash && 3945 (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) { 3946 zone_status_t status; 3947 3948 status = zone_status_get(ztmp); 3949 if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 3950 error = EEXIST; 3951 else 3952 error = EBUSY; 3953 3954 if (insert_label_hash) 3955 error2 = ZE_LABELINUSE; 3956 3957 goto errout; 3958 } 3959 3960 /* 3961 * Don't allow zone creations which would cause one zone's rootpath to 3962 * be accessible from that of another (non-global) zone. 3963 */ 3964 if (zone_is_nested(zone->zone_rootpath)) { 3965 error = EBUSY; 3966 goto errout; 3967 } 3968 3969 ASSERT(zonecount != 0); /* check for leaks */ 3970 if (zonecount + 1 > maxzones) { 3971 error = ENOMEM; 3972 goto errout; 3973 } 3974 3975 if (zone_mount_count(zone->zone_rootpath) != 0) { 3976 error = EBUSY; 3977 error2 = ZE_AREMOUNTS; 3978 goto errout; 3979 } 3980 3981 /* 3982 * Zone is still incomplete, but we need to drop all locks while 3983 * zsched() initializes this zone's kernel process. We 3984 * optimistically add the zone to the hashtable and associated 3985 * lists so a parallel zone_create() doesn't try to create the 3986 * same zone. 3987 */ 3988 zonecount++; 3989 (void) mod_hash_insert(zonehashbyid, 3990 (mod_hash_key_t)(uintptr_t)zone->zone_id, 3991 (mod_hash_val_t)(uintptr_t)zone); 3992 str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 3993 (void) strcpy(str, zone->zone_name); 3994 (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 3995 (mod_hash_val_t)(uintptr_t)zone); 3996 if (insert_label_hash) { 3997 (void) mod_hash_insert(zonehashbylabel, 3998 (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone); 3999 zone->zone_flags |= ZF_HASHED_LABEL; 4000 } 4001 4002 /* 4003 * Insert into active list. At this point there are no 'hold's 4004 * on the zone, but everyone else knows not to use it, so we can 4005 * continue to use it. zsched() will do a zone_hold() if the 4006 * newproc() is successful. 4007 */ 4008 list_insert_tail(&zone_active, zone); 4009 mutex_exit(&zonehash_lock); 4010 4011 zarg.zone = zone; 4012 zarg.nvlist = rctls; 4013 /* 4014 * The process, task, and project rctls are probably wrong; 4015 * we need an interface to get the default values of all rctls, 4016 * and initialize zsched appropriately. I'm not sure that that 4017 * makes much of a difference, though. 4018 */ 4019 if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) { 4020 /* 4021 * We need to undo all globally visible state. 4022 */ 4023 mutex_enter(&zonehash_lock); 4024 list_remove(&zone_active, zone); 4025 if (zone->zone_flags & ZF_HASHED_LABEL) { 4026 ASSERT(zone->zone_slabel != NULL); 4027 (void) mod_hash_destroy(zonehashbylabel, 4028 (mod_hash_key_t)zone->zone_slabel); 4029 } 4030 (void) mod_hash_destroy(zonehashbyname, 4031 (mod_hash_key_t)(uintptr_t)zone->zone_name); 4032 (void) mod_hash_destroy(zonehashbyid, 4033 (mod_hash_key_t)(uintptr_t)zone->zone_id); 4034 ASSERT(zonecount > 1); 4035 zonecount--; 4036 goto errout; 4037 } 4038 4039 /* 4040 * Zone creation can't fail from now on. 4041 */ 4042 4043 /* 4044 * Create zone kstats 4045 */ 4046 zone_kstat_create(zone); 4047 4048 /* 4049 * Let the other lwps continue. 4050 */ 4051 mutex_enter(&pp->p_lock); 4052 if (curthread != pp->p_agenttp) 4053 continuelwps(pp); 4054 mutex_exit(&pp->p_lock); 4055 4056 /* 4057 * Wait for zsched to finish initializing the zone. 4058 */ 4059 zone_status_wait(zone, ZONE_IS_READY); 4060 /* 4061 * The zone is fully visible, so we can let mounts progress. 4062 */ 4063 resume_mounts(); 4064 if (rctls) 4065 nvlist_free(rctls); 4066 4067 return (zoneid); 4068 4069 errout: 4070 mutex_exit(&zonehash_lock); 4071 /* 4072 * Let the other lwps continue. 4073 */ 4074 mutex_enter(&pp->p_lock); 4075 if (curthread != pp->p_agenttp) 4076 continuelwps(pp); 4077 mutex_exit(&pp->p_lock); 4078 4079 resume_mounts(); 4080 if (rctls) 4081 nvlist_free(rctls); 4082 /* 4083 * There is currently one reference to the zone, a cred_ref from 4084 * zone_kcred. To free the zone, we call crfree, which will call 4085 * zone_cred_rele, which will call zone_free. 4086 */ 4087 ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 4088 ASSERT(zone->zone_kcred->cr_ref == 1); 4089 ASSERT(zone->zone_ref == 0); 4090 zkcr = zone->zone_kcred; 4091 zone->zone_kcred = NULL; 4092 crfree(zkcr); /* triggers call to zone_free */ 4093 return (zone_create_error(error, error2, extended_error)); 4094 } 4095 4096 /* 4097 * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 4098 * the heavy lifting. initname is the path to the program to launch 4099 * at the "top" of the zone; if this is NULL, we use the system default, 4100 * which is stored at zone_default_initname. 4101 */ 4102 static int 4103 zone_boot(zoneid_t zoneid) 4104 { 4105 int err; 4106 zone_t *zone; 4107 4108 if (secpolicy_zone_config(CRED()) != 0) 4109 return (set_errno(EPERM)); 4110 if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 4111 return (set_errno(EINVAL)); 4112 4113 mutex_enter(&zonehash_lock); 4114 /* 4115 * Look for zone under hash lock to prevent races with calls to 4116 * zone_shutdown, zone_destroy, etc. 4117 */ 4118 if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 4119 mutex_exit(&zonehash_lock); 4120 return (set_errno(EINVAL)); 4121 } 4122 4123 mutex_enter(&zone_status_lock); 4124 if (zone_status_get(zone) != ZONE_IS_READY) { 4125 mutex_exit(&zone_status_lock); 4126 mutex_exit(&zonehash_lock); 4127 return (set_errno(EINVAL)); 4128 } 4129 zone_status_set(zone, ZONE_IS_BOOTING); 4130 mutex_exit(&zone_status_lock); 4131 4132 zone_hold(zone); /* so we can use the zone_t later */ 4133 mutex_exit(&zonehash_lock); 4134 4135 if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 4136 zone_rele(zone); 4137 return (set_errno(EINTR)); 4138 } 4139 4140 /* 4141 * Boot (starting init) might have failed, in which case the zone 4142 * will go to the SHUTTING_DOWN state; an appropriate errno will 4143 * be placed in zone->zone_boot_err, and so we return that. 4144 */ 4145 err = zone->zone_boot_err; 4146 zone_rele(zone); 4147 return (err ? set_errno(err) : 0); 4148 } 4149 4150 /* 4151 * Kills all user processes in the zone, waiting for them all to exit 4152 * before returning. 4153 */ 4154 static int 4155 zone_empty(zone_t *zone) 4156 { 4157 int waitstatus; 4158 4159 /* 4160 * We need to drop zonehash_lock before killing all 4161 * processes, otherwise we'll deadlock with zone_find_* 4162 * which can be called from the exit path. 4163 */ 4164 ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 4165 while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz, 4166 ZONE_IS_EMPTY)) == -1) { 4167 killall(zone->zone_id); 4168 } 4169 /* 4170 * return EINTR if we were signaled 4171 */ 4172 if (waitstatus == 0) 4173 return (EINTR); 4174 return (0); 4175 } 4176 4177 /* 4178 * This function implements the policy for zone visibility. 4179 * 4180 * In standard Solaris, a non-global zone can only see itself. 4181 * 4182 * In Trusted Extensions, a labeled zone can lookup any zone whose label 4183 * it dominates. For this test, the label of the global zone is treated as 4184 * admin_high so it is special-cased instead of being checked for dominance. 4185 * 4186 * Returns true if zone attributes are viewable, false otherwise. 4187 */ 4188 static boolean_t 4189 zone_list_access(zone_t *zone) 4190 { 4191 4192 if (curproc->p_zone == global_zone || 4193 curproc->p_zone == zone) { 4194 return (B_TRUE); 4195 } else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) { 4196 bslabel_t *curproc_label; 4197 bslabel_t *zone_label; 4198 4199 curproc_label = label2bslabel(curproc->p_zone->zone_slabel); 4200 zone_label = label2bslabel(zone->zone_slabel); 4201 4202 if (zone->zone_id != GLOBAL_ZONEID && 4203 bldominates(curproc_label, zone_label)) { 4204 return (B_TRUE); 4205 } else { 4206 return (B_FALSE); 4207 } 4208 } else { 4209 return (B_FALSE); 4210 } 4211 } 4212 4213 /* 4214 * Systemcall to start the zone's halt sequence. By the time this 4215 * function successfully returns, all user processes and kernel threads 4216 * executing in it will have exited, ZSD shutdown callbacks executed, 4217 * and the zone status set to ZONE_IS_DOWN. 4218 * 4219 * It is possible that the call will interrupt itself if the caller is the 4220 * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 4221 */ 4222 static int 4223 zone_shutdown(zoneid_t zoneid) 4224 { 4225 int error; 4226 zone_t *zone; 4227 zone_status_t status; 4228 4229 if (secpolicy_zone_config(CRED()) != 0) 4230 return (set_errno(EPERM)); 4231 if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 4232 return (set_errno(EINVAL)); 4233 4234 /* 4235 * Block mounts so that VFS_MOUNT() can get an accurate view of 4236 * the zone's status with regards to ZONE_IS_SHUTTING down. 4237 * 4238 * e.g. NFS can fail the mount if it determines that the zone 4239 * has already begun the shutdown sequence. 4240 */ 4241 if (block_mounts() == 0) 4242 return (set_errno(EINTR)); 4243 mutex_enter(&zonehash_lock); 4244 /* 4245 * Look for zone under hash lock to prevent races with other 4246 * calls to zone_shutdown and zone_destroy. 4247 */ 4248 if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 4249 mutex_exit(&zonehash_lock); 4250 resume_mounts(); 4251 return (set_errno(EINVAL)); 4252 } 4253 mutex_enter(&zone_status_lock); 4254 status = zone_status_get(zone); 4255 /* 4256 * Fail if the zone isn't fully initialized yet. 4257 */ 4258 if (status < ZONE_IS_READY) { 4259 mutex_exit(&zone_status_lock); 4260 mutex_exit(&zonehash_lock); 4261 resume_mounts(); 4262 return (set_errno(EINVAL)); 4263 } 4264 /* 4265 * If conditions required for zone_shutdown() to return have been met, 4266 * return success. 4267 */ 4268 if (status >= ZONE_IS_DOWN) { 4269 mutex_exit(&zone_status_lock); 4270 mutex_exit(&zonehash_lock); 4271 resume_mounts(); 4272 return (0); 4273 } 4274 /* 4275 * If zone_shutdown() hasn't been called before, go through the motions. 4276 * If it has, there's nothing to do but wait for the kernel threads to 4277 * drain. 4278 */ 4279 if (status < ZONE_IS_EMPTY) { 4280 uint_t ntasks; 4281 4282 mutex_enter(&zone->zone_lock); 4283 if ((ntasks = zone->zone_ntasks) != 1) { 4284 /* 4285 * There's still stuff running. 4286 */ 4287 zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 4288 } 4289 mutex_exit(&zone->zone_lock); 4290 if (ntasks == 1) { 4291 /* 4292 * The only way to create another task is through 4293 * zone_enter(), which will block until we drop 4294 * zonehash_lock. The zone is empty. 4295 */ 4296 if (zone->zone_kthreads == NULL) { 4297 /* 4298 * Skip ahead to ZONE_IS_DOWN 4299 */ 4300 zone_status_set(zone, ZONE_IS_DOWN); 4301 } else { 4302 zone_status_set(zone, ZONE_IS_EMPTY); 4303 } 4304 } 4305 } 4306 zone_hold(zone); /* so we can use the zone_t later */ 4307 mutex_exit(&zone_status_lock); 4308 mutex_exit(&zonehash_lock); 4309 resume_mounts(); 4310 4311 if (error = zone_empty(zone)) { 4312 zone_rele(zone); 4313 return (set_errno(error)); 4314 } 4315 /* 4316 * After the zone status goes to ZONE_IS_DOWN this zone will no 4317 * longer be notified of changes to the pools configuration, so 4318 * in order to not end up with a stale pool pointer, we point 4319 * ourselves at the default pool and remove all resource 4320 * visibility. This is especially important as the zone_t may 4321 * languish on the deathrow for a very long time waiting for 4322 * cred's to drain out. 4323 * 4324 * This rebinding of the zone can happen multiple times 4325 * (presumably due to interrupted or parallel systemcalls) 4326 * without any adverse effects. 4327 */ 4328 if (pool_lock_intr() != 0) { 4329 zone_rele(zone); 4330 return (set_errno(EINTR)); 4331 } 4332 if (pool_state == POOL_ENABLED) { 4333 mutex_enter(&cpu_lock); 4334 zone_pool_set(zone, pool_default); 4335 /* 4336 * The zone no longer needs to be able to see any cpus. 4337 */ 4338 zone_pset_set(zone, ZONE_PS_INVAL); 4339 mutex_exit(&cpu_lock); 4340 } 4341 pool_unlock(); 4342 4343 /* 4344 * ZSD shutdown callbacks can be executed multiple times, hence 4345 * it is safe to not be holding any locks across this call. 4346 */ 4347 zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 4348 4349 mutex_enter(&zone_status_lock); 4350 if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 4351 zone_status_set(zone, ZONE_IS_DOWN); 4352 mutex_exit(&zone_status_lock); 4353 4354 /* 4355 * Wait for kernel threads to drain. 4356 */ 4357 if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 4358 zone_rele(zone); 4359 return (set_errno(EINTR)); 4360 } 4361 4362 /* 4363 * Zone can be become down/destroyable even if the above wait 4364 * returns EINTR, so any code added here may never execute. 4365 * (i.e. don't add code here) 4366 */ 4367 4368 zone_rele(zone); 4369 return (0); 4370 } 4371 4372 /* 4373 * Systemcall entry point to finalize the zone halt process. The caller 4374 * must have already successfully called zone_shutdown(). 4375 * 4376 * Upon successful completion, the zone will have been fully destroyed: 4377 * zsched will have exited, destructor callbacks executed, and the zone 4378 * removed from the list of active zones. 4379 */ 4380 static int 4381 zone_destroy(zoneid_t zoneid) 4382 { 4383 uint64_t uniqid; 4384 zone_t *zone; 4385 zone_status_t status; 4386 4387 if (secpolicy_zone_config(CRED()) != 0) 4388 return (set_errno(EPERM)); 4389 if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 4390 return (set_errno(EINVAL)); 4391 4392 mutex_enter(&zonehash_lock); 4393 /* 4394 * Look for zone under hash lock to prevent races with other 4395 * calls to zone_destroy. 4396 */ 4397 if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 4398 mutex_exit(&zonehash_lock); 4399 return (set_errno(EINVAL)); 4400 } 4401 4402 if (zone_mount_count(zone->zone_rootpath) != 0) { 4403 mutex_exit(&zonehash_lock); 4404 return (set_errno(EBUSY)); 4405 } 4406 mutex_enter(&zone_status_lock); 4407 status = zone_status_get(zone); 4408 if (status < ZONE_IS_DOWN) { 4409 mutex_exit(&zone_status_lock); 4410 mutex_exit(&zonehash_lock); 4411 return (set_errno(EBUSY)); 4412 } else if (status == ZONE_IS_DOWN) { 4413 zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 4414 } 4415 mutex_exit(&zone_status_lock); 4416 zone_hold(zone); 4417 mutex_exit(&zonehash_lock); 4418 4419 /* 4420 * wait for zsched to exit 4421 */ 4422 zone_status_wait(zone, ZONE_IS_DEAD); 4423 zone_zsd_callbacks(zone, ZSD_DESTROY); 4424 zone->zone_netstack = NULL; 4425 uniqid = zone->zone_uniqid; 4426 zone_rele(zone); 4427 zone = NULL; /* potentially free'd */ 4428 4429 mutex_enter(&zonehash_lock); 4430 for (; /* ever */; ) { 4431 boolean_t unref; 4432 4433 if ((zone = zone_find_all_by_id(zoneid)) == NULL || 4434 zone->zone_uniqid != uniqid) { 4435 /* 4436 * The zone has gone away. Necessary conditions 4437 * are met, so we return success. 4438 */ 4439 mutex_exit(&zonehash_lock); 4440 return (0); 4441 } 4442 mutex_enter(&zone->zone_lock); 4443 unref = ZONE_IS_UNREF(zone); 4444 mutex_exit(&zone->zone_lock); 4445 if (unref) { 4446 /* 4447 * There is only one reference to the zone -- that 4448 * added when the zone was added to the hashtables -- 4449 * and things will remain this way until we drop 4450 * zonehash_lock... we can go ahead and cleanup the 4451 * zone. 4452 */ 4453 break; 4454 } 4455 4456 if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 4457 /* Signaled */ 4458 mutex_exit(&zonehash_lock); 4459 return (set_errno(EINTR)); 4460 } 4461 4462 } 4463 4464 /* 4465 * Remove CPU cap for this zone now since we're not going to 4466 * fail below this point. 4467 */ 4468 cpucaps_zone_remove(zone); 4469 4470 /* Get rid of the zone's kstats */ 4471 zone_kstat_delete(zone); 4472 4473 /* free brand specific data */ 4474 if (ZONE_IS_BRANDED(zone)) 4475 ZBROP(zone)->b_free_brand_data(zone); 4476 4477 /* Say goodbye to brand framework. */ 4478 brand_unregister_zone(zone->zone_brand); 4479 4480 /* 4481 * It is now safe to let the zone be recreated; remove it from the 4482 * lists. The memory will not be freed until the last cred 4483 * reference goes away. 4484 */ 4485 ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 4486 zonecount--; 4487 /* remove from active list and hash tables */ 4488 list_remove(&zone_active, zone); 4489 (void) mod_hash_destroy(zonehashbyname, 4490 (mod_hash_key_t)zone->zone_name); 4491 (void) mod_hash_destroy(zonehashbyid, 4492 (mod_hash_key_t)(uintptr_t)zone->zone_id); 4493 if (zone->zone_flags & ZF_HASHED_LABEL) 4494 (void) mod_hash_destroy(zonehashbylabel, 4495 (mod_hash_key_t)zone->zone_slabel); 4496 mutex_exit(&zonehash_lock); 4497 4498 /* 4499 * Release the root vnode; we're not using it anymore. Nor should any 4500 * other thread that might access it exist. 4501 */ 4502 if (zone->zone_rootvp != NULL) { 4503 VN_RELE(zone->zone_rootvp); 4504 zone->zone_rootvp = NULL; 4505 } 4506 4507 /* add to deathrow list */ 4508 mutex_enter(&zone_deathrow_lock); 4509 list_insert_tail(&zone_deathrow, zone); 4510 mutex_exit(&zone_deathrow_lock); 4511 4512 /* 4513 * Drop last reference (which was added by zsched()), this will 4514 * free the zone unless there are outstanding cred references. 4515 */ 4516 zone_rele(zone); 4517 return (0); 4518 } 4519 4520 /* 4521 * Systemcall entry point for zone_getattr(2). 4522 */ 4523 static ssize_t 4524 zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 4525 { 4526 size_t size; 4527 int error = 0, err; 4528 zone_t *zone; 4529 char *zonepath; 4530 char *outstr; 4531 zone_status_t zone_status; 4532 pid_t initpid; 4533 boolean_t global = (curzone == global_zone); 4534 boolean_t inzone = (curzone->zone_id == zoneid); 4535 ushort_t flags; 4536 4537 mutex_enter(&zonehash_lock); 4538 if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 4539 mutex_exit(&zonehash_lock); 4540 return (set_errno(EINVAL)); 4541 } 4542 zone_status = zone_status_get(zone); 4543 if (zone_status < ZONE_IS_INITIALIZED) { 4544 mutex_exit(&zonehash_lock); 4545 return (set_errno(EINVAL)); 4546 } 4547 zone_hold(zone); 4548 mutex_exit(&zonehash_lock); 4549 4550 /* 4551 * If not in the global zone, don't show information about other zones, 4552 * unless the system is labeled and the local zone's label dominates 4553 * the other zone. 4554 */ 4555 if (!zone_list_access(zone)) { 4556 zone_rele(zone); 4557 return (set_errno(EINVAL)); 4558 } 4559 4560 switch (attr) { 4561 case ZONE_ATTR_ROOT: 4562 if (global) { 4563 /* 4564 * Copy the path to trim the trailing "/" (except for 4565 * the global zone). 4566 */ 4567 if (zone != global_zone) 4568 size = zone->zone_rootpathlen - 1; 4569 else 4570 size = zone->zone_rootpathlen; 4571 zonepath = kmem_alloc(size, KM_SLEEP); 4572 bcopy(zone->zone_rootpath, zonepath, size); 4573 zonepath[size - 1] = '\0'; 4574 } else { 4575 if (inzone || !is_system_labeled()) { 4576 /* 4577 * Caller is not in the global zone. 4578 * if the query is on the current zone 4579 * or the system is not labeled, 4580 * just return faked-up path for current zone. 4581 */ 4582 zonepath = "/"; 4583 size = 2; 4584 } else { 4585 /* 4586 * Return related path for current zone. 4587 */ 4588 int prefix_len = strlen(zone_prefix); 4589 int zname_len = strlen(zone->zone_name); 4590 4591 size = prefix_len + zname_len + 1; 4592 zonepath = kmem_alloc(size, KM_SLEEP); 4593 bcopy(zone_prefix, zonepath, prefix_len); 4594 bcopy(zone->zone_name, zonepath + 4595 prefix_len, zname_len); 4596 zonepath[size - 1] = '\0'; 4597 } 4598 } 4599 if (bufsize > size) 4600 bufsize = size; 4601 if (buf != NULL) { 4602 err = copyoutstr(zonepath, buf, bufsize, NULL); 4603 if (err != 0 && err != ENAMETOOLONG) 4604 error = EFAULT; 4605 } 4606 if (global || (is_system_labeled() && !inzone)) 4607 kmem_free(zonepath, size); 4608 break; 4609 4610 case ZONE_ATTR_NAME: 4611 size = strlen(zone->zone_name) + 1; 4612 if (bufsize > size) 4613 bufsize = size; 4614 if (buf != NULL) { 4615 err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 4616 if (err != 0 && err != ENAMETOOLONG) 4617 error = EFAULT; 4618 } 4619 break; 4620 4621 case ZONE_ATTR_STATUS: 4622 /* 4623 * Since we're not holding zonehash_lock, the zone status 4624 * may be anything; leave it up to userland to sort it out. 4625 */ 4626 size = sizeof (zone_status); 4627 if (bufsize > size) 4628 bufsize = size; 4629 zone_status = zone_status_get(zone); 4630 if (buf != NULL && 4631 copyout(&zone_status, buf, bufsize) != 0) 4632 error = EFAULT; 4633 break; 4634 case ZONE_ATTR_FLAGS: 4635 size = sizeof (zone->zone_flags); 4636 if (bufsize > size) 4637 bufsize = size; 4638 flags = zone->zone_flags; 4639 if (buf != NULL && 4640 copyout(&flags, buf, bufsize) != 0) 4641 error = EFAULT; 4642 break; 4643 case ZONE_ATTR_PRIVSET: 4644 size = sizeof (priv_set_t); 4645 if (bufsize > size) 4646 bufsize = size; 4647 if (buf != NULL && 4648 copyout(zone->zone_privset, buf, bufsize) != 0) 4649 error = EFAULT; 4650 break; 4651 case ZONE_ATTR_UNIQID: 4652 size = sizeof (zone->zone_uniqid); 4653 if (bufsize > size) 4654 bufsize = size; 4655 if (buf != NULL && 4656 copyout(&zone->zone_uniqid, buf, bufsize) != 0) 4657 error = EFAULT; 4658 break; 4659 case ZONE_ATTR_POOLID: 4660 { 4661 pool_t *pool; 4662 poolid_t poolid; 4663 4664 if (pool_lock_intr() != 0) { 4665 error = EINTR; 4666 break; 4667 } 4668 pool = zone_pool_get(zone); 4669 poolid = pool->pool_id; 4670 pool_unlock(); 4671 size = sizeof (poolid); 4672 if (bufsize > size) 4673 bufsize = size; 4674 if (buf != NULL && copyout(&poolid, buf, size) != 0) 4675 error = EFAULT; 4676 } 4677 break; 4678 case ZONE_ATTR_SLBL: 4679 size = sizeof (bslabel_t); 4680 if (bufsize > size) 4681 bufsize = size; 4682 if (zone->zone_slabel == NULL) 4683 error = EINVAL; 4684 else if (buf != NULL && 4685 copyout(label2bslabel(zone->zone_slabel), buf, 4686 bufsize) != 0) 4687 error = EFAULT; 4688 break; 4689 case ZONE_ATTR_INITPID: 4690 size = sizeof (initpid); 4691 if (bufsize > size) 4692 bufsize = size; 4693 initpid = zone->zone_proc_initpid; 4694 if (initpid == -1) { 4695 error = ESRCH; 4696 break; 4697 } 4698 if (buf != NULL && 4699 copyout(&initpid, buf, bufsize) != 0) 4700 error = EFAULT; 4701 break; 4702 case ZONE_ATTR_BRAND: 4703 size = strlen(zone->zone_brand->b_name) + 1; 4704 4705 if (bufsize > size) 4706 bufsize = size; 4707 if (buf != NULL) { 4708 err = copyoutstr(zone->zone_brand->b_name, buf, 4709 bufsize, NULL); 4710 if (err != 0 && err != ENAMETOOLONG) 4711 error = EFAULT; 4712 } 4713 break; 4714 case ZONE_ATTR_INITNAME: 4715 size = strlen(zone->zone_initname) + 1; 4716 if (bufsize > size) 4717 bufsize = size; 4718 if (buf != NULL) { 4719 err = copyoutstr(zone->zone_initname, buf, bufsize, 4720 NULL); 4721 if (err != 0 && err != ENAMETOOLONG) 4722 error = EFAULT; 4723 } 4724 break; 4725 case ZONE_ATTR_BOOTARGS: 4726 if (zone->zone_bootargs == NULL) 4727 outstr = ""; 4728 else 4729 outstr = zone->zone_bootargs; 4730 size = strlen(outstr) + 1; 4731 if (bufsize > size) 4732 bufsize = size; 4733 if (buf != NULL) { 4734 err = copyoutstr(outstr, buf, bufsize, NULL); 4735 if (err != 0 && err != ENAMETOOLONG) 4736 error = EFAULT; 4737 } 4738 break; 4739 case ZONE_ATTR_PHYS_MCAP: 4740 size = sizeof (zone->zone_phys_mcap); 4741 if (bufsize > size) 4742 bufsize = size; 4743 if (buf != NULL && 4744 copyout(&zone->zone_phys_mcap, buf, bufsize) != 0) 4745 error = EFAULT; 4746 break; 4747 case ZONE_ATTR_SCHED_CLASS: 4748 mutex_enter(&class_lock); 4749 4750 if (zone->zone_defaultcid >= loaded_classes) 4751 outstr = ""; 4752 else 4753 outstr = sclass[zone->zone_defaultcid].cl_name; 4754 size = strlen(outstr) + 1; 4755 if (bufsize > size) 4756 bufsize = size; 4757 if (buf != NULL) { 4758 err = copyoutstr(outstr, buf, bufsize, NULL); 4759 if (err != 0 && err != ENAMETOOLONG) 4760 error = EFAULT; 4761 } 4762 4763 mutex_exit(&class_lock); 4764 break; 4765 case ZONE_ATTR_HOSTID: 4766 if (zone->zone_hostid != HW_INVALID_HOSTID && 4767 bufsize == sizeof (zone->zone_hostid)) { 4768 size = sizeof (zone->zone_hostid); 4769 if (buf != NULL && copyout(&zone->zone_hostid, buf, 4770 bufsize) != 0) 4771 error = EFAULT; 4772 } else { 4773 error = EINVAL; 4774 } 4775 break; 4776 default: 4777 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) { 4778 size = bufsize; 4779 error = ZBROP(zone)->b_getattr(zone, attr, buf, &size); 4780 } else { 4781 error = EINVAL; 4782 } 4783 } 4784 zone_rele(zone); 4785 4786 if (error) 4787 return (set_errno(error)); 4788 return ((ssize_t)size); 4789 } 4790 4791 /* 4792 * Systemcall entry point for zone_setattr(2). 4793 */ 4794 /*ARGSUSED*/ 4795 static int 4796 zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 4797 { 4798 zone_t *zone; 4799 zone_status_t zone_status; 4800 int err; 4801 4802 if (secpolicy_zone_config(CRED()) != 0) 4803 return (set_errno(EPERM)); 4804 4805 /* 4806 * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the 4807 * global zone. 4808 */ 4809 if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) { 4810 return (set_errno(EINVAL)); 4811 } 4812 4813 mutex_enter(&zonehash_lock); 4814 if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 4815 mutex_exit(&zonehash_lock); 4816 return (set_errno(EINVAL)); 4817 } 4818 zone_hold(zone); 4819 mutex_exit(&zonehash_lock); 4820 4821 /* 4822 * At present most attributes can only be set on non-running, 4823 * non-global zones. 4824 */ 4825 zone_status = zone_status_get(zone); 4826 if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY) 4827 goto done; 4828 4829 switch (attr) { 4830 case ZONE_ATTR_INITNAME: 4831 err = zone_set_initname(zone, (const char *)buf); 4832 break; 4833 case ZONE_ATTR_BOOTARGS: 4834 err = zone_set_bootargs(zone, (const char *)buf); 4835 break; 4836 case ZONE_ATTR_BRAND: 4837 err = zone_set_brand(zone, (const char *)buf); 4838 break; 4839 case ZONE_ATTR_PHYS_MCAP: 4840 err = zone_set_phys_mcap(zone, (const uint64_t *)buf); 4841 break; 4842 case ZONE_ATTR_SCHED_CLASS: 4843 err = zone_set_sched_class(zone, (const char *)buf); 4844 break; 4845 case ZONE_ATTR_HOSTID: 4846 if (bufsize == sizeof (zone->zone_hostid)) { 4847 if (copyin(buf, &zone->zone_hostid, bufsize) == 0) 4848 err = 0; 4849 else 4850 err = EFAULT; 4851 } else { 4852 err = EINVAL; 4853 } 4854 break; 4855 default: 4856 if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) 4857 err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize); 4858 else 4859 err = EINVAL; 4860 } 4861 4862 done: 4863 zone_rele(zone); 4864 return (err != 0 ? set_errno(err) : 0); 4865 } 4866 4867 /* 4868 * Return zero if the process has at least one vnode mapped in to its 4869 * address space which shouldn't be allowed to change zones. 4870 * 4871 * Also return zero if the process has any shared mappings which reserve 4872 * swap. This is because the counting for zone.max-swap does not allow swap 4873 * reservation to be shared between zones. zone swap reservation is counted 4874 * on zone->zone_max_swap. 4875 */ 4876 static int 4877 as_can_change_zones(void) 4878 { 4879 proc_t *pp = curproc; 4880 struct seg *seg; 4881 struct as *as = pp->p_as; 4882 vnode_t *vp; 4883 int allow = 1; 4884 4885 ASSERT(pp->p_as != &kas); 4886 AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 4887 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 4888 4889 /* 4890 * Cannot enter zone with shared anon memory which 4891 * reserves swap. See comment above. 4892 */ 4893 if (seg_can_change_zones(seg) == B_FALSE) { 4894 allow = 0; 4895 break; 4896 } 4897 /* 4898 * if we can't get a backing vnode for this segment then skip 4899 * it. 4900 */ 4901 vp = NULL; 4902 if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 4903 continue; 4904 if (!vn_can_change_zones(vp)) { /* bail on first match */ 4905 allow = 0; 4906 break; 4907 } 4908 } 4909 AS_LOCK_EXIT(as, &as->a_lock); 4910 return (allow); 4911 } 4912 4913 /* 4914 * Count swap reserved by curproc's address space 4915 */ 4916 static size_t 4917 as_swresv(void) 4918 { 4919 proc_t *pp = curproc; 4920 struct seg *seg; 4921 struct as *as = pp->p_as; 4922 size_t swap = 0; 4923 4924 ASSERT(pp->p_as != &kas); 4925 ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 4926 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) 4927 swap += seg_swresv(seg); 4928 4929 return (swap); 4930 } 4931 4932 /* 4933 * Systemcall entry point for zone_enter(). 4934 * 4935 * The current process is injected into said zone. In the process 4936 * it will change its project membership, privileges, rootdir/cwd, 4937 * zone-wide rctls, and pool association to match those of the zone. 4938 * 4939 * The first zone_enter() called while the zone is in the ZONE_IS_READY 4940 * state will transition it to ZONE_IS_RUNNING. Processes may only 4941 * enter a zone that is "ready" or "running". 4942 */ 4943 static int 4944 zone_enter(zoneid_t zoneid) 4945 { 4946 zone_t *zone; 4947 vnode_t *vp; 4948 proc_t *pp = curproc; 4949 contract_t *ct; 4950 cont_process_t *ctp; 4951 task_t *tk, *oldtk; 4952 kproject_t *zone_proj0; 4953 cred_t *cr, *newcr; 4954 pool_t *oldpool, *newpool; 4955 sess_t *sp; 4956 uid_t uid; 4957 zone_status_t status; 4958 int err = 0; 4959 rctl_entity_p_t e; 4960 size_t swap; 4961 kthread_id_t t; 4962 4963 if (secpolicy_zone_config(CRED()) != 0) 4964 return (set_errno(EPERM)); 4965 if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 4966 return (set_errno(EINVAL)); 4967 4968 /* 4969 * Stop all lwps so we don't need to hold a lock to look at 4970 * curproc->p_zone. This needs to happen before we grab any 4971 * locks to avoid deadlock (another lwp in the process could 4972 * be waiting for the held lock). 4973 */ 4974 if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 4975 return (set_errno(EINTR)); 4976 4977 /* 4978 * Make sure we're not changing zones with files open or mapped in 4979 * to our address space which shouldn't be changing zones. 4980 */ 4981 if (!files_can_change_zones()) { 4982 err = EBADF; 4983 goto out; 4984 } 4985 if (!as_can_change_zones()) { 4986 err = EFAULT; 4987 goto out; 4988 } 4989 4990 mutex_enter(&zonehash_lock); 4991 if (pp->p_zone != global_zone) { 4992 mutex_exit(&zonehash_lock); 4993 err = EINVAL; 4994 goto out; 4995 } 4996 4997 zone = zone_find_all_by_id(zoneid); 4998 if (zone == NULL) { 4999 mutex_exit(&zonehash_lock); 5000 err = EINVAL; 5001 goto out; 5002 } 5003 5004 /* 5005 * To prevent processes in a zone from holding contracts on 5006 * extrazonal resources, and to avoid process contract 5007 * memberships which span zones, contract holders and processes 5008 * which aren't the sole members of their encapsulating process 5009 * contracts are not allowed to zone_enter. 5010 */ 5011 ctp = pp->p_ct_process; 5012 ct = &ctp->conp_contract; 5013 mutex_enter(&ct->ct_lock); 5014 mutex_enter(&pp->p_lock); 5015 if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 5016 mutex_exit(&pp->p_lock); 5017 mutex_exit(&ct->ct_lock); 5018 mutex_exit(&zonehash_lock); 5019 err = EINVAL; 5020 goto out; 5021 } 5022 5023 /* 5024 * Moreover, we don't allow processes whose encapsulating 5025 * process contracts have inherited extrazonal contracts. 5026 * While it would be easier to eliminate all process contracts 5027 * with inherited contracts, we need to be able to give a 5028 * restarted init (or other zone-penetrating process) its 5029 * predecessor's contracts. 5030 */ 5031 if (ctp->conp_ninherited != 0) { 5032 contract_t *next; 5033 for (next = list_head(&ctp->conp_inherited); next; 5034 next = list_next(&ctp->conp_inherited, next)) { 5035 if (contract_getzuniqid(next) != zone->zone_uniqid) { 5036 mutex_exit(&pp->p_lock); 5037 mutex_exit(&ct->ct_lock); 5038 mutex_exit(&zonehash_lock); 5039 err = EINVAL; 5040 goto out; 5041 } 5042 } 5043 } 5044 5045 mutex_exit(&pp->p_lock); 5046 mutex_exit(&ct->ct_lock); 5047 5048 status = zone_status_get(zone); 5049 if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 5050 /* 5051 * Can't join 5052 */ 5053 mutex_exit(&zonehash_lock); 5054 err = EINVAL; 5055 goto out; 5056 } 5057 5058 /* 5059 * Make sure new priv set is within the permitted set for caller 5060 */ 5061 if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 5062 mutex_exit(&zonehash_lock); 5063 err = EPERM; 5064 goto out; 5065 } 5066 /* 5067 * We want to momentarily drop zonehash_lock while we optimistically 5068 * bind curproc to the pool it should be running in. This is safe 5069 * since the zone can't disappear (we have a hold on it). 5070 */ 5071 zone_hold(zone); 5072 mutex_exit(&zonehash_lock); 5073 5074 /* 5075 * Grab pool_lock to keep the pools configuration from changing 5076 * and to stop ourselves from getting rebound to another pool 5077 * until we join the zone. 5078 */ 5079 if (pool_lock_intr() != 0) { 5080 zone_rele(zone); 5081 err = EINTR; 5082 goto out; 5083 } 5084 ASSERT(secpolicy_pool(CRED()) == 0); 5085 /* 5086 * Bind ourselves to the pool currently associated with the zone. 5087 */ 5088 oldpool = curproc->p_pool; 5089 newpool = zone_pool_get(zone); 5090 if (pool_state == POOL_ENABLED && newpool != oldpool && 5091 (err = pool_do_bind(newpool, P_PID, P_MYID, 5092 POOL_BIND_ALL)) != 0) { 5093 pool_unlock(); 5094 zone_rele(zone); 5095 goto out; 5096 } 5097 5098 /* 5099 * Grab cpu_lock now; we'll need it later when we call 5100 * task_join(). 5101 */ 5102 mutex_enter(&cpu_lock); 5103 mutex_enter(&zonehash_lock); 5104 /* 5105 * Make sure the zone hasn't moved on since we dropped zonehash_lock. 5106 */ 5107 if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 5108 /* 5109 * Can't join anymore. 5110 */ 5111 mutex_exit(&zonehash_lock); 5112 mutex_exit(&cpu_lock); 5113 if (pool_state == POOL_ENABLED && 5114 newpool != oldpool) 5115 (void) pool_do_bind(oldpool, P_PID, P_MYID, 5116 POOL_BIND_ALL); 5117 pool_unlock(); 5118 zone_rele(zone); 5119 err = EINVAL; 5120 goto out; 5121 } 5122 5123 /* 5124 * a_lock must be held while transfering locked memory and swap 5125 * reservation from the global zone to the non global zone because 5126 * asynchronous faults on the processes' address space can lock 5127 * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE 5128 * segments respectively. 5129 */ 5130 AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER); 5131 swap = as_swresv(); 5132 mutex_enter(&pp->p_lock); 5133 zone_proj0 = zone->zone_zsched->p_task->tk_proj; 5134 /* verify that we do not exceed and task or lwp limits */ 5135 mutex_enter(&zone->zone_nlwps_lock); 5136 /* add new lwps to zone and zone's proj0 */ 5137 zone_proj0->kpj_nlwps += pp->p_lwpcnt; 5138 zone->zone_nlwps += pp->p_lwpcnt; 5139 /* add 1 task to zone's proj0 */ 5140 zone_proj0->kpj_ntasks += 1; 5141 mutex_exit(&zone->zone_nlwps_lock); 5142 5143 mutex_enter(&zone->zone_mem_lock); 5144 zone->zone_locked_mem += pp->p_locked_mem; 5145 zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem; 5146 zone->zone_max_swap += swap; 5147 mutex_exit(&zone->zone_mem_lock); 5148 5149 mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock)); 5150 zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem; 5151 mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock)); 5152 5153 /* remove lwps from proc's old zone and old project */ 5154 mutex_enter(&pp->p_zone->zone_nlwps_lock); 5155 pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 5156 pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 5157 mutex_exit(&pp->p_zone->zone_nlwps_lock); 5158 5159 mutex_enter(&pp->p_zone->zone_mem_lock); 5160 pp->p_zone->zone_locked_mem -= pp->p_locked_mem; 5161 pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem; 5162 pp->p_zone->zone_max_swap -= swap; 5163 mutex_exit(&pp->p_zone->zone_mem_lock); 5164 5165 mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 5166 pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem; 5167 mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock)); 5168 5169 pp->p_flag |= SZONETOP; 5170 pp->p_zone = zone; 5171 mutex_exit(&pp->p_lock); 5172 AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock); 5173 5174 /* 5175 * Joining the zone cannot fail from now on. 5176 * 5177 * This means that a lot of the following code can be commonized and 5178 * shared with zsched(). 5179 */ 5180 5181 /* 5182 * If the process contract fmri was inherited, we need to 5183 * flag this so that any contract status will not leak 5184 * extra zone information, svc_fmri in this case 5185 */ 5186 if (ctp->conp_svc_ctid != ct->ct_id) { 5187 mutex_enter(&ct->ct_lock); 5188 ctp->conp_svc_zone_enter = ct->ct_id; 5189 mutex_exit(&ct->ct_lock); 5190 } 5191 5192 /* 5193 * Reset the encapsulating process contract's zone. 5194 */ 5195 ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 5196 contract_setzuniqid(ct, zone->zone_uniqid); 5197 5198 /* 5199 * Create a new task and associate the process with the project keyed 5200 * by (projid,zoneid). 5201 * 5202 * We might as well be in project 0; the global zone's projid doesn't 5203 * make much sense in a zone anyhow. 5204 * 5205 * This also increments zone_ntasks, and returns with p_lock held. 5206 */ 5207 tk = task_create(0, zone); 5208 oldtk = task_join(tk, 0); 5209 mutex_exit(&cpu_lock); 5210 5211 /* 5212 * call RCTLOP_SET functions on this proc 5213 */ 5214 e.rcep_p.zone = zone; 5215 e.rcep_t = RCENTITY_ZONE; 5216 (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 5217 RCD_CALLBACK); 5218 mutex_exit(&pp->p_lock); 5219 5220 /* 5221 * We don't need to hold any of zsched's locks here; not only do we know 5222 * the process and zone aren't going away, we know its session isn't 5223 * changing either. 5224 * 5225 * By joining zsched's session here, we mimic the behavior in the 5226 * global zone of init's sid being the pid of sched. We extend this 5227 * to all zlogin-like zone_enter()'ing processes as well. 5228 */ 5229 mutex_enter(&pidlock); 5230 sp = zone->zone_zsched->p_sessp; 5231 sess_hold(zone->zone_zsched); 5232 mutex_enter(&pp->p_lock); 5233 pgexit(pp); 5234 sess_rele(pp->p_sessp, B_TRUE); 5235 pp->p_sessp = sp; 5236 pgjoin(pp, zone->zone_zsched->p_pidp); 5237 5238 /* 5239 * If any threads are scheduled to be placed on zone wait queue they 5240 * should abandon the idea since the wait queue is changing. 5241 * We need to be holding pidlock & p_lock to do this. 5242 */ 5243 if ((t = pp->p_tlist) != NULL) { 5244 do { 5245 thread_lock(t); 5246 /* 5247 * Kick this thread so that he doesn't sit 5248 * on a wrong wait queue. 5249 */ 5250 if (ISWAITING(t)) 5251 setrun_locked(t); 5252 5253 if (t->t_schedflag & TS_ANYWAITQ) 5254 t->t_schedflag &= ~ TS_ANYWAITQ; 5255 5256 thread_unlock(t); 5257 } while ((t = t->t_forw) != pp->p_tlist); 5258 } 5259 5260 /* 5261 * If there is a default scheduling class for the zone and it is not 5262 * the class we are currently in, change all of the threads in the 5263 * process to the new class. We need to be holding pidlock & p_lock 5264 * when we call parmsset so this is a good place to do it. 5265 */ 5266 if (zone->zone_defaultcid > 0 && 5267 zone->zone_defaultcid != curthread->t_cid) { 5268 pcparms_t pcparms; 5269 5270 pcparms.pc_cid = zone->zone_defaultcid; 5271 pcparms.pc_clparms[0] = 0; 5272 5273 /* 5274 * If setting the class fails, we still want to enter the zone. 5275 */ 5276 if ((t = pp->p_tlist) != NULL) { 5277 do { 5278 (void) parmsset(&pcparms, t); 5279 } while ((t = t->t_forw) != pp->p_tlist); 5280 } 5281 } 5282 5283 mutex_exit(&pp->p_lock); 5284 mutex_exit(&pidlock); 5285 5286 mutex_exit(&zonehash_lock); 5287 /* 5288 * We're firmly in the zone; let pools progress. 5289 */ 5290 pool_unlock(); 5291 task_rele(oldtk); 5292 /* 5293 * We don't need to retain a hold on the zone since we already 5294 * incremented zone_ntasks, so the zone isn't going anywhere. 5295 */ 5296 zone_rele(zone); 5297 5298 /* 5299 * Chroot 5300 */ 5301 vp = zone->zone_rootvp; 5302 zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 5303 zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 5304 5305 /* 5306 * Change process credentials 5307 */ 5308 newcr = cralloc(); 5309 mutex_enter(&pp->p_crlock); 5310 cr = pp->p_cred; 5311 crcopy_to(cr, newcr); 5312 crsetzone(newcr, zone); 5313 pp->p_cred = newcr; 5314 5315 /* 5316 * Restrict all process privilege sets to zone limit 5317 */ 5318 priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 5319 priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 5320 priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 5321 priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 5322 mutex_exit(&pp->p_crlock); 5323 crset(pp, newcr); 5324 5325 /* 5326 * Adjust upcount to reflect zone entry. 5327 */ 5328 uid = crgetruid(newcr); 5329 mutex_enter(&pidlock); 5330 upcount_dec(uid, GLOBAL_ZONEID); 5331 upcount_inc(uid, zoneid); 5332 mutex_exit(&pidlock); 5333 5334 /* 5335 * Set up core file path and content. 5336 */ 5337 set_core_defaults(); 5338 5339 out: 5340 /* 5341 * Let the other lwps continue. 5342 */ 5343 mutex_enter(&pp->p_lock); 5344 if (curthread != pp->p_agenttp) 5345 continuelwps(pp); 5346 mutex_exit(&pp->p_lock); 5347 5348 return (err != 0 ? set_errno(err) : 0); 5349 } 5350 5351 /* 5352 * Systemcall entry point for zone_list(2). 5353 * 5354 * Processes running in a (non-global) zone only see themselves. 5355 * On labeled systems, they see all zones whose label they dominate. 5356 */ 5357 static int 5358 zone_list(zoneid_t *zoneidlist, uint_t *numzones) 5359 { 5360 zoneid_t *zoneids; 5361 zone_t *zone, *myzone; 5362 uint_t user_nzones, real_nzones; 5363 uint_t domi_nzones; 5364 int error; 5365 5366 if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 5367 return (set_errno(EFAULT)); 5368 5369 myzone = curproc->p_zone; 5370 if (myzone != global_zone) { 5371 bslabel_t *mybslab; 5372 5373 if (!is_system_labeled()) { 5374 /* just return current zone */ 5375 real_nzones = domi_nzones = 1; 5376 zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 5377 zoneids[0] = myzone->zone_id; 5378 } else { 5379 /* return all zones that are dominated */ 5380 mutex_enter(&zonehash_lock); 5381 real_nzones = zonecount; 5382 domi_nzones = 0; 5383 if (real_nzones > 0) { 5384 zoneids = kmem_alloc(real_nzones * 5385 sizeof (zoneid_t), KM_SLEEP); 5386 mybslab = label2bslabel(myzone->zone_slabel); 5387 for (zone = list_head(&zone_active); 5388 zone != NULL; 5389 zone = list_next(&zone_active, zone)) { 5390 if (zone->zone_id == GLOBAL_ZONEID) 5391 continue; 5392 if (zone != myzone && 5393 (zone->zone_flags & ZF_IS_SCRATCH)) 5394 continue; 5395 /* 5396 * Note that a label always dominates 5397 * itself, so myzone is always included 5398 * in the list. 5399 */ 5400 if (bldominates(mybslab, 5401 label2bslabel(zone->zone_slabel))) { 5402 zoneids[domi_nzones++] = 5403 zone->zone_id; 5404 } 5405 } 5406 } 5407 mutex_exit(&zonehash_lock); 5408 } 5409 } else { 5410 mutex_enter(&zonehash_lock); 5411 real_nzones = zonecount; 5412 domi_nzones = 0; 5413 if (real_nzones > 0) { 5414 zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 5415 KM_SLEEP); 5416 for (zone = list_head(&zone_active); zone != NULL; 5417 zone = list_next(&zone_active, zone)) 5418 zoneids[domi_nzones++] = zone->zone_id; 5419 ASSERT(domi_nzones == real_nzones); 5420 } 5421 mutex_exit(&zonehash_lock); 5422 } 5423 5424 /* 5425 * If user has allocated space for fewer entries than we found, then 5426 * return only up to his limit. Either way, tell him exactly how many 5427 * we found. 5428 */ 5429 if (domi_nzones < user_nzones) 5430 user_nzones = domi_nzones; 5431 error = 0; 5432 if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) { 5433 error = EFAULT; 5434 } else if (zoneidlist != NULL && user_nzones != 0) { 5435 if (copyout(zoneids, zoneidlist, 5436 user_nzones * sizeof (zoneid_t)) != 0) 5437 error = EFAULT; 5438 } 5439 5440 if (real_nzones > 0) 5441 kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 5442 5443 if (error != 0) 5444 return (set_errno(error)); 5445 else 5446 return (0); 5447 } 5448 5449 /* 5450 * Systemcall entry point for zone_lookup(2). 5451 * 5452 * Non-global zones are only able to see themselves and (on labeled systems) 5453 * the zones they dominate. 5454 */ 5455 static zoneid_t 5456 zone_lookup(const char *zone_name) 5457 { 5458 char *kname; 5459 zone_t *zone; 5460 zoneid_t zoneid; 5461 int err; 5462 5463 if (zone_name == NULL) { 5464 /* return caller's zone id */ 5465 return (getzoneid()); 5466 } 5467 5468 kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 5469 if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 5470 kmem_free(kname, ZONENAME_MAX); 5471 return (set_errno(err)); 5472 } 5473 5474 mutex_enter(&zonehash_lock); 5475 zone = zone_find_all_by_name(kname); 5476 kmem_free(kname, ZONENAME_MAX); 5477 /* 5478 * In a non-global zone, can only lookup global and own name. 5479 * In Trusted Extensions zone label dominance rules apply. 5480 */ 5481 if (zone == NULL || 5482 zone_status_get(zone) < ZONE_IS_READY || 5483 !zone_list_access(zone)) { 5484 mutex_exit(&zonehash_lock); 5485 return (set_errno(EINVAL)); 5486 } else { 5487 zoneid = zone->zone_id; 5488 mutex_exit(&zonehash_lock); 5489 return (zoneid); 5490 } 5491 } 5492 5493 static int 5494 zone_version(int *version_arg) 5495 { 5496 int version = ZONE_SYSCALL_API_VERSION; 5497 5498 if (copyout(&version, version_arg, sizeof (int)) != 0) 5499 return (set_errno(EFAULT)); 5500 return (0); 5501 } 5502 5503 /* ARGSUSED */ 5504 long 5505 zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4) 5506 { 5507 zone_def zs; 5508 int err; 5509 5510 switch (cmd) { 5511 case ZONE_CREATE: 5512 if (get_udatamodel() == DATAMODEL_NATIVE) { 5513 if (copyin(arg1, &zs, sizeof (zone_def))) { 5514 return (set_errno(EFAULT)); 5515 } 5516 } else { 5517 #ifdef _SYSCALL32_IMPL 5518 zone_def32 zs32; 5519 5520 if (copyin(arg1, &zs32, sizeof (zone_def32))) { 5521 return (set_errno(EFAULT)); 5522 } 5523 zs.zone_name = 5524 (const char *)(unsigned long)zs32.zone_name; 5525 zs.zone_root = 5526 (const char *)(unsigned long)zs32.zone_root; 5527 zs.zone_privs = 5528 (const struct priv_set *) 5529 (unsigned long)zs32.zone_privs; 5530 zs.zone_privssz = zs32.zone_privssz; 5531 zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 5532 zs.rctlbufsz = zs32.rctlbufsz; 5533 zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf; 5534 zs.zfsbufsz = zs32.zfsbufsz; 5535 zs.extended_error = 5536 (int *)(unsigned long)zs32.extended_error; 5537 zs.match = zs32.match; 5538 zs.doi = zs32.doi; 5539 zs.label = (const bslabel_t *)(uintptr_t)zs32.label; 5540 zs.flags = zs32.flags; 5541 #else 5542 panic("get_udatamodel() returned bogus result\n"); 5543 #endif 5544 } 5545 5546 return (zone_create(zs.zone_name, zs.zone_root, 5547 zs.zone_privs, zs.zone_privssz, 5548 (caddr_t)zs.rctlbuf, zs.rctlbufsz, 5549 (caddr_t)zs.zfsbuf, zs.zfsbufsz, 5550 zs.extended_error, zs.match, zs.doi, 5551 zs.label, zs.flags)); 5552 case ZONE_BOOT: 5553 return (zone_boot((zoneid_t)(uintptr_t)arg1)); 5554 case ZONE_DESTROY: 5555 return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 5556 case ZONE_GETATTR: 5557 return (zone_getattr((zoneid_t)(uintptr_t)arg1, 5558 (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 5559 case ZONE_SETATTR: 5560 return (zone_setattr((zoneid_t)(uintptr_t)arg1, 5561 (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 5562 case ZONE_ENTER: 5563 return (zone_enter((zoneid_t)(uintptr_t)arg1)); 5564 case ZONE_LIST: 5565 return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 5566 case ZONE_SHUTDOWN: 5567 return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 5568 case ZONE_LOOKUP: 5569 return (zone_lookup((const char *)arg1)); 5570 case ZONE_VERSION: 5571 return (zone_version((int *)arg1)); 5572 case ZONE_ADD_DATALINK: 5573 return (zone_add_datalink((zoneid_t)(uintptr_t)arg1, 5574 (datalink_id_t)(uintptr_t)arg2)); 5575 case ZONE_DEL_DATALINK: 5576 return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1, 5577 (datalink_id_t)(uintptr_t)arg2)); 5578 case ZONE_CHECK_DATALINK: { 5579 zoneid_t zoneid; 5580 boolean_t need_copyout; 5581 5582 if (copyin(arg1, &zoneid, sizeof (zoneid)) != 0) 5583 return (EFAULT); 5584 need_copyout = (zoneid == ALL_ZONES); 5585 err = zone_check_datalink(&zoneid, 5586 (datalink_id_t)(uintptr_t)arg2); 5587 if (err == 0 && need_copyout) { 5588 if (copyout(&zoneid, arg1, sizeof (zoneid)) != 0) 5589 err = EFAULT; 5590 } 5591 return (err == 0 ? 0 : set_errno(err)); 5592 } 5593 case ZONE_LIST_DATALINK: 5594 return (zone_list_datalink((zoneid_t)(uintptr_t)arg1, 5595 (int *)arg2, (datalink_id_t *)(uintptr_t)arg3)); 5596 default: 5597 return (set_errno(EINVAL)); 5598 } 5599 } 5600 5601 struct zarg { 5602 zone_t *zone; 5603 zone_cmd_arg_t arg; 5604 }; 5605 5606 static int 5607 zone_lookup_door(const char *zone_name, door_handle_t *doorp) 5608 { 5609 char *buf; 5610 size_t buflen; 5611 int error; 5612 5613 buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 5614 buf = kmem_alloc(buflen, KM_SLEEP); 5615 (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 5616 error = door_ki_open(buf, doorp); 5617 kmem_free(buf, buflen); 5618 return (error); 5619 } 5620 5621 static void 5622 zone_release_door(door_handle_t *doorp) 5623 { 5624 door_ki_rele(*doorp); 5625 *doorp = NULL; 5626 } 5627 5628 static void 5629 zone_ki_call_zoneadmd(struct zarg *zargp) 5630 { 5631 door_handle_t door = NULL; 5632 door_arg_t darg, save_arg; 5633 char *zone_name; 5634 size_t zone_namelen; 5635 zoneid_t zoneid; 5636 zone_t *zone; 5637 zone_cmd_arg_t arg; 5638 uint64_t uniqid; 5639 size_t size; 5640 int error; 5641 int retry; 5642 5643 zone = zargp->zone; 5644 arg = zargp->arg; 5645 kmem_free(zargp, sizeof (*zargp)); 5646 5647 zone_namelen = strlen(zone->zone_name) + 1; 5648 zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 5649 bcopy(zone->zone_name, zone_name, zone_namelen); 5650 zoneid = zone->zone_id; 5651 uniqid = zone->zone_uniqid; 5652 /* 5653 * zoneadmd may be down, but at least we can empty out the zone. 5654 * We can ignore the return value of zone_empty() since we're called 5655 * from a kernel thread and know we won't be delivered any signals. 5656 */ 5657 ASSERT(curproc == &p0); 5658 (void) zone_empty(zone); 5659 ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 5660 zone_rele(zone); 5661 5662 size = sizeof (arg); 5663 darg.rbuf = (char *)&arg; 5664 darg.data_ptr = (char *)&arg; 5665 darg.rsize = size; 5666 darg.data_size = size; 5667 darg.desc_ptr = NULL; 5668 darg.desc_num = 0; 5669 5670 save_arg = darg; 5671 /* 5672 * Since we're not holding a reference to the zone, any number of 5673 * things can go wrong, including the zone disappearing before we get a 5674 * chance to talk to zoneadmd. 5675 */ 5676 for (retry = 0; /* forever */; retry++) { 5677 if (door == NULL && 5678 (error = zone_lookup_door(zone_name, &door)) != 0) { 5679 goto next; 5680 } 5681 ASSERT(door != NULL); 5682 5683 if ((error = door_ki_upcall_limited(door, &darg, NULL, 5684 SIZE_MAX, 0)) == 0) { 5685 break; 5686 } 5687 switch (error) { 5688 case EINTR: 5689 /* FALLTHROUGH */ 5690 case EAGAIN: /* process may be forking */ 5691 /* 5692 * Back off for a bit 5693 */ 5694 break; 5695 case EBADF: 5696 zone_release_door(&door); 5697 if (zone_lookup_door(zone_name, &door) != 0) { 5698 /* 5699 * zoneadmd may be dead, but it may come back to 5700 * life later. 5701 */ 5702 break; 5703 } 5704 break; 5705 default: 5706 cmn_err(CE_WARN, 5707 "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 5708 error); 5709 goto out; 5710 } 5711 next: 5712 /* 5713 * If this isn't the same zone_t that we originally had in mind, 5714 * then this is the same as if two kadmin requests come in at 5715 * the same time: the first one wins. This means we lose, so we 5716 * bail. 5717 */ 5718 if ((zone = zone_find_by_id(zoneid)) == NULL) { 5719 /* 5720 * Problem is solved. 5721 */ 5722 break; 5723 } 5724 if (zone->zone_uniqid != uniqid) { 5725 /* 5726 * zoneid recycled 5727 */ 5728 zone_rele(zone); 5729 break; 5730 } 5731 /* 5732 * We could zone_status_timedwait(), but there doesn't seem to 5733 * be much point in doing that (plus, it would mean that 5734 * zone_free() isn't called until this thread exits). 5735 */ 5736 zone_rele(zone); 5737 delay(hz); 5738 darg = save_arg; 5739 } 5740 out: 5741 if (door != NULL) { 5742 zone_release_door(&door); 5743 } 5744 kmem_free(zone_name, zone_namelen); 5745 thread_exit(); 5746 } 5747 5748 /* 5749 * Entry point for uadmin() to tell the zone to go away or reboot. Analog to 5750 * kadmin(). The caller is a process in the zone. 5751 * 5752 * In order to shutdown the zone, we will hand off control to zoneadmd 5753 * (running in the global zone) via a door. We do a half-hearted job at 5754 * killing all processes in the zone, create a kernel thread to contact 5755 * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 5756 * a form of generation number used to let zoneadmd (as well as 5757 * zone_destroy()) know exactly which zone they're re talking about. 5758 */ 5759 int 5760 zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp) 5761 { 5762 struct zarg *zargp; 5763 zone_cmd_t zcmd; 5764 zone_t *zone; 5765 5766 zone = curproc->p_zone; 5767 ASSERT(getzoneid() != GLOBAL_ZONEID); 5768 5769 switch (cmd) { 5770 case A_SHUTDOWN: 5771 switch (fcn) { 5772 case AD_HALT: 5773 case AD_POWEROFF: 5774 zcmd = Z_HALT; 5775 break; 5776 case AD_BOOT: 5777 zcmd = Z_REBOOT; 5778 break; 5779 case AD_IBOOT: 5780 case AD_SBOOT: 5781 case AD_SIBOOT: 5782 case AD_NOSYNC: 5783 return (ENOTSUP); 5784 default: 5785 return (EINVAL); 5786 } 5787 break; 5788 case A_REBOOT: 5789 zcmd = Z_REBOOT; 5790 break; 5791 case A_FTRACE: 5792 case A_REMOUNT: 5793 case A_FREEZE: 5794 case A_DUMP: 5795 case A_CONFIG: 5796 return (ENOTSUP); 5797 default: 5798 ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 5799 return (EINVAL); 5800 } 5801 5802 if (secpolicy_zone_admin(credp, B_FALSE)) 5803 return (EPERM); 5804 mutex_enter(&zone_status_lock); 5805 5806 /* 5807 * zone_status can't be ZONE_IS_EMPTY or higher since curproc 5808 * is in the zone. 5809 */ 5810 ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 5811 if (zone_status_get(zone) > ZONE_IS_RUNNING) { 5812 /* 5813 * This zone is already on its way down. 5814 */ 5815 mutex_exit(&zone_status_lock); 5816 return (0); 5817 } 5818 /* 5819 * Prevent future zone_enter()s 5820 */ 5821 zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 5822 mutex_exit(&zone_status_lock); 5823 5824 /* 5825 * Kill everyone now and call zoneadmd later. 5826 * zone_ki_call_zoneadmd() will do a more thorough job of this 5827 * later. 5828 */ 5829 killall(zone->zone_id); 5830 /* 5831 * Now, create the thread to contact zoneadmd and do the rest of the 5832 * work. This thread can't be created in our zone otherwise 5833 * zone_destroy() would deadlock. 5834 */ 5835 zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP); 5836 zargp->arg.cmd = zcmd; 5837 zargp->arg.uniqid = zone->zone_uniqid; 5838 zargp->zone = zone; 5839 (void) strcpy(zargp->arg.locale, "C"); 5840 /* mdep was already copied in for us by uadmin */ 5841 if (mdep != NULL) 5842 (void) strlcpy(zargp->arg.bootbuf, mdep, 5843 sizeof (zargp->arg.bootbuf)); 5844 zone_hold(zone); 5845 5846 (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 5847 TS_RUN, minclsyspri); 5848 exit(CLD_EXITED, 0); 5849 5850 return (EINVAL); 5851 } 5852 5853 /* 5854 * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 5855 * status to ZONE_IS_SHUTTING_DOWN. 5856 * 5857 * This function also shuts down all running zones to ensure that they won't 5858 * fork new processes. 5859 */ 5860 void 5861 zone_shutdown_global(void) 5862 { 5863 zone_t *current_zonep; 5864 5865 ASSERT(INGLOBALZONE(curproc)); 5866 mutex_enter(&zonehash_lock); 5867 mutex_enter(&zone_status_lock); 5868 5869 /* Modify the global zone's status first. */ 5870 ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 5871 zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 5872 5873 /* 5874 * Now change the states of all running zones to ZONE_IS_SHUTTING_DOWN. 5875 * We don't mark all zones with ZONE_IS_SHUTTING_DOWN because doing so 5876 * could cause assertions to fail (e.g., assertions about a zone's 5877 * state during initialization, readying, or booting) or produce races. 5878 * We'll let threads continue to initialize and ready new zones: they'll 5879 * fail to boot the new zones when they see that the global zone is 5880 * shutting down. 5881 */ 5882 for (current_zonep = list_head(&zone_active); current_zonep != NULL; 5883 current_zonep = list_next(&zone_active, current_zonep)) { 5884 if (zone_status_get(current_zonep) == ZONE_IS_RUNNING) 5885 zone_status_set(current_zonep, ZONE_IS_SHUTTING_DOWN); 5886 } 5887 mutex_exit(&zone_status_lock); 5888 mutex_exit(&zonehash_lock); 5889 } 5890 5891 /* 5892 * Returns true if the named dataset is visible in the current zone. 5893 * The 'write' parameter is set to 1 if the dataset is also writable. 5894 */ 5895 int 5896 zone_dataset_visible(const char *dataset, int *write) 5897 { 5898 zone_dataset_t *zd; 5899 size_t len; 5900 zone_t *zone = curproc->p_zone; 5901 5902 if (dataset[0] == '\0') 5903 return (0); 5904 5905 /* 5906 * Walk the list once, looking for datasets which match exactly, or 5907 * specify a dataset underneath an exported dataset. If found, return 5908 * true and note that it is writable. 5909 */ 5910 for (zd = list_head(&zone->zone_datasets); zd != NULL; 5911 zd = list_next(&zone->zone_datasets, zd)) { 5912 5913 len = strlen(zd->zd_dataset); 5914 if (strlen(dataset) >= len && 5915 bcmp(dataset, zd->zd_dataset, len) == 0 && 5916 (dataset[len] == '\0' || dataset[len] == '/' || 5917 dataset[len] == '@')) { 5918 if (write) 5919 *write = 1; 5920 return (1); 5921 } 5922 } 5923 5924 /* 5925 * Walk the list a second time, searching for datasets which are parents 5926 * of exported datasets. These should be visible, but read-only. 5927 * 5928 * Note that we also have to support forms such as 'pool/dataset/', with 5929 * a trailing slash. 5930 */ 5931 for (zd = list_head(&zone->zone_datasets); zd != NULL; 5932 zd = list_next(&zone->zone_datasets, zd)) { 5933 5934 len = strlen(dataset); 5935 if (dataset[len - 1] == '/') 5936 len--; /* Ignore trailing slash */ 5937 if (len < strlen(zd->zd_dataset) && 5938 bcmp(dataset, zd->zd_dataset, len) == 0 && 5939 zd->zd_dataset[len] == '/') { 5940 if (write) 5941 *write = 0; 5942 return (1); 5943 } 5944 } 5945 5946 return (0); 5947 } 5948 5949 /* 5950 * zone_find_by_any_path() - 5951 * 5952 * kernel-private routine similar to zone_find_by_path(), but which 5953 * effectively compares against zone paths rather than zonerootpath 5954 * (i.e., the last component of zonerootpaths, which should be "root/", 5955 * are not compared.) This is done in order to accurately identify all 5956 * paths, whether zone-visible or not, including those which are parallel 5957 * to /root/, such as /dev/, /home/, etc... 5958 * 5959 * If the specified path does not fall under any zone path then global 5960 * zone is returned. 5961 * 5962 * The treat_abs parameter indicates whether the path should be treated as 5963 * an absolute path although it does not begin with "/". (This supports 5964 * nfs mount syntax such as host:any/path.) 5965 * 5966 * The caller is responsible for zone_rele of the returned zone. 5967 */ 5968 zone_t * 5969 zone_find_by_any_path(const char *path, boolean_t treat_abs) 5970 { 5971 zone_t *zone; 5972 int path_offset = 0; 5973 5974 if (path == NULL) { 5975 zone_hold(global_zone); 5976 return (global_zone); 5977 } 5978 5979 if (*path != '/') { 5980 ASSERT(treat_abs); 5981 path_offset = 1; 5982 } 5983 5984 mutex_enter(&zonehash_lock); 5985 for (zone = list_head(&zone_active); zone != NULL; 5986 zone = list_next(&zone_active, zone)) { 5987 char *c; 5988 size_t pathlen; 5989 char *rootpath_start; 5990 5991 if (zone == global_zone) /* skip global zone */ 5992 continue; 5993 5994 /* scan backwards to find start of last component */ 5995 c = zone->zone_rootpath + zone->zone_rootpathlen - 2; 5996 do { 5997 c--; 5998 } while (*c != '/'); 5999 6000 pathlen = c - zone->zone_rootpath + 1 - path_offset; 6001 rootpath_start = (zone->zone_rootpath + path_offset); 6002 if (strncmp(path, rootpath_start, pathlen) == 0) 6003 break; 6004 } 6005 if (zone == NULL) 6006 zone = global_zone; 6007 zone_hold(zone); 6008 mutex_exit(&zonehash_lock); 6009 return (zone); 6010 } 6011 6012 /* 6013 * Finds a zone_dl_t with the given linkid in the given zone. Returns the 6014 * zone_dl_t pointer if found, and NULL otherwise. 6015 */ 6016 static zone_dl_t * 6017 zone_find_dl(zone_t *zone, datalink_id_t linkid) 6018 { 6019 zone_dl_t *zdl; 6020 6021 ASSERT(mutex_owned(&zone->zone_lock)); 6022 for (zdl = list_head(&zone->zone_dl_list); zdl != NULL; 6023 zdl = list_next(&zone->zone_dl_list, zdl)) { 6024 if (zdl->zdl_id == linkid) 6025 break; 6026 } 6027 return (zdl); 6028 } 6029 6030 static boolean_t 6031 zone_dl_exists(zone_t *zone, datalink_id_t linkid) 6032 { 6033 boolean_t exists; 6034 6035 mutex_enter(&zone->zone_lock); 6036 exists = (zone_find_dl(zone, linkid) != NULL); 6037 mutex_exit(&zone->zone_lock); 6038 return (exists); 6039 } 6040 6041 /* 6042 * Add an data link name for the zone. 6043 */ 6044 static int 6045 zone_add_datalink(zoneid_t zoneid, datalink_id_t linkid) 6046 { 6047 zone_dl_t *zdl; 6048 zone_t *zone; 6049 zone_t *thiszone; 6050 6051 if ((thiszone = zone_find_by_id(zoneid)) == NULL) 6052 return (set_errno(ENXIO)); 6053 6054 /* Verify that the datalink ID doesn't already belong to a zone. */ 6055 mutex_enter(&zonehash_lock); 6056 for (zone = list_head(&zone_active); zone != NULL; 6057 zone = list_next(&zone_active, zone)) { 6058 if (zone_dl_exists(zone, linkid)) { 6059 mutex_exit(&zonehash_lock); 6060 zone_rele(thiszone); 6061 return (set_errno((zone == thiszone) ? EEXIST : EPERM)); 6062 } 6063 } 6064 6065 zdl = kmem_zalloc(sizeof (*zdl), KM_SLEEP); 6066 zdl->zdl_id = linkid; 6067 mutex_enter(&thiszone->zone_lock); 6068 list_insert_head(&thiszone->zone_dl_list, zdl); 6069 mutex_exit(&thiszone->zone_lock); 6070 mutex_exit(&zonehash_lock); 6071 zone_rele(thiszone); 6072 return (0); 6073 } 6074 6075 static int 6076 zone_remove_datalink(zoneid_t zoneid, datalink_id_t linkid) 6077 { 6078 zone_dl_t *zdl; 6079 zone_t *zone; 6080 int err = 0; 6081 6082 if ((zone = zone_find_by_id(zoneid)) == NULL) 6083 return (set_errno(EINVAL)); 6084 6085 mutex_enter(&zone->zone_lock); 6086 if ((zdl = zone_find_dl(zone, linkid)) == NULL) { 6087 err = ENXIO; 6088 } else { 6089 list_remove(&zone->zone_dl_list, zdl); 6090 kmem_free(zdl, sizeof (zone_dl_t)); 6091 } 6092 mutex_exit(&zone->zone_lock); 6093 zone_rele(zone); 6094 return (err == 0 ? 0 : set_errno(err)); 6095 } 6096 6097 /* 6098 * Using the zoneidp as ALL_ZONES, we can lookup which zone has been assigned 6099 * the linkid. Otherwise we just check if the specified zoneidp has been 6100 * assigned the supplied linkid. 6101 */ 6102 int 6103 zone_check_datalink(zoneid_t *zoneidp, datalink_id_t linkid) 6104 { 6105 zone_t *zone; 6106 int err = ENXIO; 6107 6108 if (*zoneidp != ALL_ZONES) { 6109 if ((zone = zone_find_by_id(*zoneidp)) != NULL) { 6110 if (zone_dl_exists(zone, linkid)) 6111 err = 0; 6112 zone_rele(zone); 6113 } 6114 return (err); 6115 } 6116 6117 mutex_enter(&zonehash_lock); 6118 for (zone = list_head(&zone_active); zone != NULL; 6119 zone = list_next(&zone_active, zone)) { 6120 if (zone_dl_exists(zone, linkid)) { 6121 *zoneidp = zone->zone_id; 6122 err = 0; 6123 break; 6124 } 6125 } 6126 mutex_exit(&zonehash_lock); 6127 return (err); 6128 } 6129 6130 /* 6131 * Get the list of datalink IDs assigned to a zone. 6132 * 6133 * On input, *nump is the number of datalink IDs that can fit in the supplied 6134 * idarray. Upon return, *nump is either set to the number of datalink IDs 6135 * that were placed in the array if the array was large enough, or to the 6136 * number of datalink IDs that the function needs to place in the array if the 6137 * array is too small. 6138 */ 6139 static int 6140 zone_list_datalink(zoneid_t zoneid, int *nump, datalink_id_t *idarray) 6141 { 6142 uint_t num, dlcount; 6143 zone_t *zone; 6144 zone_dl_t *zdl; 6145 datalink_id_t *idptr = idarray; 6146 6147 if (copyin(nump, &dlcount, sizeof (dlcount)) != 0) 6148 return (set_errno(EFAULT)); 6149 if ((zone = zone_find_by_id(zoneid)) == NULL) 6150 return (set_errno(ENXIO)); 6151 6152 num = 0; 6153 mutex_enter(&zone->zone_lock); 6154 for (zdl = list_head(&zone->zone_dl_list); zdl != NULL; 6155 zdl = list_next(&zone->zone_dl_list, zdl)) { 6156 /* 6157 * If the list is bigger than what the caller supplied, just 6158 * count, don't do copyout. 6159 */ 6160 if (++num > dlcount) 6161 continue; 6162 if (copyout(&zdl->zdl_id, idptr, sizeof (*idptr)) != 0) { 6163 mutex_exit(&zone->zone_lock); 6164 zone_rele(zone); 6165 return (set_errno(EFAULT)); 6166 } 6167 idptr++; 6168 } 6169 mutex_exit(&zone->zone_lock); 6170 zone_rele(zone); 6171 6172 /* Increased or decreased, caller should be notified. */ 6173 if (num != dlcount) { 6174 if (copyout(&num, nump, sizeof (num)) != 0) 6175 return (set_errno(EFAULT)); 6176 } 6177 return (0); 6178 } 6179 6180 /* 6181 * Public interface for looking up a zone by zoneid. It's a customized version 6182 * for netstack_zone_create(). It can only be called from the zsd create 6183 * callbacks, since it doesn't have reference on the zone structure hence if 6184 * it is called elsewhere the zone could disappear after the zonehash_lock 6185 * is dropped. 6186 * 6187 * Furthermore it 6188 * 1. Doesn't check the status of the zone. 6189 * 2. It will be called even before zone_init is called, in that case the 6190 * address of zone0 is returned directly, and netstack_zone_create() 6191 * will only assign a value to zone0.zone_netstack, won't break anything. 6192 * 3. Returns without the zone being held. 6193 */ 6194 zone_t * 6195 zone_find_by_id_nolock(zoneid_t zoneid) 6196 { 6197 zone_t *zone; 6198 6199 mutex_enter(&zonehash_lock); 6200 if (zonehashbyid == NULL) 6201 zone = &zone0; 6202 else 6203 zone = zone_find_all_by_id(zoneid); 6204 mutex_exit(&zonehash_lock); 6205 return (zone); 6206 } 6207 6208 /* 6209 * Walk the datalinks for a given zone 6210 */ 6211 int 6212 zone_datalink_walk(zoneid_t zoneid, int (*cb)(datalink_id_t, void *), 6213 void *data) 6214 { 6215 zone_t *zone; 6216 zone_dl_t *zdl; 6217 datalink_id_t *idarray; 6218 uint_t idcount = 0; 6219 int i, ret = 0; 6220 6221 if ((zone = zone_find_by_id(zoneid)) == NULL) 6222 return (ENOENT); 6223 6224 /* 6225 * We first build an array of linkid's so that we can walk these and 6226 * execute the callback with the zone_lock dropped. 6227 */ 6228 mutex_enter(&zone->zone_lock); 6229 for (zdl = list_head(&zone->zone_dl_list); zdl != NULL; 6230 zdl = list_next(&zone->zone_dl_list, zdl)) { 6231 idcount++; 6232 } 6233 6234 if (idcount == 0) { 6235 mutex_exit(&zone->zone_lock); 6236 zone_rele(zone); 6237 return (0); 6238 } 6239 6240 idarray = kmem_alloc(sizeof (datalink_id_t) * idcount, KM_NOSLEEP); 6241 if (idarray == NULL) { 6242 mutex_exit(&zone->zone_lock); 6243 zone_rele(zone); 6244 return (ENOMEM); 6245 } 6246 6247 for (i = 0, zdl = list_head(&zone->zone_dl_list); zdl != NULL; 6248 i++, zdl = list_next(&zone->zone_dl_list, zdl)) { 6249 idarray[i] = zdl->zdl_id; 6250 } 6251 6252 mutex_exit(&zone->zone_lock); 6253 6254 for (i = 0; i < idcount && ret == 0; i++) { 6255 if ((ret = (*cb)(idarray[i], data)) != 0) 6256 break; 6257 } 6258 6259 zone_rele(zone); 6260 kmem_free(idarray, sizeof (datalink_id_t) * idcount); 6261 return (ret); 6262 } 6263