summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/zfd.c
blob: 3edaa62bbe3ea8227f137ddf830f9a0e775b34b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Copyright 2014 Joyent, Inc.  All rights reserved.
 */

/*
 * Zone File Descriptor Driver.
 *
 * This driver is derived from the zcons driver which is in turn derived from
 * the pts/ptm drivers. The purpose is to expose file descriptors within the
 * zone which are connected to zoneadmd and used for logging or an interactive
 * connection to a process within the zone.
 *
 * Its implementation is straightforward. Each instance of the driver
 * represents a global-zone/local-zone pair. Unlike the zcons device, zoneadmd
 * uses these devices unidirectionally to provide stdin, stdout and stderr to
 * the process within the zone.
 *
 * Instances of zfd are onlined as children of /pseudo/zfdnex@2/ by zoneadmd,
 * using the devctl framework; thus the driver does not need to maintain any
 * sort of "admin" node.
 *
 * The driver shuttles I/O from master side to slave side and back.  In a break
 * from the pts/ptm semantics, if one side is not open, I/O directed towards
 * it will simply be discarded. This is so that if zoneadmd is not holding the
 * master side fd open (i.e. it has died somehow), processes in the zone do not
 * experience any errors and I/O to the fd does not cause the process to hang.
 */

#include <sys/types.h>
#include <sys/cmn_err.h>
#include <sys/conf.h>
#include <sys/cred.h>
#include <sys/ddi.h>
#include <sys/debug.h>
#include <sys/devops.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/kstr.h>
#include <sys/modctl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/strsun.h>
#include <sys/sunddi.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/zfd.h>
#include <sys/vnode.h>
#include <sys/fs/snode.h>
#include <sys/zone.h>

static int zfd_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int zfd_attach(dev_info_t *, ddi_attach_cmd_t);
static int zfd_detach(dev_info_t *, ddi_detach_cmd_t);

static int zfd_open(queue_t *, dev_t *, int, int, cred_t *);
static int zfd_close(queue_t *, int, cred_t *);
static void zfd_wput(queue_t *, mblk_t *);
static void zfd_rsrv(queue_t *);
static void zfd_wsrv(queue_t *);

/*
 * The instance number is encoded in the dev_t in the minor number; the lowest
 * bit of the minor number is used to track the master vs. slave side of the
 * fd. The rest of the bits in the minor number are the instance.
 */
#define	ZFD_MASTER_MINOR		0
#define	ZFD_SLAVE_MINOR		1

#define	ZFD_INSTANCE(x)		(getminor((x)) >> 1)
#define	ZFD_NODE(x)		(getminor((x)) & 0x01)

/*
 * This macro converts a zfd_state_t pointer to the associated slave minor
 * node's dev_t.
 */
#define	ZFD_STATE_TO_SLAVEDEV(x)	\
	(makedevice(ddi_driver_major((x)->zfd_devinfo), \
	(minor_t)(ddi_get_instance((x)->zfd_devinfo) << 1 | ZFD_SLAVE_MINOR)))

int zfd_debug = 0;
#define	DBG(a)		if (zfd_debug) cmn_err(CE_NOTE, a)
#define	DBG1(a, b)	if (zfd_debug) cmn_err(CE_NOTE, a, b)

/*
 * ZFD Pseudo Terminal Module: stream data structure definitions,
 * based on zcons.
 */
static struct module_info zfd_info = {
	0x20FD,	/* ZOFD - 8445 */
	"zfd",
	0,		/* min packet size */
	INFPSZ,		/* max packet size - infinity */
	2048,		/* high water */
	128		/* low water */
};

static struct qinit zfd_rinit = {
	NULL,
	(int (*)()) zfd_rsrv,
	zfd_open,
	zfd_close,
	NULL,
	&zfd_info,
	NULL
};

static struct qinit zfd_winit = {
	(int (*)()) zfd_wput,
	(int (*)()) zfd_wsrv,
	NULL,
	NULL,
	NULL,
	&zfd_info,
	NULL
};

static struct streamtab zfd_tab_info = {
	&zfd_rinit,
	&zfd_winit,
	NULL,
	NULL
};

#define	ZFD_CONF_FLAG	(D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL)

/*
 * this will define (struct cb_ops cb_zfd_ops) and (struct dev_ops zfd_ops)
 */
