1 // SPDX-License-Identifier: GPL-2.0
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()!
10 #include <linux/namei.h>
11 #include <linux/sunrpc/svc_xprt.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 * Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6)
36 encode_time3(__be32 *p, struct timespec64 *time)
38 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
43 svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep)
47 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
50 timep->tv_sec = be32_to_cpup(p++);
51 timep->tv_nsec = be32_to_cpup(p);
57 * svcxdr_decode_nfs_fh3 - Decode an NFSv3 file handle
58 * @xdr: XDR stream positioned at an undecoded NFSv3 FH
59 * @fhp: OUT: filled-in server file handle
62 * %false: The encoded file handle was not valid
63 * %true: @fhp has been initialized
66 svcxdr_decode_nfs_fh3(struct xdr_stream *xdr, struct svc_fh *fhp)
71 if (xdr_stream_decode_u32(xdr, &size) < 0)
73 if (size == 0 || size > NFS3_FHSIZE)
75 p = xdr_inline_decode(xdr, size);
78 fh_init(fhp, NFS3_FHSIZE);
79 fhp->fh_handle.fh_size = size;
80 memcpy(&fhp->fh_handle.fh_base, p, size);
86 encode_fh(__be32 *p, struct svc_fh *fhp)
88 unsigned int size = fhp->fh_handle.fh_size;
90 if (size) p[XDR_QUADLEN(size)-1]=0;
91 memcpy(p, &fhp->fh_handle.fh_base, size);
92 return p + XDR_QUADLEN(size);
96 svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len)
102 if (xdr_stream_decode_u32(xdr, &size) < 0)
104 if (size == 0 || size > NFS3_MAXNAMLEN)
106 p = xdr_inline_decode(xdr, size);
112 for (i = 0, c = *name; i < size; i++, c++) {
113 if (*c == '\0' || *c == '/')
121 svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp,
122 char **name, unsigned int *len)
124 return svcxdr_decode_nfs_fh3(xdr, fhp) &&
125 svcxdr_decode_filename3(xdr, name, len);
129 svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
136 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
141 if (xdr_stream_decode_u32(xdr, &mode) < 0)
143 iap->ia_valid |= ATTR_MODE;
146 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
151 if (xdr_stream_decode_u32(xdr, &uid) < 0)
153 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid);
154 if (uid_valid(iap->ia_uid))
155 iap->ia_valid |= ATTR_UID;
157 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
162 if (xdr_stream_decode_u32(xdr, &gid) < 0)
164 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid);
165 if (gid_valid(iap->ia_gid))
166 iap->ia_valid |= ATTR_GID;
168 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
173 if (xdr_stream_decode_u64(xdr, &newsize) < 0)
175 iap->ia_valid |= ATTR_SIZE;
176 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
178 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
183 case SET_TO_SERVER_TIME:
184 iap->ia_valid |= ATTR_ATIME;
186 case SET_TO_CLIENT_TIME:
187 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime))
189 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
194 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
199 case SET_TO_SERVER_TIME:
200 iap->ia_valid |= ATTR_MTIME;
202 case SET_TO_CLIENT_TIME:
203 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime))
205 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
215 svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args)
220 if (xdr_stream_decode_bool(xdr, &check) < 0)
223 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
226 args->check_guard = 1;
227 args->guardtime = be32_to_cpup(p);
229 args->check_guard = 0;
235 svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
239 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
242 args->major = be32_to_cpup(p++);
243 args->minor = be32_to_cpup(p);
249 svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
250 struct nfsd3_mknodargs *args)
252 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
253 svcxdr_decode_specdata3(xdr, args);
256 static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
259 switch(fsid_source(fhp)) {
262 p = xdr_encode_hyper(p, (u64)huge_encode_dev
263 (fhp->fh_dentry->d_sb->s_dev));
265 case FSIDSOURCE_FSID:
266 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
268 case FSIDSOURCE_UUID:
269 f = ((u64*)fhp->fh_export->ex_uuid)[0];
270 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
271 p = xdr_encode_hyper(p, f);
278 encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
281 struct user_namespace *userns = nfsd_user_namespace(rqstp);
282 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
283 *p++ = htonl((u32) (stat->mode & S_IALLUGO));
284 *p++ = htonl((u32) stat->nlink);
285 *p++ = htonl((u32) from_kuid_munged(userns, stat->uid));
286 *p++ = htonl((u32) from_kgid_munged(userns, stat->gid));
287 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
288 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
290 p = xdr_encode_hyper(p, (u64) stat->size);
292 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
293 *p++ = htonl((u32) MAJOR(stat->rdev));
294 *p++ = htonl((u32) MINOR(stat->rdev));
295 p = encode_fsid(p, fhp);
296 p = xdr_encode_hyper(p, stat->ino);
297 p = encode_time3(p, &stat->atime);
298 p = encode_time3(p, &stat->mtime);
299 p = encode_time3(p, &stat->ctime);
305 encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
307 /* Attributes to follow */
309 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
313 * Encode post-operation attributes.
314 * The inode may be NULL if the call failed because of a stale file
315 * handle. In this case, no attributes are returned.
318 encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
320 struct dentry *dentry = fhp->fh_dentry;
321 if (!fhp->fh_no_wcc && dentry && d_really_is_positive(dentry)) {
325 err = fh_getattr(fhp, &stat);
327 *p++ = xdr_one; /* attributes follow */
328 lease_get_mtime(d_inode(dentry), &stat.mtime);
329 return encode_fattr3(rqstp, p, fhp, &stat);
336 /* Helper for NFSv3 ACLs */
338 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
340 return encode_post_op_attr(rqstp, p, fhp);
344 * Enocde weak cache consistency data
347 encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
349 struct dentry *dentry = fhp->fh_dentry;
351 if (dentry && d_really_is_positive(dentry) && fhp->fh_post_saved) {
352 if (fhp->fh_pre_saved) {
354 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
355 p = encode_time3(p, &fhp->fh_pre_mtime);
356 p = encode_time3(p, &fhp->fh_pre_ctime);
360 return encode_saved_post_attr(rqstp, p, fhp);
362 /* no pre- or post-attrs */
364 return encode_post_op_attr(rqstp, p, fhp);
367 static bool fs_supports_change_attribute(struct super_block *sb)
369 return sb->s_flags & SB_I_VERSION || sb->s_export_op->fetch_iversion;
373 * Fill in the pre_op attr for the wcc data
375 void fill_pre_wcc(struct svc_fh *fhp)
379 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
381 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
383 inode = d_inode(fhp->fh_dentry);
384 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
385 __be32 err = fh_getattr(fhp, &stat);
388 /* Grab the times from inode anyway */
389 stat.mtime = inode->i_mtime;
390 stat.ctime = inode->i_ctime;
391 stat.size = inode->i_size;
393 fhp->fh_pre_mtime = stat.mtime;
394 fhp->fh_pre_ctime = stat.ctime;
395 fhp->fh_pre_size = stat.size;
398 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
400 fhp->fh_pre_saved = true;
404 * Fill in the post_op attr for the wcc data
406 void fill_post_wcc(struct svc_fh *fhp)
408 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
409 struct inode *inode = d_inode(fhp->fh_dentry);
414 if (fhp->fh_post_saved)
415 printk("nfsd: inode locked twice during operation.\n");
417 fhp->fh_post_saved = true;
419 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
420 __be32 err = fh_getattr(fhp, &fhp->fh_post_attr);
423 fhp->fh_post_saved = false;
424 fhp->fh_post_attr.ctime = inode->i_ctime;
428 fhp->fh_post_change =
429 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
433 * XDR decode functions
437 nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
439 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
440 struct nfsd_fhandle *args = rqstp->rq_argp;
442 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
446 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
448 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
449 struct nfsd3_sattrargs *args = rqstp->rq_argp;
451 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
452 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
453 svcxdr_decode_sattrguard3(xdr, args);
457 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
459 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
460 struct nfsd3_diropargs *args = rqstp->rq_argp;
462 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
466 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
468 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
469 struct nfsd3_accessargs *args = rqstp->rq_argp;
471 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
473 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
480 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
482 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
483 struct nfsd3_readargs *args = rqstp->rq_argp;
485 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
487 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
489 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
496 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
498 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
499 struct nfsd3_writeargs *args = rqstp->rq_argp;
500 u32 max_blocksize = svc_max_payload(rqstp);
501 struct kvec *head = rqstp->rq_arg.head;
502 struct kvec *tail = rqstp->rq_arg.tail;
505 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
507 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
509 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
511 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
515 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
519 if (args->count != args->len)
521 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
522 remaining -= xdr_stream_pos(xdr);
523 if (remaining < xdr_align_size(args->len))
525 if (args->count > max_blocksize) {
526 args->count = max_blocksize;
527 args->len = max_blocksize;
530 args->first.iov_base = xdr->p;
531 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
537 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
539 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
540 struct nfsd3_createargs *args = rqstp->rq_argp;
542 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
544 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
546 switch (args->createmode) {
547 case NFS3_CREATE_UNCHECKED:
548 case NFS3_CREATE_GUARDED:
549 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
550 case NFS3_CREATE_EXCLUSIVE:
551 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
562 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
564 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
565 struct nfsd3_createargs *args = rqstp->rq_argp;
567 return svcxdr_decode_diropargs3(xdr, &args->fh,
568 &args->name, &args->len) &&
569 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
573 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
575 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
576 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
577 struct kvec *head = rqstp->rq_arg.head;
578 struct kvec *tail = rqstp->rq_arg.tail;
581 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
583 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
585 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
589 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
590 remaining -= xdr_stream_pos(xdr);
591 if (remaining < xdr_align_size(args->tlen))
594 args->first.iov_base = xdr->p;
595 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
601 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
603 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
604 struct nfsd3_mknodargs *args = rqstp->rq_argp;
606 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
608 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
610 switch (args->ftype) {
613 return svcxdr_decode_devicedata3(rqstp, xdr, args);
616 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
620 /* Valid XDR but illegal file types */
630 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
632 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
633 struct nfsd3_renameargs *args = rqstp->rq_argp;
635 return svcxdr_decode_diropargs3(xdr, &args->ffh,
636 &args->fname, &args->flen) &&
637 svcxdr_decode_diropargs3(xdr, &args->tfh,
638 &args->tname, &args->tlen);
642 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
644 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
645 struct nfsd3_linkargs *args = rqstp->rq_argp;
647 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
648 svcxdr_decode_diropargs3(xdr, &args->tfh,
649 &args->tname, &args->tlen);
653 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
655 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
656 struct nfsd3_readdirargs *args = rqstp->rq_argp;
658 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
660 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
662 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
665 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
672 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
674 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
675 struct nfsd3_readdirargs *args = rqstp->rq_argp;
678 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
680 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
682 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
685 /* dircount is ignored */
686 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
688 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
695 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
697 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
698 struct nfsd3_commitargs *args = rqstp->rq_argp;
700 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
702 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
704 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
711 * XDR encode functions
716 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p)
718 struct nfsd3_attrstat *resp = rqstp->rq_resp;
721 if (resp->status == 0) {
722 lease_get_mtime(d_inode(resp->fh.fh_dentry),
724 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
726 return xdr_ressize_check(rqstp, p);
729 /* SETATTR, REMOVE, RMDIR */
731 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
733 struct nfsd3_attrstat *resp = rqstp->rq_resp;
736 p = encode_wcc_data(rqstp, p, &resp->fh);
737 return xdr_ressize_check(rqstp, p);
742 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
744 struct nfsd3_diropres *resp = rqstp->rq_resp;
747 if (resp->status == 0) {
748 p = encode_fh(p, &resp->fh);
749 p = encode_post_op_attr(rqstp, p, &resp->fh);
751 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
752 return xdr_ressize_check(rqstp, p);
757 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
759 struct nfsd3_accessres *resp = rqstp->rq_resp;
762 p = encode_post_op_attr(rqstp, p, &resp->fh);
763 if (resp->status == 0)
764 *p++ = htonl(resp->access);
765 return xdr_ressize_check(rqstp, p);
770 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
772 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
773 struct kvec *head = rqstp->rq_res.head;
776 p = encode_post_op_attr(rqstp, p, &resp->fh);
777 if (resp->status == 0) {
778 *p++ = htonl(resp->len);
779 xdr_ressize_check(rqstp, p);
780 rqstp->rq_res.page_len = resp->len;
782 /* need to pad the tail */
783 rqstp->rq_res.tail[0].iov_base = p;
785 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
787 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len))
791 return xdr_ressize_check(rqstp, p);
796 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
798 struct nfsd3_readres *resp = rqstp->rq_resp;
799 struct kvec *head = rqstp->rq_res.head;
802 p = encode_post_op_attr(rqstp, p, &resp->fh);
803 if (resp->status == 0) {
804 *p++ = htonl(resp->count);
805 *p++ = htonl(resp->eof);
806 *p++ = htonl(resp->count); /* xdr opaque count */
807 xdr_ressize_check(rqstp, p);
808 /* now update rqstp->rq_res to reflect data as well */
809 rqstp->rq_res.page_len = resp->count;
810 if (resp->count & 3) {
811 /* need to pad the tail */
812 rqstp->rq_res.tail[0].iov_base = p;
814 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
816 if (svc_encode_result_payload(rqstp, head->iov_len,
821 return xdr_ressize_check(rqstp, p);
826 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
828 struct nfsd3_writeres *resp = rqstp->rq_resp;
831 p = encode_wcc_data(rqstp, p, &resp->fh);
832 if (resp->status == 0) {
833 *p++ = htonl(resp->count);
834 *p++ = htonl(resp->committed);
835 *p++ = resp->verf[0];
836 *p++ = resp->verf[1];
838 return xdr_ressize_check(rqstp, p);
841 /* CREATE, MKDIR, SYMLINK, MKNOD */
843 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
845 struct nfsd3_diropres *resp = rqstp->rq_resp;
848 if (resp->status == 0) {
850 p = encode_fh(p, &resp->fh);
851 p = encode_post_op_attr(rqstp, p, &resp->fh);
853 p = encode_wcc_data(rqstp, p, &resp->dirfh);
854 return xdr_ressize_check(rqstp, p);
859 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
861 struct nfsd3_renameres *resp = rqstp->rq_resp;
864 p = encode_wcc_data(rqstp, p, &resp->ffh);
865 p = encode_wcc_data(rqstp, p, &resp->tfh);
866 return xdr_ressize_check(rqstp, p);
871 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
873 struct nfsd3_linkres *resp = rqstp->rq_resp;
876 p = encode_post_op_attr(rqstp, p, &resp->fh);
877 p = encode_wcc_data(rqstp, p, &resp->tfh);
878 return xdr_ressize_check(rqstp, p);
883 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
885 struct nfsd3_readdirres *resp = rqstp->rq_resp;
888 p = encode_post_op_attr(rqstp, p, &resp->fh);
890 if (resp->status == 0) {
891 /* stupid readdir cookie */
892 memcpy(p, resp->verf, 8); p += 2;
893 xdr_ressize_check(rqstp, p);
894 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
895 return 1; /*No room for trailer */
896 rqstp->rq_res.page_len = (resp->count) << 2;
898 /* add the 'tail' to the end of the 'head' page - page 0. */
899 rqstp->rq_res.tail[0].iov_base = p;
900 *p++ = 0; /* no more entries */
901 *p++ = htonl(resp->common.err == nfserr_eof);
902 rqstp->rq_res.tail[0].iov_len = 2<<2;
905 return xdr_ressize_check(rqstp, p);
909 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
912 *p++ = xdr_one; /* mark entry present */
913 p = xdr_encode_hyper(p, ino); /* file id */
914 p = xdr_encode_array(p, name, namlen);/* name length & name */
916 cd->offset = p; /* remember pointer */
917 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
923 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
924 const char *name, int namlen, u64 ino)
926 struct svc_export *exp;
927 struct dentry *dparent, *dchild;
928 __be32 rv = nfserr_noent;
930 dparent = cd->fh.fh_dentry;
931 exp = cd->fh.fh_export;
933 if (isdotent(name, namlen)) {
935 dchild = dget_parent(dparent);
937 * Don't return filehandle for ".." if we're at
938 * the filesystem or export root:
940 if (dchild == dparent)
942 if (dparent == exp->ex_path.dentry)
945 dchild = dget(dparent);
947 dchild = lookup_positive_unlocked(name, dparent, namlen);
950 if (d_mountpoint(dchild))
952 if (dchild->d_inode->i_ino != ino)
954 rv = fh_compose(fhp, exp, dchild, &cd->fh);
960 static __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, int namlen, u64 ino)
962 struct svc_fh *fh = &cd->scratch;
965 fh_init(fh, NFS3_FHSIZE);
966 err = compose_entry_fh(cd, fh, name, namlen, ino);
972 p = encode_post_op_attr(cd->rqstp, p, fh);
973 *p++ = xdr_one; /* yes, a file handle follows */
974 p = encode_fh(p, fh);
981 * Encode a directory entry. This one works for both normal readdir
983 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
984 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
986 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
990 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
991 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
993 encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
994 loff_t offset, u64 ino, unsigned int d_type, int plus)
996 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
998 __be32 *p = cd->buffer;
999 caddr_t curr_page_addr = NULL;
1000 struct page ** page;
1001 int slen; /* string (name) length */
1002 int elen; /* estimated entry length in words */
1003 int num_entry_words = 0; /* actual number of words */
1006 u64 offset64 = offset;
1008 if (unlikely(cd->offset1)) {
1009 /* we ended up with offset on a page boundary */
1010 *cd->offset = htonl(offset64 >> 32);
1011 *cd->offset1 = htonl(offset64 & 0xffffffff);
1014 xdr_encode_hyper(cd->offset, offset64);
1020 dprintk("encode_entry(%.*s @%ld%s)\n",
1021 namlen, name, (long) offset, plus? " plus" : "");
1024 /* truncate filename if too long */
1025 namlen = min(namlen, NFS3_MAXNAMLEN);
1027 slen = XDR_QUADLEN(namlen);
1028 elen = slen + NFS3_ENTRY_BAGGAGE
1029 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
1031 if (cd->buflen < elen) {
1032 cd->common.err = nfserr_toosmall;
1036 /* determine which page in rq_respages[] we are currently filling */
1037 for (page = cd->rqstp->rq_respages + 1;
1038 page < cd->rqstp->rq_next_page; page++) {
1039 curr_page_addr = page_address(*page);
1041 if (((caddr_t)cd->buffer >= curr_page_addr) &&
1042 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
1046 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
1047 /* encode entry in current page */
1049 p = encode_entry_baggage(cd, p, name, namlen, ino);
1052 p = encode_entryplus_baggage(cd, p, name, namlen, ino);
1053 num_entry_words = p - cd->buffer;
1054 } else if (*(page+1) != NULL) {
1055 /* temporarily encode entry into next page, then move back to
1056 * current and next page in rq_respages[] */
1060 /* grab next page for temporary storage of entry */
1061 p1 = tmp = page_address(*(page+1));
1063 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
1066 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
1068 /* determine entry word length and lengths to go in pages */
1069 num_entry_words = p1 - tmp;
1070 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
1071 if ((num_entry_words << 2) < len1) {
1072 /* the actual number of words in the entry is less
1073 * than elen and can still fit in the current page
1075 memmove(p, tmp, num_entry_words << 2);
1076 p += num_entry_words;
1079 cd->offset = cd->buffer + (cd->offset - tmp);
1081 unsigned int offset_r = (cd->offset - tmp) << 2;
1083 /* update pointer to offset location.
1084 * This is a 64bit quantity, so we need to
1085 * deal with 3 cases:
1086 * - entirely in first page
1087 * - entirely in second page
1088 * - 4 bytes in each page
1090 if (offset_r + 8 <= len1) {
1091 cd->offset = p + (cd->offset - tmp);
1092 } else if (offset_r >= len1) {
1093 cd->offset -= len1 >> 2;
1095 /* sitting on the fence */
1096 BUG_ON(offset_r != len1 - 4);
1097 cd->offset = p + (cd->offset - tmp);
1101 len2 = (num_entry_words << 2) - len1;
1103 /* move from temp page to current and next pages */
1104 memmove(p, tmp, len1);
1105 memmove(tmp, (caddr_t)tmp+len1, len2);
1107 p = tmp + (len2 >> 2);
1111 cd->common.err = nfserr_toosmall;
1115 cd->buflen -= num_entry_words;
1117 cd->common.err = nfs_ok;
1123 nfs3svc_encode_entry(void *cd, const char *name,
1124 int namlen, loff_t offset, u64 ino, unsigned int d_type)
1126 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1130 nfs3svc_encode_entry_plus(void *cd, const char *name,
1131 int namlen, loff_t offset, u64 ino,
1132 unsigned int d_type)
1134 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1139 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
1141 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
1142 struct kstatfs *s = &resp->stats;
1143 u64 bs = s->f_bsize;
1145 *p++ = resp->status;
1146 *p++ = xdr_zero; /* no post_op_attr */
1148 if (resp->status == 0) {
1149 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1150 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1151 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1152 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1153 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1154 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1155 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1157 return xdr_ressize_check(rqstp, p);
1162 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
1164 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1166 *p++ = resp->status;
1167 *p++ = xdr_zero; /* no post_op_attr */
1169 if (resp->status == 0) {
1170 *p++ = htonl(resp->f_rtmax);
1171 *p++ = htonl(resp->f_rtpref);
1172 *p++ = htonl(resp->f_rtmult);
1173 *p++ = htonl(resp->f_wtmax);
1174 *p++ = htonl(resp->f_wtpref);
1175 *p++ = htonl(resp->f_wtmult);
1176 *p++ = htonl(resp->f_dtpref);
1177 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1180 *p++ = htonl(resp->f_properties);
1183 return xdr_ressize_check(rqstp, p);
1188 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
1190 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1192 *p++ = resp->status;
1193 *p++ = xdr_zero; /* no post_op_attr */
1195 if (resp->status == 0) {
1196 *p++ = htonl(resp->p_link_max);
1197 *p++ = htonl(resp->p_name_max);
1198 *p++ = htonl(resp->p_no_trunc);
1199 *p++ = htonl(resp->p_chown_restricted);
1200 *p++ = htonl(resp->p_case_insensitive);
1201 *p++ = htonl(resp->p_case_preserving);
1204 return xdr_ressize_check(rqstp, p);
1209 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
1211 struct nfsd3_commitres *resp = rqstp->rq_resp;
1213 *p++ = resp->status;
1214 p = encode_wcc_data(rqstp, p, &resp->fh);
1215 /* Write verifier */
1216 if (resp->status == 0) {
1217 *p++ = resp->verf[0];
1218 *p++ = resp->verf[1];
1220 return xdr_ressize_check(rqstp, p);
1224 * XDR release functions
1227 nfs3svc_release_fhandle(struct svc_rqst *rqstp)
1229 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1235 nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
1237 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;