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