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