subr_hash.c revision 130023
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 130023 2004-06-03 01:47:37Z tjr $"); 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; 7998849Sken vm_pindex_t upindex, kpindex; 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")); 10098849Sken vm_page_busy(user_pg); 101107371Salc pmap_remove_all(user_pg); 10298849Sken vm_page_free(user_pg); 103108139Salc } else 104108139Salc vm_page_lock_queues(); 10598849Sken if (kern_pg->busy || ((kern_pg->queue - kern_pg->pc) == PQ_FREE) || 10698849Sken (kern_pg->hold_count != 0)|| (kern_pg->flags & PG_BUSY)) { 10798849Sken printf("vm_pgmoveco: pindex(%lu), busy(%d), PG_BUSY(%d), " 10898849Sken "hold(%d) paddr(0x%lx)\n", (u_long)kern_pg->pindex, 10998849Sken kern_pg->busy, (kern_pg->flags & PG_BUSY) ? 1 : 0, 11098849Sken kern_pg->hold_count, (u_long)kern_pg->phys_addr); 11198849Sken if ((kern_pg->queue - kern_pg->pc) == PQ_FREE) 11298849Sken panic("vm_pgmoveco: renaming free page"); 11398849Sken else 11498849Sken panic("vm_pgmoveco: renaming busy page"); 11598849Sken } 11698849Sken kpindex = kern_pg->pindex; 11798849Sken vm_page_busy(kern_pg); 11898849Sken vm_page_rename(kern_pg, uobject, upindex); 11998849Sken vm_page_flag_clear(kern_pg, PG_BUSY); 12098849Sken kern_pg->valid = VM_PAGE_BITS_ALL; 121108139Salc vm_page_unlock_queues(); 122116110Salc VM_OBJECT_UNLOCK(uobject); 12398849Sken vm_map_lookup_done(map, entry); 12498849Sken return(KERN_SUCCESS); 12598849Sken} 12698849Sken#endif /* ZERO_COPY_SOCKETS */ 12798849Sken 1281549Srgrimesint 129111739Sdesuiomove(void *cp, int n, struct uio *uio) 1301541Srgrimes{ 13183366Sjulian struct thread *td = curthread; 132111737Sdes struct iovec *iov; 1331541Srgrimes u_int cnt; 13444681Sjulian int error = 0; 13544681Sjulian int save = 0; 1361541Srgrimes 13742408Seivind KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 13842453Seivind ("uiomove: mode")); 13983366Sjulian KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 14042453Seivind ("uiomove proc")); 14142408Seivind 14283366Sjulian if (td) { 143130023Stjr save = td->td_pflags & TDP_DEADLKTREAT; 144130023Stjr td->td_pflags |= TDP_DEADLKTREAT; 14544681Sjulian } 14644681Sjulian 1471541Srgrimes while (n > 0 && uio->uio_resid) { 1481541Srgrimes iov = uio->uio_iov; 1491541Srgrimes cnt = iov->iov_len; 1501541Srgrimes if (cnt == 0) { 1511541Srgrimes uio->uio_iov++; 1521541Srgrimes uio->uio_iovcnt--; 1531541Srgrimes continue; 1541541Srgrimes } 1551541Srgrimes if (cnt > n) 1561541Srgrimes cnt = n; 1576324Sdg 1581541Srgrimes switch (uio->uio_segflg) { 1591541Srgrimes 1601541Srgrimes case UIO_USERSPACE: 16170861Sjake if (ticks - PCPU_GET(switchticks) >= hogticks) 16243529Sbde uio_yield(); 1631541Srgrimes if (uio->uio_rw == UIO_READ) 1641541Srgrimes error = copyout(cp, iov->iov_base, cnt); 1651541Srgrimes else 1661541Srgrimes error = copyin(iov->iov_base, cp, cnt); 1671541Srgrimes if (error) 16890413Stmm goto out; 1691541Srgrimes break; 1701541Srgrimes 1711541Srgrimes case UIO_SYSSPACE: 1721541Srgrimes if (uio->uio_rw == UIO_READ) 17398998Salfred bcopy(cp, iov->iov_base, cnt); 1741541Srgrimes else 17598998Salfred bcopy(iov->iov_base, cp, cnt); 1761541Srgrimes break; 1777611Sdg case UIO_NOCOPY: 1787611Sdg break; 1791541Srgrimes } 180104908Smike iov->iov_base = (char *)iov->iov_base + cnt; 1811541Srgrimes iov->iov_len -= cnt; 1821541Srgrimes uio->uio_resid -= cnt; 1831541Srgrimes uio->uio_offset += cnt; 184111739Sdes cp = (char *)cp + cnt; 1851541Srgrimes n -= cnt; 1861541Srgrimes } 18790413Stmmout: 188130023Stjr if (td && save == 0) 189130023Stjr td->td_pflags &= ~TDP_DEADLKTREAT; 19044681Sjulian return (error); 1911541Srgrimes} 1921541Srgrimes 193120665Snectar/* 194120665Snectar * Wrapper for uiomove() that validates the arguments against a known-good 195120665Snectar * kernel buffer. Currently, uiomove accepts a signed (n) argument, which 196120665Snectar * is almost definitely a bad thing, so we catch that here as well. We 197120665Snectar * return a runtime failure, but it might be desirable to generate a runtime 198120665Snectar * assertion failure instead. 199120665Snectar */ 200120665Snectarint 201120665Snectaruiomove_frombuf(void *buf, int buflen, struct uio *uio) 202120665Snectar{ 203120665Snectar unsigned int offset, n; 204120665Snectar 205120665Snectar if (uio->uio_offset < 0 || uio->uio_resid < 0 || 206120665Snectar (offset = uio->uio_offset) != uio->uio_offset) 207120665Snectar return (EINVAL); 208120665Snectar if (buflen <= 0 || offset >= buflen) 209120665Snectar return (0); 210120665Snectar if ((n = buflen - offset) > INT_MAX) 211120665Snectar return (EINVAL); 212120665Snectar return (uiomove((char *)buf + offset, n, uio)); 213120665Snectar} 214120665Snectar 215111937Salc#ifdef ZERO_COPY_SOCKETS 21696080Salc/* 21796080Salc * Experimental support for zero-copy I/O 21896080Salc */ 21998849Skenstatic int 220111739Sdesuserspaceco(void *cp, u_int cnt, struct uio *uio, struct vm_object *obj, 221111737Sdes int disposable) 22298849Sken{ 22398849Sken struct iovec *iov; 22498849Sken int error; 22598849Sken 22698849Sken iov = uio->uio_iov; 22798849Sken if (uio->uio_rw == UIO_READ) { 22898849Sken if ((so_zero_copy_receive != 0) 22998849Sken && (obj != NULL) 23098849Sken && ((cnt & PAGE_MASK) == 0) 23198849Sken && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) 23298849Sken && ((uio->uio_offset & PAGE_MASK) == 0) 23398849Sken && ((((intptr_t) cp) & PAGE_MASK) == 0) 23498849Sken && (obj->type == OBJT_DEFAULT) 23598849Sken && (disposable != 0)) { 23698849Sken /* SOCKET: use page-trading */ 23798849Sken /* 23898849Sken * We only want to call vm_pgmoveco() on 23998849Sken * disposeable pages, since it gives the 24098849Sken * kernel page to the userland process. 24198849Sken */ 24298849Sken error = vm_pgmoveco(&curproc->p_vmspace->vm_map, 243111737Sdes obj, (vm_offset_t)cp, 24498849Sken (vm_offset_t)iov->iov_base); 24598849Sken 24698849Sken /* 24798849Sken * If we get an error back, attempt 24898849Sken * to use copyout() instead. The 24998849Sken * disposable page should be freed 25098849Sken * automatically if we weren't able to move 25198849Sken * it into userland. 25298849Sken */ 25398849Sken if (error != 0) 25498849Sken error = copyout(cp, iov->iov_base, cnt); 25598849Sken } else { 25698849Sken error = copyout(cp, iov->iov_base, cnt); 25798849Sken } 25898849Sken } else { 25998849Sken error = copyin(iov->iov_base, cp, cnt); 26098849Sken } 26198849Sken return (error); 26298849Sken} 26398849Sken 26431853Sdysonint 265111739Sdesuiomoveco(void *cp, int n, struct uio *uio, struct vm_object *obj, 266111737Sdes int disposable) 26731853Sdyson{ 26831853Sdyson struct iovec *iov; 26931853Sdyson u_int cnt; 27031853Sdyson int error; 27131853Sdyson 27242408Seivind KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 27342453Seivind ("uiomoveco: mode")); 27483366Sjulian KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 27542453Seivind ("uiomoveco proc")); 27642408Seivind 27731853Sdyson while (n > 0 && uio->uio_resid) { 27831853Sdyson iov = uio->uio_iov; 27931853Sdyson cnt = iov->iov_len; 28031853Sdyson if (cnt == 0) { 28131853Sdyson uio->uio_iov++; 28231853Sdyson uio->uio_iovcnt--; 28331853Sdyson continue; 28431853Sdyson } 28531853Sdyson if (cnt > n) 28631853Sdyson cnt = n; 28731853Sdyson 28831853Sdyson switch (uio->uio_segflg) { 28931853Sdyson 29031853Sdyson case UIO_USERSPACE: 29170861Sjake if (ticks - PCPU_GET(switchticks) >= hogticks) 29243529Sbde uio_yield(); 29398849Sken 29498849Sken error = userspaceco(cp, cnt, uio, obj, disposable); 29598849Sken 29631853Sdyson if (error) 29731853Sdyson return (error); 29831853Sdyson break; 29931853Sdyson 30031853Sdyson case UIO_SYSSPACE: 30131853Sdyson if (uio->uio_rw == UIO_READ) 30298998Salfred bcopy(cp, iov->iov_base, cnt); 30331853Sdyson else 30498998Salfred bcopy(iov->iov_base, cp, cnt); 30531853Sdyson break; 30631853Sdyson case UIO_NOCOPY: 30731853Sdyson break; 30831853Sdyson } 309104908Smike iov->iov_base = (char *)iov->iov_base + cnt; 31031853Sdyson iov->iov_len -= cnt; 31131853Sdyson uio->uio_resid -= cnt; 31231853Sdyson uio->uio_offset += cnt; 313111739Sdes cp = (char *)cp + cnt; 31431853Sdyson n -= cnt; 31531853Sdyson } 31631853Sdyson return (0); 31731853Sdyson} 318111937Salc#endif /* ZERO_COPY_SOCKETS */ 31931853Sdyson 32096080Salc/* 3211541Srgrimes * Give next character to user as result of read. 3221541Srgrimes */ 3231549Srgrimesint 324111737Sdesureadc(int c, struct uio *uio) 3251541Srgrimes{ 326111737Sdes struct iovec *iov; 327111737Sdes char *iov_base; 3281541Srgrimes 3291541Srgrimesagain: 3301541Srgrimes if (uio->uio_iovcnt == 0 || uio->uio_resid == 0) 3311541Srgrimes panic("ureadc"); 3321541Srgrimes iov = uio->uio_iov; 3331541Srgrimes if (iov->iov_len == 0) { 3341541Srgrimes uio->uio_iovcnt--; 3351541Srgrimes uio->uio_iov++; 3361541Srgrimes goto again; 3371541Srgrimes } 3381541Srgrimes switch (uio->uio_segflg) { 3391541Srgrimes 3401541Srgrimes case UIO_USERSPACE: 3411541Srgrimes if (subyte(iov->iov_base, c) < 0) 3421541Srgrimes return (EFAULT); 3431541Srgrimes break; 3441541Srgrimes 3451541Srgrimes case UIO_SYSSPACE: 346104908Smike iov_base = iov->iov_base; 347104908Smike *iov_base = c; 348104908Smike iov->iov_base = iov_base; 3491541Srgrimes break; 3501541Srgrimes 3518177Sdg case UIO_NOCOPY: 3528177Sdg break; 3531541Srgrimes } 354104908Smike iov->iov_base = (char *)iov->iov_base + 1; 3551541Srgrimes iov->iov_len--; 3561541Srgrimes uio->uio_resid--; 3571541Srgrimes uio->uio_offset++; 3581541Srgrimes return (0); 3591541Srgrimes} 3601541Srgrimes 3611541Srgrimes/* 3621541Srgrimes * General routine to allocate a hash table. 3631541Srgrimes */ 3641541Srgrimesvoid * 365111737Sdeshashinit(int elements, struct malloc_type *type, u_long *hashmask) 3661541Srgrimes{ 3671541Srgrimes long hashsize; 36860938Sjake LIST_HEAD(generic, generic) *hashtbl; 3691541Srgrimes int i; 3701541Srgrimes 3711541Srgrimes if (elements <= 0) 3728364Sdg panic("hashinit: bad elements"); 3731541Srgrimes for (hashsize = 1; hashsize <= elements; hashsize <<= 1) 3741541Srgrimes continue; 3751541Srgrimes hashsize >>= 1; 376111119Simp hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); 3771541Srgrimes for (i = 0; i < hashsize; i++) 3781541Srgrimes LIST_INIT(&hashtbl[i]); 3791541Srgrimes *hashmask = hashsize - 1; 3801541Srgrimes return (hashtbl); 3811541Srgrimes} 3827611Sdg 38399098Siedowsevoid 384111737Sdeshashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask) 38599098Siedowse{ 38699098Siedowse LIST_HEAD(generic, generic) *hashtbl, *hp; 38799098Siedowse 38899098Siedowse hashtbl = vhashtbl; 38999098Siedowse for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++) 39099098Siedowse if (!LIST_EMPTY(hp)) 39199098Siedowse panic("hashdestroy: hash not empty"); 39299098Siedowse free(hashtbl, type); 39399098Siedowse} 39499098Siedowse 3957683Sdgstatic int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039, 3967683Sdg 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653, 3977611Sdg 7159, 7673, 8191, 12281, 16381, 24571, 32749 }; 39826205Salex#define NPRIMES (sizeof(primes) / sizeof(primes[0])) 3997611Sdg 4007611Sdg/* 4017611Sdg * General routine to allocate a prime number sized hash table. 4027611Sdg */ 4037611Sdgvoid * 404111737Sdesphashinit(int elements, struct malloc_type *type, u_long *nentries) 4057611Sdg{ 4067611Sdg long hashsize; 40760938Sjake LIST_HEAD(generic, generic) *hashtbl; 4087611Sdg int i; 4097611Sdg 4107611Sdg if (elements <= 0) 4118364Sdg panic("phashinit: bad elements"); 4127611Sdg for (i = 1, hashsize = primes[1]; hashsize <= elements;) { 4137611Sdg i++; 4147611Sdg if (i == NPRIMES) 4157611Sdg break; 4167611Sdg hashsize = primes[i]; 4177611Sdg } 4187611Sdg hashsize = primes[i - 1]; 419111119Simp hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); 4207611Sdg for (i = 0; i < hashsize; i++) 4217611Sdg LIST_INIT(&hashtbl[i]); 4227611Sdg *nentries = hashsize; 4237611Sdg return (hashtbl); 4247611Sdg} 42543529Sbde 42683959Sdillonvoid 427111737Sdesuio_yield(void) 42843529Sbde{ 42983366Sjulian struct thread *td; 43043529Sbde 43183366Sjulian td = curthread; 43272200Sbmilekic mtx_lock_spin(&sched_lock); 43388900Sjhb DROP_GIANT(); 434104964Sjeff sched_prio(td, td->td_ksegrp->kg_user_pri); /* XXXKSE */ 435124944Sjeff mi_switch(SW_INVOL); 43672200Sbmilekic mtx_unlock_spin(&sched_lock); 43768808Sjhb PICKUP_GIANT(); 43843529Sbde} 43972537Sjlemon 44072537Sjlemonint 441123852Salfredcopyinfrom(const void * __restrict src, void * __restrict dst, size_t len, 442123852Salfred int seg) 44372537Sjlemon{ 44472537Sjlemon int error = 0; 44572537Sjlemon 44672537Sjlemon switch (seg) { 44772537Sjlemon case UIO_USERSPACE: 44872537Sjlemon error = copyin(src, dst, len); 44972537Sjlemon break; 45072537Sjlemon case UIO_SYSSPACE: 45172537Sjlemon bcopy(src, dst, len); 45272537Sjlemon break; 45372537Sjlemon default: 45472537Sjlemon panic("copyinfrom: bad seg %d\n", seg); 45572537Sjlemon } 45672537Sjlemon return (error); 45772537Sjlemon} 45872537Sjlemon 45972537Sjlemonint 460123852Salfredcopyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len, 461123852Salfred size_t * __restrict copied, int seg) 46272537Sjlemon{ 46372537Sjlemon int error = 0; 46472537Sjlemon 46572537Sjlemon switch (seg) { 46672537Sjlemon case UIO_USERSPACE: 46772537Sjlemon error = copyinstr(src, dst, len, copied); 46872537Sjlemon break; 46972537Sjlemon case UIO_SYSSPACE: 47072537Sjlemon error = copystr(src, dst, len, copied); 47172537Sjlemon break; 47272537Sjlemon default: 47372537Sjlemon panic("copyinstrfrom: bad seg %d\n", seg); 47472537Sjlemon } 47572537Sjlemon return (error); 47672537Sjlemon} 477125296Ssilby 478125296Ssilbyint 479125423Ssilbyuiofromiov(struct iovec *iovp, u_int iovcnt, struct uio *uio) 480125296Ssilby{ 481125420Ssilby struct iovec *iov; 482125296Ssilby u_int iovlen; 483125420Ssilby int error, i; 484125296Ssilby 485125420Ssilby /* note: can't use iovlen until iovcnt is validated */ 486125420Ssilby iovlen = iovcnt * sizeof (struct iovec); 487125420Ssilby if (iovcnt > UIO_MAXIOV) { 488125420Ssilby error = EINVAL; 489125420Ssilby goto done; 490125420Ssilby } 491125420Ssilby MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 492125420Ssilby uio->uio_iov = iov; 493125420Ssilby uio->uio_iovcnt = iovcnt; 494125420Ssilby uio->uio_segflg = UIO_USERSPACE; 495125420Ssilby uio->uio_offset = -1; 496125420Ssilby if ((error = copyin(iovp, iov, iovlen))) 497125420Ssilby goto done; 498125420Ssilby uio->uio_resid = 0; 499125420Ssilby for (i = 0; i < iovcnt; i++) { 500125420Ssilby if (iov->iov_len > INT_MAX - uio->uio_resid) { 501125420Ssilby error = EINVAL; 502125420Ssilby goto done; 503125420Ssilby } 504125420Ssilby uio->uio_resid += iov->iov_len; 505125420Ssilby iov++; 506125420Ssilby } 507125296Ssilby 508125296Ssilbydone: 509125420Ssilby if (error && uio->uio_iov) { 510125420Ssilby FREE(uio->uio_iov, M_IOV); 511125420Ssilby uio->uio_iov = NULL; 512125296Ssilby } 513125296Ssilby return (error); 514125296Ssilby 515125296Ssilby} 516