DDI_DEFINE_STREAM_OPS(zfd_ops, nulldev, nulldev, zfd_attach, zfd_detach, \
	nodev, zfd_getinfo, ZFD_CONF_FLAG, &zfd_tab_info, \
	ddi_quiesce_not_needed);

/*
 * Module linkage information for the kernel.
 */

static struct modldrv modldrv = {
	&mod_driverops, 	/* Type of module (this is a pseudo driver) */
	"Zone FD driver",	/* description of module */
	&zfd_ops		/* driver ops */
};

static struct modlinkage modlinkage = {
	MODREV_1,
	&modldrv,
	NULL
};

typedef struct zfd_state {
	dev_info_t *zfd_devinfo;
	queue_t *zfd_master_rdq;
	queue_t *zfd_slave_rdq;
	vnode_t *zfd_slave_vnode;
	int zfd_state;
	int zfd_tty;
} zfd_state_t;

#define	ZFD_STATE_MOPEN	0x01
#define	ZFD_STATE_SOPEN	0x02

static void *zfd_soft_state;

/*
 * List of STREAMS modules that is pushed onto a slave instance after the
 * ZFD_MAKETTY ioctl has been received.
 */
static char *zfd_mods[] = {
	"ptem",
	"ldterm",
	"ttcompat",
	NULL
};

int
_init(void)
{
	int err;

	if ((err = ddi_soft_state_init(&zfd_soft_state, sizeof (zfd_state_t),
	    0)) != 0) {
		return (err);
	}

	if ((err = mod_install(&modlinkage)) != 0)
		ddi_soft_state_fini(zfd_soft_state);

	return (err);
}


int
_fini(void)
{
	int err;

	if ((err = mod_remove(&modlinkage)) != 0) {
		return (err);
	}

	ddi_soft_state_fini(&zfd_soft_state);
	return (0);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

static int
zfd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	zfd_state_t *zfds;
	int instance;
	char masternm[ZFD_NAME_LEN], slavenm[ZFD_NAME_LEN];

	if (cmd != DDI_ATTACH)
		return (DDI_FAILURE);

	instance = ddi_get_instance(dip);
	if (ddi_soft_state_zalloc(zfd_soft_state, instance) != DDI_SUCCESS)
		return (DDI_FAILURE);

	(void) snprintf(masternm, sizeof (masternm), "%s%d", ZFD_MASTER_NAME,
	    instance);
	(void) snprintf(slavenm, sizeof (slavenm), "%s%d", ZFD_SLAVE_NAME,
	    instance);

	/*
	 * Create the master and slave minor nodes.
	 */
	if ((ddi_create_minor_node(dip, slavenm, S_IFCHR,
	    instance << 1 | ZFD_SLAVE_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE) ||
	    (ddi_create_minor_node(dip, masternm, S_IFCHR,
	    instance << 1 | ZFD_MASTER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE)) {
		ddi_remove_minor_node(dip, NULL);
		ddi_soft_state_free(zfd_soft_state, instance);
		return (DDI_FAILURE);
	}

	VERIFY((zfds = ddi_get_soft_state(zfd_soft_state, instance)) != NULL);
	zfds->zfd_devinfo = dip;
	zfds->zfd_tty = 0;
	return (DDI_SUCCESS);
}

static int
zfd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	zfd_state_t *zfds;
	int instance;

	if (cmd != DDI_DETACH)
		return (DDI_FAILURE);

	instance = ddi_get_instance(dip);
	if ((zfds = ddi_get_soft_state(zfd_soft_state, instance)) == NULL)
		return (DDI_FAILURE);

	if ((zfds->zfd_state & ZFD_STATE_MOPEN) ||
	    (zfds->zfd_state & ZFD_STATE_SOPEN)) {
		DBG1("zfd_detach: device (dip=%p) still open\n", (void *)dip);
		return (DDI_FAILURE);
	}

	ddi_remove_minor_node(dip, NULL);
	ddi_soft_state_free(zfd_soft_state, instance);

	return (DDI_SUCCESS);
}

/*
 * zfd_getinfo()
 *	getinfo(9e) entrypoint.
 */
/*ARGSUSED*/
static int
zfd_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
	zfd_state_t *zfds;
	int instance = ZFD_INSTANCE((dev_t)arg);

	switch (infocmd) {
	case DDI_INFO_DEVT2DEVINFO:
		if ((zfds = ddi_get_soft_state(zfd_soft_state,
		    instance)) == NULL)
			return (DDI_FAILURE);
		*result = zfds->zfd_devinfo;
		return (DDI_SUCCESS);
	case DDI_INFO_DEVT2INSTANCE:
		*result = (void *)(uintptr_t)instance;
		return (DDI_SUCCESS);
	}
	return (DDI_FAILURE);
}

