xref: /freebsd/sys/kern/kern_sendfile.c (revision 98549e2dc6fb0c38fef2a5357b10c4eb99674d9d)
133a2a37bSGleb Smirnoff /*-
233a2a37bSGleb Smirnoff  * Copyright (c) 2013-2015 Gleb Smirnoff <glebius@FreeBSD.org>
333a2a37bSGleb Smirnoff  * Copyright (c) 1998, David Greenman. All rights reserved.
433a2a37bSGleb Smirnoff  *
533a2a37bSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
633a2a37bSGleb Smirnoff  * modification, are permitted provided that the following conditions
733a2a37bSGleb Smirnoff  * are met:
833a2a37bSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
933a2a37bSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
1033a2a37bSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
1133a2a37bSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
1233a2a37bSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
1369a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1433a2a37bSGleb Smirnoff  *    may be used to endorse or promote products derived from this software
1533a2a37bSGleb Smirnoff  *    without specific prior written permission.
1633a2a37bSGleb Smirnoff  *
1733a2a37bSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1833a2a37bSGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1933a2a37bSGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2033a2a37bSGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2133a2a37bSGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2233a2a37bSGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2333a2a37bSGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2433a2a37bSGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2533a2a37bSGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2633a2a37bSGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2733a2a37bSGleb Smirnoff  * SUCH DAMAGE.
2833a2a37bSGleb Smirnoff  */
2933a2a37bSGleb Smirnoff 
3033a2a37bSGleb Smirnoff #include <sys/cdefs.h>
3133a2a37bSGleb Smirnoff __FBSDID("$FreeBSD$");
3233a2a37bSGleb Smirnoff 
3333a2a37bSGleb Smirnoff #include <sys/param.h>
3433a2a37bSGleb Smirnoff #include <sys/systm.h>
3533a2a37bSGleb Smirnoff #include <sys/capsicum.h>
3633a2a37bSGleb Smirnoff #include <sys/kernel.h>
37cec06a3eSJohn Baldwin #include <netinet/in.h>
3833a2a37bSGleb Smirnoff #include <sys/lock.h>
3933a2a37bSGleb Smirnoff #include <sys/mutex.h>
4033a2a37bSGleb Smirnoff #include <sys/sysproto.h>
4133a2a37bSGleb Smirnoff #include <sys/malloc.h>
4233a2a37bSGleb Smirnoff #include <sys/proc.h>
4333a2a37bSGleb Smirnoff #include <sys/mman.h>
4433a2a37bSGleb Smirnoff #include <sys/mount.h>
4533a2a37bSGleb Smirnoff #include <sys/mbuf.h>
4633a2a37bSGleb Smirnoff #include <sys/protosw.h>
4733a2a37bSGleb Smirnoff #include <sys/rwlock.h>
4833a2a37bSGleb Smirnoff #include <sys/sf_buf.h>
4933a2a37bSGleb Smirnoff #include <sys/socket.h>
5033a2a37bSGleb Smirnoff #include <sys/socketvar.h>
5133a2a37bSGleb Smirnoff #include <sys/syscallsubr.h>
5233a2a37bSGleb Smirnoff #include <sys/sysctl.h>
5333a2a37bSGleb Smirnoff #include <sys/vnode.h>
5433a2a37bSGleb Smirnoff 
5533a2a37bSGleb Smirnoff #include <net/vnet.h>
5633a2a37bSGleb Smirnoff 
5733a2a37bSGleb Smirnoff #include <security/audit/audit.h>
5833a2a37bSGleb Smirnoff #include <security/mac/mac_framework.h>
5933a2a37bSGleb Smirnoff 
6033a2a37bSGleb Smirnoff #include <vm/vm.h>
6133a2a37bSGleb Smirnoff #include <vm/vm_object.h>
6233a2a37bSGleb Smirnoff #include <vm/vm_pager.h>
6333a2a37bSGleb Smirnoff 
649c82bec4SGleb Smirnoff #define	EXT_FLAG_SYNC		EXT_FLAG_VENDOR1
659c82bec4SGleb Smirnoff #define	EXT_FLAG_NOCACHE	EXT_FLAG_VENDOR2
66cec06a3eSJohn Baldwin #define	EXT_FLAG_CACHE_LAST	EXT_FLAG_VENDOR3
679c82bec4SGleb Smirnoff 
6833a2a37bSGleb Smirnoff /*
6933a2a37bSGleb Smirnoff  * Structure describing a single sendfile(2) I/O, which may consist of
7033a2a37bSGleb Smirnoff  * several underlying pager I/Os.
7133a2a37bSGleb Smirnoff  *
7233a2a37bSGleb Smirnoff  * The syscall context allocates the structure and initializes 'nios'
7333a2a37bSGleb Smirnoff  * to 1.  As sendfile_swapin() runs through pages and starts asynchronous
7433a2a37bSGleb Smirnoff  * paging operations, it increments 'nios'.
7533a2a37bSGleb Smirnoff  *
7633a2a37bSGleb Smirnoff  * Every I/O completion calls sendfile_iodone(), which decrements the 'nios',
7733a2a37bSGleb Smirnoff  * and the syscall also calls sendfile_iodone() after allocating all mbufs,
7833a2a37bSGleb Smirnoff  * linking them and sending to socket.  Whoever reaches zero 'nios' is
7933a2a37bSGleb Smirnoff  * responsible to * call pru_ready on the socket, to notify it of readyness
8033a2a37bSGleb Smirnoff  * of the data.
8133a2a37bSGleb Smirnoff  */
8233a2a37bSGleb Smirnoff struct sf_io {
8333a2a37bSGleb Smirnoff 	volatile u_int	nios;
8433a2a37bSGleb Smirnoff 	u_int		error;
8533a2a37bSGleb Smirnoff 	int		npages;
86d37aa3ccSGleb Smirnoff 	struct socket	*so;
8733a2a37bSGleb Smirnoff 	struct mbuf	*m;
8833a2a37bSGleb Smirnoff 	vm_page_t	pa[];
8933a2a37bSGleb Smirnoff };
9033a2a37bSGleb Smirnoff 
9133a2a37bSGleb Smirnoff /*
9233a2a37bSGleb Smirnoff  * Structure used to track requests with SF_SYNC flag.
9333a2a37bSGleb Smirnoff  */
9433a2a37bSGleb Smirnoff struct sendfile_sync {
9533a2a37bSGleb Smirnoff 	struct mtx	mtx;
9633a2a37bSGleb Smirnoff 	struct cv	cv;
9733a2a37bSGleb Smirnoff 	unsigned	count;
9833a2a37bSGleb Smirnoff };
9933a2a37bSGleb Smirnoff 
10033a2a37bSGleb Smirnoff counter_u64_t sfstat[sizeof(struct sfstat) / sizeof(uint64_t)];
10133a2a37bSGleb Smirnoff 
10233a2a37bSGleb Smirnoff static void
10333a2a37bSGleb Smirnoff sfstat_init(const void *unused)
10433a2a37bSGleb Smirnoff {
10533a2a37bSGleb Smirnoff 
10633a2a37bSGleb Smirnoff 	COUNTER_ARRAY_ALLOC(sfstat, sizeof(struct sfstat) / sizeof(uint64_t),
10733a2a37bSGleb Smirnoff 	    M_WAITOK);
10833a2a37bSGleb Smirnoff }
10933a2a37bSGleb Smirnoff SYSINIT(sfstat, SI_SUB_MBUF, SI_ORDER_FIRST, sfstat_init, NULL);
11033a2a37bSGleb Smirnoff 
11133a2a37bSGleb Smirnoff static int
11233a2a37bSGleb Smirnoff sfstat_sysctl(SYSCTL_HANDLER_ARGS)
11333a2a37bSGleb Smirnoff {
11433a2a37bSGleb Smirnoff 	struct sfstat s;
11533a2a37bSGleb Smirnoff 
11633a2a37bSGleb Smirnoff 	COUNTER_ARRAY_COPY(sfstat, &s, sizeof(s) / sizeof(uint64_t));
11733a2a37bSGleb Smirnoff 	if (req->newptr)
11833a2a37bSGleb Smirnoff 		COUNTER_ARRAY_ZERO(sfstat, sizeof(s) / sizeof(uint64_t));
11933a2a37bSGleb Smirnoff 	return (SYSCTL_OUT(req, &s, sizeof(s)));
12033a2a37bSGleb Smirnoff }
12133a2a37bSGleb Smirnoff SYSCTL_PROC(_kern_ipc, OID_AUTO, sfstat, CTLTYPE_OPAQUE | CTLFLAG_RW,
12233a2a37bSGleb Smirnoff     NULL, 0, sfstat_sysctl, "I", "sendfile statistics");
12333a2a37bSGleb Smirnoff 
1249c82bec4SGleb Smirnoff static void
1259c82bec4SGleb Smirnoff sendfile_free_mext(struct mbuf *m)
1269c82bec4SGleb Smirnoff {
1279c82bec4SGleb Smirnoff 	struct sf_buf *sf;
1289c82bec4SGleb Smirnoff 	vm_page_t pg;
129*98549e2dSMark Johnston 	int flags;
1309c82bec4SGleb Smirnoff 
1319c82bec4SGleb Smirnoff 	KASSERT(m->m_flags & M_EXT && m->m_ext.ext_type == EXT_SFBUF,
1329c82bec4SGleb Smirnoff 	    ("%s: m %p !M_EXT or !EXT_SFBUF", __func__, m));
1339c82bec4SGleb Smirnoff 
1349c82bec4SGleb Smirnoff 	sf = m->m_ext.ext_arg1;
1359c82bec4SGleb Smirnoff 	pg = sf_buf_page(sf);
136*98549e2dSMark Johnston 	flags = (m->m_ext.ext_flags & EXT_FLAG_NOCACHE) != 0 ? VPR_TRYFREE : 0;
1379c82bec4SGleb Smirnoff 
1389c82bec4SGleb Smirnoff 	sf_buf_free(sf);
139*98549e2dSMark Johnston 	vm_page_release(pg, flags);
1409c82bec4SGleb Smirnoff 
1419c82bec4SGleb Smirnoff 	if (m->m_ext.ext_flags & EXT_FLAG_SYNC) {
1429c82bec4SGleb Smirnoff 		struct sendfile_sync *sfs = m->m_ext.ext_arg2;
1439c82bec4SGleb Smirnoff 
14433a2a37bSGleb Smirnoff 		mtx_lock(&sfs->mtx);
14533a2a37bSGleb Smirnoff 		KASSERT(sfs->count > 0, ("Sendfile sync botchup count == 0"));
14633a2a37bSGleb Smirnoff 		if (--sfs->count == 0)
14733a2a37bSGleb Smirnoff 			cv_signal(&sfs->cv);
14833a2a37bSGleb Smirnoff 		mtx_unlock(&sfs->mtx);
14933a2a37bSGleb Smirnoff 	}
15033a2a37bSGleb Smirnoff }
15133a2a37bSGleb Smirnoff 
152cec06a3eSJohn Baldwin static void
153cec06a3eSJohn Baldwin sendfile_free_mext_pg(struct mbuf *m)
154cec06a3eSJohn Baldwin {
155cec06a3eSJohn Baldwin 	struct mbuf_ext_pgs *ext_pgs;
156cec06a3eSJohn Baldwin 	vm_page_t pg;
157*98549e2dSMark Johnston 	int flags, i;
158*98549e2dSMark Johnston 	bool cache_last;
159cec06a3eSJohn Baldwin 
160cec06a3eSJohn Baldwin 	KASSERT(m->m_flags & M_EXT && m->m_ext.ext_type == EXT_PGS,
161cec06a3eSJohn Baldwin 	    ("%s: m %p !M_EXT or !EXT_PGS", __func__, m));
162cec06a3eSJohn Baldwin 
163cec06a3eSJohn Baldwin 	cache_last = m->m_ext.ext_flags & EXT_FLAG_CACHE_LAST;
164cec06a3eSJohn Baldwin 	ext_pgs = m->m_ext.ext_pgs;
165*98549e2dSMark Johnston 	flags = (m->m_ext.ext_flags & EXT_FLAG_NOCACHE) != 0 ? VPR_TRYFREE : 0;
166cec06a3eSJohn Baldwin 
167cec06a3eSJohn Baldwin 	for (i = 0; i < ext_pgs->npgs; i++) {
168cec06a3eSJohn Baldwin 		if (cache_last && i == ext_pgs->npgs - 1)
169*98549e2dSMark Johnston 			flags = 0;
170cec06a3eSJohn Baldwin 		pg = PHYS_TO_VM_PAGE(ext_pgs->pa[i]);
171*98549e2dSMark Johnston 		vm_page_release(pg, flags);
172cec06a3eSJohn Baldwin 	}
173cec06a3eSJohn Baldwin 
174cec06a3eSJohn Baldwin 	if (m->m_ext.ext_flags & EXT_FLAG_SYNC) {
175cec06a3eSJohn Baldwin 		struct sendfile_sync *sfs = m->m_ext.ext_arg2;
176cec06a3eSJohn Baldwin 
177cec06a3eSJohn Baldwin 		mtx_lock(&sfs->mtx);
178cec06a3eSJohn Baldwin 		KASSERT(sfs->count > 0, ("Sendfile sync botchup count == 0"));
179cec06a3eSJohn Baldwin 		if (--sfs->count == 0)
180cec06a3eSJohn Baldwin 			cv_signal(&sfs->cv);
181cec06a3eSJohn Baldwin 		mtx_unlock(&sfs->mtx);
182cec06a3eSJohn Baldwin 	}
183cec06a3eSJohn Baldwin }
184cec06a3eSJohn Baldwin 
18533a2a37bSGleb Smirnoff /*
18633a2a37bSGleb Smirnoff  * Helper function to calculate how much data to put into page i of n.
18733a2a37bSGleb Smirnoff  * Only first and last pages are special.
18833a2a37bSGleb Smirnoff  */
18933a2a37bSGleb Smirnoff static inline off_t
19033a2a37bSGleb Smirnoff xfsize(int i, int n, off_t off, off_t len)
19133a2a37bSGleb Smirnoff {
19233a2a37bSGleb Smirnoff 
19333a2a37bSGleb Smirnoff 	if (i == 0)
19433a2a37bSGleb Smirnoff 		return (omin(PAGE_SIZE - (off & PAGE_MASK), len));
19533a2a37bSGleb Smirnoff 
19633a2a37bSGleb Smirnoff 	if (i == n - 1 && ((off + len) & PAGE_MASK) > 0)
19733a2a37bSGleb Smirnoff 		return ((off + len) & PAGE_MASK);
19833a2a37bSGleb Smirnoff 
19933a2a37bSGleb Smirnoff 	return (PAGE_SIZE);
20033a2a37bSGleb Smirnoff }
20133a2a37bSGleb Smirnoff 
20233a2a37bSGleb Smirnoff /*
20333a2a37bSGleb Smirnoff  * Helper function to get offset within object for i page.
20433a2a37bSGleb Smirnoff  */
205d712b799SAlan Cox static inline vm_ooffset_t
20633a2a37bSGleb Smirnoff vmoff(int i, off_t off)
20733a2a37bSGleb Smirnoff {
20833a2a37bSGleb Smirnoff 
20933a2a37bSGleb Smirnoff 	if (i == 0)
210d712b799SAlan Cox 		return ((vm_ooffset_t)off);
21133a2a37bSGleb Smirnoff 
21233a2a37bSGleb Smirnoff 	return (trunc_page(off + i * PAGE_SIZE));
21333a2a37bSGleb Smirnoff }
21433a2a37bSGleb Smirnoff 
21533a2a37bSGleb Smirnoff /*
21633a2a37bSGleb Smirnoff  * Helper function used when allocation of a page or sf_buf failed.
21733a2a37bSGleb Smirnoff  * Pretend as if we don't have enough space, subtract xfsize() of
21833a2a37bSGleb Smirnoff  * all pages that failed.
21933a2a37bSGleb Smirnoff  */
22033a2a37bSGleb Smirnoff static inline void
22133a2a37bSGleb Smirnoff fixspace(int old, int new, off_t off, int *space)
22233a2a37bSGleb Smirnoff {
22333a2a37bSGleb Smirnoff 
22433a2a37bSGleb Smirnoff 	KASSERT(old > new, ("%s: old %d new %d", __func__, old, new));
22533a2a37bSGleb Smirnoff 
22633a2a37bSGleb Smirnoff 	/* Subtract last one. */
22733a2a37bSGleb Smirnoff 	*space -= xfsize(old - 1, old, off, *space);
22833a2a37bSGleb Smirnoff 	old--;
22933a2a37bSGleb Smirnoff 
23033a2a37bSGleb Smirnoff 	if (new == old)
23133a2a37bSGleb Smirnoff 		/* There was only one page. */
23233a2a37bSGleb Smirnoff 		return;
23333a2a37bSGleb Smirnoff 
23433a2a37bSGleb Smirnoff 	/* Subtract first one. */
23533a2a37bSGleb Smirnoff 	if (new == 0) {
23633a2a37bSGleb Smirnoff 		*space -= xfsize(0, old, off, *space);
23733a2a37bSGleb Smirnoff 		new++;
23833a2a37bSGleb Smirnoff 	}
23933a2a37bSGleb Smirnoff 
24033a2a37bSGleb Smirnoff 	/* Rest of pages are full sized. */
24133a2a37bSGleb Smirnoff 	*space -= (old - new) * PAGE_SIZE;
24233a2a37bSGleb Smirnoff 
24333a2a37bSGleb Smirnoff 	KASSERT(*space >= 0, ("%s: space went backwards", __func__));
24433a2a37bSGleb Smirnoff }
24533a2a37bSGleb Smirnoff 
24633a2a37bSGleb Smirnoff /*
24733a2a37bSGleb Smirnoff  * I/O completion callback.
24833a2a37bSGleb Smirnoff  */
24933a2a37bSGleb Smirnoff static void
25033a2a37bSGleb Smirnoff sendfile_iodone(void *arg, vm_page_t *pg, int count, int error)
25133a2a37bSGleb Smirnoff {
25233a2a37bSGleb Smirnoff 	struct sf_io *sfio = arg;
253d37aa3ccSGleb Smirnoff 	struct socket *so = sfio->so;
25433a2a37bSGleb Smirnoff 
25533a2a37bSGleb Smirnoff 	for (int i = 0; i < count; i++)
2565dba303dSGleb Smirnoff 		if (pg[i] != bogus_page)
25733a2a37bSGleb Smirnoff 			vm_page_xunbusy(pg[i]);
25833a2a37bSGleb Smirnoff 
25933a2a37bSGleb Smirnoff 	if (error)
26033a2a37bSGleb Smirnoff 		sfio->error = error;
26133a2a37bSGleb Smirnoff 
26233a2a37bSGleb Smirnoff 	if (!refcount_release(&sfio->nios))
26333a2a37bSGleb Smirnoff 		return;
26433a2a37bSGleb Smirnoff 
265b4f55763SGleb Smirnoff 	CURVNET_SET(so->so_vnet);
26633a2a37bSGleb Smirnoff 	if (sfio->error) {
26733a2a37bSGleb Smirnoff 		/*
26833a2a37bSGleb Smirnoff 		 * I/O operation failed.  The state of data in the socket
26933a2a37bSGleb Smirnoff 		 * is now inconsistent, and all what we can do is to tear
27033a2a37bSGleb Smirnoff 		 * it down. Protocol abort method would tear down protocol
27133a2a37bSGleb Smirnoff 		 * state, free all ready mbufs and detach not ready ones.
27233a2a37bSGleb Smirnoff 		 * We will free the mbufs corresponding to this I/O manually.
27333a2a37bSGleb Smirnoff 		 *
27433a2a37bSGleb Smirnoff 		 * The socket would be marked with EIO and made available
27533a2a37bSGleb Smirnoff 		 * for read, so that application receives EIO on next
27633a2a37bSGleb Smirnoff 		 * syscall and eventually closes the socket.
27733a2a37bSGleb Smirnoff 		 */
27833a2a37bSGleb Smirnoff 		so->so_proto->pr_usrreqs->pru_abort(so);
27933a2a37bSGleb Smirnoff 		so->so_error = EIO;
28033a2a37bSGleb Smirnoff 
281cec06a3eSJohn Baldwin 		mb_free_notready(sfio->m, sfio->npages);
282b4f55763SGleb Smirnoff 	} else
28333a2a37bSGleb Smirnoff 		(void)(so->so_proto->pr_usrreqs->pru_ready)(so, sfio->m,
28433a2a37bSGleb Smirnoff 		    sfio->npages);
28533a2a37bSGleb Smirnoff 
286d37aa3ccSGleb Smirnoff 	SOCK_LOCK(so);
287d37aa3ccSGleb Smirnoff 	sorele(so);
288b4f55763SGleb Smirnoff 	CURVNET_RESTORE();
28933a2a37bSGleb Smirnoff 	free(sfio, M_TEMP);
29033a2a37bSGleb Smirnoff }
29133a2a37bSGleb Smirnoff 
29233a2a37bSGleb Smirnoff /*
29333a2a37bSGleb Smirnoff  * Iterate through pages vector and request paging for non-valid pages.
29433a2a37bSGleb Smirnoff  */
29533a2a37bSGleb Smirnoff static int
296fca79580SAlan Somers sendfile_swapin(vm_object_t obj, struct sf_io *sfio, int *nios, off_t off,
297fca79580SAlan Somers     off_t len, int npages, int rhpages, int flags)
29833a2a37bSGleb Smirnoff {
29933a2a37bSGleb Smirnoff 	vm_page_t *pa = sfio->pa;
300fca79580SAlan Somers 	int grabbed;
30133a2a37bSGleb Smirnoff 
302fca79580SAlan Somers 	*nios = 0;
30333a2a37bSGleb Smirnoff 	flags = (flags & SF_NODISKIO) ? VM_ALLOC_NOWAIT : 0;
30433a2a37bSGleb Smirnoff 
30533a2a37bSGleb Smirnoff 	/*
30633a2a37bSGleb Smirnoff 	 * First grab all the pages and wire them.  Note that we grab
30733a2a37bSGleb Smirnoff 	 * only required pages.  Readahead pages are dealt with later.
30833a2a37bSGleb Smirnoff 	 */
30933a2a37bSGleb Smirnoff 	VM_OBJECT_WLOCK(obj);
310af0460beSMark Johnston 
311af0460beSMark Johnston 	grabbed = vm_page_grab_pages(obj, OFF_TO_IDX(off),
312af0460beSMark Johnston 	    VM_ALLOC_NORMAL | VM_ALLOC_WIRED | flags, pa, npages);
313af0460beSMark Johnston 	if (grabbed < npages) {
314af0460beSMark Johnston 		for (int i = grabbed; i < npages; i++)
315af0460beSMark Johnston 			pa[i] = NULL;
316af0460beSMark Johnston 		npages = grabbed;
31733a2a37bSGleb Smirnoff 		rhpages = 0;
31833a2a37bSGleb Smirnoff 	}
31933a2a37bSGleb Smirnoff 
32033a2a37bSGleb Smirnoff 	for (int i = 0; i < npages;) {
321fca79580SAlan Somers 		int j, a, count, rv;
32233a2a37bSGleb Smirnoff 
32333a2a37bSGleb Smirnoff 		/* Skip valid pages. */
32433a2a37bSGleb Smirnoff 		if (vm_page_is_valid(pa[i], vmoff(i, off) & PAGE_MASK,
32533a2a37bSGleb Smirnoff 		    xfsize(i, npages, off, len))) {
32633a2a37bSGleb Smirnoff 			vm_page_xunbusy(pa[i]);
32733a2a37bSGleb Smirnoff 			SFSTAT_INC(sf_pages_valid);
32833a2a37bSGleb Smirnoff 			i++;
32933a2a37bSGleb Smirnoff 			continue;
33033a2a37bSGleb Smirnoff 		}
33133a2a37bSGleb Smirnoff 
33233a2a37bSGleb Smirnoff 		/*
3335dba303dSGleb Smirnoff 		 * Next page is invalid.  Check if it belongs to pager.  It
3345dba303dSGleb Smirnoff 		 * may not be there, which is a regular situation for shmem
3355dba303dSGleb Smirnoff 		 * pager.  For vnode pager this happens only in case of
3365dba303dSGleb Smirnoff 		 * a sparse file.
33733a2a37bSGleb Smirnoff 		 *
33833a2a37bSGleb Smirnoff 		 * Important feature of vm_pager_has_page() is the hint
33933a2a37bSGleb Smirnoff 		 * stored in 'a', about how many pages we can pagein after
34033a2a37bSGleb Smirnoff 		 * this page in a single I/O.
34133a2a37bSGleb Smirnoff 		 */
3425dba303dSGleb Smirnoff 		if (!vm_pager_has_page(obj, OFF_TO_IDX(vmoff(i, off)), NULL,
3435dba303dSGleb Smirnoff 		    &a)) {
34433a2a37bSGleb Smirnoff 			pmap_zero_page(pa[i]);
34533a2a37bSGleb Smirnoff 			pa[i]->valid = VM_PAGE_BITS_ALL;
3466921451dSAlan Cox 			MPASS(pa[i]->dirty == 0);
34733a2a37bSGleb Smirnoff 			vm_page_xunbusy(pa[i]);
34833a2a37bSGleb Smirnoff 			i++;
34933a2a37bSGleb Smirnoff 			continue;
3505dba303dSGleb Smirnoff 		}
35133a2a37bSGleb Smirnoff 
35233a2a37bSGleb Smirnoff 		/*
35333a2a37bSGleb Smirnoff 		 * We want to pagein as many pages as possible, limited only
35433a2a37bSGleb Smirnoff 		 * by the 'a' hint and actual request.
35533a2a37bSGleb Smirnoff 		 */
35633a2a37bSGleb Smirnoff 		count = min(a + 1, npages - i);
35733a2a37bSGleb Smirnoff 
3585dba303dSGleb Smirnoff 		/*
3595dba303dSGleb Smirnoff 		 * We should not pagein into a valid page, thus we first trim
3605dba303dSGleb Smirnoff 		 * any valid pages off the end of request, and substitute
3615dba303dSGleb Smirnoff 		 * to bogus_page those, that are in the middle.
3625dba303dSGleb Smirnoff 		 */
3635dba303dSGleb Smirnoff 		for (j = i + count - 1; j > i; j--) {
3645dba303dSGleb Smirnoff 			if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK,
3655dba303dSGleb Smirnoff 			    xfsize(j, npages, off, len))) {
3665dba303dSGleb Smirnoff 				count--;
3675dba303dSGleb Smirnoff 				rhpages = 0;
3685dba303dSGleb Smirnoff 			} else
3695dba303dSGleb Smirnoff 				break;
3705dba303dSGleb Smirnoff 		}
3715dba303dSGleb Smirnoff 		for (j = i + 1; j < i + count - 1; j++)
3725dba303dSGleb Smirnoff 			if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK,
3735dba303dSGleb Smirnoff 			    xfsize(j, npages, off, len))) {
3745dba303dSGleb Smirnoff 				vm_page_xunbusy(pa[j]);
3755dba303dSGleb Smirnoff 				SFSTAT_INC(sf_pages_valid);
3765dba303dSGleb Smirnoff 				SFSTAT_INC(sf_pages_bogus);
3775dba303dSGleb Smirnoff 				pa[j] = bogus_page;
3785dba303dSGleb Smirnoff 			}
3795dba303dSGleb Smirnoff 
38033a2a37bSGleb Smirnoff 		refcount_acquire(&sfio->nios);
38133a2a37bSGleb Smirnoff 		rv = vm_pager_get_pages_async(obj, pa + i, count, NULL,
38233a2a37bSGleb Smirnoff 		    i + count == npages ? &rhpages : NULL,
38333a2a37bSGleb Smirnoff 		    &sendfile_iodone, sfio);
384fca79580SAlan Somers 		if (rv != VM_PAGER_OK) {
3850367bca4SAlan Somers 			for (j = i; j < i + count; j++) {
3860367bca4SAlan Somers 				if (pa[j] != bogus_page) {
3870367bca4SAlan Somers 					vm_page_lock(pa[j]);
3880367bca4SAlan Somers 					vm_page_unwire(pa[j], PQ_INACTIVE);
3890367bca4SAlan Somers 					vm_page_unlock(pa[j]);
3900367bca4SAlan Somers 				}
391fca79580SAlan Somers 			}
392fca79580SAlan Somers 			VM_OBJECT_WUNLOCK(obj);
3930367bca4SAlan Somers 			return (EIO);
394fca79580SAlan Somers 		}
39533a2a37bSGleb Smirnoff 		KASSERT(rv == VM_PAGER_OK, ("%s: pager fail obj %p page %p",
39633a2a37bSGleb Smirnoff 		    __func__, obj, pa[i]));
39733a2a37bSGleb Smirnoff 
39833a2a37bSGleb Smirnoff 		SFSTAT_INC(sf_iocnt);
39933a2a37bSGleb Smirnoff 		SFSTAT_ADD(sf_pages_read, count);
40033a2a37bSGleb Smirnoff 		if (i + count == npages)
40133a2a37bSGleb Smirnoff 			SFSTAT_ADD(sf_rhpages_read, rhpages);
40233a2a37bSGleb Smirnoff 
4035dba303dSGleb Smirnoff 		/*
4045dba303dSGleb Smirnoff 		 * Restore the valid page pointers.  They are already
4055dba303dSGleb Smirnoff 		 * unbusied, but still wired.
4065dba303dSGleb Smirnoff 		 */
4075dba303dSGleb Smirnoff 		for (j = i; j < i + count; j++)
4085dba303dSGleb Smirnoff 			if (pa[j] == bogus_page) {
4095dba303dSGleb Smirnoff 				pa[j] = vm_page_lookup(obj,
4105dba303dSGleb Smirnoff 				    OFF_TO_IDX(vmoff(j, off)));
4115dba303dSGleb Smirnoff 				KASSERT(pa[j], ("%s: page %p[%d] disappeared",
4125dba303dSGleb Smirnoff 				    __func__, pa, j));
4135dba303dSGleb Smirnoff 
4145dba303dSGleb Smirnoff 			}
41533a2a37bSGleb Smirnoff 		i += count;
416fca79580SAlan Somers 		(*nios)++;
41733a2a37bSGleb Smirnoff 	}
41833a2a37bSGleb Smirnoff 
41933a2a37bSGleb Smirnoff 	VM_OBJECT_WUNLOCK(obj);
42033a2a37bSGleb Smirnoff 
421fca79580SAlan Somers 	if (*nios == 0 && npages != 0)
42233a2a37bSGleb Smirnoff 		SFSTAT_INC(sf_noiocnt);
42333a2a37bSGleb Smirnoff 
424fca79580SAlan Somers 	return (0);
42533a2a37bSGleb Smirnoff }
42633a2a37bSGleb Smirnoff 
42733a2a37bSGleb Smirnoff static int
42833a2a37bSGleb Smirnoff sendfile_getobj(struct thread *td, struct file *fp, vm_object_t *obj_res,
42933a2a37bSGleb Smirnoff     struct vnode **vp_res, struct shmfd **shmfd_res, off_t *obj_size,
43033a2a37bSGleb Smirnoff     int *bsize)
43133a2a37bSGleb Smirnoff {
43233a2a37bSGleb Smirnoff 	struct vattr va;
43333a2a37bSGleb Smirnoff 	vm_object_t obj;
43433a2a37bSGleb Smirnoff 	struct vnode *vp;
43533a2a37bSGleb Smirnoff 	struct shmfd *shmfd;
43633a2a37bSGleb Smirnoff 	int error;
43733a2a37bSGleb Smirnoff 
43833a2a37bSGleb Smirnoff 	vp = *vp_res = NULL;
43933a2a37bSGleb Smirnoff 	obj = NULL;
44033a2a37bSGleb Smirnoff 	shmfd = *shmfd_res = NULL;
44133a2a37bSGleb Smirnoff 	*bsize = 0;
44233a2a37bSGleb Smirnoff 
44333a2a37bSGleb Smirnoff 	/*
44433a2a37bSGleb Smirnoff 	 * The file descriptor must be a regular file and have a
44533a2a37bSGleb Smirnoff 	 * backing VM object.
44633a2a37bSGleb Smirnoff 	 */
44733a2a37bSGleb Smirnoff 	if (fp->f_type == DTYPE_VNODE) {
44833a2a37bSGleb Smirnoff 		vp = fp->f_vnode;
44933a2a37bSGleb Smirnoff 		vn_lock(vp, LK_SHARED | LK_RETRY);
45033a2a37bSGleb Smirnoff 		if (vp->v_type != VREG) {
45133a2a37bSGleb Smirnoff 			error = EINVAL;
45233a2a37bSGleb Smirnoff 			goto out;
45333a2a37bSGleb Smirnoff 		}
45433a2a37bSGleb Smirnoff 		*bsize = vp->v_mount->mnt_stat.f_iosize;
45533a2a37bSGleb Smirnoff 		error = VOP_GETATTR(vp, &va, td->td_ucred);
45633a2a37bSGleb Smirnoff 		if (error != 0)
45733a2a37bSGleb Smirnoff 			goto out;
45833a2a37bSGleb Smirnoff 		*obj_size = va.va_size;
45933a2a37bSGleb Smirnoff 		obj = vp->v_object;
46033a2a37bSGleb Smirnoff 		if (obj == NULL) {
46133a2a37bSGleb Smirnoff 			error = EINVAL;
46233a2a37bSGleb Smirnoff 			goto out;
46333a2a37bSGleb Smirnoff 		}
46433a2a37bSGleb Smirnoff 	} else if (fp->f_type == DTYPE_SHM) {
46533a2a37bSGleb Smirnoff 		error = 0;
46633a2a37bSGleb Smirnoff 		shmfd = fp->f_data;
46733a2a37bSGleb Smirnoff 		obj = shmfd->shm_object;
46833a2a37bSGleb Smirnoff 		*obj_size = shmfd->shm_size;
46933a2a37bSGleb Smirnoff 	} else {
47033a2a37bSGleb Smirnoff 		error = EINVAL;
47133a2a37bSGleb Smirnoff 		goto out;
47233a2a37bSGleb Smirnoff 	}
47333a2a37bSGleb Smirnoff 
47433a2a37bSGleb Smirnoff 	VM_OBJECT_WLOCK(obj);
47533a2a37bSGleb Smirnoff 	if ((obj->flags & OBJ_DEAD) != 0) {
47633a2a37bSGleb Smirnoff 		VM_OBJECT_WUNLOCK(obj);
47733a2a37bSGleb Smirnoff 		error = EBADF;
47833a2a37bSGleb Smirnoff 		goto out;
47933a2a37bSGleb Smirnoff 	}
48033a2a37bSGleb Smirnoff 
48133a2a37bSGleb Smirnoff 	/*
48233a2a37bSGleb Smirnoff 	 * Temporarily increase the backing VM object's reference
48333a2a37bSGleb Smirnoff 	 * count so that a forced reclamation of its vnode does not
48433a2a37bSGleb Smirnoff 	 * immediately destroy it.
48533a2a37bSGleb Smirnoff 	 */
48633a2a37bSGleb Smirnoff 	vm_object_reference_locked(obj);
48733a2a37bSGleb Smirnoff 	VM_OBJECT_WUNLOCK(obj);
48833a2a37bSGleb Smirnoff 	*obj_res = obj;
48933a2a37bSGleb Smirnoff 	*vp_res = vp;
49033a2a37bSGleb Smirnoff 	*shmfd_res = shmfd;
49133a2a37bSGleb Smirnoff 
49233a2a37bSGleb Smirnoff out:
49333a2a37bSGleb Smirnoff 	if (vp != NULL)
49433a2a37bSGleb Smirnoff 		VOP_UNLOCK(vp, 0);
49533a2a37bSGleb Smirnoff 	return (error);
49633a2a37bSGleb Smirnoff }
49733a2a37bSGleb Smirnoff 
49833a2a37bSGleb Smirnoff static int
49933a2a37bSGleb Smirnoff sendfile_getsock(struct thread *td, int s, struct file **sock_fp,
50033a2a37bSGleb Smirnoff     struct socket **so)
50133a2a37bSGleb Smirnoff {
50233a2a37bSGleb Smirnoff 	int error;
50333a2a37bSGleb Smirnoff 
50433a2a37bSGleb Smirnoff 	*sock_fp = NULL;
50533a2a37bSGleb Smirnoff 	*so = NULL;
50633a2a37bSGleb Smirnoff 
50733a2a37bSGleb Smirnoff 	/*
50833a2a37bSGleb Smirnoff 	 * The socket must be a stream socket and connected.
50933a2a37bSGleb Smirnoff 	 */
510cbd92ce6SMatt Macy 	error = getsock_cap(td, s, &cap_send_rights,
51185b0f9deSMariusz Zaborski 	    sock_fp, NULL, NULL);
51233a2a37bSGleb Smirnoff 	if (error != 0)
51333a2a37bSGleb Smirnoff 		return (error);
51433a2a37bSGleb Smirnoff 	*so = (*sock_fp)->f_data;
51533a2a37bSGleb Smirnoff 	if ((*so)->so_type != SOCK_STREAM)
51633a2a37bSGleb Smirnoff 		return (EINVAL);
51747f1ea51SGleb Smirnoff 	if (SOLISTENING(*so))
51847f1ea51SGleb Smirnoff 		return (ENOTCONN);
51933a2a37bSGleb Smirnoff 	return (0);
52033a2a37bSGleb Smirnoff }
52133a2a37bSGleb Smirnoff 
52233a2a37bSGleb Smirnoff int
52333a2a37bSGleb Smirnoff vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
52433a2a37bSGleb Smirnoff     struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
5259c64cfe5SGleb Smirnoff     struct thread *td)
52633a2a37bSGleb Smirnoff {
52733a2a37bSGleb Smirnoff 	struct file *sock_fp;
52833a2a37bSGleb Smirnoff 	struct vnode *vp;
52933a2a37bSGleb Smirnoff 	struct vm_object *obj;
53033a2a37bSGleb Smirnoff 	struct socket *so;
531cec06a3eSJohn Baldwin 	struct mbuf_ext_pgs *ext_pgs;
53233a2a37bSGleb Smirnoff 	struct mbuf *m, *mh, *mhtail;
53333a2a37bSGleb Smirnoff 	struct sf_buf *sf;
53433a2a37bSGleb Smirnoff 	struct shmfd *shmfd;
53533a2a37bSGleb Smirnoff 	struct sendfile_sync *sfs;
53633a2a37bSGleb Smirnoff 	struct vattr va;
53733a2a37bSGleb Smirnoff 	off_t off, sbytes, rem, obj_size;
538cec06a3eSJohn Baldwin 	int bsize, error, ext_pgs_idx, hdrlen, max_pgs, softerr;
539cec06a3eSJohn Baldwin 	bool use_ext_pgs;
54033a2a37bSGleb Smirnoff 
54133a2a37bSGleb Smirnoff 	obj = NULL;
54233a2a37bSGleb Smirnoff 	so = NULL;
54333a2a37bSGleb Smirnoff 	m = mh = NULL;
54433a2a37bSGleb Smirnoff 	sfs = NULL;
5459c64cfe5SGleb Smirnoff 	hdrlen = sbytes = 0;
54633a2a37bSGleb Smirnoff 	softerr = 0;
547cec06a3eSJohn Baldwin 	use_ext_pgs = false;
54833a2a37bSGleb Smirnoff 
54933a2a37bSGleb Smirnoff 	error = sendfile_getobj(td, fp, &obj, &vp, &shmfd, &obj_size, &bsize);
55033a2a37bSGleb Smirnoff 	if (error != 0)
55133a2a37bSGleb Smirnoff 		return (error);
55233a2a37bSGleb Smirnoff 
55333a2a37bSGleb Smirnoff 	error = sendfile_getsock(td, sockfd, &sock_fp, &so);
55433a2a37bSGleb Smirnoff 	if (error != 0)
55533a2a37bSGleb Smirnoff 		goto out;
55633a2a37bSGleb Smirnoff 
55733a2a37bSGleb Smirnoff #ifdef MAC
55833a2a37bSGleb Smirnoff 	error = mac_socket_check_send(td->td_ucred, so);
55933a2a37bSGleb Smirnoff 	if (error != 0)
56033a2a37bSGleb Smirnoff 		goto out;
56133a2a37bSGleb Smirnoff #endif
56233a2a37bSGleb Smirnoff 
56333a2a37bSGleb Smirnoff 	SFSTAT_INC(sf_syscalls);
56433a2a37bSGleb Smirnoff 	SFSTAT_ADD(sf_rhpages_requested, SF_READAHEAD(flags));
56533a2a37bSGleb Smirnoff 
56633a2a37bSGleb Smirnoff 	if (flags & SF_SYNC) {
56733a2a37bSGleb Smirnoff 		sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
56833a2a37bSGleb Smirnoff 		mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
56933a2a37bSGleb Smirnoff 		cv_init(&sfs->cv, "sendfile");
57033a2a37bSGleb Smirnoff 	}
57133a2a37bSGleb Smirnoff 
57233a2a37bSGleb Smirnoff 	rem = nbytes ? omin(nbytes, obj_size - offset) : obj_size - offset;
57333a2a37bSGleb Smirnoff 
57433a2a37bSGleb Smirnoff 	/*
57533a2a37bSGleb Smirnoff 	 * Protect against multiple writers to the socket.
57633a2a37bSGleb Smirnoff 	 *
57733a2a37bSGleb Smirnoff 	 * XXXRW: Historically this has assumed non-interruptibility, so now
57833a2a37bSGleb Smirnoff 	 * we implement that, but possibly shouldn't.
57933a2a37bSGleb Smirnoff 	 */
58033a2a37bSGleb Smirnoff 	(void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
58133a2a37bSGleb Smirnoff 
58233a2a37bSGleb Smirnoff 	/*
58333a2a37bSGleb Smirnoff 	 * Loop through the pages of the file, starting with the requested
58433a2a37bSGleb Smirnoff 	 * offset. Get a file page (do I/O if necessary), map the file page
58533a2a37bSGleb Smirnoff 	 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
58633a2a37bSGleb Smirnoff 	 * it on the socket.
58733a2a37bSGleb Smirnoff 	 * This is done in two loops.  The inner loop turns as many pages
58833a2a37bSGleb Smirnoff 	 * as it can, up to available socket buffer space, without blocking
58933a2a37bSGleb Smirnoff 	 * into mbufs to have it bulk delivered into the socket send buffer.
59033a2a37bSGleb Smirnoff 	 * The outer loop checks the state and available space of the socket
59133a2a37bSGleb Smirnoff 	 * and takes care of the overall progress.
59233a2a37bSGleb Smirnoff 	 */
59333a2a37bSGleb Smirnoff 	for (off = offset; rem > 0; ) {
59433a2a37bSGleb Smirnoff 		struct sf_io *sfio;
59533a2a37bSGleb Smirnoff 		vm_page_t *pa;
59633a2a37bSGleb Smirnoff 		struct mbuf *mtail;
59733a2a37bSGleb Smirnoff 		int nios, space, npages, rhpages;
59833a2a37bSGleb Smirnoff 
59933a2a37bSGleb Smirnoff 		mtail = NULL;
60033a2a37bSGleb Smirnoff 		/*
60133a2a37bSGleb Smirnoff 		 * Check the socket state for ongoing connection,
60233a2a37bSGleb Smirnoff 		 * no errors and space in socket buffer.
60333a2a37bSGleb Smirnoff 		 * If space is low allow for the remainder of the
60433a2a37bSGleb Smirnoff 		 * file to be processed if it fits the socket buffer.
60533a2a37bSGleb Smirnoff 		 * Otherwise block in waiting for sufficient space
60633a2a37bSGleb Smirnoff 		 * to proceed, or if the socket is nonblocking, return
60733a2a37bSGleb Smirnoff 		 * to userland with EAGAIN while reporting how far
60833a2a37bSGleb Smirnoff 		 * we've come.
60933a2a37bSGleb Smirnoff 		 * We wait until the socket buffer has significant free
61033a2a37bSGleb Smirnoff 		 * space to do bulk sends.  This makes good use of file
61133a2a37bSGleb Smirnoff 		 * system read ahead and allows packet segmentation
61233a2a37bSGleb Smirnoff 		 * offloading hardware to take over lots of work.  If
61333a2a37bSGleb Smirnoff 		 * we were not careful here we would send off only one
61433a2a37bSGleb Smirnoff 		 * sfbuf at a time.
61533a2a37bSGleb Smirnoff 		 */
61633a2a37bSGleb Smirnoff 		SOCKBUF_LOCK(&so->so_snd);
61733a2a37bSGleb Smirnoff 		if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
61833a2a37bSGleb Smirnoff 			so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
61933a2a37bSGleb Smirnoff retry_space:
62033a2a37bSGleb Smirnoff 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
62133a2a37bSGleb Smirnoff 			error = EPIPE;
62233a2a37bSGleb Smirnoff 			SOCKBUF_UNLOCK(&so->so_snd);
62333a2a37bSGleb Smirnoff 			goto done;
62433a2a37bSGleb Smirnoff 		} else if (so->so_error) {
62533a2a37bSGleb Smirnoff 			error = so->so_error;
62633a2a37bSGleb Smirnoff 			so->so_error = 0;
62733a2a37bSGleb Smirnoff 			SOCKBUF_UNLOCK(&so->so_snd);
62833a2a37bSGleb Smirnoff 			goto done;
62933a2a37bSGleb Smirnoff 		}
6301f9916edSSean Bruno 		if ((so->so_state & SS_ISCONNECTED) == 0) {
6311f9916edSSean Bruno 			SOCKBUF_UNLOCK(&so->so_snd);
6321f9916edSSean Bruno 			error = ENOTCONN;
6331f9916edSSean Bruno 			goto done;
6341f9916edSSean Bruno 		}
6351f9916edSSean Bruno 
63633a2a37bSGleb Smirnoff 		space = sbspace(&so->so_snd);
63733a2a37bSGleb Smirnoff 		if (space < rem &&
63833a2a37bSGleb Smirnoff 		    (space <= 0 ||
63933a2a37bSGleb Smirnoff 		     space < so->so_snd.sb_lowat)) {
64033a2a37bSGleb Smirnoff 			if (so->so_state & SS_NBIO) {
64133a2a37bSGleb Smirnoff 				SOCKBUF_UNLOCK(&so->so_snd);
64233a2a37bSGleb Smirnoff 				error = EAGAIN;
64333a2a37bSGleb Smirnoff 				goto done;
64433a2a37bSGleb Smirnoff 			}
64533a2a37bSGleb Smirnoff 			/*
64633a2a37bSGleb Smirnoff 			 * sbwait drops the lock while sleeping.
64733a2a37bSGleb Smirnoff 			 * When we loop back to retry_space the
64833a2a37bSGleb Smirnoff 			 * state may have changed and we retest
64933a2a37bSGleb Smirnoff 			 * for it.
65033a2a37bSGleb Smirnoff 			 */
65133a2a37bSGleb Smirnoff 			error = sbwait(&so->so_snd);
65233a2a37bSGleb Smirnoff 			/*
65333a2a37bSGleb Smirnoff 			 * An error from sbwait usually indicates that we've
65433a2a37bSGleb Smirnoff 			 * been interrupted by a signal. If we've sent anything
65533a2a37bSGleb Smirnoff 			 * then return bytes sent, otherwise return the error.
65633a2a37bSGleb Smirnoff 			 */
65733a2a37bSGleb Smirnoff 			if (error != 0) {
65833a2a37bSGleb Smirnoff 				SOCKBUF_UNLOCK(&so->so_snd);
65933a2a37bSGleb Smirnoff 				goto done;
66033a2a37bSGleb Smirnoff 			}
66133a2a37bSGleb Smirnoff 			goto retry_space;
66233a2a37bSGleb Smirnoff 		}
66333a2a37bSGleb Smirnoff 		SOCKBUF_UNLOCK(&so->so_snd);
66433a2a37bSGleb Smirnoff 
66533a2a37bSGleb Smirnoff 		/*
6669c64cfe5SGleb Smirnoff 		 * At the beginning of the first loop check if any headers
6679c64cfe5SGleb Smirnoff 		 * are specified and copy them into mbufs.  Reduce space in
6689c64cfe5SGleb Smirnoff 		 * the socket buffer by the size of the header mbuf chain.
6699c64cfe5SGleb Smirnoff 		 * Clear hdr_uio here and hdrlen at the end of the first loop.
67033a2a37bSGleb Smirnoff 		 */
6719c64cfe5SGleb Smirnoff 		if (hdr_uio != NULL && hdr_uio->uio_resid > 0) {
6729c64cfe5SGleb Smirnoff 			hdr_uio->uio_td = td;
6739c64cfe5SGleb Smirnoff 			hdr_uio->uio_rw = UIO_WRITE;
674a2d8f9d2SGleb Smirnoff 			mh = m_uiotombuf(hdr_uio, M_WAITOK, space, 0, 0);
6759c64cfe5SGleb Smirnoff 			hdrlen = m_length(mh, &mhtail);
67633a2a37bSGleb Smirnoff 			space -= hdrlen;
677a2d8f9d2SGleb Smirnoff 			/*
678a2d8f9d2SGleb Smirnoff 			 * If header consumed all the socket buffer space,
679a2d8f9d2SGleb Smirnoff 			 * don't waste CPU cycles and jump to the end.
680a2d8f9d2SGleb Smirnoff 			 */
681a2d8f9d2SGleb Smirnoff 			if (space == 0) {
682a2d8f9d2SGleb Smirnoff 				sfio = NULL;
683a2d8f9d2SGleb Smirnoff 				nios = 0;
684a2d8f9d2SGleb Smirnoff 				goto prepend_header;
685a2d8f9d2SGleb Smirnoff 			}
6869c64cfe5SGleb Smirnoff 			hdr_uio = NULL;
6879c64cfe5SGleb Smirnoff 		}
68833a2a37bSGleb Smirnoff 
68933a2a37bSGleb Smirnoff 		if (vp != NULL) {
69033a2a37bSGleb Smirnoff 			error = vn_lock(vp, LK_SHARED);
69133a2a37bSGleb Smirnoff 			if (error != 0)
69233a2a37bSGleb Smirnoff 				goto done;
69333a2a37bSGleb Smirnoff 			error = VOP_GETATTR(vp, &va, td->td_ucred);
69433a2a37bSGleb Smirnoff 			if (error != 0 || off >= va.va_size) {
69533a2a37bSGleb Smirnoff 				VOP_UNLOCK(vp, 0);
69633a2a37bSGleb Smirnoff 				goto done;
69733a2a37bSGleb Smirnoff 			}
69833a2a37bSGleb Smirnoff 			if (va.va_size != obj_size) {
69933a2a37bSGleb Smirnoff 				obj_size = va.va_size;
7009e3c8bd3SGleb Smirnoff 				rem = nbytes ?
7019e3c8bd3SGleb Smirnoff 				    omin(nbytes + offset, obj_size) : obj_size;
7029e3c8bd3SGleb Smirnoff 				rem -= off;
70333a2a37bSGleb Smirnoff 			}
70433a2a37bSGleb Smirnoff 		}
70533a2a37bSGleb Smirnoff 
70633a2a37bSGleb Smirnoff 		if (space > rem)
70733a2a37bSGleb Smirnoff 			space = rem;
708cec06a3eSJohn Baldwin 		else if (space > PAGE_SIZE) {
709cec06a3eSJohn Baldwin 			/*
710cec06a3eSJohn Baldwin 			 * Use page boundaries when possible for large
711cec06a3eSJohn Baldwin 			 * requests.
712cec06a3eSJohn Baldwin 			 */
713cec06a3eSJohn Baldwin 			if (off & PAGE_MASK)
714cec06a3eSJohn Baldwin 				space -= (PAGE_SIZE - (off & PAGE_MASK));
715cec06a3eSJohn Baldwin 			space = trunc_page(space);
716cec06a3eSJohn Baldwin 			if (off & PAGE_MASK)
717cec06a3eSJohn Baldwin 				space += (PAGE_SIZE - (off & PAGE_MASK));
718cec06a3eSJohn Baldwin 		}
71933a2a37bSGleb Smirnoff 
72033a2a37bSGleb Smirnoff 		npages = howmany(space + (off & PAGE_MASK), PAGE_SIZE);
72133a2a37bSGleb Smirnoff 
72233a2a37bSGleb Smirnoff 		/*
72333a2a37bSGleb Smirnoff 		 * Calculate maximum allowed number of pages for readahead
72400b5ffdeSGleb Smirnoff 		 * at this iteration.  If SF_USER_READAHEAD was set, we don't
72500b5ffdeSGleb Smirnoff 		 * do any heuristics and use exactly the value supplied by
72600b5ffdeSGleb Smirnoff 		 * application.  Otherwise, we allow readahead up to "rem".
72733a2a37bSGleb Smirnoff 		 * If application wants more, let it be, but there is no
72833a2a37bSGleb Smirnoff 		 * reason to go above MAXPHYS.  Also check against "obj_size",
72933a2a37bSGleb Smirnoff 		 * since vm_pager_has_page() can hint beyond EOF.
73033a2a37bSGleb Smirnoff 		 */
73100b5ffdeSGleb Smirnoff 		if (flags & SF_USER_READAHEAD) {
73200b5ffdeSGleb Smirnoff 			rhpages = SF_READAHEAD(flags);
73300b5ffdeSGleb Smirnoff 		} else {
73400b5ffdeSGleb Smirnoff 			rhpages = howmany(rem + (off & PAGE_MASK), PAGE_SIZE) -
73500b5ffdeSGleb Smirnoff 			    npages;
73633a2a37bSGleb Smirnoff 			rhpages += SF_READAHEAD(flags);
73700b5ffdeSGleb Smirnoff 		}
73833a2a37bSGleb Smirnoff 		rhpages = min(howmany(MAXPHYS, PAGE_SIZE), rhpages);
73933a2a37bSGleb Smirnoff 		rhpages = min(howmany(obj_size - trunc_page(off), PAGE_SIZE) -
74033a2a37bSGleb Smirnoff 		    npages, rhpages);
74133a2a37bSGleb Smirnoff 
74233a2a37bSGleb Smirnoff 		sfio = malloc(sizeof(struct sf_io) +
74333a2a37bSGleb Smirnoff 		    npages * sizeof(vm_page_t), M_TEMP, M_WAITOK);
74433a2a37bSGleb Smirnoff 		refcount_init(&sfio->nios, 1);
745d37aa3ccSGleb Smirnoff 		sfio->so = so;
74633a2a37bSGleb Smirnoff 		sfio->error = 0;
74733a2a37bSGleb Smirnoff 
748fca79580SAlan Somers 		error = sendfile_swapin(obj, sfio, &nios, off, space, npages,
749fca79580SAlan Somers 		    rhpages, flags);
7500367bca4SAlan Somers 		if (error != 0) {
751fca79580SAlan Somers 			if (vp != NULL)
752fca79580SAlan Somers 				VOP_UNLOCK(vp, 0);
7530367bca4SAlan Somers 			free(sfio, M_TEMP);
754fca79580SAlan Somers 			goto done;
755fca79580SAlan Somers 		}
75633a2a37bSGleb Smirnoff 
75733a2a37bSGleb Smirnoff 		/*
75833a2a37bSGleb Smirnoff 		 * Loop and construct maximum sized mbuf chain to be bulk
75933a2a37bSGleb Smirnoff 		 * dumped into socket buffer.
76033a2a37bSGleb Smirnoff 		 */
76133a2a37bSGleb Smirnoff 		pa = sfio->pa;
762cec06a3eSJohn Baldwin 
763cec06a3eSJohn Baldwin 		/*
764cec06a3eSJohn Baldwin 		 * Use unmapped mbufs if enabled for TCP.  Unmapped
765cec06a3eSJohn Baldwin 		 * bufs are restricted to TCP as that is what has been
766cec06a3eSJohn Baldwin 		 * tested.  In particular, unmapped mbufs have not
767cec06a3eSJohn Baldwin 		 * been tested with UNIX-domain sockets.
768cec06a3eSJohn Baldwin 		 */
769cec06a3eSJohn Baldwin 		if (mb_use_ext_pgs &&
770cec06a3eSJohn Baldwin 		    so->so_proto->pr_protocol == IPPROTO_TCP) {
771cec06a3eSJohn Baldwin 			use_ext_pgs = true;
772cec06a3eSJohn Baldwin 			max_pgs = MBUF_PEXT_MAX_PGS;
773cec06a3eSJohn Baldwin 
774cec06a3eSJohn Baldwin 			/* Start at last index, to wrap on first use. */
775cec06a3eSJohn Baldwin 			ext_pgs_idx = max_pgs - 1;
776cec06a3eSJohn Baldwin 		}
777cec06a3eSJohn Baldwin 
77833a2a37bSGleb Smirnoff 		for (int i = 0; i < npages; i++) {
77933a2a37bSGleb Smirnoff 			struct mbuf *m0;
78033a2a37bSGleb Smirnoff 
78133a2a37bSGleb Smirnoff 			/*
78233a2a37bSGleb Smirnoff 			 * If a page wasn't grabbed successfully, then
78333a2a37bSGleb Smirnoff 			 * trim the array. Can happen only with SF_NODISKIO.
78433a2a37bSGleb Smirnoff 			 */
78533a2a37bSGleb Smirnoff 			if (pa[i] == NULL) {
78633a2a37bSGleb Smirnoff 				SFSTAT_INC(sf_busy);
78733a2a37bSGleb Smirnoff 				fixspace(npages, i, off, &space);
78833a2a37bSGleb Smirnoff 				npages = i;
78933a2a37bSGleb Smirnoff 				softerr = EBUSY;
79033a2a37bSGleb Smirnoff 				break;
79133a2a37bSGleb Smirnoff 			}
79233a2a37bSGleb Smirnoff 
793cec06a3eSJohn Baldwin 			if (use_ext_pgs) {
794cec06a3eSJohn Baldwin 				off_t xfs;
795cec06a3eSJohn Baldwin 
796cec06a3eSJohn Baldwin 				ext_pgs_idx++;
797cec06a3eSJohn Baldwin 				if (ext_pgs_idx == max_pgs) {
798cec06a3eSJohn Baldwin 					m0 = mb_alloc_ext_pgs(M_WAITOK, false,
799cec06a3eSJohn Baldwin 					    sendfile_free_mext_pg);
800cec06a3eSJohn Baldwin 
801cec06a3eSJohn Baldwin 					if (flags & SF_NOCACHE) {
802cec06a3eSJohn Baldwin 						m0->m_ext.ext_flags |=
803cec06a3eSJohn Baldwin 						    EXT_FLAG_NOCACHE;
804cec06a3eSJohn Baldwin 
805cec06a3eSJohn Baldwin 						/*
806cec06a3eSJohn Baldwin 						 * See comment below regarding
807cec06a3eSJohn Baldwin 						 * ignoring SF_NOCACHE for the
808cec06a3eSJohn Baldwin 						 * last page.
809cec06a3eSJohn Baldwin 						 */
810cec06a3eSJohn Baldwin 						if ((npages - i <= max_pgs) &&
811cec06a3eSJohn Baldwin 						    ((off + space) & PAGE_MASK) &&
812cec06a3eSJohn Baldwin 						    (rem > space || rhpages > 0))
813cec06a3eSJohn Baldwin 							m0->m_ext.ext_flags |=
814cec06a3eSJohn Baldwin 							    EXT_FLAG_CACHE_LAST;
815cec06a3eSJohn Baldwin 					}
816cec06a3eSJohn Baldwin 					if (sfs != NULL) {
817cec06a3eSJohn Baldwin 						m0->m_ext.ext_flags |=
818cec06a3eSJohn Baldwin 						    EXT_FLAG_SYNC;
819cec06a3eSJohn Baldwin 						m0->m_ext.ext_arg2 = sfs;
820cec06a3eSJohn Baldwin 						mtx_lock(&sfs->mtx);
821cec06a3eSJohn Baldwin 						sfs->count++;
822cec06a3eSJohn Baldwin 						mtx_unlock(&sfs->mtx);
823cec06a3eSJohn Baldwin 					}
824cec06a3eSJohn Baldwin 					ext_pgs = m0->m_ext.ext_pgs;
825cec06a3eSJohn Baldwin 					if (i == 0)
826cec06a3eSJohn Baldwin 						sfio->m = m0;
827cec06a3eSJohn Baldwin 					ext_pgs_idx = 0;
828cec06a3eSJohn Baldwin 
829cec06a3eSJohn Baldwin 					/* Append to mbuf chain. */
830cec06a3eSJohn Baldwin 					if (mtail != NULL)
831cec06a3eSJohn Baldwin 						mtail->m_next = m0;
832cec06a3eSJohn Baldwin 					else
833cec06a3eSJohn Baldwin 						m = m0;
834cec06a3eSJohn Baldwin 					mtail = m0;
835cec06a3eSJohn Baldwin 					ext_pgs->first_pg_off =
836cec06a3eSJohn Baldwin 					    vmoff(i, off) & PAGE_MASK;
837cec06a3eSJohn Baldwin 				}
838cec06a3eSJohn Baldwin 				if (nios) {
839cec06a3eSJohn Baldwin 					mtail->m_flags |= M_NOTREADY;
840cec06a3eSJohn Baldwin 					ext_pgs->nrdy++;
841cec06a3eSJohn Baldwin 				}
842cec06a3eSJohn Baldwin 
843cec06a3eSJohn Baldwin 				ext_pgs->pa[ext_pgs_idx] = VM_PAGE_TO_PHYS(pa[i]);
844cec06a3eSJohn Baldwin 				ext_pgs->npgs++;
845cec06a3eSJohn Baldwin 				xfs = xfsize(i, npages, off, space);
846cec06a3eSJohn Baldwin 				ext_pgs->last_pg_len = xfs;
847cec06a3eSJohn Baldwin 				MBUF_EXT_PGS_ASSERT_SANITY(ext_pgs);
848cec06a3eSJohn Baldwin 				mtail->m_len += xfs;
849cec06a3eSJohn Baldwin 				mtail->m_ext.ext_size += PAGE_SIZE;
850cec06a3eSJohn Baldwin 				continue;
851cec06a3eSJohn Baldwin 			}
852cec06a3eSJohn Baldwin 
85333a2a37bSGleb Smirnoff 			/*
85433a2a37bSGleb Smirnoff 			 * Get a sendfile buf.  When allocating the
85533a2a37bSGleb Smirnoff 			 * first buffer for mbuf chain, we usually
85633a2a37bSGleb Smirnoff 			 * wait as long as necessary, but this wait
85733a2a37bSGleb Smirnoff 			 * can be interrupted.  For consequent
85833a2a37bSGleb Smirnoff 			 * buffers, do not sleep, since several
85933a2a37bSGleb Smirnoff 			 * threads might exhaust the buffers and then
86033a2a37bSGleb Smirnoff 			 * deadlock.
86133a2a37bSGleb Smirnoff 			 */
86233a2a37bSGleb Smirnoff 			sf = sf_buf_alloc(pa[i],
86333a2a37bSGleb Smirnoff 			    m != NULL ? SFB_NOWAIT : SFB_CATCH);
86433a2a37bSGleb Smirnoff 			if (sf == NULL) {
86533a2a37bSGleb Smirnoff 				SFSTAT_INC(sf_allocfail);
86633a2a37bSGleb Smirnoff 				for (int j = i; j < npages; j++) {
86733a2a37bSGleb Smirnoff 					vm_page_lock(pa[j]);
86833a2a37bSGleb Smirnoff 					vm_page_unwire(pa[j], PQ_INACTIVE);
86933a2a37bSGleb Smirnoff 					vm_page_unlock(pa[j]);
87033a2a37bSGleb Smirnoff 				}
87133a2a37bSGleb Smirnoff 				if (m == NULL)
87233a2a37bSGleb Smirnoff 					softerr = ENOBUFS;
87333a2a37bSGleb Smirnoff 				fixspace(npages, i, off, &space);
87433a2a37bSGleb Smirnoff 				npages = i;
87533a2a37bSGleb Smirnoff 				break;
87633a2a37bSGleb Smirnoff 			}
87733a2a37bSGleb Smirnoff 
87833a2a37bSGleb Smirnoff 			m0 = m_get(M_WAITOK, MT_DATA);
87933a2a37bSGleb Smirnoff 			m0->m_ext.ext_buf = (char *)sf_buf_kva(sf);
88033a2a37bSGleb Smirnoff 			m0->m_ext.ext_size = PAGE_SIZE;
88133a2a37bSGleb Smirnoff 			m0->m_ext.ext_arg1 = sf;
8829c82bec4SGleb Smirnoff 			m0->m_ext.ext_type = EXT_SFBUF;
8839c82bec4SGleb Smirnoff 			m0->m_ext.ext_flags = EXT_FLAG_EMBREF;
8849c82bec4SGleb Smirnoff 			m0->m_ext.ext_free = sendfile_free_mext;
88533a2a37bSGleb Smirnoff 			/*
88633a2a37bSGleb Smirnoff 			 * SF_NOCACHE sets the page as being freed upon send.
88733a2a37bSGleb Smirnoff 			 * However, we ignore it for the last page in 'space',
88833a2a37bSGleb Smirnoff 			 * if the page is truncated, and we got more data to
88933a2a37bSGleb Smirnoff 			 * send (rem > space), or if we have readahead
89033a2a37bSGleb Smirnoff 			 * configured (rhpages > 0).
89133a2a37bSGleb Smirnoff 			 */
8929c82bec4SGleb Smirnoff 			if ((flags & SF_NOCACHE) &&
8939c82bec4SGleb Smirnoff 			    (i != npages - 1 ||
8949c82bec4SGleb Smirnoff 			    !((off + space) & PAGE_MASK) ||
8959c82bec4SGleb Smirnoff 			    !(rem > space || rhpages > 0)))
8969c82bec4SGleb Smirnoff 				m0->m_ext.ext_flags |= EXT_FLAG_NOCACHE;
8979c82bec4SGleb Smirnoff 			if (sfs != NULL) {
8989c82bec4SGleb Smirnoff 				m0->m_ext.ext_flags |= EXT_FLAG_SYNC;
8999c82bec4SGleb Smirnoff 				m0->m_ext.ext_arg2 = sfs;
9009c82bec4SGleb Smirnoff 				mtx_lock(&sfs->mtx);
9019c82bec4SGleb Smirnoff 				sfs->count++;
9029c82bec4SGleb Smirnoff 				mtx_unlock(&sfs->mtx);
9039c82bec4SGleb Smirnoff 			}
90456a5f52eSGleb Smirnoff 			m0->m_ext.ext_count = 1;
90533a2a37bSGleb Smirnoff 			m0->m_flags |= (M_EXT | M_RDONLY);
90633a2a37bSGleb Smirnoff 			if (nios)
90733a2a37bSGleb Smirnoff 				m0->m_flags |= M_NOTREADY;
90833a2a37bSGleb Smirnoff 			m0->m_data = (char *)sf_buf_kva(sf) +
90933a2a37bSGleb Smirnoff 			    (vmoff(i, off) & PAGE_MASK);
91033a2a37bSGleb Smirnoff 			m0->m_len = xfsize(i, npages, off, space);
91133a2a37bSGleb Smirnoff 
91233a2a37bSGleb Smirnoff 			if (i == 0)
91333a2a37bSGleb Smirnoff 				sfio->m = m0;
91433a2a37bSGleb Smirnoff 
91533a2a37bSGleb Smirnoff 			/* Append to mbuf chain. */
91633a2a37bSGleb Smirnoff 			if (mtail != NULL)
91733a2a37bSGleb Smirnoff 				mtail->m_next = m0;
91833a2a37bSGleb Smirnoff 			else
91933a2a37bSGleb Smirnoff 				m = m0;
92033a2a37bSGleb Smirnoff 			mtail = m0;
92133a2a37bSGleb Smirnoff 		}
92233a2a37bSGleb Smirnoff 
92333a2a37bSGleb Smirnoff 		if (vp != NULL)
92433a2a37bSGleb Smirnoff 			VOP_UNLOCK(vp, 0);
92533a2a37bSGleb Smirnoff 
92633a2a37bSGleb Smirnoff 		/* Keep track of bytes processed. */
92733a2a37bSGleb Smirnoff 		off += space;
92833a2a37bSGleb Smirnoff 		rem -= space;
92933a2a37bSGleb Smirnoff 
93033a2a37bSGleb Smirnoff 		/* Prepend header, if any. */
93133a2a37bSGleb Smirnoff 		if (hdrlen) {
932a2d8f9d2SGleb Smirnoff prepend_header:
93333a2a37bSGleb Smirnoff 			mhtail->m_next = m;
93433a2a37bSGleb Smirnoff 			m = mh;
93533a2a37bSGleb Smirnoff 			mh = NULL;
93633a2a37bSGleb Smirnoff 		}
93733a2a37bSGleb Smirnoff 
93833a2a37bSGleb Smirnoff 		if (m == NULL) {
93933a2a37bSGleb Smirnoff 			KASSERT(softerr, ("%s: m NULL, no error", __func__));
94033a2a37bSGleb Smirnoff 			error = softerr;
94133a2a37bSGleb Smirnoff 			free(sfio, M_TEMP);
94233a2a37bSGleb Smirnoff 			goto done;
94333a2a37bSGleb Smirnoff 		}
94433a2a37bSGleb Smirnoff 
94533a2a37bSGleb Smirnoff 		/* Add the buffer chain to the socket buffer. */
94633a2a37bSGleb Smirnoff 		KASSERT(m_length(m, NULL) == space + hdrlen,
94733a2a37bSGleb Smirnoff 		    ("%s: mlen %u space %d hdrlen %d",
94833a2a37bSGleb Smirnoff 		    __func__, m_length(m, NULL), space, hdrlen));
94933a2a37bSGleb Smirnoff 
95033a2a37bSGleb Smirnoff 		CURVNET_SET(so->so_vnet);
95133a2a37bSGleb Smirnoff 		if (nios == 0) {
95233a2a37bSGleb Smirnoff 			/*
95333a2a37bSGleb Smirnoff 			 * If sendfile_swapin() didn't initiate any I/Os,
95433a2a37bSGleb Smirnoff 			 * which happens if all data is cached in VM, then
95533a2a37bSGleb Smirnoff 			 * we can send data right now without the
95633a2a37bSGleb Smirnoff 			 * PRUS_NOTREADY flag.
95733a2a37bSGleb Smirnoff 			 */
95833a2a37bSGleb Smirnoff 			free(sfio, M_TEMP);
95933a2a37bSGleb Smirnoff 			error = (*so->so_proto->pr_usrreqs->pru_send)
96033a2a37bSGleb Smirnoff 			    (so, 0, m, NULL, NULL, td);
96133a2a37bSGleb Smirnoff 		} else {
96233a2a37bSGleb Smirnoff 			sfio->npages = npages;
963d37aa3ccSGleb Smirnoff 			soref(so);
96433a2a37bSGleb Smirnoff 			error = (*so->so_proto->pr_usrreqs->pru_send)
96533a2a37bSGleb Smirnoff 			    (so, PRUS_NOTREADY, m, NULL, NULL, td);
96633a2a37bSGleb Smirnoff 			sendfile_iodone(sfio, NULL, 0, 0);
96733a2a37bSGleb Smirnoff 		}
96833a2a37bSGleb Smirnoff 		CURVNET_RESTORE();
96933a2a37bSGleb Smirnoff 
97033a2a37bSGleb Smirnoff 		m = NULL;	/* pru_send always consumes */
97133a2a37bSGleb Smirnoff 		if (error)
97233a2a37bSGleb Smirnoff 			goto done;
97333a2a37bSGleb Smirnoff 		sbytes += space + hdrlen;
97433a2a37bSGleb Smirnoff 		if (hdrlen)
97533a2a37bSGleb Smirnoff 			hdrlen = 0;
97633a2a37bSGleb Smirnoff 		if (softerr) {
97733a2a37bSGleb Smirnoff 			error = softerr;
97833a2a37bSGleb Smirnoff 			goto done;
97933a2a37bSGleb Smirnoff 		}
98033a2a37bSGleb Smirnoff 	}
98133a2a37bSGleb Smirnoff 
98233a2a37bSGleb Smirnoff 	/*
98333a2a37bSGleb Smirnoff 	 * Send trailers. Wimp out and use writev(2).
98433a2a37bSGleb Smirnoff 	 */
98533a2a37bSGleb Smirnoff 	if (trl_uio != NULL) {
98633a2a37bSGleb Smirnoff 		sbunlock(&so->so_snd);
98733a2a37bSGleb Smirnoff 		error = kern_writev(td, sockfd, trl_uio);
98833a2a37bSGleb Smirnoff 		if (error == 0)
98933a2a37bSGleb Smirnoff 			sbytes += td->td_retval[0];
99033a2a37bSGleb Smirnoff 		goto out;
99133a2a37bSGleb Smirnoff 	}
99233a2a37bSGleb Smirnoff 
99333a2a37bSGleb Smirnoff done:
99433a2a37bSGleb Smirnoff 	sbunlock(&so->so_snd);
99533a2a37bSGleb Smirnoff out:
99633a2a37bSGleb Smirnoff 	/*
99733a2a37bSGleb Smirnoff 	 * If there was no error we have to clear td->td_retval[0]
99833a2a37bSGleb Smirnoff 	 * because it may have been set by writev.
99933a2a37bSGleb Smirnoff 	 */
100033a2a37bSGleb Smirnoff 	if (error == 0) {
100133a2a37bSGleb Smirnoff 		td->td_retval[0] = 0;
100233a2a37bSGleb Smirnoff 	}
100333a2a37bSGleb Smirnoff 	if (sent != NULL) {
100433a2a37bSGleb Smirnoff 		(*sent) = sbytes;
100533a2a37bSGleb Smirnoff 	}
100633a2a37bSGleb Smirnoff 	if (obj != NULL)
100733a2a37bSGleb Smirnoff 		vm_object_deallocate(obj);
100833a2a37bSGleb Smirnoff 	if (so)
100933a2a37bSGleb Smirnoff 		fdrop(sock_fp, td);
101033a2a37bSGleb Smirnoff 	if (m)
101133a2a37bSGleb Smirnoff 		m_freem(m);
101233a2a37bSGleb Smirnoff 	if (mh)
101333a2a37bSGleb Smirnoff 		m_freem(mh);
101433a2a37bSGleb Smirnoff 
101533a2a37bSGleb Smirnoff 	if (sfs != NULL) {
101633a2a37bSGleb Smirnoff 		mtx_lock(&sfs->mtx);
101733a2a37bSGleb Smirnoff 		if (sfs->count != 0)
101833a2a37bSGleb Smirnoff 			cv_wait(&sfs->cv, &sfs->mtx);
101933a2a37bSGleb Smirnoff 		KASSERT(sfs->count == 0, ("sendfile sync still busy"));
102033a2a37bSGleb Smirnoff 		cv_destroy(&sfs->cv);
102133a2a37bSGleb Smirnoff 		mtx_destroy(&sfs->mtx);
102233a2a37bSGleb Smirnoff 		free(sfs, M_TEMP);
102333a2a37bSGleb Smirnoff 	}
102433a2a37bSGleb Smirnoff 
102533a2a37bSGleb Smirnoff 	if (error == ERESTART)
102633a2a37bSGleb Smirnoff 		error = EINTR;
102733a2a37bSGleb Smirnoff 
102833a2a37bSGleb Smirnoff 	return (error);
102933a2a37bSGleb Smirnoff }
103033a2a37bSGleb Smirnoff 
103133a2a37bSGleb Smirnoff static int
103233a2a37bSGleb Smirnoff sendfile(struct thread *td, struct sendfile_args *uap, int compat)
103333a2a37bSGleb Smirnoff {
103433a2a37bSGleb Smirnoff 	struct sf_hdtr hdtr;
103533a2a37bSGleb Smirnoff 	struct uio *hdr_uio, *trl_uio;
103633a2a37bSGleb Smirnoff 	struct file *fp;
103733a2a37bSGleb Smirnoff 	off_t sbytes;
103833a2a37bSGleb Smirnoff 	int error;
103933a2a37bSGleb Smirnoff 
104033a2a37bSGleb Smirnoff 	/*
104133a2a37bSGleb Smirnoff 	 * File offset must be positive.  If it goes beyond EOF
104233a2a37bSGleb Smirnoff 	 * we send only the header/trailer and no payload data.
104333a2a37bSGleb Smirnoff 	 */
104433a2a37bSGleb Smirnoff 	if (uap->offset < 0)
104533a2a37bSGleb Smirnoff 		return (EINVAL);
104633a2a37bSGleb Smirnoff 
1047ef3266d5SGleb Smirnoff 	sbytes = 0;
104833a2a37bSGleb Smirnoff 	hdr_uio = trl_uio = NULL;
104933a2a37bSGleb Smirnoff 
105033a2a37bSGleb Smirnoff 	if (uap->hdtr != NULL) {
105133a2a37bSGleb Smirnoff 		error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
105233a2a37bSGleb Smirnoff 		if (error != 0)
105333a2a37bSGleb Smirnoff 			goto out;
105433a2a37bSGleb Smirnoff 		if (hdtr.headers != NULL) {
105533a2a37bSGleb Smirnoff 			error = copyinuio(hdtr.headers, hdtr.hdr_cnt,
105633a2a37bSGleb Smirnoff 			    &hdr_uio);
105733a2a37bSGleb Smirnoff 			if (error != 0)
105833a2a37bSGleb Smirnoff 				goto out;
10599c64cfe5SGleb Smirnoff #ifdef COMPAT_FREEBSD4
10609c64cfe5SGleb Smirnoff 			/*
10619c64cfe5SGleb Smirnoff 			 * In FreeBSD < 5.0 the nbytes to send also included
10629c64cfe5SGleb Smirnoff 			 * the header.  If compat is specified subtract the
10639c64cfe5SGleb Smirnoff 			 * header size from nbytes.
10649c64cfe5SGleb Smirnoff 			 */
10659c64cfe5SGleb Smirnoff 			if (compat) {
10669c64cfe5SGleb Smirnoff 				if (uap->nbytes > hdr_uio->uio_resid)
10679c64cfe5SGleb Smirnoff 					uap->nbytes -= hdr_uio->uio_resid;
10689c64cfe5SGleb Smirnoff 				else
10699c64cfe5SGleb Smirnoff 					uap->nbytes = 0;
10709c64cfe5SGleb Smirnoff 			}
10719c64cfe5SGleb Smirnoff #endif
107233a2a37bSGleb Smirnoff 		}
107333a2a37bSGleb Smirnoff 		if (hdtr.trailers != NULL) {
107433a2a37bSGleb Smirnoff 			error = copyinuio(hdtr.trailers, hdtr.trl_cnt,
107533a2a37bSGleb Smirnoff 			    &trl_uio);
107633a2a37bSGleb Smirnoff 			if (error != 0)
107733a2a37bSGleb Smirnoff 				goto out;
107833a2a37bSGleb Smirnoff 		}
107933a2a37bSGleb Smirnoff 	}
108033a2a37bSGleb Smirnoff 
108133a2a37bSGleb Smirnoff 	AUDIT_ARG_FD(uap->fd);
108233a2a37bSGleb Smirnoff 
108333a2a37bSGleb Smirnoff 	/*
108433a2a37bSGleb Smirnoff 	 * sendfile(2) can start at any offset within a file so we require
108533a2a37bSGleb Smirnoff 	 * CAP_READ+CAP_SEEK = CAP_PREAD.
108633a2a37bSGleb Smirnoff 	 */
1087cbd92ce6SMatt Macy 	if ((error = fget_read(td, uap->fd, &cap_pread_rights, &fp)) != 0)
108833a2a37bSGleb Smirnoff 		goto out;
108933a2a37bSGleb Smirnoff 
109033a2a37bSGleb Smirnoff 	error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset,
10919c64cfe5SGleb Smirnoff 	    uap->nbytes, &sbytes, uap->flags, td);
109233a2a37bSGleb Smirnoff 	fdrop(fp, td);
109333a2a37bSGleb Smirnoff 
109433a2a37bSGleb Smirnoff 	if (uap->sbytes != NULL)
109533a2a37bSGleb Smirnoff 		copyout(&sbytes, uap->sbytes, sizeof(off_t));
109633a2a37bSGleb Smirnoff 
109733a2a37bSGleb Smirnoff out:
109833a2a37bSGleb Smirnoff 	free(hdr_uio, M_IOV);
109933a2a37bSGleb Smirnoff 	free(trl_uio, M_IOV);
110033a2a37bSGleb Smirnoff 	return (error);
110133a2a37bSGleb Smirnoff }
110233a2a37bSGleb Smirnoff 
110333a2a37bSGleb Smirnoff /*
110433a2a37bSGleb Smirnoff  * sendfile(2)
110533a2a37bSGleb Smirnoff  *
110633a2a37bSGleb Smirnoff  * int sendfile(int fd, int s, off_t offset, size_t nbytes,
110733a2a37bSGleb Smirnoff  *       struct sf_hdtr *hdtr, off_t *sbytes, int flags)
110833a2a37bSGleb Smirnoff  *
110933a2a37bSGleb Smirnoff  * Send a file specified by 'fd' and starting at 'offset' to a socket
111033a2a37bSGleb Smirnoff  * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
111133a2a37bSGleb Smirnoff  * 0.  Optionally add a header and/or trailer to the socket output.  If
111233a2a37bSGleb Smirnoff  * specified, write the total number of bytes sent into *sbytes.
111333a2a37bSGleb Smirnoff  */
111433a2a37bSGleb Smirnoff int
111533a2a37bSGleb Smirnoff sys_sendfile(struct thread *td, struct sendfile_args *uap)
111633a2a37bSGleb Smirnoff {
111733a2a37bSGleb Smirnoff 
111833a2a37bSGleb Smirnoff 	return (sendfile(td, uap, 0));
111933a2a37bSGleb Smirnoff }
112033a2a37bSGleb Smirnoff 
112133a2a37bSGleb Smirnoff #ifdef COMPAT_FREEBSD4
112233a2a37bSGleb Smirnoff int
112333a2a37bSGleb Smirnoff freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
112433a2a37bSGleb Smirnoff {
112533a2a37bSGleb Smirnoff 	struct sendfile_args args;
112633a2a37bSGleb Smirnoff 
112733a2a37bSGleb Smirnoff 	args.fd = uap->fd;
112833a2a37bSGleb Smirnoff 	args.s = uap->s;
112933a2a37bSGleb Smirnoff 	args.offset = uap->offset;
113033a2a37bSGleb Smirnoff 	args.nbytes = uap->nbytes;
113133a2a37bSGleb Smirnoff 	args.hdtr = uap->hdtr;
113233a2a37bSGleb Smirnoff 	args.sbytes = uap->sbytes;
113333a2a37bSGleb Smirnoff 	args.flags = uap->flags;
113433a2a37bSGleb Smirnoff 
113533a2a37bSGleb Smirnoff 	return (sendfile(td, &args, 1));
113633a2a37bSGleb Smirnoff }
113733a2a37bSGleb Smirnoff #endif /* COMPAT_FREEBSD4 */
1138