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 33b2e60773SJohn Baldwin #include "opt_kern_tls.h" 34b2e60773SJohn Baldwin 3533a2a37bSGleb Smirnoff #include <sys/param.h> 3633a2a37bSGleb Smirnoff #include <sys/systm.h> 3733a2a37bSGleb Smirnoff #include <sys/capsicum.h> 3833a2a37bSGleb Smirnoff #include <sys/kernel.h> 39cec06a3eSJohn Baldwin #include <netinet/in.h> 4033a2a37bSGleb Smirnoff #include <sys/lock.h> 41b2e60773SJohn Baldwin #include <sys/ktls.h> 4233a2a37bSGleb Smirnoff #include <sys/mutex.h> 4333a2a37bSGleb Smirnoff #include <sys/sysproto.h> 4433a2a37bSGleb Smirnoff #include <sys/malloc.h> 4533a2a37bSGleb Smirnoff #include <sys/proc.h> 4633a2a37bSGleb Smirnoff #include <sys/mman.h> 4733a2a37bSGleb Smirnoff #include <sys/mount.h> 4833a2a37bSGleb Smirnoff #include <sys/mbuf.h> 4933a2a37bSGleb Smirnoff #include <sys/protosw.h> 5033a2a37bSGleb Smirnoff #include <sys/rwlock.h> 5133a2a37bSGleb Smirnoff #include <sys/sf_buf.h> 5233a2a37bSGleb Smirnoff #include <sys/socket.h> 5333a2a37bSGleb Smirnoff #include <sys/socketvar.h> 5433a2a37bSGleb Smirnoff #include <sys/syscallsubr.h> 5533a2a37bSGleb Smirnoff #include <sys/sysctl.h> 5633a2a37bSGleb Smirnoff #include <sys/vnode.h> 5733a2a37bSGleb Smirnoff 5833a2a37bSGleb Smirnoff #include <net/vnet.h> 599e14430dSJohn Baldwin #include <netinet/tcp.h> 6033a2a37bSGleb Smirnoff 6133a2a37bSGleb Smirnoff #include <security/audit/audit.h> 6233a2a37bSGleb Smirnoff #include <security/mac/mac_framework.h> 6333a2a37bSGleb Smirnoff 6433a2a37bSGleb Smirnoff #include <vm/vm.h> 6533a2a37bSGleb Smirnoff #include <vm/vm_object.h> 6633a2a37bSGleb Smirnoff #include <vm/vm_pager.h> 6733a2a37bSGleb Smirnoff 689c82bec4SGleb Smirnoff #define EXT_FLAG_SYNC EXT_FLAG_VENDOR1 699c82bec4SGleb Smirnoff #define EXT_FLAG_NOCACHE EXT_FLAG_VENDOR2 70cec06a3eSJohn Baldwin #define EXT_FLAG_CACHE_LAST EXT_FLAG_VENDOR3 719c82bec4SGleb Smirnoff 7233a2a37bSGleb Smirnoff /* 7333a2a37bSGleb Smirnoff * Structure describing a single sendfile(2) I/O, which may consist of 7433a2a37bSGleb Smirnoff * several underlying pager I/Os. 7533a2a37bSGleb Smirnoff * 7633a2a37bSGleb Smirnoff * The syscall context allocates the structure and initializes 'nios' 7733a2a37bSGleb Smirnoff * to 1. As sendfile_swapin() runs through pages and starts asynchronous 7833a2a37bSGleb Smirnoff * paging operations, it increments 'nios'. 7933a2a37bSGleb Smirnoff * 8033a2a37bSGleb Smirnoff * Every I/O completion calls sendfile_iodone(), which decrements the 'nios', 8133a2a37bSGleb Smirnoff * and the syscall also calls sendfile_iodone() after allocating all mbufs, 8233a2a37bSGleb Smirnoff * linking them and sending to socket. Whoever reaches zero 'nios' is 8333a2a37bSGleb Smirnoff * responsible to * call pru_ready on the socket, to notify it of readyness 8433a2a37bSGleb Smirnoff * of the data. 8533a2a37bSGleb Smirnoff */ 8633a2a37bSGleb Smirnoff struct sf_io { 8733a2a37bSGleb Smirnoff volatile u_int nios; 8833a2a37bSGleb Smirnoff u_int error; 8933a2a37bSGleb Smirnoff int npages; 90d37aa3ccSGleb Smirnoff struct socket *so; 9133a2a37bSGleb Smirnoff struct mbuf *m; 92d6e13f3bSJeff Roberson vm_object_t obj; 93818d7553SJohn Baldwin #ifdef KERN_TLS 94b2e60773SJohn Baldwin struct ktls_session *tls; 95818d7553SJohn Baldwin #endif 9633a2a37bSGleb Smirnoff vm_page_t pa[]; 9733a2a37bSGleb Smirnoff }; 9833a2a37bSGleb Smirnoff 9933a2a37bSGleb Smirnoff /* 10033a2a37bSGleb Smirnoff * Structure used to track requests with SF_SYNC flag. 10133a2a37bSGleb Smirnoff */ 10233a2a37bSGleb Smirnoff struct sendfile_sync { 10333a2a37bSGleb Smirnoff struct mtx mtx; 10433a2a37bSGleb Smirnoff struct cv cv; 10533a2a37bSGleb Smirnoff unsigned count; 10633a2a37bSGleb Smirnoff }; 10733a2a37bSGleb Smirnoff 10833a2a37bSGleb Smirnoff counter_u64_t sfstat[sizeof(struct sfstat) / sizeof(uint64_t)]; 10933a2a37bSGleb Smirnoff 11033a2a37bSGleb Smirnoff static void 11133a2a37bSGleb Smirnoff sfstat_init(const void *unused) 11233a2a37bSGleb Smirnoff { 11333a2a37bSGleb Smirnoff 11433a2a37bSGleb Smirnoff COUNTER_ARRAY_ALLOC(sfstat, sizeof(struct sfstat) / sizeof(uint64_t), 11533a2a37bSGleb Smirnoff M_WAITOK); 11633a2a37bSGleb Smirnoff } 11733a2a37bSGleb Smirnoff SYSINIT(sfstat, SI_SUB_MBUF, SI_ORDER_FIRST, sfstat_init, NULL); 11833a2a37bSGleb Smirnoff 11933a2a37bSGleb Smirnoff static int 12033a2a37bSGleb Smirnoff sfstat_sysctl(SYSCTL_HANDLER_ARGS) 12133a2a37bSGleb Smirnoff { 12233a2a37bSGleb Smirnoff struct sfstat s; 12333a2a37bSGleb Smirnoff 12433a2a37bSGleb Smirnoff COUNTER_ARRAY_COPY(sfstat, &s, sizeof(s) / sizeof(uint64_t)); 12533a2a37bSGleb Smirnoff if (req->newptr) 12633a2a37bSGleb Smirnoff COUNTER_ARRAY_ZERO(sfstat, sizeof(s) / sizeof(uint64_t)); 12733a2a37bSGleb Smirnoff return (SYSCTL_OUT(req, &s, sizeof(s))); 12833a2a37bSGleb Smirnoff } 1297029da5cSPawel Biernacki SYSCTL_PROC(_kern_ipc, OID_AUTO, sfstat, 1307029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0, 1317029da5cSPawel Biernacki sfstat_sysctl, "I", 1327029da5cSPawel Biernacki "sendfile statistics"); 13333a2a37bSGleb Smirnoff 1349c82bec4SGleb Smirnoff static void 1359c82bec4SGleb Smirnoff sendfile_free_mext(struct mbuf *m) 1369c82bec4SGleb Smirnoff { 1379c82bec4SGleb Smirnoff struct sf_buf *sf; 1389c82bec4SGleb Smirnoff vm_page_t pg; 13998549e2dSMark Johnston int flags; 1409c82bec4SGleb Smirnoff 1419c82bec4SGleb Smirnoff KASSERT(m->m_flags & M_EXT && m->m_ext.ext_type == EXT_SFBUF, 1429c82bec4SGleb Smirnoff ("%s: m %p !M_EXT or !EXT_SFBUF", __func__, m)); 1439c82bec4SGleb Smirnoff 1449c82bec4SGleb Smirnoff sf = m->m_ext.ext_arg1; 1459c82bec4SGleb Smirnoff pg = sf_buf_page(sf); 14698549e2dSMark Johnston flags = (m->m_ext.ext_flags & EXT_FLAG_NOCACHE) != 0 ? VPR_TRYFREE : 0; 1479c82bec4SGleb Smirnoff 1489c82bec4SGleb Smirnoff sf_buf_free(sf); 14998549e2dSMark Johnston vm_page_release(pg, flags); 1509c82bec4SGleb Smirnoff 1519c82bec4SGleb Smirnoff if (m->m_ext.ext_flags & EXT_FLAG_SYNC) { 1529c82bec4SGleb Smirnoff struct sendfile_sync *sfs = m->m_ext.ext_arg2; 1539c82bec4SGleb Smirnoff 15433a2a37bSGleb Smirnoff mtx_lock(&sfs->mtx); 15533a2a37bSGleb Smirnoff KASSERT(sfs->count > 0, ("Sendfile sync botchup count == 0")); 15633a2a37bSGleb Smirnoff if (--sfs->count == 0) 15733a2a37bSGleb Smirnoff cv_signal(&sfs->cv); 15833a2a37bSGleb Smirnoff mtx_unlock(&sfs->mtx); 15933a2a37bSGleb Smirnoff } 16033a2a37bSGleb Smirnoff } 16133a2a37bSGleb Smirnoff 162cec06a3eSJohn Baldwin static void 163cec06a3eSJohn Baldwin sendfile_free_mext_pg(struct mbuf *m) 164cec06a3eSJohn Baldwin { 165cec06a3eSJohn Baldwin struct mbuf_ext_pgs *ext_pgs; 166cec06a3eSJohn Baldwin vm_page_t pg; 16798549e2dSMark Johnston int flags, i; 16898549e2dSMark Johnston bool cache_last; 169cec06a3eSJohn Baldwin 170cec06a3eSJohn Baldwin KASSERT(m->m_flags & M_EXT && m->m_ext.ext_type == EXT_PGS, 171cec06a3eSJohn Baldwin ("%s: m %p !M_EXT or !EXT_PGS", __func__, m)); 172cec06a3eSJohn Baldwin 173cec06a3eSJohn Baldwin cache_last = m->m_ext.ext_flags & EXT_FLAG_CACHE_LAST; 174cec06a3eSJohn Baldwin ext_pgs = m->m_ext.ext_pgs; 17598549e2dSMark Johnston flags = (m->m_ext.ext_flags & EXT_FLAG_NOCACHE) != 0 ? VPR_TRYFREE : 0; 176cec06a3eSJohn Baldwin 177cec06a3eSJohn Baldwin for (i = 0; i < ext_pgs->npgs; i++) { 178cec06a3eSJohn Baldwin if (cache_last && i == ext_pgs->npgs - 1) 17998549e2dSMark Johnston flags = 0; 180cec06a3eSJohn Baldwin pg = PHYS_TO_VM_PAGE(ext_pgs->pa[i]); 18198549e2dSMark Johnston vm_page_release(pg, flags); 182cec06a3eSJohn Baldwin } 183cec06a3eSJohn Baldwin 184cec06a3eSJohn Baldwin if (m->m_ext.ext_flags & EXT_FLAG_SYNC) { 185cec06a3eSJohn Baldwin struct sendfile_sync *sfs = m->m_ext.ext_arg2; 186cec06a3eSJohn Baldwin 187cec06a3eSJohn Baldwin mtx_lock(&sfs->mtx); 188cec06a3eSJohn Baldwin KASSERT(sfs->count > 0, ("Sendfile sync botchup count == 0")); 189cec06a3eSJohn Baldwin if (--sfs->count == 0) 190cec06a3eSJohn Baldwin cv_signal(&sfs->cv); 191cec06a3eSJohn Baldwin mtx_unlock(&sfs->mtx); 192cec06a3eSJohn Baldwin } 193cec06a3eSJohn Baldwin } 194cec06a3eSJohn Baldwin 19533a2a37bSGleb Smirnoff /* 19633a2a37bSGleb Smirnoff * Helper function to calculate how much data to put into page i of n. 19733a2a37bSGleb Smirnoff * Only first and last pages are special. 19833a2a37bSGleb Smirnoff */ 19933a2a37bSGleb Smirnoff static inline off_t 20033a2a37bSGleb Smirnoff xfsize(int i, int n, off_t off, off_t len) 20133a2a37bSGleb Smirnoff { 20233a2a37bSGleb Smirnoff 20333a2a37bSGleb Smirnoff if (i == 0) 20433a2a37bSGleb Smirnoff return (omin(PAGE_SIZE - (off & PAGE_MASK), len)); 20533a2a37bSGleb Smirnoff 20633a2a37bSGleb Smirnoff if (i == n - 1 && ((off + len) & PAGE_MASK) > 0) 20733a2a37bSGleb Smirnoff return ((off + len) & PAGE_MASK); 20833a2a37bSGleb Smirnoff 20933a2a37bSGleb Smirnoff return (PAGE_SIZE); 21033a2a37bSGleb Smirnoff } 21133a2a37bSGleb Smirnoff 21233a2a37bSGleb Smirnoff /* 21333a2a37bSGleb Smirnoff * Helper function to get offset within object for i page. 21433a2a37bSGleb Smirnoff */ 215d712b799SAlan Cox static inline vm_ooffset_t 21633a2a37bSGleb Smirnoff vmoff(int i, off_t off) 21733a2a37bSGleb Smirnoff { 21833a2a37bSGleb Smirnoff 21933a2a37bSGleb Smirnoff if (i == 0) 220d712b799SAlan Cox return ((vm_ooffset_t)off); 22133a2a37bSGleb Smirnoff 22233a2a37bSGleb Smirnoff return (trunc_page(off + i * PAGE_SIZE)); 22333a2a37bSGleb Smirnoff } 22433a2a37bSGleb Smirnoff 22533a2a37bSGleb Smirnoff /* 22633a2a37bSGleb Smirnoff * Helper function used when allocation of a page or sf_buf failed. 22733a2a37bSGleb Smirnoff * Pretend as if we don't have enough space, subtract xfsize() of 22833a2a37bSGleb Smirnoff * all pages that failed. 22933a2a37bSGleb Smirnoff */ 23033a2a37bSGleb Smirnoff static inline void 23133a2a37bSGleb Smirnoff fixspace(int old, int new, off_t off, int *space) 23233a2a37bSGleb Smirnoff { 23333a2a37bSGleb Smirnoff 23433a2a37bSGleb Smirnoff KASSERT(old > new, ("%s: old %d new %d", __func__, old, new)); 23533a2a37bSGleb Smirnoff 23633a2a37bSGleb Smirnoff /* Subtract last one. */ 23733a2a37bSGleb Smirnoff *space -= xfsize(old - 1, old, off, *space); 23833a2a37bSGleb Smirnoff old--; 23933a2a37bSGleb Smirnoff 24033a2a37bSGleb Smirnoff if (new == old) 24133a2a37bSGleb Smirnoff /* There was only one page. */ 24233a2a37bSGleb Smirnoff return; 24333a2a37bSGleb Smirnoff 24433a2a37bSGleb Smirnoff /* Subtract first one. */ 24533a2a37bSGleb Smirnoff if (new == 0) { 24633a2a37bSGleb Smirnoff *space -= xfsize(0, old, off, *space); 24733a2a37bSGleb Smirnoff new++; 24833a2a37bSGleb Smirnoff } 24933a2a37bSGleb Smirnoff 25033a2a37bSGleb Smirnoff /* Rest of pages are full sized. */ 25133a2a37bSGleb Smirnoff *space -= (old - new) * PAGE_SIZE; 25233a2a37bSGleb Smirnoff 25333a2a37bSGleb Smirnoff KASSERT(*space >= 0, ("%s: space went backwards", __func__)); 25433a2a37bSGleb Smirnoff } 25533a2a37bSGleb Smirnoff 25633a2a37bSGleb Smirnoff /* 25733a2a37bSGleb Smirnoff * I/O completion callback. 25833a2a37bSGleb Smirnoff */ 25933a2a37bSGleb Smirnoff static void 26033a2a37bSGleb Smirnoff sendfile_iodone(void *arg, vm_page_t *pg, int count, int error) 26133a2a37bSGleb Smirnoff { 26233a2a37bSGleb Smirnoff struct sf_io *sfio = arg; 2636bc27f08SGleb Smirnoff struct socket *so; 26433a2a37bSGleb Smirnoff 26533a2a37bSGleb Smirnoff for (int i = 0; i < count; i++) 2665dba303dSGleb Smirnoff if (pg[i] != bogus_page) 267b631c36fSKonstantin Belousov vm_page_xunbusy_unchecked(pg[i]); 26833a2a37bSGleb Smirnoff 26933a2a37bSGleb Smirnoff if (error) 27033a2a37bSGleb Smirnoff sfio->error = error; 27133a2a37bSGleb Smirnoff 27233a2a37bSGleb Smirnoff if (!refcount_release(&sfio->nios)) 27333a2a37bSGleb Smirnoff return; 27433a2a37bSGleb Smirnoff 275d6e13f3bSJeff Roberson vm_object_pip_wakeup(sfio->obj); 276d6e13f3bSJeff Roberson 2776bc27f08SGleb Smirnoff if (sfio->m == NULL) { 278b8c92303SGleb Smirnoff /* 2796bc27f08SGleb Smirnoff * Either I/O operation failed, or we failed to allocate 2806bc27f08SGleb Smirnoff * buffers, or we bailed out on first busy page, or we 2816bc27f08SGleb Smirnoff * succeeded filling the request without any I/Os. Anyway, 2826bc27f08SGleb Smirnoff * pru_send hadn't been executed - nothing had been sent 2836bc27f08SGleb Smirnoff * to the socket yet. 284b8c92303SGleb Smirnoff */ 2856bc27f08SGleb Smirnoff MPASS((curthread->td_pflags & TDP_KTHREAD) == 0); 286b8c92303SGleb Smirnoff free(sfio, M_TEMP); 287b8c92303SGleb Smirnoff return; 288b8c92303SGleb Smirnoff } 289b8c92303SGleb Smirnoff 290818d7553SJohn Baldwin #if defined(KERN_TLS) && defined(INVARIANTS) 291b2e60773SJohn Baldwin if ((sfio->m->m_flags & M_EXT) != 0 && 292b2e60773SJohn Baldwin sfio->m->m_ext.ext_type == EXT_PGS) 293b2e60773SJohn Baldwin KASSERT(sfio->tls == sfio->m->m_ext.ext_pgs->tls, 294b2e60773SJohn Baldwin ("TLS session mismatch")); 295b2e60773SJohn Baldwin else 296b2e60773SJohn Baldwin KASSERT(sfio->tls == NULL, 297b2e60773SJohn Baldwin ("non-ext_pgs mbuf with TLS session")); 298b2e60773SJohn Baldwin #endif 2996bc27f08SGleb Smirnoff so = sfio->so; 300b4f55763SGleb Smirnoff CURVNET_SET(so->so_vnet); 301b8c92303SGleb Smirnoff if (__predict_false(sfio->error)) { 30233a2a37bSGleb Smirnoff /* 30333a2a37bSGleb Smirnoff * I/O operation failed. The state of data in the socket 30433a2a37bSGleb Smirnoff * is now inconsistent, and all what we can do is to tear 30533a2a37bSGleb Smirnoff * it down. Protocol abort method would tear down protocol 30633a2a37bSGleb Smirnoff * state, free all ready mbufs and detach not ready ones. 30733a2a37bSGleb Smirnoff * We will free the mbufs corresponding to this I/O manually. 30833a2a37bSGleb Smirnoff * 30933a2a37bSGleb Smirnoff * The socket would be marked with EIO and made available 31033a2a37bSGleb Smirnoff * for read, so that application receives EIO on next 31133a2a37bSGleb Smirnoff * syscall and eventually closes the socket. 31233a2a37bSGleb Smirnoff */ 31333a2a37bSGleb Smirnoff so->so_proto->pr_usrreqs->pru_abort(so); 31433a2a37bSGleb Smirnoff so->so_error = EIO; 31533a2a37bSGleb Smirnoff 316cec06a3eSJohn Baldwin mb_free_notready(sfio->m, sfio->npages); 317b2e60773SJohn Baldwin #ifdef KERN_TLS 3189e14430dSJohn Baldwin } else if (sfio->tls != NULL && sfio->tls->mode == TCP_TLS_MODE_SW) { 319b2e60773SJohn Baldwin /* 320b2e60773SJohn Baldwin * I/O operation is complete, but we still need to 321b2e60773SJohn Baldwin * encrypt. We cannot do this in the interrupt thread 322b2e60773SJohn Baldwin * of the disk controller, so forward the mbufs to a 323b2e60773SJohn Baldwin * different thread. 324b2e60773SJohn Baldwin * 325b2e60773SJohn Baldwin * Donate the socket reference from sfio to rather 326b2e60773SJohn Baldwin * than explicitly invoking soref(). 327b2e60773SJohn Baldwin */ 328b2e60773SJohn Baldwin ktls_enqueue(sfio->m, so, sfio->npages); 329b2e60773SJohn Baldwin goto out_with_ref; 330b2e60773SJohn Baldwin #endif 331b4f55763SGleb Smirnoff } else 33233a2a37bSGleb Smirnoff (void)(so->so_proto->pr_usrreqs->pru_ready)(so, sfio->m, 33333a2a37bSGleb Smirnoff sfio->npages); 33433a2a37bSGleb Smirnoff 335d37aa3ccSGleb Smirnoff SOCK_LOCK(so); 336d37aa3ccSGleb Smirnoff sorele(so); 337b2e60773SJohn Baldwin #ifdef KERN_TLS 338b2e60773SJohn Baldwin out_with_ref: 339b2e60773SJohn Baldwin #endif 340b4f55763SGleb Smirnoff CURVNET_RESTORE(); 34133a2a37bSGleb Smirnoff free(sfio, M_TEMP); 34233a2a37bSGleb Smirnoff } 34333a2a37bSGleb Smirnoff 34433a2a37bSGleb Smirnoff /* 34533a2a37bSGleb Smirnoff * Iterate through pages vector and request paging for non-valid pages. 34633a2a37bSGleb Smirnoff */ 34733a2a37bSGleb Smirnoff static int 348fca79580SAlan Somers sendfile_swapin(vm_object_t obj, struct sf_io *sfio, int *nios, off_t off, 349fca79580SAlan Somers off_t len, int npages, int rhpages, int flags) 35033a2a37bSGleb Smirnoff { 35133a2a37bSGleb Smirnoff vm_page_t *pa = sfio->pa; 352fca79580SAlan Somers int grabbed; 353*7aaf252cSJeff Roberson bool locked; 35433a2a37bSGleb Smirnoff 355fca79580SAlan Somers *nios = 0; 35633a2a37bSGleb Smirnoff flags = (flags & SF_NODISKIO) ? VM_ALLOC_NOWAIT : 0; 35733a2a37bSGleb Smirnoff 35833a2a37bSGleb Smirnoff /* 35933a2a37bSGleb Smirnoff * First grab all the pages and wire them. Note that we grab 36033a2a37bSGleb Smirnoff * only required pages. Readahead pages are dealt with later. 36133a2a37bSGleb Smirnoff */ 362*7aaf252cSJeff Roberson locked = false; 363af0460beSMark Johnston 364*7aaf252cSJeff Roberson grabbed = vm_page_grab_pages_unlocked(obj, OFF_TO_IDX(off), 365af0460beSMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_WIRED | flags, pa, npages); 366af0460beSMark Johnston if (grabbed < npages) { 367af0460beSMark Johnston for (int i = grabbed; i < npages; i++) 368af0460beSMark Johnston pa[i] = NULL; 369af0460beSMark Johnston npages = grabbed; 37033a2a37bSGleb Smirnoff rhpages = 0; 37133a2a37bSGleb Smirnoff } 37233a2a37bSGleb Smirnoff 37333a2a37bSGleb Smirnoff for (int i = 0; i < npages;) { 374fca79580SAlan Somers int j, a, count, rv; 37533a2a37bSGleb Smirnoff 37633a2a37bSGleb Smirnoff /* Skip valid pages. */ 37733a2a37bSGleb Smirnoff if (vm_page_is_valid(pa[i], vmoff(i, off) & PAGE_MASK, 37833a2a37bSGleb Smirnoff xfsize(i, npages, off, len))) { 37933a2a37bSGleb Smirnoff vm_page_xunbusy(pa[i]); 38033a2a37bSGleb Smirnoff SFSTAT_INC(sf_pages_valid); 38133a2a37bSGleb Smirnoff i++; 38233a2a37bSGleb Smirnoff continue; 38333a2a37bSGleb Smirnoff } 384*7aaf252cSJeff Roberson if (!locked) { 385*7aaf252cSJeff Roberson VM_OBJECT_WLOCK(obj); 386*7aaf252cSJeff Roberson locked = true; 387*7aaf252cSJeff Roberson } 38833a2a37bSGleb Smirnoff 38933a2a37bSGleb Smirnoff /* 3905dba303dSGleb Smirnoff * Next page is invalid. Check if it belongs to pager. It 3915dba303dSGleb Smirnoff * may not be there, which is a regular situation for shmem 3925dba303dSGleb Smirnoff * pager. For vnode pager this happens only in case of 3935dba303dSGleb Smirnoff * a sparse file. 39433a2a37bSGleb Smirnoff * 39533a2a37bSGleb Smirnoff * Important feature of vm_pager_has_page() is the hint 39633a2a37bSGleb Smirnoff * stored in 'a', about how many pages we can pagein after 39733a2a37bSGleb Smirnoff * this page in a single I/O. 39833a2a37bSGleb Smirnoff */ 3995dba303dSGleb Smirnoff if (!vm_pager_has_page(obj, OFF_TO_IDX(vmoff(i, off)), NULL, 4005dba303dSGleb Smirnoff &a)) { 40133a2a37bSGleb Smirnoff pmap_zero_page(pa[i]); 40291e31c3cSJeff Roberson vm_page_valid(pa[i]); 4036921451dSAlan Cox MPASS(pa[i]->dirty == 0); 40433a2a37bSGleb Smirnoff vm_page_xunbusy(pa[i]); 40533a2a37bSGleb Smirnoff i++; 40633a2a37bSGleb Smirnoff continue; 4075dba303dSGleb Smirnoff } 40833a2a37bSGleb Smirnoff 40933a2a37bSGleb Smirnoff /* 41033a2a37bSGleb Smirnoff * We want to pagein as many pages as possible, limited only 41133a2a37bSGleb Smirnoff * by the 'a' hint and actual request. 41233a2a37bSGleb Smirnoff */ 41333a2a37bSGleb Smirnoff count = min(a + 1, npages - i); 41433a2a37bSGleb Smirnoff 4155dba303dSGleb Smirnoff /* 4165dba303dSGleb Smirnoff * We should not pagein into a valid page, thus we first trim 4175dba303dSGleb Smirnoff * any valid pages off the end of request, and substitute 4185dba303dSGleb Smirnoff * to bogus_page those, that are in the middle. 4195dba303dSGleb Smirnoff */ 4205dba303dSGleb Smirnoff for (j = i + count - 1; j > i; j--) { 4215dba303dSGleb Smirnoff if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK, 4225dba303dSGleb Smirnoff xfsize(j, npages, off, len))) { 4235dba303dSGleb Smirnoff count--; 4245dba303dSGleb Smirnoff rhpages = 0; 4255dba303dSGleb Smirnoff } else 4265dba303dSGleb Smirnoff break; 4275dba303dSGleb Smirnoff } 4285dba303dSGleb Smirnoff for (j = i + 1; j < i + count - 1; j++) 4295dba303dSGleb Smirnoff if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK, 4305dba303dSGleb Smirnoff xfsize(j, npages, off, len))) { 4315dba303dSGleb Smirnoff vm_page_xunbusy(pa[j]); 4325dba303dSGleb Smirnoff SFSTAT_INC(sf_pages_valid); 4335dba303dSGleb Smirnoff SFSTAT_INC(sf_pages_bogus); 4345dba303dSGleb Smirnoff pa[j] = bogus_page; 4355dba303dSGleb Smirnoff } 4365dba303dSGleb Smirnoff 43733a2a37bSGleb Smirnoff refcount_acquire(&sfio->nios); 438d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(obj); 43933a2a37bSGleb Smirnoff rv = vm_pager_get_pages_async(obj, pa + i, count, NULL, 44033a2a37bSGleb Smirnoff i + count == npages ? &rhpages : NULL, 44133a2a37bSGleb Smirnoff &sendfile_iodone, sfio); 442d6e13f3bSJeff Roberson VM_OBJECT_WLOCK(obj); 443b8c92303SGleb Smirnoff if (__predict_false(rv != VM_PAGER_OK)) { 444b8c92303SGleb Smirnoff /* 445b8c92303SGleb Smirnoff * Perform full pages recovery before returning EIO. 446b8c92303SGleb Smirnoff * Pages from 0 to npages are wired. 447b8c92303SGleb Smirnoff * Pages from i to npages are also busied. 448b8c92303SGleb Smirnoff * Pages from (i + 1) to (i + count - 1) may be 449b8c92303SGleb Smirnoff * substituted to bogus page, and not busied. 450b8c92303SGleb Smirnoff */ 451b8c92303SGleb Smirnoff for (j = 0; j < npages; j++) { 452b8c92303SGleb Smirnoff if (j > i && j < i + count - 1 && 453b8c92303SGleb Smirnoff pa[j] == bogus_page) 454b8c92303SGleb Smirnoff pa[j] = vm_page_lookup(obj, 455b8c92303SGleb Smirnoff OFF_TO_IDX(vmoff(j, off))); 456b8c92303SGleb Smirnoff else if (j >= i) 457b8c92303SGleb Smirnoff vm_page_xunbusy(pa[j]); 458b8c92303SGleb Smirnoff KASSERT(pa[j] != NULL && pa[j] != bogus_page, 459b8c92303SGleb Smirnoff ("%s: page %p[%d] I/O recovery failure", 460b8c92303SGleb Smirnoff __func__, pa, j)); 4610367bca4SAlan Somers vm_page_unwire(pa[j], PQ_INACTIVE); 462fca79580SAlan Somers } 463fca79580SAlan Somers VM_OBJECT_WUNLOCK(obj); 4640367bca4SAlan Somers return (EIO); 465fca79580SAlan Somers } 46633a2a37bSGleb Smirnoff 46733a2a37bSGleb Smirnoff SFSTAT_INC(sf_iocnt); 46833a2a37bSGleb Smirnoff SFSTAT_ADD(sf_pages_read, count); 46933a2a37bSGleb Smirnoff if (i + count == npages) 47033a2a37bSGleb Smirnoff SFSTAT_ADD(sf_rhpages_read, rhpages); 47133a2a37bSGleb Smirnoff 4725dba303dSGleb Smirnoff /* 4735dba303dSGleb Smirnoff * Restore the valid page pointers. They are already 4745dba303dSGleb Smirnoff * unbusied, but still wired. 4755dba303dSGleb Smirnoff */ 47669302907SGleb Smirnoff for (j = i + 1; j < i + count - 1; j++) 4775dba303dSGleb Smirnoff if (pa[j] == bogus_page) { 4785dba303dSGleb Smirnoff pa[j] = vm_page_lookup(obj, 4795dba303dSGleb Smirnoff OFF_TO_IDX(vmoff(j, off))); 4805dba303dSGleb Smirnoff KASSERT(pa[j], ("%s: page %p[%d] disappeared", 4815dba303dSGleb Smirnoff __func__, pa, j)); 4825dba303dSGleb Smirnoff 4835dba303dSGleb Smirnoff } 48433a2a37bSGleb Smirnoff i += count; 485fca79580SAlan Somers (*nios)++; 48633a2a37bSGleb Smirnoff } 48733a2a37bSGleb Smirnoff 488*7aaf252cSJeff Roberson if (locked) 48933a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 49033a2a37bSGleb Smirnoff 491fca79580SAlan Somers if (*nios == 0 && npages != 0) 49233a2a37bSGleb Smirnoff SFSTAT_INC(sf_noiocnt); 49333a2a37bSGleb Smirnoff 494fca79580SAlan Somers return (0); 49533a2a37bSGleb Smirnoff } 49633a2a37bSGleb Smirnoff 49733a2a37bSGleb Smirnoff static int 49833a2a37bSGleb Smirnoff sendfile_getobj(struct thread *td, struct file *fp, vm_object_t *obj_res, 49933a2a37bSGleb Smirnoff struct vnode **vp_res, struct shmfd **shmfd_res, off_t *obj_size, 50033a2a37bSGleb Smirnoff int *bsize) 50133a2a37bSGleb Smirnoff { 50233a2a37bSGleb Smirnoff struct vattr va; 50333a2a37bSGleb Smirnoff vm_object_t obj; 50433a2a37bSGleb Smirnoff struct vnode *vp; 50533a2a37bSGleb Smirnoff struct shmfd *shmfd; 50633a2a37bSGleb Smirnoff int error; 50733a2a37bSGleb Smirnoff 50833a2a37bSGleb Smirnoff vp = *vp_res = NULL; 50933a2a37bSGleb Smirnoff obj = NULL; 51033a2a37bSGleb Smirnoff shmfd = *shmfd_res = NULL; 51133a2a37bSGleb Smirnoff *bsize = 0; 51233a2a37bSGleb Smirnoff 51333a2a37bSGleb Smirnoff /* 51433a2a37bSGleb Smirnoff * The file descriptor must be a regular file and have a 51533a2a37bSGleb Smirnoff * backing VM object. 51633a2a37bSGleb Smirnoff */ 51733a2a37bSGleb Smirnoff if (fp->f_type == DTYPE_VNODE) { 51833a2a37bSGleb Smirnoff vp = fp->f_vnode; 51933a2a37bSGleb Smirnoff vn_lock(vp, LK_SHARED | LK_RETRY); 52033a2a37bSGleb Smirnoff if (vp->v_type != VREG) { 52133a2a37bSGleb Smirnoff error = EINVAL; 52233a2a37bSGleb Smirnoff goto out; 52333a2a37bSGleb Smirnoff } 52433a2a37bSGleb Smirnoff *bsize = vp->v_mount->mnt_stat.f_iosize; 52533a2a37bSGleb Smirnoff error = VOP_GETATTR(vp, &va, td->td_ucred); 52633a2a37bSGleb Smirnoff if (error != 0) 52733a2a37bSGleb Smirnoff goto out; 52833a2a37bSGleb Smirnoff *obj_size = va.va_size; 52933a2a37bSGleb Smirnoff obj = vp->v_object; 53033a2a37bSGleb Smirnoff if (obj == NULL) { 53133a2a37bSGleb Smirnoff error = EINVAL; 53233a2a37bSGleb Smirnoff goto out; 53333a2a37bSGleb Smirnoff } 53433a2a37bSGleb Smirnoff } else if (fp->f_type == DTYPE_SHM) { 53533a2a37bSGleb Smirnoff error = 0; 53633a2a37bSGleb Smirnoff shmfd = fp->f_data; 53733a2a37bSGleb Smirnoff obj = shmfd->shm_object; 53833a2a37bSGleb Smirnoff *obj_size = shmfd->shm_size; 53933a2a37bSGleb Smirnoff } else { 54033a2a37bSGleb Smirnoff error = EINVAL; 54133a2a37bSGleb Smirnoff goto out; 54233a2a37bSGleb Smirnoff } 54333a2a37bSGleb Smirnoff 54433a2a37bSGleb Smirnoff VM_OBJECT_WLOCK(obj); 54533a2a37bSGleb Smirnoff if ((obj->flags & OBJ_DEAD) != 0) { 54633a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 54733a2a37bSGleb Smirnoff error = EBADF; 54833a2a37bSGleb Smirnoff goto out; 54933a2a37bSGleb Smirnoff } 55033a2a37bSGleb Smirnoff 55133a2a37bSGleb Smirnoff /* 55233a2a37bSGleb Smirnoff * Temporarily increase the backing VM object's reference 55333a2a37bSGleb Smirnoff * count so that a forced reclamation of its vnode does not 55433a2a37bSGleb Smirnoff * immediately destroy it. 55533a2a37bSGleb Smirnoff */ 55633a2a37bSGleb Smirnoff vm_object_reference_locked(obj); 55733a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 55833a2a37bSGleb Smirnoff *obj_res = obj; 55933a2a37bSGleb Smirnoff *vp_res = vp; 56033a2a37bSGleb Smirnoff *shmfd_res = shmfd; 56133a2a37bSGleb Smirnoff 56233a2a37bSGleb Smirnoff out: 56333a2a37bSGleb Smirnoff if (vp != NULL) 564b249ce48SMateusz Guzik VOP_UNLOCK(vp); 56533a2a37bSGleb Smirnoff return (error); 56633a2a37bSGleb Smirnoff } 56733a2a37bSGleb Smirnoff 56833a2a37bSGleb Smirnoff static int 56933a2a37bSGleb Smirnoff sendfile_getsock(struct thread *td, int s, struct file **sock_fp, 57033a2a37bSGleb Smirnoff struct socket **so) 57133a2a37bSGleb Smirnoff { 57233a2a37bSGleb Smirnoff int error; 57333a2a37bSGleb Smirnoff 57433a2a37bSGleb Smirnoff *sock_fp = NULL; 57533a2a37bSGleb Smirnoff *so = NULL; 57633a2a37bSGleb Smirnoff 57733a2a37bSGleb Smirnoff /* 57833a2a37bSGleb Smirnoff * The socket must be a stream socket and connected. 57933a2a37bSGleb Smirnoff */ 580cbd92ce6SMatt Macy error = getsock_cap(td, s, &cap_send_rights, 58185b0f9deSMariusz Zaborski sock_fp, NULL, NULL); 58233a2a37bSGleb Smirnoff if (error != 0) 58333a2a37bSGleb Smirnoff return (error); 58433a2a37bSGleb Smirnoff *so = (*sock_fp)->f_data; 58533a2a37bSGleb Smirnoff if ((*so)->so_type != SOCK_STREAM) 58633a2a37bSGleb Smirnoff return (EINVAL); 58747f1ea51SGleb Smirnoff if (SOLISTENING(*so)) 58847f1ea51SGleb Smirnoff return (ENOTCONN); 58933a2a37bSGleb Smirnoff return (0); 59033a2a37bSGleb Smirnoff } 59133a2a37bSGleb Smirnoff 59233a2a37bSGleb Smirnoff int 59333a2a37bSGleb Smirnoff vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, 59433a2a37bSGleb Smirnoff struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags, 5959c64cfe5SGleb Smirnoff struct thread *td) 59633a2a37bSGleb Smirnoff { 59733a2a37bSGleb Smirnoff struct file *sock_fp; 59833a2a37bSGleb Smirnoff struct vnode *vp; 59933a2a37bSGleb Smirnoff struct vm_object *obj; 60033a2a37bSGleb Smirnoff struct socket *so; 601b2e60773SJohn Baldwin #ifdef KERN_TLS 602b2e60773SJohn Baldwin struct ktls_session *tls; 603b2e60773SJohn Baldwin #endif 604cec06a3eSJohn Baldwin struct mbuf_ext_pgs *ext_pgs; 60533a2a37bSGleb Smirnoff struct mbuf *m, *mh, *mhtail; 60633a2a37bSGleb Smirnoff struct sf_buf *sf; 60733a2a37bSGleb Smirnoff struct shmfd *shmfd; 60833a2a37bSGleb Smirnoff struct sendfile_sync *sfs; 60933a2a37bSGleb Smirnoff struct vattr va; 61033a2a37bSGleb Smirnoff off_t off, sbytes, rem, obj_size; 611cec06a3eSJohn Baldwin int bsize, error, ext_pgs_idx, hdrlen, max_pgs, softerr; 612b2e60773SJohn Baldwin #ifdef KERN_TLS 613b2e60773SJohn Baldwin int tls_enq_cnt; 614b2e60773SJohn Baldwin #endif 615cec06a3eSJohn Baldwin bool use_ext_pgs; 61633a2a37bSGleb Smirnoff 61733a2a37bSGleb Smirnoff obj = NULL; 61833a2a37bSGleb Smirnoff so = NULL; 61933a2a37bSGleb Smirnoff m = mh = NULL; 62033a2a37bSGleb Smirnoff sfs = NULL; 621b2e60773SJohn Baldwin #ifdef KERN_TLS 622b2e60773SJohn Baldwin tls = NULL; 623b2e60773SJohn Baldwin #endif 6249c64cfe5SGleb Smirnoff hdrlen = sbytes = 0; 62533a2a37bSGleb Smirnoff softerr = 0; 626cec06a3eSJohn Baldwin use_ext_pgs = false; 62733a2a37bSGleb Smirnoff 62833a2a37bSGleb Smirnoff error = sendfile_getobj(td, fp, &obj, &vp, &shmfd, &obj_size, &bsize); 62933a2a37bSGleb Smirnoff if (error != 0) 63033a2a37bSGleb Smirnoff return (error); 63133a2a37bSGleb Smirnoff 63233a2a37bSGleb Smirnoff error = sendfile_getsock(td, sockfd, &sock_fp, &so); 63333a2a37bSGleb Smirnoff if (error != 0) 63433a2a37bSGleb Smirnoff goto out; 63533a2a37bSGleb Smirnoff 63633a2a37bSGleb Smirnoff #ifdef MAC 63733a2a37bSGleb Smirnoff error = mac_socket_check_send(td->td_ucred, so); 63833a2a37bSGleb Smirnoff if (error != 0) 63933a2a37bSGleb Smirnoff goto out; 64033a2a37bSGleb Smirnoff #endif 64133a2a37bSGleb Smirnoff 64233a2a37bSGleb Smirnoff SFSTAT_INC(sf_syscalls); 64333a2a37bSGleb Smirnoff SFSTAT_ADD(sf_rhpages_requested, SF_READAHEAD(flags)); 64433a2a37bSGleb Smirnoff 64533a2a37bSGleb Smirnoff if (flags & SF_SYNC) { 64633a2a37bSGleb Smirnoff sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO); 64733a2a37bSGleb Smirnoff mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF); 64833a2a37bSGleb Smirnoff cv_init(&sfs->cv, "sendfile"); 64933a2a37bSGleb Smirnoff } 65033a2a37bSGleb Smirnoff 65133a2a37bSGleb Smirnoff rem = nbytes ? omin(nbytes, obj_size - offset) : obj_size - offset; 65233a2a37bSGleb Smirnoff 65333a2a37bSGleb Smirnoff /* 65433a2a37bSGleb Smirnoff * Protect against multiple writers to the socket. 65533a2a37bSGleb Smirnoff * 65633a2a37bSGleb Smirnoff * XXXRW: Historically this has assumed non-interruptibility, so now 65733a2a37bSGleb Smirnoff * we implement that, but possibly shouldn't. 65833a2a37bSGleb Smirnoff */ 65933a2a37bSGleb Smirnoff (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR); 660b2e60773SJohn Baldwin #ifdef KERN_TLS 661b2e60773SJohn Baldwin tls = ktls_hold(so->so_snd.sb_tls_info); 662b2e60773SJohn Baldwin #endif 66333a2a37bSGleb Smirnoff 66433a2a37bSGleb Smirnoff /* 66533a2a37bSGleb Smirnoff * Loop through the pages of the file, starting with the requested 66633a2a37bSGleb Smirnoff * offset. Get a file page (do I/O if necessary), map the file page 66733a2a37bSGleb Smirnoff * into an sf_buf, attach an mbuf header to the sf_buf, and queue 66833a2a37bSGleb Smirnoff * it on the socket. 66933a2a37bSGleb Smirnoff * This is done in two loops. The inner loop turns as many pages 67033a2a37bSGleb Smirnoff * as it can, up to available socket buffer space, without blocking 67133a2a37bSGleb Smirnoff * into mbufs to have it bulk delivered into the socket send buffer. 67233a2a37bSGleb Smirnoff * The outer loop checks the state and available space of the socket 67333a2a37bSGleb Smirnoff * and takes care of the overall progress. 67433a2a37bSGleb Smirnoff */ 67533a2a37bSGleb Smirnoff for (off = offset; rem > 0; ) { 67633a2a37bSGleb Smirnoff struct sf_io *sfio; 67733a2a37bSGleb Smirnoff vm_page_t *pa; 6786bc27f08SGleb Smirnoff struct mbuf *m0, *mtail; 67933a2a37bSGleb Smirnoff int nios, space, npages, rhpages; 68033a2a37bSGleb Smirnoff 68133a2a37bSGleb Smirnoff mtail = NULL; 68233a2a37bSGleb Smirnoff /* 68333a2a37bSGleb Smirnoff * Check the socket state for ongoing connection, 68433a2a37bSGleb Smirnoff * no errors and space in socket buffer. 68533a2a37bSGleb Smirnoff * If space is low allow for the remainder of the 68633a2a37bSGleb Smirnoff * file to be processed if it fits the socket buffer. 68733a2a37bSGleb Smirnoff * Otherwise block in waiting for sufficient space 68833a2a37bSGleb Smirnoff * to proceed, or if the socket is nonblocking, return 68933a2a37bSGleb Smirnoff * to userland with EAGAIN while reporting how far 69033a2a37bSGleb Smirnoff * we've come. 69133a2a37bSGleb Smirnoff * We wait until the socket buffer has significant free 69233a2a37bSGleb Smirnoff * space to do bulk sends. This makes good use of file 69333a2a37bSGleb Smirnoff * system read ahead and allows packet segmentation 69433a2a37bSGleb Smirnoff * offloading hardware to take over lots of work. If 69533a2a37bSGleb Smirnoff * we were not careful here we would send off only one 69633a2a37bSGleb Smirnoff * sfbuf at a time. 69733a2a37bSGleb Smirnoff */ 69833a2a37bSGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 69933a2a37bSGleb Smirnoff if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2) 70033a2a37bSGleb Smirnoff so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2; 70133a2a37bSGleb Smirnoff retry_space: 70233a2a37bSGleb Smirnoff if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 70333a2a37bSGleb Smirnoff error = EPIPE; 70433a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 70533a2a37bSGleb Smirnoff goto done; 70633a2a37bSGleb Smirnoff } else if (so->so_error) { 70733a2a37bSGleb Smirnoff error = so->so_error; 70833a2a37bSGleb Smirnoff so->so_error = 0; 70933a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 71033a2a37bSGleb Smirnoff goto done; 71133a2a37bSGleb Smirnoff } 7121f9916edSSean Bruno if ((so->so_state & SS_ISCONNECTED) == 0) { 7131f9916edSSean Bruno SOCKBUF_UNLOCK(&so->so_snd); 7141f9916edSSean Bruno error = ENOTCONN; 7151f9916edSSean Bruno goto done; 7161f9916edSSean Bruno } 7171f9916edSSean Bruno 71833a2a37bSGleb Smirnoff space = sbspace(&so->so_snd); 71933a2a37bSGleb Smirnoff if (space < rem && 72033a2a37bSGleb Smirnoff (space <= 0 || 72133a2a37bSGleb Smirnoff space < so->so_snd.sb_lowat)) { 72233a2a37bSGleb Smirnoff if (so->so_state & SS_NBIO) { 72333a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 72433a2a37bSGleb Smirnoff error = EAGAIN; 72533a2a37bSGleb Smirnoff goto done; 72633a2a37bSGleb Smirnoff } 72733a2a37bSGleb Smirnoff /* 72833a2a37bSGleb Smirnoff * sbwait drops the lock while sleeping. 72933a2a37bSGleb Smirnoff * When we loop back to retry_space the 73033a2a37bSGleb Smirnoff * state may have changed and we retest 73133a2a37bSGleb Smirnoff * for it. 73233a2a37bSGleb Smirnoff */ 73333a2a37bSGleb Smirnoff error = sbwait(&so->so_snd); 73433a2a37bSGleb Smirnoff /* 73533a2a37bSGleb Smirnoff * An error from sbwait usually indicates that we've 73633a2a37bSGleb Smirnoff * been interrupted by a signal. If we've sent anything 73733a2a37bSGleb Smirnoff * then return bytes sent, otherwise return the error. 73833a2a37bSGleb Smirnoff */ 73933a2a37bSGleb Smirnoff if (error != 0) { 74033a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 74133a2a37bSGleb Smirnoff goto done; 74233a2a37bSGleb Smirnoff } 74333a2a37bSGleb Smirnoff goto retry_space; 74433a2a37bSGleb Smirnoff } 74533a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 74633a2a37bSGleb Smirnoff 74733a2a37bSGleb Smirnoff /* 7489c64cfe5SGleb Smirnoff * At the beginning of the first loop check if any headers 7499c64cfe5SGleb Smirnoff * are specified and copy them into mbufs. Reduce space in 7509c64cfe5SGleb Smirnoff * the socket buffer by the size of the header mbuf chain. 7519c64cfe5SGleb Smirnoff * Clear hdr_uio here and hdrlen at the end of the first loop. 75233a2a37bSGleb Smirnoff */ 7539c64cfe5SGleb Smirnoff if (hdr_uio != NULL && hdr_uio->uio_resid > 0) { 7549c64cfe5SGleb Smirnoff hdr_uio->uio_td = td; 7559c64cfe5SGleb Smirnoff hdr_uio->uio_rw = UIO_WRITE; 756b2e60773SJohn Baldwin #ifdef KERN_TLS 757b2e60773SJohn Baldwin if (tls != NULL) 758b2e60773SJohn Baldwin mh = m_uiotombuf(hdr_uio, M_WAITOK, space, 759b2e60773SJohn Baldwin tls->params.max_frame_len, M_NOMAP); 760b2e60773SJohn Baldwin else 761b2e60773SJohn Baldwin #endif 762b2e60773SJohn Baldwin mh = m_uiotombuf(hdr_uio, M_WAITOK, 763b2e60773SJohn Baldwin space, 0, 0); 7649c64cfe5SGleb Smirnoff hdrlen = m_length(mh, &mhtail); 76533a2a37bSGleb Smirnoff space -= hdrlen; 766a2d8f9d2SGleb Smirnoff /* 767a2d8f9d2SGleb Smirnoff * If header consumed all the socket buffer space, 768a2d8f9d2SGleb Smirnoff * don't waste CPU cycles and jump to the end. 769a2d8f9d2SGleb Smirnoff */ 770a2d8f9d2SGleb Smirnoff if (space == 0) { 771a2d8f9d2SGleb Smirnoff sfio = NULL; 772a2d8f9d2SGleb Smirnoff nios = 0; 773a2d8f9d2SGleb Smirnoff goto prepend_header; 774a2d8f9d2SGleb Smirnoff } 7759c64cfe5SGleb Smirnoff hdr_uio = NULL; 7769c64cfe5SGleb Smirnoff } 77733a2a37bSGleb Smirnoff 77833a2a37bSGleb Smirnoff if (vp != NULL) { 77933a2a37bSGleb Smirnoff error = vn_lock(vp, LK_SHARED); 78033a2a37bSGleb Smirnoff if (error != 0) 78133a2a37bSGleb Smirnoff goto done; 78233a2a37bSGleb Smirnoff error = VOP_GETATTR(vp, &va, td->td_ucred); 78333a2a37bSGleb Smirnoff if (error != 0 || off >= va.va_size) { 784b249ce48SMateusz Guzik VOP_UNLOCK(vp); 78533a2a37bSGleb Smirnoff goto done; 78633a2a37bSGleb Smirnoff } 78733a2a37bSGleb Smirnoff if (va.va_size != obj_size) { 78833a2a37bSGleb Smirnoff obj_size = va.va_size; 7899e3c8bd3SGleb Smirnoff rem = nbytes ? 7909e3c8bd3SGleb Smirnoff omin(nbytes + offset, obj_size) : obj_size; 7919e3c8bd3SGleb Smirnoff rem -= off; 79233a2a37bSGleb Smirnoff } 79333a2a37bSGleb Smirnoff } 79433a2a37bSGleb Smirnoff 79533a2a37bSGleb Smirnoff if (space > rem) 79633a2a37bSGleb Smirnoff space = rem; 797cec06a3eSJohn Baldwin else if (space > PAGE_SIZE) { 798cec06a3eSJohn Baldwin /* 799cec06a3eSJohn Baldwin * Use page boundaries when possible for large 800cec06a3eSJohn Baldwin * requests. 801cec06a3eSJohn Baldwin */ 802cec06a3eSJohn Baldwin if (off & PAGE_MASK) 803cec06a3eSJohn Baldwin space -= (PAGE_SIZE - (off & PAGE_MASK)); 804cec06a3eSJohn Baldwin space = trunc_page(space); 805cec06a3eSJohn Baldwin if (off & PAGE_MASK) 806cec06a3eSJohn Baldwin space += (PAGE_SIZE - (off & PAGE_MASK)); 807cec06a3eSJohn Baldwin } 80833a2a37bSGleb Smirnoff 80933a2a37bSGleb Smirnoff npages = howmany(space + (off & PAGE_MASK), PAGE_SIZE); 81033a2a37bSGleb Smirnoff 81133a2a37bSGleb Smirnoff /* 81233a2a37bSGleb Smirnoff * Calculate maximum allowed number of pages for readahead 81300b5ffdeSGleb Smirnoff * at this iteration. If SF_USER_READAHEAD was set, we don't 81400b5ffdeSGleb Smirnoff * do any heuristics and use exactly the value supplied by 81500b5ffdeSGleb Smirnoff * application. Otherwise, we allow readahead up to "rem". 81633a2a37bSGleb Smirnoff * If application wants more, let it be, but there is no 81733a2a37bSGleb Smirnoff * reason to go above MAXPHYS. Also check against "obj_size", 81833a2a37bSGleb Smirnoff * since vm_pager_has_page() can hint beyond EOF. 81933a2a37bSGleb Smirnoff */ 82000b5ffdeSGleb Smirnoff if (flags & SF_USER_READAHEAD) { 82100b5ffdeSGleb Smirnoff rhpages = SF_READAHEAD(flags); 82200b5ffdeSGleb Smirnoff } else { 82300b5ffdeSGleb Smirnoff rhpages = howmany(rem + (off & PAGE_MASK), PAGE_SIZE) - 82400b5ffdeSGleb Smirnoff npages; 82533a2a37bSGleb Smirnoff rhpages += SF_READAHEAD(flags); 82600b5ffdeSGleb Smirnoff } 82733a2a37bSGleb Smirnoff rhpages = min(howmany(MAXPHYS, PAGE_SIZE), rhpages); 82833a2a37bSGleb Smirnoff rhpages = min(howmany(obj_size - trunc_page(off), PAGE_SIZE) - 82933a2a37bSGleb Smirnoff npages, rhpages); 83033a2a37bSGleb Smirnoff 83133a2a37bSGleb Smirnoff sfio = malloc(sizeof(struct sf_io) + 83233a2a37bSGleb Smirnoff npages * sizeof(vm_page_t), M_TEMP, M_WAITOK); 83333a2a37bSGleb Smirnoff refcount_init(&sfio->nios, 1); 834d6e13f3bSJeff Roberson sfio->obj = obj; 83533a2a37bSGleb Smirnoff sfio->error = 0; 8366bc27f08SGleb Smirnoff sfio->m = NULL; 837b2e60773SJohn Baldwin #ifdef KERN_TLS 838b2e60773SJohn Baldwin /* 839b2e60773SJohn Baldwin * This doesn't use ktls_hold() because sfio->m will 840b2e60773SJohn Baldwin * also have a reference on 'tls' that will be valid 841b2e60773SJohn Baldwin * for all of sfio's lifetime. 842b2e60773SJohn Baldwin */ 843b2e60773SJohn Baldwin sfio->tls = tls; 844b2e60773SJohn Baldwin #endif 8456bc27f08SGleb Smirnoff vm_object_pip_add(obj, 1); 846fca79580SAlan Somers error = sendfile_swapin(obj, sfio, &nios, off, space, npages, 847fca79580SAlan Somers rhpages, flags); 8480367bca4SAlan Somers if (error != 0) { 849fca79580SAlan Somers if (vp != NULL) 850b249ce48SMateusz Guzik VOP_UNLOCK(vp); 851b8c92303SGleb Smirnoff sendfile_iodone(sfio, NULL, 0, error); 852fca79580SAlan Somers goto done; 853fca79580SAlan Somers } 85433a2a37bSGleb Smirnoff 85533a2a37bSGleb Smirnoff /* 85633a2a37bSGleb Smirnoff * Loop and construct maximum sized mbuf chain to be bulk 85733a2a37bSGleb Smirnoff * dumped into socket buffer. 85833a2a37bSGleb Smirnoff */ 85933a2a37bSGleb Smirnoff pa = sfio->pa; 860cec06a3eSJohn Baldwin 861cec06a3eSJohn Baldwin /* 862cec06a3eSJohn Baldwin * Use unmapped mbufs if enabled for TCP. Unmapped 863cec06a3eSJohn Baldwin * bufs are restricted to TCP as that is what has been 864cec06a3eSJohn Baldwin * tested. In particular, unmapped mbufs have not 865cec06a3eSJohn Baldwin * been tested with UNIX-domain sockets. 866b2e60773SJohn Baldwin * 867b2e60773SJohn Baldwin * TLS frames always require unmapped mbufs. 868cec06a3eSJohn Baldwin */ 869b2e60773SJohn Baldwin if ((mb_use_ext_pgs && 870b2e60773SJohn Baldwin so->so_proto->pr_protocol == IPPROTO_TCP) 871b2e60773SJohn Baldwin #ifdef KERN_TLS 872b2e60773SJohn Baldwin || tls != NULL 873b2e60773SJohn Baldwin #endif 874b2e60773SJohn Baldwin ) { 875cec06a3eSJohn Baldwin use_ext_pgs = true; 876b2e60773SJohn Baldwin #ifdef KERN_TLS 877b2e60773SJohn Baldwin if (tls != NULL) 878b2e60773SJohn Baldwin max_pgs = num_pages(tls->params.max_frame_len); 879b2e60773SJohn Baldwin else 880b2e60773SJohn Baldwin #endif 881cec06a3eSJohn Baldwin max_pgs = MBUF_PEXT_MAX_PGS; 882cec06a3eSJohn Baldwin 883cec06a3eSJohn Baldwin /* Start at last index, to wrap on first use. */ 884cec06a3eSJohn Baldwin ext_pgs_idx = max_pgs - 1; 885cec06a3eSJohn Baldwin } 886cec06a3eSJohn Baldwin 88733a2a37bSGleb Smirnoff for (int i = 0; i < npages; i++) { 88833a2a37bSGleb Smirnoff /* 88933a2a37bSGleb Smirnoff * If a page wasn't grabbed successfully, then 89033a2a37bSGleb Smirnoff * trim the array. Can happen only with SF_NODISKIO. 89133a2a37bSGleb Smirnoff */ 89233a2a37bSGleb Smirnoff if (pa[i] == NULL) { 89333a2a37bSGleb Smirnoff SFSTAT_INC(sf_busy); 89433a2a37bSGleb Smirnoff fixspace(npages, i, off, &space); 89533a2a37bSGleb Smirnoff npages = i; 89633a2a37bSGleb Smirnoff softerr = EBUSY; 89733a2a37bSGleb Smirnoff break; 89833a2a37bSGleb Smirnoff } 89933a2a37bSGleb Smirnoff 900cec06a3eSJohn Baldwin if (use_ext_pgs) { 901cec06a3eSJohn Baldwin off_t xfs; 902cec06a3eSJohn Baldwin 903cec06a3eSJohn Baldwin ext_pgs_idx++; 904cec06a3eSJohn Baldwin if (ext_pgs_idx == max_pgs) { 905cec06a3eSJohn Baldwin m0 = mb_alloc_ext_pgs(M_WAITOK, false, 906cec06a3eSJohn Baldwin sendfile_free_mext_pg); 907cec06a3eSJohn Baldwin 908cec06a3eSJohn Baldwin if (flags & SF_NOCACHE) { 909cec06a3eSJohn Baldwin m0->m_ext.ext_flags |= 910cec06a3eSJohn Baldwin EXT_FLAG_NOCACHE; 911cec06a3eSJohn Baldwin 912cec06a3eSJohn Baldwin /* 913cec06a3eSJohn Baldwin * See comment below regarding 914cec06a3eSJohn Baldwin * ignoring SF_NOCACHE for the 915cec06a3eSJohn Baldwin * last page. 916cec06a3eSJohn Baldwin */ 917cec06a3eSJohn Baldwin if ((npages - i <= max_pgs) && 918cec06a3eSJohn Baldwin ((off + space) & PAGE_MASK) && 919cec06a3eSJohn Baldwin (rem > space || rhpages > 0)) 920cec06a3eSJohn Baldwin m0->m_ext.ext_flags |= 921cec06a3eSJohn Baldwin EXT_FLAG_CACHE_LAST; 922cec06a3eSJohn Baldwin } 923cec06a3eSJohn Baldwin if (sfs != NULL) { 924cec06a3eSJohn Baldwin m0->m_ext.ext_flags |= 925cec06a3eSJohn Baldwin EXT_FLAG_SYNC; 926cec06a3eSJohn Baldwin m0->m_ext.ext_arg2 = sfs; 927cec06a3eSJohn Baldwin mtx_lock(&sfs->mtx); 928cec06a3eSJohn Baldwin sfs->count++; 929cec06a3eSJohn Baldwin mtx_unlock(&sfs->mtx); 930cec06a3eSJohn Baldwin } 931cec06a3eSJohn Baldwin ext_pgs = m0->m_ext.ext_pgs; 932cec06a3eSJohn Baldwin ext_pgs_idx = 0; 933cec06a3eSJohn Baldwin 934cec06a3eSJohn Baldwin /* Append to mbuf chain. */ 935cec06a3eSJohn Baldwin if (mtail != NULL) 936cec06a3eSJohn Baldwin mtail->m_next = m0; 937cec06a3eSJohn Baldwin else 938cec06a3eSJohn Baldwin m = m0; 939cec06a3eSJohn Baldwin mtail = m0; 940cec06a3eSJohn Baldwin ext_pgs->first_pg_off = 941cec06a3eSJohn Baldwin vmoff(i, off) & PAGE_MASK; 942cec06a3eSJohn Baldwin } 943cec06a3eSJohn Baldwin if (nios) { 944cec06a3eSJohn Baldwin mtail->m_flags |= M_NOTREADY; 945cec06a3eSJohn Baldwin ext_pgs->nrdy++; 946cec06a3eSJohn Baldwin } 947cec06a3eSJohn Baldwin 948cec06a3eSJohn Baldwin ext_pgs->pa[ext_pgs_idx] = VM_PAGE_TO_PHYS(pa[i]); 949cec06a3eSJohn Baldwin ext_pgs->npgs++; 950cec06a3eSJohn Baldwin xfs = xfsize(i, npages, off, space); 951cec06a3eSJohn Baldwin ext_pgs->last_pg_len = xfs; 952cec06a3eSJohn Baldwin MBUF_EXT_PGS_ASSERT_SANITY(ext_pgs); 953cec06a3eSJohn Baldwin mtail->m_len += xfs; 954cec06a3eSJohn Baldwin mtail->m_ext.ext_size += PAGE_SIZE; 955cec06a3eSJohn Baldwin continue; 956cec06a3eSJohn Baldwin } 957cec06a3eSJohn Baldwin 95833a2a37bSGleb Smirnoff /* 95933a2a37bSGleb Smirnoff * Get a sendfile buf. When allocating the 96033a2a37bSGleb Smirnoff * first buffer for mbuf chain, we usually 96133a2a37bSGleb Smirnoff * wait as long as necessary, but this wait 96233a2a37bSGleb Smirnoff * can be interrupted. For consequent 96333a2a37bSGleb Smirnoff * buffers, do not sleep, since several 96433a2a37bSGleb Smirnoff * threads might exhaust the buffers and then 96533a2a37bSGleb Smirnoff * deadlock. 96633a2a37bSGleb Smirnoff */ 96733a2a37bSGleb Smirnoff sf = sf_buf_alloc(pa[i], 96833a2a37bSGleb Smirnoff m != NULL ? SFB_NOWAIT : SFB_CATCH); 96933a2a37bSGleb Smirnoff if (sf == NULL) { 97033a2a37bSGleb Smirnoff SFSTAT_INC(sf_allocfail); 971fee2a2faSMark Johnston for (int j = i; j < npages; j++) 97233a2a37bSGleb Smirnoff vm_page_unwire(pa[j], PQ_INACTIVE); 97333a2a37bSGleb Smirnoff if (m == NULL) 97433a2a37bSGleb Smirnoff softerr = ENOBUFS; 97533a2a37bSGleb Smirnoff fixspace(npages, i, off, &space); 97633a2a37bSGleb Smirnoff npages = i; 97733a2a37bSGleb Smirnoff break; 97833a2a37bSGleb Smirnoff } 97933a2a37bSGleb Smirnoff 98033a2a37bSGleb Smirnoff m0 = m_get(M_WAITOK, MT_DATA); 98133a2a37bSGleb Smirnoff m0->m_ext.ext_buf = (char *)sf_buf_kva(sf); 98233a2a37bSGleb Smirnoff m0->m_ext.ext_size = PAGE_SIZE; 98333a2a37bSGleb Smirnoff m0->m_ext.ext_arg1 = sf; 9849c82bec4SGleb Smirnoff m0->m_ext.ext_type = EXT_SFBUF; 9859c82bec4SGleb Smirnoff m0->m_ext.ext_flags = EXT_FLAG_EMBREF; 9869c82bec4SGleb Smirnoff m0->m_ext.ext_free = sendfile_free_mext; 98733a2a37bSGleb Smirnoff /* 98833a2a37bSGleb Smirnoff * SF_NOCACHE sets the page as being freed upon send. 98933a2a37bSGleb Smirnoff * However, we ignore it for the last page in 'space', 99033a2a37bSGleb Smirnoff * if the page is truncated, and we got more data to 99133a2a37bSGleb Smirnoff * send (rem > space), or if we have readahead 99233a2a37bSGleb Smirnoff * configured (rhpages > 0). 99333a2a37bSGleb Smirnoff */ 9949c82bec4SGleb Smirnoff if ((flags & SF_NOCACHE) && 9959c82bec4SGleb Smirnoff (i != npages - 1 || 9969c82bec4SGleb Smirnoff !((off + space) & PAGE_MASK) || 9979c82bec4SGleb Smirnoff !(rem > space || rhpages > 0))) 9989c82bec4SGleb Smirnoff m0->m_ext.ext_flags |= EXT_FLAG_NOCACHE; 9999c82bec4SGleb Smirnoff if (sfs != NULL) { 10009c82bec4SGleb Smirnoff m0->m_ext.ext_flags |= EXT_FLAG_SYNC; 10019c82bec4SGleb Smirnoff m0->m_ext.ext_arg2 = sfs; 10029c82bec4SGleb Smirnoff mtx_lock(&sfs->mtx); 10039c82bec4SGleb Smirnoff sfs->count++; 10049c82bec4SGleb Smirnoff mtx_unlock(&sfs->mtx); 10059c82bec4SGleb Smirnoff } 100656a5f52eSGleb Smirnoff m0->m_ext.ext_count = 1; 100733a2a37bSGleb Smirnoff m0->m_flags |= (M_EXT | M_RDONLY); 100833a2a37bSGleb Smirnoff if (nios) 100933a2a37bSGleb Smirnoff m0->m_flags |= M_NOTREADY; 101033a2a37bSGleb Smirnoff m0->m_data = (char *)sf_buf_kva(sf) + 101133a2a37bSGleb Smirnoff (vmoff(i, off) & PAGE_MASK); 101233a2a37bSGleb Smirnoff m0->m_len = xfsize(i, npages, off, space); 101333a2a37bSGleb Smirnoff 101433a2a37bSGleb Smirnoff /* Append to mbuf chain. */ 101533a2a37bSGleb Smirnoff if (mtail != NULL) 101633a2a37bSGleb Smirnoff mtail->m_next = m0; 101733a2a37bSGleb Smirnoff else 101833a2a37bSGleb Smirnoff m = m0; 101933a2a37bSGleb Smirnoff mtail = m0; 102033a2a37bSGleb Smirnoff } 102133a2a37bSGleb Smirnoff 102233a2a37bSGleb Smirnoff if (vp != NULL) 1023b249ce48SMateusz Guzik VOP_UNLOCK(vp); 102433a2a37bSGleb Smirnoff 102533a2a37bSGleb Smirnoff /* Keep track of bytes processed. */ 102633a2a37bSGleb Smirnoff off += space; 102733a2a37bSGleb Smirnoff rem -= space; 102833a2a37bSGleb Smirnoff 10296bc27f08SGleb Smirnoff /* 10306bc27f08SGleb Smirnoff * Prepend header, if any. Save pointer to first mbuf 10316bc27f08SGleb Smirnoff * with a page. 10326bc27f08SGleb Smirnoff */ 103333a2a37bSGleb Smirnoff if (hdrlen) { 1034a2d8f9d2SGleb Smirnoff prepend_header: 10356bc27f08SGleb Smirnoff m0 = mhtail->m_next = m; 103633a2a37bSGleb Smirnoff m = mh; 103733a2a37bSGleb Smirnoff mh = NULL; 10386bc27f08SGleb Smirnoff } else 10396bc27f08SGleb Smirnoff m0 = m; 104033a2a37bSGleb Smirnoff 104133a2a37bSGleb Smirnoff if (m == NULL) { 104233a2a37bSGleb Smirnoff KASSERT(softerr, ("%s: m NULL, no error", __func__)); 104333a2a37bSGleb Smirnoff error = softerr; 10446bc27f08SGleb Smirnoff sendfile_iodone(sfio, NULL, 0, 0); 104533a2a37bSGleb Smirnoff goto done; 104633a2a37bSGleb Smirnoff } 104733a2a37bSGleb Smirnoff 104833a2a37bSGleb Smirnoff /* Add the buffer chain to the socket buffer. */ 104933a2a37bSGleb Smirnoff KASSERT(m_length(m, NULL) == space + hdrlen, 105033a2a37bSGleb Smirnoff ("%s: mlen %u space %d hdrlen %d", 105133a2a37bSGleb Smirnoff __func__, m_length(m, NULL), space, hdrlen)); 105233a2a37bSGleb Smirnoff 105333a2a37bSGleb Smirnoff CURVNET_SET(so->so_vnet); 1054b2e60773SJohn Baldwin #ifdef KERN_TLS 1055f85e1a80SGleb Smirnoff if (tls != NULL) 1056f85e1a80SGleb Smirnoff ktls_frame(m, tls, &tls_enq_cnt, TLS_RLTYPE_APP); 1057b2e60773SJohn Baldwin #endif 105833a2a37bSGleb Smirnoff if (nios == 0) { 105933a2a37bSGleb Smirnoff /* 106033a2a37bSGleb Smirnoff * If sendfile_swapin() didn't initiate any I/Os, 10616bc27f08SGleb Smirnoff * which happens if all data is cached in VM, or if 10626bc27f08SGleb Smirnoff * the header consumed all socket buffer space and 10636bc27f08SGleb Smirnoff * sfio is NULL, then we can send data right now 10646bc27f08SGleb Smirnoff * without the PRUS_NOTREADY flag. 106533a2a37bSGleb Smirnoff */ 10666bc27f08SGleb Smirnoff if (sfio != NULL) 10676bc27f08SGleb Smirnoff sendfile_iodone(sfio, NULL, 0, 0); 1068b2e60773SJohn Baldwin #ifdef KERN_TLS 10699e14430dSJohn Baldwin if (tls != NULL && tls->mode == TCP_TLS_MODE_SW) { 1070b2e60773SJohn Baldwin error = (*so->so_proto->pr_usrreqs->pru_send) 1071b2e60773SJohn Baldwin (so, PRUS_NOTREADY, m, NULL, NULL, td); 1072b2e60773SJohn Baldwin soref(so); 1073b2e60773SJohn Baldwin ktls_enqueue(m, so, tls_enq_cnt); 1074b2e60773SJohn Baldwin } else 1075b2e60773SJohn Baldwin #endif 107633a2a37bSGleb Smirnoff error = (*so->so_proto->pr_usrreqs->pru_send) 107733a2a37bSGleb Smirnoff (so, 0, m, NULL, NULL, td); 107833a2a37bSGleb Smirnoff } else { 10796bc27f08SGleb Smirnoff sfio->so = so; 10806bc27f08SGleb Smirnoff sfio->m = m0; 108133a2a37bSGleb Smirnoff sfio->npages = npages; 1082d37aa3ccSGleb Smirnoff soref(so); 108333a2a37bSGleb Smirnoff error = (*so->so_proto->pr_usrreqs->pru_send) 108433a2a37bSGleb Smirnoff (so, PRUS_NOTREADY, m, NULL, NULL, td); 108533a2a37bSGleb Smirnoff sendfile_iodone(sfio, NULL, 0, 0); 108633a2a37bSGleb Smirnoff } 108733a2a37bSGleb Smirnoff CURVNET_RESTORE(); 108833a2a37bSGleb Smirnoff 108933a2a37bSGleb Smirnoff m = NULL; /* pru_send always consumes */ 109033a2a37bSGleb Smirnoff if (error) 109133a2a37bSGleb Smirnoff goto done; 109233a2a37bSGleb Smirnoff sbytes += space + hdrlen; 109333a2a37bSGleb Smirnoff if (hdrlen) 109433a2a37bSGleb Smirnoff hdrlen = 0; 109533a2a37bSGleb Smirnoff if (softerr) { 109633a2a37bSGleb Smirnoff error = softerr; 109733a2a37bSGleb Smirnoff goto done; 109833a2a37bSGleb Smirnoff } 109933a2a37bSGleb Smirnoff } 110033a2a37bSGleb Smirnoff 110133a2a37bSGleb Smirnoff /* 110233a2a37bSGleb Smirnoff * Send trailers. Wimp out and use writev(2). 110333a2a37bSGleb Smirnoff */ 110433a2a37bSGleb Smirnoff if (trl_uio != NULL) { 110533a2a37bSGleb Smirnoff sbunlock(&so->so_snd); 110633a2a37bSGleb Smirnoff error = kern_writev(td, sockfd, trl_uio); 110733a2a37bSGleb Smirnoff if (error == 0) 110833a2a37bSGleb Smirnoff sbytes += td->td_retval[0]; 110933a2a37bSGleb Smirnoff goto out; 111033a2a37bSGleb Smirnoff } 111133a2a37bSGleb Smirnoff 111233a2a37bSGleb Smirnoff done: 111333a2a37bSGleb Smirnoff sbunlock(&so->so_snd); 111433a2a37bSGleb Smirnoff out: 111533a2a37bSGleb Smirnoff /* 111633a2a37bSGleb Smirnoff * If there was no error we have to clear td->td_retval[0] 111733a2a37bSGleb Smirnoff * because it may have been set by writev. 111833a2a37bSGleb Smirnoff */ 111933a2a37bSGleb Smirnoff if (error == 0) { 112033a2a37bSGleb Smirnoff td->td_retval[0] = 0; 112133a2a37bSGleb Smirnoff } 112233a2a37bSGleb Smirnoff if (sent != NULL) { 112333a2a37bSGleb Smirnoff (*sent) = sbytes; 112433a2a37bSGleb Smirnoff } 112533a2a37bSGleb Smirnoff if (obj != NULL) 112633a2a37bSGleb Smirnoff vm_object_deallocate(obj); 112733a2a37bSGleb Smirnoff if (so) 112833a2a37bSGleb Smirnoff fdrop(sock_fp, td); 112933a2a37bSGleb Smirnoff if (m) 113033a2a37bSGleb Smirnoff m_freem(m); 113133a2a37bSGleb Smirnoff if (mh) 113233a2a37bSGleb Smirnoff m_freem(mh); 113333a2a37bSGleb Smirnoff 113433a2a37bSGleb Smirnoff if (sfs != NULL) { 113533a2a37bSGleb Smirnoff mtx_lock(&sfs->mtx); 113633a2a37bSGleb Smirnoff if (sfs->count != 0) 113733a2a37bSGleb Smirnoff cv_wait(&sfs->cv, &sfs->mtx); 113833a2a37bSGleb Smirnoff KASSERT(sfs->count == 0, ("sendfile sync still busy")); 113933a2a37bSGleb Smirnoff cv_destroy(&sfs->cv); 114033a2a37bSGleb Smirnoff mtx_destroy(&sfs->mtx); 114133a2a37bSGleb Smirnoff free(sfs, M_TEMP); 114233a2a37bSGleb Smirnoff } 1143b2e60773SJohn Baldwin #ifdef KERN_TLS 1144b2e60773SJohn Baldwin if (tls != NULL) 1145b2e60773SJohn Baldwin ktls_free(tls); 1146b2e60773SJohn Baldwin #endif 114733a2a37bSGleb Smirnoff 114833a2a37bSGleb Smirnoff if (error == ERESTART) 114933a2a37bSGleb Smirnoff error = EINTR; 115033a2a37bSGleb Smirnoff 115133a2a37bSGleb Smirnoff return (error); 115233a2a37bSGleb Smirnoff } 115333a2a37bSGleb Smirnoff 115433a2a37bSGleb Smirnoff static int 115533a2a37bSGleb Smirnoff sendfile(struct thread *td, struct sendfile_args *uap, int compat) 115633a2a37bSGleb Smirnoff { 115733a2a37bSGleb Smirnoff struct sf_hdtr hdtr; 115833a2a37bSGleb Smirnoff struct uio *hdr_uio, *trl_uio; 115933a2a37bSGleb Smirnoff struct file *fp; 116033a2a37bSGleb Smirnoff off_t sbytes; 116133a2a37bSGleb Smirnoff int error; 116233a2a37bSGleb Smirnoff 116333a2a37bSGleb Smirnoff /* 116433a2a37bSGleb Smirnoff * File offset must be positive. If it goes beyond EOF 116533a2a37bSGleb Smirnoff * we send only the header/trailer and no payload data. 116633a2a37bSGleb Smirnoff */ 116733a2a37bSGleb Smirnoff if (uap->offset < 0) 116833a2a37bSGleb Smirnoff return (EINVAL); 116933a2a37bSGleb Smirnoff 1170ef3266d5SGleb Smirnoff sbytes = 0; 117133a2a37bSGleb Smirnoff hdr_uio = trl_uio = NULL; 117233a2a37bSGleb Smirnoff 117333a2a37bSGleb Smirnoff if (uap->hdtr != NULL) { 117433a2a37bSGleb Smirnoff error = copyin(uap->hdtr, &hdtr, sizeof(hdtr)); 117533a2a37bSGleb Smirnoff if (error != 0) 117633a2a37bSGleb Smirnoff goto out; 117733a2a37bSGleb Smirnoff if (hdtr.headers != NULL) { 117833a2a37bSGleb Smirnoff error = copyinuio(hdtr.headers, hdtr.hdr_cnt, 117933a2a37bSGleb Smirnoff &hdr_uio); 118033a2a37bSGleb Smirnoff if (error != 0) 118133a2a37bSGleb Smirnoff goto out; 11829c64cfe5SGleb Smirnoff #ifdef COMPAT_FREEBSD4 11839c64cfe5SGleb Smirnoff /* 11849c64cfe5SGleb Smirnoff * In FreeBSD < 5.0 the nbytes to send also included 11859c64cfe5SGleb Smirnoff * the header. If compat is specified subtract the 11869c64cfe5SGleb Smirnoff * header size from nbytes. 11879c64cfe5SGleb Smirnoff */ 11889c64cfe5SGleb Smirnoff if (compat) { 11899c64cfe5SGleb Smirnoff if (uap->nbytes > hdr_uio->uio_resid) 11909c64cfe5SGleb Smirnoff uap->nbytes -= hdr_uio->uio_resid; 11919c64cfe5SGleb Smirnoff else 11929c64cfe5SGleb Smirnoff uap->nbytes = 0; 11939c64cfe5SGleb Smirnoff } 11949c64cfe5SGleb Smirnoff #endif 119533a2a37bSGleb Smirnoff } 119633a2a37bSGleb Smirnoff if (hdtr.trailers != NULL) { 119733a2a37bSGleb Smirnoff error = copyinuio(hdtr.trailers, hdtr.trl_cnt, 119833a2a37bSGleb Smirnoff &trl_uio); 119933a2a37bSGleb Smirnoff if (error != 0) 120033a2a37bSGleb Smirnoff goto out; 120133a2a37bSGleb Smirnoff } 120233a2a37bSGleb Smirnoff } 120333a2a37bSGleb Smirnoff 120433a2a37bSGleb Smirnoff AUDIT_ARG_FD(uap->fd); 120533a2a37bSGleb Smirnoff 120633a2a37bSGleb Smirnoff /* 120733a2a37bSGleb Smirnoff * sendfile(2) can start at any offset within a file so we require 120833a2a37bSGleb Smirnoff * CAP_READ+CAP_SEEK = CAP_PREAD. 120933a2a37bSGleb Smirnoff */ 1210cbd92ce6SMatt Macy if ((error = fget_read(td, uap->fd, &cap_pread_rights, &fp)) != 0) 121133a2a37bSGleb Smirnoff goto out; 121233a2a37bSGleb Smirnoff 121333a2a37bSGleb Smirnoff error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset, 12149c64cfe5SGleb Smirnoff uap->nbytes, &sbytes, uap->flags, td); 121533a2a37bSGleb Smirnoff fdrop(fp, td); 121633a2a37bSGleb Smirnoff 121733a2a37bSGleb Smirnoff if (uap->sbytes != NULL) 121833a2a37bSGleb Smirnoff copyout(&sbytes, uap->sbytes, sizeof(off_t)); 121933a2a37bSGleb Smirnoff 122033a2a37bSGleb Smirnoff out: 122133a2a37bSGleb Smirnoff free(hdr_uio, M_IOV); 122233a2a37bSGleb Smirnoff free(trl_uio, M_IOV); 122333a2a37bSGleb Smirnoff return (error); 122433a2a37bSGleb Smirnoff } 122533a2a37bSGleb Smirnoff 122633a2a37bSGleb Smirnoff /* 122733a2a37bSGleb Smirnoff * sendfile(2) 122833a2a37bSGleb Smirnoff * 122933a2a37bSGleb Smirnoff * int sendfile(int fd, int s, off_t offset, size_t nbytes, 123033a2a37bSGleb Smirnoff * struct sf_hdtr *hdtr, off_t *sbytes, int flags) 123133a2a37bSGleb Smirnoff * 123233a2a37bSGleb Smirnoff * Send a file specified by 'fd' and starting at 'offset' to a socket 123333a2a37bSGleb Smirnoff * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes == 123433a2a37bSGleb Smirnoff * 0. Optionally add a header and/or trailer to the socket output. If 123533a2a37bSGleb Smirnoff * specified, write the total number of bytes sent into *sbytes. 123633a2a37bSGleb Smirnoff */ 123733a2a37bSGleb Smirnoff int 123833a2a37bSGleb Smirnoff sys_sendfile(struct thread *td, struct sendfile_args *uap) 123933a2a37bSGleb Smirnoff { 124033a2a37bSGleb Smirnoff 124133a2a37bSGleb Smirnoff return (sendfile(td, uap, 0)); 124233a2a37bSGleb Smirnoff } 124333a2a37bSGleb Smirnoff 124433a2a37bSGleb Smirnoff #ifdef COMPAT_FREEBSD4 124533a2a37bSGleb Smirnoff int 124633a2a37bSGleb Smirnoff freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap) 124733a2a37bSGleb Smirnoff { 124833a2a37bSGleb Smirnoff struct sendfile_args args; 124933a2a37bSGleb Smirnoff 125033a2a37bSGleb Smirnoff args.fd = uap->fd; 125133a2a37bSGleb Smirnoff args.s = uap->s; 125233a2a37bSGleb Smirnoff args.offset = uap->offset; 125333a2a37bSGleb Smirnoff args.nbytes = uap->nbytes; 125433a2a37bSGleb Smirnoff args.hdtr = uap->hdtr; 125533a2a37bSGleb Smirnoff args.sbytes = uap->sbytes; 125633a2a37bSGleb Smirnoff args.flags = uap->flags; 125733a2a37bSGleb Smirnoff 125833a2a37bSGleb Smirnoff return (sendfile(td, &args, 1)); 125933a2a37bSGleb Smirnoff } 126033a2a37bSGleb Smirnoff #endif /* COMPAT_FREEBSD4 */ 1261