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