/*
 * Return the equivalent queue from the other side of the relationship.
 * e.g.: given the slave's write queue, return the master's write queue.
 */
static queue_t *
zfd_switch(queue_t *qp)
{
	zfd_state_t *zfds = qp->q_ptr;
	ASSERT(zfds != NULL);

	if (qp == zfds->zfd_master_rdq)
		return (zfds->zfd_slave_rdq);
	else if (OTHERQ(qp) == zfds->zfd_master_rdq && zfds->zfd_slave_rdq
	    != NULL)
		return (OTHERQ(zfds->zfd_slave_rdq));
	else if (qp == zfds->zfd_slave_rdq)
		return (zfds->zfd_master_rdq);
	else if (OTHERQ(qp) == zfds->zfd_slave_rdq && zfds->zfd_master_rdq
	    != NULL)
		return (OTHERQ(zfds->zfd_master_rdq));
	else
		return (NULL);
}

/*
 * For debugging and outputting messages.  Returns the name of the side of
 * the relationship associated with this queue.
 */
static const char *
zfd_side(queue_t *qp)
{
	zfd_state_t *zfds = qp->q_ptr;
	ASSERT(zfds != NULL);

	if (qp == zfds->zfd_master_rdq ||
	    OTHERQ(qp) == zfds->zfd_master_rdq) {
		return ("master");
	}
	ASSERT(qp == zfds->zfd_slave_rdq || OTHERQ(qp) == zfds->zfd_slave_rdq);
	return ("slave");
}

/*ARGSUSED*/
static int
zfd_master_open(zfd_state_t *zfds,
    queue_t	*rqp,	/* pointer to the read side queue */
    dev_t	*devp,	/* pointer to stream tail's dev */
    int		oflag,	/* the user open(2) supplied flags */
    int		sflag,	/* open state flag */
    cred_t	*credp)	/* credentials */
{
	mblk_t *mop;
	struct stroptions *sop;

	/*
	 * Enforce exclusivity on the master side; the only consumer should
	 * be the zoneadmd for the zone.
	 */
	if ((zfds->zfd_state & ZFD_STATE_MOPEN) != 0)
		return (EBUSY);

	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
		DBG("zfd_master_open(): mop allocation failed\n");
		return (ENOMEM);
	}

	zfds->zfd_state |= ZFD_STATE_MOPEN;

	/*
	 * q_ptr stores driver private data; stash the soft state data on both
	 * read and write sides of the queue.
	 */
	WR(rqp)->q_ptr = rqp->q_ptr = zfds;
	qprocson(rqp);

	/*
	 * Following qprocson(), the master side is fully plumbed into the
	 * STREAM and may send/receive messages.  Setting zfds->zfd_master_rdq
	 * will allow the slave to send messages to us (the master).
	 * This cannot occur before qprocson() because the master is not
	 * ready to process them until that point.
	 */
	zfds->zfd_master_rdq = rqp;

	/*
	 * set up hi/lo water marks on stream head read queue and add
	 * controlling tty as needed.
	 */
	mop->b_datap->db_type = M_SETOPTS;
	mop->b_wptr += sizeof (struct stroptions);
	sop = (struct stroptions *)(void *)mop->b_rptr;
	if (oflag & FNOCTTY)
		sop->so_flags = SO_HIWAT | SO_LOWAT;
	else
		sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
	sop->so_hiwat = 512;
	sop->so_lowat = 256;
	putnext(rqp, mop);

	return (0);
}

