Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/xdr.c |
| 3 | * |
| 4 | * Generic XDR support. |
| 5 | * |
| 6 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/socket.h> |
| 11 | #include <linux/string.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/in.h> |
| 16 | #include <linux/net.h> |
| 17 | #include <net/sock.h> |
| 18 | #include <linux/sunrpc/xdr.h> |
| 19 | #include <linux/sunrpc/msg_prot.h> |
| 20 | |
| 21 | /* |
| 22 | * XDR functions for basic NFS types |
| 23 | */ |
| 24 | u32 * |
| 25 | xdr_encode_netobj(u32 *p, const struct xdr_netobj *obj) |
| 26 | { |
| 27 | unsigned int quadlen = XDR_QUADLEN(obj->len); |
| 28 | |
| 29 | p[quadlen] = 0; /* zero trailing bytes */ |
| 30 | *p++ = htonl(obj->len); |
| 31 | memcpy(p, obj->data, obj->len); |
| 32 | return p + XDR_QUADLEN(obj->len); |
| 33 | } |
| 34 | |
| 35 | u32 * |
| 36 | xdr_decode_netobj(u32 *p, struct xdr_netobj *obj) |
| 37 | { |
| 38 | unsigned int len; |
| 39 | |
| 40 | if ((len = ntohl(*p++)) > XDR_MAX_NETOBJ) |
| 41 | return NULL; |
| 42 | obj->len = len; |
| 43 | obj->data = (u8 *) p; |
| 44 | return p + XDR_QUADLEN(len); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * xdr_encode_opaque_fixed - Encode fixed length opaque data |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 49 | * @p: pointer to current position in XDR buffer. |
| 50 | * @ptr: pointer to data to encode (or NULL) |
| 51 | * @nbytes: size of data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * |
| 53 | * Copy the array of data of length nbytes at ptr to the XDR buffer |
| 54 | * at position p, then align to the next 32-bit boundary by padding |
| 55 | * with zero bytes (see RFC1832). |
| 56 | * Note: if ptr is NULL, only the padding is performed. |
| 57 | * |
| 58 | * Returns the updated current XDR buffer position |
| 59 | * |
| 60 | */ |
| 61 | u32 *xdr_encode_opaque_fixed(u32 *p, const void *ptr, unsigned int nbytes) |
| 62 | { |
| 63 | if (likely(nbytes != 0)) { |
| 64 | unsigned int quadlen = XDR_QUADLEN(nbytes); |
| 65 | unsigned int padding = (quadlen << 2) - nbytes; |
| 66 | |
| 67 | if (ptr != NULL) |
| 68 | memcpy(p, ptr, nbytes); |
| 69 | if (padding != 0) |
| 70 | memset((char *)p + nbytes, 0, padding); |
| 71 | p += quadlen; |
| 72 | } |
| 73 | return p; |
| 74 | } |
| 75 | EXPORT_SYMBOL(xdr_encode_opaque_fixed); |
| 76 | |
| 77 | /** |
| 78 | * xdr_encode_opaque - Encode variable length opaque data |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 79 | * @p: pointer to current position in XDR buffer. |
| 80 | * @ptr: pointer to data to encode (or NULL) |
| 81 | * @nbytes: size of data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * |
| 83 | * Returns the updated current XDR buffer position |
| 84 | */ |
| 85 | u32 *xdr_encode_opaque(u32 *p, const void *ptr, unsigned int nbytes) |
| 86 | { |
| 87 | *p++ = htonl(nbytes); |
| 88 | return xdr_encode_opaque_fixed(p, ptr, nbytes); |
| 89 | } |
| 90 | EXPORT_SYMBOL(xdr_encode_opaque); |
| 91 | |
| 92 | u32 * |
| 93 | xdr_encode_string(u32 *p, const char *string) |
| 94 | { |
| 95 | return xdr_encode_array(p, string, strlen(string)); |
| 96 | } |
| 97 | |
| 98 | u32 * |
| 99 | xdr_decode_string(u32 *p, char **sp, int *lenp, int maxlen) |
| 100 | { |
| 101 | unsigned int len; |
| 102 | char *string; |
| 103 | |
| 104 | if ((len = ntohl(*p++)) > maxlen) |
| 105 | return NULL; |
| 106 | if (lenp) |
| 107 | *lenp = len; |
| 108 | if ((len % 4) != 0) { |
| 109 | string = (char *) p; |
| 110 | } else { |
| 111 | string = (char *) (p - 1); |
| 112 | memmove(string, p, len); |
| 113 | } |
| 114 | string[len] = '\0'; |
| 115 | *sp = string; |
| 116 | return p + XDR_QUADLEN(len); |
| 117 | } |
| 118 | |
| 119 | u32 * |
| 120 | xdr_decode_string_inplace(u32 *p, char **sp, int *lenp, int maxlen) |
| 121 | { |
| 122 | unsigned int len; |
| 123 | |
| 124 | if ((len = ntohl(*p++)) > maxlen) |
| 125 | return NULL; |
| 126 | *lenp = len; |
| 127 | *sp = (char *) p; |
| 128 | return p + XDR_QUADLEN(len); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | xdr_encode_pages(struct xdr_buf *xdr, struct page **pages, unsigned int base, |
| 133 | unsigned int len) |
| 134 | { |
| 135 | struct kvec *tail = xdr->tail; |
| 136 | u32 *p; |
| 137 | |
| 138 | xdr->pages = pages; |
| 139 | xdr->page_base = base; |
| 140 | xdr->page_len = len; |
| 141 | |
| 142 | p = (u32 *)xdr->head[0].iov_base + XDR_QUADLEN(xdr->head[0].iov_len); |
| 143 | tail->iov_base = p; |
| 144 | tail->iov_len = 0; |
| 145 | |
| 146 | if (len & 3) { |
| 147 | unsigned int pad = 4 - (len & 3); |
| 148 | |
| 149 | *p = 0; |
| 150 | tail->iov_base = (char *)p + (len & 3); |
| 151 | tail->iov_len = pad; |
| 152 | len += pad; |
| 153 | } |
| 154 | xdr->buflen += len; |
| 155 | xdr->len += len; |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset, |
| 160 | struct page **pages, unsigned int base, unsigned int len) |
| 161 | { |
| 162 | struct kvec *head = xdr->head; |
| 163 | struct kvec *tail = xdr->tail; |
| 164 | char *buf = (char *)head->iov_base; |
| 165 | unsigned int buflen = head->iov_len; |
| 166 | |
| 167 | head->iov_len = offset; |
| 168 | |
| 169 | xdr->pages = pages; |
| 170 | xdr->page_base = base; |
| 171 | xdr->page_len = len; |
| 172 | |
| 173 | tail->iov_base = buf + offset; |
| 174 | tail->iov_len = buflen - offset; |
| 175 | |
| 176 | xdr->buflen += len; |
| 177 | } |
| 178 | |
Olaf Kirch | e053d1a | 2005-06-22 17:16:24 +0000 | [diff] [blame^] | 179 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | xdr_partial_copy_from_skb(struct xdr_buf *xdr, unsigned int base, |
| 181 | skb_reader_t *desc, |
| 182 | skb_read_actor_t copy_actor) |
| 183 | { |
| 184 | struct page **ppage = xdr->pages; |
| 185 | unsigned int len, pglen = xdr->page_len; |
| 186 | int ret; |
| 187 | |
| 188 | len = xdr->head[0].iov_len; |
| 189 | if (base < len) { |
| 190 | len -= base; |
| 191 | ret = copy_actor(desc, (char *)xdr->head[0].iov_base + base, len); |
| 192 | if (ret != len || !desc->count) |
Olaf Kirch | e053d1a | 2005-06-22 17:16:24 +0000 | [diff] [blame^] | 193 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | base = 0; |
| 195 | } else |
| 196 | base -= len; |
| 197 | |
| 198 | if (pglen == 0) |
| 199 | goto copy_tail; |
| 200 | if (base >= pglen) { |
| 201 | base -= pglen; |
| 202 | goto copy_tail; |
| 203 | } |
| 204 | if (base || xdr->page_base) { |
| 205 | pglen -= base; |
| 206 | base += xdr->page_base; |
| 207 | ppage += base >> PAGE_CACHE_SHIFT; |
| 208 | base &= ~PAGE_CACHE_MASK; |
| 209 | } |
| 210 | do { |
| 211 | char *kaddr; |
| 212 | |
Olaf Kirch | e053d1a | 2005-06-22 17:16:24 +0000 | [diff] [blame^] | 213 | /* ACL likes to be lazy in allocating pages - ACLs |
| 214 | * are small by default but can get huge. */ |
| 215 | if (unlikely(*ppage == NULL)) { |
| 216 | *ppage = alloc_page(GFP_ATOMIC); |
| 217 | if (unlikely(*ppage == NULL)) |
| 218 | return -ENOMEM; |
| 219 | } |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | len = PAGE_CACHE_SIZE; |
| 222 | kaddr = kmap_atomic(*ppage, KM_SKB_SUNRPC_DATA); |
| 223 | if (base) { |
| 224 | len -= base; |
| 225 | if (pglen < len) |
| 226 | len = pglen; |
| 227 | ret = copy_actor(desc, kaddr + base, len); |
| 228 | base = 0; |
| 229 | } else { |
| 230 | if (pglen < len) |
| 231 | len = pglen; |
| 232 | ret = copy_actor(desc, kaddr, len); |
| 233 | } |
| 234 | flush_dcache_page(*ppage); |
| 235 | kunmap_atomic(kaddr, KM_SKB_SUNRPC_DATA); |
| 236 | if (ret != len || !desc->count) |
Olaf Kirch | e053d1a | 2005-06-22 17:16:24 +0000 | [diff] [blame^] | 237 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | ppage++; |
| 239 | } while ((pglen -= len) != 0); |
| 240 | copy_tail: |
| 241 | len = xdr->tail[0].iov_len; |
| 242 | if (base < len) |
| 243 | copy_actor(desc, (char *)xdr->tail[0].iov_base + base, len - base); |
Olaf Kirch | e053d1a | 2005-06-22 17:16:24 +0000 | [diff] [blame^] | 244 | |
| 245 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
| 249 | int |
| 250 | xdr_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, |
| 251 | struct xdr_buf *xdr, unsigned int base, int msgflags) |
| 252 | { |
| 253 | struct page **ppage = xdr->pages; |
| 254 | unsigned int len, pglen = xdr->page_len; |
| 255 | int err, ret = 0; |
| 256 | ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int); |
| 257 | |
| 258 | len = xdr->head[0].iov_len; |
| 259 | if (base < len || (addr != NULL && base == 0)) { |
| 260 | struct kvec iov = { |
| 261 | .iov_base = xdr->head[0].iov_base + base, |
| 262 | .iov_len = len - base, |
| 263 | }; |
| 264 | struct msghdr msg = { |
| 265 | .msg_name = addr, |
| 266 | .msg_namelen = addrlen, |
| 267 | .msg_flags = msgflags, |
| 268 | }; |
| 269 | if (xdr->len > len) |
| 270 | msg.msg_flags |= MSG_MORE; |
| 271 | |
| 272 | if (iov.iov_len != 0) |
| 273 | err = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); |
| 274 | else |
| 275 | err = kernel_sendmsg(sock, &msg, NULL, 0, 0); |
| 276 | if (ret == 0) |
| 277 | ret = err; |
| 278 | else if (err > 0) |
| 279 | ret += err; |
| 280 | if (err != iov.iov_len) |
| 281 | goto out; |
| 282 | base = 0; |
| 283 | } else |
| 284 | base -= len; |
| 285 | |
| 286 | if (pglen == 0) |
| 287 | goto copy_tail; |
| 288 | if (base >= pglen) { |
| 289 | base -= pglen; |
| 290 | goto copy_tail; |
| 291 | } |
| 292 | if (base || xdr->page_base) { |
| 293 | pglen -= base; |
| 294 | base += xdr->page_base; |
| 295 | ppage += base >> PAGE_CACHE_SHIFT; |
| 296 | base &= ~PAGE_CACHE_MASK; |
| 297 | } |
| 298 | |
| 299 | sendpage = sock->ops->sendpage ? : sock_no_sendpage; |
| 300 | do { |
| 301 | int flags = msgflags; |
| 302 | |
| 303 | len = PAGE_CACHE_SIZE; |
| 304 | if (base) |
| 305 | len -= base; |
| 306 | if (pglen < len) |
| 307 | len = pglen; |
| 308 | |
| 309 | if (pglen != len || xdr->tail[0].iov_len != 0) |
| 310 | flags |= MSG_MORE; |
| 311 | |
| 312 | /* Hmm... We might be dealing with highmem pages */ |
| 313 | if (PageHighMem(*ppage)) |
| 314 | sendpage = sock_no_sendpage; |
| 315 | err = sendpage(sock, *ppage, base, len, flags); |
| 316 | if (ret == 0) |
| 317 | ret = err; |
| 318 | else if (err > 0) |
| 319 | ret += err; |
| 320 | if (err != len) |
| 321 | goto out; |
| 322 | base = 0; |
| 323 | ppage++; |
| 324 | } while ((pglen -= len) != 0); |
| 325 | copy_tail: |
| 326 | len = xdr->tail[0].iov_len; |
| 327 | if (base < len) { |
| 328 | struct kvec iov = { |
| 329 | .iov_base = xdr->tail[0].iov_base + base, |
| 330 | .iov_len = len - base, |
| 331 | }; |
| 332 | struct msghdr msg = { |
| 333 | .msg_flags = msgflags, |
| 334 | }; |
| 335 | err = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); |
| 336 | if (ret == 0) |
| 337 | ret = err; |
| 338 | else if (err > 0) |
| 339 | ret += err; |
| 340 | } |
| 341 | out: |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /* |
| 347 | * Helper routines for doing 'memmove' like operations on a struct xdr_buf |
| 348 | * |
| 349 | * _shift_data_right_pages |
| 350 | * @pages: vector of pages containing both the source and dest memory area. |
| 351 | * @pgto_base: page vector address of destination |
| 352 | * @pgfrom_base: page vector address of source |
| 353 | * @len: number of bytes to copy |
| 354 | * |
| 355 | * Note: the addresses pgto_base and pgfrom_base are both calculated in |
| 356 | * the same way: |
| 357 | * if a memory area starts at byte 'base' in page 'pages[i]', |
| 358 | * then its address is given as (i << PAGE_CACHE_SHIFT) + base |
| 359 | * Also note: pgfrom_base must be < pgto_base, but the memory areas |
| 360 | * they point to may overlap. |
| 361 | */ |
| 362 | static void |
| 363 | _shift_data_right_pages(struct page **pages, size_t pgto_base, |
| 364 | size_t pgfrom_base, size_t len) |
| 365 | { |
| 366 | struct page **pgfrom, **pgto; |
| 367 | char *vfrom, *vto; |
| 368 | size_t copy; |
| 369 | |
| 370 | BUG_ON(pgto_base <= pgfrom_base); |
| 371 | |
| 372 | pgto_base += len; |
| 373 | pgfrom_base += len; |
| 374 | |
| 375 | pgto = pages + (pgto_base >> PAGE_CACHE_SHIFT); |
| 376 | pgfrom = pages + (pgfrom_base >> PAGE_CACHE_SHIFT); |
| 377 | |
| 378 | pgto_base &= ~PAGE_CACHE_MASK; |
| 379 | pgfrom_base &= ~PAGE_CACHE_MASK; |
| 380 | |
| 381 | do { |
| 382 | /* Are any pointers crossing a page boundary? */ |
| 383 | if (pgto_base == 0) { |
| 384 | flush_dcache_page(*pgto); |
| 385 | pgto_base = PAGE_CACHE_SIZE; |
| 386 | pgto--; |
| 387 | } |
| 388 | if (pgfrom_base == 0) { |
| 389 | pgfrom_base = PAGE_CACHE_SIZE; |
| 390 | pgfrom--; |
| 391 | } |
| 392 | |
| 393 | copy = len; |
| 394 | if (copy > pgto_base) |
| 395 | copy = pgto_base; |
| 396 | if (copy > pgfrom_base) |
| 397 | copy = pgfrom_base; |
| 398 | pgto_base -= copy; |
| 399 | pgfrom_base -= copy; |
| 400 | |
| 401 | vto = kmap_atomic(*pgto, KM_USER0); |
| 402 | vfrom = kmap_atomic(*pgfrom, KM_USER1); |
| 403 | memmove(vto + pgto_base, vfrom + pgfrom_base, copy); |
| 404 | kunmap_atomic(vfrom, KM_USER1); |
| 405 | kunmap_atomic(vto, KM_USER0); |
| 406 | |
| 407 | } while ((len -= copy) != 0); |
| 408 | flush_dcache_page(*pgto); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * _copy_to_pages |
| 413 | * @pages: array of pages |
| 414 | * @pgbase: page vector address of destination |
| 415 | * @p: pointer to source data |
| 416 | * @len: length |
| 417 | * |
| 418 | * Copies data from an arbitrary memory location into an array of pages |
| 419 | * The copy is assumed to be non-overlapping. |
| 420 | */ |
| 421 | static void |
| 422 | _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len) |
| 423 | { |
| 424 | struct page **pgto; |
| 425 | char *vto; |
| 426 | size_t copy; |
| 427 | |
| 428 | pgto = pages + (pgbase >> PAGE_CACHE_SHIFT); |
| 429 | pgbase &= ~PAGE_CACHE_MASK; |
| 430 | |
| 431 | do { |
| 432 | copy = PAGE_CACHE_SIZE - pgbase; |
| 433 | if (copy > len) |
| 434 | copy = len; |
| 435 | |
| 436 | vto = kmap_atomic(*pgto, KM_USER0); |
| 437 | memcpy(vto + pgbase, p, copy); |
| 438 | kunmap_atomic(vto, KM_USER0); |
| 439 | |
| 440 | pgbase += copy; |
| 441 | if (pgbase == PAGE_CACHE_SIZE) { |
| 442 | flush_dcache_page(*pgto); |
| 443 | pgbase = 0; |
| 444 | pgto++; |
| 445 | } |
| 446 | p += copy; |
| 447 | |
| 448 | } while ((len -= copy) != 0); |
| 449 | flush_dcache_page(*pgto); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * _copy_from_pages |
| 454 | * @p: pointer to destination |
| 455 | * @pages: array of pages |
| 456 | * @pgbase: offset of source data |
| 457 | * @len: length |
| 458 | * |
| 459 | * Copies data into an arbitrary memory location from an array of pages |
| 460 | * The copy is assumed to be non-overlapping. |
| 461 | */ |
| 462 | static void |
| 463 | _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len) |
| 464 | { |
| 465 | struct page **pgfrom; |
| 466 | char *vfrom; |
| 467 | size_t copy; |
| 468 | |
| 469 | pgfrom = pages + (pgbase >> PAGE_CACHE_SHIFT); |
| 470 | pgbase &= ~PAGE_CACHE_MASK; |
| 471 | |
| 472 | do { |
| 473 | copy = PAGE_CACHE_SIZE - pgbase; |
| 474 | if (copy > len) |
| 475 | copy = len; |
| 476 | |
| 477 | vfrom = kmap_atomic(*pgfrom, KM_USER0); |
| 478 | memcpy(p, vfrom + pgbase, copy); |
| 479 | kunmap_atomic(vfrom, KM_USER0); |
| 480 | |
| 481 | pgbase += copy; |
| 482 | if (pgbase == PAGE_CACHE_SIZE) { |
| 483 | pgbase = 0; |
| 484 | pgfrom++; |
| 485 | } |
| 486 | p += copy; |
| 487 | |
| 488 | } while ((len -= copy) != 0); |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * xdr_shrink_bufhead |
| 493 | * @buf: xdr_buf |
| 494 | * @len: bytes to remove from buf->head[0] |
| 495 | * |
| 496 | * Shrinks XDR buffer's header kvec buf->head[0] by |
| 497 | * 'len' bytes. The extra data is not lost, but is instead |
| 498 | * moved into the inlined pages and/or the tail. |
| 499 | */ |
| 500 | static void |
| 501 | xdr_shrink_bufhead(struct xdr_buf *buf, size_t len) |
| 502 | { |
| 503 | struct kvec *head, *tail; |
| 504 | size_t copy, offs; |
| 505 | unsigned int pglen = buf->page_len; |
| 506 | |
| 507 | tail = buf->tail; |
| 508 | head = buf->head; |
| 509 | BUG_ON (len > head->iov_len); |
| 510 | |
| 511 | /* Shift the tail first */ |
| 512 | if (tail->iov_len != 0) { |
| 513 | if (tail->iov_len > len) { |
| 514 | copy = tail->iov_len - len; |
| 515 | memmove((char *)tail->iov_base + len, |
| 516 | tail->iov_base, copy); |
| 517 | } |
| 518 | /* Copy from the inlined pages into the tail */ |
| 519 | copy = len; |
| 520 | if (copy > pglen) |
| 521 | copy = pglen; |
| 522 | offs = len - copy; |
| 523 | if (offs >= tail->iov_len) |
| 524 | copy = 0; |
| 525 | else if (copy > tail->iov_len - offs) |
| 526 | copy = tail->iov_len - offs; |
| 527 | if (copy != 0) |
| 528 | _copy_from_pages((char *)tail->iov_base + offs, |
| 529 | buf->pages, |
| 530 | buf->page_base + pglen + offs - len, |
| 531 | copy); |
| 532 | /* Do we also need to copy data from the head into the tail ? */ |
| 533 | if (len > pglen) { |
| 534 | offs = copy = len - pglen; |
| 535 | if (copy > tail->iov_len) |
| 536 | copy = tail->iov_len; |
| 537 | memcpy(tail->iov_base, |
| 538 | (char *)head->iov_base + |
| 539 | head->iov_len - offs, |
| 540 | copy); |
| 541 | } |
| 542 | } |
| 543 | /* Now handle pages */ |
| 544 | if (pglen != 0) { |
| 545 | if (pglen > len) |
| 546 | _shift_data_right_pages(buf->pages, |
| 547 | buf->page_base + len, |
| 548 | buf->page_base, |
| 549 | pglen - len); |
| 550 | copy = len; |
| 551 | if (len > pglen) |
| 552 | copy = pglen; |
| 553 | _copy_to_pages(buf->pages, buf->page_base, |
| 554 | (char *)head->iov_base + head->iov_len - len, |
| 555 | copy); |
| 556 | } |
| 557 | head->iov_len -= len; |
| 558 | buf->buflen -= len; |
| 559 | /* Have we truncated the message? */ |
| 560 | if (buf->len > buf->buflen) |
| 561 | buf->len = buf->buflen; |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * xdr_shrink_pagelen |
| 566 | * @buf: xdr_buf |
| 567 | * @len: bytes to remove from buf->pages |
| 568 | * |
| 569 | * Shrinks XDR buffer's page array buf->pages by |
| 570 | * 'len' bytes. The extra data is not lost, but is instead |
| 571 | * moved into the tail. |
| 572 | */ |
| 573 | static void |
| 574 | xdr_shrink_pagelen(struct xdr_buf *buf, size_t len) |
| 575 | { |
| 576 | struct kvec *tail; |
| 577 | size_t copy; |
| 578 | char *p; |
| 579 | unsigned int pglen = buf->page_len; |
| 580 | |
| 581 | tail = buf->tail; |
| 582 | BUG_ON (len > pglen); |
| 583 | |
| 584 | /* Shift the tail first */ |
| 585 | if (tail->iov_len != 0) { |
| 586 | p = (char *)tail->iov_base + len; |
| 587 | if (tail->iov_len > len) { |
| 588 | copy = tail->iov_len - len; |
| 589 | memmove(p, tail->iov_base, copy); |
| 590 | } else |
| 591 | buf->buflen -= len; |
| 592 | /* Copy from the inlined pages into the tail */ |
| 593 | copy = len; |
| 594 | if (copy > tail->iov_len) |
| 595 | copy = tail->iov_len; |
| 596 | _copy_from_pages((char *)tail->iov_base, |
| 597 | buf->pages, buf->page_base + pglen - len, |
| 598 | copy); |
| 599 | } |
| 600 | buf->page_len -= len; |
| 601 | buf->buflen -= len; |
| 602 | /* Have we truncated the message? */ |
| 603 | if (buf->len > buf->buflen) |
| 604 | buf->len = buf->buflen; |
| 605 | } |
| 606 | |
| 607 | void |
| 608 | xdr_shift_buf(struct xdr_buf *buf, size_t len) |
| 609 | { |
| 610 | xdr_shrink_bufhead(buf, len); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * xdr_init_encode - Initialize a struct xdr_stream for sending data. |
| 615 | * @xdr: pointer to xdr_stream struct |
| 616 | * @buf: pointer to XDR buffer in which to encode data |
| 617 | * @p: current pointer inside XDR buffer |
| 618 | * |
| 619 | * Note: at the moment the RPC client only passes the length of our |
| 620 | * scratch buffer in the xdr_buf's header kvec. Previously this |
| 621 | * meant we needed to call xdr_adjust_iovec() after encoding the |
| 622 | * data. With the new scheme, the xdr_stream manages the details |
| 623 | * of the buffer length, and takes care of adjusting the kvec |
| 624 | * length for us. |
| 625 | */ |
| 626 | void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p) |
| 627 | { |
| 628 | struct kvec *iov = buf->head; |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 629 | 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] | 630 | |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 631 | BUG_ON(scratch_len < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | xdr->buf = buf; |
| 633 | xdr->iov = iov; |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 634 | xdr->p = (uint32_t *)((char *)iov->iov_base + iov->iov_len); |
| 635 | xdr->end = (uint32_t *)((char *)iov->iov_base + scratch_len); |
| 636 | BUG_ON(iov->iov_len > scratch_len); |
| 637 | |
| 638 | if (p != xdr->p && p != NULL) { |
| 639 | size_t len; |
| 640 | |
| 641 | BUG_ON(p < xdr->p || p > xdr->end); |
| 642 | len = (char *)p - (char *)xdr->p; |
| 643 | xdr->p = p; |
| 644 | buf->len += len; |
| 645 | iov->iov_len += len; |
| 646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | EXPORT_SYMBOL(xdr_init_encode); |
| 649 | |
| 650 | /** |
| 651 | * xdr_reserve_space - Reserve buffer space for sending |
| 652 | * @xdr: pointer to xdr_stream |
| 653 | * @nbytes: number of bytes to reserve |
| 654 | * |
| 655 | * Checks that we have enough buffer space to encode 'nbytes' more |
| 656 | * bytes of data. If so, update the total xdr_buf length, and |
| 657 | * adjust the length of the current kvec. |
| 658 | */ |
| 659 | uint32_t * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes) |
| 660 | { |
| 661 | uint32_t *p = xdr->p; |
| 662 | uint32_t *q; |
| 663 | |
| 664 | /* align nbytes on the next 32-bit boundary */ |
| 665 | nbytes += 3; |
| 666 | nbytes &= ~3; |
| 667 | q = p + (nbytes >> 2); |
| 668 | if (unlikely(q > xdr->end || q < p)) |
| 669 | return NULL; |
| 670 | xdr->p = q; |
| 671 | xdr->iov->iov_len += nbytes; |
| 672 | xdr->buf->len += nbytes; |
| 673 | return p; |
| 674 | } |
| 675 | EXPORT_SYMBOL(xdr_reserve_space); |
| 676 | |
| 677 | /** |
| 678 | * xdr_write_pages - Insert a list of pages into an XDR buffer for sending |
| 679 | * @xdr: pointer to xdr_stream |
| 680 | * @pages: list of pages |
| 681 | * @base: offset of first byte |
| 682 | * @len: length of data in bytes |
| 683 | * |
| 684 | */ |
| 685 | void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base, |
| 686 | unsigned int len) |
| 687 | { |
| 688 | struct xdr_buf *buf = xdr->buf; |
| 689 | struct kvec *iov = buf->tail; |
| 690 | buf->pages = pages; |
| 691 | buf->page_base = base; |
| 692 | buf->page_len = len; |
| 693 | |
| 694 | iov->iov_base = (char *)xdr->p; |
| 695 | iov->iov_len = 0; |
| 696 | xdr->iov = iov; |
| 697 | |
| 698 | if (len & 3) { |
| 699 | unsigned int pad = 4 - (len & 3); |
| 700 | |
| 701 | BUG_ON(xdr->p >= xdr->end); |
| 702 | iov->iov_base = (char *)xdr->p + (len & 3); |
| 703 | iov->iov_len += pad; |
| 704 | len += pad; |
| 705 | *xdr->p++ = 0; |
| 706 | } |
| 707 | buf->buflen += len; |
| 708 | buf->len += len; |
| 709 | } |
| 710 | EXPORT_SYMBOL(xdr_write_pages); |
| 711 | |
| 712 | /** |
| 713 | * xdr_init_decode - Initialize an xdr_stream for decoding data. |
| 714 | * @xdr: pointer to xdr_stream struct |
| 715 | * @buf: pointer to XDR buffer from which to decode data |
| 716 | * @p: current pointer inside XDR buffer |
| 717 | */ |
| 718 | void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p) |
| 719 | { |
| 720 | struct kvec *iov = buf->head; |
| 721 | unsigned int len = iov->iov_len; |
| 722 | |
| 723 | if (len > buf->len) |
| 724 | len = buf->len; |
| 725 | xdr->buf = buf; |
| 726 | xdr->iov = iov; |
| 727 | xdr->p = p; |
| 728 | xdr->end = (uint32_t *)((char *)iov->iov_base + len); |
| 729 | } |
| 730 | EXPORT_SYMBOL(xdr_init_decode); |
| 731 | |
| 732 | /** |
| 733 | * xdr_inline_decode - Retrieve non-page XDR data to decode |
| 734 | * @xdr: pointer to xdr_stream struct |
| 735 | * @nbytes: number of bytes of data to decode |
| 736 | * |
| 737 | * Check if the input buffer is long enough to enable us to decode |
| 738 | * 'nbytes' more bytes of data starting at the current position. |
| 739 | * If so return the current pointer, then update the current |
| 740 | * pointer position. |
| 741 | */ |
| 742 | uint32_t * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes) |
| 743 | { |
| 744 | uint32_t *p = xdr->p; |
| 745 | uint32_t *q = p + XDR_QUADLEN(nbytes); |
| 746 | |
| 747 | if (unlikely(q > xdr->end || q < p)) |
| 748 | return NULL; |
| 749 | xdr->p = q; |
| 750 | return p; |
| 751 | } |
| 752 | EXPORT_SYMBOL(xdr_inline_decode); |
| 753 | |
| 754 | /** |
| 755 | * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position |
| 756 | * @xdr: pointer to xdr_stream struct |
| 757 | * @len: number of bytes of page data |
| 758 | * |
| 759 | * Moves data beyond the current pointer position from the XDR head[] buffer |
| 760 | * into the page list. Any data that lies beyond current position + "len" |
| 761 | * bytes is moved into the XDR tail[]. The current pointer is then |
| 762 | * repositioned at the beginning of the XDR tail. |
| 763 | */ |
| 764 | void xdr_read_pages(struct xdr_stream *xdr, unsigned int len) |
| 765 | { |
| 766 | struct xdr_buf *buf = xdr->buf; |
| 767 | struct kvec *iov; |
| 768 | ssize_t shift; |
| 769 | unsigned int end; |
| 770 | int padding; |
| 771 | |
| 772 | /* Realign pages to current pointer position */ |
| 773 | iov = buf->head; |
| 774 | shift = iov->iov_len + (char *)iov->iov_base - (char *)xdr->p; |
| 775 | if (shift > 0) |
| 776 | xdr_shrink_bufhead(buf, shift); |
| 777 | |
| 778 | /* Truncate page data and move it into the tail */ |
| 779 | if (buf->page_len > len) |
| 780 | xdr_shrink_pagelen(buf, buf->page_len - len); |
| 781 | padding = (XDR_QUADLEN(len) << 2) - len; |
| 782 | xdr->iov = iov = buf->tail; |
| 783 | /* Compute remaining message length. */ |
| 784 | end = iov->iov_len; |
| 785 | shift = buf->buflen - buf->len; |
| 786 | if (shift < end) |
| 787 | end -= shift; |
| 788 | else if (shift > 0) |
| 789 | end = 0; |
| 790 | /* |
| 791 | * Position current pointer at beginning of tail, and |
| 792 | * set remaining message length. |
| 793 | */ |
| 794 | xdr->p = (uint32_t *)((char *)iov->iov_base + padding); |
| 795 | xdr->end = (uint32_t *)((char *)iov->iov_base + end); |
| 796 | } |
| 797 | EXPORT_SYMBOL(xdr_read_pages); |
| 798 | |
| 799 | static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0}; |
| 800 | |
| 801 | void |
| 802 | xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf) |
| 803 | { |
| 804 | buf->head[0] = *iov; |
| 805 | buf->tail[0] = empty_iov; |
| 806 | buf->page_len = 0; |
| 807 | buf->buflen = buf->len = iov->iov_len; |
| 808 | } |
| 809 | |
| 810 | /* Sets subiov to the intersection of iov with the buffer of length len |
| 811 | * starting base bytes after iov. Indicates empty intersection by setting |
| 812 | * length of subiov to zero. Decrements len by length of subiov, sets base |
| 813 | * to zero (or decrements it by length of iov if subiov is empty). */ |
| 814 | static void |
| 815 | iov_subsegment(struct kvec *iov, struct kvec *subiov, int *base, int *len) |
| 816 | { |
| 817 | if (*base > iov->iov_len) { |
| 818 | subiov->iov_base = NULL; |
| 819 | subiov->iov_len = 0; |
| 820 | *base -= iov->iov_len; |
| 821 | } else { |
| 822 | subiov->iov_base = iov->iov_base + *base; |
| 823 | subiov->iov_len = min(*len, (int)iov->iov_len - *base); |
| 824 | *base = 0; |
| 825 | } |
| 826 | *len -= subiov->iov_len; |
| 827 | } |
| 828 | |
| 829 | /* Sets subbuf to the portion of buf of length len beginning base bytes |
| 830 | * from the start of buf. Returns -1 if base of length are out of bounds. */ |
| 831 | int |
| 832 | xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf, |
| 833 | int base, int len) |
| 834 | { |
| 835 | int i; |
| 836 | |
| 837 | subbuf->buflen = subbuf->len = len; |
| 838 | iov_subsegment(buf->head, subbuf->head, &base, &len); |
| 839 | |
| 840 | if (base < buf->page_len) { |
| 841 | i = (base + buf->page_base) >> PAGE_CACHE_SHIFT; |
| 842 | subbuf->pages = &buf->pages[i]; |
| 843 | subbuf->page_base = (base + buf->page_base) & ~PAGE_CACHE_MASK; |
| 844 | subbuf->page_len = min((int)buf->page_len - base, len); |
| 845 | len -= subbuf->page_len; |
| 846 | base = 0; |
| 847 | } else { |
| 848 | base -= buf->page_len; |
| 849 | subbuf->page_len = 0; |
| 850 | } |
| 851 | |
| 852 | iov_subsegment(buf->tail, subbuf->tail, &base, &len); |
| 853 | if (base || len) |
| 854 | return -1; |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | /* obj is assumed to point to allocated memory of size at least len: */ |
| 859 | int |
| 860 | read_bytes_from_xdr_buf(struct xdr_buf *buf, int base, void *obj, int len) |
| 861 | { |
| 862 | struct xdr_buf subbuf; |
| 863 | int this_len; |
| 864 | int status; |
| 865 | |
| 866 | status = xdr_buf_subsegment(buf, &subbuf, base, len); |
| 867 | if (status) |
| 868 | goto out; |
| 869 | this_len = min(len, (int)subbuf.head[0].iov_len); |
| 870 | memcpy(obj, subbuf.head[0].iov_base, this_len); |
| 871 | len -= this_len; |
| 872 | obj += this_len; |
| 873 | this_len = min(len, (int)subbuf.page_len); |
| 874 | if (this_len) |
| 875 | _copy_from_pages(obj, subbuf.pages, subbuf.page_base, this_len); |
| 876 | len -= this_len; |
| 877 | obj += this_len; |
| 878 | this_len = min(len, (int)subbuf.tail[0].iov_len); |
| 879 | memcpy(obj, subbuf.tail[0].iov_base, this_len); |
| 880 | out: |
| 881 | return status; |
| 882 | } |
| 883 | |
| 884 | static int |
| 885 | read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj) |
| 886 | { |
| 887 | u32 raw; |
| 888 | int status; |
| 889 | |
| 890 | status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj)); |
| 891 | if (status) |
| 892 | return status; |
| 893 | *obj = ntohl(raw); |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | /* If the netobj starting offset bytes from the start of xdr_buf is contained |
| 898 | * entirely in the head or the tail, set object to point to it; otherwise |
| 899 | * try to find space for it at the end of the tail, copy it there, and |
| 900 | * set obj to point to it. */ |
| 901 | int |
| 902 | xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, int offset) |
| 903 | { |
| 904 | u32 tail_offset = buf->head[0].iov_len + buf->page_len; |
| 905 | u32 obj_end_offset; |
| 906 | |
| 907 | if (read_u32_from_xdr_buf(buf, offset, &obj->len)) |
| 908 | goto out; |
| 909 | obj_end_offset = offset + 4 + obj->len; |
| 910 | |
| 911 | if (obj_end_offset <= buf->head[0].iov_len) { |
| 912 | /* The obj is contained entirely in the head: */ |
| 913 | obj->data = buf->head[0].iov_base + offset + 4; |
| 914 | } else if (offset + 4 >= tail_offset) { |
| 915 | if (obj_end_offset - tail_offset |
| 916 | > buf->tail[0].iov_len) |
| 917 | goto out; |
| 918 | /* The obj is contained entirely in the tail: */ |
| 919 | obj->data = buf->tail[0].iov_base |
| 920 | + offset - tail_offset + 4; |
| 921 | } else { |
| 922 | /* use end of tail as storage for obj: |
| 923 | * (We don't copy to the beginning because then we'd have |
| 924 | * to worry about doing a potentially overlapping copy. |
| 925 | * This assumes the object is at most half the length of the |
| 926 | * tail.) */ |
| 927 | if (obj->len > buf->tail[0].iov_len) |
| 928 | goto out; |
| 929 | obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len - |
| 930 | obj->len; |
| 931 | if (read_bytes_from_xdr_buf(buf, offset + 4, |
| 932 | obj->data, obj->len)) |
| 933 | goto out; |
| 934 | |
| 935 | } |
| 936 | return 0; |
| 937 | out: |
| 938 | return -1; |
| 939 | } |