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