Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Berkeley style UIO structures - Alan Cox 1994. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | */ | |
607ca46e DH |
9 | #ifndef __LINUX_UIO_H |
10 | #define __LINUX_UIO_H | |
1da177e4 | 11 | |
92236878 | 12 | #include <linux/kernel.h> |
aa28de27 | 13 | #include <linux/thread_info.h> |
607ca46e | 14 | #include <uapi/linux/uio.h> |
1da177e4 | 15 | |
92236878 | 16 | struct page; |
241699cd | 17 | struct pipe_inode_info; |
812ed032 JS |
18 | |
19 | struct kvec { | |
20 | void *iov_base; /* and that should *never* hold a userland pointer */ | |
21 | size_t iov_len; | |
22 | }; | |
23 | ||
00e23707 | 24 | enum iter_type { |
62a8067a AV |
25 | ITER_IOVEC = 0, |
26 | ITER_KVEC = 2, | |
27 | ITER_BVEC = 4, | |
241699cd | 28 | ITER_PIPE = 8, |
62a8067a AV |
29 | }; |
30 | ||
92236878 | 31 | struct iov_iter { |
aa563d7b | 32 | unsigned int type; |
92236878 KO |
33 | size_t iov_offset; |
34 | size_t count; | |
62a8067a AV |
35 | union { |
36 | const struct iovec *iov; | |
a280455f | 37 | const struct kvec *kvec; |
62a8067a | 38 | const struct bio_vec *bvec; |
241699cd AV |
39 | struct pipe_inode_info *pipe; |
40 | }; | |
41 | union { | |
42 | unsigned long nr_segs; | |
27c0e374 AV |
43 | struct { |
44 | int idx; | |
45 | int start_idx; | |
46 | }; | |
62a8067a | 47 | }; |
92236878 KO |
48 | }; |
49 | ||
00e23707 DH |
50 | static inline enum iter_type iov_iter_type(const struct iov_iter *i) |
51 | { | |
52 | return i->type & ~(READ | WRITE); | |
53 | } | |
54 | ||
55 | static inline bool iter_is_iovec(const struct iov_iter *i) | |
56 | { | |
57 | return iov_iter_type(i) == ITER_IOVEC; | |
58 | } | |
59 | ||
60 | static inline bool iov_iter_is_kvec(const struct iov_iter *i) | |
61 | { | |
62 | return iov_iter_type(i) == ITER_KVEC; | |
63 | } | |
64 | ||
65 | static inline bool iov_iter_is_bvec(const struct iov_iter *i) | |
66 | { | |
67 | return iov_iter_type(i) == ITER_BVEC; | |
68 | } | |
69 | ||
70 | static inline bool iov_iter_is_pipe(const struct iov_iter *i) | |
71 | { | |
72 | return iov_iter_type(i) == ITER_PIPE; | |
73 | } | |
74 | ||
75 | static inline unsigned char iov_iter_rw(const struct iov_iter *i) | |
76 | { | |
77 | return i->type & (READ | WRITE); | |
78 | } | |
79 | ||
1da177e4 LT |
80 | /* |
81 | * Total number of bytes covered by an iovec. | |
82 | * | |
83 | * NOTE that it is not safe to use this function until all the iovec's | |
84 | * segment lengths have been validated. Because the individual lengths can | |
85 | * overflow a size_t when added together. | |
86 | */ | |
87 | static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs) | |
88 | { | |
89 | unsigned long seg; | |
90 | size_t ret = 0; | |
91 | ||
92 | for (seg = 0; seg < nr_segs; seg++) | |
93 | ret += iov[seg].iov_len; | |
94 | return ret; | |
95 | } | |
96 | ||
92236878 KO |
97 | static inline struct iovec iov_iter_iovec(const struct iov_iter *iter) |
98 | { | |
99 | return (struct iovec) { | |
100 | .iov_base = iter->iov->iov_base + iter->iov_offset, | |
101 | .iov_len = min(iter->count, | |
102 | iter->iov->iov_len - iter->iov_offset), | |
103 | }; | |
104 | } | |
105 | ||
106 | #define iov_for_each(iov, iter, start) \ | |
00e23707 DH |
107 | if (iov_iter_type(start) == ITER_IOVEC || \ |
108 | iov_iter_type(start) == ITER_KVEC) \ | |
92236878 KO |
109 | for (iter = (start); \ |
110 | (iter).count && \ | |
111 | ((iov = iov_iter_iovec(&(iter))), 1); \ | |
112 | iov_iter_advance(&(iter), (iov).iov_len)) | |
113 | ||
92236878 KO |
114 | size_t iov_iter_copy_from_user_atomic(struct page *page, |
115 | struct iov_iter *i, unsigned long offset, size_t bytes); | |
92236878 | 116 | void iov_iter_advance(struct iov_iter *i, size_t bytes); |
27c0e374 | 117 | void iov_iter_revert(struct iov_iter *i, size_t bytes); |
92236878 KO |
118 | int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes); |
119 | size_t iov_iter_single_seg_count(const struct iov_iter *i); | |
6e58e79d AV |
120 | size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, |
121 | struct iov_iter *i); | |
f0d1bec9 AV |
122 | size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, |
123 | struct iov_iter *i); | |
aa28de27 AV |
124 | |
125 | size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i); | |
126 | size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i); | |
127 | bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i); | |
128 | size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i); | |
129 | bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i); | |
130 | ||
131 | static __always_inline __must_check | |
132 | size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) | |
133 | { | |
134 | if (unlikely(!check_copy_size(addr, bytes, true))) | |
c43aeb19 | 135 | return 0; |
aa28de27 AV |
136 | else |
137 | return _copy_to_iter(addr, bytes, i); | |
138 | } | |
139 | ||
140 | static __always_inline __must_check | |
141 | size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) | |
142 | { | |
143 | if (unlikely(!check_copy_size(addr, bytes, false))) | |
c43aeb19 | 144 | return 0; |
aa28de27 AV |
145 | else |
146 | return _copy_from_iter(addr, bytes, i); | |
147 | } | |
148 | ||
149 | static __always_inline __must_check | |
150 | bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i) | |
151 | { | |
152 | if (unlikely(!check_copy_size(addr, bytes, false))) | |
153 | return false; | |
154 | else | |
155 | return _copy_from_iter_full(addr, bytes, i); | |
156 | } | |
157 | ||
158 | static __always_inline __must_check | |
159 | size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) | |
160 | { | |
161 | if (unlikely(!check_copy_size(addr, bytes, false))) | |
c43aeb19 | 162 | return 0; |
aa28de27 AV |
163 | else |
164 | return _copy_from_iter_nocache(addr, bytes, i); | |
165 | } | |
166 | ||
167 | static __always_inline __must_check | |
168 | bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i) | |
169 | { | |
170 | if (unlikely(!check_copy_size(addr, bytes, false))) | |
171 | return false; | |
172 | else | |
173 | return _copy_from_iter_full_nocache(addr, bytes, i); | |
174 | } | |
175 | ||
0aed55af DW |
176 | #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE |
177 | /* | |
178 | * Note, users like pmem that depend on the stricter semantics of | |
179 | * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for | |
180 | * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the | |
181 | * destination is flushed from the cache on return. | |
182 | */ | |
6a37e940 | 183 | size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i); |
0aed55af | 184 | #else |
6a37e940 LT |
185 | #define _copy_from_iter_flushcache _copy_from_iter_nocache |
186 | #endif | |
187 | ||
8780356e | 188 | #ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE |
522239b4 | 189 | size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i); |
8780356e DW |
190 | #else |
191 | #define _copy_to_iter_mcsafe _copy_to_iter | |
192 | #endif | |
193 | ||
6a37e940 LT |
194 | static __always_inline __must_check |
195 | size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) | |
0aed55af | 196 | { |
6a37e940 | 197 | if (unlikely(!check_copy_size(addr, bytes, false))) |
c43aeb19 | 198 | return 0; |
6a37e940 LT |
199 | else |
200 | return _copy_from_iter_flushcache(addr, bytes, i); | |
0aed55af | 201 | } |
6a37e940 | 202 | |
8780356e DW |
203 | static __always_inline __must_check |
204 | size_t copy_to_iter_mcsafe(void *addr, size_t bytes, struct iov_iter *i) | |
205 | { | |
dfb06cba | 206 | if (unlikely(!check_copy_size(addr, bytes, true))) |
8780356e DW |
207 | return 0; |
208 | else | |
209 | return _copy_to_iter_mcsafe(addr, bytes, i); | |
210 | } | |
211 | ||
c35e0248 | 212 | size_t iov_iter_zero(size_t bytes, struct iov_iter *); |
886a3911 | 213 | unsigned long iov_iter_alignment(const struct iov_iter *i); |
357f435d | 214 | unsigned long iov_iter_gap_alignment(const struct iov_iter *i); |
aa563d7b | 215 | void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov, |
71d8e532 | 216 | unsigned long nr_segs, size_t count); |
aa563d7b | 217 | void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec, |
05afcb77 | 218 | unsigned long nr_segs, size_t count); |
aa563d7b | 219 | void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec, |
abb78f87 | 220 | unsigned long nr_segs, size_t count); |
aa563d7b | 221 | void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe, |
241699cd | 222 | size_t count); |
7b2c99d1 | 223 | ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages, |
2c80929c | 224 | size_t maxsize, unsigned maxpages, size_t *start); |
91f79c43 AV |
225 | ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages, |
226 | size_t maxsize, size_t *start); | |
f67da30c | 227 | int iov_iter_npages(const struct iov_iter *i, int maxpages); |
92236878 | 228 | |
4b8164b9 AV |
229 | const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags); |
230 | ||
b57332b4 | 231 | static inline size_t iov_iter_count(const struct iov_iter *i) |
92236878 KO |
232 | { |
233 | return i->count; | |
234 | } | |
235 | ||
0b86dbf6 AV |
236 | /* |
237 | * Cap the iov_iter by given limit; note that the second argument is | |
238 | * *not* the new size - it's upper limit for such. Passing it a value | |
239 | * greater than the amount of data in iov_iter is fine - it'll just do | |
240 | * nothing in that case. | |
241 | */ | |
242 | static inline void iov_iter_truncate(struct iov_iter *i, u64 count) | |
0c949334 | 243 | { |
0b86dbf6 AV |
244 | /* |
245 | * count doesn't have to fit in size_t - comparison extends both | |
246 | * operands to u64 here and any value that would be truncated by | |
247 | * conversion in assignement is by definition greater than all | |
248 | * values of size_t, including old i->count. | |
249 | */ | |
0c949334 AV |
250 | if (i->count > count) |
251 | i->count = count; | |
252 | } | |
253 | ||
b42b15fd AV |
254 | /* |
255 | * reexpand a previously truncated iterator; count must be no more than how much | |
256 | * we had shrunk it. | |
257 | */ | |
258 | static inline void iov_iter_reexpand(struct iov_iter *i, size_t count) | |
259 | { | |
260 | i->count = count; | |
261 | } | |
36f7a8a4 | 262 | size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum, struct iov_iter *i); |
a604ec7e | 263 | size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i); |
cbbd26b8 | 264 | bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i); |
b42b15fd | 265 | |
bc917be8 AV |
266 | int import_iovec(int type, const struct iovec __user * uvector, |
267 | unsigned nr_segs, unsigned fast_segs, | |
268 | struct iovec **iov, struct iov_iter *i); | |
269 | ||
270 | #ifdef CONFIG_COMPAT | |
271 | struct compat_iovec; | |
272 | int compat_import_iovec(int type, const struct compat_iovec __user * uvector, | |
273 | unsigned nr_segs, unsigned fast_segs, | |
274 | struct iovec **iov, struct iov_iter *i); | |
275 | #endif | |
276 | ||
277 | int import_single_range(int type, void __user *buf, size_t len, | |
278 | struct iovec *iov, struct iov_iter *i); | |
279 | ||
09cf698a AV |
280 | int iov_iter_for_each_range(struct iov_iter *i, size_t bytes, |
281 | int (*f)(struct kvec *vec, void *context), | |
282 | void *context); | |
283 | ||
812ed032 | 284 | #endif |