vserver 1.9.3
[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                 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
774                         return 1; /*No room for trailer */
775                 rqstp->rq_res.page_len = (resp->count) << 2;
776
777                 /* add the 'tail' to the end of the 'head' page - page 0. */
778                 rqstp->rq_restailpage = 0;
779                 rqstp->rq_res.tail[0].iov_base = p;
780                 *p++ = 0;               /* no more entries */
781                 *p++ = htonl(resp->common.err == nfserr_eof);
782                 rqstp->rq_res.tail[0].iov_len = 2<<2;
783                 return 1;
784         } else
785                 return xdr_ressize_check(rqstp, p);
786 }
787
788 static inline u32 *
789 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
790              int namlen, ino_t ino)
791 {
792         *p++ = xdr_one;                          /* mark entry present */
793         p    = xdr_encode_hyper(p, ino);         /* file id */
794         p    = xdr_encode_array(p, name, namlen);/* name length & name */
795
796         cd->offset = p;                         /* remember pointer */
797         p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
798
799         return p;
800 }
801
802 static inline u32 *
803 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
804                 struct svc_fh *fhp)
805 {
806                 p = encode_post_op_attr(cd->rqstp, p, fhp);
807                 *p++ = xdr_one;                 /* yes, a file handle follows */
808                 p = encode_fh(p, fhp);
809                 fh_put(fhp);
810                 return p;
811 }
812
813 static int
814 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
815                 const char *name, int namlen)
816 {
817         struct svc_export       *exp;
818         struct dentry           *dparent, *dchild;
819         int rv = 0;
820
821         dparent = cd->fh.fh_dentry;
822         exp  = cd->fh.fh_export;
823
824         fh_init(fhp, NFS3_FHSIZE);
825         if (isdotent(name, namlen)) {
826                 if (namlen == 2) {
827                         dchild = dget_parent(dparent);
828                 } else
829                         dchild = dget(dparent);
830         } else
831                 dchild = lookup_one_len(name, dparent, namlen);
832         if (IS_ERR(dchild))
833                 return 1;
834         if (d_mountpoint(dchild) ||
835             fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
836             !dchild->d_inode)
837                 rv = 1;
838         dput(dchild);
839         return rv;
840 }
841
842 /*
843  * Encode a directory entry. This one works for both normal readdir
844  * and readdirplus.
845  * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
846  * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
847  * 
848  * The readdirplus baggage is 1+21 words for post_op_attr, plus the
849  * file handle.
850  */
851
852 #define NFS3_ENTRY_BAGGAGE      (2 + 1 + 2 + 1)
853 #define NFS3_ENTRYPLUS_BAGGAGE  (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
854 static int
855 encode_entry(struct readdir_cd *ccd, const char *name,
856              int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
857 {
858         struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
859                                                         common);
860         u32             *p = cd->buffer;
861         caddr_t         curr_page_addr = NULL;
862         int             pn;             /* current page number */
863         int             slen;           /* string (name) length */
864         int             elen;           /* estimated entry length in words */
865         int             num_entry_words = 0;    /* actual number of words */
866
867         if (cd->offset) {
868                 u64 offset64 = offset;
869
870                 if (unlikely(cd->offset1)) {
871                         /* we ended up with offset on a page boundary */
872                         *cd->offset = htonl(offset64 >> 32);
873                         *cd->offset1 = htonl(offset64 & 0xffffffff);
874                         cd->offset1 = NULL;
875                 } else {
876                         xdr_encode_hyper(cd->offset, (u64) offset);
877                 }
878         }
879
880         /*
881         dprintk("encode_entry(%.*s @%ld%s)\n",
882                 namlen, name, (long) offset, plus? " plus" : "");
883          */
884
885         /* truncate filename if too long */
886         if (namlen > NFS3_MAXNAMLEN)
887                 namlen = NFS3_MAXNAMLEN;
888
889         slen = XDR_QUADLEN(namlen);
890         elen = slen + NFS3_ENTRY_BAGGAGE
891                 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
892
893         if (cd->buflen < elen) {
894                 cd->common.err = nfserr_toosmall;
895                 return -EINVAL;
896         }
897
898         /* determine which page in rq_respages[] we are currently filling */
899         for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
900                 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
901
902                 if (((caddr_t)cd->buffer >= curr_page_addr) &&
903                     ((caddr_t)cd->buffer <  curr_page_addr + PAGE_SIZE))
904                         break;
905         }
906
907         if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
908                 /* encode entry in current page */
909
910                 p = encode_entry_baggage(cd, p, name, namlen, ino);
911
912                 /* throw in readdirplus baggage */
913                 if (plus) {
914                         struct svc_fh   fh;
915
916                         if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
917                                 *p++ = 0;
918                                 *p++ = 0;
919                         } else
920                                 p = encode_entryplus_baggage(cd, p, &fh);
921                 }
922                 num_entry_words = p - cd->buffer;
923         } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
924                 /* temporarily encode entry into next page, then move back to
925                  * current and next page in rq_respages[] */
926                 u32 *p1, *tmp;
927                 int len1, len2;
928
929                 /* grab next page for temporary storage of entry */
930                 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
931
932                 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
933
934                 /* throw in readdirplus baggage */
935                 if (plus) {
936                         struct svc_fh   fh;
937
938                         if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
939                                 /* zero out the filehandle */
940                                 *p1++ = 0;
941                                 *p1++ = 0;
942                         } else
943                                 p1 = encode_entryplus_baggage(cd, p1, &fh);
944                 }
945
946                 /* determine entry word length and lengths to go in pages */
947                 num_entry_words = p1 - tmp;
948                 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
949                 if ((num_entry_words << 2) < len1) {
950                         /* the actual number of words in the entry is less
951                          * than elen and can still fit in the current page
952                          */
953                         memmove(p, tmp, num_entry_words << 2);
954                         p += num_entry_words;
955
956                         /* update offset */
957                         cd->offset = cd->buffer + (cd->offset - tmp);
958                 } else {
959                         unsigned int offset_r = (cd->offset - tmp) << 2;
960
961                         /* update pointer to offset location.
962                          * This is a 64bit quantity, so we need to
963                          * deal with 3 cases:
964                          *  -   entirely in first page
965                          *  -   entirely in second page
966                          *  -   4 bytes in each page
967                          */
968                         if (offset_r + 8 <= len1) {
969                                 cd->offset = p + (cd->offset - tmp);
970                         } else if (offset_r >= len1) {
971                                 cd->offset -= len1 >> 2;
972                         } else {
973                                 /* sitting on the fence */
974                                 BUG_ON(offset_r != len1 - 4);
975                                 cd->offset = p + (cd->offset - tmp);
976                                 cd->offset1 = tmp;
977                         }
978
979                         len2 = (num_entry_words << 2) - len1;
980
981                         /* move from temp page to current and next pages */
982                         memmove(p, tmp, len1);
983                         memmove(tmp, (caddr_t)tmp+len1, len2);
984
985                         p = tmp + (len2 >> 2);
986                 }
987         }
988         else {
989                 cd->common.err = nfserr_toosmall;
990                 return -EINVAL;
991         }
992
993         cd->buflen -= num_entry_words;
994         cd->buffer = p;
995         cd->common.err = nfs_ok;
996         return 0;
997
998 }
999
1000 int
1001 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
1002                      int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1003 {
1004         return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1005 }
1006
1007 int
1008 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
1009                           int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1010 {
1011         return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1012 }
1013
1014 /* FSSTAT */
1015 int
1016 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1017                                         struct nfsd3_fsstatres *resp)
1018 {
1019         struct kstatfs  *s = &resp->stats;
1020         u64             bs = s->f_bsize;
1021
1022         *p++ = xdr_zero;        /* no post_op_attr */
1023
1024         if (resp->status == 0) {
1025                 p = xdr_encode_hyper(p, bs * s->f_blocks);      /* total bytes */
1026                 p = xdr_encode_hyper(p, bs * s->f_bfree);       /* free bytes */
1027                 p = xdr_encode_hyper(p, bs * s->f_bavail);      /* user available bytes */
1028                 p = xdr_encode_hyper(p, s->f_files);    /* total inodes */
1029                 p = xdr_encode_hyper(p, s->f_ffree);    /* free inodes */
1030                 p = xdr_encode_hyper(p, s->f_ffree);    /* user available inodes */
1031                 *p++ = htonl(resp->invarsec);   /* mean unchanged time */
1032         }
1033         return xdr_ressize_check(rqstp, p);
1034 }
1035
1036 /* FSINFO */
1037 int
1038 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1039                                         struct nfsd3_fsinfores *resp)
1040 {
1041         *p++ = xdr_zero;        /* no post_op_attr */
1042
1043         if (resp->status == 0) {
1044                 *p++ = htonl(resp->f_rtmax);
1045                 *p++ = htonl(resp->f_rtpref);
1046                 *p++ = htonl(resp->f_rtmult);
1047                 *p++ = htonl(resp->f_wtmax);
1048                 *p++ = htonl(resp->f_wtpref);
1049                 *p++ = htonl(resp->f_wtmult);
1050                 *p++ = htonl(resp->f_dtpref);
1051                 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1052                 *p++ = xdr_one;
1053                 *p++ = xdr_zero;
1054                 *p++ = htonl(resp->f_properties);
1055         }
1056
1057         return xdr_ressize_check(rqstp, p);
1058 }
1059
1060 /* PATHCONF */
1061 int
1062 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1063                                         struct nfsd3_pathconfres *resp)
1064 {
1065         *p++ = xdr_zero;        /* no post_op_attr */
1066
1067         if (resp->status == 0) {
1068                 *p++ = htonl(resp->p_link_max);
1069                 *p++ = htonl(resp->p_name_max);
1070                 *p++ = htonl(resp->p_no_trunc);
1071                 *p++ = htonl(resp->p_chown_restricted);
1072                 *p++ = htonl(resp->p_case_insensitive);
1073                 *p++ = htonl(resp->p_case_preserving);
1074         }
1075
1076         return xdr_ressize_check(rqstp, p);
1077 }
1078
1079 /* COMMIT */
1080 int
1081 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1082                                         struct nfsd3_commitres *resp)
1083 {
1084         p = encode_wcc_data(rqstp, p, &resp->fh);
1085         /* Write verifier */
1086         if (resp->status == 0) {
1087                 *p++ = htonl(nfssvc_boot.tv_sec);
1088                 *p++ = htonl(nfssvc_boot.tv_usec);
1089         }
1090         return xdr_ressize_check(rqstp, p);
1091 }
1092
1093 /*
1094  * XDR release functions
1095  */
1096 int
1097 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1098                                         struct nfsd3_attrstat *resp)
1099 {
1100         fh_put(&resp->fh);
1101         return 1;
1102 }
1103
1104 int
1105 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1106                                         struct nfsd3_fhandle_pair *resp)
1107 {
1108         fh_put(&resp->fh1);
1109         fh_put(&resp->fh2);
1110         return 1;
1111 }