/*ARGSUSED*/
static int
zfd_slave_open(zfd_state_t *zfds,
    queue_t	*rqp,	/* pointer to the read side queue */
    dev_t	*devp,	/* pointer to stream tail's dev */
    int		oflag,	/* the user open(2) supplied flags */
    int		sflag,	/* open state flag */
    cred_t	*credp)	/* credentials */
{
	mblk_t *mop;
	struct stroptions *sop;
	/*
	 * The slave side can be opened as many times as needed.
	 */
	if ((zfds->zfd_state & ZFD_STATE_SOPEN) != 0) {
		ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zfds));
		return (0);
	}

	if (zfds->zfd_tty == 1) {
		major_t major;
		minor_t minor;
		minor_t lastminor;
		uint_t anchorindex;

		/*
		 * Set up sad(7D) so that the necessary STREAMS modules will
		 * be in place.  A wrinkle is that 'ptem' must be anchored
		 * in place (see streamio(7i)) because we always want the
		 * fd to have terminal semantics.
		 */
		minor =
		    ddi_get_instance(zfds->zfd_devinfo) << 1 | ZFD_SLAVE_MINOR;
		major = ddi_driver_major(zfds->zfd_devinfo);
		lastminor = 0;
		anchorindex = 1;
		if (kstr_autopush(SET_AUTOPUSH, &major, &minor, &lastminor,
		    &anchorindex, zfd_mods) != 0) {
			DBG("zfd_slave_open(): kstr_autopush() failed\n");
			return (EIO);
		}
	}

	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
		DBG("zfd_slave_open(): mop allocation failed\n");
		return (ENOMEM);
	}

	zfds->zfd_state |= ZFD_STATE_SOPEN;

	/*
	 * q_ptr stores driver private data; stash the soft state data on both
	 * read and write sides of the queue.
	 */
	WR(rqp)->q_ptr = rqp->q_ptr = zfds;

	qprocson(rqp);

	/*
	 * Must follow qprocson(), since we aren't ready to process until then.
	 */
	zfds->zfd_slave_rdq = rqp;

	/*
	 * set up hi/lo water marks on stream head read queue and add
	 * controlling tty as needed.
	 */
	mop->b_datap->db_type = M_SETOPTS;
	mop->b_wptr += sizeof (struct stroptions);
	sop = (struct stroptions *)(void *)mop->b_rptr;
	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
	sop->so_hiwat = 512;
	sop->so_lowat = 256;
	putnext(rqp, mop);

	return (0);
}

/*
 * open(9e) entrypoint; checks sflag, and rejects anything unordinary.
 */
static int
zfd_open(queue_t *rqp,		/* pointer to the read side queue */
	dev_t   *devp,		/* pointer to stream tail's dev */
	int	oflag,		/* the user open(2) supplied flags */
	int	sflag,		/* open state flag */
	cred_t  *credp)		/* credentials */
{
	int instance = ZFD_INSTANCE(*devp);
	int ret;
	zfd_state_t *zfds;

	if (sflag != 0)
		return (EINVAL);

	if ((zfds = ddi_get_soft_state(zfd_soft_state, instance)) == NULL)
		return (ENXIO);

	switch (ZFD_NODE(*devp)) {
	case ZFD_MASTER_MINOR:
		ret = zfd_master_open(zfds, rqp, devp, oflag, sflag, credp);
		break;
	case ZFD_SLAVE_MINOR:
		ret = zfd_slave_open(zfds, rqp, devp, oflag, sflag, credp);
		break;
	default:
		ret = ENXIO;
		break;
	}

	return (ret);
}

/*
 * close(9e) entrypoint.
 */
/*ARGSUSED1*/
static int
zfd_close(queue_t *rqp, int flag, cred_t *credp)
{
	queue_t *wqp;
	mblk_t	*bp;
	zfd_state_t *zfds;
	major_t major;
	minor_t minor;

	zfds = (zfd_state_t *)rqp->q_ptr;

	if (rqp == zfds->zfd_master_rdq) {
		DBG("Closing master side");

		zfds->zfd_master_rdq = NULL;
		zfds->zfd_state &= ~ZFD_STATE_MOPEN;

		/*
		 * qenable slave side write queue so that it can flush
		 * its messages as master's read queue is going away
		 */
		if (zfds->zfd_slave_rdq != NULL) {
			qenable(WR(zfds->zfd_slave_rdq));
		}

		qprocsoff(rqp);
		WR(rqp)->q_ptr = rqp->q_ptr = NULL;

	} else if (rqp == zfds->zfd_slave_rdq) {

		DBG("Closing slave side");
		zfds->zfd_state &= ~ZFD_STATE_SOPEN;
		zfds->zfd_slave_rdq = NULL;

		wqp = WR(rqp);
		while ((bp = getq(wqp)) != NULL) {
			if (zfds->zfd_master_rdq != NULL)
				putnext(zfds->zfd_master_rdq, bp);
			else if (bp->b_datap->db_type == M_IOCTL)
				miocnak(wqp, bp, 0, 0);
			else
				freemsg(bp);
		}

		/*
		 * Qenable master side write queue so that it can flush its
		 * messages as slaves's read queue is going away.
		 */
		if (zfds->zfd_master_rdq != NULL)
			qenable(WR(zfds->zfd_master_rdq));

		qprocsoff(rqp);
		WR(rqp)->q_ptr = rqp->q_ptr = NULL;

		if (zfds->zfd_tty == 1) {
			/*
			 * Clear the sad configuration so that reopening
			 * doesn't fail to set up sad configuration.
			 */
			major = ddi_driver_major(zfds->zfd_devinfo);
			minor = ddi_get_instance(zfds->zfd_devinfo) << 1 |
			    ZFD_SLAVE_MINOR;
			(void) kstr_autopush(CLR_AUTOPUSH, &major, &minor,
			    NULL, NULL, NULL);
		}
	}

	return (0);
}

