]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zcommon/zfs_uio.c
Merge branch 'add_breaks_replaces_zfs_initramfs' into 'master'
[mirror_zfs-debian.git] / module / zcommon / zfs_uio.c
CommitLineData
590329b5
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27/* All Rights Reserved */
28
29/*
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
32 * All Rights Reserved
33 *
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
36 * contributors.
37 */
e10b0808
AX
38/*
39 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
40 */
590329b5
BB
41
42/*
43 * The uio support from OpenSolaris has been added as a short term
44 * work around. The hope is to adopt native Linux type and drop the
45 * use of uio's entirely. Under Linux they only add overhead and
46 * when possible we want to use native APIs for the ZPL layer.
47 */
48#ifdef _KERNEL
49
50#include <sys/types.h>
51#include <sys/uio_impl.h>
e10b0808 52#include <linux/kmap_compat.h>
590329b5
BB
53
54/*
55 * Move "n" bytes at byte address "p"; "rw" indicates the direction
56 * of the move, and the I/O parameters are provided in "uio", which is
57 * update to reflect the data which was moved. Returns 0 on success or
58 * a non-zero errno on failure.
59 */
e10b0808
AX
60static int
61uiomove_iov(void *p, size_t n, enum uio_rw rw, struct uio *uio)
590329b5 62{
e10b0808
AX
63 const struct iovec *iov = uio->uio_iov;
64 size_t skip = uio->uio_skip;
590329b5
BB
65 ulong_t cnt;
66
67 while (n && uio->uio_resid) {
e10b0808 68 cnt = MIN(iov->iov_len - skip, n);
590329b5
BB
69 switch (uio->uio_segflg) {
70 case UIO_USERSPACE:
71 case UIO_USERISPACE:
a08ee875
LG
72 /*
73 * p = kernel data pointer
74 * iov->iov_base = user data pointer
75 */
590329b5 76 if (rw == UIO_READ) {
e10b0808 77 if (copy_to_user(iov->iov_base+skip, p, cnt))
a08ee875 78 return (EFAULT);
590329b5 79 } else {
e10b0808 80 if (copy_from_user(p, iov->iov_base+skip, cnt))
a08ee875 81 return (EFAULT);
590329b5
BB
82 }
83 break;
84 case UIO_SYSSPACE:
85 if (rw == UIO_READ)
e10b0808 86 bcopy(p, iov->iov_base + skip, cnt);
590329b5 87 else
e10b0808 88 bcopy(iov->iov_base + skip, p, cnt);
590329b5 89 break;
e10b0808
AX
90 default:
91 ASSERT(0);
92 }
93 skip += cnt;
94 if (skip == iov->iov_len) {
95 skip = 0;
96 uio->uio_iov = (++iov);
97 uio->uio_iovcnt--;
98 }
99 uio->uio_skip = skip;
100 uio->uio_resid -= cnt;
101 uio->uio_loffset += cnt;
102 p = (caddr_t)p + cnt;
103 n -= cnt;
104 }
105 return (0);
106}
107
108static int
109uiomove_bvec(void *p, size_t n, enum uio_rw rw, struct uio *uio)
110{
111 const struct bio_vec *bv = uio->uio_bvec;
112 size_t skip = uio->uio_skip;
113 ulong_t cnt;
114
115 while (n && uio->uio_resid) {
116 void *paddr;
117 cnt = MIN(bv->bv_len - skip, n);
118
119 paddr = zfs_kmap_atomic(bv->bv_page, KM_USER1);
120 if (rw == UIO_READ)
121 bcopy(p, paddr + bv->bv_offset + skip, cnt);
122 else
123 bcopy(paddr + bv->bv_offset + skip, p, cnt);
124 zfs_kunmap_atomic(paddr, KM_USER1);
125
126 skip += cnt;
127 if (skip == bv->bv_len) {
128 skip = 0;
129 uio->uio_bvec = (++bv);
130 uio->uio_iovcnt--;
590329b5 131 }
e10b0808 132 uio->uio_skip = skip;
590329b5
BB
133 uio->uio_resid -= cnt;
134 uio->uio_loffset += cnt;
135 p = (caddr_t)p + cnt;
136 n -= cnt;
137 }
138 return (0);
139}
e10b0808
AX
140
141int
142uiomove(void *p, size_t n, enum uio_rw rw, struct uio *uio)
143{
144 if (uio->uio_segflg != UIO_BVEC)
145 return (uiomove_iov(p, n, rw, uio));
146 else
147 return (uiomove_bvec(p, n, rw, uio));
148}
590329b5
BB
149EXPORT_SYMBOL(uiomove);
150
a08ee875 151#define fuword8(uptr, vptr) get_user((*vptr), (uptr))
590329b5
BB
152
153/*
154 * Fault in the pages of the first n bytes specified by the uio structure.
155 * 1 byte in each page is touched and the uio struct is unmodified. Any
156 * error will terminate the process as this is only a best attempt to get
157 * the pages resident.
158 */
159void
160uio_prefaultpages(ssize_t n, struct uio *uio)
161{
e10b0808 162 const struct iovec *iov;
590329b5
BB
163 ulong_t cnt, incr;
164 caddr_t p;
165 uint8_t tmp;
166 int iovcnt;
cae5b340 167 size_t skip;
e10b0808
AX
168
169 /* no need to fault in kernel pages */
170 switch (uio->uio_segflg) {
171 case UIO_SYSSPACE:
172 case UIO_BVEC:
173 return;
174 case UIO_USERSPACE:
175 case UIO_USERISPACE:
176 break;
177 default:
178 ASSERT(0);
179 }
590329b5
BB
180
181 iov = uio->uio_iov;
182 iovcnt = uio->uio_iovcnt;
cae5b340 183 skip = uio->uio_skip;
590329b5 184
cae5b340 185 for (; n > 0 && iovcnt > 0; iov++, iovcnt--, skip = 0) {
e10b0808 186 cnt = MIN(iov->iov_len - skip, n);
cae5b340
AX
187 /* empty iov */
188 if (cnt == 0)
189 continue;
590329b5
BB
190 n -= cnt;
191 /*
192 * touch each page in this segment.
193 */
e10b0808 194 p = iov->iov_base + skip;
590329b5 195 while (cnt) {
cae5b340 196 if (fuword8((uint8_t *)p, &tmp))
e10b0808 197 return;
590329b5
BB
198 incr = MIN(cnt, PAGESIZE);
199 p += incr;
200 cnt -= incr;
201 }
202 /*
203 * touch the last byte in case it straddles a page.
204 */
205 p--;
cae5b340 206 if (fuword8((uint8_t *)p, &tmp))
e10b0808 207 return;
590329b5
BB
208 }
209}
210EXPORT_SYMBOL(uio_prefaultpages);
211
212/*
213 * same as uiomove() but doesn't modify uio structure.
214 * return in cbytes how many bytes were copied.
215 */
216int
217uiocopy(void *p, size_t n, enum uio_rw rw, struct uio *uio, size_t *cbytes)
218{
e10b0808
AX
219 struct uio uio_copy;
220 int ret;
590329b5 221
e10b0808
AX
222 bcopy(uio, &uio_copy, sizeof (struct uio));
223 ret = uiomove(p, n, rw, &uio_copy);
224 *cbytes = uio->uio_resid - uio_copy.uio_resid;
225 return (ret);
590329b5
BB
226}
227EXPORT_SYMBOL(uiocopy);
228
229/*
230 * Drop the next n chars out of *uiop.
231 */
232void
233uioskip(uio_t *uiop, size_t n)
234{
235 if (n > uiop->uio_resid)
236 return;
590329b5 237
e10b0808
AX
238 uiop->uio_skip += n;
239 if (uiop->uio_segflg != UIO_BVEC) {
240 while (uiop->uio_iovcnt &&
241 uiop->uio_skip >= uiop->uio_iov->iov_len) {
242 uiop->uio_skip -= uiop->uio_iov->iov_len;
590329b5
BB
243 uiop->uio_iov++;
244 uiop->uio_iovcnt--;
590329b5 245 }
e10b0808
AX
246 } else {
247 while (uiop->uio_iovcnt &&
248 uiop->uio_skip >= uiop->uio_bvec->bv_len) {
249 uiop->uio_skip -= uiop->uio_bvec->bv_len;
250 uiop->uio_bvec++;
251 uiop->uio_iovcnt--;
252 }
590329b5 253 }
e10b0808
AX
254 uiop->uio_loffset += n;
255 uiop->uio_resid -= n;
590329b5
BB
256}
257EXPORT_SYMBOL(uioskip);
258#endif /* _KERNEL */