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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 320Sstevel@tonic-gate * The Regents of the University of California 330Sstevel@tonic-gate * All Rights Reserved 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 370Sstevel@tonic-gate * contributors. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * Kernel TLI-like function to initialize a transport 440Sstevel@tonic-gate * endpoint using the protocol specified. 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * Returns: 470Sstevel@tonic-gate * 0 on success and "tiptr" is set to a valid transport pointer, 480Sstevel@tonic-gate * else a positive error code. 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate 510Sstevel@tonic-gate #include <sys/param.h> 520Sstevel@tonic-gate #include <sys/types.h> 530Sstevel@tonic-gate #include <sys/proc.h> 540Sstevel@tonic-gate #include <sys/file.h> 550Sstevel@tonic-gate #include <sys/user.h> 560Sstevel@tonic-gate #include <sys/vnode.h> 570Sstevel@tonic-gate #include <sys/errno.h> 580Sstevel@tonic-gate #include <sys/stream.h> 590Sstevel@tonic-gate #include <sys/ioctl.h> 600Sstevel@tonic-gate #include <sys/stropts.h> 610Sstevel@tonic-gate #include <sys/strsubr.h> 620Sstevel@tonic-gate #include <sys/tihdr.h> 630Sstevel@tonic-gate #include <sys/timod.h> 640Sstevel@tonic-gate #include <sys/tiuser.h> 650Sstevel@tonic-gate #include <sys/t_kuser.h> 660Sstevel@tonic-gate #include <sys/kmem.h> 670Sstevel@tonic-gate #include <sys/cmn_err.h> 680Sstevel@tonic-gate 690Sstevel@tonic-gate static t_scalar_t _t_setsize(t_scalar_t); 700Sstevel@tonic-gate 710Sstevel@tonic-gate int 720Sstevel@tonic-gate t_kopen(file_t *fp, dev_t rdev, int flags, TIUSER **tiptr, cred_t *cr) 730Sstevel@tonic-gate { 740Sstevel@tonic-gate int madefp = 0; 750Sstevel@tonic-gate struct T_info_ack inforeq; 760Sstevel@tonic-gate int retval; 770Sstevel@tonic-gate vnode_t *vp; 780Sstevel@tonic-gate struct strioctl strioc; 790Sstevel@tonic-gate int error; 800Sstevel@tonic-gate TIUSER *ntiptr; 810Sstevel@tonic-gate int rtries = 0; 820Sstevel@tonic-gate 83*766Scarlsonj /* 84*766Scarlsonj * Special case for install: miniroot needs to be able to access files 85*766Scarlsonj * via NFS as though it were always in the global zone. 86*766Scarlsonj */ 87*766Scarlsonj if (nfs_global_client_only != 0) 88*766Scarlsonj cr = kcred; 89*766Scarlsonj 900Sstevel@tonic-gate KTLILOG(2, "t_kopen: fp %x, ", fp); 910Sstevel@tonic-gate KTLILOG(2, "rdev %x, ", rdev); 920Sstevel@tonic-gate KTLILOG(2, "flags %x\n", flags); 930Sstevel@tonic-gate 940Sstevel@tonic-gate *tiptr = NULL; 950Sstevel@tonic-gate error = 0; 960Sstevel@tonic-gate retval = 0; 970Sstevel@tonic-gate if (fp == NULL) { 980Sstevel@tonic-gate if (rdev == 0 || rdev == NODEV) { 990Sstevel@tonic-gate KTLILOG(1, "t_kopen: null device\n", 0); 1000Sstevel@tonic-gate return (EINVAL); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * allocate a file pointer, but 1050Sstevel@tonic-gate * no file descripter. 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate if ((error = falloc(NULL, flags, &fp, NULL)) != 0) { 1080Sstevel@tonic-gate KTLILOG(1, "t_kopen: falloc: %d\n", error); 1090Sstevel@tonic-gate return (error); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* Install proper cred in file */ 1130Sstevel@tonic-gate if (cr != fp->f_cred) { 1140Sstevel@tonic-gate crhold(cr); 1150Sstevel@tonic-gate crfree(fp->f_cred); 1160Sstevel@tonic-gate fp->f_cred = cr; 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate vp = makespecvp(rdev, VCHR); 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * this will call the streams open for us. 1230Sstevel@tonic-gate * Want to retry if error is EAGAIN, the streams open routine 1240Sstevel@tonic-gate * might fail due to temporarely out of memory. 1250Sstevel@tonic-gate */ 1260Sstevel@tonic-gate do { 1270Sstevel@tonic-gate if ((error = VOP_OPEN(&vp, flags, cr)) == 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 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