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 * Force construction of an empty post-op attr
23 static const struct svc_fh nfs3svc_null_fh = {
28 * time_delta. {1, 0} means the server is accurate only
29 * to the nearest second.
31 static const struct timespec64 nfs3svc_time_delta = {
37 * Mapping of S_IF* types to NFS file types
39 static const u32 nfs3_ftypes[] = {
40 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
41 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
42 NF3REG, NF3BAD, NF3LNK, NF3BAD,
43 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
48 * Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6)
52 encode_nfstime3(__be32 *p, const struct timespec64 *time)
54 *p++ = cpu_to_be32((u32)time->tv_sec);
55 *p++ = cpu_to_be32(time->tv_nsec);
61 svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep)
65 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
68 timep->tv_sec = be32_to_cpup(p++);
69 timep->tv_nsec = be32_to_cpup(p);
75 * svcxdr_decode_nfs_fh3 - Decode an NFSv3 file handle
76 * @xdr: XDR stream positioned at an undecoded NFSv3 FH
77 * @fhp: OUT: filled-in server file handle
80 * %false: The encoded file handle was not valid
81 * %true: @fhp has been initialized
84 svcxdr_decode_nfs_fh3(struct xdr_stream *xdr, struct svc_fh *fhp)
89 if (xdr_stream_decode_u32(xdr, &size) < 0)
91 if (size == 0 || size > NFS3_FHSIZE)
93 p = xdr_inline_decode(xdr, size);
96 fh_init(fhp, NFS3_FHSIZE);
97 fhp->fh_handle.fh_size = size;
98 memcpy(&fhp->fh_handle.fh_base, p, size);
104 * svcxdr_encode_nfsstat3 - Encode an NFSv3 status code
106 * @status: status value to encode
109 * %false: Send buffer space was exhausted
113 svcxdr_encode_nfsstat3(struct xdr_stream *xdr, __be32 status)
117 p = xdr_reserve_space(xdr, sizeof(status));
126 svcxdr_encode_nfs_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
128 u32 size = fhp->fh_handle.fh_size;
131 p = xdr_reserve_space(xdr, XDR_UNIT + size);
134 *p++ = cpu_to_be32(size);
136 p[XDR_QUADLEN(size) - 1] = 0;
137 memcpy(p, &fhp->fh_handle.fh_base, size);
143 svcxdr_encode_post_op_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
145 if (xdr_stream_encode_item_present(xdr) < 0)
147 if (!svcxdr_encode_nfs_fh3(xdr, fhp))
154 svcxdr_encode_cookieverf3(struct xdr_stream *xdr, const __be32 *verf)
158 p = xdr_reserve_space(xdr, NFS3_COOKIEVERFSIZE);
161 memcpy(p, verf, NFS3_COOKIEVERFSIZE);
167 svcxdr_encode_writeverf3(struct xdr_stream *xdr, const __be32 *verf)
171 p = xdr_reserve_space(xdr, NFS3_WRITEVERFSIZE);
174 memcpy(p, verf, NFS3_WRITEVERFSIZE);
180 svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len)
186 if (xdr_stream_decode_u32(xdr, &size) < 0)
188 if (size == 0 || size > NFS3_MAXNAMLEN)
190 p = xdr_inline_decode(xdr, size);
196 for (i = 0, c = *name; i < size; i++, c++) {
197 if (*c == '\0' || *c == '/')
205 svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp,
206 char **name, unsigned int *len)
208 return svcxdr_decode_nfs_fh3(xdr, fhp) &&
209 svcxdr_decode_filename3(xdr, name, len);
213 svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
220 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
225 if (xdr_stream_decode_u32(xdr, &mode) < 0)
227 iap->ia_valid |= ATTR_MODE;
230 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
235 if (xdr_stream_decode_u32(xdr, &uid) < 0)
237 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid);
238 if (uid_valid(iap->ia_uid))
239 iap->ia_valid |= ATTR_UID;
241 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
246 if (xdr_stream_decode_u32(xdr, &gid) < 0)
248 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid);
249 if (gid_valid(iap->ia_gid))
250 iap->ia_valid |= ATTR_GID;
252 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
257 if (xdr_stream_decode_u64(xdr, &newsize) < 0)
259 iap->ia_valid |= ATTR_SIZE;
260 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
262 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
267 case SET_TO_SERVER_TIME:
268 iap->ia_valid |= ATTR_ATIME;
270 case SET_TO_CLIENT_TIME:
271 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime))
273 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
278 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
283 case SET_TO_SERVER_TIME:
284 iap->ia_valid |= ATTR_MTIME;
286 case SET_TO_CLIENT_TIME:
287 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime))
289 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
299 svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args)
304 if (xdr_stream_decode_bool(xdr, &check) < 0)
307 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
310 args->check_guard = 1;
311 args->guardtime = be32_to_cpup(p);
313 args->check_guard = 0;
319 svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
323 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
326 args->major = be32_to_cpup(p++);
327 args->minor = be32_to_cpup(p);
333 svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
334 struct nfsd3_mknodargs *args)
336 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
337 svcxdr_decode_specdata3(xdr, args);
341 svcxdr_encode_fattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
342 const struct svc_fh *fhp, const struct kstat *stat)
344 struct user_namespace *userns = nfsd_user_namespace(rqstp);
348 p = xdr_reserve_space(xdr, XDR_UNIT * 21);
352 *p++ = cpu_to_be32(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
353 *p++ = cpu_to_be32((u32)(stat->mode & S_IALLUGO));
354 *p++ = cpu_to_be32((u32)stat->nlink);
355 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
356 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
357 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN)
358 p = xdr_encode_hyper(p, (u64)NFS3_MAXPATHLEN);
360 p = xdr_encode_hyper(p, (u64)stat->size);
363 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
366 *p++ = cpu_to_be32((u32)MAJOR(stat->rdev));
367 *p++ = cpu_to_be32((u32)MINOR(stat->rdev));
369 switch(fsid_source(fhp)) {
370 case FSIDSOURCE_FSID:
371 fsid = (u64)fhp->fh_export->ex_fsid;
373 case FSIDSOURCE_UUID:
374 fsid = ((u64 *)fhp->fh_export->ex_uuid)[0];
375 fsid ^= ((u64 *)fhp->fh_export->ex_uuid)[1];
378 fsid = (u64)huge_encode_dev(fhp->fh_dentry->d_sb->s_dev);
380 p = xdr_encode_hyper(p, fsid);
383 p = xdr_encode_hyper(p, stat->ino);
385 p = encode_nfstime3(p, &stat->atime);
386 p = encode_nfstime3(p, &stat->mtime);
387 encode_nfstime3(p, &stat->ctime);
393 svcxdr_encode_wcc_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
397 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
400 p = xdr_encode_hyper(p, (u64)fhp->fh_pre_size);
401 p = encode_nfstime3(p, &fhp->fh_pre_mtime);
402 encode_nfstime3(p, &fhp->fh_pre_ctime);
408 svcxdr_encode_pre_op_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
410 if (!fhp->fh_pre_saved) {
411 if (xdr_stream_encode_item_absent(xdr) < 0)
416 if (xdr_stream_encode_item_present(xdr) < 0)
418 return svcxdr_encode_wcc_attr(xdr, fhp);
422 * svcxdr_encode_post_op_attr - Encode NFSv3 post-op attributes
423 * @rqstp: Context of a completed RPC transaction
425 * @fhp: File handle to encode
428 * %false: Send buffer space was exhausted
432 svcxdr_encode_post_op_attr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
433 const struct svc_fh *fhp)
435 struct dentry *dentry = fhp->fh_dentry;
439 * The inode may be NULL if the call failed because of a
440 * stale file handle. In this case, no attributes are
443 if (fhp->fh_no_wcc || !dentry || !d_really_is_positive(dentry))
444 goto no_post_op_attrs;
445 if (fh_getattr(fhp, &stat) != nfs_ok)
446 goto no_post_op_attrs;
448 if (xdr_stream_encode_item_present(xdr) < 0)
450 lease_get_mtime(d_inode(dentry), &stat.mtime);
451 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &stat))
457 return xdr_stream_encode_item_absent(xdr) > 0;
461 * Encode weak cache consistency data
464 svcxdr_encode_wcc_data(struct svc_rqst *rqstp, struct xdr_stream *xdr,
465 const struct svc_fh *fhp)
467 struct dentry *dentry = fhp->fh_dentry;
469 if (!dentry || !d_really_is_positive(dentry) || !fhp->fh_post_saved)
473 if (!svcxdr_encode_pre_op_attr(xdr, fhp))
477 if (xdr_stream_encode_item_present(xdr) < 0)
479 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &fhp->fh_post_attr))
485 if (xdr_stream_encode_item_absent(xdr) < 0)
487 if (!svcxdr_encode_post_op_attr(rqstp, xdr, fhp))
493 static bool fs_supports_change_attribute(struct super_block *sb)
495 return sb->s_flags & SB_I_VERSION || sb->s_export_op->fetch_iversion;
499 * Fill in the pre_op attr for the wcc data
501 void fill_pre_wcc(struct svc_fh *fhp)
505 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
507 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
509 inode = d_inode(fhp->fh_dentry);
510 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
511 __be32 err = fh_getattr(fhp, &stat);
514 /* Grab the times from inode anyway */
515 stat.mtime = inode->i_mtime;
516 stat.ctime = inode->i_ctime;
517 stat.size = inode->i_size;
519 fhp->fh_pre_mtime = stat.mtime;
520 fhp->fh_pre_ctime = stat.ctime;
521 fhp->fh_pre_size = stat.size;
524 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
526 fhp->fh_pre_saved = true;
530 * Fill in the post_op attr for the wcc data
532 void fill_post_wcc(struct svc_fh *fhp)
534 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
535 struct inode *inode = d_inode(fhp->fh_dentry);
540 if (fhp->fh_post_saved)
541 printk("nfsd: inode locked twice during operation.\n");
543 fhp->fh_post_saved = true;
545 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
546 __be32 err = fh_getattr(fhp, &fhp->fh_post_attr);
549 fhp->fh_post_saved = false;
550 fhp->fh_post_attr.ctime = inode->i_ctime;
554 fhp->fh_post_change =
555 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
559 * XDR decode functions
563 nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
565 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
566 struct nfsd_fhandle *args = rqstp->rq_argp;
568 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
572 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
574 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
575 struct nfsd3_sattrargs *args = rqstp->rq_argp;
577 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
578 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
579 svcxdr_decode_sattrguard3(xdr, args);
583 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
585 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
586 struct nfsd3_diropargs *args = rqstp->rq_argp;
588 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
592 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
594 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
595 struct nfsd3_accessargs *args = rqstp->rq_argp;
597 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
599 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
606 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
608 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
609 struct nfsd3_readargs *args = rqstp->rq_argp;
611 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
613 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
615 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
622 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
624 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
625 struct nfsd3_writeargs *args = rqstp->rq_argp;
626 u32 max_blocksize = svc_max_payload(rqstp);
627 struct kvec *head = rqstp->rq_arg.head;
628 struct kvec *tail = rqstp->rq_arg.tail;
631 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
633 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
635 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
637 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
641 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
645 if (args->count != args->len)
647 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
648 remaining -= xdr_stream_pos(xdr);
649 if (remaining < xdr_align_size(args->len))
651 if (args->count > max_blocksize) {
652 args->count = max_blocksize;
653 args->len = max_blocksize;
656 args->first.iov_base = xdr->p;
657 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
663 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
665 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
666 struct nfsd3_createargs *args = rqstp->rq_argp;
668 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
670 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
672 switch (args->createmode) {
673 case NFS3_CREATE_UNCHECKED:
674 case NFS3_CREATE_GUARDED:
675 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
676 case NFS3_CREATE_EXCLUSIVE:
677 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
688 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
690 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
691 struct nfsd3_createargs *args = rqstp->rq_argp;
693 return svcxdr_decode_diropargs3(xdr, &args->fh,
694 &args->name, &args->len) &&
695 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
699 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
701 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
702 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
703 struct kvec *head = rqstp->rq_arg.head;
704 struct kvec *tail = rqstp->rq_arg.tail;
707 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
709 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
711 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
715 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
716 remaining -= xdr_stream_pos(xdr);
717 if (remaining < xdr_align_size(args->tlen))
720 args->first.iov_base = xdr->p;
721 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
727 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
729 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
730 struct nfsd3_mknodargs *args = rqstp->rq_argp;
732 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
734 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
736 switch (args->ftype) {
739 return svcxdr_decode_devicedata3(rqstp, xdr, args);
742 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
746 /* Valid XDR but illegal file types */
756 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
758 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
759 struct nfsd3_renameargs *args = rqstp->rq_argp;
761 return svcxdr_decode_diropargs3(xdr, &args->ffh,
762 &args->fname, &args->flen) &&
763 svcxdr_decode_diropargs3(xdr, &args->tfh,
764 &args->tname, &args->tlen);
768 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
770 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
771 struct nfsd3_linkargs *args = rqstp->rq_argp;
773 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
774 svcxdr_decode_diropargs3(xdr, &args->tfh,
775 &args->tname, &args->tlen);
779 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
781 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
782 struct nfsd3_readdirargs *args = rqstp->rq_argp;
784 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
786 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
788 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
791 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
798 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
800 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
801 struct nfsd3_readdirargs *args = rqstp->rq_argp;
804 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
806 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
808 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
811 /* dircount is ignored */
812 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
814 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
821 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
823 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
824 struct nfsd3_commitargs *args = rqstp->rq_argp;
826 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
828 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
830 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
837 * XDR encode functions
842 nfs3svc_encode_getattrres(struct svc_rqst *rqstp, __be32 *p)
844 struct xdr_stream *xdr = &rqstp->rq_res_stream;
845 struct nfsd3_attrstat *resp = rqstp->rq_resp;
847 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
849 switch (resp->status) {
851 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
852 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
860 /* SETATTR, REMOVE, RMDIR */
862 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
864 struct xdr_stream *xdr = &rqstp->rq_res_stream;
865 struct nfsd3_attrstat *resp = rqstp->rq_resp;
867 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
868 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
872 int nfs3svc_encode_lookupres(struct svc_rqst *rqstp, __be32 *p)
874 struct xdr_stream *xdr = &rqstp->rq_res_stream;
875 struct nfsd3_diropres *resp = rqstp->rq_resp;
877 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
879 switch (resp->status) {
881 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
883 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
885 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
889 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
898 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
900 struct xdr_stream *xdr = &rqstp->rq_res_stream;
901 struct nfsd3_accessres *resp = rqstp->rq_resp;
903 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
905 switch (resp->status) {
907 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
909 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
913 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
922 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
924 struct xdr_stream *xdr = &rqstp->rq_res_stream;
925 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
926 struct kvec *head = rqstp->rq_res.head;
928 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
930 switch (resp->status) {
932 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
934 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
936 xdr_write_pages(xdr, resp->pages, 0, resp->len);
937 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
941 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
950 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
952 struct xdr_stream *xdr = &rqstp->rq_res_stream;
953 struct nfsd3_readres *resp = rqstp->rq_resp;
954 struct kvec *head = rqstp->rq_res.head;
956 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
958 switch (resp->status) {
960 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
962 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
964 if (xdr_stream_encode_bool(xdr, resp->eof) < 0)
966 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
968 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
970 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
974 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
983 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
985 struct xdr_stream *xdr = &rqstp->rq_res_stream;
986 struct nfsd3_writeres *resp = rqstp->rq_resp;
988 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
990 switch (resp->status) {
992 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
994 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
996 if (xdr_stream_encode_u32(xdr, resp->committed) < 0)
998 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1002 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1009 /* CREATE, MKDIR, SYMLINK, MKNOD */
1011 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
1013 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1014 struct nfsd3_diropres *resp = rqstp->rq_resp;
1016 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1018 switch (resp->status) {
1020 if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh))
1022 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1024 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1028 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1037 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
1039 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1040 struct nfsd3_renameres *resp = rqstp->rq_resp;
1042 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1043 svcxdr_encode_wcc_data(rqstp, xdr, &resp->ffh) &&
1044 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
1049 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
1051 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1052 struct nfsd3_linkres *resp = rqstp->rq_resp;
1054 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1055 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh) &&
1056 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
1061 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
1063 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1064 struct nfsd3_readdirres *resp = rqstp->rq_resp;
1065 struct xdr_buf *dirlist = &resp->dirlist;
1067 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1069 switch (resp->status) {
1071 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1073 if (!svcxdr_encode_cookieverf3(xdr, resp->verf))
1075 xdr_write_pages(xdr, dirlist->pages, 0, dirlist->len);
1076 /* no more entries */
1077 if (xdr_stream_encode_item_absent(xdr) < 0)
1079 if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
1083 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1091 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
1092 const char *name, int namlen, u64 ino)
1094 struct svc_export *exp;
1095 struct dentry *dparent, *dchild;
1096 __be32 rv = nfserr_noent;
1098 dparent = cd->fh.fh_dentry;
1099 exp = cd->fh.fh_export;
1101 if (isdotent(name, namlen)) {
1103 dchild = dget_parent(dparent);
1105 * Don't return filehandle for ".." if we're at
1106 * the filesystem or export root:
1108 if (dchild == dparent)
1110 if (dparent == exp->ex_path.dentry)
1113 dchild = dget(dparent);
1115 dchild = lookup_positive_unlocked(name, dparent, namlen);
1118 if (d_mountpoint(dchild))
1120 if (dchild->d_inode->i_ino != ino)
1122 rv = fh_compose(fhp, exp, dchild, &cd->fh);
1129 * nfs3svc_encode_cookie3 - Encode a directory offset cookie
1130 * @resp: readdir result context
1131 * @offset: offset cookie to encode
1133 * The buffer space for the offset cookie has already been reserved
1134 * by svcxdr_encode_entry3_common().
1136 void nfs3svc_encode_cookie3(struct nfsd3_readdirres *resp, u64 offset)
1138 __be64 cookie = cpu_to_be64(offset);
1140 if (!resp->cookie_offset)
1142 write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
1144 resp->cookie_offset = 0;
1148 svcxdr_encode_entry3_common(struct nfsd3_readdirres *resp, const char *name,
1149 int namlen, loff_t offset, u64 ino)
1151 struct xdr_buf *dirlist = &resp->dirlist;
1152 struct xdr_stream *xdr = &resp->xdr;
1154 if (xdr_stream_encode_item_present(xdr) < 0)
1157 if (xdr_stream_encode_u64(xdr, ino) < 0)
1160 if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS3_MAXNAMLEN)) < 0)
1163 resp->cookie_offset = dirlist->len;
1164 if (xdr_stream_encode_u64(xdr, NFS_OFFSET_MAX) < 0)
1171 * nfs3svc_encode_entry3 - encode one NFSv3 READDIR entry
1172 * @data: directory context
1173 * @name: name of the object to be encoded
1174 * @namlen: length of that name, in bytes
1175 * @offset: the offset of the previous entry
1176 * @ino: the fileid of this entry
1180 * %0: Entry was successfully encoded.
1181 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1183 * On exit, the following fields are updated:
1185 * - resp->common.err
1186 * - resp->cookie_offset
1188 int nfs3svc_encode_entry3(void *data, const char *name, int namlen,
1189 loff_t offset, u64 ino, unsigned int d_type)
1191 struct readdir_cd *ccd = data;
1192 struct nfsd3_readdirres *resp = container_of(ccd,
1193 struct nfsd3_readdirres,
1195 unsigned int starting_length = resp->dirlist.len;
1197 /* The offset cookie for the previous entry */
1198 nfs3svc_encode_cookie3(resp, offset);
1200 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1203 xdr_commit_encode(&resp->xdr);
1204 resp->common.err = nfs_ok;
1208 resp->cookie_offset = 0;
1209 resp->common.err = nfserr_toosmall;
1210 resp->dirlist.len = starting_length;
1215 svcxdr_encode_entry3_plus(struct nfsd3_readdirres *resp, const char *name,
1216 int namlen, u64 ino)
1218 struct xdr_stream *xdr = &resp->xdr;
1219 struct svc_fh *fhp = &resp->scratch;
1223 fh_init(fhp, NFS3_FHSIZE);
1224 if (compose_entry_fh(resp, fhp, name, namlen, ino) != nfs_ok)
1227 if (!svcxdr_encode_post_op_attr(resp->rqstp, xdr, fhp))
1229 if (!svcxdr_encode_post_op_fh3(xdr, fhp))
1238 if (xdr_stream_encode_item_absent(xdr) < 0)
1240 if (xdr_stream_encode_item_absent(xdr) < 0)
1246 * nfs3svc_encode_entryplus3 - encode one NFSv3 READDIRPLUS entry
1247 * @data: directory context
1248 * @name: name of the object to be encoded
1249 * @namlen: length of that name, in bytes
1250 * @offset: the offset of the previous entry
1251 * @ino: the fileid of this entry
1255 * %0: Entry was successfully encoded.
1256 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1258 * On exit, the following fields are updated:
1260 * - resp->common.err
1261 * - resp->cookie_offset
1263 int nfs3svc_encode_entryplus3(void *data, const char *name, int namlen,
1264 loff_t offset, u64 ino, unsigned int d_type)
1266 struct readdir_cd *ccd = data;
1267 struct nfsd3_readdirres *resp = container_of(ccd,
1268 struct nfsd3_readdirres,
1270 unsigned int starting_length = resp->dirlist.len;
1272 /* The offset cookie for the previous entry */
1273 nfs3svc_encode_cookie3(resp, offset);
1275 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1277 if (!svcxdr_encode_entry3_plus(resp, name, namlen, ino))
1280 xdr_commit_encode(&resp->xdr);
1281 resp->common.err = nfs_ok;
1285 resp->cookie_offset = 0;
1286 resp->common.err = nfserr_toosmall;
1287 resp->dirlist.len = starting_length;
1292 svcxdr_encode_fsstat3resok(struct xdr_stream *xdr,
1293 const struct nfsd3_fsstatres *resp)
1295 const struct kstatfs *s = &resp->stats;
1296 u64 bs = s->f_bsize;
1299 p = xdr_reserve_space(xdr, XDR_UNIT * 13);
1302 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1303 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1304 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1305 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1306 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1307 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1308 *p = cpu_to_be32(resp->invarsec); /* mean unchanged time */
1315 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
1317 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1318 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
1320 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1322 switch (resp->status) {
1324 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1326 if (!svcxdr_encode_fsstat3resok(xdr, resp))
1330 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1338 svcxdr_encode_fsinfo3resok(struct xdr_stream *xdr,
1339 const struct nfsd3_fsinfores *resp)
1343 p = xdr_reserve_space(xdr, XDR_UNIT * 12);
1346 *p++ = cpu_to_be32(resp->f_rtmax);
1347 *p++ = cpu_to_be32(resp->f_rtpref);
1348 *p++ = cpu_to_be32(resp->f_rtmult);
1349 *p++ = cpu_to_be32(resp->f_wtmax);
1350 *p++ = cpu_to_be32(resp->f_wtpref);
1351 *p++ = cpu_to_be32(resp->f_wtmult);
1352 *p++ = cpu_to_be32(resp->f_dtpref);
1353 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1354 p = encode_nfstime3(p, &nfs3svc_time_delta);
1355 *p = cpu_to_be32(resp->f_properties);
1362 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
1364 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1365 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1367 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1369 switch (resp->status) {
1371 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1373 if (!svcxdr_encode_fsinfo3resok(xdr, resp))
1377 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1385 svcxdr_encode_pathconf3resok(struct xdr_stream *xdr,
1386 const struct nfsd3_pathconfres *resp)
1390 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
1393 *p++ = cpu_to_be32(resp->p_link_max);
1394 *p++ = cpu_to_be32(resp->p_name_max);
1395 p = xdr_encode_bool(p, resp->p_no_trunc);
1396 p = xdr_encode_bool(p, resp->p_chown_restricted);
1397 p = xdr_encode_bool(p, resp->p_case_insensitive);
1398 xdr_encode_bool(p, resp->p_case_preserving);
1405 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
1407 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1408 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1410 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1412 switch (resp->status) {
1414 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1416 if (!svcxdr_encode_pathconf3resok(xdr, resp))
1420 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1429 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
1431 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1432 struct nfsd3_commitres *resp = rqstp->rq_resp;
1434 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1436 switch (resp->status) {
1438 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1440 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1444 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1452 * XDR release functions
1455 nfs3svc_release_fhandle(struct svc_rqst *rqstp)
1457 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1463 nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
1465 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;