xref: /openbmc/linux/lib/iov_iter.c (revision 33b75c1d)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27999096fSHerbert Xu #include <crypto/hash.h>
3d879cb83SAl Viro #include <linux/export.h>
42f8b5444SChristoph Hellwig #include <linux/bvec.h>
54d0e9df5SAlbert van der Linde #include <linux/fault-inject-usercopy.h>
6d879cb83SAl Viro #include <linux/uio.h>
7d879cb83SAl Viro #include <linux/pagemap.h>
828961998SIra Weiny #include <linux/highmem.h>
9d879cb83SAl Viro #include <linux/slab.h>
10d879cb83SAl Viro #include <linux/vmalloc.h>
11241699cdSAl Viro #include <linux/splice.h>
12bfdc5970SChristoph Hellwig #include <linux/compat.h>
13d879cb83SAl Viro #include <net/checksum.h>
14d05f4435SSagi Grimberg #include <linux/scatterlist.h>
15d0ef4c36SMarco Elver #include <linux/instrumented.h>
16d879cb83SAl Viro 
17241699cdSAl Viro #define PIPE_PARANOIA /* for now */
18241699cdSAl Viro 
19fcb14cb1SAl Viro /* covers ubuf and kbuf alike */
20fcb14cb1SAl Viro #define iterate_buf(i, n, base, len, off, __p, STEP) {		\
21fcb14cb1SAl Viro 	size_t __maybe_unused off = 0;				\
22fcb14cb1SAl Viro 	len = n;						\
23fcb14cb1SAl Viro 	base = __p + i->iov_offset;				\
24fcb14cb1SAl Viro 	len -= (STEP);						\
25fcb14cb1SAl Viro 	i->iov_offset += len;					\
26fcb14cb1SAl Viro 	n = len;						\
27fcb14cb1SAl Viro }
28fcb14cb1SAl Viro 
295c67aa90SAl Viro /* covers iovec and kvec alike */
30a6e4ec7bSAl Viro #define iterate_iovec(i, n, base, len, off, __p, STEP) {	\
317baa5099SAl Viro 	size_t off = 0;						\
32a6e4ec7bSAl Viro 	size_t skip = i->iov_offset;				\
337a1bcb5dSAl Viro 	do {							\
347baa5099SAl Viro 		len = min(n, __p->iov_len - skip);		\
357baa5099SAl Viro 		if (likely(len)) {				\
367baa5099SAl Viro 			base = __p->iov_base + skip;		\
377baa5099SAl Viro 			len -= (STEP);				\
387baa5099SAl Viro 			off += len;				\
397baa5099SAl Viro 			skip += len;				\
407baa5099SAl Viro 			n -= len;				\
417a1bcb5dSAl Viro 			if (skip < __p->iov_len)		\
427a1bcb5dSAl Viro 				break;				\
43d879cb83SAl Viro 		}						\
44d879cb83SAl Viro 		__p++;						\
457a1bcb5dSAl Viro 		skip = 0;					\
467a1bcb5dSAl Viro 	} while (n);						\
47a6e4ec7bSAl Viro 	i->iov_offset = skip;					\
487baa5099SAl Viro 	n = off;						\
49d879cb83SAl Viro }
50d879cb83SAl Viro 
51a6e4ec7bSAl Viro #define iterate_bvec(i, n, base, len, off, p, STEP) {		\
527baa5099SAl Viro 	size_t off = 0;						\
53a6e4ec7bSAl Viro 	unsigned skip = i->iov_offset;				\
547491a2bfSAl Viro 	while (n) {						\
557491a2bfSAl Viro 		unsigned offset = p->bv_offset + skip;		\
561b4fb5ffSAl Viro 		unsigned left;					\
5721b56c84SAl Viro 		void *kaddr = kmap_local_page(p->bv_page +	\
5821b56c84SAl Viro 					offset / PAGE_SIZE);	\
597baa5099SAl Viro 		base = kaddr + offset % PAGE_SIZE;		\
60a6e4ec7bSAl Viro 		len = min(min(n, (size_t)(p->bv_len - skip)),	\
617491a2bfSAl Viro 		     (size_t)(PAGE_SIZE - offset % PAGE_SIZE));	\
621b4fb5ffSAl Viro 		left = (STEP);					\
6321b56c84SAl Viro 		kunmap_local(kaddr);				\
647baa5099SAl Viro 		len -= left;					\
657baa5099SAl Viro 		off += len;					\
667baa5099SAl Viro 		skip += len;					\
677491a2bfSAl Viro 		if (skip == p->bv_len) {			\
687491a2bfSAl Viro 			skip = 0;				\
697491a2bfSAl Viro 			p++;					\
70d879cb83SAl Viro 		}						\
717baa5099SAl Viro 		n -= len;					\
721b4fb5ffSAl Viro 		if (left)					\
731b4fb5ffSAl Viro 			break;					\
747491a2bfSAl Viro 	}							\
75a6e4ec7bSAl Viro 	i->iov_offset = skip;					\
767baa5099SAl Viro 	n = off;						\
77d879cb83SAl Viro }
78d879cb83SAl Viro 
79a6e4ec7bSAl Viro #define iterate_xarray(i, n, base, len, __off, STEP) {		\
801b4fb5ffSAl Viro 	__label__ __out;					\
81622838f3SAl Viro 	size_t __off = 0;					\
82821979f5SMatthew Wilcox (Oracle) 	struct folio *folio;					\
83a6e4ec7bSAl Viro 	loff_t start = i->xarray_start + i->iov_offset;		\
844b179e9aSAl Viro 	pgoff_t index = start / PAGE_SIZE;			\
857ff50620SDavid Howells 	XA_STATE(xas, i->xarray, index);			\
867ff50620SDavid Howells 								\
87821979f5SMatthew Wilcox (Oracle) 	len = PAGE_SIZE - offset_in_page(start);		\
887ff50620SDavid Howells 	rcu_read_lock();					\
89821979f5SMatthew Wilcox (Oracle) 	xas_for_each(&xas, folio, ULONG_MAX) {			\
901b4fb5ffSAl Viro 		unsigned left;					\
91821979f5SMatthew Wilcox (Oracle) 		size_t offset;					\
92821979f5SMatthew Wilcox (Oracle) 		if (xas_retry(&xas, folio))			\
937ff50620SDavid Howells 			continue;				\
94821979f5SMatthew Wilcox (Oracle) 		if (WARN_ON(xa_is_value(folio)))		\
957ff50620SDavid Howells 			break;					\
96821979f5SMatthew Wilcox (Oracle) 		if (WARN_ON(folio_test_hugetlb(folio)))		\
977ff50620SDavid Howells 			break;					\
98821979f5SMatthew Wilcox (Oracle) 		offset = offset_in_folio(folio, start + __off);	\
99821979f5SMatthew Wilcox (Oracle) 		while (offset < folio_size(folio)) {		\
100821979f5SMatthew Wilcox (Oracle) 			base = kmap_local_folio(folio, offset);	\
1017baa5099SAl Viro 			len = min(n, len);			\
1021b4fb5ffSAl Viro 			left = (STEP);				\
103821979f5SMatthew Wilcox (Oracle) 			kunmap_local(base);			\
1047baa5099SAl Viro 			len -= left;				\
1057baa5099SAl Viro 			__off += len;				\
1067baa5099SAl Viro 			n -= len;				\
1071b4fb5ffSAl Viro 			if (left || n == 0)			\
1081b4fb5ffSAl Viro 				goto __out;			\
109821979f5SMatthew Wilcox (Oracle) 			offset += len;				\
110821979f5SMatthew Wilcox (Oracle) 			len = PAGE_SIZE;			\
1117ff50620SDavid Howells 		}						\
1127ff50620SDavid Howells 	}							\
1131b4fb5ffSAl Viro __out:								\
1147ff50620SDavid Howells 	rcu_read_unlock();					\
115a6e4ec7bSAl Viro 	i->iov_offset += __off;					\
116622838f3SAl Viro 	n = __off;						\
1177ff50620SDavid Howells }
1187ff50620SDavid Howells 
1197baa5099SAl Viro #define __iterate_and_advance(i, n, base, len, off, I, K) {	\
120dd254f5aSAl Viro 	if (unlikely(i->count < n))				\
121dd254f5aSAl Viro 		n = i->count;					\
122f5da8354SAl Viro 	if (likely(n)) {					\
123fcb14cb1SAl Viro 		if (likely(iter_is_ubuf(i))) {			\
124fcb14cb1SAl Viro 			void __user *base;			\
125fcb14cb1SAl Viro 			size_t len;				\
126fcb14cb1SAl Viro 			iterate_buf(i, n, base, len, off,	\
127fcb14cb1SAl Viro 						i->ubuf, (I)) 	\
128fcb14cb1SAl Viro 		} else if (likely(iter_is_iovec(i))) {		\
1295c67aa90SAl Viro 			const struct iovec *iov = i->iov;	\
1307baa5099SAl Viro 			void __user *base;			\
1317baa5099SAl Viro 			size_t len;				\
1327baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
133a6e4ec7bSAl Viro 						iov, (I))	\
134d879cb83SAl Viro 			i->nr_segs -= iov - i->iov;		\
135d879cb83SAl Viro 			i->iov = iov;				\
13628f38db7SAl Viro 		} else if (iov_iter_is_bvec(i)) {		\
13728f38db7SAl Viro 			const struct bio_vec *bvec = i->bvec;	\
1387baa5099SAl Viro 			void *base;				\
1397baa5099SAl Viro 			size_t len;				\
1407baa5099SAl Viro 			iterate_bvec(i, n, base, len, off,	\
141a6e4ec7bSAl Viro 						bvec, (K))	\
1427491a2bfSAl Viro 			i->nr_segs -= bvec - i->bvec;		\
1437491a2bfSAl Viro 			i->bvec = bvec;				\
14428f38db7SAl Viro 		} else if (iov_iter_is_kvec(i)) {		\
1455c67aa90SAl Viro 			const struct kvec *kvec = i->kvec;	\
1467baa5099SAl Viro 			void *base;				\
1477baa5099SAl Viro 			size_t len;				\
1487baa5099SAl Viro 			iterate_iovec(i, n, base, len, off,	\
149a6e4ec7bSAl Viro 						kvec, (K))	\
15028f38db7SAl Viro 			i->nr_segs -= kvec - i->kvec;		\
15128f38db7SAl Viro 			i->kvec = kvec;				\
15228f38db7SAl Viro 		} else if (iov_iter_is_xarray(i)) {		\
1537baa5099SAl Viro 			void *base;				\
1547baa5099SAl Viro 			size_t len;				\
1557baa5099SAl Viro 			iterate_xarray(i, n, base, len, off,	\
156a6e4ec7bSAl Viro 							(K))	\
157d879cb83SAl Viro 		}						\
158d879cb83SAl Viro 		i->count -= n;					\
159dd254f5aSAl Viro 	}							\
160d879cb83SAl Viro }
1617baa5099SAl Viro #define iterate_and_advance(i, n, base, len, off, I, K) \
1627baa5099SAl Viro 	__iterate_and_advance(i, n, base, len, off, I, ((void)(K),0))
163d879cb83SAl Viro 
16409fc68dcSAl Viro static int copyout(void __user *to, const void *from, size_t n)
16509fc68dcSAl Viro {
1664d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1674d0e9df5SAlbert van der Linde 		return n;
16896d4f267SLinus Torvalds 	if (access_ok(to, n)) {
169d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
17009fc68dcSAl Viro 		n = raw_copy_to_user(to, from, n);
17109fc68dcSAl Viro 	}
17209fc68dcSAl Viro 	return n;
17309fc68dcSAl Viro }
17409fc68dcSAl Viro 
17509fc68dcSAl Viro static int copyin(void *to, const void __user *from, size_t n)
17609fc68dcSAl Viro {
177*33b75c1dSAlexander Potapenko 	size_t res = n;
178*33b75c1dSAlexander Potapenko 
1794d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1804d0e9df5SAlbert van der Linde 		return n;
18196d4f267SLinus Torvalds 	if (access_ok(from, n)) {
182*33b75c1dSAlexander Potapenko 		instrument_copy_from_user_before(to, from, n);
183*33b75c1dSAlexander Potapenko 		res = raw_copy_from_user(to, from, n);
184*33b75c1dSAlexander Potapenko 		instrument_copy_from_user_after(to, from, n, res);
18509fc68dcSAl Viro 	}
186*33b75c1dSAlexander Potapenko 	return res;
18709fc68dcSAl Viro }
18809fc68dcSAl Viro 
1892dcedb2aSAl Viro static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe,
1902dcedb2aSAl Viro 					   unsigned int slot)
1912dcedb2aSAl Viro {
1922dcedb2aSAl Viro 	return &pipe->bufs[slot & (pipe->ring_size - 1)];
1932dcedb2aSAl Viro }
1942dcedb2aSAl Viro 
195241699cdSAl Viro #ifdef PIPE_PARANOIA
196241699cdSAl Viro static bool sanity(const struct iov_iter *i)
197241699cdSAl Viro {
198241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
1998cefc107SDavid Howells 	unsigned int p_head = pipe->head;
2008cefc107SDavid Howells 	unsigned int p_tail = pipe->tail;
2018cefc107SDavid Howells 	unsigned int p_occupancy = pipe_occupancy(p_head, p_tail);
2028cefc107SDavid Howells 	unsigned int i_head = i->head;
2038cefc107SDavid Howells 	unsigned int idx;
2048cefc107SDavid Howells 
20510f525a8SAl Viro 	if (i->last_offset) {
206241699cdSAl Viro 		struct pipe_buffer *p;
2078cefc107SDavid Howells 		if (unlikely(p_occupancy == 0))
208241699cdSAl Viro 			goto Bad;	// pipe must be non-empty
2098cefc107SDavid Howells 		if (unlikely(i_head != p_head - 1))
210241699cdSAl Viro 			goto Bad;	// must be at the last buffer...
211241699cdSAl Viro 
2122dcedb2aSAl Viro 		p = pipe_buf(pipe, i_head);
21310f525a8SAl Viro 		if (unlikely(p->offset + p->len != abs(i->last_offset)))
214241699cdSAl Viro 			goto Bad;	// ... at the end of segment
215241699cdSAl Viro 	} else {
2168cefc107SDavid Howells 		if (i_head != p_head)
217241699cdSAl Viro 			goto Bad;	// must be right after the last buffer
218241699cdSAl Viro 	}
219241699cdSAl Viro 	return true;
220241699cdSAl Viro Bad:
22110f525a8SAl Viro 	printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_offset);
2228cefc107SDavid Howells 	printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n",
2238cefc107SDavid Howells 			p_head, p_tail, pipe->ring_size);
2248cefc107SDavid Howells 	for (idx = 0; idx < pipe->ring_size; idx++)
225241699cdSAl Viro 		printk(KERN_ERR "[%p %p %d %d]\n",
226241699cdSAl Viro 			pipe->bufs[idx].ops,
227241699cdSAl Viro 			pipe->bufs[idx].page,
228241699cdSAl Viro 			pipe->bufs[idx].offset,
229241699cdSAl Viro 			pipe->bufs[idx].len);
230241699cdSAl Viro 	WARN_ON(1);
231241699cdSAl Viro 	return false;
232241699cdSAl Viro }
233241699cdSAl Viro #else
234241699cdSAl Viro #define sanity(i) true
235241699cdSAl Viro #endif
236241699cdSAl Viro 
23747b7fcaeSAl Viro static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size)
23847b7fcaeSAl Viro {
23947b7fcaeSAl Viro 	struct page *page = alloc_page(GFP_USER);
24047b7fcaeSAl Viro 	if (page) {
24147b7fcaeSAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
24247b7fcaeSAl Viro 		*buf = (struct pipe_buffer) {
24347b7fcaeSAl Viro 			.ops = &default_pipe_buf_ops,
24447b7fcaeSAl Viro 			.page = page,
24547b7fcaeSAl Viro 			.offset = 0,
24647b7fcaeSAl Viro 			.len = size
24747b7fcaeSAl Viro 		};
24847b7fcaeSAl Viro 	}
24947b7fcaeSAl Viro 	return page;
25047b7fcaeSAl Viro }
25147b7fcaeSAl Viro 
25247b7fcaeSAl Viro static void push_page(struct pipe_inode_info *pipe, struct page *page,
25347b7fcaeSAl Viro 			unsigned int offset, unsigned int size)
25447b7fcaeSAl Viro {
25547b7fcaeSAl Viro 	struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++);
25647b7fcaeSAl Viro 	*buf = (struct pipe_buffer) {
25747b7fcaeSAl Viro 		.ops = &page_cache_pipe_buf_ops,
25847b7fcaeSAl Viro 		.page = page,
25947b7fcaeSAl Viro 		.offset = offset,
26047b7fcaeSAl Viro 		.len = size
26147b7fcaeSAl Viro 	};
26247b7fcaeSAl Viro 	get_page(page);
26347b7fcaeSAl Viro }
26447b7fcaeSAl Viro 
26510f525a8SAl Viro static inline int last_offset(const struct pipe_buffer *buf)
2668fad7767SAl Viro {
26710f525a8SAl Viro 	if (buf->ops == &default_pipe_buf_ops)
26810f525a8SAl Viro 		return buf->len;	// buf->offset is 0 for those
26910f525a8SAl Viro 	else
27010f525a8SAl Viro 		return -(buf->offset + buf->len);
2718fad7767SAl Viro }
2728fad7767SAl Viro 
2738fad7767SAl Viro static struct page *append_pipe(struct iov_iter *i, size_t size,
2748fad7767SAl Viro 				unsigned int *off)
2758fad7767SAl Viro {
2768fad7767SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
27710f525a8SAl Viro 	int offset = i->last_offset;
2788fad7767SAl Viro 	struct pipe_buffer *buf;
2798fad7767SAl Viro 	struct page *page;
2808fad7767SAl Viro 
28110f525a8SAl Viro 	if (offset > 0 && offset < PAGE_SIZE) {
28210f525a8SAl Viro 		// some space in the last buffer; add to it
2838fad7767SAl Viro 		buf = pipe_buf(pipe, pipe->head - 1);
2848fad7767SAl Viro 		size = min_t(size_t, size, PAGE_SIZE - offset);
2858fad7767SAl Viro 		buf->len += size;
28610f525a8SAl Viro 		i->last_offset += size;
2878fad7767SAl Viro 		i->count -= size;
2888fad7767SAl Viro 		*off = offset;
2898fad7767SAl Viro 		return buf->page;
2908fad7767SAl Viro 	}
2918fad7767SAl Viro 	// OK, we need a new buffer
2928fad7767SAl Viro 	*off = 0;
2938fad7767SAl Viro 	size = min_t(size_t, size, PAGE_SIZE);
2948fad7767SAl Viro 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
2958fad7767SAl Viro 		return NULL;
2968fad7767SAl Viro 	page = push_anon(pipe, size);
2978fad7767SAl Viro 	if (!page)
2988fad7767SAl Viro 		return NULL;
2998fad7767SAl Viro 	i->head = pipe->head - 1;
30010f525a8SAl Viro 	i->last_offset = size;
3018fad7767SAl Viro 	i->count -= size;
3028fad7767SAl Viro 	return page;
3038fad7767SAl Viro }
3048fad7767SAl Viro 
305241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
306241699cdSAl Viro 			 struct iov_iter *i)
307241699cdSAl Viro {
308241699cdSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
30947b7fcaeSAl Viro 	unsigned int head = pipe->head;
310241699cdSAl Viro 
311241699cdSAl Viro 	if (unlikely(bytes > i->count))
312241699cdSAl Viro 		bytes = i->count;
313241699cdSAl Viro 
314241699cdSAl Viro 	if (unlikely(!bytes))
315241699cdSAl Viro 		return 0;
316241699cdSAl Viro 
317241699cdSAl Viro 	if (!sanity(i))
318241699cdSAl Viro 		return 0;
319241699cdSAl Viro 
32010f525a8SAl Viro 	if (offset && i->last_offset == -offset) { // could we merge it?
32147b7fcaeSAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, head - 1);
32247b7fcaeSAl Viro 		if (buf->page == page) {
323241699cdSAl Viro 			buf->len += bytes;
32410f525a8SAl Viro 			i->last_offset -= bytes;
32547b7fcaeSAl Viro 			i->count -= bytes;
32647b7fcaeSAl Viro 			return bytes;
327241699cdSAl Viro 		}
328241699cdSAl Viro 	}
32947b7fcaeSAl Viro 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
330241699cdSAl Viro 		return 0;
3318cefc107SDavid Howells 
33247b7fcaeSAl Viro 	push_page(pipe, page, offset, bytes);
33310f525a8SAl Viro 	i->last_offset = -(offset + bytes);
33447b7fcaeSAl Viro 	i->head = head;
335241699cdSAl Viro 	i->count -= bytes;
336241699cdSAl Viro 	return bytes;
337241699cdSAl Viro }
338241699cdSAl Viro 
339d879cb83SAl Viro /*
340a6294593SAndreas Gruenbacher  * fault_in_iov_iter_readable - fault in iov iterator for reading
341a6294593SAndreas Gruenbacher  * @i: iterator
342a6294593SAndreas Gruenbacher  * @size: maximum length
343171a0203SAnton Altaparmakov  *
344a6294593SAndreas Gruenbacher  * Fault in one or more iovecs of the given iov_iter, to a maximum length of
345a6294593SAndreas Gruenbacher  * @size.  For each iovec, fault in each page that constitutes the iovec.
346a6294593SAndreas Gruenbacher  *
347a6294593SAndreas Gruenbacher  * Returns the number of bytes not faulted in (like copy_to_user() and
348a6294593SAndreas Gruenbacher  * copy_from_user()).
349a6294593SAndreas Gruenbacher  *
350a6294593SAndreas Gruenbacher  * Always returns 0 for non-userspace iterators.
351171a0203SAnton Altaparmakov  */
352a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size)
353171a0203SAnton Altaparmakov {
354fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
355fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
356fcb14cb1SAl Viro 		n -= fault_in_readable(i->ubuf + i->iov_offset, n);
357fcb14cb1SAl Viro 		return size - n;
358fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
359a6294593SAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
3608409a0d2SAl Viro 		const struct iovec *p;
3618409a0d2SAl Viro 		size_t skip;
3628409a0d2SAl Viro 
363a6294593SAndreas Gruenbacher 		size -= count;
364a6294593SAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
365a6294593SAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
366a6294593SAndreas Gruenbacher 			size_t ret;
3678409a0d2SAl Viro 
3688409a0d2SAl Viro 			if (unlikely(!len))
3698409a0d2SAl Viro 				continue;
370a6294593SAndreas Gruenbacher 			ret = fault_in_readable(p->iov_base + skip, len);
371a6294593SAndreas Gruenbacher 			count -= len - ret;
372a6294593SAndreas Gruenbacher 			if (ret)
373a6294593SAndreas Gruenbacher 				break;
3748409a0d2SAl Viro 		}
375a6294593SAndreas Gruenbacher 		return count + size;
376171a0203SAnton Altaparmakov 	}
377171a0203SAnton Altaparmakov 	return 0;
378171a0203SAnton Altaparmakov }
379a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable);
380171a0203SAnton Altaparmakov 
381cdd591fcSAndreas Gruenbacher /*
382cdd591fcSAndreas Gruenbacher  * fault_in_iov_iter_writeable - fault in iov iterator for writing
383cdd591fcSAndreas Gruenbacher  * @i: iterator
384cdd591fcSAndreas Gruenbacher  * @size: maximum length
385cdd591fcSAndreas Gruenbacher  *
386cdd591fcSAndreas Gruenbacher  * Faults in the iterator using get_user_pages(), i.e., without triggering
387cdd591fcSAndreas Gruenbacher  * hardware page faults.  This is primarily useful when we already know that
388cdd591fcSAndreas Gruenbacher  * some or all of the pages in @i aren't in memory.
389cdd591fcSAndreas Gruenbacher  *
390cdd591fcSAndreas Gruenbacher  * Returns the number of bytes not faulted in, like copy_to_user() and
391cdd591fcSAndreas Gruenbacher  * copy_from_user().
392cdd591fcSAndreas Gruenbacher  *
393cdd591fcSAndreas Gruenbacher  * Always returns 0 for non-user-space iterators.
394cdd591fcSAndreas Gruenbacher  */
395cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size)
396cdd591fcSAndreas Gruenbacher {
397fcb14cb1SAl Viro 	if (iter_is_ubuf(i)) {
398fcb14cb1SAl Viro 		size_t n = min(size, iov_iter_count(i));
399fcb14cb1SAl Viro 		n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n);
400fcb14cb1SAl Viro 		return size - n;
401fcb14cb1SAl Viro 	} else if (iter_is_iovec(i)) {
402cdd591fcSAndreas Gruenbacher 		size_t count = min(size, iov_iter_count(i));
403cdd591fcSAndreas Gruenbacher 		const struct iovec *p;
404cdd591fcSAndreas Gruenbacher 		size_t skip;
405cdd591fcSAndreas Gruenbacher 
406cdd591fcSAndreas Gruenbacher 		size -= count;
407cdd591fcSAndreas Gruenbacher 		for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) {
408cdd591fcSAndreas Gruenbacher 			size_t len = min(count, p->iov_len - skip);
409cdd591fcSAndreas Gruenbacher 			size_t ret;
410cdd591fcSAndreas Gruenbacher 
411cdd591fcSAndreas Gruenbacher 			if (unlikely(!len))
412cdd591fcSAndreas Gruenbacher 				continue;
413cdd591fcSAndreas Gruenbacher 			ret = fault_in_safe_writeable(p->iov_base + skip, len);
414cdd591fcSAndreas Gruenbacher 			count -= len - ret;
415cdd591fcSAndreas Gruenbacher 			if (ret)
416cdd591fcSAndreas Gruenbacher 				break;
417cdd591fcSAndreas Gruenbacher 		}
418cdd591fcSAndreas Gruenbacher 		return count + size;
419cdd591fcSAndreas Gruenbacher 	}
420cdd591fcSAndreas Gruenbacher 	return 0;
421cdd591fcSAndreas Gruenbacher }
422cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable);
423cdd591fcSAndreas Gruenbacher 
424aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction,
425d879cb83SAl Viro 			const struct iovec *iov, unsigned long nr_segs,
426d879cb83SAl Viro 			size_t count)
427d879cb83SAl Viro {
428aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
4298cd54c1cSAl Viro 	*i = (struct iov_iter) {
4308cd54c1cSAl Viro 		.iter_type = ITER_IOVEC,
4313337ab08SAndreas Gruenbacher 		.nofault = false,
432fcb14cb1SAl Viro 		.user_backed = true,
4338cd54c1cSAl Viro 		.data_source = direction,
4348cd54c1cSAl Viro 		.iov = iov,
4358cd54c1cSAl Viro 		.nr_segs = nr_segs,
4368cd54c1cSAl Viro 		.iov_offset = 0,
4378cd54c1cSAl Viro 		.count = count
4388cd54c1cSAl Viro 	};
439d879cb83SAl Viro }
440d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init);
441d879cb83SAl Viro 
44212d426abSAl Viro // returns the offset in partial buffer (if any)
44312d426abSAl Viro static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages)
444241699cdSAl Viro {
44512d426abSAl Viro 	struct pipe_inode_info *pipe = i->pipe;
44612d426abSAl Viro 	int used = pipe->head - pipe->tail;
44710f525a8SAl Viro 	int off = i->last_offset;
4488cefc107SDavid Howells 
44912d426abSAl Viro 	*npages = max((int)pipe->max_usage - used, 0);
45012d426abSAl Viro 
45110f525a8SAl Viro 	if (off > 0 && off < PAGE_SIZE) { // anon and not full
45212d426abSAl Viro 		(*npages)++;
45312d426abSAl Viro 		return off;
45410f525a8SAl Viro 	}
45512d426abSAl Viro 	return 0;
456241699cdSAl Viro }
457241699cdSAl Viro 
458241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
459241699cdSAl Viro 				struct iov_iter *i)
460241699cdSAl Viro {
4618fad7767SAl Viro 	unsigned int off, chunk;
4628fad7767SAl Viro 
4638fad7767SAl Viro 	if (unlikely(bytes > i->count))
4648fad7767SAl Viro 		bytes = i->count;
4658fad7767SAl Viro 	if (unlikely(!bytes))
4668fad7767SAl Viro 		return 0;
467241699cdSAl Viro 
468241699cdSAl Viro 	if (!sanity(i))
469241699cdSAl Viro 		return 0;
470241699cdSAl Viro 
4718fad7767SAl Viro 	for (size_t n = bytes; n; n -= chunk) {
4728fad7767SAl Viro 		struct page *page = append_pipe(i, n, &off);
4738fad7767SAl Viro 		chunk = min_t(size_t, n, PAGE_SIZE - off);
4748fad7767SAl Viro 		if (!page)
4758fad7767SAl Viro 			return bytes - n;
4768fad7767SAl Viro 		memcpy_to_page(page, off, addr, chunk);
477241699cdSAl Viro 		addr += chunk;
4788fad7767SAl Viro 	}
479241699cdSAl Viro 	return bytes;
480241699cdSAl Viro }
481241699cdSAl Viro 
482f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len,
483f9152895SAl Viro 			      __wsum sum, size_t off)
484f9152895SAl Viro {
485cc44c17bSAl Viro 	__wsum next = csum_partial_copy_nocheck(from, to, len);
486f9152895SAl Viro 	return csum_block_add(sum, next, off);
487f9152895SAl Viro }
488f9152895SAl Viro 
48978e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes,
4906852df12SAl Viro 					 struct iov_iter *i, __wsum *sump)
49178e1f386SAl Viro {
4926852df12SAl Viro 	__wsum sum = *sump;
4936852df12SAl Viro 	size_t off = 0;
4948fad7767SAl Viro 	unsigned int chunk, r;
4958fad7767SAl Viro 
4968fad7767SAl Viro 	if (unlikely(bytes > i->count))
4978fad7767SAl Viro 		bytes = i->count;
4988fad7767SAl Viro 	if (unlikely(!bytes))
4998fad7767SAl Viro 		return 0;
50078e1f386SAl Viro 
50178e1f386SAl Viro 	if (!sanity(i))
50278e1f386SAl Viro 		return 0;
50378e1f386SAl Viro 
5046852df12SAl Viro 	while (bytes) {
5058fad7767SAl Viro 		struct page *page = append_pipe(i, bytes, &r);
5068fad7767SAl Viro 		char *p;
5078fad7767SAl Viro 
5088fad7767SAl Viro 		if (!page)
5098fad7767SAl Viro 			break;
5108fad7767SAl Viro 		chunk = min_t(size_t, bytes, PAGE_SIZE - r);
5118fad7767SAl Viro 		p = kmap_local_page(page);
5126852df12SAl Viro 		sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off);
5132495bdccSAl Viro 		kunmap_local(p);
51478e1f386SAl Viro 		off += chunk;
5158fad7767SAl Viro 		bytes -= chunk;
5166852df12SAl Viro 	}
5176852df12SAl Viro 	*sump = sum;
5186852df12SAl Viro 	return off;
51978e1f386SAl Viro }
52078e1f386SAl Viro 
521aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
522d879cb83SAl Viro {
52300e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
524241699cdSAl Viro 		return copy_pipe_to_iter(addr, bytes, i);
525fcb14cb1SAl Viro 	if (user_backed_iter(i))
52609fc68dcSAl Viro 		might_fault();
5277baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
5287baa5099SAl Viro 		copyout(base, addr + off, len),
5297baa5099SAl Viro 		memcpy(base, addr + off, len)
530d879cb83SAl Viro 	)
531d879cb83SAl Viro 
532d879cb83SAl Viro 	return bytes;
533d879cb83SAl Viro }
534aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter);
535d879cb83SAl Viro 
536ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC
537ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n)
5388780356eSDan Williams {
53996d4f267SLinus Torvalds 	if (access_ok(to, n)) {
540d0ef4c36SMarco Elver 		instrument_copy_to_user(to, from, n);
541ec6347bbSDan Williams 		n = copy_mc_to_user((__force void *) to, from, n);
5428780356eSDan Williams 	}
5438780356eSDan Williams 	return n;
5448780356eSDan Williams }
5458780356eSDan Williams 
546ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes,
547ca146f6fSDan Williams 				struct iov_iter *i)
548ca146f6fSDan Williams {
5498fad7767SAl Viro 	size_t xfer = 0;
5508fad7767SAl Viro 	unsigned int off, chunk;
5518fad7767SAl Viro 
5528fad7767SAl Viro 	if (unlikely(bytes > i->count))
5538fad7767SAl Viro 		bytes = i->count;
5548fad7767SAl Viro 	if (unlikely(!bytes))
5558fad7767SAl Viro 		return 0;
556ca146f6fSDan Williams 
557ca146f6fSDan Williams 	if (!sanity(i))
558ca146f6fSDan Williams 		return 0;
559ca146f6fSDan Williams 
5608fad7767SAl Viro 	while (bytes) {
5618fad7767SAl Viro 		struct page *page = append_pipe(i, bytes, &off);
562ca146f6fSDan Williams 		unsigned long rem;
5638fad7767SAl Viro 		char *p;
5648fad7767SAl Viro 
5658fad7767SAl Viro 		if (!page)
5668fad7767SAl Viro 			break;
5678fad7767SAl Viro 		chunk = min_t(size_t, bytes, PAGE_SIZE - off);
5688fad7767SAl Viro 		p = kmap_local_page(page);
5692a510a74SAl Viro 		rem = copy_mc_to_kernel(p + off, addr + xfer, chunk);
5702a510a74SAl Viro 		chunk -= rem;
5712a510a74SAl Viro 		kunmap_local(p);
5722a510a74SAl Viro 		xfer += chunk;
5738fad7767SAl Viro 		bytes -= chunk;
574c3497fd0SAl Viro 		if (rem) {
5758fad7767SAl Viro 			iov_iter_revert(i, rem);
576ca146f6fSDan Williams 			break;
577c3497fd0SAl Viro 		}
5782a510a74SAl Viro 	}
579ca146f6fSDan Williams 	return xfer;
580ca146f6fSDan Williams }
581ca146f6fSDan Williams 
582bf3eeb9bSDan Williams /**
583ec6347bbSDan Williams  * _copy_mc_to_iter - copy to iter with source memory error exception handling
584bf3eeb9bSDan Williams  * @addr: source kernel address
585bf3eeb9bSDan Williams  * @bytes: total transfer length
58644e55997SRandy Dunlap  * @i: destination iterator
587bf3eeb9bSDan Williams  *
588ec6347bbSDan Williams  * The pmem driver deploys this for the dax operation
589ec6347bbSDan Williams  * (dax_copy_to_iter()) for dax reads (bypass page-cache and the
590ec6347bbSDan Williams  * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes
591ec6347bbSDan Williams  * successfully copied.
592bf3eeb9bSDan Williams  *
593ec6347bbSDan Williams  * The main differences between this and typical _copy_to_iter().
594bf3eeb9bSDan Williams  *
595bf3eeb9bSDan Williams  * * Typical tail/residue handling after a fault retries the copy
596bf3eeb9bSDan Williams  *   byte-by-byte until the fault happens again. Re-triggering machine
597bf3eeb9bSDan Williams  *   checks is potentially fatal so the implementation uses source
598bf3eeb9bSDan Williams  *   alignment and poison alignment assumptions to avoid re-triggering
599bf3eeb9bSDan Williams  *   hardware exceptions.
600bf3eeb9bSDan Williams  *
601bf3eeb9bSDan Williams  * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies.
602bf3eeb9bSDan Williams  *   Compare to copy_to_iter() where only ITER_IOVEC attempts might return
603bf3eeb9bSDan Williams  *   a short copy.
60444e55997SRandy Dunlap  *
60544e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
606bf3eeb9bSDan Williams  */
607ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
6088780356eSDan Williams {
60900e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
610ec6347bbSDan Williams 		return copy_mc_pipe_to_iter(addr, bytes, i);
611fcb14cb1SAl Viro 	if (user_backed_iter(i))
6128780356eSDan Williams 		might_fault();
6137baa5099SAl Viro 	__iterate_and_advance(i, bytes, base, len, off,
6147baa5099SAl Viro 		copyout_mc(base, addr + off, len),
6157baa5099SAl Viro 		copy_mc_to_kernel(base, addr + off, len)
6168780356eSDan Williams 	)
6178780356eSDan Williams 
6188780356eSDan Williams 	return bytes;
6198780356eSDan Williams }
620ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
621ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */
6228780356eSDan Williams 
623aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
624d879cb83SAl Viro {
62500e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
626241699cdSAl Viro 		WARN_ON(1);
627241699cdSAl Viro 		return 0;
628241699cdSAl Viro 	}
629fcb14cb1SAl Viro 	if (user_backed_iter(i))
63009fc68dcSAl Viro 		might_fault();
6317baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6327baa5099SAl Viro 		copyin(addr + off, base, len),
6337baa5099SAl Viro 		memcpy(addr + off, base, len)
634d879cb83SAl Viro 	)
635d879cb83SAl Viro 
636d879cb83SAl Viro 	return bytes;
637d879cb83SAl Viro }
638aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter);
639d879cb83SAl Viro 
640aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
641d879cb83SAl Viro {
64200e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
643241699cdSAl Viro 		WARN_ON(1);
644241699cdSAl Viro 		return 0;
645241699cdSAl Viro 	}
6467baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6477baa5099SAl Viro 		__copy_from_user_inatomic_nocache(addr + off, base, len),
6487baa5099SAl Viro 		memcpy(addr + off, base, len)
649d879cb83SAl Viro 	)
650d879cb83SAl Viro 
651d879cb83SAl Viro 	return bytes;
652d879cb83SAl Viro }
653aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache);
654d879cb83SAl Viro 
6550aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
656abd08d7dSDan Williams /**
657abd08d7dSDan Williams  * _copy_from_iter_flushcache - write destination through cpu cache
658abd08d7dSDan Williams  * @addr: destination kernel address
659abd08d7dSDan Williams  * @bytes: total transfer length
66044e55997SRandy Dunlap  * @i: source iterator
661abd08d7dSDan Williams  *
662abd08d7dSDan Williams  * The pmem driver arranges for filesystem-dax to use this facility via
663abd08d7dSDan Williams  * dax_copy_from_iter() for ensuring that writes to persistent memory
664abd08d7dSDan Williams  * are flushed through the CPU cache. It is differentiated from
665abd08d7dSDan Williams  * _copy_from_iter_nocache() in that guarantees all data is flushed for
666abd08d7dSDan Williams  * all iterator types. The _copy_from_iter_nocache() only attempts to
667abd08d7dSDan Williams  * bypass the cache for the ITER_IOVEC case, and on some archs may use
668abd08d7dSDan Williams  * instructions that strand dirty-data in the cache.
66944e55997SRandy Dunlap  *
67044e55997SRandy Dunlap  * Return: number of bytes copied (may be %0)
671abd08d7dSDan Williams  */
6726a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
6730aed55afSDan Williams {
67400e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
6750aed55afSDan Williams 		WARN_ON(1);
6760aed55afSDan Williams 		return 0;
6770aed55afSDan Williams 	}
6787baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
6797baa5099SAl Viro 		__copy_from_user_flushcache(addr + off, base, len),
6807baa5099SAl Viro 		memcpy_flushcache(addr + off, base, len)
6810aed55afSDan Williams 	)
6820aed55afSDan Williams 
6830aed55afSDan Williams 	return bytes;
6840aed55afSDan Williams }
6856a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
6860aed55afSDan Williams #endif
6870aed55afSDan Williams 
68872e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
68972e809edSAl Viro {
6906daef95bSEric Dumazet 	struct page *head;
6916daef95bSEric Dumazet 	size_t v = n + offset;
6926daef95bSEric Dumazet 
6936daef95bSEric Dumazet 	/*
6946daef95bSEric Dumazet 	 * The general case needs to access the page order in order
6956daef95bSEric Dumazet 	 * to compute the page size.
6966daef95bSEric Dumazet 	 * However, we mostly deal with order-0 pages and thus can
6976daef95bSEric Dumazet 	 * avoid a possible cache line miss for requests that fit all
6986daef95bSEric Dumazet 	 * page orders.
6996daef95bSEric Dumazet 	 */
7006daef95bSEric Dumazet 	if (n <= v && v <= PAGE_SIZE)
7016daef95bSEric Dumazet 		return true;
7026daef95bSEric Dumazet 
7036daef95bSEric Dumazet 	head = compound_head(page);
7046daef95bSEric Dumazet 	v += (page - head) << PAGE_SHIFT;
705a90bcb86SPetar Penkov 
706a50b854eSMatthew Wilcox (Oracle) 	if (likely(n <= v && v <= (page_size(head))))
70772e809edSAl Viro 		return true;
70872e809edSAl Viro 	WARN_ON(1);
70972e809edSAl Viro 	return false;
71072e809edSAl Viro }
711cbbd26b8SAl Viro 
71208aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
71308aa6479SAl Viro 			 struct iov_iter *i)
71408aa6479SAl Viro {
71508aa6479SAl Viro 	size_t res = 0;
71608aa6479SAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes)))
71708aa6479SAl Viro 		return 0;
718f0f6b614SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
719f0f6b614SAl Viro 		return copy_page_to_iter_pipe(page, offset, bytes, i);
72008aa6479SAl Viro 	page += offset / PAGE_SIZE; // first subpage
72108aa6479SAl Viro 	offset %= PAGE_SIZE;
72208aa6479SAl Viro 	while (1) {
723f0f6b614SAl Viro 		void *kaddr = kmap_local_page(page);
724f0f6b614SAl Viro 		size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
725f0f6b614SAl Viro 		n = _copy_to_iter(kaddr + offset, n, i);
726f0f6b614SAl Viro 		kunmap_local(kaddr);
72708aa6479SAl Viro 		res += n;
72808aa6479SAl Viro 		bytes -= n;
72908aa6479SAl Viro 		if (!bytes || !n)
73008aa6479SAl Viro 			break;
73108aa6479SAl Viro 		offset += n;
73208aa6479SAl Viro 		if (offset == PAGE_SIZE) {
73308aa6479SAl Viro 			page++;
73408aa6479SAl Viro 			offset = 0;
73508aa6479SAl Viro 		}
73608aa6479SAl Viro 	}
73708aa6479SAl Viro 	return res;
73808aa6479SAl Viro }
739d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter);
740d879cb83SAl Viro 
741d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
742d879cb83SAl Viro 			 struct iov_iter *i)
743d879cb83SAl Viro {
744c03f05f1SAl Viro 	size_t res = 0;
745c03f05f1SAl Viro 	if (!page_copy_sane(page, offset, bytes))
74628f38db7SAl Viro 		return 0;
747c03f05f1SAl Viro 	page += offset / PAGE_SIZE; // first subpage
748c03f05f1SAl Viro 	offset %= PAGE_SIZE;
749c03f05f1SAl Viro 	while (1) {
750c03f05f1SAl Viro 		void *kaddr = kmap_local_page(page);
751c03f05f1SAl Viro 		size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
752c03f05f1SAl Viro 		n = _copy_from_iter(kaddr + offset, n, i);
753c03f05f1SAl Viro 		kunmap_local(kaddr);
754c03f05f1SAl Viro 		res += n;
755c03f05f1SAl Viro 		bytes -= n;
756c03f05f1SAl Viro 		if (!bytes || !n)
757c03f05f1SAl Viro 			break;
758c03f05f1SAl Viro 		offset += n;
759c03f05f1SAl Viro 		if (offset == PAGE_SIZE) {
760c03f05f1SAl Viro 			page++;
761c03f05f1SAl Viro 			offset = 0;
762c03f05f1SAl Viro 		}
763c03f05f1SAl Viro 	}
764c03f05f1SAl Viro 	return res;
765d879cb83SAl Viro }
766d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter);
767d879cb83SAl Viro 
768241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i)
769241699cdSAl Viro {
7708fad7767SAl Viro 	unsigned int chunk, off;
7718fad7767SAl Viro 
7728fad7767SAl Viro 	if (unlikely(bytes > i->count))
7738fad7767SAl Viro 		bytes = i->count;
7748fad7767SAl Viro 	if (unlikely(!bytes))
7758fad7767SAl Viro 		return 0;
776241699cdSAl Viro 
777241699cdSAl Viro 	if (!sanity(i))
778241699cdSAl Viro 		return 0;
779241699cdSAl Viro 
7808fad7767SAl Viro 	for (size_t n = bytes; n; n -= chunk) {
7818fad7767SAl Viro 		struct page *page = append_pipe(i, n, &off);
7828fad7767SAl Viro 		char *p;
783241699cdSAl Viro 
7848fad7767SAl Viro 		if (!page)
7858fad7767SAl Viro 			return bytes - n;
7868fad7767SAl Viro 		chunk = min_t(size_t, n, PAGE_SIZE - off);
7878fad7767SAl Viro 		p = kmap_local_page(page);
788893839fdSAl Viro 		memset(p + off, 0, chunk);
789893839fdSAl Viro 		kunmap_local(p);
7908fad7767SAl Viro 	}
791241699cdSAl Viro 	return bytes;
792241699cdSAl Viro }
793241699cdSAl Viro 
794d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
795d879cb83SAl Viro {
79600e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i)))
797241699cdSAl Viro 		return pipe_zero(bytes, i);
7987baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, count,
7997baa5099SAl Viro 		clear_user(base, len),
8007baa5099SAl Viro 		memset(base, 0, len)
801d879cb83SAl Viro 	)
802d879cb83SAl Viro 
803d879cb83SAl Viro 	return bytes;
804d879cb83SAl Viro }
805d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero);
806d879cb83SAl Viro 
807f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
808f0b65f39SAl Viro 				  struct iov_iter *i)
809d879cb83SAl Viro {
810d879cb83SAl Viro 	char *kaddr = kmap_atomic(page), *p = kaddr + offset;
81172e809edSAl Viro 	if (unlikely(!page_copy_sane(page, offset, bytes))) {
81272e809edSAl Viro 		kunmap_atomic(kaddr);
81372e809edSAl Viro 		return 0;
81472e809edSAl Viro 	}
8159ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
816241699cdSAl Viro 		kunmap_atomic(kaddr);
817241699cdSAl Viro 		WARN_ON(1);
818241699cdSAl Viro 		return 0;
819241699cdSAl Viro 	}
8207baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off,
8217baa5099SAl Viro 		copyin(p + off, base, len),
8227baa5099SAl Viro 		memcpy(p + off, base, len)
823d879cb83SAl Viro 	)
824d879cb83SAl Viro 	kunmap_atomic(kaddr);
825d879cb83SAl Viro 	return bytes;
826d879cb83SAl Viro }
827f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic);
828d879cb83SAl Viro 
829b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size)
830b9dc6f65SAl Viro {
831b9dc6f65SAl Viro 	struct pipe_inode_info *pipe = i->pipe;
83210f525a8SAl Viro 	int off = i->last_offset;
8338cefc107SDavid Howells 
8342c855de9SAl Viro 	if (!off && !size) {
8352c855de9SAl Viro 		pipe_discard_from(pipe, i->start_head); // discard everything
8362c855de9SAl Viro 		return;
837b9dc6f65SAl Viro 	}
838b9dc6f65SAl Viro 	i->count -= size;
8392c855de9SAl Viro 	while (1) {
8402c855de9SAl Viro 		struct pipe_buffer *buf = pipe_buf(pipe, i->head);
8412c855de9SAl Viro 		if (off) /* make it relative to the beginning of buffer */
84210f525a8SAl Viro 			size += abs(off) - buf->offset;
8432c855de9SAl Viro 		if (size <= buf->len) {
8442c855de9SAl Viro 			buf->len = size;
84510f525a8SAl Viro 			i->last_offset = last_offset(buf);
8462c855de9SAl Viro 			break;
8472c855de9SAl Viro 		}
8482c855de9SAl Viro 		size -= buf->len;
8492c855de9SAl Viro 		i->head++;
8502c855de9SAl Viro 		off = 0;
8512c855de9SAl Viro 	}
8522c855de9SAl Viro 	pipe_discard_from(pipe, i->head + 1); // discard everything past this one
853241699cdSAl Viro }
854241699cdSAl Viro 
85554c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size)
85654c8195bSPavel Begunkov {
85718fa9af7SAl Viro 	const struct bio_vec *bvec, *end;
85854c8195bSPavel Begunkov 
85918fa9af7SAl Viro 	if (!i->count)
86018fa9af7SAl Viro 		return;
86118fa9af7SAl Viro 	i->count -= size;
86254c8195bSPavel Begunkov 
86318fa9af7SAl Viro 	size += i->iov_offset;
86418fa9af7SAl Viro 
86518fa9af7SAl Viro 	for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) {
86618fa9af7SAl Viro 		if (likely(size < bvec->bv_len))
86718fa9af7SAl Viro 			break;
86818fa9af7SAl Viro 		size -= bvec->bv_len;
86918fa9af7SAl Viro 	}
87018fa9af7SAl Viro 	i->iov_offset = size;
87118fa9af7SAl Viro 	i->nr_segs -= bvec - i->bvec;
87218fa9af7SAl Viro 	i->bvec = bvec;
87354c8195bSPavel Begunkov }
87454c8195bSPavel Begunkov 
875185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size)
876185ac4d4SAl Viro {
877185ac4d4SAl Viro 	const struct iovec *iov, *end;
878185ac4d4SAl Viro 
879185ac4d4SAl Viro 	if (!i->count)
880185ac4d4SAl Viro 		return;
881185ac4d4SAl Viro 	i->count -= size;
882185ac4d4SAl Viro 
883185ac4d4SAl Viro 	size += i->iov_offset; // from beginning of current segment
884185ac4d4SAl Viro 	for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) {
885185ac4d4SAl Viro 		if (likely(size < iov->iov_len))
886185ac4d4SAl Viro 			break;
887185ac4d4SAl Viro 		size -= iov->iov_len;
888185ac4d4SAl Viro 	}
889185ac4d4SAl Viro 	i->iov_offset = size;
890185ac4d4SAl Viro 	i->nr_segs -= iov - i->iov;
891185ac4d4SAl Viro 	i->iov = iov;
892185ac4d4SAl Viro }
893185ac4d4SAl Viro 
894d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size)
895d879cb83SAl Viro {
8963b3fc051SAl Viro 	if (unlikely(i->count < size))
8973b3fc051SAl Viro 		size = i->count;
898fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) {
899fcb14cb1SAl Viro 		i->iov_offset += size;
900fcb14cb1SAl Viro 		i->count -= size;
901fcb14cb1SAl Viro 	} else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) {
902185ac4d4SAl Viro 		/* iovec and kvec have identical layouts */
903185ac4d4SAl Viro 		iov_iter_iovec_advance(i, size);
904185ac4d4SAl Viro 	} else if (iov_iter_is_bvec(i)) {
905185ac4d4SAl Viro 		iov_iter_bvec_advance(i, size);
906185ac4d4SAl Viro 	} else if (iov_iter_is_pipe(i)) {
907241699cdSAl Viro 		pipe_advance(i, size);
908185ac4d4SAl Viro 	} else if (iov_iter_is_discard(i)) {
909185ac4d4SAl Viro 		i->count -= size;
9107ff50620SDavid Howells 	}
911d879cb83SAl Viro }
912d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance);
913d879cb83SAl Viro 
91427c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll)
91527c0e374SAl Viro {
91627c0e374SAl Viro 	if (!unroll)
91727c0e374SAl Viro 		return;
9185b47d59aSAl Viro 	if (WARN_ON(unroll > MAX_RW_COUNT))
9195b47d59aSAl Viro 		return;
92027c0e374SAl Viro 	i->count += unroll;
92100e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(i))) {
92227c0e374SAl Viro 		struct pipe_inode_info *pipe = i->pipe;
92392acdc4fSAl Viro 		unsigned int head = pipe->head;
92492acdc4fSAl Viro 
92592acdc4fSAl Viro 		while (head > i->start_head) {
92692acdc4fSAl Viro 			struct pipe_buffer *b = pipe_buf(pipe, --head);
92792acdc4fSAl Viro 			if (unroll < b->len) {
92892acdc4fSAl Viro 				b->len -= unroll;
92910f525a8SAl Viro 				i->last_offset = last_offset(b);
93092acdc4fSAl Viro 				i->head = head;
93192acdc4fSAl Viro 				return;
93227c0e374SAl Viro 			}
93392acdc4fSAl Viro 			unroll -= b->len;
93492acdc4fSAl Viro 			pipe_buf_release(pipe, b);
93592acdc4fSAl Viro 			pipe->head--;
93627c0e374SAl Viro 		}
93710f525a8SAl Viro 		i->last_offset = 0;
93892acdc4fSAl Viro 		i->head = head;
93927c0e374SAl Viro 		return;
94027c0e374SAl Viro 	}
9419ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_discard(i)))
9429ea9ce04SDavid Howells 		return;
94327c0e374SAl Viro 	if (unroll <= i->iov_offset) {
94427c0e374SAl Viro 		i->iov_offset -= unroll;
94527c0e374SAl Viro 		return;
94627c0e374SAl Viro 	}
94727c0e374SAl Viro 	unroll -= i->iov_offset;
948fcb14cb1SAl Viro 	if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) {
9497ff50620SDavid Howells 		BUG(); /* We should never go beyond the start of the specified
9507ff50620SDavid Howells 			* range since we might then be straying into pages that
9517ff50620SDavid Howells 			* aren't pinned.
9527ff50620SDavid Howells 			*/
9537ff50620SDavid Howells 	} else if (iov_iter_is_bvec(i)) {
95427c0e374SAl Viro 		const struct bio_vec *bvec = i->bvec;
95527c0e374SAl Viro 		while (1) {
95627c0e374SAl Viro 			size_t n = (--bvec)->bv_len;
95727c0e374SAl Viro 			i->nr_segs++;
95827c0e374SAl Viro 			if (unroll <= n) {
95927c0e374SAl Viro 				i->bvec = bvec;
96027c0e374SAl Viro 				i->iov_offset = n - unroll;
96127c0e374SAl Viro 				return;
96227c0e374SAl Viro 			}
96327c0e374SAl Viro 			unroll -= n;
96427c0e374SAl Viro 		}
96527c0e374SAl Viro 	} else { /* same logics for iovec and kvec */
96627c0e374SAl Viro 		const struct iovec *iov = i->iov;
96727c0e374SAl Viro 		while (1) {
96827c0e374SAl Viro 			size_t n = (--iov)->iov_len;
96927c0e374SAl Viro 			i->nr_segs++;
97027c0e374SAl Viro 			if (unroll <= n) {
97127c0e374SAl Viro 				i->iov = iov;
97227c0e374SAl Viro 				i->iov_offset = n - unroll;
97327c0e374SAl Viro 				return;
97427c0e374SAl Viro 			}
97527c0e374SAl Viro 			unroll -= n;
97627c0e374SAl Viro 		}
97727c0e374SAl Viro 	}
97827c0e374SAl Viro }
97927c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert);
98027c0e374SAl Viro 
981d879cb83SAl Viro /*
982d879cb83SAl Viro  * Return the count of just the current iov_iter segment.
983d879cb83SAl Viro  */
984d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i)
985d879cb83SAl Viro {
98628f38db7SAl Viro 	if (i->nr_segs > 1) {
98728f38db7SAl Viro 		if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
98828f38db7SAl Viro 			return min(i->count, i->iov->iov_len - i->iov_offset);
9897ff50620SDavid Howells 		if (iov_iter_is_bvec(i))
990d879cb83SAl Viro 			return min(i->count, i->bvec->bv_len - i->iov_offset);
99128f38db7SAl Viro 	}
99228f38db7SAl Viro 	return i->count;
993d879cb83SAl Viro }
994d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count);
995d879cb83SAl Viro 
996aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
997d879cb83SAl Viro 			const struct kvec *kvec, unsigned long nr_segs,
998d879cb83SAl Viro 			size_t count)
999d879cb83SAl Viro {
1000aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
10018cd54c1cSAl Viro 	*i = (struct iov_iter){
10028cd54c1cSAl Viro 		.iter_type = ITER_KVEC,
10038cd54c1cSAl Viro 		.data_source = direction,
10048cd54c1cSAl Viro 		.kvec = kvec,
10058cd54c1cSAl Viro 		.nr_segs = nr_segs,
10068cd54c1cSAl Viro 		.iov_offset = 0,
10078cd54c1cSAl Viro 		.count = count
10088cd54c1cSAl Viro 	};
1009d879cb83SAl Viro }
1010d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec);
1011d879cb83SAl Viro 
1012aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
1013d879cb83SAl Viro 			const struct bio_vec *bvec, unsigned long nr_segs,
1014d879cb83SAl Viro 			size_t count)
1015d879cb83SAl Viro {
1016aa563d7bSDavid Howells 	WARN_ON(direction & ~(READ | WRITE));
10178cd54c1cSAl Viro 	*i = (struct iov_iter){
10188cd54c1cSAl Viro 		.iter_type = ITER_BVEC,
10198cd54c1cSAl Viro 		.data_source = direction,
10208cd54c1cSAl Viro 		.bvec = bvec,
10218cd54c1cSAl Viro 		.nr_segs = nr_segs,
10228cd54c1cSAl Viro 		.iov_offset = 0,
10238cd54c1cSAl Viro 		.count = count
10248cd54c1cSAl Viro 	};
1025d879cb83SAl Viro }
1026d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec);
1027d879cb83SAl Viro 
1028aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
1029241699cdSAl Viro 			struct pipe_inode_info *pipe,
1030241699cdSAl Viro 			size_t count)
1031241699cdSAl Viro {
1032aa563d7bSDavid Howells 	BUG_ON(direction != READ);
10338cefc107SDavid Howells 	WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size));
10348cd54c1cSAl Viro 	*i = (struct iov_iter){
10358cd54c1cSAl Viro 		.iter_type = ITER_PIPE,
10368cd54c1cSAl Viro 		.data_source = false,
10378cd54c1cSAl Viro 		.pipe = pipe,
10388cd54c1cSAl Viro 		.head = pipe->head,
10398cd54c1cSAl Viro 		.start_head = pipe->head,
104010f525a8SAl Viro 		.last_offset = 0,
10418cd54c1cSAl Viro 		.count = count
10428cd54c1cSAl Viro 	};
1043241699cdSAl Viro }
1044241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe);
1045241699cdSAl Viro 
10469ea9ce04SDavid Howells /**
10477ff50620SDavid Howells  * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray
10487ff50620SDavid Howells  * @i: The iterator to initialise.
10497ff50620SDavid Howells  * @direction: The direction of the transfer.
10507ff50620SDavid Howells  * @xarray: The xarray to access.
10517ff50620SDavid Howells  * @start: The start file position.
10527ff50620SDavid Howells  * @count: The size of the I/O buffer in bytes.
10537ff50620SDavid Howells  *
10547ff50620SDavid Howells  * Set up an I/O iterator to either draw data out of the pages attached to an
10557ff50620SDavid Howells  * inode or to inject data into those pages.  The pages *must* be prevented
10567ff50620SDavid Howells  * from evaporation, either by taking a ref on them or locking them by the
10577ff50620SDavid Howells  * caller.
10587ff50620SDavid Howells  */
10597ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
10607ff50620SDavid Howells 		     struct xarray *xarray, loff_t start, size_t count)
10617ff50620SDavid Howells {
10627ff50620SDavid Howells 	BUG_ON(direction & ~1);
10638cd54c1cSAl Viro 	*i = (struct iov_iter) {
10648cd54c1cSAl Viro 		.iter_type = ITER_XARRAY,
10658cd54c1cSAl Viro 		.data_source = direction,
10668cd54c1cSAl Viro 		.xarray = xarray,
10678cd54c1cSAl Viro 		.xarray_start = start,
10688cd54c1cSAl Viro 		.count = count,
10698cd54c1cSAl Viro 		.iov_offset = 0
10708cd54c1cSAl Viro 	};
10717ff50620SDavid Howells }
10727ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray);
10737ff50620SDavid Howells 
10747ff50620SDavid Howells /**
10759ea9ce04SDavid Howells  * iov_iter_discard - Initialise an I/O iterator that discards data
10769ea9ce04SDavid Howells  * @i: The iterator to initialise.
10779ea9ce04SDavid Howells  * @direction: The direction of the transfer.
10789ea9ce04SDavid Howells  * @count: The size of the I/O buffer in bytes.
10799ea9ce04SDavid Howells  *
10809ea9ce04SDavid Howells  * Set up an I/O iterator that just discards everything that's written to it.
10819ea9ce04SDavid Howells  * It's only available as a READ iterator.
10829ea9ce04SDavid Howells  */
10839ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
10849ea9ce04SDavid Howells {
10859ea9ce04SDavid Howells 	BUG_ON(direction != READ);
10868cd54c1cSAl Viro 	*i = (struct iov_iter){
10878cd54c1cSAl Viro 		.iter_type = ITER_DISCARD,
10888cd54c1cSAl Viro 		.data_source = false,
10898cd54c1cSAl Viro 		.count = count,
10908cd54c1cSAl Viro 		.iov_offset = 0
10918cd54c1cSAl Viro 	};
10929ea9ce04SDavid Howells }
10939ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard);
10949ea9ce04SDavid Howells 
1095cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
1096cfa320f7SKeith Busch 				   unsigned len_mask)
1097cfa320f7SKeith Busch {
1098cfa320f7SKeith Busch 	size_t size = i->count;
1099cfa320f7SKeith Busch 	size_t skip = i->iov_offset;
1100cfa320f7SKeith Busch 	unsigned k;
1101cfa320f7SKeith Busch 
1102cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1103cfa320f7SKeith Busch 		size_t len = i->iov[k].iov_len - skip;
1104cfa320f7SKeith Busch 
1105cfa320f7SKeith Busch 		if (len > size)
1106cfa320f7SKeith Busch 			len = size;
1107cfa320f7SKeith Busch 		if (len & len_mask)
1108cfa320f7SKeith Busch 			return false;
1109cfa320f7SKeith Busch 		if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
1110cfa320f7SKeith Busch 			return false;
1111cfa320f7SKeith Busch 
1112cfa320f7SKeith Busch 		size -= len;
1113cfa320f7SKeith Busch 		if (!size)
1114cfa320f7SKeith Busch 			break;
1115cfa320f7SKeith Busch 	}
1116cfa320f7SKeith Busch 	return true;
1117cfa320f7SKeith Busch }
1118cfa320f7SKeith Busch 
1119cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
1120cfa320f7SKeith Busch 				  unsigned len_mask)
1121cfa320f7SKeith Busch {
1122cfa320f7SKeith Busch 	size_t size = i->count;
1123cfa320f7SKeith Busch 	unsigned skip = i->iov_offset;
1124cfa320f7SKeith Busch 	unsigned k;
1125cfa320f7SKeith Busch 
1126cfa320f7SKeith Busch 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
1127cfa320f7SKeith Busch 		size_t len = i->bvec[k].bv_len - skip;
1128cfa320f7SKeith Busch 
1129cfa320f7SKeith Busch 		if (len > size)
1130cfa320f7SKeith Busch 			len = size;
1131cfa320f7SKeith Busch 		if (len & len_mask)
1132cfa320f7SKeith Busch 			return false;
1133cfa320f7SKeith Busch 		if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
1134cfa320f7SKeith Busch 			return false;
1135cfa320f7SKeith Busch 
1136cfa320f7SKeith Busch 		size -= len;
1137cfa320f7SKeith Busch 		if (!size)
1138cfa320f7SKeith Busch 			break;
1139cfa320f7SKeith Busch 	}
1140cfa320f7SKeith Busch 	return true;
1141cfa320f7SKeith Busch }
1142cfa320f7SKeith Busch 
1143cfa320f7SKeith Busch /**
1144cfa320f7SKeith Busch  * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
1145cfa320f7SKeith Busch  * 	are aligned to the parameters.
1146cfa320f7SKeith Busch  *
1147cfa320f7SKeith Busch  * @i: &struct iov_iter to restore
1148cfa320f7SKeith Busch  * @addr_mask: bit mask to check against the iov element's addresses
1149cfa320f7SKeith Busch  * @len_mask: bit mask to check against the iov element's lengths
1150cfa320f7SKeith Busch  *
1151cfa320f7SKeith Busch  * Return: false if any addresses or lengths intersect with the provided masks
1152cfa320f7SKeith Busch  */
1153cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
1154cfa320f7SKeith Busch 			 unsigned len_mask)
1155cfa320f7SKeith Busch {
1156fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1157fcb14cb1SAl Viro 		if (i->count & len_mask)
1158fcb14cb1SAl Viro 			return false;
1159fcb14cb1SAl Viro 		if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask)
1160fcb14cb1SAl Viro 			return false;
1161fcb14cb1SAl Viro 		return true;
1162fcb14cb1SAl Viro 	}
1163fcb14cb1SAl Viro 
1164cfa320f7SKeith Busch 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
1165cfa320f7SKeith Busch 		return iov_iter_aligned_iovec(i, addr_mask, len_mask);
1166cfa320f7SKeith Busch 
1167cfa320f7SKeith Busch 	if (iov_iter_is_bvec(i))
1168cfa320f7SKeith Busch 		return iov_iter_aligned_bvec(i, addr_mask, len_mask);
1169cfa320f7SKeith Busch 
1170cfa320f7SKeith Busch 	if (iov_iter_is_pipe(i)) {
1171cfa320f7SKeith Busch 		size_t size = i->count;
1172cfa320f7SKeith Busch 
1173cfa320f7SKeith Busch 		if (size & len_mask)
1174cfa320f7SKeith Busch 			return false;
117510f525a8SAl Viro 		if (size && i->last_offset > 0) {
117610f525a8SAl Viro 			if (i->last_offset & addr_mask)
1177cfa320f7SKeith Busch 				return false;
1178cfa320f7SKeith Busch 		}
1179cfa320f7SKeith Busch 
1180cfa320f7SKeith Busch 		return true;
1181cfa320f7SKeith Busch 	}
1182cfa320f7SKeith Busch 
1183cfa320f7SKeith Busch 	if (iov_iter_is_xarray(i)) {
1184cfa320f7SKeith Busch 		if (i->count & len_mask)
1185cfa320f7SKeith Busch 			return false;
1186cfa320f7SKeith Busch 		if ((i->xarray_start + i->iov_offset) & addr_mask)
1187cfa320f7SKeith Busch 			return false;
1188cfa320f7SKeith Busch 	}
1189cfa320f7SKeith Busch 
1190cfa320f7SKeith Busch 	return true;
1191cfa320f7SKeith Busch }
1192cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
1193cfa320f7SKeith Busch 
11949221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
1195d879cb83SAl Viro {
1196d879cb83SAl Viro 	unsigned long res = 0;
1197d879cb83SAl Viro 	size_t size = i->count;
11989221d2e3SAl Viro 	size_t skip = i->iov_offset;
11999221d2e3SAl Viro 	unsigned k;
1200d879cb83SAl Viro 
12019221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
12029221d2e3SAl Viro 		size_t len = i->iov[k].iov_len - skip;
12039221d2e3SAl Viro 		if (len) {
12049221d2e3SAl Viro 			res |= (unsigned long)i->iov[k].iov_base + skip;
12059221d2e3SAl Viro 			if (len > size)
12069221d2e3SAl Viro 				len = size;
12079221d2e3SAl Viro 			res |= len;
12089221d2e3SAl Viro 			size -= len;
12099221d2e3SAl Viro 			if (!size)
12109221d2e3SAl Viro 				break;
12119221d2e3SAl Viro 		}
12129221d2e3SAl Viro 	}
12139221d2e3SAl Viro 	return res;
12149221d2e3SAl Viro }
12159221d2e3SAl Viro 
12169221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
12179221d2e3SAl Viro {
12189221d2e3SAl Viro 	unsigned res = 0;
12199221d2e3SAl Viro 	size_t size = i->count;
12209221d2e3SAl Viro 	unsigned skip = i->iov_offset;
12219221d2e3SAl Viro 	unsigned k;
12229221d2e3SAl Viro 
12239221d2e3SAl Viro 	for (k = 0; k < i->nr_segs; k++, skip = 0) {
12249221d2e3SAl Viro 		size_t len = i->bvec[k].bv_len - skip;
12259221d2e3SAl Viro 		res |= (unsigned long)i->bvec[k].bv_offset + skip;
12269221d2e3SAl Viro 		if (len > size)
12279221d2e3SAl Viro 			len = size;
12289221d2e3SAl Viro 		res |= len;
12299221d2e3SAl Viro 		size -= len;
12309221d2e3SAl Viro 		if (!size)
12319221d2e3SAl Viro 			break;
12329221d2e3SAl Viro 	}
12339221d2e3SAl Viro 	return res;
12349221d2e3SAl Viro }
12359221d2e3SAl Viro 
12369221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i)
12379221d2e3SAl Viro {
1238fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1239fcb14cb1SAl Viro 		size_t size = i->count;
1240fcb14cb1SAl Viro 		if (size)
1241fcb14cb1SAl Viro 			return ((unsigned long)i->ubuf + i->iov_offset) | size;
1242fcb14cb1SAl Viro 		return 0;
1243fcb14cb1SAl Viro 	}
1244fcb14cb1SAl Viro 
12459221d2e3SAl Viro 	/* iovec and kvec have identical layouts */
12469221d2e3SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
12479221d2e3SAl Viro 		return iov_iter_alignment_iovec(i);
12489221d2e3SAl Viro 
12499221d2e3SAl Viro 	if (iov_iter_is_bvec(i))
12509221d2e3SAl Viro 		return iov_iter_alignment_bvec(i);
12519221d2e3SAl Viro 
12529221d2e3SAl Viro 	if (iov_iter_is_pipe(i)) {
12539221d2e3SAl Viro 		size_t size = i->count;
1254e0ff126eSJan Kara 
125510f525a8SAl Viro 		if (size && i->last_offset > 0)
125610f525a8SAl Viro 			return size | i->last_offset;
1257241699cdSAl Viro 		return size;
1258241699cdSAl Viro 	}
12599221d2e3SAl Viro 
12609221d2e3SAl Viro 	if (iov_iter_is_xarray(i))
12613d14ec1fSDavid Howells 		return (i->xarray_start + i->iov_offset) | i->count;
12629221d2e3SAl Viro 
12639221d2e3SAl Viro 	return 0;
1264d879cb83SAl Viro }
1265d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment);
1266d879cb83SAl Viro 
1267357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
1268357f435dSAl Viro {
1269357f435dSAl Viro 	unsigned long res = 0;
1270610c7a71SAl Viro 	unsigned long v = 0;
1271357f435dSAl Viro 	size_t size = i->count;
1272610c7a71SAl Viro 	unsigned k;
1273357f435dSAl Viro 
1274fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1275fcb14cb1SAl Viro 		return 0;
1276fcb14cb1SAl Viro 
1277610c7a71SAl Viro 	if (WARN_ON(!iter_is_iovec(i)))
1278241699cdSAl Viro 		return ~0U;
1279241699cdSAl Viro 
1280610c7a71SAl Viro 	for (k = 0; k < i->nr_segs; k++) {
1281610c7a71SAl Viro 		if (i->iov[k].iov_len) {
1282610c7a71SAl Viro 			unsigned long base = (unsigned long)i->iov[k].iov_base;
1283610c7a71SAl Viro 			if (v) // if not the first one
1284610c7a71SAl Viro 				res |= base | v; // this start | previous end
1285610c7a71SAl Viro 			v = base + i->iov[k].iov_len;
1286610c7a71SAl Viro 			if (size <= i->iov[k].iov_len)
1287610c7a71SAl Viro 				break;
1288610c7a71SAl Viro 			size -= i->iov[k].iov_len;
1289610c7a71SAl Viro 		}
1290610c7a71SAl Viro 	}
1291357f435dSAl Viro 	return res;
1292357f435dSAl Viro }
1293357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment);
1294357f435dSAl Viro 
12953cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size,
12963cf42da3SAl Viro 			    size_t start, unsigned int maxpages)
1297acbdeb83SAl Viro {
12983cf42da3SAl Viro 	unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE);
12993cf42da3SAl Viro 
13003cf42da3SAl Viro 	if (count > maxpages)
13013cf42da3SAl Viro 		count = maxpages;
13023cf42da3SAl Viro 	WARN_ON(!count);	// caller should've prevented that
13033cf42da3SAl Viro 	if (!*res) {
13043cf42da3SAl Viro 		*res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
13053cf42da3SAl Viro 		if (!*res)
13063cf42da3SAl Viro 			return 0;
13073cf42da3SAl Viro 	}
13083cf42da3SAl Viro 	return count;
1309acbdeb83SAl Viro }
1310acbdeb83SAl Viro 
131185200084SAl Viro static ssize_t pipe_get_pages(struct iov_iter *i,
131285200084SAl Viro 		   struct page ***pages, size_t maxsize, unsigned maxpages,
131385200084SAl Viro 		   size_t *start)
1314241699cdSAl Viro {
1315746de1f8SAl Viro 	unsigned int npages, count, off, chunk;
131685200084SAl Viro 	struct page **p;
1317746de1f8SAl Viro 	size_t left;
1318241699cdSAl Viro 
131985200084SAl Viro 	if (!sanity(i))
132085200084SAl Viro 		return -EFAULT;
132185200084SAl Viro 
132285200084SAl Viro 	*start = off = pipe_npages(i, &npages);
13233cf42da3SAl Viro 	if (!npages)
13243cf42da3SAl Viro 		return -EFAULT;
13253cf42da3SAl Viro 	count = want_pages_array(pages, maxsize, off, min(npages, maxpages));
13263cf42da3SAl Viro 	if (!count)
132785200084SAl Viro 		return -ENOMEM;
13283cf42da3SAl Viro 	p = *pages;
1329746de1f8SAl Viro 	for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) {
1330746de1f8SAl Viro 		struct page *page = append_pipe(i, left, &off);
1331e3b42964SAl Viro 		if (!page)
1332e3b42964SAl Viro 			break;
1333746de1f8SAl Viro 		chunk = min_t(size_t, left, PAGE_SIZE - off);
133485200084SAl Viro 		get_page(*p++ = page);
1335e3b42964SAl Viro 	}
133685200084SAl Viro 	if (!npages)
1337241699cdSAl Viro 		return -EFAULT;
1338746de1f8SAl Viro 	return maxsize - left;
1339241699cdSAl Viro }
1340241699cdSAl Viro 
13417ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
13427ff50620SDavid Howells 					  pgoff_t index, unsigned int nr_pages)
13437ff50620SDavid Howells {
13447ff50620SDavid Howells 	XA_STATE(xas, xa, index);
13457ff50620SDavid Howells 	struct page *page;
13467ff50620SDavid Howells 	unsigned int ret = 0;
13477ff50620SDavid Howells 
13487ff50620SDavid Howells 	rcu_read_lock();
13497ff50620SDavid Howells 	for (page = xas_load(&xas); page; page = xas_next(&xas)) {
13507ff50620SDavid Howells 		if (xas_retry(&xas, page))
13517ff50620SDavid Howells 			continue;
13527ff50620SDavid Howells 
13537ff50620SDavid Howells 		/* Has the page moved or been split? */
13547ff50620SDavid Howells 		if (unlikely(page != xas_reload(&xas))) {
13557ff50620SDavid Howells 			xas_reset(&xas);
13567ff50620SDavid Howells 			continue;
13577ff50620SDavid Howells 		}
13587ff50620SDavid Howells 
13597ff50620SDavid Howells 		pages[ret] = find_subpage(page, xas.xa_index);
13607ff50620SDavid Howells 		get_page(pages[ret]);
13617ff50620SDavid Howells 		if (++ret == nr_pages)
13627ff50620SDavid Howells 			break;
13637ff50620SDavid Howells 	}
13647ff50620SDavid Howells 	rcu_read_unlock();
13657ff50620SDavid Howells 	return ret;
13667ff50620SDavid Howells }
13677ff50620SDavid Howells 
13687ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i,
136968fe506fSAl Viro 				     struct page ***pages, size_t maxsize,
13707ff50620SDavid Howells 				     unsigned maxpages, size_t *_start_offset)
13717ff50620SDavid Howells {
13723cf42da3SAl Viro 	unsigned nr, offset, count;
13733cf42da3SAl Viro 	pgoff_t index;
13747ff50620SDavid Howells 	loff_t pos;
13757ff50620SDavid Howells 
13767ff50620SDavid Howells 	pos = i->xarray_start + i->iov_offset;
13777ff50620SDavid Howells 	index = pos >> PAGE_SHIFT;
13787ff50620SDavid Howells 	offset = pos & ~PAGE_MASK;
13797ff50620SDavid Howells 	*_start_offset = offset;
13807ff50620SDavid Howells 
13813cf42da3SAl Viro 	count = want_pages_array(pages, maxsize, offset, maxpages);
13823cf42da3SAl Viro 	if (!count)
138368fe506fSAl Viro 		return -ENOMEM;
138468fe506fSAl Viro 	nr = iter_xarray_populate_pages(*pages, i->xarray, index, count);
13857ff50620SDavid Howells 	if (nr == 0)
13867ff50620SDavid Howells 		return 0;
13877ff50620SDavid Howells 
1388eba2d3d7SAl Viro 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
1389310d9d5aSAl Viro 	i->iov_offset += maxsize;
1390310d9d5aSAl Viro 	i->count -= maxsize;
1391eba2d3d7SAl Viro 	return maxsize;
13927ff50620SDavid Howells }
13937ff50620SDavid Howells 
1394fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
1395dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size)
13963d671ca6SAl Viro {
13973d671ca6SAl Viro 	size_t skip;
13983d671ca6SAl Viro 	long k;
13993d671ca6SAl Viro 
1400fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1401fcb14cb1SAl Viro 		return (unsigned long)i->ubuf + i->iov_offset;
1402fcb14cb1SAl Viro 
14033d671ca6SAl Viro 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
14043d671ca6SAl Viro 		size_t len = i->iov[k].iov_len - skip;
14053d671ca6SAl Viro 
14063d671ca6SAl Viro 		if (unlikely(!len))
14073d671ca6SAl Viro 			continue;
140859dbd7d0SAl Viro 		if (*size > len)
14093d671ca6SAl Viro 			*size = len;
1410dd45ab9dSAl Viro 		return (unsigned long)i->iov[k].iov_base + skip;
14113d671ca6SAl Viro 	}
14123d671ca6SAl Viro 	BUG(); // if it had been empty, we wouldn't get called
14133d671ca6SAl Viro }
14143d671ca6SAl Viro 
14153d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */
14163d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i,
141759dbd7d0SAl Viro 				       size_t *size, size_t *start)
14183d671ca6SAl Viro {
14193d671ca6SAl Viro 	struct page *page;
14203d671ca6SAl Viro 	size_t skip = i->iov_offset, len;
14213d671ca6SAl Viro 
14223d671ca6SAl Viro 	len = i->bvec->bv_len - skip;
142359dbd7d0SAl Viro 	if (*size > len)
142459dbd7d0SAl Viro 		*size = len;
14253d671ca6SAl Viro 	skip += i->bvec->bv_offset;
14263d671ca6SAl Viro 	page = i->bvec->bv_page + skip / PAGE_SIZE;
1427dda8e5d1SAl Viro 	*start = skip % PAGE_SIZE;
14283d671ca6SAl Viro 	return page;
14293d671ca6SAl Viro }
14303d671ca6SAl Viro 
143191329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
1432d879cb83SAl Viro 		   struct page ***pages, size_t maxsize,
1433451c0ba9SAl Viro 		   unsigned int maxpages, size_t *start)
1434d879cb83SAl Viro {
14353cf42da3SAl Viro 	unsigned int n;
1436d879cb83SAl Viro 
1437d879cb83SAl Viro 	if (maxsize > i->count)
1438d879cb83SAl Viro 		maxsize = i->count;
14393d671ca6SAl Viro 	if (!maxsize)
14403d671ca6SAl Viro 		return 0;
14417392ed17SAl Viro 	if (maxsize > MAX_RW_COUNT)
14427392ed17SAl Viro 		maxsize = MAX_RW_COUNT;
1443d879cb83SAl Viro 
1444fcb14cb1SAl Viro 	if (likely(user_backed_iter(i))) {
14453337ab08SAndreas Gruenbacher 		unsigned int gup_flags = 0;
14463d671ca6SAl Viro 		unsigned long addr;
14473cf42da3SAl Viro 		int res;
14489ea9ce04SDavid Howells 
14493337ab08SAndreas Gruenbacher 		if (iov_iter_rw(i) != WRITE)
14503337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_WRITE;
14513337ab08SAndreas Gruenbacher 		if (i->nofault)
14523337ab08SAndreas Gruenbacher 			gup_flags |= FOLL_NOFAULT;
14533337ab08SAndreas Gruenbacher 
1454dd45ab9dSAl Viro 		addr = first_iovec_segment(i, &maxsize);
1455dd45ab9dSAl Viro 		*start = addr % PAGE_SIZE;
1456dd45ab9dSAl Viro 		addr &= PAGE_MASK;
14573cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
14583cf42da3SAl Viro 		if (!n)
1459d879cb83SAl Viro 			return -ENOMEM;
1460451c0ba9SAl Viro 		res = get_user_pages_fast(addr, n, gup_flags, *pages);
146191329559SAl Viro 		if (unlikely(res <= 0))
1462d879cb83SAl Viro 			return res;
1463eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start);
1464eba2d3d7SAl Viro 		iov_iter_advance(i, maxsize);
1465eba2d3d7SAl Viro 		return maxsize;
14663d671ca6SAl Viro 	}
14673d671ca6SAl Viro 	if (iov_iter_is_bvec(i)) {
1468451c0ba9SAl Viro 		struct page **p;
14693d671ca6SAl Viro 		struct page *page;
14703d671ca6SAl Viro 
147159dbd7d0SAl Viro 		page = first_bvec_segment(i, &maxsize, start);
14723cf42da3SAl Viro 		n = want_pages_array(pages, maxsize, *start, maxpages);
14733cf42da3SAl Viro 		if (!n)
1474d879cb83SAl Viro 			return -ENOMEM;
14753cf42da3SAl Viro 		p = *pages;
1476dda8e5d1SAl Viro 		for (int k = 0; k < n; k++)
1477eba2d3d7SAl Viro 			get_page(p[k] = page + k);
1478eba2d3d7SAl Viro 		maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
1479310d9d5aSAl Viro 		i->count -= maxsize;
1480310d9d5aSAl Viro 		i->iov_offset += maxsize;
1481310d9d5aSAl Viro 		if (i->iov_offset == i->bvec->bv_len) {
1482310d9d5aSAl Viro 			i->iov_offset = 0;
1483310d9d5aSAl Viro 			i->bvec++;
1484310d9d5aSAl Viro 			i->nr_segs--;
1485310d9d5aSAl Viro 		}
1486eba2d3d7SAl Viro 		return maxsize;
14873d671ca6SAl Viro 	}
14883d671ca6SAl Viro 	if (iov_iter_is_pipe(i))
1489451c0ba9SAl Viro 		return pipe_get_pages(i, pages, maxsize, maxpages, start);
14903d671ca6SAl Viro 	if (iov_iter_is_xarray(i))
1491451c0ba9SAl Viro 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
1492d879cb83SAl Viro 	return -EFAULT;
1493d879cb83SAl Viro }
149491329559SAl Viro 
1495eba2d3d7SAl Viro ssize_t iov_iter_get_pages2(struct iov_iter *i,
1496451c0ba9SAl Viro 		   struct page **pages, size_t maxsize, unsigned maxpages,
1497451c0ba9SAl Viro 		   size_t *start)
1498451c0ba9SAl Viro {
1499451c0ba9SAl Viro 	if (!maxpages)
1500451c0ba9SAl Viro 		return 0;
1501451c0ba9SAl Viro 	BUG_ON(!pages);
1502451c0ba9SAl Viro 
1503451c0ba9SAl Viro 	return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, start);
1504451c0ba9SAl Viro }
1505eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2);
1506451c0ba9SAl Viro 
1507eba2d3d7SAl Viro ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i,
150891329559SAl Viro 		   struct page ***pages, size_t maxsize,
150991329559SAl Viro 		   size_t *start)
151091329559SAl Viro {
151191329559SAl Viro 	ssize_t len;
151291329559SAl Viro 
151391329559SAl Viro 	*pages = NULL;
151491329559SAl Viro 
1515451c0ba9SAl Viro 	len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start);
151691329559SAl Viro 	if (len <= 0) {
151791329559SAl Viro 		kvfree(*pages);
151891329559SAl Viro 		*pages = NULL;
151991329559SAl Viro 	}
152091329559SAl Viro 	return len;
152191329559SAl Viro }
1522eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2);
1523d879cb83SAl Viro 
1524d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1525d879cb83SAl Viro 			       struct iov_iter *i)
1526d879cb83SAl Viro {
1527d879cb83SAl Viro 	__wsum sum, next;
1528d879cb83SAl Viro 	sum = *csum;
15299ea9ce04SDavid Howells 	if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) {
1530241699cdSAl Viro 		WARN_ON(1);
1531241699cdSAl Viro 		return 0;
1532241699cdSAl Viro 	}
15337baa5099SAl Viro 	iterate_and_advance(i, bytes, base, len, off, ({
15347baa5099SAl Viro 		next = csum_and_copy_from_user(base, addr + off, len);
1535d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
15367baa5099SAl Viro 		next ? 0 : len;
1537d879cb83SAl Viro 	}), ({
15387baa5099SAl Viro 		sum = csum_and_memcpy(addr + off, base, len, sum, off);
1539d879cb83SAl Viro 	})
1540d879cb83SAl Viro 	)
1541d879cb83SAl Viro 	*csum = sum;
1542d879cb83SAl Viro 	return bytes;
1543d879cb83SAl Viro }
1544d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter);
1545d879cb83SAl Viro 
154652cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate,
1547d879cb83SAl Viro 			     struct iov_iter *i)
1548d879cb83SAl Viro {
154952cbd23aSWillem de Bruijn 	struct csum_state *csstate = _csstate;
1550d879cb83SAl Viro 	__wsum sum, next;
155178e1f386SAl Viro 
155278e1f386SAl Viro 	if (unlikely(iov_iter_is_discard(i))) {
1553241699cdSAl Viro 		WARN_ON(1);	/* for now */
1554241699cdSAl Viro 		return 0;
1555241699cdSAl Viro 	}
15566852df12SAl Viro 
15576852df12SAl Viro 	sum = csum_shift(csstate->csum, csstate->off);
15586852df12SAl Viro 	if (unlikely(iov_iter_is_pipe(i)))
15596852df12SAl Viro 		bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum);
15606852df12SAl Viro 	else iterate_and_advance(i, bytes, base, len, off, ({
15617baa5099SAl Viro 		next = csum_and_copy_to_user(addr + off, base, len);
1562d879cb83SAl Viro 		sum = csum_block_add(sum, next, off);
15637baa5099SAl Viro 		next ? 0 : len;
1564d879cb83SAl Viro 	}), ({
15657baa5099SAl Viro 		sum = csum_and_memcpy(base, addr + off, len, sum, off);
1566d879cb83SAl Viro 	})
1567d879cb83SAl Viro 	)
1568594e450bSAl Viro 	csstate->csum = csum_shift(sum, csstate->off);
1569594e450bSAl Viro 	csstate->off += bytes;
1570d879cb83SAl Viro 	return bytes;
1571d879cb83SAl Viro }
1572d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter);
1573d879cb83SAl Viro 
1574d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
1575d05f4435SSagi Grimberg 		struct iov_iter *i)
1576d05f4435SSagi Grimberg {
15777999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH
1578d05f4435SSagi Grimberg 	struct ahash_request *hash = hashp;
1579d05f4435SSagi Grimberg 	struct scatterlist sg;
1580d05f4435SSagi Grimberg 	size_t copied;
1581d05f4435SSagi Grimberg 
1582d05f4435SSagi Grimberg 	copied = copy_to_iter(addr, bytes, i);
1583d05f4435SSagi Grimberg 	sg_init_one(&sg, addr, copied);
1584d05f4435SSagi Grimberg 	ahash_request_set_crypt(hash, &sg, NULL, copied);
1585d05f4435SSagi Grimberg 	crypto_ahash_update(hash);
1586d05f4435SSagi Grimberg 	return copied;
158727fad74aSYueHaibing #else
158827fad74aSYueHaibing 	return 0;
158927fad74aSYueHaibing #endif
1590d05f4435SSagi Grimberg }
1591d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter);
1592d05f4435SSagi Grimberg 
159366531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages)
1594d879cb83SAl Viro {
159566531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
159666531c65SAl Viro 	const struct iovec *p;
1597d879cb83SAl Viro 	int npages = 0;
1598d879cb83SAl Viro 
159966531c65SAl Viro 	for (p = i->iov; size; skip = 0, p++) {
160066531c65SAl Viro 		unsigned offs = offset_in_page(p->iov_base + skip);
160166531c65SAl Viro 		size_t len = min(p->iov_len - skip, size);
1602d879cb83SAl Viro 
160366531c65SAl Viro 		if (len) {
160466531c65SAl Viro 			size -= len;
160566531c65SAl Viro 			npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
160666531c65SAl Viro 			if (unlikely(npages > maxpages))
160766531c65SAl Viro 				return maxpages;
160866531c65SAl Viro 		}
160966531c65SAl Viro 	}
161066531c65SAl Viro 	return npages;
161166531c65SAl Viro }
161266531c65SAl Viro 
161366531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages)
161466531c65SAl Viro {
161566531c65SAl Viro 	size_t skip = i->iov_offset, size = i->count;
161666531c65SAl Viro 	const struct bio_vec *p;
161766531c65SAl Viro 	int npages = 0;
161866531c65SAl Viro 
161966531c65SAl Viro 	for (p = i->bvec; size; skip = 0, p++) {
162066531c65SAl Viro 		unsigned offs = (p->bv_offset + skip) % PAGE_SIZE;
162166531c65SAl Viro 		size_t len = min(p->bv_len - skip, size);
162266531c65SAl Viro 
162366531c65SAl Viro 		size -= len;
162466531c65SAl Viro 		npages += DIV_ROUND_UP(offs + len, PAGE_SIZE);
162566531c65SAl Viro 		if (unlikely(npages > maxpages))
162666531c65SAl Viro 			return maxpages;
162766531c65SAl Viro 	}
162866531c65SAl Viro 	return npages;
162966531c65SAl Viro }
163066531c65SAl Viro 
163166531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages)
163266531c65SAl Viro {
163366531c65SAl Viro 	if (unlikely(!i->count))
163466531c65SAl Viro 		return 0;
1635fcb14cb1SAl Viro 	if (likely(iter_is_ubuf(i))) {
1636fcb14cb1SAl Viro 		unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
1637fcb14cb1SAl Viro 		int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
1638fcb14cb1SAl Viro 		return min(npages, maxpages);
1639fcb14cb1SAl Viro 	}
164066531c65SAl Viro 	/* iovec and kvec have identical layouts */
164166531c65SAl Viro 	if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
164266531c65SAl Viro 		return iov_npages(i, maxpages);
164366531c65SAl Viro 	if (iov_iter_is_bvec(i))
164466531c65SAl Viro 		return bvec_npages(i, maxpages);
164566531c65SAl Viro 	if (iov_iter_is_pipe(i)) {
164666531c65SAl Viro 		int npages;
1647241699cdSAl Viro 
1648241699cdSAl Viro 		if (!sanity(i))
1649241699cdSAl Viro 			return 0;
1650241699cdSAl Viro 
165112d426abSAl Viro 		pipe_npages(i, &npages);
165266531c65SAl Viro 		return min(npages, maxpages);
165366531c65SAl Viro 	}
165466531c65SAl Viro 	if (iov_iter_is_xarray(i)) {
1655e4f8df86SAl Viro 		unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE;
1656e4f8df86SAl Viro 		int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE);
165766531c65SAl Viro 		return min(npages, maxpages);
165866531c65SAl Viro 	}
165966531c65SAl Viro 	return 0;
1660d879cb83SAl Viro }
1661d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages);
1662d879cb83SAl Viro 
1663d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1664d879cb83SAl Viro {
1665d879cb83SAl Viro 	*new = *old;
166600e23707SDavid Howells 	if (unlikely(iov_iter_is_pipe(new))) {
1667241699cdSAl Viro 		WARN_ON(1);
1668241699cdSAl Viro 		return NULL;
1669241699cdSAl Viro 	}
167000e23707SDavid Howells 	if (iov_iter_is_bvec(new))
1671d879cb83SAl Viro 		return new->bvec = kmemdup(new->bvec,
1672d879cb83SAl Viro 				    new->nr_segs * sizeof(struct bio_vec),
1673d879cb83SAl Viro 				    flags);
1674fcb14cb1SAl Viro 	else if (iov_iter_is_kvec(new) || iter_is_iovec(new))
1675d879cb83SAl Viro 		/* iovec and kvec have identical layout */
1676d879cb83SAl Viro 		return new->iov = kmemdup(new->iov,
1677d879cb83SAl Viro 				   new->nr_segs * sizeof(struct iovec),
1678d879cb83SAl Viro 				   flags);
1679fcb14cb1SAl Viro 	return NULL;
1680d879cb83SAl Viro }
1681d879cb83SAl Viro EXPORT_SYMBOL(dup_iter);
1682bc917be8SAl Viro 
1683bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov,
1684bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1685bfdc5970SChristoph Hellwig {
1686bfdc5970SChristoph Hellwig 	const struct compat_iovec __user *uiov =
1687bfdc5970SChristoph Hellwig 		(const struct compat_iovec __user *)uvec;
1688bfdc5970SChristoph Hellwig 	int ret = -EFAULT, i;
1689bfdc5970SChristoph Hellwig 
1690a959a978SChristoph Hellwig 	if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
1691bfdc5970SChristoph Hellwig 		return -EFAULT;
1692bfdc5970SChristoph Hellwig 
1693bfdc5970SChristoph Hellwig 	for (i = 0; i < nr_segs; i++) {
1694bfdc5970SChristoph Hellwig 		compat_uptr_t buf;
1695bfdc5970SChristoph Hellwig 		compat_ssize_t len;
1696bfdc5970SChristoph Hellwig 
1697bfdc5970SChristoph Hellwig 		unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
1698bfdc5970SChristoph Hellwig 		unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
1699bfdc5970SChristoph Hellwig 
1700bfdc5970SChristoph Hellwig 		/* check for compat_size_t not fitting in compat_ssize_t .. */
1701bfdc5970SChristoph Hellwig 		if (len < 0) {
1702bfdc5970SChristoph Hellwig 			ret = -EINVAL;
1703bfdc5970SChristoph Hellwig 			goto uaccess_end;
1704bfdc5970SChristoph Hellwig 		}
1705bfdc5970SChristoph Hellwig 		iov[i].iov_base = compat_ptr(buf);
1706bfdc5970SChristoph Hellwig 		iov[i].iov_len = len;
1707bfdc5970SChristoph Hellwig 	}
1708bfdc5970SChristoph Hellwig 
1709bfdc5970SChristoph Hellwig 	ret = 0;
1710bfdc5970SChristoph Hellwig uaccess_end:
1711bfdc5970SChristoph Hellwig 	user_access_end();
1712bfdc5970SChristoph Hellwig 	return ret;
1713bfdc5970SChristoph Hellwig }
1714bfdc5970SChristoph Hellwig 
1715bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov,
1716bfdc5970SChristoph Hellwig 		const struct iovec __user *uvec, unsigned long nr_segs)
1717fb041b59SDavid Laight {
1718fb041b59SDavid Laight 	unsigned long seg;
1719bfdc5970SChristoph Hellwig 
1720bfdc5970SChristoph Hellwig 	if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec)))
1721bfdc5970SChristoph Hellwig 		return -EFAULT;
1722bfdc5970SChristoph Hellwig 	for (seg = 0; seg < nr_segs; seg++) {
1723bfdc5970SChristoph Hellwig 		if ((ssize_t)iov[seg].iov_len < 0)
1724bfdc5970SChristoph Hellwig 			return -EINVAL;
1725bfdc5970SChristoph Hellwig 	}
1726bfdc5970SChristoph Hellwig 
1727bfdc5970SChristoph Hellwig 	return 0;
1728bfdc5970SChristoph Hellwig }
1729bfdc5970SChristoph Hellwig 
1730bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec,
1731bfdc5970SChristoph Hellwig 		unsigned long nr_segs, unsigned long fast_segs,
1732bfdc5970SChristoph Hellwig 		struct iovec *fast_iov, bool compat)
1733bfdc5970SChristoph Hellwig {
1734bfdc5970SChristoph Hellwig 	struct iovec *iov = fast_iov;
1735bfdc5970SChristoph Hellwig 	int ret;
1736fb041b59SDavid Laight 
1737fb041b59SDavid Laight 	/*
1738bfdc5970SChristoph Hellwig 	 * SuS says "The readv() function *may* fail if the iovcnt argument was
1739bfdc5970SChristoph Hellwig 	 * less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1740fb041b59SDavid Laight 	 * traditionally returned zero for zero segments, so...
1741fb041b59SDavid Laight 	 */
1742bfdc5970SChristoph Hellwig 	if (nr_segs == 0)
1743bfdc5970SChristoph Hellwig 		return iov;
1744bfdc5970SChristoph Hellwig 	if (nr_segs > UIO_MAXIOV)
1745bfdc5970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1746fb041b59SDavid Laight 	if (nr_segs > fast_segs) {
1747fb041b59SDavid Laight 		iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1748bfdc5970SChristoph Hellwig 		if (!iov)
1749bfdc5970SChristoph Hellwig 			return ERR_PTR(-ENOMEM);
1750fb041b59SDavid Laight 	}
1751bfdc5970SChristoph Hellwig 
1752bfdc5970SChristoph Hellwig 	if (compat)
1753bfdc5970SChristoph Hellwig 		ret = copy_compat_iovec_from_user(iov, uvec, nr_segs);
1754bfdc5970SChristoph Hellwig 	else
1755bfdc5970SChristoph Hellwig 		ret = copy_iovec_from_user(iov, uvec, nr_segs);
1756bfdc5970SChristoph Hellwig 	if (ret) {
1757bfdc5970SChristoph Hellwig 		if (iov != fast_iov)
1758bfdc5970SChristoph Hellwig 			kfree(iov);
1759bfdc5970SChristoph Hellwig 		return ERR_PTR(ret);
1760fb041b59SDavid Laight 	}
1761bfdc5970SChristoph Hellwig 
1762bfdc5970SChristoph Hellwig 	return iov;
1763bfdc5970SChristoph Hellwig }
1764bfdc5970SChristoph Hellwig 
1765bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec,
1766bfdc5970SChristoph Hellwig 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
1767bfdc5970SChristoph Hellwig 		 struct iov_iter *i, bool compat)
1768bfdc5970SChristoph Hellwig {
1769bfdc5970SChristoph Hellwig 	ssize_t total_len = 0;
1770bfdc5970SChristoph Hellwig 	unsigned long seg;
1771bfdc5970SChristoph Hellwig 	struct iovec *iov;
1772bfdc5970SChristoph Hellwig 
1773bfdc5970SChristoph Hellwig 	iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat);
1774bfdc5970SChristoph Hellwig 	if (IS_ERR(iov)) {
1775bfdc5970SChristoph Hellwig 		*iovp = NULL;
1776bfdc5970SChristoph Hellwig 		return PTR_ERR(iov);
1777fb041b59SDavid Laight 	}
1778fb041b59SDavid Laight 
1779fb041b59SDavid Laight 	/*
1780bfdc5970SChristoph Hellwig 	 * According to the Single Unix Specification we should return EINVAL if
1781bfdc5970SChristoph Hellwig 	 * an element length is < 0 when cast to ssize_t or if the total length
1782bfdc5970SChristoph Hellwig 	 * would overflow the ssize_t return value of the system call.
1783fb041b59SDavid Laight 	 *
1784fb041b59SDavid Laight 	 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1785fb041b59SDavid Laight 	 * overflow case.
1786fb041b59SDavid Laight 	 */
1787fb041b59SDavid Laight 	for (seg = 0; seg < nr_segs; seg++) {
1788fb041b59SDavid Laight 		ssize_t len = (ssize_t)iov[seg].iov_len;
1789fb041b59SDavid Laight 
1790bfdc5970SChristoph Hellwig 		if (!access_ok(iov[seg].iov_base, len)) {
1791bfdc5970SChristoph Hellwig 			if (iov != *iovp)
1792bfdc5970SChristoph Hellwig 				kfree(iov);
1793bfdc5970SChristoph Hellwig 			*iovp = NULL;
1794bfdc5970SChristoph Hellwig 			return -EFAULT;
1795fb041b59SDavid Laight 		}
1796bfdc5970SChristoph Hellwig 
1797bfdc5970SChristoph Hellwig 		if (len > MAX_RW_COUNT - total_len) {
1798bfdc5970SChristoph Hellwig 			len = MAX_RW_COUNT - total_len;
1799fb041b59SDavid Laight 			iov[seg].iov_len = len;
1800fb041b59SDavid Laight 		}
1801bfdc5970SChristoph Hellwig 		total_len += len;
1802fb041b59SDavid Laight 	}
1803bfdc5970SChristoph Hellwig 
1804bfdc5970SChristoph Hellwig 	iov_iter_init(i, type, iov, nr_segs, total_len);
1805bfdc5970SChristoph Hellwig 	if (iov == *iovp)
1806bfdc5970SChristoph Hellwig 		*iovp = NULL;
1807bfdc5970SChristoph Hellwig 	else
1808bfdc5970SChristoph Hellwig 		*iovp = iov;
1809bfdc5970SChristoph Hellwig 	return total_len;
1810fb041b59SDavid Laight }
1811fb041b59SDavid Laight 
1812ffecee4fSVegard Nossum /**
1813ffecee4fSVegard Nossum  * import_iovec() - Copy an array of &struct iovec from userspace
1814ffecee4fSVegard Nossum  *     into the kernel, check that it is valid, and initialize a new
1815ffecee4fSVegard Nossum  *     &struct iov_iter iterator to access it.
1816ffecee4fSVegard Nossum  *
1817ffecee4fSVegard Nossum  * @type: One of %READ or %WRITE.
1818bfdc5970SChristoph Hellwig  * @uvec: Pointer to the userspace array.
1819ffecee4fSVegard Nossum  * @nr_segs: Number of elements in userspace array.
1820ffecee4fSVegard Nossum  * @fast_segs: Number of elements in @iov.
1821bfdc5970SChristoph Hellwig  * @iovp: (input and output parameter) Pointer to pointer to (usually small
1822ffecee4fSVegard Nossum  *     on-stack) kernel array.
1823ffecee4fSVegard Nossum  * @i: Pointer to iterator that will be initialized on success.
1824ffecee4fSVegard Nossum  *
1825ffecee4fSVegard Nossum  * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1826ffecee4fSVegard Nossum  * then this function places %NULL in *@iov on return. Otherwise, a new
1827ffecee4fSVegard Nossum  * array will be allocated and the result placed in *@iov. This means that
1828ffecee4fSVegard Nossum  * the caller may call kfree() on *@iov regardless of whether the small
1829ffecee4fSVegard Nossum  * on-stack array was used or not (and regardless of whether this function
1830ffecee4fSVegard Nossum  * returns an error or not).
1831ffecee4fSVegard Nossum  *
183287e5e6daSJens Axboe  * Return: Negative error code on error, bytes imported on success
1833ffecee4fSVegard Nossum  */
1834bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec,
1835bc917be8SAl Viro 		 unsigned nr_segs, unsigned fast_segs,
1836bfdc5970SChristoph Hellwig 		 struct iovec **iovp, struct iov_iter *i)
1837bc917be8SAl Viro {
183889cd35c5SChristoph Hellwig 	return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
183989cd35c5SChristoph Hellwig 			      in_compat_syscall());
1840bc917be8SAl Viro }
1841bc917be8SAl Viro EXPORT_SYMBOL(import_iovec);
1842bc917be8SAl Viro 
1843bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len,
1844bc917be8SAl Viro 		 struct iovec *iov, struct iov_iter *i)
1845bc917be8SAl Viro {
1846bc917be8SAl Viro 	if (len > MAX_RW_COUNT)
1847bc917be8SAl Viro 		len = MAX_RW_COUNT;
184896d4f267SLinus Torvalds 	if (unlikely(!access_ok(buf, len)))
1849bc917be8SAl Viro 		return -EFAULT;
1850bc917be8SAl Viro 
1851bc917be8SAl Viro 	iov->iov_base = buf;
1852bc917be8SAl Viro 	iov->iov_len = len;
1853bc917be8SAl Viro 	iov_iter_init(i, rw, iov, 1, len);
1854bc917be8SAl Viro 	return 0;
1855bc917be8SAl Viro }
1856e1267585SAl Viro EXPORT_SYMBOL(import_single_range);
18578fb0f47aSJens Axboe 
18588fb0f47aSJens Axboe /**
18598fb0f47aSJens Axboe  * iov_iter_restore() - Restore a &struct iov_iter to the same state as when
18608fb0f47aSJens Axboe  *     iov_iter_save_state() was called.
18618fb0f47aSJens Axboe  *
18628fb0f47aSJens Axboe  * @i: &struct iov_iter to restore
18638fb0f47aSJens Axboe  * @state: state to restore from
18648fb0f47aSJens Axboe  *
18658fb0f47aSJens Axboe  * Used after iov_iter_save_state() to bring restore @i, if operations may
18668fb0f47aSJens Axboe  * have advanced it.
18678fb0f47aSJens Axboe  *
18688fb0f47aSJens Axboe  * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC
18698fb0f47aSJens Axboe  */
18708fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
18718fb0f47aSJens Axboe {
18728fb0f47aSJens Axboe 	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
1873fcb14cb1SAl Viro 			 !iov_iter_is_kvec(i) && !iter_is_ubuf(i))
18748fb0f47aSJens Axboe 		return;
18758fb0f47aSJens Axboe 	i->iov_offset = state->iov_offset;
18768fb0f47aSJens Axboe 	i->count = state->count;
1877fcb14cb1SAl Viro 	if (iter_is_ubuf(i))
1878fcb14cb1SAl Viro 		return;
18798fb0f47aSJens Axboe 	/*
18808fb0f47aSJens Axboe 	 * For the *vec iters, nr_segs + iov is constant - if we increment
18818fb0f47aSJens Axboe 	 * the vec, then we also decrement the nr_segs count. Hence we don't
18828fb0f47aSJens Axboe 	 * need to track both of these, just one is enough and we can deduct
18838fb0f47aSJens Axboe 	 * the other from that. ITER_KVEC and ITER_IOVEC are the same struct
18848fb0f47aSJens Axboe 	 * size, so we can just increment the iov pointer as they are unionzed.
18858fb0f47aSJens Axboe 	 * ITER_BVEC _may_ be the same size on some archs, but on others it is
18868fb0f47aSJens Axboe 	 * not. Be safe and handle it separately.
18878fb0f47aSJens Axboe 	 */
18888fb0f47aSJens Axboe 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
18898fb0f47aSJens Axboe 	if (iov_iter_is_bvec(i))
18908fb0f47aSJens Axboe 		i->bvec -= state->nr_segs - i->nr_segs;
18918fb0f47aSJens Axboe 	else
18928fb0f47aSJens Axboe 		i->iov -= state->nr_segs - i->nr_segs;
18938fb0f47aSJens Axboe 	i->nr_segs = state->nr_segs;
18948fb0f47aSJens Axboe }
1895