xref: /onnv-gate/usr/src/uts/common/ktli/t_kopen.c (revision 5331:3047ad28a67b)
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*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * 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 /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * Kernel TLI-like function to initialize a transport
430Sstevel@tonic-gate  * endpoint using the protocol specified.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * Returns:
460Sstevel@tonic-gate  * 	0 on success and "tiptr" is set to a valid transport pointer,
470Sstevel@tonic-gate  * 	else a positive error code.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <sys/param.h>
510Sstevel@tonic-gate #include <sys/types.h>
520Sstevel@tonic-gate #include <sys/proc.h>
530Sstevel@tonic-gate #include <sys/file.h>
540Sstevel@tonic-gate #include <sys/user.h>
550Sstevel@tonic-gate #include <sys/vnode.h>
560Sstevel@tonic-gate #include <sys/errno.h>
570Sstevel@tonic-gate #include <sys/stream.h>
580Sstevel@tonic-gate #include <sys/ioctl.h>
590Sstevel@tonic-gate #include <sys/stropts.h>
600Sstevel@tonic-gate #include <sys/strsubr.h>
610Sstevel@tonic-gate #include <sys/tihdr.h>
620Sstevel@tonic-gate #include <sys/timod.h>
630Sstevel@tonic-gate #include <sys/tiuser.h>
640Sstevel@tonic-gate #include <sys/t_kuser.h>
650Sstevel@tonic-gate #include <sys/kmem.h>
660Sstevel@tonic-gate #include <sys/cmn_err.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static t_scalar_t _t_setsize(t_scalar_t);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate int
t_kopen(file_t * fp,dev_t rdev,int flags,TIUSER ** tiptr,cred_t * cr)710Sstevel@tonic-gate t_kopen(file_t *fp, dev_t rdev, int flags, TIUSER **tiptr, cred_t *cr)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	int			madefp = 0;
740Sstevel@tonic-gate 	struct T_info_ack	inforeq;
750Sstevel@tonic-gate 	int			retval;
760Sstevel@tonic-gate 	vnode_t			*vp;
770Sstevel@tonic-gate 	struct strioctl		strioc;
780Sstevel@tonic-gate 	int			error;
790Sstevel@tonic-gate 	TIUSER			*ntiptr;
800Sstevel@tonic-gate 	int			rtries = 0;
810Sstevel@tonic-gate 
82766Scarlsonj 	/*
83766Scarlsonj 	 * Special case for install: miniroot needs to be able to access files
84766Scarlsonj 	 * via NFS as though it were always in the global zone.
85766Scarlsonj 	 */
86766Scarlsonj 	if (nfs_global_client_only != 0)
87766Scarlsonj 		cr = kcred;
88766Scarlsonj 
890Sstevel@tonic-gate 	KTLILOG(2, "t_kopen: fp %x, ", fp);
900Sstevel@tonic-gate 	KTLILOG(2, "rdev %x, ", rdev);
910Sstevel@tonic-gate 	KTLILOG(2, "flags %x\n", flags);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	*tiptr = NULL;
940Sstevel@tonic-gate 	error = 0;
950Sstevel@tonic-gate 	retval = 0;
960Sstevel@tonic-gate 	if (fp == NULL) {
970Sstevel@tonic-gate 		if (rdev == 0 || rdev == NODEV) {
980Sstevel@tonic-gate 			KTLILOG(1, "t_kopen: null device\n", 0);
990Sstevel@tonic-gate 			return (EINVAL);
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		/*
1030Sstevel@tonic-gate 		 * allocate a file pointer, but
1040Sstevel@tonic-gate 		 * no file descripter.
1050Sstevel@tonic-gate 		 */
1060Sstevel@tonic-gate 		if ((error = falloc(NULL, flags, &fp, NULL)) != 0) {
1070Sstevel@tonic-gate 			KTLILOG(1, "t_kopen: falloc: %d\n", error);
1080Sstevel@tonic-gate 			return (error);
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		/* Install proper cred in file */
1120Sstevel@tonic-gate 		if (cr != fp->f_cred) {
1130Sstevel@tonic-gate 			crhold(cr);
1140Sstevel@tonic-gate 			crfree(fp->f_cred);
1150Sstevel@tonic-gate 			fp->f_cred = cr;
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 		vp = makespecvp(rdev, VCHR);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		/*
1210Sstevel@tonic-gate 		 * this will call the streams open for us.
1220Sstevel@tonic-gate 		 * Want to retry if error is EAGAIN, the streams open routine
1230Sstevel@tonic-gate 		 * might fail due to temporarely out of memory.
1240Sstevel@tonic-gate 		 */
1250Sstevel@tonic-gate 		do {
126*5331Samw 			if ((error = VOP_OPEN(&vp, flags, cr, NULL))
127*5331Samw 			    == EAGAIN) {
1280Sstevel@tonic-gate 				(void) delay(hz);
1290Sstevel@tonic-gate 			}
1300Sstevel@tonic-gate 		} while (error == EAGAIN && ++rtries < 5);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		if (error) {
1330Sstevel@tonic-gate 			KTLILOG(1, "t_kopen: VOP_OPEN: %d\n", error);
1340Sstevel@tonic-gate 			unfalloc(fp);
1350Sstevel@tonic-gate 			VN_RELE(vp);
1360Sstevel@tonic-gate 			return (error);
1370Sstevel@tonic-gate 		}
1380Sstevel@tonic-gate 		/*
1390Sstevel@tonic-gate 		 * fp is completely initialized so drop the write lock.
1400Sstevel@tonic-gate 		 * I actually don't need any locking on fp in here since
1410Sstevel@tonic-gate 		 * there is no fd pointing at it.  However, since I could
1420Sstevel@tonic-gate 		 * call closef if there is an error and closef requires
1430Sstevel@tonic-gate 		 * the fp read locked, I will acquire the read lock here
1440Sstevel@tonic-gate 		 * and make sure I release it before I leave this routine.
1450Sstevel@tonic-gate 		 */
1460Sstevel@tonic-gate 		fp->f_vnode = vp;
1470Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 		madefp = 1;
1500Sstevel@tonic-gate 	} else {
1510Sstevel@tonic-gate 		vp = fp->f_vnode;
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (vp->v_stream == NULL) {
1550Sstevel@tonic-gate 		if (madefp)
1560Sstevel@tonic-gate 			(void) closef(fp);
1570Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: not a streams device\n", 0);
1580Sstevel@tonic-gate 		return (ENOSTR);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/*
1620Sstevel@tonic-gate 	 * allocate a new transport structure
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	ntiptr = kmem_alloc(TIUSERSZ, KM_SLEEP);
1650Sstevel@tonic-gate 	ntiptr->fp = fp;
1660Sstevel@tonic-gate 	ntiptr->flags = madefp ? MADE_FP : 0;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	KTLILOG(2, "t_kopen: vp %x, ", vp);
1690Sstevel@tonic-gate 	KTLILOG(2, "stp %x\n", vp->v_stream);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/*
1720Sstevel@tonic-gate 	 * see if TIMOD is already pushed
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	error = strioctl(vp, I_FIND, (intptr_t)"timod", 0, K_TO_K, cr, &retval);
1750Sstevel@tonic-gate 	if (error) {
1760Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
1770Sstevel@tonic-gate 		if (madefp)
1780Sstevel@tonic-gate 			(void) closef(fp);
1790Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: strioctl(I_FIND, timod): %d\n", error);
1800Sstevel@tonic-gate 		return (error);
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (retval == 0) {
1840Sstevel@tonic-gate tryagain:
1850Sstevel@tonic-gate 		error = strioctl(vp, I_PUSH, (intptr_t)"timod", 0, K_TO_K, cr,
1860Sstevel@tonic-gate 		    &retval);
1870Sstevel@tonic-gate 		if (error) {
1880Sstevel@tonic-gate 			switch (error) {
1890Sstevel@tonic-gate 			case ENOSPC:
1900Sstevel@tonic-gate 			case EAGAIN:
1910Sstevel@tonic-gate 			case ENOSR:
1920Sstevel@tonic-gate 				/*
1930Sstevel@tonic-gate 				 * This probably means the master file
1940Sstevel@tonic-gate 				 * should be tuned.
1950Sstevel@tonic-gate 				 */
1960Sstevel@tonic-gate 				cmn_err(CE_WARN,
1970Sstevel@tonic-gate 				"t_kopen: I_PUSH of timod failed, error %d\n",
1980Sstevel@tonic-gate 				    error);
1990Sstevel@tonic-gate 				(void) delay(hz);
2000Sstevel@tonic-gate 				error = 0;
2010Sstevel@tonic-gate 				goto tryagain;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 			default:
2040Sstevel@tonic-gate 				kmem_free(ntiptr, TIUSERSZ);
2050Sstevel@tonic-gate 				if (madefp)
2060Sstevel@tonic-gate 					(void) closef(fp);
2070Sstevel@tonic-gate 				KTLILOG(1, "t_kopen: I_PUSH (timod): %d",
2080Sstevel@tonic-gate 				    error);
2090Sstevel@tonic-gate 				return (error);
2100Sstevel@tonic-gate 			}
2110Sstevel@tonic-gate 		}
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	inforeq.PRIM_type = T_INFO_REQ;
2150Sstevel@tonic-gate 	strioc.ic_cmd = TI_GETINFO;
2160Sstevel@tonic-gate 	strioc.ic_timout = 0;
2170Sstevel@tonic-gate 	strioc.ic_dp = (char *)&inforeq;
2180Sstevel@tonic-gate 	strioc.ic_len = (int)sizeof (struct T_info_req);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	error = strdoioctl(vp->v_stream, &strioc, FNATIVE, K_TO_K, cr, &retval);
2210Sstevel@tonic-gate 	if (error) {
2220Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
2230Sstevel@tonic-gate 		if (madefp)
2240Sstevel@tonic-gate 			(void) closef(fp);
2250Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): %d\n", error);
2260Sstevel@tonic-gate 		return (error);
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (retval) {
2300Sstevel@tonic-gate 		if ((retval & 0xff) == TSYSERR)
2310Sstevel@tonic-gate 			error = (retval >> 8) & 0xff;
2320Sstevel@tonic-gate 		else
2330Sstevel@tonic-gate 			error = t_tlitosyserr(retval & 0xff);
2340Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
2350Sstevel@tonic-gate 		if (madefp)
2360Sstevel@tonic-gate 			(void) closef(fp);
2370Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): retval: 0x%x\n",
2380Sstevel@tonic-gate 		    retval);
2390Sstevel@tonic-gate 		return (error);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (strioc.ic_len != sizeof (struct T_info_ack)) {
2430Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
2440Sstevel@tonic-gate 		if (madefp)
2450Sstevel@tonic-gate 			(void) closef(fp);
2460Sstevel@tonic-gate 		KTLILOG(1,
2470Sstevel@tonic-gate 		"t_kopen: strioc.ic_len != sizeof (struct T_info_ack): %d\n",
2480Sstevel@tonic-gate 		    strioc.ic_len);
2490Sstevel@tonic-gate 		return (EPROTO);
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	ntiptr->tp_info.addr = _t_setsize(inforeq.ADDR_size);
2530Sstevel@tonic-gate 	ntiptr->tp_info.options = _t_setsize(inforeq.OPT_size);
2540Sstevel@tonic-gate 	ntiptr->tp_info.tsdu = _t_setsize(inforeq.TSDU_size);
2550Sstevel@tonic-gate 	ntiptr->tp_info.etsdu = _t_setsize(inforeq.ETSDU_size);
2560Sstevel@tonic-gate 	ntiptr->tp_info.connect = _t_setsize(inforeq.CDATA_size);
2570Sstevel@tonic-gate 	ntiptr->tp_info.discon = _t_setsize(inforeq.DDATA_size);
2580Sstevel@tonic-gate 	ntiptr->tp_info.servtype = inforeq.SERV_type;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	*tiptr = ntiptr;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	return (0);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate #define	DEFSIZE	128
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate static t_scalar_t
_t_setsize(t_scalar_t infosize)2680Sstevel@tonic-gate _t_setsize(t_scalar_t infosize)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	switch (infosize) {
2710Sstevel@tonic-gate 	case -1:
2720Sstevel@tonic-gate 		return (DEFSIZE);
2730Sstevel@tonic-gate 	case -2:
2740Sstevel@tonic-gate 		return (0);
2750Sstevel@tonic-gate 	default:
2760Sstevel@tonic-gate 		return (infosize);
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate }
279