[xarray] iov_iter_npages(): just use DIV_ROUND_UP()
[linux-block.git] / include / linux / uio.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4
LT
2/*
3 * Berkeley style UIO structures - Alan Cox 1994.
1da177e4 4 */
607ca46e
DH
5#ifndef __LINUX_UIO_H
6#define __LINUX_UIO_H
1da177e4 7
92236878 8#include <linux/kernel.h>
aa28de27 9#include <linux/thread_info.h>
607ca46e 10#include <uapi/linux/uio.h>
1da177e4 11
92236878 12struct page;
241699cd 13struct pipe_inode_info;
812ed032
JS
14
15struct kvec {
16 void *iov_base; /* and that should *never* hold a userland pointer */
17 size_t iov_len;
18};
19
00e23707 20enum iter_type {
875f1d07 21 /* iter types */
8cd54c1c
AV
22 ITER_IOVEC,
23 ITER_KVEC,
24 ITER_BVEC,
25 ITER_PIPE,
26 ITER_XARRAY,
27 ITER_DISCARD,
62a8067a
AV
28};
29
92236878 30struct iov_iter {
8cd54c1c
AV
31 u8 iter_type;
32 bool data_source;
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;
7ff50620 39 struct xarray *xarray;
241699cd
AV
40 struct pipe_inode_info *pipe;
41 };
42 union {
43 unsigned long nr_segs;
27c0e374 44 struct {
8cefc107
DH
45 unsigned int head;
46 unsigned int start_head;
27c0e374 47 };
7ff50620 48 loff_t xarray_start;
62a8067a 49 };
92236878
KO
50};
51
00e23707
DH
52static inline enum iter_type iov_iter_type(const struct iov_iter *i)
53{
8cd54c1c 54 return i->iter_type;
00e23707
DH
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
7ff50620
DH
82static inline bool iov_iter_is_xarray(const struct iov_iter *i)
83{
84 return iov_iter_type(i) == ITER_XARRAY;
85}
86
00e23707
DH
87static inline unsigned char iov_iter_rw(const struct iov_iter *i)
88{
8cd54c1c 89 return i->data_source ? WRITE : READ;
00e23707
DH
90}
91
1da177e4
LT
92/*
93 * Total number of bytes covered by an iovec.
94 *
95 * NOTE that it is not safe to use this function until all the iovec's
96 * segment lengths have been validated. Because the individual lengths can
97 * overflow a size_t when added together.
98 */
99static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
100{
101 unsigned long seg;
102 size_t ret = 0;
103
104 for (seg = 0; seg < nr_segs; seg++)
105 ret += iov[seg].iov_len;
106 return ret;
107}
108
92236878
KO
109static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
110{
111 return (struct iovec) {
112 .iov_base = iter->iov->iov_base + iter->iov_offset,
113 .iov_len = min(iter->count,
114 iter->iov->iov_len - iter->iov_offset),
115 };
116}
117
92236878
KO
118size_t iov_iter_copy_from_user_atomic(struct page *page,
119 struct iov_iter *i, unsigned long offset, size_t bytes);
92236878 120void iov_iter_advance(struct iov_iter *i, size_t bytes);
27c0e374 121void iov_iter_revert(struct iov_iter *i, size_t bytes);
8409a0d2 122int iov_iter_fault_in_readable(const struct iov_iter *i, size_t bytes);
92236878 123size_t iov_iter_single_seg_count(const struct iov_iter *i);
6e58e79d
AV
124size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
125 struct iov_iter *i);
f0d1bec9
AV
126size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
127 struct iov_iter *i);
aa28de27
AV
128
129size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
130size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
aa28de27 131size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
aa28de27
AV
132
133static __always_inline __must_check
134size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
135{
136 if (unlikely(!check_copy_size(addr, bytes, true)))
c43aeb19 137 return 0;
aa28de27
AV
138 else
139 return _copy_to_iter(addr, bytes, i);
140}
141
142static __always_inline __must_check
143size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
144{
145 if (unlikely(!check_copy_size(addr, bytes, false)))
c43aeb19 146 return 0;
aa28de27
AV
147 else
148 return _copy_from_iter(addr, bytes, i);
149}
150
151static __always_inline __must_check
152bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
153{
4b6c132b
AV
154 size_t copied = copy_from_iter(addr, bytes, i);
155 if (likely(copied == bytes))
156 return true;
157 iov_iter_revert(i, copied);
158 return false;
aa28de27
AV
159}
160
161static __always_inline __must_check
162size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
163{
164 if (unlikely(!check_copy_size(addr, bytes, false)))
c43aeb19 165 return 0;
aa28de27
AV
166 else
167 return _copy_from_iter_nocache(addr, bytes, i);
168}
169
170static __always_inline __must_check
171bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
172{
4b6c132b
AV
173 size_t copied = copy_from_iter_nocache(addr, bytes, i);
174 if (likely(copied == bytes))
175 return true;
176 iov_iter_revert(i, copied);
177 return false;
aa28de27
AV
178}
179
0aed55af
DW
180#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
181/*
182 * Note, users like pmem that depend on the stricter semantics of
183 * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for
184 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
185 * destination is flushed from the cache on return.
186 */
6a37e940 187size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
0aed55af 188#else
6a37e940
LT
189#define _copy_from_iter_flushcache _copy_from_iter_nocache
190#endif
191
ec6347bb
DW
192#ifdef CONFIG_ARCH_HAS_COPY_MC
193size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
8780356e 194#else
ec6347bb 195#define _copy_mc_to_iter _copy_to_iter
8780356e
DW
196#endif
197
6a37e940
LT
198static __always_inline __must_check
199size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
0aed55af 200{
6a37e940 201 if (unlikely(!check_copy_size(addr, bytes, false)))
c43aeb19 202 return 0;
6a37e940
LT
203 else
204 return _copy_from_iter_flushcache(addr, bytes, i);
0aed55af 205}
6a37e940 206
8780356e 207static __always_inline __must_check
ec6347bb 208size_t copy_mc_to_iter(void *addr, size_t bytes, struct iov_iter *i)
8780356e 209{
dfb06cba 210 if (unlikely(!check_copy_size(addr, bytes, true)))
8780356e
DW
211 return 0;
212 else
ec6347bb 213 return _copy_mc_to_iter(addr, bytes, i);
8780356e
DW
214}
215
c35e0248 216size_t iov_iter_zero(size_t bytes, struct iov_iter *);
886a3911 217unsigned long iov_iter_alignment(const struct iov_iter *i);
357f435d 218unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
aa563d7b 219void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
71d8e532 220 unsigned long nr_segs, size_t count);
aa563d7b 221void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
05afcb77 222 unsigned long nr_segs, size_t count);
aa563d7b 223void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
abb78f87 224 unsigned long nr_segs, size_t count);
aa563d7b 225void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
241699cd 226 size_t count);
9ea9ce04 227void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count);
7ff50620
DH
228void iov_iter_xarray(struct iov_iter *i, unsigned int direction, struct xarray *xarray,
229 loff_t start, size_t count);
7b2c99d1 230ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
2c80929c 231 size_t maxsize, unsigned maxpages, size_t *start);
91f79c43
AV
232ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
233 size_t maxsize, size_t *start);
f67da30c 234int iov_iter_npages(const struct iov_iter *i, int maxpages);
92236878 235
4b8164b9
AV
236const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
237
b57332b4 238static inline size_t iov_iter_count(const struct iov_iter *i)
92236878
KO
239{
240 return i->count;
241}
242
0b86dbf6
AV
243/*
244 * Cap the iov_iter by given limit; note that the second argument is
245 * *not* the new size - it's upper limit for such. Passing it a value
246 * greater than the amount of data in iov_iter is fine - it'll just do
247 * nothing in that case.
248 */
249static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
0c949334 250{
0b86dbf6
AV
251 /*
252 * count doesn't have to fit in size_t - comparison extends both
253 * operands to u64 here and any value that would be truncated by
254 * conversion in assignement is by definition greater than all
255 * values of size_t, including old i->count.
256 */
0c949334
AV
257 if (i->count > count)
258 i->count = count;
259}
260
b42b15fd
AV
261/*
262 * reexpand a previously truncated iterator; count must be no more than how much
263 * we had shrunk it.
264 */
265static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
266{
267 i->count = count;
268}
52cbd23a
WB
269
270struct csum_state {
271 __wsum csum;
272 size_t off;
273};
274
275size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csstate, struct iov_iter *i);
a604ec7e 276size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
4b6c132b
AV
277
278static __always_inline __must_check
279bool csum_and_copy_from_iter_full(void *addr, size_t bytes,
280 __wsum *csum, struct iov_iter *i)
281{
282 size_t copied = csum_and_copy_from_iter(addr, bytes, csum, i);
283 if (likely(copied == bytes))
284 return true;
285 iov_iter_revert(i, copied);
286 return false;
287}
d05f4435
SG
288size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
289 struct iov_iter *i);
b42b15fd 290
bfdc5970
CH
291struct iovec *iovec_from_user(const struct iovec __user *uvector,
292 unsigned long nr_segs, unsigned long fast_segs,
293 struct iovec *fast_iov, bool compat);
294ssize_t import_iovec(int type, const struct iovec __user *uvec,
295 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
296 struct iov_iter *i);
297ssize_t __import_iovec(int type, const struct iovec __user *uvec,
298 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
299 struct iov_iter *i, bool compat);
bc917be8
AV
300int import_single_range(int type, void __user *buf, size_t len,
301 struct iovec *iov, struct iov_iter *i);
302
812ed032 303#endif