xdr.c 38.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8
/*
 * linux/net/sunrpc/xdr.c
 *
 * Generic XDR support.
 *
 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
 */

9
#include <linux/module.h>
10
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
11 12 13 14 15 16 17 18 19 20 21
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/pagemap.h>
#include <linux/errno.h>
#include <linux/sunrpc/xdr.h>
#include <linux/sunrpc/msg_prot.h>

/*
 * XDR functions for basic NFS types
 */
22 23
__be32 *
xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj)
Linus Torvalds's avatar
Linus Torvalds committed
24 25 26 27
{
	unsigned int	quadlen = XDR_QUADLEN(obj->len);

	p[quadlen] = 0;		/* zero trailing bytes */
Benny Halevy's avatar
Benny Halevy committed
28
	*p++ = cpu_to_be32(obj->len);
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31
	memcpy(p, obj->data, obj->len);
	return p + XDR_QUADLEN(obj->len);
}
32
EXPORT_SYMBOL_GPL(xdr_encode_netobj);
Linus Torvalds's avatar
Linus Torvalds committed
33

34 35
__be32 *
xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj)
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38
{
	unsigned int	len;

Benny Halevy's avatar
Benny Halevy committed
39
	if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ)
Linus Torvalds's avatar
Linus Torvalds committed
40 41 42 43 44
		return NULL;
	obj->len  = len;
	obj->data = (u8 *) p;
	return p + XDR_QUADLEN(len);
}
45
EXPORT_SYMBOL_GPL(xdr_decode_netobj);
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48

/**
 * xdr_encode_opaque_fixed - Encode fixed length opaque data
49 50 51
 * @p: pointer to current position in XDR buffer.
 * @ptr: pointer to data to encode (or NULL)
 * @nbytes: size of data.
Linus Torvalds's avatar
Linus Torvalds committed
52 53 54 55 56 57 58 59 60
 *
 * Copy the array of data of length nbytes at ptr to the XDR buffer
 * at position p, then align to the next 32-bit boundary by padding
 * with zero bytes (see RFC1832).
 * Note: if ptr is NULL, only the padding is performed.
 *
 * Returns the updated current XDR buffer position
 *
 */
61
__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
Linus Torvalds's avatar
Linus Torvalds committed
62 63 64 65 66 67 68 69 70 71 72 73 74
{
	if (likely(nbytes != 0)) {
		unsigned int quadlen = XDR_QUADLEN(nbytes);
		unsigned int padding = (quadlen << 2) - nbytes;

		if (ptr != NULL)
			memcpy(p, ptr, nbytes);
		if (padding != 0)
			memset((char *)p + nbytes, 0, padding);
		p += quadlen;
	}
	return p;
}
75
EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
Linus Torvalds's avatar
Linus Torvalds committed
76 77 78

/**
 * xdr_encode_opaque - Encode variable length opaque data
79 80 81
 * @p: pointer to current position in XDR buffer.
 * @ptr: pointer to data to encode (or NULL)
 * @nbytes: size of data.
Linus Torvalds's avatar
Linus Torvalds committed
82 83 84
 *
 * Returns the updated current XDR buffer position
 */
85
__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
Linus Torvalds's avatar
Linus Torvalds committed
86
{
Benny Halevy's avatar
Benny Halevy committed
87
	*p++ = cpu_to_be32(nbytes);
Linus Torvalds's avatar
Linus Torvalds committed
88 89
	return xdr_encode_opaque_fixed(p, ptr, nbytes);
}
90
EXPORT_SYMBOL_GPL(xdr_encode_opaque);
Linus Torvalds's avatar
Linus Torvalds committed
91

92 93
__be32 *
xdr_encode_string(__be32 *p, const char *string)
Linus Torvalds's avatar
Linus Torvalds committed
94 95 96
{
	return xdr_encode_array(p, string, strlen(string));
}
97
EXPORT_SYMBOL_GPL(xdr_encode_string);
Linus Torvalds's avatar
Linus Torvalds committed
98

99
__be32 *
100 101
xdr_decode_string_inplace(__be32 *p, char **sp,
			  unsigned int *lenp, unsigned int maxlen)
Linus Torvalds's avatar
Linus Torvalds committed
102
{
103
	u32 len;
Linus Torvalds's avatar
Linus Torvalds committed
104

Benny Halevy's avatar
Benny Halevy committed
105
	len = be32_to_cpu(*p++);
106
	if (len > maxlen)
Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111
		return NULL;
	*lenp = len;
	*sp = (char *) p;
	return p + XDR_QUADLEN(len);
}
112
EXPORT_SYMBOL_GPL(xdr_decode_string_inplace);
Linus Torvalds's avatar
Linus Torvalds committed
113

114 115 116 117 118 119 120 121 122 123 124
/**
 * xdr_terminate_string - '\0'-terminate a string residing in an xdr_buf
 * @buf: XDR buffer where string resides
 * @len: length of string, in bytes
 *
 */
void
xdr_terminate_string(struct xdr_buf *buf, const u32 len)
{
	char *kaddr;

125
	kaddr = kmap_atomic(buf->pages[0]);
126
	kaddr[buf->page_base + len] = '\0';
127
	kunmap_atomic(kaddr);
128
}
129
EXPORT_SYMBOL_GPL(xdr_terminate_string);
130

Linus Torvalds's avatar
Linus Torvalds committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
void
xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
		 struct page **pages, unsigned int base, unsigned int len)
{
	struct kvec *head = xdr->head;
	struct kvec *tail = xdr->tail;
	char *buf = (char *)head->iov_base;
	unsigned int buflen = head->iov_len;

	head->iov_len  = offset;

	xdr->pages = pages;
	xdr->page_base = base;
	xdr->page_len = len;

	tail->iov_base = buf + offset;
	tail->iov_len = buflen - offset;

	xdr->buflen += len;
}
151
EXPORT_SYMBOL_GPL(xdr_inline_pages);
Linus Torvalds's avatar
Linus Torvalds committed
152 153 154

/*
 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
155 156 157
 */

/**
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160 161 162 163 164 165 166
 * _shift_data_right_pages
 * @pages: vector of pages containing both the source and dest memory area.
 * @pgto_base: page vector address of destination
 * @pgfrom_base: page vector address of source
 * @len: number of bytes to copy
 *
 * Note: the addresses pgto_base and pgfrom_base are both calculated in
 *       the same way:
 *            if a memory area starts at byte 'base' in page 'pages[i]',
167
 *            then its address is given as (i << PAGE_SHIFT) + base
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
 * Also note: pgfrom_base must be < pgto_base, but the memory areas
 * 	they point to may overlap.
 */
