xref: /freebsd/sys/kern/kern_sendfile.c (revision 314cb279959b08811543612a715e47266f685c7b)
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>
31b2e60773SJohn Baldwin #include "opt_kern_tls.h"
32b2e60773SJohn Baldwin 
3333a2a37bSGleb Smirnoff #include <sys/param.h>
3433a2a37bSGleb Smirnoff #include <sys/systm.h>
3533a2a37bSGleb Smirnoff #include <sys/capsicum.h>
3633a2a37bSGleb Smirnoff #include <sys/kernel.h>
3733a2a37bSGleb Smirnoff #include <sys/lock.h>
38b2e60773SJohn Baldwin #include <sys/ktls.h>
3933a2a37bSGleb Smirnoff #include <sys/mutex.h>
4033a2a37bSGleb Smirnoff #include <sys/malloc.h>
4133a2a37bSGleb Smirnoff #include <sys/mman.h>
4233a2a37bSGleb Smirnoff #include <sys/mount.h>
4333a2a37bSGleb Smirnoff #include <sys/mbuf.h>
440ac8511aSKonstantin Belousov #include <sys/proc.h>
4533a2a37bSGleb Smirnoff #include <sys/protosw.h>
4633a2a37bSGleb Smirnoff #include <sys/rwlock.h>
4733a2a37bSGleb Smirnoff #include <sys/sf_buf.h>
4833a2a37bSGleb Smirnoff #include <sys/socket.h>
4933a2a37bSGleb Smirnoff #include <sys/socketvar.h>
5033a2a37bSGleb Smirnoff #include <sys/syscallsubr.h>
5133a2a37bSGleb Smirnoff #include <sys/sysctl.h>
520ac8511aSKonstantin Belousov #include <sys/sysproto.h>
5333a2a37bSGleb Smirnoff #include <sys/vnode.h>
5433a2a37bSGleb Smirnoff 
5533a2a37bSGleb Smirnoff #include <net/vnet.h>
560ac8511aSKonstantin Belousov #include <netinet/in.h>
579e14430dSJohn Baldwin #include <netinet/tcp.h>
5873ee5756SRandall Stewart #include <netinet/in_pcb.h>
5973ee5756SRandall Stewart #include <netinet/tcp_var.h>
6073ee5756SRandall Stewart #include <netinet/tcp_log_buf.h>
6133a2a37bSGleb Smirnoff 
6233a2a37bSGleb Smirnoff #include <security/audit/audit.h>
6333a2a37bSGleb Smirnoff #include <security/mac/mac_framework.h>
6433a2a37bSGleb Smirnoff 
6533a2a37bSGleb Smirnoff #include <vm/vm.h>
6633a2a37bSGleb Smirnoff #include <vm/vm_object.h>
6733a2a37bSGleb Smirnoff #include <vm/vm_pager.h>
6833a2a37bSGleb Smirnoff 
698f0a223cSKonstantin Belousov static MALLOC_DEFINE(M_SENDFILE, "sendfile", "sendfile dynamic memory");
708f0a223cSKonstantin Belousov 
719c82bec4SGleb Smirnoff #define	EXT_FLAG_SYNC		EXT_FLAG_VENDOR1
729c82bec4SGleb Smirnoff #define	EXT_FLAG_NOCACHE	EXT_FLAG_VENDOR2
73cec06a3eSJohn Baldwin #define	EXT_FLAG_CACHE_LAST	EXT_FLAG_VENDOR3
749c82bec4SGleb Smirnoff 
7533a2a37bSGleb Smirnoff /*
7633a2a37bSGleb Smirnoff  * Structure describing a single sendfile(2) I/O, which may consist of
7733a2a37bSGleb Smirnoff  * several underlying pager I/Os.
7833a2a37bSGleb Smirnoff  *
7933a2a37bSGleb Smirnoff  * The syscall context allocates the structure and initializes 'nios'
8033a2a37bSGleb Smirnoff  * to 1.  As sendfile_swapin() runs through pages and starts asynchronous
8133a2a37bSGleb Smirnoff  * paging operations, it increments 'nios'.
8233a2a37bSGleb Smirnoff  *
8333a2a37bSGleb Smirnoff  * Every I/O completion calls sendfile_iodone(), which decrements the 'nios',
8433a2a37bSGleb Smirnoff  * and the syscall also calls sendfile_iodone() after allocating all mbufs,
8533a2a37bSGleb Smirnoff  * linking them and sending to socket.  Whoever reaches zero 'nios' is
8633a2a37bSGleb Smirnoff  * responsible to * call pru_ready on the socket, to notify it of readyness
8733a2a37bSGleb Smirnoff  * of the data.
8833a2a37bSGleb Smirnoff  */
8933a2a37bSGleb Smirnoff struct sf_io {
9033a2a37bSGleb Smirnoff 	volatile u_int	nios;
9133a2a37bSGleb Smirnoff 	u_int		error;
9233a2a37bSGleb Smirnoff 	int		npages;
93d37aa3ccSGleb Smirnoff 	struct socket	*so;
9433a2a37bSGleb Smirnoff 	struct mbuf	*m;
95d6e13f3bSJeff Roberson 	vm_object_t	obj;
96c506a638SKonstantin Belousov 	vm_pindex_t	pindex0;
97818d7553SJohn Baldwin #ifdef KERN_TLS
98b2e60773SJohn Baldwin 	struct ktls_session *tls;
99818d7553SJohn Baldwin #endif
10033a2a37bSGleb Smirnoff 	vm_page_t	pa[];
10133a2a37bSGleb Smirnoff };
10233a2a37bSGleb Smirnoff 
10333a2a37bSGleb Smirnoff /*
10433a2a37bSGleb Smirnoff  * Structure used to track requests with SF_SYNC flag.
10533a2a37bSGleb Smirnoff  */
10633a2a37bSGleb Smirnoff struct sendfile_sync {
10733a2a37bSGleb Smirnoff 	struct mtx	mtx;
10833a2a37bSGleb Smirnoff 	struct cv	cv;
10933a2a37bSGleb Smirnoff 	unsigned	count;
110b7eae758SMark Johnston 	bool		waiting;
11133a2a37bSGleb Smirnoff };
11233a2a37bSGleb Smirnoff 
113b7eae758SMark Johnston static void
sendfile_sync_destroy(struct sendfile_sync * sfs)114b7eae758SMark Johnston sendfile_sync_destroy(struct sendfile_sync *sfs)
115b7eae758SMark Johnston {
116b7eae758SMark Johnston 	KASSERT(sfs->count == 0, ("sendfile sync %p still busy", sfs));
117b7eae758SMark Johnston 
118b7eae758SMark Johnston 	cv_destroy(&sfs->cv);
119b7eae758SMark Johnston 	mtx_destroy(&sfs->mtx);
120b7eae758SMark Johnston 	free(sfs, M_SENDFILE);
121b7eae758SMark Johnston }
122b7eae758SMark Johnston 
123b7eae758SMark Johnston static void
sendfile_sync_signal(struct sendfile_sync * sfs)124b7eae758SMark Johnston sendfile_sync_signal(struct sendfile_sync *sfs)
125b7eae758SMark Johnston {
126b7eae758SMark Johnston 	mtx_lock(&sfs->mtx);
127b7eae758SMark Johnston 	KASSERT(sfs->count > 0, ("sendfile sync %p not busy", sfs));
128b7eae758SMark Johnston 	if (--sfs->count == 0) {
129b7eae758SMark Johnston 		if (!sfs->waiting) {
130b7eae758SMark Johnston 			/* The sendfile() waiter was interrupted by a signal. */
131b7eae758SMark Johnston 			sendfile_sync_destroy(sfs);
132b7eae758SMark Johnston 			return;
133b7eae758SMark Johnston 		} else {
134b7eae758SMark Johnston 			cv_signal(&sfs->cv);
135b7eae758SMark Johnston 		}
136b7eae758SMark Johnston 	}
137b7eae758SMark Johnston 	mtx_unlock(&sfs->mtx);
138b7eae758SMark Johnston }
139b7eae758SMark Johnston 
14033a2a37bSGleb Smirnoff counter_u64_t sfstat[sizeof(struct sfstat) / sizeof(uint64_t)];
14133a2a37bSGleb Smirnoff 
14233a2a37bSGleb Smirnoff static void
sfstat_init(const void * unused)14333a2a37bSGleb Smirnoff sfstat_init(const void *unused)
14433a2a37bSGleb Smirnoff {
14533a2a37bSGleb Smirnoff 
14633a2a37bSGleb Smirnoff 	COUNTER_ARRAY_ALLOC(sfstat, sizeof(struct sfstat) / sizeof(uint64_t),
14733a2a37bSGleb Smirnoff 	    M_WAITOK);
14833a2a37bSGleb Smirnoff }
14933a2a37bSGleb Smirnoff SYSINIT(sfstat, SI_SUB_MBUF, SI_ORDER_FIRST, sfstat_init, NULL);
15033a2a37bSGleb Smirnoff 
15133a2a37bSGleb Smirnoff static int
sfstat_sysctl(SYSCTL_HANDLER_ARGS)15233a2a37bSGleb Smirnoff sfstat_sysctl(SYSCTL_HANDLER_ARGS)
15333a2a37bSGleb Smirnoff {
15433a2a37bSGleb Smirnoff 	struct sfstat s;
15533a2a37bSGleb Smirnoff 
15633a2a37bSGleb Smirnoff 	COUNTER_ARRAY_COPY(sfstat, &s, sizeof(s) / sizeof(uint64_t));
15733a2a37bSGleb Smirnoff 	if (req->newptr)
15833a2a37bSGleb Smirnoff 		COUNTER_ARRAY_ZERO(sfstat, sizeof(s) / sizeof(uint64_t));
15933a2a37bSGleb Smirnoff 	return (SYSCTL_OUT(req, &s, sizeof(s)));
16033a2a37bSGleb Smirnoff }
1617029da5cSPawel Biernacki SYSCTL_PROC(_kern_ipc, OID_AUTO, sfstat,
162fe27f1dbSAlexander Motin     CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
1637029da5cSPawel Biernacki     sfstat_sysctl, "I",
1647029da5cSPawel Biernacki     "sendfile statistics");
16533a2a37bSGleb Smirnoff 
1669c82bec4SGleb Smirnoff static void
sendfile_free_mext(struct mbuf * m)1679c82bec4SGleb Smirnoff sendfile_free_mext(struct mbuf *m)
1689c82bec4SGleb Smirnoff {
1699c82bec4SGleb Smirnoff 	struct sf_buf *sf;
1709c82bec4SGleb Smirnoff 	vm_page_t pg;
17198549e2dSMark Johnston 	int flags;
1729c82bec4SGleb Smirnoff 
1739c82bec4SGleb Smirnoff 	KASSERT(m->m_flags & M_EXT && m->m_ext.ext_type == EXT_SFBUF,
1749c82bec4SGleb Smirnoff 	    ("%s: m %p !M_EXT or !EXT_SFBUF", __func__, m));
1759c82bec4SGleb Smirnoff 
1769c82bec4SGleb Smirnoff 	sf = m->m_ext.ext_arg1;
1779c82bec4SGleb Smirnoff 	pg = sf_buf_page(sf);
17898549e2dSMark Johnston 	flags = (m->m_ext.ext_flags & EXT_FLAG_NOCACHE) != 0 ? VPR_TRYFREE : 0;
1799c82bec4SGleb Smirnoff 
1809c82bec4SGleb Smirnoff 	sf_buf_free(sf);
18198549e2dSMark Johnston 	vm_page_release(pg, flags);
1829c82bec4SGleb Smirnoff 
1839c82bec4SGleb Smirnoff 	if (m->m_ext.ext_flags & EXT_FLAG_SYNC) {
1849c82bec4SGleb Smirnoff 		struct sendfile_sync *sfs = m->m_ext.ext_arg2;
185b7eae758SMark Johnston 		sendfile_sync_signal(sfs);
18633a2a37bSGleb Smirnoff 	}
18733a2a37bSGleb Smirnoff }
18833a2a37bSGleb Smirnoff 
189cec06a3eSJohn Baldwin static void
sendfile_free_mext_pg(struct mbuf * m)190cec06a3eSJohn Baldwin sendfile_free_mext_pg(struct mbuf *m)
191cec06a3eSJohn Baldwin {
192cec06a3eSJohn Baldwin 	vm_page_t pg;
19398549e2dSMark Johnston 	int flags, i;
19498549e2dSMark Johnston 	bool cache_last;
195cec06a3eSJohn Baldwin 
19661664ee7SGleb Smirnoff 	M_ASSERTEXTPG(m);
197cec06a3eSJohn Baldwin 
198cec06a3eSJohn Baldwin 	cache_last = m->m_ext.ext_flags & EXT_FLAG_CACHE_LAST;
19998549e2dSMark Johnston 	flags = (m->m_ext.ext_flags & EXT_FLAG_NOCACHE) != 0 ? VPR_TRYFREE : 0;
200cec06a3eSJohn Baldwin 
2017b6c99d0SGleb Smirnoff 	for (i = 0; i < m->m_epg_npgs; i++) {
2027b6c99d0SGleb Smirnoff 		if (cache_last && i == m->m_epg_npgs - 1)
20398549e2dSMark Johnston 			flags = 0;
2040c103266SGleb Smirnoff 		pg = PHYS_TO_VM_PAGE(m->m_epg_pa[i]);
20598549e2dSMark Johnston 		vm_page_release(pg, flags);
206cec06a3eSJohn Baldwin 	}
207cec06a3eSJohn Baldwin 
208cec06a3eSJohn Baldwin 	if (m->m_ext.ext_flags & EXT_FLAG_SYNC) {
20923feb563SAndrew Gallatin 		struct sendfile_sync *sfs = m->m_ext.ext_arg1;
210b7eae758SMark Johnston 		sendfile_sync_signal(sfs);
211cec06a3eSJohn Baldwin 	}
212cec06a3eSJohn Baldwin }
213cec06a3eSJohn Baldwin 
21433a2a37bSGleb Smirnoff /*
21533a2a37bSGleb Smirnoff  * Helper function to calculate how much data to put into page i of n.
21633a2a37bSGleb Smirnoff  * Only first and last pages are special.
21733a2a37bSGleb Smirnoff  */
21833a2a37bSGleb Smirnoff static inline off_t
xfsize(int i,int n,off_t off,off_t len)21933a2a37bSGleb Smirnoff xfsize(int i, int n, off_t off, off_t len)
22033a2a37bSGleb Smirnoff {
22133a2a37bSGleb Smirnoff 
22233a2a37bSGleb Smirnoff 	if (i == 0)
22333a2a37bSGleb Smirnoff 		return (omin(PAGE_SIZE - (off & PAGE_MASK), len));
22433a2a37bSGleb Smirnoff 
22533a2a37bSGleb Smirnoff 	if (i == n - 1 && ((off + len) & PAGE_MASK) > 0)
22633a2a37bSGleb Smirnoff 		return ((off + len) & PAGE_MASK);
22733a2a37bSGleb Smirnoff 
22833a2a37bSGleb Smirnoff 	return (PAGE_SIZE);
22933a2a37bSGleb Smirnoff }
23033a2a37bSGleb Smirnoff 
23133a2a37bSGleb Smirnoff /*
23233a2a37bSGleb Smirnoff  * Helper function to get offset within object for i page.
23333a2a37bSGleb Smirnoff  */
234d712b799SAlan Cox static inline vm_ooffset_t
vmoff(int i,off_t off)23533a2a37bSGleb Smirnoff vmoff(int i, off_t off)
23633a2a37bSGleb Smirnoff {
23733a2a37bSGleb Smirnoff 
23833a2a37bSGleb Smirnoff 	if (i == 0)
239d712b799SAlan Cox 		return ((vm_ooffset_t)off);
24033a2a37bSGleb Smirnoff 
24133a2a37bSGleb Smirnoff 	return (trunc_page(off + i * PAGE_SIZE));
24233a2a37bSGleb Smirnoff }
24333a2a37bSGleb Smirnoff 
24433a2a37bSGleb Smirnoff /*
24533a2a37bSGleb Smirnoff  * Helper function used when allocation of a page or sf_buf failed.
24633a2a37bSGleb Smirnoff  * Pretend as if we don't have enough space, subtract xfsize() of
24733a2a37bSGleb Smirnoff  * all pages that failed.
24833a2a37bSGleb Smirnoff  */
24933a2a37bSGleb Smirnoff static inline void
fixspace(int old,int new,off_t off,int * space)25033a2a37bSGleb Smirnoff fixspace(int old, int new, off_t off, int *space)
25133a2a37bSGleb Smirnoff {
25233a2a37bSGleb Smirnoff 
25333a2a37bSGleb Smirnoff 	KASSERT(old > new, ("%s: old %d new %d", __func__, old, new));
25433a2a37bSGleb Smirnoff 
25533a2a37bSGleb Smirnoff 	/* Subtract last one. */
25633a2a37bSGleb Smirnoff 	*space -= xfsize(old - 1, old, off, *space);
25733a2a37bSGleb Smirnoff 	old--;
25833a2a37bSGleb Smirnoff 
25933a2a37bSGleb Smirnoff 	if (new == old)
26033a2a37bSGleb Smirnoff 		/* There was only one page. */
26133a2a37bSGleb Smirnoff 		return;
26233a2a37bSGleb Smirnoff 
26333a2a37bSGleb Smirnoff 	/* Subtract first one. */
26433a2a37bSGleb Smirnoff 	if (new == 0) {
26533a2a37bSGleb Smirnoff 		*space -= xfsize(0, old, off, *space);
26633a2a37bSGleb Smirnoff 		new++;
26733a2a37bSGleb Smirnoff 	}
26833a2a37bSGleb Smirnoff 
26933a2a37bSGleb Smirnoff 	/* Rest of pages are full sized. */
27033a2a37bSGleb Smirnoff 	*space -= (old - new) * PAGE_SIZE;
27133a2a37bSGleb Smirnoff 
27233a2a37bSGleb Smirnoff 	KASSERT(*space >= 0, ("%s: space went backwards", __func__));
27333a2a37bSGleb Smirnoff }
27433a2a37bSGleb Smirnoff 
27533a2a37bSGleb Smirnoff /*
27659e1ac9dSKonstantin Belousov  * Wait for all in-flight ios to complete, we must not unwire pages
27759e1ac9dSKonstantin Belousov  * under them.
27859e1ac9dSKonstantin Belousov  */
27959e1ac9dSKonstantin Belousov static void
sendfile_iowait(struct sf_io * sfio,const char * wmesg)28059e1ac9dSKonstantin Belousov sendfile_iowait(struct sf_io *sfio, const char *wmesg)
28159e1ac9dSKonstantin Belousov {
28259e1ac9dSKonstantin Belousov 	while (atomic_load_int(&sfio->nios) != 1)
28359e1ac9dSKonstantin Belousov 		pause(wmesg, 1);
28459e1ac9dSKonstantin Belousov }
28559e1ac9dSKonstantin Belousov 
28659e1ac9dSKonstantin Belousov /*
28733a2a37bSGleb Smirnoff  * I/O completion callback.
28833a2a37bSGleb Smirnoff  */
28933a2a37bSGleb Smirnoff static void
sendfile_iodone(void * arg,vm_page_t * pa,int count,int error)290c506a638SKonstantin Belousov sendfile_iodone(void *arg, vm_page_t *pa, int count, int error)
29133a2a37bSGleb Smirnoff {
29233a2a37bSGleb Smirnoff 	struct sf_io *sfio = arg;
2936bc27f08SGleb Smirnoff 	struct socket *so;
294c506a638SKonstantin Belousov 	int i;
29533a2a37bSGleb Smirnoff 
296c2ea3d44SChuck Silvers 	if (error != 0)
29733a2a37bSGleb Smirnoff 		sfio->error = error;
298c2ea3d44SChuck Silvers 
299c506a638SKonstantin Belousov 	/*
300c506a638SKonstantin Belousov 	 * Restore the valid page pointers.  They are already
301c2ea3d44SChuck Silvers 	 * unbusied, but still wired.
302c506a638SKonstantin Belousov 	 *
303c506a638SKonstantin Belousov 	 * XXXKIB since pages are only wired, and we do not
304c506a638SKonstantin Belousov 	 * own the object lock, other users might have
305c506a638SKonstantin Belousov 	 * invalidated them in meantime.  Similarly, after we
306c506a638SKonstantin Belousov 	 * unbusied the swapped-in pages, they can become
307c506a638SKonstantin Belousov 	 * invalid under us.
308c506a638SKonstantin Belousov 	 */
309e795a040SKonstantin Belousov 	MPASS(count == 0 || pa[0] != bogus_page);
310c506a638SKonstantin Belousov 	for (i = 0; i < count; i++) {
311c506a638SKonstantin Belousov 		if (pa[i] == bogus_page) {
312e795a040SKonstantin Belousov 			sfio->pa[(pa[0]->pindex - sfio->pindex0) + i] =
313c506a638SKonstantin Belousov 			    pa[i] = vm_page_relookup(sfio->obj,
314e795a040SKonstantin Belousov 			    pa[0]->pindex + i);
315c506a638SKonstantin Belousov 			KASSERT(pa[i] != NULL,
316c506a638SKonstantin Belousov 			    ("%s: page %p[%d] disappeared",
317c506a638SKonstantin Belousov 			    __func__, pa, i));
318c506a638SKonstantin Belousov 		} else {
319c506a638SKonstantin Belousov 			vm_page_xunbusy_unchecked(pa[i]);
320c506a638SKonstantin Belousov 		}
321c506a638SKonstantin Belousov 	}
32233a2a37bSGleb Smirnoff 
32333a2a37bSGleb Smirnoff 	if (!refcount_release(&sfio->nios))
32433a2a37bSGleb Smirnoff 		return;
32533a2a37bSGleb Smirnoff 
326acf65eefSKonstantin Belousov #ifdef INVARIANTS
327acf65eefSKonstantin Belousov 	for (i = 1; i < sfio->npages; i++) {
328acf65eefSKonstantin Belousov 		if (sfio->pa[i] == NULL)
329acf65eefSKonstantin Belousov 			break;
330acf65eefSKonstantin Belousov 		KASSERT(vm_page_wired(sfio->pa[i]),
331acf65eefSKonstantin Belousov 		    ("sfio %p page %d %p not wired", sfio, i, sfio->pa[i]));
332acf65eefSKonstantin Belousov 		if (i == 0)
333acf65eefSKonstantin Belousov 			continue;
334acf65eefSKonstantin Belousov 		KASSERT(sfio->pa[0]->object == sfio->pa[i]->object,
335acf65eefSKonstantin Belousov 		    ("sfio %p page %d %p wrong owner %p %p", sfio, i,
336acf65eefSKonstantin Belousov 		    sfio->pa[i], sfio->pa[0]->object, sfio->pa[i]->object));
337acf65eefSKonstantin Belousov 		KASSERT(sfio->pa[0]->pindex + i == sfio->pa[i]->pindex,
338acf65eefSKonstantin Belousov 		    ("sfio %p page %d %p wrong index %jx %jx", sfio, i,
339acf65eefSKonstantin Belousov 		    sfio->pa[i], (uintmax_t)sfio->pa[0]->pindex,
340acf65eefSKonstantin Belousov 		    (uintmax_t)sfio->pa[i]->pindex));
341acf65eefSKonstantin Belousov 	}
342acf65eefSKonstantin Belousov #endif
343acf65eefSKonstantin Belousov 
344d6e13f3bSJeff Roberson 	vm_object_pip_wakeup(sfio->obj);
345d6e13f3bSJeff Roberson 
3466bc27f08SGleb Smirnoff 	if (sfio->m == NULL) {
347b8c92303SGleb Smirnoff 		/*
3486bc27f08SGleb Smirnoff 		 * Either I/O operation failed, or we failed to allocate
3496bc27f08SGleb Smirnoff 		 * buffers, or we bailed out on first busy page, or we
3506bc27f08SGleb Smirnoff 		 * succeeded filling the request without any I/Os. Anyway,
3516bc27f08SGleb Smirnoff 		 * pru_send hadn't been executed - nothing had been sent
3526bc27f08SGleb Smirnoff 		 * to the socket yet.
353b8c92303SGleb Smirnoff 		 */
3546bc27f08SGleb Smirnoff 		MPASS((curthread->td_pflags & TDP_KTHREAD) == 0);
3558f0a223cSKonstantin Belousov 		free(sfio, M_SENDFILE);
356b8c92303SGleb Smirnoff 		return;
357b8c92303SGleb Smirnoff 	}
358b8c92303SGleb Smirnoff 
359818d7553SJohn Baldwin #if defined(KERN_TLS) && defined(INVARIANTS)
36061664ee7SGleb Smirnoff 	if ((sfio->m->m_flags & M_EXTPG) != 0)
3617b6c99d0SGleb Smirnoff 		KASSERT(sfio->tls == sfio->m->m_epg_tls,
362b2e60773SJohn Baldwin 		    ("TLS session mismatch"));
363b2e60773SJohn Baldwin 	else
364b2e60773SJohn Baldwin 		KASSERT(sfio->tls == NULL,
365b2e60773SJohn Baldwin 		    ("non-ext_pgs mbuf with TLS session"));
366b2e60773SJohn Baldwin #endif
3676bc27f08SGleb Smirnoff 	so = sfio->so;
368b4f55763SGleb Smirnoff 	CURVNET_SET(so->so_vnet);
369b8c92303SGleb Smirnoff 	if (__predict_false(sfio->error)) {
37033a2a37bSGleb Smirnoff 		/*
37133a2a37bSGleb Smirnoff 		 * I/O operation failed.  The state of data in the socket
37233a2a37bSGleb Smirnoff 		 * is now inconsistent, and all what we can do is to tear
37333a2a37bSGleb Smirnoff 		 * it down. Protocol abort method would tear down protocol
37433a2a37bSGleb Smirnoff 		 * state, free all ready mbufs and detach not ready ones.
37533a2a37bSGleb Smirnoff 		 * We will free the mbufs corresponding to this I/O manually.
37633a2a37bSGleb Smirnoff 		 *
37733a2a37bSGleb Smirnoff 		 * The socket would be marked with EIO and made available
37833a2a37bSGleb Smirnoff 		 * for read, so that application receives EIO on next
37933a2a37bSGleb Smirnoff 		 * syscall and eventually closes the socket.
38033a2a37bSGleb Smirnoff 		 */
381e7d02be1SGleb Smirnoff 		so->so_proto->pr_abort(so);
38233a2a37bSGleb Smirnoff 		so->so_error = EIO;
38333a2a37bSGleb Smirnoff 
384cec06a3eSJohn Baldwin 		mb_free_notready(sfio->m, sfio->npages);
385b2e60773SJohn Baldwin #ifdef KERN_TLS
3869e14430dSJohn Baldwin 	} else if (sfio->tls != NULL && sfio->tls->mode == TCP_TLS_MODE_SW) {
387b2e60773SJohn Baldwin 		/*
388b2e60773SJohn Baldwin 		 * I/O operation is complete, but we still need to
389b2e60773SJohn Baldwin 		 * encrypt.  We cannot do this in the interrupt thread
390b2e60773SJohn Baldwin 		 * of the disk controller, so forward the mbufs to a
391b2e60773SJohn Baldwin 		 * different thread.
392b2e60773SJohn Baldwin 		 *
393b2e60773SJohn Baldwin 		 * Donate the socket reference from sfio to rather
394b2e60773SJohn Baldwin 		 * than explicitly invoking soref().
395b2e60773SJohn Baldwin 		 */
396b2e60773SJohn Baldwin 		ktls_enqueue(sfio->m, so, sfio->npages);
397b2e60773SJohn Baldwin 		goto out_with_ref;
398b2e60773SJohn Baldwin #endif
399b4f55763SGleb Smirnoff 	} else
400e7d02be1SGleb Smirnoff 		(void)so->so_proto->pr_ready(so, sfio->m, sfio->npages);
40133a2a37bSGleb Smirnoff 
402d37aa3ccSGleb Smirnoff 	sorele(so);
403b2e60773SJohn Baldwin #ifdef KERN_TLS
404b2e60773SJohn Baldwin out_with_ref:
405b2e60773SJohn Baldwin #endif
406b4f55763SGleb Smirnoff 	CURVNET_RESTORE();
4078f0a223cSKonstantin Belousov 	free(sfio, M_SENDFILE);
40833a2a37bSGleb Smirnoff }
40933a2a37bSGleb Smirnoff 
41033a2a37bSGleb Smirnoff /*
41133a2a37bSGleb Smirnoff  * Iterate through pages vector and request paging for non-valid pages.
41233a2a37bSGleb Smirnoff  */
41333a2a37bSGleb Smirnoff static int
sendfile_swapin(vm_object_t obj,struct sf_io * sfio,int * nios,off_t off,off_t len,int rhpages,int flags)414fca79580SAlan Somers sendfile_swapin(vm_object_t obj, struct sf_io *sfio, int *nios, off_t off,
41526b23f07SMark Johnston     off_t len, int rhpages, int flags)
41633a2a37bSGleb Smirnoff {
417c506a638SKonstantin Belousov 	vm_page_t *pa;
41826b23f07SMark Johnston 	int a, count, count1, grabbed, i, j, npages, rv;
41933a2a37bSGleb Smirnoff 
420c506a638SKonstantin Belousov 	pa = sfio->pa;
42126b23f07SMark Johnston 	npages = sfio->npages;
422fca79580SAlan Somers 	*nios = 0;
42333a2a37bSGleb Smirnoff 	flags = (flags & SF_NODISKIO) ? VM_ALLOC_NOWAIT : 0;
424c506a638SKonstantin Belousov 	sfio->pindex0 = OFF_TO_IDX(off);
42533a2a37bSGleb Smirnoff 
42633a2a37bSGleb Smirnoff 	/*
42733a2a37bSGleb Smirnoff 	 * First grab all the pages and wire them.  Note that we grab
42833a2a37bSGleb Smirnoff 	 * only required pages.  Readahead pages are dealt with later.
42933a2a37bSGleb Smirnoff 	 */
4307aaf252cSJeff Roberson 	grabbed = vm_page_grab_pages_unlocked(obj, OFF_TO_IDX(off),
431af0460beSMark Johnston 	    VM_ALLOC_NORMAL | VM_ALLOC_WIRED | flags, pa, npages);
432af0460beSMark Johnston 	if (grabbed < npages) {
433af0460beSMark Johnston 		for (int i = grabbed; i < npages; i++)
434af0460beSMark Johnston 			pa[i] = NULL;
435af0460beSMark Johnston 		npages = grabbed;
43633a2a37bSGleb Smirnoff 		rhpages = 0;
43733a2a37bSGleb Smirnoff 	}
43833a2a37bSGleb Smirnoff 
439c506a638SKonstantin Belousov 	for (i = 0; i < npages;) {
44033a2a37bSGleb Smirnoff 		/* Skip valid pages. */
44133a2a37bSGleb Smirnoff 		if (vm_page_is_valid(pa[i], vmoff(i, off) & PAGE_MASK,
44233a2a37bSGleb Smirnoff 		    xfsize(i, npages, off, len))) {
44333a2a37bSGleb Smirnoff 			vm_page_xunbusy(pa[i]);
44433a2a37bSGleb Smirnoff 			SFSTAT_INC(sf_pages_valid);
44533a2a37bSGleb Smirnoff 			i++;
44633a2a37bSGleb Smirnoff 			continue;
44733a2a37bSGleb Smirnoff 		}
44833a2a37bSGleb Smirnoff 
44933a2a37bSGleb Smirnoff 		/*
4505dba303dSGleb Smirnoff 		 * Next page is invalid.  Check if it belongs to pager.  It
4515dba303dSGleb Smirnoff 		 * may not be there, which is a regular situation for shmem
4525dba303dSGleb Smirnoff 		 * pager.  For vnode pager this happens only in case of
4535dba303dSGleb Smirnoff 		 * a sparse file.
45433a2a37bSGleb Smirnoff 		 *
45533a2a37bSGleb Smirnoff 		 * Important feature of vm_pager_has_page() is the hint
45633a2a37bSGleb Smirnoff 		 * stored in 'a', about how many pages we can pagein after
45733a2a37bSGleb Smirnoff 		 * this page in a single I/O.
45833a2a37bSGleb Smirnoff 		 */
4596be21eb7SJeff Roberson 		VM_OBJECT_RLOCK(obj);
4605dba303dSGleb Smirnoff 		if (!vm_pager_has_page(obj, OFF_TO_IDX(vmoff(i, off)), NULL,
4615dba303dSGleb Smirnoff 		    &a)) {
4626be21eb7SJeff Roberson 			VM_OBJECT_RUNLOCK(obj);
46333a2a37bSGleb Smirnoff 			pmap_zero_page(pa[i]);
46491e31c3cSJeff Roberson 			vm_page_valid(pa[i]);
4656921451dSAlan Cox 			MPASS(pa[i]->dirty == 0);
46633a2a37bSGleb Smirnoff 			vm_page_xunbusy(pa[i]);
46733a2a37bSGleb Smirnoff 			i++;
46833a2a37bSGleb Smirnoff 			continue;
4695dba303dSGleb Smirnoff 		}
4706be21eb7SJeff Roberson 		VM_OBJECT_RUNLOCK(obj);
47133a2a37bSGleb Smirnoff 
47233a2a37bSGleb Smirnoff 		/*
47333a2a37bSGleb Smirnoff 		 * We want to pagein as many pages as possible, limited only
47433a2a37bSGleb Smirnoff 		 * by the 'a' hint and actual request.
47533a2a37bSGleb Smirnoff 		 */
47633a2a37bSGleb Smirnoff 		count = min(a + 1, npages - i);
47733a2a37bSGleb Smirnoff 
4785dba303dSGleb Smirnoff 		/*
479c506a638SKonstantin Belousov 		 * We should not pagein into a valid page because
480c506a638SKonstantin Belousov 		 * there might be still unfinished write tracked by
481c506a638SKonstantin Belousov 		 * e.g. a buffer, thus we substitute any valid pages
482c506a638SKonstantin Belousov 		 * with the bogus one.
483c506a638SKonstantin Belousov 		 *
484c506a638SKonstantin Belousov 		 * We must not leave around xbusy pages which are not
485c506a638SKonstantin Belousov 		 * part of the run passed to vm_pager_getpages(),
486c506a638SKonstantin Belousov 		 * otherwise pager might deadlock waiting for the busy
487c506a638SKonstantin Belousov 		 * status of the page, e.g. if it constitues the
488c506a638SKonstantin Belousov 		 * buffer needed to validate other page.
489c506a638SKonstantin Belousov 		 *
490c506a638SKonstantin Belousov 		 * First trim the end of the run consisting of the
491c506a638SKonstantin Belousov 		 * valid pages, then replace the rest of the valid
492c506a638SKonstantin Belousov 		 * with bogus.
4935dba303dSGleb Smirnoff 		 */
494c506a638SKonstantin Belousov 		count1 = count;
4955dba303dSGleb Smirnoff 		for (j = i + count - 1; j > i; j--) {
4965dba303dSGleb Smirnoff 			if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK,
4975dba303dSGleb Smirnoff 			    xfsize(j, npages, off, len))) {
498c506a638SKonstantin Belousov 				vm_page_xunbusy(pa[j]);
499c506a638SKonstantin Belousov 				SFSTAT_INC(sf_pages_valid);
5005dba303dSGleb Smirnoff 				count--;
501c506a638SKonstantin Belousov 			} else {
5025dba303dSGleb Smirnoff 				break;
5035dba303dSGleb Smirnoff 			}
504c506a638SKonstantin Belousov 		}
505c506a638SKonstantin Belousov 
506c506a638SKonstantin Belousov 		/*
507c506a638SKonstantin Belousov 		 * The last page in the run pa[i + count - 1] is
508c506a638SKonstantin Belousov 		 * guaranteed to be invalid by the trim above, so it
509c506a638SKonstantin Belousov 		 * is not replaced with bogus, thus -1 in the loop end
510c506a638SKonstantin Belousov 		 * condition.
511c506a638SKonstantin Belousov 		 */
512c506a638SKonstantin Belousov 		MPASS(pa[i + count - 1]->valid != VM_PAGE_BITS_ALL);
513c506a638SKonstantin Belousov 		for (j = i + 1; j < i + count - 1; j++) {
5145dba303dSGleb Smirnoff 			if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK,
5155dba303dSGleb Smirnoff 			    xfsize(j, npages, off, len))) {
5165dba303dSGleb Smirnoff 				vm_page_xunbusy(pa[j]);
5175dba303dSGleb Smirnoff 				SFSTAT_INC(sf_pages_valid);
5185dba303dSGleb Smirnoff 				SFSTAT_INC(sf_pages_bogus);
5195dba303dSGleb Smirnoff 				pa[j] = bogus_page;
5205dba303dSGleb Smirnoff 			}
521c506a638SKonstantin Belousov 		}
5225dba303dSGleb Smirnoff 
52333a2a37bSGleb Smirnoff 		refcount_acquire(&sfio->nios);
52433a2a37bSGleb Smirnoff 		rv = vm_pager_get_pages_async(obj, pa + i, count, NULL,
52533a2a37bSGleb Smirnoff 		    i + count == npages ? &rhpages : NULL,
52633a2a37bSGleb Smirnoff 		    &sendfile_iodone, sfio);
527b8c92303SGleb Smirnoff 		if (__predict_false(rv != VM_PAGER_OK)) {
52859e1ac9dSKonstantin Belousov 			sendfile_iowait(sfio, "sferrio");
52959e1ac9dSKonstantin Belousov 
530b8c92303SGleb Smirnoff 			/*
531c2ea3d44SChuck Silvers 			 * Do remaining pages recovery before returning EIO.
532b8c92303SGleb Smirnoff 			 * Pages from 0 to npages are wired.
533c2ea3d44SChuck Silvers 			 * Pages from (i + count1) to npages are busied.
534b8c92303SGleb Smirnoff 			 */
535b8c92303SGleb Smirnoff 			for (j = 0; j < npages; j++) {
536c2ea3d44SChuck Silvers 				if (j >= i + count1)
537b8c92303SGleb Smirnoff 					vm_page_xunbusy(pa[j]);
538b8c92303SGleb Smirnoff 				KASSERT(pa[j] != NULL && pa[j] != bogus_page,
539b8c92303SGleb Smirnoff 				    ("%s: page %p[%d] I/O recovery failure",
540b8c92303SGleb Smirnoff 				    __func__, pa, j));
5410367bca4SAlan Somers 				vm_page_unwire(pa[j], PQ_INACTIVE);
542214257daSMark Johnston 				pa[j] = NULL;
543fca79580SAlan Somers 			}
5440367bca4SAlan Somers 			return (EIO);
545fca79580SAlan Somers 		}
54633a2a37bSGleb Smirnoff 
54733a2a37bSGleb Smirnoff 		SFSTAT_INC(sf_iocnt);
54833a2a37bSGleb Smirnoff 		SFSTAT_ADD(sf_pages_read, count);
54933a2a37bSGleb Smirnoff 		if (i + count == npages)
55033a2a37bSGleb Smirnoff 			SFSTAT_ADD(sf_rhpages_read, rhpages);
55133a2a37bSGleb Smirnoff 
552c506a638SKonstantin Belousov 		i += count1;
553fca79580SAlan Somers 		(*nios)++;
55433a2a37bSGleb Smirnoff 	}
55533a2a37bSGleb Smirnoff 
556fca79580SAlan Somers 	if (*nios == 0 && npages != 0)
55733a2a37bSGleb Smirnoff 		SFSTAT_INC(sf_noiocnt);
55833a2a37bSGleb Smirnoff 
559fca79580SAlan Somers 	return (0);
56033a2a37bSGleb Smirnoff }
56133a2a37bSGleb Smirnoff 
56233a2a37bSGleb Smirnoff static int
sendfile_getobj(struct thread * td,struct file * fp,vm_object_t * obj_res,struct vnode ** vp_res,struct shmfd ** shmfd_res,off_t * obj_size,int * bsize)56333a2a37bSGleb Smirnoff sendfile_getobj(struct thread *td, struct file *fp, vm_object_t *obj_res,
56433a2a37bSGleb Smirnoff     struct vnode **vp_res, struct shmfd **shmfd_res, off_t *obj_size,
56533a2a37bSGleb Smirnoff     int *bsize)
56633a2a37bSGleb Smirnoff {
56733a2a37bSGleb Smirnoff 	vm_object_t obj;
56833a2a37bSGleb Smirnoff 	struct vnode *vp;
56933a2a37bSGleb Smirnoff 	struct shmfd *shmfd;
57033a2a37bSGleb Smirnoff 	int error;
57133a2a37bSGleb Smirnoff 
57252a99c72SMark Johnston 	error = 0;
57333a2a37bSGleb Smirnoff 	vp = *vp_res = NULL;
57433a2a37bSGleb Smirnoff 	obj = NULL;
57533a2a37bSGleb Smirnoff 	shmfd = *shmfd_res = NULL;
57633a2a37bSGleb Smirnoff 	*bsize = 0;
57733a2a37bSGleb Smirnoff 
57833a2a37bSGleb Smirnoff 	/*
57933a2a37bSGleb Smirnoff 	 * The file descriptor must be a regular file and have a
58033a2a37bSGleb Smirnoff 	 * backing VM object.
58133a2a37bSGleb Smirnoff 	 */
58233a2a37bSGleb Smirnoff 	if (fp->f_type == DTYPE_VNODE) {
58333a2a37bSGleb Smirnoff 		vp = fp->f_vnode;
58433a2a37bSGleb Smirnoff 		vn_lock(vp, LK_SHARED | LK_RETRY);
58533a2a37bSGleb Smirnoff 		if (vp->v_type != VREG) {
58633a2a37bSGleb Smirnoff 			error = EINVAL;
58733a2a37bSGleb Smirnoff 			goto out;
58833a2a37bSGleb Smirnoff 		}
58933a2a37bSGleb Smirnoff 		*bsize = vp->v_mount->mnt_stat.f_iosize;
59033a2a37bSGleb Smirnoff 		obj = vp->v_object;
59133a2a37bSGleb Smirnoff 		if (obj == NULL) {
59233a2a37bSGleb Smirnoff 			error = EINVAL;
59333a2a37bSGleb Smirnoff 			goto out;
59433a2a37bSGleb Smirnoff 		}
595faa998f6SMark Johnston 
596faa998f6SMark Johnston 		/*
597faa998f6SMark Johnston 		 * Use the pager size when available to simplify synchronization
598faa998f6SMark Johnston 		 * with filesystems, which otherwise must atomically update both
599faa998f6SMark Johnston 		 * the vnode pager size and file size.
600faa998f6SMark Johnston 		 */
601faa998f6SMark Johnston 		if (obj->type == OBJT_VNODE) {
602faa998f6SMark Johnston 			VM_OBJECT_RLOCK(obj);
603faa998f6SMark Johnston 			*obj_size = obj->un_pager.vnp.vnp_size;
604faa998f6SMark Johnston 		} else {
605f45feecfSMateusz Guzik 			error = vn_getsize_locked(vp, obj_size, td->td_ucred);
606faa998f6SMark Johnston 			if (error != 0)
607faa998f6SMark Johnston 				goto out;
608faa998f6SMark Johnston 			VM_OBJECT_RLOCK(obj);
609faa998f6SMark Johnston 		}
61033a2a37bSGleb Smirnoff 	} else if (fp->f_type == DTYPE_SHM) {
61133a2a37bSGleb Smirnoff 		shmfd = fp->f_data;
61233a2a37bSGleb Smirnoff 		obj = shmfd->shm_object;
613faa998f6SMark Johnston 		VM_OBJECT_RLOCK(obj);
61433a2a37bSGleb Smirnoff 		*obj_size = shmfd->shm_size;
61533a2a37bSGleb Smirnoff 	} else {
61633a2a37bSGleb Smirnoff 		error = EINVAL;
61733a2a37bSGleb Smirnoff 		goto out;
61833a2a37bSGleb Smirnoff 	}
61933a2a37bSGleb Smirnoff 
62033a2a37bSGleb Smirnoff 	if ((obj->flags & OBJ_DEAD) != 0) {
621faa998f6SMark Johnston 		VM_OBJECT_RUNLOCK(obj);
62233a2a37bSGleb Smirnoff 		error = EBADF;
62333a2a37bSGleb Smirnoff 		goto out;
62433a2a37bSGleb Smirnoff 	}
62533a2a37bSGleb Smirnoff 
62633a2a37bSGleb Smirnoff 	/*
62733a2a37bSGleb Smirnoff 	 * Temporarily increase the backing VM object's reference
62833a2a37bSGleb Smirnoff 	 * count so that a forced reclamation of its vnode does not
62933a2a37bSGleb Smirnoff 	 * immediately destroy it.
63033a2a37bSGleb Smirnoff 	 */
63133a2a37bSGleb Smirnoff 	vm_object_reference_locked(obj);
632faa998f6SMark Johnston 	VM_OBJECT_RUNLOCK(obj);
63333a2a37bSGleb Smirnoff 	*obj_res = obj;
63433a2a37bSGleb Smirnoff 	*vp_res = vp;
63533a2a37bSGleb Smirnoff 	*shmfd_res = shmfd;
63633a2a37bSGleb Smirnoff 
63733a2a37bSGleb Smirnoff out:
63833a2a37bSGleb Smirnoff 	if (vp != NULL)
639b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
64033a2a37bSGleb Smirnoff 	return (error);
64133a2a37bSGleb Smirnoff }
64233a2a37bSGleb Smirnoff 
64333a2a37bSGleb Smirnoff static int
sendfile_getsock(struct thread * td,int s,struct file ** sock_fp,struct socket ** so)64433a2a37bSGleb Smirnoff sendfile_getsock(struct thread *td, int s, struct file **sock_fp,
64533a2a37bSGleb Smirnoff     struct socket **so)
64633a2a37bSGleb Smirnoff {
64733a2a37bSGleb Smirnoff 	int error;
64833a2a37bSGleb Smirnoff 
64933a2a37bSGleb Smirnoff 	*sock_fp = NULL;
65033a2a37bSGleb Smirnoff 	*so = NULL;
65133a2a37bSGleb Smirnoff 
6520020e1b6SGleb Smirnoff 	/*
6530020e1b6SGleb Smirnoff 	 * The socket must be a stream socket and connected.
6540020e1b6SGleb Smirnoff 	 */
6553212ad15SMateusz Guzik 	error = getsock(td, s, &cap_send_rights, sock_fp);
65633a2a37bSGleb Smirnoff 	if (error != 0)
65733a2a37bSGleb Smirnoff 		return (error);
65833a2a37bSGleb Smirnoff 	*so = (*sock_fp)->f_data;
6590020e1b6SGleb Smirnoff 	if ((*so)->so_type != SOCK_STREAM)
6600020e1b6SGleb Smirnoff 		return (EINVAL);
661db4493f7SMichael Tuexen 	/*
6620020e1b6SGleb Smirnoff 	 * SCTP one-to-one style sockets currently don't work with
6630020e1b6SGleb Smirnoff 	 * sendfile(). So indicate EINVAL for now.
664db4493f7SMichael Tuexen 	 */
6650020e1b6SGleb Smirnoff 	if ((*so)->so_proto->pr_protocol == IPPROTO_SCTP)
666db4493f7SMichael Tuexen 		return (EINVAL);
66733a2a37bSGleb Smirnoff 	return (0);
66833a2a37bSGleb Smirnoff }
66933a2a37bSGleb Smirnoff 
67033a2a37bSGleb Smirnoff int
vn_sendfile(struct file * fp,int sockfd,struct uio * hdr_uio,struct uio * trl_uio,off_t offset,size_t nbytes,off_t * sent,int flags,struct thread * td)67133a2a37bSGleb Smirnoff vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
67233a2a37bSGleb Smirnoff     struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
6739c64cfe5SGleb Smirnoff     struct thread *td)
67433a2a37bSGleb Smirnoff {
67533a2a37bSGleb Smirnoff 	struct file *sock_fp;
67633a2a37bSGleb Smirnoff 	struct vnode *vp;
67733a2a37bSGleb Smirnoff 	struct vm_object *obj;
678f709eee6SKonstantin Belousov 	vm_page_t pga;
67933a2a37bSGleb Smirnoff 	struct socket *so;
680b2e60773SJohn Baldwin #ifdef KERN_TLS
681b2e60773SJohn Baldwin 	struct ktls_session *tls;
682b2e60773SJohn Baldwin #endif
68333a2a37bSGleb Smirnoff 	struct mbuf *m, *mh, *mhtail;
68433a2a37bSGleb Smirnoff 	struct sf_buf *sf;
68533a2a37bSGleb Smirnoff 	struct shmfd *shmfd;
68633a2a37bSGleb Smirnoff 	struct sendfile_sync *sfs;
68733a2a37bSGleb Smirnoff 	struct vattr va;
688faa998f6SMark Johnston 	off_t off, sbytes, rem, obj_size, nobj_size;
689cec06a3eSJohn Baldwin 	int bsize, error, ext_pgs_idx, hdrlen, max_pgs, softerr;
690b2e60773SJohn Baldwin #ifdef KERN_TLS
691b2e60773SJohn Baldwin 	int tls_enq_cnt;
692b2e60773SJohn Baldwin #endif
693cec06a3eSJohn Baldwin 	bool use_ext_pgs;
69433a2a37bSGleb Smirnoff 
69533a2a37bSGleb Smirnoff 	obj = NULL;
69633a2a37bSGleb Smirnoff 	so = NULL;
69733a2a37bSGleb Smirnoff 	m = mh = NULL;
69833a2a37bSGleb Smirnoff 	sfs = NULL;
699b2e60773SJohn Baldwin #ifdef KERN_TLS
700b2e60773SJohn Baldwin 	tls = NULL;
701b2e60773SJohn Baldwin #endif
7029c64cfe5SGleb Smirnoff 	hdrlen = sbytes = 0;
70333a2a37bSGleb Smirnoff 	softerr = 0;
704cec06a3eSJohn Baldwin 	use_ext_pgs = false;
70533a2a37bSGleb Smirnoff 
70633a2a37bSGleb Smirnoff 	error = sendfile_getobj(td, fp, &obj, &vp, &shmfd, &obj_size, &bsize);
70733a2a37bSGleb Smirnoff 	if (error != 0)
70833a2a37bSGleb Smirnoff 		return (error);
70933a2a37bSGleb Smirnoff 
71033a2a37bSGleb Smirnoff 	error = sendfile_getsock(td, sockfd, &sock_fp, &so);
71133a2a37bSGleb Smirnoff 	if (error != 0)
71233a2a37bSGleb Smirnoff 		goto out;
71333a2a37bSGleb Smirnoff 
71433a2a37bSGleb Smirnoff #ifdef MAC
71533a2a37bSGleb Smirnoff 	error = mac_socket_check_send(td->td_ucred, so);
71633a2a37bSGleb Smirnoff 	if (error != 0)
71733a2a37bSGleb Smirnoff 		goto out;
71833a2a37bSGleb Smirnoff #endif
71933a2a37bSGleb Smirnoff 
72033a2a37bSGleb Smirnoff 	SFSTAT_INC(sf_syscalls);
72133a2a37bSGleb Smirnoff 	SFSTAT_ADD(sf_rhpages_requested, SF_READAHEAD(flags));
72233a2a37bSGleb Smirnoff 
72333a2a37bSGleb Smirnoff 	if (flags & SF_SYNC) {
7248f0a223cSKonstantin Belousov 		sfs = malloc(sizeof(*sfs), M_SENDFILE, M_WAITOK | M_ZERO);
72533a2a37bSGleb Smirnoff 		mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
72633a2a37bSGleb Smirnoff 		cv_init(&sfs->cv, "sendfile");
727b7eae758SMark Johnston 		sfs->waiting = true;
72833a2a37bSGleb Smirnoff 	}
72933a2a37bSGleb Smirnoff 
73033a2a37bSGleb Smirnoff 	rem = nbytes ? omin(nbytes, obj_size - offset) : obj_size - offset;
73133a2a37bSGleb Smirnoff 
73233a2a37bSGleb Smirnoff 	/*
73333a2a37bSGleb Smirnoff 	 * Protect against multiple writers to the socket.
73433a2a37bSGleb Smirnoff 	 *
73533a2a37bSGleb Smirnoff 	 * XXXRW: Historically this has assumed non-interruptibility, so now
73633a2a37bSGleb Smirnoff 	 * we implement that, but possibly shouldn't.
73733a2a37bSGleb Smirnoff 	 */
738f94acf52SMark Johnston 	error = SOCK_IO_SEND_LOCK(so, SBL_WAIT | SBL_NOINTR);
739f94acf52SMark Johnston 	if (error != 0)
740f94acf52SMark Johnston 		goto out;
741b2e60773SJohn Baldwin #ifdef KERN_TLS
742b2e60773SJohn Baldwin 	tls = ktls_hold(so->so_snd.sb_tls_info);
743b2e60773SJohn Baldwin #endif
74433a2a37bSGleb Smirnoff 
74533a2a37bSGleb Smirnoff 	/*
74633a2a37bSGleb Smirnoff 	 * Loop through the pages of the file, starting with the requested
74733a2a37bSGleb Smirnoff 	 * offset. Get a file page (do I/O if necessary), map the file page
74833a2a37bSGleb Smirnoff 	 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
74933a2a37bSGleb Smirnoff 	 * it on the socket.
75033a2a37bSGleb Smirnoff 	 * This is done in two loops.  The inner loop turns as many pages
75133a2a37bSGleb Smirnoff 	 * as it can, up to available socket buffer space, without blocking
75233a2a37bSGleb Smirnoff 	 * into mbufs to have it bulk delivered into the socket send buffer.
75333a2a37bSGleb Smirnoff 	 * The outer loop checks the state and available space of the socket
75433a2a37bSGleb Smirnoff 	 * and takes care of the overall progress.
75533a2a37bSGleb Smirnoff 	 */
75633a2a37bSGleb Smirnoff 	for (off = offset; rem > 0; ) {
75733a2a37bSGleb Smirnoff 		struct sf_io *sfio;
75833a2a37bSGleb Smirnoff 		vm_page_t *pa;
7596bc27f08SGleb Smirnoff 		struct mbuf *m0, *mtail;
76033a2a37bSGleb Smirnoff 		int nios, space, npages, rhpages;
76133a2a37bSGleb Smirnoff 
76233a2a37bSGleb Smirnoff 		mtail = NULL;
76333a2a37bSGleb Smirnoff 		/*
76433a2a37bSGleb Smirnoff 		 * Check the socket state for ongoing connection,
76533a2a37bSGleb Smirnoff 		 * no errors and space in socket buffer.
76633a2a37bSGleb Smirnoff 		 * If space is low allow for the remainder of the
76733a2a37bSGleb Smirnoff 		 * file to be processed if it fits the socket buffer.
76833a2a37bSGleb Smirnoff 		 * Otherwise block in waiting for sufficient space
76933a2a37bSGleb Smirnoff 		 * to proceed, or if the socket is nonblocking, return
77033a2a37bSGleb Smirnoff 		 * to userland with EAGAIN while reporting how far
77133a2a37bSGleb Smirnoff 		 * we've come.
77233a2a37bSGleb Smirnoff 		 * We wait until the socket buffer has significant free
77333a2a37bSGleb Smirnoff 		 * space to do bulk sends.  This makes good use of file
77433a2a37bSGleb Smirnoff 		 * system read ahead and allows packet segmentation
77533a2a37bSGleb Smirnoff 		 * offloading hardware to take over lots of work.  If
77633a2a37bSGleb Smirnoff 		 * we were not careful here we would send off only one
77733a2a37bSGleb Smirnoff 		 * sfbuf at a time.
77833a2a37bSGleb Smirnoff 		 */
77933a2a37bSGleb Smirnoff 		SOCKBUF_LOCK(&so->so_snd);
78033a2a37bSGleb Smirnoff 		if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
78133a2a37bSGleb Smirnoff 			so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
78233a2a37bSGleb Smirnoff retry_space:
78333a2a37bSGleb Smirnoff 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
78433a2a37bSGleb Smirnoff 			error = EPIPE;
78533a2a37bSGleb Smirnoff 			SOCKBUF_UNLOCK(&so->so_snd);
78633a2a37bSGleb Smirnoff 			goto done;
78733a2a37bSGleb Smirnoff 		} else if (so->so_error) {
78833a2a37bSGleb Smirnoff 			error = so->so_error;
78933a2a37bSGleb Smirnoff 			so->so_error = 0;
79033a2a37bSGleb Smirnoff 			SOCKBUF_UNLOCK(&so->so_snd);
79133a2a37bSGleb Smirnoff 			goto done;
79233a2a37bSGleb Smirnoff 		}
7931f9916edSSean Bruno 		if ((so->so_state & SS_ISCONNECTED) == 0) {
7941f9916edSSean Bruno 			SOCKBUF_UNLOCK(&so->so_snd);
7951f9916edSSean Bruno 			error = ENOTCONN;
7961f9916edSSean Bruno 			goto done;
7971f9916edSSean Bruno 		}
7981f9916edSSean Bruno 
79933a2a37bSGleb Smirnoff 		space = sbspace(&so->so_snd);
80033a2a37bSGleb Smirnoff 		if (space < rem &&
80133a2a37bSGleb Smirnoff 		    (space <= 0 ||
80233a2a37bSGleb Smirnoff 		     space < so->so_snd.sb_lowat)) {
80333a2a37bSGleb Smirnoff 			if (so->so_state & SS_NBIO) {
80433a2a37bSGleb Smirnoff 				SOCKBUF_UNLOCK(&so->so_snd);
80533a2a37bSGleb Smirnoff 				error = EAGAIN;
80633a2a37bSGleb Smirnoff 				goto done;
80733a2a37bSGleb Smirnoff 			}
80833a2a37bSGleb Smirnoff 			/*
80933a2a37bSGleb Smirnoff 			 * sbwait drops the lock while sleeping.
81033a2a37bSGleb Smirnoff 			 * When we loop back to retry_space the
81133a2a37bSGleb Smirnoff 			 * state may have changed and we retest
81233a2a37bSGleb Smirnoff 			 * for it.
81333a2a37bSGleb Smirnoff 			 */
81443283184SGleb Smirnoff 			error = sbwait(so, SO_SND);
81533a2a37bSGleb Smirnoff 			/*
81633a2a37bSGleb Smirnoff 			 * An error from sbwait usually indicates that we've
81733a2a37bSGleb Smirnoff 			 * been interrupted by a signal. If we've sent anything
81833a2a37bSGleb Smirnoff 			 * then return bytes sent, otherwise return the error.
81933a2a37bSGleb Smirnoff 			 */
82033a2a37bSGleb Smirnoff 			if (error != 0) {
82133a2a37bSGleb Smirnoff 				SOCKBUF_UNLOCK(&so->so_snd);
82233a2a37bSGleb Smirnoff 				goto done;
82333a2a37bSGleb Smirnoff 			}
82433a2a37bSGleb Smirnoff 			goto retry_space;
82533a2a37bSGleb Smirnoff 		}
82633a2a37bSGleb Smirnoff 		SOCKBUF_UNLOCK(&so->so_snd);
82733a2a37bSGleb Smirnoff 
82833a2a37bSGleb Smirnoff 		/*
8299c64cfe5SGleb Smirnoff 		 * At the beginning of the first loop check if any headers
8309c64cfe5SGleb Smirnoff 		 * are specified and copy them into mbufs.  Reduce space in
8319c64cfe5SGleb Smirnoff 		 * the socket buffer by the size of the header mbuf chain.
8329c64cfe5SGleb Smirnoff 		 * Clear hdr_uio here and hdrlen at the end of the first loop.
83333a2a37bSGleb Smirnoff 		 */
8349c64cfe5SGleb Smirnoff 		if (hdr_uio != NULL && hdr_uio->uio_resid > 0) {
8359c64cfe5SGleb Smirnoff 			hdr_uio->uio_td = td;
8369c64cfe5SGleb Smirnoff 			hdr_uio->uio_rw = UIO_WRITE;
837b2e60773SJohn Baldwin #ifdef KERN_TLS
838b2e60773SJohn Baldwin 			if (tls != NULL)
839b2e60773SJohn Baldwin 				mh = m_uiotombuf(hdr_uio, M_WAITOK, space,
8406edfd179SGleb Smirnoff 				    tls->params.max_frame_len, M_EXTPG);
841b2e60773SJohn Baldwin 			else
842b2e60773SJohn Baldwin #endif
843b2e60773SJohn Baldwin 				mh = m_uiotombuf(hdr_uio, M_WAITOK,
844b2e60773SJohn Baldwin 				    space, 0, 0);
8459c64cfe5SGleb Smirnoff 			hdrlen = m_length(mh, &mhtail);
84633a2a37bSGleb Smirnoff 			space -= hdrlen;
847a2d8f9d2SGleb Smirnoff 			/*
848a2d8f9d2SGleb Smirnoff 			 * If header consumed all the socket buffer space,
849a2d8f9d2SGleb Smirnoff 			 * don't waste CPU cycles and jump to the end.
850a2d8f9d2SGleb Smirnoff 			 */
851a2d8f9d2SGleb Smirnoff 			if (space == 0) {
852a2d8f9d2SGleb Smirnoff 				sfio = NULL;
853a2d8f9d2SGleb Smirnoff 				nios = 0;
854a2d8f9d2SGleb Smirnoff 				goto prepend_header;
855a2d8f9d2SGleb Smirnoff 			}
8569c64cfe5SGleb Smirnoff 			hdr_uio = NULL;
8579c64cfe5SGleb Smirnoff 		}
85833a2a37bSGleb Smirnoff 
85933a2a37bSGleb Smirnoff 		if (vp != NULL) {
86033a2a37bSGleb Smirnoff 			error = vn_lock(vp, LK_SHARED);
86133a2a37bSGleb Smirnoff 			if (error != 0)
86233a2a37bSGleb Smirnoff 				goto done;
863faa998f6SMark Johnston 
864faa998f6SMark Johnston 			/*
865faa998f6SMark Johnston 			 * Check to see if the file size has changed.
866faa998f6SMark Johnston 			 */
867faa998f6SMark Johnston 			if (obj->type == OBJT_VNODE) {
868faa998f6SMark Johnston 				VM_OBJECT_RLOCK(obj);
869faa998f6SMark Johnston 				nobj_size = obj->un_pager.vnp.vnp_size;
870faa998f6SMark Johnston 				VM_OBJECT_RUNLOCK(obj);
871faa998f6SMark Johnston 			} else {
87233a2a37bSGleb Smirnoff 				error = VOP_GETATTR(vp, &va, td->td_ucred);
873faa998f6SMark Johnston 				if (error != 0) {
874b249ce48SMateusz Guzik 					VOP_UNLOCK(vp);
87533a2a37bSGleb Smirnoff 					goto done;
87633a2a37bSGleb Smirnoff 				}
877faa998f6SMark Johnston 				nobj_size = va.va_size;
878faa998f6SMark Johnston 			}
879faa998f6SMark Johnston 			if (off >= nobj_size) {
880faa998f6SMark Johnston 				VOP_UNLOCK(vp);
881faa998f6SMark Johnston 				goto done;
882faa998f6SMark Johnston 			}
883faa998f6SMark Johnston 			if (nobj_size != obj_size) {
884faa998f6SMark Johnston 				obj_size = nobj_size;
885faa998f6SMark Johnston 				rem = nbytes ? omin(nbytes + offset, obj_size) :
886faa998f6SMark Johnston 				    obj_size;
8879e3c8bd3SGleb Smirnoff 				rem -= off;
88833a2a37bSGleb Smirnoff 			}
88933a2a37bSGleb Smirnoff 		}
89033a2a37bSGleb Smirnoff 
89133a2a37bSGleb Smirnoff 		if (space > rem)
89233a2a37bSGleb Smirnoff 			space = rem;
893cec06a3eSJohn Baldwin 		else if (space > PAGE_SIZE) {
894cec06a3eSJohn Baldwin 			/*
895cec06a3eSJohn Baldwin 			 * Use page boundaries when possible for large
896cec06a3eSJohn Baldwin 			 * requests.
897cec06a3eSJohn Baldwin 			 */
898cec06a3eSJohn Baldwin 			if (off & PAGE_MASK)
899cec06a3eSJohn Baldwin 				space -= (PAGE_SIZE - (off & PAGE_MASK));
900cec06a3eSJohn Baldwin 			space = trunc_page(space);
901cec06a3eSJohn Baldwin 			if (off & PAGE_MASK)
902cec06a3eSJohn Baldwin 				space += (PAGE_SIZE - (off & PAGE_MASK));
903cec06a3eSJohn Baldwin 		}
90433a2a37bSGleb Smirnoff 
90533a2a37bSGleb Smirnoff 		npages = howmany(space + (off & PAGE_MASK), PAGE_SIZE);
90633a2a37bSGleb Smirnoff 
90733a2a37bSGleb Smirnoff 		/*
90833a2a37bSGleb Smirnoff 		 * Calculate maximum allowed number of pages for readahead
90900b5ffdeSGleb Smirnoff 		 * at this iteration.  If SF_USER_READAHEAD was set, we don't
91000b5ffdeSGleb Smirnoff 		 * do any heuristics and use exactly the value supplied by
91100b5ffdeSGleb Smirnoff 		 * application.  Otherwise, we allow readahead up to "rem".
91233a2a37bSGleb Smirnoff 		 * If application wants more, let it be, but there is no
913cd853791SKonstantin Belousov 		 * reason to go above maxphys.  Also check against "obj_size",
91433a2a37bSGleb Smirnoff 		 * since vm_pager_has_page() can hint beyond EOF.
91533a2a37bSGleb Smirnoff 		 */
91600b5ffdeSGleb Smirnoff 		if (flags & SF_USER_READAHEAD) {
91700b5ffdeSGleb Smirnoff 			rhpages = SF_READAHEAD(flags);
91800b5ffdeSGleb Smirnoff 		} else {
91900b5ffdeSGleb Smirnoff 			rhpages = howmany(rem + (off & PAGE_MASK), PAGE_SIZE) -
92000b5ffdeSGleb Smirnoff 			    npages;
92133a2a37bSGleb Smirnoff 			rhpages += SF_READAHEAD(flags);
92200b5ffdeSGleb Smirnoff 		}
923cd853791SKonstantin Belousov 		rhpages = min(howmany(maxphys, PAGE_SIZE), rhpages);
92433a2a37bSGleb Smirnoff 		rhpages = min(howmany(obj_size - trunc_page(off), PAGE_SIZE) -
92533a2a37bSGleb Smirnoff 		    npages, rhpages);
92633a2a37bSGleb Smirnoff 
92733a2a37bSGleb Smirnoff 		sfio = malloc(sizeof(struct sf_io) +
9288f0a223cSKonstantin Belousov 		    npages * sizeof(vm_page_t), M_SENDFILE, M_WAITOK);
92933a2a37bSGleb Smirnoff 		refcount_init(&sfio->nios, 1);
930d6e13f3bSJeff Roberson 		sfio->obj = obj;
93133a2a37bSGleb Smirnoff 		sfio->error = 0;
9326bc27f08SGleb Smirnoff 		sfio->m = NULL;
93326b23f07SMark Johnston 		sfio->npages = npages;
934b2e60773SJohn Baldwin #ifdef KERN_TLS
935b2e60773SJohn Baldwin 		/*
936b2e60773SJohn Baldwin 		 * This doesn't use ktls_hold() because sfio->m will
937b2e60773SJohn Baldwin 		 * also have a reference on 'tls' that will be valid
938b2e60773SJohn Baldwin 		 * for all of sfio's lifetime.
939b2e60773SJohn Baldwin 		 */
940b2e60773SJohn Baldwin 		sfio->tls = tls;
941b2e60773SJohn Baldwin #endif
9426bc27f08SGleb Smirnoff 		vm_object_pip_add(obj, 1);
94326b23f07SMark Johnston 		error = sendfile_swapin(obj, sfio, &nios, off, space, rhpages,
94426b23f07SMark Johnston 		    flags);
9450367bca4SAlan Somers 		if (error != 0) {
946fca79580SAlan Somers 			if (vp != NULL)
947b249ce48SMateusz Guzik 				VOP_UNLOCK(vp);
948b8c92303SGleb Smirnoff 			sendfile_iodone(sfio, NULL, 0, error);
949fca79580SAlan Somers 			goto done;
950fca79580SAlan Somers 		}
95133a2a37bSGleb Smirnoff 
95233a2a37bSGleb Smirnoff 		/*
95333a2a37bSGleb Smirnoff 		 * Loop and construct maximum sized mbuf chain to be bulk
95433a2a37bSGleb Smirnoff 		 * dumped into socket buffer.
95533a2a37bSGleb Smirnoff 		 */
95633a2a37bSGleb Smirnoff 		pa = sfio->pa;
957cec06a3eSJohn Baldwin 
958cec06a3eSJohn Baldwin 		/*
959cec06a3eSJohn Baldwin 		 * Use unmapped mbufs if enabled for TCP.  Unmapped
960cec06a3eSJohn Baldwin 		 * bufs are restricted to TCP as that is what has been
961cec06a3eSJohn Baldwin 		 * tested.  In particular, unmapped mbufs have not
962cec06a3eSJohn Baldwin 		 * been tested with UNIX-domain sockets.
963b2e60773SJohn Baldwin 		 *
964b2e60773SJohn Baldwin 		 * TLS frames always require unmapped mbufs.
965cec06a3eSJohn Baldwin 		 */
966b2e60773SJohn Baldwin 		if ((mb_use_ext_pgs &&
967b2e60773SJohn Baldwin 		    so->so_proto->pr_protocol == IPPROTO_TCP)
968b2e60773SJohn Baldwin #ifdef KERN_TLS
969b2e60773SJohn Baldwin 		    || tls != NULL
970b2e60773SJohn Baldwin #endif
971b2e60773SJohn Baldwin 		    ) {
972cec06a3eSJohn Baldwin 			use_ext_pgs = true;
973b2e60773SJohn Baldwin #ifdef KERN_TLS
974b2e60773SJohn Baldwin 			if (tls != NULL)
975b2e60773SJohn Baldwin 				max_pgs = num_pages(tls->params.max_frame_len);
976b2e60773SJohn Baldwin 			else
977b2e60773SJohn Baldwin #endif
978cec06a3eSJohn Baldwin 				max_pgs = MBUF_PEXT_MAX_PGS;
979cec06a3eSJohn Baldwin 
980cec06a3eSJohn Baldwin 			/* Start at last index, to wrap on first use. */
981cec06a3eSJohn Baldwin 			ext_pgs_idx = max_pgs - 1;
982cec06a3eSJohn Baldwin 		}
983cec06a3eSJohn Baldwin 
98433a2a37bSGleb Smirnoff 		for (int i = 0; i < npages; i++) {
98533a2a37bSGleb Smirnoff 			/*
98633a2a37bSGleb Smirnoff 			 * If a page wasn't grabbed successfully, then
98733a2a37bSGleb Smirnoff 			 * trim the array. Can happen only with SF_NODISKIO.
98833a2a37bSGleb Smirnoff 			 */
98933a2a37bSGleb Smirnoff 			if (pa[i] == NULL) {
99033a2a37bSGleb Smirnoff 				SFSTAT_INC(sf_busy);
99133a2a37bSGleb Smirnoff 				fixspace(npages, i, off, &space);
99226b23f07SMark Johnston 				sfio->npages = i;
99333a2a37bSGleb Smirnoff 				softerr = EBUSY;
99433a2a37bSGleb Smirnoff 				break;
99533a2a37bSGleb Smirnoff 			}
996f709eee6SKonstantin Belousov 			pga = pa[i];
997f709eee6SKonstantin Belousov 			if (pga == bogus_page)
998f709eee6SKonstantin Belousov 				pga = vm_page_relookup(obj, sfio->pindex0 + i);
99933a2a37bSGleb Smirnoff 
1000cec06a3eSJohn Baldwin 			if (use_ext_pgs) {
1001cec06a3eSJohn Baldwin 				off_t xfs;
1002cec06a3eSJohn Baldwin 
1003cec06a3eSJohn Baldwin 				ext_pgs_idx++;
1004cec06a3eSJohn Baldwin 				if (ext_pgs_idx == max_pgs) {
100523feb563SAndrew Gallatin 					m0 = mb_alloc_ext_pgs(M_WAITOK,
1006*314cb279SJohn Baldwin 					    sendfile_free_mext_pg, M_RDONLY);
1007cec06a3eSJohn Baldwin 
1008cec06a3eSJohn Baldwin 					if (flags & SF_NOCACHE) {
1009cec06a3eSJohn Baldwin 						m0->m_ext.ext_flags |=
1010cec06a3eSJohn Baldwin 						    EXT_FLAG_NOCACHE;
1011cec06a3eSJohn Baldwin 
1012cec06a3eSJohn Baldwin 						/*
1013cec06a3eSJohn Baldwin 						 * See comment below regarding
1014cec06a3eSJohn Baldwin 						 * ignoring SF_NOCACHE for the
1015cec06a3eSJohn Baldwin 						 * last page.
1016cec06a3eSJohn Baldwin 						 */
1017cec06a3eSJohn Baldwin 						if ((npages - i <= max_pgs) &&
1018cec06a3eSJohn Baldwin 						    ((off + space) & PAGE_MASK) &&
1019cec06a3eSJohn Baldwin 						    (rem > space || rhpages > 0))
1020cec06a3eSJohn Baldwin 							m0->m_ext.ext_flags |=
1021cec06a3eSJohn Baldwin 							    EXT_FLAG_CACHE_LAST;
1022cec06a3eSJohn Baldwin 					}
1023cec06a3eSJohn Baldwin 					if (sfs != NULL) {
1024cec06a3eSJohn Baldwin 						m0->m_ext.ext_flags |=
1025cec06a3eSJohn Baldwin 						    EXT_FLAG_SYNC;
102661664ee7SGleb Smirnoff 						m0->m_ext.ext_arg1 = sfs;
1027cec06a3eSJohn Baldwin 						mtx_lock(&sfs->mtx);
1028cec06a3eSJohn Baldwin 						sfs->count++;
1029cec06a3eSJohn Baldwin 						mtx_unlock(&sfs->mtx);
1030cec06a3eSJohn Baldwin 					}
1031cec06a3eSJohn Baldwin 					ext_pgs_idx = 0;
1032cec06a3eSJohn Baldwin 
1033cec06a3eSJohn Baldwin 					/* Append to mbuf chain. */
1034cec06a3eSJohn Baldwin 					if (mtail != NULL)
1035cec06a3eSJohn Baldwin 						mtail->m_next = m0;
1036cec06a3eSJohn Baldwin 					else
1037cec06a3eSJohn Baldwin 						m = m0;
1038cec06a3eSJohn Baldwin 					mtail = m0;
10397b6c99d0SGleb Smirnoff 					m0->m_epg_1st_off =
1040cec06a3eSJohn Baldwin 					    vmoff(i, off) & PAGE_MASK;
1041cec06a3eSJohn Baldwin 				}
1042cec06a3eSJohn Baldwin 				if (nios) {
1043cec06a3eSJohn Baldwin 					mtail->m_flags |= M_NOTREADY;
10447b6c99d0SGleb Smirnoff 					m0->m_epg_nrdy++;
1045cec06a3eSJohn Baldwin 				}
1046cec06a3eSJohn Baldwin 
10470c103266SGleb Smirnoff 				m0->m_epg_pa[ext_pgs_idx] = VM_PAGE_TO_PHYS(pga);
10487b6c99d0SGleb Smirnoff 				m0->m_epg_npgs++;
1049cec06a3eSJohn Baldwin 				xfs = xfsize(i, npages, off, space);
10507b6c99d0SGleb Smirnoff 				m0->m_epg_last_len = xfs;
10510c103266SGleb Smirnoff 				MBUF_EXT_PGS_ASSERT_SANITY(m0);
1052cec06a3eSJohn Baldwin 				mtail->m_len += xfs;
1053cec06a3eSJohn Baldwin 				mtail->m_ext.ext_size += PAGE_SIZE;
1054cec06a3eSJohn Baldwin 				continue;
1055cec06a3eSJohn Baldwin 			}
1056cec06a3eSJohn Baldwin 
105733a2a37bSGleb Smirnoff 			/*
105833a2a37bSGleb Smirnoff 			 * Get a sendfile buf.  When allocating the
105933a2a37bSGleb Smirnoff 			 * first buffer for mbuf chain, we usually
106033a2a37bSGleb Smirnoff 			 * wait as long as necessary, but this wait
106133a2a37bSGleb Smirnoff 			 * can be interrupted.  For consequent
106233a2a37bSGleb Smirnoff 			 * buffers, do not sleep, since several
106333a2a37bSGleb Smirnoff 			 * threads might exhaust the buffers and then
106433a2a37bSGleb Smirnoff 			 * deadlock.
106533a2a37bSGleb Smirnoff 			 */
1066f709eee6SKonstantin Belousov 			sf = sf_buf_alloc(pga,
106733a2a37bSGleb Smirnoff 			    m != NULL ? SFB_NOWAIT : SFB_CATCH);
106833a2a37bSGleb Smirnoff 			if (sf == NULL) {
106933a2a37bSGleb Smirnoff 				SFSTAT_INC(sf_allocfail);
107059e1ac9dSKonstantin Belousov 				sendfile_iowait(sfio, "sfnosf");
107126b23f07SMark Johnston 				for (int j = i; j < npages; j++) {
107233a2a37bSGleb Smirnoff 					vm_page_unwire(pa[j], PQ_INACTIVE);
107326b23f07SMark Johnston 					pa[j] = NULL;
107426b23f07SMark Johnston 				}
107533a2a37bSGleb Smirnoff 				if (m == NULL)
107633a2a37bSGleb Smirnoff 					softerr = ENOBUFS;
107733a2a37bSGleb Smirnoff 				fixspace(npages, i, off, &space);
107826b23f07SMark Johnston 				sfio->npages = i;
107933a2a37bSGleb Smirnoff 				break;
108033a2a37bSGleb Smirnoff 			}
108133a2a37bSGleb Smirnoff 
108233a2a37bSGleb Smirnoff 			m0 = m_get(M_WAITOK, MT_DATA);
108333a2a37bSGleb Smirnoff 			m0->m_ext.ext_buf = (char *)sf_buf_kva(sf);
108433a2a37bSGleb Smirnoff 			m0->m_ext.ext_size = PAGE_SIZE;
108533a2a37bSGleb Smirnoff 			m0->m_ext.ext_arg1 = sf;
10869c82bec4SGleb Smirnoff 			m0->m_ext.ext_type = EXT_SFBUF;
10879c82bec4SGleb Smirnoff 			m0->m_ext.ext_flags = EXT_FLAG_EMBREF;
10889c82bec4SGleb Smirnoff 			m0->m_ext.ext_free = sendfile_free_mext;
108933a2a37bSGleb Smirnoff 			/*
109033a2a37bSGleb Smirnoff 			 * SF_NOCACHE sets the page as being freed upon send.
109133a2a37bSGleb Smirnoff 			 * However, we ignore it for the last page in 'space',
109233a2a37bSGleb Smirnoff 			 * if the page is truncated, and we got more data to
109333a2a37bSGleb Smirnoff 			 * send (rem > space), or if we have readahead
109433a2a37bSGleb Smirnoff 			 * configured (rhpages > 0).
109533a2a37bSGleb Smirnoff 			 */
10969c82bec4SGleb Smirnoff 			if ((flags & SF_NOCACHE) &&
10979c82bec4SGleb Smirnoff 			    (i != npages - 1 ||
10989c82bec4SGleb Smirnoff 			    !((off + space) & PAGE_MASK) ||
10999c82bec4SGleb Smirnoff 			    !(rem > space || rhpages > 0)))
11009c82bec4SGleb Smirnoff 				m0->m_ext.ext_flags |= EXT_FLAG_NOCACHE;
11019c82bec4SGleb Smirnoff 			if (sfs != NULL) {
11029c82bec4SGleb Smirnoff 				m0->m_ext.ext_flags |= EXT_FLAG_SYNC;
11039c82bec4SGleb Smirnoff 				m0->m_ext.ext_arg2 = sfs;
11049c82bec4SGleb Smirnoff 				mtx_lock(&sfs->mtx);
11059c82bec4SGleb Smirnoff 				sfs->count++;
11069c82bec4SGleb Smirnoff 				mtx_unlock(&sfs->mtx);
11079c82bec4SGleb Smirnoff 			}
110856a5f52eSGleb Smirnoff 			m0->m_ext.ext_count = 1;
110933a2a37bSGleb Smirnoff 			m0->m_flags |= (M_EXT | M_RDONLY);
111033a2a37bSGleb Smirnoff 			if (nios)
111133a2a37bSGleb Smirnoff 				m0->m_flags |= M_NOTREADY;
111233a2a37bSGleb Smirnoff 			m0->m_data = (char *)sf_buf_kva(sf) +
111333a2a37bSGleb Smirnoff 			    (vmoff(i, off) & PAGE_MASK);
111433a2a37bSGleb Smirnoff 			m0->m_len = xfsize(i, npages, off, space);
111533a2a37bSGleb Smirnoff 
111633a2a37bSGleb Smirnoff 			/* Append to mbuf chain. */
111733a2a37bSGleb Smirnoff 			if (mtail != NULL)
111833a2a37bSGleb Smirnoff 				mtail->m_next = m0;
111933a2a37bSGleb Smirnoff 			else
112033a2a37bSGleb Smirnoff 				m = m0;
112133a2a37bSGleb Smirnoff 			mtail = m0;
112233a2a37bSGleb Smirnoff 		}
112333a2a37bSGleb Smirnoff 
112433a2a37bSGleb Smirnoff 		if (vp != NULL)
1125b249ce48SMateusz Guzik 			VOP_UNLOCK(vp);
112633a2a37bSGleb Smirnoff 
112733a2a37bSGleb Smirnoff 		/* Keep track of bytes processed. */
112833a2a37bSGleb Smirnoff 		off += space;
112933a2a37bSGleb Smirnoff 		rem -= space;
113033a2a37bSGleb Smirnoff 
11316bc27f08SGleb Smirnoff 		/*
11326bc27f08SGleb Smirnoff 		 * Prepend header, if any.  Save pointer to first mbuf
11336bc27f08SGleb Smirnoff 		 * with a page.
11346bc27f08SGleb Smirnoff 		 */
113533a2a37bSGleb Smirnoff 		if (hdrlen) {
1136a2d8f9d2SGleb Smirnoff prepend_header:
11376bc27f08SGleb Smirnoff 			m0 = mhtail->m_next = m;
113833a2a37bSGleb Smirnoff 			m = mh;
113933a2a37bSGleb Smirnoff 			mh = NULL;
11406bc27f08SGleb Smirnoff 		} else
11416bc27f08SGleb Smirnoff 			m0 = m;
114233a2a37bSGleb Smirnoff 
114333a2a37bSGleb Smirnoff 		if (m == NULL) {
114433a2a37bSGleb Smirnoff 			KASSERT(softerr, ("%s: m NULL, no error", __func__));
114533a2a37bSGleb Smirnoff 			error = softerr;
11466bc27f08SGleb Smirnoff 			sendfile_iodone(sfio, NULL, 0, 0);
114733a2a37bSGleb Smirnoff 			goto done;
114833a2a37bSGleb Smirnoff 		}
114933a2a37bSGleb Smirnoff 
115033a2a37bSGleb Smirnoff 		/* Add the buffer chain to the socket buffer. */
115133a2a37bSGleb Smirnoff 		KASSERT(m_length(m, NULL) == space + hdrlen,
115233a2a37bSGleb Smirnoff 		    ("%s: mlen %u space %d hdrlen %d",
115333a2a37bSGleb Smirnoff 		    __func__, m_length(m, NULL), space, hdrlen));
115433a2a37bSGleb Smirnoff 
115533a2a37bSGleb Smirnoff 		CURVNET_SET(so->so_vnet);
1156b2e60773SJohn Baldwin #ifdef KERN_TLS
1157f85e1a80SGleb Smirnoff 		if (tls != NULL)
1158f85e1a80SGleb Smirnoff 			ktls_frame(m, tls, &tls_enq_cnt, TLS_RLTYPE_APP);
1159b2e60773SJohn Baldwin #endif
116033a2a37bSGleb Smirnoff 		if (nios == 0) {
116133a2a37bSGleb Smirnoff 			/*
116233a2a37bSGleb Smirnoff 			 * If sendfile_swapin() didn't initiate any I/Os,
11636bc27f08SGleb Smirnoff 			 * which happens if all data is cached in VM, or if
11646bc27f08SGleb Smirnoff 			 * the header consumed all socket buffer space and
11656bc27f08SGleb Smirnoff 			 * sfio is NULL, then we can send data right now
11666bc27f08SGleb Smirnoff 			 * without the PRUS_NOTREADY flag.
116733a2a37bSGleb Smirnoff 			 */
11686bc27f08SGleb Smirnoff 			if (sfio != NULL)
11696bc27f08SGleb Smirnoff 				sendfile_iodone(sfio, NULL, 0, 0);
1170b2e60773SJohn Baldwin #ifdef KERN_TLS
11719e14430dSJohn Baldwin 			if (tls != NULL && tls->mode == TCP_TLS_MODE_SW) {
1172e7d02be1SGleb Smirnoff 				error = so->so_proto->pr_send(so,
1173e7d02be1SGleb Smirnoff 				    PRUS_NOTREADY, m, NULL, NULL, td);
1174916c61a5SMark Johnston 				if (error != 0) {
1175916c61a5SMark Johnston 					m_freem(m);
1176916c61a5SMark Johnston 				} else {
1177b2e60773SJohn Baldwin 					soref(so);
1178b2e60773SJohn Baldwin 					ktls_enqueue(m, so, tls_enq_cnt);
1179916c61a5SMark Johnston 				}
1180b2e60773SJohn Baldwin 			} else
1181b2e60773SJohn Baldwin #endif
1182e7d02be1SGleb Smirnoff 				error = so->so_proto->pr_send(so, 0, m, NULL,
1183e7d02be1SGleb Smirnoff 				    NULL, td);
118433a2a37bSGleb Smirnoff 		} else {
11856bc27f08SGleb Smirnoff 			sfio->so = so;
11866bc27f08SGleb Smirnoff 			sfio->m = m0;
1187d37aa3ccSGleb Smirnoff 			soref(so);
1188e7d02be1SGleb Smirnoff 			error = so->so_proto->pr_send(so, PRUS_NOTREADY, m,
1189e7d02be1SGleb Smirnoff 			    NULL, NULL, td);
1190916c61a5SMark Johnston 			sendfile_iodone(sfio, NULL, 0, error);
119133a2a37bSGleb Smirnoff 		}
119273ee5756SRandall Stewart #ifdef TCP_REQUEST_TRK
119373ee5756SRandall Stewart 		if (so->so_proto->pr_protocol == IPPROTO_TCP) {
119473ee5756SRandall Stewart 			/* log the sendfile call to the TCP log, if enabled */
119573ee5756SRandall Stewart 			tcp_log_sendfile(so, offset, nbytes, flags);
119673ee5756SRandall Stewart 		}
119773ee5756SRandall Stewart #endif
119833a2a37bSGleb Smirnoff 		CURVNET_RESTORE();
119933a2a37bSGleb Smirnoff 
1200916c61a5SMark Johnston 		m = NULL;
120133a2a37bSGleb Smirnoff 		if (error)
120233a2a37bSGleb Smirnoff 			goto done;
120333a2a37bSGleb Smirnoff 		sbytes += space + hdrlen;
120433a2a37bSGleb Smirnoff 		if (hdrlen)
120533a2a37bSGleb Smirnoff 			hdrlen = 0;
120633a2a37bSGleb Smirnoff 		if (softerr) {
120733a2a37bSGleb Smirnoff 			error = softerr;
120833a2a37bSGleb Smirnoff 			goto done;
120933a2a37bSGleb Smirnoff 		}
121033a2a37bSGleb Smirnoff 	}
121133a2a37bSGleb Smirnoff 
121233a2a37bSGleb Smirnoff 	/*
121333a2a37bSGleb Smirnoff 	 * Send trailers. Wimp out and use writev(2).
121433a2a37bSGleb Smirnoff 	 */
121533a2a37bSGleb Smirnoff 	if (trl_uio != NULL) {
1216f94acf52SMark Johnston 		SOCK_IO_SEND_UNLOCK(so);
121733a2a37bSGleb Smirnoff 		error = kern_writev(td, sockfd, trl_uio);
121833a2a37bSGleb Smirnoff 		if (error == 0)
121933a2a37bSGleb Smirnoff 			sbytes += td->td_retval[0];
122033a2a37bSGleb Smirnoff 		goto out;
122133a2a37bSGleb Smirnoff 	}
122233a2a37bSGleb Smirnoff 
122333a2a37bSGleb Smirnoff done:
1224f94acf52SMark Johnston 	SOCK_IO_SEND_UNLOCK(so);
122533a2a37bSGleb Smirnoff out:
122633a2a37bSGleb Smirnoff 	/*
122733a2a37bSGleb Smirnoff 	 * If there was no error we have to clear td->td_retval[0]
122833a2a37bSGleb Smirnoff 	 * because it may have been set by writev.
122933a2a37bSGleb Smirnoff 	 */
123033a2a37bSGleb Smirnoff 	if (error == 0) {
123133a2a37bSGleb Smirnoff 		td->td_retval[0] = 0;
123233a2a37bSGleb Smirnoff 	}
123333a2a37bSGleb Smirnoff 	if (sent != NULL) {
123433a2a37bSGleb Smirnoff 		(*sent) = sbytes;
123533a2a37bSGleb Smirnoff 	}
123633a2a37bSGleb Smirnoff 	if (obj != NULL)
123733a2a37bSGleb Smirnoff 		vm_object_deallocate(obj);
123833a2a37bSGleb Smirnoff 	if (so)
123933a2a37bSGleb Smirnoff 		fdrop(sock_fp, td);
124033a2a37bSGleb Smirnoff 	if (m)
124133a2a37bSGleb Smirnoff 		m_freem(m);
124233a2a37bSGleb Smirnoff 	if (mh)
124333a2a37bSGleb Smirnoff 		m_freem(mh);
124433a2a37bSGleb Smirnoff 
124533a2a37bSGleb Smirnoff 	if (sfs != NULL) {
124633a2a37bSGleb Smirnoff 		mtx_lock(&sfs->mtx);
124733a2a37bSGleb Smirnoff 		if (sfs->count != 0)
1248b7eae758SMark Johnston 			error = cv_wait_sig(&sfs->cv, &sfs->mtx);
1249b7eae758SMark Johnston 		if (sfs->count == 0) {
1250b7eae758SMark Johnston 			sendfile_sync_destroy(sfs);
1251b7eae758SMark Johnston 		} else {
1252b7eae758SMark Johnston 			sfs->waiting = false;
1253b7eae758SMark Johnston 			mtx_unlock(&sfs->mtx);
1254b7eae758SMark Johnston 		}
125533a2a37bSGleb Smirnoff 	}
1256b2e60773SJohn Baldwin #ifdef KERN_TLS
1257b2e60773SJohn Baldwin 	if (tls != NULL)
1258b2e60773SJohn Baldwin 		ktls_free(tls);
1259b2e60773SJohn Baldwin #endif
126033a2a37bSGleb Smirnoff 
126133a2a37bSGleb Smirnoff 	if (error == ERESTART)
126233a2a37bSGleb Smirnoff 		error = EINTR;
126333a2a37bSGleb Smirnoff 
126433a2a37bSGleb Smirnoff 	return (error);
126533a2a37bSGleb Smirnoff }
126633a2a37bSGleb Smirnoff 
126733a2a37bSGleb Smirnoff static int
sendfile(struct thread * td,struct sendfile_args * uap,int compat)126833a2a37bSGleb Smirnoff sendfile(struct thread *td, struct sendfile_args *uap, int compat)
126933a2a37bSGleb Smirnoff {
127033a2a37bSGleb Smirnoff 	struct sf_hdtr hdtr;
127133a2a37bSGleb Smirnoff 	struct uio *hdr_uio, *trl_uio;
127233a2a37bSGleb Smirnoff 	struct file *fp;
127333a2a37bSGleb Smirnoff 	off_t sbytes;
127433a2a37bSGleb Smirnoff 	int error;
127533a2a37bSGleb Smirnoff 
127633a2a37bSGleb Smirnoff 	/*
127733a2a37bSGleb Smirnoff 	 * File offset must be positive.  If it goes beyond EOF
127833a2a37bSGleb Smirnoff 	 * we send only the header/trailer and no payload data.
127933a2a37bSGleb Smirnoff 	 */
128033a2a37bSGleb Smirnoff 	if (uap->offset < 0)
128133a2a37bSGleb Smirnoff 		return (EINVAL);
128233a2a37bSGleb Smirnoff 
1283ef3266d5SGleb Smirnoff 	sbytes = 0;
128433a2a37bSGleb Smirnoff 	hdr_uio = trl_uio = NULL;
128533a2a37bSGleb Smirnoff 
128633a2a37bSGleb Smirnoff 	if (uap->hdtr != NULL) {
128733a2a37bSGleb Smirnoff 		error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
128833a2a37bSGleb Smirnoff 		if (error != 0)
128933a2a37bSGleb Smirnoff 			goto out;
129033a2a37bSGleb Smirnoff 		if (hdtr.headers != NULL) {
129133a2a37bSGleb Smirnoff 			error = copyinuio(hdtr.headers, hdtr.hdr_cnt,
129233a2a37bSGleb Smirnoff 			    &hdr_uio);
129333a2a37bSGleb Smirnoff 			if (error != 0)
129433a2a37bSGleb Smirnoff 				goto out;
12959c64cfe5SGleb Smirnoff #ifdef COMPAT_FREEBSD4
12969c64cfe5SGleb Smirnoff 			/*
12979c64cfe5SGleb Smirnoff 			 * In FreeBSD < 5.0 the nbytes to send also included
12989c64cfe5SGleb Smirnoff 			 * the header.  If compat is specified subtract the
12999c64cfe5SGleb Smirnoff 			 * header size from nbytes.
13009c64cfe5SGleb Smirnoff 			 */
13019c64cfe5SGleb Smirnoff 			if (compat) {
13029c64cfe5SGleb Smirnoff 				if (uap->nbytes > hdr_uio->uio_resid)
13039c64cfe5SGleb Smirnoff 					uap->nbytes -= hdr_uio->uio_resid;
13049c64cfe5SGleb Smirnoff 				else
13059c64cfe5SGleb Smirnoff 					uap->nbytes = 0;
13069c64cfe5SGleb Smirnoff 			}
13079c64cfe5SGleb Smirnoff #endif
130833a2a37bSGleb Smirnoff 		}
130933a2a37bSGleb Smirnoff 		if (hdtr.trailers != NULL) {
131033a2a37bSGleb Smirnoff 			error = copyinuio(hdtr.trailers, hdtr.trl_cnt,
131133a2a37bSGleb Smirnoff 			    &trl_uio);
131233a2a37bSGleb Smirnoff 			if (error != 0)
131333a2a37bSGleb Smirnoff 				goto out;
131433a2a37bSGleb Smirnoff 		}
131533a2a37bSGleb Smirnoff 	}
131633a2a37bSGleb Smirnoff 
131733a2a37bSGleb Smirnoff 	AUDIT_ARG_FD(uap->fd);
131833a2a37bSGleb Smirnoff 
131933a2a37bSGleb Smirnoff 	/*
132033a2a37bSGleb Smirnoff 	 * sendfile(2) can start at any offset within a file so we require
132133a2a37bSGleb Smirnoff 	 * CAP_READ+CAP_SEEK = CAP_PREAD.
132233a2a37bSGleb Smirnoff 	 */
1323cbd92ce6SMatt Macy 	if ((error = fget_read(td, uap->fd, &cap_pread_rights, &fp)) != 0)
132433a2a37bSGleb Smirnoff 		goto out;
132533a2a37bSGleb Smirnoff 
132633a2a37bSGleb Smirnoff 	error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset,
13279c64cfe5SGleb Smirnoff 	    uap->nbytes, &sbytes, uap->flags, td);
132833a2a37bSGleb Smirnoff 	fdrop(fp, td);
132933a2a37bSGleb Smirnoff 
133033a2a37bSGleb Smirnoff 	if (uap->sbytes != NULL)
1331d0adc2f2SMark Johnston 		(void)copyout(&sbytes, uap->sbytes, sizeof(off_t));
133233a2a37bSGleb Smirnoff 
133333a2a37bSGleb Smirnoff out:
133461cc4830SAlfredo Mazzinghi 	freeuio(hdr_uio);
133561cc4830SAlfredo Mazzinghi 	freeuio(trl_uio);
133633a2a37bSGleb Smirnoff 	return (error);
133733a2a37bSGleb Smirnoff }
133833a2a37bSGleb Smirnoff 
133933a2a37bSGleb Smirnoff /*
134033a2a37bSGleb Smirnoff  * sendfile(2)
134133a2a37bSGleb Smirnoff  *
134233a2a37bSGleb Smirnoff  * int sendfile(int fd, int s, off_t offset, size_t nbytes,
134333a2a37bSGleb Smirnoff  *       struct sf_hdtr *hdtr, off_t *sbytes, int flags)
134433a2a37bSGleb Smirnoff  *
134533a2a37bSGleb Smirnoff  * Send a file specified by 'fd' and starting at 'offset' to a socket
134633a2a37bSGleb Smirnoff  * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
134733a2a37bSGleb Smirnoff  * 0.  Optionally add a header and/or trailer to the socket output.  If
134833a2a37bSGleb Smirnoff  * specified, write the total number of bytes sent into *sbytes.
134933a2a37bSGleb Smirnoff  */
135033a2a37bSGleb Smirnoff int
sys_sendfile(struct thread * td,struct sendfile_args * uap)135133a2a37bSGleb Smirnoff sys_sendfile(struct thread *td, struct sendfile_args *uap)
135233a2a37bSGleb Smirnoff {
135333a2a37bSGleb Smirnoff 
135433a2a37bSGleb Smirnoff 	return (sendfile(td, uap, 0));
135533a2a37bSGleb Smirnoff }
135633a2a37bSGleb Smirnoff 
135733a2a37bSGleb Smirnoff #ifdef COMPAT_FREEBSD4
135833a2a37bSGleb Smirnoff int
freebsd4_sendfile(struct thread * td,struct freebsd4_sendfile_args * uap)135933a2a37bSGleb Smirnoff freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
136033a2a37bSGleb Smirnoff {
136133a2a37bSGleb Smirnoff 	struct sendfile_args args;
136233a2a37bSGleb Smirnoff 
136333a2a37bSGleb Smirnoff 	args.fd = uap->fd;
136433a2a37bSGleb Smirnoff 	args.s = uap->s;
136533a2a37bSGleb Smirnoff 	args.offset = uap->offset;
136633a2a37bSGleb Smirnoff 	args.nbytes = uap->nbytes;
136733a2a37bSGleb Smirnoff 	args.hdtr = uap->hdtr;
136833a2a37bSGleb Smirnoff 	args.sbytes = uap->sbytes;
136933a2a37bSGleb Smirnoff 	args.flags = uap->flags;
137033a2a37bSGleb Smirnoff 
137133a2a37bSGleb Smirnoff 	return (sendfile(td, &args, 1));
137233a2a37bSGleb Smirnoff }
137333a2a37bSGleb Smirnoff #endif /* COMPAT_FREEBSD4 */
1374