1 /* $OpenBSD: criov.c,v 1.16 2006/12/29 13:04:37 pedro Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Theo de Raadt 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/systm.h> 30 #include <sys/proc.h> 31 #include <sys/errno.h> 32 #include <sys/malloc.h> 33 #include <sys/kernel.h> 34 #include <sys/mbuf.h> 35 36 #include <uvm/uvm_extern.h> 37 38 #include <crypto/cryptodev.h> 39 40 void 41 cuio_copydata(struct uio *uio, int off, int len, caddr_t cp) 42 { 43 struct iovec *iov = uio->uio_iov; 44 int iol = uio->uio_iovcnt; 45 unsigned count; 46 47 if (off < 0) 48 panic("cuio_copydata: off %d < 0", off); 49 if (len < 0) 50 panic("cuio_copydata: len %d < 0", len); 51 while (off > 0) { 52 if (iol == 0) 53 panic("iov_copydata: empty in skip"); 54 if (off < iov->iov_len) 55 break; 56 off -= iov->iov_len; 57 iol--; 58 iov++; 59 } 60 while (len > 0) { 61 if (iol == 0) 62 panic("cuio_copydata: empty"); 63 count = min(iov->iov_len - off, len); 64 bcopy(((caddr_t)iov->iov_base) + off, cp, count); 65 len -= count; 66 cp += count; 67 off = 0; 68 iol--; 69 iov++; 70 } 71 } 72 73 void 74 cuio_copyback(struct uio *uio, int off, int len, const void *_cp) 75 { 76 struct iovec *iov = uio->uio_iov; 77 int iol = uio->uio_iovcnt; 78 unsigned count; 79 caddr_t cp = (caddr_t)_cp; 80 81 if (off < 0) 82 panic("cuio_copyback: off %d < 0", off); 83 if (len < 0) 84 panic("cuio_copyback: len %d < 0", len); 85 while (off > 0) { 86 if (iol == 0) 87 panic("cuio_copyback: empty in skip"); 88 if (off < iov->iov_len) 89 break; 90 off -= iov->iov_len; 91 iol--; 92 iov++; 93 } 94 while (len > 0) { 95 if (iol == 0) 96 panic("uio_copyback: empty"); 97 count = min(iov->iov_len - off, len); 98 bcopy(cp, ((caddr_t)iov->iov_base) + off, count); 99 len -= count; 100 cp += count; 101 off = 0; 102 iol--; 103 iov++; 104 } 105 } 106 107 int 108 cuio_getptr(struct uio *uio, int loc, int *off) 109 { 110 int ind, len; 111 112 ind = 0; 113 while (loc >= 0 && ind < uio->uio_iovcnt) { 114 len = uio->uio_iov[ind].iov_len; 115 if (len > loc) { 116 *off = loc; 117 return (ind); 118 } 119 loc -= len; 120 ind++; 121 } 122 123 if (ind > 0 && loc == 0) { 124 ind--; 125 *off = uio->uio_iov[ind].iov_len; 126 return (ind); 127 } 128 129 return (-1); 130 } 131 132 int 133 cuio_apply(struct uio *uio, int off, int len, 134 int (*f)(caddr_t, caddr_t, unsigned int), caddr_t fstate) 135 { 136 int rval, ind, uiolen; 137 unsigned int count; 138 139 if (len < 0) 140 panic("cuio_apply: len %d < 0", len); 141 if (off < 0) 142 panic("cuio_apply: off %d < 0", off); 143 144 ind = 0; 145 while (off > 0) { 146 if (ind >= uio->uio_iovcnt) 147 panic("cuio_apply: ind %d >= uio_iovcnt %d for off", 148 ind, uio->uio_iovcnt); 149 uiolen = uio->uio_iov[ind].iov_len; 150 if (off < uiolen) 151 break; 152 off -= uiolen; 153 ind++; 154 } 155 while (len > 0) { 156 if (ind >= uio->uio_iovcnt) 157 panic("cuio_apply: ind %d >= uio_iovcnt %d for len", 158 ind, uio->uio_iovcnt); 159 count = min(uio->uio_iov[ind].iov_len - off, len); 160 161 rval = f(fstate, (char *)uio->uio_iov[ind].iov_base + off, 162 count); 163 if (rval) 164 return (rval); 165 166 len -= count; 167 off = 0; 168 ind++; 169 } 170 171 return (0); 172 } 173