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