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