| 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 |  | 
|  | 179 | void | 
|  | 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) | 
|  | 193 | return; | 
|  | 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 |  | 
|  | 213 | len = PAGE_CACHE_SIZE; | 
|  | 214 | kaddr = kmap_atomic(*ppage, KM_SKB_SUNRPC_DATA); | 
|  | 215 | if (base) { | 
|  | 216 | len -= base; | 
|  | 217 | if (pglen < len) | 
|  | 218 | len = pglen; | 
|  | 219 | ret = copy_actor(desc, kaddr + base, len); | 
|  | 220 | base = 0; | 
|  | 221 | } else { | 
|  | 222 | if (pglen < len) | 
|  | 223 | len = pglen; | 
|  | 224 | ret = copy_actor(desc, kaddr, len); | 
|  | 225 | } | 
|  | 226 | flush_dcache_page(*ppage); | 
|  | 227 | kunmap_atomic(kaddr, KM_SKB_SUNRPC_DATA); | 
|  | 228 | if (ret != len || !desc->count) | 
|  | 229 | return; | 
|  | 230 | ppage++; | 
|  | 231 | } while ((pglen -= len) != 0); | 
|  | 232 | copy_tail: | 
|  | 233 | len = xdr->tail[0].iov_len; | 
|  | 234 | if (base < len) | 
|  | 235 | copy_actor(desc, (char *)xdr->tail[0].iov_base + base, len - base); | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 |  | 
|  | 239 | int | 
|  | 240 | xdr_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, | 
|  | 241 | struct xdr_buf *xdr, unsigned int base, int msgflags) | 
|  | 242 | { | 
|  | 243 | struct page **ppage = xdr->pages; | 
|  | 244 | unsigned int len, pglen = xdr->page_len; | 
|  | 245 | int err, ret = 0; | 
|  | 246 | ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int); | 
|  | 247 |  | 
|  | 248 | len = xdr->head[0].iov_len; | 
|  | 249 | if (base < len || (addr != NULL && base == 0)) { | 
|  | 250 | struct kvec iov = { | 
|  | 251 | .iov_base = xdr->head[0].iov_base + base, | 
|  | 252 | .iov_len  = len - base, | 
|  | 253 | }; | 
|  | 254 | struct msghdr msg = { | 
|  | 255 | .msg_name    = addr, | 
|  | 256 | .msg_namelen = addrlen, | 
|  | 257 | .msg_flags   = msgflags, | 
|  | 258 | }; | 
|  | 259 | if (xdr->len > len) | 
|  | 260 | msg.msg_flags |= MSG_MORE; | 
|  | 261 |  | 
|  | 262 | if (iov.iov_len != 0) | 
|  | 263 | err = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); | 
|  | 264 | else | 
|  | 265 | err = kernel_sendmsg(sock, &msg, NULL, 0, 0); | 
|  | 266 | if (ret == 0) | 
|  | 267 | ret = err; | 
|  | 268 | else if (err > 0) | 
|  | 269 | ret += err; | 
|  | 270 | if (err != iov.iov_len) | 
|  | 271 | goto out; | 
|  | 272 | base = 0; | 
|  | 273 | } else | 
|  | 274 | base -= len; | 
|  | 275 |  | 
|  | 276 | if (pglen == 0) | 
|  | 277 | goto copy_tail; | 
|  | 278 | if (base >= pglen) { | 
|  | 279 | base -= pglen; | 
|  | 280 | goto copy_tail; | 
|  | 281 | } | 
|  | 282 | if (base || xdr->page_base) { | 
|  | 283 | pglen -= base; | 
|  | 284 | base  += xdr->page_base; | 
|  | 285 | ppage += base >> PAGE_CACHE_SHIFT; | 
|  | 286 | base &= ~PAGE_CACHE_MASK; | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | sendpage = sock->ops->sendpage ? : sock_no_sendpage; | 
|  | 290 | do { | 
|  | 291 | int flags = msgflags; | 
|  | 292 |  | 
|  | 293 | len = PAGE_CACHE_SIZE; | 
|  | 294 | if (base) | 
|  | 295 | len -= base; | 
|  | 296 | if (pglen < len) | 
|  | 297 | len = pglen; | 
|  | 298 |  | 
|  | 299 | if (pglen != len || xdr->tail[0].iov_len != 0) | 
|  | 300 | flags |= MSG_MORE; | 
|  | 301 |  | 
|  | 302 | /* Hmm... We might be dealing with highmem pages */ | 
|  | 303 | if (PageHighMem(*ppage)) | 
|  | 304 | sendpage = sock_no_sendpage; | 
|  | 305 | err = sendpage(sock, *ppage, base, len, flags); | 
|  | 306 | if (ret == 0) | 
|  | 307 | ret = err; | 
|  | 308 | else if (err > 0) | 
|  | 309 | ret += err; | 
|  | 310 | if (err != len) | 
|  | 311 | goto out; | 
|  | 312 | base = 0; | 
|  | 313 | ppage++; | 
|  | 314 | } while ((pglen -= len) != 0); | 
|  | 315 | copy_tail: | 
|  | 316 | len = xdr->tail[0].iov_len; | 
|  | 317 | if (base < len) { | 
|  | 318 | struct kvec iov = { | 
|  | 319 | .iov_base = xdr->tail[0].iov_base + base, | 
|  | 320 | .iov_len  = len - base, | 
|  | 321 | }; | 
|  | 322 | struct msghdr msg = { | 
|  | 323 | .msg_flags   = msgflags, | 
|  | 324 | }; | 
|  | 325 | err = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); | 
|  | 326 | if (ret == 0) | 
|  | 327 | ret = err; | 
|  | 328 | else if (err > 0) | 
|  | 329 | ret += err; | 
|  | 330 | } | 
|  | 331 | out: | 
|  | 332 | return ret; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 |  | 
|  | 336 | /* | 
|  | 337 | * Helper routines for doing 'memmove' like operations on a struct xdr_buf | 
|  | 338 | * | 
|  | 339 | * _shift_data_right_pages | 
|  | 340 | * @pages: vector of pages containing both the source and dest memory area. | 
|  | 341 | * @pgto_base: page vector address of destination | 
|  | 342 | * @pgfrom_base: page vector address of source | 
|  | 343 | * @len: number of bytes to copy | 
|  | 344 | * | 
|  | 345 | * Note: the addresses pgto_base and pgfrom_base are both calculated in | 
|  | 346 | *       the same way: | 
|  | 347 | *            if a memory area starts at byte 'base' in page 'pages[i]', | 
|  | 348 | *            then its address is given as (i << PAGE_CACHE_SHIFT) + base | 
|  | 349 | * Also note: pgfrom_base must be < pgto_base, but the memory areas | 
|  | 350 | * 	they point to may overlap. | 
|  | 351 | */ | 
|  | 352 | static void | 
|  | 353 | _shift_data_right_pages(struct page **pages, size_t pgto_base, | 
|  | 354 | size_t pgfrom_base, size_t len) | 
|  | 355 | { | 
|  | 356 | struct page **pgfrom, **pgto; | 
|  | 357 | char *vfrom, *vto; | 
|  | 358 | size_t copy; | 
|  | 359 |  | 
|  | 360 | BUG_ON(pgto_base <= pgfrom_base); | 
|  | 361 |  | 
|  | 362 | pgto_base += len; | 
|  | 363 | pgfrom_base += len; | 
|  | 364 |  | 
|  | 365 | pgto = pages + (pgto_base >> PAGE_CACHE_SHIFT); | 
|  | 366 | pgfrom = pages + (pgfrom_base >> PAGE_CACHE_SHIFT); | 
|  | 367 |  | 
|  | 368 | pgto_base &= ~PAGE_CACHE_MASK; | 
|  | 369 | pgfrom_base &= ~PAGE_CACHE_MASK; | 
|  | 370 |  | 
|  | 371 | do { | 
|  | 372 | /* Are any pointers crossing a page boundary? */ | 
|  | 373 | if (pgto_base == 0) { | 
|  | 374 | flush_dcache_page(*pgto); | 
|  | 375 | pgto_base = PAGE_CACHE_SIZE; | 
|  | 376 | pgto--; | 
|  | 377 | } | 
|  | 378 | if (pgfrom_base == 0) { | 
|  | 379 | pgfrom_base = PAGE_CACHE_SIZE; | 
|  | 380 | pgfrom--; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | copy = len; | 
|  | 384 | if (copy > pgto_base) | 
|  | 385 | copy = pgto_base; | 
|  | 386 | if (copy > pgfrom_base) | 
|  | 387 | copy = pgfrom_base; | 
|  | 388 | pgto_base -= copy; | 
|  | 389 | pgfrom_base -= copy; | 
|  | 390 |  | 
|  | 391 | vto = kmap_atomic(*pgto, KM_USER0); | 
|  | 392 | vfrom = kmap_atomic(*pgfrom, KM_USER1); | 
|  | 393 | memmove(vto + pgto_base, vfrom + pgfrom_base, copy); | 
|  | 394 | kunmap_atomic(vfrom, KM_USER1); | 
|  | 395 | kunmap_atomic(vto, KM_USER0); | 
|  | 396 |  | 
|  | 397 | } while ((len -= copy) != 0); | 
|  | 398 | flush_dcache_page(*pgto); | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | /* | 
|  | 402 | * _copy_to_pages | 
|  | 403 | * @pages: array of pages | 
|  | 404 | * @pgbase: page vector address of destination | 
|  | 405 | * @p: pointer to source data | 
|  | 406 | * @len: length | 
|  | 407 | * | 
|  | 408 | * Copies data from an arbitrary memory location into an array of pages | 
|  | 409 | * The copy is assumed to be non-overlapping. | 
|  | 410 | */ | 
|  | 411 | static void | 
|  | 412 | _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len) | 
|  | 413 | { | 
|  | 414 | struct page **pgto; | 
|  | 415 | char *vto; | 
|  | 416 | size_t copy; | 
|  | 417 |  | 
|  | 418 | pgto = pages + (pgbase >> PAGE_CACHE_SHIFT); | 
|  | 419 | pgbase &= ~PAGE_CACHE_MASK; | 
|  | 420 |  | 
|  | 421 | do { | 
|  | 422 | copy = PAGE_CACHE_SIZE - pgbase; | 
|  | 423 | if (copy > len) | 
|  | 424 | copy = len; | 
|  | 425 |  | 
|  | 426 | vto = kmap_atomic(*pgto, KM_USER0); | 
|  | 427 | memcpy(vto + pgbase, p, copy); | 
|  | 428 | kunmap_atomic(vto, KM_USER0); | 
|  | 429 |  | 
|  | 430 | pgbase += copy; | 
|  | 431 | if (pgbase == PAGE_CACHE_SIZE) { | 
|  | 432 | flush_dcache_page(*pgto); | 
|  | 433 | pgbase = 0; | 
|  | 434 | pgto++; | 
|  | 435 | } | 
|  | 436 | p += copy; | 
|  | 437 |  | 
|  | 438 | } while ((len -= copy) != 0); | 
|  | 439 | flush_dcache_page(*pgto); | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | /* | 
|  | 443 | * _copy_from_pages | 
|  | 444 | * @p: pointer to destination | 
|  | 445 | * @pages: array of pages | 
|  | 446 | * @pgbase: offset of source data | 
|  | 447 | * @len: length | 
|  | 448 | * | 
|  | 449 | * Copies data into an arbitrary memory location from an array of pages | 
|  | 450 | * The copy is assumed to be non-overlapping. | 
|  | 451 | */ | 
|  | 452 | static void | 
|  | 453 | _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len) | 
|  | 454 | { | 
|  | 455 | struct page **pgfrom; | 
|  | 456 | char *vfrom; | 
|  | 457 | size_t copy; | 
|  | 458 |  | 
|  | 459 | pgfrom = pages + (pgbase >> PAGE_CACHE_SHIFT); | 
|  | 460 | pgbase &= ~PAGE_CACHE_MASK; | 
|  | 461 |  | 
|  | 462 | do { | 
|  | 463 | copy = PAGE_CACHE_SIZE - pgbase; | 
|  | 464 | if (copy > len) | 
|  | 465 | copy = len; | 
|  | 466 |  | 
|  | 467 | vfrom = kmap_atomic(*pgfrom, KM_USER0); | 
|  | 468 | memcpy(p, vfrom + pgbase, copy); | 
|  | 469 | kunmap_atomic(vfrom, KM_USER0); | 
|  | 470 |  | 
|  | 471 | pgbase += copy; | 
|  | 472 | if (pgbase == PAGE_CACHE_SIZE) { | 
|  | 473 | pgbase = 0; | 
|  | 474 | pgfrom++; | 
|  | 475 | } | 
|  | 476 | p += copy; | 
|  | 477 |  | 
|  | 478 | } while ((len -= copy) != 0); | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | /* | 
|  | 482 | * xdr_shrink_bufhead | 
|  | 483 | * @buf: xdr_buf | 
|  | 484 | * @len: bytes to remove from buf->head[0] | 
|  | 485 | * | 
|  | 486 | * Shrinks XDR buffer's header kvec buf->head[0] by | 
|  | 487 | * 'len' bytes. The extra data is not lost, but is instead | 
|  | 488 | * moved into the inlined pages and/or the tail. | 
|  | 489 | */ | 
|  | 490 | static void | 
|  | 491 | xdr_shrink_bufhead(struct xdr_buf *buf, size_t len) | 
|  | 492 | { | 
|  | 493 | struct kvec *head, *tail; | 
|  | 494 | size_t copy, offs; | 
|  | 495 | unsigned int pglen = buf->page_len; | 
|  | 496 |  | 
|  | 497 | tail = buf->tail; | 
|  | 498 | head = buf->head; | 
|  | 499 | BUG_ON (len > head->iov_len); | 
|  | 500 |  | 
|  | 501 | /* Shift the tail first */ | 
|  | 502 | if (tail->iov_len != 0) { | 
|  | 503 | if (tail->iov_len > len) { | 
|  | 504 | copy = tail->iov_len - len; | 
|  | 505 | memmove((char *)tail->iov_base + len, | 
|  | 506 | tail->iov_base, copy); | 
|  | 507 | } | 
|  | 508 | /* Copy from the inlined pages into the tail */ | 
|  | 509 | copy = len; | 
|  | 510 | if (copy > pglen) | 
|  | 511 | copy = pglen; | 
|  | 512 | offs = len - copy; | 
|  | 513 | if (offs >= tail->iov_len) | 
|  | 514 | copy = 0; | 
|  | 515 | else if (copy > tail->iov_len - offs) | 
|  | 516 | copy = tail->iov_len - offs; | 
|  | 517 | if (copy != 0) | 
|  | 518 | _copy_from_pages((char *)tail->iov_base + offs, | 
|  | 519 | buf->pages, | 
|  | 520 | buf->page_base + pglen + offs - len, | 
|  | 521 | copy); | 
|  | 522 | /* Do we also need to copy data from the head into the tail ? */ | 
|  | 523 | if (len > pglen) { | 
|  | 524 | offs = copy = len - pglen; | 
|  | 525 | if (copy > tail->iov_len) | 
|  | 526 | copy = tail->iov_len; | 
|  | 527 | memcpy(tail->iov_base, | 
|  | 528 | (char *)head->iov_base + | 
|  | 529 | head->iov_len - offs, | 
|  | 530 | copy); | 
|  | 531 | } | 
|  | 532 | } | 
|  | 533 | /* Now handle pages */ | 
|  | 534 | if (pglen != 0) { | 
|  | 535 | if (pglen > len) | 
|  | 536 | _shift_data_right_pages(buf->pages, | 
|  | 537 | buf->page_base + len, | 
|  | 538 | buf->page_base, | 
|  | 539 | pglen - len); | 
|  | 540 | copy = len; | 
|  | 541 | if (len > pglen) | 
|  | 542 | copy = pglen; | 
|  | 543 | _copy_to_pages(buf->pages, buf->page_base, | 
|  | 544 | (char *)head->iov_base + head->iov_len - len, | 
|  | 545 | copy); | 
|  | 546 | } | 
|  | 547 | head->iov_len -= len; | 
|  | 548 | buf->buflen -= len; | 
|  | 549 | /* Have we truncated the message? */ | 
|  | 550 | if (buf->len > buf->buflen) | 
|  | 551 | buf->len = buf->buflen; | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | /* | 
|  | 555 | * xdr_shrink_pagelen | 
|  | 556 | * @buf: xdr_buf | 
|  | 557 | * @len: bytes to remove from buf->pages | 
|  | 558 | * | 
|  | 559 | * Shrinks XDR buffer's page array buf->pages by | 
|  | 560 | * 'len' bytes. The extra data is not lost, but is instead | 
|  | 561 | * moved into the tail. | 
|  | 562 | */ | 
|  | 563 | static void | 
|  | 564 | xdr_shrink_pagelen(struct xdr_buf *buf, size_t len) | 
|  | 565 | { | 
|  | 566 | struct kvec *tail; | 
|  | 567 | size_t copy; | 
|  | 568 | char *p; | 
|  | 569 | unsigned int pglen = buf->page_len; | 
|  | 570 |  | 
|  | 571 | tail = buf->tail; | 
|  | 572 | BUG_ON (len > pglen); | 
|  | 573 |  | 
|  | 574 | /* Shift the tail first */ | 
|  | 575 | if (tail->iov_len != 0) { | 
|  | 576 | p = (char *)tail->iov_base + len; | 
|  | 577 | if (tail->iov_len > len) { | 
|  | 578 | copy = tail->iov_len - len; | 
|  | 579 | memmove(p, tail->iov_base, copy); | 
|  | 580 | } else | 
|  | 581 | buf->buflen -= len; | 
|  | 582 | /* Copy from the inlined pages into the tail */ | 
|  | 583 | copy = len; | 
|  | 584 | if (copy > tail->iov_len) | 
|  | 585 | copy = tail->iov_len; | 
|  | 586 | _copy_from_pages((char *)tail->iov_base, | 
|  | 587 | buf->pages, buf->page_base + pglen - len, | 
|  | 588 | copy); | 
|  | 589 | } | 
|  | 590 | buf->page_len -= len; | 
|  | 591 | buf->buflen -= len; | 
|  | 592 | /* Have we truncated the message? */ | 
|  | 593 | if (buf->len > buf->buflen) | 
|  | 594 | buf->len = buf->buflen; | 
|  | 595 | } | 
|  | 596 |  | 
|  | 597 | void | 
|  | 598 | xdr_shift_buf(struct xdr_buf *buf, size_t len) | 
|  | 599 | { | 
|  | 600 | xdr_shrink_bufhead(buf, len); | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | /** | 
|  | 604 | * xdr_init_encode - Initialize a struct xdr_stream for sending data. | 
|  | 605 | * @xdr: pointer to xdr_stream struct | 
|  | 606 | * @buf: pointer to XDR buffer in which to encode data | 
|  | 607 | * @p: current pointer inside XDR buffer | 
|  | 608 | * | 
|  | 609 | * Note: at the moment the RPC client only passes the length of our | 
|  | 610 | *	 scratch buffer in the xdr_buf's header kvec. Previously this | 
|  | 611 | *	 meant we needed to call xdr_adjust_iovec() after encoding the | 
|  | 612 | *	 data. With the new scheme, the xdr_stream manages the details | 
|  | 613 | *	 of the buffer length, and takes care of adjusting the kvec | 
|  | 614 | *	 length for us. | 
|  | 615 | */ | 
|  | 616 | void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p) | 
|  | 617 | { | 
|  | 618 | struct kvec *iov = buf->head; | 
| Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 619 | 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] | 620 |  | 
| Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 621 | BUG_ON(scratch_len < 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | xdr->buf = buf; | 
|  | 623 | xdr->iov = iov; | 
| Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 624 | xdr->p = (uint32_t *)((char *)iov->iov_base + iov->iov_len); | 
|  | 625 | xdr->end = (uint32_t *)((char *)iov->iov_base + scratch_len); | 
|  | 626 | BUG_ON(iov->iov_len > scratch_len); | 
|  | 627 |  | 
|  | 628 | if (p != xdr->p && p != NULL) { | 
|  | 629 | size_t len; | 
|  | 630 |  | 
|  | 631 | BUG_ON(p < xdr->p || p > xdr->end); | 
|  | 632 | len = (char *)p - (char *)xdr->p; | 
|  | 633 | xdr->p = p; | 
|  | 634 | buf->len += len; | 
|  | 635 | iov->iov_len += len; | 
|  | 636 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } | 
|  | 638 | EXPORT_SYMBOL(xdr_init_encode); | 
|  | 639 |  | 
|  | 640 | /** | 
|  | 641 | * xdr_reserve_space - Reserve buffer space for sending | 
|  | 642 | * @xdr: pointer to xdr_stream | 
|  | 643 | * @nbytes: number of bytes to reserve | 
|  | 644 | * | 
|  | 645 | * Checks that we have enough buffer space to encode 'nbytes' more | 
|  | 646 | * bytes of data. If so, update the total xdr_buf length, and | 
|  | 647 | * adjust the length of the current kvec. | 
|  | 648 | */ | 
|  | 649 | uint32_t * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes) | 
|  | 650 | { | 
|  | 651 | uint32_t *p = xdr->p; | 
|  | 652 | uint32_t *q; | 
|  | 653 |  | 
|  | 654 | /* align nbytes on the next 32-bit boundary */ | 
|  | 655 | nbytes += 3; | 
|  | 656 | nbytes &= ~3; | 
|  | 657 | q = p + (nbytes >> 2); | 
|  | 658 | if (unlikely(q > xdr->end || q < p)) | 
|  | 659 | return NULL; | 
|  | 660 | xdr->p = q; | 
|  | 661 | xdr->iov->iov_len += nbytes; | 
|  | 662 | xdr->buf->len += nbytes; | 
|  | 663 | return p; | 
|  | 664 | } | 
|  | 665 | EXPORT_SYMBOL(xdr_reserve_space); | 
|  | 666 |  | 
|  | 667 | /** | 
|  | 668 | * xdr_write_pages - Insert a list of pages into an XDR buffer for sending | 
|  | 669 | * @xdr: pointer to xdr_stream | 
|  | 670 | * @pages: list of pages | 
|  | 671 | * @base: offset of first byte | 
|  | 672 | * @len: length of data in bytes | 
|  | 673 | * | 
|  | 674 | */ | 
|  | 675 | void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base, | 
|  | 676 | unsigned int len) | 
|  | 677 | { | 
|  | 678 | struct xdr_buf *buf = xdr->buf; | 
|  | 679 | struct kvec *iov = buf->tail; | 
|  | 680 | buf->pages = pages; | 
|  | 681 | buf->page_base = base; | 
|  | 682 | buf->page_len = len; | 
|  | 683 |  | 
|  | 684 | iov->iov_base = (char *)xdr->p; | 
|  | 685 | iov->iov_len  = 0; | 
|  | 686 | xdr->iov = iov; | 
|  | 687 |  | 
|  | 688 | if (len & 3) { | 
|  | 689 | unsigned int pad = 4 - (len & 3); | 
|  | 690 |  | 
|  | 691 | BUG_ON(xdr->p >= xdr->end); | 
|  | 692 | iov->iov_base = (char *)xdr->p + (len & 3); | 
|  | 693 | iov->iov_len  += pad; | 
|  | 694 | len += pad; | 
|  | 695 | *xdr->p++ = 0; | 
|  | 696 | } | 
|  | 697 | buf->buflen += len; | 
|  | 698 | buf->len += len; | 
|  | 699 | } | 
|  | 700 | EXPORT_SYMBOL(xdr_write_pages); | 
|  | 701 |  | 
|  | 702 | /** | 
|  | 703 | * xdr_init_decode - Initialize an xdr_stream for decoding data. | 
|  | 704 | * @xdr: pointer to xdr_stream struct | 
|  | 705 | * @buf: pointer to XDR buffer from which to decode data | 
|  | 706 | * @p: current pointer inside XDR buffer | 
|  | 707 | */ | 
|  | 708 | void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p) | 
|  | 709 | { | 
|  | 710 | struct kvec *iov = buf->head; | 
|  | 711 | unsigned int len = iov->iov_len; | 
|  | 712 |  | 
|  | 713 | if (len > buf->len) | 
|  | 714 | len = buf->len; | 
|  | 715 | xdr->buf = buf; | 
|  | 716 | xdr->iov = iov; | 
|  | 717 | xdr->p = p; | 
|  | 718 | xdr->end = (uint32_t *)((char *)iov->iov_base + len); | 
|  | 719 | } | 
|  | 720 | EXPORT_SYMBOL(xdr_init_decode); | 
|  | 721 |  | 
|  | 722 | /** | 
|  | 723 | * xdr_inline_decode - Retrieve non-page XDR data to decode | 
|  | 724 | * @xdr: pointer to xdr_stream struct | 
|  | 725 | * @nbytes: number of bytes of data to decode | 
|  | 726 | * | 
|  | 727 | * Check if the input buffer is long enough to enable us to decode | 
|  | 728 | * 'nbytes' more bytes of data starting at the current position. | 
|  | 729 | * If so return the current pointer, then update the current | 
|  | 730 | * pointer position. | 
|  | 731 | */ | 
|  | 732 | uint32_t * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes) | 
|  | 733 | { | 
|  | 734 | uint32_t *p = xdr->p; | 
|  | 735 | uint32_t *q = p + XDR_QUADLEN(nbytes); | 
|  | 736 |  | 
|  | 737 | if (unlikely(q > xdr->end || q < p)) | 
|  | 738 | return NULL; | 
|  | 739 | xdr->p = q; | 
|  | 740 | return p; | 
|  | 741 | } | 
|  | 742 | EXPORT_SYMBOL(xdr_inline_decode); | 
|  | 743 |  | 
|  | 744 | /** | 
|  | 745 | * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position | 
|  | 746 | * @xdr: pointer to xdr_stream struct | 
|  | 747 | * @len: number of bytes of page data | 
|  | 748 | * | 
|  | 749 | * Moves data beyond the current pointer position from the XDR head[] buffer | 
|  | 750 | * into the page list. Any data that lies beyond current position + "len" | 
|  | 751 | * bytes is moved into the XDR tail[]. The current pointer is then | 
|  | 752 | * repositioned at the beginning of the XDR tail. | 
|  | 753 | */ | 
|  | 754 | void xdr_read_pages(struct xdr_stream *xdr, unsigned int len) | 
|  | 755 | { | 
|  | 756 | struct xdr_buf *buf = xdr->buf; | 
|  | 757 | struct kvec *iov; | 
|  | 758 | ssize_t shift; | 
|  | 759 | unsigned int end; | 
|  | 760 | int padding; | 
|  | 761 |  | 
|  | 762 | /* Realign pages to current pointer position */ | 
|  | 763 | iov  = buf->head; | 
|  | 764 | shift = iov->iov_len + (char *)iov->iov_base - (char *)xdr->p; | 
|  | 765 | if (shift > 0) | 
|  | 766 | xdr_shrink_bufhead(buf, shift); | 
|  | 767 |  | 
|  | 768 | /* Truncate page data and move it into the tail */ | 
|  | 769 | if (buf->page_len > len) | 
|  | 770 | xdr_shrink_pagelen(buf, buf->page_len - len); | 
|  | 771 | padding = (XDR_QUADLEN(len) << 2) - len; | 
|  | 772 | xdr->iov = iov = buf->tail; | 
|  | 773 | /* Compute remaining message length.  */ | 
|  | 774 | end = iov->iov_len; | 
|  | 775 | shift = buf->buflen - buf->len; | 
|  | 776 | if (shift < end) | 
|  | 777 | end -= shift; | 
|  | 778 | else if (shift > 0) | 
|  | 779 | end = 0; | 
|  | 780 | /* | 
|  | 781 | * Position current pointer at beginning of tail, and | 
|  | 782 | * set remaining message length. | 
|  | 783 | */ | 
|  | 784 | xdr->p = (uint32_t *)((char *)iov->iov_base + padding); | 
|  | 785 | xdr->end = (uint32_t *)((char *)iov->iov_base + end); | 
|  | 786 | } | 
|  | 787 | EXPORT_SYMBOL(xdr_read_pages); | 
|  | 788 |  | 
|  | 789 | static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0}; | 
|  | 790 |  | 
|  | 791 | void | 
|  | 792 | xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf) | 
|  | 793 | { | 
|  | 794 | buf->head[0] = *iov; | 
|  | 795 | buf->tail[0] = empty_iov; | 
|  | 796 | buf->page_len = 0; | 
|  | 797 | buf->buflen = buf->len = iov->iov_len; | 
|  | 798 | } | 
|  | 799 |  | 
|  | 800 | /* Sets subiov to the intersection of iov with the buffer of length len | 
|  | 801 | * starting base bytes after iov.  Indicates empty intersection by setting | 
|  | 802 | * length of subiov to zero.  Decrements len by length of subiov, sets base | 
|  | 803 | * to zero (or decrements it by length of iov if subiov is empty). */ | 
|  | 804 | static void | 
|  | 805 | iov_subsegment(struct kvec *iov, struct kvec *subiov, int *base, int *len) | 
|  | 806 | { | 
|  | 807 | if (*base > iov->iov_len) { | 
|  | 808 | subiov->iov_base = NULL; | 
|  | 809 | subiov->iov_len = 0; | 
|  | 810 | *base -= iov->iov_len; | 
|  | 811 | } else { | 
|  | 812 | subiov->iov_base = iov->iov_base + *base; | 
|  | 813 | subiov->iov_len = min(*len, (int)iov->iov_len - *base); | 
|  | 814 | *base = 0; | 
|  | 815 | } | 
|  | 816 | *len -= subiov->iov_len; | 
|  | 817 | } | 
|  | 818 |  | 
|  | 819 | /* Sets subbuf to the portion of buf of length len beginning base bytes | 
|  | 820 | * from the start of buf. Returns -1 if base of length are out of bounds. */ | 
|  | 821 | int | 
|  | 822 | xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf, | 
|  | 823 | int base, int len) | 
|  | 824 | { | 
|  | 825 | int i; | 
|  | 826 |  | 
|  | 827 | subbuf->buflen = subbuf->len = len; | 
|  | 828 | iov_subsegment(buf->head, subbuf->head, &base, &len); | 
|  | 829 |  | 
|  | 830 | if (base < buf->page_len) { | 
|  | 831 | i = (base + buf->page_base) >> PAGE_CACHE_SHIFT; | 
|  | 832 | subbuf->pages = &buf->pages[i]; | 
|  | 833 | subbuf->page_base = (base + buf->page_base) & ~PAGE_CACHE_MASK; | 
|  | 834 | subbuf->page_len = min((int)buf->page_len - base, len); | 
|  | 835 | len -= subbuf->page_len; | 
|  | 836 | base = 0; | 
|  | 837 | } else { | 
|  | 838 | base -= buf->page_len; | 
|  | 839 | subbuf->page_len = 0; | 
|  | 840 | } | 
|  | 841 |  | 
|  | 842 | iov_subsegment(buf->tail, subbuf->tail, &base, &len); | 
|  | 843 | if (base || len) | 
|  | 844 | return -1; | 
|  | 845 | return 0; | 
|  | 846 | } | 
|  | 847 |  | 
|  | 848 | /* obj is assumed to point to allocated memory of size at least len: */ | 
|  | 849 | int | 
|  | 850 | read_bytes_from_xdr_buf(struct xdr_buf *buf, int base, void *obj, int len) | 
|  | 851 | { | 
|  | 852 | struct xdr_buf subbuf; | 
|  | 853 | int this_len; | 
|  | 854 | int status; | 
|  | 855 |  | 
|  | 856 | status = xdr_buf_subsegment(buf, &subbuf, base, len); | 
|  | 857 | if (status) | 
|  | 858 | goto out; | 
|  | 859 | this_len = min(len, (int)subbuf.head[0].iov_len); | 
|  | 860 | memcpy(obj, subbuf.head[0].iov_base, this_len); | 
|  | 861 | len -= this_len; | 
|  | 862 | obj += this_len; | 
|  | 863 | this_len = min(len, (int)subbuf.page_len); | 
|  | 864 | if (this_len) | 
|  | 865 | _copy_from_pages(obj, subbuf.pages, subbuf.page_base, this_len); | 
|  | 866 | len -= this_len; | 
|  | 867 | obj += this_len; | 
|  | 868 | this_len = min(len, (int)subbuf.tail[0].iov_len); | 
|  | 869 | memcpy(obj, subbuf.tail[0].iov_base, this_len); | 
|  | 870 | out: | 
|  | 871 | return status; | 
|  | 872 | } | 
|  | 873 |  | 
|  | 874 | static int | 
|  | 875 | read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj) | 
|  | 876 | { | 
|  | 877 | u32	raw; | 
|  | 878 | int	status; | 
|  | 879 |  | 
|  | 880 | status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj)); | 
|  | 881 | if (status) | 
|  | 882 | return status; | 
|  | 883 | *obj = ntohl(raw); | 
|  | 884 | return 0; | 
|  | 885 | } | 
|  | 886 |  | 
|  | 887 | /* If the netobj starting offset bytes from the start of xdr_buf is contained | 
|  | 888 | * entirely in the head or the tail, set object to point to it; otherwise | 
|  | 889 | * try to find space for it at the end of the tail, copy it there, and | 
|  | 890 | * set obj to point to it. */ | 
|  | 891 | int | 
|  | 892 | xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, int offset) | 
|  | 893 | { | 
|  | 894 | u32	tail_offset = buf->head[0].iov_len + buf->page_len; | 
|  | 895 | u32	obj_end_offset; | 
|  | 896 |  | 
|  | 897 | if (read_u32_from_xdr_buf(buf, offset, &obj->len)) | 
|  | 898 | goto out; | 
|  | 899 | obj_end_offset = offset + 4 + obj->len; | 
|  | 900 |  | 
|  | 901 | if (obj_end_offset <= buf->head[0].iov_len) { | 
|  | 902 | /* The obj is contained entirely in the head: */ | 
|  | 903 | obj->data = buf->head[0].iov_base + offset + 4; | 
|  | 904 | } else if (offset + 4 >= tail_offset) { | 
|  | 905 | if (obj_end_offset - tail_offset | 
|  | 906 | > buf->tail[0].iov_len) | 
|  | 907 | goto out; | 
|  | 908 | /* The obj is contained entirely in the tail: */ | 
|  | 909 | obj->data = buf->tail[0].iov_base | 
|  | 910 | + offset - tail_offset + 4; | 
|  | 911 | } else { | 
|  | 912 | /* use end of tail as storage for obj: | 
|  | 913 | * (We don't copy to the beginning because then we'd have | 
|  | 914 | * to worry about doing a potentially overlapping copy. | 
|  | 915 | * This assumes the object is at most half the length of the | 
|  | 916 | * tail.) */ | 
|  | 917 | if (obj->len > buf->tail[0].iov_len) | 
|  | 918 | goto out; | 
|  | 919 | obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len - | 
|  | 920 | obj->len; | 
|  | 921 | if (read_bytes_from_xdr_buf(buf, offset + 4, | 
|  | 922 | obj->data, obj->len)) | 
|  | 923 | goto out; | 
|  | 924 |  | 
|  | 925 | } | 
|  | 926 | return 0; | 
|  | 927 | out: | 
|  | 928 | return -1; | 
|  | 929 | } |