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