xref: /onnv-gate/usr/src/uts/common/io/clone.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Clone Driver.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/signal.h>
390Sstevel@tonic-gate #include <sys/vfs.h>
400Sstevel@tonic-gate #include <sys/vnode.h>
410Sstevel@tonic-gate #include <sys/pcb.h>
420Sstevel@tonic-gate #include <sys/user.h>
430Sstevel@tonic-gate #include <sys/stropts.h>
440Sstevel@tonic-gate #include <sys/stream.h>
450Sstevel@tonic-gate #include <sys/errno.h>
460Sstevel@tonic-gate #include <sys/sysinfo.h>
470Sstevel@tonic-gate #include <sys/systm.h>
480Sstevel@tonic-gate #include <sys/conf.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/cred.h>
510Sstevel@tonic-gate #include <sys/mkdev.h>
520Sstevel@tonic-gate #include <sys/open.h>
530Sstevel@tonic-gate #include <sys/strsubr.h>
540Sstevel@tonic-gate #include <sys/ddi.h>
550Sstevel@tonic-gate #include <sys/sunddi.h>
560Sstevel@tonic-gate #include <sys/modctl.h>
570Sstevel@tonic-gate #include <sys/policy.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate int clnopen(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *crp);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static struct module_info clnm_info = { 0, "CLONE", 0, 0, 0, 0 };
620Sstevel@tonic-gate static struct qinit clnrinit = { NULL, NULL, clnopen, NULL, NULL, &clnm_info,
630Sstevel@tonic-gate     NULL };
640Sstevel@tonic-gate static struct qinit clnwinit = { NULL, NULL, NULL, NULL, NULL, &clnm_info,
650Sstevel@tonic-gate     NULL };
660Sstevel@tonic-gate struct streamtab clninfo = { &clnrinit, &clnwinit };
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static int cln_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
690Sstevel@tonic-gate static int cln_attach(dev_info_t *, ddi_attach_cmd_t);
700Sstevel@tonic-gate static dev_info_t *cln_dip;		/* private copy of devinfo pointer */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	CLONE_CONF_FLAG		(D_NEW|D_MP)
730Sstevel@tonic-gate 
740Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(clone_ops, nulldev, nulldev, cln_attach, nodev, nodev, \
75*7656SSherry.Moore@Sun.COM     cln_info, CLONE_CONF_FLAG, &clninfo, ddi_quiesce_not_needed);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Module linkage information for the kernel.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static struct modldrv modldrv = {
820Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
830Sstevel@tonic-gate 	"Clone Pseudodriver 'clone'",
840Sstevel@tonic-gate 	&clone_ops,	/* driver ops */
850Sstevel@tonic-gate };
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static struct modlinkage modlinkage = {
880Sstevel@tonic-gate 	MODREV_1,
890Sstevel@tonic-gate 	(void *)&modldrv,
900Sstevel@tonic-gate 	NULL
910Sstevel@tonic-gate };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 
940Sstevel@tonic-gate int
_init(void)950Sstevel@tonic-gate _init(void)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	return (mod_install(&modlinkage));
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate int
_fini(void)1010Sstevel@tonic-gate _fini(void)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	/*
1040Sstevel@tonic-gate 	 * Since the clone driver's reference count is unreliable,
1050Sstevel@tonic-gate 	 * make sure we are never unloaded.
1060Sstevel@tonic-gate 	 */
1070Sstevel@tonic-gate 	return (EBUSY);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1110Sstevel@tonic-gate _info(struct modinfo *modinfop)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /* ARGSUSED */
1170Sstevel@tonic-gate static int
cln_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1180Sstevel@tonic-gate cln_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	cln_dip = devi;
1210Sstevel@tonic-gate 	return (DDI_SUCCESS);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /* ARGSUSED */
1250Sstevel@tonic-gate static int
cln_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1260Sstevel@tonic-gate cln_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1270Sstevel@tonic-gate 	void **result)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	int error;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	switch (infocmd) {
1320Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1330Sstevel@tonic-gate 		if (cln_dip == NULL) {
1340Sstevel@tonic-gate 			error = DDI_FAILURE;
1350Sstevel@tonic-gate 		} else {
1360Sstevel@tonic-gate 			*result = (void *)cln_dip;
1370Sstevel@tonic-gate 			error = DDI_SUCCESS;
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 		break;
1400Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1410Sstevel@tonic-gate 		*result = (void *)0;
1420Sstevel@tonic-gate 		error = DDI_SUCCESS;
1430Sstevel@tonic-gate 		break;
1440Sstevel@tonic-gate 	default:
1450Sstevel@tonic-gate 		error = DDI_FAILURE;
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	return (error);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Clone open.  Maj is the major device number of the streams
1520Sstevel@tonic-gate  * device to open.  Look up the device in the cdevsw[].  Attach
1530Sstevel@tonic-gate  * its qinit structures to the read and write queues and call its
1540Sstevel@tonic-gate  * open with the sflag set to CLONEOPEN.  Swap in a new vnode with
1550Sstevel@tonic-gate  * the real device number constructed from either
1560Sstevel@tonic-gate  *	a) for old-style drivers:
1570Sstevel@tonic-gate  *		maj and the minor returned by the device open, or
1580Sstevel@tonic-gate  *	b) for new-style drivers:
1590Sstevel@tonic-gate  *		the whole dev passed back as a reference parameter
1600Sstevel@tonic-gate  *		from the device open.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate int
clnopen(queue_t * rq,dev_t * devp,int flag,int sflag,cred_t * crp)1630Sstevel@tonic-gate clnopen(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *crp)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	struct streamtab *str;
1660Sstevel@tonic-gate 	dev_t newdev;
1670Sstevel@tonic-gate 	int error = 0;
1680Sstevel@tonic-gate 	major_t maj;
1690Sstevel@tonic-gate 	minor_t emaj;
1700Sstevel@tonic-gate 	struct qinit *rinit, *winit;
1710Sstevel@tonic-gate 	cdevsw_impl_t *dp;
1720Sstevel@tonic-gate 	uint32_t qflag;
1730Sstevel@tonic-gate 	uint32_t sqtype;
1740Sstevel@tonic-gate 	perdm_t *dmp;
1750Sstevel@tonic-gate 	vnode_t *vp;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (sflag)
1780Sstevel@tonic-gate 		return (ENXIO);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * Get the device to open.
1820Sstevel@tonic-gate 	 */
1830Sstevel@tonic-gate 	emaj = getminor(*devp); /* minor is major for a cloned driver */
1840Sstevel@tonic-gate 	maj = etoimajor(emaj);	/* get internal major of cloned driver */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (maj >= devcnt)
1870Sstevel@tonic-gate 		return (ENXIO);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * NOTE We call ddi_hold_installed_driver() here to attach
1910Sstevel@tonic-gate 	 *	all instances of the driver, since we do not know
1920Sstevel@tonic-gate 	 *	a priori which instance the Stream is associated with.
1930Sstevel@tonic-gate 	 *
1940Sstevel@tonic-gate 	 *	For Style-2 network drivers, we know that the association
1950Sstevel@tonic-gate 	 *	happens at DL_ATTACH time. For other types of drivers,
1960Sstevel@tonic-gate 	 *	open probably requires attaching instance 0 (pseudo dip).
1970Sstevel@tonic-gate 	 *
1980Sstevel@tonic-gate 	 *	To eliminate ddi_hold_installed_driver(), the following
1990Sstevel@tonic-gate 	 *	should happen:
2000Sstevel@tonic-gate 	 *
2010Sstevel@tonic-gate 	 *	- GLD be modified to include gld_init(). The driver will
2020Sstevel@tonic-gate 	 *	  register information for gld_open() to succeed. It will
2030Sstevel@tonic-gate 	 *	  also inform framework if driver assigns instance=PPA.
2040Sstevel@tonic-gate 	 *	- ddi_hold_devi_by_instance() be modified to actively
2050Sstevel@tonic-gate 	 *	  attach the instance via top-down enumeration.
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	if (ddi_hold_installed_driver(maj) == NULL)
2080Sstevel@tonic-gate 		return (ENXIO);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if ((str = STREAMSTAB(maj)) == NULL) {
2110Sstevel@tonic-gate 		ddi_rele_driver(maj);
2120Sstevel@tonic-gate 		return (ENXIO);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	newdev = makedevice(emaj, 0);	/* create new style device number  */
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 * Check for security here. For DLPI style 2 network
2190Sstevel@tonic-gate 	 * drivers, we need to apply the default network policy.
2200Sstevel@tonic-gate 	 * Clone is weird in that the network driver isn't loaded
2210Sstevel@tonic-gate 	 * and attached at spec_open() time, we need to load the
2220Sstevel@tonic-gate 	 * driver to see if it is a network driver. Hence, we
2230Sstevel@tonic-gate 	 * check security here (after ddi_hold_installed_driver
2240Sstevel@tonic-gate 	 * call above).
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	vp = makespecvp(newdev, VCHR);
2270Sstevel@tonic-gate 	error = secpolicy_spec_open(crp, vp, flag);
2280Sstevel@tonic-gate 	VN_RELE(vp);
2290Sstevel@tonic-gate 	if (error) {
2300Sstevel@tonic-gate 		ddi_rele_driver(maj);
2310Sstevel@tonic-gate 		return (error);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * Save so that we can restore the q on failure.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	rinit = rq->q_qinfo;
2380Sstevel@tonic-gate 	winit = WR(rq)->q_qinfo;
2390Sstevel@tonic-gate 	ASSERT(rq->q_syncq->sq_type == (SQ_CI|SQ_CO));
2400Sstevel@tonic-gate 	ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	dp = &devimpl[maj];
2430Sstevel@tonic-gate 	ASSERT(str == dp->d_str);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	qflag = dp->d_qflag;
2460Sstevel@tonic-gate 	sqtype = dp->d_sqtype;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/* create perdm_t if needed */
2490Sstevel@tonic-gate 	if (NEED_DM(dp->d_dmp, qflag))
2500Sstevel@tonic-gate 		dp->d_dmp = hold_dm(str, qflag, sqtype);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	dmp = dp->d_dmp;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * Set the syncq state what qattach started off with. This is safe
2560Sstevel@tonic-gate 	 * since no other thread can access this queue at this point
2570Sstevel@tonic-gate 	 * (stream open, close, push, and pop are single threaded
2580Sstevel@tonic-gate 	 * by the framework.)
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	leavesq(rq->q_syncq, SQ_OPENCLOSE);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Substitute the real qinit values for the current ones.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	/* setq might sleep in kmem_alloc - avoid holding locks. */
2660Sstevel@tonic-gate 	setq(rq, str->st_rdinit, str->st_wrinit, dmp, qflag, sqtype, B_FALSE);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/*
2690Sstevel@tonic-gate 	 * Open the attached module or driver.
2700Sstevel@tonic-gate 	 *
2710Sstevel@tonic-gate 	 * If there is an outer perimeter get exclusive access during
2720Sstevel@tonic-gate 	 * the open procedure.
2730Sstevel@tonic-gate 	 * Bump up the reference count on the queue.
2740Sstevel@tonic-gate 	 */
2750Sstevel@tonic-gate 	entersq(rq->q_syncq, SQ_OPENCLOSE);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * Call the device open with the stream flag CLONEOPEN.  The device
2790Sstevel@tonic-gate 	 * will either fail this or return the device number.
2800Sstevel@tonic-gate 	 */
2810Sstevel@tonic-gate 	error = (*rq->q_qinfo->qi_qopen)(rq, &newdev, flag, CLONEOPEN, crp);
2820Sstevel@tonic-gate 	if (error != 0)
2830Sstevel@tonic-gate 		goto failed;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	*devp = newdev;
2860Sstevel@tonic-gate 	if (getmajor(newdev) != emaj)
2870Sstevel@tonic-gate 		goto bad_major;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	return (0);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate bad_major:
2920Sstevel@tonic-gate 	/*
2930Sstevel@tonic-gate 	 * Close the device
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	(*rq->q_qinfo->qi_qclose)(rq, flag, crp);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate #ifdef DEBUG
2980Sstevel@tonic-gate 	cmn_err(CE_NOTE, "cannot clone major number %d(%s)->%d", emaj,
2990Sstevel@tonic-gate 	    ddi_major_to_name(emaj), getmajor(newdev));
3000Sstevel@tonic-gate #endif
3010Sstevel@tonic-gate 	error = ENXIO;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate failed:
3040Sstevel@tonic-gate 	/*
3050Sstevel@tonic-gate 	 * open failed; pretty up to look like original
3060Sstevel@tonic-gate 	 * queue.
3070Sstevel@tonic-gate 	 */
3080Sstevel@tonic-gate 	if (backq(WR(rq)) && backq(WR(rq))->q_next == WR(rq))
3090Sstevel@tonic-gate 		qprocsoff(rq);
3100Sstevel@tonic-gate 	leavesq(rq->q_syncq, SQ_OPENCLOSE);
3110Sstevel@tonic-gate 	rq->q_next = WR(rq)->q_next = NULL;
3120Sstevel@tonic-gate 	ASSERT(flush_syncq(rq->q_syncq, rq) == 0);
3130Sstevel@tonic-gate 	ASSERT(flush_syncq(WR(rq)->q_syncq, WR(rq)) == 0);
3140Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = NULL;
3150Sstevel@tonic-gate 	/* setq might sleep in kmem_alloc - avoid holding locks. */
3160Sstevel@tonic-gate 	setq(rq, rinit, winit, NULL, QMTSAFE, SQ_CI|SQ_CO,
3170Sstevel@tonic-gate 	    B_FALSE);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	/* Restore back to what qattach will expect */
3200Sstevel@tonic-gate 	entersq(rq->q_syncq, SQ_OPENCLOSE);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	ddi_rele_driver(maj);
3230Sstevel@tonic-gate 	return (error);
3240Sstevel@tonic-gate }
325