[PATCH] remove NFSD_OPTIMIZE_SPACE
[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 *
153 encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
154               struct kstat *stat)
155 {
156         struct dentry   *dentry = fhp->fh_dentry;
157         struct timespec time;
158
159         *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
160         *p++ = htonl((u32) stat->mode);
161         *p++ = htonl((u32) stat->nlink);
162         *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
163         *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
164         if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
165                 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
166         } else {
167                 p = xdr_encode_hyper(p, (u64) stat->size);
168         }
169         p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
170         *p++ = htonl((u32) MAJOR(stat->rdev));
171         *p++ = htonl((u32) MINOR(stat->rdev));
172         if (is_fsid(fhp, rqstp->rq_reffh))
173                 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
174         else
175                 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat->dev));
176         p = xdr_encode_hyper(p, (u64) stat->ino);
177         p = encode_time3(p, &stat->atime);
178         lease_get_mtime(dentry->d_inode, &time); 
179         p = encode_time3(p, &time);
180         p = encode_time3(p, &stat->ctime);
181
182         return p;
183 }
184
185 static __be32 *
186 encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
187 {
188         struct inode    *inode = fhp->fh_dentry->d_inode;
189
190         /* Attributes to follow */
191         *p++ = xdr_one;
192
193         *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
194         *p++ = htonl((u32) fhp->fh_post_mode);
195         *p++ = htonl((u32) fhp->fh_post_nlink);
196         *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
197         *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
198         if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
199                 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
200         } else {
201                 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
202         }
203         p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
204         *p++ = fhp->fh_post_rdev[0];
205         *p++ = fhp->fh_post_rdev[1];
206         if (is_fsid(fhp, rqstp->rq_reffh))
207                 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
208         else
209                 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
210         p = xdr_encode_hyper(p, (u64) inode->i_ino);
211         p = encode_time3(p, &fhp->fh_post_atime);
212         p = encode_time3(p, &fhp->fh_post_mtime);
213         p = encode_time3(p, &fhp->fh_post_ctime);
214
215         return p;
216 }
217
218 /*
219  * Encode post-operation attributes.
220  * The inode may be NULL if the call failed because of a stale file
221  * handle. In this case, no attributes are returned.
222  */
223 static __be32 *
224 encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
225 {
226         struct dentry *dentry = fhp->fh_dentry;
227         if (dentry && dentry->d_inode != NULL) {
228                 int err;
229                 struct kstat stat;
230
231                 err = vfs_getattr(fhp->fh_export->ex_mnt, dentry, &stat);
232                 if (!err) {
233                         *p++ = xdr_one;         /* attributes follow */
234                         return encode_fattr3(rqstp, p, fhp, &stat);
235                 }
236         }
237         *p++ = xdr_zero;
238         return p;
239 }
240
241 /* Helper for NFSv3 ACLs */
242 __be32 *
243 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
244 {
245         return encode_post_op_attr(rqstp, p, fhp);
246 }
247
248 /*
249  * Enocde weak cache consistency data
250  */
251 static __be32 *
252 encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
253 {
254         struct dentry   *dentry = fhp->fh_dentry;
255
256         if (dentry && dentry->d_inode && fhp->fh_post_saved) {
257                 if (fhp->fh_pre_saved) {
258                         *p++ = xdr_one;
259                         p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
260                         p = encode_time3(p, &fhp->fh_pre_mtime);
261                         p = encode_time3(p, &fhp->fh_pre_ctime);
262                 } else {
263                         *p++ = xdr_zero;
264                 }
265                 return encode_saved_post_attr(rqstp, p, fhp);
266         }
267         /* no pre- or post-attrs */
268         *p++ = xdr_zero;
269         return encode_post_op_attr(rqstp, p, fhp);
270 }
271
272
273 /*
274  * XDR decode functions
275  */
276 int
277 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
278 {
279         if (!(p = decode_fh(p, &args->fh)))
280                 return 0;
281         return xdr_argsize_check(rqstp, p);
282 }
283
284 int
285 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
286                                         struct nfsd3_sattrargs *args)
287 {
288         if (!(p = decode_fh(p, &args->fh))
289          || !(p = decode_sattr3(p, &args->attrs)))
290                 return 0;
291
292         if ((args->check_guard = ntohl(*p++)) != 0) { 
293                 struct timespec time; 
294                 p = decode_time3(p, &time);
295                 args->guardtime = time.tv_sec;
296         }
297
298         return xdr_argsize_check(rqstp, p);
299 }
300
301 int
302 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
303                                         struct nfsd3_diropargs *args)
304 {
305         if (!(p = decode_fh(p, &args->fh))
306          || !(p = decode_filename(p, &args->name, &args->len)))
307                 return 0;
308
309         return xdr_argsize_check(rqstp, p);
310 }
311
312 int
313 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
314                                         struct nfsd3_accessargs *args)
315 {
316         if (!(p = decode_fh(p, &args->fh)))
317                 return 0;
318         args->access = ntohl(*p++);
319
320         return xdr_argsize_check(rqstp, p);
321 }
322
323 int
324 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
325                                         struct nfsd3_readargs *args)
326 {
327         unsigned int len;
328         int v,pn;
329         u32 max_blocksize = svc_max_payload(rqstp);
330
331         if (!(p = decode_fh(p, &args->fh))
332          || !(p = xdr_decode_hyper(p, &args->offset)))
333                 return 0;
334
335         len = args->count = ntohl(*p++);
336
337         if (len > max_blocksize)
338                 len = max_blocksize;
339
340         /* set up the kvec */
341         v=0;
342         while (len > 0) {
343                 pn = rqstp->rq_resused++;
344                 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
345                 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
346                 len -= rqstp->rq_vec[v].iov_len;
347                 v++;
348         }
349         args->vlen = v;
350         return xdr_argsize_check(rqstp, p);
351 }
352
353 int
354 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
355                                         struct nfsd3_writeargs *args)
356 {
357         unsigned int len, v, hdr;
358         u32 max_blocksize = svc_max_payload(rqstp);
359
360         if (!(p = decode_fh(p, &args->fh))
361          || !(p = xdr_decode_hyper(p, &args->offset)))
362                 return 0;
363
364         args->count = ntohl(*p++);
365         args->stable = ntohl(*p++);
366         len = args->len = ntohl(*p++);
367
368         hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
369         if (rqstp->rq_arg.len < hdr ||
370             rqstp->rq_arg.len - hdr < len)
371                 return 0;
372
373         rqstp->rq_vec[0].iov_base = (void*)p;
374         rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
375
376         if (len > max_blocksize)
377                 len = max_blocksize;
378         v=  0;
379         while (len > rqstp->rq_vec[v].iov_len) {
380                 len -= rqstp->rq_vec[v].iov_len;
381                 v++;
382                 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
383                 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
384         }
385         rqstp->rq_vec[v].iov_len = len;
386         args->vlen = v+1;
387
388         return args->count == args->len && rqstp->rq_vec[0].iov_len > 0;
389 }
390
391 int
392 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
393                                         struct nfsd3_createargs *args)
394 {
395         if (!(p = decode_fh(p, &args->fh))
396          || !(p = decode_filename(p, &args->name, &args->len)))
397                 return 0;
398
399         switch (args->createmode = ntohl(*p++)) {
400         case NFS3_CREATE_UNCHECKED:
401         case NFS3_CREATE_GUARDED:
402                 if (!(p = decode_sattr3(p, &args->attrs)))
403                         return 0;
404                 break;
405         case NFS3_CREATE_EXCLUSIVE:
406                 args->verf = p;
407                 p += 2;
408                 break;
409         default:
410                 return 0;
411         }
412
413         return xdr_argsize_check(rqstp, p);
414 }
415 int
416 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
417                                         struct nfsd3_createargs *args)
418 {
419         if (!(p = decode_fh(p, &args->fh))
420          || !(p = decode_filename(p, &args->name, &args->len))
421          || !(p = decode_sattr3(p, &args->attrs)))
422                 return 0;
423
424         return xdr_argsize_check(rqstp, p);
425 }
426
427 int
428 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
429                                         struct nfsd3_symlinkargs *args)
430 {
431         unsigned int len;
432         int avail;
433         char *old, *new;
434         struct kvec *vec;
435
436         if (!(p = decode_fh(p, &args->ffh))
437          || !(p = decode_filename(p, &args->fname, &args->flen))
438          || !(p = decode_sattr3(p, &args->attrs))
439                 )
440                 return 0;
441         /* now decode the pathname, which might be larger than the first page.
442          * As we have to check for nul's anyway, we copy it into a new page
443          * This page appears in the rq_res.pages list, but as pages_len is always
444          * 0, it won't get in the way
445          */
446         len = ntohl(*p++);
447         if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
448                 return 0;
449         args->tname = new =
450                 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
451         args->tlen = len;
452         /* first copy and check from the first page */
453         old = (char*)p;
454         vec = &rqstp->rq_arg.head[0];
455         avail = vec->iov_len - (old - (char*)vec->iov_base);
456         while (len && avail && *old) {
457                 *new++ = *old++;
458                 len--;
459                 avail--;
460         }
461         /* now copy next page if there is one */
462         if (len && !avail && rqstp->rq_arg.page_len) {
463                 avail = rqstp->rq_arg.page_len;
464                 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
465                 old = page_address(rqstp->rq_arg.pages[0]);
466         }
467         while (len && avail && *old) {
468                 *new++ = *old++;
469                 len--;
470                 avail--;
471         }
472         *new = '\0';
473         if (len)
474                 return 0;
475
476         return 1;
477 }
478
479 int
480 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
481                                         struct nfsd3_mknodargs *args)
482 {
483         if (!(p = decode_fh(p, &args->fh))
484          || !(p = decode_filename(p, &args->name, &args->len)))
485                 return 0;
486
487         args->ftype = ntohl(*p++);
488
489         if (args->ftype == NF3BLK  || args->ftype == NF3CHR
490          || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
491                 if (!(p = decode_sattr3(p, &args->attrs)))
492                         return 0;
493         }
494
495         if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
496                 args->major = ntohl(*p++);
497                 args->minor = ntohl(*p++);
498         }
499
500         return xdr_argsize_check(rqstp, p);
501 }
502
503 int
504 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
505                                         struct nfsd3_renameargs *args)
506 {
507         if (!(p = decode_fh(p, &args->ffh))
508          || !(p = decode_filename(p, &args->fname, &args->flen))
509          || !(p = decode_fh(p, &args->tfh))
510          || !(p = decode_filename(p, &args->tname, &args->tlen)))
511                 return 0;
512
513         return xdr_argsize_check(rqstp, p);
514 }
515
516 int
517 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
518                                         struct nfsd3_readlinkargs *args)
519 {
520         if (!(p = decode_fh(p, &args->fh)))
521                 return 0;
522         args->buffer =
523                 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
524
525         return xdr_argsize_check(rqstp, p);
526 }
527
528 int
529 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
530                                         struct nfsd3_linkargs *args)
531 {
532         if (!(p = decode_fh(p, &args->ffh))
533          || !(p = decode_fh(p, &args->tfh))
534          || !(p = decode_filename(p, &args->tname, &args->tlen)))
535                 return 0;
536
537         return xdr_argsize_check(rqstp, p);
538 }
539
540 int
541 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
542                                         struct nfsd3_readdirargs *args)
543 {
544         if (!(p = decode_fh(p, &args->fh)))
545                 return 0;
546         p = xdr_decode_hyper(p, &args->cookie);
547         args->verf   = p; p += 2;
548         args->dircount = ~0;
549         args->count  = ntohl(*p++);
550
551         if (args->count > PAGE_SIZE)
552                 args->count = PAGE_SIZE;
553
554         args->buffer =
555                 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
556
557         return xdr_argsize_check(rqstp, p);
558 }
559
560 int
561 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
562                                         struct nfsd3_readdirargs *args)
563 {
564         int len, pn;
565         u32 max_blocksize = svc_max_payload(rqstp);
566
567         if (!(p = decode_fh(p, &args->fh)))
568                 return 0;
569         p = xdr_decode_hyper(p, &args->cookie);
570         args->verf     = p; p += 2;
571         args->dircount = ntohl(*p++);
572         args->count    = ntohl(*p++);
573
574         len = (args->count > max_blocksize) ? max_blocksize :
575                                                   args->count;
576         args->count = len;
577
578         while (len > 0) {
579                 pn = rqstp->rq_resused++;
580                 if (!args->buffer)
581                         args->buffer = page_address(rqstp->rq_respages[pn]);
582                 len -= PAGE_SIZE;
583         }
584
585         return xdr_argsize_check(rqstp, p);
586 }
587
588 int
589 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
590                                         struct nfsd3_commitargs *args)
591 {
592         if (!(p = decode_fh(p, &args->fh)))
593                 return 0;
594         p = xdr_decode_hyper(p, &args->offset);
595         args->count = ntohl(*p++);
596
597         return xdr_argsize_check(rqstp, p);
598 }
599
600 /*
601  * XDR encode functions
602  */
603 /*
604  * There must be an encoding function for void results so svc_process
605  * will work properly.
606  */
607 int
608 nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
609 {
610         return xdr_ressize_check(rqstp, p);
611 }
612
613 /* GETATTR */
614 int
615 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
616                                         struct nfsd3_attrstat *resp)
617 {
618         if (resp->status == 0)
619                 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
620         return xdr_ressize_check(rqstp, p);
621 }
622
623 /* SETATTR, REMOVE, RMDIR */
624 int
625 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
626                                         struct nfsd3_attrstat *resp)
627 {
628         p = encode_wcc_data(rqstp, p, &resp->fh);
629         return xdr_ressize_check(rqstp, p);
630 }
631
632 /* LOOKUP */
633 int
634 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
635                                         struct nfsd3_diropres *resp)
636 {
637         if (resp->status == 0) {
638                 p = encode_fh(p, &resp->fh);
639                 p = encode_post_op_attr(rqstp, p, &resp->fh);
640         }
641         p = encode_post_op_attr(rqstp, p, &resp->dirfh);
642         return xdr_ressize_check(rqstp, p);
643 }
644
645 /* ACCESS */
646 int
647 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
648                                         struct nfsd3_accessres *resp)
649 {
650         p = encode_post_op_attr(rqstp, p, &resp->fh);
651         if (resp->status == 0)
652                 *p++ = htonl(resp->access);
653         return xdr_ressize_check(rqstp, p);
654 }
655
656 /* READLINK */
657 int
658 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
659                                         struct nfsd3_readlinkres *resp)
660 {
661         p = encode_post_op_attr(rqstp, p, &resp->fh);
662         if (resp->status == 0) {
663                 *p++ = htonl(resp->len);
664                 xdr_ressize_check(rqstp, p);
665                 rqstp->rq_res.page_len = resp->len;
666                 if (resp->len & 3) {
667                         /* need to pad the tail */
668                         rqstp->rq_res.tail[0].iov_base = p;
669                         *p = 0;
670                         rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
671                 }
672                 return 1;
673         } else
674                 return xdr_ressize_check(rqstp, p);
675 }
676
677 /* READ */
678 int
679 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
680                                         struct nfsd3_readres *resp)
681 {
682         p = encode_post_op_attr(rqstp, p, &resp->fh);
683         if (resp->status == 0) {
684                 *p++ = htonl(resp->count);
685                 *p++ = htonl(resp->eof);
686                 *p++ = htonl(resp->count);      /* xdr opaque count */
687                 xdr_ressize_check(rqstp, p);
688                 /* now update rqstp->rq_res to reflect data aswell */
689                 rqstp->rq_res.page_len = resp->count;
690                 if (resp->count & 3) {
691                         /* need to pad the tail */
692                         rqstp->rq_res.tail[0].iov_base = p;
693                         *p = 0;
694                         rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
695                 }
696                 return 1;
697         } else
698                 return xdr_ressize_check(rqstp, p);
699 }
700
701 /* WRITE */
702 int
703 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
704                                         struct nfsd3_writeres *resp)
705 {
706         p = encode_wcc_data(rqstp, p, &resp->fh);
707         if (resp->status == 0) {
708                 *p++ = htonl(resp->count);
709                 *p++ = htonl(resp->committed);
710                 *p++ = htonl(nfssvc_boot.tv_sec);
711                 *p++ = htonl(nfssvc_boot.tv_usec);
712         }
713         return xdr_ressize_check(rqstp, p);
714 }
715
716 /* CREATE, MKDIR, SYMLINK, MKNOD */
717 int
718 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
719                                         struct nfsd3_diropres *resp)
720 {
721         if (resp->status == 0) {
722                 *p++ = xdr_one;
723                 p = encode_fh(p, &resp->fh);
724                 p = encode_post_op_attr(rqstp, p, &resp->fh);
725         }
726         p = encode_wcc_data(rqstp, p, &resp->dirfh);
727         return xdr_ressize_check(rqstp, p);
728 }
729
730 /* RENAME */
731 int
732 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
733                                         struct nfsd3_renameres *resp)
734 {
735         p = encode_wcc_data(rqstp, p, &resp->ffh);
736         p = encode_wcc_data(rqstp, p, &resp->tfh);
737         return xdr_ressize_check(rqstp, p);
738 }
739
740 /* LINK */
741 int
742 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
743                                         struct nfsd3_linkres *resp)
744 {
745         p = encode_post_op_attr(rqstp, p, &resp->fh);
746         p = encode_wcc_data(rqstp, p, &resp->tfh);
747         return xdr_ressize_check(rqstp, p);
748 }
749
750 /* READDIR */
751 int
752 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
753                                         struct nfsd3_readdirres *resp)
754 {
755         p = encode_post_op_attr(rqstp, p, &resp->fh);
756
757         if (resp->status == 0) {
758                 /* stupid readdir cookie */
759                 memcpy(p, resp->verf, 8); p += 2;
760                 xdr_ressize_check(rqstp, p);
761                 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
762                         return 1; /*No room for trailer */
763                 rqstp->rq_res.page_len = (resp->count) << 2;
764
765                 /* add the 'tail' to the end of the 'head' page - page 0. */
766                 rqstp->rq_res.tail[0].iov_base = p;
767                 *p++ = 0;               /* no more entries */
768                 *p++ = htonl(resp->common.err == nfserr_eof);
769                 rqstp->rq_res.tail[0].iov_len = 2<<2;
770                 return 1;
771         } else
772                 return xdr_ressize_check(rqstp, p);
773 }
774
775 static __be32 *
776 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
777              int namlen, ino_t ino)
778 {
779         *p++ = xdr_one;                          /* mark entry present */
780         p    = xdr_encode_hyper(p, ino);         /* file id */
781         p    = xdr_encode_array(p, name, namlen);/* name length & name */
782
783         cd->offset = p;                         /* remember pointer */
784         p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
785
786         return p;
787 }
788
789 static __be32 *
790 encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
791                 struct svc_fh *fhp)
792 {
793                 p = encode_post_op_attr(cd->rqstp, p, fhp);
794                 *p++ = xdr_one;                 /* yes, a file handle follows */
795                 p = encode_fh(p, fhp);
796                 fh_put(fhp);
797                 return p;
798 }
799
800 static int
801 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
802                 const char *name, int namlen)
803 {
804         struct svc_export       *exp;
805         struct dentry           *dparent, *dchild;
806         int rv = 0;
807
808         dparent = cd->fh.fh_dentry;
809         exp  = cd->fh.fh_export;
810
811         fh_init(fhp, NFS3_FHSIZE);
812         if (isdotent(name, namlen)) {
813                 if (namlen == 2) {
814                         dchild = dget_parent(dparent);
815                         if (dchild == dparent) {
816                                 /* filesystem root - cannot return filehandle for ".." */
817                                 dput(dchild);
818                                 return 1;
819                         }
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         __be32          *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                 __be32 *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, __be32 *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, __be32 *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, __be32 *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, __be32 *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, __be32 *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, __be32 *p,
1098                                         struct nfsd3_fhandle_pair *resp)
1099 {
1100         fh_put(&resp->fh1);
1101         fh_put(&resp->fh2);
1102         return 1;
1103 }