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