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