xref: /illumos-gate/usr/src/uts/common/os/zone.c (revision 2428aad8)
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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2015, Joyent Inc. All rights reserved.
25  * Copyright (c) 2016 by Delphix. All rights reserved.
26  */
27 
28 /*
29  * Zones
30  *
31  *   A zone is a named collection of processes, namespace constraints,
32  *   and other system resources which comprise a secure and manageable
33  *   application containment facility.
34  *
35  *   Zones (represented by the reference counted zone_t) are tracked in
36  *   the kernel in the zonehash.  Elsewhere in the kernel, Zone IDs
37  *   (zoneid_t) are used to track zone association.  Zone IDs are
38  *   dynamically generated when the zone is created; if a persistent
39  *   identifier is needed (core files, accounting logs, audit trail,
40  *   etc.), the zone name should be used.
41  *
42  *
43  *   Global Zone:
44  *
45  *   The global zone (zoneid 0) is automatically associated with all
46  *   system resources that have not been bound to a user-created zone.
47  *   This means that even systems where zones are not in active use
48  *   have a global zone, and all processes, mounts, etc. are
49  *   associated with that zone.  The global zone is generally
50  *   unconstrained in terms of privileges and access, though the usual
51  *   credential and privilege based restrictions apply.
52  *
53  *
54  *   Zone States:
55  *
56  *   The states in which a zone may be in and the transitions are as
57  *   follows:
58  *
59  *   ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially
60  *   initialized zone is added to the list of active zones on the system but
61  *   isn't accessible.
62  *
63  *   ZONE_IS_INITIALIZED: Initialization complete except the ZSD callbacks are
64  *   not yet completed. Not possible to enter the zone, but attributes can
65  *   be retrieved.
66  *
67  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
68  *   ready.  The zone is made visible after the ZSD constructor callbacks are
69  *   executed.  A zone remains in this state until it transitions into
70  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
71  *
72  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
73  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
74  *   state.
75  *
76  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
77  *   successfully started init.   A zone remains in this state until
78  *   zone_shutdown() is called.
79  *
80  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
81  *   killing all processes running in the zone. The zone remains
82  *   in this state until there are no more user processes running in the zone.
83  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
84  *   Since zone_shutdown() is restartable, it may be called successfully
85  *   multiple times for the same zone_t.  Setting of the zone's state to
86  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
87  *   the zone's status without worrying about it being a moving target.
88  *
89  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
90  *   are no more user processes in the zone.  The zone remains in this
91  *   state until there are no more kernel threads associated with the
92  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
93  *   fail.
94  *
95  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
96  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
97  *   join the zone or create kernel threads therein.
98  *
99  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
100  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
101  *   return NULL from now on.
102  *
103  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
104  *   processes or threads doing work on behalf of the zone.  The zone is
105  *   removed from the list of active zones.  zone_destroy() returns, and
106  *   the zone can be recreated.
107  *
108  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
109  *   callbacks are executed, and all memory associated with the zone is
110  *   freed.
111  *
112  *   Threads can wait for the zone to enter a requested state by using
113  *   zone_status_wait() or zone_status_timedwait() with the desired
114  *   state passed in as an argument.  Zone state transitions are
115  *   uni-directional; it is not possible to move back to an earlier state.
116  *
117  *
118  *   Zone-Specific Data:
119  *
120  *   Subsystems needing to maintain zone-specific data can store that
121  *   data using the ZSD mechanism.  This provides a zone-specific data
122  *   store, similar to thread-specific data (see pthread_getspecific(3C)
123  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
124  *   to register callbacks to be invoked when a zone is created, shut
125  *   down, or destroyed.  This can be used to initialize zone-specific
126  *   data for new zones and to clean up when zones go away.
127  *
128  *
129  *   Data Structures:
130  *
131  *   The per-zone structure (zone_t) is reference counted, and freed
132  *   when all references are released.  zone_hold and zone_rele can be
133  *   used to adjust the reference count.  In addition, reference counts
134  *   associated with the cred_t structure are tracked separately using
135  *   zone_cred_hold and zone_cred_rele.
136  *
137  *   Pointers to active zone_t's are stored in two hash tables; one
138  *   for searching by id, the other for searching by name.  Lookups
139  *   can be performed on either basis, using zone_find_by_id and
140  *   zone_find_by_name.  Both return zone_t pointers with the zone
141  *   held, so zone_rele should be called when the pointer is no longer
142  *   needed.  Zones can also be searched by path; zone_find_by_path
143  *   returns the zone with which a path name is associated (global
144  *   zone if the path is not within some other zone's file system
145  *   hierarchy).  This currently requires iterating through each zone,
146  *   so it is slower than an id or name search via a hash table.
147  *
148  *
149  *   Locking:
150  *
151  *   zonehash_lock: This is a top-level global lock used to protect the
152  *       zone hash tables and lists.  Zones cannot be created or destroyed
153  *       while this lock is held.
154  *   zone_status_lock: This is a global lock protecting zone state.
155  *       Zones cannot change state while this lock is held.  It also
156  *       protects the list of kernel threads associated with a zone.
157  *   zone_lock: This is a per-zone lock used to protect several fields of
158  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
159  *       this lock means that the zone cannot go away.
160  *   zone_nlwps_lock: This is a per-zone lock used to protect the fields
161  *	 related to the zone.max-lwps rctl.
162  *   zone_mem_lock: This is a per-zone lock used to protect the fields
163  *	 related to the zone.max-locked-memory and zone.max-swap rctls.
164  *   zone_rctl_lock: This is a per-zone lock used to protect other rctls,
165  *       currently just max_lofi
166  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
167  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
168  *       list (a list of zones in the ZONE_IS_DEAD state).
169  *
170  *   Ordering requirements:
171  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
172  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
173  *
174  *   When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is:
175  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
176  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_nlwps_lock
177  *
178  *   Blocking memory allocations are permitted while holding any of the
179  *   zone locks.
180  *
181  *
182  *   System Call Interface:
183  *
184  *   The zone subsystem can be managed and queried from user level with
185  *   the following system calls (all subcodes of the primary "zone"
186  *   system call):
187  *   - zone_create: creates a zone with selected attributes (name,
188  *     root path, privileges, resource controls, ZFS datasets)
189  *   - zone_enter: allows the current process to enter a zone
190  *   - zone_getattr: reports attributes of a zone
191  *   - zone_setattr: set attributes of a zone
192  *   - zone_boot: set 'init' running for the zone
193  *   - zone_list: lists all zones active in the system
194  *   - zone_lookup: looks up zone id based on name
195  *   - zone_shutdown: initiates shutdown process (see states above)
196  *   - zone_destroy: completes shutdown process (see states above)
197  *
198  */
199 
200 #include <sys/priv_impl.h>
201 #include <sys/cred.h>
202 #include <c2/audit.h>
203 #include <sys/debug.h>
204 #include <sys/file.h>
205 #include <sys/kmem.h>
206 #include <sys/kstat.h>
207 #include <sys/mutex.h>
208 #include <sys/note.h>
209 #include <sys/pathname.h>
210 #include <sys/proc.h>
211 #include <sys/project.h>
212 #include <sys/sysevent.h>
213 #include <sys/task.h>
214 #include <sys/systm.h>
215 #include <sys/types.h>
216 #include <sys/utsname.h>
217 #include <sys/vnode.h>
218 #include <sys/vfs.h>
219 #include <sys/systeminfo.h>
220 #include <sys/policy.h>
221 #include <sys/cred_impl.h>
222 #include <sys/contract_impl.h>
223 #include <sys/contract/process_impl.h>
224 #include <sys/class.h>
225 #include <sys/pool.h>
226 #include <sys/pool_pset.h>
227 #include <sys/pset.h>
228 #include <sys/strlog.h>
229 #include <sys/sysmacros.h>
230 #include <sys/callb.h>
231 #include <sys/vmparam.h>
232 #include <sys/corectl.h>
233 #include <sys/ipc_impl.h>
234 #include <sys/klpd.h>
235 
236 #include <sys/door.h>
237 #include <sys/cpuvar.h>
238 #include <sys/sdt.h>
239 
240 #include <sys/uadmin.h>
241 #include <sys/session.h>
242 #include <sys/cmn_err.h>
243 #include <sys/modhash.h>
244 #include <sys/sunddi.h>
245 #include <sys/nvpair.h>
246 #include <sys/rctl.h>
247 #include <sys/fss.h>
248 #include <sys/brand.h>
249 #include <sys/zone.h>
250 #include <net/if.h>
251 #include <sys/cpucaps.h>
252 #include <vm/seg.h>
253 #include <sys/mac.h>
254 
255 /*
256  * This constant specifies the number of seconds that threads waiting for
257  * subsystems to release a zone's general-purpose references will wait before
258  * they log the zone's reference counts.  The constant's value shouldn't
259  * be so small that reference counts are unnecessarily reported for zones
260  * whose references are slowly released.  On the other hand, it shouldn't be so
261  * large that users reboot their systems out of frustration over hung zones
262  * before the system logs the zones' reference counts.
263  */
264 #define	ZONE_DESTROY_TIMEOUT_SECS	60
265 
266 /* List of data link IDs which are accessible from the zone */
267 typedef struct zone_dl {
268 	datalink_id_t	zdl_id;
269 	nvlist_t	*zdl_net;
270 	list_node_t	zdl_linkage;
271 } zone_dl_t;
272 
273 /*
274  * cv used to signal that all references to the zone have been released.  This
275  * needs to be global since there may be multiple waiters, and the first to
276  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
277  */
278 static kcondvar_t zone_destroy_cv;
279 /*
280  * Lock used to serialize access to zone_cv.  This could have been per-zone,
281  * but then we'd need another lock for zone_destroy_cv, and why bother?
282  */
283 static kmutex_t zone_status_lock;
284 
285 /*
286  * ZSD-related global variables.
287  */
288 static kmutex_t zsd_key_lock;	/* protects the following two */
289 /*
290  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
291  */
292 static zone_key_t zsd_keyval = 0;
293 /*
294  * Global list of registered keys.  We use this when a new zone is created.
295  */
296 static list_t zsd_registered_keys;
297 
298 int zone_hash_size = 256;
299 static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel;
300 static kmutex_t zonehash_lock;
301 static uint_t zonecount;
302 static id_space_t *zoneid_space;
303 
304 /*
305  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
306  * kernel proper runs, and which manages all other zones.
307  *
308  * Although not declared as static, the variable "zone0" should not be used
309  * except for by code that needs to reference the global zone early on in boot,
310  * before it is fully initialized.  All other consumers should use
311  * 'global_zone'.
312  */
313 zone_t zone0;
314 zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
315 
316 /*
317  * List of active zones, protected by zonehash_lock.
318  */
319 static list_t zone_active;
320 
321 /*
322  * List of destroyed zones that still have outstanding cred references.
323  * Used for debugging.  Uses a separate lock to avoid lock ordering
324  * problems in zone_free.
325  */
326 static list_t zone_deathrow;
327 static kmutex_t zone_deathrow_lock;
328 
329 /* number of zones is limited by virtual interface limit in IP */
330 uint_t maxzones = 8192;
331 
332 /* Event channel to sent zone state change notifications */
333 evchan_t *zone_event_chan;
334 
335 /*
336  * This table holds the mapping from kernel zone states to
337  * states visible in the state notification API.
338  * The idea is that we only expose "obvious" states and
339  * do not expose states which are just implementation details.
340  */
341 const char  *zone_status_table[] = {
342 	ZONE_EVENT_UNINITIALIZED,	/* uninitialized */
343 	ZONE_EVENT_INITIALIZED,		/* initialized */
344 	ZONE_EVENT_READY,		/* ready */
345 	ZONE_EVENT_READY,		/* booting */
346 	ZONE_EVENT_RUNNING,		/* running */
347 	ZONE_EVENT_SHUTTING_DOWN,	/* shutting_down */
348 	ZONE_EVENT_SHUTTING_DOWN,	/* empty */
349 	ZONE_EVENT_SHUTTING_DOWN,	/* down */
350 	ZONE_EVENT_SHUTTING_DOWN,	/* dying */
351 	ZONE_EVENT_UNINITIALIZED,	/* dead */
352 };
353 
354 /*
355  * This array contains the names of the subsystems listed in zone_ref_subsys_t
356  * (see sys/zone.h).
357  */
358 static char *zone_ref_subsys_names[] = {
359 	"NFS",		/* ZONE_REF_NFS */
360 	"NFSv4",	/* ZONE_REF_NFSV4 */
361 	"SMBFS",	/* ZONE_REF_SMBFS */
362 	"MNTFS",	/* ZONE_REF_MNTFS */
363 	"LOFI",		/* ZONE_REF_LOFI */
364 	"VFS",		/* ZONE_REF_VFS */
365 	"IPC"		/* ZONE_REF_IPC */
366 };
367 
368 /*
369  * This isn't static so lint doesn't complain.
370  */
371 rctl_hndl_t rc_zone_cpu_shares;
372 rctl_hndl_t rc_zone_locked_mem;
373 rctl_hndl_t rc_zone_max_swap;
374 rctl_hndl_t rc_zone_max_lofi;
375 rctl_hndl_t rc_zone_cpu_cap;
376 rctl_hndl_t rc_zone_nlwps;
377 rctl_hndl_t rc_zone_nprocs;
378 rctl_hndl_t rc_zone_shmmax;
379 rctl_hndl_t rc_zone_shmmni;
380 rctl_hndl_t rc_zone_semmni;
381 rctl_hndl_t rc_zone_msgmni;
382 
383 const char * const zone_default_initname = "/sbin/init";
384 static char * const zone_prefix = "/zone/";
385 static int zone_shutdown(zoneid_t zoneid);
386 static int zone_add_datalink(zoneid_t, datalink_id_t);
387 static int zone_remove_datalink(zoneid_t, datalink_id_t);
388 static int zone_list_datalink(zoneid_t, int *, datalink_id_t *);
389 static int zone_set_network(zoneid_t, zone_net_data_t *);
390 static int zone_get_network(zoneid_t, zone_net_data_t *);
391 
392 typedef boolean_t zsd_applyfn_t(kmutex_t *, boolean_t, zone_t *, zone_key_t);
393 
394 static void zsd_apply_all_zones(zsd_applyfn_t *, zone_key_t);
395 static void zsd_apply_all_keys(zsd_applyfn_t *, zone_t *);
396 static boolean_t zsd_apply_create(kmutex_t *, boolean_t, zone_t *, zone_key_t);
397 static boolean_t zsd_apply_shutdown(kmutex_t *, boolean_t, zone_t *,
398     zone_key_t);
399 static boolean_t zsd_apply_destroy(kmutex_t *, boolean_t, zone_t *, zone_key_t);
400 static boolean_t zsd_wait_for_creator(zone_t *, struct zsd_entry *,
401     kmutex_t *);
402 static boolean_t zsd_wait_for_inprogress(zone_t *, struct zsd_entry *,
403     kmutex_t *);
404 
405 /*
406  * Bump this number when you alter the zone syscall interfaces; this is
407  * because we need to have support for previous API versions in libc
408  * to support patching; libc calls into the kernel to determine this number.
409  *
410  * Version 1 of the API is the version originally shipped with Solaris 10
411  * Version 2 alters the zone_create system call in order to support more
412  *     arguments by moving the args into a structure; and to do better
413  *     error reporting when zone_create() fails.
414  * Version 3 alters the zone_create system call in order to support the
415  *     import of ZFS datasets to zones.
416  * Version 4 alters the zone_create system call in order to support
417  *     Trusted Extensions.
418  * Version 5 alters the zone_boot system call, and converts its old
419  *     bootargs parameter to be set by the zone_setattr API instead.
420  * Version 6 adds the flag argument to zone_create.
421  */
422 static const int ZONE_SYSCALL_API_VERSION = 6;
423 
424 /*
425  * Certain filesystems (such as NFS and autofs) need to know which zone
426  * the mount is being placed in.  Because of this, we need to be able to
427  * ensure that a zone isn't in the process of being created/destroyed such
428  * that nfs_mount() thinks it is in the global/NGZ zone, while by the time
429  * it gets added the list of mounted zones, it ends up on the wrong zone's
430  * mount list. Since a zone can't reside on an NFS file system, we don't
431  * have to worry about the zonepath itself.
432  *
433  * The following functions: block_mounts()/resume_mounts() and
434  * mount_in_progress()/mount_completed() are used by zones and the VFS
435  * layer (respectively) to synchronize zone state transitions and new
436  * mounts within a zone. This syncronization is on a per-zone basis, so
437  * activity for one zone will not interfere with activity for another zone.
438  *
439  * The semantics are like a reader-reader lock such that there may
440  * either be multiple mounts (or zone state transitions, if that weren't
441  * serialized by zonehash_lock) in progress at the same time, but not
442  * both.
443  *
444  * We use cv's so the user can ctrl-C out of the operation if it's
445  * taking too long.
446  *
447  * The semantics are such that there is unfair bias towards the
448  * "current" operation.  This means that zone halt may starve if
449  * there is a rapid succession of new mounts coming in to the zone.
450  */
451 /*
452  * Prevent new mounts from progressing to the point of calling
453  * VFS_MOUNT().  If there are already mounts in this "region", wait for
454  * them to complete.
455  */
456 static int
457 block_mounts(zone_t *zp)
458 {
459 	int retval = 0;
460 
461 	/*
462 	 * Since it may block for a long time, block_mounts() shouldn't be
463 	 * called with zonehash_lock held.
464 	 */
465 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
466 	mutex_enter(&zp->zone_mount_lock);
467 	while (zp->zone_mounts_in_progress > 0) {
468 		if (cv_wait_sig(&zp->zone_mount_cv, &zp->zone_mount_lock) == 0)
469 			goto signaled;
470 	}
471 	/*
472 	 * A negative value of mounts_in_progress indicates that mounts
473 	 * have been blocked by (-mounts_in_progress) different callers
474 	 * (remotely possible if two threads enter zone_shutdown at the same
475 	 * time).
476 	 */
477 	zp->zone_mounts_in_progress--;
478 	retval = 1;
479 signaled:
480 	mutex_exit(&zp->zone_mount_lock);
481 	return (retval);
482 }
483 
484 /*
485  * The VFS layer may progress with new mounts as far as we're concerned.
486  * Allow them to progress if we were the last obstacle.
487  */
488 static void
489 resume_mounts(zone_t *zp)
490 {
491 	mutex_enter(&zp->zone_mount_lock);
492 	if (++zp->zone_mounts_in_progress == 0)
493 		cv_broadcast(&zp->zone_mount_cv);
494 	mutex_exit(&zp->zone_mount_lock);
495 }
496 
497 /*
498  * The VFS layer is busy with a mount; this zone should wait until all
499  * of its mounts are completed to progress.
500  */
501 void
502 mount_in_progress(zone_t *zp)
503 {
504 	mutex_enter(&zp->zone_mount_lock);
505 	while (zp->zone_mounts_in_progress < 0)
506 		cv_wait(&zp->zone_mount_cv, &zp->zone_mount_lock);
507 	zp->zone_mounts_in_progress++;
508 	mutex_exit(&zp->zone_mount_lock);
509 }
510 
511 /*
512  * VFS is done with one mount; wake up any waiting block_mounts()
513  * callers if this is the last mount.
514  */
515 void
516 mount_completed(zone_t *zp)
517 {
518 	mutex_enter(&zp->zone_mount_lock);
519 	if (--zp->zone_mounts_in_progress == 0)
520 		cv_broadcast(&zp->zone_mount_cv);
521 	mutex_exit(&zp->zone_mount_lock);
522 }
523 
524 /*
525  * ZSD routines.
526  *
527  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
528  * defined by the pthread_key_create() and related interfaces.
529  *
530  * Kernel subsystems may register one or more data items and/or
531  * callbacks to be executed when a zone is created, shutdown, or
532  * destroyed.
533  *
534  * Unlike the thread counterpart, destructor callbacks will be executed
535  * even if the data pointer is NULL and/or there are no constructor
536  * callbacks, so it is the responsibility of such callbacks to check for
537  * NULL data values if necessary.
538  *
539  * The locking strategy and overall picture is as follows:
540  *
541  * When someone calls zone_key_create(), a template ZSD entry is added to the
542  * global list "zsd_registered_keys", protected by zsd_key_lock.  While
543  * holding that lock all the existing zones are marked as
544  * ZSD_CREATE_NEEDED and a copy of the ZSD entry added to the per-zone
545  * zone_zsd list (protected by zone_lock). The global list is updated first
546  * (under zone_key_lock) to make sure that newly created zones use the
547  * most recent list of keys. Then under zonehash_lock we walk the zones
548  * and mark them.  Similar locking is used in zone_key_delete().
549  *
550  * The actual create, shutdown, and destroy callbacks are done without
551  * holding any lock. And zsd_flags are used to ensure that the operations
552  * completed so that when zone_key_create (and zone_create) is done, as well as
553  * zone_key_delete (and zone_destroy) is done, all the necessary callbacks
554  * are completed.
555  *
556  * When new zones are created constructor callbacks for all registered ZSD
557  * entries will be called. That also uses the above two phases of marking
558  * what needs to be done, and then running the callbacks without holding
559  * any locks.
560  *
561  * The framework does not provide any locking around zone_getspecific() and
562  * zone_setspecific() apart from that needed for internal consistency, so
563  * callers interested in atomic "test-and-set" semantics will need to provide
564  * their own locking.
565  */
566 
567 /*
568  * Helper function to find the zsd_entry associated with the key in the
569  * given list.
570  */
571 static struct zsd_entry *
572 zsd_find(list_t *l, zone_key_t key)
573 {
574 	struct zsd_entry *zsd;
575 
576 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
577 		if (zsd->zsd_key == key) {
578 			return (zsd);
579 		}
580 	}
581 	return (NULL);
582 }
583 
584 /*
585  * Helper function to find the zsd_entry associated with the key in the
586  * given list. Move it to the front of the list.
587  */
588 static struct zsd_entry *
589 zsd_find_mru(list_t *l, zone_key_t key)
590 {
591 	struct zsd_entry *zsd;
592 
593 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
594 		if (zsd->zsd_key == key) {
595 			/*
596 			 * Move to head of list to keep list in MRU order.
597 			 */
598 			if (zsd != list_head(l)) {
599 				list_remove(l, zsd);
600 				list_insert_head(l, zsd);
601 			}
602 			return (zsd);
603 		}
604 	}
605 	return (NULL);
606 }
607 
608 void
609 zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
610     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
611 {
612 	struct zsd_entry *zsdp;
613 	struct zsd_entry *t;
614 	struct zone *zone;
615 	zone_key_t  key;
616 
617 	zsdp = kmem_zalloc(sizeof (*zsdp), KM_SLEEP);
618 	zsdp->zsd_data = NULL;
619 	zsdp->zsd_create = create;
620 	zsdp->zsd_shutdown = shutdown;
621 	zsdp->zsd_destroy = destroy;
622 
623 	/*
624 	 * Insert in global list of callbacks. Makes future zone creations
625 	 * see it.
626 	 */
627 	mutex_enter(&zsd_key_lock);
628 	key = zsdp->zsd_key = ++zsd_keyval;
629 	ASSERT(zsd_keyval != 0);
630 	list_insert_tail(&zsd_registered_keys, zsdp);
631 	mutex_exit(&zsd_key_lock);
632 
633 	/*
634 	 * Insert for all existing zones and mark them as needing
635 	 * a create callback.
636 	 */
637 	mutex_enter(&zonehash_lock);	/* stop the world */
638 	for (zone = list_head(&zone_active); zone != NULL;
639 	    zone = list_next(&zone_active, zone)) {
640 		zone_status_t status;
641 
642 		mutex_enter(&zone->zone_lock);
643 
644 		/* Skip zones that are on the way down or not yet up */
645 		status = zone_status_get(zone);
646 		if (status >= ZONE_IS_DOWN ||
647 		    status == ZONE_IS_UNINITIALIZED) {
648 			mutex_exit(&zone->zone_lock);
649 			continue;
650 		}
651 
652 		t = zsd_find_mru(&zone->zone_zsd, key);
653 		if (t != NULL) {
654 			/*
655 			 * A zsd_configure already inserted it after
656 			 * we dropped zsd_key_lock above.
657 			 */
658 			mutex_exit(&zone->zone_lock);
659 			continue;
660 		}
661 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
662 		t->zsd_key = key;
663 		t->zsd_create = create;
664 		t->zsd_shutdown = shutdown;
665 		t->zsd_destroy = destroy;
666 		if (create != NULL) {
667 			t->zsd_flags = ZSD_CREATE_NEEDED;
668 			DTRACE_PROBE2(zsd__create__needed,
669 			    zone_t *, zone, zone_key_t, key);
670 		}
671 		list_insert_tail(&zone->zone_zsd, t);
672 		mutex_exit(&zone->zone_lock);
673 	}
674 	mutex_exit(&zonehash_lock);
675 
676 	if (create != NULL) {
677 		/* Now call the create callback for this key */
678 		zsd_apply_all_zones(zsd_apply_create, key);
679 	}
680 	/*
681 	 * It is safe for consumers to use the key now, make it
682 	 * globally visible. Specifically zone_getspecific() will
683 	 * always successfully return the zone specific data associated
684 	 * with the key.
685 	 */
686 	*keyp = key;
687 
688 }
689 
690 /*
691  * Function called when a module is being unloaded, or otherwise wishes
692  * to unregister its ZSD key and callbacks.
693  *
694  * Remove from the global list and determine the functions that need to
695  * be called under a global lock. Then call the functions without
696  * holding any locks. Finally free up the zone_zsd entries. (The apply
697  * functions need to access the zone_zsd entries to find zsd_data etc.)
698  */
699 int
700 zone_key_delete(zone_key_t key)
701 {
702 	struct zsd_entry *zsdp = NULL;
703 	zone_t *zone;
704 
705 	mutex_enter(&zsd_key_lock);
706 	zsdp = zsd_find_mru(&zsd_registered_keys, key);
707 	if (zsdp == NULL) {
708 		mutex_exit(&zsd_key_lock);
709 		return (-1);
710 	}
711 	list_remove(&zsd_registered_keys, zsdp);
712 	mutex_exit(&zsd_key_lock);
713 
714 	mutex_enter(&zonehash_lock);
715 	for (zone = list_head(&zone_active); zone != NULL;
716 	    zone = list_next(&zone_active, zone)) {
717 		struct zsd_entry *del;
718 
719 		mutex_enter(&zone->zone_lock);
720 		del = zsd_find_mru(&zone->zone_zsd, key);
721 		if (del == NULL) {
722 			/*
723 			 * Somebody else got here first e.g the zone going
724 			 * away.
725 			 */
726 			mutex_exit(&zone->zone_lock);
727 			continue;
728 		}
729 		ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
730 		ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
731 		if (del->zsd_shutdown != NULL &&
732 		    (del->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
733 			del->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
734 			DTRACE_PROBE2(zsd__shutdown__needed,
735 			    zone_t *, zone, zone_key_t, key);
736 		}
737 		if (del->zsd_destroy != NULL &&
738 		    (del->zsd_flags & ZSD_DESTROY_ALL) == 0) {
739 			del->zsd_flags |= ZSD_DESTROY_NEEDED;
740 			DTRACE_PROBE2(zsd__destroy__needed,
741 			    zone_t *, zone, zone_key_t, key);
742 		}
743 		mutex_exit(&zone->zone_lock);
744 	}
745 	mutex_exit(&zonehash_lock);
746 	kmem_free(zsdp, sizeof (*zsdp));
747 
748 	/* Now call the shutdown and destroy callback for this key */
749 	zsd_apply_all_zones(zsd_apply_shutdown, key);
750 	zsd_apply_all_zones(zsd_apply_destroy, key);
751 
752 	/* Now we can free up the zsdp structures in each zone */
753 	mutex_enter(&zonehash_lock);
754 	for (zone = list_head(&zone_active); zone != NULL;
755 	    zone = list_next(&zone_active, zone)) {
756 		struct zsd_entry *del;
757 
758 		mutex_enter(&zone->zone_lock);
759 		del = zsd_find(&zone->zone_zsd, key);
760 		if (del != NULL) {
761 			list_remove(&zone->zone_zsd, del);
762 			ASSERT(!(del->zsd_flags & ZSD_ALL_INPROGRESS));
763 			kmem_free(del, sizeof (*del));
764 		}
765 		mutex_exit(&zone->zone_lock);
766 	}
767 	mutex_exit(&zonehash_lock);
768 
769 	return (0);
770 }
771 
772 /*
773  * ZSD counterpart of pthread_setspecific().
774  *
775  * Since all zsd callbacks, including those with no create function,
776  * have an entry in zone_zsd, if the key is registered it is part of
777  * the zone_zsd list.
778  * Return an error if the key wasn't registerd.
779  */
780 int
781 zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
782 {
783 	struct zsd_entry *t;
784 
785 	mutex_enter(&zone->zone_lock);
786 	t = zsd_find_mru(&zone->zone_zsd, key);
787 	if (t != NULL) {
788 		/*
789 		 * Replace old value with new
790 		 */
791 		t->zsd_data = (void *)data;
792 		mutex_exit(&zone->zone_lock);
793 		return (0);
794 	}
795 	mutex_exit(&zone->zone_lock);
796 	return (-1);
797 }
798 
799 /*
800  * ZSD counterpart of pthread_getspecific().
801  */
802 void *
803 zone_getspecific(zone_key_t key, zone_t *zone)
804 {
805 	struct zsd_entry *t;
806 	void *data;
807 
808 	mutex_enter(&zone->zone_lock);
809 	t = zsd_find_mru(&zone->zone_zsd, key);
810 	data = (t == NULL ? NULL : t->zsd_data);
811 	mutex_exit(&zone->zone_lock);
812 	return (data);
813 }
814 
815 /*
816  * Function used to initialize a zone's list of ZSD callbacks and data
817  * when the zone is being created.  The callbacks are initialized from
818  * the template list (zsd_registered_keys). The constructor callback is
819  * executed later (once the zone exists and with locks dropped).
820  */
821 static void
822 zone_zsd_configure(zone_t *zone)
823 {
824 	struct zsd_entry *zsdp;
825 	struct zsd_entry *t;
826 
827 	ASSERT(MUTEX_HELD(&zonehash_lock));
828 	ASSERT(list_head(&zone->zone_zsd) == NULL);
829 	mutex_enter(&zone->zone_lock);
830 	mutex_enter(&zsd_key_lock);
831 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
832 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
833 		/*
834 		 * Since this zone is ZONE_IS_UNCONFIGURED, zone_key_create
835 		 * should not have added anything to it.
836 		 */
837 		ASSERT(zsd_find(&zone->zone_zsd, zsdp->zsd_key) == NULL);
838 
839 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
840 		t->zsd_key = zsdp->zsd_key;
841 		t->zsd_create = zsdp->zsd_create;
842 		t->zsd_shutdown = zsdp->zsd_shutdown;
843 		t->zsd_destroy = zsdp->zsd_destroy;
844 		if (zsdp->zsd_create != NULL) {
845 			t->zsd_flags = ZSD_CREATE_NEEDED;
846 			DTRACE_PROBE2(zsd__create__needed,
847 			    zone_t *, zone, zone_key_t, zsdp->zsd_key);
848 		}
849 		list_insert_tail(&zone->zone_zsd, t);
850 	}
851 	mutex_exit(&zsd_key_lock);
852 	mutex_exit(&zone->zone_lock);
853 }
854 
855 enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
856 
857 /*
858  * Helper function to execute shutdown or destructor callbacks.
859  */
860 static void
861 zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
862 {
863 	struct zsd_entry *t;
864 
865 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
866 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
867 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
868 
869 	/*
870 	 * Run the callback solely based on what is registered for the zone
871 	 * in zone_zsd. The global list can change independently of this
872 	 * as keys are registered and unregistered and we don't register new
873 	 * callbacks for a zone that is in the process of going away.
874 	 */
875 	mutex_enter(&zone->zone_lock);
876 	for (t = list_head(&zone->zone_zsd); t != NULL;
877 	    t = list_next(&zone->zone_zsd, t)) {
878 		zone_key_t key = t->zsd_key;
879 
880 		/* Skip if no callbacks registered */
881 
882 		if (ct == ZSD_SHUTDOWN) {
883 			if (t->zsd_shutdown != NULL &&
884 			    (t->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
885 				t->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
886 				DTRACE_PROBE2(zsd__shutdown__needed,
887 				    zone_t *, zone, zone_key_t, key);
888 			}
889 		} else {
890 			if (t->zsd_destroy != NULL &&
891 			    (t->zsd_flags & ZSD_DESTROY_ALL) == 0) {
892 				t->zsd_flags |= ZSD_DESTROY_NEEDED;
893 				DTRACE_PROBE2(zsd__destroy__needed,
894 				    zone_t *, zone, zone_key_t, key);
895 			}
896 		}
897 	}
898 	mutex_exit(&zone->zone_lock);
899 
900 	/* Now call the shutdown and destroy callback for this key */
901 	zsd_apply_all_keys(zsd_apply_shutdown, zone);
902 	zsd_apply_all_keys(zsd_apply_destroy, zone);
903 
904 }
905 
906 /*
907  * Called when the zone is going away; free ZSD-related memory, and
908  * destroy the zone_zsd list.
909  */
910 static void
911 zone_free_zsd(zone_t *zone)
912 {
913 	struct zsd_entry *t, *next;
914 
915 	/*
916 	 * Free all the zsd_entry's we had on this zone.
917 	 */
918 	mutex_enter(&zone->zone_lock);
919 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
920 		next = list_next(&zone->zone_zsd, t);
921 		list_remove(&zone->zone_zsd, t);
922 		ASSERT(!(t->zsd_flags & ZSD_ALL_INPROGRESS));
923 		kmem_free(t, sizeof (*t));
924 	}
925 	list_destroy(&zone->zone_zsd);
926 	mutex_exit(&zone->zone_lock);
927 
928 }
929 
930 /*
931  * Apply a function to all zones for particular key value.
932  *
933  * The applyfn has to drop zonehash_lock if it does some work, and
934  * then reacquire it before it returns.
935  * When the lock is dropped we don't follow list_next even
936  * if it is possible to do so without any hazards. This is
937  * because we want the design to allow for the list of zones
938  * to change in any arbitrary way during the time the
939  * lock was dropped.
940  *
941  * It is safe to restart the loop at list_head since the applyfn
942  * changes the zsd_flags as it does work, so a subsequent
943  * pass through will have no effect in applyfn, hence the loop will terminate
944  * in at worst O(N^2).
945  */
946 static void
947 zsd_apply_all_zones(zsd_applyfn_t *applyfn, zone_key_t key)
948 {
949 	zone_t *zone;
950 
951 	mutex_enter(&zonehash_lock);
952 	zone = list_head(&zone_active);
953 	while (zone != NULL) {
954 		if ((applyfn)(&zonehash_lock, B_FALSE, zone, key)) {
955 			/* Lock dropped - restart at head */
956 			zone = list_head(&zone_active);
957 		} else {
958 			zone = list_next(&zone_active, zone);
959 		}
960 	}
961 	mutex_exit(&zonehash_lock);
962 }
963 
964 /*
965  * Apply a function to all keys for a particular zone.
966  *
967  * The applyfn has to drop zonehash_lock if it does some work, and
968  * then reacquire it before it returns.
969  * When the lock is dropped we don't follow list_next even
970  * if it is possible to do so without any hazards. This is
971  * because we want the design to allow for the list of zsd callbacks
972  * to change in any arbitrary way during the time the
973  * lock was dropped.
974  *
975  * It is safe to restart the loop at list_head since the applyfn
976  * changes the zsd_flags as it does work, so a subsequent
977  * pass through will have no effect in applyfn, hence the loop will terminate
978  * in at worst O(N^2).
979  */
980 static void
981 zsd_apply_all_keys(zsd_applyfn_t *applyfn, zone_t *zone)
982 {
983 	struct zsd_entry *t;
984 
985 	mutex_enter(&zone->zone_lock);
986 	t = list_head(&zone->zone_zsd);
987 	while (t != NULL) {
988 		if ((applyfn)(NULL, B_TRUE, zone, t->zsd_key)) {
989 			/* Lock dropped - restart at head */
990 			t = list_head(&zone->zone_zsd);
991 		} else {
992 			t = list_next(&zone->zone_zsd, t);
993 		}
994 	}
995 	mutex_exit(&zone->zone_lock);
996 }
997 
998 /*
999  * Call the create function for the zone and key if CREATE_NEEDED
1000  * is set.
1001  * If some other thread gets here first and sets CREATE_INPROGRESS, then
1002  * we wait for that thread to complete so that we can ensure that
1003  * all the callbacks are done when we've looped over all zones/keys.
1004  *
1005  * When we call the create function, we drop the global held by the
1006  * caller, and return true to tell the caller it needs to re-evalute the
1007  * state.
1008  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1009  * remains held on exit.
1010  */
1011 static boolean_t
1012 zsd_apply_create(kmutex_t *lockp, boolean_t zone_lock_held,
1013     zone_t *zone, zone_key_t key)
1014 {
1015 	void *result;
1016 	struct zsd_entry *t;
1017 	boolean_t dropped;
1018 
1019 	if (lockp != NULL) {
1020 		ASSERT(MUTEX_HELD(lockp));
1021 	}
1022 	if (zone_lock_held) {
1023 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1024 	} else {
1025 		mutex_enter(&zone->zone_lock);
1026 	}
1027 
1028 	t = zsd_find(&zone->zone_zsd, key);
1029 	if (t == NULL) {
1030 		/*
1031 		 * Somebody else got here first e.g the zone going
1032 		 * away.
1033 		 */
1034 		if (!zone_lock_held)
1035 			mutex_exit(&zone->zone_lock);
1036 		return (B_FALSE);
1037 	}
1038 	dropped = B_FALSE;
1039 	if (zsd_wait_for_inprogress(zone, t, lockp))
1040 		dropped = B_TRUE;
1041 
1042 	if (t->zsd_flags & ZSD_CREATE_NEEDED) {
1043 		t->zsd_flags &= ~ZSD_CREATE_NEEDED;
1044 		t->zsd_flags |= ZSD_CREATE_INPROGRESS;
1045 		DTRACE_PROBE2(zsd__create__inprogress,
1046 		    zone_t *, zone, zone_key_t, key);
1047 		mutex_exit(&zone->zone_lock);
1048 		if (lockp != NULL)
1049 			mutex_exit(lockp);
1050 
1051 		dropped = B_TRUE;
1052 		ASSERT(t->zsd_create != NULL);
1053 		DTRACE_PROBE2(zsd__create__start,
1054 		    zone_t *, zone, zone_key_t, key);
1055 
1056 		result = (*t->zsd_create)(zone->zone_id);
1057 
1058 		DTRACE_PROBE2(zsd__create__end,
1059 		    zone_t *, zone, voidn *, result);
1060 
1061 		ASSERT(result != NULL);
1062 		if (lockp != NULL)
1063 			mutex_enter(lockp);
1064 		mutex_enter(&zone->zone_lock);
1065 		t->zsd_data = result;
1066 		t->zsd_flags &= ~ZSD_CREATE_INPROGRESS;
1067 		t->zsd_flags |= ZSD_CREATE_COMPLETED;
1068 		cv_broadcast(&t->zsd_cv);
1069 		DTRACE_PROBE2(zsd__create__completed,
1070 		    zone_t *, zone, zone_key_t, key);
1071 	}
1072 	if (!zone_lock_held)
1073 		mutex_exit(&zone->zone_lock);
1074 	return (dropped);
1075 }
1076 
1077 /*
1078  * Call the shutdown function for the zone and key if SHUTDOWN_NEEDED
1079  * is set.
1080  * If some other thread gets here first and sets *_INPROGRESS, then
1081  * we wait for that thread to complete so that we can ensure that
1082  * all the callbacks are done when we've looped over all zones/keys.
1083  *
1084  * When we call the shutdown function, we drop the global held by the
1085  * caller, and return true to tell the caller it needs to re-evalute the
1086  * state.
1087  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1088  * remains held on exit.
1089  */
1090 static boolean_t
1091 zsd_apply_shutdown(kmutex_t *lockp, boolean_t zone_lock_held,
1092     zone_t *zone, zone_key_t key)
1093 {
1094 	struct zsd_entry *t;
1095 	void *data;
1096 	boolean_t dropped;
1097 
1098 	if (lockp != NULL) {
1099 		ASSERT(MUTEX_HELD(lockp));
1100 	}
1101 	if (zone_lock_held) {
1102 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1103 	} else {
1104 		mutex_enter(&zone->zone_lock);
1105 	}
1106 
1107 	t = zsd_find(&zone->zone_zsd, key);
1108 	if (t == NULL) {
1109 		/*
1110 		 * Somebody else got here first e.g the zone going
1111 		 * away.
1112 		 */
1113 		if (!zone_lock_held)
1114 			mutex_exit(&zone->zone_lock);
1115 		return (B_FALSE);
1116 	}
1117 	dropped = B_FALSE;
1118 	if (zsd_wait_for_creator(zone, t, lockp))
1119 		dropped = B_TRUE;
1120 
1121 	if (zsd_wait_for_inprogress(zone, t, lockp))
1122 		dropped = B_TRUE;
1123 
1124 	if (t->zsd_flags & ZSD_SHUTDOWN_NEEDED) {
1125 		t->zsd_flags &= ~ZSD_SHUTDOWN_NEEDED;
1126 		t->zsd_flags |= ZSD_SHUTDOWN_INPROGRESS;
1127 		DTRACE_PROBE2(zsd__shutdown__inprogress,
1128 		    zone_t *, zone, zone_key_t, key);
1129 		mutex_exit(&zone->zone_lock);
1130 		if (lockp != NULL)
1131 			mutex_exit(lockp);
1132 		dropped = B_TRUE;
1133 
1134 		ASSERT(t->zsd_shutdown != NULL);
1135 		data = t->zsd_data;
1136 
1137 		DTRACE_PROBE2(zsd__shutdown__start,
1138 		    zone_t *, zone, zone_key_t, key);
1139 
1140 		(t->zsd_shutdown)(zone->zone_id, data);
1141 		DTRACE_PROBE2(zsd__shutdown__end,
1142 		    zone_t *, zone, zone_key_t, key);
1143 
1144 		if (lockp != NULL)
1145 			mutex_enter(lockp);
1146 		mutex_enter(&zone->zone_lock);
1147 		t->zsd_flags &= ~ZSD_SHUTDOWN_INPROGRESS;
1148 		t->zsd_flags |= ZSD_SHUTDOWN_COMPLETED;
1149 		cv_broadcast(&t->zsd_cv);
1150 		DTRACE_PROBE2(zsd__shutdown__completed,
1151 		    zone_t *, zone, zone_key_t, key);
1152 	}
1153 	if (!zone_lock_held)
1154 		mutex_exit(&zone->zone_lock);
1155 	return (dropped);
1156 }
1157 
1158 /*
1159  * Call the destroy function for the zone and key if DESTROY_NEEDED
1160  * is set.
1161  * If some other thread gets here first and sets *_INPROGRESS, then
1162  * we wait for that thread to complete so that we can ensure that
1163  * all the callbacks are done when we've looped over all zones/keys.
1164  *
1165  * When we call the destroy function, we drop the global held by the
1166  * caller, and return true to tell the caller it needs to re-evalute the
1167  * state.
1168  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1169  * remains held on exit.
1170  */
1171 static boolean_t
1172 zsd_apply_destroy(kmutex_t *lockp, boolean_t zone_lock_held,
1173     zone_t *zone, zone_key_t key)
1174 {
1175 	struct zsd_entry *t;
1176 	void *data;
1177 	boolean_t dropped;
1178 
1179 	if (lockp != NULL) {
1180 		ASSERT(MUTEX_HELD(lockp));
1181 	}
1182 	if (zone_lock_held) {
1183 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1184 	} else {
1185 		mutex_enter(&zone->zone_lock);
1186 	}
1187 
1188 	t = zsd_find(&zone->zone_zsd, key);
1189 	if (t == NULL) {
1190 		/*
1191 		 * Somebody else got here first e.g the zone going
1192 		 * away.
1193 		 */
1194 		if (!zone_lock_held)
1195 			mutex_exit(&zone->zone_lock);
1196 		return (B_FALSE);
1197 	}
1198 	dropped = B_FALSE;
1199 	if (zsd_wait_for_creator(zone, t, lockp))
1200 		dropped = B_TRUE;
1201 
1202 	if (zsd_wait_for_inprogress(zone, t, lockp))
1203 		dropped = B_TRUE;
1204 
1205 	if (t->zsd_flags & ZSD_DESTROY_NEEDED) {
1206 		t->zsd_flags &= ~ZSD_DESTROY_NEEDED;
1207 		t->zsd_flags |= ZSD_DESTROY_INPROGRESS;
1208 		DTRACE_PROBE2(zsd__destroy__inprogress,
1209 		    zone_t *, zone, zone_key_t, key);
1210 		mutex_exit(&zone->zone_lock);
1211 		if (lockp != NULL)
1212 			mutex_exit(lockp);
1213 		dropped = B_TRUE;
1214 
1215 		ASSERT(t->zsd_destroy != NULL);
1216 		data = t->zsd_data;
1217 		DTRACE_PROBE2(zsd__destroy__start,
1218 		    zone_t *, zone, zone_key_t, key);
1219 
1220 		(t->zsd_destroy)(zone->zone_id, data);
1221 		DTRACE_PROBE2(zsd__destroy__end,
1222 		    zone_t *, zone, zone_key_t, key);
1223 
1224 		if (lockp != NULL)
1225 			mutex_enter(lockp);
1226 		mutex_enter(&zone->zone_lock);
1227 		t->zsd_data = NULL;
1228 		t->zsd_flags &= ~ZSD_DESTROY_INPROGRESS;
1229 		t->zsd_flags |= ZSD_DESTROY_COMPLETED;
1230 		cv_broadcast(&t->zsd_cv);
1231 		DTRACE_PROBE2(zsd__destroy__completed,
1232 		    zone_t *, zone, zone_key_t, key);
1233 	}
1234 	if (!zone_lock_held)
1235 		mutex_exit(&zone->zone_lock);
1236 	return (dropped);
1237 }
1238 
1239 /*
1240  * Wait for any CREATE_NEEDED flag to be cleared.
1241  * Returns true if lockp was temporarily dropped while waiting.
1242  */
1243 static boolean_t
1244 zsd_wait_for_creator(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
1245 {
1246 	boolean_t dropped = B_FALSE;
1247 
1248 	while (t->zsd_flags & ZSD_CREATE_NEEDED) {
1249 		DTRACE_PROBE2(zsd__wait__for__creator,
1250 		    zone_t *, zone, struct zsd_entry *, t);
1251 		if (lockp != NULL) {
1252 			dropped = B_TRUE;
1253 			mutex_exit(lockp);
1254 		}
1255 		cv_wait(&t->zsd_cv, &zone->zone_lock);
1256 		if (lockp != NULL) {
1257 			/* First drop zone_lock to preserve order */
1258 			mutex_exit(&zone->zone_lock);
1259 			mutex_enter(lockp);
1260 			mutex_enter(&zone->zone_lock);
1261 		}
1262 	}
1263 	return (dropped);
1264 }
1265 
1266 /*
1267  * Wait for any INPROGRESS flag to be cleared.
1268  * Returns true if lockp was temporarily dropped while waiting.
1269  */
1270 static boolean_t
1271 zsd_wait_for_inprogress(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
1272 {
1273 	boolean_t dropped = B_FALSE;
1274 
1275 	while (t->zsd_flags & ZSD_ALL_INPROGRESS) {
1276 		DTRACE_PROBE2(zsd__wait__for__inprogress,
1277 		    zone_t *, zone, struct zsd_entry *, t);
1278 		if (lockp != NULL) {
1279 			dropped = B_TRUE;
1280 			mutex_exit(lockp);
1281 		}
1282 		cv_wait(&t->zsd_cv, &zone->zone_lock);
1283 		if (lockp != NULL) {
1284 			/* First drop zone_lock to preserve order */
1285 			mutex_exit(&zone->zone_lock);
1286 			mutex_enter(lockp);
1287 			mutex_enter(&zone->zone_lock);
1288 		}
1289 	}
1290 	return (dropped);
1291 }
1292 
1293 /*
1294  * Frees memory associated with the zone dataset list.
1295  */
1296 static void
1297 zone_free_datasets(zone_t *zone)
1298 {
1299 	zone_dataset_t *t, *next;
1300 
1301 	for (t = list_head(&zone->zone_datasets); t != NULL; t = next) {
1302 		next = list_next(&zone->zone_datasets, t);
1303 		list_remove(&zone->zone_datasets, t);
1304 		kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1);
1305 		kmem_free(t, sizeof (*t));
1306 	}
1307 	list_destroy(&zone->zone_datasets);
1308 }
1309 
1310 /*
1311  * zone.cpu-shares resource control support.
1312  */
1313 /*ARGSUSED*/
1314 static rctl_qty_t
1315 zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
1316 {
1317 	ASSERT(MUTEX_HELD(&p->p_lock));
1318 	return (p->p_zone->zone_shares);
1319 }
1320 
1321 /*ARGSUSED*/
1322 static int
1323 zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1324     rctl_qty_t nv)
1325 {
1326 	ASSERT(MUTEX_HELD(&p->p_lock));
1327 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1328 	if (e->rcep_p.zone == NULL)
1329 		return (0);
1330 
1331 	e->rcep_p.zone->zone_shares = nv;
1332 	return (0);
1333 }
1334 
1335 static rctl_ops_t zone_cpu_shares_ops = {
1336 	rcop_no_action,
1337 	zone_cpu_shares_usage,
1338 	zone_cpu_shares_set,
1339 	rcop_no_test
1340 };
1341 
1342 /*
1343  * zone.cpu-cap resource control support.
1344  */
1345 /*ARGSUSED*/
1346 static rctl_qty_t
1347 zone_cpu_cap_get(rctl_t *rctl, struct proc *p)
1348 {
1349 	ASSERT(MUTEX_HELD(&p->p_lock));
1350 	return (cpucaps_zone_get(p->p_zone));
1351 }
1352 
1353 /*ARGSUSED*/
1354 static int
1355 zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1356     rctl_qty_t nv)
1357 {
1358 	zone_t *zone = e->rcep_p.zone;
1359 
1360 	ASSERT(MUTEX_HELD(&p->p_lock));
1361 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1362 
1363 	if (zone == NULL)
1364 		return (0);
1365 
1366 	/*
1367 	 * set cap to the new value.
1368 	 */
1369 	return (cpucaps_zone_set(zone, nv));
1370 }
1371 
1372 static rctl_ops_t zone_cpu_cap_ops = {
1373 	rcop_no_action,
1374 	zone_cpu_cap_get,
1375 	zone_cpu_cap_set,
1376 	rcop_no_test
1377 };
1378 
1379 /*ARGSUSED*/
1380 static rctl_qty_t
1381 zone_lwps_usage(rctl_t *r, proc_t *p)
1382 {
1383 	rctl_qty_t nlwps;
1384 	zone_t *zone = p->p_zone;
1385 
1386 	ASSERT(MUTEX_HELD(&p->p_lock));
1387 
1388 	mutex_enter(&zone->zone_nlwps_lock);
1389 	nlwps = zone->zone_nlwps;
1390 	mutex_exit(&zone->zone_nlwps_lock);
1391 
1392 	return (nlwps);
1393 }
1394 
1395 /*ARGSUSED*/
1396 static int
1397 zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
1398     rctl_qty_t incr, uint_t flags)
1399 {
1400 	rctl_qty_t nlwps;
1401 
1402 	ASSERT(MUTEX_HELD(&p->p_lock));
1403 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1404 	if (e->rcep_p.zone == NULL)
1405 		return (0);
1406 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
1407 	nlwps = e->rcep_p.zone->zone_nlwps;
1408 
1409 	if (nlwps + incr > rcntl->rcv_value)
1410 		return (1);
1411 
1412 	return (0);
1413 }
1414 
1415 /*ARGSUSED*/
1416 static int
1417 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
1418 {
1419 	ASSERT(MUTEX_HELD(&p->p_lock));
1420 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1421 	if (e->rcep_p.zone == NULL)
1422 		return (0);
1423 	e->rcep_p.zone->zone_nlwps_ctl = nv;
1424 	return (0);
1425 }
1426 
1427 static rctl_ops_t zone_lwps_ops = {
1428 	rcop_no_action,
1429 	zone_lwps_usage,
1430 	zone_lwps_set,
1431 	zone_lwps_test,
1432 };
1433 
1434 /*ARGSUSED*/
1435 static rctl_qty_t
1436 zone_procs_usage(rctl_t *r, proc_t *p)
1437 {
1438 	rctl_qty_t nprocs;
1439 	zone_t *zone = p->p_zone;
1440 
1441 	ASSERT(MUTEX_HELD(&p->p_lock));
1442 
1443 	mutex_enter(&zone->zone_nlwps_lock);
1444 	nprocs = zone->zone_nprocs;
1445 	mutex_exit(&zone->zone_nlwps_lock);
1446 
1447 	return (nprocs);
1448 }
1449 
1450 /*ARGSUSED*/
1451 static int
1452 zone_procs_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
1453     rctl_qty_t incr, uint_t flags)
1454 {
1455 	rctl_qty_t nprocs;
1456 
1457 	ASSERT(MUTEX_HELD(&p->p_lock));
1458 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1459 	if (e->rcep_p.zone == NULL)
1460 		return (0);
1461 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
1462 	nprocs = e->rcep_p.zone->zone_nprocs;
1463 
1464 	if (nprocs + incr > rcntl->rcv_value)
1465 		return (1);
1466 
1467 	return (0);
1468 }
1469 
1470 /*ARGSUSED*/
1471 static int
1472 zone_procs_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
1473 {
1474 	ASSERT(MUTEX_HELD(&p->p_lock));
1475 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1476 	if (e->rcep_p.zone == NULL)
1477 		return (0);
1478 	e->rcep_p.zone->zone_nprocs_ctl = nv;
1479 	return (0);
1480 }
1481 
1482 static rctl_ops_t zone_procs_ops = {
1483 	rcop_no_action,
1484 	zone_procs_usage,
1485 	zone_procs_set,
1486 	zone_procs_test,
1487 };
1488 
1489 /*ARGSUSED*/
1490 static rctl_qty_t
1491 zone_shmmax_usage(rctl_t *rctl, struct proc *p)
1492 {
1493 	ASSERT(MUTEX_HELD(&p->p_lock));
1494 	return (p->p_zone->zone_shmmax);
1495 }
1496 
1497 /*ARGSUSED*/
1498 static int
1499 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1500     rctl_qty_t incr, uint_t flags)
1501 {
1502 	rctl_qty_t v;
1503 	ASSERT(MUTEX_HELD(&p->p_lock));
1504 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1505 	v = e->rcep_p.zone->zone_shmmax + incr;
1506 	if (v > rval->rcv_value)
1507 		return (1);
1508 	return (0);
1509 }
1510 
1511 static rctl_ops_t zone_shmmax_ops = {
1512 	rcop_no_action,
1513 	zone_shmmax_usage,
1514 	rcop_no_set,
1515 	zone_shmmax_test
1516 };
1517 
1518 /*ARGSUSED*/
1519 static rctl_qty_t
1520 zone_shmmni_usage(rctl_t *rctl, struct proc *p)
1521 {
1522 	ASSERT(MUTEX_HELD(&p->p_lock));
1523 	return (p->p_zone->zone_ipc.ipcq_shmmni);
1524 }
1525 
1526 /*ARGSUSED*/
1527 static int
1528 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1529     rctl_qty_t incr, uint_t flags)
1530 {
1531 	rctl_qty_t v;
1532 	ASSERT(MUTEX_HELD(&p->p_lock));
1533 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1534 	v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr;
1535 	if (v > rval->rcv_value)
1536 		return (1);
1537 	return (0);
1538 }
1539 
1540 static rctl_ops_t zone_shmmni_ops = {
1541 	rcop_no_action,
1542 	zone_shmmni_usage,
1543 	rcop_no_set,
1544 	zone_shmmni_test
1545 };
1546 
1547 /*ARGSUSED*/
1548 static rctl_qty_t
1549 zone_semmni_usage(rctl_t *rctl, struct proc *p)
1550 {
1551 	ASSERT(MUTEX_HELD(&p->p_lock));
1552 	return (p->p_zone->zone_ipc.ipcq_semmni);
1553 }
1554 
1555 /*ARGSUSED*/
1556 static int
1557 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1558     rctl_qty_t incr, uint_t flags)
1559 {
1560 	rctl_qty_t v;
1561 	ASSERT(MUTEX_HELD(&p->p_lock));
1562 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1563 	v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr;
1564 	if (v > rval->rcv_value)
1565 		return (1);
1566 	return (0);
1567 }
1568 
1569 static rctl_ops_t zone_semmni_ops = {
1570 	rcop_no_action,
1571 	zone_semmni_usage,
1572 	rcop_no_set,
1573 	zone_semmni_test
1574 };
1575 
1576 /*ARGSUSED*/
1577 static rctl_qty_t
1578 zone_msgmni_usage(rctl_t *rctl, struct proc *p)
1579 {
1580 	ASSERT(MUTEX_HELD(&p->p_lock));
1581 	return (p->p_zone->zone_ipc.ipcq_msgmni);
1582 }
1583 
1584 /*ARGSUSED*/
1585 static int
1586 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1587     rctl_qty_t incr, uint_t flags)
1588 {
1589 	rctl_qty_t v;
1590 	ASSERT(MUTEX_HELD(&p->p_lock));
1591 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1592 	v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr;
1593 	if (v > rval->rcv_value)
1594 		return (1);
1595 	return (0);
1596 }
1597 
1598 static rctl_ops_t zone_msgmni_ops = {
1599 	rcop_no_action,
1600 	zone_msgmni_usage,
1601 	rcop_no_set,
1602 	zone_msgmni_test
1603 };
1604 
1605 /*ARGSUSED*/
1606 static rctl_qty_t
1607 zone_locked_mem_usage(rctl_t *rctl, struct proc *p)
1608 {
1609 	rctl_qty_t q;
1610 	ASSERT(MUTEX_HELD(&p->p_lock));
1611 	mutex_enter(&p->p_zone->zone_mem_lock);
1612 	q = p->p_zone->zone_locked_mem;
1613 	mutex_exit(&p->p_zone->zone_mem_lock);
1614 	return (q);
1615 }
1616 
1617 /*ARGSUSED*/
1618 static int
1619 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
1620     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
1621 {
1622 	rctl_qty_t q;
1623 	zone_t *z;
1624 
1625 	z = e->rcep_p.zone;
1626 	ASSERT(MUTEX_HELD(&p->p_lock));
1627 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
1628 	q = z->zone_locked_mem;
1629 	if (q + incr > rcntl->rcv_value)
1630 		return (1);
1631 	return (0);
1632 }
1633 
1634 /*ARGSUSED*/
1635 static int
1636 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1637     rctl_qty_t nv)
1638 {
1639 	ASSERT(MUTEX_HELD(&p->p_lock));
1640 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1641 	if (e->rcep_p.zone == NULL)
1642 		return (0);
1643 	e->rcep_p.zone->zone_locked_mem_ctl = nv;
1644 	return (0);
1645 }
1646 
1647 static rctl_ops_t zone_locked_mem_ops = {
1648 	rcop_no_action,
1649 	zone_locked_mem_usage,
1650 	zone_locked_mem_set,
1651 	zone_locked_mem_test
1652 };
1653 
1654 /*ARGSUSED*/
1655 static rctl_qty_t
1656 zone_max_swap_usage(rctl_t *rctl, struct proc *p)
1657 {
1658 	rctl_qty_t q;
1659 	zone_t *z = p->p_zone;
1660 
1661 	ASSERT(MUTEX_HELD(&p->p_lock));
1662 	mutex_enter(&z->zone_mem_lock);
1663 	q = z->zone_max_swap;
1664 	mutex_exit(&z->zone_mem_lock);
1665 	return (q);
1666 }
1667 
1668 /*ARGSUSED*/
1669 static int
1670 zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
1671     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
1672 {
1673 	rctl_qty_t q;
1674 	zone_t *z;
1675 
1676 	z = e->rcep_p.zone;
1677 	ASSERT(MUTEX_HELD(&p->p_lock));
1678 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
1679 	q = z->zone_max_swap;
1680 	if (q + incr > rcntl->rcv_value)
1681 		return (1);
1682 	return (0);
1683 }
1684 
1685 /*ARGSUSED*/
1686 static int
1687 zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1688     rctl_qty_t nv)
1689 {
1690 	ASSERT(MUTEX_HELD(&p->p_lock));
1691 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1692 	if (e->rcep_p.zone == NULL)
1693 		return (0);
1694 	e->rcep_p.zone->zone_max_swap_ctl = nv;
1695 	return (0);
1696 }
1697 
1698 static rctl_ops_t zone_max_swap_ops = {
1699 	rcop_no_action,
1700 	zone_max_swap_usage,
1701 	zone_max_swap_set,
1702 	zone_max_swap_test
1703 };
1704 
1705 /*ARGSUSED*/
1706 static rctl_qty_t
1707 zone_max_lofi_usage(rctl_t *rctl, struct proc *p)
1708 {
1709 	rctl_qty_t q;
1710 	zone_t *z = p->p_zone;
1711 
1712 	ASSERT(MUTEX_HELD(&p->p_lock));
1713 	mutex_enter(&z->zone_rctl_lock);
1714 	q = z->zone_max_lofi;
1715 	mutex_exit(&z->zone_rctl_lock);
1716 	return (q);
1717 }
1718 
1719 /*ARGSUSED*/
1720 static int
1721 zone_max_lofi_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
1722     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
1723 {
1724 	rctl_qty_t q;
1725 	zone_t *z;
1726 
1727 	z = e->rcep_p.zone;
1728 	ASSERT(MUTEX_HELD(&p->p_lock));
1729 	ASSERT(MUTEX_HELD(&z->zone_rctl_lock));
1730 	q = z->zone_max_lofi;
1731 	if (q + incr > rcntl->rcv_value)
1732 		return (1);
1733 	return (0);
1734 }
1735 
1736 /*ARGSUSED*/
1737 static int
1738 zone_max_lofi_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1739     rctl_qty_t nv)
1740 {
1741 	ASSERT(MUTEX_HELD(&p->p_lock));
1742 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1743 	if (e->rcep_p.zone == NULL)
1744 		return (0);
1745 	e->rcep_p.zone->zone_max_lofi_ctl = nv;
1746 	return (0);
1747 }
1748 
1749 static rctl_ops_t zone_max_lofi_ops = {
1750 	rcop_no_action,
1751 	zone_max_lofi_usage,
1752 	zone_max_lofi_set,
1753 	zone_max_lofi_test
1754 };
1755 
1756 /*
1757  * Helper function to brand the zone with a unique ID.
1758  */
1759 static void
1760 zone_uniqid(zone_t *zone)
1761 {
1762 	static uint64_t uniqid = 0;
1763 
1764 	ASSERT(MUTEX_HELD(&zonehash_lock));
1765 	zone->zone_uniqid = uniqid++;
1766 }
1767 
1768 /*
1769  * Returns a held pointer to the "kcred" for the specified zone.
1770  */
1771 struct cred *
1772 zone_get_kcred(zoneid_t zoneid)
1773 {
1774 	zone_t *zone;
1775 	cred_t *cr;
1776 
1777 	if ((zone = zone_find_by_id(zoneid)) == NULL)
1778 		return (NULL);
1779 	cr = zone->zone_kcred;
1780 	crhold(cr);
1781 	zone_rele(zone);
1782 	return (cr);
1783 }
1784 
1785 static int
1786 zone_lockedmem_kstat_update(kstat_t *ksp, int rw)
1787 {
1788 	zone_t *zone = ksp->ks_private;
1789 	zone_kstat_t *zk = ksp->ks_data;
1790 
1791 	if (rw == KSTAT_WRITE)
1792 		return (EACCES);
1793 
1794 	zk->zk_usage.value.ui64 = zone->zone_locked_mem;
1795 	zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl;
1796 	return (0);
1797 }
1798 
1799 static int
1800 zone_nprocs_kstat_update(kstat_t *ksp, int rw)
1801 {
1802 	zone_t *zone = ksp->ks_private;
1803 	zone_kstat_t *zk = ksp->ks_data;
1804 
1805 	if (rw == KSTAT_WRITE)
1806 		return (EACCES);
1807 
1808 	zk->zk_usage.value.ui64 = zone->zone_nprocs;
1809 	zk->zk_value.value.ui64 = zone->zone_nprocs_ctl;
1810 	return (0);
1811 }
1812 
1813 static int
1814 zone_swapresv_kstat_update(kstat_t *ksp, int rw)
1815 {
1816 	zone_t *zone = ksp->ks_private;
1817 	zone_kstat_t *zk = ksp->ks_data;
1818 
1819 	if (rw == KSTAT_WRITE)
1820 		return (EACCES);
1821 
1822 	zk->zk_usage.value.ui64 = zone->zone_max_swap;
1823 	zk->zk_value.value.ui64 = zone->zone_max_swap_ctl;
1824 	return (0);
1825 }
1826 
1827 static kstat_t *
1828 zone_kstat_create_common(zone_t *zone, char *name,
1829     int (*updatefunc) (kstat_t *, int))
1830 {
1831 	kstat_t *ksp;
1832 	zone_kstat_t *zk;
1833 
1834 	ksp = rctl_kstat_create_zone(zone, name, KSTAT_TYPE_NAMED,
1835 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
1836 	    KSTAT_FLAG_VIRTUAL);
1837 
1838 	if (ksp == NULL)
1839 		return (NULL);
1840 
1841 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
1842 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
1843 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
1844 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
1845 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
1846 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
1847 	ksp->ks_update = updatefunc;
1848 	ksp->ks_private = zone;
1849 	kstat_install(ksp);
1850 	return (ksp);
1851 }
1852 
1853 
1854 static int
1855 zone_mcap_kstat_update(kstat_t *ksp, int rw)
1856 {
1857 	zone_t *zone = ksp->ks_private;
1858 	zone_mcap_kstat_t *zmp = ksp->ks_data;
1859 
1860 	if (rw == KSTAT_WRITE)
1861 		return (EACCES);
1862 
1863 	zmp->zm_pgpgin.value.ui64 = zone->zone_pgpgin;
1864 	zmp->zm_anonpgin.value.ui64 = zone->zone_anonpgin;
1865 	zmp->zm_execpgin.value.ui64 = zone->zone_execpgin;
1866 	zmp->zm_fspgin.value.ui64 = zone->zone_fspgin;
1867 	zmp->zm_anon_alloc_fail.value.ui64 = zone->zone_anon_alloc_fail;
1868 
1869 	return (0);
1870 }
1871 
1872 static kstat_t *
1873 zone_mcap_kstat_create(zone_t *zone)
1874 {
1875 	kstat_t *ksp;
1876 	zone_mcap_kstat_t *zmp;
1877 
1878 	if ((ksp = kstat_create_zone("memory_cap", zone->zone_id,
1879 	    zone->zone_name, "zone_memory_cap", KSTAT_TYPE_NAMED,
1880 	    sizeof (zone_mcap_kstat_t) / sizeof (kstat_named_t),
1881 	    KSTAT_FLAG_VIRTUAL, zone->zone_id)) == NULL)
1882 		return (NULL);
1883 
1884 	if (zone->zone_id != GLOBAL_ZONEID)
1885 		kstat_zone_add(ksp, GLOBAL_ZONEID);
1886 
1887 	zmp = ksp->ks_data = kmem_zalloc(sizeof (zone_mcap_kstat_t), KM_SLEEP);
1888 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
1889 	ksp->ks_lock = &zone->zone_mcap_lock;
1890 	zone->zone_mcap_stats = zmp;
1891 
1892 	/* The kstat "name" field is not large enough for a full zonename */
1893 	kstat_named_init(&zmp->zm_zonename, "zonename", KSTAT_DATA_STRING);
1894 	kstat_named_setstr(&zmp->zm_zonename, zone->zone_name);
1895 	kstat_named_init(&zmp->zm_pgpgin, "pgpgin", KSTAT_DATA_UINT64);
1896 	kstat_named_init(&zmp->zm_anonpgin, "anonpgin", KSTAT_DATA_UINT64);
1897 	kstat_named_init(&zmp->zm_execpgin, "execpgin", KSTAT_DATA_UINT64);
1898 	kstat_named_init(&zmp->zm_fspgin, "fspgin", KSTAT_DATA_UINT64);
1899 	kstat_named_init(&zmp->zm_anon_alloc_fail, "anon_alloc_fail",
1900 	    KSTAT_DATA_UINT64);
1901 
1902 	ksp->ks_update = zone_mcap_kstat_update;
1903 	ksp->ks_private = zone;
1904 
1905 	kstat_install(ksp);
1906 	return (ksp);
1907 }
1908 
1909 static int
1910 zone_misc_kstat_update(kstat_t *ksp, int rw)
1911 {
1912 	zone_t *zone = ksp->ks_private;
1913 	zone_misc_kstat_t *zmp = ksp->ks_data;
1914 	hrtime_t tmp;
1915 
1916 	if (rw == KSTAT_WRITE)
1917 		return (EACCES);
1918 
1919 	tmp = zone->zone_utime;
1920 	scalehrtime(&tmp);
1921 	zmp->zm_utime.value.ui64 = tmp;
1922 	tmp = zone->zone_stime;
1923 	scalehrtime(&tmp);
1924 	zmp->zm_stime.value.ui64 = tmp;
1925 	tmp = zone->zone_wtime;
1926 	scalehrtime(&tmp);
1927 	zmp->zm_wtime.value.ui64 = tmp;
1928 
1929 	zmp->zm_avenrun1.value.ui32 = zone->zone_avenrun[0];
1930 	zmp->zm_avenrun5.value.ui32 = zone->zone_avenrun[1];
1931 	zmp->zm_avenrun15.value.ui32 = zone->zone_avenrun[2];
1932 
1933 	zmp->zm_ffcap.value.ui32 = zone->zone_ffcap;
1934 	zmp->zm_ffnoproc.value.ui32 = zone->zone_ffnoproc;
1935 	zmp->zm_ffnomem.value.ui32 = zone->zone_ffnomem;
1936 	zmp->zm_ffmisc.value.ui32 = zone->zone_ffmisc;
1937 
1938 	zmp->zm_nested_intp.value.ui32 = zone->zone_nested_intp;
1939 
1940 	zmp->zm_init_pid.value.ui32 = zone->zone_proc_initpid;
1941 	zmp->zm_boot_time.value.ui64 = (uint64_t)zone->zone_boot_time;
1942 
1943 	return (0);
1944 }
1945 
1946 static kstat_t *
1947 zone_misc_kstat_create(zone_t *zone)
1948 {
1949 	kstat_t *ksp;
1950 	zone_misc_kstat_t *zmp;
1951 
1952 	if ((ksp = kstat_create_zone("zones", zone->zone_id,
1953 	    zone->zone_name, "zone_misc", KSTAT_TYPE_NAMED,
1954 	    sizeof (zone_misc_kstat_t) / sizeof (kstat_named_t),
1955 	    KSTAT_FLAG_VIRTUAL, zone->zone_id)) == NULL)
1956 		return (NULL);
1957 
1958 	if (zone->zone_id != GLOBAL_ZONEID)
1959 		kstat_zone_add(ksp, GLOBAL_ZONEID);
1960 
1961 	zmp = ksp->ks_data = kmem_zalloc(sizeof (zone_misc_kstat_t), KM_SLEEP);
1962 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
1963 	ksp->ks_lock = &zone->zone_misc_lock;
1964 	zone->zone_misc_stats = zmp;
1965 
1966 	/* The kstat "name" field is not large enough for a full zonename */
1967 	kstat_named_init(&zmp->zm_zonename, "zonename", KSTAT_DATA_STRING);
1968 	kstat_named_setstr(&zmp->zm_zonename, zone->zone_name);
1969 	kstat_named_init(&zmp->zm_utime, "nsec_user", KSTAT_DATA_UINT64);
1970 	kstat_named_init(&zmp->zm_stime, "nsec_sys", KSTAT_DATA_UINT64);
1971 	kstat_named_init(&zmp->zm_wtime, "nsec_waitrq", KSTAT_DATA_UINT64);
1972 	kstat_named_init(&zmp->zm_avenrun1, "avenrun_1min", KSTAT_DATA_UINT32);
1973 	kstat_named_init(&zmp->zm_avenrun5, "avenrun_5min", KSTAT_DATA_UINT32);
1974 	kstat_named_init(&zmp->zm_avenrun15, "avenrun_15min",
1975 	    KSTAT_DATA_UINT32);
1976 	kstat_named_init(&zmp->zm_ffcap, "forkfail_cap", KSTAT_DATA_UINT32);
1977 	kstat_named_init(&zmp->zm_ffnoproc, "forkfail_noproc",
1978 	    KSTAT_DATA_UINT32);
1979 	kstat_named_init(&zmp->zm_ffnomem, "forkfail_nomem", KSTAT_DATA_UINT32);
1980 	kstat_named_init(&zmp->zm_ffmisc, "forkfail_misc", KSTAT_DATA_UINT32);
1981 	kstat_named_init(&zmp->zm_nested_intp, "nested_interp",
1982 	    KSTAT_DATA_UINT32);
1983 	kstat_named_init(&zmp->zm_init_pid, "init_pid", KSTAT_DATA_UINT32);
1984 	kstat_named_init(&zmp->zm_boot_time, "boot_time", KSTAT_DATA_UINT64);
1985 
1986 	ksp->ks_update = zone_misc_kstat_update;
1987 	ksp->ks_private = zone;
1988 
1989 	kstat_install(ksp);
1990 	return (ksp);
1991 }
1992 
1993 static void
1994 zone_kstat_create(zone_t *zone)
1995 {
1996 	zone->zone_lockedmem_kstat = zone_kstat_create_common(zone,
1997 	    "lockedmem", zone_lockedmem_kstat_update);
1998 	zone->zone_swapresv_kstat = zone_kstat_create_common(zone,
1999 	    "swapresv", zone_swapresv_kstat_update);
2000 	zone->zone_nprocs_kstat = zone_kstat_create_common(zone,
2001 	    "nprocs", zone_nprocs_kstat_update);
2002 
2003 	if ((zone->zone_mcap_ksp = zone_mcap_kstat_create(zone)) == NULL) {
2004 		zone->zone_mcap_stats = kmem_zalloc(
2005 		    sizeof (zone_mcap_kstat_t), KM_SLEEP);
2006 	}
2007 
2008 	if ((zone->zone_misc_ksp = zone_misc_kstat_create(zone)) == NULL) {
2009 		zone->zone_misc_stats = kmem_zalloc(
2010 		    sizeof (zone_misc_kstat_t), KM_SLEEP);
2011 	}
2012 }
2013 
2014 static void
2015 zone_kstat_delete_common(kstat_t **pkstat, size_t datasz)
2016 {
2017 	void *data;
2018 
2019 	if (*pkstat != NULL) {
2020 		data = (*pkstat)->ks_data;
2021 		kstat_delete(*pkstat);
2022 		kmem_free(data, datasz);
2023 		*pkstat = NULL;
2024 	}
2025 }
2026 
2027 static void
2028 zone_kstat_delete(zone_t *zone)
2029 {
2030 	zone_kstat_delete_common(&zone->zone_lockedmem_kstat,
2031 	    sizeof (zone_kstat_t));
2032 	zone_kstat_delete_common(&zone->zone_swapresv_kstat,
2033 	    sizeof (zone_kstat_t));
2034 	zone_kstat_delete_common(&zone->zone_nprocs_kstat,
2035 	    sizeof (zone_kstat_t));
2036 	zone_kstat_delete_common(&zone->zone_mcap_ksp,
2037 	    sizeof (zone_mcap_kstat_t));
2038 	zone_kstat_delete_common(&zone->zone_misc_ksp,
2039 	    sizeof (zone_misc_kstat_t));
2040 }
2041 
2042 /*
2043  * Called very early on in boot to initialize the ZSD list so that
2044  * zone_key_create() can be called before zone_init().  It also initializes
2045  * portions of zone0 which may be used before zone_init() is called.  The
2046  * variable "global_zone" will be set when zone0 is fully initialized by
2047  * zone_init().
2048  */
2049 void
2050 zone_zsd_init(void)
2051 {
2052 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
2053 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
2054 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
2055 	    offsetof(struct zsd_entry, zsd_linkage));
2056 	list_create(&zone_active, sizeof (zone_t),
2057 	    offsetof(zone_t, zone_linkage));
2058 	list_create(&zone_deathrow, sizeof (zone_t),
2059 	    offsetof(zone_t, zone_linkage));
2060 
2061 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
2062 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
2063 	mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
2064 	zone0.zone_shares = 1;
2065 	zone0.zone_nlwps = 0;
2066 	zone0.zone_nlwps_ctl = INT_MAX;
2067 	zone0.zone_nprocs = 0;
2068 	zone0.zone_nprocs_ctl = INT_MAX;
2069 	zone0.zone_locked_mem = 0;
2070 	zone0.zone_locked_mem_ctl = UINT64_MAX;
2071 	ASSERT(zone0.zone_max_swap == 0);
2072 	zone0.zone_max_swap_ctl = UINT64_MAX;
2073 	zone0.zone_max_lofi = 0;
2074 	zone0.zone_max_lofi_ctl = UINT64_MAX;
2075 	zone0.zone_shmmax = 0;
2076 	zone0.zone_ipc.ipcq_shmmni = 0;
2077 	zone0.zone_ipc.ipcq_semmni = 0;
2078 	zone0.zone_ipc.ipcq_msgmni = 0;
2079 	zone0.zone_name = GLOBAL_ZONENAME;
2080 	zone0.zone_nodename = utsname.nodename;
2081 	zone0.zone_domain = srpc_domain;
2082 	zone0.zone_hostid = HW_INVALID_HOSTID;
2083 	zone0.zone_fs_allowed = NULL;
2084 	psecflags_default(&zone0.zone_secflags);
2085 	zone0.zone_ref = 1;
2086 	zone0.zone_id = GLOBAL_ZONEID;
2087 	zone0.zone_status = ZONE_IS_RUNNING;
2088 	zone0.zone_rootpath = "/";
2089 	zone0.zone_rootpathlen = 2;
2090 	zone0.zone_psetid = ZONE_PS_INVAL;
2091 	zone0.zone_ncpus = 0;
2092 	zone0.zone_ncpus_online = 0;
2093 	zone0.zone_proc_initpid = 1;
2094 	zone0.zone_initname = initname;
2095 	zone0.zone_lockedmem_kstat = NULL;
2096 	zone0.zone_swapresv_kstat = NULL;
2097 	zone0.zone_nprocs_kstat = NULL;
2098 
2099 	zone0.zone_stime = 0;
2100 	zone0.zone_utime = 0;
2101 	zone0.zone_wtime = 0;
2102 
2103 	list_create(&zone0.zone_ref_list, sizeof (zone_ref_t),
2104 	    offsetof(zone_ref_t, zref_linkage));
2105 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
2106 	    offsetof(struct zsd_entry, zsd_linkage));
2107 	list_insert_head(&zone_active, &zone0);
2108 
2109 	/*
2110 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
2111 	 * to anything meaningful.  It is assigned to be 'rootdir' in
2112 	 * vfs_mountroot().
2113 	 */
2114 	zone0.zone_rootvp = NULL;
2115 	zone0.zone_vfslist = NULL;
2116 	zone0.zone_bootargs = initargs;
2117 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
2118 	/*
2119 	 * The global zone has all privileges
2120 	 */
2121 	priv_fillset(zone0.zone_privset);
2122 	/*
2123 	 * Add p0 to the global zone
2124 	 */
2125 	zone0.zone_zsched = &p0;
2126 	p0.p_zone = &zone0;
2127 }
2128 
2129 /*
2130  * Compute a hash value based on the contents of the label and the DOI.  The
2131  * hash algorithm is somewhat arbitrary, but is based on the observation that
2132  * humans will likely pick labels that differ by amounts that work out to be
2133  * multiples of the number of hash chains, and thus stirring in some primes
2134  * should help.
2135  */
2136 static uint_t
2137 hash_bylabel(void *hdata, mod_hash_key_t key)
2138 {
2139 	const ts_label_t *lab = (ts_label_t *)key;
2140 	const uint32_t *up, *ue;
2141 	uint_t hash;
2142 	int i;
2143 
2144 	_NOTE(ARGUNUSED(hdata));
2145 
2146 	hash = lab->tsl_doi + (lab->tsl_doi << 1);
2147 	/* we depend on alignment of label, but not representation */
2148 	up = (const uint32_t *)&lab->tsl_label;
2149 	ue = up + sizeof (lab->tsl_label) / sizeof (*up);
2150 	i = 1;
2151 	while (up < ue) {
2152 		/* using 2^n + 1, 1 <= n <= 16 as source of many primes */
2153 		hash += *up + (*up << ((i % 16) + 1));
2154 		up++;
2155 		i++;
2156 	}
2157 	return (hash);
2158 }
2159 
2160 /*
2161  * All that mod_hash cares about here is zero (equal) versus non-zero (not
2162  * equal).  This may need to be changed if less than / greater than is ever
2163  * needed.
2164  */
2165 static int
2166 hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
2167 {
2168 	ts_label_t *lab1 = (ts_label_t *)key1;
2169 	ts_label_t *lab2 = (ts_label_t *)key2;
2170 
2171 	return (label_equal(lab1, lab2) ? 0 : 1);
2172 }
2173 
2174 /*
2175  * Called by main() to initialize the zones framework.
2176  */
2177 void
2178 zone_init(void)
2179 {
2180 	rctl_dict_entry_t *rde;
2181 	rctl_val_t *dval;
2182 	rctl_set_t *set;
2183 	rctl_alloc_gp_t *gp;
2184 	rctl_entity_p_t e;
2185 	int res;
2186 
2187 	ASSERT(curproc == &p0);
2188 
2189 	/*
2190 	 * Create ID space for zone IDs.  ID 0 is reserved for the
2191 	 * global zone.
2192 	 */
2193 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
2194 
2195 	/*
2196 	 * Initialize generic zone resource controls, if any.
2197 	 */
2198 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
2199 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
2200 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
2201 	    FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops);
2202 
2203 	rc_zone_cpu_cap = rctl_register("zone.cpu-cap",
2204 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS |
2205 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER |
2206 	    RCTL_GLOBAL_INFINITE,
2207 	    MAXCAP, MAXCAP, &zone_cpu_cap_ops);
2208 
2209 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
2210 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
2211 	    INT_MAX, INT_MAX, &zone_lwps_ops);
2212 
2213 	rc_zone_nprocs = rctl_register("zone.max-processes", RCENTITY_ZONE,
2214 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
2215 	    INT_MAX, INT_MAX, &zone_procs_ops);
2216 
2217 	/*
2218 	 * System V IPC resource controls
2219 	 */
2220 	rc_zone_msgmni = rctl_register("zone.max-msg-ids",
2221 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2222 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops);
2223 
2224 	rc_zone_semmni = rctl_register("zone.max-sem-ids",
2225 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2226 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops);
2227 
2228 	rc_zone_shmmni = rctl_register("zone.max-shm-ids",
2229 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2230 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops);
2231 
2232 	rc_zone_shmmax = rctl_register("zone.max-shm-memory",
2233 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2234 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops);
2235 
2236 	/*
2237 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
2238 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
2239 	 */
2240 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
2241 	bzero(dval, sizeof (rctl_val_t));
2242 	dval->rcv_value = 1;
2243 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
2244 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
2245 	dval->rcv_action_recip_pid = -1;
2246 
2247 	rde = rctl_dict_lookup("zone.cpu-shares");
2248 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
2249 
2250 	rc_zone_locked_mem = rctl_register("zone.max-locked-memory",
2251 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
2252 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
2253 	    &zone_locked_mem_ops);
2254 
2255 	rc_zone_max_swap = rctl_register("zone.max-swap",
2256 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
2257 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
2258 	    &zone_max_swap_ops);
2259 
2260 	rc_zone_max_lofi = rctl_register("zone.max-lofi",
2261 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |
2262 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
2263 	    &zone_max_lofi_ops);
2264 
2265 	/*
2266 	 * Initialize the ``global zone''.
2267 	 */
2268 	set = rctl_set_create();
2269 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
2270 	mutex_enter(&p0.p_lock);
2271 	e.rcep_p.zone = &zone0;
2272 	e.rcep_t = RCENTITY_ZONE;
2273 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
2274 	    gp);
2275 
2276 	zone0.zone_nlwps = p0.p_lwpcnt;
2277 	zone0.zone_nprocs = 1;
2278 	zone0.zone_ntasks = 1;
2279 	mutex_exit(&p0.p_lock);
2280 	zone0.zone_restart_init = B_TRUE;
2281 	zone0.zone_brand = &native_brand;
2282 	rctl_prealloc_destroy(gp);
2283 	/*
2284 	 * pool_default hasn't been initialized yet, so we let pool_init()
2285 	 * take care of making sure the global zone is in the default pool.
2286 	 */
2287 
2288 	/*
2289 	 * Initialize global zone kstats
2290 	 */
2291 	zone_kstat_create(&zone0);
2292 
2293 	/*
2294 	 * Initialize zone label.
2295 	 * mlp are initialized when tnzonecfg is loaded.
2296 	 */
2297 	zone0.zone_slabel = l_admin_low;
2298 	rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
2299 	label_hold(l_admin_low);
2300 
2301 	/*
2302 	 * Initialise the lock for the database structure used by mntfs.
2303 	 */
2304 	rw_init(&zone0.zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL);
2305 
2306 	mutex_enter(&zonehash_lock);
2307 	zone_uniqid(&zone0);
2308 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
2309 
2310 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
2311 	    mod_hash_null_valdtor);
2312 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
2313 	    zone_hash_size, mod_hash_null_valdtor);
2314 	/*
2315 	 * maintain zonehashbylabel only for labeled systems
2316 	 */
2317 	if (is_system_labeled())
2318 		zonehashbylabel = mod_hash_create_extended("zone_by_label",
2319 		    zone_hash_size, mod_hash_null_keydtor,
2320 		    mod_hash_null_valdtor, hash_bylabel, NULL,
2321 		    hash_labelkey_cmp, KM_SLEEP);
2322 	zonecount = 1;
2323 
2324 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
2325 	    (mod_hash_val_t)&zone0);
2326 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
2327 	    (mod_hash_val_t)&zone0);
2328 	if (is_system_labeled()) {
2329 		zone0.zone_flags |= ZF_HASHED_LABEL;
2330 		(void) mod_hash_insert(zonehashbylabel,
2331 		    (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0);
2332 	}
2333 	mutex_exit(&zonehash_lock);
2334 
2335 	/*
2336 	 * We avoid setting zone_kcred until now, since kcred is initialized
2337 	 * sometime after zone_zsd_init() and before zone_init().
2338 	 */
2339 	zone0.zone_kcred = kcred;
2340 	/*
2341 	 * The global zone is fully initialized (except for zone_rootvp which
2342 	 * will be set when the root filesystem is mounted).
2343 	 */
2344 	global_zone = &zone0;
2345 
2346 	/*
2347 	 * Setup an event channel to send zone status change notifications on
2348 	 */
2349 	res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan,
2350 	    EVCH_CREAT);
2351 
2352 	if (res)
2353 		panic("Sysevent_evc_bind failed during zone setup.\n");
2354 
2355 }
2356 
2357 static void
2358 zone_free(zone_t *zone)
2359 {
2360 	ASSERT(zone != global_zone);
2361 	ASSERT(zone->zone_ntasks == 0);
2362 	ASSERT(zone->zone_nlwps == 0);
2363 	ASSERT(zone->zone_nprocs == 0);
2364 	ASSERT(zone->zone_cred_ref == 0);
2365 	ASSERT(zone->zone_kcred == NULL);
2366 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
2367 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
2368 	ASSERT(list_is_empty(&zone->zone_ref_list));
2369 
2370 	/*
2371 	 * Remove any zone caps.
2372 	 */
2373 	cpucaps_zone_remove(zone);
2374 
2375 	ASSERT(zone->zone_cpucap == NULL);
2376 
2377 	/* remove from deathrow list */
2378 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
2379 		ASSERT(zone->zone_ref == 0);
2380 		mutex_enter(&zone_deathrow_lock);
2381 		list_remove(&zone_deathrow, zone);
2382 		mutex_exit(&zone_deathrow_lock);
2383 	}
2384 
2385 	list_destroy(&zone->zone_ref_list);
2386 	zone_free_zsd(zone);
2387 	zone_free_datasets(zone);
2388 	list_destroy(&zone->zone_dl_list);
2389 
2390 	if (zone->zone_rootvp != NULL)
2391 		VN_RELE(zone->zone_rootvp);
2392 	if (zone->zone_rootpath)
2393 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
2394 	if (zone->zone_name != NULL)
2395 		kmem_free(zone->zone_name, ZONENAME_MAX);
2396 	if (zone->zone_slabel != NULL)
2397 		label_rele(zone->zone_slabel);
2398 	if (zone->zone_nodename != NULL)
2399 		kmem_free(zone->zone_nodename, _SYS_NMLN);
2400 	if (zone->zone_domain != NULL)
2401 		kmem_free(zone->zone_domain, _SYS_NMLN);
2402 	if (zone->zone_privset != NULL)
2403 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
2404 	if (zone->zone_rctls != NULL)
2405 		rctl_set_free(zone->zone_rctls);
2406 	if (zone->zone_bootargs != NULL)
2407 		strfree(zone->zone_bootargs);
2408 	if (zone->zone_initname != NULL)
2409 		strfree(zone->zone_initname);
2410 	if (zone->zone_fs_allowed != NULL)
2411 		strfree(zone->zone_fs_allowed);
2412 	if (zone->zone_pfexecd != NULL)
2413 		klpd_freelist(&zone->zone_pfexecd);
2414 	id_free(zoneid_space, zone->zone_id);
2415 	mutex_destroy(&zone->zone_lock);
2416 	cv_destroy(&zone->zone_cv);
2417 	rw_destroy(&zone->zone_mlps.mlpl_rwlock);
2418 	rw_destroy(&zone->zone_mntfs_db_lock);
2419 	kmem_free(zone, sizeof (zone_t));
2420 }
2421 
2422 /*
2423  * See block comment at the top of this file for information about zone
2424  * status values.
2425  */
2426 /*
2427  * Convenience function for setting zone status.
2428  */
2429 static void
2430 zone_status_set(zone_t *zone, zone_status_t status)
2431 {
2432 
2433 	nvlist_t *nvl = NULL;
2434 	ASSERT(MUTEX_HELD(&zone_status_lock));
2435 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
2436 	    status >= zone_status_get(zone));
2437 
2438 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) ||
2439 	    nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) ||
2440 	    nvlist_add_string(nvl, ZONE_CB_NEWSTATE,
2441 	    zone_status_table[status]) ||
2442 	    nvlist_add_string(nvl, ZONE_CB_OLDSTATE,
2443 	    zone_status_table[zone->zone_status]) ||
2444 	    nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) ||
2445 	    nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) ||
2446 	    sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS,
2447 	    ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) {
2448 #ifdef DEBUG
2449 		(void) printf(
2450 		    "Failed to allocate and send zone state change event.\n");
2451 #endif
2452 	}
2453 	nvlist_free(nvl);
2454 
2455 	zone->zone_status = status;
2456 
2457 	cv_broadcast(&zone->zone_cv);
2458 }
2459 
2460 /*
2461  * Public function to retrieve the zone status.  The zone status may
2462  * change after it is retrieved.
2463  */
2464 zone_status_t
2465 zone_status_get(zone_t *zone)
2466 {
2467 	return (zone->zone_status);
2468 }
2469 
2470 static int
2471 zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
2472 {
2473 	char *buf = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
2474 	int err = 0;
2475 
2476 	ASSERT(zone != global_zone);
2477 	if ((err = copyinstr(zone_bootargs, buf, BOOTARGS_MAX, NULL)) != 0)
2478 		goto done;	/* EFAULT or ENAMETOOLONG */
2479 
2480 	if (zone->zone_bootargs != NULL)
2481 		strfree(zone->zone_bootargs);
2482 
2483 	zone->zone_bootargs = strdup(buf);
2484 
2485 done:
2486 	kmem_free(buf, BOOTARGS_MAX);
2487 	return (err);
2488 }
2489 
2490 static int
2491 zone_set_brand(zone_t *zone, const char *brand)
2492 {
2493 	struct brand_attr *attrp;
2494 	brand_t *bp;
2495 
2496 	attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP);
2497 	if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) {
2498 		kmem_free(attrp, sizeof (struct brand_attr));
2499 		return (EFAULT);
2500 	}
2501 
2502 	bp = brand_register_zone(attrp);
2503 	kmem_free(attrp, sizeof (struct brand_attr));
2504 	if (bp == NULL)
2505 		return (EINVAL);
2506 
2507 	/*
2508 	 * This is the only place where a zone can change it's brand.
2509 	 * We already need to hold zone_status_lock to check the zone
2510 	 * status, so we'll just use that lock to serialize zone
2511 	 * branding requests as well.
2512 	 */
2513 	mutex_enter(&zone_status_lock);
2514 
2515 	/* Re-Branding is not allowed and the zone can't be booted yet */
2516 	if ((ZONE_IS_BRANDED(zone)) ||
2517 	    (zone_status_get(zone) >= ZONE_IS_BOOTING)) {
2518 		mutex_exit(&zone_status_lock);
2519 		brand_unregister_zone(bp);
2520 		return (EINVAL);
2521 	}
2522 
2523 	/* set up the brand specific data */
2524 	zone->zone_brand = bp;
2525 	ZBROP(zone)->b_init_brand_data(zone);
2526 
2527 	mutex_exit(&zone_status_lock);
2528 	return (0);
2529 }
2530 
2531 static int
2532 zone_set_secflags(zone_t *zone, const psecflags_t *zone_secflags)
2533 {
2534 	int err = 0;
2535 	psecflags_t psf;
2536 
2537 	ASSERT(zone != global_zone);
2538 
2539 	if ((err = copyin(zone_secflags, &psf, sizeof (psf))) != 0)
2540 		return (err);
2541 
2542 	if (zone_status_get(zone) > ZONE_IS_READY)
2543 		return (EINVAL);
2544 
2545 	if (!psecflags_validate(&psf))
2546 		return (EINVAL);
2547 
2548 	(void) memcpy(&zone->zone_secflags, &psf, sizeof (psf));
2549 
2550 	/* Set security flags on the zone's zsched */
2551 	(void) memcpy(&zone->zone_zsched->p_secflags, &zone->zone_secflags,
2552 	    sizeof (zone->zone_zsched->p_secflags));
2553 
2554 	return (0);
2555 }
2556 
2557 static int
2558 zone_set_fs_allowed(zone_t *zone, const char *zone_fs_allowed)
2559 {
2560 	char *buf = kmem_zalloc(ZONE_FS_ALLOWED_MAX, KM_SLEEP);
2561 	int err = 0;
2562 
2563 	ASSERT(zone != global_zone);
2564 	if ((err = copyinstr(zone_fs_allowed, buf,
2565 	    ZONE_FS_ALLOWED_MAX, NULL)) != 0)
2566 		goto done;
2567 
2568 	if (zone->zone_fs_allowed != NULL)
2569 		strfree(zone->zone_fs_allowed);
2570 
2571 	zone->zone_fs_allowed = strdup(buf);
2572 
2573 done:
2574 	kmem_free(buf, ZONE_FS_ALLOWED_MAX);
2575 	return (err);
2576 }
2577 
2578 static int
2579 zone_set_initname(zone_t *zone, const char *zone_initname)
2580 {
2581 	char initname[INITNAME_SZ];
2582 	size_t len;
2583 	int err = 0;
2584 
2585 	ASSERT(zone != global_zone);
2586 	if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0)
2587 		return (err);	/* EFAULT or ENAMETOOLONG */
2588 
2589 	if (zone->zone_initname != NULL)
2590 		strfree(zone->zone_initname);
2591 
2592 	zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP);
2593 	(void) strcpy(zone->zone_initname, initname);
2594 	return (0);
2595 }
2596 
2597 static int
2598 zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap)
2599 {
2600 	uint64_t mcap;
2601 	int err = 0;
2602 
2603 	if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0)
2604 		zone->zone_phys_mcap = mcap;
2605 
2606 	return (err);
2607 }
2608 
2609 static int
2610 zone_set_sched_class(zone_t *zone, const char *new_class)
2611 {
2612 	char sched_class[PC_CLNMSZ];
2613 	id_t classid;
2614 	int err;
2615 
2616 	ASSERT(zone != global_zone);
2617 	if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0)
2618 		return (err);	/* EFAULT or ENAMETOOLONG */
2619 
2620 	if (getcid(sched_class, &classid) != 0 || CLASS_KERNEL(classid))
2621 		return (set_errno(EINVAL));
2622 	zone->zone_defaultcid = classid;
2623 	ASSERT(zone->zone_defaultcid > 0 &&
2624 	    zone->zone_defaultcid < loaded_classes);
2625 
2626 	return (0);
2627 }
2628 
2629 /*
2630  * Block indefinitely waiting for (zone_status >= status)
2631  */
2632 void
2633 zone_status_wait(zone_t *zone, zone_status_t status)
2634 {
2635 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
2636 
2637 	mutex_enter(&zone_status_lock);
2638 	while (zone->zone_status < status) {
2639 		cv_wait(&zone->zone_cv, &zone_status_lock);
2640 	}
2641 	mutex_exit(&zone_status_lock);
2642 }
2643 
2644 /*
2645  * Private CPR-safe version of zone_status_wait().
2646  */
2647 static void
2648 zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
2649 {
2650 	callb_cpr_t cprinfo;
2651 
2652 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
2653 
2654 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
2655 	    str);
2656 	mutex_enter(&zone_status_lock);
2657 	while (zone->zone_status < status) {
2658 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2659 		cv_wait(&zone->zone_cv, &zone_status_lock);
2660 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
2661 	}
2662 	/*
2663 	 * zone_status_lock is implicitly released by the following.
2664 	 */
2665 	CALLB_CPR_EXIT(&cprinfo);
2666 }
2667 
2668 /*
2669  * Block until zone enters requested state or signal is received.  Return (0)
2670  * if signaled, non-zero otherwise.
2671  */
2672 int
2673 zone_status_wait_sig(zone_t *zone, zone_status_t status)
2674 {
2675 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
2676 
2677 	mutex_enter(&zone_status_lock);
2678 	while (zone->zone_status < status) {
2679 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
2680 			mutex_exit(&zone_status_lock);
2681 			return (0);
2682 		}
2683 	}
2684 	mutex_exit(&zone_status_lock);
2685 	return (1);
2686 }
2687 
2688 /*
2689  * Block until the zone enters the requested state or the timeout expires,
2690  * whichever happens first.  Return (-1) if operation timed out, time remaining
2691  * otherwise.
2692  */
2693 clock_t
2694 zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
2695 {
2696 	clock_t timeleft = 0;
2697 
2698 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
2699 
2700 	mutex_enter(&zone_status_lock);
2701 	while (zone->zone_status < status && timeleft != -1) {
2702 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
2703 	}
2704 	mutex_exit(&zone_status_lock);
2705 	return (timeleft);
2706 }
2707 
2708 /*
2709  * Block until the zone enters the requested state, the current process is
2710  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
2711  * operation timed out, 0 if signaled, time remaining otherwise.
2712  */
2713 clock_t
2714 zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
2715 {
2716 	clock_t timeleft = tim - ddi_get_lbolt();
2717 
2718 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
2719 
2720 	mutex_enter(&zone_status_lock);
2721 	while (zone->zone_status < status) {
2722 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
2723 		    tim);
2724 		if (timeleft <= 0)
2725 			break;
2726 	}
2727 	mutex_exit(&zone_status_lock);
2728 	return (timeleft);
2729 }
2730 
2731 /*
2732  * Zones have two reference counts: one for references from credential
2733  * structures (zone_cred_ref), and one (zone_ref) for everything else.
2734  * This is so we can allow a zone to be rebooted while there are still
2735  * outstanding cred references, since certain drivers cache dblks (which
2736  * implicitly results in cached creds).  We wait for zone_ref to drop to
2737  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
2738  * later freed when the zone_cred_ref drops to 0, though nothing other
2739  * than the zone id and privilege set should be accessed once the zone
2740  * is "dead".
2741  *
2742  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
2743  * to force halt/reboot to block waiting for the zone_cred_ref to drop
2744  * to 0.  This can be useful to flush out other sources of cached creds
2745  * that may be less innocuous than the driver case.
2746  *
2747  * Zones also provide a tracked reference counting mechanism in which zone
2748  * references are represented by "crumbs" (zone_ref structures).  Crumbs help
2749  * debuggers determine the sources of leaked zone references.  See
2750  * zone_hold_ref() and zone_rele_ref() below for more information.
2751  */
2752 
2753 int zone_wait_for_cred = 0;
2754 
2755 static void
2756 zone_hold_locked(zone_t *z)
2757 {
2758 	ASSERT(MUTEX_HELD(&z->zone_lock));
2759 	z->zone_ref++;
2760 	ASSERT(z->zone_ref != 0);
2761 }
2762 
2763 /*
2764  * Increment the specified zone's reference count.  The zone's zone_t structure
2765  * will not be freed as long as the zone's reference count is nonzero.
2766  * Decrement the zone's reference count via zone_rele().
2767  *
2768  * NOTE: This function should only be used to hold zones for short periods of
2769  * time.  Use zone_hold_ref() if the zone must be held for a long time.
2770  */
2771 void
2772 zone_hold(zone_t *z)
2773 {
2774 	mutex_enter(&z->zone_lock);
2775 	zone_hold_locked(z);
2776 	mutex_exit(&z->zone_lock);
2777 }
2778 
2779 /*
2780  * If the non-cred ref count drops to 1 and either the cred ref count
2781  * is 0 or we aren't waiting for cred references, the zone is ready to
2782  * be destroyed.
2783  */
2784 #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
2785 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
2786 
2787 /*
2788  * Common zone reference release function invoked by zone_rele() and
2789  * zone_rele_ref().  If subsys is ZONE_REF_NUM_SUBSYS, then the specified
2790  * zone's subsystem-specific reference counters are not affected by the
2791  * release.  If ref is not NULL, then the zone_ref_t to which it refers is
2792  * removed from the specified zone's reference list.  ref must be non-NULL iff
2793  * subsys is not ZONE_REF_NUM_SUBSYS.
2794  */
2795 static void
2796 zone_rele_common(zone_t *z, zone_ref_t *ref, zone_ref_subsys_t subsys)
2797 {
2798 	boolean_t wakeup;
2799 
2800 	mutex_enter(&z->zone_lock);
2801 	ASSERT(z->zone_ref != 0);
2802 	z->zone_ref--;
2803 	if (subsys != ZONE_REF_NUM_SUBSYS) {
2804 		ASSERT(z->zone_subsys_ref[subsys] != 0);
2805 		z->zone_subsys_ref[subsys]--;
2806 		list_remove(&z->zone_ref_list, ref);
2807 	}
2808 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
2809 		/* no more refs, free the structure */
2810 		mutex_exit(&z->zone_lock);
2811 		zone_free(z);
2812 		return;
2813 	}
2814 	/* signal zone_destroy so the zone can finish halting */
2815 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
2816 	mutex_exit(&z->zone_lock);
2817 
2818 	if (wakeup) {
2819 		/*
2820 		 * Grabbing zonehash_lock here effectively synchronizes with
2821 		 * zone_destroy() to avoid missed signals.
2822 		 */
2823 		mutex_enter(&zonehash_lock);
2824 		cv_broadcast(&zone_destroy_cv);
2825 		mutex_exit(&zonehash_lock);
2826 	}
2827 }
2828 
2829 /*
2830  * Decrement the specified zone's reference count.  The specified zone will
2831  * cease to exist after this function returns if the reference count drops to
2832  * zero.  This function should be paired with zone_hold().
2833  */
2834 void
2835 zone_rele(zone_t *z)
2836 {
2837 	zone_rele_common(z, NULL, ZONE_REF_NUM_SUBSYS);
2838 }
2839 
2840 /*
2841  * Initialize a zone reference structure.  This function must be invoked for
2842  * a reference structure before the structure is passed to zone_hold_ref().
2843  */
2844 void
2845 zone_init_ref(zone_ref_t *ref)
2846 {
2847 	ref->zref_zone = NULL;
2848 	list_link_init(&ref->zref_linkage);
2849 }
2850 
2851 /*
2852  * Acquire a reference to zone z.  The caller must specify the
2853  * zone_ref_subsys_t constant associated with its subsystem.  The specified
2854  * zone_ref_t structure will represent a reference to the specified zone.  Use
2855  * zone_rele_ref() to release the reference.
2856  *
2857  * The referenced zone_t structure will not be freed as long as the zone_t's
2858  * zone_status field is not ZONE_IS_DEAD and the zone has outstanding
2859  * references.
2860  *
2861  * NOTE: The zone_ref_t structure must be initialized before it is used.
2862  * See zone_init_ref() above.
2863  */
2864 void
2865 zone_hold_ref(zone_t *z, zone_ref_t *ref, zone_ref_subsys_t subsys)
2866 {
2867 	ASSERT(subsys >= 0 && subsys < ZONE_REF_NUM_SUBSYS);
2868 
2869 	/*
2870 	 * Prevent consumers from reusing a reference structure before
2871 	 * releasing it.
2872 	 */
2873 	VERIFY(ref->zref_zone == NULL);
2874 
2875 	ref->zref_zone = z;
2876 	mutex_enter(&z->zone_lock);
2877 	zone_hold_locked(z);
2878 	z->zone_subsys_ref[subsys]++;
2879 	ASSERT(z->zone_subsys_ref[subsys] != 0);
2880 	list_insert_head(&z->zone_ref_list, ref);
2881 	mutex_exit(&z->zone_lock);
2882 }
2883 
2884 /*
2885  * Release the zone reference represented by the specified zone_ref_t.
2886  * The reference is invalid after it's released; however, the zone_ref_t
2887  * structure can be reused without having to invoke zone_init_ref().
2888  * subsys should be the same value that was passed to zone_hold_ref()
2889  * when the reference was acquired.
2890  */
2891 void
2892 zone_rele_ref(zone_ref_t *ref, zone_ref_subsys_t subsys)
2893 {
2894 	zone_rele_common(ref->zref_zone, ref, subsys);
2895 
2896 	/*
2897 	 * Set the zone_ref_t's zref_zone field to NULL to generate panics
2898 	 * when consumers dereference the reference.  This helps us catch
2899 	 * consumers who use released references.  Furthermore, this lets
2900 	 * consumers reuse the zone_ref_t structure without having to
2901 	 * invoke zone_init_ref().
2902 	 */
2903 	ref->zref_zone = NULL;
2904 }
2905 
2906 void
2907 zone_cred_hold(zone_t *z)
2908 {
2909 	mutex_enter(&z->zone_lock);
2910 	z->zone_cred_ref++;
2911 	ASSERT(z->zone_cred_ref != 0);
2912 	mutex_exit(&z->zone_lock);
2913 }
2914 
2915 void
2916 zone_cred_rele(zone_t *z)
2917 {
2918 	boolean_t wakeup;
2919 
2920 	mutex_enter(&z->zone_lock);
2921 	ASSERT(z->zone_cred_ref != 0);
2922 	z->zone_cred_ref--;
2923 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
2924 		/* no more refs, free the structure */
2925 		mutex_exit(&z->zone_lock);
2926 		zone_free(z);
2927 		return;
2928 	}
2929 	/*
2930 	 * If zone_destroy is waiting for the cred references to drain
2931 	 * out, and they have, signal it.
2932 	 */
2933 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
2934 	    zone_status_get(z) >= ZONE_IS_DEAD);
2935 	mutex_exit(&z->zone_lock);
2936 
2937 	if (wakeup) {
2938 		/*
2939 		 * Grabbing zonehash_lock here effectively synchronizes with
2940 		 * zone_destroy() to avoid missed signals.
2941 		 */
2942 		mutex_enter(&zonehash_lock);
2943 		cv_broadcast(&zone_destroy_cv);
2944 		mutex_exit(&zonehash_lock);
2945 	}
2946 }
2947 
2948 void
2949 zone_task_hold(zone_t *z)
2950 {
2951 	mutex_enter(&z->zone_lock);
2952 	z->zone_ntasks++;
2953 	ASSERT(z->zone_ntasks != 0);
2954 	mutex_exit(&z->zone_lock);
2955 }
2956 
2957 void
2958 zone_task_rele(zone_t *zone)
2959 {
2960 	uint_t refcnt;
2961 
2962 	mutex_enter(&zone->zone_lock);
2963 	ASSERT(zone->zone_ntasks != 0);
2964 	refcnt = --zone->zone_ntasks;
2965 	if (refcnt > 1)	{	/* Common case */
2966 		mutex_exit(&zone->zone_lock);
2967 		return;
2968 	}
2969 	zone_hold_locked(zone);	/* so we can use the zone_t later */
2970 	mutex_exit(&zone->zone_lock);
2971 	if (refcnt == 1) {
2972 		/*
2973 		 * See if the zone is shutting down.
2974 		 */
2975 		mutex_enter(&zone_status_lock);
2976 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
2977 			goto out;
2978 		}
2979 
2980 		/*
2981 		 * Make sure the ntasks didn't change since we
2982 		 * dropped zone_lock.
2983 		 */
2984 		mutex_enter(&zone->zone_lock);
2985 		if (refcnt != zone->zone_ntasks) {
2986 			mutex_exit(&zone->zone_lock);
2987 			goto out;
2988 		}
2989 		mutex_exit(&zone->zone_lock);
2990 
2991 		/*
2992 		 * No more user processes in the zone.  The zone is empty.
2993 		 */
2994 		zone_status_set(zone, ZONE_IS_EMPTY);
2995 		goto out;
2996 	}
2997 
2998 	ASSERT(refcnt == 0);
2999 	/*
3000 	 * zsched has exited; the zone is dead.
3001 	 */
3002 	zone->zone_zsched = NULL;		/* paranoia */
3003 	mutex_enter(&zone_status_lock);
3004 	zone_status_set(zone, ZONE_IS_DEAD);
3005 out:
3006 	mutex_exit(&zone_status_lock);
3007 	zone_rele(zone);
3008 }
3009 
3010 zoneid_t
3011 getzoneid(void)
3012 {
3013 	return (curproc->p_zone->zone_id);
3014 }
3015 
3016 /*
3017  * Internal versions of zone_find_by_*().  These don't zone_hold() or
3018  * check the validity of a zone's state.
3019  */
3020 static zone_t *
3021 zone_find_all_by_id(zoneid_t zoneid)
3022 {
3023 	mod_hash_val_t hv;
3024 	zone_t *zone = NULL;
3025 
3026 	ASSERT(MUTEX_HELD(&zonehash_lock));
3027 
3028 	if (mod_hash_find(zonehashbyid,
3029 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
3030 		zone = (zone_t *)hv;
3031 	return (zone);
3032 }
3033 
3034 static zone_t *
3035 zone_find_all_by_label(const ts_label_t *label)
3036 {
3037 	mod_hash_val_t hv;
3038 	zone_t *zone = NULL;
3039 
3040 	ASSERT(MUTEX_HELD(&zonehash_lock));
3041 
3042 	/*
3043 	 * zonehashbylabel is not maintained for unlabeled systems
3044 	 */
3045 	if (!is_system_labeled())
3046 		return (NULL);
3047 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
3048 		zone = (zone_t *)hv;
3049 	return (zone);
3050 }
3051 
3052 static zone_t *
3053 zone_find_all_by_name(char *name)
3054 {
3055 	mod_hash_val_t hv;
3056 	zone_t *zone = NULL;
3057 
3058 	ASSERT(MUTEX_HELD(&zonehash_lock));
3059 
3060 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
3061 		zone = (zone_t *)hv;
3062 	return (zone);
3063 }
3064 
3065 /*
3066  * Public interface for looking up a zone by zoneid.  Only returns the zone if
3067  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
3068  * Caller must call zone_rele() once it is done with the zone.
3069  *
3070  * The zone may begin the zone_destroy() sequence immediately after this
3071  * function returns, but may be safely used until zone_rele() is called.
3072  */
3073 zone_t *
3074 zone_find_by_id(zoneid_t zoneid)
3075 {
3076 	zone_t *zone;
3077 	zone_status_t status;
3078 
3079 	mutex_enter(&zonehash_lock);
3080 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
3081 		mutex_exit(&zonehash_lock);
3082 		return (NULL);
3083 	}
3084 	status = zone_status_get(zone);
3085 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
3086 		/*
3087 		 * For all practical purposes the zone doesn't exist.
3088 		 */
3089 		mutex_exit(&zonehash_lock);
3090 		return (NULL);
3091 	}
3092 	zone_hold(zone);
3093 	mutex_exit(&zonehash_lock);
3094 	return (zone);
3095 }
3096 
3097 /*
3098  * Similar to zone_find_by_id, but using zone label as the key.
3099  */
3100 zone_t *
3101 zone_find_by_label(const ts_label_t *label)
3102 {
3103 	zone_t *zone;
3104 	zone_status_t status;
3105 
3106 	mutex_enter(&zonehash_lock);
3107 	if ((zone = zone_find_all_by_label(label)) == NULL) {
3108 		mutex_exit(&zonehash_lock);
3109 		return (NULL);
3110 	}
3111 
3112 	status = zone_status_get(zone);
3113 	if (status > ZONE_IS_DOWN) {
3114 		/*
3115 		 * For all practical purposes the zone doesn't exist.
3116 		 */
3117 		mutex_exit(&zonehash_lock);
3118 		return (NULL);
3119 	}
3120 	zone_hold(zone);
3121 	mutex_exit(&zonehash_lock);
3122 	return (zone);
3123 }
3124 
3125 /*
3126  * Similar to zone_find_by_id, but using zone name as the key.
3127  */
3128 zone_t *
3129 zone_find_by_name(char *name)
3130 {
3131 	zone_t *zone;
3132 	zone_status_t status;
3133 
3134 	mutex_enter(&zonehash_lock);
3135 	if ((zone = zone_find_all_by_name(name)) == NULL) {
3136 		mutex_exit(&zonehash_lock);
3137 		return (NULL);
3138 	}
3139 	status = zone_status_get(zone);
3140 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
3141 		/*
3142 		 * For all practical purposes the zone doesn't exist.
3143 		 */
3144 		mutex_exit(&zonehash_lock);
3145 		return (NULL);
3146 	}
3147 	zone_hold(zone);
3148 	mutex_exit(&zonehash_lock);
3149 	return (zone);
3150 }
3151 
3152 /*
3153  * Similar to zone_find_by_id(), using the path as a key.  For instance,
3154  * if there is a zone "foo" rooted at /foo/root, and the path argument
3155  * is "/foo/root/proc", it will return the held zone_t corresponding to
3156  * zone "foo".
3157  *
3158  * zone_find_by_path() always returns a non-NULL value, since at the
3159  * very least every path will be contained in the global zone.
3160  *
3161  * As with the other zone_find_by_*() functions, the caller is
3162  * responsible for zone_rele()ing the return value of this function.
3163  */
3164 zone_t *
3165 zone_find_by_path(const char *path)
3166 {
3167 	zone_t *zone;
3168 	zone_t *zret = NULL;
3169 	zone_status_t status;
3170 
3171 	if (path == NULL) {
3172 		/*
3173 		 * Call from rootconf().
3174 		 */
3175 		zone_hold(global_zone);
3176 		return (global_zone);
3177 	}
3178 	ASSERT(*path == '/');
3179 	mutex_enter(&zonehash_lock);
3180 	for (zone = list_head(&zone_active); zone != NULL;
3181 	    zone = list_next(&zone_active, zone)) {
3182 		if (ZONE_PATH_VISIBLE(path, zone))
3183 			zret = zone;
3184 	}
3185 	ASSERT(zret != NULL);
3186 	status = zone_status_get(zret);
3187 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
3188 		/*
3189 		 * Zone practically doesn't exist.
3190 		 */
3191 		zret = global_zone;
3192 	}
3193 	zone_hold(zret);
3194 	mutex_exit(&zonehash_lock);
3195 	return (zret);
3196 }
3197 
3198 /*
3199  * Public interface for updating per-zone load averages.  Called once per
3200  * second.
3201  *
3202  * Based on loadavg_update(), genloadavg() and calcloadavg() from clock.c.
3203  */
3204 void
3205 zone_loadavg_update()
3206 {
3207 	zone_t *zp;
3208 	zone_status_t status;
3209 	struct loadavg_s *lavg;
3210 	hrtime_t zone_total;
3211 	int i;
3212 	hrtime_t hr_avg;
3213 	int nrun;
3214 	static int64_t f[3] = { 135, 27, 9 };
3215 	int64_t q, r;
3216 
3217 	mutex_enter(&zonehash_lock);
3218 	for (zp = list_head(&zone_active); zp != NULL;
3219 	    zp = list_next(&zone_active, zp)) {
3220 		mutex_enter(&zp->zone_lock);
3221 
3222 		/* Skip zones that are on the way down or not yet up */
3223 		status = zone_status_get(zp);
3224 		if (status < ZONE_IS_READY || status >= ZONE_IS_DOWN) {
3225 			/* For all practical purposes the zone doesn't exist. */
3226 			mutex_exit(&zp->zone_lock);
3227 			continue;
3228 		}
3229 
3230 		/*
3231 		 * Update the 10 second moving average data in zone_loadavg.
3232 		 */
3233 		lavg = &zp->zone_loadavg;
3234 
3235 		zone_total = zp->zone_utime + zp->zone_stime + zp->zone_wtime;
3236 		scalehrtime(&zone_total);
3237 
3238 		/* The zone_total should always be increasing. */
3239 		lavg->lg_loads[lavg->lg_cur] = (zone_total > lavg->lg_total) ?
3240 		    zone_total - lavg->lg_total : 0;
3241 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
3242 		/* lg_total holds the prev. 1 sec. total */
3243 		lavg->lg_total = zone_total;
3244 
3245 		/*
3246 		 * To simplify the calculation, we don't calculate the load avg.
3247 		 * until the zone has been up for at least 10 seconds and our
3248 		 * moving average is thus full.
3249 		 */
3250 		if ((lavg->lg_len + 1) < S_LOADAVG_SZ) {
3251 			lavg->lg_len++;
3252 			mutex_exit(&zp->zone_lock);
3253 			continue;
3254 		}
3255 
3256 		/* Now calculate the 1min, 5min, 15 min load avg. */
3257 		hr_avg = 0;
3258 		for (i = 0; i < S_LOADAVG_SZ; i++)
3259 			hr_avg += lavg->lg_loads[i];
3260 		hr_avg = hr_avg / S_LOADAVG_SZ;
3261 		nrun = hr_avg / (NANOSEC / LGRP_LOADAVG_IN_THREAD_MAX);
3262 
3263 		/* Compute load avg. See comment in calcloadavg() */
3264 		for (i = 0; i < 3; i++) {
3265 			q = (zp->zone_hp_avenrun[i] >> 16) << 7;
3266 			r = (zp->zone_hp_avenrun[i] & 0xffff) << 7;
3267 			zp->zone_hp_avenrun[i] +=
3268 			    ((nrun - q) * f[i] - ((r * f[i]) >> 16)) >> 4;
3269 
3270 			/* avenrun[] can only hold 31 bits of load avg. */
3271 			if (zp->zone_hp_avenrun[i] <
3272 			    ((uint64_t)1<<(31+16-FSHIFT)))
3273 				zp->zone_avenrun[i] = (int32_t)
3274 				    (zp->zone_hp_avenrun[i] >> (16 - FSHIFT));
3275 			else
3276 				zp->zone_avenrun[i] = 0x7fffffff;
3277 		}
3278 
3279 		mutex_exit(&zp->zone_lock);
3280 	}
3281 	mutex_exit(&zonehash_lock);
3282 }
3283 
3284 /*
3285  * Get the number of cpus visible to this zone.  The system-wide global
3286  * 'ncpus' is returned if pools are disabled, the caller is in the
3287  * global zone, or a NULL zone argument is passed in.
3288  */
3289 int
3290 zone_ncpus_get(zone_t *zone)
3291 {
3292 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
3293 
3294 	return (myncpus != 0 ? myncpus : ncpus);
3295 }
3296 
3297 /*
3298  * Get the number of online cpus visible to this zone.  The system-wide
3299  * global 'ncpus_online' is returned if pools are disabled, the caller
3300  * is in the global zone, or a NULL zone argument is passed in.
3301  */
3302 int
3303 zone_ncpus_online_get(zone_t *zone)
3304 {
3305 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
3306 
3307 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
3308 }
3309 
3310 /*
3311  * Return the pool to which the zone is currently bound.
3312  */
3313 pool_t *
3314 zone_pool_get(zone_t *zone)
3315 {
3316 	ASSERT(pool_lock_held());
3317 
3318 	return (zone->zone_pool);
3319 }
3320 
3321 /*
3322  * Set the zone's pool pointer and update the zone's visibility to match
3323  * the resources in the new pool.
3324  */
3325 void
3326 zone_pool_set(zone_t *zone, pool_t *pool)
3327 {
3328 	ASSERT(pool_lock_held());
3329 	ASSERT(MUTEX_HELD(&cpu_lock));
3330 
3331 	zone->zone_pool = pool;
3332 	zone_pset_set(zone, pool->pool_pset->pset_id);
3333 }
3334 
3335 /*
3336  * Return the cached value of the id of the processor set to which the
3337  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
3338  * facility is disabled.
3339  */
3340 psetid_t
3341 zone_pset_get(zone_t *zone)
3342 {
3343 	ASSERT(MUTEX_HELD(&cpu_lock));
3344 
3345 	return (zone->zone_psetid);
3346 }
3347 
3348 /*
3349  * Set the cached value of the id of the processor set to which the zone
3350  * is currently bound.  Also update the zone's visibility to match the
3351  * resources in the new processor set.
3352  */
3353 void
3354 zone_pset_set(zone_t *zone, psetid_t newpsetid)
3355 {
3356 	psetid_t oldpsetid;
3357 
3358 	ASSERT(MUTEX_HELD(&cpu_lock));
3359 	oldpsetid = zone_pset_get(zone);
3360 
3361 	if (oldpsetid == newpsetid)
3362 		return;
3363 	/*
3364 	 * Global zone sees all.
3365 	 */
3366 	if (zone != global_zone) {
3367 		zone->zone_psetid = newpsetid;
3368 		if (newpsetid != ZONE_PS_INVAL)
3369 			pool_pset_visibility_add(newpsetid, zone);
3370 		if (oldpsetid != ZONE_PS_INVAL)
3371 			pool_pset_visibility_remove(oldpsetid, zone);
3372 	}
3373 	/*
3374 	 * Disabling pools, so we should start using the global values
3375 	 * for ncpus and ncpus_online.
3376 	 */
3377 	if (newpsetid == ZONE_PS_INVAL) {
3378 		zone->zone_ncpus = 0;
3379 		zone->zone_ncpus_online = 0;
3380 	}
3381 }
3382 
3383 /*
3384  * Walk the list of active zones and issue the provided callback for
3385  * each of them.
3386  *
3387  * Caller must not be holding any locks that may be acquired under
3388  * zonehash_lock.  See comment at the beginning of the file for a list of
3389  * common locks and their interactions with zones.
3390  */
3391 int
3392 zone_walk(int (*cb)(zone_t *, void *), void *data)
3393 {
3394 	zone_t *zone;
3395 	int ret = 0;
3396 	zone_status_t status;
3397 
3398 	mutex_enter(&zonehash_lock);
3399 	for (zone = list_head(&zone_active); zone != NULL;
3400 	    zone = list_next(&zone_active, zone)) {
3401 		/*
3402 		 * Skip zones that shouldn't be externally visible.
3403 		 */
3404 		status = zone_status_get(zone);
3405 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
3406 			continue;
3407 		/*
3408 		 * Bail immediately if any callback invocation returns a
3409 		 * non-zero value.
3410 		 */
3411 		ret = (*cb)(zone, data);
3412 		if (ret != 0)
3413 			break;
3414 	}
3415 	mutex_exit(&zonehash_lock);
3416 	return (ret);
3417 }
3418 
3419 static int
3420 zone_set_root(zone_t *zone, const char *upath)
3421 {
3422 	vnode_t *vp;
3423 	int trycount;
3424 	int error = 0;
3425 	char *path;
3426 	struct pathname upn, pn;
3427 	size_t pathlen;
3428 
3429 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
3430 		return (error);
3431 
3432 	pn_alloc(&pn);
3433 
3434 	/* prevent infinite loop */
3435 	trycount = 10;
3436 	for (;;) {
3437 		if (--trycount <= 0) {
3438 			error = ESTALE;
3439 			goto out;
3440 		}
3441 
3442 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
3443 			/*
3444 			 * VOP_ACCESS() may cover 'vp' with a new
3445 			 * filesystem, if 'vp' is an autoFS vnode.
3446 			 * Get the new 'vp' if so.
3447 			 */
3448 			if ((error =
3449 			    VOP_ACCESS(vp,