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