static void
handle_mflush(queue_t *qp, mblk_t *mp)
{
	mblk_t *nmp;
	DBG1("M_FLUSH on %s side", zfd_side(qp));

	if (*mp->b_rptr & FLUSHW) {
		DBG1("M_FLUSH, FLUSHW, %s side", zfd_side(qp));
		flushq(qp, FLUSHDATA);
		*mp->b_rptr &= ~FLUSHW;
		if ((*mp->b_rptr & FLUSHR) == 0) {
			/*
			 * FLUSHW only. Change to FLUSHR and putnext other side,
			 * then we are done.
			 */
			*mp->b_rptr |= FLUSHR;
			if (zfd_switch(RD(qp)) != NULL) {
				putnext(zfd_switch(RD(qp)), mp);
				return;
			}
		} else if ((zfd_switch(RD(qp)) != NULL) &&
		    (nmp = copyb(mp)) != NULL) {
			/*
			 * It is a FLUSHRW; we copy the mblk and send
			 * it to the other side, since we still need to use
			 * the mblk in FLUSHR processing, below.
			 */
			putnext(zfd_switch(RD(qp)), nmp);
		}
	}

	if (*mp->b_rptr & FLUSHR) {
		DBG("qreply(qp) turning FLUSHR around\n");
		qreply(qp, mp);
		return;
	}
	freemsg(mp);
}

/*
 * wput(9E) is symmetric for master and slave sides, so this handles both
 * without splitting the codepath.  (The only exception to this is the
 * processing of zfd ioctls, which is restricted to the master side.)
 *
 * zfd_wput() looks at the other side; if there is no process holding that
 * side open, it frees the message.  This prevents processes from hanging
 * if no one is holding open the fd.  Otherwise, it putnext's high
 * priority messages, putnext's normal messages if possible, and otherwise
 * enqueues the messages; in the case that something is enqueued, wsrv(9E)
 * will take care of eventually shuttling I/O to the other side.
 */