static void
_shift_data_right_pages(struct page **pages, size_t pgto_base,
		size_t pgfrom_base, size_t len)
{
	struct page **pgfrom, **pgto;
	char *vfrom, *vto;
	size_t copy;

	BUG_ON(pgto_base <= pgfrom_base);

	pgto_base += len;
	pgfrom_base += len;

184 185
	pgto = pages + (pgto_base >> PAGE_SHIFT);
	pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
Linus Torvalds's avatar
Linus Torvalds committed
186

187 188
	pgto_base &= ~PAGE_MASK;
	pgfrom_base &= ~PAGE_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
189 190 191 192

	do {
		/* Are any pointers crossing a page boundary? */
		if (pgto_base == 0) {
193
			pgto_base = PAGE_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
194 195 196
			pgto--;
		}
		if (pgfrom_base == 0) {
197
			pgfrom_base = PAGE_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201 202 203 204 205 206 207 208
			pgfrom--;
		}

		copy = len;
		if (copy > pgto_base)
			copy = pgto_base;
		if (copy > pgfrom_base)
			copy = pgfrom_base;
		pgto_base -= copy;
		pgfrom_base -= copy;

209
		vto = kmap_atomic(*pgto);
210 211 212 213 214 215
		if (*pgto != *pgfrom) {
			vfrom = kmap_atomic(*pgfrom);
			memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
			kunmap_atomic(vfrom);
		} else
			memmove(vto + pgto_base, vto + pgfrom_base, copy);
216
		flush_dcache_page(*pgto);
217
		kunmap_atomic(vto);
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220 221

	} while ((len -= copy) != 0);
}

222
/**
Linus Torvalds's avatar
Linus Torvalds committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
 * _copy_to_pages
 * @pages: array of pages
 * @pgbase: page vector address of destination
 * @p: pointer to source data
 * @len: length
 *
 * Copies data from an arbitrary memory location into an array of pages
 * The copy is assumed to be non-overlapping.
 */
static void
_copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
{
	struct page **pgto;
	char *vto;
	size_t copy;

239 240
	pgto = pages + (pgbase >> PAGE_SHIFT);
	pgbase &= ~PAGE_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
241

242
	for (;;) {
243
		copy = PAGE_SIZE - pgbase;
Linus Torvalds's avatar
Linus Torvalds committed
244 245 246
		if (copy > len)
			copy = len;

247
		vto = kmap_atomic(*pgto);
Linus Torvalds's avatar
Linus Torvalds committed
248
		memcpy(vto + pgbase, p, copy);
249
		kunmap_atomic(vto);
Linus Torvalds's avatar
Linus Torvalds committed
250

251 252 253 254
		len -= copy;
		if (len == 0)
			break;

Linus Torvalds's avatar
Linus Torvalds committed
255
		pgbase += copy;
256
		if (pgbase == PAGE_SIZE) {
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260 261
			flush_dcache_page(*pgto);
			pgbase = 0;
			pgto++;
		}
		p += copy;
262
	}
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265
	flush_dcache_page(*pgto);
}

266
/**
Linus Torvalds's avatar
Linus Torvalds committed
267 268 269 270 271 272 273 274 275
 * _copy_from_pages
 * @p: pointer to destination
 * @pages: array of pages
 * @pgbase: offset of source data
 * @len: length
 *
 * Copies data into an arbitrary memory location from an array of pages
 * The copy is assumed to be non-overlapping.
 */
276
void
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280 281 282
_copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
{
	struct page **pgfrom;
	char *vfrom;
	size_t copy;

283 284
	pgfrom = pages + (pgbase >> PAGE_SHIFT);
	pgbase &= ~PAGE_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
285 286

	do {
287
		copy = PAGE_SIZE - pgbase;
Linus Torvalds's avatar
Linus Torvalds committed
288 289 290
		if (copy > len)
			copy = len;

291
		vfrom = kmap_atomic(*pgfrom);
Linus Torvalds's avatar
Linus Torvalds committed
292
		memcpy(p, vfrom + pgbase, copy);
293
		kunmap_atomic(vfrom);
Linus Torvalds's avatar
Linus Torvalds committed
294 295

		pgbase += copy;
296
		if (pgbase == PAGE_SIZE) {
Linus Torvalds's avatar
Linus Torvalds committed
297 298 299 300 301 302 303
			pgbase = 0;
			pgfrom++;
		}
		p += copy;

	} while ((len -= copy) != 0);
}
304
EXPORT_SYMBOL_GPL(_copy_from_pages);
Linus Torvalds's avatar
Linus Torvalds committed
305

306
/**
Linus Torvalds's avatar
Linus Torvalds committed
307 308 309 310
 * xdr_shrink_bufhead
 * @buf: xdr_buf
 * @len: bytes to remove from buf->head[0]
 *
311
 * Shrinks XDR buffer's header kvec buf->head[0] by
Linus Torvalds's avatar
Linus Torvalds committed
312 313 314 315 316 317 318 319 320 321 322 323
 * 'len' bytes. The extra data is not lost, but is instead
 * moved into the inlined pages and/or the tail.
 */
static void
xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
{
	struct kvec *head, *tail;
	size_t copy, offs;
	unsigned int pglen = buf->page_len;

	tail = buf->tail;
	head = buf->head;
324 325 326 327

	WARN_ON_ONCE(len > head->iov_len);
	if (len > head->iov_len)
		len = head->iov_len;
Linus Torvalds's avatar
Linus Torvalds committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

	/* Shift the tail first */
	if (tail->iov_len != 0) {
		if (tail->iov_len > len) {
			copy = tail->iov_len - len;
			memmove((char *)tail->iov_base + len,
					tail->iov_base, copy);
		}
		/* Copy from the inlined pages into the tail */
		copy = len;
		if (copy > pglen)
			copy = pglen;
		offs = len - copy;
		if (offs >= tail->iov_len)
			copy = 0;
		else if (copy > tail->iov_len - offs)
			copy = tail->iov_len - offs;
		if (copy != 0)
			_copy_from_pages((char *)tail->iov_base + offs,
					buf->pages,
					buf->page_base + pglen + offs - len,
					copy);
		/* Do we also need to copy data from the head into the tail ? */
		if (len > pglen) {
			offs = copy = len - pglen;
			if (copy > tail->iov_len)
				copy = tail->iov_len;
			memcpy(tail->iov_base,
					(char *)head->iov_base +
					head->iov_len - offs,
					copy);
		}
	}
	/* Now handle pages */
	if (pglen != 0) {
		if (pglen > len)
			_shift_data_right_pages(buf->pages,
					buf->page_base + len,
					buf->page_base,
					pglen - len);
		copy = len;
		if (len > pglen)
			copy = pglen;
		_copy_to_pages(buf->pages, buf->page_base,
				(char *)head->iov_base + head->iov_len - len,
				copy);
	}
	head->iov_len -= len;
	buf->buflen -= len;
	/* Have we truncated the message? */
	if (buf->len > buf->buflen)
		buf->len = buf->buflen;
}

