1 // SPDX-License-Identifier: GPL-2.0
3 * XDR support for nfsd/protocol version 3.
7 * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
10 #include <linux/namei.h>
11 #include <linux/sunrpc/svc_xprt.h>
18 * Force construction of an empty post-op attr
20 static const struct svc_fh nfs3svc_null_fh = {
25 * time_delta. {1, 0} means the server is accurate only
26 * to the nearest second.
28 static const struct timespec64 nfs3svc_time_delta = {
34 * Mapping of S_IF* types to NFS file types
36 static const u32 nfs3_ftypes[] = {
37 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
38 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
39 NF3REG, NF3BAD, NF3LNK, NF3BAD,
40 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
45 * Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6)
49 encode_nfstime3(__be32 *p, const struct timespec64 *time)
51 *p++ = cpu_to_be32((u32)time->tv_sec);
52 *p++ = cpu_to_be32(time->tv_nsec);
58 svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep)
62 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
65 timep->tv_sec = be32_to_cpup(p++);
66 timep->tv_nsec = be32_to_cpup(p);
72 * svcxdr_decode_nfs_fh3 - Decode an NFSv3 file handle
73 * @xdr: XDR stream positioned at an undecoded NFSv3 FH
74 * @fhp: OUT: filled-in server file handle
77 * %false: The encoded file handle was not valid
78 * %true: @fhp has been initialized
81 svcxdr_decode_nfs_fh3(struct xdr_stream *xdr, struct svc_fh *fhp)
86 if (xdr_stream_decode_u32(xdr, &size) < 0)
88 if (size == 0 || size > NFS3_FHSIZE)
90 p = xdr_inline_decode(xdr, size);
93 fh_init(fhp, NFS3_FHSIZE);
94 fhp->fh_handle.fh_size = size;
95 memcpy(&fhp->fh_handle.fh_raw, p, size);
101 * svcxdr_encode_nfsstat3 - Encode an NFSv3 status code
103 * @status: status value to encode
106 * %false: Send buffer space was exhausted
110 svcxdr_encode_nfsstat3(struct xdr_stream *xdr, __be32 status)
114 p = xdr_reserve_space(xdr, sizeof(status));
123 svcxdr_encode_nfs_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
125 u32 size = fhp->fh_handle.fh_size;
128 p = xdr_reserve_space(xdr, XDR_UNIT + size);
131 *p++ = cpu_to_be32(size);
133 p[XDR_QUADLEN(size) - 1] = 0;
134 memcpy(p, &fhp->fh_handle.fh_raw, size);
140 svcxdr_encode_post_op_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
142 if (xdr_stream_encode_item_present(xdr) < 0)
144 if (!svcxdr_encode_nfs_fh3(xdr, fhp))
151 svcxdr_encode_cookieverf3(struct xdr_stream *xdr, const __be32 *verf)
155 p = xdr_reserve_space(xdr, NFS3_COOKIEVERFSIZE);
158 memcpy(p, verf, NFS3_COOKIEVERFSIZE);
164 svcxdr_encode_writeverf3(struct xdr_stream *xdr, const __be32 *verf)
168 p = xdr_reserve_space(xdr, NFS3_WRITEVERFSIZE);
171 memcpy(p, verf, NFS3_WRITEVERFSIZE);
177 svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len)
183 if (xdr_stream_decode_u32(xdr, &size) < 0)
185 if (size == 0 || size > NFS3_MAXNAMLEN)
187 p = xdr_inline_decode(xdr, size);
193 for (i = 0, c = *name; i < size; i++, c++) {
194 if (*c == '\0' || *c == '/')
202 svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp,
203 char **name, unsigned int *len)
205 return svcxdr_decode_nfs_fh3(xdr, fhp) &&
206 svcxdr_decode_filename3(xdr, name, len);
210 svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
217 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
222 if (xdr_stream_decode_u32(xdr, &mode) < 0)
224 iap->ia_valid |= ATTR_MODE;
227 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
232 if (xdr_stream_decode_u32(xdr, &uid) < 0)
234 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid);
235 if (uid_valid(iap->ia_uid))
236 iap->ia_valid |= ATTR_UID;
238 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
243 if (xdr_stream_decode_u32(xdr, &gid) < 0)
245 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid);
246 if (gid_valid(iap->ia_gid))
247 iap->ia_valid |= ATTR_GID;
249 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
254 if (xdr_stream_decode_u64(xdr, &newsize) < 0)
256 iap->ia_valid |= ATTR_SIZE;
257 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
259 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
264 case SET_TO_SERVER_TIME:
265 iap->ia_valid |= ATTR_ATIME;
267 case SET_TO_CLIENT_TIME:
268 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime))
270 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
275 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
280 case SET_TO_SERVER_TIME:
281 iap->ia_valid |= ATTR_MTIME;
283 case SET_TO_CLIENT_TIME:
284 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime))
286 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
296 svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args)
301 if (xdr_stream_decode_bool(xdr, &check) < 0)
304 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
307 args->check_guard = 1;
308 args->guardtime = be32_to_cpup(p);
310 args->check_guard = 0;
316 svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
320 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
323 args->major = be32_to_cpup(p++);
324 args->minor = be32_to_cpup(p);
330 svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
331 struct nfsd3_mknodargs *args)
333 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
334 svcxdr_decode_specdata3(xdr, args);
338 svcxdr_encode_fattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
339 const struct svc_fh *fhp, const struct kstat *stat)
341 struct user_namespace *userns = nfsd_user_namespace(rqstp);
345 p = xdr_reserve_space(xdr, XDR_UNIT * 21);
349 *p++ = cpu_to_be32(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
350 *p++ = cpu_to_be32((u32)(stat->mode & S_IALLUGO));
351 *p++ = cpu_to_be32((u32)stat->nlink);
352 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
353 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
354 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN)
355 p = xdr_encode_hyper(p, (u64)NFS3_MAXPATHLEN);
357 p = xdr_encode_hyper(p, (u64)stat->size);
360 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
363 *p++ = cpu_to_be32((u32)MAJOR(stat->rdev));
364 *p++ = cpu_to_be32((u32)MINOR(stat->rdev));
366 switch(fsid_source(fhp)) {
367 case FSIDSOURCE_FSID:
368 fsid = (u64)fhp->fh_export->ex_fsid;
370 case FSIDSOURCE_UUID:
371 fsid = ((u64 *)fhp->fh_export->ex_uuid)[0];
372 fsid ^= ((u64 *)fhp->fh_export->ex_uuid)[1];
375 fsid = (u64)huge_encode_dev(fhp->fh_dentry->d_sb->s_dev);
377 p = xdr_encode_hyper(p, fsid);
380 p = xdr_encode_hyper(p, stat->ino);
382 p = encode_nfstime3(p, &stat->atime);
383 p = encode_nfstime3(p, &stat->mtime);
384 encode_nfstime3(p, &stat->ctime);
390 svcxdr_encode_wcc_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
394 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
397 p = xdr_encode_hyper(p, (u64)fhp->fh_pre_size);
398 p = encode_nfstime3(p, &fhp->fh_pre_mtime);
399 encode_nfstime3(p, &fhp->fh_pre_ctime);
405 svcxdr_encode_pre_op_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
407 if (!fhp->fh_pre_saved) {
408 if (xdr_stream_encode_item_absent(xdr) < 0)
413 if (xdr_stream_encode_item_present(xdr) < 0)
415 return svcxdr_encode_wcc_attr(xdr, fhp);
419 * svcxdr_encode_post_op_attr - Encode NFSv3 post-op attributes
420 * @rqstp: Context of a completed RPC transaction
422 * @fhp: File handle to encode
425 * %false: Send buffer space was exhausted
429 svcxdr_encode_post_op_attr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
430 const struct svc_fh *fhp)
432 struct dentry *dentry = fhp->fh_dentry;
436 * The inode may be NULL if the call failed because of a
437 * stale file handle. In this case, no attributes are
440 if (fhp->fh_no_wcc || !dentry || !d_really_is_positive(dentry))
441 goto no_post_op_attrs;
442 if (fh_getattr(fhp, &stat) != nfs_ok)
443 goto no_post_op_attrs;
445 if (xdr_stream_encode_item_present(xdr) < 0)
447 lease_get_mtime(d_inode(dentry), &stat.mtime);
448 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &stat))
454 return xdr_stream_encode_item_absent(xdr) > 0;
458 * Encode weak cache consistency data
461 svcxdr_encode_wcc_data(struct svc_rqst *rqstp, struct xdr_stream *xdr,
462 const struct svc_fh *fhp)
464 struct dentry *dentry = fhp->fh_dentry;
466 if (!dentry || !d_really_is_positive(dentry) || !fhp->fh_post_saved)
470 if (!svcxdr_encode_pre_op_attr(xdr, fhp))
474 if (xdr_stream_encode_item_present(xdr) < 0)
476 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &fhp->fh_post_attr))
482 if (xdr_stream_encode_item_absent(xdr) < 0)
484 if (!svcxdr_encode_post_op_attr(rqstp, xdr, fhp))
490 static bool fs_supports_change_attribute(struct super_block *sb)
492 return sb->s_flags & SB_I_VERSION || sb->s_export_op->fetch_iversion;
496 * Fill in the pre_op attr for the wcc data
498 void fill_pre_wcc(struct svc_fh *fhp)
502 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
504 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
506 inode = d_inode(fhp->fh_dentry);
507 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
508 __be32 err = fh_getattr(fhp, &stat);
511 /* Grab the times from inode anyway */
512 stat.mtime = inode->i_mtime;
513 stat.ctime = inode->i_ctime;
514 stat.size = inode->i_size;
516 fhp->fh_pre_mtime = stat.mtime;
517 fhp->fh_pre_ctime = stat.ctime;
518 fhp->fh_pre_size = stat.size;
521 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
523 fhp->fh_pre_saved = true;
527 * Fill in the post_op attr for the wcc data
529 void fill_post_wcc(struct svc_fh *fhp)
531 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
532 struct inode *inode = d_inode(fhp->fh_dentry);
537 if (fhp->fh_post_saved)
538 printk("nfsd: inode locked twice during operation.\n");
540 fhp->fh_post_saved = true;
542 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
543 __be32 err = fh_getattr(fhp, &fhp->fh_post_attr);
546 fhp->fh_post_saved = false;
547 fhp->fh_post_attr.ctime = inode->i_ctime;
551 fhp->fh_post_change =
552 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
556 * XDR decode functions
560 nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
562 struct nfsd_fhandle *args = rqstp->rq_argp;
564 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
568 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
570 struct nfsd3_sattrargs *args = rqstp->rq_argp;
572 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
573 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
574 svcxdr_decode_sattrguard3(xdr, args);
578 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
580 struct nfsd3_diropargs *args = rqstp->rq_argp;
582 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
586 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
588 struct nfsd3_accessargs *args = rqstp->rq_argp;
590 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
592 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
599 nfs3svc_decode_readargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
601 struct nfsd3_readargs *args = rqstp->rq_argp;
603 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
605 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
607 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
614 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
616 struct nfsd3_writeargs *args = rqstp->rq_argp;
617 u32 max_blocksize = svc_max_payload(rqstp);
619 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
621 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
623 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
625 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
629 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
633 if (args->count != args->len)
635 if (args->count > max_blocksize) {
636 args->count = max_blocksize;
637 args->len = max_blocksize;
639 if (!xdr_stream_subsegment(xdr, &args->payload, args->count))
646 nfs3svc_decode_createargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
648 struct nfsd3_createargs *args = rqstp->rq_argp;
650 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
652 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
654 switch (args->createmode) {
655 case NFS3_CREATE_UNCHECKED:
656 case NFS3_CREATE_GUARDED:
657 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
658 case NFS3_CREATE_EXCLUSIVE:
659 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
670 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
672 struct nfsd3_createargs *args = rqstp->rq_argp;
674 return svcxdr_decode_diropargs3(xdr, &args->fh,
675 &args->name, &args->len) &&
676 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
680 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
682 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
683 struct kvec *head = rqstp->rq_arg.head;
684 struct kvec *tail = rqstp->rq_arg.tail;
687 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
689 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
691 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
695 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
696 remaining -= xdr_stream_pos(xdr);
697 if (remaining < xdr_align_size(args->tlen))
700 args->first.iov_base = xdr->p;
701 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
707 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
709 struct nfsd3_mknodargs *args = rqstp->rq_argp;
711 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
713 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
715 switch (args->ftype) {
718 return svcxdr_decode_devicedata3(rqstp, xdr, args);
721 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
725 /* Valid XDR but illegal file types */
735 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
737 struct nfsd3_renameargs *args = rqstp->rq_argp;
739 return svcxdr_decode_diropargs3(xdr, &args->ffh,
740 &args->fname, &args->flen) &&
741 svcxdr_decode_diropargs3(xdr, &args->tfh,
742 &args->tname, &args->tlen);
746 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
748 struct nfsd3_linkargs *args = rqstp->rq_argp;
750 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
751 svcxdr_decode_diropargs3(xdr, &args->tfh,
752 &args->tname, &args->tlen);
756 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
758 struct nfsd3_readdirargs *args = rqstp->rq_argp;
760 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
762 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
764 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
767 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
774 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
776 struct nfsd3_readdirargs *args = rqstp->rq_argp;
779 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
781 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
783 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
786 /* dircount is ignored */
787 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
789 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
796 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
798 struct nfsd3_commitargs *args = rqstp->rq_argp;
800 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
802 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
804 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
811 * XDR encode functions
816 nfs3svc_encode_getattrres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
818 struct nfsd3_attrstat *resp = rqstp->rq_resp;
820 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
822 switch (resp->status) {
824 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
825 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
833 /* SETATTR, REMOVE, RMDIR */
835 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, struct xdr_stream *xdr)
837 struct nfsd3_attrstat *resp = rqstp->rq_resp;
839 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
840 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
845 nfs3svc_encode_lookupres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
847 struct nfsd3_diropres *resp = rqstp->rq_resp;
849 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
851 switch (resp->status) {
853 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
855 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
857 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
861 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
870 nfs3svc_encode_accessres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
872 struct nfsd3_accessres *resp = rqstp->rq_resp;
874 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
876 switch (resp->status) {
878 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
880 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
884 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
893 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
895 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
896 struct kvec *head = rqstp->rq_res.head;
898 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
900 switch (resp->status) {
902 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
904 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
906 xdr_write_pages(xdr, resp->pages, 0, resp->len);
907 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
911 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
920 nfs3svc_encode_readres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
922 struct nfsd3_readres *resp = rqstp->rq_resp;
923 struct kvec *head = rqstp->rq_res.head;
925 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
927 switch (resp->status) {
929 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
931 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
933 if (xdr_stream_encode_bool(xdr, resp->eof) < 0)
935 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
937 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
939 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
943 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
952 nfs3svc_encode_writeres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
954 struct nfsd3_writeres *resp = rqstp->rq_resp;
956 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
958 switch (resp->status) {
960 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
962 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
964 if (xdr_stream_encode_u32(xdr, resp->committed) < 0)
966 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
970 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
977 /* CREATE, MKDIR, SYMLINK, MKNOD */
979 nfs3svc_encode_createres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
981 struct nfsd3_diropres *resp = rqstp->rq_resp;
983 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
985 switch (resp->status) {
987 if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh))
989 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
991 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
995 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1004 nfs3svc_encode_renameres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1006 struct nfsd3_renameres *resp = rqstp->rq_resp;
1008 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1009 svcxdr_encode_wcc_data(rqstp, xdr, &resp->ffh) &&
1010 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
1015 nfs3svc_encode_linkres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1017 struct nfsd3_linkres *resp = rqstp->rq_resp;
1019 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1020 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh) &&
1021 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
1026 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1028 struct nfsd3_readdirres *resp = rqstp->rq_resp;
1029 struct xdr_buf *dirlist = &resp->dirlist;
1031 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1033 switch (resp->status) {
1035 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1037 if (!svcxdr_encode_cookieverf3(xdr, resp->verf))
1039 xdr_write_pages(xdr, dirlist->pages, 0, dirlist->len);
1040 /* no more entries */
1041 if (xdr_stream_encode_item_absent(xdr) < 0)
1043 if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
1047 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1055 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
1056 const char *name, int namlen, u64 ino)
1058 struct svc_export *exp;
1059 struct dentry *dparent, *dchild;
1060 __be32 rv = nfserr_noent;
1062 dparent = cd->fh.fh_dentry;
1063 exp = cd->fh.fh_export;
1065 if (isdotent(name, namlen)) {
1067 dchild = dget_parent(dparent);
1069 * Don't return filehandle for ".." if we're at
1070 * the filesystem or export root:
1072 if (dchild == dparent)
1074 if (dparent == exp->ex_path.dentry)
1077 dchild = dget(dparent);
1079 dchild = lookup_positive_unlocked(name, dparent, namlen);
1082 if (d_mountpoint(dchild))
1084 if (dchild->d_inode->i_ino != ino)
1086 rv = fh_compose(fhp, exp, dchild, &cd->fh);
1093 * nfs3svc_encode_cookie3 - Encode a directory offset cookie
1094 * @resp: readdir result context
1095 * @offset: offset cookie to encode
1097 * The buffer space for the offset cookie has already been reserved
1098 * by svcxdr_encode_entry3_common().
1100 void nfs3svc_encode_cookie3(struct nfsd3_readdirres *resp, u64 offset)
1102 __be64 cookie = cpu_to_be64(offset);
1104 if (!resp->cookie_offset)
1106 write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
1108 resp->cookie_offset = 0;
1112 svcxdr_encode_entry3_common(struct nfsd3_readdirres *resp, const char *name,
1113 int namlen, loff_t offset, u64 ino)
1115 struct xdr_buf *dirlist = &resp->dirlist;
1116 struct xdr_stream *xdr = &resp->xdr;
1118 if (xdr_stream_encode_item_present(xdr) < 0)
1121 if (xdr_stream_encode_u64(xdr, ino) < 0)
1124 if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS3_MAXNAMLEN)) < 0)
1127 resp->cookie_offset = dirlist->len;
1128 if (xdr_stream_encode_u64(xdr, NFS_OFFSET_MAX) < 0)
1135 * nfs3svc_encode_entry3 - encode one NFSv3 READDIR entry
1136 * @data: directory context
1137 * @name: name of the object to be encoded
1138 * @namlen: length of that name, in bytes
1139 * @offset: the offset of the previous entry
1140 * @ino: the fileid of this entry
1144 * %0: Entry was successfully encoded.
1145 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1147 * On exit, the following fields are updated:
1149 * - resp->common.err
1150 * - resp->cookie_offset
1152 int nfs3svc_encode_entry3(void *data, const char *name, int namlen,
1153 loff_t offset, u64 ino, unsigned int d_type)
1155 struct readdir_cd *ccd = data;
1156 struct nfsd3_readdirres *resp = container_of(ccd,
1157 struct nfsd3_readdirres,
1159 unsigned int starting_length = resp->dirlist.len;
1161 /* The offset cookie for the previous entry */
1162 nfs3svc_encode_cookie3(resp, offset);
1164 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1167 xdr_commit_encode(&resp->xdr);
1168 resp->common.err = nfs_ok;
1172 resp->cookie_offset = 0;
1173 resp->common.err = nfserr_toosmall;
1174 resp->dirlist.len = starting_length;
1179 svcxdr_encode_entry3_plus(struct nfsd3_readdirres *resp, const char *name,
1180 int namlen, u64 ino)
1182 struct xdr_stream *xdr = &resp->xdr;
1183 struct svc_fh *fhp = &resp->scratch;
1187 fh_init(fhp, NFS3_FHSIZE);
1188 if (compose_entry_fh(resp, fhp, name, namlen, ino) != nfs_ok)
1191 if (!svcxdr_encode_post_op_attr(resp->rqstp, xdr, fhp))
1193 if (!svcxdr_encode_post_op_fh3(xdr, fhp))
1202 if (xdr_stream_encode_item_absent(xdr) < 0)
1204 if (xdr_stream_encode_item_absent(xdr) < 0)
1210 * nfs3svc_encode_entryplus3 - encode one NFSv3 READDIRPLUS entry
1211 * @data: directory context
1212 * @name: name of the object to be encoded
1213 * @namlen: length of that name, in bytes
1214 * @offset: the offset of the previous entry
1215 * @ino: the fileid of this entry
1219 * %0: Entry was successfully encoded.
1220 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1222 * On exit, the following fields are updated:
1224 * - resp->common.err
1225 * - resp->cookie_offset
1227 int nfs3svc_encode_entryplus3(void *data, const char *name, int namlen,
1228 loff_t offset, u64 ino, unsigned int d_type)
1230 struct readdir_cd *ccd = data;
1231 struct nfsd3_readdirres *resp = container_of(ccd,
1232 struct nfsd3_readdirres,
1234 unsigned int starting_length = resp->dirlist.len;
1236 /* The offset cookie for the previous entry */
1237 nfs3svc_encode_cookie3(resp, offset);
1239 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1241 if (!svcxdr_encode_entry3_plus(resp, name, namlen, ino))
1244 xdr_commit_encode(&resp->xdr);
1245 resp->common.err = nfs_ok;
1249 resp->cookie_offset = 0;
1250 resp->common.err = nfserr_toosmall;
1251 resp->dirlist.len = starting_length;
1256 svcxdr_encode_fsstat3resok(struct xdr_stream *xdr,
1257 const struct nfsd3_fsstatres *resp)
1259 const struct kstatfs *s = &resp->stats;
1260 u64 bs = s->f_bsize;
1263 p = xdr_reserve_space(xdr, XDR_UNIT * 13);
1266 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1267 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1268 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1269 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1270 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1271 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1272 *p = cpu_to_be32(resp->invarsec); /* mean unchanged time */
1279 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1281 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
1283 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1285 switch (resp->status) {
1287 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1289 if (!svcxdr_encode_fsstat3resok(xdr, resp))
1293 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1301 svcxdr_encode_fsinfo3resok(struct xdr_stream *xdr,
1302 const struct nfsd3_fsinfores *resp)
1306 p = xdr_reserve_space(xdr, XDR_UNIT * 12);
1309 *p++ = cpu_to_be32(resp->f_rtmax);
1310 *p++ = cpu_to_be32(resp->f_rtpref);
1311 *p++ = cpu_to_be32(resp->f_rtmult);
1312 *p++ = cpu_to_be32(resp->f_wtmax);
1313 *p++ = cpu_to_be32(resp->f_wtpref);
1314 *p++ = cpu_to_be32(resp->f_wtmult);
1315 *p++ = cpu_to_be32(resp->f_dtpref);
1316 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1317 p = encode_nfstime3(p, &nfs3svc_time_delta);
1318 *p = cpu_to_be32(resp->f_properties);
1325 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1327 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1329 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1331 switch (resp->status) {
1333 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1335 if (!svcxdr_encode_fsinfo3resok(xdr, resp))
1339 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1347 svcxdr_encode_pathconf3resok(struct xdr_stream *xdr,
1348 const struct nfsd3_pathconfres *resp)
1352 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
1355 *p++ = cpu_to_be32(resp->p_link_max);
1356 *p++ = cpu_to_be32(resp->p_name_max);
1357 p = xdr_encode_bool(p, resp->p_no_trunc);
1358 p = xdr_encode_bool(p, resp->p_chown_restricted);
1359 p = xdr_encode_bool(p, resp->p_case_insensitive);
1360 xdr_encode_bool(p, resp->p_case_preserving);
1367 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1369 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1371 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1373 switch (resp->status) {
1375 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1377 if (!svcxdr_encode_pathconf3resok(xdr, resp))
1381 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1390 nfs3svc_encode_commitres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1392 struct nfsd3_commitres *resp = rqstp->rq_resp;
1394 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1396 switch (resp->status) {
1398 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1400 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1404 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1412 * XDR release functions
1415 nfs3svc_release_fhandle(struct svc_rqst *rqstp)
1417 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1423 nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
1425 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;