Introduce zpool_get_prop_literal interface
[zfs.git] / module / zcommon / zfs_uio.c
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  */
38
39 /*
40  * The uio support from OpenSolaris has been added as a short term
41  * work around.  The hope is to adopt native Linux type and drop the
42  * use of uio's entirely.  Under Linux they only add overhead and
43  * when possible we want to use native APIs for the ZPL layer.
44  */
45 #ifdef _KERNEL
46
47 #include <sys/types.h>
48 #include <sys/uio_impl.h>
49
50 /*
51  * Move "n" bytes at byte address "p"; "rw" indicates the direction
52  * of the move, and the I/O parameters are provided in "uio", which is
53  * update to reflect the data which was moved.  Returns 0 on success or
54  * a non-zero errno on failure.
55  */
56 int
57 uiomove(void *p, size_t n, enum uio_rw rw, struct uio *uio)
58 {
59         struct iovec *iov;
60         ulong_t cnt;
61
62         while (n && uio->uio_resid) {
63                 iov = uio->uio_iov;
64                 cnt = MIN(iov->iov_len, n);
65                 if (cnt == 0l) {
66                         uio->uio_iov++;
67                         uio->uio_iovcnt--;
68                         continue;
69                 }
70                 switch (uio->uio_segflg) {
71                 case UIO_USERSPACE:
72                 case UIO_USERISPACE:
73                         /* p = kernel data pointer
74                          * iov->iov_base = user data pointer */
75
76                         if (rw == UIO_READ) {
77                                 if (copy_to_user(iov->iov_base, p, cnt))
78                                         return EFAULT;
79                                 /* error = xcopyout_nta(p, iov->iov_base, cnt,
80                                  * (uio->uio_extflg & UIO_COPY_CACHED)); */
81                         } else {
82                                 /* error = xcopyin_nta(iov->iov_base, p, cnt,
83                                  * (uio->uio_extflg & UIO_COPY_CACHED)); */
84                                 if (copy_from_user(p, iov->iov_base, cnt))
85                                         return EFAULT;
86                         }
87                         break;
88                 case UIO_SYSSPACE:
89                         if (rw == UIO_READ)
90                                 bcopy(p, iov->iov_base, cnt);
91                         else
92                                 bcopy(iov->iov_base, p, cnt);
93                         break;
94                 }
95                 iov->iov_base += cnt;
96                 iov->iov_len -= cnt;
97                 uio->uio_resid -= cnt;
98                 uio->uio_loffset += cnt;
99                 p = (caddr_t)p + cnt;
100                 n -= cnt;
101         }
102         return (0);
103 }
104 EXPORT_SYMBOL(uiomove);
105
106 #define fuword8(uptr, vptr) get_user((*vptr), (uptr))
107
108 /*
109  * Fault in the pages of the first n bytes specified by the uio structure.
110  * 1 byte in each page is touched and the uio struct is unmodified. Any
111  * error will terminate the process as this is only a best attempt to get
112  * the pages resident.
113  */
114 void
115 uio_prefaultpages(ssize_t n, struct uio *uio)
116 {
117         struct iovec *iov;
118         ulong_t cnt, incr;
119         caddr_t p;
120         uint8_t tmp;
121         int iovcnt;
122
123         iov = uio->uio_iov;
124         iovcnt = uio->uio_iovcnt;
125
126         while ((n > 0) && (iovcnt > 0)) {
127                 cnt = MIN(iov->iov_len, n);
128                 if (cnt == 0) {
129                         /* empty iov entry */
130                         iov++;
131                         iovcnt--;
132                         continue;
133                 }
134                 n -= cnt;
135                 /*
136                  * touch each page in this segment.
137                  */
138                 p = iov->iov_base;
139                 while (cnt) {
140                         switch (uio->uio_segflg) {
141                         case UIO_USERSPACE:
142                         case UIO_USERISPACE:
143                                 if (fuword8((uint8_t *) p, &tmp))
144                                         return;
145                                 break;
146                         case UIO_SYSSPACE:
147                                 bcopy(p, &tmp, 1);
148                                 break;
149                         }
150                         incr = MIN(cnt, PAGESIZE);
151                         p += incr;
152                         cnt -= incr;
153                 }
154                 /*
155                  * touch the last byte in case it straddles a page.
156                  */
157                 p--;
158                 switch (uio->uio_segflg) {
159                 case UIO_USERSPACE:
160                 case UIO_USERISPACE:
161                         if (fuword8((uint8_t *) p, &tmp))
162                                 return;
163                         break;
164                 case UIO_SYSSPACE:
165                         bcopy(p, &tmp, 1);
166                         break;
167                 }
168                 iov++;
169                 iovcnt--;
170         }
171 }
172 EXPORT_SYMBOL(uio_prefaultpages);
173
174 /*
175  * same as uiomove() but doesn't modify uio structure.
176  * return in cbytes how many bytes were copied.
177  */
178 int
179 uiocopy(void *p, size_t n, enum uio_rw rw, struct uio *uio, size_t *cbytes)
180 {
181         struct iovec *iov;
182         ulong_t cnt;
183         int iovcnt;
184
185         iovcnt = uio->uio_iovcnt;
186         *cbytes = 0;
187
188         for (iov = uio->uio_iov; n && iovcnt; iov++, iovcnt--) {
189                 cnt = MIN(iov->iov_len, n);
190                 if (cnt == 0)
191                         continue;
192
193                 switch (uio->uio_segflg) {
194
195                 case UIO_USERSPACE:
196                 case UIO_USERISPACE:
197                         /* p = kernel data pointer
198                          * iov->iov_base = user data pointer */
199
200                         if (rw == UIO_READ) {
201                                 /* * UIO_READ = copy data from kernel to user * */
202                                 if (copy_to_user(iov->iov_base, p, cnt))
203                                         return EFAULT;
204                                 /* error = xcopyout_nta(p, iov->iov_base, cnt,
205                                  * (uio->uio_extflg & UIO_COPY_CACHED)); */
206                         } else {
207                                 /* * UIO_WRITE = copy data from user to kernel * */
208                                 /* error = xcopyin_nta(iov->iov_base, p, cnt,
209                                  * (uio->uio_extflg & UIO_COPY_CACHED)); */
210                                 if (copy_from_user(p, iov->iov_base, cnt))
211                                         return EFAULT;
212                         }
213                         break;
214
215                 case UIO_SYSSPACE:
216                         if (rw == UIO_READ)
217                                 bcopy(p, iov->iov_base, cnt);
218                         else
219                                 bcopy(iov->iov_base, p, cnt);
220                         break;
221                 }
222                 p = (caddr_t)p + cnt;
223                 n -= cnt;
224                 *cbytes += cnt;
225         }
226         return (0);
227 }
228 EXPORT_SYMBOL(uiocopy);
229
230 /*
231  * Drop the next n chars out of *uiop.
232  */
233 void
234 uioskip(uio_t *uiop, size_t n)
235 {
236         if (n > uiop->uio_resid)
237                 return;
238         while (n != 0) {
239                 iovec_t *iovp = uiop->uio_iov;
240                 size_t          niovb = MIN(iovp->iov_len, n);
241
242                 if (niovb == 0) {
243                         uiop->uio_iov++;
244                         uiop->uio_iovcnt--;
245                         continue;
246                 }
247                 iovp->iov_base += niovb;
248                 uiop->uio_loffset += niovb;
249                 iovp->iov_len -= niovb;
250                 uiop->uio_resid -= niovb;
251                 n -= niovb;
252         }
253 }
254 EXPORT_SYMBOL(uioskip);
255 #endif /* _KERNEL */