382
/**
Linus Torvalds's avatar
Linus Torvalds committed
383 384 385 386
 * xdr_shrink_pagelen
 * @buf: xdr_buf
 * @len: bytes to remove from buf->pages
 *
387
 * Shrinks XDR buffer's page array buf->pages by
Linus Torvalds's avatar
Linus Torvalds committed
388 389 390 391 392 393 394 395 396
 * 'len' bytes. The extra data is not lost, but is instead
 * moved into the tail.
 */
static void
xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
{
	struct kvec *tail;
	size_t copy;
	unsigned int pglen = buf->page_len;
397
	unsigned int tailbuf_len;
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400 401

	tail = buf->tail;
	BUG_ON (len > pglen);

402 403
	tailbuf_len = buf->buflen - buf->head->iov_len - buf->page_len;

Linus Torvalds's avatar
Linus Torvalds committed
404
	/* Shift the tail first */
405 406 407 408 409 410 411
	if (tailbuf_len != 0) {
		unsigned int free_space = tailbuf_len - tail->iov_len;

		if (len < free_space)
			free_space = len;
		tail->iov_len += free_space;

412
		copy = len;
Linus Torvalds's avatar
Linus Torvalds committed
413
		if (tail->iov_len > len) {
414
			char *p = (char *)tail->iov_base + len;
415
			memmove(p, tail->iov_base, tail->iov_len - len);
416
		} else
Linus Torvalds's avatar
Linus Torvalds committed
417
			copy = tail->iov_len;
418
		/* Copy from the inlined pages into the tail */
Linus Torvalds's avatar
Linus Torvalds committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
		_copy_from_pages((char *)tail->iov_base,
				buf->pages, buf->page_base + pglen - len,
				copy);
	}
	buf->page_len -= len;
	buf->buflen -= len;
	/* Have we truncated the message? */
	if (buf->len > buf->buflen)
		buf->len = buf->buflen;
}

void
xdr_shift_buf(struct xdr_buf *buf, size_t len)
{
	xdr_shrink_bufhead(buf, len);
}
435
EXPORT_SYMBOL_GPL(xdr_shift_buf);
Linus Torvalds's avatar
Linus Torvalds committed
436

437 438 439 440 441 442 443 444 445 446
/**
 * xdr_stream_pos - Return the current offset from the start of the xdr_stream
 * @xdr: pointer to struct xdr_stream
 */
unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
{
	return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
}
EXPORT_SYMBOL_GPL(xdr_stream_pos);

Linus Torvalds's avatar
Linus Torvalds committed
447 448 449 450 451 452 453 454 455 456 457 458 459
/**
 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
 * @xdr: pointer to xdr_stream struct
 * @buf: pointer to XDR buffer in which to encode data
 * @p: current pointer inside XDR buffer
 *
 * Note: at the moment the RPC client only passes the length of our
 *	 scratch buffer in the xdr_buf's header kvec. Previously this
 *	 meant we needed to call xdr_adjust_iovec() after encoding the
 *	 data. With the new scheme, the xdr_stream manages the details
 *	 of the buffer length, and takes care of adjusting the kvec
 *	 length for us.
 */
460
void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
Linus Torvalds's avatar
Linus Torvalds committed
461 462
{
	struct kvec *iov = buf->head;
463
	int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
Linus Torvalds's avatar
Linus Torvalds committed
464

465
	xdr_set_scratch_buffer(xdr, NULL, 0);
466
	BUG_ON(scratch_len < 0);
Linus Torvalds's avatar
Linus Torvalds committed
467 468
	xdr->buf = buf;
	xdr->iov = iov;
469 470
	xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
	xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
471 472 473 474 475 476 477 478 479 480 481
	BUG_ON(iov->iov_len > scratch_len);

	if (p != xdr->p && p != NULL) {
		size_t len;

		BUG_ON(p < xdr->p || p > xdr->end);
		len = (char *)p - (char *)xdr->p;
		xdr->p = p;
		buf->len += len;
		iov->iov_len += len;
	}
Linus Torvalds's avatar
Linus Torvalds committed
482
}
483
EXPORT_SYMBOL_GPL(xdr_init_encode);
Linus Torvalds's avatar
Linus Torvalds committed
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
/**
 * xdr_commit_encode - Ensure all data is written to buffer
 * @xdr: pointer to xdr_stream
 *
 * We handle encoding across page boundaries by giving the caller a
 * temporary location to write to, then later copying the data into
 * place; xdr_commit_encode does that copying.
 *
 * Normally the caller doesn't need to call this directly, as the
 * following xdr_reserve_space will do it.  But an explicit call may be
 * required at the end of encoding, or any other time when the xdr_buf
 * data might be read.
 */
void xdr_commit_encode(struct xdr_stream *xdr)
{
	int shift = xdr->scratch.iov_len;
	void *page;

	if (shift == 0)
		return;
	page = page_address(*xdr->page_ptr);
	memcpy(xdr->scratch.iov_base, page, shift);
	memmove(page, page + shift, (void *)xdr->p - page);
	xdr->scratch.iov_len = 0;
}
EXPORT_SYMBOL_GPL(xdr_commit_encode);

512 513
static __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
		size_t nbytes)
514 515 516 517 518 519 520 521 522 523 524 525 526
{
	static __be32 *p;
	int space_left;
	int frag1bytes, frag2bytes;

	if (nbytes > PAGE_SIZE)
		return NULL; /* Bigger buffers require special handling */
	if (xdr->buf->len + nbytes > xdr->buf->buflen)
		return NULL; /* Sorry, we're totally out of space */
	frag1bytes = (xdr->end - xdr->p) << 2;
	frag2bytes = nbytes - frag1bytes;
	if (xdr->iov)
		xdr->iov->iov_len += frag1bytes;
527
	else
528
		xdr->buf->page_len += frag1bytes;
529
	xdr->page_ptr++;
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	xdr->iov = NULL;
	/*
	 * If the last encode didn't end exactly on a page boundary, the
	 * next one will straddle boundaries.  Encode into the next
	 * page, then copy it back later in xdr_commit_encode.  We use
	 * the "scratch" iov to track any temporarily unused fragment of
	 * space at the end of the previous buffer:
	 */
	xdr->scratch.iov_base = xdr->p;
	xdr->scratch.iov_len = frag1bytes;
	p = page_address(*xdr->page_ptr);
	/*
	 * Note this is where the next encode will start after we've
	 * shifted this one back:
	 */
	xdr->p = (void *)p + frag2bytes;
	space_left = xdr->buf->buflen - xdr->buf->len;
	xdr->end = (void *)p + min_t(int, space_left, PAGE_SIZE);
	xdr->buf->page_len += frag2bytes;
	xdr->buf->len += nbytes;
	return p;
}

