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 "opt_compat.h" 3433a2a37bSGleb Smirnoff 3533a2a37bSGleb Smirnoff #include <sys/param.h> 3633a2a37bSGleb Smirnoff #include <sys/systm.h> 3733a2a37bSGleb Smirnoff #include <sys/capsicum.h> 3833a2a37bSGleb Smirnoff #include <sys/kernel.h> 3933a2a37bSGleb Smirnoff #include <sys/lock.h> 4033a2a37bSGleb Smirnoff #include <sys/mutex.h> 4133a2a37bSGleb Smirnoff #include <sys/sysproto.h> 4233a2a37bSGleb Smirnoff #include <sys/malloc.h> 4333a2a37bSGleb Smirnoff #include <sys/proc.h> 4433a2a37bSGleb Smirnoff #include <sys/mman.h> 4533a2a37bSGleb Smirnoff #include <sys/mount.h> 4633a2a37bSGleb Smirnoff #include <sys/mbuf.h> 4733a2a37bSGleb Smirnoff #include <sys/protosw.h> 4833a2a37bSGleb Smirnoff #include <sys/rwlock.h> 4933a2a37bSGleb Smirnoff #include <sys/sf_buf.h> 5033a2a37bSGleb Smirnoff #include <sys/socket.h> 5133a2a37bSGleb Smirnoff #include <sys/socketvar.h> 5233a2a37bSGleb Smirnoff #include <sys/syscallsubr.h> 5333a2a37bSGleb Smirnoff #include <sys/sysctl.h> 5433a2a37bSGleb Smirnoff #include <sys/vnode.h> 5533a2a37bSGleb Smirnoff 5633a2a37bSGleb Smirnoff #include <net/vnet.h> 5733a2a37bSGleb Smirnoff 5833a2a37bSGleb Smirnoff #include <security/audit/audit.h> 5933a2a37bSGleb Smirnoff #include <security/mac/mac_framework.h> 6033a2a37bSGleb Smirnoff 6133a2a37bSGleb Smirnoff #include <vm/vm.h> 6233a2a37bSGleb Smirnoff #include <vm/vm_object.h> 6333a2a37bSGleb Smirnoff #include <vm/vm_pager.h> 6433a2a37bSGleb Smirnoff 65*9c82bec4SGleb Smirnoff #define EXT_FLAG_SYNC EXT_FLAG_VENDOR1 66*9c82bec4SGleb Smirnoff #define EXT_FLAG_NOCACHE EXT_FLAG_VENDOR2 67*9c82bec4SGleb 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 12433a2a37bSGleb Smirnoff /* 12533a2a37bSGleb Smirnoff * Detach mapped page and release resources back to the system. Called 12633a2a37bSGleb Smirnoff * by mbuf(9) code when last reference to a page is freed. 12733a2a37bSGleb Smirnoff */ 128*9c82bec4SGleb Smirnoff static void 129*9c82bec4SGleb Smirnoff sendfile_free_page(vm_page_t pg, bool nocache) 13033a2a37bSGleb Smirnoff { 13133a2a37bSGleb Smirnoff 13233a2a37bSGleb Smirnoff vm_page_lock(pg); 13333a2a37bSGleb Smirnoff /* 134*9c82bec4SGleb Smirnoff * In either case check for the object going away on us. This can 135*9c82bec4SGleb Smirnoff * happen since we don't hold a reference to it. If so, we're 136*9c82bec4SGleb Smirnoff * responsible for freeing the page. In 'noncache' case try to free 137*9c82bec4SGleb Smirnoff * the page, but only if it is cheap to. 13833a2a37bSGleb Smirnoff */ 139*9c82bec4SGleb Smirnoff if (vm_page_unwire(pg, nocache ? PQ_NONE : PQ_INACTIVE)) { 14033a2a37bSGleb Smirnoff vm_object_t obj; 14133a2a37bSGleb Smirnoff 14233a2a37bSGleb Smirnoff if ((obj = pg->object) == NULL) 14333a2a37bSGleb Smirnoff vm_page_free(pg); 144*9c82bec4SGleb Smirnoff else if (nocache) { 145*9c82bec4SGleb Smirnoff if (!vm_page_xbusied(pg) && VM_OBJECT_TRYWLOCK(obj)) { 14633a2a37bSGleb Smirnoff vm_page_free(pg); 14733a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 14833a2a37bSGleb Smirnoff } else 14933a2a37bSGleb Smirnoff vm_page_deactivate(pg); 15033a2a37bSGleb Smirnoff } 151*9c82bec4SGleb Smirnoff } 15233a2a37bSGleb Smirnoff vm_page_unlock(pg); 153*9c82bec4SGleb Smirnoff } 15433a2a37bSGleb Smirnoff 155*9c82bec4SGleb Smirnoff static void 156*9c82bec4SGleb Smirnoff sendfile_free_mext(struct mbuf *m) 157*9c82bec4SGleb Smirnoff { 158*9c82bec4SGleb Smirnoff struct sf_buf *sf; 159*9c82bec4SGleb Smirnoff vm_page_t pg; 160*9c82bec4SGleb Smirnoff bool nocache; 161*9c82bec4SGleb Smirnoff 162*9c82bec4SGleb Smirnoff KASSERT(m->m_flags & M_EXT && m->m_ext.ext_type == EXT_SFBUF, 163*9c82bec4SGleb Smirnoff ("%s: m %p !M_EXT or !EXT_SFBUF", __func__, m)); 164*9c82bec4SGleb Smirnoff 165*9c82bec4SGleb Smirnoff sf = m->m_ext.ext_arg1; 166*9c82bec4SGleb Smirnoff pg = sf_buf_page(sf); 167*9c82bec4SGleb Smirnoff nocache = m->m_ext.ext_flags & EXT_FLAG_NOCACHE; 168*9c82bec4SGleb Smirnoff 169*9c82bec4SGleb Smirnoff sf_buf_free(sf); 170*9c82bec4SGleb Smirnoff sendfile_free_page(pg, nocache); 171*9c82bec4SGleb Smirnoff 172*9c82bec4SGleb Smirnoff if (m->m_ext.ext_flags & EXT_FLAG_SYNC) { 173*9c82bec4SGleb Smirnoff struct sendfile_sync *sfs = m->m_ext.ext_arg2; 174*9c82bec4SGleb Smirnoff 17533a2a37bSGleb Smirnoff mtx_lock(&sfs->mtx); 17633a2a37bSGleb Smirnoff KASSERT(sfs->count > 0, ("Sendfile sync botchup count == 0")); 17733a2a37bSGleb Smirnoff if (--sfs->count == 0) 17833a2a37bSGleb Smirnoff cv_signal(&sfs->cv); 17933a2a37bSGleb Smirnoff mtx_unlock(&sfs->mtx); 18033a2a37bSGleb Smirnoff } 18133a2a37bSGleb Smirnoff } 18233a2a37bSGleb Smirnoff 18333a2a37bSGleb Smirnoff /* 18433a2a37bSGleb Smirnoff * Helper function to calculate how much data to put into page i of n. 18533a2a37bSGleb Smirnoff * Only first and last pages are special. 18633a2a37bSGleb Smirnoff */ 18733a2a37bSGleb Smirnoff static inline off_t 18833a2a37bSGleb Smirnoff xfsize(int i, int n, off_t off, off_t len) 18933a2a37bSGleb Smirnoff { 19033a2a37bSGleb Smirnoff 19133a2a37bSGleb Smirnoff if (i == 0) 19233a2a37bSGleb Smirnoff return (omin(PAGE_SIZE - (off & PAGE_MASK), len)); 19333a2a37bSGleb Smirnoff 19433a2a37bSGleb Smirnoff if (i == n - 1 && ((off + len) & PAGE_MASK) > 0) 19533a2a37bSGleb Smirnoff return ((off + len) & PAGE_MASK); 19633a2a37bSGleb Smirnoff 19733a2a37bSGleb Smirnoff return (PAGE_SIZE); 19833a2a37bSGleb Smirnoff } 19933a2a37bSGleb Smirnoff 20033a2a37bSGleb Smirnoff /* 20133a2a37bSGleb Smirnoff * Helper function to get offset within object for i page. 20233a2a37bSGleb Smirnoff */ 203d712b799SAlan Cox static inline vm_ooffset_t 20433a2a37bSGleb Smirnoff vmoff(int i, off_t off) 20533a2a37bSGleb Smirnoff { 20633a2a37bSGleb Smirnoff 20733a2a37bSGleb Smirnoff if (i == 0) 208d712b799SAlan Cox return ((vm_ooffset_t)off); 20933a2a37bSGleb Smirnoff 21033a2a37bSGleb Smirnoff return (trunc_page(off + i * PAGE_SIZE)); 21133a2a37bSGleb Smirnoff } 21233a2a37bSGleb Smirnoff 21333a2a37bSGleb Smirnoff /* 21433a2a37bSGleb Smirnoff * Helper function used when allocation of a page or sf_buf failed. 21533a2a37bSGleb Smirnoff * Pretend as if we don't have enough space, subtract xfsize() of 21633a2a37bSGleb Smirnoff * all pages that failed. 21733a2a37bSGleb Smirnoff */ 21833a2a37bSGleb Smirnoff static inline void 21933a2a37bSGleb Smirnoff fixspace(int old, int new, off_t off, int *space) 22033a2a37bSGleb Smirnoff { 22133a2a37bSGleb Smirnoff 22233a2a37bSGleb Smirnoff KASSERT(old > new, ("%s: old %d new %d", __func__, old, new)); 22333a2a37bSGleb Smirnoff 22433a2a37bSGleb Smirnoff /* Subtract last one. */ 22533a2a37bSGleb Smirnoff *space -= xfsize(old - 1, old, off, *space); 22633a2a37bSGleb Smirnoff old--; 22733a2a37bSGleb Smirnoff 22833a2a37bSGleb Smirnoff if (new == old) 22933a2a37bSGleb Smirnoff /* There was only one page. */ 23033a2a37bSGleb Smirnoff return; 23133a2a37bSGleb Smirnoff 23233a2a37bSGleb Smirnoff /* Subtract first one. */ 23333a2a37bSGleb Smirnoff if (new == 0) { 23433a2a37bSGleb Smirnoff *space -= xfsize(0, old, off, *space); 23533a2a37bSGleb Smirnoff new++; 23633a2a37bSGleb Smirnoff } 23733a2a37bSGleb Smirnoff 23833a2a37bSGleb Smirnoff /* Rest of pages are full sized. */ 23933a2a37bSGleb Smirnoff *space -= (old - new) * PAGE_SIZE; 24033a2a37bSGleb Smirnoff 24133a2a37bSGleb Smirnoff KASSERT(*space >= 0, ("%s: space went backwards", __func__)); 24233a2a37bSGleb Smirnoff } 24333a2a37bSGleb Smirnoff 24433a2a37bSGleb Smirnoff /* 24533a2a37bSGleb Smirnoff * I/O completion callback. 24633a2a37bSGleb Smirnoff */ 24733a2a37bSGleb Smirnoff static void 24833a2a37bSGleb Smirnoff sendfile_iodone(void *arg, vm_page_t *pg, int count, int error) 24933a2a37bSGleb Smirnoff { 25033a2a37bSGleb Smirnoff struct sf_io *sfio = arg; 251d37aa3ccSGleb Smirnoff struct socket *so = sfio->so; 25233a2a37bSGleb Smirnoff 25333a2a37bSGleb Smirnoff for (int i = 0; i < count; i++) 2545dba303dSGleb Smirnoff if (pg[i] != bogus_page) 25533a2a37bSGleb Smirnoff vm_page_xunbusy(pg[i]); 25633a2a37bSGleb Smirnoff 25733a2a37bSGleb Smirnoff if (error) 25833a2a37bSGleb Smirnoff sfio->error = error; 25933a2a37bSGleb Smirnoff 26033a2a37bSGleb Smirnoff if (!refcount_release(&sfio->nios)) 26133a2a37bSGleb Smirnoff return; 26233a2a37bSGleb Smirnoff 26333a2a37bSGleb Smirnoff if (sfio->error) { 26433a2a37bSGleb Smirnoff struct mbuf *m; 26533a2a37bSGleb Smirnoff 26633a2a37bSGleb Smirnoff /* 26733a2a37bSGleb Smirnoff * I/O operation failed. The state of data in the socket 26833a2a37bSGleb Smirnoff * is now inconsistent, and all what we can do is to tear 26933a2a37bSGleb Smirnoff * it down. Protocol abort method would tear down protocol 27033a2a37bSGleb Smirnoff * state, free all ready mbufs and detach not ready ones. 27133a2a37bSGleb Smirnoff * We will free the mbufs corresponding to this I/O manually. 27233a2a37bSGleb Smirnoff * 27333a2a37bSGleb Smirnoff * The socket would be marked with EIO and made available 27433a2a37bSGleb Smirnoff * for read, so that application receives EIO on next 27533a2a37bSGleb Smirnoff * syscall and eventually closes the socket. 27633a2a37bSGleb Smirnoff */ 27733a2a37bSGleb Smirnoff so->so_proto->pr_usrreqs->pru_abort(so); 27833a2a37bSGleb Smirnoff so->so_error = EIO; 27933a2a37bSGleb Smirnoff 28033a2a37bSGleb Smirnoff m = sfio->m; 28133a2a37bSGleb Smirnoff for (int i = 0; i < sfio->npages; i++) 28233a2a37bSGleb Smirnoff m = m_free(m); 28333a2a37bSGleb Smirnoff } else { 28433a2a37bSGleb Smirnoff CURVNET_SET(so->so_vnet); 28533a2a37bSGleb Smirnoff (void )(so->so_proto->pr_usrreqs->pru_ready)(so, sfio->m, 28633a2a37bSGleb Smirnoff sfio->npages); 28733a2a37bSGleb Smirnoff CURVNET_RESTORE(); 28833a2a37bSGleb Smirnoff } 28933a2a37bSGleb Smirnoff 290d37aa3ccSGleb Smirnoff SOCK_LOCK(so); 291d37aa3ccSGleb Smirnoff sorele(so); 29233a2a37bSGleb Smirnoff free(sfio, M_TEMP); 29333a2a37bSGleb Smirnoff } 29433a2a37bSGleb Smirnoff 29533a2a37bSGleb Smirnoff /* 29633a2a37bSGleb Smirnoff * Iterate through pages vector and request paging for non-valid pages. 29733a2a37bSGleb Smirnoff */ 29833a2a37bSGleb Smirnoff static int 29933a2a37bSGleb Smirnoff sendfile_swapin(vm_object_t obj, struct sf_io *sfio, off_t off, off_t len, 30033a2a37bSGleb Smirnoff int npages, int rhpages, int flags) 30133a2a37bSGleb Smirnoff { 30233a2a37bSGleb Smirnoff vm_page_t *pa = sfio->pa; 303af0460beSMark Johnston int grabbed, nios; 30433a2a37bSGleb Smirnoff 30533a2a37bSGleb Smirnoff nios = 0; 30633a2a37bSGleb Smirnoff flags = (flags & SF_NODISKIO) ? VM_ALLOC_NOWAIT : 0; 30733a2a37bSGleb Smirnoff 30833a2a37bSGleb Smirnoff /* 30933a2a37bSGleb Smirnoff * First grab all the pages and wire them. Note that we grab 31033a2a37bSGleb Smirnoff * only required pages. Readahead pages are dealt with later. 31133a2a37bSGleb Smirnoff */ 31233a2a37bSGleb Smirnoff VM_OBJECT_WLOCK(obj); 313af0460beSMark Johnston 314af0460beSMark Johnston grabbed = vm_page_grab_pages(obj, OFF_TO_IDX(off), 315af0460beSMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_WIRED | flags, pa, npages); 316af0460beSMark Johnston if (grabbed < npages) { 317af0460beSMark Johnston for (int i = grabbed; i < npages; i++) 318af0460beSMark Johnston pa[i] = NULL; 319af0460beSMark Johnston npages = grabbed; 32033a2a37bSGleb Smirnoff rhpages = 0; 32133a2a37bSGleb Smirnoff } 32233a2a37bSGleb Smirnoff 32333a2a37bSGleb Smirnoff for (int i = 0; i < npages;) { 32433a2a37bSGleb Smirnoff int j, a, count, rv; 32533a2a37bSGleb Smirnoff 32633a2a37bSGleb Smirnoff /* Skip valid pages. */ 32733a2a37bSGleb Smirnoff if (vm_page_is_valid(pa[i], vmoff(i, off) & PAGE_MASK, 32833a2a37bSGleb Smirnoff xfsize(i, npages, off, len))) { 32933a2a37bSGleb Smirnoff vm_page_xunbusy(pa[i]); 33033a2a37bSGleb Smirnoff SFSTAT_INC(sf_pages_valid); 33133a2a37bSGleb Smirnoff i++; 33233a2a37bSGleb Smirnoff continue; 33333a2a37bSGleb Smirnoff } 33433a2a37bSGleb Smirnoff 33533a2a37bSGleb Smirnoff /* 3365dba303dSGleb Smirnoff * Next page is invalid. Check if it belongs to pager. It 3375dba303dSGleb Smirnoff * may not be there, which is a regular situation for shmem 3385dba303dSGleb Smirnoff * pager. For vnode pager this happens only in case of 3395dba303dSGleb Smirnoff * a sparse file. 34033a2a37bSGleb Smirnoff * 34133a2a37bSGleb Smirnoff * Important feature of vm_pager_has_page() is the hint 34233a2a37bSGleb Smirnoff * stored in 'a', about how many pages we can pagein after 34333a2a37bSGleb Smirnoff * this page in a single I/O. 34433a2a37bSGleb Smirnoff */ 3455dba303dSGleb Smirnoff if (!vm_pager_has_page(obj, OFF_TO_IDX(vmoff(i, off)), NULL, 3465dba303dSGleb Smirnoff &a)) { 34733a2a37bSGleb Smirnoff pmap_zero_page(pa[i]); 34833a2a37bSGleb Smirnoff pa[i]->valid = VM_PAGE_BITS_ALL; 3496921451dSAlan Cox MPASS(pa[i]->dirty == 0); 35033a2a37bSGleb Smirnoff vm_page_xunbusy(pa[i]); 35133a2a37bSGleb Smirnoff i++; 35233a2a37bSGleb Smirnoff continue; 3535dba303dSGleb Smirnoff } 35433a2a37bSGleb Smirnoff 35533a2a37bSGleb Smirnoff /* 35633a2a37bSGleb Smirnoff * We want to pagein as many pages as possible, limited only 35733a2a37bSGleb Smirnoff * by the 'a' hint and actual request. 35833a2a37bSGleb Smirnoff */ 35933a2a37bSGleb Smirnoff count = min(a + 1, npages - i); 36033a2a37bSGleb Smirnoff 3615dba303dSGleb Smirnoff /* 3625dba303dSGleb Smirnoff * We should not pagein into a valid page, thus we first trim 3635dba303dSGleb Smirnoff * any valid pages off the end of request, and substitute 3645dba303dSGleb Smirnoff * to bogus_page those, that are in the middle. 3655dba303dSGleb Smirnoff */ 3665dba303dSGleb Smirnoff for (j = i + count - 1; j > i; j--) { 3675dba303dSGleb Smirnoff if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK, 3685dba303dSGleb Smirnoff xfsize(j, npages, off, len))) { 3695dba303dSGleb Smirnoff count--; 3705dba303dSGleb Smirnoff rhpages = 0; 3715dba303dSGleb Smirnoff } else 3725dba303dSGleb Smirnoff break; 3735dba303dSGleb Smirnoff } 3745dba303dSGleb Smirnoff for (j = i + 1; j < i + count - 1; j++) 3755dba303dSGleb Smirnoff if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK, 3765dba303dSGleb Smirnoff xfsize(j, npages, off, len))) { 3775dba303dSGleb Smirnoff vm_page_xunbusy(pa[j]); 3785dba303dSGleb Smirnoff SFSTAT_INC(sf_pages_valid); 3795dba303dSGleb Smirnoff SFSTAT_INC(sf_pages_bogus); 3805dba303dSGleb Smirnoff pa[j] = bogus_page; 3815dba303dSGleb Smirnoff } 3825dba303dSGleb Smirnoff 38333a2a37bSGleb Smirnoff refcount_acquire(&sfio->nios); 38433a2a37bSGleb Smirnoff rv = vm_pager_get_pages_async(obj, pa + i, count, NULL, 38533a2a37bSGleb Smirnoff i + count == npages ? &rhpages : NULL, 38633a2a37bSGleb Smirnoff &sendfile_iodone, sfio); 38733a2a37bSGleb Smirnoff KASSERT(rv == VM_PAGER_OK, ("%s: pager fail obj %p page %p", 38833a2a37bSGleb Smirnoff __func__, obj, pa[i])); 38933a2a37bSGleb Smirnoff 39033a2a37bSGleb Smirnoff SFSTAT_INC(sf_iocnt); 39133a2a37bSGleb Smirnoff SFSTAT_ADD(sf_pages_read, count); 39233a2a37bSGleb Smirnoff if (i + count == npages) 39333a2a37bSGleb Smirnoff SFSTAT_ADD(sf_rhpages_read, rhpages); 39433a2a37bSGleb Smirnoff 3955dba303dSGleb Smirnoff /* 3965dba303dSGleb Smirnoff * Restore the valid page pointers. They are already 3975dba303dSGleb Smirnoff * unbusied, but still wired. 3985dba303dSGleb Smirnoff */ 3995dba303dSGleb Smirnoff for (j = i; j < i + count; j++) 4005dba303dSGleb Smirnoff if (pa[j] == bogus_page) { 4015dba303dSGleb Smirnoff pa[j] = vm_page_lookup(obj, 4025dba303dSGleb Smirnoff OFF_TO_IDX(vmoff(j, off))); 4035dba303dSGleb Smirnoff KASSERT(pa[j], ("%s: page %p[%d] disappeared", 4045dba303dSGleb Smirnoff __func__, pa, j)); 4055dba303dSGleb Smirnoff 4065dba303dSGleb Smirnoff } 40733a2a37bSGleb Smirnoff i += count; 40833a2a37bSGleb Smirnoff nios++; 40933a2a37bSGleb Smirnoff } 41033a2a37bSGleb Smirnoff 41133a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 41233a2a37bSGleb Smirnoff 41333a2a37bSGleb Smirnoff if (nios == 0 && npages != 0) 41433a2a37bSGleb Smirnoff SFSTAT_INC(sf_noiocnt); 41533a2a37bSGleb Smirnoff 41633a2a37bSGleb Smirnoff return (nios); 41733a2a37bSGleb Smirnoff } 41833a2a37bSGleb Smirnoff 41933a2a37bSGleb Smirnoff static int 42033a2a37bSGleb Smirnoff sendfile_getobj(struct thread *td, struct file *fp, vm_object_t *obj_res, 42133a2a37bSGleb Smirnoff struct vnode **vp_res, struct shmfd **shmfd_res, off_t *obj_size, 42233a2a37bSGleb Smirnoff int *bsize) 42333a2a37bSGleb Smirnoff { 42433a2a37bSGleb Smirnoff struct vattr va; 42533a2a37bSGleb Smirnoff vm_object_t obj; 42633a2a37bSGleb Smirnoff struct vnode *vp; 42733a2a37bSGleb Smirnoff struct shmfd *shmfd; 42833a2a37bSGleb Smirnoff int error; 42933a2a37bSGleb Smirnoff 43033a2a37bSGleb Smirnoff vp = *vp_res = NULL; 43133a2a37bSGleb Smirnoff obj = NULL; 43233a2a37bSGleb Smirnoff shmfd = *shmfd_res = NULL; 43333a2a37bSGleb Smirnoff *bsize = 0; 43433a2a37bSGleb Smirnoff 43533a2a37bSGleb Smirnoff /* 43633a2a37bSGleb Smirnoff * The file descriptor must be a regular file and have a 43733a2a37bSGleb Smirnoff * backing VM object. 43833a2a37bSGleb Smirnoff */ 43933a2a37bSGleb Smirnoff if (fp->f_type == DTYPE_VNODE) { 44033a2a37bSGleb Smirnoff vp = fp->f_vnode; 44133a2a37bSGleb Smirnoff vn_lock(vp, LK_SHARED | LK_RETRY); 44233a2a37bSGleb Smirnoff if (vp->v_type != VREG) { 44333a2a37bSGleb Smirnoff error = EINVAL; 44433a2a37bSGleb Smirnoff goto out; 44533a2a37bSGleb Smirnoff } 44633a2a37bSGleb Smirnoff *bsize = vp->v_mount->mnt_stat.f_iosize; 44733a2a37bSGleb Smirnoff error = VOP_GETATTR(vp, &va, td->td_ucred); 44833a2a37bSGleb Smirnoff if (error != 0) 44933a2a37bSGleb Smirnoff goto out; 45033a2a37bSGleb Smirnoff *obj_size = va.va_size; 45133a2a37bSGleb Smirnoff obj = vp->v_object; 45233a2a37bSGleb Smirnoff if (obj == NULL) { 45333a2a37bSGleb Smirnoff error = EINVAL; 45433a2a37bSGleb Smirnoff goto out; 45533a2a37bSGleb Smirnoff } 45633a2a37bSGleb Smirnoff } else if (fp->f_type == DTYPE_SHM) { 45733a2a37bSGleb Smirnoff error = 0; 45833a2a37bSGleb Smirnoff shmfd = fp->f_data; 45933a2a37bSGleb Smirnoff obj = shmfd->shm_object; 46033a2a37bSGleb Smirnoff *obj_size = shmfd->shm_size; 46133a2a37bSGleb Smirnoff } else { 46233a2a37bSGleb Smirnoff error = EINVAL; 46333a2a37bSGleb Smirnoff goto out; 46433a2a37bSGleb Smirnoff } 46533a2a37bSGleb Smirnoff 46633a2a37bSGleb Smirnoff VM_OBJECT_WLOCK(obj); 46733a2a37bSGleb Smirnoff if ((obj->flags & OBJ_DEAD) != 0) { 46833a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 46933a2a37bSGleb Smirnoff error = EBADF; 47033a2a37bSGleb Smirnoff goto out; 47133a2a37bSGleb Smirnoff } 47233a2a37bSGleb Smirnoff 47333a2a37bSGleb Smirnoff /* 47433a2a37bSGleb Smirnoff * Temporarily increase the backing VM object's reference 47533a2a37bSGleb Smirnoff * count so that a forced reclamation of its vnode does not 47633a2a37bSGleb Smirnoff * immediately destroy it. 47733a2a37bSGleb Smirnoff */ 47833a2a37bSGleb Smirnoff vm_object_reference_locked(obj); 47933a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 48033a2a37bSGleb Smirnoff *obj_res = obj; 48133a2a37bSGleb Smirnoff *vp_res = vp; 48233a2a37bSGleb Smirnoff *shmfd_res = shmfd; 48333a2a37bSGleb Smirnoff 48433a2a37bSGleb Smirnoff out: 48533a2a37bSGleb Smirnoff if (vp != NULL) 48633a2a37bSGleb Smirnoff VOP_UNLOCK(vp, 0); 48733a2a37bSGleb Smirnoff return (error); 48833a2a37bSGleb Smirnoff } 48933a2a37bSGleb Smirnoff 49033a2a37bSGleb Smirnoff static int 49133a2a37bSGleb Smirnoff sendfile_getsock(struct thread *td, int s, struct file **sock_fp, 49233a2a37bSGleb Smirnoff struct socket **so) 49333a2a37bSGleb Smirnoff { 49433a2a37bSGleb Smirnoff cap_rights_t rights; 49533a2a37bSGleb Smirnoff int error; 49633a2a37bSGleb Smirnoff 49733a2a37bSGleb Smirnoff *sock_fp = NULL; 49833a2a37bSGleb Smirnoff *so = NULL; 49933a2a37bSGleb Smirnoff 50033a2a37bSGleb Smirnoff /* 50133a2a37bSGleb Smirnoff * The socket must be a stream socket and connected. 50233a2a37bSGleb Smirnoff */ 50333a2a37bSGleb Smirnoff error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SEND), 50485b0f9deSMariusz Zaborski sock_fp, NULL, NULL); 50533a2a37bSGleb Smirnoff if (error != 0) 50633a2a37bSGleb Smirnoff return (error); 50733a2a37bSGleb Smirnoff *so = (*sock_fp)->f_data; 50833a2a37bSGleb Smirnoff if ((*so)->so_type != SOCK_STREAM) 50933a2a37bSGleb Smirnoff return (EINVAL); 51075c8dfb6SSean Bruno if ((*so)->so_error) { 51175c8dfb6SSean Bruno error = (*so)->so_error; 51275c8dfb6SSean Bruno (*so)->so_error = 0; 51375c8dfb6SSean Bruno return (error); 51475c8dfb6SSean Bruno } 51533a2a37bSGleb Smirnoff if (((*so)->so_state & SS_ISCONNECTED) == 0) 51633a2a37bSGleb Smirnoff return (ENOTCONN); 51733a2a37bSGleb Smirnoff return (0); 51833a2a37bSGleb Smirnoff } 51933a2a37bSGleb Smirnoff 52033a2a37bSGleb Smirnoff int 52133a2a37bSGleb Smirnoff vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, 52233a2a37bSGleb Smirnoff struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags, 5239c64cfe5SGleb Smirnoff struct thread *td) 52433a2a37bSGleb Smirnoff { 52533a2a37bSGleb Smirnoff struct file *sock_fp; 52633a2a37bSGleb Smirnoff struct vnode *vp; 52733a2a37bSGleb Smirnoff struct vm_object *obj; 52833a2a37bSGleb Smirnoff struct socket *so; 52933a2a37bSGleb Smirnoff struct mbuf *m, *mh, *mhtail; 53033a2a37bSGleb Smirnoff struct sf_buf *sf; 53133a2a37bSGleb Smirnoff struct shmfd *shmfd; 53233a2a37bSGleb Smirnoff struct sendfile_sync *sfs; 53333a2a37bSGleb Smirnoff struct vattr va; 53433a2a37bSGleb Smirnoff off_t off, sbytes, rem, obj_size; 53533a2a37bSGleb Smirnoff int error, softerr, bsize, hdrlen; 53633a2a37bSGleb Smirnoff 53733a2a37bSGleb Smirnoff obj = NULL; 53833a2a37bSGleb Smirnoff so = NULL; 53933a2a37bSGleb Smirnoff m = mh = NULL; 54033a2a37bSGleb Smirnoff sfs = NULL; 5419c64cfe5SGleb Smirnoff hdrlen = sbytes = 0; 54233a2a37bSGleb Smirnoff softerr = 0; 54333a2a37bSGleb Smirnoff 54433a2a37bSGleb Smirnoff error = sendfile_getobj(td, fp, &obj, &vp, &shmfd, &obj_size, &bsize); 54533a2a37bSGleb Smirnoff if (error != 0) 54633a2a37bSGleb Smirnoff return (error); 54733a2a37bSGleb Smirnoff 54833a2a37bSGleb Smirnoff error = sendfile_getsock(td, sockfd, &sock_fp, &so); 54933a2a37bSGleb Smirnoff if (error != 0) 55033a2a37bSGleb Smirnoff goto out; 55133a2a37bSGleb Smirnoff 55233a2a37bSGleb Smirnoff #ifdef MAC 55333a2a37bSGleb Smirnoff error = mac_socket_check_send(td->td_ucred, so); 55433a2a37bSGleb Smirnoff if (error != 0) 55533a2a37bSGleb Smirnoff goto out; 55633a2a37bSGleb Smirnoff #endif 55733a2a37bSGleb Smirnoff 55833a2a37bSGleb Smirnoff SFSTAT_INC(sf_syscalls); 55933a2a37bSGleb Smirnoff SFSTAT_ADD(sf_rhpages_requested, SF_READAHEAD(flags)); 56033a2a37bSGleb Smirnoff 56133a2a37bSGleb Smirnoff if (flags & SF_SYNC) { 56233a2a37bSGleb Smirnoff sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO); 56333a2a37bSGleb Smirnoff mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF); 56433a2a37bSGleb Smirnoff cv_init(&sfs->cv, "sendfile"); 56533a2a37bSGleb Smirnoff } 56633a2a37bSGleb Smirnoff 56733a2a37bSGleb Smirnoff rem = nbytes ? omin(nbytes, obj_size - offset) : obj_size - offset; 56833a2a37bSGleb Smirnoff 56933a2a37bSGleb Smirnoff /* 57033a2a37bSGleb Smirnoff * Protect against multiple writers to the socket. 57133a2a37bSGleb Smirnoff * 57233a2a37bSGleb Smirnoff * XXXRW: Historically this has assumed non-interruptibility, so now 57333a2a37bSGleb Smirnoff * we implement that, but possibly shouldn't. 57433a2a37bSGleb Smirnoff */ 57533a2a37bSGleb Smirnoff (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR); 57633a2a37bSGleb Smirnoff 57733a2a37bSGleb Smirnoff /* 57833a2a37bSGleb Smirnoff * Loop through the pages of the file, starting with the requested 57933a2a37bSGleb Smirnoff * offset. Get a file page (do I/O if necessary), map the file page 58033a2a37bSGleb Smirnoff * into an sf_buf, attach an mbuf header to the sf_buf, and queue 58133a2a37bSGleb Smirnoff * it on the socket. 58233a2a37bSGleb Smirnoff * This is done in two loops. The inner loop turns as many pages 58333a2a37bSGleb Smirnoff * as it can, up to available socket buffer space, without blocking 58433a2a37bSGleb Smirnoff * into mbufs to have it bulk delivered into the socket send buffer. 58533a2a37bSGleb Smirnoff * The outer loop checks the state and available space of the socket 58633a2a37bSGleb Smirnoff * and takes care of the overall progress. 58733a2a37bSGleb Smirnoff */ 58833a2a37bSGleb Smirnoff for (off = offset; rem > 0; ) { 58933a2a37bSGleb Smirnoff struct sf_io *sfio; 59033a2a37bSGleb Smirnoff vm_page_t *pa; 59133a2a37bSGleb Smirnoff struct mbuf *mtail; 59233a2a37bSGleb Smirnoff int nios, space, npages, rhpages; 59333a2a37bSGleb Smirnoff 59433a2a37bSGleb Smirnoff mtail = NULL; 59533a2a37bSGleb Smirnoff /* 59633a2a37bSGleb Smirnoff * Check the socket state for ongoing connection, 59733a2a37bSGleb Smirnoff * no errors and space in socket buffer. 59833a2a37bSGleb Smirnoff * If space is low allow for the remainder of the 59933a2a37bSGleb Smirnoff * file to be processed if it fits the socket buffer. 60033a2a37bSGleb Smirnoff * Otherwise block in waiting for sufficient space 60133a2a37bSGleb Smirnoff * to proceed, or if the socket is nonblocking, return 60233a2a37bSGleb Smirnoff * to userland with EAGAIN while reporting how far 60333a2a37bSGleb Smirnoff * we've come. 60433a2a37bSGleb Smirnoff * We wait until the socket buffer has significant free 60533a2a37bSGleb Smirnoff * space to do bulk sends. This makes good use of file 60633a2a37bSGleb Smirnoff * system read ahead and allows packet segmentation 60733a2a37bSGleb Smirnoff * offloading hardware to take over lots of work. If 60833a2a37bSGleb Smirnoff * we were not careful here we would send off only one 60933a2a37bSGleb Smirnoff * sfbuf at a time. 61033a2a37bSGleb Smirnoff */ 61133a2a37bSGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 61233a2a37bSGleb Smirnoff if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2) 61333a2a37bSGleb Smirnoff so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2; 61433a2a37bSGleb Smirnoff retry_space: 61533a2a37bSGleb Smirnoff if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 61633a2a37bSGleb Smirnoff error = EPIPE; 61733a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 61833a2a37bSGleb Smirnoff goto done; 61933a2a37bSGleb Smirnoff } else if (so->so_error) { 62033a2a37bSGleb Smirnoff error = so->so_error; 62133a2a37bSGleb Smirnoff so->so_error = 0; 62233a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 62333a2a37bSGleb Smirnoff goto done; 62433a2a37bSGleb Smirnoff } 62533a2a37bSGleb Smirnoff space = sbspace(&so->so_snd); 62633a2a37bSGleb Smirnoff if (space < rem && 62733a2a37bSGleb Smirnoff (space <= 0 || 62833a2a37bSGleb Smirnoff space < so->so_snd.sb_lowat)) { 62933a2a37bSGleb Smirnoff if (so->so_state & SS_NBIO) { 63033a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 63133a2a37bSGleb Smirnoff error = EAGAIN; 63233a2a37bSGleb Smirnoff goto done; 63333a2a37bSGleb Smirnoff } 63433a2a37bSGleb Smirnoff /* 63533a2a37bSGleb Smirnoff * sbwait drops the lock while sleeping. 63633a2a37bSGleb Smirnoff * When we loop back to retry_space the 63733a2a37bSGleb Smirnoff * state may have changed and we retest 63833a2a37bSGleb Smirnoff * for it. 63933a2a37bSGleb Smirnoff */ 64033a2a37bSGleb Smirnoff error = sbwait(&so->so_snd); 64133a2a37bSGleb Smirnoff /* 64233a2a37bSGleb Smirnoff * An error from sbwait usually indicates that we've 64333a2a37bSGleb Smirnoff * been interrupted by a signal. If we've sent anything 64433a2a37bSGleb Smirnoff * then return bytes sent, otherwise return the error. 64533a2a37bSGleb Smirnoff */ 64633a2a37bSGleb Smirnoff if (error != 0) { 64733a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 64833a2a37bSGleb Smirnoff goto done; 64933a2a37bSGleb Smirnoff } 65033a2a37bSGleb Smirnoff goto retry_space; 65133a2a37bSGleb Smirnoff } 65233a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 65333a2a37bSGleb Smirnoff 65433a2a37bSGleb Smirnoff /* 6559c64cfe5SGleb Smirnoff * At the beginning of the first loop check if any headers 6569c64cfe5SGleb Smirnoff * are specified and copy them into mbufs. Reduce space in 6579c64cfe5SGleb Smirnoff * the socket buffer by the size of the header mbuf chain. 6589c64cfe5SGleb Smirnoff * Clear hdr_uio here and hdrlen at the end of the first loop. 65933a2a37bSGleb Smirnoff */ 6609c64cfe5SGleb Smirnoff if (hdr_uio != NULL && hdr_uio->uio_resid > 0) { 6619c64cfe5SGleb Smirnoff hdr_uio->uio_td = td; 6629c64cfe5SGleb Smirnoff hdr_uio->uio_rw = UIO_WRITE; 663a2d8f9d2SGleb Smirnoff mh = m_uiotombuf(hdr_uio, M_WAITOK, space, 0, 0); 6649c64cfe5SGleb Smirnoff hdrlen = m_length(mh, &mhtail); 66533a2a37bSGleb Smirnoff space -= hdrlen; 666a2d8f9d2SGleb Smirnoff /* 667a2d8f9d2SGleb Smirnoff * If header consumed all the socket buffer space, 668a2d8f9d2SGleb Smirnoff * don't waste CPU cycles and jump to the end. 669a2d8f9d2SGleb Smirnoff */ 670a2d8f9d2SGleb Smirnoff if (space == 0) { 671a2d8f9d2SGleb Smirnoff sfio = NULL; 672a2d8f9d2SGleb Smirnoff nios = 0; 673a2d8f9d2SGleb Smirnoff goto prepend_header; 674a2d8f9d2SGleb Smirnoff } 6759c64cfe5SGleb Smirnoff hdr_uio = NULL; 6769c64cfe5SGleb Smirnoff } 67733a2a37bSGleb Smirnoff 67833a2a37bSGleb Smirnoff if (vp != NULL) { 67933a2a37bSGleb Smirnoff error = vn_lock(vp, LK_SHARED); 68033a2a37bSGleb Smirnoff if (error != 0) 68133a2a37bSGleb Smirnoff goto done; 68233a2a37bSGleb Smirnoff error = VOP_GETATTR(vp, &va, td->td_ucred); 68333a2a37bSGleb Smirnoff if (error != 0 || off >= va.va_size) { 68433a2a37bSGleb Smirnoff VOP_UNLOCK(vp, 0); 68533a2a37bSGleb Smirnoff goto done; 68633a2a37bSGleb Smirnoff } 68733a2a37bSGleb Smirnoff if (va.va_size != obj_size) { 68833a2a37bSGleb Smirnoff obj_size = va.va_size; 6899e3c8bd3SGleb Smirnoff rem = nbytes ? 6909e3c8bd3SGleb Smirnoff omin(nbytes + offset, obj_size) : obj_size; 6919e3c8bd3SGleb Smirnoff rem -= off; 69233a2a37bSGleb Smirnoff } 69333a2a37bSGleb Smirnoff } 69433a2a37bSGleb Smirnoff 69533a2a37bSGleb Smirnoff if (space > rem) 69633a2a37bSGleb Smirnoff space = rem; 69733a2a37bSGleb Smirnoff 69833a2a37bSGleb Smirnoff npages = howmany(space + (off & PAGE_MASK), PAGE_SIZE); 69933a2a37bSGleb Smirnoff 70033a2a37bSGleb Smirnoff /* 70133a2a37bSGleb Smirnoff * Calculate maximum allowed number of pages for readahead 70200b5ffdeSGleb Smirnoff * at this iteration. If SF_USER_READAHEAD was set, we don't 70300b5ffdeSGleb Smirnoff * do any heuristics and use exactly the value supplied by 70400b5ffdeSGleb Smirnoff * application. Otherwise, we allow readahead up to "rem". 70533a2a37bSGleb Smirnoff * If application wants more, let it be, but there is no 70633a2a37bSGleb Smirnoff * reason to go above MAXPHYS. Also check against "obj_size", 70733a2a37bSGleb Smirnoff * since vm_pager_has_page() can hint beyond EOF. 70833a2a37bSGleb Smirnoff */ 70900b5ffdeSGleb Smirnoff if (flags & SF_USER_READAHEAD) { 71000b5ffdeSGleb Smirnoff rhpages = SF_READAHEAD(flags); 71100b5ffdeSGleb Smirnoff } else { 71200b5ffdeSGleb Smirnoff rhpages = howmany(rem + (off & PAGE_MASK), PAGE_SIZE) - 71300b5ffdeSGleb Smirnoff npages; 71433a2a37bSGleb Smirnoff rhpages += SF_READAHEAD(flags); 71500b5ffdeSGleb Smirnoff } 71633a2a37bSGleb Smirnoff rhpages = min(howmany(MAXPHYS, PAGE_SIZE), rhpages); 71733a2a37bSGleb Smirnoff rhpages = min(howmany(obj_size - trunc_page(off), PAGE_SIZE) - 71833a2a37bSGleb Smirnoff npages, rhpages); 71933a2a37bSGleb Smirnoff 72033a2a37bSGleb Smirnoff sfio = malloc(sizeof(struct sf_io) + 72133a2a37bSGleb Smirnoff npages * sizeof(vm_page_t), M_TEMP, M_WAITOK); 72233a2a37bSGleb Smirnoff refcount_init(&sfio->nios, 1); 723d37aa3ccSGleb Smirnoff sfio->so = so; 72433a2a37bSGleb Smirnoff sfio->error = 0; 72533a2a37bSGleb Smirnoff 72633a2a37bSGleb Smirnoff nios = sendfile_swapin(obj, sfio, off, space, npages, rhpages, 72733a2a37bSGleb Smirnoff flags); 72833a2a37bSGleb Smirnoff 72933a2a37bSGleb Smirnoff /* 73033a2a37bSGleb Smirnoff * Loop and construct maximum sized mbuf chain to be bulk 73133a2a37bSGleb Smirnoff * dumped into socket buffer. 73233a2a37bSGleb Smirnoff */ 73333a2a37bSGleb Smirnoff pa = sfio->pa; 73433a2a37bSGleb Smirnoff for (int i = 0; i < npages; i++) { 73533a2a37bSGleb Smirnoff struct mbuf *m0; 73633a2a37bSGleb Smirnoff 73733a2a37bSGleb Smirnoff /* 73833a2a37bSGleb Smirnoff * If a page wasn't grabbed successfully, then 73933a2a37bSGleb Smirnoff * trim the array. Can happen only with SF_NODISKIO. 74033a2a37bSGleb Smirnoff */ 74133a2a37bSGleb Smirnoff if (pa[i] == NULL) { 74233a2a37bSGleb Smirnoff SFSTAT_INC(sf_busy); 74333a2a37bSGleb Smirnoff fixspace(npages, i, off, &space); 74433a2a37bSGleb Smirnoff npages = i; 74533a2a37bSGleb Smirnoff softerr = EBUSY; 74633a2a37bSGleb Smirnoff break; 74733a2a37bSGleb Smirnoff } 74833a2a37bSGleb Smirnoff 74933a2a37bSGleb Smirnoff /* 75033a2a37bSGleb Smirnoff * Get a sendfile buf. When allocating the 75133a2a37bSGleb Smirnoff * first buffer for mbuf chain, we usually 75233a2a37bSGleb Smirnoff * wait as long as necessary, but this wait 75333a2a37bSGleb Smirnoff * can be interrupted. For consequent 75433a2a37bSGleb Smirnoff * buffers, do not sleep, since several 75533a2a37bSGleb Smirnoff * threads might exhaust the buffers and then 75633a2a37bSGleb Smirnoff * deadlock. 75733a2a37bSGleb Smirnoff */ 75833a2a37bSGleb Smirnoff sf = sf_buf_alloc(pa[i], 75933a2a37bSGleb Smirnoff m != NULL ? SFB_NOWAIT : SFB_CATCH); 76033a2a37bSGleb Smirnoff if (sf == NULL) { 76133a2a37bSGleb Smirnoff SFSTAT_INC(sf_allocfail); 76233a2a37bSGleb Smirnoff for (int j = i; j < npages; j++) { 76333a2a37bSGleb Smirnoff vm_page_lock(pa[j]); 76433a2a37bSGleb Smirnoff vm_page_unwire(pa[j], PQ_INACTIVE); 76533a2a37bSGleb Smirnoff vm_page_unlock(pa[j]); 76633a2a37bSGleb Smirnoff } 76733a2a37bSGleb Smirnoff if (m == NULL) 76833a2a37bSGleb Smirnoff softerr = ENOBUFS; 76933a2a37bSGleb Smirnoff fixspace(npages, i, off, &space); 77033a2a37bSGleb Smirnoff npages = i; 77133a2a37bSGleb Smirnoff break; 77233a2a37bSGleb Smirnoff } 77333a2a37bSGleb Smirnoff 77433a2a37bSGleb Smirnoff m0 = m_get(M_WAITOK, MT_DATA); 77533a2a37bSGleb Smirnoff m0->m_ext.ext_buf = (char *)sf_buf_kva(sf); 77633a2a37bSGleb Smirnoff m0->m_ext.ext_size = PAGE_SIZE; 77733a2a37bSGleb Smirnoff m0->m_ext.ext_arg1 = sf; 778*9c82bec4SGleb Smirnoff m0->m_ext.ext_type = EXT_SFBUF; 779*9c82bec4SGleb Smirnoff m0->m_ext.ext_flags = EXT_FLAG_EMBREF; 780*9c82bec4SGleb Smirnoff m0->m_ext.ext_free = sendfile_free_mext; 78133a2a37bSGleb Smirnoff /* 78233a2a37bSGleb Smirnoff * SF_NOCACHE sets the page as being freed upon send. 78333a2a37bSGleb Smirnoff * However, we ignore it for the last page in 'space', 78433a2a37bSGleb Smirnoff * if the page is truncated, and we got more data to 78533a2a37bSGleb Smirnoff * send (rem > space), or if we have readahead 78633a2a37bSGleb Smirnoff * configured (rhpages > 0). 78733a2a37bSGleb Smirnoff */ 788*9c82bec4SGleb Smirnoff if ((flags & SF_NOCACHE) && 789*9c82bec4SGleb Smirnoff (i != npages - 1 || 790*9c82bec4SGleb Smirnoff !((off + space) & PAGE_MASK) || 791*9c82bec4SGleb Smirnoff !(rem > space || rhpages > 0))) 792*9c82bec4SGleb Smirnoff m0->m_ext.ext_flags |= EXT_FLAG_NOCACHE; 793*9c82bec4SGleb Smirnoff if (sfs != NULL) { 794*9c82bec4SGleb Smirnoff m0->m_ext.ext_flags |= EXT_FLAG_SYNC; 795*9c82bec4SGleb Smirnoff m0->m_ext.ext_arg2 = sfs; 796*9c82bec4SGleb Smirnoff mtx_lock(&sfs->mtx); 797*9c82bec4SGleb Smirnoff sfs->count++; 798*9c82bec4SGleb Smirnoff mtx_unlock(&sfs->mtx); 799*9c82bec4SGleb Smirnoff } 80056a5f52eSGleb Smirnoff m0->m_ext.ext_count = 1; 80133a2a37bSGleb Smirnoff m0->m_flags |= (M_EXT | M_RDONLY); 80233a2a37bSGleb Smirnoff if (nios) 80333a2a37bSGleb Smirnoff m0->m_flags |= M_NOTREADY; 80433a2a37bSGleb Smirnoff m0->m_data = (char *)sf_buf_kva(sf) + 80533a2a37bSGleb Smirnoff (vmoff(i, off) & PAGE_MASK); 80633a2a37bSGleb Smirnoff m0->m_len = xfsize(i, npages, off, space); 80733a2a37bSGleb Smirnoff 80833a2a37bSGleb Smirnoff if (i == 0) 80933a2a37bSGleb Smirnoff sfio->m = m0; 81033a2a37bSGleb Smirnoff 81133a2a37bSGleb Smirnoff /* Append to mbuf chain. */ 81233a2a37bSGleb Smirnoff if (mtail != NULL) 81333a2a37bSGleb Smirnoff mtail->m_next = m0; 81433a2a37bSGleb Smirnoff else 81533a2a37bSGleb Smirnoff m = m0; 81633a2a37bSGleb Smirnoff mtail = m0; 81733a2a37bSGleb Smirnoff } 81833a2a37bSGleb Smirnoff 81933a2a37bSGleb Smirnoff if (vp != NULL) 82033a2a37bSGleb Smirnoff VOP_UNLOCK(vp, 0); 82133a2a37bSGleb Smirnoff 82233a2a37bSGleb Smirnoff /* Keep track of bytes processed. */ 82333a2a37bSGleb Smirnoff off += space; 82433a2a37bSGleb Smirnoff rem -= space; 82533a2a37bSGleb Smirnoff 82633a2a37bSGleb Smirnoff /* Prepend header, if any. */ 82733a2a37bSGleb Smirnoff if (hdrlen) { 828a2d8f9d2SGleb Smirnoff prepend_header: 82933a2a37bSGleb Smirnoff mhtail->m_next = m; 83033a2a37bSGleb Smirnoff m = mh; 83133a2a37bSGleb Smirnoff mh = NULL; 83233a2a37bSGleb Smirnoff } 83333a2a37bSGleb Smirnoff 83433a2a37bSGleb Smirnoff if (m == NULL) { 83533a2a37bSGleb Smirnoff KASSERT(softerr, ("%s: m NULL, no error", __func__)); 83633a2a37bSGleb Smirnoff error = softerr; 83733a2a37bSGleb Smirnoff free(sfio, M_TEMP); 83833a2a37bSGleb Smirnoff goto done; 83933a2a37bSGleb Smirnoff } 84033a2a37bSGleb Smirnoff 84133a2a37bSGleb Smirnoff /* Add the buffer chain to the socket buffer. */ 84233a2a37bSGleb Smirnoff KASSERT(m_length(m, NULL) == space + hdrlen, 84333a2a37bSGleb Smirnoff ("%s: mlen %u space %d hdrlen %d", 84433a2a37bSGleb Smirnoff __func__, m_length(m, NULL), space, hdrlen)); 84533a2a37bSGleb Smirnoff 84633a2a37bSGleb Smirnoff CURVNET_SET(so->so_vnet); 84733a2a37bSGleb Smirnoff if (nios == 0) { 84833a2a37bSGleb Smirnoff /* 84933a2a37bSGleb Smirnoff * If sendfile_swapin() didn't initiate any I/Os, 85033a2a37bSGleb Smirnoff * which happens if all data is cached in VM, then 85133a2a37bSGleb Smirnoff * we can send data right now without the 85233a2a37bSGleb Smirnoff * PRUS_NOTREADY flag. 85333a2a37bSGleb Smirnoff */ 85433a2a37bSGleb Smirnoff free(sfio, M_TEMP); 85533a2a37bSGleb Smirnoff error = (*so->so_proto->pr_usrreqs->pru_send) 85633a2a37bSGleb Smirnoff (so, 0, m, NULL, NULL, td); 85733a2a37bSGleb Smirnoff } else { 85833a2a37bSGleb Smirnoff sfio->npages = npages; 859d37aa3ccSGleb Smirnoff soref(so); 86033a2a37bSGleb Smirnoff error = (*so->so_proto->pr_usrreqs->pru_send) 86133a2a37bSGleb Smirnoff (so, PRUS_NOTREADY, m, NULL, NULL, td); 86233a2a37bSGleb Smirnoff sendfile_iodone(sfio, NULL, 0, 0); 86333a2a37bSGleb Smirnoff } 86433a2a37bSGleb Smirnoff CURVNET_RESTORE(); 86533a2a37bSGleb Smirnoff 86633a2a37bSGleb Smirnoff m = NULL; /* pru_send always consumes */ 86733a2a37bSGleb Smirnoff if (error) 86833a2a37bSGleb Smirnoff goto done; 86933a2a37bSGleb Smirnoff sbytes += space + hdrlen; 87033a2a37bSGleb Smirnoff if (hdrlen) 87133a2a37bSGleb Smirnoff hdrlen = 0; 87233a2a37bSGleb Smirnoff if (softerr) { 87333a2a37bSGleb Smirnoff error = softerr; 87433a2a37bSGleb Smirnoff goto done; 87533a2a37bSGleb Smirnoff } 87633a2a37bSGleb Smirnoff } 87733a2a37bSGleb Smirnoff 87833a2a37bSGleb Smirnoff /* 87933a2a37bSGleb Smirnoff * Send trailers. Wimp out and use writev(2). 88033a2a37bSGleb Smirnoff */ 88133a2a37bSGleb Smirnoff if (trl_uio != NULL) { 88233a2a37bSGleb Smirnoff sbunlock(&so->so_snd); 88333a2a37bSGleb Smirnoff error = kern_writev(td, sockfd, trl_uio); 88433a2a37bSGleb Smirnoff if (error == 0) 88533a2a37bSGleb Smirnoff sbytes += td->td_retval[0]; 88633a2a37bSGleb Smirnoff goto out; 88733a2a37bSGleb Smirnoff } 88833a2a37bSGleb Smirnoff 88933a2a37bSGleb Smirnoff done: 89033a2a37bSGleb Smirnoff sbunlock(&so->so_snd); 89133a2a37bSGleb Smirnoff out: 89233a2a37bSGleb Smirnoff /* 89333a2a37bSGleb Smirnoff * If there was no error we have to clear td->td_retval[0] 89433a2a37bSGleb Smirnoff * because it may have been set by writev. 89533a2a37bSGleb Smirnoff */ 89633a2a37bSGleb Smirnoff if (error == 0) { 89733a2a37bSGleb Smirnoff td->td_retval[0] = 0; 89833a2a37bSGleb Smirnoff } 89933a2a37bSGleb Smirnoff if (sent != NULL) { 90033a2a37bSGleb Smirnoff (*sent) = sbytes; 90133a2a37bSGleb Smirnoff } 90233a2a37bSGleb Smirnoff if (obj != NULL) 90333a2a37bSGleb Smirnoff vm_object_deallocate(obj); 90433a2a37bSGleb Smirnoff if (so) 90533a2a37bSGleb Smirnoff fdrop(sock_fp, td); 90633a2a37bSGleb Smirnoff if (m) 90733a2a37bSGleb Smirnoff m_freem(m); 90833a2a37bSGleb Smirnoff if (mh) 90933a2a37bSGleb Smirnoff m_freem(mh); 91033a2a37bSGleb Smirnoff 91133a2a37bSGleb Smirnoff if (sfs != NULL) { 91233a2a37bSGleb Smirnoff mtx_lock(&sfs->mtx); 91333a2a37bSGleb Smirnoff if (sfs->count != 0) 91433a2a37bSGleb Smirnoff cv_wait(&sfs->cv, &sfs->mtx); 91533a2a37bSGleb Smirnoff KASSERT(sfs->count == 0, ("sendfile sync still busy")); 91633a2a37bSGleb Smirnoff cv_destroy(&sfs->cv); 91733a2a37bSGleb Smirnoff mtx_destroy(&sfs->mtx); 91833a2a37bSGleb Smirnoff free(sfs, M_TEMP); 91933a2a37bSGleb Smirnoff } 92033a2a37bSGleb Smirnoff 92133a2a37bSGleb Smirnoff if (error == ERESTART) 92233a2a37bSGleb Smirnoff error = EINTR; 92333a2a37bSGleb Smirnoff 92433a2a37bSGleb Smirnoff return (error); 92533a2a37bSGleb Smirnoff } 92633a2a37bSGleb Smirnoff 92733a2a37bSGleb Smirnoff static int 92833a2a37bSGleb Smirnoff sendfile(struct thread *td, struct sendfile_args *uap, int compat) 92933a2a37bSGleb Smirnoff { 93033a2a37bSGleb Smirnoff struct sf_hdtr hdtr; 93133a2a37bSGleb Smirnoff struct uio *hdr_uio, *trl_uio; 93233a2a37bSGleb Smirnoff struct file *fp; 93333a2a37bSGleb Smirnoff cap_rights_t rights; 93433a2a37bSGleb Smirnoff off_t sbytes; 93533a2a37bSGleb Smirnoff int error; 93633a2a37bSGleb Smirnoff 93733a2a37bSGleb Smirnoff /* 93833a2a37bSGleb Smirnoff * File offset must be positive. If it goes beyond EOF 93933a2a37bSGleb Smirnoff * we send only the header/trailer and no payload data. 94033a2a37bSGleb Smirnoff */ 94133a2a37bSGleb Smirnoff if (uap->offset < 0) 94233a2a37bSGleb Smirnoff return (EINVAL); 94333a2a37bSGleb Smirnoff 944ef3266d5SGleb Smirnoff sbytes = 0; 94533a2a37bSGleb Smirnoff hdr_uio = trl_uio = NULL; 94633a2a37bSGleb Smirnoff 94733a2a37bSGleb Smirnoff if (uap->hdtr != NULL) { 94833a2a37bSGleb Smirnoff error = copyin(uap->hdtr, &hdtr, sizeof(hdtr)); 94933a2a37bSGleb Smirnoff if (error != 0) 95033a2a37bSGleb Smirnoff goto out; 95133a2a37bSGleb Smirnoff if (hdtr.headers != NULL) { 95233a2a37bSGleb Smirnoff error = copyinuio(hdtr.headers, hdtr.hdr_cnt, 95333a2a37bSGleb Smirnoff &hdr_uio); 95433a2a37bSGleb Smirnoff if (error != 0) 95533a2a37bSGleb Smirnoff goto out; 9569c64cfe5SGleb Smirnoff #ifdef COMPAT_FREEBSD4 9579c64cfe5SGleb Smirnoff /* 9589c64cfe5SGleb Smirnoff * In FreeBSD < 5.0 the nbytes to send also included 9599c64cfe5SGleb Smirnoff * the header. If compat is specified subtract the 9609c64cfe5SGleb Smirnoff * header size from nbytes. 9619c64cfe5SGleb Smirnoff */ 9629c64cfe5SGleb Smirnoff if (compat) { 9639c64cfe5SGleb Smirnoff if (uap->nbytes > hdr_uio->uio_resid) 9649c64cfe5SGleb Smirnoff uap->nbytes -= hdr_uio->uio_resid; 9659c64cfe5SGleb Smirnoff else 9669c64cfe5SGleb Smirnoff uap->nbytes = 0; 9679c64cfe5SGleb Smirnoff } 9689c64cfe5SGleb Smirnoff #endif 96933a2a37bSGleb Smirnoff } 97033a2a37bSGleb Smirnoff if (hdtr.trailers != NULL) { 97133a2a37bSGleb Smirnoff error = copyinuio(hdtr.trailers, hdtr.trl_cnt, 97233a2a37bSGleb Smirnoff &trl_uio); 97333a2a37bSGleb Smirnoff if (error != 0) 97433a2a37bSGleb Smirnoff goto out; 97533a2a37bSGleb Smirnoff } 97633a2a37bSGleb Smirnoff } 97733a2a37bSGleb Smirnoff 97833a2a37bSGleb Smirnoff AUDIT_ARG_FD(uap->fd); 97933a2a37bSGleb Smirnoff 98033a2a37bSGleb Smirnoff /* 98133a2a37bSGleb Smirnoff * sendfile(2) can start at any offset within a file so we require 98233a2a37bSGleb Smirnoff * CAP_READ+CAP_SEEK = CAP_PREAD. 98333a2a37bSGleb Smirnoff */ 98433a2a37bSGleb Smirnoff if ((error = fget_read(td, uap->fd, 98533a2a37bSGleb Smirnoff cap_rights_init(&rights, CAP_PREAD), &fp)) != 0) { 98633a2a37bSGleb Smirnoff goto out; 98733a2a37bSGleb Smirnoff } 98833a2a37bSGleb Smirnoff 98933a2a37bSGleb Smirnoff error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset, 9909c64cfe5SGleb Smirnoff uap->nbytes, &sbytes, uap->flags, td); 99133a2a37bSGleb Smirnoff fdrop(fp, td); 99233a2a37bSGleb Smirnoff 99333a2a37bSGleb Smirnoff if (uap->sbytes != NULL) 99433a2a37bSGleb Smirnoff copyout(&sbytes, uap->sbytes, sizeof(off_t)); 99533a2a37bSGleb Smirnoff 99633a2a37bSGleb Smirnoff out: 99733a2a37bSGleb Smirnoff free(hdr_uio, M_IOV); 99833a2a37bSGleb Smirnoff free(trl_uio, M_IOV); 99933a2a37bSGleb Smirnoff return (error); 100033a2a37bSGleb Smirnoff } 100133a2a37bSGleb Smirnoff 100233a2a37bSGleb Smirnoff /* 100333a2a37bSGleb Smirnoff * sendfile(2) 100433a2a37bSGleb Smirnoff * 100533a2a37bSGleb Smirnoff * int sendfile(int fd, int s, off_t offset, size_t nbytes, 100633a2a37bSGleb Smirnoff * struct sf_hdtr *hdtr, off_t *sbytes, int flags) 100733a2a37bSGleb Smirnoff * 100833a2a37bSGleb Smirnoff * Send a file specified by 'fd' and starting at 'offset' to a socket 100933a2a37bSGleb Smirnoff * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes == 101033a2a37bSGleb Smirnoff * 0. Optionally add a header and/or trailer to the socket output. If 101133a2a37bSGleb Smirnoff * specified, write the total number of bytes sent into *sbytes. 101233a2a37bSGleb Smirnoff */ 101333a2a37bSGleb Smirnoff int 101433a2a37bSGleb Smirnoff sys_sendfile(struct thread *td, struct sendfile_args *uap) 101533a2a37bSGleb Smirnoff { 101633a2a37bSGleb Smirnoff 101733a2a37bSGleb Smirnoff return (sendfile(td, uap, 0)); 101833a2a37bSGleb Smirnoff } 101933a2a37bSGleb Smirnoff 102033a2a37bSGleb Smirnoff #ifdef COMPAT_FREEBSD4 102133a2a37bSGleb Smirnoff int 102233a2a37bSGleb Smirnoff freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap) 102333a2a37bSGleb Smirnoff { 102433a2a37bSGleb Smirnoff struct sendfile_args args; 102533a2a37bSGleb Smirnoff 102633a2a37bSGleb Smirnoff args.fd = uap->fd; 102733a2a37bSGleb Smirnoff args.s = uap->s; 102833a2a37bSGleb Smirnoff args.offset = uap->offset; 102933a2a37bSGleb Smirnoff args.nbytes = uap->nbytes; 103033a2a37bSGleb Smirnoff args.hdtr = uap->hdtr; 103133a2a37bSGleb Smirnoff args.sbytes = uap->sbytes; 103233a2a37bSGleb Smirnoff args.flags = uap->flags; 103333a2a37bSGleb Smirnoff 103433a2a37bSGleb Smirnoff return (sendfile(td, &args, 1)); 103533a2a37bSGleb Smirnoff } 103633a2a37bSGleb Smirnoff #endif /* COMPAT_FREEBSD4 */ 1037