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