Linus Torvalds's avatar
Linus Torvalds committed
553 554 555 556 557 558 559 560 561
/**
 * xdr_reserve_space - Reserve buffer space for sending
 * @xdr: pointer to xdr_stream
 * @nbytes: number of bytes to reserve
 *
 * Checks that we have enough buffer space to encode 'nbytes' more
 * bytes of data. If so, update the total xdr_buf length, and
 * adjust the length of the current kvec.
 */
562
__be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds's avatar
Linus Torvalds committed
563
{
564 565
	__be32 *p = xdr->p;
	__be32 *q;
Linus Torvalds's avatar
Linus Torvalds committed
566

567
	xdr_commit_encode(xdr);
Linus Torvalds's avatar
Linus Torvalds committed
568 569 570 571 572
	/* align nbytes on the next 32-bit boundary */
	nbytes += 3;
	nbytes &= ~3;
	q = p + (nbytes >> 2);
	if (unlikely(q > xdr->end || q < p))
573
		return xdr_get_next_encode_buffer(xdr, nbytes);
Linus Torvalds's avatar
Linus Torvalds committed
574
	xdr->p = q;
575 576 577 578
	if (xdr->iov)
		xdr->iov->iov_len += nbytes;
	else
		xdr->buf->page_len += nbytes;
Linus Torvalds's avatar
Linus Torvalds committed
579 580 581
	xdr->buf->len += nbytes;
	return p;
}
582
EXPORT_SYMBOL_GPL(xdr_reserve_space);
Linus Torvalds's avatar
Linus Torvalds committed
583

J. Bruce Fields's avatar
J. Bruce Fields committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
/**
 * xdr_truncate_encode - truncate an encode buffer
 * @xdr: pointer to xdr_stream
 * @len: new length of buffer
 *
 * Truncates the xdr stream, so that xdr->buf->len == len,
 * and xdr->p points at offset len from the start of the buffer, and
 * head, tail, and page lengths are adjusted to correspond.
 *
 * If this means moving xdr->p to a different buffer, we assume that
 * that the end pointer should be set to the end of the current page,
 * except in the case of the head buffer when we assume the head
 * buffer's current length represents the end of the available buffer.
 *
 * This is *not* safe to use on a buffer that already has inlined page
 * cache pages (as in a zero-copy server read reply), except for the
 * simple case of truncating from one position in the tail to another.
 *
 */
void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
{
	struct xdr_buf *buf = xdr->buf;
	struct kvec *head = buf->head;
	struct kvec *tail = buf->tail;
	int fraglen;
609
	int new;
J. Bruce Fields's avatar
J. Bruce Fields committed
610 611 612 613 614

	if (len > buf->len) {
		WARN_ON_ONCE(1);
		return;
	}
615
	xdr_commit_encode(xdr);
J. Bruce Fields's avatar
J. Bruce Fields committed
616 617 618 619

	fraglen = min_t(int, buf->len - len, tail->iov_len);
	tail->iov_len -= fraglen;
	buf->len -= fraglen;
620
	if (tail->iov_len) {
J. Bruce Fields's avatar
J. Bruce Fields committed
621
		xdr->p = tail->iov_base + tail->iov_len;
622 623
		WARN_ON_ONCE(!xdr->end);
		WARN_ON_ONCE(!xdr->iov);
J. Bruce Fields's avatar
J. Bruce Fields committed
624 625 626 627 628 629 630 631
		return;
	}
	WARN_ON_ONCE(fraglen);
	fraglen = min_t(int, buf->len - len, buf->page_len);
	buf->page_len -= fraglen;
	buf->len -= fraglen;

	new = buf->page_base + buf->page_len;
632 633

	xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
J. Bruce Fields's avatar
J. Bruce Fields committed
634

635
	if (buf->page_len) {
J. Bruce Fields's avatar
J. Bruce Fields committed
636 637 638
		xdr->p = page_address(*xdr->page_ptr);
		xdr->end = (void *)xdr->p + PAGE_SIZE;
		xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
639
		WARN_ON_ONCE(xdr->iov);
J. Bruce Fields's avatar
J. Bruce Fields committed
640 641
		return;
	}
642
	if (fraglen) {
J. Bruce Fields's avatar
J. Bruce Fields committed
643
		xdr->end = head->iov_base + head->iov_len;
644 645
		xdr->page_ptr--;
	}
J. Bruce Fields's avatar
J. Bruce Fields committed
646 647 648 649 650 651 652 653
	/* (otherwise assume xdr->end is already set) */
	head->iov_len = len;
	buf->len = len;
	xdr->p = head->iov_base + head->iov_len;
	xdr->iov = buf->head;
}
EXPORT_SYMBOL(xdr_truncate_encode);

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
/**
 * xdr_restrict_buflen - decrease available buffer space
 * @xdr: pointer to xdr_stream
 * @newbuflen: new maximum number of bytes available
 *
 * Adjust our idea of how much space is available in the buffer.
 * If we've already used too much space in the buffer, returns -1.
 * If the available space is already smaller than newbuflen, returns 0
 * and does nothing.  Otherwise, adjusts xdr->buf->buflen to newbuflen
 * and ensures xdr->end is set at most offset newbuflen from the start
 * of the buffer.
 */
int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
{
	struct xdr_buf *buf = xdr->buf;
	int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
	int end_offset = buf->len + left_in_this_buf;

	if (newbuflen < 0 || newbuflen < buf->len)
		return -1;
	if (newbuflen > buf->buflen)
		return 0;
	if (newbuflen < end_offset)
		xdr->end = (void *)xdr->end + newbuflen - end_offset;
	buf->buflen = newbuflen;
	return 0;
}
EXPORT_SYMBOL(xdr_restrict_buflen);

Linus Torvalds's avatar
Linus Torvalds committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
/**
 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
 * @xdr: pointer to xdr_stream
 * @pages: list of pages
 * @base: offset of first byte
 * @len: length of data in bytes
 *
 */
void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
		 unsigned int len)
{
	struct xdr_buf *buf = xdr->buf;
	struct kvec *iov = buf->tail;
	buf->pages = pages;
	buf->page_base = base;
	buf->page_len = len;

	iov->iov_base = (char *)xdr->p;
	iov->iov_len  = 0;
	xdr->iov = iov;

	if (len & 3) {
		unsigned int pad = 4 - (len & 3);

		BUG_ON(xdr->p >= xdr->end);
		iov->iov_base = (char *)xdr->p + (len & 3);
		iov->iov_len  += pad;
		len += pad;
		*xdr->p++ = 0;
	}
	buf->buflen += len;
	buf->len += len;
}
716
EXPORT_SYMBOL_GPL(xdr_write_pages);
Linus Torvalds's avatar
Linus Torvalds committed
717

