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