]> err.no Git - linux-2.6/blob - net/sunrpc/xdr.c
[PATCH] RPC: extract socket logic common to both client and server
[linux-2.6] / net / sunrpc / xdr.c
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
49  * @p: pointer to current position in XDR buffer.
50  * @ptr: pointer to data to encode (or NULL)
51  * @nbytes: size of data.
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
79  * @p: pointer to current position in XDR buffer.
80  * @ptr: pointer to data to encode (or NULL)
81  * @nbytes: size of data.
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
180 int
181 xdr_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen,
182                 struct xdr_buf *xdr, unsigned int base, int msgflags)
183 {
184         struct page **ppage = xdr->pages;
185         unsigned int len, pglen = xdr->page_len;
186         int err, ret = 0;
187         ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
188
189         len = xdr->head[0].iov_len;
190         if (base < len || (addr != NULL && base == 0)) {
191                 struct kvec iov = {
192                         .iov_base = xdr->head[0].iov_base + base,
193                         .iov_len  = len - base,
194                 };
195                 struct msghdr msg = {
196                         .msg_name    = addr,
197                         .msg_namelen = addrlen,
198                         .msg_flags   = msgflags,
199                 };
200                 if (xdr->len > len)
201                         msg.msg_flags |= MSG_MORE;
202
203                 if (iov.iov_len != 0)
204                         err = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
205                 else
206                         err = kernel_sendmsg(sock, &msg, NULL, 0, 0);
207                 if (ret == 0)
208                         ret = err;
209                 else if (err > 0)
210                         ret += err;
211                 if (err != iov.iov_len)
212                         goto out;
213                 base = 0;
214         } else
215                 base -= len;
216
217         if (pglen == 0)
218                 goto copy_tail;
219         if (base >= pglen) {
220                 base -= pglen;
221                 goto copy_tail;
222         }
223         if (base || xdr->page_base) {
224                 pglen -= base;
225                 base  += xdr->page_base;
226                 ppage += base >> PAGE_CACHE_SHIFT;
227                 base &= ~PAGE_CACHE_MASK;
228         }
229
230         sendpage = sock->ops->sendpage ? : sock_no_sendpage;
231         do {
232                 int flags = msgflags;
233
234                 len = PAGE_CACHE_SIZE;
235                 if (base)
236                         len -= base;
237                 if (pglen < len)
238                         len = pglen;
239
240                 if (pglen != len || xdr->tail[0].iov_len != 0)
241                         flags |= MSG_MORE;
242
243                 /* Hmm... We might be dealing with highmem pages */
244                 if (PageHighMem(*ppage))
245                         sendpage = sock_no_sendpage;
246                 err = sendpage(sock, *ppage, base, len, flags);
247                 if (ret == 0)
248                         ret = err;
249                 else if (err > 0)
250                         ret += err;
251                 if (err != len)
252                         goto out;
253                 base = 0;
254                 ppage++;
255         } while ((pglen -= len) != 0);
256 copy_tail:
257         len = xdr->tail[0].iov_len;
258         if (base < len) {
259                 struct kvec iov = {
260                         .iov_base = xdr->tail[0].iov_base + base,
261                         .iov_len  = len - base,
262                 };
263                 struct msghdr msg = {
264                         .msg_flags   = msgflags,
265                 };
266                 err = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
267                 if (ret == 0)
268                         ret = err;
269                 else if (err > 0)
270                         ret += err;
271         }
272 out:
273         return ret;
274 }
275
276
277 /*
278  * Helper routines for doing 'memmove' like operations on a struct xdr_buf
279  *
280  * _shift_data_right_pages
281  * @pages: vector of pages containing both the source and dest memory area.
282  * @pgto_base: page vector address of destination
283  * @pgfrom_base: page vector address of source
284  * @len: number of bytes to copy
285  *
286  * Note: the addresses pgto_base and pgfrom_base are both calculated in
287  *       the same way:
288  *            if a memory area starts at byte 'base' in page 'pages[i]',
289  *            then its address is given as (i << PAGE_CACHE_SHIFT) + base
290  * Also note: pgfrom_base must be < pgto_base, but the memory areas
291  *      they point to may overlap.
292  */
293 static void
294 _shift_data_right_pages(struct page **pages, size_t pgto_base,
295                 size_t pgfrom_base, size_t len)
296 {
297         struct page **pgfrom, **pgto;
298         char *vfrom, *vto;
299         size_t copy;
300
301         BUG_ON(pgto_base <= pgfrom_base);
302
303         pgto_base += len;
304         pgfrom_base += len;
305
306         pgto = pages + (pgto_base >> PAGE_CACHE_SHIFT);
307         pgfrom = pages + (pgfrom_base >> PAGE_CACHE_SHIFT);
308
309         pgto_base &= ~PAGE_CACHE_MASK;
310         pgfrom_base &= ~PAGE_CACHE_MASK;
311
312         do {
313                 /* Are any pointers crossing a page boundary? */
314                 if (pgto_base == 0) {
315                         flush_dcache_page(*pgto);
316                         pgto_base = PAGE_CACHE_SIZE;
317                         pgto--;
318                 }
319                 if (pgfrom_base == 0) {
320                         pgfrom_base = PAGE_CACHE_SIZE;
321                         pgfrom--;
322                 }
323
324                 copy = len;
325                 if (copy > pgto_base)
326                         copy = pgto_base;
327                 if (copy > pgfrom_base)
328                         copy = pgfrom_base;
329                 pgto_base -= copy;
330                 pgfrom_base -= copy;
331
332                 vto = kmap_atomic(*pgto, KM_USER0);
333                 vfrom = kmap_atomic(*pgfrom, KM_USER1);
334                 memmove(vto + pgto_base, vfrom + pgfrom_base, copy);
335                 kunmap_atomic(vfrom, KM_USER1);
336                 kunmap_atomic(vto, KM_USER0);
337
338         } while ((len -= copy) != 0);
339         flush_dcache_page(*pgto);
340 }
341
342 /*
343  * _copy_to_pages
344  * @pages: array of pages
345  * @pgbase: page vector address of destination
346  * @p: pointer to source data
347  * @len: length
348  *
349  * Copies data from an arbitrary memory location into an array of pages
350  * The copy is assumed to be non-overlapping.
351  */
352 static void
353 _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
354 {
355         struct page **pgto;
356         char *vto;
357         size_t copy;
358
359         pgto = pages + (pgbase >> PAGE_CACHE_SHIFT);
360         pgbase &= ~PAGE_CACHE_MASK;
361
362         do {
363                 copy = PAGE_CACHE_SIZE - pgbase;
364                 if (copy > len)
365                         copy = len;
366
367                 vto = kmap_atomic(*pgto, KM_USER0);
368                 memcpy(vto + pgbase, p, copy);
369                 kunmap_atomic(vto, KM_USER0);
370
371                 pgbase += copy;
372                 if (pgbase == PAGE_CACHE_SIZE) {
373                         flush_dcache_page(*pgto);
374                         pgbase = 0;
375                         pgto++;
376                 }
377                 p += copy;
378
379         } while ((len -= copy) != 0);
380         flush_dcache_page(*pgto);
381 }
382
383 /*
384  * _copy_from_pages
385  * @p: pointer to destination
386  * @pages: array of pages
387  * @pgbase: offset of source data
388  * @len: length
389  *
390  * Copies data into an arbitrary memory location from an array of pages
391  * The copy is assumed to be non-overlapping.
392  */
393 static void
394 _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
395 {
396         struct page **pgfrom;
397         char *vfrom;
398         size_t copy;
399
400         pgfrom = pages + (pgbase >> PAGE_CACHE_SHIFT);
401         pgbase &= ~PAGE_CACHE_MASK;
402
403         do {
404                 copy = PAGE_CACHE_SIZE - pgbase;
405                 if (copy > len)
406                         copy = len;
407
408                 vfrom = kmap_atomic(*pgfrom, KM_USER0);
409                 memcpy(p, vfrom + pgbase, copy);
410                 kunmap_atomic(vfrom, KM_USER0);
411
412                 pgbase += copy;
413                 if (pgbase == PAGE_CACHE_SIZE) {
414                         pgbase = 0;
415                         pgfrom++;
416                 }
417                 p += copy;
418
419         } while ((len -= copy) != 0);
420 }
421
422 /*
423  * xdr_shrink_bufhead
424  * @buf: xdr_buf
425  * @len: bytes to remove from buf->head[0]
426  *
427  * Shrinks XDR buffer's header kvec buf->head[0] by 
428  * 'len' bytes. The extra data is not lost, but is instead
429  * moved into the inlined pages and/or the tail.
430  */
431 static void
432 xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
433 {
434         struct kvec *head, *tail;
435         size_t copy, offs;
436         unsigned int pglen = buf->page_len;
437
438         tail = buf->tail;
439         head = buf->head;
440         BUG_ON (len > head->iov_len);
441
442         /* Shift the tail first */
443         if (tail->iov_len != 0) {
444                 if (tail->iov_len > len) {
445                         copy = tail->iov_len - len;
446                         memmove((char *)tail->iov_base + len,
447                                         tail->iov_base, copy);
448                 }
449                 /* Copy from the inlined pages into the tail */
450                 copy = len;
451                 if (copy > pglen)
452                         copy = pglen;
453                 offs = len - copy;
454                 if (offs >= tail->iov_len)
455                         copy = 0;
456                 else if (copy > tail->iov_len - offs)
457                         copy = tail->iov_len - offs;
458                 if (copy != 0)
459                         _copy_from_pages((char *)tail->iov_base + offs,
460                                         buf->pages,
461                                         buf->page_base + pglen + offs - len,
462                                         copy);
463                 /* Do we also need to copy data from the head into the tail ? */
464                 if (len > pglen) {
465                         offs = copy = len - pglen;
466                         if (copy > tail->iov_len)
467                                 copy = tail->iov_len;
468                         memcpy(tail->iov_base,
469                                         (char *)head->iov_base +
470                                         head->iov_len - offs,
471                                         copy);
472                 }
473         }
474         /* Now handle pages */
475         if (pglen != 0) {
476                 if (pglen > len)
477                         _shift_data_right_pages(buf->pages,
478                                         buf->page_base + len,
479                                         buf->page_base,
480                                         pglen - len);
481                 copy = len;
482                 if (len > pglen)
483                         copy = pglen;
484                 _copy_to_pages(buf->pages, buf->page_base,
485                                 (char *)head->iov_base + head->iov_len - len,
486                                 copy);
487         }
488         head->iov_len -= len;
489         buf->buflen -= len;
490         /* Have we truncated the message? */
491         if (buf->len > buf->buflen)
492                 buf->len = buf->buflen;
493 }
494
495 /*
496  * xdr_shrink_pagelen
497  * @buf: xdr_buf
498  * @len: bytes to remove from buf->pages
499  *
500  * Shrinks XDR buffer's page array buf->pages by 
501  * 'len' bytes. The extra data is not lost, but is instead
502  * moved into the tail.
503  */
504 static void
505 xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
506 {
507         struct kvec *tail;
508         size_t copy;
509         char *p;
510         unsigned int pglen = buf->page_len;
511
512         tail = buf->tail;
513         BUG_ON (len > pglen);
514
515         /* Shift the tail first */
516         if (tail->iov_len != 0) {
517                 p = (char *)tail->iov_base + len;
518                 if (tail->iov_len > len) {
519                         copy = tail->iov_len - len;
520                         memmove(p, tail->iov_base, copy);
521                 } else
522                         buf->buflen -= len;
523                 /* Copy from the inlined pages into the tail */
524                 copy = len;
525                 if (copy > tail->iov_len)
526                         copy = tail->iov_len;
527                 _copy_from_pages((char *)tail->iov_base,
528                                 buf->pages, buf->page_base + pglen - len,
529                                 copy);
530         }
531         buf->page_len -= len;
532         buf->buflen -= len;
533         /* Have we truncated the message? */
534         if (buf->len > buf->buflen)
535                 buf->len = buf->buflen;
536 }
537
538 void
539 xdr_shift_buf(struct xdr_buf *buf, size_t len)
540 {
541         xdr_shrink_bufhead(buf, len);
542 }
543
544 /**
545  * xdr_init_encode - Initialize a struct xdr_stream for sending data.
546  * @xdr: pointer to xdr_stream struct
547  * @buf: pointer to XDR buffer in which to encode data
548  * @p: current pointer inside XDR buffer
549  *
550  * Note: at the moment the RPC client only passes the length of our
551  *       scratch buffer in the xdr_buf's header kvec. Previously this
552  *       meant we needed to call xdr_adjust_iovec() after encoding the
553  *       data. With the new scheme, the xdr_stream manages the details
554  *       of the buffer length, and takes care of adjusting the kvec
555  *       length for us.
556  */
557 void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p)
558 {
559         struct kvec *iov = buf->head;
560         int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
561
562         BUG_ON(scratch_len < 0);
563         xdr->buf = buf;
564         xdr->iov = iov;
565         xdr->p = (uint32_t *)((char *)iov->iov_base + iov->iov_len);
566         xdr->end = (uint32_t *)((char *)iov->iov_base + scratch_len);
567         BUG_ON(iov->iov_len > scratch_len);
568
569         if (p != xdr->p && p != NULL) {
570                 size_t len;
571
572                 BUG_ON(p < xdr->p || p > xdr->end);
573                 len = (char *)p - (char *)xdr->p;
574                 xdr->p = p;
575                 buf->len += len;
576                 iov->iov_len += len;
577         }
578 }
579 EXPORT_SYMBOL(xdr_init_encode);
580
581 /**
582  * xdr_reserve_space - Reserve buffer space for sending
583  * @xdr: pointer to xdr_stream
584  * @nbytes: number of bytes to reserve
585  *
586  * Checks that we have enough buffer space to encode 'nbytes' more
587  * bytes of data. If so, update the total xdr_buf length, and
588  * adjust the length of the current kvec.
589  */
590 uint32_t * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
591 {
592         uint32_t *p = xdr->p;
593         uint32_t *q;
594
595         /* align nbytes on the next 32-bit boundary */
596         nbytes += 3;
597         nbytes &= ~3;
598         q = p + (nbytes >> 2);
599         if (unlikely(q > xdr->end || q < p))
600                 return NULL;
601         xdr->p = q;
602         xdr->iov->iov_len += nbytes;
603         xdr->buf->len += nbytes;
604         return p;
605 }
606 EXPORT_SYMBOL(xdr_reserve_space);
607
608 /**
609  * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
610  * @xdr: pointer to xdr_stream
611  * @pages: list of pages
612  * @base: offset of first byte
613  * @len: length of data in bytes
614  *
615  */
616 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
617                  unsigned int len)
618 {
619         struct xdr_buf *buf = xdr->buf;
620         struct kvec *iov = buf->tail;
621         buf->pages = pages;
622         buf->page_base = base;
623         buf->page_len = len;
624
625         iov->iov_base = (char *)xdr->p;
626         iov->iov_len  = 0;
627         xdr->iov = iov;
628
629         if (len & 3) {
630                 unsigned int pad = 4 - (len & 3);
631
632                 BUG_ON(xdr->p >= xdr->end);
633                 iov->iov_base = (char *)xdr->p + (len & 3);
634                 iov->iov_len  += pad;
635                 len += pad;
636                 *xdr->p++ = 0;
637         }
638         buf->buflen += len;
639         buf->len += len;
640 }
641 EXPORT_SYMBOL(xdr_write_pages);
642
643 /**
644  * xdr_init_decode - Initialize an xdr_stream for decoding data.
645  * @xdr: pointer to xdr_stream struct
646  * @buf: pointer to XDR buffer from which to decode data
647  * @p: current pointer inside XDR buffer
648  */
649 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p)
650 {
651         struct kvec *iov = buf->head;
652         unsigned int len = iov->iov_len;
653
654         if (len > buf->len)
655                 len = buf->len;
656         xdr->buf = buf;
657         xdr->iov = iov;
658         xdr->p = p;
659         xdr->end = (uint32_t *)((char *)iov->iov_base + len);
660 }
661 EXPORT_SYMBOL(xdr_init_decode);
662
663 /**
664  * xdr_inline_decode - Retrieve non-page XDR data to decode
665  * @xdr: pointer to xdr_stream struct
666  * @nbytes: number of bytes of data to decode
667  *
668  * Check if the input buffer is long enough to enable us to decode
669  * 'nbytes' more bytes of data starting at the current position.
670  * If so return the current pointer, then update the current
671  * pointer position.
672  */
673 uint32_t * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
674 {
675         uint32_t *p = xdr->p;
676         uint32_t *q = p + XDR_QUADLEN(nbytes);
677
678         if (unlikely(q > xdr->end || q < p))
679                 return NULL;
680         xdr->p = q;
681         return p;
682 }
683 EXPORT_SYMBOL(xdr_inline_decode);
684
685 /**
686  * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
687  * @xdr: pointer to xdr_stream struct
688  * @len: number of bytes of page data
689  *
690  * Moves data beyond the current pointer position from the XDR head[] buffer
691  * into the page list. Any data that lies beyond current position + "len"
692  * bytes is moved into the XDR tail[]. The current pointer is then
693  * repositioned at the beginning of the XDR tail.
694  */
695 void xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
696 {
697         struct xdr_buf *buf = xdr->buf;
698         struct kvec *iov;
699         ssize_t shift;
700         unsigned int end;
701         int padding;
702
703         /* Realign pages to current pointer position */
704         iov  = buf->head;
705         shift = iov->iov_len + (char *)iov->iov_base - (char *)xdr->p;
706         if (shift > 0)
707                 xdr_shrink_bufhead(buf, shift);
708
709         /* Truncate page data and move it into the tail */
710         if (buf->page_len > len)
711                 xdr_shrink_pagelen(buf, buf->page_len - len);
712         padding = (XDR_QUADLEN(len) << 2) - len;
713         xdr->iov = iov = buf->tail;
714         /* Compute remaining message length.  */
715         end = iov->iov_len;
716         shift = buf->buflen - buf->len;
717         if (shift < end)
718                 end -= shift;
719         else if (shift > 0)
720                 end = 0;
721         /*
722          * Position current pointer at beginning of tail, and
723          * set remaining message length.
724          */
725         xdr->p = (uint32_t *)((char *)iov->iov_base + padding);
726         xdr->end = (uint32_t *)((char *)iov->iov_base + end);
727 }
728 EXPORT_SYMBOL(xdr_read_pages);
729
730 static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
731
732 void
733 xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
734 {
735         buf->head[0] = *iov;
736         buf->tail[0] = empty_iov;
737         buf->page_len = 0;
738         buf->buflen = buf->len = iov->iov_len;
739 }
740
741 /* Sets subiov to the intersection of iov with the buffer of length len
742  * starting base bytes after iov.  Indicates empty intersection by setting
743  * length of subiov to zero.  Decrements len by length of subiov, sets base
744  * to zero (or decrements it by length of iov if subiov is empty). */
745 static void
746 iov_subsegment(struct kvec *iov, struct kvec *subiov, int *base, int *len)
747 {
748         if (*base > iov->iov_len) {
749                 subiov->iov_base = NULL;
750                 subiov->iov_len = 0;
751                 *base -= iov->iov_len;
752         } else {
753                 subiov->iov_base = iov->iov_base + *base;
754                 subiov->iov_len = min(*len, (int)iov->iov_len - *base);
755                 *base = 0;
756         }
757         *len -= subiov->iov_len; 
758 }
759
760 /* Sets subbuf to the portion of buf of length len beginning base bytes
761  * from the start of buf. Returns -1 if base of length are out of bounds. */
762 int
763 xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
764                         int base, int len)
765 {
766         int i;
767
768         subbuf->buflen = subbuf->len = len;
769         iov_subsegment(buf->head, subbuf->head, &base, &len);
770
771         if (base < buf->page_len) {
772                 i = (base + buf->page_base) >> PAGE_CACHE_SHIFT;
773                 subbuf->pages = &buf->pages[i];
774                 subbuf->page_base = (base + buf->page_base) & ~PAGE_CACHE_MASK;
775                 subbuf->page_len = min((int)buf->page_len - base, len);
776                 len -= subbuf->page_len;
777                 base = 0;
778         } else {
779                 base -= buf->page_len;
780                 subbuf->page_len = 0;
781         }
782
783         iov_subsegment(buf->tail, subbuf->tail, &base, &len);
784         if (base || len)
785                 return -1;
786         return 0;
787 }
788
789 /* obj is assumed to point to allocated memory of size at least len: */
790 int
791 read_bytes_from_xdr_buf(struct xdr_buf *buf, int base, void *obj, int len)
792 {
793         struct xdr_buf subbuf;
794         int this_len;
795         int status;
796
797         status = xdr_buf_subsegment(buf, &subbuf, base, len);
798         if (status)
799                 goto out;
800         this_len = min(len, (int)subbuf.head[0].iov_len);
801         memcpy(obj, subbuf.head[0].iov_base, this_len);
802         len -= this_len;
803         obj += this_len;
804         this_len = min(len, (int)subbuf.page_len);
805         if (this_len)
806                 _copy_from_pages(obj, subbuf.pages, subbuf.page_base, this_len);
807         len -= this_len;
808         obj += this_len;
809         this_len = min(len, (int)subbuf.tail[0].iov_len);
810         memcpy(obj, subbuf.tail[0].iov_base, this_len);
811 out:
812         return status;
813 }
814
815 /* obj is assumed to point to allocated memory of size at least len: */
816 int
817 write_bytes_to_xdr_buf(struct xdr_buf *buf, int base, void *obj, int len)
818 {
819         struct xdr_buf subbuf;
820         int this_len;
821         int status;
822
823         status = xdr_buf_subsegment(buf, &subbuf, base, len);
824         if (status)
825                 goto out;
826         this_len = min(len, (int)subbuf.head[0].iov_len);
827         memcpy(subbuf.head[0].iov_base, obj, this_len);
828         len -= this_len;
829         obj += this_len;
830         this_len = min(len, (int)subbuf.page_len);
831         if (this_len)
832                 _copy_to_pages(subbuf.pages, subbuf.page_base, obj, this_len);
833         len -= this_len;
834         obj += this_len;
835         this_len = min(len, (int)subbuf.tail[0].iov_len);
836         memcpy(subbuf.tail[0].iov_base, obj, this_len);
837 out:
838         return status;
839 }
840
841 int
842 xdr_decode_word(struct xdr_buf *buf, int base, u32 *obj)
843 {
844         u32     raw;
845         int     status;
846
847         status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
848         if (status)
849                 return status;
850         *obj = ntohl(raw);
851         return 0;
852 }
853
854 int
855 xdr_encode_word(struct xdr_buf *buf, int base, u32 obj)
856 {
857         u32     raw = htonl(obj);
858
859         return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
860 }
861
862 /* If the netobj starting offset bytes from the start of xdr_buf is contained
863  * entirely in the head or the tail, set object to point to it; otherwise
864  * try to find space for it at the end of the tail, copy it there, and
865  * set obj to point to it. */
866 int
867 xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, int offset)
868 {
869         u32     tail_offset = buf->head[0].iov_len + buf->page_len;
870         u32     obj_end_offset;
871
872         if (xdr_decode_word(buf, offset, &obj->len))
873                 goto out;
874         obj_end_offset = offset + 4 + obj->len;
875
876         if (obj_end_offset <= buf->head[0].iov_len) {
877                 /* The obj is contained entirely in the head: */
878                 obj->data = buf->head[0].iov_base + offset + 4;
879         } else if (offset + 4 >= tail_offset) {
880                 if (obj_end_offset - tail_offset
881                                 > buf->tail[0].iov_len)
882                         goto out;
883                 /* The obj is contained entirely in the tail: */
884                 obj->data = buf->tail[0].iov_base
885                         + offset - tail_offset + 4;
886         } else {
887                 /* use end of tail as storage for obj:
888                  * (We don't copy to the beginning because then we'd have
889                  * to worry about doing a potentially overlapping copy.
890                  * This assumes the object is at most half the length of the
891                  * tail.) */
892                 if (obj->len > buf->tail[0].iov_len)
893                         goto out;
894                 obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len - 
895                                 obj->len;
896                 if (read_bytes_from_xdr_buf(buf, offset + 4,
897                                         obj->data, obj->len))
898                         goto out;
899
900         }
901         return 0;
902 out:
903         return -1;
904 }
905
906 /* Returns 0 on success, or else a negative error code. */
907 static int
908 xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
909                  struct xdr_array2_desc *desc, int encode)
910 {
911         char *elem = NULL, *c;
912         unsigned int copied = 0, todo, avail_here;
913         struct page **ppages = NULL;
914         int err;
915
916         if (encode) {
917                 if (xdr_encode_word(buf, base, desc->array_len) != 0)
918                         return -EINVAL;
919         } else {
920                 if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
921                     desc->array_len > desc->array_maxlen ||
922                     (unsigned long) base + 4 + desc->array_len *
923                                     desc->elem_size > buf->len)
924                         return -EINVAL;
925         }
926         base += 4;
927
928         if (!desc->xcode)
929                 return 0;
930
931         todo = desc->array_len * desc->elem_size;
932
933         /* process head */
934         if (todo && base < buf->head->iov_len) {
935                 c = buf->head->iov_base + base;
936                 avail_here = min_t(unsigned int, todo,
937                                    buf->head->iov_len - base);
938                 todo -= avail_here;
939
940                 while (avail_here >= desc->elem_size) {
941                         err = desc->xcode(desc, c);
942                         if (err)
943                                 goto out;
944                         c += desc->elem_size;
945                         avail_here -= desc->elem_size;
946                 }
947                 if (avail_here) {
948                         if (!elem) {
949                                 elem = kmalloc(desc->elem_size, GFP_KERNEL);
950                                 err = -ENOMEM;
951                                 if (!elem)
952                                         goto out;
953                         }
954                         if (encode) {
955                                 err = desc->xcode(desc, elem);
956                                 if (err)
957                                         goto out;
958                                 memcpy(c, elem, avail_here);
959                         } else
960                                 memcpy(elem, c, avail_here);
961                         copied = avail_here;
962                 }
963                 base = buf->head->iov_len;  /* align to start of pages */
964         }
965
966         /* process pages array */
967         base -= buf->head->iov_len;
968         if (todo && base < buf->page_len) {
969                 unsigned int avail_page;
970
971                 avail_here = min(todo, buf->page_len - base);
972                 todo -= avail_here;
973
974                 base += buf->page_base;
975                 ppages = buf->pages + (base >> PAGE_CACHE_SHIFT);
976                 base &= ~PAGE_CACHE_MASK;
977                 avail_page = min_t(unsigned int, PAGE_CACHE_SIZE - base,
978                                         avail_here);
979                 c = kmap(*ppages) + base;
980
981                 while (avail_here) {
982                         avail_here -= avail_page;
983                         if (copied || avail_page < desc->elem_size) {
984                                 unsigned int l = min(avail_page,
985                                         desc->elem_size - copied);
986                                 if (!elem) {
987                                         elem = kmalloc(desc->elem_size,
988                                                        GFP_KERNEL);
989                                         err = -ENOMEM;
990                                         if (!elem)
991                                                 goto out;
992                                 }
993                                 if (encode) {
994                                         if (!copied) {
995                                                 err = desc->xcode(desc, elem);
996                                                 if (err)
997                                                         goto out;
998                                         }
999                                         memcpy(c, elem + copied, l);
1000                                         copied += l;
1001                                         if (copied == desc->elem_size)
1002                                                 copied = 0;
1003                                 } else {
1004                                         memcpy(elem + copied, c, l);
1005                                         copied += l;
1006                                         if (copied == desc->elem_size) {
1007                                                 err = desc->xcode(desc, elem);
1008                                                 if (err)
1009                                                         goto out;
1010                                                 copied = 0;
1011                                         }
1012                                 }
1013                                 avail_page -= l;
1014                                 c += l;
1015                         }
1016                         while (avail_page >= desc->elem_size) {
1017                                 err = desc->xcode(desc, c);
1018                                 if (err)
1019                                         goto out;
1020                                 c += desc->elem_size;
1021                                 avail_page -= desc->elem_size;
1022                         }
1023                         if (avail_page) {
1024                                 unsigned int l = min(avail_page,
1025                                             desc->elem_size - copied);
1026                                 if (!elem) {
1027                                         elem = kmalloc(desc->elem_size,
1028                                                        GFP_KERNEL);
1029                                         err = -ENOMEM;
1030                                         if (!elem)
1031                                                 goto out;
1032                                 }
1033                                 if (encode) {
1034                                         if (!copied) {
1035                                                 err = desc->xcode(desc, elem);
1036                                                 if (err)
1037                                                         goto out;
1038                                         }
1039                                         memcpy(c, elem + copied, l);
1040                                         copied += l;
1041                                         if (copied == desc->elem_size)
1042                                                 copied = 0;
1043                                 } else {
1044                                         memcpy(elem + copied, c, l);
1045                                         copied += l;
1046                                         if (copied == desc->elem_size) {
1047                                                 err = desc->xcode(desc, elem);
1048                                                 if (err)
1049                                                         goto out;
1050                                                 copied = 0;
1051                                         }
1052                                 }
1053                         }
1054                         if (avail_here) {
1055                                 kunmap(*ppages);
1056                                 ppages++;
1057                                 c = kmap(*ppages);
1058                         }
1059
1060                         avail_page = min(avail_here,
1061                                  (unsigned int) PAGE_CACHE_SIZE);
1062                 }
1063                 base = buf->page_len;  /* align to start of tail */
1064         }
1065
1066         /* process tail */
1067         base -= buf->page_len;
1068         if (todo) {
1069                 c = buf->tail->iov_base + base;
1070                 if (copied) {
1071                         unsigned int l = desc->elem_size - copied;
1072
1073                         if (encode)
1074                                 memcpy(c, elem + copied, l);
1075                         else {
1076                                 memcpy(elem + copied, c, l);
1077                                 err = desc->xcode(desc, elem);
1078                                 if (err)
1079                                         goto out;
1080                         }
1081                         todo -= l;
1082                         c += l;
1083                 }
1084                 while (todo) {
1085                         err = desc->xcode(desc, c);
1086                         if (err)
1087                                 goto out;
1088                         c += desc->elem_size;
1089                         todo -= desc->elem_size;
1090                 }
1091         }
1092         err = 0;
1093
1094 out:
1095         if (elem)
1096                 kfree(elem);
1097         if (ppages)
1098                 kunmap(*ppages);
1099         return err;
1100 }
1101
1102 int
1103 xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
1104                   struct xdr_array2_desc *desc)
1105 {
1106         if (base >= buf->len)
1107                 return -EINVAL;
1108
1109         return xdr_xcode_array2(buf, base, desc, 0);
1110 }
1111
1112 int
1113 xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
1114                   struct xdr_array2_desc *desc)
1115 {
1116         if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
1117             buf->head->iov_len + buf->page_len + buf->tail->iov_len)
1118                 return -EINVAL;
1119
1120         return xdr_xcode_array2(buf, base, desc, 1);
1121 }