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