Print this page
fixup .text where possible
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/zcons.c
+++ new/usr/src/uts/common/io/zcons.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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26
27 27 /*
28 28 * Zone Console Driver.
29 29 *
30 30 * This driver, derived from the pts/ptm drivers, is the pseudo console driver
31 31 * for system zones. Its implementation is straightforward. Each instance
32 32 * of the driver represents a global-zone/local-zone pair (this maps in a
33 33 * straightforward way to the commonly used terminal notion of "master side"
34 34 * and "slave side", and we use that terminology throughout).
35 35 *
36 36 * Instances of zcons are onlined as children of /pseudo/zconsnex@1/
37 37 * by zoneadmd in userland, using the devctl framework; thus the driver
38 38 * does not need to maintain any sort of "admin" node.
39 39 *
40 40 * The driver shuttles I/O from master side to slave side and back. In a break
41 41 * from the pts/ptm semantics, if one side is not open, I/O directed towards
42 42 * it will simply be discarded. This is so that if zoneadmd is not holding
43 43 * the master side console open (i.e. it has died somehow), processes in
44 44 * the zone do not experience any errors and I/O to the console does not
45 45 * hang.
46 46 *
47 47 * TODO: we may want to revisit the other direction; i.e. we may want
48 48 * zoneadmd to be able to detect whether no zone processes are holding the
49 49 * console open, an unusual situation.
50 50 *
51 51 *
52 52 *
53 53 * MASTER SIDE IOCTLS
54 54 *
55 55 * The ZC_HOLDSLAVE and ZC_RELEASESLAVE ioctls instruct the master side of the
56 56 * console to hold and release a reference to the slave side's vnode. They are
57 57 * meant to be issued by zoneadmd after the console device node is created and
58 58 * before it is destroyed so that the slave's STREAMS anchor, ptem, is
59 59 * preserved when ttymon starts popping STREAMS modules from within the
60 60 * associated zone. This guarantees that the zone console will always have
61 61 * terminal semantics while the zone is running.
62 62 *
63 63 * Here is the issue: the ptem module is anchored in the zone console
64 64 * (slave side) so that processes within the associated non-global zone will
65 65 * fail to pop it off, thus ensuring that the slave will retain terminal
66 66 * semantics. When a process attempts to pop the anchor off of a stream, the
67 67 * STREAMS subsystem checks whether the calling process' zone is the same as
68 68 * that of the process that pushed the anchor onto the stream and cancels the
69 69 * pop if they differ. zoneadmd used to hold an open file descriptor for the
70 70 * slave while the associated non-global zone ran, thus ensuring that the
71 71 * slave's STREAMS anchor would never be popped from within the non-global zone
72 72 * (because zoneadmd runs in the global zone). However, this file descriptor
73 73 * was removed to make zone console management more robust. sad(7D) is now
74 74 * used to automatically set up the slave's STREAMS modules when the zone
75 75 * console is freshly opened within the associated non-global zone. However,
76 76 * when a process within the non-global zone freshly opens the zone console, the
77 77 * anchor is pushed from within the non-global zone, making it possible for
78 78 * processes within the non-global zone (e.g., ttymon) to pop the anchor and
79 79 * destroy the zone console's terminal semantics.
80 80 *
81 81 * One solution is to make the zcons device hold the slave open while the
82 82 * associated non-global zone runs so that the STREAMS anchor will always be
83 83 * associated with the global zone. Unfortunately, the slave cannot be opened
84 84 * from within the zcons driver because the driver is not reentrant: it has
85 85 * an outer STREAMS perimeter. Therefore, the next best option is for zcons to
86 86 * provide an ioctl interface to zoneadmd to manage holding and releasing
87 87 * the slave side of the console. It is sufficient to hold the slave side's
88 88 * vnode and bump the associated snode's reference count to preserve the slave's
89 89 * STREAMS configuration while the associated zone runs, so that's what the
90 90 * ioctls do.
91 91 *
92 92 *
93 93 * ZC_HOLDSLAVE
94 94 *
95 95 * This ioctl takes a file descriptor as an argument. It effectively gets a
96 96 * reference to the slave side's minor node's vnode and bumps the associated
97 97 * snode's reference count. The vnode reference is stored in the zcons device
98 98 * node's soft state. This ioctl succeeds if the given file descriptor refers
99 99 * to the slave side's minor node or if there is already a reference to the
100 100 * slave side's minor node's vnode in the device's soft state.
101 101 *
102 102 *
103 103 * ZC_RELEASESLAVE
104 104 *
105 105 * This ioctl takes a file descriptor as an argument. It effectively releases
106 106 * the vnode reference stored in the zcons device node's soft state (which was
107 107 * previously acquired via ZC_HOLDSLAVE) and decrements the reference count of
108 108 * the snode associated with the vnode. This ioctl succeeds if the given file
109 109 * descriptor refers to the slave side's minor node or if no reference to the
110 110 * slave side's minor node's vnode is stored in the device's soft state.
111 111 *
112 112 *
113 113 * Note that the file descriptor arguments for both ioctls must be cast to
114 114 * integers of pointer width.
115 115 *
116 116 * Here's how the dance between zcons and zoneadmd works:
117 117 *
118 118 * Zone boot:
119 119 * 1. While booting the zone, zoneadmd creates an instance of zcons.
120 120 * 2. zoneadmd opens the master and slave sides of the new zone console
121 121 * and issues the ZC_HOLDSLAVE ioctl on the master side, passing its
122 122 * file descriptor for the slave side as the ioctl argument.
123 123 * 3. zcons holds the slave side's vnode, bumps the snode's reference
124 124 * count, and stores a pointer to the vnode in the device's soft
125 125 * state.
126 126 * 4. zoneadmd closes the master and slave sides and continues to boot
127 127 * the zone.
128 128 *
129 129 * Zone halt:
130 130 * 1. While halting the zone, zoneadmd opens the master and slave sides
131 131 * of the zone's console and issues the ZC_RELEASESLAVE ioctl on the
132 132 * master side, passing its file descriptor for the slave side as the
133 133 * ioctl argument.
134 134 * 2. zcons decrements the slave side's snode's reference count, releases
135 135 * the slave's vnode, and eliminates its reference to the vnode in the
136 136 * device's soft state.
137 137 * 3. zoneadmd closes the master and slave sides.
138 138 * 4. zoneadmd destroys the zcons device and continues to halt the zone.
139 139 *
140 140 * It is necessary for zoneadmd to hold the slave open while issuing
141 141 * ZC_RELEASESLAVE because zcons might otherwise release the last reference to
142 142 * the slave's vnode. If it does, then specfs will panic because it will expect
143 143 * that the STREAMS configuration for the vnode was destroyed, which VN_RELE
144 144 * doesn't do. Forcing zoneadmd to hold the slave open guarantees that zcons
145 145 * won't release the vnode's last reference. zoneadmd will properly destroy the
146 146 * vnode and the snode when it closes the file descriptor.
147 147 *
148 148 * Technically, any process that can access the master side can issue these
149 149 * ioctls, but they should be treated as private interfaces for zoneadmd.
150 150 */
151 151
152 152 #include <sys/types.h>
153 153 #include <sys/cmn_err.h>
154 154 #include <sys/conf.h>
155 155 #include <sys/cred.h>
156 156 #include <sys/ddi.h>
157 157 #include <sys/debug.h>
158 158 #include <sys/devops.h>
159 159 #include <sys/errno.h>
160 160 #include <sys/file.h>
161 161 #include <sys/kstr.h>
162 162 #include <sys/modctl.h>
163 163 #include <sys/param.h>
164 164 #include <sys/stat.h>
165 165 #include <sys/stream.h>
166 166 #include <sys/stropts.h>
167 167 #include <sys/strsun.h>
168 168 #include <sys/sunddi.h>
169 169 #include <sys/sysmacros.h>
170 170 #include <sys/systm.h>
171 171 #include <sys/types.h>
172 172 #include <sys/zcons.h>
173 173 #include <sys/vnode.h>
174 174 #include <sys/fs/snode.h>
175 175 #include <sys/zone.h>
176 176
177 177 static int zc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
178 178 static int zc_attach(dev_info_t *, ddi_attach_cmd_t);
179 179 static int zc_detach(dev_info_t *, ddi_detach_cmd_t);
180 180
181 181 static int zc_open(queue_t *, dev_t *, int, int, cred_t *);
182 182 static int zc_close(queue_t *, int, cred_t *);
183 183 static void zc_wput(queue_t *, mblk_t *);
184 184 static void zc_rsrv(queue_t *);
185 185 static void zc_wsrv(queue_t *);
186 186
187 187 /*
188 188 * The instance number is encoded in the dev_t in the minor number; the lowest
189 189 * bit of the minor number is used to track the master vs. slave side of the
190 190 * virtual console. The rest of the bits in the minor number are the instance.
191 191 */
192 192 #define ZC_MASTER_MINOR 0
193 193 #define ZC_SLAVE_MINOR 1
194 194
195 195 #define ZC_INSTANCE(x) (getminor((x)) >> 1)
196 196 #define ZC_NODE(x) (getminor((x)) & 0x01)
197 197
198 198 /*
199 199 * This macro converts a zc_state_t pointer to the associated slave minor node's
200 200 * dev_t.
201 201 */
202 202 #define ZC_STATE_TO_SLAVEDEV(x) (makedevice(ddi_driver_major((x)->zc_devinfo), \
203 203 (minor_t)(ddi_get_instance((x)->zc_devinfo) << 1 | ZC_SLAVE_MINOR)))
204 204
205 205 int zcons_debug = 0;
206 206 #define DBG(a) if (zcons_debug) cmn_err(CE_NOTE, a)
207 207 #define DBG1(a, b) if (zcons_debug) cmn_err(CE_NOTE, a, b)
208 208
209 209
210 210 /*
211 211 * Zone Console Pseudo Terminal Module: stream data structure definitions
212 212 */
213 213 static struct module_info zc_info = {
214 214 31337, /* c0z we r hAx0rs */
215 215 "zcons",
216 216 0,
217 217 INFPSZ,
218 218 2048,
219 219 128
220 220 };
221 221
222 222 static struct qinit zc_rinit = {
223 223 NULL,
224 224 (int (*)()) zc_rsrv,
225 225 zc_open,
226 226 zc_close,
227 227 NULL,
228 228 &zc_info,
229 229 NULL
230 230 };
231 231
232 232 static struct qinit zc_winit = {
233 233 (int (*)()) zc_wput,
234 234 (int (*)()) zc_wsrv,
235 235 NULL,
236 236 NULL,
237 237 NULL,
238 238 &zc_info,
239 239 NULL
240 240 };
241 241
242 242 static struct streamtab zc_tab_info = {
243 243 &zc_rinit,
244 244 &zc_winit,
245 245 NULL,
246 246 NULL
247 247 };
248 248
249 249 #define ZC_CONF_FLAG (D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL)
250 250
251 251 /*
252 252 * this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops)
253 253 */
254 254 DDI_DEFINE_STREAM_OPS(zc_ops, nulldev, nulldev, zc_attach, zc_detach, nodev, \
255 255 zc_getinfo, ZC_CONF_FLAG, &zc_tab_info, ddi_quiesce_not_needed);
256 256
257 257 /*
258 258 * Module linkage information for the kernel.
↓ open down ↓ |
258 lines elided |
↑ open up ↑ |
259 259 */
260 260
261 261 static struct modldrv modldrv = {
262 262 &mod_driverops, /* Type of module (this is a pseudo driver) */
263 263 "Zone console driver", /* description of module */
264 264 &zc_ops /* driver ops */
265 265 };
266 266
267 267 static struct modlinkage modlinkage = {
268 268 MODREV_1,
269 - &modldrv,
270 - NULL
269 + { &modldrv,
270 + NULL }
271 271 };
272 272
273 273 typedef struct zc_state {
274 274 dev_info_t *zc_devinfo;
275 275 queue_t *zc_master_rdq;
276 276 queue_t *zc_slave_rdq;
277 277 vnode_t *zc_slave_vnode;
278 278 int zc_state;
279 279 } zc_state_t;
280 280
281 281 #define ZC_STATE_MOPEN 0x01
282 282 #define ZC_STATE_SOPEN 0x02
283 283
284 284 static void *zc_soft_state;
285 285
286 286 /*
287 287 * List of STREAMS modules that should be pushed onto every slave instance.
288 288 */
289 289 static char *zcons_mods[] = {
290 290 "ptem",
291 291 "ldterm",
292 292 "ttcompat",
293 293 NULL
294 294 };
295 295
296 296 int
297 297 _init(void)
298 298 {
299 299 int err;
300 300
301 301 if ((err = ddi_soft_state_init(&zc_soft_state,
302 302 sizeof (zc_state_t), 0)) != 0) {
303 303 return (err);
304 304 }
305 305
306 306 if ((err = mod_install(&modlinkage)) != 0)
307 307 ddi_soft_state_fini(zc_soft_state);
308 308
309 309 return (err);
310 310 }
311 311
312 312
313 313 int
314 314 _fini(void)
315 315 {
316 316 int err;
317 317
318 318 if ((err = mod_remove(&modlinkage)) != 0) {
319 319 return (err);
320 320 }
321 321
322 322 ddi_soft_state_fini(&zc_soft_state);
323 323 return (0);
324 324 }
325 325
326 326 int
327 327 _info(struct modinfo *modinfop)
328 328 {
329 329 return (mod_info(&modlinkage, modinfop));
330 330 }
331 331
332 332 static int
333 333 zc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
334 334 {
335 335 zc_state_t *zcs;
336 336 int instance;
337 337
338 338 if (cmd != DDI_ATTACH)
339 339 return (DDI_FAILURE);
340 340
341 341 instance = ddi_get_instance(dip);
342 342 if (ddi_soft_state_zalloc(zc_soft_state, instance) != DDI_SUCCESS)
343 343 return (DDI_FAILURE);
344 344
345 345 /*
346 346 * Create the master and slave minor nodes.
347 347 */
348 348 if ((ddi_create_minor_node(dip, ZCONS_SLAVE_NAME, S_IFCHR,
349 349 instance << 1 | ZC_SLAVE_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE) ||
350 350 (ddi_create_minor_node(dip, ZCONS_MASTER_NAME, S_IFCHR,
351 351 instance << 1 | ZC_MASTER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE)) {
352 352 ddi_remove_minor_node(dip, NULL);
353 353 ddi_soft_state_free(zc_soft_state, instance);
354 354 return (DDI_FAILURE);
355 355 }
356 356
357 357 VERIFY((zcs = ddi_get_soft_state(zc_soft_state, instance)) != NULL);
358 358 zcs->zc_devinfo = dip;
359 359 return (DDI_SUCCESS);
360 360 }
361 361
362 362 static int
363 363 zc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
364 364 {
365 365 zc_state_t *zcs;
366 366 int instance;
367 367
368 368 if (cmd != DDI_DETACH)
369 369 return (DDI_FAILURE);
370 370
371 371 instance = ddi_get_instance(dip);
372 372 if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
373 373 return (DDI_FAILURE);
374 374
375 375 if ((zcs->zc_state & ZC_STATE_MOPEN) ||
376 376 (zcs->zc_state & ZC_STATE_SOPEN)) {
377 377 DBG1("zc_detach: device (dip=%p) still open\n", (void *)dip);
378 378 return (DDI_FAILURE);
379 379 }
380 380
381 381 ddi_remove_minor_node(dip, NULL);
382 382 ddi_soft_state_free(zc_soft_state, instance);
383 383
384 384 return (DDI_SUCCESS);
385 385 }
386 386
387 387 /*
388 388 * zc_getinfo()
389 389 * getinfo(9e) entrypoint.
390 390 */
391 391 /*ARGSUSED*/
392 392 static int
393 393 zc_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
394 394 {
395 395 zc_state_t *zcs;
396 396 int instance = ZC_INSTANCE((dev_t)arg);
397 397
398 398 switch (infocmd) {
399 399 case DDI_INFO_DEVT2DEVINFO:
400 400 if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
401 401 return (DDI_FAILURE);
402 402 *result = zcs->zc_devinfo;
403 403 return (DDI_SUCCESS);
404 404 case DDI_INFO_DEVT2INSTANCE:
405 405 *result = (void *)(uintptr_t)instance;
406 406 return (DDI_SUCCESS);
407 407 }
408 408 return (DDI_FAILURE);
409 409 }
410 410
411 411 /*
412 412 * Return the equivalent queue from the other side of the relationship.
413 413 * e.g.: given the slave's write queue, return the master's write queue.
414 414 */
415 415 static queue_t *
416 416 zc_switch(queue_t *qp)
417 417 {
418 418 zc_state_t *zcs = qp->q_ptr;
419 419 ASSERT(zcs != NULL);
420 420
421 421 if (qp == zcs->zc_master_rdq)
422 422 return (zcs->zc_slave_rdq);
423 423 else if (OTHERQ(qp) == zcs->zc_master_rdq && zcs->zc_slave_rdq != NULL)
424 424 return (OTHERQ(zcs->zc_slave_rdq));
425 425 else if (qp == zcs->zc_slave_rdq)
426 426 return (zcs->zc_master_rdq);
427 427 else if (OTHERQ(qp) == zcs->zc_slave_rdq && zcs->zc_master_rdq != NULL)
428 428 return (OTHERQ(zcs->zc_master_rdq));
429 429 else
430 430 return (NULL);
431 431 }
432 432
433 433 /*
434 434 * For debugging and outputting messages. Returns the name of the side of
435 435 * the relationship associated with this queue.
436 436 */
437 437 static const char *
438 438 zc_side(queue_t *qp)
439 439 {
440 440 zc_state_t *zcs = qp->q_ptr;
441 441 ASSERT(zcs != NULL);
442 442
443 443 if (qp == zcs->zc_master_rdq ||
444 444 OTHERQ(qp) == zcs->zc_master_rdq) {
445 445 return ("master");
446 446 }
447 447 ASSERT(qp == zcs->zc_slave_rdq || OTHERQ(qp) == zcs->zc_slave_rdq);
448 448 return ("slave");
449 449 }
450 450
451 451 /*ARGSUSED*/
452 452 static int
453 453 zc_master_open(zc_state_t *zcs,
454 454 queue_t *rqp, /* pointer to the read side queue */
455 455 dev_t *devp, /* pointer to stream tail's dev */
456 456 int oflag, /* the user open(2) supplied flags */
457 457 int sflag, /* open state flag */
458 458 cred_t *credp) /* credentials */
459 459 {
460 460 mblk_t *mop;
461 461 struct stroptions *sop;
462 462
463 463 /*
464 464 * Enforce exclusivity on the master side; the only consumer should
465 465 * be the zoneadmd for the zone.
466 466 */
467 467 if ((zcs->zc_state & ZC_STATE_MOPEN) != 0)
468 468 return (EBUSY);
469 469
470 470 if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
471 471 DBG("zc_master_open(): mop allocation failed\n");
472 472 return (ENOMEM);
473 473 }
474 474
475 475 zcs->zc_state |= ZC_STATE_MOPEN;
476 476
477 477 /*
478 478 * q_ptr stores driver private data; stash the soft state data on both
479 479 * read and write sides of the queue.
480 480 */
481 481 WR(rqp)->q_ptr = rqp->q_ptr = zcs;
482 482 qprocson(rqp);
483 483
484 484 /*
485 485 * Following qprocson(), the master side is fully plumbed into the
486 486 * STREAM and may send/receive messages. Setting zcs->zc_master_rdq
487 487 * will allow the slave to send messages to us (the master).
488 488 * This cannot occur before qprocson() because the master is not
489 489 * ready to process them until that point.
490 490 */
491 491 zcs->zc_master_rdq = rqp;
492 492
493 493 /*
494 494 * set up hi/lo water marks on stream head read queue and add
495 495 * controlling tty as needed.
496 496 */
497 497 mop->b_datap->db_type = M_SETOPTS;
498 498 mop->b_wptr += sizeof (struct stroptions);
499 499 sop = (struct stroptions *)(void *)mop->b_rptr;
500 500 if (oflag & FNOCTTY)
501 501 sop->so_flags = SO_HIWAT | SO_LOWAT;
502 502 else
503 503 sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
504 504 sop->so_hiwat = 512;
505 505 sop->so_lowat = 256;
506 506 putnext(rqp, mop);
507 507
508 508 return (0);
509 509 }
510 510
511 511 /*ARGSUSED*/
512 512 static int
513 513 zc_slave_open(zc_state_t *zcs,
514 514 queue_t *rqp, /* pointer to the read side queue */
515 515 dev_t *devp, /* pointer to stream tail's dev */
516 516 int oflag, /* the user open(2) supplied flags */
517 517 int sflag, /* open state flag */
518 518 cred_t *credp) /* credentials */
519 519 {
520 520 mblk_t *mop;
521 521 struct stroptions *sop;
522 522 major_t major;
523 523 minor_t minor;
524 524 minor_t lastminor;
525 525 uint_t anchorindex;
526 526
527 527 /*
528 528 * The slave side can be opened as many times as needed.
529 529 */
530 530 if ((zcs->zc_state & ZC_STATE_SOPEN) != 0) {
531 531 ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zcs));
532 532 return (0);
533 533 }
534 534
535 535 /*
536 536 * Set up sad(7D) so that the necessary STREAMS modules will be in
537 537 * place. A wrinkle is that 'ptem' must be anchored
538 538 * in place (see streamio(7i)) because we always want the console to
539 539 * have terminal semantics.
540 540 */
541 541 minor = ddi_get_instance(zcs->zc_devinfo) << 1 | ZC_SLAVE_MINOR;
542 542 major = ddi_driver_major(zcs->zc_devinfo);
543 543 lastminor = 0;
544 544 anchorindex = 1;
545 545 if (kstr_autopush(SET_AUTOPUSH, &major, &minor, &lastminor,
546 546 &anchorindex, zcons_mods) != 0) {
547 547 DBG("zc_slave_open(): kstr_autopush() failed\n");
548 548 return (EIO);
549 549 }
550 550
551 551 if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
552 552 DBG("zc_slave_open(): mop allocation failed\n");
553 553 return (ENOMEM);
554 554 }
555 555
556 556 zcs->zc_state |= ZC_STATE_SOPEN;
557 557
558 558 /*
559 559 * q_ptr stores driver private data; stash the soft state data on both
560 560 * read and write sides of the queue.
561 561 */
562 562 WR(rqp)->q_ptr = rqp->q_ptr = zcs;
563 563
564 564 qprocson(rqp);
565 565
566 566 /*
567 567 * Must follow qprocson(), since we aren't ready to process until then.
568 568 */
569 569 zcs->zc_slave_rdq = rqp;
570 570
571 571 /*
572 572 * set up hi/lo water marks on stream head read queue and add
573 573 * controlling tty as needed.
574 574 */
575 575 mop->b_datap->db_type = M_SETOPTS;
576 576 mop->b_wptr += sizeof (struct stroptions);
577 577 sop = (struct stroptions *)(void *)mop->b_rptr;
578 578 sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
579 579 sop->so_hiwat = 512;
580 580 sop->so_lowat = 256;
581 581 putnext(rqp, mop);
582 582
583 583 return (0);
584 584 }
585 585
586 586 /*
587 587 * open(9e) entrypoint; checks sflag, and rejects anything unordinary.
588 588 */
589 589 static int
590 590 zc_open(queue_t *rqp, /* pointer to the read side queue */
591 591 dev_t *devp, /* pointer to stream tail's dev */
592 592 int oflag, /* the user open(2) supplied flags */
593 593 int sflag, /* open state flag */
594 594 cred_t *credp) /* credentials */
595 595 {
596 596 int instance = ZC_INSTANCE(*devp);
597 597 int ret;
598 598 zc_state_t *zcs;
599 599
600 600 if (sflag != 0)
601 601 return (EINVAL);
602 602
603 603 if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
604 604 return (ENXIO);
605 605
606 606 switch (ZC_NODE(*devp)) {
607 607 case ZC_MASTER_MINOR:
608 608 ret = zc_master_open(zcs, rqp, devp, oflag, sflag, credp);
609 609 break;
610 610 case ZC_SLAVE_MINOR:
611 611 ret = zc_slave_open(zcs, rqp, devp, oflag, sflag, credp);
612 612 break;
613 613 default:
614 614 ret = ENXIO;
615 615 break;
616 616 }
617 617
618 618 return (ret);
619 619 }
620 620
621 621 /*
622 622 * close(9e) entrypoint.
623 623 */
624 624 /*ARGSUSED1*/
625 625 static int
626 626 zc_close(queue_t *rqp, int flag, cred_t *credp)
627 627 {
628 628 queue_t *wqp;
629 629 mblk_t *bp;
630 630 zc_state_t *zcs;
631 631 major_t major;
632 632 minor_t minor;
633 633
634 634 zcs = (zc_state_t *)rqp->q_ptr;
635 635
636 636 if (rqp == zcs->zc_master_rdq) {
637 637 DBG("Closing master side");
638 638
639 639 zcs->zc_master_rdq = NULL;
640 640 zcs->zc_state &= ~ZC_STATE_MOPEN;
641 641
642 642 /*
643 643 * qenable slave side write queue so that it can flush
644 644 * its messages as master's read queue is going away
645 645 */
646 646 if (zcs->zc_slave_rdq != NULL) {
647 647 qenable(WR(zcs->zc_slave_rdq));
648 648 }
649 649
650 650 qprocsoff(rqp);
651 651 WR(rqp)->q_ptr = rqp->q_ptr = NULL;
652 652
653 653 } else if (rqp == zcs->zc_slave_rdq) {
654 654
655 655 DBG("Closing slave side");
656 656 zcs->zc_state &= ~ZC_STATE_SOPEN;
657 657 zcs->zc_slave_rdq = NULL;
658 658
659 659 wqp = WR(rqp);
660 660 while ((bp = getq(wqp)) != NULL) {
661 661 if (zcs->zc_master_rdq != NULL)
662 662 putnext(zcs->zc_master_rdq, bp);
663 663 else if (bp->b_datap->db_type == M_IOCTL)
664 664 miocnak(wqp, bp, 0, 0);
665 665 else
666 666 freemsg(bp);
667 667 }
668 668
669 669 /*
670 670 * Qenable master side write queue so that it can flush its
671 671 * messages as slaves's read queue is going away.
672 672 */
673 673 if (zcs->zc_master_rdq != NULL)
674 674 qenable(WR(zcs->zc_master_rdq));
675 675
676 676 qprocsoff(rqp);
677 677 WR(rqp)->q_ptr = rqp->q_ptr = NULL;
678 678
679 679 /*
680 680 * Clear the sad configuration so that reopening doesn't fail
681 681 * to set up sad configuration.
682 682 */
683 683 major = ddi_driver_major(zcs->zc_devinfo);
684 684 minor = ddi_get_instance(zcs->zc_devinfo) << 1 | ZC_SLAVE_MINOR;
685 685 (void) kstr_autopush(CLR_AUTOPUSH, &major, &minor, NULL, NULL,
686 686 NULL);
687 687 }
688 688
689 689 return (0);
690 690 }
691 691
692 692 static void
693 693 handle_mflush(queue_t *qp, mblk_t *mp)
694 694 {
695 695 mblk_t *nmp;
696 696 DBG1("M_FLUSH on %s side", zc_side(qp));
697 697
698 698 if (*mp->b_rptr & FLUSHW) {
699 699 DBG1("M_FLUSH, FLUSHW, %s side", zc_side(qp));
700 700 flushq(qp, FLUSHDATA);
701 701 *mp->b_rptr &= ~FLUSHW;
702 702 if ((*mp->b_rptr & FLUSHR) == 0) {
703 703 /*
704 704 * FLUSHW only. Change to FLUSHR and putnext other side,
705 705 * then we are done.
706 706 */
707 707 *mp->b_rptr |= FLUSHR;
708 708 if (zc_switch(RD(qp)) != NULL) {
709 709 putnext(zc_switch(RD(qp)), mp);
710 710 return;
711 711 }
712 712 } else if ((zc_switch(RD(qp)) != NULL) &&
713 713 (nmp = copyb(mp)) != NULL) {
714 714 /*
715 715 * It is a FLUSHRW; we copy the mblk and send
716 716 * it to the other side, since we still need to use
717 717 * the mblk in FLUSHR processing, below.
718 718 */
719 719 putnext(zc_switch(RD(qp)), nmp);
720 720 }
721 721 }
722 722
723 723 if (*mp->b_rptr & FLUSHR) {
724 724 DBG("qreply(qp) turning FLUSHR around\n");
725 725 qreply(qp, mp);
726 726 return;
727 727 }
728 728 freemsg(mp);
729 729 }
730 730
731 731 /*
732 732 * wput(9E) is symmetric for master and slave sides, so this handles both
733 733 * without splitting the codepath. (The only exception to this is the
734 734 * processing of zcons ioctls, which is restricted to the master side.)
735 735 *
736 736 * zc_wput() looks at the other side; if there is no process holding that
737 737 * side open, it frees the message. This prevents processes from hanging
738 738 * if no one is holding open the console. Otherwise, it putnext's high
739 739 * priority messages, putnext's normal messages if possible, and otherwise
740 740 * enqueues the messages; in the case that something is enqueued, wsrv(9E)
741 741 * will take care of eventually shuttling I/O to the other side.
742 742 */
743 743 static void
744 744 zc_wput(queue_t *qp, mblk_t *mp)
745 745 {
746 746 unsigned char type = mp->b_datap->db_type;
747 747 zc_state_t *zcs;
748 748 struct iocblk *iocbp;
749 749 file_t *slave_filep;
750 750 struct snode *slave_snodep;
751 751 int slave_fd;
752 752
753 753 ASSERT(qp->q_ptr);
754 754
755 755 DBG1("entering zc_wput, %s side", zc_side(qp));
756 756
757 757 /*
758 758 * Process zcons ioctl messages if qp is the master console's write
759 759 * queue.
760 760 */
761 761 zcs = (zc_state_t *)qp->q_ptr;
762 762 if (zcs->zc_master_rdq != NULL && qp == WR(zcs->zc_master_rdq) &&
763 763 type == M_IOCTL) {
764 764 iocbp = (struct iocblk *)(void *)mp->b_rptr;
765 765 switch (iocbp->ioc_cmd) {
766 766 case ZC_HOLDSLAVE:
767 767 /*
768 768 * Hold the slave's vnode and increment the refcount
769 769 * of the snode. If the vnode is already held, then
770 770 * indicate success.
771 771 */
772 772 if (iocbp->ioc_count != TRANSPARENT) {
773 773 miocack(qp, mp, 0, EINVAL);
774 774 return;
775 775 }
776 776 if (zcs->zc_slave_vnode != NULL) {
777 777 miocack(qp, mp, 0, 0);
778 778 return;
779 779 }
780 780
781 781 /*
782 782 * The process that passed the ioctl must be running in
783 783 * the global zone.
784 784 */
785 785 if (curzone != global_zone) {
786 786 miocack(qp, mp, 0, EINVAL);
787 787 return;
788 788 }
789 789
790 790 /*
791 791 * The calling process must pass a file descriptor for
792 792 * the slave device.
793 793 */
794 794 slave_fd =
795 795 (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont->
796 796 b_rptr;
797 797 slave_filep = getf(slave_fd);
798 798 if (slave_filep == NULL) {
799 799 miocack(qp, mp, 0, EINVAL);
800 800 return;
801 801 }
802 802 if (ZC_STATE_TO_SLAVEDEV(zcs) !=
803 803 slave_filep->f_vnode->v_rdev) {
804 804 releasef(slave_fd);
805 805 miocack(qp, mp, 0, EINVAL);
806 806 return;
807 807 }
808 808
809 809 /*
810 810 * Get a reference to the slave's vnode. Also bump the
811 811 * reference count on the associated snode.
812 812 */
813 813 ASSERT(vn_matchops(slave_filep->f_vnode,
814 814 spec_getvnodeops()));
815 815 zcs->zc_slave_vnode = slave_filep->f_vnode;
816 816 VN_HOLD(zcs->zc_slave_vnode);
817 817 slave_snodep = VTOCS(zcs->zc_slave_vnode);
818 818 mutex_enter(&slave_snodep->s_lock);
819 819 ++slave_snodep->s_count;
820 820 mutex_exit(&slave_snodep->s_lock);
821 821 releasef(slave_fd);
822 822 miocack(qp, mp, 0, 0);
823 823 return;
824 824 case ZC_RELEASESLAVE:
825 825 /*
826 826 * Release the master's handle on the slave's vnode.
827 827 * If there isn't a handle for the vnode, then indicate
828 828 * success.
829 829 */
830 830 if (iocbp->ioc_count != TRANSPARENT) {
831 831 miocack(qp, mp, 0, EINVAL);
832 832 return;
833 833 }
834 834 if (zcs->zc_slave_vnode == NULL) {
835 835 miocack(qp, mp, 0, 0);
836 836 return;
837 837 }
838 838
839 839 /*
840 840 * The process that passed the ioctl must be running in
841 841 * the global zone.
842 842 */
843 843 if (curzone != global_zone) {
844 844 miocack(qp, mp, 0, EINVAL);
845 845 return;
846 846 }
847 847
848 848 /*
849 849 * The process that passed the ioctl must have provided
850 850 * a file descriptor for the slave device. Make sure
851 851 * this is correct.
852 852 */
853 853 slave_fd =
854 854 (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont->
855 855 b_rptr;
856 856 slave_filep = getf(slave_fd);
857 857 if (slave_filep == NULL) {
858 858 miocack(qp, mp, 0, EINVAL);
859 859 return;
860 860 }
861 861 if (zcs->zc_slave_vnode->v_rdev !=
862 862 slave_filep->f_vnode->v_rdev) {
863 863 releasef(slave_fd);
864 864 miocack(qp, mp, 0, EINVAL);
865 865 return;
866 866 }
867 867
868 868 /*
869 869 * Decrement the snode's reference count and release the
870 870 * vnode.
871 871 */
872 872 ASSERT(vn_matchops(slave_filep->f_vnode,
873 873 spec_getvnodeops()));
874 874 slave_snodep = VTOCS(zcs->zc_slave_vnode);
875 875 mutex_enter(&slave_snodep->s_lock);
876 876 --slave_snodep->s_count;
877 877 mutex_exit(&slave_snodep->s_lock);
878 878 VN_RELE(zcs->zc_slave_vnode);
879 879 zcs->zc_slave_vnode = NULL;
880 880 releasef(slave_fd);
881 881 miocack(qp, mp, 0, 0);
882 882 return;
883 883 default:
884 884 break;
885 885 }
886 886 }
887 887
888 888 if (zc_switch(RD(qp)) == NULL) {
889 889 DBG1("wput to %s side (no one listening)", zc_side(qp));
890 890 switch (type) {
891 891 case M_FLUSH:
892 892 handle_mflush(qp, mp);
893 893 break;
894 894 case M_IOCTL:
895 895 miocnak(qp, mp, 0, 0);
896 896 break;
897 897 default:
898 898 freemsg(mp);
899 899 break;
900 900 }
901 901 return;
902 902 }
903 903
904 904 if (type >= QPCTL) {
905 905 DBG1("(hipri) wput, %s side", zc_side(qp));
906 906 switch (type) {
907 907 case M_READ: /* supposedly from ldterm? */
908 908 DBG("zc_wput: tossing M_READ\n");
909 909 freemsg(mp);
910 910 break;
911 911 case M_FLUSH:
912 912 handle_mflush(qp, mp);
913 913 break;
914 914 default:
915 915 /*
916 916 * Put this to the other side.
917 917 */
918 918 ASSERT(zc_switch(RD(qp)) != NULL);
919 919 putnext(zc_switch(RD(qp)), mp);
920 920 break;
921 921 }
922 922 DBG1("done (hipri) wput, %s side", zc_side(qp));
923 923 return;
924 924 }
925 925
926 926 /*
927 927 * Only putnext if there isn't already something in the queue.
928 928 * otherwise things would wind up out of order.
929 929 */
930 930 if (qp->q_first == NULL && bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
931 931 DBG("wput: putting message to other side\n");
932 932 putnext(RD(zc_switch(qp)), mp);
933 933 } else {
934 934 DBG("wput: putting msg onto queue\n");
935 935 (void) putq(qp, mp);
936 936 }
937 937 DBG1("done wput, %s side", zc_side(qp));
938 938 }
939 939
940 940 /*
941 941 * rsrv(9E) is symmetric for master and slave, so zc_rsrv() handles both
942 942 * without splitting up the codepath.
943 943 *
944 944 * Enable the write side of the partner. This triggers the partner to send
945 945 * messages queued on its write side to this queue's read side.
946 946 */
947 947 static void
948 948 zc_rsrv(queue_t *qp)
949 949 {
950 950 zc_state_t *zcs;
951 951 zcs = (zc_state_t *)qp->q_ptr;
952 952
953 953 /*
954 954 * Care must be taken here, as either of the master or slave side
955 955 * qptr could be NULL.
956 956 */
957 957 ASSERT(qp == zcs->zc_master_rdq || qp == zcs->zc_slave_rdq);
958 958 if (zc_switch(qp) == NULL) {
959 959 DBG("zc_rsrv: other side isn't listening\n");
960 960 return;
961 961 }
962 962 qenable(WR(zc_switch(qp)));
963 963 }
964 964
965 965 /*
966 966 * This routine is symmetric for master and slave, so it handles both without
967 967 * splitting up the codepath.
968 968 *
969 969 * If there are messages on this queue that can be sent to the other, send
970 970 * them via putnext(). Else, if queued messages cannot be sent, leave them
971 971 * on this queue.
972 972 */
973 973 static void
974 974 zc_wsrv(queue_t *qp)
975 975 {
976 976 mblk_t *mp;
977 977
978 978 DBG1("zc_wsrv master (%s) side", zc_side(qp));
979 979
980 980 /*
981 981 * Partner has no read queue, so take the data, and throw it away.
982 982 */
983 983 if (zc_switch(RD(qp)) == NULL) {
984 984 DBG("zc_wsrv: other side isn't listening");
985 985 while ((mp = getq(qp)) != NULL) {
986 986 if (mp->b_datap->db_type == M_IOCTL)
987 987 miocnak(qp, mp, 0, 0);
988 988 else
989 989 freemsg(mp);
990 990 }
991 991 flushq(qp, FLUSHALL);
992 992 return;
993 993 }
994 994
995 995 /*
996 996 * while there are messages on this write queue...
997 997 */
998 998 while ((mp = getq(qp)) != NULL) {
999 999 /*
1000 1000 * Due to the way zc_wput is implemented, we should never
1001 1001 * see a control message here.
1002 1002 */
1003 1003 ASSERT(mp->b_datap->db_type < QPCTL);
1004 1004
1005 1005 if (bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
1006 1006 DBG("wsrv: send message to other side\n");
1007 1007 putnext(RD(zc_switch(qp)), mp);
1008 1008 } else {
1009 1009 DBG("wsrv: putting msg back on queue\n");
1010 1010 (void) putbq(qp, mp);
1011 1011 break;
1012 1012 }
1013 1013 }
1014 1014 }
↓ open down ↓ |
734 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX