xref: /onnv-gate/usr/src/uts/common/io/iwscons.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Redirecting driver; used to handle workstation console redirection.
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * Redirects all I/O through a given device instance to the device designated
33*0Sstevel@tonic-gate  * as the current target, as given by the vnode associated with the first
34*0Sstevel@tonic-gate  * entry in the list of redirections for the given device instance.  The
35*0Sstevel@tonic-gate  * implementation assumes that this vnode denotes a STREAMS device; this is
36*0Sstevel@tonic-gate  * perhaps a bug.
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  * Supports the SRIOCSREDIR ioctl for designating a new redirection target.
39*0Sstevel@tonic-gate  * The new target is added to the front of a list of potentially active
40*0Sstevel@tonic-gate  * designees.  Should the device at the front of this list be closed, the new
41*0Sstevel@tonic-gate  * front entry assumes active duty.  (Stated differently, redirection targets
42*0Sstevel@tonic-gate  * stack, except that it's possible for entries in the interior of the stack
43*0Sstevel@tonic-gate  * to go away.)
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  * Supports the SRIOCISREDIR ioctl for inquiring whether the descriptor given
46*0Sstevel@tonic-gate  * as argument is the current front of the redirection list associated with
47*0Sstevel@tonic-gate  * the descriptor on which the ioctl was issued.
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * Every open instance of this driver corresponds to an instance of the
50*0Sstevel@tonic-gate  * underlying client driver.  If the redirection stack would otherwise become
51*0Sstevel@tonic-gate  * empty, this device (designated by the wd_vp field of the wcd_data
52*0Sstevel@tonic-gate  * structure) is implicitly opened and added to the front of the list.  Thus,
53*0Sstevel@tonic-gate  * there's always an active device for handling i/o through an open instance
54*0Sstevel@tonic-gate  * of this driver.
55*0Sstevel@tonic-gate  *
56*0Sstevel@tonic-gate  * XXX: Names -- many of the names in this driver and its companion STREAMS
57*0Sstevel@tonic-gate  *	module still reflect its origins as the workstation console
58*0Sstevel@tonic-gate  *	redirection driver.  Ultimately, they should be changed to reflect the
59*0Sstevel@tonic-gate  *	fact that this driver is potentially a general purpose redirection
60*0Sstevel@tonic-gate  *	driver.  In the meantime, the driver is still specialized to have a
61*0Sstevel@tonic-gate  *	single client -- the workstation console driver -- and its file name
62*0Sstevel@tonic-gate  *	remains iwscons.c to reflect that specialization.
63*0Sstevel@tonic-gate  *
64*0Sstevel@tonic-gate  *	Proposed change: "iwscn" becomes either "dr" (for "streams redirecting
65*0Sstevel@tonic-gate  *	driver") or "srm" (for "streams redirecting module"), as appropriate.
66*0Sstevel@tonic-gate  *
67*0Sstevel@tonic-gate  * XXX:	Add mechanism for notifying a redirectee that it's no longer the
68*0Sstevel@tonic-gate  *	current redirectee?  (This in contrast to the current facility for
69*0Sstevel@tonic-gate  *	letting it ask.)
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate #include <sys/types.h>
73*0Sstevel@tonic-gate #include <sys/sysmacros.h>
74*0Sstevel@tonic-gate #include <sys/open.h>
75*0Sstevel@tonic-gate #include <sys/param.h>
76*0Sstevel@tonic-gate #include <sys/systm.h>
77*0Sstevel@tonic-gate #include <sys/signal.h>
78*0Sstevel@tonic-gate #include <sys/cred.h>
79*0Sstevel@tonic-gate #include <sys/user.h>
80*0Sstevel@tonic-gate #include <sys/proc.h>
81*0Sstevel@tonic-gate #include <sys/vnode.h>
82*0Sstevel@tonic-gate #include <sys/uio.h>
83*0Sstevel@tonic-gate #include <sys/file.h>
84*0Sstevel@tonic-gate #include <sys/kmem.h>
85*0Sstevel@tonic-gate #include <sys/stat.h>
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate #include <sys/stream.h>
88*0Sstevel@tonic-gate #include <sys/stropts.h>
89*0Sstevel@tonic-gate #include <sys/strsubr.h>
90*0Sstevel@tonic-gate #include <sys/poll.h>
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate #include <sys/debug.h>
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate #include <sys/strredir.h>
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate #include <sys/conf.h>
97*0Sstevel@tonic-gate #include <sys/ddi.h>
98*0Sstevel@tonic-gate #include <sys/sunddi.h>
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate static int	iwscninfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
101*0Sstevel@tonic-gate static int	iwscnattach(dev_info_t *, ddi_attach_cmd_t);
102*0Sstevel@tonic-gate static int	iwscnopen(dev_t *, int, int, cred_t *);
103*0Sstevel@tonic-gate static int	iwscnclose(dev_t, int, int, cred_t *);
104*0Sstevel@tonic-gate static int	iwscnread(dev_t, struct uio *, cred_t *);
105*0Sstevel@tonic-gate static int	iwscnwrite(dev_t, struct uio *, cred_t *);
106*0Sstevel@tonic-gate static int	iwscnioctl(dev_t, int, intptr_t, int, cred_t *, int *);
107*0Sstevel@tonic-gate static int	iwscnpoll(dev_t, short, int, short *, struct pollhead **);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * Private copy of devinfo pointer; iwscninfo uses it.
111*0Sstevel@tonic-gate  */
112*0Sstevel@tonic-gate static dev_info_t	*iwscn_dip;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate struct cb_ops	iwscn_cb_ops = {
115*0Sstevel@tonic-gate 	iwscnopen,		/* open */
116*0Sstevel@tonic-gate 	iwscnclose,		/* close */
117*0Sstevel@tonic-gate 	nodev,			/* strategy */
118*0Sstevel@tonic-gate 	nodev,			/* print */
119*0Sstevel@tonic-gate 	nodev,			/* dump */
120*0Sstevel@tonic-gate 	iwscnread,		/* read */
121*0Sstevel@tonic-gate 	iwscnwrite,		/* write */
122*0Sstevel@tonic-gate 	iwscnioctl,		/* ioctl */
123*0Sstevel@tonic-gate 	nodev,			/* devmap */
124*0Sstevel@tonic-gate 	nodev,			/* mmap */
125*0Sstevel@tonic-gate 	nodev, 			/* segmap */
126*0Sstevel@tonic-gate 	iwscnpoll,		/* poll */
127*0Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
128*0Sstevel@tonic-gate 	0,			/* streamtab  */
129*0Sstevel@tonic-gate 	D_NEW|D_MP		/* Driver compatibility flag */
130*0Sstevel@tonic-gate };
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate struct dev_ops	iwscn_ops = {
133*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
134*0Sstevel@tonic-gate 	0,			/* refcnt  */
135*0Sstevel@tonic-gate 	iwscninfo,		/* info */
136*0Sstevel@tonic-gate 	nulldev,		/* identify */
137*0Sstevel@tonic-gate 	nulldev,		/* probe */
138*0Sstevel@tonic-gate 	iwscnattach,		/* attach */
139*0Sstevel@tonic-gate 	nodev,			/* detach */
140*0Sstevel@tonic-gate 	nodev,			/* reset */
141*0Sstevel@tonic-gate 	&iwscn_cb_ops,		/* driver operations */
142*0Sstevel@tonic-gate 	NULL			/* bus operations */
143*0Sstevel@tonic-gate };
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate static krwlock_t	iwscn_lock; /* lock proecting almost everything here */
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate  * A read/write lock was used to serialize reads,writes/opens,closes.
149*0Sstevel@tonic-gate  * Sometime the open would hang due to a pending read.  The new lock
150*0Sstevel@tonic-gate  * iwscn_open_lock and the read lock are held in open to assure a single
151*0Sstevel@tonic-gate  * instance, while letting concurrent reads/writes to proceed.
152*0Sstevel@tonic-gate  */
153*0Sstevel@tonic-gate static kmutex_t 	iwscn_open_lock; /* Serializes opens  */
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate  * These next two fields, protected by iwscn_lock, pass the data to wcmopen()
157*0Sstevel@tonic-gate  * from the ioctl SRIOCSREDIR.  wcmopen() uses the data only if the thread
158*0Sstevel@tonic-gate  * matches.  This keeps other threads from interfering.
159*0Sstevel@tonic-gate  */
160*0Sstevel@tonic-gate extern kthread_id_t	iwscn_thread;	/* thread that is allowed to */
161*0Sstevel@tonic-gate 					/* push redirm */
162*0Sstevel@tonic-gate extern wcm_data_t	*iwscn_wcm_data;  /* allocated data for redirm */
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate  * Forward declarations of private routines.
166*0Sstevel@tonic-gate  */
167*0Sstevel@tonic-gate static int		srreset(wcd_data_t *, int, cred_t *);
168*0Sstevel@tonic-gate static wcrlist_t	*srrm(wcrlist_t **, vnode_t *, int);
169*0Sstevel@tonic-gate static wcd_data_t	*srilookup(minor_t);
170*0Sstevel@tonic-gate static wcd_data_t	*srialloc(minor_t);
171*0Sstevel@tonic-gate static void		sridealloc(wcd_data_t *);
172*0Sstevel@tonic-gate static wcrlist_t	*srpush(wcrlist_t **, vnode_t *);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate /*
175*0Sstevel@tonic-gate  * The head of the list of open instances.
176*0Sstevel@tonic-gate  */
177*0Sstevel@tonic-gate static wcd_data_t	*wcddata;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate  * Currently, the only client of this driver is the workstation console
181*0Sstevel@tonic-gate  * driver.  Thus, we can get away with hard-wiring a reference to it here.
182*0Sstevel@tonic-gate  *
183*0Sstevel@tonic-gate  * To handle multiple clients, the driver must be revised as follows.
184*0Sstevel@tonic-gate  * 1)	Add a registration routine that clients can call to announce
185*0Sstevel@tonic-gate  *	themselves to this driver.  The routine should take as arguments the
186*0Sstevel@tonic-gate  *	major device number of the corresponding instantiation of the
187*0Sstevel@tonic-gate  *	redirecting driver and a pointer to its dedvnops ops vector.
188*0Sstevel@tonic-gate  * 2)	Maintain a list (or perhaps hash array) or registered clients,
189*0Sstevel@tonic-gate  *	recording for each the srvnops ops vector and a pointer to the list
190*0Sstevel@tonic-gate  *	of open instances for that client.
191*0Sstevel@tonic-gate  * 3)	Modify the driver entry points to use their dev argument to look up
192*0Sstevel@tonic-gate  *	the proper instantiation, get the list of open instances, and then use
193*0Sstevel@tonic-gate  *	that as they currently use the open instance list.
194*0Sstevel@tonic-gate  * 4)	To allow clients to unload themselves, we probably need an unregister
195*0Sstevel@tonic-gate  *	routine.  This routine would have to cope with active open instances.
196*0Sstevel@tonic-gate  */
197*0Sstevel@tonic-gate extern srvnops_t	wscons_srvnops;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate #include <sys/types.h>
200*0Sstevel@tonic-gate #include <sys/conf.h>
201*0Sstevel@tonic-gate #include <sys/param.h>
202*0Sstevel@tonic-gate #include <sys/systm.h>
203*0Sstevel@tonic-gate #include <sys/errno.h>
204*0Sstevel@tonic-gate #include <sys/modctl.h>
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate  * Module linkage information for the kernel.
208*0Sstevel@tonic-gate  */
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate static struct modldrv modldrv = {
211*0Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
212*0Sstevel@tonic-gate 	"Workstation Redirection driver 'iwscn' %I%",
213*0Sstevel@tonic-gate 	&iwscn_ops,	/* driver ops */
214*0Sstevel@tonic-gate };
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
217*0Sstevel@tonic-gate 	MODREV_1,
218*0Sstevel@tonic-gate 	&modldrv,
219*0Sstevel@tonic-gate 	NULL
220*0Sstevel@tonic-gate };
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate int
224*0Sstevel@tonic-gate _init(void)
225*0Sstevel@tonic-gate {
226*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate int
230*0Sstevel@tonic-gate _fini(void)
231*0Sstevel@tonic-gate {
232*0Sstevel@tonic-gate 	return (EBUSY);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate int
236*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate /*
242*0Sstevel@tonic-gate  * DDI glue routines.
243*0Sstevel@tonic-gate  */
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate /*ARGSUSED*/
246*0Sstevel@tonic-gate static int
247*0Sstevel@tonic-gate iwscnattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
248*0Sstevel@tonic-gate {
249*0Sstevel@tonic-gate 	static char	been_here;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if (!been_here) {
252*0Sstevel@tonic-gate 		been_here = 1;
253*0Sstevel@tonic-gate 		rw_init(&iwscn_lock, NULL, RW_DEFAULT, NULL);
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "iwscn", S_IFCHR,
256*0Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
257*0Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
258*0Sstevel@tonic-gate 		return (-1);
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 	iwscn_dip = devi;
261*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate /* ARGSUSED */
265*0Sstevel@tonic-gate static int
266*0Sstevel@tonic-gate iwscninfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
267*0Sstevel@tonic-gate {
268*0Sstevel@tonic-gate 	int error;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	switch (infocmd) {
271*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
272*0Sstevel@tonic-gate 		if (iwscn_dip == NULL) {
273*0Sstevel@tonic-gate 			error = DDI_FAILURE;
274*0Sstevel@tonic-gate 		} else {
275*0Sstevel@tonic-gate 			*result = (void *)iwscn_dip;
276*0Sstevel@tonic-gate 			error = DDI_SUCCESS;
277*0Sstevel@tonic-gate 		}
278*0Sstevel@tonic-gate 		break;
279*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
280*0Sstevel@tonic-gate 		*result = (void *)0;
281*0Sstevel@tonic-gate 		error = DDI_SUCCESS;
282*0Sstevel@tonic-gate 		break;
283*0Sstevel@tonic-gate 	default:
284*0Sstevel@tonic-gate 		error = DDI_FAILURE;
285*0Sstevel@tonic-gate 	}
286*0Sstevel@tonic-gate 	return (error);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate /* ARGSUSED */
291*0Sstevel@tonic-gate static int
292*0Sstevel@tonic-gate iwscnopen(
293*0Sstevel@tonic-gate 	dev_t	*devp,
294*0Sstevel@tonic-gate 	int	flag,
295*0Sstevel@tonic-gate 	int	state,		/* should be OTYP_CHR */
296*0Sstevel@tonic-gate 	cred_t	*cred)
297*0Sstevel@tonic-gate {
298*0Sstevel@tonic-gate 	minor_t		unit = getminor(*devp);
299*0Sstevel@tonic-gate 	wcd_data_t	*wd;
300*0Sstevel@tonic-gate 	int		err = 0;
301*0Sstevel@tonic-gate 	struct wcrlist	*wwd;
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	if (state != OTYP_CHR)
304*0Sstevel@tonic-gate 		return (ENXIO);
305*0Sstevel@tonic-gate 	rw_enter(&iwscn_lock, RW_READER);
306*0Sstevel@tonic-gate 	mutex_enter(&iwscn_open_lock);
307*0Sstevel@tonic-gate 	if ((wd = srilookup(unit)) == NULL) {
308*0Sstevel@tonic-gate 		vnode_t	*vp;
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 		/*
311*0Sstevel@tonic-gate 		 * First open for this instance; get a state structure for it.
312*0Sstevel@tonic-gate 		 */
313*0Sstevel@tonic-gate 		wd = srialloc(unit);
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 		/*
316*0Sstevel@tonic-gate 		 * Call the client driver to obtain a held vnode for the
317*0Sstevel@tonic-gate 		 * underlying "real" device instance.
318*0Sstevel@tonic-gate 		 *
319*0Sstevel@tonic-gate 		 * XXX:	There's wired in knowledge of the client driver here.
320*0Sstevel@tonic-gate 		 */
321*0Sstevel@tonic-gate 		err = wscons_srvnops.svn_get(unit, &vp);
322*0Sstevel@tonic-gate 		if (err != 0) {
323*0Sstevel@tonic-gate 			sridealloc(wd);
324*0Sstevel@tonic-gate 			mutex_exit(&iwscn_open_lock);
325*0Sstevel@tonic-gate 			rw_exit(&iwscn_lock);
326*0Sstevel@tonic-gate 			return (err);
327*0Sstevel@tonic-gate 		}
328*0Sstevel@tonic-gate 		wd->wd_vp = vp;
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	/*
332*0Sstevel@tonic-gate 	 * Reinitalize the list if necessary.
333*0Sstevel@tonic-gate 	 *
334*0Sstevel@tonic-gate 	 * XXX:	Is it possible for the list to empty completely while this
335*0Sstevel@tonic-gate 	 *	instance is still open?  If not, this if should be coalesced
336*0Sstevel@tonic-gate 	 *	with the previous one.
337*0Sstevel@tonic-gate 	 */
338*0Sstevel@tonic-gate 	if (wd->wd_list == NULL) {
339*0Sstevel@tonic-gate 		wcrlist_t	*e = srpush(&wd->wd_list, wd->wd_vp);
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 		/*
342*0Sstevel@tonic-gate 		 * There's no corresponding redirecting module instance for
343*0Sstevel@tonic-gate 		 * the underlying device.
344*0Sstevel@tonic-gate 		 */
345*0Sstevel@tonic-gate 		e->wl_data = NULL;
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	err = srreset(wd, flag, cred);
349*0Sstevel@tonic-gate 	/*
350*0Sstevel@tonic-gate 	 *  XXX cleanup the sig list.  Hook for console driver.
351*0Sstevel@tonic-gate 	 */
352*0Sstevel@tonic-gate 	for (wwd = wd->wd_list; wwd != NULL; wwd = wwd->wl_next) {
353*0Sstevel@tonic-gate 		ASSERT(wwd->wl_vp->v_stream != NULL);
354*0Sstevel@tonic-gate 		str_cn_clean(wwd->wl_vp);
355*0Sstevel@tonic-gate 	}
356*0Sstevel@tonic-gate 	mutex_exit(&iwscn_open_lock);
357*0Sstevel@tonic-gate 	rw_exit(&iwscn_lock);
358*0Sstevel@tonic-gate 	return (err);
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate /* ARGSUSED */
362*0Sstevel@tonic-gate static int
363*0Sstevel@tonic-gate iwscnclose(
364*0Sstevel@tonic-gate 	dev_t	dev,
365*0Sstevel@tonic-gate 	int	flag,
366*0Sstevel@tonic-gate 	int	state,		/* should be OTYP_CHR */
367*0Sstevel@tonic-gate 	cred_t	*cred)
368*0Sstevel@tonic-gate {
369*0Sstevel@tonic-gate 	wcd_data_t	*wd;
370*0Sstevel@tonic-gate 	int		err = 0;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	if (state != OTYP_CHR)
373*0Sstevel@tonic-gate 		return (ENXIO);
374*0Sstevel@tonic-gate 	rw_enter(&iwscn_lock, RW_WRITER);
375*0Sstevel@tonic-gate 	wd = srilookup(getminor(dev));
376*0Sstevel@tonic-gate 	/*
377*0Sstevel@tonic-gate 	 * Remove all outstanding redirections for this instance.
378*0Sstevel@tonic-gate 	 */
379*0Sstevel@tonic-gate 	while (wd->wd_list != NULL)
380*0Sstevel@tonic-gate 		(void) srrm(&wd->wd_list, wd->wd_list->wl_vp, 1);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	/*
383*0Sstevel@tonic-gate 	 * Since this is the _last_ close, it's our last chance to close the
384*0Sstevel@tonic-gate 	 * underlying device.  (Note that if someone else has the underlying
385*0Sstevel@tonic-gate 	 * workstation console device open, we won't get here, since
386*0Sstevel@tonic-gate 	 * spec_close will see s_count > 1.)
387*0Sstevel@tonic-gate 	 */
388*0Sstevel@tonic-gate 	while ((wd->wd_wsconsopen != 0) && (!err)) {
389*0Sstevel@tonic-gate 		err = VOP_CLOSE(wd->wd_vp, flag, 1, (offset_t)0, cred);
390*0Sstevel@tonic-gate 		if (!err)
391*0Sstevel@tonic-gate 			wd->wd_wsconsopen--;
392*0Sstevel@tonic-gate 	}
393*0Sstevel@tonic-gate 	if (!err)
394*0Sstevel@tonic-gate 		wd->wd_vp->v_stream = NULL;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	/*
397*0Sstevel@tonic-gate 	 * We don't need the vnode that the client driver gave us any more.
398*0Sstevel@tonic-gate 	 *
399*0Sstevel@tonic-gate 	 * XXX:	There's wired in knowledge of the client driver here.
400*0Sstevel@tonic-gate 	 */
401*0Sstevel@tonic-gate 	wscons_srvnops.svn_rele(wd->wd_unit, wd->wd_vp);
402*0Sstevel@tonic-gate 	sridealloc(wd);
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	rw_exit(&iwscn_lock);
405*0Sstevel@tonic-gate 	return (err);
406*0Sstevel@tonic-gate }
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate static int
409*0Sstevel@tonic-gate iwscnread(dev_t dev, uio_t *uio, cred_t *cred)
410*0Sstevel@tonic-gate {
411*0Sstevel@tonic-gate 	wcd_data_t	*wd;
412*0Sstevel@tonic-gate 	int error;
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	rw_enter(&iwscn_lock, RW_READER);
415*0Sstevel@tonic-gate 	wd = srilookup(getminor(dev));
416*0Sstevel@tonic-gate 	error = strread(wd->wd_list->wl_vp, uio, cred);
417*0Sstevel@tonic-gate 	rw_exit(&iwscn_lock);
418*0Sstevel@tonic-gate 	return (error);
419*0Sstevel@tonic-gate }
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate static int
422*0Sstevel@tonic-gate iwscnwrite(dev_t dev, uio_t *uio, cred_t *cred)
423*0Sstevel@tonic-gate {
424*0Sstevel@tonic-gate 	wcd_data_t	*wd;
425*0Sstevel@tonic-gate 	int error;
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	rw_enter(&iwscn_lock, RW_READER);
428*0Sstevel@tonic-gate 	wd = srilookup(getminor(dev));
429*0Sstevel@tonic-gate 	error = strwrite(wd->wd_list->wl_vp, uio, cred);
430*0Sstevel@tonic-gate 	rw_exit(&iwscn_lock);
431*0Sstevel@tonic-gate 	return (error);
432*0Sstevel@tonic-gate }
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate static int
435*0Sstevel@tonic-gate iwscnioctl(dev_t dev, int cmd, intptr_t arg, int flag,
436*0Sstevel@tonic-gate     cred_t *cred, int *rvalp)
437*0Sstevel@tonic-gate {
438*0Sstevel@tonic-gate 	wcd_data_t *wd;
439*0Sstevel@tonic-gate 	int err = 0;
440*0Sstevel@tonic-gate 	file_t *f;
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	switch (cmd) {
443*0Sstevel@tonic-gate 	case SRIOCSREDIR: {
444*0Sstevel@tonic-gate 		wcrlist_t	*wlp;
445*0Sstevel@tonic-gate 		wcm_data_t	*mdp;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 		if (!rw_tryenter(&iwscn_lock, RW_WRITER)) {
448*0Sstevel@tonic-gate 			return (EBUSY);
449*0Sstevel@tonic-gate 		}
450*0Sstevel@tonic-gate 		wd = srilookup(getminor(dev));
451*0Sstevel@tonic-gate 		/*
452*0Sstevel@tonic-gate 		 * Find the vnode corresponding to the file descriptor
453*0Sstevel@tonic-gate 		 * argument and verify that it names a stream.
454*0Sstevel@tonic-gate 		 */
455*0Sstevel@tonic-gate 		if ((f = getf((int)arg)) == NULL) {
456*0Sstevel@tonic-gate 			err = EBADF;
457*0Sstevel@tonic-gate 			break;
458*0Sstevel@tonic-gate 		}
459*0Sstevel@tonic-gate 		if (f->f_vnode->v_stream == NULL) {
460*0Sstevel@tonic-gate 			err = ENOSTR;
461*0Sstevel@tonic-gate 			releasef((int)arg);
462*0Sstevel@tonic-gate 			break;
463*0Sstevel@tonic-gate 		}
464*0Sstevel@tonic-gate 		/*
465*0Sstevel@tonic-gate 		 * allocate the private data for redirmod, and pass it through
466*0Sstevel@tonic-gate 		 * a global to wcmopen().  This is all protected by iwscn_lock.
467*0Sstevel@tonic-gate 		 */
468*0Sstevel@tonic-gate 		mdp = kmem_alloc(sizeof (*mdp), KM_SLEEP);
469*0Sstevel@tonic-gate 		iwscn_wcm_data = mdp;
470*0Sstevel@tonic-gate 		iwscn_thread = curthread;
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 		/*
473*0Sstevel@tonic-gate 		 * Push a new instance of the redirecting module onto the
474*0Sstevel@tonic-gate 		 * stream, so that its close routine can notify us when the
475*0Sstevel@tonic-gate 		 * overall stream is closed.  (In turn, we'll then remove it
476*0Sstevel@tonic-gate 		 * from the redirection list.)
477*0Sstevel@tonic-gate 		 */
478*0Sstevel@tonic-gate 		if ((err = VOP_IOCTL(f->f_vnode, I_PUSH, (intptr_t)"redirmod",
479*0Sstevel@tonic-gate 		    (FREAD | FKIOCTL), cred, rvalp)) != 0) {
480*0Sstevel@tonic-gate 			iwscn_thread = NULL;
481*0Sstevel@tonic-gate 			kmem_free(mdp, sizeof (*mdp));
482*0Sstevel@tonic-gate 			releasef((int)arg);
483*0Sstevel@tonic-gate 			break;
484*0Sstevel@tonic-gate 		}
485*0Sstevel@tonic-gate 		iwscn_thread = NULL;	/* clear authorization for wcmopen() */
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 		/*
488*0Sstevel@tonic-gate 		 * Push it onto the redirection stack.
489*0Sstevel@tonic-gate 		 */
490*0Sstevel@tonic-gate 		wlp = srpush(&wd->wd_list, f->f_vnode);
491*0Sstevel@tonic-gate 		/*
492*0Sstevel@tonic-gate 		 * Fill in the redirecting module instance's private data with
493*0Sstevel@tonic-gate 		 * information to let it get to our redirection list when its
494*0Sstevel@tonic-gate 		 * close routine is called.  Cross-link it with the
495*0Sstevel@tonic-gate 		 * redirection list entry.
496*0Sstevel@tonic-gate 		 */
497*0Sstevel@tonic-gate 		mdp->wm_wd = wd;
498*0Sstevel@tonic-gate 		mdp->wm_entry = wlp;
499*0Sstevel@tonic-gate 		wlp->wl_data = mdp;
500*0Sstevel@tonic-gate 		releasef((int)arg);
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 		break;
503*0Sstevel@tonic-gate 	    }
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	case SRIOCISREDIR:
506*0Sstevel@tonic-gate 		rw_enter(&iwscn_lock, RW_READER);
507*0Sstevel@tonic-gate 		wd = srilookup(getminor(dev));
508*0Sstevel@tonic-gate 		if ((f = getf((int)arg)) == NULL) {
509*0Sstevel@tonic-gate 			err = EBADF;
510*0Sstevel@tonic-gate 			break;
511*0Sstevel@tonic-gate 		}
512*0Sstevel@tonic-gate 		/*
513*0Sstevel@tonic-gate 		 * Return value is 1 if the argument descriptor is the current
514*0Sstevel@tonic-gate 		 * redirection target, and 0 otherwise.
515*0Sstevel@tonic-gate 		 */
516*0Sstevel@tonic-gate 		*rvalp = (f->f_vnode == wd->wd_list->wl_vp) ? 1 : 0;
517*0Sstevel@tonic-gate 		releasef((int)arg);
518*0Sstevel@tonic-gate 		break;
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	case I_POP: {
521*0Sstevel@tonic-gate 		/*
522*0Sstevel@tonic-gate 		 * XXX - This is a big kludge the handles a deadlock case
523*0Sstevel@tonic-gate 		 * when we are trying to pop off the redirection
524*0Sstevel@tonic-gate 		 * module.  Since this should only happen on a close
525*0Sstevel@tonic-gate 		 * of the device, and since it hangs the system, just
526*0Sstevel@tonic-gate 		 * do not allow a pop of the redirection module to happen.
527*0Sstevel@tonic-gate 		 * Popping other modules is allowed.
528*0Sstevel@tonic-gate 		 */
529*0Sstevel@tonic-gate 		struct stdata	*stp;
530*0Sstevel@tonic-gate 		char modname[FMNAMESZ + 1] = " ";
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 		rw_enter(&iwscn_lock, RW_READER);
533*0Sstevel@tonic-gate 		wd = srilookup(getminor(dev));
534*0Sstevel@tonic-gate 		(void) strioctl(wd->wd_list->wl_vp, I_LOOK, (intptr_t)modname,
535*0Sstevel@tonic-gate 		    flag, K_TO_K, cred, rvalp);
536*0Sstevel@tonic-gate 		if (strcmp("redirmod", modname) == 0) {
537*0Sstevel@tonic-gate 			if ((f = getf((int)arg)) == NULL) {
538*0Sstevel@tonic-gate 				err = EBADF;
539*0Sstevel@tonic-gate 				break;
540*0Sstevel@tonic-gate 			}
541*0Sstevel@tonic-gate 			if ((stp = f->f_vnode->v_stream) == NULL) {
542*0Sstevel@tonic-gate 				err = ENOSTR;
543*0Sstevel@tonic-gate 				releasef((int)arg);
544*0Sstevel@tonic-gate 				break;
545*0Sstevel@tonic-gate 			}
546*0Sstevel@tonic-gate 			if (!(stp->sd_flag & STRCLOSE)) {
547*0Sstevel@tonic-gate 				releasef((int)arg);
548*0Sstevel@tonic-gate 				rw_exit(&iwscn_lock);
549*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "Popping of redirection "
550*0Sstevel@tonic-gate 				    "module not allowed");
551*0Sstevel@tonic-gate 				return (EINVAL);
552*0Sstevel@tonic-gate 			}
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 			releasef((int)arg);
555*0Sstevel@tonic-gate 		}
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 		/* Process ioctl normally */
558*0Sstevel@tonic-gate 		err = strioctl(wd->wd_list->wl_vp, cmd, arg, flag, U_TO_K,
559*0Sstevel@tonic-gate 		    cred, rvalp);
560*0Sstevel@tonic-gate 		break;
561*0Sstevel@tonic-gate 	}
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 	default:
564*0Sstevel@tonic-gate 		rw_enter(&iwscn_lock, RW_READER);
565*0Sstevel@tonic-gate 		wd = srilookup(getminor(dev));
566*0Sstevel@tonic-gate 		err = strioctl(wd->wd_list->wl_vp, cmd, arg, flag, U_TO_K,
567*0Sstevel@tonic-gate 			cred, rvalp);
568*0Sstevel@tonic-gate 		break;
569*0Sstevel@tonic-gate 	}
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	rw_exit(&iwscn_lock);
572*0Sstevel@tonic-gate 	return (err);
573*0Sstevel@tonic-gate }
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate static int
576*0Sstevel@tonic-gate iwscnpoll(
577*0Sstevel@tonic-gate 	dev_t			dev,
578*0Sstevel@tonic-gate 	short			events,
579*0Sstevel@tonic-gate 	int			anyyet,
580*0Sstevel@tonic-gate 	short			*reventsp,
581*0Sstevel@tonic-gate 	struct pollhead		**phpp)
582*0Sstevel@tonic-gate {
583*0Sstevel@tonic-gate 	wcd_data_t	*wd;
584*0Sstevel@tonic-gate 	int	error;
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 	rw_enter(&iwscn_lock, RW_READER);
587*0Sstevel@tonic-gate 	wd = srilookup(getminor(dev));
588*0Sstevel@tonic-gate 	error = strpoll(wd->wd_list->wl_vp->v_stream, events, anyyet,
589*0Sstevel@tonic-gate 		    reventsp, phpp);
590*0Sstevel@tonic-gate 	rw_exit(&iwscn_lock);
591*0Sstevel@tonic-gate 	return (error);
592*0Sstevel@tonic-gate }
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 
595*0Sstevel@tonic-gate /*
596*0Sstevel@tonic-gate  * Auxiliary routines.
597*0Sstevel@tonic-gate  */
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate /*
600*0Sstevel@tonic-gate  * Additional public interfaces.
601*0Sstevel@tonic-gate  */
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate /*
604*0Sstevel@tonic-gate  * Reset the current workstation console designee to the device denoted by the
605*0Sstevel@tonic-gate  * wl_vp field of the first entry in the redirection list.  Called from
606*0Sstevel@tonic-gate  * iwscnopen and from the SRIOCSREDIR case of iwscnioctl, in both cases after
607*0Sstevel@tonic-gate  * the target vp has been set to its new value.
608*0Sstevel@tonic-gate  */
609*0Sstevel@tonic-gate static int
610*0Sstevel@tonic-gate srreset(wcd_data_t *wd, int flag, cred_t *cred)
611*0Sstevel@tonic-gate {
612*0Sstevel@tonic-gate 	wcrlist_t	*wlp;
613*0Sstevel@tonic-gate 	int		err = 0;
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&iwscn_lock) || MUTEX_HELD(&iwscn_open_lock));
616*0Sstevel@tonic-gate 	wlp = wd->wd_list;		/* first entry */
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	/*
619*0Sstevel@tonic-gate 	 * If we're reverting back to the workstation console, make sure it's
620*0Sstevel@tonic-gate 	 * open.
621*0Sstevel@tonic-gate 	 */
622*0Sstevel@tonic-gate 	if (wlp != NULL && wlp->wl_vp == wd->wd_vp) {
623*0Sstevel@tonic-gate 		vnode_t	*vp = wd->wd_vp;	/* underlying device's vp */
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 		err = VOP_OPEN(&vp, flag, cred);
626*0Sstevel@tonic-gate 		/*
627*0Sstevel@tonic-gate 		 * The underlying driver is not allowed to have cloned itself
628*0Sstevel@tonic-gate 		 * for this open.
629*0Sstevel@tonic-gate 		 */
630*0Sstevel@tonic-gate 		if (vp != wd->wd_vp) {
631*0Sstevel@tonic-gate 			panic("srreset: Illegal clone");
632*0Sstevel@tonic-gate 			/*NOTREACHED*/
633*0Sstevel@tonic-gate 		}
634*0Sstevel@tonic-gate 		if (!err)
635*0Sstevel@tonic-gate 			wd->wd_wsconsopen++;
636*0Sstevel@tonic-gate 	}
637*0Sstevel@tonic-gate 	return (err);
638*0Sstevel@tonic-gate }
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate /*
641*0Sstevel@tonic-gate  * Remove vp from the redirection list rooted at *rwlp, should it be there.
642*0Sstevel@tonic-gate  * If zap is nonzero, deallocate the entry and remove dangling references to
643*0Sstevel@tonic-gate  * the it from the corresponding redirecting module instance's wcm_data
644*0Sstevel@tonic-gate  * structure.
645*0Sstevel@tonic-gate  *
646*0Sstevel@tonic-gate  * If the entry doesn't exist upon completion, return NULL; otherwise return a
647*0Sstevel@tonic-gate  * pointer to it.
648*0Sstevel@tonic-gate  */
649*0Sstevel@tonic-gate static wcrlist_t *
650*0Sstevel@tonic-gate srrm(wcrlist_t **rwlp, vnode_t *vp, int zap)
651*0Sstevel@tonic-gate {
652*0Sstevel@tonic-gate 	wcrlist_t	**delwlp;
653*0Sstevel@tonic-gate 	wcrlist_t	*wlp;
654*0Sstevel@tonic-gate 	wcm_data_t	*mdp;
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&iwscn_lock) || MUTEX_HELD(&iwscn_open_lock));
657*0Sstevel@tonic-gate 	for (delwlp = rwlp; (wlp = *delwlp) != NULL; delwlp = &wlp->wl_next)
658*0Sstevel@tonic-gate 		if (wlp->wl_vp == vp)
659*0Sstevel@tonic-gate 			break;
660*0Sstevel@tonic-gate 	if (wlp == NULL)
661*0Sstevel@tonic-gate 		return (NULL);
662*0Sstevel@tonic-gate 	*delwlp = wlp->wl_next;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	if (zap == 0)
665*0Sstevel@tonic-gate 		return (wlp);
666*0Sstevel@tonic-gate 
667*0Sstevel@tonic-gate 	if (wlp->wl_vp == vp)
668*0Sstevel@tonic-gate 		VN_RELE(vp);
669*0Sstevel@tonic-gate 	/*
670*0Sstevel@tonic-gate 	 * Make sure there are no dangling references leading to the entry
671*0Sstevel@tonic-gate 	 * from the corresponding redirecting module instance.
672*0Sstevel@tonic-gate 	 */
673*0Sstevel@tonic-gate 	if ((mdp = wlp->wl_data) != NULL) {
674*0Sstevel@tonic-gate 		mdp->wm_wd = NULL;
675*0Sstevel@tonic-gate 		mdp->wm_entry = NULL;
676*0Sstevel@tonic-gate 	}
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 	kmem_free(wlp, sizeof (*wlp));
679*0Sstevel@tonic-gate 	return (NULL);
680*0Sstevel@tonic-gate }
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate /*
683*0Sstevel@tonic-gate  * srpop - remove redirection because the target stream is being closed.
684*0Sstevel@tonic-gate  * Called from wcmclose().
685*0Sstevel@tonic-gate  */
686*0Sstevel@tonic-gate void
687*0Sstevel@tonic-gate srpop(wcm_data_t *mdp, int flag, cred_t *cred)
688*0Sstevel@tonic-gate {
689*0Sstevel@tonic-gate 	wcd_data_t	*ddp;
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 	rw_enter(&iwscn_lock, RW_WRITER);
692*0Sstevel@tonic-gate 	if ((ddp = mdp->wm_wd) != NULL) {
693*0Sstevel@tonic-gate 		ASSERT(mdp->wm_entry != NULL);
694*0Sstevel@tonic-gate 		(void) srrm(&ddp->wd_list, mdp->wm_entry->wl_vp, 1);
695*0Sstevel@tonic-gate 		(void) srreset(ddp, flag, cred);
696*0Sstevel@tonic-gate 	}
697*0Sstevel@tonic-gate 	rw_exit(&iwscn_lock);
698*0Sstevel@tonic-gate }
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate /*
701*0Sstevel@tonic-gate  * Routines for allocating, deallocating, and finding wcd_data structures.
702*0Sstevel@tonic-gate  *
703*0Sstevel@tonic-gate  * For a given instantiation of the driver, its open instance structures are
704*0Sstevel@tonic-gate  * linked together into a list, on the assumption that there will never be
705*0Sstevel@tonic-gate  * enough open instances to make search efficiency a serious concern.
706*0Sstevel@tonic-gate  */
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate /*
709*0Sstevel@tonic-gate  * Look up the instance structure denoted by unit.
710*0Sstevel@tonic-gate  */
711*0Sstevel@tonic-gate static wcd_data_t *
712*0Sstevel@tonic-gate srilookup(minor_t unit)
713*0Sstevel@tonic-gate {
714*0Sstevel@tonic-gate 	wcd_data_t	*wd = wcddata;
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&iwscn_lock));
717*0Sstevel@tonic-gate 	for (; wd != NULL && wd->wd_unit != unit; wd = wd->wd_next)
718*0Sstevel@tonic-gate 		continue;
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	return (wd);
721*0Sstevel@tonic-gate }
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate /*
724*0Sstevel@tonic-gate  * Allocate a wcd_data structure for the instance denoted by unit, link it in
725*0Sstevel@tonic-gate  * place, and return a pointer to it.  If it's already allocated, simply
726*0Sstevel@tonic-gate  * return a pointer to it.
727*0Sstevel@tonic-gate  */
728*0Sstevel@tonic-gate static wcd_data_t *
729*0Sstevel@tonic-gate srialloc(minor_t unit)
730*0Sstevel@tonic-gate {
731*0Sstevel@tonic-gate 	wcd_data_t	*wdp;
732*0Sstevel@tonic-gate 	wcd_data_t	**wdpp;
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&iwscn_open_lock));
735*0Sstevel@tonic-gate 	for (wdpp = &wcddata; (wdp = *wdpp) != NULL; wdpp = &wdp->wd_next) {
736*0Sstevel@tonic-gate 		if (unit < wdp->wd_unit)
737*0Sstevel@tonic-gate 			break;
738*0Sstevel@tonic-gate 		if (unit == wdp->wd_unit) {
739*0Sstevel@tonic-gate 			/* Already allocated and in place. */
740*0Sstevel@tonic-gate 			return (wdp);
741*0Sstevel@tonic-gate 		}
742*0Sstevel@tonic-gate 	}
743*0Sstevel@tonic-gate 	/*
744*0Sstevel@tonic-gate 	 * wdpp now points to the proper insertion point for unit's
745*0Sstevel@tonic-gate 	 * per-instance structure.
746*0Sstevel@tonic-gate 	 */
747*0Sstevel@tonic-gate 	wdp = kmem_zalloc(sizeof (*wdp), KM_SLEEP);
748*0Sstevel@tonic-gate 	wdp->wd_unit = unit;
749*0Sstevel@tonic-gate 	wdp->wd_next = *wdpp;
750*0Sstevel@tonic-gate 	*wdpp = wdp;
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 	return (wdp);
753*0Sstevel@tonic-gate }
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate /*
756*0Sstevel@tonic-gate  * Deallocate the wcd_data structure denoted by wd and unlink it from the
757*0Sstevel@tonic-gate  * list of open instances.
758*0Sstevel@tonic-gate  */
759*0Sstevel@tonic-gate static void
760*0Sstevel@tonic-gate sridealloc(wcd_data_t *wd)
761*0Sstevel@tonic-gate {
762*0Sstevel@tonic-gate 	wcd_data_t	*wdp;
763*0Sstevel@tonic-gate 	wcd_data_t	**wdpp;
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&iwscn_lock) || MUTEX_HELD(&iwscn_open_lock));
766*0Sstevel@tonic-gate 	for (wdpp = &wcddata; (wdp = *wdpp) != NULL; wdpp = &wdp->wd_next)
767*0Sstevel@tonic-gate 		if (wd == wdp)
768*0Sstevel@tonic-gate 			break;
769*0Sstevel@tonic-gate 	if (wdp == NULL) {
770*0Sstevel@tonic-gate 		/*
771*0Sstevel@tonic-gate 		 * Not there.  This should probably be a panic.
772*0Sstevel@tonic-gate 		 */
773*0Sstevel@tonic-gate 		return;
774*0Sstevel@tonic-gate 	}
775*0Sstevel@tonic-gate 	*wdpp = wdp->wd_next;
776*0Sstevel@tonic-gate 	kmem_free(wdp, sizeof (*wdp));
777*0Sstevel@tonic-gate }
778*0Sstevel@tonic-gate 
779*0Sstevel@tonic-gate 
780*0Sstevel@tonic-gate /*
781*0Sstevel@tonic-gate  * Push vp onto the redirection list rooted at *wlpp.  If it's already there,
782*0Sstevel@tonic-gate  * move it to the front position.  Return a pointer to its list entry.
783*0Sstevel@tonic-gate  *
784*0Sstevel@tonic-gate  * N.B.: It is the caller's responsibility to initialize all fields in the
785*0Sstevel@tonic-gate  * entry other than the wl_next and wl_vp fields.
786*0Sstevel@tonic-gate  */
787*0Sstevel@tonic-gate static wcrlist_t *
788*0Sstevel@tonic-gate srpush(wcrlist_t **wlpp, vnode_t *vp)
789*0Sstevel@tonic-gate {
790*0Sstevel@tonic-gate 	wcrlist_t	*nwlp;
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&iwscn_lock) || MUTEX_HELD(&iwscn_open_lock));
793*0Sstevel@tonic-gate 	if ((nwlp = srrm(wlpp, vp, 0)) == NULL) {
794*0Sstevel@tonic-gate 		nwlp = kmem_zalloc(sizeof (*nwlp), KM_SLEEP);
795*0Sstevel@tonic-gate 		nwlp->wl_vp = vp;
796*0Sstevel@tonic-gate 		/*
797*0Sstevel@tonic-gate 		 * The hold will prevent underlying device from closing
798*0Sstevel@tonic-gate 		 * while this vnode is still on the redirection list.
799*0Sstevel@tonic-gate 		 */
800*0Sstevel@tonic-gate 		VN_HOLD(vp);
801*0Sstevel@tonic-gate 	}
802*0Sstevel@tonic-gate 	nwlp->wl_next = *wlpp;
803*0Sstevel@tonic-gate 	*wlpp = nwlp;
804*0Sstevel@tonic-gate 
805*0Sstevel@tonic-gate 	return (nwlp);
806*0Sstevel@tonic-gate }
807