blob: 10a88a67206a5633d7b1f5c5db635ac83124b49d [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
22/*
23 * XDR functions for basic NFS types
24 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070025__be32 *
26xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 unsigned int quadlen = XDR_QUADLEN(obj->len);
29
30 p[quadlen] = 0; /* zero trailing bytes */
Benny Halevy9f162d22009-08-14 17:18:44 +030031 *p++ = cpu_to_be32(obj->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 memcpy(p, obj->data, obj->len);
33 return p + XDR_QUADLEN(obj->len);
34}
Trond Myklebust468039e2008-12-23 15:21:31 -050035EXPORT_SYMBOL_GPL(xdr_encode_netobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070037__be32 *
38xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 unsigned int len;
41
Benny Halevy98866b52009-08-14 17:18:49 +030042 if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return NULL;
44 obj->len = len;
45 obj->data = (u8 *) p;
46 return p + XDR_QUADLEN(len);
47}
Trond Myklebust468039e2008-12-23 15:21:31 -050048EXPORT_SYMBOL_GPL(xdr_decode_netobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/**
51 * xdr_encode_opaque_fixed - Encode fixed length opaque data
Pavel Pisa4dc3b162005-05-01 08:59:25 -070052 * @p: pointer to current position in XDR buffer.
53 * @ptr: pointer to data to encode (or NULL)
54 * @nbytes: size of data.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
56 * Copy the array of data of length nbytes at ptr to the XDR buffer
57 * at position p, then align to the next 32-bit boundary by padding
58 * with zero bytes (see RFC1832).
59 * Note: if ptr is NULL, only the padding is performed.
60 *
61 * Returns the updated current XDR buffer position
62 *
63 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070064__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 if (likely(nbytes != 0)) {
67 unsigned int quadlen = XDR_QUADLEN(nbytes);
68 unsigned int padding = (quadlen << 2) - nbytes;
69
70 if (ptr != NULL)
71 memcpy(p, ptr, nbytes);
72 if (padding != 0)
73 memset((char *)p + nbytes, 0, padding);
74 p += quadlen;
75 }
76 return p;
77}
Trond Myklebust468039e2008-12-23 15:21:31 -050078EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/**
81 * xdr_encode_opaque - Encode variable length opaque data
Pavel Pisa4dc3b162005-05-01 08:59:25 -070082 * @p: pointer to current position in XDR buffer.
83 * @ptr: pointer to data to encode (or NULL)
84 * @nbytes: size of data.
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 *
86 * Returns the updated current XDR buffer position
87 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070088__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Benny Halevy9f162d22009-08-14 17:18:44 +030090 *p++ = cpu_to_be32(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return xdr_encode_opaque_fixed(p, ptr, nbytes);
92}
Trond Myklebust468039e2008-12-23 15:21:31 -050093EXPORT_SYMBOL_GPL(xdr_encode_opaque);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070095__be32 *
96xdr_encode_string(__be32 *p, const char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 return xdr_encode_array(p, string, strlen(string));
99}
Trond Myklebust468039e2008-12-23 15:21:31 -0500100EXPORT_SYMBOL_GPL(xdr_encode_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700102__be32 *
Chuck Levere5cff482007-11-01 16:56:47 -0400103xdr_decode_string_inplace(__be32 *p, char **sp,
104 unsigned int *lenp, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Chuck Levere5cff482007-11-01 16:56:47 -0400106 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Benny Halevy98866b52009-08-14 17:18:49 +0300108 len = be32_to_cpu(*p++);
Chuck Levere5cff482007-11-01 16:56:47 -0400109 if (len > maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return NULL;
111 *lenp = len;
112 *sp = (char *) p;
113 return p + XDR_QUADLEN(len);
114}
Trond Myklebust468039e2008-12-23 15:21:31 -0500115EXPORT_SYMBOL_GPL(xdr_decode_string_inplace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Chuck Leverb4687da2010-09-21 16:55:48 -0400117/**
118 * xdr_terminate_string - '\0'-terminate a string residing in an xdr_buf
119 * @buf: XDR buffer where string resides
120 * @len: length of string, in bytes
121 *
122 */
123void
124xdr_terminate_string(struct xdr_buf *buf, const u32 len)
125{
126 char *kaddr;
127
Cong Wangb8541782011-11-25 23:14:40 +0800128 kaddr = kmap_atomic(buf->pages[0]);
Chuck Leverb4687da2010-09-21 16:55:48 -0400129 kaddr[buf->page_base + len] = '\0';
Cong Wangb8541782011-11-25 23:14:40 +0800130 kunmap_atomic(kaddr);
Chuck Leverb4687da2010-09-21 16:55:48 -0400131}
Trond Myklebust0d961aa2011-07-13 19:24:15 -0400132EXPORT_SYMBOL_GPL(xdr_terminate_string);
Chuck Leverb4687da2010-09-21 16:55:48 -0400133
Trond Myklebust9d96acb2018-09-13 12:22:04 -0400134size_t
135xdr_buf_pagecount(struct xdr_buf *buf)
136{
137 if (!buf->page_len)
138 return 0;
139 return (buf->page_base + buf->page_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
140}
141
142int
143xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp)
144{
145 size_t i, n = xdr_buf_pagecount(buf);
146
147 if (n != 0 && buf->bvec == NULL) {
148 buf->bvec = kmalloc_array(n, sizeof(buf->bvec[0]), gfp);
149 if (!buf->bvec)
150 return -ENOMEM;
151 for (i = 0; i < n; i++) {
152 buf->bvec[i].bv_page = buf->pages[i];
153 buf->bvec[i].bv_len = PAGE_SIZE;
154 buf->bvec[i].bv_offset = 0;
155 }
156 }
157 return 0;
158}
159
160void
161xdr_free_bvec(struct xdr_buf *buf)
162{
163 kfree(buf->bvec);
164 buf->bvec = NULL;
165}
166
Chuck Levercf500ba2019-02-11 11:25:20 -0500167/**
168 * xdr_inline_pages - Prepare receive buffer for a large reply
169 * @xdr: xdr_buf into which reply will be placed
170 * @offset: expected offset where data payload will start, in bytes
171 * @pages: vector of struct page pointers
172 * @base: offset in first page where receive should start, in bytes
173 * @len: expected size of the upper layer data payload, in bytes
174 *
175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
178 struct page **pages, unsigned int base, unsigned int len)
179{
180 struct kvec *head = xdr->head;
181 struct kvec *tail = xdr->tail;
182 char *buf = (char *)head->iov_base;
183 unsigned int buflen = head->iov_len;
184
185 head->iov_len = offset;
186
187 xdr->pages = pages;
188 xdr->page_base = base;
189 xdr->page_len = len;
190
191 tail->iov_base = buf + offset;
192 tail->iov_len = buflen - offset;
Chuck Lever02ef04e2019-02-11 11:25:25 -0500193 if ((xdr->page_len & 3) == 0)
194 tail->iov_len -= sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 xdr->buflen += len;
197}
Trond Myklebust468039e2008-12-23 15:21:31 -0500198EXPORT_SYMBOL_GPL(xdr_inline_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
201 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
Ben Hutchings2c530402012-07-10 10:55:09 +0000202 */
203
204/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * _shift_data_right_pages
206 * @pages: vector of pages containing both the source and dest memory area.
207 * @pgto_base: page vector address of destination
208 * @pgfrom_base: page vector address of source
209 * @len: number of bytes to copy
210 *
211 * Note: the addresses pgto_base and pgfrom_base are both calculated in
212 * the same way:
213 * if a memory area starts at byte 'base' in page 'pages[i]',
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300214 * then its address is given as (i << PAGE_SHIFT) + base
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Also note: pgfrom_base must be < pgto_base, but the memory areas
216 * they point to may overlap.
217 */
218static void
219_shift_data_right_pages(struct page **pages, size_t pgto_base,
220 size_t pgfrom_base, size_t len)
221{
222 struct page **pgfrom, **pgto;
223 char *vfrom, *vto;
224 size_t copy;
225
226 BUG_ON(pgto_base <= pgfrom_base);
227
228 pgto_base += len;
229 pgfrom_base += len;
230
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300231 pgto = pages + (pgto_base >> PAGE_SHIFT);
232 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300234 pgto_base &= ~PAGE_MASK;
235 pgfrom_base &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 do {
238 /* Are any pointers crossing a page boundary? */
239 if (pgto_base == 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 pgto_base = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 pgto--;
242 }
243 if (pgfrom_base == 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300244 pgfrom_base = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 pgfrom--;
246 }
247
248 copy = len;
249 if (copy > pgto_base)
250 copy = pgto_base;
251 if (copy > pgfrom_base)
252 copy = pgfrom_base;
253 pgto_base -= copy;
254 pgfrom_base -= copy;
255
Cong Wangb8541782011-11-25 23:14:40 +0800256 vto = kmap_atomic(*pgto);
Trond Myklebust347e2232013-08-28 13:35:13 -0400257 if (*pgto != *pgfrom) {
258 vfrom = kmap_atomic(*pgfrom);
259 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
260 kunmap_atomic(vfrom);
261 } else
262 memmove(vto + pgto_base, vto + pgfrom_base, copy);
Trond Myklebustbce34812006-07-05 13:17:12 -0400263 flush_dcache_page(*pgto);
Cong Wangb8541782011-11-25 23:14:40 +0800264 kunmap_atomic(vto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 } while ((len -= copy) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Anna Schumaker43f0f082020-05-06 13:21:30 -0400269static unsigned int
270_shift_data_right_tail(struct xdr_buf *buf, unsigned int pgfrom, size_t len)
271{
272 struct kvec *tail = buf->tail;
273 unsigned int tailbuf_len;
274 unsigned int result = 0;
275 size_t copy;
276
277 tailbuf_len = buf->buflen - buf->head->iov_len - buf->page_len;
278
279 /* Shift the tail first */
280 if (tailbuf_len != 0) {
281 unsigned int free_space = tailbuf_len - tail->iov_len;
282
283 if (len < free_space)
284 free_space = len;
285 if (len > free_space)
286 len = free_space;
287
288 tail->iov_len += free_space;
289 copy = len;
290
291 if (tail->iov_len > len) {
292 char *p = (char *)tail->iov_base + len;
293 memmove(p, tail->iov_base, tail->iov_len - free_space);
294 result += tail->iov_len - free_space;
295 } else
296 copy = tail->iov_len;
297
298 /* Copy from the inlined pages into the tail */
299 _copy_from_pages((char *)tail->iov_base,
300 buf->pages,
301 buf->page_base + pgfrom,
302 copy);
303 result += copy;
304 }
305
306 return result;
307}
308
Ben Hutchings2c530402012-07-10 10:55:09 +0000309/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * _copy_to_pages
311 * @pages: array of pages
312 * @pgbase: page vector address of destination
313 * @p: pointer to source data
314 * @len: length
315 *
316 * Copies data from an arbitrary memory location into an array of pages
317 * The copy is assumed to be non-overlapping.
318 */
319static void
320_copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
321{
322 struct page **pgto;
323 char *vto;
324 size_t copy;
325
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300326 pgto = pages + (pgbase >> PAGE_SHIFT);
327 pgbase &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Trond Myklebustdaeba892008-03-31 17:02:02 -0400329 for (;;) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300330 copy = PAGE_SIZE - pgbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (copy > len)
332 copy = len;
333
Cong Wangb8541782011-11-25 23:14:40 +0800334 vto = kmap_atomic(*pgto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 memcpy(vto + pgbase, p, copy);
Cong Wangb8541782011-11-25 23:14:40 +0800336 kunmap_atomic(vto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Trond Myklebustdaeba892008-03-31 17:02:02 -0400338 len -= copy;
339 if (len == 0)
340 break;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 pgbase += copy;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300343 if (pgbase == PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 flush_dcache_page(*pgto);
345 pgbase = 0;
346 pgto++;
347 }
348 p += copy;
Trond Myklebustdaeba892008-03-31 17:02:02 -0400349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 flush_dcache_page(*pgto);
351}
352
Ben Hutchings2c530402012-07-10 10:55:09 +0000353/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * _copy_from_pages
355 * @p: pointer to destination
356 * @pages: array of pages
357 * @pgbase: offset of source data
358 * @len: length
359 *
360 * Copies data into an arbitrary memory location from an array of pages
361 * The copy is assumed to be non-overlapping.
362 */
Andy Adamsonbf118a32011-12-07 11:55:27 -0500363void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364_copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
365{
366 struct page **pgfrom;
367 char *vfrom;
368 size_t copy;
369
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300370 pgfrom = pages + (pgbase >> PAGE_SHIFT);
371 pgbase &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 do {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300374 copy = PAGE_SIZE - pgbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (copy > len)
376 copy = len;
377
Cong Wangb8541782011-11-25 23:14:40 +0800378 vfrom = kmap_atomic(*pgfrom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 memcpy(p, vfrom + pgbase, copy);
Cong Wangb8541782011-11-25 23:14:40 +0800380 kunmap_atomic(vfrom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 pgbase += copy;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300383 if (pgbase == PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 pgbase = 0;
385 pgfrom++;
386 }
387 p += copy;
388
389 } while ((len -= copy) != 0);
390}
Andy Adamsonbf118a32011-12-07 11:55:27 -0500391EXPORT_SYMBOL_GPL(_copy_from_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Ben Hutchings2c530402012-07-10 10:55:09 +0000393/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * xdr_shrink_bufhead
395 * @buf: xdr_buf
396 * @len: bytes to remove from buf->head[0]
397 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800398 * Shrinks XDR buffer's header kvec buf->head[0] by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * 'len' bytes. The extra data is not lost, but is instead
400 * moved into the inlined pages and/or the tail.
401 */
Chuck Lever7be9cea32019-02-11 11:24:16 -0500402static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
404{
405 struct kvec *head, *tail;
406 size_t copy, offs;
407 unsigned int pglen = buf->page_len;
Chuck Lever7be9cea32019-02-11 11:24:16 -0500408 unsigned int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Chuck Lever7be9cea32019-02-11 11:24:16 -0500410 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 tail = buf->tail;
412 head = buf->head;
Weston Andros Adamson18e624a2012-10-23 10:43:42 -0400413
414 WARN_ON_ONCE(len > head->iov_len);
415 if (len > head->iov_len)
416 len = head->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* Shift the tail first */
419 if (tail->iov_len != 0) {
420 if (tail->iov_len > len) {
421 copy = tail->iov_len - len;
422 memmove((char *)tail->iov_base + len,
423 tail->iov_base, copy);
Chuck Lever7be9cea32019-02-11 11:24:16 -0500424 result += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 /* Copy from the inlined pages into the tail */
427 copy = len;
428 if (copy > pglen)
429 copy = pglen;
430 offs = len - copy;
431 if (offs >= tail->iov_len)
432 copy = 0;
433 else if (copy > tail->iov_len - offs)
434 copy = tail->iov_len - offs;
Chuck Lever7be9cea32019-02-11 11:24:16 -0500435 if (copy != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 _copy_from_pages((char *)tail->iov_base + offs,
437 buf->pages,
438 buf->page_base + pglen + offs - len,
439 copy);
Chuck Lever7be9cea32019-02-11 11:24:16 -0500440 result += copy;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* Do we also need to copy data from the head into the tail ? */
443 if (len > pglen) {
444 offs = copy = len - pglen;
445 if (copy > tail->iov_len)
446 copy = tail->iov_len;
447 memcpy(tail->iov_base,
448 (char *)head->iov_base +
449 head->iov_len - offs,
450 copy);
Chuck Lever7be9cea32019-02-11 11:24:16 -0500451 result += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 }
454 /* Now handle pages */
455 if (pglen != 0) {
456 if (pglen > len)
457 _shift_data_right_pages(buf->pages,
458 buf->page_base + len,
459 buf->page_base,
460 pglen - len);
461 copy = len;
462 if (len > pglen)
463 copy = pglen;
464 _copy_to_pages(buf->pages, buf->page_base,
465 (char *)head->iov_base + head->iov_len - len,
466 copy);
Chuck Lever7be9cea32019-02-11 11:24:16 -0500467 result += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 head->iov_len -= len;
470 buf->buflen -= len;
471 /* Have we truncated the message? */
472 if (buf->len > buf->buflen)
473 buf->len = buf->buflen;
Chuck Lever7be9cea32019-02-11 11:24:16 -0500474
475 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Ben Hutchings2c530402012-07-10 10:55:09 +0000478/**
Chuck Levere8d70b32019-11-15 08:39:07 -0500479 * xdr_shrink_pagelen - shrinks buf->pages by up to @len bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * @buf: xdr_buf
481 * @len: bytes to remove from buf->pages
482 *
Chuck Levere8d70b32019-11-15 08:39:07 -0500483 * The extra data is not lost, but is instead moved into buf->tail.
484 * Returns the actual number of bytes moved.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
Chuck Lever7be9cea32019-02-11 11:24:16 -0500486static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
488{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned int pglen = buf->page_len;
Chuck Lever7be9cea32019-02-11 11:24:16 -0500490 unsigned int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Chuck Levere8d70b32019-11-15 08:39:07 -0500492 if (len > buf->page_len)
493 len = buf-> page_len;
Trond Myklebustcf187c22010-08-29 12:13:16 -0400494
Anna Schumaker43f0f082020-05-06 13:21:30 -0400495 result = _shift_data_right_tail(buf, pglen - len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 buf->page_len -= len;
497 buf->buflen -= len;
498 /* Have we truncated the message? */
499 if (buf->len > buf->buflen)
500 buf->len = buf->buflen;
Chuck Lever7be9cea32019-02-11 11:24:16 -0500501
502 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505void
506xdr_shift_buf(struct xdr_buf *buf, size_t len)
507{
508 xdr_shrink_bufhead(buf, len);
509}
Trond Myklebust468039e2008-12-23 15:21:31 -0500510EXPORT_SYMBOL_GPL(xdr_shift_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512/**
Trond Myklebust4517d522012-06-21 17:14:46 -0400513 * xdr_stream_pos - Return the current offset from the start of the xdr_stream
514 * @xdr: pointer to struct xdr_stream
515 */
516unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
517{
518 return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
519}
520EXPORT_SYMBOL_GPL(xdr_stream_pos);
521
522/**
Anna Schumakercf1f08c2020-04-17 11:00:24 -0400523 * xdr_page_pos - Return the current offset from the start of the xdr pages
524 * @xdr: pointer to struct xdr_stream
525 */
526unsigned int xdr_page_pos(const struct xdr_stream *xdr)
527{
528 unsigned int pos = xdr_stream_pos(xdr);
529
530 WARN_ON(pos < xdr->buf->head[0].iov_len);
531 return pos - xdr->buf->head[0].iov_len;
532}
533EXPORT_SYMBOL_GPL(xdr_page_pos);
534
535/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
537 * @xdr: pointer to xdr_stream struct
538 * @buf: pointer to XDR buffer in which to encode data
539 * @p: current pointer inside XDR buffer
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500540 * @rqst: pointer to controlling rpc_rqst, for debugging
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 *
542 * Note: at the moment the RPC client only passes the length of our
543 * scratch buffer in the xdr_buf's header kvec. Previously this
544 * meant we needed to call xdr_adjust_iovec() after encoding the
545 * data. With the new scheme, the xdr_stream manages the details
546 * of the buffer length, and takes care of adjusting the kvec
547 * length for us.
548 */
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500549void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
550 struct rpc_rqst *rqst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct kvec *iov = buf->head;
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000553 int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400555 xdr_set_scratch_buffer(xdr, NULL, 0);
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000556 BUG_ON(scratch_len < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 xdr->buf = buf;
558 xdr->iov = iov;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700559 xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
560 xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000561 BUG_ON(iov->iov_len > scratch_len);
562
563 if (p != xdr->p && p != NULL) {
564 size_t len;
565
566 BUG_ON(p < xdr->p || p > xdr->end);
567 len = (char *)p - (char *)xdr->p;
568 xdr->p = p;
569 buf->len += len;
570 iov->iov_len += len;
571 }
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500572 xdr->rqst = rqst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Trond Myklebust468039e2008-12-23 15:21:31 -0500574EXPORT_SYMBOL_GPL(xdr_init_encode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576/**
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400577 * xdr_commit_encode - Ensure all data is written to buffer
578 * @xdr: pointer to xdr_stream
579 *
580 * We handle encoding across page boundaries by giving the caller a
581 * temporary location to write to, then later copying the data into
582 * place; xdr_commit_encode does that copying.
583 *
584 * Normally the caller doesn't need to call this directly, as the
585 * following xdr_reserve_space will do it. But an explicit call may be
586 * required at the end of encoding, or any other time when the xdr_buf
587 * data might be read.
588 */
Chuck Lever95bd8302019-08-19 18:37:05 -0400589inline void xdr_commit_encode(struct xdr_stream *xdr)
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400590{
591 int shift = xdr->scratch.iov_len;
592 void *page;
593
594 if (shift == 0)
595 return;
596 page = page_address(*xdr->page_ptr);
597 memcpy(xdr->scratch.iov_base, page, shift);
598 memmove(page, page + shift, (void *)xdr->p - page);
599 xdr->scratch.iov_len = 0;
600}
601EXPORT_SYMBOL_GPL(xdr_commit_encode);
602
Trond Myklebust22cb4382014-07-12 18:01:02 -0400603static __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
604 size_t nbytes)
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400605{
YueHaibing025911a2018-11-08 02:04:57 +0000606 __be32 *p;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400607 int space_left;
608 int frag1bytes, frag2bytes;
609
610 if (nbytes > PAGE_SIZE)
Chuck Lever55828632019-02-11 11:24:10 -0500611 goto out_overflow; /* Bigger buffers require special handling */
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400612 if (xdr->buf->len + nbytes > xdr->buf->buflen)
Chuck Lever55828632019-02-11 11:24:10 -0500613 goto out_overflow; /* Sorry, we're totally out of space */
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400614 frag1bytes = (xdr->end - xdr->p) << 2;
615 frag2bytes = nbytes - frag1bytes;
616 if (xdr->iov)
617 xdr->iov->iov_len += frag1bytes;
J. Bruce Fields05638dc2014-06-02 12:05:47 -0400618 else
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400619 xdr->buf->page_len += frag1bytes;
J. Bruce Fields05638dc2014-06-02 12:05:47 -0400620 xdr->page_ptr++;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400621 xdr->iov = NULL;
622 /*
623 * If the last encode didn't end exactly on a page boundary, the
624 * next one will straddle boundaries. Encode into the next
625 * page, then copy it back later in xdr_commit_encode. We use
626 * the "scratch" iov to track any temporarily unused fragment of
627 * space at the end of the previous buffer:
628 */
629 xdr->scratch.iov_base = xdr->p;
630 xdr->scratch.iov_len = frag1bytes;
631 p = page_address(*xdr->page_ptr);
632 /*
633 * Note this is where the next encode will start after we've
634 * shifted this one back:
635 */
636 xdr->p = (void *)p + frag2bytes;
637 space_left = xdr->buf->buflen - xdr->buf->len;
638 xdr->end = (void *)p + min_t(int, space_left, PAGE_SIZE);
639 xdr->buf->page_len += frag2bytes;
640 xdr->buf->len += nbytes;
641 return p;
Chuck Lever55828632019-02-11 11:24:10 -0500642out_overflow:
643 trace_rpc_xdr_overflow(xdr, nbytes);
644 return NULL;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400645}
646
647/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * xdr_reserve_space - Reserve buffer space for sending
649 * @xdr: pointer to xdr_stream
650 * @nbytes: number of bytes to reserve
651 *
652 * Checks that we have enough buffer space to encode 'nbytes' more
653 * bytes of data. If so, update the total xdr_buf length, and
654 * adjust the length of the current kvec.
655 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700656__be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700658 __be32 *p = xdr->p;
659 __be32 *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400661 xdr_commit_encode(xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /* align nbytes on the next 32-bit boundary */
663 nbytes += 3;
664 nbytes &= ~3;
665 q = p + (nbytes >> 2);
666 if (unlikely(q > xdr->end || q < p))
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400667 return xdr_get_next_encode_buffer(xdr, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 xdr->p = q;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400669 if (xdr->iov)
670 xdr->iov->iov_len += nbytes;
671 else
672 xdr->buf->page_len += nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 xdr->buf->len += nbytes;
674 return p;
675}
Trond Myklebust468039e2008-12-23 15:21:31 -0500676EXPORT_SYMBOL_GPL(xdr_reserve_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/**
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500679 * xdr_truncate_encode - truncate an encode buffer
680 * @xdr: pointer to xdr_stream
681 * @len: new length of buffer
682 *
683 * Truncates the xdr stream, so that xdr->buf->len == len,
684 * and xdr->p points at offset len from the start of the buffer, and
685 * head, tail, and page lengths are adjusted to correspond.
686 *
687 * If this means moving xdr->p to a different buffer, we assume that
688 * that the end pointer should be set to the end of the current page,
689 * except in the case of the head buffer when we assume the head
690 * buffer's current length represents the end of the available buffer.
691 *
692 * This is *not* safe to use on a buffer that already has inlined page
693 * cache pages (as in a zero-copy server read reply), except for the
694 * simple case of truncating from one position in the tail to another.
695 *
696 */
697void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
698{
699 struct xdr_buf *buf = xdr->buf;
700 struct kvec *head = buf->head;
701 struct kvec *tail = buf->tail;
702 int fraglen;
J. Bruce Fields49a068f2014-12-22 16:14:51 -0500703 int new;
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500704
705 if (len > buf->len) {
706 WARN_ON_ONCE(1);
707 return;
708 }
J. Bruce Fields2825a7f2013-08-26 16:04:46 -0400709 xdr_commit_encode(xdr);
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500710
711 fraglen = min_t(int, buf->len - len, tail->iov_len);
712 tail->iov_len -= fraglen;
713 buf->len -= fraglen;
J. Bruce Fieldsed38c062014-09-19 17:21:35 -0400714 if (tail->iov_len) {
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500715 xdr->p = tail->iov_base + tail->iov_len;
J. Bruce Fields280caac2014-10-01 11:36:31 -0400716 WARN_ON_ONCE(!xdr->end);
717 WARN_ON_ONCE(!xdr->iov);
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500718 return;
719 }
720 WARN_ON_ONCE(fraglen);
721 fraglen = min_t(int, buf->len - len, buf->page_len);
722 buf->page_len -= fraglen;
723 buf->len -= fraglen;
724
725 new = buf->page_base + buf->page_len;
J. Bruce Fields49a068f2014-12-22 16:14:51 -0500726
727 xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500728
J. Bruce Fieldsed38c062014-09-19 17:21:35 -0400729 if (buf->page_len) {
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500730 xdr->p = page_address(*xdr->page_ptr);
731 xdr->end = (void *)xdr->p + PAGE_SIZE;
732 xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
J. Bruce Fields280caac2014-10-01 11:36:31 -0400733 WARN_ON_ONCE(xdr->iov);
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500734 return;
735 }
Frank Sorenson5d7a5bc2018-10-30 15:10:40 -0500736 if (fraglen)
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500737 xdr->end = head->iov_base + head->iov_len;
738 /* (otherwise assume xdr->end is already set) */
Frank Sorenson5d7a5bc2018-10-30 15:10:40 -0500739 xdr->page_ptr--;
J. Bruce Fields3e19ce72014-02-25 17:44:21 -0500740 head->iov_len = len;
741 buf->len = len;
742 xdr->p = head->iov_base + head->iov_len;
743 xdr->iov = buf->head;
744}
745EXPORT_SYMBOL(xdr_truncate_encode);
746
747/**
J. Bruce Fieldsdb3f58a2014-03-06 13:22:18 -0500748 * xdr_restrict_buflen - decrease available buffer space
749 * @xdr: pointer to xdr_stream
750 * @newbuflen: new maximum number of bytes available
751 *
752 * Adjust our idea of how much space is available in the buffer.
753 * If we've already used too much space in the buffer, returns -1.
754 * If the available space is already smaller than newbuflen, returns 0
755 * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
756 * and ensures xdr->end is set at most offset newbuflen from the start
757 * of the buffer.
758 */
759int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
760{
761 struct xdr_buf *buf = xdr->buf;
762 int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
763 int end_offset = buf->len + left_in_this_buf;
764
765 if (newbuflen < 0 || newbuflen < buf->len)
766 return -1;
767 if (newbuflen > buf->buflen)
768 return 0;
769 if (newbuflen < end_offset)
770 xdr->end = (void *)xdr->end + newbuflen - end_offset;
771 buf->buflen = newbuflen;
772 return 0;
773}
774EXPORT_SYMBOL(xdr_restrict_buflen);
775
776/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
778 * @xdr: pointer to xdr_stream
779 * @pages: list of pages
780 * @base: offset of first byte
781 * @len: length of data in bytes
782 *
783 */
784void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
785 unsigned int len)
786{
787 struct xdr_buf *buf = xdr->buf;
788 struct kvec *iov = buf->tail;
789 buf->pages = pages;
790 buf->page_base = base;
791 buf->page_len = len;
792
793 iov->iov_base = (char *)xdr->p;
794 iov->iov_len = 0;
795 xdr->iov = iov;
796
797 if (len & 3) {
798 unsigned int pad = 4 - (len & 3);
799
800 BUG_ON(xdr->p >= xdr->end);
801 iov->iov_base = (char *)xdr->p + (len & 3);
802 iov->iov_len += pad;
803 len += pad;
804 *xdr->p++ = 0;
805 }
806 buf->buflen += len;
807 buf->len += len;
808}
Trond Myklebust468039e2008-12-23 15:21:31 -0500809EXPORT_SYMBOL_GPL(xdr_write_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Trond Myklebust66502392011-01-08 17:45:38 -0500811static void xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
Trond Myklebust15376932012-06-28 17:17:48 -0400812 unsigned int len)
Trond Myklebust66502392011-01-08 17:45:38 -0500813{
814 if (len > iov->iov_len)
815 len = iov->iov_len;
Trond Myklebust15376932012-06-28 17:17:48 -0400816 xdr->p = (__be32*)iov->iov_base;
Trond Myklebust66502392011-01-08 17:45:38 -0500817 xdr->end = (__be32*)(iov->iov_base + len);
818 xdr->iov = iov;
819 xdr->page_ptr = NULL;
820}
821
822static int xdr_set_page_base(struct xdr_stream *xdr,
823 unsigned int base, unsigned int len)
824{
825 unsigned int pgnr;
826 unsigned int maxlen;
827 unsigned int pgoff;
828 unsigned int pgend;
829 void *kaddr;
830
831 maxlen = xdr->buf->page_len;
832 if (base >= maxlen)
833 return -EINVAL;
834 maxlen -= base;
835 if (len > maxlen)
836 len = maxlen;
837
838 base += xdr->buf->page_base;
839
840 pgnr = base >> PAGE_SHIFT;
841 xdr->page_ptr = &xdr->buf->pages[pgnr];
842 kaddr = page_address(*xdr->page_ptr);
843
844 pgoff = base & ~PAGE_MASK;
845 xdr->p = (__be32*)(kaddr + pgoff);
846
847 pgend = pgoff + len;
848 if (pgend > PAGE_SIZE)
849 pgend = PAGE_SIZE;
850 xdr->end = (__be32*)(kaddr + pgend);
851 xdr->iov = NULL;
852 return 0;
853}
854
Anna Schumakerf7d61ee2015-01-26 17:26:19 -0500855static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
856 unsigned int len)
857{
858 if (xdr_set_page_base(xdr, base, len) < 0)
859 xdr_set_iov(xdr, xdr->buf->tail, xdr->nwords << 2);
860}
861
Trond Myklebust66502392011-01-08 17:45:38 -0500862static void xdr_set_next_page(struct xdr_stream *xdr)
863{
864 unsigned int newbase;
865
866 newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
867 newbase -= xdr->buf->page_base;
868
Anna Schumakerf7d61ee2015-01-26 17:26:19 -0500869 xdr_set_page(xdr, newbase, PAGE_SIZE);
Trond Myklebust66502392011-01-08 17:45:38 -0500870}
871
872static bool xdr_set_next_buffer(struct xdr_stream *xdr)
873{
874 if (xdr->page_ptr != NULL)
875 xdr_set_next_page(xdr);
876 else if (xdr->iov == xdr->buf->head) {
Anna Schumakerf7d61ee2015-01-26 17:26:19 -0500877 xdr_set_page(xdr, 0, PAGE_SIZE);
Trond Myklebust66502392011-01-08 17:45:38 -0500878 }
879 return xdr->p != xdr->end;
880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882/**
883 * xdr_init_decode - Initialize an xdr_stream for decoding data.
884 * @xdr: pointer to xdr_stream struct
885 * @buf: pointer to XDR buffer from which to decode data
886 * @p: current pointer inside XDR buffer
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500887 * @rqst: pointer to controlling rpc_rqst, for debugging
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500889void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
890 struct rpc_rqst *rqst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 xdr->buf = buf;
Trond Myklebust66502392011-01-08 17:45:38 -0500893 xdr->scratch.iov_base = NULL;
894 xdr->scratch.iov_len = 0;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -0400895 xdr->nwords = XDR_QUADLEN(buf->len);
Trond Myklebust66502392011-01-08 17:45:38 -0500896 if (buf->head[0].iov_len != 0)
Trond Myklebust15376932012-06-28 17:17:48 -0400897 xdr_set_iov(xdr, buf->head, buf->len);
Trond Myklebust66502392011-01-08 17:45:38 -0500898 else if (buf->page_len != 0)
899 xdr_set_page_base(xdr, 0, buf->len);
Benjamin Coddington06ef26a2016-04-06 11:32:52 -0400900 else
901 xdr_set_iov(xdr, buf->head, buf->len);
Trond Myklebustbfeea1d2012-06-20 09:58:35 -0400902 if (p != NULL && p > xdr->p && xdr->end >= p) {
903 xdr->nwords -= p - xdr->p;
Trond Myklebust15376932012-06-28 17:17:48 -0400904 xdr->p = p;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -0400905 }
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500906 xdr->rqst = rqst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
Trond Myklebust468039e2008-12-23 15:21:31 -0500908EXPORT_SYMBOL_GPL(xdr_init_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Benny Halevyf7da7a12011-05-19 14:16:47 -0400910/**
Chuck Lever7ecce752017-04-11 13:23:59 -0400911 * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
Benny Halevyf7da7a12011-05-19 14:16:47 -0400912 * @xdr: pointer to xdr_stream struct
913 * @buf: pointer to XDR buffer from which to decode data
914 * @pages: list of pages to decode into
915 * @len: length in bytes of buffer in pages
916 */
917void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
918 struct page **pages, unsigned int len)
919{
920 memset(buf, 0, sizeof(*buf));
921 buf->pages = pages;
922 buf->page_len = len;
923 buf->buflen = len;
924 buf->len = len;
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500925 xdr_init_decode(xdr, buf, NULL, NULL);
Benny Halevyf7da7a12011-05-19 14:16:47 -0400926}
927EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
928
Trond Myklebust66502392011-01-08 17:45:38 -0500929static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
Trond Myklebustba8e4522010-10-19 19:58:49 -0400930{
Trond Myklebustbfeea1d2012-06-20 09:58:35 -0400931 unsigned int nwords = XDR_QUADLEN(nbytes);
Trond Myklebustba8e4522010-10-19 19:58:49 -0400932 __be32 *p = xdr->p;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -0400933 __be32 *q = p + nwords;
Trond Myklebustba8e4522010-10-19 19:58:49 -0400934
Trond Myklebustbfeea1d2012-06-20 09:58:35 -0400935 if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
Trond Myklebustba8e4522010-10-19 19:58:49 -0400936 return NULL;
Trond Myklebust66502392011-01-08 17:45:38 -0500937 xdr->p = q;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -0400938 xdr->nwords -= nwords;
Trond Myklebustba8e4522010-10-19 19:58:49 -0400939 return p;
940}
Trond Myklebustba8e4522010-10-19 19:58:49 -0400941
942/**
Trond Myklebust66502392011-01-08 17:45:38 -0500943 * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
944 * @xdr: pointer to xdr_stream struct
945 * @buf: pointer to an empty buffer
946 * @buflen: size of 'buf'
947 *
948 * The scratch buffer is used when decoding from an array of pages.
949 * If an xdr_inline_decode() call spans across page boundaries, then
950 * we copy the data into the scratch buffer in order to allow linear
951 * access.
952 */
953void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
954{
955 xdr->scratch.iov_base = buf;
956 xdr->scratch.iov_len = buflen;
957}
958EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer);
959
960static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
961{
962 __be32 *p;
Trond Myklebustace0e142016-09-20 14:33:42 -0400963 char *cpdest = xdr->scratch.iov_base;
Trond Myklebust66502392011-01-08 17:45:38 -0500964 size_t cplen = (char *)xdr->end - (char *)xdr->p;
965
966 if (nbytes > xdr->scratch.iov_len)
Chuck Lever55828632019-02-11 11:24:10 -0500967 goto out_overflow;
Trond Myklebustace0e142016-09-20 14:33:42 -0400968 p = __xdr_inline_decode(xdr, cplen);
969 if (p == NULL)
970 return NULL;
971 memcpy(cpdest, p, cplen);
Chuck Lever55828632019-02-11 11:24:10 -0500972 if (!xdr_set_next_buffer(xdr))
973 goto out_overflow;
Trond Myklebust66502392011-01-08 17:45:38 -0500974 cpdest += cplen;
975 nbytes -= cplen;
Trond Myklebust66502392011-01-08 17:45:38 -0500976 p = __xdr_inline_decode(xdr, nbytes);
977 if (p == NULL)
978 return NULL;
979 memcpy(cpdest, p, nbytes);
980 return xdr->scratch.iov_base;
Chuck Lever55828632019-02-11 11:24:10 -0500981out_overflow:
982 trace_rpc_xdr_overflow(xdr, nbytes);
983 return NULL;
Trond Myklebust66502392011-01-08 17:45:38 -0500984}
985
986/**
987 * xdr_inline_decode - Retrieve XDR data to decode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * @xdr: pointer to xdr_stream struct
989 * @nbytes: number of bytes of data to decode
990 *
991 * Check if the input buffer is long enough to enable us to decode
992 * 'nbytes' more bytes of data starting at the current position.
993 * If so return the current pointer, then update the current
994 * pointer position.
995 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700996__be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Trond Myklebust66502392011-01-08 17:45:38 -0500998 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Chuck Lever55828632019-02-11 11:24:10 -05001000 if (unlikely(nbytes == 0))
Trond Myklebust66502392011-01-08 17:45:38 -05001001 return xdr->p;
1002 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
Chuck Lever55828632019-02-11 11:24:10 -05001003 goto out_overflow;
Trond Myklebust66502392011-01-08 17:45:38 -05001004 p = __xdr_inline_decode(xdr, nbytes);
1005 if (p != NULL)
1006 return p;
1007 return xdr_copy_to_scratch(xdr, nbytes);
Chuck Lever55828632019-02-11 11:24:10 -05001008out_overflow:
1009 trace_rpc_xdr_overflow(xdr, nbytes);
1010 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
Trond Myklebust468039e2008-12-23 15:21:31 -05001012EXPORT_SYMBOL_GPL(xdr_inline_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Anna Schumaker06216ec2020-04-20 17:38:17 -04001014static void xdr_realign_pages(struct xdr_stream *xdr)
1015{
1016 struct xdr_buf *buf = xdr->buf;
1017 struct kvec *iov = buf->head;
1018 unsigned int cur = xdr_stream_pos(xdr);
1019 unsigned int copied, offset;
1020
1021 /* Realign pages to current pointer position */
1022 if (iov->iov_len > cur) {
1023 offset = iov->iov_len - cur;
1024 copied = xdr_shrink_bufhead(buf, offset);
1025 trace_rpc_xdr_alignment(xdr, offset, copied);
1026 xdr->nwords = XDR_QUADLEN(buf->len - cur);
1027 }
1028}
1029
Trond Myklebust3994ee62012-06-26 12:34:05 -04001030static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct xdr_buf *buf = xdr->buf;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001033 unsigned int nwords = XDR_QUADLEN(len);
Trond Myklebustb760b312012-06-26 12:19:55 -04001034 unsigned int cur = xdr_stream_pos(xdr);
Chuck Lever7be9cea32019-02-11 11:24:16 -05001035 unsigned int copied, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001037 if (xdr->nwords == 0)
Trond Myklebustc337d362012-06-21 17:05:37 -04001038 return 0;
Chuck Lever7be9cea32019-02-11 11:24:16 -05001039
Anna Schumaker06216ec2020-04-20 17:38:17 -04001040 xdr_realign_pages(xdr);
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001041 if (nwords > xdr->nwords) {
1042 nwords = xdr->nwords;
1043 len = nwords << 2;
1044 }
Trond Myklebusta11a2bf2012-08-02 13:21:43 -04001045 if (buf->page_len <= len)
Trond Myklebust8a9a8b82012-08-01 14:32:13 -04001046 len = buf->page_len;
Trond Myklebusta11a2bf2012-08-02 13:21:43 -04001047 else if (nwords < xdr->nwords) {
1048 /* Truncate page data and move it into the tail */
Chuck Lever7be9cea32019-02-11 11:24:16 -05001049 offset = buf->page_len - len;
1050 copied = xdr_shrink_pagelen(buf, offset);
1051 trace_rpc_xdr_alignment(xdr, offset, copied);
Trond Myklebusta11a2bf2012-08-02 13:21:43 -04001052 xdr->nwords = XDR_QUADLEN(buf->len - cur);
1053 }
Trond Myklebust3994ee62012-06-26 12:34:05 -04001054 return len;
1055}
Trond Myklebustbd00f842012-06-26 13:50:43 -04001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057/**
1058 * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
1059 * @xdr: pointer to xdr_stream struct
1060 * @len: number of bytes of page data
1061 *
1062 * Moves data beyond the current pointer position from the XDR head[] buffer
1063 * into the page list. Any data that lies beyond current position + "len"
1064 * bytes is moved into the XDR tail[].
Trond Myklebust3994ee62012-06-26 12:34:05 -04001065 *
1066 * Returns the number of XDR encoded bytes now contained in the pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 */
Trond Myklebust3994ee62012-06-26 12:34:05 -04001068unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 struct xdr_buf *buf = xdr->buf;
1071 struct kvec *iov;
Trond Myklebust3994ee62012-06-26 12:34:05 -04001072 unsigned int nwords;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 unsigned int end;
Trond Myklebust3994ee62012-06-26 12:34:05 -04001074 unsigned int padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Trond Myklebust3994ee62012-06-26 12:34:05 -04001076 len = xdr_align_pages(xdr, len);
1077 if (len == 0)
1078 return 0;
1079 nwords = XDR_QUADLEN(len);
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001080 padding = (nwords << 2) - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 xdr->iov = iov = buf->tail;
1082 /* Compute remaining message length. */
Trond Myklebustbd00f842012-06-26 13:50:43 -04001083 end = ((xdr->nwords - nwords) << 2) + padding;
1084 if (end > iov->iov_len)
1085 end = iov->iov_len;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /*
1088 * Position current pointer at beginning of tail, and
1089 * set remaining message length.
1090 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001091 xdr->p = (__be32 *)((char *)iov->iov_base + padding);
1092 xdr->end = (__be32 *)((char *)iov->iov_base + end);
Trond Myklebust76cacaa2012-06-26 15:32:40 -04001093 xdr->page_ptr = NULL;
Trond Myklebustbfeea1d2012-06-20 09:58:35 -04001094 xdr->nwords = XDR_QUADLEN(end - padding);
Trond Myklebustc337d362012-06-21 17:05:37 -04001095 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
Trond Myklebust468039e2008-12-23 15:21:31 -05001097EXPORT_SYMBOL_GPL(xdr_read_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Trond Myklebust8b23ea72006-06-09 09:34:21 -04001099/**
1100 * xdr_enter_page - decode data from the XDR page
1101 * @xdr: pointer to xdr_stream struct
1102 * @len: number of bytes of page data
1103 *
1104 * Moves data beyond the current pointer position from the XDR head[] buffer
1105 * into the page list. Any data that lies beyond current position + "len"
1106 * bytes is moved into the XDR tail[]. The current pointer is then
1107 * repositioned at the beginning of the first XDR page.
1108 */
1109void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1110{
Trond Myklebustf8bb7f02012-06-21 14:53:10 -04001111 len = xdr_align_pages(xdr, len);
Trond Myklebust8b23ea72006-06-09 09:34:21 -04001112 /*
1113 * Position current pointer at beginning of tail, and
1114 * set remaining message length.
1115 */
Trond Myklebustf8bb7f02012-06-21 14:53:10 -04001116 if (len != 0)
1117 xdr_set_page_base(xdr, 0, len);
Trond Myklebust8b23ea72006-06-09 09:34:21 -04001118}
Trond Myklebust468039e2008-12-23 15:21:31 -05001119EXPORT_SYMBOL_GPL(xdr_enter_page);
Trond Myklebust8b23ea72006-06-09 09:34:21 -04001120
Julia Lawallc2bd2c02020-01-01 08:43:30 +01001121static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123void
1124xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
1125{
1126 buf->head[0] = *iov;
1127 buf->tail[0] = empty_iov;
1128 buf->page_len = 0;
1129 buf->buflen = buf->len = iov->iov_len;
1130}
Trond Myklebust468039e2008-12-23 15:21:31 -05001131EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
J. Bruce Fieldsde4aee22014-02-25 17:21:08 -05001133/**
1134 * xdr_buf_subsegment - set subbuf to a portion of buf
1135 * @buf: an xdr buffer
1136 * @subbuf: the result buffer
1137 * @base: beginning of range in bytes
1138 * @len: length of range in bytes
1139 *
1140 * sets @subbuf to an xdr buffer representing the portion of @buf of
1141 * length @len starting at offset @base.
1142 *
1143 * @buf and @subbuf may be pointers to the same struct xdr_buf.
1144 *
1145 * Returns -1 if base of length are out of bounds.
1146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147int
1148xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
Trond Myklebust1e789572006-08-31 15:09:19 -04001149 unsigned int base, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 subbuf->buflen = subbuf->len = len;
Trond Myklebust1e789572006-08-31 15:09:19 -04001152 if (base < buf->head[0].iov_len) {
1153 subbuf->head[0].iov_base = buf->head[0].iov_base + base;
1154 subbuf->head[0].iov_len = min_t(unsigned int, len,
1155 buf->head[0].iov_len - base);
1156 len -= subbuf->head[0].iov_len;
1157 base = 0;
1158 } else {
Trond Myklebust1e789572006-08-31 15:09:19 -04001159 base -= buf->head[0].iov_len;
Chuck Lever89a3c9f2020-06-25 11:32:34 -04001160 subbuf->head[0].iov_base = buf->head[0].iov_base;
J. Bruce Fieldsde4aee22014-02-25 17:21:08 -05001161 subbuf->head[0].iov_len = 0;
Trond Myklebust1e789572006-08-31 15:09:19 -04001162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (base < buf->page_len) {
Trond Myklebust1e789572006-08-31 15:09:19 -04001165 subbuf->page_len = min(buf->page_len - base, len);
1166 base += buf->page_base;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001167 subbuf->page_base = base & ~PAGE_MASK;
1168 subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 len -= subbuf->page_len;
1170 base = 0;
1171 } else {
1172 base -= buf->page_len;
Chuck Lever89a3c9f2020-06-25 11:32:34 -04001173 subbuf->pages = buf->pages;
1174 subbuf->page_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 subbuf->page_len = 0;
1176 }
1177
Trond Myklebust1e789572006-08-31 15:09:19 -04001178 if (base < buf->tail[0].iov_len) {
1179 subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
1180 subbuf->tail[0].iov_len = min_t(unsigned int, len,
1181 buf->tail[0].iov_len - base);
1182 len -= subbuf->tail[0].iov_len;
1183 base = 0;
1184 } else {
Trond Myklebust1e789572006-08-31 15:09:19 -04001185 base -= buf->tail[0].iov_len;
Chuck Lever89a3c9f2020-06-25 11:32:34 -04001186 subbuf->tail[0].iov_base = buf->tail[0].iov_base;
J. Bruce Fieldsde4aee22014-02-25 17:21:08 -05001187 subbuf->tail[0].iov_len = 0;
Trond Myklebust1e789572006-08-31 15:09:19 -04001188 }
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (base || len)
1191 return -1;
1192 return 0;
1193}
Trond Myklebust468039e2008-12-23 15:21:31 -05001194EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Chuck Lever0a8e7b72020-04-15 17:36:22 -04001196/**
1197 * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
1198 * @buf: buf to be trimmed
1199 * @len: number of bytes to reduce "buf" by
1200 *
1201 * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
1202 * that it's possible that we'll trim less than that amount if the xdr_buf is
1203 * too small, or if (for instance) it's all in the head and the parser has
1204 * already read too far into it.
1205 */
1206void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
1207{
1208 size_t cur;
1209 unsigned int trim = len;
1210
1211 if (buf->tail[0].iov_len) {
1212 cur = min_t(size_t, buf->tail[0].iov_len, trim);
1213 buf->tail[0].iov_len -= cur;
1214 trim -= cur;
1215 if (!trim)
1216 goto fix_len;
1217 }
1218
1219 if (buf->page_len) {
1220 cur = min_t(unsigned int, buf->page_len, trim);
1221 buf->page_len -= cur;
1222 trim -= cur;
1223 if (!trim)
1224 goto fix_len;
1225 }
1226
1227 if (buf->head[0].iov_len) {
1228 cur = min_t(size_t, buf->head[0].iov_len, trim);
1229 buf->head[0].iov_len -= cur;
1230 trim -= cur;
1231 }
1232fix_len:
1233 buf->len -= (len - trim);
1234}
1235EXPORT_SYMBOL_GPL(xdr_buf_trim);
1236
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001237static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Trond Myklebust1e789572006-08-31 15:09:19 -04001239 unsigned int this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001241 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1242 memcpy(obj, subbuf->head[0].iov_base, this_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 len -= this_len;
1244 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001245 this_len = min_t(unsigned int, len, subbuf->page_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (this_len)
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001247 _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 len -= this_len;
1249 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001250 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1251 memcpy(obj, subbuf->tail[0].iov_base, this_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001254/* obj is assumed to point to allocated memory of size at least len: */
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001255int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001256{
1257 struct xdr_buf subbuf;
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001258 int status;
1259
1260 status = xdr_buf_subsegment(buf, &subbuf, base, len);
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001261 if (status != 0)
1262 return status;
1263 __read_bytes_from_xdr_buf(&subbuf, obj, len);
1264 return 0;
1265}
Trond Myklebust468039e2008-12-23 15:21:31 -05001266EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001267
1268static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
1269{
1270 unsigned int this_len;
1271
1272 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1273 memcpy(subbuf->head[0].iov_base, obj, this_len);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001274 len -= this_len;
1275 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001276 this_len = min_t(unsigned int, len, subbuf->page_len);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001277 if (this_len)
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001278 _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001279 len -= this_len;
1280 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -04001281 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1282 memcpy(subbuf->tail[0].iov_base, obj, this_len);
1283}
1284
1285/* obj is assumed to point to allocated memory of size at least len: */
1286int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
1287{
1288 struct xdr_buf subbuf;
1289 int status;
1290
1291 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1292 if (status != 0)
1293 return status;
1294 __write_bytes_to_xdr_buf(&subbuf, obj, len);
1295 return 0;
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001296}
Kevin Coffmanc43abae2010-03-17 13:02:58 -04001297EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001298
1299int
Trond Myklebust1e789572006-08-31 15:09:19 -04001300xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001302 __be32 raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 int status;
1304
1305 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
1306 if (status)
1307 return status;
Benny Halevy98866b52009-08-14 17:18:49 +03001308 *obj = be32_to_cpu(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return 0;
1310}
Trond Myklebust468039e2008-12-23 15:21:31 -05001311EXPORT_SYMBOL_GPL(xdr_decode_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001313int
Trond Myklebust1e789572006-08-31 15:09:19 -04001314xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001315{
Benny Halevy9f162d22009-08-14 17:18:44 +03001316 __be32 raw = cpu_to_be32(obj);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001317
1318 return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
1319}
Trond Myklebust468039e2008-12-23 15:21:31 -05001320EXPORT_SYMBOL_GPL(xdr_encode_word);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001321
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001322/* Returns 0 on success, or else a negative error code. */
1323static int
1324xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
1325 struct xdr_array2_desc *desc, int encode)
1326{
1327 char *elem = NULL, *c;
1328 unsigned int copied = 0, todo, avail_here;
1329 struct page **ppages = NULL;
1330 int err;
1331
1332 if (encode) {
1333 if (xdr_encode_word(buf, base, desc->array_len) != 0)
1334 return -EINVAL;
1335 } else {
1336 if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
Trond Myklebust58fcb8d2005-08-10 18:15:12 -04001337 desc->array_len > desc->array_maxlen ||
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001338 (unsigned long) base + 4 + desc->array_len *
1339 desc->elem_size > buf->len)
1340 return -EINVAL;
1341 }
1342 base += 4;
1343
1344 if (!desc->xcode)
1345 return 0;
1346
1347 todo = desc->array_len * desc->elem_size;
1348
1349 /* process head */
1350 if (todo && base < buf->head->iov_len) {
1351 c = buf->head->iov_base + base;
1352 avail_here = min_t(unsigned int, todo,
1353 buf->head->iov_len - base);
1354 todo -= avail_here;
1355
1356 while (avail_here >= desc->elem_size) {
1357 err = desc->xcode(desc, c);
1358 if (err)
1359 goto out;
1360 c += desc->elem_size;
1361 avail_here -= desc->elem_size;
1362 }
1363 if (avail_here) {
1364 if (!elem) {
1365 elem = kmalloc(desc->elem_size, GFP_KERNEL);
1366 err = -ENOMEM;
1367 if (!elem)
1368 goto out;
1369 }
1370 if (encode) {
1371 err = desc->xcode(desc, elem);
1372 if (err)
1373 goto out;
1374 memcpy(c, elem, avail_here);
1375 } else
1376 memcpy(elem, c, avail_here);
1377 copied = avail_here;
1378 }
1379 base = buf->head->iov_len; /* align to start of pages */
1380 }
1381
1382 /* process pages array */
1383 base -= buf->head->iov_len;
1384 if (todo && base < buf->page_len) {
1385 unsigned int avail_page;
1386
1387 avail_here = min(todo, buf->page_len - base);
1388 todo -= avail_here;
1389
1390 base += buf->page_base;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001391 ppages = buf->pages + (base >> PAGE_SHIFT);
1392 base &= ~PAGE_MASK;
1393 avail_page = min_t(unsigned int, PAGE_SIZE - base,
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001394 avail_here);
1395 c = kmap(*ppages) + base;
1396
1397 while (avail_here) {
1398 avail_here -= avail_page;
1399 if (copied || avail_page < desc->elem_size) {
1400 unsigned int l = min(avail_page,
1401 desc->elem_size - copied);
1402 if (!elem) {
1403 elem = kmalloc(desc->elem_size,
1404 GFP_KERNEL);
1405 err = -ENOMEM;
1406 if (!elem)
1407 goto out;
1408 }
1409 if (encode) {
1410 if (!copied) {
1411 err = desc->xcode(desc, elem);
1412 if (err)
1413 goto out;
1414 }
1415 memcpy(c, elem + copied, l);
1416 copied += l;
1417 if (copied == desc->elem_size)
1418 copied = 0;
1419 } else {
1420 memcpy(elem + copied, c, l);
1421 copied += l;
1422 if (copied == desc->elem_size) {
1423 err = desc->xcode(desc, elem);
1424 if (err)
1425 goto out;
1426 copied = 0;
1427 }
1428 }
1429 avail_page -= l;
1430 c += l;
1431 }
1432 while (avail_page >= desc->elem_size) {
1433 err = desc->xcode(desc, c);
1434 if (err)
1435 goto out;
1436 c += desc->elem_size;
1437 avail_page -= desc->elem_size;
1438 }
1439 if (avail_page) {
1440 unsigned int l = min(avail_page,
1441 desc->elem_size - copied);
1442 if (!elem) {
1443 elem = kmalloc(desc->elem_size,
1444 GFP_KERNEL);
1445 err = -ENOMEM;
1446 if (!elem)
1447 goto out;
1448 }
1449 if (encode) {
1450 if (!copied) {
1451 err = desc->xcode(desc, elem);
1452 if (err)
1453 goto out;
1454 }
1455 memcpy(c, elem + copied, l);
1456 copied += l;
1457 if (copied == desc->elem_size)
1458 copied = 0;
1459 } else {
1460 memcpy(elem + copied, c, l);
1461 copied += l;
1462 if (copied == desc->elem_size) {
1463 err = desc->xcode(desc, elem);
1464 if (err)
1465 goto out;
1466 copied = 0;
1467 }
1468 }
1469 }
1470 if (avail_here) {
1471 kunmap(*ppages);
1472 ppages++;
1473 c = kmap(*ppages);
1474 }
1475
1476 avail_page = min(avail_here,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001477 (unsigned int) PAGE_SIZE);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001478 }
1479 base = buf->page_len; /* align to start of tail */
1480 }
1481
1482 /* process tail */
1483 base -= buf->page_len;
1484 if (todo) {
1485 c = buf->tail->iov_base + base;
1486 if (copied) {
1487 unsigned int l = desc->elem_size - copied;
1488
1489 if (encode)
1490 memcpy(c, elem + copied, l);
1491 else {
1492 memcpy(elem + copied, c, l);
1493 err = desc->xcode(desc, elem);
1494 if (err)
1495 goto out;
1496 }
1497 todo -= l;
1498 c += l;
1499 }
1500 while (todo) {
1501 err = desc->xcode(desc, c);
1502 if (err)
1503 goto out;
1504 c += desc->elem_size;
1505 todo -= desc->elem_size;
1506 }
1507 }
1508 err = 0;
1509
1510out:
Jesper Juhla51482b2005-11-08 09:41:34 -08001511 kfree(elem);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001512 if (ppages)
1513 kunmap(*ppages);
1514 return err;
1515}
1516
1517int
1518xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
1519 struct xdr_array2_desc *desc)
1520{
1521 if (base >= buf->len)
1522 return -EINVAL;
1523
1524 return xdr_xcode_array2(buf, base, desc, 0);
1525}
Trond Myklebust468039e2008-12-23 15:21:31 -05001526EXPORT_SYMBOL_GPL(xdr_decode_array2);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001527
1528int
1529xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
1530 struct xdr_array2_desc *desc)
1531{
1532 if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
1533 buf->head->iov_len + buf->page_len + buf->tail->iov_len)
1534 return -EINVAL;
1535
1536 return xdr_xcode_array2(buf, base, desc, 1);
1537}
Trond Myklebust468039e2008-12-23 15:21:31 -05001538EXPORT_SYMBOL_GPL(xdr_encode_array2);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001539
1540int
1541xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001542 int (*actor)(struct scatterlist *, void *), void *data)
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001543{
1544 int i, ret = 0;
Eric Dumazet95c96172012-04-15 05:58:06 +00001545 unsigned int page_len, thislen, page_offset;
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001546 struct scatterlist sg[1];
1547
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001548 sg_init_table(sg, 1);
1549
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001550 if (offset >= buf->head[0].iov_len) {
1551 offset -= buf->head[0].iov_len;
1552 } else {
1553 thislen = buf->head[0].iov_len - offset;
1554 if (thislen > len)
1555 thislen = len;
1556 sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
1557 ret = actor(sg, data);
1558 if (ret)
1559 goto out;
1560 offset = 0;
1561 len -= thislen;
1562 }
1563 if (len == 0)
1564 goto out;
1565
1566 if (offset >= buf->page_len) {
1567 offset -= buf->page_len;
1568 } else {
1569 page_len = buf->page_len - offset;
1570 if (page_len > len)
1571 page_len = len;
1572 len -= page_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001573 page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
1574 i = (offset + buf->page_base) >> PAGE_SHIFT;
1575 thislen = PAGE_SIZE - page_offset;
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001576 do {
1577 if (thislen > page_len)
1578 thislen = page_len;
Jens Axboe642f149032007-10-24 11:20:47 +02001579 sg_set_page(sg, buf->pages[i], thislen, page_offset);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001580 ret = actor(sg, data);
1581 if (ret)
1582 goto out;
1583 page_len -= thislen;
1584 i++;
1585 page_offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001586 thislen = PAGE_SIZE;
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001587 } while (page_len != 0);
1588 offset = 0;
1589 }
1590 if (len == 0)
1591 goto out;
1592 if (offset < buf->tail[0].iov_len) {
1593 thislen = buf->tail[0].iov_len - offset;
1594 if (thislen > len)
1595 thislen = len;
1596 sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
1597 ret = actor(sg, data);
1598 len -= thislen;
1599 }
1600 if (len != 0)
1601 ret = -EINVAL;
1602out:
1603 return ret;
1604}
Trond Myklebust468039e2008-12-23 15:21:31 -05001605EXPORT_SYMBOL_GPL(xdr_process_buf);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001606
Trond Myklebust5c741d42017-02-19 16:08:31 -05001607/**
Trond Myklebust0e779aa2018-03-20 17:03:05 -04001608 * xdr_stream_decode_opaque - Decode variable length opaque
1609 * @xdr: pointer to xdr_stream
1610 * @ptr: location to store opaque data
1611 * @size: size of storage buffer @ptr
1612 *
1613 * Return values:
1614 * On success, returns size of object stored in *@ptr
1615 * %-EBADMSG on XDR buffer overflow
1616 * %-EMSGSIZE on overflow of storage buffer @ptr
1617 */
1618ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
1619{
1620 ssize_t ret;
1621 void *p;
1622
1623 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
1624 if (ret <= 0)
1625 return ret;
1626 memcpy(ptr, p, ret);
1627 return ret;
1628}
1629EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
1630
1631/**
1632 * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
1633 * @xdr: pointer to xdr_stream
1634 * @ptr: location to store pointer to opaque data
1635 * @maxlen: maximum acceptable object size
1636 * @gfp_flags: GFP mask to use
1637 *
1638 * Return values:
1639 * On success, returns size of object stored in *@ptr
1640 * %-EBADMSG on XDR buffer overflow
1641 * %-EMSGSIZE if the size of the object would exceed @maxlen
1642 * %-ENOMEM on memory allocation failure
1643 */
1644ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
1645 size_t maxlen, gfp_t gfp_flags)
1646{
1647 ssize_t ret;
1648 void *p;
1649
1650 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
1651 if (ret > 0) {
1652 *ptr = kmemdup(p, ret, gfp_flags);
1653 if (*ptr != NULL)
1654 return ret;
1655 ret = -ENOMEM;
1656 }
1657 *ptr = NULL;
1658 return ret;
1659}
1660EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
1661
1662/**
1663 * xdr_stream_decode_string - Decode variable length string
1664 * @xdr: pointer to xdr_stream
1665 * @str: location to store string
1666 * @size: size of storage buffer @str
1667 *
1668 * Return values:
1669 * On success, returns length of NUL-terminated string stored in *@str
1670 * %-EBADMSG on XDR buffer overflow
1671 * %-EMSGSIZE on overflow of storage buffer @str
1672 */
1673ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
1674{
1675 ssize_t ret;
1676 void *p;
1677
1678 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
1679 if (ret > 0) {
1680 memcpy(str, p, ret);
1681 str[ret] = '\0';
1682 return strlen(str);
1683 }
1684 *str = '\0';
1685 return ret;
1686}
1687EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
1688
1689/**
Trond Myklebust5c741d42017-02-19 16:08:31 -05001690 * xdr_stream_decode_string_dup - Decode and duplicate variable length string
1691 * @xdr: pointer to xdr_stream
1692 * @str: location to store pointer to string
1693 * @maxlen: maximum acceptable string length
1694 * @gfp_flags: GFP mask to use
1695 *
1696 * Return values:
1697 * On success, returns length of NUL-terminated string stored in *@ptr
1698 * %-EBADMSG on XDR buffer overflow
1699 * %-EMSGSIZE if the size of the string would exceed @maxlen
1700 * %-ENOMEM on memory allocation failure
1701 */
1702ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
1703 size_t maxlen, gfp_t gfp_flags)
1704{
1705 void *p;
1706 ssize_t ret;
1707
1708 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
1709 if (ret > 0) {
1710 char *s = kmalloc(ret + 1, gfp_flags);
1711 if (s != NULL) {
1712 memcpy(s, p, ret);
1713 s[ret] = '\0';
1714 *str = s;
1715 return strlen(s);
1716 }
1717 ret = -ENOMEM;
1718 }
1719 *str = NULL;
1720 return ret;
1721}
1722EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);