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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/buf.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/cred.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
397c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/time.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <sys/open.h>
457c478bd9Sstevel@tonic-gate #include <sys/user.h>
467c478bd9Sstevel@tonic-gate #include <sys/termios.h>
477c478bd9Sstevel@tonic-gate #include <sys/stream.h>
487c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
497c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
507c478bd9Sstevel@tonic-gate #include <sys/flock.h>
517c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
527c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
537c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <sys/socket.h>
567c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
577c478bd9Sstevel@tonic-gate #include <netinet/in.h>
587c478bd9Sstevel@tonic-gate #include <sys/sendfile.h>
597c478bd9Sstevel@tonic-gate #include <sys/un.h>
607c478bd9Sstevel@tonic-gate #include <inet/nca/ncadoorhdr.h>
617c478bd9Sstevel@tonic-gate #include <inet/nca/ncaio.h>
627c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
637c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <inet/common.h>
667c478bd9Sstevel@tonic-gate #include <inet/ip.h>
677c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
687c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate extern int nca_sendfilev(file_t *, struct sendfilevec *, int, ssize_t *);
717c478bd9Sstevel@tonic-gate extern int sosendfile64(file_t *, file_t *, const struct ksendfilevec64 *,
727c478bd9Sstevel@tonic-gate 		ssize32_t *);
737c478bd9Sstevel@tonic-gate extern void nl7c_sendfilev(struct sonode *, u_offset_t, struct sendfilevec *,
747c478bd9Sstevel@tonic-gate 		int);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	SEND_MAX_CHUNK	16
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * 64 bit offsets for 32 bit applications only running either on
817c478bd9Sstevel@tonic-gate  * 64 bit kernel or 32 bit kernel. For 32 bit apps, we can't transfer
827c478bd9Sstevel@tonic-gate  * more than 2GB of data.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate int
857c478bd9Sstevel@tonic-gate sendvec_chunk64(file_t *fp, u_offset_t *fileoff, struct ksendfilevec64 *sfv,
867c478bd9Sstevel@tonic-gate     int copy_cnt, ssize32_t *count)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	struct vnode *vp;
897c478bd9Sstevel@tonic-gate 	ushort_t fflag;
907c478bd9Sstevel@tonic-gate 	int ioflag;
917c478bd9Sstevel@tonic-gate 	size32_t cnt;
927c478bd9Sstevel@tonic-gate 	ssize32_t sfv_len;
937c478bd9Sstevel@tonic-gate 	ssize32_t tmpcount;
947c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
957c478bd9Sstevel@tonic-gate 	struct uio auio;
967c478bd9Sstevel@tonic-gate 	struct iovec aiov;
977c478bd9Sstevel@tonic-gate 	int i, error;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
1007c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
1017c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
1047c478bd9Sstevel@tonic-gate 			return (EINTR);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 		/*
1077c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
1087c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
1097c478bd9Sstevel@tonic-gate 		 */
1107c478bd9Sstevel@tonic-gate 		sfv_len = (ssize32_t)sfv->sfv_len;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		if (sfv_len == 0)
1137c478bd9Sstevel@tonic-gate 			continue;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		if (sfv_len < 0)
1167c478bd9Sstevel@tonic-gate 			return (EINVAL);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
1197c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
1207c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
1217c478bd9Sstevel@tonic-gate 				(void) rctl_action(
1227c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
1237c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
1247c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
1257c478bd9Sstevel@tonic-gate 				return (EFBIG);
1267c478bd9Sstevel@tonic-gate 			}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 			if (*fileoff >= OFFSET_MAX(fp))
1297c478bd9Sstevel@tonic-gate 				return (EFBIG);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > OFFSET_MAX(fp))
1327c478bd9Sstevel@tonic-gate 				return (EINVAL);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		tmpcount = *count + sfv_len;
1367c478bd9Sstevel@tonic-gate 		if (tmpcount < 0)
1377c478bd9Sstevel@tonic-gate 			return (EINVAL);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		sfv_off = sfv->sfv_off;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
1427c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
1437c478bd9Sstevel@tonic-gate 			aiov.iov_len = sfv_len;
1447c478bd9Sstevel@tonic-gate 			aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
1457c478bd9Sstevel@tonic-gate 			auio.uio_loffset = *fileoff;
1467c478bd9Sstevel@tonic-gate 			auio.uio_iovcnt = 1;
1477c478bd9Sstevel@tonic-gate 			auio.uio_resid = sfv_len;
1487c478bd9Sstevel@tonic-gate 			auio.uio_iov = &aiov;
1497c478bd9Sstevel@tonic-gate 			auio.uio_segflg = UIO_USERSPACE;
1507c478bd9Sstevel@tonic-gate 			auio.uio_llimit = curproc->p_fsz_ctl;
1517c478bd9Sstevel@tonic-gate 			auio.uio_fmode = fflag;
1527c478bd9Sstevel@tonic-gate 			ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
1537c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
1547c478bd9Sstevel@tonic-gate 				error = VOP_WRITE(vp, &auio, ioflag,
1557c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
1567c478bd9Sstevel@tonic-gate 				cnt = sfv_len - auio.uio_resid;
1577c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
1587c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
1597c478bd9Sstevel@tonic-gate 				if (vp->v_type == VREG)
1607c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
1617c478bd9Sstevel@tonic-gate 				*count += cnt;
1627c478bd9Sstevel@tonic-gate 				if (error != 0)
1637c478bd9Sstevel@tonic-gate 					return (error);
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 		} else {
1667c478bd9Sstevel@tonic-gate 			file_t	*ffp;
1677c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
1687c478bd9Sstevel@tonic-gate 			int	readflg = 0;
1697c478bd9Sstevel@tonic-gate 			size_t	size;
1707c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
1737c478bd9Sstevel@tonic-gate 				return (EBADF);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
1767c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1777c478bd9Sstevel@tonic-gate 				return (EBADF);
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
1817c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
1827c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1837c478bd9Sstevel@tonic-gate 				return (EINVAL);
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 			/*
1877c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
1887c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
1897c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
1907c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
1917c478bd9Sstevel@tonic-gate 			 */
1927c478bd9Sstevel@tonic-gate 			if (VN_CMP(vp, readvp)) {
1937c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1947c478bd9Sstevel@tonic-gate 				return (EINVAL);
1957c478bd9Sstevel@tonic-gate 			}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 			/*
1987c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
1997c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
2007c478bd9Sstevel@tonic-gate 			 */
2017c478bd9Sstevel@tonic-gate 			(void) VOP_RWLOCK(readvp, readflg, NULL);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 			/*
2047c478bd9Sstevel@tonic-gate 			 * Same checks as in pread64.
2057c478bd9Sstevel@tonic-gate 			 */
2067c478bd9Sstevel@tonic-gate 			if (sfv_off > MAXOFFSET_T) {
2077c478bd9Sstevel@tonic-gate 				VOP_RWUNLOCK(readvp, readflg, NULL);
2087c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
2097c478bd9Sstevel@tonic-gate 				return (EINVAL);
2107c478bd9Sstevel@tonic-gate 			}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > MAXOFFSET_T)
2137c478bd9Sstevel@tonic-gate 				sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 			/* Find the native blocksize to transfer data */
2167c478bd9Sstevel@tonic-gate 			size = MIN(vp->v_vfsp->vfs_bsize,
2177c478bd9Sstevel@tonic-gate 			    readvp->v_vfsp->vfs_bsize);
2187c478bd9Sstevel@tonic-gate 			size = sfv_len < size ? sfv_len : size;
2197c478bd9Sstevel@tonic-gate 			ptr = kmem_alloc(size, KM_SLEEP);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
2227c478bd9Sstevel@tonic-gate 				size_t	iov_len;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 				iov_len = MIN(size, sfv_len);
2257c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
2267c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
2277c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
2287c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
2297c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
2307c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
2317c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
2327c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
2337c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
2347c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
2357c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 				/*
2387c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
2397c478bd9Sstevel@tonic-gate 				 * filter sync flags
2407c478bd9Sstevel@tonic-gate 				 */
2417c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
2427c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
2437c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
2447c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
2457c478bd9Sstevel@tonic-gate 				if (error) {
2467c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
2477c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
2487c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
2497c478bd9Sstevel@tonic-gate 					return (error);
2507c478bd9Sstevel@tonic-gate 				}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 				/*
2537c478bd9Sstevel@tonic-gate 				 * Check how must data was really read.
2547c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
2557c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
2567c478bd9Sstevel@tonic-gate 				 */
2577c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
2587c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
2597c478bd9Sstevel@tonic-gate 					/*
2607c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
2617c478bd9Sstevel@tonic-gate 					 * not implemented), we may now lose
2627c478bd9Sstevel@tonic-gate 					 * data.
2637c478bd9Sstevel@tonic-gate 					 */
2647c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
2657c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
2667c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
2677c478bd9Sstevel@tonic-gate 					return (EINVAL);
2687c478bd9Sstevel@tonic-gate 				}
2697c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
2707c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
2737c478bd9Sstevel@tonic-gate 				aiov.iov_len = cnt;
2747c478bd9Sstevel@tonic-gate 				auio.uio_loffset = *fileoff;
2757c478bd9Sstevel@tonic-gate 				auio.uio_resid = cnt;
2767c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
2777c478bd9Sstevel@tonic-gate 				auio.uio_llimit = curproc->p_fsz_ctl;
2787c478bd9Sstevel@tonic-gate 				auio.uio_fmode = fflag;
2797c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
2807c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
2817c478bd9Sstevel@tonic-gate 				error = VOP_WRITE(vp, &auio, ioflag,
2827c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 				/*
2857c478bd9Sstevel@tonic-gate 				 * Check how much data was written. Increment
2867c478bd9Sstevel@tonic-gate 				 * the 'len' and decrement the 'off' if all
2877c478bd9Sstevel@tonic-gate 				 * the data was not written.
2887c478bd9Sstevel@tonic-gate 				 */
2897c478bd9Sstevel@tonic-gate 				cnt -= auio.uio_resid;
2907c478bd9Sstevel@tonic-gate 				sfv_len += auio.uio_resid;
2917c478bd9Sstevel@tonic-gate 				sfv_off -= auio.uio_resid;
2927c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
2937c478bd9Sstevel@tonic-gate 				if (vp->v_type == VREG)
2947c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
2957c478bd9Sstevel@tonic-gate 				*count += cnt;
2967c478bd9Sstevel@tonic-gate 				if (error != 0) {
2977c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
2987c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
2997c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
3007c478bd9Sstevel@tonic-gate 					return (error);
3017c478bd9Sstevel@tonic-gate 				}
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(readvp, readflg, NULL);
3047c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
3057c478bd9Sstevel@tonic-gate 			kmem_free(ptr, size);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 		sfv++;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	return (0);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate ssize32_t
3137c478bd9Sstevel@tonic-gate sendvec64(file_t *fp, const struct ksendfilevec64 *vec, int sfvcnt,
3147c478bd9Sstevel@tonic-gate 	size32_t *xferred, int fildes)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	int			rwflag;
3177c478bd9Sstevel@tonic-gate 	u_offset_t		fileoff;
3187c478bd9Sstevel@tonic-gate 	int			copy_cnt;
3197c478bd9Sstevel@tonic-gate 	const struct ksendfilevec64 *copy_vec;
3207c478bd9Sstevel@tonic-gate 	struct ksendfilevec64 sfv[SEND_MAX_CHUNK];
3217c478bd9Sstevel@tonic-gate 	struct vnode *vp;
3227c478bd9Sstevel@tonic-gate 	int error;
3237c478bd9Sstevel@tonic-gate 	ssize32_t count = 0;
3247c478bd9Sstevel@tonic-gate 	int osfvcnt;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	rwflag = 1;
3277c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
3287c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	copy_vec = vec;
3317c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
3327c478bd9Sstevel@tonic-gate 	osfvcnt = sfvcnt;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	do {
3357c478bd9Sstevel@tonic-gate 		copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
3367c478bd9Sstevel@tonic-gate 		if (copyin(copy_vec, sfv, copy_cnt *
3377c478bd9Sstevel@tonic-gate 		    sizeof (struct ksendfilevec64))) {
3387c478bd9Sstevel@tonic-gate 			error = EFAULT;
3397c478bd9Sstevel@tonic-gate 			break;
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		/*
3437c478bd9Sstevel@tonic-gate 		 * Optimize the single regular file over
3447c478bd9Sstevel@tonic-gate 		 * the socket case.
3457c478bd9Sstevel@tonic-gate 		 */
3467c478bd9Sstevel@tonic-gate 		if (vp->v_type == VSOCK && osfvcnt == 1 &&
3477c478bd9Sstevel@tonic-gate 		    sfv->sfv_fd != SFV_FD_SELF) {
3487c478bd9Sstevel@tonic-gate 			file_t *rfp;
3497c478bd9Sstevel@tonic-gate 			vnode_t *rvp;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 			if ((rfp = getf(sfv->sfv_fd)) == NULL) {
3527c478bd9Sstevel@tonic-gate 				error = EBADF;
3537c478bd9Sstevel@tonic-gate 				break;
3547c478bd9Sstevel@tonic-gate 			}
3557c478bd9Sstevel@tonic-gate 			if ((rfp->f_flag & FREAD) == 0) {
3567c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
3577c478bd9Sstevel@tonic-gate 				error = EBADF;
3587c478bd9Sstevel@tonic-gate 				break;
3597c478bd9Sstevel@tonic-gate 			}
3607c478bd9Sstevel@tonic-gate 			rvp = rfp->f_vnode;
3617c478bd9Sstevel@tonic-gate 			if (rvp->v_type == VREG) {
3627c478bd9Sstevel@tonic-gate 				error = sosendfile64(fp, rfp, sfv, &count);
3637c478bd9Sstevel@tonic-gate 				break;
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 		error = sendvec_chunk64(fp, &fileoff, sfv, copy_cnt, &count);
3687c478bd9Sstevel@tonic-gate 		if (error != 0)
3697c478bd9Sstevel@tonic-gate 			break;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		copy_vec += copy_cnt;
3727c478bd9Sstevel@tonic-gate 		sfvcnt -= copy_cnt;
3737c478bd9Sstevel@tonic-gate 	} while (sfvcnt > 0);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG)
3767c478bd9Sstevel@tonic-gate 		fp->f_offset += count;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
3797c478bd9Sstevel@tonic-gate 	if (copyout(&count, xferred, sizeof (count)))
3807c478bd9Sstevel@tonic-gate 		error = EFAULT;
3817c478bd9Sstevel@tonic-gate 	releasef(fildes);
3827c478bd9Sstevel@tonic-gate 	if (error != 0)
3837c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3847c478bd9Sstevel@tonic-gate 	return (count);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate #endif
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate int
3897c478bd9Sstevel@tonic-gate sendvec_small_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
3907c478bd9Sstevel@tonic-gate     int copy_cnt, ssize_t total_size, int maxblk, ssize_t *count)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	struct vnode *vp;
3937c478bd9Sstevel@tonic-gate 	struct uio auio;
3947c478bd9Sstevel@tonic-gate 	struct iovec aiov;
3957c478bd9Sstevel@tonic-gate 	ushort_t fflag;
3967c478bd9Sstevel@tonic-gate 	int ioflag;
3977c478bd9Sstevel@tonic-gate 	int i, error;
3987c478bd9Sstevel@tonic-gate 	size_t cnt;
3997c478bd9Sstevel@tonic-gate 	ssize_t sfv_len;
4007c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
4017c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4027c478bd9Sstevel@tonic-gate 	model_t model = get_udatamodel();
4037c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
4047c478bd9Sstevel@tonic-gate 		MAXOFF32_T : MAXOFFSET_T;
4057c478bd9Sstevel@tonic-gate #else
4067c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
4077c478bd9Sstevel@tonic-gate #endif
4087c478bd9Sstevel@tonic-gate 	mblk_t *dmp = NULL;
4097c478bd9Sstevel@tonic-gate 	int wroff;
4107c478bd9Sstevel@tonic-gate 	int buf_left = 0;
4117c478bd9Sstevel@tonic-gate 	size_t	iov_len;
4127c478bd9Sstevel@tonic-gate 	mblk_t  *head, *tmp;
4137c478bd9Sstevel@tonic-gate 	size_t  size = total_size;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
4167c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VSOCK);
4197c478bd9Sstevel@tonic-gate 	ASSERT(maxblk > 0);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	wroff = (int)vp->v_stream->sd_wroff;
4227c478bd9Sstevel@tonic-gate 	buf_left = MIN(total_size, maxblk);
4237c478bd9Sstevel@tonic-gate 	head = dmp = allocb(buf_left + wroff, BPRI_HI);
4247c478bd9Sstevel@tonic-gate 	if (head == NULL)
4257c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4267c478bd9Sstevel@tonic-gate 	head->b_wptr = head->b_rptr = head->b_rptr + wroff;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
4297c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
4307c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
4317c478bd9Sstevel@tonic-gate 			return (EINTR);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		/*
4347c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
4357c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
4367c478bd9Sstevel@tonic-gate 		 */
4377c478bd9Sstevel@tonic-gate 		sfv_len = (ssize_t)sfv->sfv_len;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		if (sfv_len == 0) {
4407c478bd9Sstevel@tonic-gate 			sfv++;
4417c478bd9Sstevel@tonic-gate 			continue;
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 		/* Make sure sfv_len is not negative */
4457c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4467c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
4477c478bd9Sstevel@tonic-gate 			if ((ssize32_t)sfv_len < 0)
4487c478bd9Sstevel@tonic-gate 				return (EINVAL);
4497c478bd9Sstevel@tonic-gate 		} else
4507c478bd9Sstevel@tonic-gate #endif
4517c478bd9Sstevel@tonic-gate 		if (sfv_len < 0)
4527c478bd9Sstevel@tonic-gate 			return (EINVAL);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		/* Check for overflow */
4557c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4567c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
4577c478bd9Sstevel@tonic-gate 			if (((ssize32_t)(*count + sfv_len)) < 0)
4587c478bd9Sstevel@tonic-gate 				return (EINVAL);
4597c478bd9Sstevel@tonic-gate 		} else
4607c478bd9Sstevel@tonic-gate #endif
4617c478bd9Sstevel@tonic-gate 		if ((*count + sfv_len) < 0)
4627c478bd9Sstevel@tonic-gate 			return (EINVAL);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
4677c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
4687c478bd9Sstevel@tonic-gate 				if (buf_left == 0) {
4697c478bd9Sstevel@tonic-gate 					tmp = dmp;
4707c478bd9Sstevel@tonic-gate 					buf_left = MIN(total_size, maxblk);
4717c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
4727c478bd9Sstevel@tonic-gate 					dmp = allocb(buf_left + wroff, BPRI_HI);
4737c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
4747c478bd9Sstevel@tonic-gate 						freemsg(head);
4757c478bd9Sstevel@tonic-gate 						return (ENOMEM);
4767c478bd9Sstevel@tonic-gate 					}
4777c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr =
4787c478bd9Sstevel@tonic-gate 					    dmp->b_rptr + wroff;
4797c478bd9Sstevel@tonic-gate 					tmp->b_cont = dmp;
4807c478bd9Sstevel@tonic-gate 				} else {
4817c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
4827c478bd9Sstevel@tonic-gate 				}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
4857c478bd9Sstevel@tonic-gate 				aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
4867c478bd9Sstevel@tonic-gate 				auio.uio_loffset = *fileoff;
4877c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
4887c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
4897c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
4907c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_USERSPACE;
4917c478bd9Sstevel@tonic-gate 				auio.uio_llimit = curproc->p_fsz_ctl;
4927c478bd9Sstevel@tonic-gate 				auio.uio_fmode = fflag;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 				buf_left -= iov_len;
4957c478bd9Sstevel@tonic-gate 				total_size -= iov_len;
4967c478bd9Sstevel@tonic-gate 				sfv_len -= iov_len;
4977c478bd9Sstevel@tonic-gate 				sfv_off += iov_len;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 				error = uiomove((caddr_t)dmp->b_wptr,
5007c478bd9Sstevel@tonic-gate 				    iov_len, UIO_WRITE, &auio);
5017c478bd9Sstevel@tonic-gate 				if (error != 0) {
5027c478bd9Sstevel@tonic-gate 					freemsg(head);
5037c478bd9Sstevel@tonic-gate 					return (error);
5047c478bd9Sstevel@tonic-gate 				}
5057c478bd9Sstevel@tonic-gate 				dmp->b_wptr += iov_len;
5067c478bd9Sstevel@tonic-gate 			}
5077c478bd9Sstevel@tonic-gate 		} else {
5087c478bd9Sstevel@tonic-gate 			file_t	*ffp;
5097c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
5107c478bd9Sstevel@tonic-gate 			int	readflg = 0;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL) {
5137c478bd9Sstevel@tonic-gate 				freemsg(head);
5147c478bd9Sstevel@tonic-gate 				return (EBADF);
5157c478bd9Sstevel@tonic-gate 			}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
5187c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5197c478bd9Sstevel@tonic-gate 				freemsg(head);
5207c478bd9Sstevel@tonic-gate 				return (EACCES);
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
5247c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
5257c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5267c478bd9Sstevel@tonic-gate 				freemsg(head);
5277c478bd9Sstevel@tonic-gate 				return (EINVAL);
5287c478bd9Sstevel@tonic-gate 			}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 			/*
5317c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
5327c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
5337c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
5347c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
5357c478bd9Sstevel@tonic-gate 			 */
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 			if (VN_CMP(vp, readvp)) {
5387c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5397c478bd9Sstevel@tonic-gate 				freemsg(head);
5407c478bd9Sstevel@tonic-gate 				return (EINVAL);
5417c478bd9Sstevel@tonic-gate 			}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 			/*
5447c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
5457c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
5467c478bd9Sstevel@tonic-gate 			 */
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 			(void) VOP_RWLOCK(readvp, readflg, NULL);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
5517c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
5527c478bd9Sstevel@tonic-gate 				VOP_RWUNLOCK(readvp, readflg, NULL);
5537c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5547c478bd9Sstevel@tonic-gate 				freemsg(head);
5557c478bd9Sstevel@tonic-gate 				return (EINVAL);
5567c478bd9Sstevel@tonic-gate 			}
5577c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > maxoff) {
5587c478bd9Sstevel@tonic-gate 				sfv_len = (ssize_t)((offset_t)maxoff -
5597c478bd9Sstevel@tonic-gate 				    sfv_off);
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
5637c478bd9Sstevel@tonic-gate 				if (buf_left == 0) {
5647c478bd9Sstevel@tonic-gate 					tmp = dmp;
5657c478bd9Sstevel@tonic-gate 					buf_left = MIN(total_size, maxblk);
5667c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
5677c478bd9Sstevel@tonic-gate 					dmp = allocb(buf_left + wroff, BPRI_HI);
5687c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
5697c478bd9Sstevel@tonic-gate 						VOP_RWUNLOCK(readvp, readflg,
5707c478bd9Sstevel@tonic-gate 									NULL);
5717c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
5727c478bd9Sstevel@tonic-gate 						freemsg(head);
5737c478bd9Sstevel@tonic-gate 						return (ENOMEM);
5747c478bd9Sstevel@tonic-gate 					}
5757c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr =
5767c478bd9Sstevel@tonic-gate 					    dmp->b_rptr + wroff;
5777c478bd9Sstevel@tonic-gate 					tmp->b_cont = dmp;
5787c478bd9Sstevel@tonic-gate 				} else {
5797c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
5807c478bd9Sstevel@tonic-gate 				}
5817c478bd9Sstevel@tonic-gate 				aiov.iov_base = (caddr_t)dmp->b_wptr;
5827c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
5837c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
5847c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
5857c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
5867c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
5877c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
5887c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
5897c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
5907c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
5917c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 				/*
5947c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
5957c478bd9Sstevel@tonic-gate 				 * filter sync flags
5967c478bd9Sstevel@tonic-gate 				 */
5977c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
5987c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
5997c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
6007c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
6017c478bd9Sstevel@tonic-gate 				if (error != 0) {
6027c478bd9Sstevel@tonic-gate 					/*
6037c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
6047c478bd9Sstevel@tonic-gate 					 * not implemented), we may now loose
6057c478bd9Sstevel@tonic-gate 					 * data.
6067c478bd9Sstevel@tonic-gate 					 */
6077c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
6087c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
6097c478bd9Sstevel@tonic-gate 					freemsg(head);
6107c478bd9Sstevel@tonic-gate 					return (error);
6117c478bd9Sstevel@tonic-gate 				}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 				/*
6147c478bd9Sstevel@tonic-gate 				 * Check how much data was really read.
6157c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
6167c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
6177c478bd9Sstevel@tonic-gate 				 */
6187c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
6197c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
6207c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
6217c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
6227c478bd9Sstevel@tonic-gate 					freemsg(head);
6237c478bd9Sstevel@tonic-gate 					return (EINVAL);
6247c478bd9Sstevel@tonic-gate 				}
6257c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
6267c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
6277c478bd9Sstevel@tonic-gate 				total_size -= cnt;
6287c478bd9Sstevel@tonic-gate 				buf_left -= cnt;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 				dmp->b_wptr += cnt;
6317c478bd9Sstevel@tonic-gate 			}
6327c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(readvp, readflg, NULL);
6337c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
6347c478bd9Sstevel@tonic-gate 		}
6357c478bd9Sstevel@tonic-gate 		sfv++;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	ASSERT(total_size == 0);
6397c478bd9Sstevel@tonic-gate 	error = kstrwritemp(vp, head, fflag);
6407c478bd9Sstevel@tonic-gate 	if (error != 0) {
6417c478bd9Sstevel@tonic-gate 		freemsg(head);
6427c478bd9Sstevel@tonic-gate 		return (error);
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)size;
6457c478bd9Sstevel@tonic-gate 	*count += size;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	return (0);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate int
6527c478bd9Sstevel@tonic-gate sendvec_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
6537c478bd9Sstevel@tonic-gate     int copy_cnt, ssize_t *count)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	struct vnode *vp;
6567c478bd9Sstevel@tonic-gate 	struct uio auio;
6577c478bd9Sstevel@tonic-gate 	struct iovec aiov;
6587c478bd9Sstevel@tonic-gate 	ushort_t fflag;
6597c478bd9Sstevel@tonic-gate 	int ioflag;
6607c478bd9Sstevel@tonic-gate 	int i, error;
6617c478bd9Sstevel@tonic-gate 	size_t cnt;
6627c478bd9Sstevel@tonic-gate 	ssize_t sfv_len;
6637c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
6647c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
6657c478bd9Sstevel@tonic-gate 	model_t model = get_udatamodel();
6667c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
6677c478bd9Sstevel@tonic-gate 		MAXOFF32_T : MAXOFFSET_T;
6687c478bd9Sstevel@tonic-gate #else
6697c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
6707c478bd9Sstevel@tonic-gate #endif
671*1574ae68Sjohansen 	mblk_t	*dmp = NULL;
672*1574ae68Sjohansen 	char	*buf = NULL;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
6757c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
6787c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
6797c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
6807c478bd9Sstevel@tonic-gate 			return (EINTR);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		/*
6837c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
6847c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
6857c478bd9Sstevel@tonic-gate 		 */
6867c478bd9Sstevel@tonic-gate 		sfv_len = (ssize_t)sfv->sfv_len;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		if (sfv_len == 0) {
6897c478bd9Sstevel@tonic-gate 			sfv++;
6907c478bd9Sstevel@tonic-gate 			continue;
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		/* Make sure sfv_len is not negative */
6947c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
6957c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
6967c478bd9Sstevel@tonic-gate 			if ((ssize32_t)sfv_len < 0)
6977c478bd9Sstevel@tonic-gate 				return (EINVAL);
6987c478bd9Sstevel@tonic-gate 		} else
6997c478bd9Sstevel@tonic-gate #endif
7007c478bd9Sstevel@tonic-gate 		if (sfv_len < 0)
7017c478bd9Sstevel@tonic-gate 			return (EINVAL);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
7047c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
7057c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
7067c478bd9Sstevel@tonic-gate 				(void) rctl_action(
7077c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
7087c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
7097c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 				return (EFBIG);
7127c478bd9Sstevel@tonic-gate 			}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 			if (*fileoff >= maxoff)
7157c478bd9Sstevel@tonic-gate 				return (EFBIG);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > maxoff)
7187c478bd9Sstevel@tonic-gate 				return (EINVAL);
7197c478bd9Sstevel@tonic-gate 		}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		/* Check for overflow */
7227c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
7237c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
7247c478bd9Sstevel@tonic-gate 			if (((ssize32_t)(*count + sfv_len)) < 0)
7257c478bd9Sstevel@tonic-gate 				return (EINVAL);
7267c478bd9Sstevel@tonic-gate 		} else
7277c478bd9Sstevel@tonic-gate #endif
7287c478bd9Sstevel@tonic-gate 		if ((*count + sfv_len) < 0)
7297c478bd9Sstevel@tonic-gate 			return (EINVAL);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 		sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
7347c478bd9Sstevel@tonic-gate 			aiov.iov_len = sfv_len;
7357c478bd9Sstevel@tonic-gate 			aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
7367c478bd9Sstevel@tonic-gate 			auio.uio_loffset = *fileoff;
7377c478bd9Sstevel@tonic-gate 			auio.uio_iovcnt = 1;
7387c478bd9Sstevel@tonic-gate 			auio.uio_resid = sfv_len;
7397c478bd9Sstevel@tonic-gate 			auio.uio_iov = &aiov;
7407c478bd9Sstevel@tonic-gate 			auio.uio_segflg = UIO_USERSPACE;
7417c478bd9Sstevel@tonic-gate 			auio.uio_llimit = curproc->p_fsz_ctl;
7427c478bd9Sstevel@tonic-gate 			auio.uio_fmode = fflag;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 			if (vp->v_type == VSOCK) {
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 				/*
7477c478bd9Sstevel@tonic-gate 				 * Optimize for the socket case
7487c478bd9Sstevel@tonic-gate 				 */
7497c478bd9Sstevel@tonic-gate 				int wroff = (int)vp->v_stream->sd_wroff;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 				dmp = allocb(sfv_len + wroff, BPRI_HI);
7527c478bd9Sstevel@tonic-gate 				if (dmp == NULL)
7537c478bd9Sstevel@tonic-gate 					return (ENOMEM);
7547c478bd9Sstevel@tonic-gate 				dmp->b_wptr = dmp->b_rptr = dmp->b_rptr + wroff;
7557c478bd9Sstevel@tonic-gate 				error = uiomove((caddr_t)dmp->b_wptr,
7567c478bd9Sstevel@tonic-gate 				    sfv_len, UIO_WRITE, &auio);
7577c478bd9Sstevel@tonic-gate 				if (error != 0) {
7587c478bd9Sstevel@tonic-gate 					freeb(dmp);
7597c478bd9Sstevel@tonic-gate 					return (error);
7607c478bd9Sstevel@tonic-gate 				}
7617c478bd9Sstevel@tonic-gate 				dmp->b_wptr += sfv_len;
7627c478bd9Sstevel@tonic-gate 				error = kstrwritemp(vp, dmp, fflag);
7637c478bd9Sstevel@tonic-gate 				if (error != 0) {
7647c478bd9Sstevel@tonic-gate 					freeb(dmp);
7657c478bd9Sstevel@tonic-gate 					return (error);
7667c478bd9Sstevel@tonic-gate 				}
7677c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch +=
7687c478bd9Sstevel@tonic-gate 				    (ulong_t)sfv_len;
7697c478bd9Sstevel@tonic-gate 				*count += sfv_len;
7707c478bd9Sstevel@tonic-gate 			} else {
7717c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
7727c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
7737c478bd9Sstevel@tonic-gate 				while (sfv_len > 0) {
7747c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
7757c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
7767c478bd9Sstevel@tonic-gate 					cnt = sfv_len - auio.uio_resid;
7777c478bd9Sstevel@tonic-gate 					sfv_len -= cnt;
7787c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
7797c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
7807c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
7817c478bd9Sstevel@tonic-gate 					*count += cnt;
7827c478bd9Sstevel@tonic-gate 					if (error != 0)
7837c478bd9Sstevel@tonic-gate 						return (error);
7847c478bd9Sstevel@tonic-gate 				}
7857c478bd9Sstevel@tonic-gate 			}
7867c478bd9Sstevel@tonic-gate 		} else {
7877c478bd9Sstevel@tonic-gate 			file_t	*ffp;
7887c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
7897c478bd9Sstevel@tonic-gate 			int	readflg = 0;
7907c478bd9Sstevel@tonic-gate 			size_t	size;
7917c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
7947c478bd9Sstevel@tonic-gate 				return (EBADF);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
7977c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
7987c478bd9Sstevel@tonic-gate 				return (EBADF);
7997c478bd9Sstevel@tonic-gate 			}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
8027c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
8037c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8047c478bd9Sstevel@tonic-gate 				return (EINVAL);
8057c478bd9Sstevel@tonic-gate 			}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 			/*
8087c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
8097c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
8107c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
8117c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
8127c478bd9Sstevel@tonic-gate 			 */
8137c478bd9Sstevel@tonic-gate 			if (VN_CMP(vp, readvp)) {
8147c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8157c478bd9Sstevel@tonic-gate 				return (EINVAL);
8167c478bd9Sstevel@tonic-gate 			}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 			/*
8197c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
8207c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
8217c478bd9Sstevel@tonic-gate 			 */
8227c478bd9Sstevel@tonic-gate 			(void) VOP_RWLOCK(readvp, readflg, NULL);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
8257c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
8267c478bd9Sstevel@tonic-gate 				VOP_RWUNLOCK(readvp, readflg, NULL);
8277c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8287c478bd9Sstevel@tonic-gate 				return (EINVAL);
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > maxoff) {
8317c478bd9Sstevel@tonic-gate 				sfv_len = (ssize_t)((offset_t)maxoff -
8327c478bd9Sstevel@tonic-gate 				    sfv_off);
8337c478bd9Sstevel@tonic-gate 			}
8347c478bd9Sstevel@tonic-gate 			/* Find the native blocksize to transfer data */
8357c478bd9Sstevel@tonic-gate 			size = MIN(vp->v_vfsp->vfs_bsize,
8367c478bd9Sstevel@tonic-gate 			    readvp->v_vfsp->vfs_bsize);
8377c478bd9Sstevel@tonic-gate 			size = sfv_len < size ? sfv_len : size;
8387c478bd9Sstevel@tonic-gate 
839*1574ae68Sjohansen 			if (vp->v_type != VSOCK) {
840*1574ae68Sjohansen 				buf = kmem_alloc(size, KM_NOSLEEP);
841*1574ae68Sjohansen 				if (buf == NULL) {
842*1574ae68Sjohansen 					VOP_RWUNLOCK(readvp, readflg, NULL);
843*1574ae68Sjohansen 					releasef(sfv->sfv_fd);
844*1574ae68Sjohansen 					return (ENOMEM);
845*1574ae68Sjohansen 				}
846*1574ae68Sjohansen 			}
847*1574ae68Sjohansen 
8487c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
8497c478bd9Sstevel@tonic-gate 				size_t	iov_len;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 				iov_len = MIN(size, sfv_len);
8527c478bd9Sstevel@tonic-gate 
853*1574ae68Sjohansen 				if (vp->v_type == VSOCK) {
854*1574ae68Sjohansen 					dmp = allocb(iov_len, BPRI_HI);
855*1574ae68Sjohansen 					if (dmp == NULL) {
856*1574ae68Sjohansen 						VOP_RWUNLOCK(readvp, readflg,
857*1574ae68Sjohansen 						    NULL);
858*1574ae68Sjohansen 						releasef(sfv->sfv_fd);
859*1574ae68Sjohansen 						return (ENOMEM);
860*1574ae68Sjohansen 					}
861*1574ae68Sjohansen 					ptr = (caddr_t)dmp->b_rptr;
862*1574ae68Sjohansen 				} else {
863*1574ae68Sjohansen 					ptr = buf;
8647c478bd9Sstevel@tonic-gate 				}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
8677c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
8687c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
8697c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
8707c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
8717c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
8727c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
8737c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
8747c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
8757c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
8767c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 				/*
8797c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
8807c478bd9Sstevel@tonic-gate 				 * filter sync flags
8817c478bd9Sstevel@tonic-gate 				 */
8827c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
8837c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
8847c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
8857c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
8867c478bd9Sstevel@tonic-gate 				if (error != 0) {
8877c478bd9Sstevel@tonic-gate 					/*
8887c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
8897c478bd9Sstevel@tonic-gate 					 * not implemented), we may now lose
8907c478bd9Sstevel@tonic-gate 					 * data.
8917c478bd9Sstevel@tonic-gate 					 */
892*1574ae68Sjohansen 					if (vp->v_type == VSOCK)
893*1574ae68Sjohansen 						freeb(dmp);
894*1574ae68Sjohansen 					else
895*1574ae68Sjohansen 						kmem_free(buf, size);
8967c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
8977c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
8987c478bd9Sstevel@tonic-gate 					return (error);
8997c478bd9Sstevel@tonic-gate 				}
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 				/*
9027c478bd9Sstevel@tonic-gate 				 * Check how much data was really read.
9037c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
9047c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
9057c478bd9Sstevel@tonic-gate 				 */
9067c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
9077c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
908*1574ae68Sjohansen 					if (vp->v_type == VSOCK)
909*1574ae68Sjohansen 						freeb(dmp);
910*1574ae68Sjohansen 					else
911*1574ae68Sjohansen 						kmem_free(buf, size);
9127c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
9137c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
9147c478bd9Sstevel@tonic-gate 					return (EINVAL);
9157c478bd9Sstevel@tonic-gate 				}
9167c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
9177c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 				if (vp->v_type == VSOCK) {
9207c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr + cnt;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 					error = kstrwritemp(vp, dmp, fflag);
9237c478bd9Sstevel@tonic-gate 					if (error != 0) {
9247c478bd9Sstevel@tonic-gate 						freeb(dmp);
9257c478bd9Sstevel@tonic-gate 						VOP_RWUNLOCK(readvp, readflg,
9267c478bd9Sstevel@tonic-gate 									NULL);
9277c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
9287c478bd9Sstevel@tonic-gate 						return (error);
9297c478bd9Sstevel@tonic-gate 					}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
9327c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
9337c478bd9Sstevel@tonic-gate 					*count += cnt;
9347c478bd9Sstevel@tonic-gate 				} else {
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 					aiov.iov_base = ptr;
9377c478bd9Sstevel@tonic-gate 					aiov.iov_len = cnt;
9387c478bd9Sstevel@tonic-gate 					auio.uio_loffset = *fileoff;
9397c478bd9Sstevel@tonic-gate 					auio.uio_resid = cnt;
9407c478bd9Sstevel@tonic-gate 					auio.uio_segflg = UIO_SYSSPACE;
9417c478bd9Sstevel@tonic-gate 					auio.uio_llimit = curproc->p_fsz_ctl;
9427c478bd9Sstevel@tonic-gate 					auio.uio_fmode = fflag;
9437c478bd9Sstevel@tonic-gate 					ioflag = auio.uio_fmode &
9447c478bd9Sstevel@tonic-gate 					    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
9457c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
9467c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 					/*
9497c478bd9Sstevel@tonic-gate 					 * Check how much data was written.
9507c478bd9Sstevel@tonic-gate 					 * Increment the 'len' and decrement the
9517c478bd9Sstevel@tonic-gate 					 * 'off' if all the data was not
9527c478bd9Sstevel@tonic-gate 					 * written.
9537c478bd9Sstevel@tonic-gate 					 */
9547c478bd9Sstevel@tonic-gate 					cnt -= auio.uio_resid;
9557c478bd9Sstevel@tonic-gate 					sfv_len += auio.uio_resid;
9567c478bd9Sstevel@tonic-gate 					sfv_off -= auio.uio_resid;
9577c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
9587c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
9597c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
9607c478bd9Sstevel@tonic-gate 					*count += cnt;
9617c478bd9Sstevel@tonic-gate 					if (error != 0) {
9627c478bd9Sstevel@tonic-gate 						VOP_RWUNLOCK(readvp, readflg,
9637c478bd9Sstevel@tonic-gate 									NULL);
9647c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
9657c478bd9Sstevel@tonic-gate 						return (error);
9667c478bd9Sstevel@tonic-gate 					}
9677c478bd9Sstevel@tonic-gate 				}
9687c478bd9Sstevel@tonic-gate 			}
969*1574ae68Sjohansen 			if (buf) {
970*1574ae68Sjohansen 				kmem_free(buf, size);
971*1574ae68Sjohansen 				buf = NULL;
972*1574ae68Sjohansen 			}
9737c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(readvp, readflg, NULL);
9747c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 		sfv++;
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 	return (0);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate ssize_t
9827c478bd9Sstevel@tonic-gate sendfilev(int opcode, int fildes, const struct sendfilevec *vec, int sfvcnt,
9837c478bd9Sstevel@tonic-gate     size_t *xferred)
9847c478bd9Sstevel@tonic-gate {
9857c478bd9Sstevel@tonic-gate 	int error;
9867c478bd9Sstevel@tonic-gate 	file_t *fp;
9877c478bd9Sstevel@tonic-gate 	struct vnode *vp;
9887c478bd9Sstevel@tonic-gate 	struct sonode *so;
9897c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
9907c478bd9Sstevel@tonic-gate 	int copy_cnt;
9917c478bd9Sstevel@tonic-gate 	const struct sendfilevec *copy_vec;
9927c478bd9Sstevel@tonic-gate 	struct sendfilevec sfv[SEND_MAX_CHUNK];
9937c478bd9Sstevel@tonic-gate 	ssize_t count = 0;
9947c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
9957c478bd9Sstevel@tonic-gate 	struct ksendfilevec32 sfv32[SEND_MAX_CHUNK];
9967c478bd9Sstevel@tonic-gate #endif
9977c478bd9Sstevel@tonic-gate 	ssize_t total_size = 0;
9987c478bd9Sstevel@tonic-gate 	int i;
9997c478bd9Sstevel@tonic-gate 	boolean_t is_sock = B_FALSE;
10007c478bd9Sstevel@tonic-gate 	int maxblk = 0;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	if (sfvcnt <= 0)
10037c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	if ((fp = getf(fildes)) == NULL)
10067c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (((fp->f_flag) & FWRITE) == 0) {
10097c478bd9Sstevel@tonic-gate 		error = EBADF;
10107c478bd9Sstevel@tonic-gate 		goto err;
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
10147c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	switch (vp->v_type) {
10177c478bd9Sstevel@tonic-gate 	case VSOCK:
10187c478bd9Sstevel@tonic-gate 		so = VTOSO(vp);
10197c478bd9Sstevel@tonic-gate 		/* sendfile not supported for SCTP */
10207c478bd9Sstevel@tonic-gate 		if (so->so_protocol == IPPROTO_SCTP) {
10217c478bd9Sstevel@tonic-gate 			error = EPROTONOSUPPORT;
10227c478bd9Sstevel@tonic-gate 			goto err;
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 		is_sock = B_TRUE;
10257c478bd9Sstevel@tonic-gate 		switch (so->so_family) {
10267c478bd9Sstevel@tonic-gate 		case AF_NCA:
10277c478bd9Sstevel@tonic-gate 		case AF_INET:
10287c478bd9Sstevel@tonic-gate 		case AF_INET6:
10297c478bd9Sstevel@tonic-gate 			/*
10307c478bd9Sstevel@tonic-gate 			 * Make similar checks done in SOP_WRITE().
10317c478bd9Sstevel@tonic-gate 			 */
10327c478bd9Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
10337c478bd9Sstevel@tonic-gate 				tsignal(curthread, SIGPIPE);
10347c478bd9Sstevel@tonic-gate 				error = EPIPE;
10357c478bd9Sstevel@tonic-gate 				goto err;
10367c478bd9Sstevel@tonic-gate 			}
10377c478bd9Sstevel@tonic-gate 			if (so->so_type != SOCK_STREAM) {
10387c478bd9Sstevel@tonic-gate 				error = EOPNOTSUPP;
10397c478bd9Sstevel@tonic-gate 				goto err;
10407c478bd9Sstevel@tonic-gate 			}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 			if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
10437c478bd9Sstevel@tonic-gate 			    (SS_ISCONNECTED|SS_ISBOUND)) {
10447c478bd9Sstevel@tonic-gate 				error = ENOTCONN;
10457c478bd9Sstevel@tonic-gate 				goto err;
10467c478bd9Sstevel@tonic-gate 			}
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 			if ((so->so_state & SS_TCP_FAST_ACCEPT) &&
10497c478bd9Sstevel@tonic-gate 			    (so->so_priv != NULL)) {
10507c478bd9Sstevel@tonic-gate 				maxblk = ((tcp_t *)so->so_priv)->tcp_mss;
10517c478bd9Sstevel@tonic-gate 			} else {
10527c478bd9Sstevel@tonic-gate 				maxblk = (int)vp->v_stream->sd_maxblk;
10537c478bd9Sstevel@tonic-gate 			}
10547c478bd9Sstevel@tonic-gate 			break;
10557c478bd9Sstevel@tonic-gate 		default:
10567c478bd9Sstevel@tonic-gate 			error = EAFNOSUPPORT;
10577c478bd9Sstevel@tonic-gate 			goto err;
10587c478bd9Sstevel@tonic-gate 		}
10597c478bd9Sstevel@tonic-gate 		break;
10607c478bd9Sstevel@tonic-gate 	case VREG:
10617c478bd9Sstevel@tonic-gate 		break;
10627c478bd9Sstevel@tonic-gate 	default:
10637c478bd9Sstevel@tonic-gate 		error = EINVAL;
10647c478bd9Sstevel@tonic-gate 		goto err;
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	switch (opcode) {
10687c478bd9Sstevel@tonic-gate 	case SENDFILEV :
10697c478bd9Sstevel@tonic-gate 		break;
10707c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
10717c478bd9Sstevel@tonic-gate 	case SENDFILEV64 :
10727c478bd9Sstevel@tonic-gate 		return (sendvec64(fp, (struct ksendfilevec64 *)vec, sfvcnt,
10737c478bd9Sstevel@tonic-gate 		    (size32_t *)xferred, fildes));
10747c478bd9Sstevel@tonic-gate #endif
10757c478bd9Sstevel@tonic-gate 	default :
10767c478bd9Sstevel@tonic-gate 		error = ENOSYS;
10777c478bd9Sstevel@tonic-gate 		break;
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
10817c478bd9Sstevel@tonic-gate 	copy_vec = vec;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	do {
10847c478bd9Sstevel@tonic-gate 		copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
10857c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
10867c478bd9Sstevel@tonic-gate 		/* 32-bit callers need to have their iovec expanded. */
10877c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_ILP32) {
10887c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv32,
10897c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (ksendfilevec32_t))) {
10907c478bd9Sstevel@tonic-gate 				error = EFAULT;
10917c478bd9Sstevel@tonic-gate 				break;
10927c478bd9Sstevel@tonic-gate 			}
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
10957c478bd9Sstevel@tonic-gate 				sfv[i].sfv_fd = sfv32[i].sfv_fd;
10967c478bd9Sstevel@tonic-gate 				sfv[i].sfv_off =
10977c478bd9Sstevel@tonic-gate 					(off_t)(uint32_t)sfv32[i].sfv_off;
10987c478bd9Sstevel@tonic-gate 				sfv[i].sfv_len = (size_t)sfv32[i].sfv_len;
10997c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
11007c478bd9Sstevel@tonic-gate 				sfv[i].sfv_flag = sfv32[i].sfv_flag;
11017c478bd9Sstevel@tonic-gate 			}
11027c478bd9Sstevel@tonic-gate 		} else {
11037c478bd9Sstevel@tonic-gate #endif
11047c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv,
11057c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (sendfilevec_t))) {
11067c478bd9Sstevel@tonic-gate 				error = EFAULT;
11077c478bd9Sstevel@tonic-gate 				break;
11087c478bd9Sstevel@tonic-gate 			}
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
11117c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
11127c478bd9Sstevel@tonic-gate 			}
11137c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate #endif
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 		/*
11187c478bd9Sstevel@tonic-gate 		 * The task between deciding to use sendvec_small_chunk
11197c478bd9Sstevel@tonic-gate 		 * and sendvec_chunk is dependant on multiple things:
11207c478bd9Sstevel@tonic-gate 		 *
11217c478bd9Sstevel@tonic-gate 		 * i) latency is important for smaller files. So if the
11227c478bd9Sstevel@tonic-gate 		 * data is smaller than 'tcp_slow_start_initial' times
11237c478bd9Sstevel@tonic-gate 		 * maxblk, then use sendvec_small_chunk which creates
11247c478bd9Sstevel@tonic-gate 		 * maxblk size mblks and chains then together and sends
11257c478bd9Sstevel@tonic-gate 		 * them to TCP in one shot. It also leaves 'wroff' size
11267c478bd9Sstevel@tonic-gate 		 * space for the headers in each mblk.
11277c478bd9Sstevel@tonic-gate 		 *
11287c478bd9Sstevel@tonic-gate 		 * ii) for total size bigger than 'tcp_slow_start_initial'
11297c478bd9Sstevel@tonic-gate 		 * time maxblk, its probably real file data which is
11307c478bd9Sstevel@tonic-gate 		 * dominating. So its better to use sendvec_chunk because
11317c478bd9Sstevel@tonic-gate 		 * performance goes to dog if we don't do pagesize reads.
11327c478bd9Sstevel@tonic-gate 		 * sendvec_chunk will do pagesize reads and write them
11337c478bd9Sstevel@tonic-gate 		 * in pagesize mblks to TCP.
11347c478bd9Sstevel@tonic-gate 		 *
11357c478bd9Sstevel@tonic-gate 		 * Side Notes: A write to file has not been optimized.
11367c478bd9Sstevel@tonic-gate 		 * Future zero copy code will plugin into sendvec_chunk
11377c478bd9Sstevel@tonic-gate 		 * only because doing zero copy for files smaller then
11387c478bd9Sstevel@tonic-gate 		 * pagesize is useless.
11397c478bd9Sstevel@tonic-gate 		 *
11407c478bd9Sstevel@tonic-gate 		 * Note, if socket has NL7C enabled then call NL7C's
11417c478bd9Sstevel@tonic-gate 		 * senfilev() function to give NL7C a chance to copy
11427c478bd9Sstevel@tonic-gate 		 * the vec for caching, then continue processing as
11437c478bd9Sstevel@tonic-gate 		 * normal.
11447c478bd9Sstevel@tonic-gate 		 */
11457c478bd9Sstevel@tonic-gate 		if (is_sock) {
11467c478bd9Sstevel@tonic-gate 			switch (so->so_family) {
11477c478bd9Sstevel@tonic-gate 			case AF_INET:
11487c478bd9Sstevel@tonic-gate 			case AF_INET6:
11497c478bd9Sstevel@tonic-gate 				if (so->so_nl7c_flags != 0) {
11507c478bd9Sstevel@tonic-gate 					nl7c_sendfilev(so, fileoff,
11517c478bd9Sstevel@tonic-gate 					    sfv, copy_cnt);
11527c478bd9Sstevel@tonic-gate 				}
11537c478bd9Sstevel@tonic-gate 				if (total_size <= (4 * maxblk))
11547c478bd9Sstevel@tonic-gate 					error = sendvec_small_chunk(fp,
11557c478bd9Sstevel@tonic-gate 					    &fileoff, sfv, copy_cnt,
11567c478bd9Sstevel@tonic-gate 					    total_size, maxblk, &count);
11577c478bd9Sstevel@tonic-gate 				else
11587c478bd9Sstevel@tonic-gate 					error = sendvec_chunk(fp, &fileoff,
11597c478bd9Sstevel@tonic-gate 					    sfv, copy_cnt, &count);
11607c478bd9Sstevel@tonic-gate 				break;
11617c478bd9Sstevel@tonic-gate 			case AF_NCA:
11627c478bd9Sstevel@tonic-gate 				error = nca_sendfilev(fp, sfv, copy_cnt,
11637c478bd9Sstevel@tonic-gate 				    &count);
11647c478bd9Sstevel@tonic-gate 				break;
11657c478bd9Sstevel@tonic-gate 			}
11667c478bd9Sstevel@tonic-gate 		} else {
11677c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_type == VREG);
11687c478bd9Sstevel@tonic-gate 			error = sendvec_chunk(fp, &fileoff, sfv, copy_cnt,
11697c478bd9Sstevel@tonic-gate 			    &count);
11707c478bd9Sstevel@tonic-gate 		}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11747c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32)
11757c478bd9Sstevel@tonic-gate 		copy_vec = (const struct sendfilevec *)((char *)copy_vec +
11767c478bd9Sstevel@tonic-gate 		    (copy_cnt * sizeof (ksendfilevec32_t)));
11777c478bd9Sstevel@tonic-gate 	else
11787c478bd9Sstevel@tonic-gate #endif
11797c478bd9Sstevel@tonic-gate 		copy_vec += copy_cnt;
11807c478bd9Sstevel@tonic-gate 		sfvcnt -= copy_cnt;
11817c478bd9Sstevel@tonic-gate 	} while (sfvcnt > 0);
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG)
11847c478bd9Sstevel@tonic-gate 		fp->f_offset += count;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11907c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
11917c478bd9Sstevel@tonic-gate 		ssize32_t count32 = (ssize32_t)count;
11927c478bd9Sstevel@tonic-gate 		if (copyout(&count32, xferred, sizeof (count32)))
11937c478bd9Sstevel@tonic-gate 			error = EFAULT;
11947c478bd9Sstevel@tonic-gate 		releasef(fildes);
11957c478bd9Sstevel@tonic-gate 		if (error != 0)
11967c478bd9Sstevel@tonic-gate 			return (set_errno(error));
11977c478bd9Sstevel@tonic-gate 		return (count32);
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate #endif
12007c478bd9Sstevel@tonic-gate 	if (copyout(&count, xferred, sizeof (count)))
12017c478bd9Sstevel@tonic-gate 		error = EFAULT;
12027c478bd9Sstevel@tonic-gate 	releasef(fildes);
12037c478bd9Sstevel@tonic-gate 	if (error != 0)
12047c478bd9Sstevel@tonic-gate 		return (set_errno(error));
12057c478bd9Sstevel@tonic-gate 	return (count);
12067c478bd9Sstevel@tonic-gate err:
12077c478bd9Sstevel@tonic-gate 	ASSERT(error != 0);
12087c478bd9Sstevel@tonic-gate 	releasef(fildes);
12097c478bd9Sstevel@tonic-gate 	return (set_errno(error));
12107c478bd9Sstevel@tonic-gate }
1211