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