718
static void xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
719
		unsigned int len)
720 721 722
{
	if (len > iov->iov_len)
		len = iov->iov_len;
723
	xdr->p = (__be32*)iov->iov_base;
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	xdr->end = (__be32*)(iov->iov_base + len);
	xdr->iov = iov;
	xdr->page_ptr = NULL;
}

static int xdr_set_page_base(struct xdr_stream *xdr,
		unsigned int base, unsigned int len)
{
	unsigned int pgnr;
	unsigned int maxlen;
	unsigned int pgoff;
	unsigned int pgend;
	void *kaddr;

	maxlen = xdr->buf->page_len;
	if (base >= maxlen)
		return -EINVAL;
	maxlen -= base;
	if (len > maxlen)
		len = maxlen;

	base += xdr->buf->page_base;

	pgnr = base >> PAGE_SHIFT;
	xdr->page_ptr = &xdr->buf->pages[pgnr];
	kaddr = page_address(*xdr->page_ptr);

	pgoff = base & ~PAGE_MASK;
	xdr->p = (__be32*)(kaddr + pgoff);

	pgend = pgoff + len;
	if (pgend > PAGE_SIZE)
		pgend = PAGE_SIZE;
	xdr->end = (__be32*)(kaddr + pgend);
	xdr->iov = NULL;
	return 0;
}

static void xdr_set_next_page(struct xdr_stream *xdr)
{
	unsigned int newbase;

	newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
	newbase -= xdr->buf->page_base;

	if (xdr_set_page_base(xdr, newbase, PAGE_SIZE) < 0)
770
		xdr_set_iov(xdr, xdr->buf->tail, xdr->nwords << 2);
771 772 773 774 775 776 777 778
}

static bool xdr_set_next_buffer(struct xdr_stream *xdr)
{
	if (xdr->page_ptr != NULL)
		xdr_set_next_page(xdr);
	else if (xdr->iov == xdr->buf->head) {
		if (xdr_set_page_base(xdr, 0, PAGE_SIZE) < 0)
779
			xdr_set_iov(xdr, xdr->buf->tail, xdr->nwords << 2);
780 781 782 783
	}
	return xdr->p != xdr->end;
}

Linus Torvalds's avatar
Linus Torvalds committed
784 785 786 787 788 789
/**
 * xdr_init_decode - Initialize an xdr_stream for decoding data.
 * @xdr: pointer to xdr_stream struct
 * @buf: pointer to XDR buffer from which to decode data
 * @p: current pointer inside XDR buffer
 */
790
void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
Linus Torvalds's avatar
Linus Torvalds committed
791 792
{
	xdr->buf = buf;
793 794
	xdr->scratch.iov_base = NULL;
	xdr->scratch.iov_len = 0;
795
	xdr->nwords = XDR_QUADLEN(buf->len);
796
	if (buf->head[0].iov_len != 0)
797
		xdr_set_iov(xdr, buf->head, buf->len);
798 799
	else if (buf->page_len != 0)
		xdr_set_page_base(xdr, 0, buf->len);
800 801
	else
		xdr_set_iov(xdr, buf->head, buf->len);
802 803
	if (p != NULL && p > xdr->p && xdr->end >= p) {
		xdr->nwords -= p - xdr->p;
804
		xdr->p = p;
805
	}
Linus Torvalds's avatar
Linus Torvalds committed
806
}
807
EXPORT_SYMBOL_GPL(xdr_init_decode);
Linus Torvalds's avatar
Linus Torvalds committed
808

809
/**
810
 * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
 * @xdr: pointer to xdr_stream struct
 * @buf: pointer to XDR buffer from which to decode data
 * @pages: list of pages to decode into
 * @len: length in bytes of buffer in pages
 */
void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
			   struct page **pages, unsigned int len)
{
	memset(buf, 0, sizeof(*buf));
	buf->pages =  pages;
	buf->page_len =  len;
	buf->buflen =  len;
	buf->len = len;
	xdr_init_decode(xdr, buf, NULL);
}
EXPORT_SYMBOL_GPL(xdr_init_decode_pages);

828
static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
829
{
830
	unsigned int nwords = XDR_QUADLEN(nbytes);
831
	__be32 *p = xdr->p;
832
	__be32 *q = p + nwords;
833

834
	if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
835
		return NULL;
836
	xdr->p = q;
837
	xdr->nwords -= nwords;
838 839 840
	return p;
}

Linus Torvalds's avatar
Linus Torvalds committed
841
/**
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
 * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
 * @xdr: pointer to xdr_stream struct
 * @buf: pointer to an empty buffer
 * @buflen: size of 'buf'
 *
 * The scratch buffer is used when decoding from an array of pages.
 * If an xdr_inline_decode() call spans across page boundaries, then
 * we copy the data into the scratch buffer in order to allow linear
 * access.
 */
void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
{
	xdr->scratch.iov_base = buf;
	xdr->scratch.iov_len = buflen;
}
EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer);

static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
{
	__be32 *p;
862
	char *cpdest = xdr->scratch.iov_base;
863 864 865 866
	size_t cplen = (char *)xdr->end - (char *)xdr->p;

	if (nbytes > xdr->scratch.iov_len)
		return NULL;
867 868 869 870
	p = __xdr_inline_decode(xdr, cplen);
	if (p == NULL)
		return NULL;
	memcpy(cpdest, p, cplen);
871 872 873 874 875 876 877 878 879 880 881 882 883
	cpdest += cplen;
	nbytes -= cplen;
	if (!xdr_set_next_buffer(xdr))
		return NULL;
	p = __xdr_inline_decode(xdr, nbytes);
	if (p == NULL)
		return NULL;
	memcpy(cpdest, p, nbytes);
	return xdr->scratch.iov_base;
}

/**
 * xdr_inline_decode - Retrieve XDR data to decode
Linus Torvalds's avatar
Linus Torvalds committed
884 885 886 887 888 889 890 891
 * @xdr: pointer to xdr_stream struct
 * @nbytes: number of bytes of data to decode
 *
 * Check if the input buffer is long enough to enable us to decode
 * 'nbytes' more bytes of data starting at the current position.
 * If so return the current pointer, then update the current
 * pointer position.
 */
892
__be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds's avatar
Linus Torvalds committed
893
{
894
	__be32 *p;
Linus Torvalds's avatar
Linus Torvalds committed
895

896 897 898
	if (nbytes == 0)
		return xdr->p;
	if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
Linus Torvalds's avatar
Linus Torvalds committed
899
		return NULL;
900 901 902 903
	p = __xdr_inline_decode(xdr, nbytes);
	if (p != NULL)
		return p;
	return xdr_copy_to_scratch(xdr, nbytes);
Linus Torvalds's avatar
Linus Torvalds committed
904
}
905
EXPORT_SYMBOL_GPL(xdr_inline_decode);
Linus Torvalds's avatar
Linus Torvalds committed
906

907
static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
Linus Torvalds's avatar
Linus Torvalds committed
908 909 910
{
	struct xdr_buf *buf = xdr->buf;
	struct kvec *iov;
911
	unsigned int nwords = XDR_QUADLEN(len);
912
	unsigned int cur = xdr_stream_pos(xdr);
Linus Torvalds's avatar
Linus Torvalds committed
913

914
	if (xdr->nwords == 0)
915
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
916 917
	/* Realign pages to current pointer position */
	iov  = buf->head;
918
	if (iov->iov_len > cur) {
919
		xdr_shrink_bufhead(buf, iov->iov_len - cur);
920 921
		xdr->nwords = XDR_QUADLEN(buf->len - cur);
	}
Linus Torvalds's avatar
Linus Torvalds committed
922

923 924 925 926 927
	if (nwords > xdr->nwords) {
		nwords = xdr->nwords;
		len = nwords << 2;
	}
	if (buf->page_len <= len)
928
		len = buf->page_len;
929 930 931 932 933
	else if (nwords < xdr->nwords) {
		/* Truncate page data and move it into the tail */
		xdr_shrink_pagelen(buf, buf->page_len - len);
		xdr->nwords = XDR_QUADLEN(buf->len - cur);
	}
934 935
	return len;
}
936

Linus Torvalds's avatar
Linus Torvalds committed
937 938 939 940 941 942 943
/**
 * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
 * @xdr: pointer to xdr_stream struct
 * @len: number of bytes of page data
 *
 * Moves data beyond the current pointer position from the XDR head[] buffer
 * into the page list. Any data that lies beyond current position + "len"
944
 * bytes is moved into the XDR tail[].
945 946
 *
 * Returns the number of XDR encoded bytes now contained in the pages
Linus Torvalds's avatar
Linus Torvalds committed
947
 */
948
unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
Linus Torvalds's avatar
Linus Torvalds committed
949 950 951
{
	struct xdr_buf *buf = xdr->buf;
	struct kvec *iov;
952
	unsigned int nwords;
Linus Torvalds's avatar
Linus Torvalds committed
953
	unsigned int end;
954
	unsigned int padding;
Linus Torvalds's avatar
Linus Torvalds committed
955

956 957 958 959
	len = xdr_align_pages(xdr, len);
	if (len == 0)
		return 0;
	nwords = XDR_QUADLEN(len);
960
	padding = (nwords << 2) - len;
Linus Torvalds's avatar
Linus Torvalds committed
961 962
	xdr->iov = iov = buf->tail;
	/* Compute remaining message length.  */
963 964 965 966
	end = ((xdr->nwords - nwords) << 2) + padding;
	if (end > iov->iov_len)
		end = iov->iov_len;

Linus Torvalds's avatar
Linus Torvalds committed
967 968 969 970
	/*
	 * Position current pointer at beginning of tail, and
	 * set remaining message length.
	 */
971 972
	xdr->p = (__be32 *)((char *)iov->iov_base + padding);
	xdr->end = (__be32 *)((char *)iov->iov_base + end);
973
	xdr->page_ptr = NULL;
974
	xdr->nwords = XDR_QUADLEN(end - padding);
975
	return len;
Linus Torvalds's avatar
Linus Torvalds committed
976
}
977
EXPORT_SYMBOL_GPL(xdr_read_pages);
Linus Torvalds's avatar
Linus Torvalds committed
978

979 980 981 982 983 984 985 986 987 988 989 990
/**
 * xdr_enter_page - decode data from the XDR page
 * @xdr: pointer to xdr_stream struct
 * @len: number of bytes of page data
 *
 * Moves data beyond the current pointer position from the XDR head[] buffer
 * into the page list. Any data that lies beyond current position + "len"
 * bytes is moved into the XDR tail[]. The current pointer is then
 * repositioned at the beginning of the first XDR page.
 */
void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
{
991
	len = xdr_align_pages(xdr, len);
992 993 994 995
	/*
	 * Position current pointer at beginning of tail, and
	 * set remaining message length.
	 */
996 997
	if (len != 0)
		xdr_set_page_base(xdr, 0, len);
998
}
999
EXPORT_SYMBOL_GPL(xdr_enter_page);
1000

Linus Torvalds's avatar
Linus Torvalds committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};

void
xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
{
	buf->head[0] = *iov;
	buf->tail[0] = empty_iov;
	buf->page_len = 0;
	buf->buflen = buf->len = iov->iov_len;
}
1011
EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
Linus Torvalds's avatar
Linus Torvalds committed
1012

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
/**
 * xdr_buf_subsegment - set subbuf to a portion of buf
 * @buf: an xdr buffer
 * @subbuf: the result buffer
 * @base: beginning of range in bytes
 * @len: length of range in bytes
 *
 * sets @subbuf to an xdr buffer representing the portion of @buf of
 * length @len starting at offset @base.
 *
 * @buf and @subbuf may be pointers to the same struct xdr_buf.
 *
 * Returns -1 if base of length are out of bounds.
 */
Linus Torvalds's avatar
Linus Torvalds committed
1027 1028
int
xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
1029
			unsigned int base, unsigned int len)
Linus Torvalds's avatar
Linus Torvalds committed
1030 1031
{
	subbuf->buflen = subbuf->len = len;
1032 1033 1034 1035 1036 1037 1038 1039
	if (base < buf->head[0].iov_len) {
		subbuf->head[0].iov_base = buf->head[0].iov_base + base;
		subbuf->head[0].iov_len = min_t(unsigned int, len,
						buf->head[0].iov_len - base);
		len -= subbuf->head[0].iov_len;
		base = 0;
	} else {
		base -= buf->head[0].iov_len;
1040
		subbuf->head[0].iov_len = 0;
1041
	}
Linus Torvalds's avatar
Linus Torvalds committed
1042 1043

	if (base < buf->page_len) {
1044 1045
		subbuf->page_len = min(buf->page_len - base, len);
		base += buf->page_base;
1046 1047
		subbuf->page_base = base & ~PAGE_MASK;
		subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
Linus Torvalds's avatar
Linus Torvalds committed
1048 1049 1050 1051 1052 1053 1054
		len -= subbuf->page_len;
		base = 0;
	} else {
		base -= buf->page_len;
		subbuf->page_len = 0;
	}

1055 1056 1057 1058 1059 1060 1061 1062
	if (base < buf->tail[0].iov_len) {
		subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
		subbuf->tail[0].iov_len = min_t(unsigned int, len,
						buf->tail[0].iov_len - base);
		len -= subbuf->tail[0].iov_len;
		base = 0;
	} else {
		base -= buf->tail[0].iov_len;
1063
		subbuf->tail[0].iov_len = 0;
1064 1065
	}

Linus Torvalds's avatar
Linus Torvalds committed
1066 1067 1068 1069
	if (base || len)
		return -1;
	return 0;
}
1070
EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
Linus Torvalds's avatar
Linus Torvalds committed
1071

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
/**
 * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
 * @buf: buf to be trimmed
 * @len: number of bytes to reduce "buf" by
 *
 * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
 * that it's possible that we'll trim less than that amount if the xdr_buf is
 * too small, or if (for instance) it's all in the head and the parser has
 * already read too far into it.
 */
