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