subr_uio.c revision 137244
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1982, 1986, 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * (c) UNIX System Laboratories, Inc. 51541Srgrimes * All or some portions of this file are derived from material licensed 61541Srgrimes * to the University of California by American Telephone and Telegraph 71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81541Srgrimes * the permission of UNIX System Laboratories, Inc. 91541Srgrimes * 101541Srgrimes * Redistribution and use in source and binary forms, with or without 111541Srgrimes * modification, are permitted provided that the following conditions 121541Srgrimes * are met: 131541Srgrimes * 1. Redistributions of source code must retain the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer. 151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer in the 171541Srgrimes * documentation and/or other materials provided with the distribution. 181541Srgrimes * 4. Neither the name of the University nor the names of its contributors 191541Srgrimes * may be used to endorse or promote products derived from this software 201541Srgrimes * without specific prior written permission. 211541Srgrimes * 221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321541Srgrimes * SUCH DAMAGE. 331541Srgrimes * 341541Srgrimes * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94 351541Srgrimes */ 361541Srgrimes 37116182Sobrien#include <sys/cdefs.h> 38116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_subr.c 137244 2004-11-05 06:52:29Z alc $"); 39116182Sobrien 4098849Sken#include "opt_zero.h" 4198849Sken 421541Srgrimes#include <sys/param.h> 431541Srgrimes#include <sys/systm.h> 4444218Sbde#include <sys/kernel.h> 4565557Sjasone#include <sys/ktr.h> 46120665Snectar#include <sys/limits.h> 4776166Smarkm#include <sys/lock.h> 4876166Smarkm#include <sys/mutex.h> 491541Srgrimes#include <sys/proc.h> 501541Srgrimes#include <sys/malloc.h> 5143529Sbde#include <sys/resourcevar.h> 52104964Sjeff#include <sys/sched.h> 5378431Swollman#include <sys/sysctl.h> 5432702Sdyson#include <sys/vnode.h> 551541Srgrimes 5631853Sdyson#include <vm/vm.h> 5731853Sdyson#include <vm/vm_page.h> 5831853Sdyson#include <vm/vm_map.h> 5999848Sken#ifdef ZERO_COPY_SOCKETS 6099848Sken#include <vm/vm_param.h> 6199848Sken#include <vm/vm_object.h> 6299848Sken#endif 6331853Sdyson 64111737SdesSYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV, 6578431Swollman "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)"); 6678431Swollman 6798849Sken#ifdef ZERO_COPY_SOCKETS 6898849Sken/* Declared in uipc_socket.c */ 6998849Skenextern int so_zero_copy_receive; 7098849Sken 7198849Skenstatic int 72111737Sdesvm_pgmoveco(vm_map_t mapa, vm_object_t srcobj, vm_offset_t kaddr, 73111737Sdes vm_offset_t uaddr) 7498849Sken{ 7598849Sken vm_map_t map = mapa; 7698849Sken vm_page_t kern_pg, user_pg; 7798849Sken vm_object_t uobject; 7898849Sken vm_map_entry_t entry; 79137244Salc vm_pindex_t upindex; 8098849Sken vm_prot_t prot; 8198849Sken boolean_t wired; 8298849Sken 8398849Sken /* 8498849Sken * First lookup the kernel page. 8598849Sken */ 8698849Sken kern_pg = PHYS_TO_VM_PAGE(vtophys(kaddr)); 87116110Salc /* 88116110Salc * XXX The vm object containing kern_pg needs locking. 89116110Salc */ 9098849Sken if ((vm_map_lookup(&map, uaddr, 91111977Sken VM_PROT_WRITE, &entry, &uobject, 9298849Sken &upindex, &prot, &wired)) != KERN_SUCCESS) { 9398849Sken return(EFAULT); 9498849Sken } 95116110Salc VM_OBJECT_LOCK(uobject); 9698849Sken if ((user_pg = vm_page_lookup(uobject, upindex)) != NULL) { 97107371Salc do 98107371Salc vm_page_lock_queues(); 99107371Salc while (vm_page_sleep_if_busy(user_pg, 1, "vm_pgmoveco")); 100107371Salc pmap_remove_all(user_pg); 10198849Sken vm_page_free(user_pg); 102108139Salc } else 103108139Salc vm_page_lock_queues(); 10498849Sken if (kern_pg->busy || ((kern_pg->queue - kern_pg->pc) == PQ_FREE) || 10598849Sken (kern_pg->hold_count != 0)|| (kern_pg->flags & PG_BUSY)) { 10698849Sken printf("vm_pgmoveco: pindex(%lu), busy(%d), PG_BUSY(%d), " 10798849Sken "hold(%d) paddr(0x%lx)\n", (u_long)kern_pg->pindex, 10898849Sken kern_pg->busy, (kern_pg->flags & PG_BUSY) ? 1 : 0, 10998849Sken kern_pg->hold_count, (u_long)kern_pg->phys_addr); 11098849Sken if ((kern_pg->queue - kern_pg->pc) == PQ_FREE) 11198849Sken panic("vm_pgmoveco: renaming free page"); 11298849Sken else 11398849Sken panic("vm_pgmoveco: renaming busy page"); 11498849Sken } 11598849Sken vm_page_rename(kern_pg, uobject, upindex); 11698849Sken kern_pg->valid = VM_PAGE_BITS_ALL; 117108139Salc vm_page_unlock_queues(); 118116110Salc VM_OBJECT_UNLOCK(uobject); 11998849Sken vm_map_lookup_done(map, entry); 12098849Sken return(KERN_SUCCESS); 12198849Sken} 12298849Sken#endif /* ZERO_COPY_SOCKETS */ 12398849Sken 1241549Srgrimesint 125111739Sdesuiomove(void *cp, int n, struct uio *uio) 1261541Srgrimes{ 12783366Sjulian struct thread *td = curthread; 128111737Sdes struct iovec *iov; 1291541Srgrimes u_int cnt; 13044681Sjulian int error = 0; 13144681Sjulian int save = 0; 1321541Srgrimes 13342408Seivind KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 13442453Seivind ("uiomove: mode")); 13583366Sjulian KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 13642453Seivind ("uiomove proc")); 137136444Sjhb WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 138136444Sjhb "Calling uiomove()"); 13942408Seivind 140130028Stjr save = td->td_pflags & TDP_DEADLKTREAT; 141130028Stjr td->td_pflags |= TDP_DEADLKTREAT; 14244681Sjulian 1431541Srgrimes while (n > 0 && uio->uio_resid) { 1441541Srgrimes iov = uio->uio_iov; 1451541Srgrimes cnt = iov->iov_len; 1461541Srgrimes if (cnt == 0) { 1471541Srgrimes uio->uio_iov++; 1481541Srgrimes uio->uio_iovcnt--; 1491541Srgrimes continue; 1501541Srgrimes } 1511541Srgrimes if (cnt > n) 1521541Srgrimes cnt = n; 1536324Sdg 1541541Srgrimes switch (uio->uio_segflg) { 1551541Srgrimes 1561541Srgrimes case UIO_USERSPACE: 15770861Sjake if (ticks - PCPU_GET(switchticks) >= hogticks) 15843529Sbde uio_yield(); 1591541Srgrimes if (uio->uio_rw == UIO_READ) 1601541Srgrimes error = copyout(cp, iov->iov_base, cnt); 1611541Srgrimes else 1621541Srgrimes error = copyin(iov->iov_base, cp, cnt); 1631541Srgrimes if (error) 16490413Stmm goto out; 1651541Srgrimes break; 1661541Srgrimes 1671541Srgrimes case UIO_SYSSPACE: 1681541Srgrimes if (uio->uio_rw == UIO_READ) 16998998Salfred bcopy(cp, iov->iov_base, cnt); 1701541Srgrimes else 17198998Salfred bcopy(iov->iov_base, cp, cnt); 1721541Srgrimes break; 1737611Sdg case UIO_NOCOPY: 1747611Sdg break; 1751541Srgrimes } 176104908Smike iov->iov_base = (char *)iov->iov_base + cnt; 1771541Srgrimes iov->iov_len -= cnt; 1781541Srgrimes uio->uio_resid -= cnt; 1791541Srgrimes uio->uio_offset += cnt; 180111739Sdes cp = (char *)cp + cnt; 1811541Srgrimes n -= cnt; 1821541Srgrimes } 18390413Stmmout: 184130028Stjr if (save == 0) 185130023Stjr td->td_pflags &= ~TDP_DEADLKTREAT; 18644681Sjulian return (error); 1871541Srgrimes} 1881541Srgrimes 189120665Snectar/* 190120665Snectar * Wrapper for uiomove() that validates the arguments against a known-good 191120665Snectar * kernel buffer. Currently, uiomove accepts a signed (n) argument, which 192120665Snectar * is almost definitely a bad thing, so we catch that here as well. We 193120665Snectar * return a runtime failure, but it might be desirable to generate a runtime 194120665Snectar * assertion failure instead. 195120665Snectar */ 196120665Snectarint 197120665Snectaruiomove_frombuf(void *buf, int buflen, struct uio *uio) 198120665Snectar{ 199120665Snectar unsigned int offset, n; 200120665Snectar 201120665Snectar if (uio->uio_offset < 0 || uio->uio_resid < 0 || 202120665Snectar (offset = uio->uio_offset) != uio->uio_offset) 203120665Snectar return (EINVAL); 204120665Snectar if (buflen <= 0 || offset >= buflen) 205120665Snectar return (0); 206120665Snectar if ((n = buflen - offset) > INT_MAX) 207120665Snectar return (EINVAL); 208120665Snectar return (uiomove((char *)buf + offset, n, uio)); 209120665Snectar} 210120665Snectar 211111937Salc#ifdef ZERO_COPY_SOCKETS 21296080Salc/* 21396080Salc * Experimental support for zero-copy I/O 21496080Salc */ 21598849Skenstatic int 216111739Sdesuserspaceco(void *cp, u_int cnt, struct uio *uio, struct vm_object *obj, 217111737Sdes int disposable) 21898849Sken{ 21998849Sken struct iovec *iov; 22098849Sken int error; 22198849Sken 22298849Sken iov = uio->uio_iov; 22398849Sken if (uio->uio_rw == UIO_READ) { 22498849Sken if ((so_zero_copy_receive != 0) 22598849Sken && (obj != NULL) 22698849Sken && ((cnt & PAGE_MASK) == 0) 22798849Sken && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) 22898849Sken && ((uio->uio_offset & PAGE_MASK) == 0) 22998849Sken && ((((intptr_t) cp) & PAGE_MASK) == 0) 23098849Sken && (obj->type == OBJT_DEFAULT) 23198849Sken && (disposable != 0)) { 23298849Sken /* SOCKET: use page-trading */ 23398849Sken /* 23498849Sken * We only want to call vm_pgmoveco() on 23598849Sken * disposeable pages, since it gives the 23698849Sken * kernel page to the userland process. 23798849Sken */ 23898849Sken error = vm_pgmoveco(&curproc->p_vmspace->vm_map, 239111737Sdes obj, (vm_offset_t)cp, 24098849Sken (vm_offset_t)iov->iov_base); 24198849Sken 24298849Sken /* 24398849Sken * If we get an error back, attempt 24498849Sken * to use copyout() instead. The 24598849Sken * disposable page should be freed 24698849Sken * automatically if we weren't able to move 24798849Sken * it into userland. 24898849Sken */ 24998849Sken if (error != 0) 25098849Sken error = copyout(cp, iov->iov_base, cnt); 25198849Sken } else { 25298849Sken error = copyout(cp, iov->iov_base, cnt); 25398849Sken } 25498849Sken } else { 25598849Sken error = copyin(iov->iov_base, cp, cnt); 25698849Sken } 25798849Sken return (error); 25898849Sken} 25998849Sken 26031853Sdysonint 261111739Sdesuiomoveco(void *cp, int n, struct uio *uio, struct vm_object *obj, 262111737Sdes int disposable) 26331853Sdyson{ 26431853Sdyson struct iovec *iov; 26531853Sdyson u_int cnt; 26631853Sdyson int error; 26731853Sdyson 26842408Seivind KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 26942453Seivind ("uiomoveco: mode")); 27083366Sjulian KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 27142453Seivind ("uiomoveco proc")); 27242408Seivind 27331853Sdyson while (n > 0 && uio->uio_resid) { 27431853Sdyson iov = uio->uio_iov; 27531853Sdyson cnt = iov->iov_len; 27631853Sdyson if (cnt == 0) { 27731853Sdyson uio->uio_iov++; 27831853Sdyson uio->uio_iovcnt--; 27931853Sdyson continue; 28031853Sdyson } 28131853Sdyson if (cnt > n) 28231853Sdyson cnt = n; 28331853Sdyson 28431853Sdyson switch (uio->uio_segflg) { 28531853Sdyson 28631853Sdyson case UIO_USERSPACE: 28770861Sjake if (ticks - PCPU_GET(switchticks) >= hogticks) 28843529Sbde uio_yield(); 28998849Sken 29098849Sken error = userspaceco(cp, cnt, uio, obj, disposable); 29198849Sken 29231853Sdyson if (error) 29331853Sdyson return (error); 29431853Sdyson break; 29531853Sdyson 29631853Sdyson case UIO_SYSSPACE: 29731853Sdyson if (uio->uio_rw == UIO_READ) 29898998Salfred bcopy(cp, iov->iov_base, cnt); 29931853Sdyson else 30098998Salfred bcopy(iov->iov_base, cp, cnt); 30131853Sdyson break; 30231853Sdyson case UIO_NOCOPY: 30331853Sdyson break; 30431853Sdyson } 305104908Smike iov->iov_base = (char *)iov->iov_base + cnt; 30631853Sdyson iov->iov_len -= cnt; 30731853Sdyson uio->uio_resid -= cnt; 30831853Sdyson uio->uio_offset += cnt; 309111739Sdes cp = (char *)cp + cnt; 31031853Sdyson n -= cnt; 31131853Sdyson } 31231853Sdyson return (0); 31331853Sdyson} 314111937Salc#endif /* ZERO_COPY_SOCKETS */ 31531853Sdyson 31696080Salc/* 3171541Srgrimes * Give next character to user as result of read. 3181541Srgrimes */ 3191549Srgrimesint 320111737Sdesureadc(int c, struct uio *uio) 3211541Srgrimes{ 322111737Sdes struct iovec *iov; 323111737Sdes char *iov_base; 3241541Srgrimes 3251541Srgrimesagain: 3261541Srgrimes if (uio->uio_iovcnt == 0 || uio->uio_resid == 0) 3271541Srgrimes panic("ureadc"); 3281541Srgrimes iov = uio->uio_iov; 3291541Srgrimes if (iov->iov_len == 0) { 3301541Srgrimes uio->uio_iovcnt--; 3311541Srgrimes uio->uio_iov++; 3321541Srgrimes goto again; 3331541Srgrimes } 3341541Srgrimes switch (uio->uio_segflg) { 3351541Srgrimes 3361541Srgrimes case UIO_USERSPACE: 3371541Srgrimes if (subyte(iov->iov_base, c) < 0) 3381541Srgrimes return (EFAULT); 3391541Srgrimes break; 3401541Srgrimes 3411541Srgrimes case UIO_SYSSPACE: 342104908Smike iov_base = iov->iov_base; 343104908Smike *iov_base = c; 344104908Smike iov->iov_base = iov_base; 3451541Srgrimes break; 3461541Srgrimes 3478177Sdg case UIO_NOCOPY: 3488177Sdg break; 3491541Srgrimes } 350104908Smike iov->iov_base = (char *)iov->iov_base + 1; 3511541Srgrimes iov->iov_len--; 3521541Srgrimes uio->uio_resid--; 3531541Srgrimes uio->uio_offset++; 3541541Srgrimes return (0); 3551541Srgrimes} 3561541Srgrimes 3571541Srgrimes/* 3581541Srgrimes * General routine to allocate a hash table. 3591541Srgrimes */ 3601541Srgrimesvoid * 361111737Sdeshashinit(int elements, struct malloc_type *type, u_long *hashmask) 3621541Srgrimes{ 3631541Srgrimes long hashsize; 36460938Sjake LIST_HEAD(generic, generic) *hashtbl; 3651541Srgrimes int i; 3661541Srgrimes 3671541Srgrimes if (elements <= 0) 3688364Sdg panic("hashinit: bad elements"); 3691541Srgrimes for (hashsize = 1; hashsize <= elements; hashsize <<= 1) 3701541Srgrimes continue; 3711541Srgrimes hashsize >>= 1; 372111119Simp hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); 3731541Srgrimes for (i = 0; i < hashsize; i++) 3741541Srgrimes LIST_INIT(&hashtbl[i]); 3751541Srgrimes *hashmask = hashsize - 1; 3761541Srgrimes return (hashtbl); 3771541Srgrimes} 3787611Sdg 37999098Siedowsevoid 380111737Sdeshashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask) 38199098Siedowse{ 38299098Siedowse LIST_HEAD(generic, generic) *hashtbl, *hp; 38399098Siedowse 38499098Siedowse hashtbl = vhashtbl; 38599098Siedowse for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++) 38699098Siedowse if (!LIST_EMPTY(hp)) 38799098Siedowse panic("hashdestroy: hash not empty"); 38899098Siedowse free(hashtbl, type); 38999098Siedowse} 39099098Siedowse 3917683Sdgstatic int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039, 3927683Sdg 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653, 3937611Sdg 7159, 7673, 8191, 12281, 16381, 24571, 32749 }; 39426205Salex#define NPRIMES (sizeof(primes) / sizeof(primes[0])) 3957611Sdg 3967611Sdg/* 3977611Sdg * General routine to allocate a prime number sized hash table. 3987611Sdg */ 3997611Sdgvoid * 400111737Sdesphashinit(int elements, struct malloc_type *type, u_long *nentries) 4017611Sdg{ 4027611Sdg long hashsize; 40360938Sjake LIST_HEAD(generic, generic) *hashtbl; 4047611Sdg int i; 4057611Sdg 4067611Sdg if (elements <= 0) 4078364Sdg panic("phashinit: bad elements"); 4087611Sdg for (i = 1, hashsize = primes[1]; hashsize <= elements;) { 4097611Sdg i++; 4107611Sdg if (i == NPRIMES) 4117611Sdg break; 4127611Sdg hashsize = primes[i]; 4137611Sdg } 4147611Sdg hashsize = primes[i - 1]; 415111119Simp hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); 4167611Sdg for (i = 0; i < hashsize; i++) 4177611Sdg LIST_INIT(&hashtbl[i]); 4187611Sdg *nentries = hashsize; 4197611Sdg return (hashtbl); 4207611Sdg} 42143529Sbde 42283959Sdillonvoid 423111737Sdesuio_yield(void) 42443529Sbde{ 42583366Sjulian struct thread *td; 42643529Sbde 42783366Sjulian td = curthread; 42872200Sbmilekic mtx_lock_spin(&sched_lock); 42988900Sjhb DROP_GIANT(); 430104964Sjeff sched_prio(td, td->td_ksegrp->kg_user_pri); /* XXXKSE */ 431131473Sjhb mi_switch(SW_INVOL, NULL); 43272200Sbmilekic mtx_unlock_spin(&sched_lock); 43368808Sjhb PICKUP_GIANT(); 43443529Sbde} 43572537Sjlemon 43672537Sjlemonint 437123852Salfredcopyinfrom(const void * __restrict src, void * __restrict dst, size_t len, 438123852Salfred int seg) 43972537Sjlemon{ 44072537Sjlemon int error = 0; 44172537Sjlemon 44272537Sjlemon switch (seg) { 44372537Sjlemon case UIO_USERSPACE: 44472537Sjlemon error = copyin(src, dst, len); 44572537Sjlemon break; 44672537Sjlemon case UIO_SYSSPACE: 44772537Sjlemon bcopy(src, dst, len); 44872537Sjlemon break; 44972537Sjlemon default: 45072537Sjlemon panic("copyinfrom: bad seg %d\n", seg); 45172537Sjlemon } 45272537Sjlemon return (error); 45372537Sjlemon} 45472537Sjlemon 45572537Sjlemonint 456123852Salfredcopyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len, 457123852Salfred size_t * __restrict copied, int seg) 45872537Sjlemon{ 45972537Sjlemon int error = 0; 46072537Sjlemon 46172537Sjlemon switch (seg) { 46272537Sjlemon case UIO_USERSPACE: 46372537Sjlemon error = copyinstr(src, dst, len, copied); 46472537Sjlemon break; 46572537Sjlemon case UIO_SYSSPACE: 46672537Sjlemon error = copystr(src, dst, len, copied); 46772537Sjlemon break; 46872537Sjlemon default: 46972537Sjlemon panic("copyinstrfrom: bad seg %d\n", seg); 47072537Sjlemon } 47172537Sjlemon return (error); 47272537Sjlemon} 473125296Ssilby 474125296Ssilbyint 475131897Sphkcopyiniov(struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error) 476125296Ssilby{ 477131897Sphk u_int iovlen; 478131897Sphk 479131897Sphk *iov = NULL; 480131897Sphk if (iovcnt > UIO_MAXIOV) 481131897Sphk return (error); 482131897Sphk iovlen = iovcnt * sizeof (struct iovec); 483131897Sphk *iov = malloc(iovlen, M_IOV, M_WAITOK); 484131897Sphk error = copyin(iovp, *iov, iovlen); 485131897Sphk if (error) { 486131897Sphk free(*iov, M_IOV); 487131897Sphk *iov = NULL; 488131897Sphk } 489131897Sphk return (error); 490131897Sphk} 491131897Sphk 492131897Sphkint 493131897Sphkcopyinuio(struct iovec *iovp, u_int iovcnt, struct uio **uiop) 494131897Sphk{ 495125420Ssilby struct iovec *iov; 496131897Sphk struct uio *uio; 497125296Ssilby u_int iovlen; 498125420Ssilby int error, i; 499125296Ssilby 500131897Sphk *uiop = NULL; 501131897Sphk if (iovcnt > UIO_MAXIOV) 502131897Sphk return (EINVAL); 503125420Ssilby iovlen = iovcnt * sizeof (struct iovec); 504131897Sphk uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK); 505131897Sphk iov = (struct iovec *)(uio + 1); 506131897Sphk error = copyin(iovp, iov, iovlen); 507131897Sphk if (error) { 508131897Sphk free(uio, M_IOV); 509131897Sphk return (error); 510125420Ssilby } 511125420Ssilby uio->uio_iov = iov; 512125420Ssilby uio->uio_iovcnt = iovcnt; 513125420Ssilby uio->uio_segflg = UIO_USERSPACE; 514125420Ssilby uio->uio_offset = -1; 515125420Ssilby uio->uio_resid = 0; 516125420Ssilby for (i = 0; i < iovcnt; i++) { 517125420Ssilby if (iov->iov_len > INT_MAX - uio->uio_resid) { 518131897Sphk free(uio, M_IOV); 519131897Sphk return (EINVAL); 520125420Ssilby } 521125420Ssilby uio->uio_resid += iov->iov_len; 522125420Ssilby iov++; 523125420Ssilby } 524131897Sphk *uiop = uio; 525131897Sphk return (0); 526131897Sphk} 527125296Ssilby 528131897Sphkstruct uio * 529131897Sphkcloneuio(struct uio *uiop) 530131897Sphk{ 531131897Sphk struct uio *uio; 532131897Sphk int iovlen; 533125296Ssilby 534131897Sphk iovlen = uiop->uio_iovcnt * sizeof (struct iovec); 535131897Sphk uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK); 536131897Sphk *uio = *uiop; 537131897Sphk uio->uio_iov = (struct iovec *)(uio + 1); 538131897Sphk bcopy(uiop->uio_iov, uio->uio_iov, iovlen); 539131897Sphk return (uio); 540125296Ssilby} 541