void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
{
	size_t cur;
	unsigned int trim = len;

	if (buf->tail[0].iov_len) {
		cur = min_t(size_t, buf->tail[0].iov_len, trim);
		buf->tail[0].iov_len -= cur;
		trim -= cur;
		if (!trim)
			goto fix_len;
	}

	if (buf->page_len) {
		cur = min_t(unsigned int, buf->page_len, trim);
		buf->page_len -= cur;
		trim -= cur;
		if (!trim)
			goto fix_len;
	}

	if (buf->head[0].iov_len) {
		cur = min_t(size_t, buf->head[0].iov_len, trim);
		buf->head[0].iov_len -= cur;
		trim -= cur;
	}
fix_len:
	buf->len -= (len - trim);
}
EXPORT_SYMBOL_GPL(xdr_buf_trim);

1113
static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
Linus Torvalds's avatar
Linus Torvalds committed
1114
{
1115
	unsigned int this_len;
Linus Torvalds's avatar
Linus Torvalds committed
1116

1117 1118
	this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
	memcpy(obj, subbuf->head[0].iov_base, this_len);
Linus Torvalds's avatar
Linus Torvalds committed
1119 1120
	len -= this_len;
	obj += this_len;
1121
	this_len = min_t(unsigned int, len, subbuf->page_len);
Linus Torvalds's avatar
Linus Torvalds committed
1122
	if (this_len)
1123
		_copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
Linus Torvalds's avatar
Linus Torvalds committed
1124 1125
	len -= this_len;
	obj += this_len;
1126 1127
	this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
	memcpy(obj, subbuf->tail[0].iov_base, this_len);
Linus Torvalds's avatar
Linus Torvalds committed
1128 1129
}

1130
/* obj is assumed to point to allocated memory of size at least len: */
1131
int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
1132 1133 1134 1135 1136
{
	struct xdr_buf subbuf;
	int status;

	status = xdr_buf_subsegment(buf, &subbuf, base, len);
1137 1138 1139 1140 1141
	if (status != 0)
		return status;
	__read_bytes_from_xdr_buf(&subbuf, obj, len);
	return 0;
}
1142
EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
1143 1144 1145 1146 1147 1148 1149

static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
{
	unsigned int this_len;

	this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
	memcpy(subbuf->head[0].iov_base, obj, this_len);
1150 1151
	len -= this_len;
	obj += this_len;
1152
	this_len = min_t(unsigned int, len, subbuf->page_len);
1153
	if (this_len)
1154
		_copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
1155 1156
	len -= this_len;
	obj += this_len;
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
	memcpy(subbuf->tail[0].iov_base, obj, this_len);
}

/* obj is assumed to point to allocated memory of size at least len: */
int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
{
	struct xdr_buf subbuf;
	int status;

	status = xdr_buf_subsegment(buf, &subbuf, base, len);
	if (status != 0)
		return status;
	__write_bytes_to_xdr_buf(&subbuf, obj, len);
	return 0;
1172
}
1173
EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
1174 1175

int
1176
xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
Linus Torvalds's avatar
Linus Torvalds committed
1177
{
1178
	__be32	raw;
Linus Torvalds's avatar
Linus Torvalds committed
1179 1180 1181 1182 1183
	int	status;

	status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
	if (status)
		return status;
Benny Halevy's avatar
Benny Halevy committed
1184
	*obj = be32_to_cpu(raw);
Linus Torvalds's avatar
Linus Torvalds committed
1185 1186
	return 0;
}
1187
EXPORT_SYMBOL_GPL(xdr_decode_word);
Linus Torvalds's avatar
Linus Torvalds committed
1188

1189
int
1190
xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
1191
{
Benny Halevy's avatar
Benny Halevy committed
1192
	__be32	raw = cpu_to_be32(obj);
1193 1194 1195

	return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
}
1196
EXPORT_SYMBOL_GPL(xdr_encode_word);
1197

Linus Torvalds's avatar
Linus Torvalds committed
1198 1199 1200 1201
/* If the netobj starting offset bytes from the start of xdr_buf is contained
 * entirely in the head or the tail, set object to point to it; otherwise
 * try to find space for it at the end of the tail, copy it there, and
 * set obj to point to it. */
1202
int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
Linus Torvalds's avatar
Linus Torvalds committed
1203
{
1204
	struct xdr_buf subbuf;
Linus Torvalds's avatar
Linus Torvalds committed
1205

1206
	if (xdr_decode_word(buf, offset, &obj->len))
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
		return -EFAULT;
	if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
		return -EFAULT;

	/* Is the obj contained entirely in the head? */
	obj->data = subbuf.head[0].iov_base;
	if (subbuf.head[0].iov_len == obj->len)
		return 0;
	/* ..or is the obj contained entirely in the tail? */
	obj->data = subbuf.tail[0].iov_base;
	if (subbuf.tail[0].iov_len == obj->len)
		return 0;

	/* use end of tail as storage for obj:
	 * (We don't copy to the beginning because then we'd have
	 * to worry about doing a potentially overlapping copy.
	 * This assumes the object is at most half the length of the
	 * tail.) */
	if (obj->len > buf->buflen - buf->len)
		return -ENOMEM;
	if (buf->tail[0].iov_len != 0)
		obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
	else
		obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
	__read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
Linus Torvalds's avatar
Linus Torvalds committed
1232 1233
	return 0;
}
1234
EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

/* Returns 0 on success, or else a negative error code. */
static int
xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
		 struct xdr_array2_desc *desc, int encode)
{
	char *elem = NULL, *c;
	unsigned int copied = 0, todo, avail_here;
	struct page **ppages = NULL;
	int err;