static void
zfd_wput(queue_t *qp, mblk_t *mp)
{
	unsigned char type = mp->b_datap->db_type;
	zfd_state_t *zfds;
	struct iocblk *iocbp;

	ASSERT(qp->q_ptr);

	DBG1("entering zfd_wput, %s side", zfd_side(qp));

	/*
	 * Process zfd ioctl messages if qp is the master side's write queue.
	 */
	zfds = (zfd_state_t *)qp->q_ptr;
	if (zfds->zfd_master_rdq != NULL && qp == WR(zfds->zfd_master_rdq) &&
	    type == M_IOCTL) {
		iocbp = (struct iocblk *)(void *)mp->b_rptr;
		switch (iocbp->ioc_cmd) {
		case ZFD_MAKETTY:
			/*
			 * The process that passed the ioctl must be running in
			 * the global zone.
			 */
			if (crgetzoneid(iocbp->ioc_cr) != GLOBAL_ZONEID) {
				miocack(qp, mp, 0, EINVAL);
				return;
			}
			zfds->zfd_tty = 1;
			miocack(qp, mp, 0, 0);
			return;
		case ZFD_EOF:
			/*
			 * The process that passed the ioctl must be running in
			 * the global zone.
			 */
			if (crgetzoneid(iocbp->ioc_cr) != GLOBAL_ZONEID) {
				miocack(qp, mp, 0, EINVAL);
				return;
			}
			if (zfds->zfd_slave_rdq != NULL)
				(void) putnextctl(zfds->zfd_slave_rdq,
				    M_HANGUP);
			miocack(qp, mp, 0, 0);
			return;
		case ZFD_HAS_SLAVE:
			/*
			 * The process that passed the ioctl must be running in
			 * the global zone.
			 */
			if (crgetzoneid(iocbp->ioc_cr) != GLOBAL_ZONEID) {
				miocack(qp, mp, 0, EINVAL);
				return;
			}
			if ((zfds->zfd_state & ZFD_STATE_SOPEN) != 0) {
				miocack(qp, mp, 0, 0);
			} else {
				miocack(qp, mp, 0, ENOTTY);
			}
			return;
		default:
			break;
		}
	}

	if (zfd_switch(RD(qp)) == NULL) {
		DBG1("wput to %s side (no one listening)", zfd_side(qp));
		switch (type) {
		case M_FLUSH:
			handle_mflush(qp, mp);
			break;
		case M_IOCTL:
			miocnak(qp, mp, 0, 0);
			break;
		default:
			freemsg(mp);
			break;
		}
		return;
	}

	if (type >= QPCTL) {
		DBG1("(hipri) wput, %s side", zfd_side(qp));
		switch (type) {
		case M_READ:		/* supposedly from ldterm? */
			DBG("zfd_wput: tossing M_READ\n");
			freemsg(mp);
			break;
		case M_FLUSH:
			handle_mflush(qp, mp);
			break;
		default:
			/*
			 * Put this to the other side.
			 */
			ASSERT(zfd_switch(RD(qp)) != NULL);
			putnext(zfd_switch(RD(qp)), mp);
			break;
		}
		DBG1("done (hipri) wput, %s side", zfd_side(qp));
		return;
	}

	/*
	 * Only putnext if there isn't already something in the queue.
	 * otherwise things would wind up out of order.
	 */
	if (qp->q_first == NULL &&
	    bcanputnext(RD(zfd_switch(qp)), mp->b_band)) {
		DBG("wput: putting message to other side\n");
		putnext(RD(zfd_switch(qp)), mp);
	} else {
		DBG("wput: putting msg onto queue\n");
		(void) putq(qp, mp);
	}
	DBG1("done wput, %s side", zfd_side(qp));
}

/*
 * rsrv(9E) is symmetric for master and slave, so zfd_rsrv() handles both
 * without splitting up the codepath.
 *
 * Enable the write side of the partner.  This triggers the partner to send
 * messages queued on its write side to this queue's read side.
 */
static void
zfd_rsrv(queue_t *qp)
{
	zfd_state_t *zfds;
	zfds = (zfd_state_t *)qp->q_ptr;

	/*
	 * Care must be taken here, as either of the master or slave side
	 * qptr could be NULL.
	 */
	ASSERT(qp == zfds->zfd_master_rdq || qp == zfds->zfd_slave_rdq);
	if (zfd_switch(qp) == NULL) {
		DBG("zfd_rsrv: other side isn't listening\n");
		return;
	}
	qenable(WR(zfd_switch(qp)));
}

/*
 * This routine is symmetric for master and slave, so it handles both without
 * splitting up the codepath.
 *
 * If there are messages on this queue that can be sent to the other, send
 * them via putnext(). Else, if queued messages cannot be sent, leave them
 * on this queue.
 */
static void
zfd_wsrv(queue_t *qp)
{
	mblk_t *mp;

	DBG1("zfd_wsrv master (%s) side", zfd_side(qp));

	/*
	 * Partner has no read queue, so take the data, and throw it away.
	 */
	if (zfd_switch(RD(qp)) == NULL) {
		DBG("zfd_wsrv: other side isn't listening");
		while ((mp = getq(qp)) != NULL) {
			if (mp->b_datap->db_type == M_IOCTL)
				miocnak(qp, mp, 0, 0);
			else
				freemsg(mp);
		}
		flushq(qp, FLUSHALL);
		return;
	}

	/*
	 * while there are messages on this write queue...
	 */
	while ((mp = getq(qp)) != NULL) {
		/*
		 * Due to the way zfd_wput is implemented, we should never
		 * see a control message here.
		 */
		ASSERT(mp->b_datap->db_type < QPCTL);

		if (bcanputnext(RD(zfd_switch(qp)), mp->b_band)) {
			DBG("wsrv: send message to other side\n");
			putnext(RD(zfd_switch(qp)), mp);
		} else {
			DBG("wsrv: putting msg back on queue\n");
			(void) putbq(qp, mp);
			break;
		}
	}
}