Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / fs / nfsd / nfs3xdr.c
blob821db21ba072cd7dbf7a70c69bd8f0ee378758f6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * XDR support for nfsd/protocol version 3.
5 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
7 * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
8 */
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"
17 #define NFSDDBG_FACILITY NFSDDBG_XDR
21 * Mapping of S_IF* types to NFS file types
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,
32 * XDR functions for basic NFS types
34 static __be32 *
35 encode_time3(__be32 *p, struct timespec64 *time)
37 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
38 return p;
41 static __be32 *
42 decode_time3(__be32 *p, struct timespec64 *time)
44 time->tv_sec = ntohl(*p++);
45 time->tv_nsec = ntohl(*p++);
46 return p;
49 static __be32 *
50 decode_fh(__be32 *p, struct svc_fh *fhp)
52 unsigned int size;
53 fh_init(fhp, NFS3_FHSIZE);
54 size = ntohl(*p++);
55 if (size > NFS3_FHSIZE)
56 return NULL;
58 memcpy(&fhp->fh_handle.fh_base, p, size);
59 fhp->fh_handle.fh_size = size;
60 return p + XDR_QUADLEN(size);
63 /* Helper function for NFSv3 ACL code */
64 __be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
66 return decode_fh(p, fhp);
69 static __be32 *
70 encode_fh(__be32 *p, struct svc_fh *fhp)
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);
80 * Decode a file name and make sure that the path contains
81 * no slashes or null bytes.
83 static __be32 *
84 decode_filename(__be32 *p, char **namp, unsigned int *lenp)
86 char *name;
87 unsigned int i;
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;
96 return p;
99 static __be32 *
100 decode_sattr3(__be32 *p, struct iattr *iap, struct user_namespace *userns)
102 u32 tmp;
104 iap->ia_valid = 0;
106 if (*p++) {
107 iap->ia_valid |= ATTR_MODE;
108 iap->ia_mode = ntohl(*p++);
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;
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;
120 if (*p++) {
121 u64 newsize;
123 iap->ia_valid |= ATTR_SIZE;
124 p = xdr_decode_hyper(p, &newsize);
125 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
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++);
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++);
141 return p;
144 static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
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;
162 return p;
165 static __be32 *
166 encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
167 struct kstat *stat)
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);
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);
189 return p;
192 static __be32 *
193 encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
195 /* Attributes to follow */
196 *p++ = xdr_one;
197 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
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.
205 static __be32 *
206 encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
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;
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);
220 *p++ = xdr_zero;
221 return p;
224 /* Helper for NFSv3 ACLs */
225 __be32 *
226 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
228 return encode_post_op_attr(rqstp, p, fhp);
232 * Enocde weak cache consistency data
234 static __be32 *
235 encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
237 struct dentry *dentry = fhp->fh_dentry;
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;
248 return encode_saved_post_attr(rqstp, p, fhp);
250 /* no pre- or post-attrs */
251 *p++ = xdr_zero;
252 return encode_post_op_attr(rqstp, p, fhp);
256 * Fill in the pre_op attr for the wcc data
258 void fill_pre_wcc(struct svc_fh *fhp)
260 struct inode *inode;
261 struct kstat stat;
262 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
263 __be32 err;
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;
275 if (v4)
276 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
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;
285 * Fill in the post_op attr for the wcc data
287 void fill_post_wcc(struct svc_fh *fhp)
289 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
290 struct inode *inode = d_inode(fhp->fh_dentry);
291 __be32 err;
293 if (fhp->fh_no_wcc)
294 return;
296 if (fhp->fh_post_saved)
297 printk("nfsd: inode locked twice during operation.\n");
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);
311 * XDR decode functions
315 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p)
317 struct nfsd_fhandle *args = rqstp->rq_argp;
319 p = decode_fh(p, &args->fh);
320 if (!p)
321 return 0;
322 return xdr_argsize_check(rqstp, p);
326 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
328 struct nfsd3_sattrargs *args = rqstp->rq_argp;
330 p = decode_fh(p, &args->fh);
331 if (!p)
332 return 0;
333 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
335 if ((args->check_guard = ntohl(*p++)) != 0) {
336 struct timespec64 time;
337 p = decode_time3(p, &time);
338 args->guardtime = time.tv_sec;
341 return xdr_argsize_check(rqstp, p);
345 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
347 struct nfsd3_diropargs *args = rqstp->rq_argp;
349 if (!(p = decode_fh(p, &args->fh))
350 || !(p = decode_filename(p, &args->name, &args->len)))
351 return 0;
353 return xdr_argsize_check(rqstp, p);
357 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
359 struct nfsd3_accessargs *args = rqstp->rq_argp;
361 p = decode_fh(p, &args->fh);
362 if (!p)
363 return 0;
364 args->access = ntohl(*p++);
366 return xdr_argsize_check(rqstp, p);
370 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
372 struct nfsd3_readargs *args = rqstp->rq_argp;
373 unsigned int len;
374 int v;
375 u32 max_blocksize = svc_max_payload(rqstp);
377 p = decode_fh(p, &args->fh);
378 if (!p)
379 return 0;
380 p = xdr_decode_hyper(p, &args->offset);
382 args->count = ntohl(*p++);
383 len = min(args->count, max_blocksize);
385 /* set up the kvec */
386 v=0;
387 while (len > 0) {
388 struct page *p = *(rqstp->rq_next_page++);
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++;
395 args->vlen = v;
396 return xdr_argsize_check(rqstp, p);
400 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
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;
408 p = decode_fh(p, &args->fh);
409 if (!p)
410 return 0;
411 p = xdr_decode_hyper(p, &args->offset);
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;
419 * The count must equal the amount of data passed.
421 if (args->count != args->len)
422 return 0;
425 * Check to make sure that we got the right number of
426 * bytes.
428 hdr = (void*)p - head->iov_base;
429 dlen = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len - hdr;
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.
438 if (dlen < XDR_QUADLEN(len)*4)
439 return 0;
441 if (args->count > max_blocksize) {
442 args->count = max_blocksize;
443 len = args->len = max_blocksize;
446 args->first.iov_base = (void *)p;
447 args->first.iov_len = head->iov_len - hdr;
448 return 1;
452 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
454 struct nfsd3_createargs *args = rqstp->rq_argp;
456 if (!(p = decode_fh(p, &args->fh))
457 || !(p = decode_filename(p, &args->name, &args->len)))
458 return 0;
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;
473 return xdr_argsize_check(rqstp, p);
477 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
479 struct nfsd3_createargs *args = rqstp->rq_argp;
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));
486 return xdr_argsize_check(rqstp, p);
490 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
492 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
493 char *base = (char *)p;
494 size_t dlen;
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));
501 args->tlen = ntohl(*p++);
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;
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;
515 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
517 struct nfsd3_mknodargs *args = rqstp->rq_argp;
519 if (!(p = decode_fh(p, &args->fh))
520 || !(p = decode_filename(p, &args->name, &args->len)))
521 return 0;
523 args->ftype = ntohl(*p++);
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));
529 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
530 args->major = ntohl(*p++);
531 args->minor = ntohl(*p++);
534 return xdr_argsize_check(rqstp, p);
538 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
540 struct nfsd3_renameargs *args = rqstp->rq_argp;
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;
548 return xdr_argsize_check(rqstp, p);
552 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p)
554 struct nfsd3_readlinkargs *args = rqstp->rq_argp;
556 p = decode_fh(p, &args->fh);
557 if (!p)
558 return 0;
559 args->buffer = page_address(*(rqstp->rq_next_page++));
561 return xdr_argsize_check(rqstp, p);
565 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
567 struct nfsd3_linkargs *args = rqstp->rq_argp;
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;
574 return xdr_argsize_check(rqstp, p);
578 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
580 struct nfsd3_readdirargs *args = rqstp->rq_argp;
581 int len;
582 u32 max_blocksize = svc_max_payload(rqstp);
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);
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;
600 return xdr_argsize_check(rqstp, p);
604 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
606 struct nfsd3_readdirargs *args = rqstp->rq_argp;
607 int len;
608 u32 max_blocksize = svc_max_payload(rqstp);
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++);
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;
626 return xdr_argsize_check(rqstp, p);
630 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
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++);
639 return xdr_argsize_check(rqstp, p);
643 * XDR encode functions
646 /* GETATTR */
648 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p)
650 struct nfsd3_attrstat *resp = rqstp->rq_resp;
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);
658 return xdr_ressize_check(rqstp, p);
661 /* SETATTR, REMOVE, RMDIR */
663 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
665 struct nfsd3_attrstat *resp = rqstp->rq_resp;
667 *p++ = resp->status;
668 p = encode_wcc_data(rqstp, p, &resp->fh);
669 return xdr_ressize_check(rqstp, p);
672 /* LOOKUP */
674 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
676 struct nfsd3_diropres *resp = rqstp->rq_resp;
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);
683 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
684 return xdr_ressize_check(rqstp, p);
687 /* ACCESS */
689 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
691 struct nfsd3_accessres *resp = rqstp->rq_resp;
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);
700 /* READLINK */
702 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
704 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
705 struct kvec *head = rqstp->rq_res.head;
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);
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);
726 /* READ */
728 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
730 struct nfsd3_readres *resp = rqstp->rq_resp;
731 struct kvec *head = rqstp->rq_res.head;
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);
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);
756 /* WRITE */
758 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
760 struct nfsd3_writeres *resp = rqstp->rq_resp;
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];
770 return xdr_ressize_check(rqstp, p);
773 /* CREATE, MKDIR, SYMLINK, MKNOD */
775 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
777 struct nfsd3_diropres *resp = rqstp->rq_resp;
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);
785 p = encode_wcc_data(rqstp, p, &resp->dirfh);
786 return xdr_ressize_check(rqstp, p);
789 /* RENAME */
791 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
793 struct nfsd3_renameres *resp = rqstp->rq_resp;
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);
801 /* LINK */
803 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
805 struct nfsd3_linkres *resp = rqstp->rq_resp;
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);
813 /* READDIR */
815 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
817 struct nfsd3_readdirres *resp = rqstp->rq_resp;
819 *p++ = resp->status;
820 p = encode_post_op_attr(rqstp, p, &resp->fh);
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;
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);
840 static __be32 *
841 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
842 int namlen, u64 ino)
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 */
848 cd->offset = p; /* remember pointer */
849 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
851 return p;
854 static __be32
855 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
856 const char *name, int namlen, u64 ino)
858 struct svc_export *exp;
859 struct dentry *dparent, *dchild;
860 __be32 rv = nfserr_noent;
862 dparent = cd->fh.fh_dentry;
863 exp = cd->fh.fh_export;
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;
887 static __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, int namlen, u64 ino)
889 struct svc_fh *fh = &cd->scratch;
890 __be32 err;
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;
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;
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.
913 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
914 * file handle.
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)
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 */
932 if (cd->offset) {
933 u64 offset64 = offset;
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);
943 cd->offset = NULL;
947 dprintk("encode_entry(%.*s @%ld%s)\n",
948 namlen, name, (long) offset, plus? " plus" : "");
951 /* truncate filename if too long */
952 namlen = min(namlen, NFS3_MAXNAMLEN);
954 slen = XDR_QUADLEN(namlen);
955 elen = slen + NFS3_ENTRY_BAGGAGE
956 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
958 if (cd->buflen < elen) {
959 cd->common.err = nfserr_toosmall;
960 return -EINVAL;
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);
968 if (((caddr_t)cd->buffer >= curr_page_addr) &&
969 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
970 break;
973 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
974 /* encode entry in current page */
976 p = encode_entry_baggage(cd, p, name, namlen, ino);
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;
987 /* grab next page for temporary storage of entry */
988 p1 = tmp = page_address(*(page+1));
990 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
992 if (plus)
993 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
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
1002 memmove(p, tmp, num_entry_words << 2);
1003 p += num_entry_words;
1005 /* update offset */
1006 cd->offset = cd->buffer + (cd->offset - tmp);
1007 } else {
1008 unsigned int offset_r = (cd->offset - tmp) << 2;
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
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;
1028 len2 = (num_entry_words << 2) - len1;
1030 /* move from temp page to current and next pages */
1031 memmove(p, tmp, len1);
1032 memmove(tmp, (caddr_t)tmp+len1, len2);
1034 p = tmp + (len2 >> 2);
1037 else {
1038 cd->common.err = nfserr_toosmall;
1039 return -EINVAL;
1042 cd->buflen -= num_entry_words;
1043 cd->buffer = p;
1044 cd->common.err = nfs_ok;
1045 return 0;
1050 nfs3svc_encode_entry(void *cd, const char *name,
1051 int namlen, loff_t offset, u64 ino, unsigned int d_type)
1053 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1057 nfs3svc_encode_entry_plus(void *cd, const char *name,
1058 int namlen, loff_t offset, u64 ino,
1059 unsigned int d_type)
1061 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1064 /* FSSTAT */
1066 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
1068 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
1069 struct kstatfs *s = &resp->stats;
1070 u64 bs = s->f_bsize;
1072 *p++ = resp->status;
1073 *p++ = xdr_zero; /* no post_op_attr */
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 */
1084 return xdr_ressize_check(rqstp, p);
1087 /* FSINFO */
1089 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
1091 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1093 *p++ = resp->status;
1094 *p++ = xdr_zero; /* no post_op_attr */
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);
1110 return xdr_ressize_check(rqstp, p);
1113 /* PATHCONF */
1115 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
1117 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1119 *p++ = resp->status;
1120 *p++ = xdr_zero; /* no post_op_attr */
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);
1131 return xdr_ressize_check(rqstp, p);
1134 /* COMMIT */
1136 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
1138 struct nfsd3_commitres *resp = rqstp->rq_resp;
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];
1147 return xdr_ressize_check(rqstp, p);
1151 * XDR release functions
1153 void
1154 nfs3svc_release_fhandle(struct svc_rqst *rqstp)
1156 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1158 fh_put(&resp->fh);
1161 void
1162 nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
1164 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1166 fh_put(&resp->fh1);
1167 fh_put(&resp->fh2);