Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 10 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/string.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/sunrpc/xdr.h> |
| 18 | #include <linux/sunrpc/msg_prot.h> |
Trond Myklebust | 9d96acb | 2018-09-13 12:22:04 -0400 | [diff] [blame] | 19 | #include <linux/bvec.h> |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 20 | #include <trace/events/sunrpc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Anna Schumaker | e6ac0ac | 2020-04-21 11:27:00 -0400 | [diff] [blame] | 22 | static void _copy_to_pages(struct page **, size_t, const char *, size_t); |
| 23 | |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | /* |
| 26 | * XDR functions for basic NFS types |
| 27 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 28 | __be32 * |
| 29 | xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | unsigned int quadlen = XDR_QUADLEN(obj->len); |
| 32 | |
| 33 | p[quadlen] = 0; /* zero trailing bytes */ |
Benny Halevy | 9f162d2 | 2009-08-14 17:18:44 +0300 | [diff] [blame] | 34 | *p++ = cpu_to_be32(obj->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | memcpy(p, obj->data, obj->len); |
| 36 | return p + XDR_QUADLEN(obj->len); |
| 37 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 38 | EXPORT_SYMBOL_GPL(xdr_encode_netobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 40 | __be32 * |
| 41 | xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | unsigned int len; |
| 44 | |
Benny Halevy | 98866b5 | 2009-08-14 17:18:49 +0300 | [diff] [blame] | 45 | if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | return NULL; |
| 47 | obj->len = len; |
| 48 | obj->data = (u8 *) p; |
| 49 | return p + XDR_QUADLEN(len); |
| 50 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 51 | EXPORT_SYMBOL_GPL(xdr_decode_netobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * xdr_encode_opaque_fixed - Encode fixed length opaque data |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 55 | * @p: pointer to current position in XDR buffer. |
| 56 | * @ptr: pointer to data to encode (or NULL) |
| 57 | * @nbytes: size of data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * |
| 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 Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 67 | __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 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 Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 81 | EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * xdr_encode_opaque - Encode variable length opaque data |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 85 | * @p: pointer to current position in XDR buffer. |
| 86 | * @ptr: pointer to data to encode (or NULL) |
| 87 | * @nbytes: size of data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * |
| 89 | * Returns the updated current XDR buffer position |
| 90 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 91 | __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
Benny Halevy | 9f162d2 | 2009-08-14 17:18:44 +0300 | [diff] [blame] | 93 | *p++ = cpu_to_be32(nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return xdr_encode_opaque_fixed(p, ptr, nbytes); |
| 95 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 96 | EXPORT_SYMBOL_GPL(xdr_encode_opaque); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 98 | __be32 * |
| 99 | xdr_encode_string(__be32 *p, const char *string) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | return xdr_encode_array(p, string, strlen(string)); |
| 102 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 103 | EXPORT_SYMBOL_GPL(xdr_encode_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 105 | __be32 * |
Chuck Lever | e5cff48 | 2007-11-01 16:56:47 -0400 | [diff] [blame] | 106 | xdr_decode_string_inplace(__be32 *p, char **sp, |
| 107 | unsigned int *lenp, unsigned int maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Chuck Lever | e5cff48 | 2007-11-01 16:56:47 -0400 | [diff] [blame] | 109 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Benny Halevy | 98866b5 | 2009-08-14 17:18:49 +0300 | [diff] [blame] | 111 | len = be32_to_cpu(*p++); |
Chuck Lever | e5cff48 | 2007-11-01 16:56:47 -0400 | [diff] [blame] | 112 | if (len > maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return NULL; |
| 114 | *lenp = len; |
| 115 | *sp = (char *) p; |
| 116 | return p + XDR_QUADLEN(len); |
| 117 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 118 | EXPORT_SYMBOL_GPL(xdr_decode_string_inplace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Chuck Lever | b4687da | 2010-09-21 16:55:48 -0400 | [diff] [blame] | 120 | /** |
| 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 | */ |
| 126 | void |
| 127 | xdr_terminate_string(struct xdr_buf *buf, const u32 len) |
| 128 | { |
| 129 | char *kaddr; |
| 130 | |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 131 | kaddr = kmap_atomic(buf->pages[0]); |
Chuck Lever | b4687da | 2010-09-21 16:55:48 -0400 | [diff] [blame] | 132 | kaddr[buf->page_base + len] = '\0'; |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 133 | kunmap_atomic(kaddr); |
Chuck Lever | b4687da | 2010-09-21 16:55:48 -0400 | [diff] [blame] | 134 | } |
Trond Myklebust | 0d961aa | 2011-07-13 19:24:15 -0400 | [diff] [blame] | 135 | EXPORT_SYMBOL_GPL(xdr_terminate_string); |
Chuck Lever | b4687da | 2010-09-21 16:55:48 -0400 | [diff] [blame] | 136 | |
Trond Myklebust | 9d96acb | 2018-09-13 12:22:04 -0400 | [diff] [blame] | 137 | size_t |
| 138 | xdr_buf_pagecount(struct xdr_buf *buf) |
| 139 | { |
| 140 | if (!buf->page_len) |
| 141 | return 0; |
| 142 | return (buf->page_base + buf->page_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp) |
| 147 | { |
| 148 | size_t i, n = xdr_buf_pagecount(buf); |
| 149 | |
| 150 | if (n != 0 && buf->bvec == NULL) { |
| 151 | buf->bvec = kmalloc_array(n, sizeof(buf->bvec[0]), gfp); |
| 152 | if (!buf->bvec) |
| 153 | return -ENOMEM; |
| 154 | for (i = 0; i < n; i++) { |
| 155 | buf->bvec[i].bv_page = buf->pages[i]; |
| 156 | buf->bvec[i].bv_len = PAGE_SIZE; |
| 157 | buf->bvec[i].bv_offset = 0; |
| 158 | } |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | xdr_free_bvec(struct xdr_buf *buf) |
| 165 | { |
| 166 | kfree(buf->bvec); |
| 167 | buf->bvec = NULL; |
| 168 | } |
| 169 | |
Chuck Lever | cf500ba | 2019-02-11 11:25:20 -0500 | [diff] [blame] | 170 | /** |
| 171 | * xdr_inline_pages - Prepare receive buffer for a large reply |
| 172 | * @xdr: xdr_buf into which reply will be placed |
| 173 | * @offset: expected offset where data payload will start, in bytes |
| 174 | * @pages: vector of struct page pointers |
| 175 | * @base: offset in first page where receive should start, in bytes |
| 176 | * @len: expected size of the upper layer data payload, in bytes |
| 177 | * |
| 178 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset, |
| 181 | struct page **pages, unsigned int base, unsigned int len) |
| 182 | { |
| 183 | struct kvec *head = xdr->head; |
| 184 | struct kvec *tail = xdr->tail; |
| 185 | char *buf = (char *)head->iov_base; |
| 186 | unsigned int buflen = head->iov_len; |
| 187 | |
| 188 | head->iov_len = offset; |
| 189 | |
| 190 | xdr->pages = pages; |
| 191 | xdr->page_base = base; |
| 192 | xdr->page_len = len; |
| 193 | |
| 194 | tail->iov_base = buf + offset; |
| 195 | tail->iov_len = buflen - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | xdr->buflen += len; |
| 197 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 198 | EXPORT_SYMBOL_GPL(xdr_inline_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /* |
| 201 | * Helper routines for doing 'memmove' like operations on a struct xdr_buf |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 202 | */ |
| 203 | |
| 204 | /** |
Anna Schumaker | e6ac0ac | 2020-04-21 11:27:00 -0400 | [diff] [blame] | 205 | * _shift_data_left_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]', |
| 214 | * then its address is given as (i << PAGE_CACHE_SHIFT) + base |
| 215 | * Alse note: pgto_base must be < pgfrom_base, but the memory areas |
| 216 | * they point to may overlap. |
| 217 | */ |
| 218 | static void |
| 219 | _shift_data_left_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(pgfrom_base <= pgto_base); |
| 227 | |
Trond Myklebust | c54e959 | 2020-12-07 13:30:46 -0500 | [diff] [blame^] | 228 | if (!len) |
| 229 | return; |
| 230 | |
Anna Schumaker | e6ac0ac | 2020-04-21 11:27:00 -0400 | [diff] [blame] | 231 | pgto = pages + (pgto_base >> PAGE_SHIFT); |
| 232 | pgfrom = pages + (pgfrom_base >> PAGE_SHIFT); |
| 233 | |
| 234 | pgto_base &= ~PAGE_MASK; |
| 235 | pgfrom_base &= ~PAGE_MASK; |
| 236 | |
| 237 | do { |
| 238 | if (pgto_base >= PAGE_SIZE) { |
| 239 | pgto_base = 0; |
| 240 | pgto++; |
| 241 | } |
| 242 | if (pgfrom_base >= PAGE_SIZE){ |
| 243 | pgfrom_base = 0; |
| 244 | pgfrom++; |
| 245 | } |
| 246 | |
| 247 | copy = len; |
| 248 | if (copy > (PAGE_SIZE - pgto_base)) |
| 249 | copy = PAGE_SIZE - pgto_base; |
| 250 | if (copy > (PAGE_SIZE - pgfrom_base)) |
| 251 | copy = PAGE_SIZE - pgfrom_base; |
| 252 | |
| 253 | vto = kmap_atomic(*pgto); |
| 254 | if (*pgto != *pgfrom) { |
| 255 | vfrom = kmap_atomic(*pgfrom); |
| 256 | memcpy(vto + pgto_base, vfrom + pgfrom_base, copy); |
| 257 | kunmap_atomic(vfrom); |
| 258 | } else |
| 259 | memmove(vto + pgto_base, vto + pgfrom_base, copy); |
| 260 | flush_dcache_page(*pgto); |
| 261 | kunmap_atomic(vto); |
| 262 | |
| 263 | pgto_base += copy; |
| 264 | pgfrom_base += copy; |
| 265 | |
| 266 | } while ((len -= copy) != 0); |
| 267 | } |
| 268 | |
| 269 | static void |
| 270 | _shift_data_left_tail(struct xdr_buf *buf, unsigned int pgto, size_t len) |
| 271 | { |
| 272 | struct kvec *tail = buf->tail; |
| 273 | |
| 274 | if (len > tail->iov_len) |
| 275 | len = tail->iov_len; |
| 276 | |
| 277 | _copy_to_pages(buf->pages, |
| 278 | buf->page_base + pgto, |
| 279 | (char *)tail->iov_base, |
| 280 | len); |
| 281 | tail->iov_len -= len; |
| 282 | |
| 283 | if (tail->iov_len > 0) |
| 284 | memmove((char *)tail->iov_base, |
| 285 | tail->iov_base + len, |
| 286 | tail->iov_len); |
| 287 | } |
| 288 | |
| 289 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | * _shift_data_right_pages |
| 291 | * @pages: vector of pages containing both the source and dest memory area. |
| 292 | * @pgto_base: page vector address of destination |
| 293 | * @pgfrom_base: page vector address of source |
| 294 | * @len: number of bytes to copy |
| 295 | * |
| 296 | * Note: the addresses pgto_base and pgfrom_base are both calculated in |
| 297 | * the same way: |
| 298 | * if a memory area starts at byte 'base' in page 'pages[i]', |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 299 | * then its address is given as (i << PAGE_SHIFT) + base |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | * Also note: pgfrom_base must be < pgto_base, but the memory areas |
| 301 | * they point to may overlap. |
| 302 | */ |
| 303 | static void |
| 304 | _shift_data_right_pages(struct page **pages, size_t pgto_base, |
| 305 | size_t pgfrom_base, size_t len) |
| 306 | { |
| 307 | struct page **pgfrom, **pgto; |
| 308 | char *vfrom, *vto; |
| 309 | size_t copy; |
| 310 | |
| 311 | BUG_ON(pgto_base <= pgfrom_base); |
| 312 | |
Trond Myklebust | c54e959 | 2020-12-07 13:30:46 -0500 | [diff] [blame^] | 313 | if (!len) |
| 314 | return; |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | pgto_base += len; |
| 317 | pgfrom_base += len; |
| 318 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 319 | pgto = pages + (pgto_base >> PAGE_SHIFT); |
| 320 | pgfrom = pages + (pgfrom_base >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 322 | pgto_base &= ~PAGE_MASK; |
| 323 | pgfrom_base &= ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | do { |
| 326 | /* Are any pointers crossing a page boundary? */ |
| 327 | if (pgto_base == 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 328 | pgto_base = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | pgto--; |
| 330 | } |
| 331 | if (pgfrom_base == 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 332 | pgfrom_base = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | pgfrom--; |
| 334 | } |
| 335 | |
| 336 | copy = len; |
| 337 | if (copy > pgto_base) |
| 338 | copy = pgto_base; |
| 339 | if (copy > pgfrom_base) |
| 340 | copy = pgfrom_base; |
| 341 | pgto_base -= copy; |
| 342 | pgfrom_base -= copy; |
| 343 | |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 344 | vto = kmap_atomic(*pgto); |
Trond Myklebust | 347e223 | 2013-08-28 13:35:13 -0400 | [diff] [blame] | 345 | if (*pgto != *pgfrom) { |
| 346 | vfrom = kmap_atomic(*pgfrom); |
| 347 | memcpy(vto + pgto_base, vfrom + pgfrom_base, copy); |
| 348 | kunmap_atomic(vfrom); |
| 349 | } else |
| 350 | memmove(vto + pgto_base, vto + pgfrom_base, copy); |
Trond Myklebust | bce3481 | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 351 | flush_dcache_page(*pgto); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 352 | kunmap_atomic(vto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | } while ((len -= copy) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Anna Schumaker | 43f0f08 | 2020-05-06 13:21:30 -0400 | [diff] [blame] | 357 | static unsigned int |
| 358 | _shift_data_right_tail(struct xdr_buf *buf, unsigned int pgfrom, size_t len) |
| 359 | { |
| 360 | struct kvec *tail = buf->tail; |
| 361 | unsigned int tailbuf_len; |
| 362 | unsigned int result = 0; |
| 363 | size_t copy; |
| 364 | |
| 365 | tailbuf_len = buf->buflen - buf->head->iov_len - buf->page_len; |
| 366 | |
| 367 | /* Shift the tail first */ |
| 368 | if (tailbuf_len != 0) { |
| 369 | unsigned int free_space = tailbuf_len - tail->iov_len; |
| 370 | |
| 371 | if (len < free_space) |
| 372 | free_space = len; |
| 373 | if (len > free_space) |
| 374 | len = free_space; |
| 375 | |
| 376 | tail->iov_len += free_space; |
| 377 | copy = len; |
| 378 | |
| 379 | if (tail->iov_len > len) { |
| 380 | char *p = (char *)tail->iov_base + len; |
| 381 | memmove(p, tail->iov_base, tail->iov_len - free_space); |
| 382 | result += tail->iov_len - free_space; |
| 383 | } else |
| 384 | copy = tail->iov_len; |
| 385 | |
| 386 | /* Copy from the inlined pages into the tail */ |
| 387 | _copy_from_pages((char *)tail->iov_base, |
| 388 | buf->pages, |
| 389 | buf->page_base + pgfrom, |
| 390 | copy); |
| 391 | result += copy; |
| 392 | } |
| 393 | |
| 394 | return result; |
| 395 | } |
| 396 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 397 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | * _copy_to_pages |
| 399 | * @pages: array of pages |
| 400 | * @pgbase: page vector address of destination |
| 401 | * @p: pointer to source data |
| 402 | * @len: length |
| 403 | * |
| 404 | * Copies data from an arbitrary memory location into an array of pages |
| 405 | * The copy is assumed to be non-overlapping. |
| 406 | */ |
| 407 | static void |
| 408 | _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len) |
| 409 | { |
| 410 | struct page **pgto; |
| 411 | char *vto; |
| 412 | size_t copy; |
| 413 | |
Trond Myklebust | c54e959 | 2020-12-07 13:30:46 -0500 | [diff] [blame^] | 414 | if (!len) |
| 415 | return; |
| 416 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 417 | pgto = pages + (pgbase >> PAGE_SHIFT); |
| 418 | pgbase &= ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Trond Myklebust | daeba89 | 2008-03-31 17:02:02 -0400 | [diff] [blame] | 420 | for (;;) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 421 | copy = PAGE_SIZE - pgbase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | if (copy > len) |
| 423 | copy = len; |
| 424 | |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 425 | vto = kmap_atomic(*pgto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | memcpy(vto + pgbase, p, copy); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 427 | kunmap_atomic(vto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Trond Myklebust | daeba89 | 2008-03-31 17:02:02 -0400 | [diff] [blame] | 429 | len -= copy; |
| 430 | if (len == 0) |
| 431 | break; |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | pgbase += copy; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 434 | if (pgbase == PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | flush_dcache_page(*pgto); |
| 436 | pgbase = 0; |
| 437 | pgto++; |
| 438 | } |
| 439 | p += copy; |
Trond Myklebust | daeba89 | 2008-03-31 17:02:02 -0400 | [diff] [blame] | 440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | flush_dcache_page(*pgto); |
| 442 | } |
| 443 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 444 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | * _copy_from_pages |
| 446 | * @p: pointer to destination |
| 447 | * @pages: array of pages |
| 448 | * @pgbase: offset of source data |
| 449 | * @len: length |
| 450 | * |
| 451 | * Copies data into an arbitrary memory location from an array of pages |
| 452 | * The copy is assumed to be non-overlapping. |
| 453 | */ |
Andy Adamson | bf118a3 | 2011-12-07 11:55:27 -0500 | [diff] [blame] | 454 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len) |
| 456 | { |
| 457 | struct page **pgfrom; |
| 458 | char *vfrom; |
| 459 | size_t copy; |
| 460 | |
Trond Myklebust | c54e959 | 2020-12-07 13:30:46 -0500 | [diff] [blame^] | 461 | if (!len) |
| 462 | return; |
| 463 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 464 | pgfrom = pages + (pgbase >> PAGE_SHIFT); |
| 465 | pgbase &= ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | do { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 468 | copy = PAGE_SIZE - pgbase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | if (copy > len) |
| 470 | copy = len; |
| 471 | |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 472 | vfrom = kmap_atomic(*pgfrom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | memcpy(p, vfrom + pgbase, copy); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 474 | kunmap_atomic(vfrom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | pgbase += copy; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 477 | if (pgbase == PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | pgbase = 0; |
| 479 | pgfrom++; |
| 480 | } |
| 481 | p += copy; |
| 482 | |
| 483 | } while ((len -= copy) != 0); |
| 484 | } |
Andy Adamson | bf118a3 | 2011-12-07 11:55:27 -0500 | [diff] [blame] | 485 | EXPORT_SYMBOL_GPL(_copy_from_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 487 | /** |
Anna Schumaker | 84ce182 | 2014-05-28 13:38:53 -0400 | [diff] [blame] | 488 | * _zero_pages |
| 489 | * @pages: array of pages |
| 490 | * @pgbase: beginning page vector address |
| 491 | * @len: length |
| 492 | */ |
| 493 | static void |
| 494 | _zero_pages(struct page **pages, size_t pgbase, size_t len) |
| 495 | { |
| 496 | struct page **page; |
| 497 | char *vpage; |
| 498 | size_t zero; |
| 499 | |
| 500 | page = pages + (pgbase >> PAGE_SHIFT); |
| 501 | pgbase &= ~PAGE_MASK; |
| 502 | |
| 503 | do { |
| 504 | zero = PAGE_SIZE - pgbase; |
| 505 | if (zero > len) |
| 506 | zero = len; |
| 507 | |
| 508 | vpage = kmap_atomic(*page); |
| 509 | memset(vpage + pgbase, 0, zero); |
| 510 | kunmap_atomic(vpage); |
| 511 | |
| 512 | flush_dcache_page(*page); |
| 513 | pgbase = 0; |
| 514 | page++; |
| 515 | |
| 516 | } while ((len -= zero) != 0); |
| 517 | } |
| 518 | |
| 519 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | * xdr_shrink_bufhead |
| 521 | * @buf: xdr_buf |
| 522 | * @len: bytes to remove from buf->head[0] |
| 523 | * |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 524 | * Shrinks XDR buffer's header kvec buf->head[0] by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | * 'len' bytes. The extra data is not lost, but is instead |
| 526 | * moved into the inlined pages and/or the tail. |
| 527 | */ |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 528 | static unsigned int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | xdr_shrink_bufhead(struct xdr_buf *buf, size_t len) |
| 530 | { |
| 531 | struct kvec *head, *tail; |
| 532 | size_t copy, offs; |
| 533 | unsigned int pglen = buf->page_len; |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 534 | unsigned int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 536 | result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | tail = buf->tail; |
| 538 | head = buf->head; |
Weston Andros Adamson | 18e624a | 2012-10-23 10:43:42 -0400 | [diff] [blame] | 539 | |
| 540 | WARN_ON_ONCE(len > head->iov_len); |
| 541 | if (len > head->iov_len) |
| 542 | len = head->iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
| 544 | /* Shift the tail first */ |
| 545 | if (tail->iov_len != 0) { |
| 546 | if (tail->iov_len > len) { |
| 547 | copy = tail->iov_len - len; |
| 548 | memmove((char *)tail->iov_base + len, |
| 549 | tail->iov_base, copy); |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 550 | result += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | /* Copy from the inlined pages into the tail */ |
| 553 | copy = len; |
| 554 | if (copy > pglen) |
| 555 | copy = pglen; |
| 556 | offs = len - copy; |
| 557 | if (offs >= tail->iov_len) |
| 558 | copy = 0; |
| 559 | else if (copy > tail->iov_len - offs) |
| 560 | copy = tail->iov_len - offs; |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 561 | if (copy != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | _copy_from_pages((char *)tail->iov_base + offs, |
| 563 | buf->pages, |
| 564 | buf->page_base + pglen + offs - len, |
| 565 | copy); |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 566 | result += copy; |
| 567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | /* Do we also need to copy data from the head into the tail ? */ |
| 569 | if (len > pglen) { |
| 570 | offs = copy = len - pglen; |
| 571 | if (copy > tail->iov_len) |
| 572 | copy = tail->iov_len; |
| 573 | memcpy(tail->iov_base, |
| 574 | (char *)head->iov_base + |
| 575 | head->iov_len - offs, |
| 576 | copy); |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 577 | result += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | /* Now handle pages */ |
| 581 | if (pglen != 0) { |
| 582 | if (pglen > len) |
| 583 | _shift_data_right_pages(buf->pages, |
| 584 | buf->page_base + len, |
| 585 | buf->page_base, |
| 586 | pglen - len); |
| 587 | copy = len; |
| 588 | if (len > pglen) |
| 589 | copy = pglen; |
| 590 | _copy_to_pages(buf->pages, buf->page_base, |
| 591 | (char *)head->iov_base + head->iov_len - len, |
| 592 | copy); |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 593 | result += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | head->iov_len -= len; |
| 596 | buf->buflen -= len; |
| 597 | /* Have we truncated the message? */ |
| 598 | if (buf->len > buf->buflen) |
| 599 | buf->len = buf->buflen; |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 600 | |
| 601 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 604 | /** |
Chuck Lever | e8d70b3 | 2019-11-15 08:39:07 -0500 | [diff] [blame] | 605 | * xdr_shrink_pagelen - shrinks buf->pages by up to @len bytes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | * @buf: xdr_buf |
| 607 | * @len: bytes to remove from buf->pages |
| 608 | * |
Chuck Lever | e8d70b3 | 2019-11-15 08:39:07 -0500 | [diff] [blame] | 609 | * The extra data is not lost, but is instead moved into buf->tail. |
| 610 | * Returns the actual number of bytes moved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | */ |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 612 | static unsigned int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | xdr_shrink_pagelen(struct xdr_buf *buf, size_t len) |
| 614 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | unsigned int pglen = buf->page_len; |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 616 | unsigned int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Chuck Lever | e8d70b3 | 2019-11-15 08:39:07 -0500 | [diff] [blame] | 618 | if (len > buf->page_len) |
| 619 | len = buf-> page_len; |
Trond Myklebust | cf187c2 | 2010-08-29 12:13:16 -0400 | [diff] [blame] | 620 | |
Anna Schumaker | 43f0f08 | 2020-05-06 13:21:30 -0400 | [diff] [blame] | 621 | result = _shift_data_right_tail(buf, pglen - len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | buf->page_len -= len; |
| 623 | buf->buflen -= len; |
| 624 | /* Have we truncated the message? */ |
| 625 | if (buf->len > buf->buflen) |
| 626 | buf->len = buf->buflen; |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 627 | |
| 628 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | void |
| 632 | xdr_shift_buf(struct xdr_buf *buf, size_t len) |
| 633 | { |
| 634 | xdr_shrink_bufhead(buf, len); |
| 635 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 636 | EXPORT_SYMBOL_GPL(xdr_shift_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
| 638 | /** |
Trond Myklebust | 4517d52 | 2012-06-21 17:14:46 -0400 | [diff] [blame] | 639 | * xdr_stream_pos - Return the current offset from the start of the xdr_stream |
| 640 | * @xdr: pointer to struct xdr_stream |
| 641 | */ |
| 642 | unsigned int xdr_stream_pos(const struct xdr_stream *xdr) |
| 643 | { |
| 644 | return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2; |
| 645 | } |
| 646 | EXPORT_SYMBOL_GPL(xdr_stream_pos); |
| 647 | |
| 648 | /** |
Anna Schumaker | cf1f08c | 2020-04-17 11:00:24 -0400 | [diff] [blame] | 649 | * xdr_page_pos - Return the current offset from the start of the xdr pages |
| 650 | * @xdr: pointer to struct xdr_stream |
| 651 | */ |
| 652 | unsigned int xdr_page_pos(const struct xdr_stream *xdr) |
| 653 | { |
| 654 | unsigned int pos = xdr_stream_pos(xdr); |
| 655 | |
| 656 | WARN_ON(pos < xdr->buf->head[0].iov_len); |
| 657 | return pos - xdr->buf->head[0].iov_len; |
| 658 | } |
| 659 | EXPORT_SYMBOL_GPL(xdr_page_pos); |
| 660 | |
| 661 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | * xdr_init_encode - Initialize a struct xdr_stream for sending data. |
| 663 | * @xdr: pointer to xdr_stream struct |
| 664 | * @buf: pointer to XDR buffer in which to encode data |
| 665 | * @p: current pointer inside XDR buffer |
Chuck Lever | 0ccc61b | 2019-02-11 11:24:05 -0500 | [diff] [blame] | 666 | * @rqst: pointer to controlling rpc_rqst, for debugging |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | * |
| 668 | * Note: at the moment the RPC client only passes the length of our |
| 669 | * scratch buffer in the xdr_buf's header kvec. Previously this |
| 670 | * meant we needed to call xdr_adjust_iovec() after encoding the |
| 671 | * data. With the new scheme, the xdr_stream manages the details |
| 672 | * of the buffer length, and takes care of adjusting the kvec |
| 673 | * length for us. |
| 674 | */ |
Chuck Lever | 0ccc61b | 2019-02-11 11:24:05 -0500 | [diff] [blame] | 675 | void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p, |
| 676 | struct rpc_rqst *rqst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
| 678 | struct kvec *iov = buf->head; |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 679 | int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 681 | xdr_set_scratch_buffer(xdr, NULL, 0); |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 682 | BUG_ON(scratch_len < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | xdr->buf = buf; |
| 684 | xdr->iov = iov; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 685 | xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len); |
| 686 | xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len); |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 687 | BUG_ON(iov->iov_len > scratch_len); |
| 688 | |
| 689 | if (p != xdr->p && p != NULL) { |
| 690 | size_t len; |
| 691 | |
| 692 | BUG_ON(p < xdr->p || p > xdr->end); |
| 693 | len = (char *)p - (char *)xdr->p; |
| 694 | xdr->p = p; |
| 695 | buf->len += len; |
| 696 | iov->iov_len += len; |
| 697 | } |
Chuck Lever | 0ccc61b | 2019-02-11 11:24:05 -0500 | [diff] [blame] | 698 | xdr->rqst = rqst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 700 | EXPORT_SYMBOL_GPL(xdr_init_encode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | /** |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 703 | * xdr_commit_encode - Ensure all data is written to buffer |
| 704 | * @xdr: pointer to xdr_stream |
| 705 | * |
| 706 | * We handle encoding across page boundaries by giving the caller a |
| 707 | * temporary location to write to, then later copying the data into |
| 708 | * place; xdr_commit_encode does that copying. |
| 709 | * |
| 710 | * Normally the caller doesn't need to call this directly, as the |
| 711 | * following xdr_reserve_space will do it. But an explicit call may be |
| 712 | * required at the end of encoding, or any other time when the xdr_buf |
| 713 | * data might be read. |
| 714 | */ |
Chuck Lever | 95bd830 | 2019-08-19 18:37:05 -0400 | [diff] [blame] | 715 | inline void xdr_commit_encode(struct xdr_stream *xdr) |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 716 | { |
| 717 | int shift = xdr->scratch.iov_len; |
| 718 | void *page; |
| 719 | |
| 720 | if (shift == 0) |
| 721 | return; |
| 722 | page = page_address(*xdr->page_ptr); |
| 723 | memcpy(xdr->scratch.iov_base, page, shift); |
| 724 | memmove(page, page + shift, (void *)xdr->p - page); |
| 725 | xdr->scratch.iov_len = 0; |
| 726 | } |
| 727 | EXPORT_SYMBOL_GPL(xdr_commit_encode); |
| 728 | |
Trond Myklebust | 22cb438 | 2014-07-12 18:01:02 -0400 | [diff] [blame] | 729 | static __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr, |
| 730 | size_t nbytes) |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 731 | { |
YueHaibing | 025911a | 2018-11-08 02:04:57 +0000 | [diff] [blame] | 732 | __be32 *p; |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 733 | int space_left; |
| 734 | int frag1bytes, frag2bytes; |
| 735 | |
| 736 | if (nbytes > PAGE_SIZE) |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 737 | goto out_overflow; /* Bigger buffers require special handling */ |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 738 | if (xdr->buf->len + nbytes > xdr->buf->buflen) |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 739 | goto out_overflow; /* Sorry, we're totally out of space */ |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 740 | frag1bytes = (xdr->end - xdr->p) << 2; |
| 741 | frag2bytes = nbytes - frag1bytes; |
| 742 | if (xdr->iov) |
| 743 | xdr->iov->iov_len += frag1bytes; |
J. Bruce Fields | 05638dc | 2014-06-02 12:05:47 -0400 | [diff] [blame] | 744 | else |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 745 | xdr->buf->page_len += frag1bytes; |
J. Bruce Fields | 05638dc | 2014-06-02 12:05:47 -0400 | [diff] [blame] | 746 | xdr->page_ptr++; |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 747 | xdr->iov = NULL; |
| 748 | /* |
| 749 | * If the last encode didn't end exactly on a page boundary, the |
| 750 | * next one will straddle boundaries. Encode into the next |
| 751 | * page, then copy it back later in xdr_commit_encode. We use |
| 752 | * the "scratch" iov to track any temporarily unused fragment of |
| 753 | * space at the end of the previous buffer: |
| 754 | */ |
| 755 | xdr->scratch.iov_base = xdr->p; |
| 756 | xdr->scratch.iov_len = frag1bytes; |
| 757 | p = page_address(*xdr->page_ptr); |
| 758 | /* |
| 759 | * Note this is where the next encode will start after we've |
| 760 | * shifted this one back: |
| 761 | */ |
| 762 | xdr->p = (void *)p + frag2bytes; |
| 763 | space_left = xdr->buf->buflen - xdr->buf->len; |
| 764 | xdr->end = (void *)p + min_t(int, space_left, PAGE_SIZE); |
| 765 | xdr->buf->page_len += frag2bytes; |
| 766 | xdr->buf->len += nbytes; |
| 767 | return p; |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 768 | out_overflow: |
| 769 | trace_rpc_xdr_overflow(xdr, nbytes); |
| 770 | return NULL; |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | * xdr_reserve_space - Reserve buffer space for sending |
| 775 | * @xdr: pointer to xdr_stream |
| 776 | * @nbytes: number of bytes to reserve |
| 777 | * |
| 778 | * Checks that we have enough buffer space to encode 'nbytes' more |
| 779 | * bytes of data. If so, update the total xdr_buf length, and |
| 780 | * adjust the length of the current kvec. |
| 781 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 782 | __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 784 | __be32 *p = xdr->p; |
| 785 | __be32 *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 787 | xdr_commit_encode(xdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | /* align nbytes on the next 32-bit boundary */ |
| 789 | nbytes += 3; |
| 790 | nbytes &= ~3; |
| 791 | q = p + (nbytes >> 2); |
| 792 | if (unlikely(q > xdr->end || q < p)) |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 793 | return xdr_get_next_encode_buffer(xdr, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | xdr->p = q; |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 795 | if (xdr->iov) |
| 796 | xdr->iov->iov_len += nbytes; |
| 797 | else |
| 798 | xdr->buf->page_len += nbytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | xdr->buf->len += nbytes; |
| 800 | return p; |
| 801 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 802 | EXPORT_SYMBOL_GPL(xdr_reserve_space); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
Anna Schumaker | 403217f | 2020-08-17 12:53:06 -0400 | [diff] [blame] | 804 | |
| 805 | /** |
| 806 | * xdr_reserve_space_vec - Reserves a large amount of buffer space for sending |
| 807 | * @xdr: pointer to xdr_stream |
| 808 | * @vec: pointer to a kvec array |
| 809 | * @nbytes: number of bytes to reserve |
| 810 | * |
| 811 | * Reserves enough buffer space to encode 'nbytes' of data and stores the |
| 812 | * pointers in 'vec'. The size argument passed to xdr_reserve_space() is |
| 813 | * determined based on the number of bytes remaining in the current page to |
| 814 | * avoid invalidating iov_base pointers when xdr_commit_encode() is called. |
| 815 | */ |
| 816 | int xdr_reserve_space_vec(struct xdr_stream *xdr, struct kvec *vec, size_t nbytes) |
| 817 | { |
| 818 | int thislen; |
| 819 | int v = 0; |
| 820 | __be32 *p; |
| 821 | |
| 822 | /* |
| 823 | * svcrdma requires every READ payload to start somewhere |
| 824 | * in xdr->pages. |
| 825 | */ |
| 826 | if (xdr->iov == xdr->buf->head) { |
| 827 | xdr->iov = NULL; |
| 828 | xdr->end = xdr->p; |
| 829 | } |
| 830 | |
| 831 | while (nbytes) { |
| 832 | thislen = xdr->buf->page_len % PAGE_SIZE; |
| 833 | thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen); |
| 834 | |
| 835 | p = xdr_reserve_space(xdr, thislen); |
| 836 | if (!p) |
| 837 | return -EIO; |
| 838 | |
| 839 | vec[v].iov_base = p; |
| 840 | vec[v].iov_len = thislen; |
| 841 | v++; |
| 842 | nbytes -= thislen; |
| 843 | } |
| 844 | |
| 845 | return v; |
| 846 | } |
| 847 | EXPORT_SYMBOL_GPL(xdr_reserve_space_vec); |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | /** |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 850 | * xdr_truncate_encode - truncate an encode buffer |
| 851 | * @xdr: pointer to xdr_stream |
| 852 | * @len: new length of buffer |
| 853 | * |
| 854 | * Truncates the xdr stream, so that xdr->buf->len == len, |
| 855 | * and xdr->p points at offset len from the start of the buffer, and |
| 856 | * head, tail, and page lengths are adjusted to correspond. |
| 857 | * |
| 858 | * If this means moving xdr->p to a different buffer, we assume that |
Randy Dunlap | 1cc5213 | 2020-08-22 18:07:38 -0700 | [diff] [blame] | 859 | * the end pointer should be set to the end of the current page, |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 860 | * except in the case of the head buffer when we assume the head |
| 861 | * buffer's current length represents the end of the available buffer. |
| 862 | * |
| 863 | * This is *not* safe to use on a buffer that already has inlined page |
| 864 | * cache pages (as in a zero-copy server read reply), except for the |
| 865 | * simple case of truncating from one position in the tail to another. |
| 866 | * |
| 867 | */ |
| 868 | void xdr_truncate_encode(struct xdr_stream *xdr, size_t len) |
| 869 | { |
| 870 | struct xdr_buf *buf = xdr->buf; |
| 871 | struct kvec *head = buf->head; |
| 872 | struct kvec *tail = buf->tail; |
| 873 | int fraglen; |
J. Bruce Fields | 49a068f | 2014-12-22 16:14:51 -0500 | [diff] [blame] | 874 | int new; |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 875 | |
| 876 | if (len > buf->len) { |
| 877 | WARN_ON_ONCE(1); |
| 878 | return; |
| 879 | } |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 880 | xdr_commit_encode(xdr); |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 881 | |
| 882 | fraglen = min_t(int, buf->len - len, tail->iov_len); |
| 883 | tail->iov_len -= fraglen; |
| 884 | buf->len -= fraglen; |
J. Bruce Fields | ed38c06 | 2014-09-19 17:21:35 -0400 | [diff] [blame] | 885 | if (tail->iov_len) { |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 886 | xdr->p = tail->iov_base + tail->iov_len; |
J. Bruce Fields | 280caac | 2014-10-01 11:36:31 -0400 | [diff] [blame] | 887 | WARN_ON_ONCE(!xdr->end); |
| 888 | WARN_ON_ONCE(!xdr->iov); |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | WARN_ON_ONCE(fraglen); |
| 892 | fraglen = min_t(int, buf->len - len, buf->page_len); |
| 893 | buf->page_len -= fraglen; |
| 894 | buf->len -= fraglen; |
| 895 | |
| 896 | new = buf->page_base + buf->page_len; |
J. Bruce Fields | 49a068f | 2014-12-22 16:14:51 -0500 | [diff] [blame] | 897 | |
| 898 | xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT); |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 899 | |
J. Bruce Fields | ed38c06 | 2014-09-19 17:21:35 -0400 | [diff] [blame] | 900 | if (buf->page_len) { |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 901 | xdr->p = page_address(*xdr->page_ptr); |
| 902 | xdr->end = (void *)xdr->p + PAGE_SIZE; |
| 903 | xdr->p = (void *)xdr->p + (new % PAGE_SIZE); |
J. Bruce Fields | 280caac | 2014-10-01 11:36:31 -0400 | [diff] [blame] | 904 | WARN_ON_ONCE(xdr->iov); |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 905 | return; |
| 906 | } |
Frank Sorenson | 5d7a5bc | 2018-10-30 15:10:40 -0500 | [diff] [blame] | 907 | if (fraglen) |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 908 | xdr->end = head->iov_base + head->iov_len; |
| 909 | /* (otherwise assume xdr->end is already set) */ |
Frank Sorenson | 5d7a5bc | 2018-10-30 15:10:40 -0500 | [diff] [blame] | 910 | xdr->page_ptr--; |
J. Bruce Fields | 3e19ce7 | 2014-02-25 17:44:21 -0500 | [diff] [blame] | 911 | head->iov_len = len; |
| 912 | buf->len = len; |
| 913 | xdr->p = head->iov_base + head->iov_len; |
| 914 | xdr->iov = buf->head; |
| 915 | } |
| 916 | EXPORT_SYMBOL(xdr_truncate_encode); |
| 917 | |
| 918 | /** |
J. Bruce Fields | db3f58a | 2014-03-06 13:22:18 -0500 | [diff] [blame] | 919 | * xdr_restrict_buflen - decrease available buffer space |
| 920 | * @xdr: pointer to xdr_stream |
| 921 | * @newbuflen: new maximum number of bytes available |
| 922 | * |
| 923 | * Adjust our idea of how much space is available in the buffer. |
| 924 | * If we've already used too much space in the buffer, returns -1. |
| 925 | * If the available space is already smaller than newbuflen, returns 0 |
| 926 | * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen |
| 927 | * and ensures xdr->end is set at most offset newbuflen from the start |
| 928 | * of the buffer. |
| 929 | */ |
| 930 | int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen) |
| 931 | { |
| 932 | struct xdr_buf *buf = xdr->buf; |
| 933 | int left_in_this_buf = (void *)xdr->end - (void *)xdr->p; |
| 934 | int end_offset = buf->len + left_in_this_buf; |
| 935 | |
| 936 | if (newbuflen < 0 || newbuflen < buf->len) |
| 937 | return -1; |
| 938 | if (newbuflen > buf->buflen) |
| 939 | return 0; |
| 940 | if (newbuflen < end_offset) |
| 941 | xdr->end = (void *)xdr->end + newbuflen - end_offset; |
| 942 | buf->buflen = newbuflen; |
| 943 | return 0; |
| 944 | } |
| 945 | EXPORT_SYMBOL(xdr_restrict_buflen); |
| 946 | |
| 947 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | * xdr_write_pages - Insert a list of pages into an XDR buffer for sending |
| 949 | * @xdr: pointer to xdr_stream |
| 950 | * @pages: list of pages |
| 951 | * @base: offset of first byte |
| 952 | * @len: length of data in bytes |
| 953 | * |
| 954 | */ |
| 955 | void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base, |
| 956 | unsigned int len) |
| 957 | { |
| 958 | struct xdr_buf *buf = xdr->buf; |
| 959 | struct kvec *iov = buf->tail; |
| 960 | buf->pages = pages; |
| 961 | buf->page_base = base; |
| 962 | buf->page_len = len; |
| 963 | |
| 964 | iov->iov_base = (char *)xdr->p; |
| 965 | iov->iov_len = 0; |
| 966 | xdr->iov = iov; |
| 967 | |
| 968 | if (len & 3) { |
| 969 | unsigned int pad = 4 - (len & 3); |
| 970 | |
| 971 | BUG_ON(xdr->p >= xdr->end); |
| 972 | iov->iov_base = (char *)xdr->p + (len & 3); |
| 973 | iov->iov_len += pad; |
| 974 | len += pad; |
| 975 | *xdr->p++ = 0; |
| 976 | } |
| 977 | buf->buflen += len; |
| 978 | buf->len += len; |
| 979 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 980 | EXPORT_SYMBOL_GPL(xdr_write_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
Trond Myklebust | 8d86e37 | 2020-11-21 14:50:43 -0500 | [diff] [blame] | 982 | static unsigned int xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov, |
| 983 | unsigned int base, unsigned int len) |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 984 | { |
| 985 | if (len > iov->iov_len) |
| 986 | len = iov->iov_len; |
Trond Myklebust | 8d86e37 | 2020-11-21 14:50:43 -0500 | [diff] [blame] | 987 | if (unlikely(base > len)) |
| 988 | base = len; |
| 989 | xdr->p = (__be32*)(iov->iov_base + base); |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 990 | xdr->end = (__be32*)(iov->iov_base + len); |
| 991 | xdr->iov = iov; |
| 992 | xdr->page_ptr = NULL; |
Trond Myklebust | 8d86e37 | 2020-11-21 14:50:43 -0500 | [diff] [blame] | 993 | return len - base; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 994 | } |
| 995 | |
Trond Myklebust | 8d86e37 | 2020-11-21 14:50:43 -0500 | [diff] [blame] | 996 | static unsigned int xdr_set_page_base(struct xdr_stream *xdr, |
| 997 | unsigned int base, unsigned int len) |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 998 | { |
| 999 | unsigned int pgnr; |
| 1000 | unsigned int maxlen; |
| 1001 | unsigned int pgoff; |
| 1002 | unsigned int pgend; |
| 1003 | void *kaddr; |
| 1004 | |
| 1005 | maxlen = xdr->buf->page_len; |
Trond Myklebust | 8d86e37 | 2020-11-21 14:50:43 -0500 | [diff] [blame] | 1006 | if (base >= maxlen) { |
| 1007 | base = maxlen; |
| 1008 | maxlen = 0; |
| 1009 | } else |
| 1010 | maxlen -= base; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1011 | if (len > maxlen) |
| 1012 | len = maxlen; |
| 1013 | |
| 1014 | base += xdr->buf->page_base; |
| 1015 | |
| 1016 | pgnr = base >> PAGE_SHIFT; |
| 1017 | xdr->page_ptr = &xdr->buf->pages[pgnr]; |
| 1018 | kaddr = page_address(*xdr->page_ptr); |
| 1019 | |
| 1020 | pgoff = base & ~PAGE_MASK; |
| 1021 | xdr->p = (__be32*)(kaddr + pgoff); |
| 1022 | |
| 1023 | pgend = pgoff + len; |
| 1024 | if (pgend > PAGE_SIZE) |
| 1025 | pgend = PAGE_SIZE; |
| 1026 | xdr->end = (__be32*)(kaddr + pgend); |
| 1027 | xdr->iov = NULL; |
Trond Myklebust | 8d86e37 | 2020-11-21 14:50:43 -0500 | [diff] [blame] | 1028 | return len; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1029 | } |
| 1030 | |
Anna Schumaker | f7d61ee | 2015-01-26 17:26:19 -0500 | [diff] [blame] | 1031 | static void xdr_set_page(struct xdr_stream *xdr, unsigned int base, |
| 1032 | unsigned int len) |
| 1033 | { |
Trond Myklebust | 0279024 | 2020-11-21 21:21:11 -0500 | [diff] [blame] | 1034 | if (xdr_set_page_base(xdr, base, len) == 0) { |
| 1035 | base -= xdr->buf->page_len; |
| 1036 | xdr_set_iov(xdr, xdr->buf->tail, base, len); |
| 1037 | } |
Anna Schumaker | f7d61ee | 2015-01-26 17:26:19 -0500 | [diff] [blame] | 1038 | } |
| 1039 | |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1040 | static void xdr_set_next_page(struct xdr_stream *xdr) |
| 1041 | { |
| 1042 | unsigned int newbase; |
| 1043 | |
| 1044 | newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT; |
| 1045 | newbase -= xdr->buf->page_base; |
Trond Myklebust | 0279024 | 2020-11-21 21:21:11 -0500 | [diff] [blame] | 1046 | if (newbase < xdr->buf->page_len) |
| 1047 | xdr_set_page_base(xdr, newbase, xdr_stream_remaining(xdr)); |
| 1048 | else |
| 1049 | xdr_set_iov(xdr, xdr->buf->tail, 0, xdr_stream_remaining(xdr)); |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | static bool xdr_set_next_buffer(struct xdr_stream *xdr) |
| 1053 | { |
| 1054 | if (xdr->page_ptr != NULL) |
| 1055 | xdr_set_next_page(xdr); |
Trond Myklebust | 0279024 | 2020-11-21 21:21:11 -0500 | [diff] [blame] | 1056 | else if (xdr->iov == xdr->buf->head) |
| 1057 | xdr_set_page(xdr, 0, xdr_stream_remaining(xdr)); |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1058 | return xdr->p != xdr->end; |
| 1059 | } |
| 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | /** |
| 1062 | * xdr_init_decode - Initialize an xdr_stream for decoding data. |
| 1063 | * @xdr: pointer to xdr_stream struct |
| 1064 | * @buf: pointer to XDR buffer from which to decode data |
| 1065 | * @p: current pointer inside XDR buffer |
Chuck Lever | 0ccc61b | 2019-02-11 11:24:05 -0500 | [diff] [blame] | 1066 | * @rqst: pointer to controlling rpc_rqst, for debugging |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | */ |
Chuck Lever | 0ccc61b | 2019-02-11 11:24:05 -0500 | [diff] [blame] | 1068 | void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p, |
| 1069 | struct rpc_rqst *rqst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | xdr->buf = buf; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1072 | xdr->scratch.iov_base = NULL; |
| 1073 | xdr->scratch.iov_len = 0; |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1074 | xdr->nwords = XDR_QUADLEN(buf->len); |
Trond Myklebust | 8d86e37 | 2020-11-21 14:50:43 -0500 | [diff] [blame] | 1075 | if (xdr_set_iov(xdr, buf->head, 0, buf->len) == 0 && |
| 1076 | xdr_set_page_base(xdr, 0, buf->len) == 0) |
| 1077 | xdr_set_iov(xdr, buf->tail, 0, buf->len); |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1078 | if (p != NULL && p > xdr->p && xdr->end >= p) { |
| 1079 | xdr->nwords -= p - xdr->p; |
Trond Myklebust | 1537693 | 2012-06-28 17:17:48 -0400 | [diff] [blame] | 1080 | xdr->p = p; |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1081 | } |
Chuck Lever | 0ccc61b | 2019-02-11 11:24:05 -0500 | [diff] [blame] | 1082 | xdr->rqst = rqst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1084 | EXPORT_SYMBOL_GPL(xdr_init_decode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 1086 | /** |
Chuck Lever | 7ecce75 | 2017-04-11 13:23:59 -0400 | [diff] [blame] | 1087 | * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 1088 | * @xdr: pointer to xdr_stream struct |
| 1089 | * @buf: pointer to XDR buffer from which to decode data |
| 1090 | * @pages: list of pages to decode into |
| 1091 | * @len: length in bytes of buffer in pages |
| 1092 | */ |
| 1093 | void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf, |
| 1094 | struct page **pages, unsigned int len) |
| 1095 | { |
| 1096 | memset(buf, 0, sizeof(*buf)); |
| 1097 | buf->pages = pages; |
| 1098 | buf->page_len = len; |
| 1099 | buf->buflen = len; |
| 1100 | buf->len = len; |
Chuck Lever | 0ccc61b | 2019-02-11 11:24:05 -0500 | [diff] [blame] | 1101 | xdr_init_decode(xdr, buf, NULL, NULL); |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 1102 | } |
| 1103 | EXPORT_SYMBOL_GPL(xdr_init_decode_pages); |
| 1104 | |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1105 | static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes) |
Trond Myklebust | ba8e452 | 2010-10-19 19:58:49 -0400 | [diff] [blame] | 1106 | { |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1107 | unsigned int nwords = XDR_QUADLEN(nbytes); |
Trond Myklebust | ba8e452 | 2010-10-19 19:58:49 -0400 | [diff] [blame] | 1108 | __be32 *p = xdr->p; |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1109 | __be32 *q = p + nwords; |
Trond Myklebust | ba8e452 | 2010-10-19 19:58:49 -0400 | [diff] [blame] | 1110 | |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1111 | if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p)) |
Trond Myklebust | ba8e452 | 2010-10-19 19:58:49 -0400 | [diff] [blame] | 1112 | return NULL; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1113 | xdr->p = q; |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1114 | xdr->nwords -= nwords; |
Trond Myklebust | ba8e452 | 2010-10-19 19:58:49 -0400 | [diff] [blame] | 1115 | return p; |
| 1116 | } |
Trond Myklebust | ba8e452 | 2010-10-19 19:58:49 -0400 | [diff] [blame] | 1117 | |
| 1118 | /** |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1119 | * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data. |
| 1120 | * @xdr: pointer to xdr_stream struct |
| 1121 | * @buf: pointer to an empty buffer |
| 1122 | * @buflen: size of 'buf' |
| 1123 | * |
| 1124 | * The scratch buffer is used when decoding from an array of pages. |
| 1125 | * If an xdr_inline_decode() call spans across page boundaries, then |
| 1126 | * we copy the data into the scratch buffer in order to allow linear |
| 1127 | * access. |
| 1128 | */ |
| 1129 | void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen) |
| 1130 | { |
| 1131 | xdr->scratch.iov_base = buf; |
| 1132 | xdr->scratch.iov_len = buflen; |
| 1133 | } |
| 1134 | EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer); |
| 1135 | |
| 1136 | static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes) |
| 1137 | { |
| 1138 | __be32 *p; |
Trond Myklebust | ace0e14 | 2016-09-20 14:33:42 -0400 | [diff] [blame] | 1139 | char *cpdest = xdr->scratch.iov_base; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1140 | size_t cplen = (char *)xdr->end - (char *)xdr->p; |
| 1141 | |
| 1142 | if (nbytes > xdr->scratch.iov_len) |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 1143 | goto out_overflow; |
Trond Myklebust | ace0e14 | 2016-09-20 14:33:42 -0400 | [diff] [blame] | 1144 | p = __xdr_inline_decode(xdr, cplen); |
| 1145 | if (p == NULL) |
| 1146 | return NULL; |
| 1147 | memcpy(cpdest, p, cplen); |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 1148 | if (!xdr_set_next_buffer(xdr)) |
| 1149 | goto out_overflow; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1150 | cpdest += cplen; |
| 1151 | nbytes -= cplen; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1152 | p = __xdr_inline_decode(xdr, nbytes); |
| 1153 | if (p == NULL) |
| 1154 | return NULL; |
| 1155 | memcpy(cpdest, p, nbytes); |
| 1156 | return xdr->scratch.iov_base; |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 1157 | out_overflow: |
| 1158 | trace_rpc_xdr_overflow(xdr, nbytes); |
| 1159 | return NULL; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * xdr_inline_decode - Retrieve XDR data to decode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | * @xdr: pointer to xdr_stream struct |
| 1165 | * @nbytes: number of bytes of data to decode |
| 1166 | * |
| 1167 | * Check if the input buffer is long enough to enable us to decode |
| 1168 | * 'nbytes' more bytes of data starting at the current position. |
| 1169 | * If so return the current pointer, then update the current |
| 1170 | * pointer position. |
| 1171 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1172 | __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | { |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1174 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 1176 | if (unlikely(nbytes == 0)) |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1177 | return xdr->p; |
| 1178 | if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr)) |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 1179 | goto out_overflow; |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 1180 | p = __xdr_inline_decode(xdr, nbytes); |
| 1181 | if (p != NULL) |
| 1182 | return p; |
| 1183 | return xdr_copy_to_scratch(xdr, nbytes); |
Chuck Lever | 5582863 | 2019-02-11 11:24:10 -0500 | [diff] [blame] | 1184 | out_overflow: |
| 1185 | trace_rpc_xdr_overflow(xdr, nbytes); |
| 1186 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1188 | EXPORT_SYMBOL_GPL(xdr_inline_decode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
Anna Schumaker | 06216ec | 2020-04-20 17:38:17 -0400 | [diff] [blame] | 1190 | static void xdr_realign_pages(struct xdr_stream *xdr) |
| 1191 | { |
| 1192 | struct xdr_buf *buf = xdr->buf; |
| 1193 | struct kvec *iov = buf->head; |
| 1194 | unsigned int cur = xdr_stream_pos(xdr); |
| 1195 | unsigned int copied, offset; |
| 1196 | |
| 1197 | /* Realign pages to current pointer position */ |
| 1198 | if (iov->iov_len > cur) { |
| 1199 | offset = iov->iov_len - cur; |
| 1200 | copied = xdr_shrink_bufhead(buf, offset); |
| 1201 | trace_rpc_xdr_alignment(xdr, offset, copied); |
| 1202 | xdr->nwords = XDR_QUADLEN(buf->len - cur); |
| 1203 | } |
| 1204 | } |
| 1205 | |
Trond Myklebust | 3994ee6 | 2012-06-26 12:34:05 -0400 | [diff] [blame] | 1206 | static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | { |
| 1208 | struct xdr_buf *buf = xdr->buf; |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1209 | unsigned int nwords = XDR_QUADLEN(len); |
Trond Myklebust | b760b31 | 2012-06-26 12:19:55 -0400 | [diff] [blame] | 1210 | unsigned int cur = xdr_stream_pos(xdr); |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 1211 | unsigned int copied, offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1213 | if (xdr->nwords == 0) |
Trond Myklebust | c337d36 | 2012-06-21 17:05:37 -0400 | [diff] [blame] | 1214 | return 0; |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 1215 | |
Anna Schumaker | 06216ec | 2020-04-20 17:38:17 -0400 | [diff] [blame] | 1216 | xdr_realign_pages(xdr); |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 1217 | if (nwords > xdr->nwords) { |
| 1218 | nwords = xdr->nwords; |
| 1219 | len = nwords << 2; |
| 1220 | } |
Trond Myklebust | a11a2bf | 2012-08-02 13:21:43 -0400 | [diff] [blame] | 1221 | if (buf->page_len <= len) |
Trond Myklebust | 8a9a8b8 | 2012-08-01 14:32:13 -0400 | [diff] [blame] | 1222 | len = buf->page_len; |
Trond Myklebust | a11a2bf | 2012-08-02 13:21:43 -0400 | [diff] [blame] | 1223 | else if (nwords < xdr->nwords) { |
| 1224 | /* Truncate page data and move it into the tail */ |
Chuck Lever | 7be9cea3 | 2019-02-11 11:24:16 -0500 | [diff] [blame] | 1225 | offset = buf->page_len - len; |
| 1226 | copied = xdr_shrink_pagelen(buf, offset); |
| 1227 | trace_rpc_xdr_alignment(xdr, offset, copied); |
Trond Myklebust | a11a2bf | 2012-08-02 13:21:43 -0400 | [diff] [blame] | 1228 | xdr->nwords = XDR_QUADLEN(buf->len - cur); |
| 1229 | } |
Trond Myklebust | 3994ee6 | 2012-06-26 12:34:05 -0400 | [diff] [blame] | 1230 | return len; |
| 1231 | } |
Trond Myklebust | bd00f84 | 2012-06-26 13:50:43 -0400 | [diff] [blame] | 1232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | /** |
Trond Myklebust | 1d97316 | 2020-11-20 16:31:03 -0500 | [diff] [blame] | 1234 | * xdr_read_pages - align page-based XDR data to current pointer position |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | * @xdr: pointer to xdr_stream struct |
| 1236 | * @len: number of bytes of page data |
| 1237 | * |
| 1238 | * Moves data beyond the current pointer position from the XDR head[] buffer |
Trond Myklebust | 1d97316 | 2020-11-20 16:31:03 -0500 | [diff] [blame] | 1239 | * into the page list. Any data that lies beyond current position + @len |
| 1240 | * bytes is moved into the XDR tail[]. The xdr_stream current position is |
| 1241 | * then advanced past that data to align to the next XDR object in the tail. |
Trond Myklebust | 3994ee6 | 2012-06-26 12:34:05 -0400 | [diff] [blame] | 1242 | * |
| 1243 | * Returns the number of XDR encoded bytes now contained in the pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | */ |
Trond Myklebust | 3994ee6 | 2012-06-26 12:34:05 -0400 | [diff] [blame] | 1245 | unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | { |
Trond Myklebust | 1d97316 | 2020-11-20 16:31:03 -0500 | [diff] [blame] | 1247 | unsigned int nwords = XDR_QUADLEN(len); |
| 1248 | unsigned int base, end, pglen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | |
Trond Myklebust | 1d97316 | 2020-11-20 16:31:03 -0500 | [diff] [blame] | 1250 | pglen = xdr_align_pages(xdr, nwords << 2); |
| 1251 | if (pglen == 0) |
Trond Myklebust | 3994ee6 | 2012-06-26 12:34:05 -0400 | [diff] [blame] | 1252 | return 0; |
Trond Myklebust | bd00f84 | 2012-06-26 13:50:43 -0400 | [diff] [blame] | 1253 | |
Trond Myklebust | 1d97316 | 2020-11-20 16:31:03 -0500 | [diff] [blame] | 1254 | xdr->nwords -= nwords; |
| 1255 | base = (nwords << 2) - pglen; |
| 1256 | end = xdr_stream_remaining(xdr) - pglen; |
| 1257 | |
| 1258 | if (xdr_set_iov(xdr, xdr->buf->tail, base, end) == 0) |
| 1259 | xdr->nwords = 0; |
| 1260 | return len <= pglen ? len : pglen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1262 | EXPORT_SYMBOL_GPL(xdr_read_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
Anna Schumaker | e6ac0ac | 2020-04-21 11:27:00 -0400 | [diff] [blame] | 1264 | uint64_t xdr_align_data(struct xdr_stream *xdr, uint64_t offset, uint32_t length) |
| 1265 | { |
| 1266 | struct xdr_buf *buf = xdr->buf; |
| 1267 | unsigned int from, bytes; |
| 1268 | unsigned int shift = 0; |
| 1269 | |
| 1270 | if ((offset + length) < offset || |
| 1271 | (offset + length) > buf->page_len) |
| 1272 | length = buf->page_len - offset; |
| 1273 | |
| 1274 | xdr_realign_pages(xdr); |
| 1275 | from = xdr_page_pos(xdr); |
Trond Myklebust | eee1f54 | 2020-11-21 21:39:02 -0500 | [diff] [blame] | 1276 | bytes = xdr_stream_remaining(xdr); |
Anna Schumaker | e6ac0ac | 2020-04-21 11:27:00 -0400 | [diff] [blame] | 1277 | if (length < bytes) |
| 1278 | bytes = length; |
| 1279 | |
| 1280 | /* Move page data to the left */ |
| 1281 | if (from > offset) { |
| 1282 | shift = min_t(unsigned int, bytes, buf->page_len - from); |
| 1283 | _shift_data_left_pages(buf->pages, |
| 1284 | buf->page_base + offset, |
| 1285 | buf->page_base + from, |
| 1286 | shift); |
| 1287 | bytes -= shift; |
| 1288 | |
| 1289 | /* Move tail data into the pages, if necessary */ |
| 1290 | if (bytes > 0) |
| 1291 | _shift_data_left_tail(buf, offset + shift, bytes); |
| 1292 | } |
| 1293 | |
| 1294 | xdr->nwords -= XDR_QUADLEN(length); |
Trond Myklebust | 0279024 | 2020-11-21 21:21:11 -0500 | [diff] [blame] | 1295 | xdr_set_page(xdr, from + length, xdr_stream_remaining(xdr)); |
Anna Schumaker | e6ac0ac | 2020-04-21 11:27:00 -0400 | [diff] [blame] | 1296 | return length; |
| 1297 | } |
| 1298 | EXPORT_SYMBOL_GPL(xdr_align_data); |
| 1299 | |
Anna Schumaker | 84ce182 | 2014-05-28 13:38:53 -0400 | [diff] [blame] | 1300 | uint64_t xdr_expand_hole(struct xdr_stream *xdr, uint64_t offset, uint64_t length) |
| 1301 | { |
| 1302 | struct xdr_buf *buf = xdr->buf; |
| 1303 | unsigned int bytes; |
| 1304 | unsigned int from; |
| 1305 | unsigned int truncated = 0; |
| 1306 | |
| 1307 | if ((offset + length) < offset || |
| 1308 | (offset + length) > buf->page_len) |
| 1309 | length = buf->page_len - offset; |
| 1310 | |
| 1311 | xdr_realign_pages(xdr); |
| 1312 | from = xdr_page_pos(xdr); |
Trond Myklebust | eee1f54 | 2020-11-21 21:39:02 -0500 | [diff] [blame] | 1313 | bytes = xdr_stream_remaining(xdr); |
Anna Schumaker | 84ce182 | 2014-05-28 13:38:53 -0400 | [diff] [blame] | 1314 | |
| 1315 | if (offset + length + bytes > buf->page_len) { |
| 1316 | unsigned int shift = (offset + length + bytes) - buf->page_len; |
| 1317 | unsigned int res = _shift_data_right_tail(buf, from + bytes - shift, shift); |
| 1318 | truncated = shift - res; |
| 1319 | xdr->nwords -= XDR_QUADLEN(truncated); |
| 1320 | bytes -= shift; |
| 1321 | } |
| 1322 | |
| 1323 | /* Now move the page data over and zero pages */ |
| 1324 | if (bytes > 0) |
| 1325 | _shift_data_right_pages(buf->pages, |
| 1326 | buf->page_base + offset + length, |
| 1327 | buf->page_base + from, |
| 1328 | bytes); |
| 1329 | _zero_pages(buf->pages, buf->page_base + offset, length); |
| 1330 | |
| 1331 | buf->len += length - (from - offset) - truncated; |
Trond Myklebust | 0279024 | 2020-11-21 21:21:11 -0500 | [diff] [blame] | 1332 | xdr_set_page(xdr, offset + length, xdr_stream_remaining(xdr)); |
Anna Schumaker | 84ce182 | 2014-05-28 13:38:53 -0400 | [diff] [blame] | 1333 | return length; |
| 1334 | } |
| 1335 | EXPORT_SYMBOL_GPL(xdr_expand_hole); |
| 1336 | |
Trond Myklebust | 8b23ea7 | 2006-06-09 09:34:21 -0400 | [diff] [blame] | 1337 | /** |
| 1338 | * xdr_enter_page - decode data from the XDR page |
| 1339 | * @xdr: pointer to xdr_stream struct |
| 1340 | * @len: number of bytes of page data |
| 1341 | * |
| 1342 | * Moves data beyond the current pointer position from the XDR head[] buffer |
| 1343 | * into the page list. Any data that lies beyond current position + "len" |
| 1344 | * bytes is moved into the XDR tail[]. The current pointer is then |
| 1345 | * repositioned at the beginning of the first XDR page. |
| 1346 | */ |
| 1347 | void xdr_enter_page(struct xdr_stream *xdr, unsigned int len) |
| 1348 | { |
Trond Myklebust | f8bb7f0 | 2012-06-21 14:53:10 -0400 | [diff] [blame] | 1349 | len = xdr_align_pages(xdr, len); |
Trond Myklebust | 8b23ea7 | 2006-06-09 09:34:21 -0400 | [diff] [blame] | 1350 | /* |
| 1351 | * Position current pointer at beginning of tail, and |
| 1352 | * set remaining message length. |
| 1353 | */ |
Trond Myklebust | f8bb7f0 | 2012-06-21 14:53:10 -0400 | [diff] [blame] | 1354 | if (len != 0) |
| 1355 | xdr_set_page_base(xdr, 0, len); |
Trond Myklebust | 8b23ea7 | 2006-06-09 09:34:21 -0400 | [diff] [blame] | 1356 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1357 | EXPORT_SYMBOL_GPL(xdr_enter_page); |
Trond Myklebust | 8b23ea7 | 2006-06-09 09:34:21 -0400 | [diff] [blame] | 1358 | |
Julia Lawall | c2bd2c0 | 2020-01-01 08:43:30 +0100 | [diff] [blame] | 1359 | static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
| 1361 | void |
| 1362 | xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf) |
| 1363 | { |
| 1364 | buf->head[0] = *iov; |
| 1365 | buf->tail[0] = empty_iov; |
| 1366 | buf->page_len = 0; |
| 1367 | buf->buflen = buf->len = iov->iov_len; |
| 1368 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1369 | EXPORT_SYMBOL_GPL(xdr_buf_from_iov); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
J. Bruce Fields | de4aee2 | 2014-02-25 17:21:08 -0500 | [diff] [blame] | 1371 | /** |
| 1372 | * xdr_buf_subsegment - set subbuf to a portion of buf |
| 1373 | * @buf: an xdr buffer |
| 1374 | * @subbuf: the result buffer |
| 1375 | * @base: beginning of range in bytes |
| 1376 | * @len: length of range in bytes |
| 1377 | * |
| 1378 | * sets @subbuf to an xdr buffer representing the portion of @buf of |
| 1379 | * length @len starting at offset @base. |
| 1380 | * |
| 1381 | * @buf and @subbuf may be pointers to the same struct xdr_buf. |
| 1382 | * |
| 1383 | * Returns -1 if base of length are out of bounds. |
| 1384 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | int |
| 1386 | xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf, |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1387 | unsigned int base, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | subbuf->buflen = subbuf->len = len; |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1390 | if (base < buf->head[0].iov_len) { |
| 1391 | subbuf->head[0].iov_base = buf->head[0].iov_base + base; |
| 1392 | subbuf->head[0].iov_len = min_t(unsigned int, len, |
| 1393 | buf->head[0].iov_len - base); |
| 1394 | len -= subbuf->head[0].iov_len; |
| 1395 | base = 0; |
| 1396 | } else { |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1397 | base -= buf->head[0].iov_len; |
Chuck Lever | 89a3c9f | 2020-06-25 11:32:34 -0400 | [diff] [blame] | 1398 | subbuf->head[0].iov_base = buf->head[0].iov_base; |
J. Bruce Fields | de4aee2 | 2014-02-25 17:21:08 -0500 | [diff] [blame] | 1399 | subbuf->head[0].iov_len = 0; |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
| 1402 | if (base < buf->page_len) { |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1403 | subbuf->page_len = min(buf->page_len - base, len); |
| 1404 | base += buf->page_base; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1405 | subbuf->page_base = base & ~PAGE_MASK; |
| 1406 | subbuf->pages = &buf->pages[base >> PAGE_SHIFT]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | len -= subbuf->page_len; |
| 1408 | base = 0; |
| 1409 | } else { |
| 1410 | base -= buf->page_len; |
Chuck Lever | 89a3c9f | 2020-06-25 11:32:34 -0400 | [diff] [blame] | 1411 | subbuf->pages = buf->pages; |
| 1412 | subbuf->page_base = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | subbuf->page_len = 0; |
| 1414 | } |
| 1415 | |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1416 | if (base < buf->tail[0].iov_len) { |
| 1417 | subbuf->tail[0].iov_base = buf->tail[0].iov_base + base; |
| 1418 | subbuf->tail[0].iov_len = min_t(unsigned int, len, |
| 1419 | buf->tail[0].iov_len - base); |
| 1420 | len -= subbuf->tail[0].iov_len; |
| 1421 | base = 0; |
| 1422 | } else { |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1423 | base -= buf->tail[0].iov_len; |
Chuck Lever | 89a3c9f | 2020-06-25 11:32:34 -0400 | [diff] [blame] | 1424 | subbuf->tail[0].iov_base = buf->tail[0].iov_base; |
J. Bruce Fields | de4aee2 | 2014-02-25 17:21:08 -0500 | [diff] [blame] | 1425 | subbuf->tail[0].iov_len = 0; |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1426 | } |
| 1427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | if (base || len) |
| 1429 | return -1; |
| 1430 | return 0; |
| 1431 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1432 | EXPORT_SYMBOL_GPL(xdr_buf_subsegment); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
Chuck Lever | 0a8e7b7 | 2020-04-15 17:36:22 -0400 | [diff] [blame] | 1434 | /** |
| 1435 | * xdr_buf_trim - lop at most "len" bytes off the end of "buf" |
| 1436 | * @buf: buf to be trimmed |
| 1437 | * @len: number of bytes to reduce "buf" by |
| 1438 | * |
| 1439 | * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note |
| 1440 | * that it's possible that we'll trim less than that amount if the xdr_buf is |
| 1441 | * too small, or if (for instance) it's all in the head and the parser has |
| 1442 | * already read too far into it. |
| 1443 | */ |
| 1444 | void xdr_buf_trim(struct xdr_buf *buf, unsigned int len) |
| 1445 | { |
| 1446 | size_t cur; |
| 1447 | unsigned int trim = len; |
| 1448 | |
| 1449 | if (buf->tail[0].iov_len) { |
| 1450 | cur = min_t(size_t, buf->tail[0].iov_len, trim); |
| 1451 | buf->tail[0].iov_len -= cur; |
| 1452 | trim -= cur; |
| 1453 | if (!trim) |
| 1454 | goto fix_len; |
| 1455 | } |
| 1456 | |
| 1457 | if (buf->page_len) { |
| 1458 | cur = min_t(unsigned int, buf->page_len, trim); |
| 1459 | buf->page_len -= cur; |
| 1460 | trim -= cur; |
| 1461 | if (!trim) |
| 1462 | goto fix_len; |
| 1463 | } |
| 1464 | |
| 1465 | if (buf->head[0].iov_len) { |
| 1466 | cur = min_t(size_t, buf->head[0].iov_len, trim); |
| 1467 | buf->head[0].iov_len -= cur; |
| 1468 | trim -= cur; |
| 1469 | } |
| 1470 | fix_len: |
| 1471 | buf->len -= (len - trim); |
| 1472 | } |
| 1473 | EXPORT_SYMBOL_GPL(xdr_buf_trim); |
| 1474 | |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1475 | static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | { |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1477 | unsigned int this_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1479 | this_len = min_t(unsigned int, len, subbuf->head[0].iov_len); |
| 1480 | memcpy(obj, subbuf->head[0].iov_base, this_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | len -= this_len; |
| 1482 | obj += this_len; |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1483 | this_len = min_t(unsigned int, len, subbuf->page_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | if (this_len) |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1485 | _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | len -= this_len; |
| 1487 | obj += this_len; |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1488 | this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len); |
| 1489 | memcpy(obj, subbuf->tail[0].iov_base, this_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1492 | /* obj is assumed to point to allocated memory of size at least len: */ |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1493 | int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len) |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1494 | { |
| 1495 | struct xdr_buf subbuf; |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1496 | int status; |
| 1497 | |
| 1498 | status = xdr_buf_subsegment(buf, &subbuf, base, len); |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1499 | if (status != 0) |
| 1500 | return status; |
| 1501 | __read_bytes_from_xdr_buf(&subbuf, obj, len); |
| 1502 | return 0; |
| 1503 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1504 | EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf); |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1505 | |
| 1506 | static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len) |
| 1507 | { |
| 1508 | unsigned int this_len; |
| 1509 | |
| 1510 | this_len = min_t(unsigned int, len, subbuf->head[0].iov_len); |
| 1511 | memcpy(subbuf->head[0].iov_base, obj, this_len); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1512 | len -= this_len; |
| 1513 | obj += this_len; |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1514 | this_len = min_t(unsigned int, len, subbuf->page_len); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1515 | if (this_len) |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1516 | _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1517 | len -= this_len; |
| 1518 | obj += this_len; |
Trond Myklebust | 4e3e43a | 2006-10-17 13:47:24 -0400 | [diff] [blame] | 1519 | this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len); |
| 1520 | memcpy(subbuf->tail[0].iov_base, obj, this_len); |
| 1521 | } |
| 1522 | |
| 1523 | /* obj is assumed to point to allocated memory of size at least len: */ |
| 1524 | int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len) |
| 1525 | { |
| 1526 | struct xdr_buf subbuf; |
| 1527 | int status; |
| 1528 | |
| 1529 | status = xdr_buf_subsegment(buf, &subbuf, base, len); |
| 1530 | if (status != 0) |
| 1531 | return status; |
| 1532 | __write_bytes_to_xdr_buf(&subbuf, obj, len); |
| 1533 | return 0; |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1534 | } |
Kevin Coffman | c43abae | 2010-03-17 13:02:58 -0400 | [diff] [blame] | 1535 | EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1536 | |
| 1537 | int |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1538 | xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | { |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1540 | __be32 raw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | int status; |
| 1542 | |
| 1543 | status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj)); |
| 1544 | if (status) |
| 1545 | return status; |
Benny Halevy | 98866b5 | 2009-08-14 17:18:49 +0300 | [diff] [blame] | 1546 | *obj = be32_to_cpu(raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | return 0; |
| 1548 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1549 | EXPORT_SYMBOL_GPL(xdr_decode_word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1551 | int |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 1552 | xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1553 | { |
Benny Halevy | 9f162d2 | 2009-08-14 17:18:44 +0300 | [diff] [blame] | 1554 | __be32 raw = cpu_to_be32(obj); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1555 | |
| 1556 | return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj)); |
| 1557 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1558 | EXPORT_SYMBOL_GPL(xdr_encode_word); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1559 | |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1560 | /* Returns 0 on success, or else a negative error code. */ |
| 1561 | static int |
| 1562 | xdr_xcode_array2(struct xdr_buf *buf, unsigned int base, |
| 1563 | struct xdr_array2_desc *desc, int encode) |
| 1564 | { |
| 1565 | char *elem = NULL, *c; |
| 1566 | unsigned int copied = 0, todo, avail_here; |
| 1567 | struct page **ppages = NULL; |
| 1568 | int err; |
| 1569 | |
| 1570 | if (encode) { |
| 1571 | if (xdr_encode_word(buf, base, desc->array_len) != 0) |
| 1572 | return -EINVAL; |
| 1573 | } else { |
| 1574 | if (xdr_decode_word(buf, base, &desc->array_len) != 0 || |
Trond Myklebust | 58fcb8d | 2005-08-10 18:15:12 -0400 | [diff] [blame] | 1575 | desc->array_len > desc->array_maxlen || |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1576 | (unsigned long) base + 4 + desc->array_len * |
| 1577 | desc->elem_size > buf->len) |
| 1578 | return -EINVAL; |
| 1579 | } |
| 1580 | base += 4; |
| 1581 | |
| 1582 | if (!desc->xcode) |
| 1583 | return 0; |
| 1584 | |
| 1585 | todo = desc->array_len * desc->elem_size; |
| 1586 | |
| 1587 | /* process head */ |
| 1588 | if (todo && base < buf->head->iov_len) { |
| 1589 | c = buf->head->iov_base + base; |
| 1590 | avail_here = min_t(unsigned int, todo, |
| 1591 | buf->head->iov_len - base); |
| 1592 | todo -= avail_here; |
| 1593 | |
| 1594 | while (avail_here >= desc->elem_size) { |
| 1595 | err = desc->xcode(desc, c); |
| 1596 | if (err) |
| 1597 | goto out; |
| 1598 | c += desc->elem_size; |
| 1599 | avail_here -= desc->elem_size; |
| 1600 | } |
| 1601 | if (avail_here) { |
| 1602 | if (!elem) { |
| 1603 | elem = kmalloc(desc->elem_size, GFP_KERNEL); |
| 1604 | err = -ENOMEM; |
| 1605 | if (!elem) |
| 1606 | goto out; |
| 1607 | } |
| 1608 | if (encode) { |
| 1609 | err = desc->xcode(desc, elem); |
| 1610 | if (err) |
| 1611 | goto out; |
| 1612 | memcpy(c, elem, avail_here); |
| 1613 | } else |
| 1614 | memcpy(elem, c, avail_here); |
| 1615 | copied = avail_here; |
| 1616 | } |
| 1617 | base = buf->head->iov_len; /* align to start of pages */ |
| 1618 | } |
| 1619 | |
| 1620 | /* process pages array */ |
| 1621 | base -= buf->head->iov_len; |
| 1622 | if (todo && base < buf->page_len) { |
| 1623 | unsigned int avail_page; |
| 1624 | |
| 1625 | avail_here = min(todo, buf->page_len - base); |
| 1626 | todo -= avail_here; |
| 1627 | |
| 1628 | base += buf->page_base; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1629 | ppages = buf->pages + (base >> PAGE_SHIFT); |
| 1630 | base &= ~PAGE_MASK; |
| 1631 | avail_page = min_t(unsigned int, PAGE_SIZE - base, |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1632 | avail_here); |
| 1633 | c = kmap(*ppages) + base; |
| 1634 | |
| 1635 | while (avail_here) { |
| 1636 | avail_here -= avail_page; |
| 1637 | if (copied || avail_page < desc->elem_size) { |
| 1638 | unsigned int l = min(avail_page, |
| 1639 | desc->elem_size - copied); |
| 1640 | if (!elem) { |
| 1641 | elem = kmalloc(desc->elem_size, |
| 1642 | GFP_KERNEL); |
| 1643 | err = -ENOMEM; |
| 1644 | if (!elem) |
| 1645 | goto out; |
| 1646 | } |
| 1647 | if (encode) { |
| 1648 | if (!copied) { |
| 1649 | err = desc->xcode(desc, elem); |
| 1650 | if (err) |
| 1651 | goto out; |
| 1652 | } |
| 1653 | memcpy(c, elem + copied, l); |
| 1654 | copied += l; |
| 1655 | if (copied == desc->elem_size) |
| 1656 | copied = 0; |
| 1657 | } else { |
| 1658 | memcpy(elem + copied, c, l); |
| 1659 | copied += l; |
| 1660 | if (copied == desc->elem_size) { |
| 1661 | err = desc->xcode(desc, elem); |
| 1662 | if (err) |
| 1663 | goto out; |
| 1664 | copied = 0; |
| 1665 | } |
| 1666 | } |
| 1667 | avail_page -= l; |
| 1668 | c += l; |
| 1669 | } |
| 1670 | while (avail_page >= desc->elem_size) { |
| 1671 | err = desc->xcode(desc, c); |
| 1672 | if (err) |
| 1673 | goto out; |
| 1674 | c += desc->elem_size; |
| 1675 | avail_page -= desc->elem_size; |
| 1676 | } |
| 1677 | if (avail_page) { |
| 1678 | unsigned int l = min(avail_page, |
| 1679 | desc->elem_size - copied); |
| 1680 | if (!elem) { |
| 1681 | elem = kmalloc(desc->elem_size, |
| 1682 | GFP_KERNEL); |
| 1683 | err = -ENOMEM; |
| 1684 | if (!elem) |
| 1685 | goto out; |
| 1686 | } |
| 1687 | if (encode) { |
| 1688 | if (!copied) { |
| 1689 | err = desc->xcode(desc, elem); |
| 1690 | if (err) |
| 1691 | goto out; |
| 1692 | } |
| 1693 | memcpy(c, elem + copied, l); |
| 1694 | copied += l; |
| 1695 | if (copied == desc->elem_size) |
| 1696 | copied = 0; |
| 1697 | } else { |
| 1698 | memcpy(elem + copied, c, l); |
| 1699 | copied += l; |
| 1700 | if (copied == desc->elem_size) { |
| 1701 | err = desc->xcode(desc, elem); |
| 1702 | if (err) |
| 1703 | goto out; |
| 1704 | copied = 0; |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | if (avail_here) { |
| 1709 | kunmap(*ppages); |
| 1710 | ppages++; |
| 1711 | c = kmap(*ppages); |
| 1712 | } |
| 1713 | |
| 1714 | avail_page = min(avail_here, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1715 | (unsigned int) PAGE_SIZE); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1716 | } |
| 1717 | base = buf->page_len; /* align to start of tail */ |
| 1718 | } |
| 1719 | |
| 1720 | /* process tail */ |
| 1721 | base -= buf->page_len; |
| 1722 | if (todo) { |
| 1723 | c = buf->tail->iov_base + base; |
| 1724 | if (copied) { |
| 1725 | unsigned int l = desc->elem_size - copied; |
| 1726 | |
| 1727 | if (encode) |
| 1728 | memcpy(c, elem + copied, l); |
| 1729 | else { |
| 1730 | memcpy(elem + copied, c, l); |
| 1731 | err = desc->xcode(desc, elem); |
| 1732 | if (err) |
| 1733 | goto out; |
| 1734 | } |
| 1735 | todo -= l; |
| 1736 | c += l; |
| 1737 | } |
| 1738 | while (todo) { |
| 1739 | err = desc->xcode(desc, c); |
| 1740 | if (err) |
| 1741 | goto out; |
| 1742 | c += desc->elem_size; |
| 1743 | todo -= desc->elem_size; |
| 1744 | } |
| 1745 | } |
| 1746 | err = 0; |
| 1747 | |
| 1748 | out: |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 1749 | kfree(elem); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1750 | if (ppages) |
| 1751 | kunmap(*ppages); |
| 1752 | return err; |
| 1753 | } |
| 1754 | |
| 1755 | int |
| 1756 | xdr_decode_array2(struct xdr_buf *buf, unsigned int base, |
| 1757 | struct xdr_array2_desc *desc) |
| 1758 | { |
| 1759 | if (base >= buf->len) |
| 1760 | return -EINVAL; |
| 1761 | |
| 1762 | return xdr_xcode_array2(buf, base, desc, 0); |
| 1763 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1764 | EXPORT_SYMBOL_GPL(xdr_decode_array2); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1765 | |
| 1766 | int |
| 1767 | xdr_encode_array2(struct xdr_buf *buf, unsigned int base, |
| 1768 | struct xdr_array2_desc *desc) |
| 1769 | { |
| 1770 | if ((unsigned long) base + 4 + desc->array_len * desc->elem_size > |
| 1771 | buf->head->iov_len + buf->page_len + buf->tail->iov_len) |
| 1772 | return -EINVAL; |
| 1773 | |
| 1774 | return xdr_xcode_array2(buf, base, desc, 1); |
| 1775 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1776 | EXPORT_SYMBOL_GPL(xdr_encode_array2); |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 1777 | |
| 1778 | int |
| 1779 | xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1780 | int (*actor)(struct scatterlist *, void *), void *data) |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 1781 | { |
| 1782 | int i, ret = 0; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1783 | unsigned int page_len, thislen, page_offset; |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 1784 | struct scatterlist sg[1]; |
| 1785 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 1786 | sg_init_table(sg, 1); |
| 1787 | |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 1788 | if (offset >= buf->head[0].iov_len) { |
| 1789 | offset -= buf->head[0].iov_len; |
| 1790 | } else { |
| 1791 | thislen = buf->head[0].iov_len - offset; |
| 1792 | if (thislen > len) |
| 1793 | thislen = len; |
| 1794 | sg_set_buf(sg, buf->head[0].iov_base + offset, thislen); |
| 1795 | ret = actor(sg, data); |
| 1796 | if (ret) |
| 1797 | goto out; |
| 1798 | offset = 0; |
| 1799 | len -= thislen; |
| 1800 | } |
| 1801 | if (len == 0) |
| 1802 | goto out; |
| 1803 | |
| 1804 | if (offset >= buf->page_len) { |
| 1805 | offset -= buf->page_len; |
| 1806 | } else { |
| 1807 | page_len = buf->page_len - offset; |
| 1808 | if (page_len > len) |
| 1809 | page_len = len; |
| 1810 | len -= page_len; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1811 | page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1); |
| 1812 | i = (offset + buf->page_base) >> PAGE_SHIFT; |
| 1813 | thislen = PAGE_SIZE - page_offset; |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 1814 | do { |
| 1815 | if (thislen > page_len) |
| 1816 | thislen = page_len; |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 1817 | sg_set_page(sg, buf->pages[i], thislen, page_offset); |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 1818 | ret = actor(sg, data); |
| 1819 | if (ret) |
| 1820 | goto out; |
| 1821 | page_len -= thislen; |
| 1822 | i++; |
| 1823 | page_offset = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1824 | thislen = PAGE_SIZE; |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 1825 | } while (page_len != 0); |
| 1826 | offset = 0; |
| 1827 | } |
| 1828 | if (len == 0) |
| 1829 | goto out; |
| 1830 | if (offset < buf->tail[0].iov_len) { |
| 1831 | thislen = buf->tail[0].iov_len - offset; |
| 1832 | if (thislen > len) |
| 1833 | thislen = len; |
| 1834 | sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen); |
| 1835 | ret = actor(sg, data); |
| 1836 | len -= thislen; |
| 1837 | } |
| 1838 | if (len != 0) |
| 1839 | ret = -EINVAL; |
| 1840 | out: |
| 1841 | return ret; |
| 1842 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 1843 | EXPORT_SYMBOL_GPL(xdr_process_buf); |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 1844 | |
Trond Myklebust | 5c741d4 | 2017-02-19 16:08:31 -0500 | [diff] [blame] | 1845 | /** |
Trond Myklebust | 0e779aa | 2018-03-20 17:03:05 -0400 | [diff] [blame] | 1846 | * xdr_stream_decode_opaque - Decode variable length opaque |
| 1847 | * @xdr: pointer to xdr_stream |
| 1848 | * @ptr: location to store opaque data |
| 1849 | * @size: size of storage buffer @ptr |
| 1850 | * |
| 1851 | * Return values: |
| 1852 | * On success, returns size of object stored in *@ptr |
| 1853 | * %-EBADMSG on XDR buffer overflow |
| 1854 | * %-EMSGSIZE on overflow of storage buffer @ptr |
| 1855 | */ |
| 1856 | ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size) |
| 1857 | { |
| 1858 | ssize_t ret; |
| 1859 | void *p; |
| 1860 | |
| 1861 | ret = xdr_stream_decode_opaque_inline(xdr, &p, size); |
| 1862 | if (ret <= 0) |
| 1863 | return ret; |
| 1864 | memcpy(ptr, p, ret); |
| 1865 | return ret; |
| 1866 | } |
| 1867 | EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque); |
| 1868 | |
| 1869 | /** |
| 1870 | * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque |
| 1871 | * @xdr: pointer to xdr_stream |
| 1872 | * @ptr: location to store pointer to opaque data |
| 1873 | * @maxlen: maximum acceptable object size |
| 1874 | * @gfp_flags: GFP mask to use |
| 1875 | * |
| 1876 | * Return values: |
| 1877 | * On success, returns size of object stored in *@ptr |
| 1878 | * %-EBADMSG on XDR buffer overflow |
| 1879 | * %-EMSGSIZE if the size of the object would exceed @maxlen |
| 1880 | * %-ENOMEM on memory allocation failure |
| 1881 | */ |
| 1882 | ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr, |
| 1883 | size_t maxlen, gfp_t gfp_flags) |
| 1884 | { |
| 1885 | ssize_t ret; |
| 1886 | void *p; |
| 1887 | |
| 1888 | ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen); |
| 1889 | if (ret > 0) { |
| 1890 | *ptr = kmemdup(p, ret, gfp_flags); |
| 1891 | if (*ptr != NULL) |
| 1892 | return ret; |
| 1893 | ret = -ENOMEM; |
| 1894 | } |
| 1895 | *ptr = NULL; |
| 1896 | return ret; |
| 1897 | } |
| 1898 | EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup); |
| 1899 | |
| 1900 | /** |
| 1901 | * xdr_stream_decode_string - Decode variable length string |
| 1902 | * @xdr: pointer to xdr_stream |
| 1903 | * @str: location to store string |
| 1904 | * @size: size of storage buffer @str |
| 1905 | * |
| 1906 | * Return values: |
| 1907 | * On success, returns length of NUL-terminated string stored in *@str |
| 1908 | * %-EBADMSG on XDR buffer overflow |
| 1909 | * %-EMSGSIZE on overflow of storage buffer @str |
| 1910 | */ |
| 1911 | ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size) |
| 1912 | { |
| 1913 | ssize_t ret; |
| 1914 | void *p; |
| 1915 | |
| 1916 | ret = xdr_stream_decode_opaque_inline(xdr, &p, size); |
| 1917 | if (ret > 0) { |
| 1918 | memcpy(str, p, ret); |
| 1919 | str[ret] = '\0'; |
| 1920 | return strlen(str); |
| 1921 | } |
| 1922 | *str = '\0'; |
| 1923 | return ret; |
| 1924 | } |
| 1925 | EXPORT_SYMBOL_GPL(xdr_stream_decode_string); |
| 1926 | |
| 1927 | /** |
Trond Myklebust | 5c741d4 | 2017-02-19 16:08:31 -0500 | [diff] [blame] | 1928 | * xdr_stream_decode_string_dup - Decode and duplicate variable length string |
| 1929 | * @xdr: pointer to xdr_stream |
| 1930 | * @str: location to store pointer to string |
| 1931 | * @maxlen: maximum acceptable string length |
| 1932 | * @gfp_flags: GFP mask to use |
| 1933 | * |
| 1934 | * Return values: |
| 1935 | * On success, returns length of NUL-terminated string stored in *@ptr |
| 1936 | * %-EBADMSG on XDR buffer overflow |
| 1937 | * %-EMSGSIZE if the size of the string would exceed @maxlen |
| 1938 | * %-ENOMEM on memory allocation failure |
| 1939 | */ |
| 1940 | ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str, |
| 1941 | size_t maxlen, gfp_t gfp_flags) |
| 1942 | { |
| 1943 | void *p; |
| 1944 | ssize_t ret; |
| 1945 | |
| 1946 | ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen); |
| 1947 | if (ret > 0) { |
Trond Myklebust | 4aceaae | 2020-11-10 10:56:53 -0500 | [diff] [blame] | 1948 | char *s = kmemdup_nul(p, ret, gfp_flags); |
Trond Myklebust | 5c741d4 | 2017-02-19 16:08:31 -0500 | [diff] [blame] | 1949 | if (s != NULL) { |
Trond Myklebust | 5c741d4 | 2017-02-19 16:08:31 -0500 | [diff] [blame] | 1950 | *str = s; |
| 1951 | return strlen(s); |
| 1952 | } |
| 1953 | ret = -ENOMEM; |
| 1954 | } |
| 1955 | *str = NULL; |
| 1956 | return ret; |
| 1957 | } |
| 1958 | EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup); |