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> 5933a2a37bSGleb Smirnoff 6033a2a37bSGleb Smirnoff #include <security/audit/audit.h> 6133a2a37bSGleb Smirnoff #include <security/mac/mac_framework.h> 6233a2a37bSGleb Smirnoff 6333a2a37bSGleb Smirnoff #include <vm/vm.h> 6433a2a37bSGleb Smirnoff #include <vm/vm_object.h> 6533a2a37bSGleb Smirnoff #include <vm/vm_pager.h> 6633a2a37bSGleb Smirnoff 679c82bec4SGleb Smirnoff #define EXT_FLAG_SYNC EXT_FLAG_VENDOR1 689c82bec4SGleb Smirnoff #define EXT_FLAG_NOCACHE EXT_FLAG_VENDOR2 69cec06a3eSJohn Baldwin #define EXT_FLAG_CACHE_LAST EXT_FLAG_VENDOR3 709c82bec4SGleb Smirnoff 7133a2a37bSGleb Smirnoff /* 7233a2a37bSGleb Smirnoff * Structure describing a single sendfile(2) I/O, which may consist of 7333a2a37bSGleb Smirnoff * several underlying pager I/Os. 7433a2a37bSGleb Smirnoff * 7533a2a37bSGleb Smirnoff * The syscall context allocates the structure and initializes 'nios' 7633a2a37bSGleb Smirnoff * to 1. As sendfile_swapin() runs through pages and starts asynchronous 7733a2a37bSGleb Smirnoff * paging operations, it increments 'nios'. 7833a2a37bSGleb Smirnoff * 7933a2a37bSGleb Smirnoff * Every I/O completion calls sendfile_iodone(), which decrements the 'nios', 8033a2a37bSGleb Smirnoff * and the syscall also calls sendfile_iodone() after allocating all mbufs, 8133a2a37bSGleb Smirnoff * linking them and sending to socket. Whoever reaches zero 'nios' is 8233a2a37bSGleb Smirnoff * responsible to * call pru_ready on the socket, to notify it of readyness 8333a2a37bSGleb Smirnoff * of the data. 8433a2a37bSGleb Smirnoff */ 8533a2a37bSGleb Smirnoff struct sf_io { 8633a2a37bSGleb Smirnoff volatile u_int nios; 8733a2a37bSGleb Smirnoff u_int error; 8833a2a37bSGleb Smirnoff int npages; 89d37aa3ccSGleb Smirnoff struct socket *so; 9033a2a37bSGleb Smirnoff struct mbuf *m; 91818d7553SJohn Baldwin #ifdef KERN_TLS 92b2e60773SJohn Baldwin struct ktls_session *tls; 93818d7553SJohn Baldwin #endif 9433a2a37bSGleb Smirnoff vm_page_t pa[]; 9533a2a37bSGleb Smirnoff }; 9633a2a37bSGleb Smirnoff 9733a2a37bSGleb Smirnoff /* 9833a2a37bSGleb Smirnoff * Structure used to track requests with SF_SYNC flag. 9933a2a37bSGleb Smirnoff */ 10033a2a37bSGleb Smirnoff struct sendfile_sync { 10133a2a37bSGleb Smirnoff struct mtx mtx; 10233a2a37bSGleb Smirnoff struct cv cv; 10333a2a37bSGleb Smirnoff unsigned count; 10433a2a37bSGleb Smirnoff }; 10533a2a37bSGleb Smirnoff 10633a2a37bSGleb Smirnoff counter_u64_t sfstat[sizeof(struct sfstat) / sizeof(uint64_t)]; 10733a2a37bSGleb Smirnoff 10833a2a37bSGleb Smirnoff static void 10933a2a37bSGleb Smirnoff sfstat_init(const void *unused) 11033a2a37bSGleb Smirnoff { 11133a2a37bSGleb Smirnoff 11233a2a37bSGleb Smirnoff COUNTER_ARRAY_ALLOC(sfstat, sizeof(struct sfstat) / sizeof(uint64_t), 11333a2a37bSGleb Smirnoff M_WAITOK); 11433a2a37bSGleb Smirnoff } 11533a2a37bSGleb Smirnoff SYSINIT(sfstat, SI_SUB_MBUF, SI_ORDER_FIRST, sfstat_init, NULL); 11633a2a37bSGleb Smirnoff 11733a2a37bSGleb Smirnoff static int 11833a2a37bSGleb Smirnoff sfstat_sysctl(SYSCTL_HANDLER_ARGS) 11933a2a37bSGleb Smirnoff { 12033a2a37bSGleb Smirnoff struct sfstat s; 12133a2a37bSGleb Smirnoff 12233a2a37bSGleb Smirnoff COUNTER_ARRAY_COPY(sfstat, &s, sizeof(s) / sizeof(uint64_t)); 12333a2a37bSGleb Smirnoff if (req->newptr) 12433a2a37bSGleb Smirnoff COUNTER_ARRAY_ZERO(sfstat, sizeof(s) / sizeof(uint64_t)); 12533a2a37bSGleb Smirnoff return (SYSCTL_OUT(req, &s, sizeof(s))); 12633a2a37bSGleb Smirnoff } 12733a2a37bSGleb Smirnoff SYSCTL_PROC(_kern_ipc, OID_AUTO, sfstat, CTLTYPE_OPAQUE | CTLFLAG_RW, 12833a2a37bSGleb Smirnoff NULL, 0, sfstat_sysctl, "I", "sendfile statistics"); 12933a2a37bSGleb Smirnoff 1309c82bec4SGleb Smirnoff static void 1319c82bec4SGleb Smirnoff sendfile_free_mext(struct mbuf *m) 1329c82bec4SGleb Smirnoff { 1339c82bec4SGleb Smirnoff struct sf_buf *sf; 1349c82bec4SGleb Smirnoff vm_page_t pg; 13598549e2dSMark Johnston int flags; 1369c82bec4SGleb Smirnoff 1379c82bec4SGleb Smirnoff KASSERT(m->m_flags & M_EXT && m->m_ext.ext_type == EXT_SFBUF, 1389c82bec4SGleb Smirnoff ("%s: m %p !M_EXT or !EXT_SFBUF", __func__, m)); 1399c82bec4SGleb Smirnoff 1409c82bec4SGleb Smirnoff sf = m->m_ext.ext_arg1; 1419c82bec4SGleb Smirnoff pg = sf_buf_page(sf); 14298549e2dSMark Johnston flags = (m->m_ext.ext_flags & EXT_FLAG_NOCACHE) != 0 ? VPR_TRYFREE : 0; 1439c82bec4SGleb Smirnoff 1449c82bec4SGleb Smirnoff sf_buf_free(sf); 14598549e2dSMark Johnston vm_page_release(pg, flags); 1469c82bec4SGleb Smirnoff 1479c82bec4SGleb Smirnoff if (m->m_ext.ext_flags & EXT_FLAG_SYNC) { 1489c82bec4SGleb Smirnoff struct sendfile_sync *sfs = m->m_ext.ext_arg2; 1499c82bec4SGleb Smirnoff 15033a2a37bSGleb Smirnoff mtx_lock(&sfs->mtx); 15133a2a37bSGleb Smirnoff KASSERT(sfs->count > 0, ("Sendfile sync botchup count == 0")); 15233a2a37bSGleb Smirnoff if (--sfs->count == 0) 15333a2a37bSGleb Smirnoff cv_signal(&sfs->cv); 15433a2a37bSGleb Smirnoff mtx_unlock(&sfs->mtx); 15533a2a37bSGleb Smirnoff } 15633a2a37bSGleb Smirnoff } 15733a2a37bSGleb Smirnoff 158cec06a3eSJohn Baldwin static void 159cec06a3eSJohn Baldwin sendfile_free_mext_pg(struct mbuf *m) 160cec06a3eSJohn Baldwin { 161cec06a3eSJohn Baldwin struct mbuf_ext_pgs *ext_pgs; 162cec06a3eSJohn Baldwin vm_page_t pg; 16398549e2dSMark Johnston int flags, i; 16498549e2dSMark Johnston bool cache_last; 165cec06a3eSJohn Baldwin 166cec06a3eSJohn Baldwin KASSERT(m->m_flags & M_EXT && m->m_ext.ext_type == EXT_PGS, 167cec06a3eSJohn Baldwin ("%s: m %p !M_EXT or !EXT_PGS", __func__, m)); 168cec06a3eSJohn Baldwin 169cec06a3eSJohn Baldwin cache_last = m->m_ext.ext_flags & EXT_FLAG_CACHE_LAST; 170cec06a3eSJohn Baldwin ext_pgs = m->m_ext.ext_pgs; 17198549e2dSMark Johnston flags = (m->m_ext.ext_flags & EXT_FLAG_NOCACHE) != 0 ? VPR_TRYFREE : 0; 172cec06a3eSJohn Baldwin 173cec06a3eSJohn Baldwin for (i = 0; i < ext_pgs->npgs; i++) { 174cec06a3eSJohn Baldwin if (cache_last && i == ext_pgs->npgs - 1) 17598549e2dSMark Johnston flags = 0; 176cec06a3eSJohn Baldwin pg = PHYS_TO_VM_PAGE(ext_pgs->pa[i]); 17798549e2dSMark Johnston vm_page_release(pg, flags); 178cec06a3eSJohn Baldwin } 179cec06a3eSJohn Baldwin 180cec06a3eSJohn Baldwin if (m->m_ext.ext_flags & EXT_FLAG_SYNC) { 181cec06a3eSJohn Baldwin struct sendfile_sync *sfs = m->m_ext.ext_arg2; 182cec06a3eSJohn Baldwin 183cec06a3eSJohn Baldwin mtx_lock(&sfs->mtx); 184cec06a3eSJohn Baldwin KASSERT(sfs->count > 0, ("Sendfile sync botchup count == 0")); 185cec06a3eSJohn Baldwin if (--sfs->count == 0) 186cec06a3eSJohn Baldwin cv_signal(&sfs->cv); 187cec06a3eSJohn Baldwin mtx_unlock(&sfs->mtx); 188cec06a3eSJohn Baldwin } 189cec06a3eSJohn Baldwin } 190cec06a3eSJohn Baldwin 19133a2a37bSGleb Smirnoff /* 19233a2a37bSGleb Smirnoff * Helper function to calculate how much data to put into page i of n. 19333a2a37bSGleb Smirnoff * Only first and last pages are special. 19433a2a37bSGleb Smirnoff */ 19533a2a37bSGleb Smirnoff static inline off_t 19633a2a37bSGleb Smirnoff xfsize(int i, int n, off_t off, off_t len) 19733a2a37bSGleb Smirnoff { 19833a2a37bSGleb Smirnoff 19933a2a37bSGleb Smirnoff if (i == 0) 20033a2a37bSGleb Smirnoff return (omin(PAGE_SIZE - (off & PAGE_MASK), len)); 20133a2a37bSGleb Smirnoff 20233a2a37bSGleb Smirnoff if (i == n - 1 && ((off + len) & PAGE_MASK) > 0) 20333a2a37bSGleb Smirnoff return ((off + len) & PAGE_MASK); 20433a2a37bSGleb Smirnoff 20533a2a37bSGleb Smirnoff return (PAGE_SIZE); 20633a2a37bSGleb Smirnoff } 20733a2a37bSGleb Smirnoff 20833a2a37bSGleb Smirnoff /* 20933a2a37bSGleb Smirnoff * Helper function to get offset within object for i page. 21033a2a37bSGleb Smirnoff */ 211d712b799SAlan Cox static inline vm_ooffset_t 21233a2a37bSGleb Smirnoff vmoff(int i, off_t off) 21333a2a37bSGleb Smirnoff { 21433a2a37bSGleb Smirnoff 21533a2a37bSGleb Smirnoff if (i == 0) 216d712b799SAlan Cox return ((vm_ooffset_t)off); 21733a2a37bSGleb Smirnoff 21833a2a37bSGleb Smirnoff return (trunc_page(off + i * PAGE_SIZE)); 21933a2a37bSGleb Smirnoff } 22033a2a37bSGleb Smirnoff 22133a2a37bSGleb Smirnoff /* 22233a2a37bSGleb Smirnoff * Helper function used when allocation of a page or sf_buf failed. 22333a2a37bSGleb Smirnoff * Pretend as if we don't have enough space, subtract xfsize() of 22433a2a37bSGleb Smirnoff * all pages that failed. 22533a2a37bSGleb Smirnoff */ 22633a2a37bSGleb Smirnoff static inline void 22733a2a37bSGleb Smirnoff fixspace(int old, int new, off_t off, int *space) 22833a2a37bSGleb Smirnoff { 22933a2a37bSGleb Smirnoff 23033a2a37bSGleb Smirnoff KASSERT(old > new, ("%s: old %d new %d", __func__, old, new)); 23133a2a37bSGleb Smirnoff 23233a2a37bSGleb Smirnoff /* Subtract last one. */ 23333a2a37bSGleb Smirnoff *space -= xfsize(old - 1, old, off, *space); 23433a2a37bSGleb Smirnoff old--; 23533a2a37bSGleb Smirnoff 23633a2a37bSGleb Smirnoff if (new == old) 23733a2a37bSGleb Smirnoff /* There was only one page. */ 23833a2a37bSGleb Smirnoff return; 23933a2a37bSGleb Smirnoff 24033a2a37bSGleb Smirnoff /* Subtract first one. */ 24133a2a37bSGleb Smirnoff if (new == 0) { 24233a2a37bSGleb Smirnoff *space -= xfsize(0, old, off, *space); 24333a2a37bSGleb Smirnoff new++; 24433a2a37bSGleb Smirnoff } 24533a2a37bSGleb Smirnoff 24633a2a37bSGleb Smirnoff /* Rest of pages are full sized. */ 24733a2a37bSGleb Smirnoff *space -= (old - new) * PAGE_SIZE; 24833a2a37bSGleb Smirnoff 24933a2a37bSGleb Smirnoff KASSERT(*space >= 0, ("%s: space went backwards", __func__)); 25033a2a37bSGleb Smirnoff } 25133a2a37bSGleb Smirnoff 25233a2a37bSGleb Smirnoff /* 25333a2a37bSGleb Smirnoff * I/O completion callback. 25433a2a37bSGleb Smirnoff */ 25533a2a37bSGleb Smirnoff static void 25633a2a37bSGleb Smirnoff sendfile_iodone(void *arg, vm_page_t *pg, int count, int error) 25733a2a37bSGleb Smirnoff { 25833a2a37bSGleb Smirnoff struct sf_io *sfio = arg; 259d37aa3ccSGleb Smirnoff struct socket *so = sfio->so; 26033a2a37bSGleb Smirnoff 26133a2a37bSGleb Smirnoff for (int i = 0; i < count; i++) 2625dba303dSGleb Smirnoff if (pg[i] != bogus_page) 26333a2a37bSGleb Smirnoff vm_page_xunbusy(pg[i]); 26433a2a37bSGleb Smirnoff 26533a2a37bSGleb Smirnoff if (error) 26633a2a37bSGleb Smirnoff sfio->error = error; 26733a2a37bSGleb Smirnoff 26833a2a37bSGleb Smirnoff if (!refcount_release(&sfio->nios)) 26933a2a37bSGleb Smirnoff return; 27033a2a37bSGleb Smirnoff 271818d7553SJohn Baldwin #if defined(KERN_TLS) && defined(INVARIANTS) 272b2e60773SJohn Baldwin if ((sfio->m->m_flags & M_EXT) != 0 && 273b2e60773SJohn Baldwin sfio->m->m_ext.ext_type == EXT_PGS) 274b2e60773SJohn Baldwin KASSERT(sfio->tls == sfio->m->m_ext.ext_pgs->tls, 275b2e60773SJohn Baldwin ("TLS session mismatch")); 276b2e60773SJohn Baldwin else 277b2e60773SJohn Baldwin KASSERT(sfio->tls == NULL, 278b2e60773SJohn Baldwin ("non-ext_pgs mbuf with TLS session")); 279b2e60773SJohn Baldwin #endif 280b4f55763SGleb Smirnoff CURVNET_SET(so->so_vnet); 28133a2a37bSGleb Smirnoff if (sfio->error) { 28233a2a37bSGleb Smirnoff /* 28333a2a37bSGleb Smirnoff * I/O operation failed. The state of data in the socket 28433a2a37bSGleb Smirnoff * is now inconsistent, and all what we can do is to tear 28533a2a37bSGleb Smirnoff * it down. Protocol abort method would tear down protocol 28633a2a37bSGleb Smirnoff * state, free all ready mbufs and detach not ready ones. 28733a2a37bSGleb Smirnoff * We will free the mbufs corresponding to this I/O manually. 28833a2a37bSGleb Smirnoff * 28933a2a37bSGleb Smirnoff * The socket would be marked with EIO and made available 29033a2a37bSGleb Smirnoff * for read, so that application receives EIO on next 29133a2a37bSGleb Smirnoff * syscall and eventually closes the socket. 29233a2a37bSGleb Smirnoff */ 29333a2a37bSGleb Smirnoff so->so_proto->pr_usrreqs->pru_abort(so); 29433a2a37bSGleb Smirnoff so->so_error = EIO; 29533a2a37bSGleb Smirnoff 296cec06a3eSJohn Baldwin mb_free_notready(sfio->m, sfio->npages); 297b2e60773SJohn Baldwin #ifdef KERN_TLS 298b2e60773SJohn Baldwin } else if (sfio->tls != NULL && sfio->tls->sw_encrypt != NULL) { 299b2e60773SJohn Baldwin /* 300b2e60773SJohn Baldwin * I/O operation is complete, but we still need to 301b2e60773SJohn Baldwin * encrypt. We cannot do this in the interrupt thread 302b2e60773SJohn Baldwin * of the disk controller, so forward the mbufs to a 303b2e60773SJohn Baldwin * different thread. 304b2e60773SJohn Baldwin * 305b2e60773SJohn Baldwin * Donate the socket reference from sfio to rather 306b2e60773SJohn Baldwin * than explicitly invoking soref(). 307b2e60773SJohn Baldwin */ 308b2e60773SJohn Baldwin ktls_enqueue(sfio->m, so, sfio->npages); 309b2e60773SJohn Baldwin goto out_with_ref; 310b2e60773SJohn Baldwin #endif 311b4f55763SGleb Smirnoff } else 31233a2a37bSGleb Smirnoff (void)(so->so_proto->pr_usrreqs->pru_ready)(so, sfio->m, 31333a2a37bSGleb Smirnoff sfio->npages); 31433a2a37bSGleb Smirnoff 315d37aa3ccSGleb Smirnoff SOCK_LOCK(so); 316d37aa3ccSGleb Smirnoff sorele(so); 317b2e60773SJohn Baldwin #ifdef KERN_TLS 318b2e60773SJohn Baldwin out_with_ref: 319b2e60773SJohn Baldwin #endif 320b4f55763SGleb Smirnoff CURVNET_RESTORE(); 32133a2a37bSGleb Smirnoff free(sfio, M_TEMP); 32233a2a37bSGleb Smirnoff } 32333a2a37bSGleb Smirnoff 32433a2a37bSGleb Smirnoff /* 32533a2a37bSGleb Smirnoff * Iterate through pages vector and request paging for non-valid pages. 32633a2a37bSGleb Smirnoff */ 32733a2a37bSGleb Smirnoff static int 328fca79580SAlan Somers sendfile_swapin(vm_object_t obj, struct sf_io *sfio, int *nios, off_t off, 329fca79580SAlan Somers off_t len, int npages, int rhpages, int flags) 33033a2a37bSGleb Smirnoff { 33133a2a37bSGleb Smirnoff vm_page_t *pa = sfio->pa; 332fca79580SAlan Somers int grabbed; 33333a2a37bSGleb Smirnoff 334fca79580SAlan Somers *nios = 0; 33533a2a37bSGleb Smirnoff flags = (flags & SF_NODISKIO) ? VM_ALLOC_NOWAIT : 0; 33633a2a37bSGleb Smirnoff 33733a2a37bSGleb Smirnoff /* 33833a2a37bSGleb Smirnoff * First grab all the pages and wire them. Note that we grab 33933a2a37bSGleb Smirnoff * only required pages. Readahead pages are dealt with later. 34033a2a37bSGleb Smirnoff */ 34133a2a37bSGleb Smirnoff VM_OBJECT_WLOCK(obj); 342af0460beSMark Johnston 343af0460beSMark Johnston grabbed = vm_page_grab_pages(obj, OFF_TO_IDX(off), 344af0460beSMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_WIRED | flags, pa, npages); 345af0460beSMark Johnston if (grabbed < npages) { 346af0460beSMark Johnston for (int i = grabbed; i < npages; i++) 347af0460beSMark Johnston pa[i] = NULL; 348af0460beSMark Johnston npages = grabbed; 34933a2a37bSGleb Smirnoff rhpages = 0; 35033a2a37bSGleb Smirnoff } 35133a2a37bSGleb Smirnoff 35233a2a37bSGleb Smirnoff for (int i = 0; i < npages;) { 353fca79580SAlan Somers int j, a, count, rv; 35433a2a37bSGleb Smirnoff 35533a2a37bSGleb Smirnoff /* Skip valid pages. */ 35633a2a37bSGleb Smirnoff if (vm_page_is_valid(pa[i], vmoff(i, off) & PAGE_MASK, 35733a2a37bSGleb Smirnoff xfsize(i, npages, off, len))) { 35833a2a37bSGleb Smirnoff vm_page_xunbusy(pa[i]); 35933a2a37bSGleb Smirnoff SFSTAT_INC(sf_pages_valid); 36033a2a37bSGleb Smirnoff i++; 36133a2a37bSGleb Smirnoff continue; 36233a2a37bSGleb Smirnoff } 36333a2a37bSGleb Smirnoff 36433a2a37bSGleb Smirnoff /* 3655dba303dSGleb Smirnoff * Next page is invalid. Check if it belongs to pager. It 3665dba303dSGleb Smirnoff * may not be there, which is a regular situation for shmem 3675dba303dSGleb Smirnoff * pager. For vnode pager this happens only in case of 3685dba303dSGleb Smirnoff * a sparse file. 36933a2a37bSGleb Smirnoff * 37033a2a37bSGleb Smirnoff * Important feature of vm_pager_has_page() is the hint 37133a2a37bSGleb Smirnoff * stored in 'a', about how many pages we can pagein after 37233a2a37bSGleb Smirnoff * this page in a single I/O. 37333a2a37bSGleb Smirnoff */ 3745dba303dSGleb Smirnoff if (!vm_pager_has_page(obj, OFF_TO_IDX(vmoff(i, off)), NULL, 3755dba303dSGleb Smirnoff &a)) { 37633a2a37bSGleb Smirnoff pmap_zero_page(pa[i]); 37733a2a37bSGleb Smirnoff pa[i]->valid = VM_PAGE_BITS_ALL; 3786921451dSAlan Cox MPASS(pa[i]->dirty == 0); 37933a2a37bSGleb Smirnoff vm_page_xunbusy(pa[i]); 38033a2a37bSGleb Smirnoff i++; 38133a2a37bSGleb Smirnoff continue; 3825dba303dSGleb Smirnoff } 38333a2a37bSGleb Smirnoff 38433a2a37bSGleb Smirnoff /* 38533a2a37bSGleb Smirnoff * We want to pagein as many pages as possible, limited only 38633a2a37bSGleb Smirnoff * by the 'a' hint and actual request. 38733a2a37bSGleb Smirnoff */ 38833a2a37bSGleb Smirnoff count = min(a + 1, npages - i); 38933a2a37bSGleb Smirnoff 3905dba303dSGleb Smirnoff /* 3915dba303dSGleb Smirnoff * We should not pagein into a valid page, thus we first trim 3925dba303dSGleb Smirnoff * any valid pages off the end of request, and substitute 3935dba303dSGleb Smirnoff * to bogus_page those, that are in the middle. 3945dba303dSGleb Smirnoff */ 3955dba303dSGleb Smirnoff for (j = i + count - 1; j > i; j--) { 3965dba303dSGleb Smirnoff if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK, 3975dba303dSGleb Smirnoff xfsize(j, npages, off, len))) { 3985dba303dSGleb Smirnoff count--; 3995dba303dSGleb Smirnoff rhpages = 0; 4005dba303dSGleb Smirnoff } else 4015dba303dSGleb Smirnoff break; 4025dba303dSGleb Smirnoff } 4035dba303dSGleb Smirnoff for (j = i + 1; j < i + count - 1; j++) 4045dba303dSGleb Smirnoff if (vm_page_is_valid(pa[j], vmoff(j, off) & PAGE_MASK, 4055dba303dSGleb Smirnoff xfsize(j, npages, off, len))) { 4065dba303dSGleb Smirnoff vm_page_xunbusy(pa[j]); 4075dba303dSGleb Smirnoff SFSTAT_INC(sf_pages_valid); 4085dba303dSGleb Smirnoff SFSTAT_INC(sf_pages_bogus); 4095dba303dSGleb Smirnoff pa[j] = bogus_page; 4105dba303dSGleb Smirnoff } 4115dba303dSGleb Smirnoff 41233a2a37bSGleb Smirnoff refcount_acquire(&sfio->nios); 41333a2a37bSGleb Smirnoff rv = vm_pager_get_pages_async(obj, pa + i, count, NULL, 41433a2a37bSGleb Smirnoff i + count == npages ? &rhpages : NULL, 41533a2a37bSGleb Smirnoff &sendfile_iodone, sfio); 416fca79580SAlan Somers if (rv != VM_PAGER_OK) { 4170367bca4SAlan Somers for (j = i; j < i + count; j++) { 418*fee2a2faSMark Johnston if (pa[j] != bogus_page) 4190367bca4SAlan Somers vm_page_unwire(pa[j], PQ_INACTIVE); 420fca79580SAlan Somers } 421fca79580SAlan Somers VM_OBJECT_WUNLOCK(obj); 4220367bca4SAlan Somers return (EIO); 423fca79580SAlan Somers } 42433a2a37bSGleb Smirnoff 42533a2a37bSGleb Smirnoff SFSTAT_INC(sf_iocnt); 42633a2a37bSGleb Smirnoff SFSTAT_ADD(sf_pages_read, count); 42733a2a37bSGleb Smirnoff if (i + count == npages) 42833a2a37bSGleb Smirnoff SFSTAT_ADD(sf_rhpages_read, rhpages); 42933a2a37bSGleb Smirnoff 4305dba303dSGleb Smirnoff /* 4315dba303dSGleb Smirnoff * Restore the valid page pointers. They are already 4325dba303dSGleb Smirnoff * unbusied, but still wired. 4335dba303dSGleb Smirnoff */ 4345dba303dSGleb Smirnoff for (j = i; j < i + count; j++) 4355dba303dSGleb Smirnoff if (pa[j] == bogus_page) { 4365dba303dSGleb Smirnoff pa[j] = vm_page_lookup(obj, 4375dba303dSGleb Smirnoff OFF_TO_IDX(vmoff(j, off))); 4385dba303dSGleb Smirnoff KASSERT(pa[j], ("%s: page %p[%d] disappeared", 4395dba303dSGleb Smirnoff __func__, pa, j)); 4405dba303dSGleb Smirnoff 4415dba303dSGleb Smirnoff } 44233a2a37bSGleb Smirnoff i += count; 443fca79580SAlan Somers (*nios)++; 44433a2a37bSGleb Smirnoff } 44533a2a37bSGleb Smirnoff 44633a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 44733a2a37bSGleb Smirnoff 448fca79580SAlan Somers if (*nios == 0 && npages != 0) 44933a2a37bSGleb Smirnoff SFSTAT_INC(sf_noiocnt); 45033a2a37bSGleb Smirnoff 451fca79580SAlan Somers return (0); 45233a2a37bSGleb Smirnoff } 45333a2a37bSGleb Smirnoff 45433a2a37bSGleb Smirnoff static int 45533a2a37bSGleb Smirnoff sendfile_getobj(struct thread *td, struct file *fp, vm_object_t *obj_res, 45633a2a37bSGleb Smirnoff struct vnode **vp_res, struct shmfd **shmfd_res, off_t *obj_size, 45733a2a37bSGleb Smirnoff int *bsize) 45833a2a37bSGleb Smirnoff { 45933a2a37bSGleb Smirnoff struct vattr va; 46033a2a37bSGleb Smirnoff vm_object_t obj; 46133a2a37bSGleb Smirnoff struct vnode *vp; 46233a2a37bSGleb Smirnoff struct shmfd *shmfd; 46333a2a37bSGleb Smirnoff int error; 46433a2a37bSGleb Smirnoff 46533a2a37bSGleb Smirnoff vp = *vp_res = NULL; 46633a2a37bSGleb Smirnoff obj = NULL; 46733a2a37bSGleb Smirnoff shmfd = *shmfd_res = NULL; 46833a2a37bSGleb Smirnoff *bsize = 0; 46933a2a37bSGleb Smirnoff 47033a2a37bSGleb Smirnoff /* 47133a2a37bSGleb Smirnoff * The file descriptor must be a regular file and have a 47233a2a37bSGleb Smirnoff * backing VM object. 47333a2a37bSGleb Smirnoff */ 47433a2a37bSGleb Smirnoff if (fp->f_type == DTYPE_VNODE) { 47533a2a37bSGleb Smirnoff vp = fp->f_vnode; 47633a2a37bSGleb Smirnoff vn_lock(vp, LK_SHARED | LK_RETRY); 47733a2a37bSGleb Smirnoff if (vp->v_type != VREG) { 47833a2a37bSGleb Smirnoff error = EINVAL; 47933a2a37bSGleb Smirnoff goto out; 48033a2a37bSGleb Smirnoff } 48133a2a37bSGleb Smirnoff *bsize = vp->v_mount->mnt_stat.f_iosize; 48233a2a37bSGleb Smirnoff error = VOP_GETATTR(vp, &va, td->td_ucred); 48333a2a37bSGleb Smirnoff if (error != 0) 48433a2a37bSGleb Smirnoff goto out; 48533a2a37bSGleb Smirnoff *obj_size = va.va_size; 48633a2a37bSGleb Smirnoff obj = vp->v_object; 48733a2a37bSGleb Smirnoff if (obj == NULL) { 48833a2a37bSGleb Smirnoff error = EINVAL; 48933a2a37bSGleb Smirnoff goto out; 49033a2a37bSGleb Smirnoff } 49133a2a37bSGleb Smirnoff } else if (fp->f_type == DTYPE_SHM) { 49233a2a37bSGleb Smirnoff error = 0; 49333a2a37bSGleb Smirnoff shmfd = fp->f_data; 49433a2a37bSGleb Smirnoff obj = shmfd->shm_object; 49533a2a37bSGleb Smirnoff *obj_size = shmfd->shm_size; 49633a2a37bSGleb Smirnoff } else { 49733a2a37bSGleb Smirnoff error = EINVAL; 49833a2a37bSGleb Smirnoff goto out; 49933a2a37bSGleb Smirnoff } 50033a2a37bSGleb Smirnoff 50133a2a37bSGleb Smirnoff VM_OBJECT_WLOCK(obj); 50233a2a37bSGleb Smirnoff if ((obj->flags & OBJ_DEAD) != 0) { 50333a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 50433a2a37bSGleb Smirnoff error = EBADF; 50533a2a37bSGleb Smirnoff goto out; 50633a2a37bSGleb Smirnoff } 50733a2a37bSGleb Smirnoff 50833a2a37bSGleb Smirnoff /* 50933a2a37bSGleb Smirnoff * Temporarily increase the backing VM object's reference 51033a2a37bSGleb Smirnoff * count so that a forced reclamation of its vnode does not 51133a2a37bSGleb Smirnoff * immediately destroy it. 51233a2a37bSGleb Smirnoff */ 51333a2a37bSGleb Smirnoff vm_object_reference_locked(obj); 51433a2a37bSGleb Smirnoff VM_OBJECT_WUNLOCK(obj); 51533a2a37bSGleb Smirnoff *obj_res = obj; 51633a2a37bSGleb Smirnoff *vp_res = vp; 51733a2a37bSGleb Smirnoff *shmfd_res = shmfd; 51833a2a37bSGleb Smirnoff 51933a2a37bSGleb Smirnoff out: 52033a2a37bSGleb Smirnoff if (vp != NULL) 52133a2a37bSGleb Smirnoff VOP_UNLOCK(vp, 0); 52233a2a37bSGleb Smirnoff return (error); 52333a2a37bSGleb Smirnoff } 52433a2a37bSGleb Smirnoff 52533a2a37bSGleb Smirnoff static int 52633a2a37bSGleb Smirnoff sendfile_getsock(struct thread *td, int s, struct file **sock_fp, 52733a2a37bSGleb Smirnoff struct socket **so) 52833a2a37bSGleb Smirnoff { 52933a2a37bSGleb Smirnoff int error; 53033a2a37bSGleb Smirnoff 53133a2a37bSGleb Smirnoff *sock_fp = NULL; 53233a2a37bSGleb Smirnoff *so = NULL; 53333a2a37bSGleb Smirnoff 53433a2a37bSGleb Smirnoff /* 53533a2a37bSGleb Smirnoff * The socket must be a stream socket and connected. 53633a2a37bSGleb Smirnoff */ 537cbd92ce6SMatt Macy error = getsock_cap(td, s, &cap_send_rights, 53885b0f9deSMariusz Zaborski sock_fp, NULL, NULL); 53933a2a37bSGleb Smirnoff if (error != 0) 54033a2a37bSGleb Smirnoff return (error); 54133a2a37bSGleb Smirnoff *so = (*sock_fp)->f_data; 54233a2a37bSGleb Smirnoff if ((*so)->so_type != SOCK_STREAM) 54333a2a37bSGleb Smirnoff return (EINVAL); 54447f1ea51SGleb Smirnoff if (SOLISTENING(*so)) 54547f1ea51SGleb Smirnoff return (ENOTCONN); 54633a2a37bSGleb Smirnoff return (0); 54733a2a37bSGleb Smirnoff } 54833a2a37bSGleb Smirnoff 54933a2a37bSGleb Smirnoff int 55033a2a37bSGleb Smirnoff vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, 55133a2a37bSGleb Smirnoff struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags, 5529c64cfe5SGleb Smirnoff struct thread *td) 55333a2a37bSGleb Smirnoff { 55433a2a37bSGleb Smirnoff struct file *sock_fp; 55533a2a37bSGleb Smirnoff struct vnode *vp; 55633a2a37bSGleb Smirnoff struct vm_object *obj; 55733a2a37bSGleb Smirnoff struct socket *so; 558b2e60773SJohn Baldwin #ifdef KERN_TLS 559b2e60773SJohn Baldwin struct ktls_session *tls; 560b2e60773SJohn Baldwin #endif 561cec06a3eSJohn Baldwin struct mbuf_ext_pgs *ext_pgs; 56233a2a37bSGleb Smirnoff struct mbuf *m, *mh, *mhtail; 56333a2a37bSGleb Smirnoff struct sf_buf *sf; 56433a2a37bSGleb Smirnoff struct shmfd *shmfd; 56533a2a37bSGleb Smirnoff struct sendfile_sync *sfs; 56633a2a37bSGleb Smirnoff struct vattr va; 56733a2a37bSGleb Smirnoff off_t off, sbytes, rem, obj_size; 568cec06a3eSJohn Baldwin int bsize, error, ext_pgs_idx, hdrlen, max_pgs, softerr; 569b2e60773SJohn Baldwin #ifdef KERN_TLS 570b2e60773SJohn Baldwin int tls_enq_cnt; 571b2e60773SJohn Baldwin #endif 572cec06a3eSJohn Baldwin bool use_ext_pgs; 57333a2a37bSGleb Smirnoff 57433a2a37bSGleb Smirnoff obj = NULL; 57533a2a37bSGleb Smirnoff so = NULL; 57633a2a37bSGleb Smirnoff m = mh = NULL; 57733a2a37bSGleb Smirnoff sfs = NULL; 578b2e60773SJohn Baldwin #ifdef KERN_TLS 579b2e60773SJohn Baldwin tls = NULL; 580b2e60773SJohn Baldwin #endif 5819c64cfe5SGleb Smirnoff hdrlen = sbytes = 0; 58233a2a37bSGleb Smirnoff softerr = 0; 583cec06a3eSJohn Baldwin use_ext_pgs = false; 58433a2a37bSGleb Smirnoff 58533a2a37bSGleb Smirnoff error = sendfile_getobj(td, fp, &obj, &vp, &shmfd, &obj_size, &bsize); 58633a2a37bSGleb Smirnoff if (error != 0) 58733a2a37bSGleb Smirnoff return (error); 58833a2a37bSGleb Smirnoff 58933a2a37bSGleb Smirnoff error = sendfile_getsock(td, sockfd, &sock_fp, &so); 59033a2a37bSGleb Smirnoff if (error != 0) 59133a2a37bSGleb Smirnoff goto out; 59233a2a37bSGleb Smirnoff 59333a2a37bSGleb Smirnoff #ifdef MAC 59433a2a37bSGleb Smirnoff error = mac_socket_check_send(td->td_ucred, so); 59533a2a37bSGleb Smirnoff if (error != 0) 59633a2a37bSGleb Smirnoff goto out; 59733a2a37bSGleb Smirnoff #endif 59833a2a37bSGleb Smirnoff 59933a2a37bSGleb Smirnoff SFSTAT_INC(sf_syscalls); 60033a2a37bSGleb Smirnoff SFSTAT_ADD(sf_rhpages_requested, SF_READAHEAD(flags)); 60133a2a37bSGleb Smirnoff 60233a2a37bSGleb Smirnoff if (flags & SF_SYNC) { 60333a2a37bSGleb Smirnoff sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO); 60433a2a37bSGleb Smirnoff mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF); 60533a2a37bSGleb Smirnoff cv_init(&sfs->cv, "sendfile"); 60633a2a37bSGleb Smirnoff } 60733a2a37bSGleb Smirnoff 60833a2a37bSGleb Smirnoff rem = nbytes ? omin(nbytes, obj_size - offset) : obj_size - offset; 60933a2a37bSGleb Smirnoff 61033a2a37bSGleb Smirnoff /* 61133a2a37bSGleb Smirnoff * Protect against multiple writers to the socket. 61233a2a37bSGleb Smirnoff * 61333a2a37bSGleb Smirnoff * XXXRW: Historically this has assumed non-interruptibility, so now 61433a2a37bSGleb Smirnoff * we implement that, but possibly shouldn't. 61533a2a37bSGleb Smirnoff */ 61633a2a37bSGleb Smirnoff (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR); 617b2e60773SJohn Baldwin #ifdef KERN_TLS 618b2e60773SJohn Baldwin tls = ktls_hold(so->so_snd.sb_tls_info); 619b2e60773SJohn Baldwin #endif 62033a2a37bSGleb Smirnoff 62133a2a37bSGleb Smirnoff /* 62233a2a37bSGleb Smirnoff * Loop through the pages of the file, starting with the requested 62333a2a37bSGleb Smirnoff * offset. Get a file page (do I/O if necessary), map the file page 62433a2a37bSGleb Smirnoff * into an sf_buf, attach an mbuf header to the sf_buf, and queue 62533a2a37bSGleb Smirnoff * it on the socket. 62633a2a37bSGleb Smirnoff * This is done in two loops. The inner loop turns as many pages 62733a2a37bSGleb Smirnoff * as it can, up to available socket buffer space, without blocking 62833a2a37bSGleb Smirnoff * into mbufs to have it bulk delivered into the socket send buffer. 62933a2a37bSGleb Smirnoff * The outer loop checks the state and available space of the socket 63033a2a37bSGleb Smirnoff * and takes care of the overall progress. 63133a2a37bSGleb Smirnoff */ 63233a2a37bSGleb Smirnoff for (off = offset; rem > 0; ) { 63333a2a37bSGleb Smirnoff struct sf_io *sfio; 63433a2a37bSGleb Smirnoff vm_page_t *pa; 63533a2a37bSGleb Smirnoff struct mbuf *mtail; 63633a2a37bSGleb Smirnoff int nios, space, npages, rhpages; 63733a2a37bSGleb Smirnoff 63833a2a37bSGleb Smirnoff mtail = NULL; 63933a2a37bSGleb Smirnoff /* 64033a2a37bSGleb Smirnoff * Check the socket state for ongoing connection, 64133a2a37bSGleb Smirnoff * no errors and space in socket buffer. 64233a2a37bSGleb Smirnoff * If space is low allow for the remainder of the 64333a2a37bSGleb Smirnoff * file to be processed if it fits the socket buffer. 64433a2a37bSGleb Smirnoff * Otherwise block in waiting for sufficient space 64533a2a37bSGleb Smirnoff * to proceed, or if the socket is nonblocking, return 64633a2a37bSGleb Smirnoff * to userland with EAGAIN while reporting how far 64733a2a37bSGleb Smirnoff * we've come. 64833a2a37bSGleb Smirnoff * We wait until the socket buffer has significant free 64933a2a37bSGleb Smirnoff * space to do bulk sends. This makes good use of file 65033a2a37bSGleb Smirnoff * system read ahead and allows packet segmentation 65133a2a37bSGleb Smirnoff * offloading hardware to take over lots of work. If 65233a2a37bSGleb Smirnoff * we were not careful here we would send off only one 65333a2a37bSGleb Smirnoff * sfbuf at a time. 65433a2a37bSGleb Smirnoff */ 65533a2a37bSGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 65633a2a37bSGleb Smirnoff if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2) 65733a2a37bSGleb Smirnoff so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2; 65833a2a37bSGleb Smirnoff retry_space: 65933a2a37bSGleb Smirnoff if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 66033a2a37bSGleb Smirnoff error = EPIPE; 66133a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 66233a2a37bSGleb Smirnoff goto done; 66333a2a37bSGleb Smirnoff } else if (so->so_error) { 66433a2a37bSGleb Smirnoff error = so->so_error; 66533a2a37bSGleb Smirnoff so->so_error = 0; 66633a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 66733a2a37bSGleb Smirnoff goto done; 66833a2a37bSGleb Smirnoff } 6691f9916edSSean Bruno if ((so->so_state & SS_ISCONNECTED) == 0) { 6701f9916edSSean Bruno SOCKBUF_UNLOCK(&so->so_snd); 6711f9916edSSean Bruno error = ENOTCONN; 6721f9916edSSean Bruno goto done; 6731f9916edSSean Bruno } 6741f9916edSSean Bruno 67533a2a37bSGleb Smirnoff space = sbspace(&so->so_snd); 67633a2a37bSGleb Smirnoff if (space < rem && 67733a2a37bSGleb Smirnoff (space <= 0 || 67833a2a37bSGleb Smirnoff space < so->so_snd.sb_lowat)) { 67933a2a37bSGleb Smirnoff if (so->so_state & SS_NBIO) { 68033a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 68133a2a37bSGleb Smirnoff error = EAGAIN; 68233a2a37bSGleb Smirnoff goto done; 68333a2a37bSGleb Smirnoff } 68433a2a37bSGleb Smirnoff /* 68533a2a37bSGleb Smirnoff * sbwait drops the lock while sleeping. 68633a2a37bSGleb Smirnoff * When we loop back to retry_space the 68733a2a37bSGleb Smirnoff * state may have changed and we retest 68833a2a37bSGleb Smirnoff * for it. 68933a2a37bSGleb Smirnoff */ 69033a2a37bSGleb Smirnoff error = sbwait(&so->so_snd); 69133a2a37bSGleb Smirnoff /* 69233a2a37bSGleb Smirnoff * An error from sbwait usually indicates that we've 69333a2a37bSGleb Smirnoff * been interrupted by a signal. If we've sent anything 69433a2a37bSGleb Smirnoff * then return bytes sent, otherwise return the error. 69533a2a37bSGleb Smirnoff */ 69633a2a37bSGleb Smirnoff if (error != 0) { 69733a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 69833a2a37bSGleb Smirnoff goto done; 69933a2a37bSGleb Smirnoff } 70033a2a37bSGleb Smirnoff goto retry_space; 70133a2a37bSGleb Smirnoff } 70233a2a37bSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 70333a2a37bSGleb Smirnoff 70433a2a37bSGleb Smirnoff /* 7059c64cfe5SGleb Smirnoff * At the beginning of the first loop check if any headers 7069c64cfe5SGleb Smirnoff * are specified and copy them into mbufs. Reduce space in 7079c64cfe5SGleb Smirnoff * the socket buffer by the size of the header mbuf chain. 7089c64cfe5SGleb Smirnoff * Clear hdr_uio here and hdrlen at the end of the first loop. 70933a2a37bSGleb Smirnoff */ 7109c64cfe5SGleb Smirnoff if (hdr_uio != NULL && hdr_uio->uio_resid > 0) { 7119c64cfe5SGleb Smirnoff hdr_uio->uio_td = td; 7129c64cfe5SGleb Smirnoff hdr_uio->uio_rw = UIO_WRITE; 713b2e60773SJohn Baldwin #ifdef KERN_TLS 714b2e60773SJohn Baldwin if (tls != NULL) 715b2e60773SJohn Baldwin mh = m_uiotombuf(hdr_uio, M_WAITOK, space, 716b2e60773SJohn Baldwin tls->params.max_frame_len, M_NOMAP); 717b2e60773SJohn Baldwin else 718b2e60773SJohn Baldwin #endif 719b2e60773SJohn Baldwin mh = m_uiotombuf(hdr_uio, M_WAITOK, 720b2e60773SJohn Baldwin space, 0, 0); 7219c64cfe5SGleb Smirnoff hdrlen = m_length(mh, &mhtail); 72233a2a37bSGleb Smirnoff space -= hdrlen; 723a2d8f9d2SGleb Smirnoff /* 724a2d8f9d2SGleb Smirnoff * If header consumed all the socket buffer space, 725a2d8f9d2SGleb Smirnoff * don't waste CPU cycles and jump to the end. 726a2d8f9d2SGleb Smirnoff */ 727a2d8f9d2SGleb Smirnoff if (space == 0) { 728a2d8f9d2SGleb Smirnoff sfio = NULL; 729a2d8f9d2SGleb Smirnoff nios = 0; 730a2d8f9d2SGleb Smirnoff goto prepend_header; 731a2d8f9d2SGleb Smirnoff } 7329c64cfe5SGleb Smirnoff hdr_uio = NULL; 7339c64cfe5SGleb Smirnoff } 73433a2a37bSGleb Smirnoff 73533a2a37bSGleb Smirnoff if (vp != NULL) { 73633a2a37bSGleb Smirnoff error = vn_lock(vp, LK_SHARED); 73733a2a37bSGleb Smirnoff if (error != 0) 73833a2a37bSGleb Smirnoff goto done; 73933a2a37bSGleb Smirnoff error = VOP_GETATTR(vp, &va, td->td_ucred); 74033a2a37bSGleb Smirnoff if (error != 0 || off >= va.va_size) { 74133a2a37bSGleb Smirnoff VOP_UNLOCK(vp, 0); 74233a2a37bSGleb Smirnoff goto done; 74333a2a37bSGleb Smirnoff } 74433a2a37bSGleb Smirnoff if (va.va_size != obj_size) { 74533a2a37bSGleb Smirnoff obj_size = va.va_size; 7469e3c8bd3SGleb Smirnoff rem = nbytes ? 7479e3c8bd3SGleb Smirnoff omin(nbytes + offset, obj_size) : obj_size; 7489e3c8bd3SGleb Smirnoff rem -= off; 74933a2a37bSGleb Smirnoff } 75033a2a37bSGleb Smirnoff } 75133a2a37bSGleb Smirnoff 75233a2a37bSGleb Smirnoff if (space > rem) 75333a2a37bSGleb Smirnoff space = rem; 754cec06a3eSJohn Baldwin else if (space > PAGE_SIZE) { 755cec06a3eSJohn Baldwin /* 756cec06a3eSJohn Baldwin * Use page boundaries when possible for large 757cec06a3eSJohn Baldwin * requests. 758cec06a3eSJohn Baldwin */ 759cec06a3eSJohn Baldwin if (off & PAGE_MASK) 760cec06a3eSJohn Baldwin space -= (PAGE_SIZE - (off & PAGE_MASK)); 761cec06a3eSJohn Baldwin space = trunc_page(space); 762cec06a3eSJohn Baldwin if (off & PAGE_MASK) 763cec06a3eSJohn Baldwin space += (PAGE_SIZE - (off & PAGE_MASK)); 764cec06a3eSJohn Baldwin } 76533a2a37bSGleb Smirnoff 76633a2a37bSGleb Smirnoff npages = howmany(space + (off & PAGE_MASK), PAGE_SIZE); 76733a2a37bSGleb Smirnoff 76833a2a37bSGleb Smirnoff /* 76933a2a37bSGleb Smirnoff * Calculate maximum allowed number of pages for readahead 77000b5ffdeSGleb Smirnoff * at this iteration. If SF_USER_READAHEAD was set, we don't 77100b5ffdeSGleb Smirnoff * do any heuristics and use exactly the value supplied by 77200b5ffdeSGleb Smirnoff * application. Otherwise, we allow readahead up to "rem". 77333a2a37bSGleb Smirnoff * If application wants more, let it be, but there is no 77433a2a37bSGleb Smirnoff * reason to go above MAXPHYS. Also check against "obj_size", 77533a2a37bSGleb Smirnoff * since vm_pager_has_page() can hint beyond EOF. 77633a2a37bSGleb Smirnoff */ 77700b5ffdeSGleb Smirnoff if (flags & SF_USER_READAHEAD) { 77800b5ffdeSGleb Smirnoff rhpages = SF_READAHEAD(flags); 77900b5ffdeSGleb Smirnoff } else { 78000b5ffdeSGleb Smirnoff rhpages = howmany(rem + (off & PAGE_MASK), PAGE_SIZE) - 78100b5ffdeSGleb Smirnoff npages; 78233a2a37bSGleb Smirnoff rhpages += SF_READAHEAD(flags); 78300b5ffdeSGleb Smirnoff } 78433a2a37bSGleb Smirnoff rhpages = min(howmany(MAXPHYS, PAGE_SIZE), rhpages); 78533a2a37bSGleb Smirnoff rhpages = min(howmany(obj_size - trunc_page(off), PAGE_SIZE) - 78633a2a37bSGleb Smirnoff npages, rhpages); 78733a2a37bSGleb Smirnoff 78833a2a37bSGleb Smirnoff sfio = malloc(sizeof(struct sf_io) + 78933a2a37bSGleb Smirnoff npages * sizeof(vm_page_t), M_TEMP, M_WAITOK); 79033a2a37bSGleb Smirnoff refcount_init(&sfio->nios, 1); 791d37aa3ccSGleb Smirnoff sfio->so = so; 79233a2a37bSGleb Smirnoff sfio->error = 0; 79333a2a37bSGleb Smirnoff 794b2e60773SJohn Baldwin #ifdef KERN_TLS 795b2e60773SJohn Baldwin /* 796b2e60773SJohn Baldwin * This doesn't use ktls_hold() because sfio->m will 797b2e60773SJohn Baldwin * also have a reference on 'tls' that will be valid 798b2e60773SJohn Baldwin * for all of sfio's lifetime. 799b2e60773SJohn Baldwin */ 800b2e60773SJohn Baldwin sfio->tls = tls; 801b2e60773SJohn Baldwin #endif 802b2e60773SJohn Baldwin 803fca79580SAlan Somers error = sendfile_swapin(obj, sfio, &nios, off, space, npages, 804fca79580SAlan Somers rhpages, flags); 8050367bca4SAlan Somers if (error != 0) { 806fca79580SAlan Somers if (vp != NULL) 807fca79580SAlan Somers VOP_UNLOCK(vp, 0); 8080367bca4SAlan Somers free(sfio, M_TEMP); 809fca79580SAlan Somers goto done; 810fca79580SAlan Somers } 81133a2a37bSGleb Smirnoff 81233a2a37bSGleb Smirnoff /* 81333a2a37bSGleb Smirnoff * Loop and construct maximum sized mbuf chain to be bulk 81433a2a37bSGleb Smirnoff * dumped into socket buffer. 81533a2a37bSGleb Smirnoff */ 81633a2a37bSGleb Smirnoff pa = sfio->pa; 817cec06a3eSJohn Baldwin 818cec06a3eSJohn Baldwin /* 819cec06a3eSJohn Baldwin * Use unmapped mbufs if enabled for TCP. Unmapped 820cec06a3eSJohn Baldwin * bufs are restricted to TCP as that is what has been 821cec06a3eSJohn Baldwin * tested. In particular, unmapped mbufs have not 822cec06a3eSJohn Baldwin * been tested with UNIX-domain sockets. 823b2e60773SJohn Baldwin * 824b2e60773SJohn Baldwin * TLS frames always require unmapped mbufs. 825cec06a3eSJohn Baldwin */ 826b2e60773SJohn Baldwin if ((mb_use_ext_pgs && 827b2e60773SJohn Baldwin so->so_proto->pr_protocol == IPPROTO_TCP) 828b2e60773SJohn Baldwin #ifdef KERN_TLS 829b2e60773SJohn Baldwin || tls != NULL 830b2e60773SJohn Baldwin #endif 831b2e60773SJohn Baldwin ) { 832cec06a3eSJohn Baldwin use_ext_pgs = true; 833b2e60773SJohn Baldwin #ifdef KERN_TLS 834b2e60773SJohn Baldwin if (tls != NULL) 835b2e60773SJohn Baldwin max_pgs = num_pages(tls->params.max_frame_len); 836b2e60773SJohn Baldwin else 837b2e60773SJohn Baldwin #endif 838cec06a3eSJohn Baldwin max_pgs = MBUF_PEXT_MAX_PGS; 839cec06a3eSJohn Baldwin 840cec06a3eSJohn Baldwin /* Start at last index, to wrap on first use. */ 841cec06a3eSJohn Baldwin ext_pgs_idx = max_pgs - 1; 842cec06a3eSJohn Baldwin } 843cec06a3eSJohn Baldwin 84433a2a37bSGleb Smirnoff for (int i = 0; i < npages; i++) { 84533a2a37bSGleb Smirnoff struct mbuf *m0; 84633a2a37bSGleb Smirnoff 84733a2a37bSGleb Smirnoff /* 84833a2a37bSGleb Smirnoff * If a page wasn't grabbed successfully, then 84933a2a37bSGleb Smirnoff * trim the array. Can happen only with SF_NODISKIO. 85033a2a37bSGleb Smirnoff */ 85133a2a37bSGleb Smirnoff if (pa[i] == NULL) { 85233a2a37bSGleb Smirnoff SFSTAT_INC(sf_busy); 85333a2a37bSGleb Smirnoff fixspace(npages, i, off, &space); 85433a2a37bSGleb Smirnoff npages = i; 85533a2a37bSGleb Smirnoff softerr = EBUSY; 85633a2a37bSGleb Smirnoff break; 85733a2a37bSGleb Smirnoff } 85833a2a37bSGleb Smirnoff 859cec06a3eSJohn Baldwin if (use_ext_pgs) { 860cec06a3eSJohn Baldwin off_t xfs; 861cec06a3eSJohn Baldwin 862cec06a3eSJohn Baldwin ext_pgs_idx++; 863cec06a3eSJohn Baldwin if (ext_pgs_idx == max_pgs) { 864cec06a3eSJohn Baldwin m0 = mb_alloc_ext_pgs(M_WAITOK, false, 865cec06a3eSJohn Baldwin sendfile_free_mext_pg); 866cec06a3eSJohn Baldwin 867cec06a3eSJohn Baldwin if (flags & SF_NOCACHE) { 868cec06a3eSJohn Baldwin m0->m_ext.ext_flags |= 869cec06a3eSJohn Baldwin EXT_FLAG_NOCACHE; 870cec06a3eSJohn Baldwin 871cec06a3eSJohn Baldwin /* 872cec06a3eSJohn Baldwin * See comment below regarding 873cec06a3eSJohn Baldwin * ignoring SF_NOCACHE for the 874cec06a3eSJohn Baldwin * last page. 875cec06a3eSJohn Baldwin */ 876cec06a3eSJohn Baldwin if ((npages - i <= max_pgs) && 877cec06a3eSJohn Baldwin ((off + space) & PAGE_MASK) && 878cec06a3eSJohn Baldwin (rem > space || rhpages > 0)) 879cec06a3eSJohn Baldwin m0->m_ext.ext_flags |= 880cec06a3eSJohn Baldwin EXT_FLAG_CACHE_LAST; 881cec06a3eSJohn Baldwin } 882cec06a3eSJohn Baldwin if (sfs != NULL) { 883cec06a3eSJohn Baldwin m0->m_ext.ext_flags |= 884cec06a3eSJohn Baldwin EXT_FLAG_SYNC; 885cec06a3eSJohn Baldwin m0->m_ext.ext_arg2 = sfs; 886cec06a3eSJohn Baldwin mtx_lock(&sfs->mtx); 887cec06a3eSJohn Baldwin sfs->count++; 888cec06a3eSJohn Baldwin mtx_unlock(&sfs->mtx); 889cec06a3eSJohn Baldwin } 890cec06a3eSJohn Baldwin ext_pgs = m0->m_ext.ext_pgs; 891cec06a3eSJohn Baldwin if (i == 0) 892cec06a3eSJohn Baldwin sfio->m = m0; 893cec06a3eSJohn Baldwin ext_pgs_idx = 0; 894cec06a3eSJohn Baldwin 895cec06a3eSJohn Baldwin /* Append to mbuf chain. */ 896cec06a3eSJohn Baldwin if (mtail != NULL) 897cec06a3eSJohn Baldwin mtail->m_next = m0; 898cec06a3eSJohn Baldwin else 899cec06a3eSJohn Baldwin m = m0; 900cec06a3eSJohn Baldwin mtail = m0; 901cec06a3eSJohn Baldwin ext_pgs->first_pg_off = 902cec06a3eSJohn Baldwin vmoff(i, off) & PAGE_MASK; 903cec06a3eSJohn Baldwin } 904cec06a3eSJohn Baldwin if (nios) { 905cec06a3eSJohn Baldwin mtail->m_flags |= M_NOTREADY; 906cec06a3eSJohn Baldwin ext_pgs->nrdy++; 907cec06a3eSJohn Baldwin } 908cec06a3eSJohn Baldwin 909cec06a3eSJohn Baldwin ext_pgs->pa[ext_pgs_idx] = VM_PAGE_TO_PHYS(pa[i]); 910cec06a3eSJohn Baldwin ext_pgs->npgs++; 911cec06a3eSJohn Baldwin xfs = xfsize(i, npages, off, space); 912cec06a3eSJohn Baldwin ext_pgs->last_pg_len = xfs; 913cec06a3eSJohn Baldwin MBUF_EXT_PGS_ASSERT_SANITY(ext_pgs); 914cec06a3eSJohn Baldwin mtail->m_len += xfs; 915cec06a3eSJohn Baldwin mtail->m_ext.ext_size += PAGE_SIZE; 916cec06a3eSJohn Baldwin continue; 917cec06a3eSJohn Baldwin } 918cec06a3eSJohn Baldwin 91933a2a37bSGleb Smirnoff /* 92033a2a37bSGleb Smirnoff * Get a sendfile buf. When allocating the 92133a2a37bSGleb Smirnoff * first buffer for mbuf chain, we usually 92233a2a37bSGleb Smirnoff * wait as long as necessary, but this wait 92333a2a37bSGleb Smirnoff * can be interrupted. For consequent 92433a2a37bSGleb Smirnoff * buffers, do not sleep, since several 92533a2a37bSGleb Smirnoff * threads might exhaust the buffers and then 92633a2a37bSGleb Smirnoff * deadlock. 92733a2a37bSGleb Smirnoff */ 92833a2a37bSGleb Smirnoff sf = sf_buf_alloc(pa[i], 92933a2a37bSGleb Smirnoff m != NULL ? SFB_NOWAIT : SFB_CATCH); 93033a2a37bSGleb Smirnoff if (sf == NULL) { 93133a2a37bSGleb Smirnoff SFSTAT_INC(sf_allocfail); 932*fee2a2faSMark Johnston for (int j = i; j < npages; j++) 93333a2a37bSGleb Smirnoff vm_page_unwire(pa[j], PQ_INACTIVE); 93433a2a37bSGleb Smirnoff if (m == NULL) 93533a2a37bSGleb Smirnoff softerr = ENOBUFS; 93633a2a37bSGleb Smirnoff fixspace(npages, i, off, &space); 93733a2a37bSGleb Smirnoff npages = i; 93833a2a37bSGleb Smirnoff break; 93933a2a37bSGleb Smirnoff } 94033a2a37bSGleb Smirnoff 94133a2a37bSGleb Smirnoff m0 = m_get(M_WAITOK, MT_DATA); 94233a2a37bSGleb Smirnoff m0->m_ext.ext_buf = (char *)sf_buf_kva(sf); 94333a2a37bSGleb Smirnoff m0->m_ext.ext_size = PAGE_SIZE; 94433a2a37bSGleb Smirnoff m0->m_ext.ext_arg1 = sf; 9459c82bec4SGleb Smirnoff m0->m_ext.ext_type = EXT_SFBUF; 9469c82bec4SGleb Smirnoff m0->m_ext.ext_flags = EXT_FLAG_EMBREF; 9479c82bec4SGleb Smirnoff m0->m_ext.ext_free = sendfile_free_mext; 94833a2a37bSGleb Smirnoff /* 94933a2a37bSGleb Smirnoff * SF_NOCACHE sets the page as being freed upon send. 95033a2a37bSGleb Smirnoff * However, we ignore it for the last page in 'space', 95133a2a37bSGleb Smirnoff * if the page is truncated, and we got more data to 95233a2a37bSGleb Smirnoff * send (rem > space), or if we have readahead 95333a2a37bSGleb Smirnoff * configured (rhpages > 0). 95433a2a37bSGleb Smirnoff */ 9559c82bec4SGleb Smirnoff if ((flags & SF_NOCACHE) && 9569c82bec4SGleb Smirnoff (i != npages - 1 || 9579c82bec4SGleb Smirnoff !((off + space) & PAGE_MASK) || 9589c82bec4SGleb Smirnoff !(rem > space || rhpages > 0))) 9599c82bec4SGleb Smirnoff m0->m_ext.ext_flags |= EXT_FLAG_NOCACHE; 9609c82bec4SGleb Smirnoff if (sfs != NULL) { 9619c82bec4SGleb Smirnoff m0->m_ext.ext_flags |= EXT_FLAG_SYNC; 9629c82bec4SGleb Smirnoff m0->m_ext.ext_arg2 = sfs; 9639c82bec4SGleb Smirnoff mtx_lock(&sfs->mtx); 9649c82bec4SGleb Smirnoff sfs->count++; 9659c82bec4SGleb Smirnoff mtx_unlock(&sfs->mtx); 9669c82bec4SGleb Smirnoff } 96756a5f52eSGleb Smirnoff m0->m_ext.ext_count = 1; 96833a2a37bSGleb Smirnoff m0->m_flags |= (M_EXT | M_RDONLY); 96933a2a37bSGleb Smirnoff if (nios) 97033a2a37bSGleb Smirnoff m0->m_flags |= M_NOTREADY; 97133a2a37bSGleb Smirnoff m0->m_data = (char *)sf_buf_kva(sf) + 97233a2a37bSGleb Smirnoff (vmoff(i, off) & PAGE_MASK); 97333a2a37bSGleb Smirnoff m0->m_len = xfsize(i, npages, off, space); 97433a2a37bSGleb Smirnoff 97533a2a37bSGleb Smirnoff if (i == 0) 97633a2a37bSGleb Smirnoff sfio->m = m0; 97733a2a37bSGleb Smirnoff 97833a2a37bSGleb Smirnoff /* Append to mbuf chain. */ 97933a2a37bSGleb Smirnoff if (mtail != NULL) 98033a2a37bSGleb Smirnoff mtail->m_next = m0; 98133a2a37bSGleb Smirnoff else 98233a2a37bSGleb Smirnoff m = m0; 98333a2a37bSGleb Smirnoff mtail = m0; 98433a2a37bSGleb Smirnoff } 98533a2a37bSGleb Smirnoff 98633a2a37bSGleb Smirnoff if (vp != NULL) 98733a2a37bSGleb Smirnoff VOP_UNLOCK(vp, 0); 98833a2a37bSGleb Smirnoff 98933a2a37bSGleb Smirnoff /* Keep track of bytes processed. */ 99033a2a37bSGleb Smirnoff off += space; 99133a2a37bSGleb Smirnoff rem -= space; 99233a2a37bSGleb Smirnoff 99333a2a37bSGleb Smirnoff /* Prepend header, if any. */ 99433a2a37bSGleb Smirnoff if (hdrlen) { 995a2d8f9d2SGleb Smirnoff prepend_header: 99633a2a37bSGleb Smirnoff mhtail->m_next = m; 99733a2a37bSGleb Smirnoff m = mh; 99833a2a37bSGleb Smirnoff mh = NULL; 99933a2a37bSGleb Smirnoff } 100033a2a37bSGleb Smirnoff 100133a2a37bSGleb Smirnoff if (m == NULL) { 100233a2a37bSGleb Smirnoff KASSERT(softerr, ("%s: m NULL, no error", __func__)); 100333a2a37bSGleb Smirnoff error = softerr; 100433a2a37bSGleb Smirnoff free(sfio, M_TEMP); 100533a2a37bSGleb Smirnoff goto done; 100633a2a37bSGleb Smirnoff } 100733a2a37bSGleb Smirnoff 100833a2a37bSGleb Smirnoff /* Add the buffer chain to the socket buffer. */ 100933a2a37bSGleb Smirnoff KASSERT(m_length(m, NULL) == space + hdrlen, 101033a2a37bSGleb Smirnoff ("%s: mlen %u space %d hdrlen %d", 101133a2a37bSGleb Smirnoff __func__, m_length(m, NULL), space, hdrlen)); 101233a2a37bSGleb Smirnoff 101333a2a37bSGleb Smirnoff CURVNET_SET(so->so_vnet); 1014b2e60773SJohn Baldwin #ifdef KERN_TLS 1015b2e60773SJohn Baldwin if (tls != NULL) { 1016b2e60773SJohn Baldwin error = ktls_frame(m, tls, &tls_enq_cnt, 1017b2e60773SJohn Baldwin TLS_RLTYPE_APP); 1018b2e60773SJohn Baldwin if (error != 0) 1019b2e60773SJohn Baldwin goto done; 1020b2e60773SJohn Baldwin } 1021b2e60773SJohn Baldwin #endif 102233a2a37bSGleb Smirnoff if (nios == 0) { 102333a2a37bSGleb Smirnoff /* 102433a2a37bSGleb Smirnoff * If sendfile_swapin() didn't initiate any I/Os, 102533a2a37bSGleb Smirnoff * which happens if all data is cached in VM, then 102633a2a37bSGleb Smirnoff * we can send data right now without the 102733a2a37bSGleb Smirnoff * PRUS_NOTREADY flag. 102833a2a37bSGleb Smirnoff */ 102933a2a37bSGleb Smirnoff free(sfio, M_TEMP); 1030b2e60773SJohn Baldwin #ifdef KERN_TLS 1031b2e60773SJohn Baldwin if (tls != NULL && tls->sw_encrypt != NULL) { 1032b2e60773SJohn Baldwin error = (*so->so_proto->pr_usrreqs->pru_send) 1033b2e60773SJohn Baldwin (so, PRUS_NOTREADY, m, NULL, NULL, td); 1034b2e60773SJohn Baldwin soref(so); 1035b2e60773SJohn Baldwin ktls_enqueue(m, so, tls_enq_cnt); 1036b2e60773SJohn Baldwin } else 1037b2e60773SJohn Baldwin #endif 103833a2a37bSGleb Smirnoff error = (*so->so_proto->pr_usrreqs->pru_send) 103933a2a37bSGleb Smirnoff (so, 0, m, NULL, NULL, td); 104033a2a37bSGleb Smirnoff } else { 104133a2a37bSGleb Smirnoff sfio->npages = npages; 1042d37aa3ccSGleb Smirnoff soref(so); 104333a2a37bSGleb Smirnoff error = (*so->so_proto->pr_usrreqs->pru_send) 104433a2a37bSGleb Smirnoff (so, PRUS_NOTREADY, m, NULL, NULL, td); 104533a2a37bSGleb Smirnoff sendfile_iodone(sfio, NULL, 0, 0); 104633a2a37bSGleb Smirnoff } 104733a2a37bSGleb Smirnoff CURVNET_RESTORE(); 104833a2a37bSGleb Smirnoff 104933a2a37bSGleb Smirnoff m = NULL; /* pru_send always consumes */ 105033a2a37bSGleb Smirnoff if (error) 105133a2a37bSGleb Smirnoff goto done; 105233a2a37bSGleb Smirnoff sbytes += space + hdrlen; 105333a2a37bSGleb Smirnoff if (hdrlen) 105433a2a37bSGleb Smirnoff hdrlen = 0; 105533a2a37bSGleb Smirnoff if (softerr) { 105633a2a37bSGleb Smirnoff error = softerr; 105733a2a37bSGleb Smirnoff goto done; 105833a2a37bSGleb Smirnoff } 105933a2a37bSGleb Smirnoff } 106033a2a37bSGleb Smirnoff 106133a2a37bSGleb Smirnoff /* 106233a2a37bSGleb Smirnoff * Send trailers. Wimp out and use writev(2). 106333a2a37bSGleb Smirnoff */ 106433a2a37bSGleb Smirnoff if (trl_uio != NULL) { 106533a2a37bSGleb Smirnoff sbunlock(&so->so_snd); 106633a2a37bSGleb Smirnoff error = kern_writev(td, sockfd, trl_uio); 106733a2a37bSGleb Smirnoff if (error == 0) 106833a2a37bSGleb Smirnoff sbytes += td->td_retval[0]; 106933a2a37bSGleb Smirnoff goto out; 107033a2a37bSGleb Smirnoff } 107133a2a37bSGleb Smirnoff 107233a2a37bSGleb Smirnoff done: 107333a2a37bSGleb Smirnoff sbunlock(&so->so_snd); 107433a2a37bSGleb Smirnoff out: 107533a2a37bSGleb Smirnoff /* 107633a2a37bSGleb Smirnoff * If there was no error we have to clear td->td_retval[0] 107733a2a37bSGleb Smirnoff * because it may have been set by writev. 107833a2a37bSGleb Smirnoff */ 107933a2a37bSGleb Smirnoff if (error == 0) { 108033a2a37bSGleb Smirnoff td->td_retval[0] = 0; 108133a2a37bSGleb Smirnoff } 108233a2a37bSGleb Smirnoff if (sent != NULL) { 108333a2a37bSGleb Smirnoff (*sent) = sbytes; 108433a2a37bSGleb Smirnoff } 108533a2a37bSGleb Smirnoff if (obj != NULL) 108633a2a37bSGleb Smirnoff vm_object_deallocate(obj); 108733a2a37bSGleb Smirnoff if (so) 108833a2a37bSGleb Smirnoff fdrop(sock_fp, td); 108933a2a37bSGleb Smirnoff if (m) 109033a2a37bSGleb Smirnoff m_freem(m); 109133a2a37bSGleb Smirnoff if (mh) 109233a2a37bSGleb Smirnoff m_freem(mh); 109333a2a37bSGleb Smirnoff 109433a2a37bSGleb Smirnoff if (sfs != NULL) { 109533a2a37bSGleb Smirnoff mtx_lock(&sfs->mtx); 109633a2a37bSGleb Smirnoff if (sfs->count != 0) 109733a2a37bSGleb Smirnoff cv_wait(&sfs->cv, &sfs->mtx); 109833a2a37bSGleb Smirnoff KASSERT(sfs->count == 0, ("sendfile sync still busy")); 109933a2a37bSGleb Smirnoff cv_destroy(&sfs->cv); 110033a2a37bSGleb Smirnoff mtx_destroy(&sfs->mtx); 110133a2a37bSGleb Smirnoff free(sfs, M_TEMP); 110233a2a37bSGleb Smirnoff } 1103b2e60773SJohn Baldwin #ifdef KERN_TLS 1104b2e60773SJohn Baldwin if (tls != NULL) 1105b2e60773SJohn Baldwin ktls_free(tls); 1106b2e60773SJohn Baldwin #endif 110733a2a37bSGleb Smirnoff 110833a2a37bSGleb Smirnoff if (error == ERESTART) 110933a2a37bSGleb Smirnoff error = EINTR; 111033a2a37bSGleb Smirnoff 111133a2a37bSGleb Smirnoff return (error); 111233a2a37bSGleb Smirnoff } 111333a2a37bSGleb Smirnoff 111433a2a37bSGleb Smirnoff static int 111533a2a37bSGleb Smirnoff sendfile(struct thread *td, struct sendfile_args *uap, int compat) 111633a2a37bSGleb Smirnoff { 111733a2a37bSGleb Smirnoff struct sf_hdtr hdtr; 111833a2a37bSGleb Smirnoff struct uio *hdr_uio, *trl_uio; 111933a2a37bSGleb Smirnoff struct file *fp; 112033a2a37bSGleb Smirnoff off_t sbytes; 112133a2a37bSGleb Smirnoff int error; 112233a2a37bSGleb Smirnoff 112333a2a37bSGleb Smirnoff /* 112433a2a37bSGleb Smirnoff * File offset must be positive. If it goes beyond EOF 112533a2a37bSGleb Smirnoff * we send only the header/trailer and no payload data. 112633a2a37bSGleb Smirnoff */ 112733a2a37bSGleb Smirnoff if (uap->offset < 0) 112833a2a37bSGleb Smirnoff return (EINVAL); 112933a2a37bSGleb Smirnoff 1130ef3266d5SGleb Smirnoff sbytes = 0; 113133a2a37bSGleb Smirnoff hdr_uio = trl_uio = NULL; 113233a2a37bSGleb Smirnoff 113333a2a37bSGleb Smirnoff if (uap->hdtr != NULL) { 113433a2a37bSGleb Smirnoff error = copyin(uap->hdtr, &hdtr, sizeof(hdtr)); 113533a2a37bSGleb Smirnoff if (error != 0) 113633a2a37bSGleb Smirnoff goto out; 113733a2a37bSGleb Smirnoff if (hdtr.headers != NULL) { 113833a2a37bSGleb Smirnoff error = copyinuio(hdtr.headers, hdtr.hdr_cnt, 113933a2a37bSGleb Smirnoff &hdr_uio); 114033a2a37bSGleb Smirnoff if (error != 0) 114133a2a37bSGleb Smirnoff goto out; 11429c64cfe5SGleb Smirnoff #ifdef COMPAT_FREEBSD4 11439c64cfe5SGleb Smirnoff /* 11449c64cfe5SGleb Smirnoff * In FreeBSD < 5.0 the nbytes to send also included 11459c64cfe5SGleb Smirnoff * the header. If compat is specified subtract the 11469c64cfe5SGleb Smirnoff * header size from nbytes. 11479c64cfe5SGleb Smirnoff */ 11489c64cfe5SGleb Smirnoff if (compat) { 11499c64cfe5SGleb Smirnoff if (uap->nbytes > hdr_uio->uio_resid) 11509c64cfe5SGleb Smirnoff uap->nbytes -= hdr_uio->uio_resid; 11519c64cfe5SGleb Smirnoff else 11529c64cfe5SGleb Smirnoff uap->nbytes = 0; 11539c64cfe5SGleb Smirnoff } 11549c64cfe5SGleb Smirnoff #endif 115533a2a37bSGleb Smirnoff } 115633a2a37bSGleb Smirnoff if (hdtr.trailers != NULL) { 115733a2a37bSGleb Smirnoff error = copyinuio(hdtr.trailers, hdtr.trl_cnt, 115833a2a37bSGleb Smirnoff &trl_uio); 115933a2a37bSGleb Smirnoff if (error != 0) 116033a2a37bSGleb Smirnoff goto out; 116133a2a37bSGleb Smirnoff } 116233a2a37bSGleb Smirnoff } 116333a2a37bSGleb Smirnoff 116433a2a37bSGleb Smirnoff AUDIT_ARG_FD(uap->fd); 116533a2a37bSGleb Smirnoff 116633a2a37bSGleb Smirnoff /* 116733a2a37bSGleb Smirnoff * sendfile(2) can start at any offset within a file so we require 116833a2a37bSGleb Smirnoff * CAP_READ+CAP_SEEK = CAP_PREAD. 116933a2a37bSGleb Smirnoff */ 1170cbd92ce6SMatt Macy if ((error = fget_read(td, uap->fd, &cap_pread_rights, &fp)) != 0) 117133a2a37bSGleb Smirnoff goto out; 117233a2a37bSGleb Smirnoff 117333a2a37bSGleb Smirnoff error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset, 11749c64cfe5SGleb Smirnoff uap->nbytes, &sbytes, uap->flags, td); 117533a2a37bSGleb Smirnoff fdrop(fp, td); 117633a2a37bSGleb Smirnoff 117733a2a37bSGleb Smirnoff if (uap->sbytes != NULL) 117833a2a37bSGleb Smirnoff copyout(&sbytes, uap->sbytes, sizeof(off_t)); 117933a2a37bSGleb Smirnoff 118033a2a37bSGleb Smirnoff out: 118133a2a37bSGleb Smirnoff free(hdr_uio, M_IOV); 118233a2a37bSGleb Smirnoff free(trl_uio, M_IOV); 118333a2a37bSGleb Smirnoff return (error); 118433a2a37bSGleb Smirnoff } 118533a2a37bSGleb Smirnoff 118633a2a37bSGleb Smirnoff /* 118733a2a37bSGleb Smirnoff * sendfile(2) 118833a2a37bSGleb Smirnoff * 118933a2a37bSGleb Smirnoff * int sendfile(int fd, int s, off_t offset, size_t nbytes, 119033a2a37bSGleb Smirnoff * struct sf_hdtr *hdtr, off_t *sbytes, int flags) 119133a2a37bSGleb Smirnoff * 119233a2a37bSGleb Smirnoff * Send a file specified by 'fd' and starting at 'offset' to a socket 119333a2a37bSGleb Smirnoff * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes == 119433a2a37bSGleb Smirnoff * 0. Optionally add a header and/or trailer to the socket output. If 119533a2a37bSGleb Smirnoff * specified, write the total number of bytes sent into *sbytes. 119633a2a37bSGleb Smirnoff */ 119733a2a37bSGleb Smirnoff int 119833a2a37bSGleb Smirnoff sys_sendfile(struct thread *td, struct sendfile_args *uap) 119933a2a37bSGleb Smirnoff { 120033a2a37bSGleb Smirnoff 120133a2a37bSGleb Smirnoff return (sendfile(td, uap, 0)); 120233a2a37bSGleb Smirnoff } 120333a2a37bSGleb Smirnoff 120433a2a37bSGleb Smirnoff #ifdef COMPAT_FREEBSD4 120533a2a37bSGleb Smirnoff int 120633a2a37bSGleb Smirnoff freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap) 120733a2a37bSGleb Smirnoff { 120833a2a37bSGleb Smirnoff struct sendfile_args args; 120933a2a37bSGleb Smirnoff 121033a2a37bSGleb Smirnoff args.fd = uap->fd; 121133a2a37bSGleb Smirnoff args.s = uap->s; 121233a2a37bSGleb Smirnoff args.offset = uap->offset; 121333a2a37bSGleb Smirnoff args.nbytes = uap->nbytes; 121433a2a37bSGleb Smirnoff args.hdtr = uap->hdtr; 121533a2a37bSGleb Smirnoff args.sbytes = uap->sbytes; 121633a2a37bSGleb Smirnoff args.flags = uap->flags; 121733a2a37bSGleb Smirnoff 121833a2a37bSGleb Smirnoff return (sendfile(td, &args, 1)); 121933a2a37bSGleb Smirnoff } 122033a2a37bSGleb Smirnoff #endif /* COMPAT_FREEBSD4 */ 1221