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