]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/iov-iter.c
pwm: make PWM_POLARITY_INVERTED flag optional
[karo-tx-linux.git] / fs / iov-iter.c
1 #include <linux/module.h>
2 #include <linux/fs.h>
3 #include <linux/uaccess.h>
4 #include <linux/uio.h>
5 #include <linux/hardirq.h>
6 #include <linux/highmem.h>
7 #include <linux/pagemap.h>
8 #include <linux/bio.h>
9
10 static size_t __iovec_copy_to_user(char *vaddr, const struct iovec *iov,
11                                    size_t base, size_t bytes, int atomic)
12 {
13         size_t copied = 0, left = 0;
14
15         while (bytes) {
16                 char __user *buf = iov->iov_base + base;
17                 int copy = min(bytes, iov->iov_len - base);
18
19                 base = 0;
20                 if (atomic)
21                         left = __copy_to_user_inatomic(buf, vaddr, copy);
22                 else
23                         left = __copy_to_user(buf, vaddr, copy);
24                 copied += copy;
25                 bytes -= copy;
26                 vaddr += copy;
27                 iov++;
28
29                 if (unlikely(left))
30                         break;
31         }
32         return copied - left;
33 }
34
35 /*
36  * Copy as much as we can into the page and return the number of bytes which
37  * were sucessfully copied.  If a fault is encountered then return the number of
38  * bytes which were copied.
39  */
40 static size_t ii_iovec_copy_to_user_atomic(struct page *page,
41                 struct iov_iter *i, unsigned long offset, size_t bytes)
42 {
43         struct iovec *iov = (struct iovec *)i->data;
44         char *kaddr;
45         size_t copied;
46
47         BUG_ON(!in_atomic());
48         kaddr = kmap_atomic(page);
49         if (likely(i->nr_segs == 1)) {
50                 int left;
51                 char __user *buf = iov->iov_base + i->iov_offset;
52                 left = __copy_to_user_inatomic(buf, kaddr + offset, bytes);
53                 copied = bytes - left;
54         } else {
55                 copied = __iovec_copy_to_user(kaddr + offset, iov,
56                                               i->iov_offset, bytes, 1);
57         }
58         kunmap_atomic(kaddr);
59
60         return copied;
61 }
62
63 /*
64  * This has the same sideeffects and return value as
65  * ii_iovec_copy_to_user_atomic().
66  * The difference is that it attempts to resolve faults.
67  * Page must not be locked.
68  */
69 static size_t ii_iovec_copy_to_user(struct page *page,
70                 struct iov_iter *i, unsigned long offset, size_t bytes,
71                 int check_access)
72 {
73         struct iovec *iov = (struct iovec *)i->data;
74         char *kaddr;
75         size_t copied;
76
77         if (check_access) {
78                 might_sleep();
79                 if (generic_segment_checks(iov, &i->nr_segs, &bytes,
80                                            VERIFY_WRITE))
81                         return 0;
82         }
83
84         if (likely(i->nr_segs == 1)) {
85                 int left;
86                 char __user *buf = iov->iov_base + i->iov_offset;
87                 /*
88                  * Faults on the destination of a read are common, so do it
89                  * before taking the kmap.
90                  */
91                 if (!fault_in_pages_writeable(buf, bytes)) {
92                         kaddr = kmap_atomic(page);
93                         left = __copy_to_user_inatomic(buf, kaddr + offset,
94                                                      bytes);
95                         kunmap_atomic(kaddr);
96                         if (left == 0)
97                                 goto success;
98                 }
99                 kaddr = kmap(page);
100                 left = copy_to_user(buf, kaddr + offset, bytes);
101                 kunmap(page);
102 success:
103                 copied = bytes - left;
104         } else {
105                 kaddr = kmap(page);
106                 copied = __iovec_copy_to_user(kaddr + offset, iov,
107                                               i->iov_offset, bytes, 0);
108                 kunmap(page);
109         }
110         return copied;
111 }
112
113 #ifdef CONFIG_BLOCK
114 /*
115  * As an easily verifiable first pass, we implement all the methods that
116  * copy data to and from bvec pages with one function.  We implement it
117  * all with kmap_atomic().
118  */
119 static size_t bvec_copy_tofrom_page(struct iov_iter *iter, struct page *page,
120                                     unsigned long page_offset, size_t bytes,
121                                     int topage)
122 {
123         struct bio_vec *bvec = (struct bio_vec *)iter->data;
124         size_t bvec_offset = iter->iov_offset;
125         size_t remaining = bytes;
126         void *bvec_map;
127         void *page_map;
128         size_t copy;
129
130         page_map = kmap_atomic(page);
131
132         BUG_ON(bytes > iter->count);
133         while (remaining) {
134                 BUG_ON(bvec->bv_len == 0);
135                 BUG_ON(bvec_offset >= bvec->bv_len);
136                 copy = min(remaining, bvec->bv_len - bvec_offset);
137                 bvec_map = kmap_atomic(bvec->bv_page);
138                 if (topage)
139                         memcpy(page_map + page_offset,
140                                bvec_map + bvec->bv_offset + bvec_offset,
141                                copy);
142                 else
143                         memcpy(bvec_map + bvec->bv_offset + bvec_offset,
144                                page_map + page_offset,
145                                copy);
146                 kunmap_atomic(bvec_map);
147                 remaining -= copy;
148                 bvec_offset += copy;
149                 page_offset += copy;
150                 if (bvec_offset == bvec->bv_len) {
151                         bvec_offset = 0;
152                         bvec++;
153                 }
154         }
155
156         kunmap_atomic(page_map);
157
158         return bytes;
159 }
160
161 static size_t ii_bvec_copy_to_user_atomic(struct page *page, struct iov_iter *i,
162                                           unsigned long offset, size_t bytes)
163 {
164         return bvec_copy_tofrom_page(i, page, offset, bytes, 0);
165 }
166 static size_t ii_bvec_copy_to_user(struct page *page, struct iov_iter *i,
167                                    unsigned long offset, size_t bytes,
168                                    int check_access)
169 {
170         return bvec_copy_tofrom_page(i, page, offset, bytes, 0);
171 }
172 static size_t ii_bvec_copy_from_user_atomic(struct page *page,
173                                             struct iov_iter *i,
174                                             unsigned long offset, size_t bytes)
175 {
176         return bvec_copy_tofrom_page(i, page, offset, bytes, 1);
177 }
178 static size_t ii_bvec_copy_from_user(struct page *page, struct iov_iter *i,
179                                      unsigned long offset, size_t bytes)
180 {
181         return bvec_copy_tofrom_page(i, page, offset, bytes, 1);
182 }
183
184 /*
185  * bio_vecs have a stricter structure than iovecs that might have
186  * come from userspace.  There are no zero length bio_vec elements.
187  */
188 static void ii_bvec_advance(struct iov_iter *i, size_t bytes)
189 {
190         struct bio_vec *bvec = (struct bio_vec *)i->data;
191         size_t offset = i->iov_offset;
192         size_t delta;
193
194         BUG_ON(i->count < bytes);
195         while (bytes) {
196                 BUG_ON(bvec->bv_len == 0);
197                 BUG_ON(bvec->bv_len <= offset);
198                 delta = min(bytes, bvec->bv_len - offset);
199                 offset += delta;
200                 i->count -= delta;
201                 bytes -= delta;
202                 if (offset == bvec->bv_len) {
203                         bvec++;
204                         offset = 0;
205                 }
206         }
207
208         i->data = (unsigned long)bvec;
209         i->iov_offset = offset;
210 }
211
212 /*
213  * pages pointed to by bio_vecs are always pinned.
214  */
215 static int ii_bvec_fault_in_readable(struct iov_iter *i, size_t bytes)
216 {
217         return 0;
218 }
219
220 static size_t ii_bvec_single_seg_count(const struct iov_iter *i)
221 {
222         const struct bio_vec *bvec = (struct bio_vec *)i->data;
223         if (i->nr_segs == 1)
224                 return i->count;
225         else
226                 return min(i->count, bvec->bv_len - i->iov_offset);
227 }
228
229 static int ii_bvec_shorten(struct iov_iter *i, size_t count)
230 {
231         return -EINVAL;
232 }
233
234 struct iov_iter_ops ii_bvec_ops = {
235         .ii_copy_to_user_atomic = ii_bvec_copy_to_user_atomic,
236         .ii_copy_to_user = ii_bvec_copy_to_user,
237         .ii_copy_from_user_atomic = ii_bvec_copy_from_user_atomic,
238         .ii_copy_from_user = ii_bvec_copy_from_user,
239         .ii_advance = ii_bvec_advance,
240         .ii_fault_in_readable = ii_bvec_fault_in_readable,
241         .ii_single_seg_count = ii_bvec_single_seg_count,
242         .ii_shorten = ii_bvec_shorten,
243 };
244 EXPORT_SYMBOL(ii_bvec_ops);
245 #endif  /* CONFIG_BLOCK */
246
247 static size_t __iovec_copy_from_user(char *vaddr, const struct iovec *iov,
248                                      size_t base, size_t bytes, int atomic)
249 {
250         size_t copied = 0, left = 0;
251
252         while (bytes) {
253                 char __user *buf = iov->iov_base + base;
254                 int copy = min(bytes, iov->iov_len - base);
255
256                 base = 0;
257                 if (atomic)
258                         left = __copy_from_user_inatomic(vaddr, buf, copy);
259                 else
260                         left = __copy_from_user(vaddr, buf, copy);
261                 copied += copy;
262                 bytes -= copy;
263                 vaddr += copy;
264                 iov++;
265
266                 if (unlikely(left))
267                         break;
268         }
269         return copied - left;
270 }
271
272 /*
273  * Copy as much as we can into the page and return the number of bytes which
274  * were successfully copied.  If a fault is encountered then return the number
275  * of bytes which were copied.
276  */
277 static size_t ii_iovec_copy_from_user_atomic(struct page *page,
278                 struct iov_iter *i, unsigned long offset, size_t bytes)
279 {
280         struct iovec *iov = (struct iovec *)i->data;
281         char *kaddr;
282         size_t copied;
283
284         BUG_ON(!in_atomic());
285         kaddr = kmap_atomic(page);
286         if (likely(i->nr_segs == 1)) {
287                 int left;
288                 char __user *buf = iov->iov_base + i->iov_offset;
289                 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
290                 copied = bytes - left;
291         } else {
292                 copied = __iovec_copy_from_user(kaddr + offset, iov,
293                                                 i->iov_offset, bytes, 1);
294         }
295         kunmap_atomic(kaddr);
296
297         return copied;
298 }
299 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
300
301 /*
302  * This has the same sideeffects and return value as
303  * ii_iovec_copy_from_user_atomic().
304  * The difference is that it attempts to resolve faults.
305  * Page must not be locked.
306  */
307 static size_t ii_iovec_copy_from_user(struct page *page,
308                 struct iov_iter *i, unsigned long offset, size_t bytes)
309 {
310         struct iovec *iov = (struct iovec *)i->data;
311         char *kaddr;
312         size_t copied;
313
314         kaddr = kmap(page);
315         if (likely(i->nr_segs == 1)) {
316                 int left;
317                 char __user *buf = iov->iov_base + i->iov_offset;
318                 left = __copy_from_user(kaddr + offset, buf, bytes);
319                 copied = bytes - left;
320         } else {
321                 copied = __iovec_copy_from_user(kaddr + offset, iov,
322                                                 i->iov_offset, bytes, 0);
323         }
324         kunmap(page);
325         return copied;
326 }
327
328 static void ii_iovec_advance(struct iov_iter *i, size_t bytes)
329 {
330         BUG_ON(i->count < bytes);
331
332         if (likely(i->nr_segs == 1)) {
333                 i->iov_offset += bytes;
334                 i->count -= bytes;
335         } else {
336                 struct iovec *iov = (struct iovec *)i->data;
337                 size_t base = i->iov_offset;
338                 unsigned long nr_segs = i->nr_segs;
339
340                 /*
341                  * The !iov->iov_len check ensures we skip over unlikely
342                  * zero-length segments (without overruning the iovec).
343                  */
344                 while (bytes || unlikely(i->count && !iov->iov_len)) {
345                         int copy;
346
347                         copy = min(bytes, iov->iov_len - base);
348                         BUG_ON(!i->count || i->count < copy);
349                         i->count -= copy;
350                         bytes -= copy;
351                         base += copy;
352                         if (iov->iov_len == base) {
353                                 iov++;
354                                 nr_segs--;
355                                 base = 0;
356                         }
357                 }
358                 i->data = (unsigned long)iov;
359                 i->iov_offset = base;
360                 i->nr_segs = nr_segs;
361         }
362 }
363
364 /*
365  * Fault in the first iovec of the given iov_iter, to a maximum length
366  * of bytes. Returns 0 on success, or non-zero if the memory could not be
367  * accessed (ie. because it is an invalid address).
368  *
369  * writev-intensive code may want this to prefault several iovecs -- that
370  * would be possible (callers must not rely on the fact that _only_ the
371  * first iovec will be faulted with the current implementation).
372  */
373 static int ii_iovec_fault_in_readable(struct iov_iter *i, size_t bytes)
374 {
375         struct iovec *iov = (struct iovec *)i->data;
376         char __user *buf = iov->iov_base + i->iov_offset;
377         bytes = min(bytes, iov->iov_len - i->iov_offset);
378         return fault_in_pages_readable(buf, bytes);
379 }
380
381 /*
382  * Return the count of just the current iov_iter segment.
383  */
384 static size_t ii_iovec_single_seg_count(const struct iov_iter *i)
385 {
386         const struct iovec *iov = (struct iovec *)i->data;
387         if (i->nr_segs == 1)
388                 return i->count;
389         else
390                 return min(i->count, iov->iov_len - i->iov_offset);
391 }
392
393 static int ii_iovec_shorten(struct iov_iter *i, size_t count)
394 {
395         struct iovec *iov = (struct iovec *)i->data;
396         i->nr_segs = iov_shorten(iov, i->nr_segs, count);
397         i->count = min(i->count, count);
398         return 0;
399 }
400
401 struct iov_iter_ops ii_iovec_ops = {
402         .ii_copy_to_user_atomic = ii_iovec_copy_to_user_atomic,
403         .ii_copy_to_user = ii_iovec_copy_to_user,
404         .ii_copy_from_user_atomic = ii_iovec_copy_from_user_atomic,
405         .ii_copy_from_user = ii_iovec_copy_from_user,
406         .ii_advance = ii_iovec_advance,
407         .ii_fault_in_readable = ii_iovec_fault_in_readable,
408         .ii_single_seg_count = ii_iovec_single_seg_count,
409         .ii_shorten = ii_iovec_shorten,
410 };
411 EXPORT_SYMBOL(ii_iovec_ops);