xref: /illumos-gate/usr/src/uts/common/ktli/t_kopen.c (revision 746b0187)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Kernel TLI-like function to initialize a transport
417c478bd9Sstevel@tonic-gate  * endpoint using the protocol specified.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * Returns:
447c478bd9Sstevel@tonic-gate  * 	0 on success and "tiptr" is set to a valid transport pointer,
457c478bd9Sstevel@tonic-gate  * 	else a positive error code.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <sys/proc.h>
517c478bd9Sstevel@tonic-gate #include <sys/file.h>
527c478bd9Sstevel@tonic-gate #include <sys/user.h>
537c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
547c478bd9Sstevel@tonic-gate #include <sys/errno.h>
557c478bd9Sstevel@tonic-gate #include <sys/stream.h>
567c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
577c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
587c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
597c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
607c478bd9Sstevel@tonic-gate #include <sys/timod.h>
617c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
627c478bd9Sstevel@tonic-gate #include <sys/t_kuser.h>
637c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
647c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static t_scalar_t _t_setsize(t_scalar_t);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int
t_kopen(file_t * fp,dev_t rdev,int flags,TIUSER ** tiptr,cred_t * cr)697c478bd9Sstevel@tonic-gate t_kopen(file_t *fp, dev_t rdev, int flags, TIUSER **tiptr, cred_t *cr)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	int			madefp = 0;
727c478bd9Sstevel@tonic-gate 	struct T_info_ack	inforeq;
737c478bd9Sstevel@tonic-gate 	int			retval;
747c478bd9Sstevel@tonic-gate 	vnode_t			*vp;
757c478bd9Sstevel@tonic-gate 	struct strioctl		strioc;
767c478bd9Sstevel@tonic-gate 	int			error;
777c478bd9Sstevel@tonic-gate 	TIUSER			*ntiptr;
787c478bd9Sstevel@tonic-gate 	int			rtries = 0;
797c478bd9Sstevel@tonic-gate 
80108322fbScarlsonj 	/*
81108322fbScarlsonj 	 * Special case for install: miniroot needs to be able to access files
82108322fbScarlsonj 	 * via NFS as though it were always in the global zone.
83108322fbScarlsonj 	 */
84108322fbScarlsonj 	if (nfs_global_client_only != 0)
85108322fbScarlsonj 		cr = kcred;
86108322fbScarlsonj 
877c478bd9Sstevel@tonic-gate 	KTLILOG(2, "t_kopen: fp %x, ", fp);
887c478bd9Sstevel@tonic-gate 	KTLILOG(2, "rdev %x, ", rdev);
897c478bd9Sstevel@tonic-gate 	KTLILOG(2, "flags %x\n", flags);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	*tiptr = NULL;
927c478bd9Sstevel@tonic-gate 	error = 0;
937c478bd9Sstevel@tonic-gate 	retval = 0;
947c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
95*746b0187SRichard Lowe 		if (rdev == NODEV) {
967c478bd9Sstevel@tonic-gate 			KTLILOG(1, "t_kopen: null device\n", 0);
977c478bd9Sstevel@tonic-gate 			return (EINVAL);
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		/*
1017c478bd9Sstevel@tonic-gate 		 * allocate a file pointer, but
1027c478bd9Sstevel@tonic-gate 		 * no file descripter.
1037c478bd9Sstevel@tonic-gate 		 */
1047c478bd9Sstevel@tonic-gate 		if ((error = falloc(NULL, flags, &fp, NULL)) != 0) {
1057c478bd9Sstevel@tonic-gate 			KTLILOG(1, "t_kopen: falloc: %d\n", error);
1067c478bd9Sstevel@tonic-gate 			return (error);
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		/* Install proper cred in file */
1107c478bd9Sstevel@tonic-gate 		if (cr != fp->f_cred) {
1117c478bd9Sstevel@tonic-gate 			crhold(cr);
1127c478bd9Sstevel@tonic-gate 			crfree(fp->f_cred);
1137c478bd9Sstevel@tonic-gate 			fp->f_cred = cr;
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		vp = makespecvp(rdev, VCHR);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		/*
1197c478bd9Sstevel@tonic-gate 		 * this will call the streams open for us.
1207c478bd9Sstevel@tonic-gate 		 * Want to retry if error is EAGAIN, the streams open routine
1217c478bd9Sstevel@tonic-gate 		 * might fail due to temporarely out of memory.
1227c478bd9Sstevel@tonic-gate 		 */
1237c478bd9Sstevel@tonic-gate 		do {
124da6c28aaSamw 			if ((error = VOP_OPEN(&vp, flags, cr, NULL))
125da6c28aaSamw 			    == EAGAIN) {
1267c478bd9Sstevel@tonic-gate 				(void) delay(hz);
1277c478bd9Sstevel@tonic-gate 			}
1287c478bd9Sstevel@tonic-gate 		} while (error == EAGAIN && ++rtries < 5);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		if (error) {
1317c478bd9Sstevel@tonic-gate 			KTLILOG(1, "t_kopen: VOP_OPEN: %d\n", error);
1327c478bd9Sstevel@tonic-gate 			unfalloc(fp);
1337c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
1347c478bd9Sstevel@tonic-gate 			return (error);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		/*
1377c478bd9Sstevel@tonic-gate 		 * fp is completely initialized so drop the write lock.
1387c478bd9Sstevel@tonic-gate 		 * I actually don't need any locking on fp in here since
1397c478bd9Sstevel@tonic-gate 		 * there is no fd pointing at it.  However, since I could
1407c478bd9Sstevel@tonic-gate 		 * call closef if there is an error and closef requires
1417c478bd9Sstevel@tonic-gate 		 * the fp read locked, I will acquire the read lock here
1427c478bd9Sstevel@tonic-gate 		 * and make sure I release it before I leave this routine.
1437c478bd9Sstevel@tonic-gate 		 */
1447c478bd9Sstevel@tonic-gate 		fp->f_vnode = vp;
1457c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		madefp = 1;
1487c478bd9Sstevel@tonic-gate 	} else {
1497c478bd9Sstevel@tonic-gate 		vp = fp->f_vnode;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (vp->v_stream == NULL) {
1537c478bd9Sstevel@tonic-gate 		if (madefp)
1547c478bd9Sstevel@tonic-gate 			(void) closef(fp);
1557c478bd9Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: not a streams device\n", 0);
1567c478bd9Sstevel@tonic-gate 		return (ENOSTR);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * allocate a new transport structure
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	ntiptr = kmem_alloc(TIUSERSZ, KM_SLEEP);
1637c478bd9Sstevel@tonic-gate 	ntiptr->fp = fp;
1647c478bd9Sstevel@tonic-gate 	ntiptr->flags = madefp ? MADE_FP : 0;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	KTLILOG(2, "t_kopen: vp %x, ", vp);
1677c478bd9Sstevel@tonic-gate 	KTLILOG(2, "stp %x\n", vp->v_stream);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * see if TIMOD is already pushed
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	error = strioctl(vp, I_FIND, (intptr_t)"timod", 0, K_TO_K, cr, &retval);
1737c478bd9Sstevel@tonic-gate 	if (error) {
1747c478bd9Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
1757c478bd9Sstevel@tonic-gate 		if (madefp)
1767c478bd9Sstevel@tonic-gate 			(void) closef(fp);
1777c478bd9Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: strioctl(I_FIND, timod): %d\n", error);
1787c478bd9Sstevel@tonic-gate 		return (error);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (retval == 0) {
1827c478bd9Sstevel@tonic-gate tryagain:
1837c478bd9Sstevel@tonic-gate 		error = strioctl(vp, I_PUSH, (intptr_t)"timod", 0, K_TO_K, cr,
1847c478bd9Sstevel@tonic-gate 		    &retval);
1857c478bd9Sstevel@tonic-gate 		if (error) {
1867c478bd9Sstevel@tonic-gate 			switch (error) {
1877c478bd9Sstevel@tonic-gate 			case ENOSPC:
1887c478bd9Sstevel@tonic-gate 			case EAGAIN:
1897c478bd9Sstevel@tonic-gate 			case ENOSR:
1907c478bd9Sstevel@tonic-gate 				/*
1917c478bd9Sstevel@tonic-gate 				 * This probably means the master file
1927c478bd9Sstevel@tonic-gate 				 * should be tuned.
1937c478bd9Sstevel@tonic-gate 				 */
1947c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
1957c478bd9Sstevel@tonic-gate 				"t_kopen: I_PUSH of timod failed, error %d\n",
1967c478bd9Sstevel@tonic-gate 				    error);
1977c478bd9Sstevel@tonic-gate 				(void) delay(hz);
1987c478bd9Sstevel@tonic-gate 				error = 0;
1997c478bd9Sstevel@tonic-gate 				goto tryagain;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 			default:
2027c478bd9Sstevel@tonic-gate 				kmem_free(ntiptr, TIUSERSZ);
2037c478bd9Sstevel@tonic-gate 				if (madefp)
2047c478bd9Sstevel@tonic-gate 					(void) closef(fp);
2057c478bd9Sstevel@tonic-gate 				KTLILOG(1, "t_kopen: I_PUSH (timod): %d",
2067c478bd9Sstevel@tonic-gate 				    error);
2077c478bd9Sstevel@tonic-gate 				return (error);
2087c478bd9Sstevel@tonic-gate 			}
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	inforeq.PRIM_type = T_INFO_REQ;
2137c478bd9Sstevel@tonic-gate 	strioc.ic_cmd = TI_GETINFO;
2147c478bd9Sstevel@tonic-gate 	strioc.ic_timout = 0;
2157c478bd9Sstevel@tonic-gate 	strioc.ic_dp = (char *)&inforeq;
2167c478bd9Sstevel@tonic-gate 	strioc.ic_len = (int)sizeof (struct T_info_req);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	error = strdoioctl(vp->v_stream, &strioc, FNATIVE, K_TO_K, cr, &retval);
2197c478bd9Sstevel@tonic-gate 	if (error) {
2207c478bd9Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
2217c478bd9Sstevel@tonic-gate 		if (madefp)
2227c478bd9Sstevel@tonic-gate 			(void) closef(fp);
2237c478bd9Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): %d\n", error);
2247c478bd9Sstevel@tonic-gate 		return (error);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (retval) {
2287c478bd9Sstevel@tonic-gate 		if ((retval & 0xff) == TSYSERR)
2297c478bd9Sstevel@tonic-gate 			error = (retval >> 8) & 0xff;
2307c478bd9Sstevel@tonic-gate 		else
2317c478bd9Sstevel@tonic-gate 			error = t_tlitosyserr(retval & 0xff);
2327c478bd9Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
2337c478bd9Sstevel@tonic-gate 		if (madefp)
2347c478bd9Sstevel@tonic-gate 			(void) closef(fp);
2357c478bd9Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): retval: 0x%x\n",
2367c478bd9Sstevel@tonic-gate 		    retval);
2377c478bd9Sstevel@tonic-gate 		return (error);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (strioc.ic_len != sizeof (struct T_info_ack)) {
2417c478bd9Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
2427c478bd9Sstevel@tonic-gate 		if (madefp)
2437c478bd9Sstevel@tonic-gate 			(void) closef(fp);
2447c478bd9Sstevel@tonic-gate 		KTLILOG(1,
2457c478bd9Sstevel@tonic-gate 		"t_kopen: strioc.ic_len != sizeof (struct T_info_ack): %d\n",
2467c478bd9Sstevel@tonic-gate 		    strioc.ic_len);
2477c478bd9Sstevel@tonic-gate 		return (EPROTO);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	ntiptr->tp_info.addr = _t_setsize(inforeq.ADDR_size);
2517c478bd9Sstevel@tonic-gate 	ntiptr->tp_info.options = _t_setsize(inforeq.OPT_size);
2527c478bd9Sstevel@tonic-gate 	ntiptr->tp_info.tsdu = _t_setsize(inforeq.TSDU_size);
2537c478bd9Sstevel@tonic-gate 	ntiptr->tp_info.etsdu = _t_setsize(inforeq.ETSDU_size);
2547c478bd9Sstevel@tonic-gate 	ntiptr->tp_info.connect = _t_setsize(inforeq.CDATA_size);
2557c478bd9Sstevel@tonic-gate 	ntiptr->tp_info.discon = _t_setsize(inforeq.DDATA_size);
2567c478bd9Sstevel@tonic-gate 	ntiptr->tp_info.servtype = inforeq.SERV_type;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	*tiptr = ntiptr;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	return (0);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate #define	DEFSIZE	128
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static t_scalar_t
_t_setsize(t_scalar_t infosize)2667c478bd9Sstevel@tonic-gate _t_setsize(t_scalar_t infosize)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	switch (infosize) {
2697c478bd9Sstevel@tonic-gate 	case -1:
2707c478bd9Sstevel@tonic-gate 		return (DEFSIZE);
2717c478bd9Sstevel@tonic-gate 	case -2:
2727c478bd9Sstevel@tonic-gate 		return (0);
2737c478bd9Sstevel@tonic-gate 	default:
2747c478bd9Sstevel@tonic-gate 		return (infosize);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate }
277