1 /*	$NetBSD: uio.h,v 1.9 2015/09/26 03:32:17 christos 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 #define XUIO_XUZC_PRIV(xuio)	((xuio)->xuio_priv)
91 #define XUIO_XUZC_RW(xuio)	((xuio)->xuio_rw)
92 
93 typedef	struct uio	uio_t;
94 typedef struct xuio	xuio_t;
95 typedef	struct iovec	iovec_t;
96 
97 typedef enum uio_seg    uio_seg_t;
98 
99 #define	uio_loffset	uio_offset
100 
101 int	uiomove(void *, size_t, struct uio *);
102 
103 static __inline int
zfs_uiomove(void * cp,size_t n,enum uio_rw dir,uio_t * uio)104 zfs_uiomove(void *cp, size_t n, enum uio_rw dir, uio_t *uio)
105 {
106 
107 	assert(uio->uio_rw == dir);
108 	return (uiomove(cp, n, uio));
109 }
110 
111 static __inline int
zfs_uiocopy(void * cp,size_t n,enum uio_rw dir,uio_t * uio,size_t * cbytes)112 zfs_uiocopy(void *cp, size_t n, enum uio_rw dir, uio_t *uio, size_t *cbytes)
113 {
114 	uio_t uio2;
115 	int err;
116 
117 	memcpy(&uio2, uio, sizeof(*uio));
118 	assert(uio->uio_rw == dir);
119 	if ((err = uiomove(cp, n, &uio2)) != 0)
120 		return err;
121 
122 	*cbytes = uio->uio_resid - uio2.uio_resid;
123 
124 	return (0);
125 }
126 
127 static __inline void
zfs_uioskip(uio_t * uiop,size_t n)128 zfs_uioskip(uio_t *uiop, size_t n)
129 {
130 	if (n > (size_t)uiop->uio_resid)
131 		return;
132 	while (n != 0) {
133 		iovec_t        *iovp = uiop->uio_iov;
134 		size_t         niovb = MIN(iovp->iov_len, n);
135 
136 		if (niovb == 0) {
137 			uiop->uio_iov++;
138 			uiop->uio_iovcnt--;
139 			continue;
140 		}
141 		iovp->iov_base = (char *)iovp->iov_base + niovb;
142 		uiop->uio_offset += niovb;
143 		iovp->iov_len -= niovb;
144 		uiop->uio_resid -= niovb;
145 		n -= niovb;
146 	}
147 
148 }
149 
150 #define	uiomove(cp, n, dir, uio)	zfs_uiomove((cp), (n), (dir), (uio))
151 #define uiocopy(cp, n, dir, uio, cbytes) 	zfs_uiocopy((cp), (n), (dir), (uio), (cbytes))
152 #define uioskip(uio, size) 		zfs_uioskip((uio), (size))
153 
154 #endif	/* !_OPENSOLARIS_SYS_UIO_H_ */
155