	if (encode) {
		if (xdr_encode_word(buf, base, desc->array_len) != 0)
			return -EINVAL;
	} else {
		if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
1251
		    desc->array_len > desc->array_maxlen ||
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
		    (unsigned long) base + 4 + desc->array_len *
				    desc->elem_size > buf->len)
			return -EINVAL;
	}
	base += 4;

	if (!desc->xcode)
		return 0;

	todo = desc->array_len * desc->elem_size;

	/* process head */
	if (todo && base < buf->head->iov_len) {
		c = buf->head->iov_base + base;
		avail_here = min_t(unsigned int, todo,
				   buf->head->iov_len - base);
		todo -= avail_here;

		while (avail_here >= desc->elem_size) {
			err = desc->xcode(desc, c);
			if (err)
				goto out;
			c += desc->elem_size;
			avail_here -= desc->elem_size;
		}
		if (avail_here) {
			if (!elem) {
				elem = kmalloc(desc->elem_size, GFP_KERNEL);
				err = -ENOMEM;
				if (!elem)
					goto out;
			}
			if (encode) {
				err = desc->xcode(desc, elem);
				if (err)
					goto out;
				memcpy(c, elem, avail_here);
			} else
				memcpy(elem, c, avail_here);
			copied = avail_here;
		}
		base = buf->head->iov_len;  /* align to start of pages */
	}

	/* process pages array */
	base -= buf->head->iov_len;
	if (todo && base < buf->page_len) {
		unsigned int avail_page;

		avail_here = min(todo, buf->page_len - base);
		todo -= avail_here;

		base += buf->page_base;
1305 1306 1307
		ppages = buf->pages + (base >> PAGE_SHIFT);
		base &= ~PAGE_MASK;
		avail_page = min_t(unsigned int, PAGE_SIZE - base,
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
					avail_here);
		c = kmap(*ppages) + base;

		while (avail_here) {
			avail_here -= avail_page;
			if (copied || avail_page < desc->elem_size) {
				unsigned int l = min(avail_page,
					desc->elem_size - copied);
				if (!elem) {
					elem = kmalloc(desc->elem_size,
						       GFP_KERNEL);
					err = -ENOMEM;
					if (!elem)
						goto out;
				}
				if (encode) {
					if (!copied) {
						err = desc->xcode(desc, elem);
						if (err)
							goto out;
					}
					memcpy(c, elem + copied, l);
					copied += l;
					if (copied == desc->elem_size)
						copied = 0;
				} else {
					memcpy(elem + copied, c, l);
					copied += l;
					if (copied == desc->elem_size) {
						err = desc->xcode(desc, elem);
						if (err)
							goto out;
						copied = 0;
					}
				}
				avail_page -= l;
				c += l;
			}
			while (avail_page >= desc->elem_size) {
				err = desc->xcode(desc, c);
				if (err)
					goto out;
				c += desc->elem_size;
				avail_page -= desc->elem_size;
			}
			if (avail_page) {
				unsigned int l = min(avail_page,
					    desc->elem_size - copied);
				if (!elem) {
					elem = kmalloc(desc->elem_size,
						       GFP_KERNEL);
					err = -ENOMEM;
					if (!elem)
						goto out;
				}
				if (encode) {
					if (!copied) {
						err = desc->xcode(desc, elem);
						if (err)
							goto out;
					}
					memcpy(c, elem + copied, l);
					copied += l;
					if (copied == desc->elem_size)
						copied = 0;
				} else {
					memcpy(elem + copied, c, l);
					copied += l;
					if (copied == desc->elem_size) {
						err = desc->xcode(desc, elem);
						if (err)
							goto out;
						copied = 0;
					}
				}
			}
			if (avail_here) {
				kunmap(*ppages);
				ppages++;
				c = kmap(*ppages);
			}

			avail_page = min(avail_here,
1391
				 (unsigned int) PAGE_SIZE);
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
		}
		base = buf->page_len;  /* align to start of tail */
	}

	/* process tail */
	base -= buf->page_len;
	if (todo) {
		c = buf->tail->iov_base + base;
		if (copied) {
			unsigned int l = desc->elem_size - copied;

			if (encode)
				memcpy(c, elem + copied, l);
			else {
				memcpy(elem + copied, c, l);
				err = desc->xcode(desc, elem);
				if (err)
					goto out;
			}
			todo -= l;
			c += l;
		}
		while (todo) {
			err = desc->xcode(desc, c);
			if (err)
				goto out;
			c += desc->elem_size;
			todo -= desc->elem_size;
		}
	}
	err = 0;

out:
Jesper Juhl's avatar
Jesper Juhl committed
1425
	kfree(elem);
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
	if (ppages)
		kunmap(*ppages);
	return err;
}

int
xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
		  struct xdr_array2_desc *desc)
{
	if (base >= buf->len)
		return -EINVAL;

	return xdr_xcode_array2(buf, base, desc, 0);
}
1440
EXPORT_SYMBOL_GPL(xdr_decode_array2);
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451

int
xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
		  struct xdr_array2_desc *desc)
{
	if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
	    buf->head->iov_len + buf->page_len + buf->tail->iov_len)
		return -EINVAL;

	return xdr_xcode_array2(buf, base, desc, 1);
}
1452
EXPORT_SYMBOL_GPL(xdr_encode_array2);
1453 1454 1455

int
xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
1456
		int (*actor)(struct scatterlist *, void *), void *data)
1457 1458
{
	int i, ret = 0;
1459
	unsigned int page_len, thislen, page_offset;
1460 1461
	struct scatterlist      sg[1];

1462 1463
	sg_init_table(sg, 1);

1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
	if (offset >= buf->head[0].iov_len) {
		offset -= buf->head[0].iov_len;
	} else {
		thislen = buf->head[0].iov_len - offset;
		if (thislen > len)
			thislen = len;
		sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
		ret = actor(sg, data);
		if (ret)
			goto out;
		offset = 0;
		len -= thislen;
	}
	if (len == 0)
		goto out;

	if (offset >= buf->page_len) {
		offset -= buf->page_len;
	} else {
		page_len = buf->page_len - offset;
		if (page_len > len)
			page_len = len;
		len -= page_len;
1487 1488 1489
		page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
		i = (offset + buf->page_base) >> PAGE_SHIFT;
		thislen = PAGE_SIZE - page_offset;
1490 1491 1492
		do {
			if (thislen > page_len)
				thislen = page_len;
1493
			sg_set_page(sg, buf->pages[i], thislen, page_offset);
1494 1495 1496 1497 1498 1499
			ret = actor(sg, data);
			if (ret)
				goto out;
			page_len -= thislen;
			i++;
			page_offset = 0;
1500
			thislen = PAGE_SIZE;
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
		} while (page_len != 0);
		offset = 0;
	}
	if (len == 0)
		goto out;
	if (offset < buf->tail[0].iov_len) {
		thislen = buf->tail[0].iov_len - offset;
		if (thislen > len)
			thislen = len;
		sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
		ret = actor(sg, data);
		len -= thislen;
	}
	if (len != 0)
		ret = -EINVAL;
out:
	return ret;
}
1519
EXPORT_SYMBOL_GPL(xdr_process_buf);
1520

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536