patch-2_6_7-vs1_9_1_12
[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 #include <linux/vserver/xid.h>
25
26 #define NFSDDBG_FACILITY                NFSDDBG_XDR
27
28 #ifdef NFSD_OPTIMIZE_SPACE
29 # define inline
30 #endif
31
32
33 /*
34  * Mapping of S_IF* types to NFS file types
35  */
36 static u32      nfs3_ftypes[] = {
37         NF3NON,  NF3FIFO, NF3CHR, NF3BAD,
38         NF3DIR,  NF3BAD,  NF3BLK, NF3BAD,
39         NF3REG,  NF3BAD,  NF3LNK, NF3BAD,
40         NF3SOCK, NF3BAD,  NF3LNK, NF3BAD,
41 };
42
43 /*
44  * XDR functions for basic NFS types
45  */
46 static inline u32 *
47 encode_time3(u32 *p, struct timespec *time)
48 {
49         *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
50         return p;
51 }
52
53 static inline u32 *
54 decode_time3(u32 *p, struct timespec *time)
55 {
56         time->tv_sec = ntohl(*p++);
57         time->tv_nsec = ntohl(*p++);
58         return p;
59 }
60
61 static inline u32 *
62 decode_fh(u32 *p, struct svc_fh *fhp)
63 {
64         unsigned int size;
65         fh_init(fhp, NFS3_FHSIZE);
66         size = ntohl(*p++);
67         if (size > NFS3_FHSIZE)
68                 return NULL;
69
70         memcpy(&fhp->fh_handle.fh_base, p, size);
71         fhp->fh_handle.fh_size = size;
72         return p + XDR_QUADLEN(size);
73 }
74
75 static inline u32 *
76 encode_fh(u32 *p, struct svc_fh *fhp)
77 {
78         int size = fhp->fh_handle.fh_size;
79         *p++ = htonl(size);
80         if (size) p[XDR_QUADLEN(size)-1]=0;
81         memcpy(p, &fhp->fh_handle.fh_base, size);
82         return p + XDR_QUADLEN(size);
83 }
84
85 /*
86  * Decode a file name and make sure that the path contains
87  * no slashes or null bytes.
88  */
89 static inline u32 *
90 decode_filename(u32 *p, char **namp, int *lenp)
91 {
92         char            *name;
93         int             i;
94
95         if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
96                 for (i = 0, name = *namp; i < *lenp; i++, name++) {
97                         if (*name == '\0' || *name == '/')
98                                 return NULL;
99                 }
100         }
101
102         return p;
103 }
104
105 static inline u32 *
106 decode_pathname(u32 *p, char **namp, int *lenp)
107 {
108         char            *name;
109         int             i;
110
111         if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXPATHLEN)) != NULL) {
112                 for (i = 0, name = *namp; i < *lenp; i++, name++) {
113                         if (*name == '\0')
114                                 return NULL;
115                 }
116         }
117
118         return p;
119 }
120
121 static inline u32 *
122 decode_sattr3(u32 *p, struct iattr *iap)
123 {
124         u32     tmp;
125
126         iap->ia_valid = 0;
127
128         if (*p++) {
129                 iap->ia_valid |= ATTR_MODE;
130                 iap->ia_mode = ntohl(*p++);
131         }
132         if (*p++) {
133                 iap->ia_valid |= ATTR_UID;
134                 iap->ia_uid = ntohl(*p++);
135         }
136         if (*p++) {
137                 iap->ia_valid |= ATTR_GID;
138                 iap->ia_gid = ntohl(*p++);
139         }
140         if (*p++) {
141                 u64     newsize;
142
143                 iap->ia_valid |= ATTR_SIZE;
144                 p = xdr_decode_hyper(p, &newsize);
145                 if (newsize <= NFS_OFFSET_MAX)
146                         iap->ia_size = newsize;
147                 else
148                         iap->ia_size = NFS_OFFSET_MAX;
149         }
150         if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
151                 iap->ia_valid |= ATTR_ATIME;
152         } else if (tmp == 2) {          /* set to client time */
153                 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
154                 iap->ia_atime.tv_sec = ntohl(*p++);
155                 iap->ia_atime.tv_nsec = ntohl(*p++);
156         }
157         if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
158                 iap->ia_valid |= ATTR_MTIME;
159         } else if (tmp == 2) {          /* set to client time */
160                 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
161                 iap->ia_mtime.tv_sec = ntohl(*p++);
162                 iap->ia_mtime.tv_nsec = ntohl(*p++);
163         }
164         return p;
165 }
166
167 static inline u32 *
168 encode_fattr3(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
169 {
170         struct vfsmount *mnt = fhp->fh_export->ex_mnt;
171         struct dentry   *dentry = fhp->fh_dentry;
172         struct kstat stat;
173         struct timespec time;
174
175         vfs_getattr(mnt, dentry, &stat);
176
177         *p++ = htonl(nfs3_ftypes[(stat.mode & S_IFMT) >> 12]);
178         *p++ = htonl((u32) stat.mode);
179         *p++ = htonl((u32) stat.nlink);
180         *p++ = htonl((u32) nfsd_ruid(rqstp,
181                 XIDINO_UID(stat.uid, stat.xid)));
182         *p++ = htonl((u32) nfsd_rgid(rqstp,
183                 XIDINO_GID(stat.gid, stat.xid)));
184         if (S_ISLNK(stat.mode) && stat.size > NFS3_MAXPATHLEN) {
185                 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
186         } else {
187                 p = xdr_encode_hyper(p, (u64) stat.size);
188         }
189         p = xdr_encode_hyper(p, ((u64)stat.blocks) << 9);
190         *p++ = htonl((u32) MAJOR(stat.rdev));
191         *p++ = htonl((u32) MINOR(stat.rdev));
192         if (is_fsid(fhp, rqstp->rq_reffh))
193                 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
194         else
195                 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat.dev));
196         p = xdr_encode_hyper(p, (u64) stat.ino);
197         p = encode_time3(p, &stat.atime);
198         lease_get_mtime(dentry->d_inode, &time); 
199         p = encode_time3(p, &time);
200         p = encode_time3(p, &stat.ctime);
201
202         return p;
203 }
204
205 static inline u32 *
206 encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
207 {
208         struct inode    *inode = fhp->fh_dentry->d_inode;
209
210         /* Attributes to follow */
211         *p++ = xdr_one;
212
213         *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
214         *p++ = htonl((u32) fhp->fh_post_mode);
215         *p++ = htonl((u32) fhp->fh_post_nlink);
216         *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
217         *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
218         if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
219                 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
220         } else {
221                 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
222         }
223         p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
224         *p++ = fhp->fh_post_rdev[0];
225         *p++ = fhp->fh_post_rdev[1];
226         if (is_fsid(fhp, rqstp->rq_reffh))
227                 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
228         else
229                 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
230         p = xdr_encode_hyper(p, (u64) inode->i_ino);
231         p = encode_time3(p, &fhp->fh_post_atime);
232         p = encode_time3(p, &fhp->fh_post_mtime);
233         p = encode_time3(p, &fhp->fh_post_ctime);
234
235         return p;
236 }
237
238 /*
239  * Encode post-operation attributes.
240  * The inode may be NULL if the call failed because of a stale file
241  * handle. In this case, no attributes are returned.
242  */
243 static u32 *
244 encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
245 {
246         struct dentry *dentry = fhp->fh_dentry;
247         if (dentry && dentry->d_inode != NULL) {
248                 *p++ = xdr_one;         /* attributes follow */
249                 return encode_fattr3(rqstp, p, fhp);
250         }
251         *p++ = xdr_zero;
252         return p;
253 }
254
255 /*
256  * Enocde weak cache consistency data
257  */
258 static u32 *
259 encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
260 {
261         struct dentry   *dentry = fhp->fh_dentry;
262
263         if (dentry && dentry->d_inode && fhp->fh_post_saved) {
264                 if (fhp->fh_pre_saved) {
265                         *p++ = xdr_one;
266                         p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
267                         p = encode_time3(p, &fhp->fh_pre_mtime);
268                         p = encode_time3(p, &fhp->fh_pre_ctime);
269                 } else {
270                         *p++ = xdr_zero;
271                 }
272                 return encode_saved_post_attr(rqstp, p, fhp);
273         }
274         /* no pre- or post-attrs */
275         *p++ = xdr_zero;
276         return encode_post_op_attr(rqstp, p, fhp);
277 }
278
279
280 /*
281  * XDR decode functions
282  */
283 int
284 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
285 {
286         if (!(p = decode_fh(p, &args->fh)))
287                 return 0;
288         return xdr_argsize_check(rqstp, p);
289 }
290
291 int
292 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
293                                         struct nfsd3_sattrargs *args)
294 {
295         if (!(p = decode_fh(p, &args->fh))
296          || !(p = decode_sattr3(p, &args->attrs)))
297                 return 0;
298
299         if ((args->check_guard = ntohl(*p++)) != 0) { 
300                 struct timespec time; 
301                 p = decode_time3(p, &time);
302                 args->guardtime = time.tv_sec;
303         }
304
305         return xdr_argsize_check(rqstp, p);
306 }
307
308 int
309 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
310                                         struct nfsd3_diropargs *args)
311 {
312         if (!(p = decode_fh(p, &args->fh))
313          || !(p = decode_filename(p, &args->name, &args->len)))
314                 return 0;
315
316         return xdr_argsize_check(rqstp, p);
317 }
318
319 int
320 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
321                                         struct nfsd3_accessargs *args)
322 {
323         if (!(p = decode_fh(p, &args->fh)))
324                 return 0;
325         args->access = ntohl(*p++);
326
327         return xdr_argsize_check(rqstp, p);
328 }
329
330 int
331 nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
332                                         struct nfsd3_readargs *args)
333 {
334         int len;
335         int v,pn;
336
337         if (!(p = decode_fh(p, &args->fh))
338          || !(p = xdr_decode_hyper(p, &args->offset)))
339                 return 0;
340
341         len = args->count = ntohl(*p++);
342
343         if (len > NFSSVC_MAXBLKSIZE)
344                 len = NFSSVC_MAXBLKSIZE;
345
346         /* set up the iovec */
347         v=0;
348         while (len > 0) {
349                 pn = rqstp->rq_resused;
350                 svc_take_page(rqstp);
351                 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
352                 args->vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
353                 v++;
354                 len -= PAGE_SIZE;
355         }
356         args->vlen = v;
357         return xdr_argsize_check(rqstp, p);
358 }
359
360 int
361 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
362                                         struct nfsd3_writeargs *args)
363 {
364         int len, v;
365
366         if (!(p = decode_fh(p, &args->fh))
367          || !(p = xdr_decode_hyper(p, &args->offset)))
368                 return 0;
369
370         args->count = ntohl(*p++);
371         args->stable = ntohl(*p++);
372         len = args->len = ntohl(*p++);
373
374         args->vec[0].iov_base = (void*)p;
375         args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len -
376                 (((void*)p) - rqstp->rq_arg.head[0].iov_base);
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         int len;
434         int avail;
435         char *old, *new;
436         struct iovec *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                 p = resp->buffer;
766                 *p++ = 0;               /* no more entries */
767                 *p++ = htonl(resp->common.err == nfserr_eof);
768                 rqstp->rq_res.page_len = (resp->count + 2) << 2;
769                 return 1;
770         } else
771                 return xdr_ressize_check(rqstp, p);
772 }
773
774 static inline u32 *
775 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
776              int namlen, ino_t ino)
777 {
778         *p++ = xdr_one;                          /* mark entry present */
779         p    = xdr_encode_hyper(p, ino);         /* file id */
780         p    = xdr_encode_array(p, name, namlen);/* name length & name */
781
782         cd->offset = p;                         /* remember pointer */
783         p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
784
785         return p;
786 }
787
788 static inline u32 *
789 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
790                 struct svc_fh *fhp)
791 {
792                 p = encode_post_op_attr(cd->rqstp, p, fhp);
793                 *p++ = xdr_one;                 /* yes, a file handle follows */
794                 p = encode_fh(p, fhp);
795                 fh_put(fhp);
796                 return p;
797 }
798
799 static int
800 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
801                 const char *name, int namlen)
802 {
803         struct svc_export       *exp;
804         struct dentry           *dparent, *dchild;
805         int rv = 0;
806
807         dparent = cd->fh.fh_dentry;
808         exp  = cd->fh.fh_export;
809
810         fh_init(fhp, NFS3_FHSIZE);
811         if (isdotent(name, namlen)) {
812                 if (namlen == 2) {
813                         dchild = dget_parent(dparent);
814                 } else
815                         dchild = dget(dparent);
816         } else
817                 dchild = lookup_one_len(name, dparent, namlen);
818         if (IS_ERR(dchild))
819                 return 1;
820         if (d_mountpoint(dchild) ||
821             fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
822             !dchild->d_inode)
823                 rv = 1;
824         dput(dchild);
825         return rv;
826 }
827
828 /*
829  * Encode a directory entry. This one works for both normal readdir
830  * and readdirplus.
831  * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
832  * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
833  * 
834  * The readdirplus baggage is 1+21 words for post_op_attr, plus the
835  * file handle.
836  */
837
838 #define NFS3_ENTRY_BAGGAGE      (2 + 1 + 2 + 1)
839 #define NFS3_ENTRYPLUS_BAGGAGE  (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
840 static int
841 encode_entry(struct readdir_cd *ccd, const char *name,
842              int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
843 {
844         struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
845                                                         common);
846         u32             *p = cd->buffer;
847         caddr_t         curr_page_addr = NULL;
848         int             pn;             /* current page number */
849         int             slen;           /* string (name) length */
850         int             elen;           /* estimated entry length in words */
851         int             num_entry_words = 0;    /* actual number of words */
852
853         if (cd->offset) {
854                 u64 offset64 = offset;
855
856                 if (unlikely(cd->offset1)) {
857                         /* we ended up with offset on a page boundary */
858                         *cd->offset = htonl(offset64 >> 32);
859                         *cd->offset1 = htonl(offset64 & 0xffffffff);
860                         cd->offset1 = NULL;
861                 } else {
862                         xdr_encode_hyper(cd->offset, (u64) offset);
863                 }
864         }
865
866         /*
867         dprintk("encode_entry(%.*s @%ld%s)\n",
868                 namlen, name, (long) offset, plus? " plus" : "");
869          */
870
871         /* truncate filename if too long */
872         if (namlen > NFS3_MAXNAMLEN)
873                 namlen = NFS3_MAXNAMLEN;
874
875         slen = XDR_QUADLEN(namlen);
876         elen = slen + NFS3_ENTRY_BAGGAGE
877                 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
878
879         if (cd->buflen < elen) {
880                 cd->common.err = nfserr_toosmall;
881                 return -EINVAL;
882         }
883
884         /* determine which page in rq_respages[] we are currently filling */
885         for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
886                 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
887
888                 if (((caddr_t)cd->buffer >= curr_page_addr) &&
889                     ((caddr_t)cd->buffer <  curr_page_addr + PAGE_SIZE))
890                         break;
891         }
892
893         if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
894                 /* encode entry in current page */
895
896                 p = encode_entry_baggage(cd, p, name, namlen, ino);
897
898                 /* throw in readdirplus baggage */
899                 if (plus) {
900                         struct svc_fh   fh;
901
902                         if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
903                                 *p++ = 0;
904                                 *p++ = 0;
905                         } else
906                                 p = encode_entryplus_baggage(cd, p, &fh);
907                 }
908                 num_entry_words = p - cd->buffer;
909         } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
910                 /* temporarily encode entry into next page, then move back to
911                  * current and next page in rq_respages[] */
912                 u32 *p1, *tmp;
913                 int len1, len2;
914
915                 /* grab next page for temporary storage of entry */
916                 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
917
918                 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
919
920                 /* throw in readdirplus baggage */
921                 if (plus) {
922                         struct svc_fh   fh;
923
924                         if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
925                                 /* zero out the filehandle */
926                                 *p1++ = 0;
927                                 *p1++ = 0;
928                         } else
929                                 p1 = encode_entryplus_baggage(cd, p1, &fh);
930                 }
931
932                 /* determine entry word length and lengths to go in pages */
933                 num_entry_words = p1 - tmp;
934                 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
935                 if ((num_entry_words << 2) < len1) {
936                         /* the actual number of words in the entry is less
937                          * than elen and can still fit in the current page
938                          */
939                         memmove(p, tmp, num_entry_words << 2);
940                         p += num_entry_words;
941
942                         /* update offset */
943                         cd->offset = cd->buffer + (cd->offset - tmp);
944                 } else {
945                         unsigned int offset_r = (cd->offset - tmp) << 2;
946
947                         /* update pointer to offset location.
948                          * This is a 64bit quantity, so we need to
949                          * deal with 3 cases:
950                          *  -   entirely in first page
951                          *  -   entirely in second page
952                          *  -   4 bytes in each page
953                          */
954                         if (offset_r + 8 <= len1) {
955                                 cd->offset = p + (cd->offset - tmp);
956                         } else if (offset_r >= len1) {
957                                 cd->offset -= len1 >> 2;
958                         } else {
959                                 /* sitting on the fence */
960                                 BUG_ON(offset_r != len1 - 4);
961                                 cd->offset = p + (cd->offset - tmp);
962                                 cd->offset1 = tmp;
963                         }
964
965                         len2 = (num_entry_words << 2) - len1;
966
967                         /* move from temp page to current and next pages */
968                         memmove(p, tmp, len1);
969                         memmove(tmp, (caddr_t)tmp+len1, len2);
970
971                         p = tmp + (len2 >> 2);
972                 }
973         }
974         else {
975                 cd->common.err = nfserr_toosmall;
976                 return -EINVAL;
977         }
978
979         cd->buflen -= num_entry_words;
980         cd->buffer = p;
981         cd->common.err = nfs_ok;
982         return 0;
983
984 }
985
986 int
987 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
988                      int namlen, loff_t offset, ino_t ino, unsigned int d_type)
989 {
990         return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
991 }
992
993 int
994 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
995                           int namlen, loff_t offset, ino_t ino, unsigned int d_type)
996 {
997         return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
998 }
999
1000 /* FSSTAT */
1001 int
1002 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1003                                         struct nfsd3_fsstatres *resp)
1004 {
1005         struct kstatfs  *s = &resp->stats;
1006         u64             bs = s->f_bsize;
1007
1008         *p++ = xdr_zero;        /* no post_op_attr */
1009
1010         if (resp->status == 0) {
1011                 p = xdr_encode_hyper(p, bs * s->f_blocks);      /* total bytes */
1012                 p = xdr_encode_hyper(p, bs * s->f_bfree);       /* free bytes */
1013                 p = xdr_encode_hyper(p, bs * s->f_bavail);      /* user available bytes */
1014                 p = xdr_encode_hyper(p, s->f_files);    /* total inodes */
1015                 p = xdr_encode_hyper(p, s->f_ffree);    /* free inodes */
1016                 p = xdr_encode_hyper(p, s->f_ffree);    /* user available inodes */
1017                 *p++ = htonl(resp->invarsec);   /* mean unchanged time */
1018         }
1019         return xdr_ressize_check(rqstp, p);
1020 }
1021
1022 /* FSINFO */
1023 int
1024 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1025                                         struct nfsd3_fsinfores *resp)
1026 {
1027         *p++ = xdr_zero;        /* no post_op_attr */
1028
1029         if (resp->status == 0) {
1030                 *p++ = htonl(resp->f_rtmax);
1031                 *p++ = htonl(resp->f_rtpref);
1032                 *p++ = htonl(resp->f_rtmult);
1033                 *p++ = htonl(resp->f_wtmax);
1034                 *p++ = htonl(resp->f_wtpref);
1035                 *p++ = htonl(resp->f_wtmult);
1036                 *p++ = htonl(resp->f_dtpref);
1037                 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1038                 *p++ = xdr_one;
1039                 *p++ = xdr_zero;
1040                 *p++ = htonl(resp->f_properties);
1041         }
1042
1043         return xdr_ressize_check(rqstp, p);
1044 }
1045
1046 /* PATHCONF */
1047 int
1048 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1049                                         struct nfsd3_pathconfres *resp)
1050 {
1051         *p++ = xdr_zero;        /* no post_op_attr */
1052
1053         if (resp->status == 0) {
1054                 *p++ = htonl(resp->p_link_max);
1055                 *p++ = htonl(resp->p_name_max);
1056                 *p++ = htonl(resp->p_no_trunc);
1057                 *p++ = htonl(resp->p_chown_restricted);
1058                 *p++ = htonl(resp->p_case_insensitive);
1059                 *p++ = htonl(resp->p_case_preserving);
1060         }
1061
1062         return xdr_ressize_check(rqstp, p);
1063 }
1064
1065 /* COMMIT */
1066 int
1067 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1068                                         struct nfsd3_commitres *resp)
1069 {
1070         p = encode_wcc_data(rqstp, p, &resp->fh);
1071         /* Write verifier */
1072         if (resp->status == 0) {
1073                 *p++ = htonl(nfssvc_boot.tv_sec);
1074                 *p++ = htonl(nfssvc_boot.tv_usec);
1075         }
1076         return xdr_ressize_check(rqstp, p);
1077 }
1078
1079 /*
1080  * XDR release functions
1081  */
1082 int
1083 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1084                                         struct nfsd3_attrstat *resp)
1085 {
1086         fh_put(&resp->fh);
1087         return 1;
1088 }
1089
1090 int
1091 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1092                                         struct nfsd3_fhandle_pair *resp)
1093 {
1094         fh_put(&resp->fh1);
1095         fh_put(&resp->fh2);
1096         return 1;
1097 }