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