xref: /titanic_51/usr/src/uts/common/io/clone.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" /* from S5R4 1.10 */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * Clone Driver.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/open.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate int clnopen(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *crp);
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static struct module_info clnm_info = { 0, "CLONE", 0, 0, 0, 0 };
64*7c478bd9Sstevel@tonic-gate static struct qinit clnrinit = { NULL, NULL, clnopen, NULL, NULL, &clnm_info,
65*7c478bd9Sstevel@tonic-gate     NULL };
66*7c478bd9Sstevel@tonic-gate static struct qinit clnwinit = { NULL, NULL, NULL, NULL, NULL, &clnm_info,
67*7c478bd9Sstevel@tonic-gate     NULL };
68*7c478bd9Sstevel@tonic-gate struct streamtab clninfo = { &clnrinit, &clnwinit };
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate static int cln_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
71*7c478bd9Sstevel@tonic-gate static int cln_attach(dev_info_t *, ddi_attach_cmd_t);
72*7c478bd9Sstevel@tonic-gate static dev_info_t *cln_dip;		/* private copy of devinfo pointer */
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #define	CLONE_CONF_FLAG		(D_NEW|D_MP)
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(clone_ops, nulldev, nulldev, cln_attach, nodev, nodev, \
77*7c478bd9Sstevel@tonic-gate     cln_info, CLONE_CONF_FLAG, &clninfo);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate /*
80*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
81*7c478bd9Sstevel@tonic-gate  */
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
84*7c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
85*7c478bd9Sstevel@tonic-gate 	"Clone Pseudodriver 'clone'",
86*7c478bd9Sstevel@tonic-gate 	&clone_ops,	/* driver ops */
87*7c478bd9Sstevel@tonic-gate };
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
90*7c478bd9Sstevel@tonic-gate 	MODREV_1,
91*7c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
92*7c478bd9Sstevel@tonic-gate 	NULL
93*7c478bd9Sstevel@tonic-gate };
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate int
97*7c478bd9Sstevel@tonic-gate _init(void)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate int
103*7c478bd9Sstevel@tonic-gate _fini(void)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	/*
106*7c478bd9Sstevel@tonic-gate 	 * Since the clone driver's reference count is unreliable,
107*7c478bd9Sstevel@tonic-gate 	 * make sure we are never unloaded.
108*7c478bd9Sstevel@tonic-gate 	 */
109*7c478bd9Sstevel@tonic-gate 	return (EBUSY);
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate int
113*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
119*7c478bd9Sstevel@tonic-gate static int
120*7c478bd9Sstevel@tonic-gate cln_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	cln_dip = devi;
123*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
127*7c478bd9Sstevel@tonic-gate static int
128*7c478bd9Sstevel@tonic-gate cln_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
129*7c478bd9Sstevel@tonic-gate 	void **result)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	int error;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
134*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
135*7c478bd9Sstevel@tonic-gate 		if (cln_dip == NULL) {
136*7c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
137*7c478bd9Sstevel@tonic-gate 		} else {
138*7c478bd9Sstevel@tonic-gate 			*result = (void *)cln_dip;
139*7c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
140*7c478bd9Sstevel@tonic-gate 		}
141*7c478bd9Sstevel@tonic-gate 		break;
142*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
143*7c478bd9Sstevel@tonic-gate 		*result = (void *)0;
144*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
145*7c478bd9Sstevel@tonic-gate 		break;
146*7c478bd9Sstevel@tonic-gate 	default:
147*7c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	return (error);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate /*
153*7c478bd9Sstevel@tonic-gate  * Clone open.  Maj is the major device number of the streams
154*7c478bd9Sstevel@tonic-gate  * device to open.  Look up the device in the cdevsw[].  Attach
155*7c478bd9Sstevel@tonic-gate  * its qinit structures to the read and write queues and call its
156*7c478bd9Sstevel@tonic-gate  * open with the sflag set to CLONEOPEN.  Swap in a new vnode with
157*7c478bd9Sstevel@tonic-gate  * the real device number constructed from either
158*7c478bd9Sstevel@tonic-gate  *	a) for old-style drivers:
159*7c478bd9Sstevel@tonic-gate  *		maj and the minor returned by the device open, or
160*7c478bd9Sstevel@tonic-gate  *	b) for new-style drivers:
161*7c478bd9Sstevel@tonic-gate  *		the whole dev passed back as a reference parameter
162*7c478bd9Sstevel@tonic-gate  *		from the device open.
163*7c478bd9Sstevel@tonic-gate  */
164*7c478bd9Sstevel@tonic-gate int
165*7c478bd9Sstevel@tonic-gate clnopen(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *crp)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	struct streamtab *str;
168*7c478bd9Sstevel@tonic-gate 	dev_t newdev;
169*7c478bd9Sstevel@tonic-gate 	int error = 0;
170*7c478bd9Sstevel@tonic-gate 	major_t maj;
171*7c478bd9Sstevel@tonic-gate 	minor_t emaj;
172*7c478bd9Sstevel@tonic-gate 	struct qinit *rinit, *winit;
173*7c478bd9Sstevel@tonic-gate 	cdevsw_impl_t *dp;
174*7c478bd9Sstevel@tonic-gate 	uint32_t qflag;
175*7c478bd9Sstevel@tonic-gate 	uint32_t sqtype;
176*7c478bd9Sstevel@tonic-gate 	perdm_t *dmp;
177*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	if (sflag)
180*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	/*
183*7c478bd9Sstevel@tonic-gate 	 * Get the device to open.
184*7c478bd9Sstevel@tonic-gate 	 */
185*7c478bd9Sstevel@tonic-gate 	emaj = getminor(*devp); /* minor is major for a cloned driver */
186*7c478bd9Sstevel@tonic-gate 	maj = etoimajor(emaj);	/* get internal major of cloned driver */
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	if (maj >= devcnt)
189*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	/*
192*7c478bd9Sstevel@tonic-gate 	 * NOTE We call ddi_hold_installed_driver() here to attach
193*7c478bd9Sstevel@tonic-gate 	 *	all instances of the driver, since we do not know
194*7c478bd9Sstevel@tonic-gate 	 *	a priori which instance the Stream is associated with.
195*7c478bd9Sstevel@tonic-gate 	 *
196*7c478bd9Sstevel@tonic-gate 	 *	For Style-2 network drivers, we know that the association
197*7c478bd9Sstevel@tonic-gate 	 *	happens at DL_ATTACH time. For other types of drivers,
198*7c478bd9Sstevel@tonic-gate 	 *	open probably requires attaching instance 0 (pseudo dip).
199*7c478bd9Sstevel@tonic-gate 	 *
200*7c478bd9Sstevel@tonic-gate 	 *	To eliminate ddi_hold_installed_driver(), the following
201*7c478bd9Sstevel@tonic-gate 	 *	should happen:
202*7c478bd9Sstevel@tonic-gate 	 *
203*7c478bd9Sstevel@tonic-gate 	 *	- GLD be modified to include gld_init(). The driver will
204*7c478bd9Sstevel@tonic-gate 	 *	  register information for gld_open() to succeed. It will
205*7c478bd9Sstevel@tonic-gate 	 *	  also inform framework if driver assigns instance=PPA.
206*7c478bd9Sstevel@tonic-gate 	 *	- ddi_hold_devi_by_instance() be modified to actively
207*7c478bd9Sstevel@tonic-gate 	 *	  attach the instance via top-down enumeration.
208*7c478bd9Sstevel@tonic-gate 	 */
209*7c478bd9Sstevel@tonic-gate 	if (ddi_hold_installed_driver(maj) == NULL)
210*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	if ((str = STREAMSTAB(maj)) == NULL) {
213*7c478bd9Sstevel@tonic-gate 		ddi_rele_driver(maj);
214*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	newdev = makedevice(emaj, 0);	/* create new style device number  */
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	/*
220*7c478bd9Sstevel@tonic-gate 	 * Check for security here. For DLPI style 2 network
221*7c478bd9Sstevel@tonic-gate 	 * drivers, we need to apply the default network policy.
222*7c478bd9Sstevel@tonic-gate 	 * Clone is weird in that the network driver isn't loaded
223*7c478bd9Sstevel@tonic-gate 	 * and attached at spec_open() time, we need to load the
224*7c478bd9Sstevel@tonic-gate 	 * driver to see if it is a network driver. Hence, we
225*7c478bd9Sstevel@tonic-gate 	 * check security here (after ddi_hold_installed_driver
226*7c478bd9Sstevel@tonic-gate 	 * call above).
227*7c478bd9Sstevel@tonic-gate 	 */
228*7c478bd9Sstevel@tonic-gate 	vp = makespecvp(newdev, VCHR);
229*7c478bd9Sstevel@tonic-gate 	error = secpolicy_spec_open(crp, vp, flag);
230*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
231*7c478bd9Sstevel@tonic-gate 	if (error) {
232*7c478bd9Sstevel@tonic-gate 		ddi_rele_driver(maj);
233*7c478bd9Sstevel@tonic-gate 		return (error);
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	/*
237*7c478bd9Sstevel@tonic-gate 	 * Save so that we can restore the q on failure.
238*7c478bd9Sstevel@tonic-gate 	 */
239*7c478bd9Sstevel@tonic-gate 	rinit = rq->q_qinfo;
240*7c478bd9Sstevel@tonic-gate 	winit = WR(rq)->q_qinfo;
241*7c478bd9Sstevel@tonic-gate 	ASSERT(rq->q_syncq->sq_type == (SQ_CI|SQ_CO));
242*7c478bd9Sstevel@tonic-gate 	ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE);
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	dp = &devimpl[maj];
245*7c478bd9Sstevel@tonic-gate 	ASSERT(str == dp->d_str);
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	qflag = dp->d_qflag;
248*7c478bd9Sstevel@tonic-gate 	sqtype = dp->d_sqtype;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	/* create perdm_t if needed */
251*7c478bd9Sstevel@tonic-gate 	if (NEED_DM(dp->d_dmp, qflag))
252*7c478bd9Sstevel@tonic-gate 		dp->d_dmp = hold_dm(str, qflag, sqtype);
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	dmp = dp->d_dmp;
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	/*
257*7c478bd9Sstevel@tonic-gate 	 * Set the syncq state what qattach started off with. This is safe
258*7c478bd9Sstevel@tonic-gate 	 * since no other thread can access this queue at this point
259*7c478bd9Sstevel@tonic-gate 	 * (stream open, close, push, and pop are single threaded
260*7c478bd9Sstevel@tonic-gate 	 * by the framework.)
261*7c478bd9Sstevel@tonic-gate 	 */
262*7c478bd9Sstevel@tonic-gate 	leavesq(rq->q_syncq, SQ_OPENCLOSE);
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	/*
265*7c478bd9Sstevel@tonic-gate 	 * Substitute the real qinit values for the current ones.
266*7c478bd9Sstevel@tonic-gate 	 */
267*7c478bd9Sstevel@tonic-gate 	/* setq might sleep in kmem_alloc - avoid holding locks. */
268*7c478bd9Sstevel@tonic-gate 	setq(rq, str->st_rdinit, str->st_wrinit, dmp, qflag, sqtype, B_FALSE);
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	/*
271*7c478bd9Sstevel@tonic-gate 	 * Open the attached module or driver.
272*7c478bd9Sstevel@tonic-gate 	 *
273*7c478bd9Sstevel@tonic-gate 	 * If there is an outer perimeter get exclusive access during
274*7c478bd9Sstevel@tonic-gate 	 * the open procedure.
275*7c478bd9Sstevel@tonic-gate 	 * Bump up the reference count on the queue.
276*7c478bd9Sstevel@tonic-gate 	 */
277*7c478bd9Sstevel@tonic-gate 	entersq(rq->q_syncq, SQ_OPENCLOSE);
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	/*
280*7c478bd9Sstevel@tonic-gate 	 * Call the device open with the stream flag CLONEOPEN.  The device
281*7c478bd9Sstevel@tonic-gate 	 * will either fail this or return the device number.
282*7c478bd9Sstevel@tonic-gate 	 */
283*7c478bd9Sstevel@tonic-gate 	error = (*rq->q_qinfo->qi_qopen)(rq, &newdev, flag, CLONEOPEN, crp);
284*7c478bd9Sstevel@tonic-gate 	if (error != 0)
285*7c478bd9Sstevel@tonic-gate 		goto failed;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	*devp = newdev;
288*7c478bd9Sstevel@tonic-gate 	if (getmajor(newdev) != emaj)
289*7c478bd9Sstevel@tonic-gate 		goto bad_major;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	return (0);
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate bad_major:
294*7c478bd9Sstevel@tonic-gate 	/*
295*7c478bd9Sstevel@tonic-gate 	 * Close the device
296*7c478bd9Sstevel@tonic-gate 	 */
297*7c478bd9Sstevel@tonic-gate 	(*rq->q_qinfo->qi_qclose)(rq, flag, crp);
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
300*7c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "cannot clone major number %d(%s)->%d", emaj,
301*7c478bd9Sstevel@tonic-gate 	    ddi_major_to_name(emaj), getmajor(newdev));
302*7c478bd9Sstevel@tonic-gate #endif
303*7c478bd9Sstevel@tonic-gate 	error = ENXIO;
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate failed:
306*7c478bd9Sstevel@tonic-gate 	/*
307*7c478bd9Sstevel@tonic-gate 	 * open failed; pretty up to look like original
308*7c478bd9Sstevel@tonic-gate 	 * queue.
309*7c478bd9Sstevel@tonic-gate 	 */
310*7c478bd9Sstevel@tonic-gate 	if (backq(WR(rq)) && backq(WR(rq))->q_next == WR(rq))
311*7c478bd9Sstevel@tonic-gate 		qprocsoff(rq);
312*7c478bd9Sstevel@tonic-gate 	leavesq(rq->q_syncq, SQ_OPENCLOSE);
313*7c478bd9Sstevel@tonic-gate 	rq->q_next = WR(rq)->q_next = NULL;
314*7c478bd9Sstevel@tonic-gate 	ASSERT(flush_syncq(rq->q_syncq, rq) == 0);
315*7c478bd9Sstevel@tonic-gate 	ASSERT(flush_syncq(WR(rq)->q_syncq, WR(rq)) == 0);
316*7c478bd9Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = NULL;
317*7c478bd9Sstevel@tonic-gate 	/* setq might sleep in kmem_alloc - avoid holding locks. */
318*7c478bd9Sstevel@tonic-gate 	setq(rq, rinit, winit, NULL, QMTSAFE, SQ_CI|SQ_CO,
319*7c478bd9Sstevel@tonic-gate 	    B_FALSE);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	/* Restore back to what qattach will expect */
322*7c478bd9Sstevel@tonic-gate 	entersq(rq->q_syncq, SQ_OPENCLOSE);
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	ddi_rele_driver(maj);
325*7c478bd9Sstevel@tonic-gate 	return (error);
326*7c478bd9Sstevel@tonic-gate }
327