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