1 /* $NetBSD: uio.h,v 1.5 2010/12/14 01:21:25 haad Exp $ */ 2 3 /*- 4 * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org> 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 * 57 * $FreeBSD: src/sys/compat/opensolaris/sys/uio.h,v 1.1 2007/04/06 01:09:06 pjd Exp $ 58 */ 59 60 #ifndef _OPENSOLARIS_SYS_UIO_H_ 61 #define _OPENSOLARIS_SYS_UIO_H_ 62 63 #include_next <sys/uio.h> 64 #include <sys/sysmacros.h> 65 66 67 #ifndef _KERNEL 68 #include <assert.h> 69 #include <string.h> 70 71 #define FOF_OFFSET 1 /* Use the offset in uio argument */ 72 73 struct uio { 74 struct iovec *uio_iov; 75 int uio_iovcnt; 76 off_t uio_offset; 77 int uio_resid; 78 enum uio_seg uio_segflg; 79 enum uio_rw uio_rw; 80 void *uio_td; 81 }; 82 #endif 83 84 struct xuio { 85 struct uio xu_uio; 86 int xuio_rw; 87 void *xuio_priv; 88 }; 89 90 /* XXX HACK ? how xuio can be handled properly */ 91 #define uio_extflg uio_offset 92 #define UIO_XUIO 0x0004 /* Structure is xuio_t */ 93 94 #define XUIO_XUZC_PRIV(xuio) ((xuio)->xuio_priv); 95 #define XUIO_XUZC_RW(xuio) ((xuio)->xuio_rw); 96 97 typedef struct uio uio_t; 98 typedef struct xuio xuio_t; 99 typedef struct iovec iovec_t; 100 101 typedef enum uio_seg uio_seg_t; 102 103 #define uio_loffset uio_offset 104 105 int uiomove(void *, size_t, struct uio *); 106 107 static __inline int 108 zfs_uiomove(void *cp, size_t n, enum uio_rw dir, uio_t *uio) 109 { 110 111 assert(uio->uio_rw == dir); 112 return (uiomove(cp, (int)n, uio)); 113 } 114 115 static __inline int 116 zfs_uiocopy(void *cp, size_t n, enum uio_rw dir, uio_t *uio, size_t *cbytes) 117 { 118 uio_t uio2; 119 int err; 120 121 memcpy(&uio2, uio, sizeof(*uio)); 122 assert(uio->uio_rw == dir); 123 if ((err = uiomove(cp, (int)n, &uio2)) != 0) 124 return err; 125 126 *cbytes = uio->uio_resid - uio2.uio_resid; 127 128 return (0); 129 } 130 131 static __inline void 132 zfs_uioskip(uio_t *uiop, size_t n) 133 { 134 if (n > uiop->uio_resid) 135 return; 136 while (n != 0) { 137 register iovec_t *iovp = uiop->uio_iov; 138 register size_t niovb = MIN(iovp->iov_len, n); 139 140 if (niovb == 0) { 141 uiop->uio_iov++; 142 uiop->uio_iovcnt--; 143 continue; 144 } 145 iovp->iov_base += niovb; 146 uiop->uio_offset += niovb; 147 iovp->iov_len -= niovb; 148 uiop->uio_resid -= niovb; 149 n -= niovb; 150 } 151 152 } 153 154 #define uiomove(cp, n, dir, uio) zfs_uiomove((cp), (n), (dir), (uio)) 155 #define uiocopy(cp, n, dir, uio, cbytes) zfs_uiocopy((cp), (n), (dir), (uio), (cbytes)) 156 #define uioskip(uio, size) zfs_uioskip((uio), (size)) 157 158 #endif /* !_OPENSOLARIS_SYS_UIO_H_ */ 159