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