kernel.org linux-2.6.9
[linux-2.6.git] / fs / nfsd / nfs3xdr.c
1 /*
2  * linux/fs/nfsd/nfs3xdr.c
3  *
4  * XDR support for nfsd/protocol version 3.
5  *
6  * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
7  *
8  * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
9  */
10
11 #include <linux/types.h>
12 #include <linux/time.h>
13 #include <linux/nfs3.h>
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <linux/dcache.h>
17 #include <linux/namei.h>
18 #include <linux/mm.h>
19 #include <linux/vfs.h>
20 #include <linux/sunrpc/xdr.h>
21 #include <linux/sunrpc/svc.h>
22 #include <linux/nfsd/nfsd.h>
23 #include <linux/nfsd/xdr3.h>
24
25 #define NFSDDBG_FACILITY                NFSDDBG_XDR
26
27 #ifdef NFSD_OPTIMIZE_SPACE
28 # define inline
29 #endif
30
31
32 /*
33  * Mapping of S_IF* types to NFS file types
34  */
35 static u32      nfs3_ftypes[] = {
36         NF3NON,  NF3FIFO, NF3CHR, NF3BAD,
37         NF3DIR,  NF3BAD,  NF3BLK, NF3BAD,
38         NF3REG,  NF3BAD,  NF3LNK, NF3BAD,
39         NF3SOCK, NF3BAD,  NF3LNK, NF3BAD,
40 };
41
42 /*
43  * XDR functions for basic NFS types
44  */
45 static inline u32 *
46 encode_time3(u32 *p, struct timespec *time)
47 {
48         *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
49         return p;
50 }
51
52 static inline u32 *
53 decode_time3(u32 *p, struct timespec *time)
54 {
55         time->tv_sec = ntohl(*p++);
56         time->tv_nsec = ntohl(*p++);
57         return p;
58 }
59
60 static inline u32 *
61 decode_fh(u32 *p, struct svc_fh *fhp)
62 {
63         unsigned int size;
64         fh_init(fhp, NFS3_FHSIZE);
65         size = ntohl(*p++);
66         if (size > NFS3_FHSIZE)
67                 return NULL;
68
69         memcpy(&fhp->fh_handle.fh_base, p, size);
70         fhp->fh_handle.fh_size = size;
71         return p + XDR_QUADLEN(size);
72 }
73
74 static inline u32 *
75 encode_fh(u32 *p, struct svc_fh *fhp)
76 {
77         unsigned int size = fhp->fh_handle.fh_size;
78         *p++ = htonl(size);
79         if (size) p[XDR_QUADLEN(size)-1]=0;
80         memcpy(p, &fhp->fh_handle.fh_base, size);
81         return p + XDR_QUADLEN(size);
82 }
83
84 /*
85  * Decode a file name and make sure that the path contains
86  * no slashes or null bytes.
87  */
88 static inline u32 *
89 decode_filename(u32 *p, char **namp, int *lenp)
90 {
91         char            *name;
92         int             i;
93
94         if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
95                 for (i = 0, name = *namp; i < *lenp; i++, name++) {
96                         if (*name == '\0' || *name == '/')
97                                 return NULL;
98                 }
99         }
100
101         return p;
102 }
103
104 static inline u32 *
105 decode_pathname(u32 *p, char **namp, int *lenp)
106 {
107         char            *name;
108         int             i;
109
110         if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXPATHLEN)) != NULL) {
111                 for (i = 0, name = *namp; i < *lenp; i++, name++) {
112                         if (*name == '\0')
113                                 return NULL;
114                 }
115         }
116
117         return p;
118 }
119
120 static inline u32 *
121 decode_sattr3(u32 *p, struct iattr *iap)
122 {
123         u32     tmp;
124
125         iap->ia_valid = 0;
126
127         if (*p++) {
128                 iap->ia_valid |= ATTR_MODE;
129                 iap->ia_mode = ntohl(*p++);
130         }
131         if (*p++) {
132                 iap->ia_valid |= ATTR_UID;
133                 iap->ia_uid = ntohl(*p++);
134         }
135         if (*p++) {
136                 iap->ia_valid |= ATTR_GID;
137                 iap->ia_gid = ntohl(*p++);
138         }
139         if (*p++) {
140                 u64     newsize;
141
142                 iap->ia_valid |= ATTR_SIZE;
143                 p = xdr_decode_hyper(p, &newsize);
144                 if (newsize <= NFS_OFFSET_MAX)
145                         iap->ia_size = newsize;
146                 else
147                         iap->ia_size = NFS_OFFSET_MAX;
148         }
149         if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
150                 iap->ia_valid |= ATTR_ATIME;
151         } else if (tmp == 2) {          /* set to client time */
152                 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
153                 iap->ia_atime.tv_sec = ntohl(*p++);
154                 iap->ia_atime.tv_nsec = ntohl(*p++);
155         }
156         if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
157                 iap->ia_valid |= ATTR_MTIME;
158         } else if (tmp == 2) {          /* set to client time */
159                 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
160                 iap->ia_mtime.tv_sec = ntohl(*p++);
161                 iap->ia_mtime.tv_nsec = ntohl(*p++);
162         }
163         return p;
164 }
165
166 static inline u32 *
167 encode_fattr3(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
168 {
169         struct vfsmount *mnt = fhp->fh_export->ex_mnt;
170         struct dentry   *dentry = fhp->fh_dentry;
171         struct kstat stat;
172         struct timespec time;
173
174         vfs_getattr(mnt, dentry, &stat);
175
176         *p++ = htonl(nfs3_ftypes[(stat.mode & S_IFMT) >> 12]);
177         *p++ = htonl((u32) stat.mode);
178         *p++ = htonl((u32) stat.nlink);
179         *p++ = htonl((u32) nfsd_ruid(rqstp, stat.uid));
180         *p++ = htonl((u32) nfsd_rgid(rqstp, stat.gid));
181         if (S_ISLNK(stat.mode) && stat.size > NFS3_MAXPATHLEN) {
182                 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
183         } else {
184                 p = xdr_encode_hyper(p, (u64) stat.size);
185         }
186         p = xdr_encode_hyper(p, ((u64)stat.blocks) << 9);
187         *p++ = htonl((u32) MAJOR(stat.rdev));
188         *p++ = htonl((u32) MINOR(stat.rdev));
189         if (is_fsid(fhp, rqstp->rq_reffh))
190                 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
191         else
192                 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat.dev));
193         p = xdr_encode_hyper(p, (u64) stat.ino);
194         p = encode_time3(p, &stat.atime);
195         lease_get_mtime(dentry->d_inode, &time); 
196         p = encode_time3(p, &time);
197         p = encode_time3(p, &stat.ctime);
198
199         return p;
200 }
201
202 static inline u32 *
203 encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
204 {
205         struct inode    *inode = fhp->fh_dentry->d_inode;
206
207         /* Attributes to follow */
208         *p++ = xdr_one;
209
210         *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
211         *p++ = htonl((u32) fhp->fh_post_mode);
212         *p++ = htonl((u32) fhp->fh_post_nlink);
213         *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
214         *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
215         if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
216                 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
217         } else {
218                 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
219         }
220         p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
221         *p++ = fhp->fh_post_rdev[0];
222         *p++ = fhp->fh_post_rdev[1];
223         if (is_fsid(fhp, rqstp->rq_reffh))
224                 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
225         else
226                 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
227         p = xdr_encode_hyper(p, (u64) inode->i_ino);
228         p = encode_time3(p, &fhp->fh_post_atime);
229         p = encode_time3(p, &fhp->fh_post_mtime);
230         p = encode_time3(p, &fhp->fh_post_ctime);
231
232         return p;
233 }
234
235 /*
236  * Encode post-operation attributes.
237  * The inode may be NULL if the call failed because of a stale file
238  * handle. In this case, no attributes are returned.
239  */
240 static u32 *
241 encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
242 {
243         struct dentry *dentry = fhp->fh_dentry;
244         if (dentry && dentry->d_inode != NULL) {
245                 *p++ = xdr_one;         /* attributes follow */
246                 return encode_fattr3(rqstp, p, fhp);
247         }
248         *p++ = xdr_zero;
249         return p;
250 }
251
252 /*
253  * Enocde weak cache consistency data
254  */
255 static u32 *
256 encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
257 {
258         struct dentry   *dentry = fhp->fh_dentry;
259
260         if (dentry && dentry->d_inode && fhp->fh_post_saved) {
261                 if (fhp->fh_pre_saved) {
262                         *p++ = xdr_one;
263                         p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
264                         p = encode_time3(p, &fhp->fh_pre_mtime);
265                         p = encode_time3(p, &fhp->fh_pre_ctime);
266                 } else {
267                         *p++ = xdr_zero;
268                 }
269                 return encode_saved_post_attr(rqstp, p, fhp);
270         }
271         /* no pre- or post-attrs */
272         *p++ = xdr_zero;
273         return encode_post_op_attr(rqstp, p, fhp);
274 }
275
276
277 /*
278  * XDR decode functions
279  */
280 int
281 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
282 {
283         if (!(p = decode_fh(p, &args->fh)))
284                 return 0;
285         return xdr_argsize_check(rqstp, p);
286 }
287
288 int
289 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
290                                         struct nfsd3_sattrargs *args)
291 {
292         if (!(p = decode_fh(p, &args->fh))
293          || !(p = decode_sattr3(p, &args->attrs)))
294                 return 0;
295
296         if ((args->check_guard = ntohl(*p++)) != 0) { 
297                 struct timespec time; 
298                 p = decode_time3(p, &time);
299                 args->guardtime = time.tv_sec;
300         }
301
302         return xdr_argsize_check(rqstp, p);
303 }
304
305 int
306 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
307                                         struct nfsd3_diropargs *args)
308 {
309         if (!(p = decode_fh(p, &args->fh))
310          || !(p = decode_filename(p, &args->name, &args->len)))
311                 return 0;
312
313         return xdr_argsize_check(rqstp, p);
314 }
315
316 int
317 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
318                                         struct nfsd3_accessargs *args)
319 {
320         if (!(p = decode_fh(p, &args->fh)))
321                 return 0;
322         args->access = ntohl(*p++);
323
324         return xdr_argsize_check(rqstp, p);
325 }
326
327 int
328 nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
329                                         struct nfsd3_readargs *args)
330 {
331         unsigned int len;
332         int v,pn;
333
334         if (!(p = decode_fh(p, &args->fh))
335          || !(p = xdr_decode_hyper(p, &args->offset)))
336                 return 0;
337
338         len = args->count = ntohl(*p++);
339
340         if (len > NFSSVC_MAXBLKSIZE)
341                 len = NFSSVC_MAXBLKSIZE;
342
343         /* set up the kvec */
344         v=0;
345         while (len > 0) {
346                 pn = rqstp->rq_resused;
347                 svc_take_page(rqstp);
348                 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
349                 args->vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
350                 len -= args->vec[v].iov_len;
351                 v++;
352         }
353         args->vlen = v;
354         return xdr_argsize_check(rqstp, p);
355 }
356
357 int
358 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
359                                         struct nfsd3_writeargs *args)
360 {
361         unsigned int len, v, hdr;
362
363         if (!(p = decode_fh(p, &args->fh))
364          || !(p = xdr_decode_hyper(p, &args->offset)))
365                 return 0;
366
367         args->count = ntohl(*p++);
368         args->stable = ntohl(*p++);
369         len = args->len = ntohl(*p++);
370
371         hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
372         if (rqstp->rq_arg.len < len + hdr)
373                 return 0;
374
375         args->vec[0].iov_base = (void*)p;
376         args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
377
378         if (len > NFSSVC_MAXBLKSIZE)
379                 len = NFSSVC_MAXBLKSIZE;
380         v=  0;
381         while (len > args->vec[v].iov_len) {
382                 len -= args->vec[v].iov_len;
383                 v++;
384                 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]);
385                 args->vec[v].iov_len = PAGE_SIZE;
386         }
387         args->vec[v].iov_len = len;
388         args->vlen = v+1;
389
390         return args->count == args->len && args->vec[0].iov_len > 0;
391 }
392
393 int
394 nfs3svc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
395                                         struct nfsd3_createargs *args)
396 {
397         if (!(p = decode_fh(p, &args->fh))
398          || !(p = decode_filename(p, &args->name, &args->len)))
399                 return 0;
400
401         switch (args->createmode = ntohl(*p++)) {
402         case NFS3_CREATE_UNCHECKED:
403         case NFS3_CREATE_GUARDED:
404                 if (!(p = decode_sattr3(p, &args->attrs)))
405                         return 0;
406                 break;
407         case NFS3_CREATE_EXCLUSIVE:
408                 args->verf = p;
409                 p += 2;
410                 break;
411         default:
412                 return 0;
413         }
414
415         return xdr_argsize_check(rqstp, p);
416 }
417 int
418 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, u32 *p,
419                                         struct nfsd3_createargs *args)
420 {
421         if (!(p = decode_fh(p, &args->fh))
422          || !(p = decode_filename(p, &args->name, &args->len))
423          || !(p = decode_sattr3(p, &args->attrs)))
424                 return 0;
425
426         return xdr_argsize_check(rqstp, p);
427 }
428
429 int
430 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
431                                         struct nfsd3_symlinkargs *args)
432 {
433         unsigned int len;
434         int avail;
435         char *old, *new;
436         struct kvec *vec;
437
438         if (!(p = decode_fh(p, &args->ffh))
439          || !(p = decode_filename(p, &args->fname, &args->flen))
440          || !(p = decode_sattr3(p, &args->attrs))
441                 )
442                 return 0;
443         /* now decode the pathname, which might be larger than the first page.
444          * As we have to check for nul's anyway, we copy it into a new page
445          * This page appears in the rq_res.pages list, but as pages_len is always
446          * 0, it won't get in the way
447          */
448         svc_take_page(rqstp);
449         len = ntohl(*p++);
450         if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
451                 return 0;
452         args->tname = new = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
453         args->tlen = len;
454         /* first copy and check from the first page */
455         old = (char*)p;
456         vec = &rqstp->rq_arg.head[0];
457         avail = vec->iov_len - (old - (char*)vec->iov_base);
458         while (len && avail && *old) {
459                 *new++ = *old++;
460                 len--;
461                 avail--;
462         }
463         /* now copy next page if there is one */
464         if (len && !avail && rqstp->rq_arg.page_len) {
465                 avail = rqstp->rq_arg.page_len;
466                 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
467                 old = page_address(rqstp->rq_arg.pages[0]);
468         }
469         while (len && avail && *old) {
470                 *new++ = *old++;
471                 len--;
472                 avail--;
473         }
474         *new = '\0';
475         if (len)
476                 return 0;
477
478         return 1;
479 }
480
481 int
482 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, u32 *p,
483                                         struct nfsd3_mknodargs *args)
484 {
485         if (!(p = decode_fh(p, &args->fh))
486          || !(p = decode_filename(p, &args->name, &args->len)))
487                 return 0;
488
489         args->ftype = ntohl(*p++);
490
491         if (args->ftype == NF3BLK  || args->ftype == NF3CHR
492          || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
493                 if (!(p = decode_sattr3(p, &args->attrs)))
494                         return 0;
495         }
496
497         if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
498                 args->major = ntohl(*p++);
499                 args->minor = ntohl(*p++);
500         }
501
502         return xdr_argsize_check(rqstp, p);
503 }
504
505 int
506 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
507                                         struct nfsd3_renameargs *args)
508 {
509         if (!(p = decode_fh(p, &args->ffh))
510          || !(p = decode_filename(p, &args->fname, &args->flen))
511          || !(p = decode_fh(p, &args->tfh))
512          || !(p = decode_filename(p, &args->tname, &args->tlen)))
513                 return 0;
514
515         return xdr_argsize_check(rqstp, p);
516 }
517
518 int
519 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p,
520                                         struct nfsd3_readlinkargs *args)
521 {
522         if (!(p = decode_fh(p, &args->fh)))
523                 return 0;
524         svc_take_page(rqstp);
525         args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
526
527         return xdr_argsize_check(rqstp, p);
528 }
529
530 int
531 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
532                                         struct nfsd3_linkargs *args)
533 {
534         if (!(p = decode_fh(p, &args->ffh))
535          || !(p = decode_fh(p, &args->tfh))
536          || !(p = decode_filename(p, &args->tname, &args->tlen)))
537                 return 0;
538
539         return xdr_argsize_check(rqstp, p);
540 }
541
542 int
543 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
544                                         struct nfsd3_readdirargs *args)
545 {
546         if (!(p = decode_fh(p, &args->fh)))
547                 return 0;
548         p = xdr_decode_hyper(p, &args->cookie);
549         args->verf   = p; p += 2;
550         args->dircount = ~0;
551         args->count  = ntohl(*p++);
552
553         if (args->count > PAGE_SIZE)
554                 args->count = PAGE_SIZE;
555
556         svc_take_page(rqstp);
557         args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
558
559         return xdr_argsize_check(rqstp, p);
560 }
561
562 int
563 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, u32 *p,
564                                         struct nfsd3_readdirargs *args)
565 {
566         int len, pn;
567
568         if (!(p = decode_fh(p, &args->fh)))
569                 return 0;
570         p = xdr_decode_hyper(p, &args->cookie);
571         args->verf     = p; p += 2;
572         args->dircount = ntohl(*p++);
573         args->count    = ntohl(*p++);
574
575         len = (args->count > NFSSVC_MAXBLKSIZE) ? NFSSVC_MAXBLKSIZE :
576                                                   args->count;
577         args->count = len;
578
579         while (len > 0) {
580                 pn = rqstp->rq_resused;
581                 svc_take_page(rqstp);
582                 if (!args->buffer)
583                         args->buffer = page_address(rqstp->rq_respages[pn]);
584                 len -= PAGE_SIZE;
585         }
586
587         return xdr_argsize_check(rqstp, p);
588 }
589
590 int
591 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, u32 *p,
592                                         struct nfsd3_commitargs *args)
593 {
594         if (!(p = decode_fh(p, &args->fh)))
595                 return 0;
596         p = xdr_decode_hyper(p, &args->offset);
597         args->count = ntohl(*p++);
598
599         return xdr_argsize_check(rqstp, p);
600 }
601
602 /*
603  * XDR encode functions
604  */
605 /*
606  * There must be an encoding function for void results so svc_process
607  * will work properly.
608  */
609 int
610 nfs3svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
611 {
612         return xdr_ressize_check(rqstp, p);
613 }
614
615 /* GETATTR */
616 int
617 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
618                                         struct nfsd3_attrstat *resp)
619 {
620         if (resp->status == 0)
621                 p = encode_fattr3(rqstp, p, &resp->fh);
622         return xdr_ressize_check(rqstp, p);
623 }
624
625 /* SETATTR, REMOVE, RMDIR */
626 int
627 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, u32 *p,
628                                         struct nfsd3_attrstat *resp)
629 {
630         p = encode_wcc_data(rqstp, p, &resp->fh);
631         return xdr_ressize_check(rqstp, p);
632 }
633
634 /* LOOKUP */
635 int
636 nfs3svc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
637                                         struct nfsd3_diropres *resp)
638 {
639         if (resp->status == 0) {
640                 p = encode_fh(p, &resp->fh);
641                 p = encode_post_op_attr(rqstp, p, &resp->fh);
642         }
643         p = encode_post_op_attr(rqstp, p, &resp->dirfh);
644         return xdr_ressize_check(rqstp, p);
645 }
646
647 /* ACCESS */
648 int
649 nfs3svc_encode_accessres(struct svc_rqst *rqstp, u32 *p,
650                                         struct nfsd3_accessres *resp)
651 {
652         p = encode_post_op_attr(rqstp, p, &resp->fh);
653         if (resp->status == 0)
654                 *p++ = htonl(resp->access);
655         return xdr_ressize_check(rqstp, p);
656 }
657
658 /* READLINK */
659 int
660 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
661                                         struct nfsd3_readlinkres *resp)
662 {
663         p = encode_post_op_attr(rqstp, p, &resp->fh);
664         if (resp->status == 0) {
665                 *p++ = htonl(resp->len);
666                 xdr_ressize_check(rqstp, p);
667                 rqstp->rq_res.page_len = resp->len;
668                 if (resp->len & 3) {
669                         /* need to pad the tail */
670                         rqstp->rq_restailpage = 0;
671                         rqstp->rq_res.tail[0].iov_base = p;
672                         *p = 0;
673                         rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
674                 }
675                 return 1;
676         } else
677                 return xdr_ressize_check(rqstp, p);
678 }
679
680 /* READ */
681 int
682 nfs3svc_encode_readres(struct svc_rqst *rqstp, u32 *p,
683                                         struct nfsd3_readres *resp)
684 {
685         p = encode_post_op_attr(rqstp, p, &resp->fh);
686         if (resp->status == 0) {
687                 *p++ = htonl(resp->count);
688                 *p++ = htonl(resp->eof);
689                 *p++ = htonl(resp->count);      /* xdr opaque count */
690                 xdr_ressize_check(rqstp, p);
691                 /* now update rqstp->rq_res to reflect data aswell */
692                 rqstp->rq_res.page_len = resp->count;
693                 if (resp->count & 3) {
694                         /* need to pad the tail */
695                         rqstp->rq_restailpage = 0;
696                         rqstp->rq_res.tail[0].iov_base = p;
697                         *p = 0;
698                         rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
699                 }
700                 return 1;
701         } else
702                 return xdr_ressize_check(rqstp, p);
703 }
704
705 /* WRITE */
706 int
707 nfs3svc_encode_writeres(struct svc_rqst *rqstp, u32 *p,
708                                         struct nfsd3_writeres *resp)
709 {
710         p = encode_wcc_data(rqstp, p, &resp->fh);
711         if (resp->status == 0) {
712                 *p++ = htonl(resp->count);
713                 *p++ = htonl(resp->committed);
714                 *p++ = htonl(nfssvc_boot.tv_sec);
715                 *p++ = htonl(nfssvc_boot.tv_usec);
716         }
717         return xdr_ressize_check(rqstp, p);
718 }
719
720 /* CREATE, MKDIR, SYMLINK, MKNOD */
721 int
722 nfs3svc_encode_createres(struct svc_rqst *rqstp, u32 *p,
723                                         struct nfsd3_diropres *resp)
724 {
725         if (resp->status == 0) {
726                 *p++ = xdr_one;
727                 p = encode_fh(p, &resp->fh);
728                 p = encode_post_op_attr(rqstp, p, &resp->fh);
729         }
730         p = encode_wcc_data(rqstp, p, &resp->dirfh);
731         return xdr_ressize_check(rqstp, p);
732 }
733
734 /* RENAME */
735 int
736 nfs3svc_encode_renameres(struct svc_rqst *rqstp, u32 *p,
737                                         struct nfsd3_renameres *resp)
738 {
739         p = encode_wcc_data(rqstp, p, &resp->ffh);
740         p = encode_wcc_data(rqstp, p, &resp->tfh);
741         return xdr_ressize_check(rqstp, p);
742 }
743
744 /* LINK */
745 int
746 nfs3svc_encode_linkres(struct svc_rqst *rqstp, u32 *p,
747                                         struct nfsd3_linkres *resp)
748 {
749         p = encode_post_op_attr(rqstp, p, &resp->fh);
750         p = encode_wcc_data(rqstp, p, &resp->tfh);
751         return xdr_ressize_check(rqstp, p);
752 }
753
754 /* READDIR */
755 int
756 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
757                                         struct nfsd3_readdirres *resp)
758 {
759         p = encode_post_op_attr(rqstp, p, &resp->fh);
760
761         if (resp->status == 0) {
762                 /* stupid readdir cookie */
763                 memcpy(p, resp->verf, 8); p += 2;
764                 xdr_ressize_check(rqstp, p);
765                 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
766                         return 1; /*No room for trailer */
767                 rqstp->rq_res.page_len = (resp->count) << 2;
768
769                 /* add the 'tail' to the end of the 'head' page - page 0. */
770                 rqstp->rq_restailpage = 0;
771                 rqstp->rq_res.tail[0].iov_base = p;
772                 *p++ = 0;               /* no more entries */
773                 *p++ = htonl(resp->common.err == nfserr_eof);
774                 rqstp->rq_res.tail[0].iov_len = 2<<2;
775                 return 1;
776         } else
777                 return xdr_ressize_check(rqstp, p);
778 }
779
780 static inline u32 *
781 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
782              int namlen, ino_t ino)
783 {
784         *p++ = xdr_one;                          /* mark entry present */
785         p    = xdr_encode_hyper(p, ino);         /* file id */
786         p    = xdr_encode_array(p, name, namlen);/* name length & name */
787
788         cd->offset = p;                         /* remember pointer */
789         p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
790
791         return p;
792 }
793
794 static inline u32 *
795 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
796                 struct svc_fh *fhp)
797 {
798                 p = encode_post_op_attr(cd->rqstp, p, fhp);
799                 *p++ = xdr_one;                 /* yes, a file handle follows */
800                 p = encode_fh(p, fhp);
801                 fh_put(fhp);
802                 return p;
803 }
804
805 static int
806 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
807                 const char *name, int namlen)
808 {
809         struct svc_export       *exp;
810         struct dentry           *dparent, *dchild;
811         int rv = 0;
812
813         dparent = cd->fh.fh_dentry;
814         exp  = cd->fh.fh_export;
815
816         fh_init(fhp, NFS3_FHSIZE);
817         if (isdotent(name, namlen)) {
818                 if (namlen == 2) {
819                         dchild = dget_parent(dparent);
820                 } else
821                         dchild = dget(dparent);
822         } else
823                 dchild = lookup_one_len(name, dparent, namlen);
824         if (IS_ERR(dchild))
825                 return 1;
826         if (d_mountpoint(dchild) ||
827             fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
828             !dchild->d_inode)
829                 rv = 1;
830         dput(dchild);
831         return rv;
832 }
833
834 /*
835  * Encode a directory entry. This one works for both normal readdir
836  * and readdirplus.
837  * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
838  * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
839  * 
840  * The readdirplus baggage is 1+21 words for post_op_attr, plus the
841  * file handle.
842  */
843
844 #define NFS3_ENTRY_BAGGAGE      (2 + 1 + 2 + 1)
845 #define NFS3_ENTRYPLUS_BAGGAGE  (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
846 static int
847 encode_entry(struct readdir_cd *ccd, const char *name,
848              int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
849 {
850         struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
851                                                         common);
852         u32             *p = cd->buffer;
853         caddr_t         curr_page_addr = NULL;
854         int             pn;             /* current page number */
855         int             slen;           /* string (name) length */
856         int             elen;           /* estimated entry length in words */
857         int             num_entry_words = 0;    /* actual number of words */
858
859         if (cd->offset) {
860                 u64 offset64 = offset;
861
862                 if (unlikely(cd->offset1)) {
863                         /* we ended up with offset on a page boundary */
864                         *cd->offset = htonl(offset64 >> 32);
865                         *cd->offset1 = htonl(offset64 & 0xffffffff);
866                         cd->offset1 = NULL;
867                 } else {
868                         xdr_encode_hyper(cd->offset, (u64) offset);
869                 }
870         }
871
872         /*
873         dprintk("encode_entry(%.*s @%ld%s)\n",
874                 namlen, name, (long) offset, plus? " plus" : "");
875          */
876
877         /* truncate filename if too long */
878         if (namlen > NFS3_MAXNAMLEN)
879                 namlen = NFS3_MAXNAMLEN;
880
881         slen = XDR_QUADLEN(namlen);
882         elen = slen + NFS3_ENTRY_BAGGAGE
883                 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
884
885         if (cd->buflen < elen) {
886                 cd->common.err = nfserr_toosmall;
887                 return -EINVAL;
888         }
889
890         /* determine which page in rq_respages[] we are currently filling */
891         for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
892                 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
893
894                 if (((caddr_t)cd->buffer >= curr_page_addr) &&
895                     ((caddr_t)cd->buffer <  curr_page_addr + PAGE_SIZE))
896                         break;
897         }
898
899         if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
900                 /* encode entry in current page */
901
902                 p = encode_entry_baggage(cd, p, name, namlen, ino);
903
904                 /* throw in readdirplus baggage */
905                 if (plus) {
906                         struct svc_fh   fh;
907
908                         if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
909                                 *p++ = 0;
910                                 *p++ = 0;
911                         } else
912                                 p = encode_entryplus_baggage(cd, p, &fh);
913                 }
914                 num_entry_words = p - cd->buffer;
915         } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
916                 /* temporarily encode entry into next page, then move back to
917                  * current and next page in rq_respages[] */
918                 u32 *p1, *tmp;
919                 int len1, len2;
920
921                 /* grab next page for temporary storage of entry */
922                 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
923
924                 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
925
926                 /* throw in readdirplus baggage */
927                 if (plus) {
928                         struct svc_fh   fh;
929
930                         if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
931                                 /* zero out the filehandle */
932                                 *p1++ = 0;
933                                 *p1++ = 0;
934                         } else
935                                 p1 = encode_entryplus_baggage(cd, p1, &fh);
936                 }
937
938                 /* determine entry word length and lengths to go in pages */
939                 num_entry_words = p1 - tmp;
940                 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
941                 if ((num_entry_words << 2) < len1) {
942                         /* the actual number of words in the entry is less
943                          * than elen and can still fit in the current page
944                          */
945                         memmove(p, tmp, num_entry_words << 2);
946                         p += num_entry_words;
947
948                         /* update offset */
949                         cd->offset = cd->buffer + (cd->offset - tmp);
950                 } else {
951                         unsigned int offset_r = (cd->offset - tmp) << 2;
952
953                         /* update pointer to offset location.
954                          * This is a 64bit quantity, so we need to
955                          * deal with 3 cases:
956                          *  -   entirely in first page
957                          *  -   entirely in second page
958                          *  -   4 bytes in each page
959                          */
960                         if (offset_r + 8 <= len1) {
961                                 cd->offset = p + (cd->offset - tmp);
962                         } else if (offset_r >= len1) {
963                                 cd->offset -= len1 >> 2;
964                         } else {
965                                 /* sitting on the fence */
966                                 BUG_ON(offset_r != len1 - 4);
967                                 cd->offset = p + (cd->offset - tmp);
968                                 cd->offset1 = tmp;
969                         }
970
971                         len2 = (num_entry_words << 2) - len1;
972
973                         /* move from temp page to current and next pages */
974                         memmove(p, tmp, len1);
975                         memmove(tmp, (caddr_t)tmp+len1, len2);
976
977                         p = tmp + (len2 >> 2);
978                 }
979         }
980         else {
981                 cd->common.err = nfserr_toosmall;
982                 return -EINVAL;
983         }
984
985         cd->buflen -= num_entry_words;
986         cd->buffer = p;
987         cd->common.err = nfs_ok;
988         return 0;
989
990 }
991
992 int
993 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
994                      int namlen, loff_t offset, ino_t ino, unsigned int d_type)
995 {
996         return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
997 }
998
999 int
1000 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
1001                           int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1002 {
1003         return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1004 }
1005
1006 /* FSSTAT */
1007 int
1008 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1009                                         struct nfsd3_fsstatres *resp)
1010 {
1011         struct kstatfs  *s = &resp->stats;
1012         u64             bs = s->f_bsize;
1013
1014         *p++ = xdr_zero;        /* no post_op_attr */
1015
1016         if (resp->status == 0) {
1017                 p = xdr_encode_hyper(p, bs * s->f_blocks);      /* total bytes */
1018                 p = xdr_encode_hyper(p, bs * s->f_bfree);       /* free bytes */
1019                 p = xdr_encode_hyper(p, bs * s->f_bavail);      /* user available bytes */
1020                 p = xdr_encode_hyper(p, s->f_files);    /* total inodes */
1021                 p = xdr_encode_hyper(p, s->f_ffree);    /* free inodes */
1022                 p = xdr_encode_hyper(p, s->f_ffree);    /* user available inodes */
1023                 *p++ = htonl(resp->invarsec);   /* mean unchanged time */
1024         }
1025         return xdr_ressize_check(rqstp, p);
1026 }
1027
1028 /* FSINFO */
1029 int
1030 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1031                                         struct nfsd3_fsinfores *resp)
1032 {
1033         *p++ = xdr_zero;        /* no post_op_attr */
1034
1035         if (resp->status == 0) {
1036                 *p++ = htonl(resp->f_rtmax);
1037                 *p++ = htonl(resp->f_rtpref);
1038                 *p++ = htonl(resp->f_rtmult);
1039                 *p++ = htonl(resp->f_wtmax);
1040                 *p++ = htonl(resp->f_wtpref);
1041                 *p++ = htonl(resp->f_wtmult);
1042                 *p++ = htonl(resp->f_dtpref);
1043                 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1044                 *p++ = xdr_one;
1045                 *p++ = xdr_zero;
1046                 *p++ = htonl(resp->f_properties);
1047         }
1048
1049         return xdr_ressize_check(rqstp, p);
1050 }
1051
1052 /* PATHCONF */
1053 int
1054 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1055                                         struct nfsd3_pathconfres *resp)
1056 {
1057         *p++ = xdr_zero;        /* no post_op_attr */
1058
1059         if (resp->status == 0) {
1060                 *p++ = htonl(resp->p_link_max);
1061                 *p++ = htonl(resp->p_name_max);
1062                 *p++ = htonl(resp->p_no_trunc);
1063                 *p++ = htonl(resp->p_chown_restricted);
1064                 *p++ = htonl(resp->p_case_insensitive);
1065                 *p++ = htonl(resp->p_case_preserving);
1066         }
1067
1068         return xdr_ressize_check(rqstp, p);
1069 }
1070
1071 /* COMMIT */
1072 int
1073 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1074                                         struct nfsd3_commitres *resp)
1075 {
1076         p = encode_wcc_data(rqstp, p, &resp->fh);
1077         /* Write verifier */
1078         if (resp->status == 0) {
1079                 *p++ = htonl(nfssvc_boot.tv_sec);
1080                 *p++ = htonl(nfssvc_boot.tv_usec);
1081         }
1082         return xdr_ressize_check(rqstp, p);
1083 }
1084
1085 /*
1086  * XDR release functions
1087  */
1088 int
1089 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1090                                         struct nfsd3_attrstat *resp)
1091 {
1092         fh_put(&resp->fh);
1093         return 1;
1094 }
1095
1096 int
1097 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1098                                         struct nfsd3_fhandle_pair *resp)
1099 {
1100         fh_put(&resp->fh1);
1101         fh_put(&resp->fh2);
1102         return 1;
1103 }