blob: df194cc070350469f51ea83619c57b70d76e234d [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/net/sunrpc/xdr.c
4 *
5 * Generic XDR support.
6 *
7 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8 */
9
Chuck Levera246b012005-08-11 16:25:23 -040010#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/string.h>
14#include <linux/kernel.h>
15#include <linux/pagemap.h>
16#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/sunrpc/xdr.h>
18#include <linux/sunrpc/msg_prot.h>
Trond Myklebust9d96acb2018-09-13 12:22:04 -040019#include <linux/bvec.h>
Chuck Lever55828632019-02-11 11:24:10 -050020#include <trace/events/sunrpc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Anna Schumakere6ac0ac2020-04-21 11:27:00 -040022static void _copy_to_pages(struct page **, size_t, const char *, size_t);
23
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/*
26 * XDR functions for basic NFS types
27 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070028__be32 *
29xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 unsigned int quadlen = XDR_QUADLEN(obj->len);
32
33 p[quadlen] = 0; /* zero trailing bytes */
Benny Halevy9f162d22009-08-14 17:18:44 +030034 *p++ = cpu_to_be32(obj->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 memcpy(p, obj->data, obj->len);
36 return p + XDR_QUADLEN(obj->len);
37}
Trond Myklebust468039e2008-12-23 15:21:31 -050038EXPORT_SYMBOL_GPL(xdr_encode_netobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070040__be32 *
41xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 unsigned int len;
44
Benny Halevy98866b52009-08-14 17:18:49 +030045 if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return NULL;
47 obj->len = len;
48 obj->data = (u8 *) p;
49 return p + XDR_QUADLEN(len);
50}
Trond Myklebust468039e2008-12-23 15:21:31 -050051EXPORT_SYMBOL_GPL(xdr_decode_netobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/**
54 * xdr_encode_opaque_fixed - Encode fixed length opaque data
Pavel Pisa4dc3b162005-05-01 08:59:25 -070055 * @p: pointer to current position in XDR buffer.
56 * @ptr: pointer to data to encode (or NULL)
57 * @nbytes: size of data.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 *
59 * Copy the array of data of length nbytes at ptr to the XDR buffer
60 * at position p, then align to the next 32-bit boundary by padding
61 * with zero bytes (see RFC1832).
62 * Note: if ptr is NULL, only the padding is performed.
63 *
64 * Returns the updated current XDR buffer position
65 *
66 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070067__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 if (likely(nbytes != 0)) {
70 unsigned int quadlen = XDR_QUADLEN(nbytes);
71 unsigned int padding = (quadlen << 2) - nbytes;
72
73 if (ptr != NULL)
74 memcpy(p, ptr, nbytes);
75 if (padding != 0)
76 memset((char *)p + nbytes, 0, padding);
77 p += quadlen;
78 }
79 return p;
80}
Trond Myklebust468039e2008-12-23 15:21:31 -050081EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/**
84 * xdr_encode_opaque - Encode variable length opaque data
Pavel Pisa4dc3b162005-05-01 08:59:25 -070085 * @p: pointer to current position in XDR buffer.
86 * @ptr: pointer to data to encode (or NULL)
87 * @nbytes: size of data.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
89 * Returns the updated current XDR buffer position
90 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070091__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Benny Halevy9f162d22009-08-14 17:18:44 +030093 *p++ = cpu_to_be32(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return xdr_encode_opaque_fixed(p, ptr, nbytes);
95}
Trond Myklebust468039e2008-12-23 15:21:31 -050096EXPORT_SYMBOL_GPL(xdr_encode_opaque);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070098__be32 *
99xdr_encode_string(__be32 *p, const char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 return xdr_encode_array(p, string, strlen(string));
102}
Trond Myklebust468039e2008-12-23 15:21:31 -0500103EXPORT_SYMBOL_GPL(xdr_encode_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700105__be32 *
Chuck Levere5cff482007-11-01 16:56:47 -0400106xdr_decode_string_inplace(__be32 *p, char **sp,
107 unsigned int *lenp, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Chuck Levere5cff482007-11-01 16:56:47 -0400109 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Benny Halevy98866b52009-08-14 17:18:49 +0300111 len = be32_to_cpu(*p++);
Chuck Levere5cff482007-11-01 16:56:47 -0400112 if (len > maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return NULL;
114 *lenp = len;
115 *sp = (char *) p;
116 return p + XDR_QUADLEN(len);
117}
Trond Myklebust468039e2008-12-23 15:21:31 -0500118EXPORT_SYMBOL_GPL(xdr_decode_string_inplace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Chuck Leverb4687da2010-09-21 16:55:48 -0400120/**
121 * xdr_terminate_string - '\0'-terminate a string residing in an xdr_buf
122 * @buf: XDR buffer where string resides
123 * @len: length of string, in bytes
124 *
125 */
Trond Myklebustf8d0e602020-12-08 22:56:18 -0500126void xdr_terminate_string(const struct xdr_buf *buf, const u32 len)
Chuck Leverb4687da2010-09-21 16:55:48 -0400127{
128 char *kaddr;
129
Cong Wangb8541782011-11-25 23:14:40 +0800130 kaddr = kmap_atomic(buf->pages[0]);
Chuck Leverb4687da2010-09-21 16:55:48 -0400131 kaddr[buf->page_base + len] = '\0';
Cong Wangb8541782011-11-25 23:14:40 +0800132 kunmap_atomic(kaddr);
Chuck Leverb4687da2010-09-21 16:55:48 -0400133}
Trond Myklebust0d961aa2011-07-13 19:24:15 -0400134EXPORT_SYMBOL_GPL(xdr_terminate_string);
Chuck Leverb4687da2010-09-21 16:55:48 -0400135
Trond Myklebustf8d0e602020-12-08 22:56:18 -0500136size_t xdr_buf_pagecount(const struct xdr_buf *buf)
Trond Myklebust9d96acb2018-09-13 12:22:04 -0400137{
138 if (!buf->page_len)
139 return 0;
140 return (buf->page_base + buf->page_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
141}
142
143int
144xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp)
145{
146 size_t i, n = xdr_buf_pagecount(buf);
147
148 if (n != 0 && buf->bvec == NULL) {
149 buf->bvec = kmalloc_array(n, sizeof(buf->bvec[0]), gfp);
150 if (!buf->bvec)
151 return -ENOMEM;
152 for (i = 0; i < n; i++) {
153 buf->bvec[i].bv_page = buf->pages[i];
154 buf->bvec[i].bv_len = PAGE_SIZE;
155 buf->bvec[i].bv_offset = 0;
156 }
157 }
158 return 0;
159}
160
161void
162xdr_free_bvec(struct xdr_buf *buf)
163{
164 kfree(buf->bvec);
165 buf->bvec = NULL;
166}
167
Chuck Levercf500ba2019-02-11 11:25:20 -0500168/**
169 * xdr_inline_pages - Prepare receive buffer for a large reply
170 * @xdr: xdr_buf into which reply will be placed
171 * @offset: expected offset where data payload will start, in bytes
172 * @pages: vector of struct page pointers
173 * @base: offset in first page where receive should start, in bytes
174 * @len: expected size of the upper layer data payload, in bytes
175 *
176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
179 struct page **pages, unsigned int base, unsigned int len)
180{
181 struct kvec *head = xdr->head;
182 struct kvec *tail = xdr->tail;
183 char *buf = (char *)head->iov_base;
184 unsigned int buflen = head->iov_len;
185
186 head->iov_len = offset;
187
188 xdr->pages = pages;
189 xdr->page_base = base;
190 xdr->page_len = len;
191
192 tail->iov_base = buf + offset;
193 tail->iov_len = buflen - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 xdr->buflen += len;
195}
Trond Myklebust468039e2008-12-23 15:21:31 -0500196EXPORT_SYMBOL_GPL(xdr_inline_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
199 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
Ben Hutchings2c530402012-07-10 10:55:09 +0000200 */
201
202/**
Anna Schumakere6ac0ac2020-04-21 11:27:00 -0400203 * _shift_data_left_pages
204 * @pages: vector of pages containing both the source and dest memory area.
205 * @pgto_base: page vector address of destination
206 * @pgfrom_base: page vector address of source
207 * @len: number of bytes to copy
208 *
209 * Note: the addresses pgto_base and pgfrom_base are both calculated in
210 * the same way:
211 * if a memory area starts at byte 'base' in page 'pages[i]',
212 * then its address is given as (i << PAGE_CACHE_SHIFT) + base
213 * Alse note: pgto_base must be < pgfrom_base, but the memory areas
214 * they point to may overlap.
215 */
216static void
217_shift_data_left_pages(struct page **pages, size_t pgto_base,
218 size_t pgfrom_base, size_t len)
219{
220 struct page **pgfrom, **pgto;
221 char *vfrom, *vto;
222 size_t copy;
223
224 BUG_ON(pgfrom_base <= pgto_base);
225
Trond Myklebustc54e9592020-12-07 13:30:46 -0500226 if (!len)
227 return;
228
Anna Schumakere6ac0ac2020-04-21 11:27:00 -0400229 pgto = pages + (pgto_base >> PAGE_SHIFT);
230 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
231
232 pgto_base &= ~PAGE_MASK;
233 pgfrom_base &= ~PAGE_MASK;
234
235 do {
236 if (pgto_base >= PAGE_SIZE) {
237 pgto_base = 0;
238 pgto++;
239 }
240 if (pgfrom_base >= PAGE_SIZE){
241 pgfrom_base = 0;
242 pgfrom++;
243 }
244
245 copy = len;
246 if (copy > (PAGE_SIZE - pgto_base))
247 copy = PAGE_SIZE - pgto_base;
248 if (copy > (PAGE_SIZE - pgfrom_base))
249 copy = PAGE_SIZE - pgfrom_base;
250
251 vto = kmap_atomic(*pgto);
252 if (*pgto != *pgfrom) {
253 vfrom = kmap_atomic(*pgfrom);
254 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
255 kunmap_atomic(vfrom);
256 } else
257 memmove(vto + pgto_base, vto + pgfrom_base, copy);
258 flush_dcache_page(*pgto);
259 kunmap_atomic(vto);
260
261 pgto_base += copy;
262 pgfrom_base += copy;
263
264 } while ((len -= copy) != 0);
265}
266
Anna Schumakere6ac0ac2020-04-21 11:27:00 -0400267/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * _shift_data_right_pages
269 * @pages: vector of pages containing both the source and dest memory area.
270 * @pgto_base: page vector address of destination
271 * @pgfrom_base: page vector address of source
272 * @len: number of bytes to copy
273 *
274 * Note: the addresses pgto_base and pgfrom_base are both calculated in
275 * the same way:
276 * if a memory area starts at byte 'base' in page 'pages[i]',
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300277 * then its address is given as (i << PAGE_SHIFT) + base
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * Also note: pgfrom_base must be < pgto_base, but the memory areas
279 * they point to may overlap.
280 */
281static void
282_shift_data_right_pages(struct page **pages, size_t pgto_base,
283 size_t pgfrom_base, size_t len)
284{
285 struct page **pgfrom, **pgto;
286 char *vfrom, *vto;
287 size_t copy;
288
289 BUG_ON(pgto_base <= pgfrom_base);
290
Trond Myklebustc54e9592020-12-07 13:30:46 -0500291 if (!len)
292 return;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 pgto_base += len;
295 pgfrom_base += len;
296
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300297 pgto = pages + (pgto_base >> PAGE_SHIFT);
298 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300300 pgto_base &= ~PAGE_MASK;
301 pgfrom_base &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 do {
304 /* Are any pointers crossing a page boundary? */
305 if (pgto_base == 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300306 pgto_base = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 pgto--;
308 }
309 if (pgfrom_base == 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300310 pgfrom_base = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 pgfrom--;
312 }
313
314 copy = len;
315 if (copy > pgto_base)
316 copy = pgto_base;
317 if (copy > pgfrom_base)
318 copy = pgfrom_base;
319 pgto_base -= copy;
320 pgfrom_base -= copy;
321
Cong Wangb8541782011-11-25 23:14:40 +0800322 vto = kmap_atomic(*pgto);
Trond Myklebust347e2232013-08-28 13:35:13 -0400323 if (*pgto != *pgfrom) {
324 vfrom = kmap_atomic(*pgfrom);
325 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
326 kunmap_atomic(vfrom);
327 } else
328 memmove(vto + pgto_base, vto + pgfrom_base, copy);
Trond Myklebustbce34812006-07-05 13:17:12 -0400329 flush_dcache_page(*pgto);
Cong Wangb8541782011-11-25 23:14:40 +0800330 kunmap_atomic(vto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 } while ((len -= copy) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Ben Hutchings2c530402012-07-10 10:55:09 +0000335/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * _copy_to_pages
337 * @pages: array of pages
338 * @pgbase: page vector address of destination
339 * @p: pointer to source data
340 * @len: length
341 *
342 * Copies data from an arbitrary memory location into an array of pages
343 * The copy is assumed to be non-overlapping.
344 */
345static void
346_copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
347{
348 struct page **pgto;
349 char *vto;
350 size_t copy;
351
Trond Myklebustc54e9592020-12-07 13:30:46 -0500352 if (!len)
353 return;
354
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300355 pgto = pages + (pgbase >> PAGE_SHIFT);
356 pgbase &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Trond Myklebustdaeba892008-03-31 17:02:02 -0400358 for (;;) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300359 copy = PAGE_SIZE - pgbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (copy > len)
361 copy = len;
362
Cong Wangb8541782011-11-25 23:14:40 +0800363 vto = kmap_atomic(*pgto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 memcpy(vto + pgbase, p, copy);
Cong Wangb8541782011-11-25 23:14:40 +0800365 kunmap_atomic(vto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Trond Myklebustdaeba892008-03-31 17:02:02 -0400367 len -= copy;
368 if (len == 0)
369 break;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 pgbase += copy;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300372 if (pgbase == PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 flush_dcache_page(*pgto);
374 pgbase = 0;
375 pgto++;
376 }
377 p += copy;
Trond Myklebustdaeba892008-03-31 17:02:02 -0400378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 flush_dcache_page(*pgto);
380}
381
Ben Hutchings2c530402012-07-10 10:55:09 +0000382/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 * _copy_from_pages
384 * @p: pointer to destination
385 * @pages: array of pages
386 * @pgbase: offset of source data
387 * @len: length
388 *
389 * Copies data into an arbitrary memory location from an array of pages
390 * The copy is assumed to be non-overlapping.
391 */
Andy Adamsonbf118a32011-12-07 11:55:27 -0500392void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393_copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
394{
395 struct page **pgfrom;
396 char *vfrom;
397 size_t copy;
398
Trond Myklebustc54e9592020-12-07 13:30:46 -0500399 if (!len)
400 return;
401
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300402 pgfrom = pages + (pgbase >> PAGE_SHIFT);
403 pgbase &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 do {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300406 copy = PAGE_SIZE - pgbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (copy > len)
408 copy = len;
409
Cong Wangb8541782011-11-25 23:14:40 +0800410 vfrom = kmap_atomic(*pgfrom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 memcpy(p, vfrom + pgbase, copy);
Cong Wangb8541782011-11-25 23:14:40 +0800412 kunmap_atomic(vfrom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 pgbase += copy;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300415 if (pgbase == PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 pgbase = 0;
417 pgfrom++;
418 }
419 p += copy;
420
421 } while ((len -= copy) != 0);
422}
Andy Adamsonbf118a32011-12-07 11:55:27 -0500423EXPORT_SYMBOL_GPL(_copy_from_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500425static void xdr_buf_iov_zero(const struct kvec *iov, unsigned int base,
426 unsigned int len)
427{
428 if (base >= iov->iov_len)
429 return;
430 if (len > iov->iov_len - base)
431 len = iov->iov_len - base;
432 memset(iov->iov_base + base, 0, len);
433}
434
Ben Hutchings2c530402012-07-10 10:55:09 +0000435/**
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500436 * xdr_buf_pages_zero
437 * @buf: xdr_buf
438 * @pgbase: beginning offset
Anna Schumaker84ce1822014-05-28 13:38:53 -0400439 * @len: length
440 */
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500441static void xdr_buf_pages_zero(const struct xdr_buf *buf, unsigned int pgbase,
442 unsigned int len)
Anna Schumaker84ce1822014-05-28 13:38:53 -0400443{
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500444 struct page **pages = buf->pages;
Anna Schumaker84ce1822014-05-28 13:38:53 -0400445 struct page **page;
446 char *vpage;
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500447 unsigned int zero;
448
449 if (!len)
450 return;
451 if (pgbase >= buf->page_len) {
452 xdr_buf_iov_zero(buf->tail, pgbase - buf->page_len, len);
453 return;
454 }
455 if (pgbase + len > buf->page_len) {
456 xdr_buf_iov_zero(buf->tail, 0, pgbase + len - buf->page_len);
457 len = buf->page_len - pgbase;
458 }
459
460 pgbase += buf->page_base;
Anna Schumaker84ce1822014-05-28 13:38:53 -0400461
462 page = pages + (pgbase >> PAGE_SHIFT);
463 pgbase &= ~PAGE_MASK;
464
465 do {
466 zero = PAGE_SIZE - pgbase;
467 if (zero > len)
468 zero = len;
469
470 vpage = kmap_atomic(*page);
471 memset(vpage + pgbase, 0, zero);
472 kunmap_atomic(vpage);
473
474 flush_dcache_page(*page);
475 pgbase = 0;
476 page++;
477
478 } while ((len -= zero) != 0);
479}
480
Trond Myklebust5802f7c2020-12-10 08:55:35 -0500481static unsigned int xdr_buf_pages_fill_sparse(const struct xdr_buf *buf,
482 unsigned int buflen, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Trond Myklebust5802f7c2020-12-10 08:55:35 -0500484 unsigned int i, npages, pagelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Trond Myklebust5802f7c2020-12-10 08:55:35 -0500486 if (!(buf->flags & XDRBUF_SPARSE_PAGES))
487 return buflen;
488 if (buflen <= buf->head->iov_len)
489 return buflen;
490 pagelen = buflen - buf->head->iov_len;
491 if (pagelen > buf->page_len)
492 pagelen = buf->page_len;
493 npages = (pagelen + buf->page_base + PAGE_SIZE - 1) >> PAGE_SHIFT;
494 for (i = 0; i < npages; i++) {
495 if (!buf->pages[i])
496 continue;
497 buf->pages[i] = alloc_page(gfp);
498 if (likely(buf->pages[i]))
499 continue;
500 buflen -= pagelen;
501 pagelen = i << PAGE_SHIFT;
502 if (pagelen > buf->page_base)
503 buflen += pagelen - buf->page_base;
504 break;
505 }
506 return buflen;
507}
Weston Andros Adamson18e624a2012-10-23 10:43:42 -0400508
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500509static void xdr_buf_try_expand(struct xdr_buf *buf, unsigned int len)
510{
511 struct kvec *head = buf->head;
512 struct kvec *tail = buf->tail;
513 unsigned int sum = head->iov_len + buf->page_len + tail->iov_len;
Trond Myklebust5802f7c2020-12-10 08:55:35 -0500514 unsigned int free_space, newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500516 if (sum > buf->len) {
517 free_space = min_t(unsigned int, sum - buf->len, len);
Trond Myklebust5802f7c2020-12-10 08:55:35 -0500518 newlen = xdr_buf_pages_fill_sparse(buf, buf->len + free_space,
519 GFP_KERNEL);
520 free_space = newlen - buf->len;
521 buf->len = newlen;
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500522 len -= free_space;
523 if (!len)
524 return;
525 }
526
527 if (buf->buflen > sum) {
528 /* Expand the tail buffer */
529 free_space = min_t(unsigned int, buf->buflen - sum, len);
530 tail->iov_len += free_space;
531 buf->len += free_space;
532 }
533}
534
535static void xdr_buf_tail_copy_right(const struct xdr_buf *buf,
536 unsigned int base, unsigned int len,
537 unsigned int shift)
538{
539 const struct kvec *tail = buf->tail;
540 unsigned int to = base + shift;
541
542 if (to >= tail->iov_len)
543 return;
544 if (len + to > tail->iov_len)
545 len = tail->iov_len - to;
546 memmove(tail->iov_base + to, tail->iov_base + base, len);
547}
548
549static void xdr_buf_pages_copy_right(const struct xdr_buf *buf,
550 unsigned int base, unsigned int len,
551 unsigned int shift)
552{
553 const struct kvec *tail = buf->tail;
554 unsigned int to = base + shift;
555 unsigned int pglen = 0;
556 unsigned int talen = 0, tato = 0;
557
558 if (base >= buf->page_len)
559 return;
560 if (len > buf->page_len - base)
561 len = buf->page_len - base;
562 if (to >= buf->page_len) {
563 tato = to - buf->page_len;
564 if (tail->iov_len >= len + tato)
565 talen = len;
566 else if (tail->iov_len > tato)
567 talen = tail->iov_len - tato;
568 } else if (len + to >= buf->page_len) {
569 pglen = buf->page_len - to;
570 talen = len - pglen;
571 if (talen > tail->iov_len)
572 talen = tail->iov_len;
573 } else
574 pglen = len;
575
576 _copy_from_pages(tail->iov_base + tato, buf->pages,
577 buf->page_base + base + pglen, talen);
578 _shift_data_right_pages(buf->pages, buf->page_base + to,
579 buf->page_base + base, pglen);
580}
581
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500582static void xdr_buf_head_copy_right(const struct xdr_buf *buf,
583 unsigned int base, unsigned int len,
584 unsigned int shift)
585{
586 const struct kvec *head = buf->head;
587 const struct kvec *tail = buf->tail;
588 unsigned int to = base + shift;
589 unsigned int pglen = 0, pgto = 0;
590 unsigned int talen = 0, tato = 0;
591
592 if (base >= head->iov_len)
593 return;
594 if (len > head->iov_len - base)
595 len = head->iov_len - base;
596 if (to >= buf->page_len + head->iov_len) {
597 tato = to - buf->page_len - head->iov_len;
598 talen = len;
599 } else if (to >= head->iov_len) {
600 pgto = to - head->iov_len;
601 pglen = len;
602 if (pgto + pglen > buf->page_len) {
603 talen = pgto + pglen - buf->page_len;
604 pglen -= talen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500606 } else {
607 pglen = len - to;
608 if (pglen > buf->page_len) {
609 talen = pglen - buf->page_len;
610 pglen = buf->page_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612 }
Chuck Lever7be9cea32019-02-11 11:24:16 -0500613
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500614 len -= talen;
615 base += len;
616 if (talen + tato > tail->iov_len)
617 talen = tail->iov_len > tato ? tail->iov_len - tato : 0;
618 memcpy(tail->iov_base + tato, head->iov_base + base, talen);
619
620 len -= pglen;
621 base -= pglen;
622 _copy_to_pages(buf->pages, buf->page_base + pgto, head->iov_base + base,
623 pglen);
624
625 base -= len;
626 memmove(head->iov_base + to, head->iov_base + base, len);
627}
628
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500629static void xdr_buf_tail_shift_right(const struct xdr_buf *buf,
630 unsigned int base, unsigned int len,
631 unsigned int shift)
632{
633 const struct kvec *tail = buf->tail;
634
635 if (base >= tail->iov_len || !shift || !len)
636 return;
637 xdr_buf_tail_copy_right(buf, base, len, shift);
638}
639
640static void xdr_buf_pages_shift_right(const struct xdr_buf *buf,
641 unsigned int base, unsigned int len,
642 unsigned int shift)
643{
644 if (!shift || !len)
645 return;
646 if (base >= buf->page_len) {
647 xdr_buf_tail_shift_right(buf, base - buf->page_len, len, shift);
648 return;
649 }
650 if (base + len > buf->page_len)
651 xdr_buf_tail_shift_right(buf, 0, base + len - buf->page_len,
652 shift);
653 xdr_buf_pages_copy_right(buf, base, len, shift);
654}
655
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500656static void xdr_buf_head_shift_right(const struct xdr_buf *buf,
657 unsigned int base, unsigned int len,
658 unsigned int shift)
659{
660 const struct kvec *head = buf->head;
661
662 if (!shift)
663 return;
664 if (base >= head->iov_len) {
665 xdr_buf_pages_shift_right(buf, head->iov_len - base, len,
666 shift);
667 return;
668 }
669 if (base + len > head->iov_len)
670 xdr_buf_pages_shift_right(buf, 0, base + len - head->iov_len,
671 shift);
672 xdr_buf_head_copy_right(buf, base, len, shift);
673}
674
Trond Myklebust9a20f6f2020-12-04 15:16:46 -0500675static void xdr_buf_tail_copy_left(const struct xdr_buf *buf, unsigned int base,
676 unsigned int len, unsigned int shift)
677{
678 const struct kvec *tail = buf->tail;
679
680 if (base >= tail->iov_len)
681 return;
682 if (len > tail->iov_len - base)
683 len = tail->iov_len - base;
684 /* Shift data into head */
685 if (shift > buf->page_len + base) {
686 const struct kvec *head = buf->head;
687 unsigned int hdto =
688 head->iov_len + buf->page_len + base - shift;
689 unsigned int hdlen = len;
690
691 if (WARN_ONCE(shift > head->iov_len + buf->page_len + base,
692 "SUNRPC: Misaligned data.\n"))
693 return;
694 if (hdto + hdlen > head->iov_len)
695 hdlen = head->iov_len - hdto;
696 memcpy(head->iov_base + hdto, tail->iov_base + base, hdlen);
697 base += hdlen;
698 len -= hdlen;
699 if (!len)
700 return;
701 }
702 /* Shift data into pages */
703 if (shift > base) {
704 unsigned int pgto = buf->page_len + base - shift;
705 unsigned int pglen = len;
706
707 if (pgto + pglen > buf->page_len)
708 pglen = buf->page_len - pgto;
709 _copy_to_pages(buf->pages, buf->page_base + pgto,
710 tail->iov_base + base, pglen);
711 base += pglen;
712 len -= pglen;
713 if (!len)
714 return;
715 }
716 memmove(tail->iov_base + base - shift, tail->iov_base + base, len);
717}
718
719static void xdr_buf_pages_copy_left(const struct xdr_buf *buf,
720 unsigned int base, unsigned int len,
721 unsigned int shift)
722{
723 unsigned int pgto;
724
725 if (base >= buf->page_len)
726 return;
727 if (len > buf->page_len - base)
728 len = buf->page_len - base;
729 /* Shift data into head */
730 if (shift > base) {
731 const struct kvec *head = buf->head;
732 unsigned int hdto = head->iov_len + base - shift;
733 unsigned int hdlen = len;
734
735 if (WARN_ONCE(shift > head->iov_len + base,
736 "SUNRPC: Misaligned data.\n"))
737 return;
738 if (hdto + hdlen > head->iov_len)
739 hdlen = head->iov_len - hdto;
740 _copy_from_pages(head->iov_base + hdto, buf->pages,
741 buf->page_base + base, hdlen);
742 base += hdlen;
743 len -= hdlen;
744 if (!len)
745 return;
746 }
747 pgto = base - shift;
748 _shift_data_left_pages(buf->pages, buf->page_base + pgto,
749 buf->page_base + base, len);
750}
751
752static void xdr_buf_tail_shift_left(const struct xdr_buf *buf,
753 unsigned int base, unsigned int len,
754 unsigned int shift)
755{
756 if (!shift || !len)
757 return;
758 xdr_buf_tail_copy_left(buf, base, len, shift);
759}
760
761static void xdr_buf_pages_shift_left(const struct xdr_buf *buf,
762 unsigned int base, unsigned int len,
763 unsigned int shift)
764{
765 if (!shift || !len)
766 return;
767 if (base >= buf->page_len) {
768 xdr_buf_tail_shift_left(buf, base - buf->page_len, len, shift);
769 return;
770 }
771 xdr_buf_pages_copy_left(buf, base, len, shift);
772 len += base;
773 if (len <= buf->page_len)
774 return;
775 xdr_buf_tail_copy_left(buf, 0, len - buf->page_len, shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Ben Hutchings2c530402012-07-10 10:55:09 +0000778/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * xdr_shrink_bufhead
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * @buf: xdr_buf
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500781 * @len: new length of buf->head[0]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500783 * Shrinks XDR buffer's header kvec buf->head[0], setting it to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * 'len' bytes. The extra data is not lost, but is instead
785 * moved into the inlined pages and/or the tail.
786 */
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500787static unsigned int xdr_shrink_bufhead(struct xdr_buf *buf, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500789 struct kvec *head = buf->head;
790 unsigned int shift, buflen = max(buf->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 WARN_ON_ONCE(len > head->iov_len);
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500793 if (head->iov_len > buflen) {
794 buf->buflen -= head->iov_len - buflen;
795 head->iov_len = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500797 if (len >= head->iov_len)
798 return 0;
799 shift = head->iov_len - len;
800 xdr_buf_try_expand(buf, shift);
801 xdr_buf_head_shift_right(buf, len, buflen - len, shift);
802 head->iov_len = len;
803 buf->buflen -= shift;
804 buf->len -= shift;
805 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808/**
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500809 * xdr_shrink_pagelen - shrinks buf->pages to @len bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * @buf: xdr_buf
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500811 * @len: new page buffer length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 *
Chuck Levere8d70b32019-11-15 08:39:07 -0500813 * The extra data is not lost, but is instead moved into buf->tail.
814 * Returns the actual number of bytes moved.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500816static unsigned int xdr_shrink_pagelen(struct xdr_buf *buf, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500818 unsigned int shift, buflen = buf->len - buf->head->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500820 WARN_ON_ONCE(len > buf->page_len);
821 if (buf->head->iov_len >= buf->len || len > buflen)
822 buflen = len;
823 if (buf->page_len > buflen) {
824 buf->buflen -= buf->page_len - buflen;
825 buf->page_len = buflen;
826 }
827 if (len >= buf->page_len)
828 return 0;
829 shift = buf->page_len - len;
830 xdr_buf_try_expand(buf, shift);
831 xdr_buf_pages_shift_right(buf, len, buflen - len, shift);
832 buf->page_len = len;
833 buf->len -= shift;
834 buf->buflen -= shift;
835 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838void
839xdr_shift_buf(struct xdr_buf *buf, size_t len)
840{
Trond Myklebust6707fbd2020-12-06 10:15:04 -0500841 xdr_shrink_bufhead(buf, buf->head->iov_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
Trond Myklebust468039e2008-12-23 15:21:31 -0500843EXPORT_SYMBOL_GPL(xdr_shift_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845/**
Trond Myklebust4517d522012-06-21 17:14:46 -0400846 * xdr_stream_pos - Return the current offset from the start of the xdr_stream
847 * @xdr: pointer to struct xdr_stream
848 */
849unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
850{
851 return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
852}
853EXPORT_SYMBOL_GPL(xdr_stream_pos);
854
Trond Myklebustc4f2f592020-12-04 17:15:09 -0500855static void xdr_stream_set_pos(struct xdr_stream *xdr, unsigned int pos)
856{
857 unsigned int blen = xdr->buf->len;
858
859 xdr->nwords = blen > pos ? XDR_QUADLEN(blen) - XDR_QUADLEN(pos) : 0;
860}
861
862static void xdr_stream_page_set_pos(struct xdr_stream *xdr, unsigned int pos)
863{
864 xdr_stream_set_pos(xdr, pos + xdr->buf->head[0].iov_len);
865}
866
Trond Myklebust4517d522012-06-21 17:14:46 -0400867/**
Anna Schumakercf1f08c2020-04-17 11:00:24 -0400868 * xdr_page_pos - Return the current offset from the start of the xdr pages
869 * @xdr: pointer to struct xdr_stream
870 */
871unsigned int xdr_page_pos(const struct xdr_stream *xdr)
872{
873 unsigned int pos = xdr_stream_pos(xdr);
874
875 WARN_ON(pos < xdr->buf->head[0].iov_len);
876 return pos - xdr->buf->head[0].iov_len;
877}
878EXPORT_SYMBOL_GPL(xdr_page_pos);
879
880/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
882 * @xdr: pointer to xdr_stream struct
883 * @buf: pointer to XDR buffer in which to encode data
884 * @p: current pointer inside XDR buffer
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500885 * @rqst: pointer to controlling rpc_rqst, for debugging
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 *
887 * Note: at the moment the RPC client only passes the length of our
888 * scratch buffer in the xdr_buf's header kvec. Previously this
889 * meant we needed to call xdr_adjust_iovec() after encoding the
890 * data. With the new scheme, the xdr_stream manages the details
891 * of the buffer length, and takes care of adjusting the kvec
892 * length for us.
893 */
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500894void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
895 struct rpc_rqst *rqst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct kvec *iov = buf->head;
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000898 int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Chuck Lever0ae4c3e2020-11-11 15:52:47 -0500900 xdr_reset_scratch_buffer(xdr);
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000901 BUG_ON(scratch_len < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 xdr->buf = buf;
903 xdr->iov = iov;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700904 xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
905 xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000906 BUG_ON(iov->iov_len > scratch_len);
907
908 if (p != xdr->p && p != NULL) {
909 size_t len;
910
911 BUG_ON(p < xdr->p || p > xdr->end);
912 len = (char *)p - (char *)xdr->p;
913 xdr->p = p;
914 buf->len += len;
915 iov->iov_len += len;
916 }
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500917 xdr->rqst = rqst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
Trond Myklebust468039e2008-12-23 15:21:31 -0500919EXPORT_SYMBOL_GPL(xdr_init_encode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921/**
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400922 * xdr_commit_encode - Ensure all data is written to buffer
923 * @xdr: pointer to xdr_stream
924 *
925 * We handle encoding across page boundaries by giving the caller a
926 * temporary location to write to, then later copying the data into
927 * place; xdr_commit_encode does that copying.
928 *
929 * Normally the caller doesn't need to call this directly, as the
930 * following xdr_reserve_space will do it. But an explicit call may be
931 * required at the end of encoding, or any other time when the xdr_buf
932 * data might be read.
933 */
Chuck Lever95bd8302019-08-19 18:37:05 -0400934inline void xdr_commit_encode(struct xdr_stream *xdr)
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400935{
936 int shift = xdr->scratch.iov_len;
937 void *page;
938
939 if (shift == 0)
940 return;
941 page = page_address(*xdr->page_ptr);
942 memcpy(xdr->scratch.iov_base, page, shift);
943 memmove(page, page + shift, (void *)xdr->p - page);
Chuck Lever0ae4c3e2020-11-11 15:52:47 -0500944 xdr_reset_scratch_buffer(xdr);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400945}
946EXPORT_SYMBOL_GPL(xdr_commit_encode);
947
Trond Myklebust22cb4382014-07-12 18:01:02 -0400948static __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
949 size_t nbytes)
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400950{
YueHaibing025911a2018-11-08 02:04:57 +0000951 __be32 *p;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400952 int space_left;
953 int frag1bytes, frag2bytes;
954
955 if (nbytes > PAGE_SIZE)
Chuck Lever55828632019-02-11 11:24:10 -0500956 goto out_overflow; /* Bigger buffers require special handling */
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400957 if (xdr->buf->len + nbytes > xdr->buf->buflen)
Chuck Lever55828632019-02-11 11:24:10 -0500958 goto out_overflow; /* Sorry, we're totally out of space */
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400959 frag1bytes = (xdr->end - xdr->p) << 2;
960 frag2bytes = nbytes - frag1bytes;
961 if (xdr->iov)
962 xdr->iov->iov_len += frag1bytes;
J. Bruce Fields05638dc2014-06-02 12:05:47 -0400963 else
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400964 xdr->buf->page_len += frag1bytes;
J. Bruce Fields05638dc2014-06-02 12:05:47 -0400965 xdr->page_ptr++;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400966 xdr->iov = NULL;
967 /*
968 * If the last encode didn't end exactly on a page boundary, the
969 * next one will straddle boundaries. Encode into the next
970 * page, then copy it back later in xdr_commit_encode. We use
971 * the "scratch" iov to track any temporarily unused fragment of
972 * space at the end of the previous buffer:
973 */
Chuck Lever0ae4c3e2020-11-11 15:52:47 -0500974 xdr_set_scratch_buffer(xdr, xdr->p, frag1bytes);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400975 p = page_address(*xdr->page_ptr);
976 /*
977 * Note this is where the next encode will start after we've
978 * shifted this one back:
979 */
980 xdr->p = (void *)p + frag2bytes;
981 space_left = xdr->buf->buflen - xdr->buf->len;
982 xdr->end = (void *)p + min_t(int, space_left, PAGE_SIZE);
983 xdr->buf->page_len += frag2bytes;
984 xdr->buf->len += nbytes;
985 return p;
Chuck Lever55828632019-02-11 11:24:10 -0500986out_overflow:
987 trace_rpc_xdr_overflow(xdr, nbytes);
988 return NULL;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400989}
990
991/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * xdr_reserve_space - Reserve buffer space for sending
993 * @xdr: pointer to xdr_stream
994 * @nbytes: number of bytes to reserve
995 *
996 * Checks that we have enough buffer space to encode 'nbytes' more
997 * bytes of data. If so, update the total xdr_buf length, and
998 * adjust the length of the current kvec.
999 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001000__be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001002 __be32 *p = xdr->p;
1003 __be32 *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04001005 xdr_commit_encode(xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* align nbytes on the next 32-bit boundary */
1007 nbytes += 3;
1008 nbytes &= ~3;
1009 q = p + (nbytes >> 2);
1010 if (unlikely(q > xdr->end || q < p))
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04001011 return xdr_get_next_encode_buffer(xdr, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 xdr->p = q;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04001013 if (xdr->iov)
1014 xdr->iov->iov_len += nbytes;
1015 else
1016 xdr->buf->page_len += nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 xdr->buf->len += nbytes;
1018 return p;
1019}
Trond Myklebust468039e2008-12-23 15:21:31 -05001020EXPORT_SYMBOL_GPL(xdr_reserve_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Anna Schumaker403217f2020-08-17 12:53:06 -04001022
1023/**
1024 * xdr_reserve_space_vec - Reserves a large amount of buffer space for sending
1025 * @xdr: pointer to xdr_stream
1026 * @vec: pointer to a kvec array
1027 * @nbytes: number of bytes to reserve
1028 *
1029 * Reserves enough buffer space to encode 'nbytes' of data and stores the
1030 * pointers in 'vec'. The size argument passed to xdr_reserve_space() is
1031 * determined based on the number of bytes remaining in the current page to
1032 * avoid invalidating iov_base pointers when xdr_commit_encode() is called.
1033 */
1034int xdr_reserve_space_vec(struct xdr_stream *xdr, struct kvec *vec, size_t nbytes)
1035{
1036 int thislen;
1037 int v = 0;
1038 __be32 *p;
1039
1040 /*
1041 * svcrdma requires every READ payload to start somewhere
1042 * in xdr->pages.
1043 */
1044 if (xdr->iov == xdr->buf->head) {
1045 xdr->iov = NULL;
1046 xdr->end = xdr->p;
1047 }
1048
1049 while (nbytes) {
1050 thislen = xdr->buf->page_len % PAGE_SIZE;
1051 thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen);
1052
1053 p = xdr_reserve_space(xdr, thislen);
1054 if (!p)
1055 return -EIO;
1056
1057 vec[v].iov_base = p;
1058 vec[v].iov_len = thislen;
1059 v++;
1060 nbytes -= thislen;
1061 }
1062
1063 return v;
1064}
1065EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/**
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001068 * xdr_truncate_encode - truncate an encode buffer
1069 * @xdr: pointer to xdr_stream
1070 * @len: new length of buffer
1071 *
1072 * Truncates the xdr stream, so that xdr->buf->len == len,
1073 * and xdr->p points at offset len from the start of the buffer, and
1074 * head, tail, and page lengths are adjusted to correspond.
1075 *
1076 * If this means moving xdr->p to a different buffer, we assume that
Randy Dunlap1cc52132020-08-22 18:07:38 -07001077 * the end pointer should be set to the end of the current page,
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001078 * except in the case of the head buffer when we assume the head
1079 * buffer's current length represents the end of the available buffer.
1080 *
1081 * This is *not* safe to use on a buffer that already has inlined page
1082 * cache pages (as in a zero-copy server read reply), except for the
1083 * simple case of truncating from one position in the tail to another.
1084 *
1085 */
1086void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
1087{
1088 struct xdr_buf *buf = xdr->buf;
1089 struct kvec *head = buf->head;
1090 struct kvec *tail = buf->tail;
1091 int fraglen;
J. Bruce Fields49a068f2014-12-22 16:14:51 -05001092 int new;
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001093
1094 if (len > buf->len) {
1095 WARN_ON_ONCE(1);
1096 return;
1097 }
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04001098 xdr_commit_encode(xdr);
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001099
1100 fraglen = min_t(int, buf->len - len, tail->iov_len);
1101 tail->iov_len -= fraglen;
1102 buf->len -= fraglen;
J. Bruce Fieldsed38c062014-09-19 17:21:35 -04001103 if (tail->iov_len) {
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001104 xdr->p = tail->iov_base + tail->iov_len;
J. Bruce Fields280caac2014-10-01 11:36:31 -04001105 WARN_ON_ONCE(!xdr->end);
1106 WARN_ON_ONCE(!xdr->iov);
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001107 return;
1108 }
1109 WARN_ON_ONCE(fraglen);
1110 fraglen = min_t(int, buf->len - len, buf->page_len);
1111 buf->page_len -= fraglen;
1112 buf->len -= fraglen;
1113
1114 new = buf->page_base + buf->page_len;
J. Bruce Fields49a068f2014-12-22 16:14:51 -05001115
1116 xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001117
J. Bruce Fieldsed38c062014-09-19 17:21:35 -04001118 if (buf->page_len) {
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001119 xdr->p = page_address(*xdr->page_ptr);
1120 xdr->end = (void *)xdr->p + PAGE_SIZE;
1121 xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
J. Bruce Fields280caac2014-10-01 11:36:31 -04001122 WARN_ON_ONCE(xdr->iov);
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001123 return;
1124 }
Frank Sorenson5d7a5bc2018-10-30 15:10:40 -05001125 if (fraglen)
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001126 xdr->end = head->iov_base + head->iov_len;
1127 /* (otherwise assume xdr->end is already set) */
Frank Sorenson5d7a5bc2018-10-30 15:10:40 -05001128 xdr->page_ptr--;
J. Bruce Fields3e19ce72014-02-25 17:44:21 -05001129 head->iov_len = len;
1130 buf->len = len;
1131 xdr->p = head->iov_base + head->iov_len;
1132 xdr->iov = buf->head;
1133}
1134EXPORT_SYMBOL(xdr_truncate_encode);
1135
1136/**
J. Bruce Fieldsdb3f58a2014-03-06 13:22:18 -05001137 * xdr_restrict_buflen - decrease available buffer space
1138 * @xdr: pointer to xdr_stream
1139 * @newbuflen: new maximum number of bytes available
1140 *
1141 * Adjust our idea of how much space is available in the buffer.
1142 * If we've already used too much space in the buffer, returns -1.
1143 * If the available space is already smaller than newbuflen, returns 0
1144 * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
1145 * and ensures xdr->end is set at most offset newbuflen from the start
1146 * of the buffer.
1147 */
1148int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
1149{
1150 struct xdr_buf *buf = xdr->buf;
1151 int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
1152 int end_offset = buf->len + left_in_this_buf;
1153
1154 if (newbuflen < 0 || newbuflen < buf->len)
1155 return -1;
1156 if (newbuflen > buf->buflen)
1157 return 0;
1158 if (newbuflen < end_offset)
1159 xdr->end = (void *)xdr->end + newbuflen - end_offset;
1160 buf->buflen = newbuflen;
1161 return 0;
1162}
1163EXPORT_SYMBOL(xdr_restrict_buflen);
1164
1165/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
1167 * @xdr: pointer to xdr_stream
1168 * @pages: list of pages
1169 * @base: offset of first byte
1170 * @len: length of data in bytes
1171 *
1172 */
1173void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
1174 unsigned int len)
1175{
1176 struct xdr_buf *buf = xdr->buf;
1177 struct kvec *iov = buf->tail;
1178 buf->pages = pages;
1179 buf->page_base = base;
1180 buf->page_len = len;
1181
1182 iov->iov_base = (char *)xdr->p;
1183 iov->iov_len = 0;
1184 xdr->iov = iov;
1185
1186 if (len & 3) {
1187 unsigned int pad = 4 - (len & 3);
1188
1189 BUG_ON(xdr->p >= xdr->end);
1190 iov->iov_base = (char *)xdr->p + (len & 3);
1191 iov->iov_len += pad;
1192 len += pad;
1193 *xdr->p++ = 0;
1194 }
1195 buf->buflen += len;
1196 buf->len += len;
1197}
Trond Myklebust468039e2008-12-23 15:21:31 -05001198EXPORT_SYMBOL_GPL(xdr_write_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Trond Myklebust8d86e372020-11-21 14:50:43 -05001200static unsigned int xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
1201 unsigned int base, unsigned int len)
Trond Myklebust66502392011-01-08 17:45:38 -05001202{
1203 if (len > iov->iov_len)
1204 len = iov->iov_len;
Trond Myklebust8d86e372020-11-21 14:50:43 -05001205 if (unlikely(base > len))
1206 base = len;
1207 xdr->p = (__be32*)(iov->iov_base + base);
Trond Myklebust66502392011-01-08 17:45:38 -05001208 xdr->end = (__be32*)(iov->iov_base + len);
1209 xdr->iov = iov;
1210 xdr->page_ptr = NULL;
Trond Myklebust8d86e372020-11-21 14:50:43 -05001211 return len - base;
Trond Myklebust66502392011-01-08 17:45:38 -05001212}
1213
Trond Myklebust5a5f1c22020-12-08 15:02:14 -05001214static unsigned int xdr_set_tail_base(struct xdr_stream *xdr,
1215 unsigned int base, unsigned int len)
1216{
1217 struct xdr_buf *buf = xdr->buf;
1218
1219 xdr_stream_set_pos(xdr, base + buf->page_len + buf->head->iov_len);
1220 return xdr_set_iov(xdr, buf->tail, base, len);
1221}
1222
Trond Myklebust8d86e372020-11-21 14:50:43 -05001223static unsigned int xdr_set_page_base(struct xdr_stream *xdr,
1224 unsigned int base, unsigned int len)
Trond Myklebust66502392011-01-08 17:45:38 -05001225{
1226 unsigned int pgnr;
1227 unsigned int maxlen;
1228 unsigned int pgoff;
1229 unsigned int pgend;
1230 void *kaddr;
1231
1232 maxlen = xdr->buf->page_len;
Anna Schumaker6d1c0f32021-06-09 17:07:29 -04001233 if (base >= maxlen)
1234 return 0;
1235 else
Trond Myklebust8d86e372020-11-21 14:50:43 -05001236 maxlen -= base;
Trond Myklebust66502392011-01-08 17:45:38 -05001237 if (len > maxlen)
1238 len = maxlen;
1239
Trond Myklebust5a5f1c22020-12-08 15:02:14 -05001240 xdr_stream_page_set_pos(xdr, base);
Trond Myklebust66502392011-01-08 17:45:38 -05001241 base += xdr->buf->page_base;
1242
1243 pgnr = base >> PAGE_SHIFT;
1244 xdr->page_ptr = &xdr->buf->pages[pgnr];
1245 kaddr = page_address(*xdr->page_ptr);
1246
1247 pgoff = base & ~PAGE_MASK;
1248 xdr->p = (__be32*)(kaddr + pgoff);
1249
1250 pgend = pgoff + len;
1251 if (pgend > PAGE_SIZE)
1252 pgend = PAGE_SIZE;
1253 xdr->end = (__be32*)(kaddr + pgend);
1254 xdr->iov = NULL;
Trond Myklebust8d86e372020-11-21 14:50:43 -05001255 return len;
Trond Myklebust66502392011-01-08 17:45:38 -05001256}
1257
Anna Schumakerf7d61ee2015-01-26 17:26:19 -05001258static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
1259 unsigned int len)
1260{
Trond Myklebust02790242020-11-21 21:21:11 -05001261 if (xdr_set_page_base(xdr, base, len) == 0) {
1262 base -= xdr->buf->page_len;
Trond Myklebust5a5f1c22020-12-08 15:02:14 -05001263 xdr_set_tail_base(xdr, base, len);
Trond Myklebust02790242020-11-21 21:21:11 -05001264 }
Anna Schumakerf7d61ee2015-01-26 17:26:19 -05001265}
1266
Trond Myklebust66502392011-01-08 17:45:38 -05001267static void xdr_set_next_page(struct xdr_stream *xdr)
1268{
1269 unsigned int newbase;
1270
1271 newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
1272 newbase -= xdr->buf->page_base;
Trond Myklebust02790242020-11-21 21:21:11 -05001273 if (newbase < xdr->buf->page_len)
1274 xdr_set_page_base(xdr, newbase, xdr_stream_remaining(xdr));
1275 else
Trond Myklebust5a5f1c22020-12-08 15:02:14 -05001276 xdr_set_tail_base(xdr, 0, xdr_stream_remaining(xdr));
Trond Myklebust66502392011-01-08 17:45:38 -05001277}
1278
1279static bool xdr_set_next_buffer(struct xdr_stream *xdr)
1280{
1281 if (xdr->page_ptr != NULL)
1282 xdr_set_next_page(xdr);
Trond Myklebust02790242020-11-21 21:21:11 -05001283 else if (xdr->iov == xdr->buf->head)
1284 xdr_set_page(xdr, 0, xdr_stream_remaining(xdr));
Trond Myklebust66502392011-01-08 17:45:38 -05001285 return xdr->p != xdr->end;
1286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/**
1289 * xdr_init_decode - Initialize an xdr_stream for decoding data.
1290 * @xdr: pointer to xdr_stream struct
1291 * @buf: pointer to XDR buffer from which to decode data
1292 * @p: current pointer inside XDR buffer
Chuck Lever0ccc61b2019-02-11 11:24:05 -05001293 * @rqst: pointer to controlling rpc_rqst, for debugging
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 */
Chuck Lever0ccc61b2019-02-11 11:24:05 -05001295void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
1296 struct rpc_rqst *rqst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 xdr->buf = buf;
Chuck Lever0ae4c3e2020-11-11 15:52:47 -05001299 xdr_reset_scratch_buffer(xdr);
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001300 xdr->nwords = XDR_QUADLEN(buf->len);
Trond Myklebust8d86e372020-11-21 14:50:43 -05001301 if (xdr_set_iov(xdr, buf->head, 0, buf->len) == 0 &&
1302 xdr_set_page_base(xdr, 0, buf->len) == 0)
1303 xdr_set_iov(xdr, buf->tail, 0, buf->len);
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001304 if (p != NULL && p > xdr->p && xdr->end >= p) {
1305 xdr->nwords -= p - xdr->p;
Trond Myklebust15376932012-06-28 17:17:48 -04001306 xdr->p = p;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001307 }
Chuck Lever0ccc61b2019-02-11 11:24:05 -05001308 xdr->rqst = rqst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
Trond Myklebust468039e2008-12-23 15:21:31 -05001310EXPORT_SYMBOL_GPL(xdr_init_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Benny Halevyf7da7a12011-05-19 14:16:47 -04001312/**
Chuck Lever7ecce752017-04-11 13:23:59 -04001313 * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
Benny Halevyf7da7a12011-05-19 14:16:47 -04001314 * @xdr: pointer to xdr_stream struct
1315 * @buf: pointer to XDR buffer from which to decode data
1316 * @pages: list of pages to decode into
1317 * @len: length in bytes of buffer in pages
1318 */
1319void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
1320 struct page **pages, unsigned int len)
1321{
1322 memset(buf, 0, sizeof(*buf));
1323 buf->pages = pages;
1324 buf->page_len = len;
1325 buf->buflen = len;
1326 buf->len = len;
Chuck Lever0ccc61b2019-02-11 11:24:05 -05001327 xdr_init_decode(xdr, buf, NULL, NULL);
Benny Halevyf7da7a12011-05-19 14:16:47 -04001328}
1329EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
1330
Trond Myklebust66502392011-01-08 17:45:38 -05001331static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
Trond Myklebustba8e4522010-10-19 19:58:49 -04001332{
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001333 unsigned int nwords = XDR_QUADLEN(nbytes);
Trond Myklebustba8e4522010-10-19 19:58:49 -04001334 __be32 *p = xdr->p;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001335 __be32 *q = p + nwords;
Trond Myklebustba8e4522010-10-19 19:58:49 -04001336
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001337 if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
Trond Myklebustba8e4522010-10-19 19:58:49 -04001338 return NULL;
Trond Myklebust66502392011-01-08 17:45:38 -05001339 xdr->p = q;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001340 xdr->nwords -= nwords;
Trond Myklebustba8e4522010-10-19 19:58:49 -04001341 return p;
1342}
Trond Myklebustba8e4522010-10-19 19:58:49 -04001343
Trond Myklebust66502392011-01-08 17:45:38 -05001344static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
1345{
1346 __be32 *p;
Trond Myklebustace0e142016-09-20 14:33:42 -04001347 char *cpdest = xdr->scratch.iov_base;
Trond Myklebust66502392011-01-08 17:45:38 -05001348 size_t cplen = (char *)xdr->end - (char *)xdr->p;
1349
1350 if (nbytes > xdr->scratch.iov_len)
Chuck Lever55828632019-02-11 11:24:10 -05001351 goto out_overflow;
Trond Myklebustace0e142016-09-20 14:33:42 -04001352 p = __xdr_inline_decode(xdr, cplen);
1353 if (p == NULL)
1354 return NULL;
1355 memcpy(cpdest, p, cplen);
Chuck Lever55828632019-02-11 11:24:10 -05001356 if (!xdr_set_next_buffer(xdr))
1357 goto out_overflow;
Trond Myklebust66502392011-01-08 17:45:38 -05001358 cpdest += cplen;
1359 nbytes -= cplen;
Trond Myklebust66502392011-01-08 17:45:38 -05001360 p = __xdr_inline_decode(xdr, nbytes);
1361 if (p == NULL)
1362 return NULL;
1363 memcpy(cpdest, p, nbytes);
1364 return xdr->scratch.iov_base;
Chuck Lever55828632019-02-11 11:24:10 -05001365out_overflow:
1366 trace_rpc_xdr_overflow(xdr, nbytes);
1367 return NULL;
Trond Myklebust66502392011-01-08 17:45:38 -05001368}
1369
1370/**
1371 * xdr_inline_decode - Retrieve XDR data to decode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * @xdr: pointer to xdr_stream struct
1373 * @nbytes: number of bytes of data to decode
1374 *
1375 * Check if the input buffer is long enough to enable us to decode
1376 * 'nbytes' more bytes of data starting at the current position.
1377 * If so return the current pointer, then update the current
1378 * pointer position.
1379 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001380__be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Trond Myklebust66502392011-01-08 17:45:38 -05001382 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Chuck Lever55828632019-02-11 11:24:10 -05001384 if (unlikely(nbytes == 0))
Trond Myklebust66502392011-01-08 17:45:38 -05001385 return xdr->p;
1386 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
Chuck Lever55828632019-02-11 11:24:10 -05001387 goto out_overflow;
Trond Myklebust66502392011-01-08 17:45:38 -05001388 p = __xdr_inline_decode(xdr, nbytes);
1389 if (p != NULL)
1390 return p;
1391 return xdr_copy_to_scratch(xdr, nbytes);
Chuck Lever55828632019-02-11 11:24:10 -05001392out_overflow:
1393 trace_rpc_xdr_overflow(xdr, nbytes);
1394 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
Trond Myklebust468039e2008-12-23 15:21:31 -05001396EXPORT_SYMBOL_GPL(xdr_inline_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Anna Schumaker06216ec2020-04-20 17:38:17 -04001398static void xdr_realign_pages(struct xdr_stream *xdr)
1399{
1400 struct xdr_buf *buf = xdr->buf;
1401 struct kvec *iov = buf->head;
1402 unsigned int cur = xdr_stream_pos(xdr);
Trond Myklebust6707fbd2020-12-06 10:15:04 -05001403 unsigned int copied;
Anna Schumaker06216ec2020-04-20 17:38:17 -04001404
1405 /* Realign pages to current pointer position */
1406 if (iov->iov_len > cur) {
Trond Myklebust6707fbd2020-12-06 10:15:04 -05001407 copied = xdr_shrink_bufhead(buf, cur);
1408 trace_rpc_xdr_alignment(xdr, cur, copied);
Trond Myklebust5a5f1c22020-12-08 15:02:14 -05001409 xdr_set_page(xdr, 0, buf->page_len);
Anna Schumaker06216ec2020-04-20 17:38:17 -04001410 }
1411}
1412
Trond Myklebust3994ee62012-06-26 12:34:05 -04001413static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 struct xdr_buf *buf = xdr->buf;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001416 unsigned int nwords = XDR_QUADLEN(len);
Trond Myklebustc4f2f592020-12-04 17:15:09 -05001417 unsigned int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001419 if (xdr->nwords == 0)
Trond Myklebustc337d362012-06-21 17:05:37 -04001420 return 0;
Chuck Lever7be9cea32019-02-11 11:24:16 -05001421
Anna Schumaker06216ec2020-04-20 17:38:17 -04001422 xdr_realign_pages(xdr);
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001423 if (nwords > xdr->nwords) {
1424 nwords = xdr->nwords;
1425 len = nwords << 2;
1426 }
Trond Myklebusta11a2bf2012-08-02 13:21:43 -04001427 if (buf->page_len <= len)
Trond Myklebust8a9a8b82012-08-01 14:32:13 -04001428 len = buf->page_len;
Trond Myklebusta11a2bf2012-08-02 13:21:43 -04001429 else if (nwords < xdr->nwords) {
1430 /* Truncate page data and move it into the tail */
Trond Myklebustc4f2f592020-12-04 17:15:09 -05001431 copied = xdr_shrink_pagelen(buf, len);
1432 trace_rpc_xdr_alignment(xdr, len, copied);
Trond Myklebusta11a2bf2012-08-02 13:21:43 -04001433 }
Trond Myklebust3994ee62012-06-26 12:34:05 -04001434 return len;
1435}
Trond Myklebustbd00f842012-06-26 13:50:43 -04001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/**
Trond Myklebust1d973162020-11-20 16:31:03 -05001438 * xdr_read_pages - align page-based XDR data to current pointer position
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * @xdr: pointer to xdr_stream struct
1440 * @len: number of bytes of page data
1441 *
1442 * Moves data beyond the current pointer position from the XDR head[] buffer
Trond Myklebust1d973162020-11-20 16:31:03 -05001443 * into the page list. Any data that lies beyond current position + @len
1444 * bytes is moved into the XDR tail[]. The xdr_stream current position is
1445 * then advanced past that data to align to the next XDR object in the tail.
Trond Myklebust3994ee62012-06-26 12:34:05 -04001446 *
1447 * Returns the number of XDR encoded bytes now contained in the pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
Trond Myklebust3994ee62012-06-26 12:34:05 -04001449unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Trond Myklebust1d973162020-11-20 16:31:03 -05001451 unsigned int nwords = XDR_QUADLEN(len);
1452 unsigned int base, end, pglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Trond Myklebust1d973162020-11-20 16:31:03 -05001454 pglen = xdr_align_pages(xdr, nwords << 2);
1455 if (pglen == 0)
Trond Myklebust3994ee62012-06-26 12:34:05 -04001456 return 0;
Trond Myklebustbd00f842012-06-26 13:50:43 -04001457
Trond Myklebust1d973162020-11-20 16:31:03 -05001458 base = (nwords << 2) - pglen;
1459 end = xdr_stream_remaining(xdr) - pglen;
1460
Trond Myklebust5a5f1c22020-12-08 15:02:14 -05001461 xdr_set_tail_base(xdr, base, end);
Trond Myklebust1d973162020-11-20 16:31:03 -05001462 return len <= pglen ? len : pglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
Trond Myklebust468039e2008-12-23 15:21:31 -05001464EXPORT_SYMBOL_GPL(xdr_read_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Trond Myklebust9a20f6f2020-12-04 15:16:46 -05001466unsigned int xdr_align_data(struct xdr_stream *xdr, unsigned int offset,
1467 unsigned int length)
Anna Schumakere6ac0ac2020-04-21 11:27:00 -04001468{
1469 struct xdr_buf *buf = xdr->buf;
Trond Myklebust9a20f6f2020-12-04 15:16:46 -05001470 unsigned int from, bytes, len;
1471 unsigned int shift;
Anna Schumakere6ac0ac2020-04-21 11:27:00 -04001472
1473 xdr_realign_pages(xdr);
1474 from = xdr_page_pos(xdr);
Trond Myklebust9a20f6f2020-12-04 15:16:46 -05001475
1476 if (from >= buf->page_len + buf->tail->iov_len)
1477 return 0;
1478 if (from + buf->head->iov_len >= buf->len)
1479 return 0;
1480
1481 len = buf->len - buf->head->iov_len;
1482
1483 /* We only shift data left! */
1484 if (WARN_ONCE(from < offset, "SUNRPC: misaligned data src=%u dst=%u\n",
1485 from, offset))
1486 return 0;
1487 if (WARN_ONCE(offset > buf->page_len,
1488 "SUNRPC: buffer overflow. offset=%u, page_len=%u\n",
1489 offset, buf->page_len))
1490 return 0;
Anna Schumakere6ac0ac2020-04-21 11:27:00 -04001491
1492 /* Move page data to the left */
Trond Myklebust9a20f6f2020-12-04 15:16:46 -05001493 shift = from - offset;
1494 xdr_buf_pages_shift_left(buf, from, len, shift);
Anna Schumakere6ac0ac2020-04-21 11:27:00 -04001495
Trond Myklebust9a20f6f2020-12-04 15:16:46 -05001496 bytes = xdr_stream_remaining(xdr);
1497 if (length > bytes)
1498 length = bytes;
1499 bytes -= length;
Anna Schumakere6ac0ac2020-04-21 11:27:00 -04001500
Trond Myklebust5a5f1c22020-12-08 15:02:14 -05001501 xdr->buf->len -= shift;
Trond Myklebust9a20f6f2020-12-04 15:16:46 -05001502 xdr_set_page(xdr, offset + length, bytes);
Anna Schumakere6ac0ac2020-04-21 11:27:00 -04001503 return length;
1504}
1505EXPORT_SYMBOL_GPL(xdr_align_data);
1506
Trond Myklebustc4f2f592020-12-04 17:15:09 -05001507unsigned int xdr_expand_hole(struct xdr_stream *xdr, unsigned int offset,
1508 unsigned int length)
Anna Schumaker84ce1822014-05-28 13:38:53 -04001509{
1510 struct xdr_buf *buf = xdr->buf;
Trond Myklebustc4f2f592020-12-04 17:15:09 -05001511 unsigned int from, to, shift;
Anna Schumaker84ce1822014-05-28 13:38:53 -04001512
1513 xdr_realign_pages(xdr);
1514 from = xdr_page_pos(xdr);
Trond Myklebustc4f2f592020-12-04 17:15:09 -05001515 to = xdr_align_size(offset + length);
Anna Schumaker84ce1822014-05-28 13:38:53 -04001516
Trond Myklebustc4f2f592020-12-04 17:15:09 -05001517 /* Could the hole be behind us? */
1518 if (to > from) {
1519 unsigned int buflen = buf->len - buf->head->iov_len;
1520 shift = to - from;
1521 xdr_buf_try_expand(buf, shift);
1522 xdr_buf_pages_shift_right(buf, from, buflen, shift);
Trond Myklebust5a5f1c22020-12-08 15:02:14 -05001523 xdr_set_page(xdr, to, xdr_stream_remaining(xdr));
Trond Myklebustc4f2f592020-12-04 17:15:09 -05001524 } else if (to != from)
1525 xdr_align_data(xdr, to, 0);
1526 xdr_buf_pages_zero(buf, offset, length);
Anna Schumaker84ce1822014-05-28 13:38:53 -04001527
Anna Schumaker84ce1822014-05-28 13:38:53 -04001528 return length;
1529}
1530EXPORT_SYMBOL_GPL(xdr_expand_hole);
1531
Trond Myklebust8b23ea72006-06-09 09:34:21 -04001532/**
1533 * xdr_enter_page - decode data from the XDR page
1534 * @xdr: pointer to xdr_stream struct
1535 * @len: number of bytes of page data
1536 *
1537 * Moves data beyond the current pointer position from the XDR head[] buffer
1538 * into the page list. Any data that lies beyond current position + "len"
1539 * bytes is moved into the XDR tail[]. The current pointer is then
1540 * repositioned at the beginning of the first XDR page.
1541 */
1542void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1543{
Trond Myklebustf8bb7f02012-06-21 14:53:10 -04001544 len = xdr_align_pages(xdr, len);
Trond Myklebust8b23ea72006-06-09 09:34:21 -04001545 /*
1546 * Position current pointer at beginning of tail, and
1547 * set remaining message length.
1548 */
Trond Myklebustf8bb7f02012-06-21 14:53:10 -04001549 if (len != 0)
1550 xdr_set_page_base(xdr, 0, len);
Trond Myklebust8b23ea72006-06-09 09:34:21 -04001551}
Trond Myklebust468039e2008-12-23 15:21:31 -05001552EXPORT_SYMBOL_GPL(xdr_enter_page);
Trond Myklebust8b23ea72006-06-09 09:34:21 -04001553
Julia Lawallc2bd2c02020-01-01 08:43:30 +01001554static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Trond Myklebustf8d0e602020-12-08 22:56:18 -05001556void xdr_buf_from_iov(const struct kvec *iov, struct xdr_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 buf->head[0] = *iov;
1559 buf->tail[0] = empty_iov;
1560 buf->page_len = 0;
1561 buf->buflen = buf->len = iov->iov_len;
1562}
Trond Myklebust468039e2008-12-23 15:21:31 -05001563EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
J. Bruce Fieldsde4aee22014-02-25 17:21:08 -05001565/**
1566 * xdr_buf_subsegment - set subbuf to a portion of buf
1567 * @buf: an xdr buffer
1568 * @subbuf: the result buffer
1569 * @base: beginning of range in bytes
1570 * @len: length of range in bytes
1571 *
1572 * sets @subbuf to an xdr buffer representing the portion of @buf of
1573 * length @len starting at offset @base.
1574 *
1575 * @buf and @subbuf may be pointers to the same struct xdr_buf.
1576 *
1577 * Returns -1 if base of length are out of bounds.
1578 */
Chuck Lever5a7e7022020-03-13 10:42:09 -04001579int xdr_buf_subsegment(const struct xdr_buf *buf, struct xdr_buf *subbuf,
1580 unsigned int base, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 subbuf->buflen = subbuf->len = len;
Trond Myklebust1e789572006-08-31 15:09:19 -04001583 if (base < buf->head[0].iov_len) {
1584 subbuf->head[0].iov_base = buf->head[0].iov_base + base;
1585 subbuf->head[0].iov_len = min_t(unsigned int, len,
1586 buf->head[0].iov_len - base);
1587 len -= subbuf->head[0].iov_len;
1588 base = 0;
1589 } else {
Trond Myklebust1e789572006-08-31 15:09:19 -04001590 base -= buf->head[0].iov_len;
Chuck Lever89a3c9f2020-06-25 11:32:34 -04001591 subbuf->head[0].iov_base = buf->head[0].iov_base;
J. Bruce Fieldsde4aee22014-02-25 17:21:08 -05001592 subbuf->head[0].iov_len = 0;
Trond Myklebust1e789572006-08-31 15:09:19 -04001593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 if (base < buf->page_len) {
Trond Myklebust1e789572006-08-31 15:09:19 -04001596 subbuf->page_len = min(buf->page_len - base, len);
1597 base += buf->page_base;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001598 subbuf->page_base = base & ~PAGE_MASK;
1599 subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 len -= subbuf->page_len;
1601 base = 0;
1602 } else {
1603 base -= buf->page_len;
Chuck Lever89a3c9f2020-06-25 11:32:34 -04001604 subbuf->pages = buf->pages;
1605 subbuf->page_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 subbuf->page_len = 0;
1607 }
1608
Trond Myklebust1e789572006-08-31 15:09:19 -04001609 if (base < buf->tail[0].iov_len) {
1610 subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
1611 subbuf->tail[0].iov_len = min_t(unsigned int, len,
1612 buf->tail[0].iov_len - base);
1613 len -= subbuf->tail[0].iov_len;
1614 base = 0;
1615 } else {
Trond Myklebust1e789572006-08-31 15:09:19 -04001616 base -= buf->tail[0].iov_len;
Chuck Lever89a3c9f2020-06-25 11:32:34 -04001617 subbuf->tail[0].iov_base = buf->tail[0].iov_base;
J. Bruce Fieldsde4aee22014-02-25 17:21:08 -05001618 subbuf->tail[0].iov_len = 0;
Trond Myklebust1e789572006-08-31 15:09:19 -04001619 }
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (base || len)
1622 return -1;
1623 return 0;
1624}
Trond Myklebust468039e2008-12-23 15:21:31 -05001625EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Chuck Lever0a8e7b72020-04-15 17:36:22 -04001627/**
Chuck Leverc1346a122020-11-03 11:54:23 -05001628 * xdr_stream_subsegment - set @subbuf to a portion of @xdr
1629 * @xdr: an xdr_stream set up for decoding
1630 * @subbuf: the result buffer
1631 * @nbytes: length of @xdr to extract, in bytes
1632 *
1633 * Sets up @subbuf to represent a portion of @xdr. The portion
1634 * starts at the current offset in @xdr, and extends for a length
1635 * of @nbytes. If this is successful, @xdr is advanced to the next
Chuck Leverf49b68d2021-09-30 17:06:15 -04001636 * XDR data item following that portion.
Chuck Leverc1346a122020-11-03 11:54:23 -05001637 *
1638 * Return values:
1639 * %true: @subbuf has been initialized, and @xdr has been advanced.
1640 * %false: a bounds error has occurred
1641 */
1642bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
1643 unsigned int nbytes)
1644{
Chuck Leverf49b68d2021-09-30 17:06:15 -04001645 unsigned int start = xdr_stream_pos(xdr);
1646 unsigned int remaining, len;
Chuck Leverc1346a122020-11-03 11:54:23 -05001647
Chuck Leverf49b68d2021-09-30 17:06:15 -04001648 /* Extract @subbuf and bounds-check the fn arguments */
1649 if (xdr_buf_subsegment(xdr->buf, subbuf, start, nbytes))
Chuck Leverc1346a122020-11-03 11:54:23 -05001650 return false;
1651
Chuck Leverf49b68d2021-09-30 17:06:15 -04001652 /* Advance @xdr by @nbytes */
1653 for (remaining = nbytes; remaining;) {
Chuck Leverc1346a122020-11-03 11:54:23 -05001654 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1655 return false;
Chuck Leverc1346a122020-11-03 11:54:23 -05001656
Chuck Leverf49b68d2021-09-30 17:06:15 -04001657 len = (char *)xdr->end - (char *)xdr->p;
1658 if (remaining <= len) {
1659 xdr->p = (__be32 *)((char *)xdr->p +
1660 (remaining + xdr_pad_size(nbytes)));
1661 break;
1662 }
1663
1664 xdr->p = (__be32 *)((char *)xdr->p + len);
1665 xdr->end = xdr->p;
Chuck Leverc1346a122020-11-03 11:54:23 -05001666 remaining -= len;
Chuck Leverc1346a122020-11-03 11:54:23 -05001667 }
1668
Chuck Leverf49b68d2021-09-30 17:06:15 -04001669 xdr_stream_set_pos(xdr, start + nbytes);
Chuck Leverc1346a122020-11-03 11:54:23 -05001670 return true;
1671}
1672EXPORT_SYMBOL_GPL(xdr_stream_subsegment);
1673
1674/**
Chuck Lever0a8e7b72020-04-15 17:36:22 -04001675 * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
1676 * @buf: buf to be trimmed
1677 * @len: number of bytes to reduce "buf" by
1678 *
1679 * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
1680 * that it's possible that we'll trim less than that amount if the xdr_buf is
1681 * too small, or if (for instance) it's all in the head and the parser has
1682 * already read too far into it.
1683 */
1684void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
1685{
1686 size_t cur;
1687 unsigned int trim = len;
1688
1689 if (buf->tail[0].iov_len) {
1690 cur = min_t(size_t, buf->tail[0].iov_len, trim);
1691 buf->tail[0].iov_len -= cur;
1692 trim -= cur;
1693 if (!trim)
1694 goto fix_len;
1695 }
1696
1697 if (buf->page_len) {
1698 cur = min_t(unsigned int, buf->page_len, trim);
1699 buf->page_len -= cur;
1700 trim -= cur;
1701 if (!trim)
1702 goto fix_len;
1703 }
1704
1705 if (buf->head[0].iov_len) {
1706 cur = min_t(size_t, buf->head[0].iov_len, trim);
1707 buf->head[0].iov_len -= cur;
1708 trim -= cur;
1709 }
1710fix_len:
1711 buf->len -= (len - trim);
1712}
1713EXPORT_SYMBOL_GPL(xdr_buf_trim);
1714
Trond Myklebustf8d0e602020-12-08 22:56:18 -05001715static void __read_bytes_from_xdr_buf(const struct xdr_buf *subbuf,
1716 void *obj, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Trond Myklebust1e789572006-08-31 15:09:19 -04001718 unsigned int this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001720 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1721 memcpy(obj, subbuf->head[0].iov_base, this_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 len -= this_len;
1723 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001724 this_len = min_t(unsigned int, len, subbuf->page_len);
Trond Myklebuste43ac222020-12-06 12:41:41 -05001725 _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 len -= this_len;
1727 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001728 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1729 memcpy(obj, subbuf->tail[0].iov_base, this_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001732/* obj is assumed to point to allocated memory of size at least len: */
Trond Myklebustf8d0e602020-12-08 22:56:18 -05001733int read_bytes_from_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1734 void *obj, unsigned int len)
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001735{
1736 struct xdr_buf subbuf;
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001737 int status;
1738
1739 status = xdr_buf_subsegment(buf, &subbuf, base, len);
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001740 if (status != 0)
1741 return status;
1742 __read_bytes_from_xdr_buf(&subbuf, obj, len);
1743 return 0;
1744}
Trond Myklebust468039e2008-12-23 15:21:31 -05001745EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001746
Trond Myklebustf8d0e602020-12-08 22:56:18 -05001747static void __write_bytes_to_xdr_buf(const struct xdr_buf *subbuf,
1748 void *obj, unsigned int len)
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001749{
1750 unsigned int this_len;
1751
1752 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1753 memcpy(subbuf->head[0].iov_base, obj, this_len);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001754 len -= this_len;
1755 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001756 this_len = min_t(unsigned int, len, subbuf->page_len);
Trond Myklebuste43ac222020-12-06 12:41:41 -05001757 _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001758 len -= this_len;
1759 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001760 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1761 memcpy(subbuf->tail[0].iov_base, obj, this_len);
1762}
1763
1764/* obj is assumed to point to allocated memory of size at least len: */
Trond Myklebustf8d0e602020-12-08 22:56:18 -05001765int write_bytes_to_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1766 void *obj, unsigned int len)
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001767{
1768 struct xdr_buf subbuf;
1769 int status;
1770
1771 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1772 if (status != 0)
1773 return status;
1774 __write_bytes_to_xdr_buf(&subbuf, obj, len);
1775 return 0;
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001776}
Kevin Coffmanc43abae2010-03-17 13:02:58 -04001777EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001778
Trond Myklebustf8d0e602020-12-08 22:56:18 -05001779int xdr_decode_word(const struct xdr_buf *buf, unsigned int base, u32 *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001781 __be32 raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 int status;
1783
1784 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
1785 if (status)
1786 return status;
Benny Halevy98866b52009-08-14 17:18:49 +03001787 *obj = be32_to_cpu(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return 0;
1789}
Trond Myklebust468039e2008-12-23 15:21:31 -05001790EXPORT_SYMBOL_GPL(xdr_decode_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Trond Myklebustf8d0e602020-12-08 22:56:18 -05001792int xdr_encode_word(const struct xdr_buf *buf, unsigned int base, u32 obj)
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001793{
Benny Halevy9f162d22009-08-14 17:18:44 +03001794 __be32 raw = cpu_to_be32(obj);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001795
1796 return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
1797}
Trond Myklebust468039e2008-12-23 15:21:31 -05001798EXPORT_SYMBOL_GPL(xdr_encode_word);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001799
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001800/* Returns 0 on success, or else a negative error code. */
Trond Myklebustf8d0e602020-12-08 22:56:18 -05001801static int xdr_xcode_array2(const struct xdr_buf *buf, unsigned int base,
1802 struct xdr_array2_desc *desc, int encode)
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001803{
1804 char *elem = NULL, *c;
1805 unsigned int copied = 0, todo, avail_here;
1806 struct page **ppages = NULL;
1807 int err;
1808
1809 if (encode) {
1810 if (xdr_encode_word(buf, base, desc->array_len) != 0)
1811 return -EINVAL;
1812 } else {
1813 if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
Trond Myklebust58fcb8d2005-08-10 18:15:12 -04001814 desc->array_len > desc->array_maxlen ||
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001815 (unsigned long) base + 4 + desc->array_len *
1816 desc->elem_size > buf->len)
1817 return -EINVAL;
1818 }
1819 base += 4;
1820
1821 if (!desc->xcode)
1822 return 0;
1823
1824 todo = desc->array_len * desc->elem_size;
1825
1826 /* process head */
1827 if (todo && base < buf->head->iov_len) {
1828 c = buf->head->iov_base + base;
1829 avail_here = min_t(unsigned int, todo,
1830 buf->head->iov_len - base);
1831 todo -= avail_here;
1832
1833 while (avail_here >= desc->elem_size) {
1834 err = desc->xcode(desc, c);
1835 if (err)
1836 goto out;
1837 c += desc->elem_size;
1838 avail_here -= desc->elem_size;
1839 }
1840 if (avail_here) {
1841 if (!elem) {
1842 elem = kmalloc(desc->elem_size, GFP_KERNEL);
1843 err = -ENOMEM;
1844 if (!elem)
1845 goto out;
1846 }
1847 if (encode) {
1848 err = desc->xcode(desc, elem);
1849 if (err)
1850 goto out;
1851 memcpy(c, elem, avail_here);
1852 } else
1853 memcpy(elem, c, avail_here);
1854 copied = avail_here;
1855 }
1856 base = buf->head->iov_len; /* align to start of pages */
1857 }
1858
1859 /* process pages array */
1860 base -= buf->head->iov_len;
1861 if (todo && base < buf->page_len) {
1862 unsigned int avail_page;
1863
1864 avail_here = min(todo, buf->page_len - base);
1865 todo -= avail_here;
1866
1867 base += buf->page_base;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001868 ppages = buf->pages + (base >> PAGE_SHIFT);
1869 base &= ~PAGE_MASK;
1870 avail_page = min_t(unsigned int, PAGE_SIZE - base,
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001871 avail_here);
1872 c = kmap(*ppages) + base;
1873
1874 while (avail_here) {
1875 avail_here -= avail_page;
1876 if (copied || avail_page < desc->elem_size) {
1877 unsigned int l = min(avail_page,
1878 desc->elem_size - copied);
1879 if (!elem) {
1880 elem = kmalloc(desc->elem_size,
1881 GFP_KERNEL);
1882 err = -ENOMEM;
1883 if (!elem)
1884 goto out;
1885 }
1886 if (encode) {
1887 if (!copied) {
1888 err = desc->xcode(desc, elem);
1889 if (err)
1890 goto out;
1891 }
1892 memcpy(c, elem + copied, l);
1893 copied += l;
1894 if (copied == desc->elem_size)
1895 copied = 0;
1896 } else {
1897 memcpy(elem + copied, c, l);
1898 copied += l;
1899 if (copied == desc->elem_size) {
1900 err = desc->xcode(desc, elem);
1901 if (err)
1902 goto out;
1903 copied = 0;
1904 }
1905 }
1906 avail_page -= l;
1907 c += l;
1908 }
1909 while (avail_page >= desc->elem_size) {
1910 err = desc->xcode(desc, c);
1911 if (err)
1912 goto out;
1913 c += desc->elem_size;
1914 avail_page -= desc->elem_size;
1915 }
1916 if (avail_page) {
1917 unsigned int l = min(avail_page,
1918 desc->elem_size - copied);
1919 if (!elem) {
1920 elem = kmalloc(desc->elem_size,
1921 GFP_KERNEL);
1922 err = -ENOMEM;
1923 if (!elem)
1924 goto out;
1925 }
1926 if (encode) {
1927 if (!copied) {
1928 err = desc->xcode(desc, elem);
1929 if (err)
1930 goto out;
1931 }
1932 memcpy(c, elem + copied, l);
1933 copied += l;
1934 if (copied == desc->elem_size)
1935 copied = 0;
1936 } else {
1937 memcpy(elem + copied, c, l);
1938 copied += l;
1939 if (copied == desc->elem_size) {
1940 err = desc->xcode(desc, elem);
1941 if (err)
1942 goto out;
1943 copied = 0;
1944 }
1945 }
1946 }
1947 if (avail_here) {
1948 kunmap(*ppages);
1949 ppages++;
1950 c = kmap(*ppages);
1951 }
1952
1953 avail_page = min(avail_here,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001954 (unsigned int) PAGE_SIZE);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001955 }
1956 base = buf->page_len; /* align to start of tail */
1957 }
1958
1959 /* process tail */
1960 base -= buf->page_len;
1961 if (todo) {
1962 c = buf->tail->iov_base + base;
1963 if (copied) {
1964 unsigned int l = desc->elem_size - copied;
1965
1966 if (encode)
1967 memcpy(c, elem + copied, l);
1968 else {
1969 memcpy(elem + copied, c, l);
1970 err = desc->xcode(desc, elem);
1971 if (err)
1972 goto out;
1973 }
1974 todo -= l;
1975 c += l;
1976 }
1977 while (todo) {
1978 err = desc->xcode(desc, c);
1979 if (err)
1980 goto out;
1981 c += desc->elem_size;
1982 todo -= desc->elem_size;
1983 }
1984 }
1985 err = 0;
1986
1987out:
Jesper Juhla51482b2005-11-08 09:41:34 -08001988 kfree(elem);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001989 if (ppages)
1990 kunmap(*ppages);
1991 return err;
1992}
1993
Trond Myklebustf8d0e602020-12-08 22:56:18 -05001994int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
1995 struct xdr_array2_desc *desc)
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001996{
1997 if (base >= buf->len)
1998 return -EINVAL;
1999
2000 return xdr_xcode_array2(buf, base, desc, 0);
2001}
Trond Myklebust468039e2008-12-23 15:21:31 -05002002EXPORT_SYMBOL_GPL(xdr_decode_array2);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00002003
Trond Myklebustf8d0e602020-12-08 22:56:18 -05002004int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
2005 struct xdr_array2_desc *desc)
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00002006{
2007 if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
2008 buf->head->iov_len + buf->page_len + buf->tail->iov_len)
2009 return -EINVAL;
2010
2011 return xdr_xcode_array2(buf, base, desc, 1);
2012}
Trond Myklebust468039e2008-12-23 15:21:31 -05002013EXPORT_SYMBOL_GPL(xdr_encode_array2);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05002014
Trond Myklebustf8d0e602020-12-08 22:56:18 -05002015int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset,
2016 unsigned int len,
2017 int (*actor)(struct scatterlist *, void *), void *data)
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05002018{
2019 int i, ret = 0;
Eric Dumazet95c96172012-04-15 05:58:06 +00002020 unsigned int page_len, thislen, page_offset;
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05002021 struct scatterlist sg[1];
2022
Herbert Xu68e3f5d2007-10-27 00:52:07 -07002023 sg_init_table(sg, 1);
2024
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05002025 if (offset >= buf->head[0].iov_len) {
2026 offset -= buf->head[0].iov_len;
2027 } else {
2028 thislen = buf->head[0].iov_len - offset;
2029 if (thislen > len)
2030 thislen = len;
2031 sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
2032 ret = actor(sg, data);
2033 if (ret)
2034 goto out;
2035 offset = 0;
2036 len -= thislen;
2037 }
2038 if (len == 0)
2039 goto out;
2040
2041 if (offset >= buf->page_len) {
2042 offset -= buf->page_len;
2043 } else {
2044 page_len = buf->page_len - offset;
2045 if (page_len > len)
2046 page_len = len;
2047 len -= page_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002048 page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
2049 i = (offset + buf->page_base) >> PAGE_SHIFT;
2050 thislen = PAGE_SIZE - page_offset;
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05002051 do {
2052 if (thislen > page_len)
2053 thislen = page_len;
Jens Axboe642f149032007-10-24 11:20:47 +02002054 sg_set_page(sg, buf->pages[i], thislen, page_offset);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05002055 ret = actor(sg, data);
2056 if (ret)
2057 goto out;
2058 page_len -= thislen;
2059 i++;
2060 page_offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002061 thislen = PAGE_SIZE;
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05002062 } while (page_len != 0);
2063 offset = 0;
2064 }
2065 if (len == 0)
2066 goto out;
2067 if (offset < buf->tail[0].iov_len) {
2068 thislen = buf->tail[0].iov_len - offset;
2069 if (thislen > len)
2070 thislen = len;
2071 sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
2072 ret = actor(sg, data);
2073 len -= thislen;
2074 }
2075 if (len != 0)
2076 ret = -EINVAL;
2077out:
2078 return ret;
2079}
Trond Myklebust468039e2008-12-23 15:21:31 -05002080EXPORT_SYMBOL_GPL(xdr_process_buf);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05002081
Trond Myklebust5c741d42017-02-19 16:08:31 -05002082/**
Trond Myklebust0e779aa2018-03-20 17:03:05 -04002083 * xdr_stream_decode_opaque - Decode variable length opaque
2084 * @xdr: pointer to xdr_stream
2085 * @ptr: location to store opaque data
2086 * @size: size of storage buffer @ptr
2087 *
2088 * Return values:
2089 * On success, returns size of object stored in *@ptr
2090 * %-EBADMSG on XDR buffer overflow
2091 * %-EMSGSIZE on overflow of storage buffer @ptr
2092 */
2093ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
2094{
2095 ssize_t ret;
2096 void *p;
2097
2098 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2099 if (ret <= 0)
2100 return ret;
2101 memcpy(ptr, p, ret);
2102 return ret;
2103}
2104EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
2105
2106/**
2107 * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
2108 * @xdr: pointer to xdr_stream
2109 * @ptr: location to store pointer to opaque data
2110 * @maxlen: maximum acceptable object size
2111 * @gfp_flags: GFP mask to use
2112 *
2113 * Return values:
2114 * On success, returns size of object stored in *@ptr
2115 * %-EBADMSG on XDR buffer overflow
2116 * %-EMSGSIZE if the size of the object would exceed @maxlen
2117 * %-ENOMEM on memory allocation failure
2118 */
2119ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
2120 size_t maxlen, gfp_t gfp_flags)
2121{
2122 ssize_t ret;
2123 void *p;
2124
2125 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2126 if (ret > 0) {
2127 *ptr = kmemdup(p, ret, gfp_flags);
2128 if (*ptr != NULL)
2129 return ret;
2130 ret = -ENOMEM;
2131 }
2132 *ptr = NULL;
2133 return ret;
2134}
2135EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
2136
2137/**
2138 * xdr_stream_decode_string - Decode variable length string
2139 * @xdr: pointer to xdr_stream
2140 * @str: location to store string
2141 * @size: size of storage buffer @str
2142 *
2143 * Return values:
2144 * On success, returns length of NUL-terminated string stored in *@str
2145 * %-EBADMSG on XDR buffer overflow
2146 * %-EMSGSIZE on overflow of storage buffer @str
2147 */
2148ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
2149{
2150 ssize_t ret;
2151 void *p;
2152
2153 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2154 if (ret > 0) {
2155 memcpy(str, p, ret);
2156 str[ret] = '\0';
2157 return strlen(str);
2158 }
2159 *str = '\0';
2160 return ret;
2161}
2162EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
2163
2164/**
Trond Myklebust5c741d42017-02-19 16:08:31 -05002165 * xdr_stream_decode_string_dup - Decode and duplicate variable length string
2166 * @xdr: pointer to xdr_stream
2167 * @str: location to store pointer to string
2168 * @maxlen: maximum acceptable string length
2169 * @gfp_flags: GFP mask to use
2170 *
2171 * Return values:
2172 * On success, returns length of NUL-terminated string stored in *@ptr
2173 * %-EBADMSG on XDR buffer overflow
2174 * %-EMSGSIZE if the size of the string would exceed @maxlen
2175 * %-ENOMEM on memory allocation failure
2176 */
2177ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
2178 size_t maxlen, gfp_t gfp_flags)
2179{
2180 void *p;
2181 ssize_t ret;
2182
2183 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2184 if (ret > 0) {
Trond Myklebust4aceaae2020-11-10 10:56:53 -05002185 char *s = kmemdup_nul(p, ret, gfp_flags);
Trond Myklebust5c741d42017-02-19 16:08:31 -05002186 if (s != NULL) {
Trond Myklebust5c741d42017-02-19 16:08:31 -05002187 *str = s;
2188 return strlen(s);
2189 }
2190 ret = -ENOMEM;
2191 }
2192 *str = NULL;
2193 return ret;
2194}
2195EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);