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 * XDR functions for basic NFS types
35 encode_time3(__be32 *p, struct timespec64 *time)
37 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
42 decode_time3(__be32 *p, struct timespec64 *time)
44 time->tv_sec = ntohl(*p++);
45 time->tv_nsec = ntohl(*p++);
50 decode_fh(__be32 *p, struct svc_fh *fhp)
53 fh_init(fhp, NFS3_FHSIZE);
55 if (size > NFS3_FHSIZE)
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);
70 encode_fh(__be32 *p, struct svc_fh *fhp)
72 unsigned int size = fhp->fh_handle.fh_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.
84 decode_filename(__be32 *p, char **namp, unsigned int *lenp)
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 == '/')
100 decode_sattr3(__be32 *p, struct iattr *iap, struct user_namespace *userns)
107 iap->ia_valid |= ATTR_MODE;
108 iap->ia_mode = ntohl(*p++);
111 iap->ia_uid = make_kuid(userns, ntohl(*p++));
112 if (uid_valid(iap->ia_uid))
113 iap->ia_valid |= ATTR_UID;
116 iap->ia_gid = make_kgid(userns, ntohl(*p++));
117 if (gid_valid(iap->ia_gid))
118 iap->ia_valid |= ATTR_GID;
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++);
144 static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
147 switch(fsid_source(fhp)) {
150 p = xdr_encode_hyper(p, (u64)huge_encode_dev
151 (fhp->fh_dentry->d_sb->s_dev));
153 case FSIDSOURCE_FSID:
154 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
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);
166 encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
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);
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);
193 encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
195 /* Attributes to follow */
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.
206 encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
208 struct dentry *dentry = fhp->fh_dentry;
209 if (dentry && d_really_is_positive(dentry)) {
213 err = fh_getattr(fhp, &stat);
215 *p++ = xdr_one; /* attributes follow */
216 lease_get_mtime(d_inode(dentry), &stat.mtime);
217 return encode_fattr3(rqstp, p, fhp, &stat);
224 /* Helper for NFSv3 ACLs */
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
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) {
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);
248 return encode_saved_post_attr(rqstp, p, fhp);
250 /* no pre- or post-attrs */
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)
264 if (fhp->fh_pre_saved)
267 inode = d_inode(fhp->fh_dentry);
268 err = fh_getattr(fhp, &stat);
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;
276 fhp->fh_pre_mtime = stat.mtime;
277 fhp->fh_pre_ctime = stat.ctime;
278 fhp->fh_pre_size = stat.size;
279 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
280 fhp->fh_pre_saved = true;
284 * Fill in the post_op attr for the wcc data
286 void fill_post_wcc(struct svc_fh *fhp)
290 if (fhp->fh_post_saved)
291 printk("nfsd: inode locked twice during operation.\n");
293 err = fh_getattr(fhp, &fhp->fh_post_attr);
294 fhp->fh_post_change = nfsd4_change_attribute(&fhp->fh_post_attr,
295 d_inode(fhp->fh_dentry));
297 fhp->fh_post_saved = false;
298 /* Grab the ctime anyway - set_change_info might use it */
299 fhp->fh_post_attr.ctime = d_inode(fhp->fh_dentry)->i_ctime;
301 fhp->fh_post_saved = true;
305 * XDR decode functions
308 nfs3svc_decode_voidarg(struct svc_rqst *rqstp, __be32 *p)
314 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p)
316 struct nfsd_fhandle *args = rqstp->rq_argp;
318 p = decode_fh(p, &args->fh);
321 return xdr_argsize_check(rqstp, p);
325 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
327 struct nfsd3_sattrargs *args = rqstp->rq_argp;
329 p = decode_fh(p, &args->fh);
332 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
334 if ((args->check_guard = ntohl(*p++)) != 0) {
335 struct timespec64 time;
336 p = decode_time3(p, &time);
337 args->guardtime = time.tv_sec;
340 return xdr_argsize_check(rqstp, p);
344 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
346 struct nfsd3_diropargs *args = rqstp->rq_argp;
348 if (!(p = decode_fh(p, &args->fh))
349 || !(p = decode_filename(p, &args->name, &args->len)))
352 return xdr_argsize_check(rqstp, p);
356 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
358 struct nfsd3_accessargs *args = rqstp->rq_argp;
360 p = decode_fh(p, &args->fh);
363 args->access = ntohl(*p++);
365 return xdr_argsize_check(rqstp, p);
369 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
371 struct nfsd3_readargs *args = rqstp->rq_argp;
374 u32 max_blocksize = svc_max_payload(rqstp);
376 p = decode_fh(p, &args->fh);
379 p = xdr_decode_hyper(p, &args->offset);
381 args->count = ntohl(*p++);
382 len = min(args->count, max_blocksize);
384 /* set up the kvec */
387 struct page *p = *(rqstp->rq_next_page++);
389 rqstp->rq_vec[v].iov_base = page_address(p);
390 rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
391 len -= rqstp->rq_vec[v].iov_len;
395 return xdr_argsize_check(rqstp, p);
399 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
401 struct nfsd3_writeargs *args = rqstp->rq_argp;
402 unsigned int len, hdr, dlen;
403 u32 max_blocksize = svc_max_payload(rqstp);
404 struct kvec *head = rqstp->rq_arg.head;
405 struct kvec *tail = rqstp->rq_arg.tail;
407 p = decode_fh(p, &args->fh);
410 p = xdr_decode_hyper(p, &args->offset);
412 args->count = ntohl(*p++);
413 args->stable = ntohl(*p++);
414 len = args->len = ntohl(*p++);
415 if ((void *)p > head->iov_base + head->iov_len)
418 * The count must equal the amount of data passed.
420 if (args->count != args->len)
424 * Check to make sure that we got the right number of
427 hdr = (void*)p - head->iov_base;
428 dlen = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len - hdr;
430 * Round the length of the data which was specified up to
431 * the next multiple of XDR units and then compare that
432 * against the length which was actually received.
433 * Note that when RPCSEC/GSS (for example) is used, the
434 * data buffer can be padded so dlen might be larger
435 * than required. It must never be smaller.
437 if (dlen < XDR_QUADLEN(len)*4)
440 if (args->count > max_blocksize) {
441 args->count = max_blocksize;
442 len = args->len = max_blocksize;
445 args->first.iov_base = (void *)p;
446 args->first.iov_len = head->iov_len - hdr;
451 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
453 struct nfsd3_createargs *args = rqstp->rq_argp;
455 if (!(p = decode_fh(p, &args->fh))
456 || !(p = decode_filename(p, &args->name, &args->len)))
459 switch (args->createmode = ntohl(*p++)) {
460 case NFS3_CREATE_UNCHECKED:
461 case NFS3_CREATE_GUARDED:
462 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
464 case NFS3_CREATE_EXCLUSIVE:
472 return xdr_argsize_check(rqstp, p);
476 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
478 struct nfsd3_createargs *args = rqstp->rq_argp;
480 if (!(p = decode_fh(p, &args->fh)) ||
481 !(p = decode_filename(p, &args->name, &args->len)))
483 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
485 return xdr_argsize_check(rqstp, p);
489 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
491 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
492 char *base = (char *)p;
495 if (!(p = decode_fh(p, &args->ffh)) ||
496 !(p = decode_filename(p, &args->fname, &args->flen)))
498 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
500 args->tlen = ntohl(*p++);
502 args->first.iov_base = p;
503 args->first.iov_len = rqstp->rq_arg.head[0].iov_len;
504 args->first.iov_len -= (char *)p - base;
506 dlen = args->first.iov_len + rqstp->rq_arg.page_len +
507 rqstp->rq_arg.tail[0].iov_len;
508 if (dlen < XDR_QUADLEN(args->tlen) << 2)
514 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
516 struct nfsd3_mknodargs *args = rqstp->rq_argp;
518 if (!(p = decode_fh(p, &args->fh))
519 || !(p = decode_filename(p, &args->name, &args->len)))
522 args->ftype = ntohl(*p++);
524 if (args->ftype == NF3BLK || args->ftype == NF3CHR
525 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
526 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
528 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
529 args->major = ntohl(*p++);
530 args->minor = ntohl(*p++);
533 return xdr_argsize_check(rqstp, p);
537 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
539 struct nfsd3_renameargs *args = rqstp->rq_argp;
541 if (!(p = decode_fh(p, &args->ffh))
542 || !(p = decode_filename(p, &args->fname, &args->flen))
543 || !(p = decode_fh(p, &args->tfh))
544 || !(p = decode_filename(p, &args->tname, &args->tlen)))
547 return xdr_argsize_check(rqstp, p);
551 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p)
553 struct nfsd3_readlinkargs *args = rqstp->rq_argp;
555 p = decode_fh(p, &args->fh);
558 args->buffer = page_address(*(rqstp->rq_next_page++));
560 return xdr_argsize_check(rqstp, p);
564 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
566 struct nfsd3_linkargs *args = rqstp->rq_argp;
568 if (!(p = decode_fh(p, &args->ffh))
569 || !(p = decode_fh(p, &args->tfh))
570 || !(p = decode_filename(p, &args->tname, &args->tlen)))
573 return xdr_argsize_check(rqstp, p);
577 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
579 struct nfsd3_readdirargs *args = rqstp->rq_argp;
581 u32 max_blocksize = svc_max_payload(rqstp);
583 p = decode_fh(p, &args->fh);
586 p = xdr_decode_hyper(p, &args->cookie);
587 args->verf = p; p += 2;
589 args->count = ntohl(*p++);
590 len = args->count = min_t(u32, args->count, max_blocksize);
593 struct page *p = *(rqstp->rq_next_page++);
595 args->buffer = page_address(p);
599 return xdr_argsize_check(rqstp, p);
603 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
605 struct nfsd3_readdirargs *args = rqstp->rq_argp;
607 u32 max_blocksize = svc_max_payload(rqstp);
609 p = decode_fh(p, &args->fh);
612 p = xdr_decode_hyper(p, &args->cookie);
613 args->verf = p; p += 2;
614 args->dircount = ntohl(*p++);
615 args->count = ntohl(*p++);
617 len = args->count = min(args->count, max_blocksize);
619 struct page *p = *(rqstp->rq_next_page++);
621 args->buffer = page_address(p);
625 return xdr_argsize_check(rqstp, p);
629 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
631 struct nfsd3_commitargs *args = rqstp->rq_argp;
632 p = decode_fh(p, &args->fh);
635 p = xdr_decode_hyper(p, &args->offset);
636 args->count = ntohl(*p++);
638 return xdr_argsize_check(rqstp, p);
642 * XDR encode functions
646 nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p)
648 return xdr_ressize_check(rqstp, p);
653 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p)
655 struct nfsd3_attrstat *resp = rqstp->rq_resp;
658 if (resp->status == 0) {
659 lease_get_mtime(d_inode(resp->fh.fh_dentry),
661 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
663 return xdr_ressize_check(rqstp, p);
666 /* SETATTR, REMOVE, RMDIR */
668 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
670 struct nfsd3_attrstat *resp = rqstp->rq_resp;
673 p = encode_wcc_data(rqstp, p, &resp->fh);
674 return xdr_ressize_check(rqstp, p);
679 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
681 struct nfsd3_diropres *resp = rqstp->rq_resp;
684 if (resp->status == 0) {
685 p = encode_fh(p, &resp->fh);
686 p = encode_post_op_attr(rqstp, p, &resp->fh);
688 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
689 return xdr_ressize_check(rqstp, p);
694 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
696 struct nfsd3_accessres *resp = rqstp->rq_resp;
699 p = encode_post_op_attr(rqstp, p, &resp->fh);
700 if (resp->status == 0)
701 *p++ = htonl(resp->access);
702 return xdr_ressize_check(rqstp, p);
707 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
709 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
712 p = encode_post_op_attr(rqstp, p, &resp->fh);
713 if (resp->status == 0) {
714 *p++ = htonl(resp->len);
715 xdr_ressize_check(rqstp, p);
716 rqstp->rq_res.page_len = resp->len;
718 /* need to pad the tail */
719 rqstp->rq_res.tail[0].iov_base = p;
721 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
725 return xdr_ressize_check(rqstp, p);
730 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
732 struct nfsd3_readres *resp = rqstp->rq_resp;
735 p = encode_post_op_attr(rqstp, p, &resp->fh);
736 if (resp->status == 0) {
737 *p++ = htonl(resp->count);
738 *p++ = htonl(resp->eof);
739 *p++ = htonl(resp->count); /* xdr opaque count */
740 xdr_ressize_check(rqstp, p);
741 /* now update rqstp->rq_res to reflect data as well */
742 rqstp->rq_res.page_len = resp->count;
743 if (resp->count & 3) {
744 /* need to pad the tail */
745 rqstp->rq_res.tail[0].iov_base = p;
747 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
751 return xdr_ressize_check(rqstp, p);
756 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
758 struct nfsd3_writeres *resp = rqstp->rq_resp;
761 p = encode_wcc_data(rqstp, p, &resp->fh);
762 if (resp->status == 0) {
763 *p++ = htonl(resp->count);
764 *p++ = htonl(resp->committed);
765 *p++ = resp->verf[0];
766 *p++ = resp->verf[1];
768 return xdr_ressize_check(rqstp, p);
771 /* CREATE, MKDIR, SYMLINK, MKNOD */
773 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
775 struct nfsd3_diropres *resp = rqstp->rq_resp;
778 if (resp->status == 0) {
780 p = encode_fh(p, &resp->fh);
781 p = encode_post_op_attr(rqstp, p, &resp->fh);
783 p = encode_wcc_data(rqstp, p, &resp->dirfh);
784 return xdr_ressize_check(rqstp, p);
789 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
791 struct nfsd3_renameres *resp = rqstp->rq_resp;
794 p = encode_wcc_data(rqstp, p, &resp->ffh);
795 p = encode_wcc_data(rqstp, p, &resp->tfh);
796 return xdr_ressize_check(rqstp, p);
801 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
803 struct nfsd3_linkres *resp = rqstp->rq_resp;
806 p = encode_post_op_attr(rqstp, p, &resp->fh);
807 p = encode_wcc_data(rqstp, p, &resp->tfh);
808 return xdr_ressize_check(rqstp, p);
813 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
815 struct nfsd3_readdirres *resp = rqstp->rq_resp;
818 p = encode_post_op_attr(rqstp, p, &resp->fh);
820 if (resp->status == 0) {
821 /* stupid readdir cookie */
822 memcpy(p, resp->verf, 8); p += 2;
823 xdr_ressize_check(rqstp, p);
824 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
825 return 1; /*No room for trailer */
826 rqstp->rq_res.page_len = (resp->count) << 2;
828 /* add the 'tail' to the end of the 'head' page - page 0. */
829 rqstp->rq_res.tail[0].iov_base = p;
830 *p++ = 0; /* no more entries */
831 *p++ = htonl(resp->common.err == nfserr_eof);
832 rqstp->rq_res.tail[0].iov_len = 2<<2;
835 return xdr_ressize_check(rqstp, p);
839 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
842 *p++ = xdr_one; /* mark entry present */
843 p = xdr_encode_hyper(p, ino); /* file id */
844 p = xdr_encode_array(p, name, namlen);/* name length & name */
846 cd->offset = p; /* remember pointer */
847 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
853 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
854 const char *name, int namlen, u64 ino)
856 struct svc_export *exp;
857 struct dentry *dparent, *dchild;
858 __be32 rv = nfserr_noent;
860 dparent = cd->fh.fh_dentry;
861 exp = cd->fh.fh_export;
863 if (isdotent(name, namlen)) {
865 dchild = dget_parent(dparent);
867 * Don't return filehandle for ".." if we're at
868 * the filesystem or export root:
870 if (dchild == dparent)
872 if (dparent == exp->ex_path.dentry)
875 dchild = dget(dparent);
877 dchild = lookup_positive_unlocked(name, dparent, namlen);
880 if (d_mountpoint(dchild))
882 if (dchild->d_inode->i_ino != ino)
884 rv = fh_compose(fhp, exp, dchild, &cd->fh);
890 static __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, int namlen, u64 ino)
892 struct svc_fh *fh = &cd->scratch;
895 fh_init(fh, NFS3_FHSIZE);
896 err = compose_entry_fh(cd, fh, name, namlen, ino);
902 p = encode_post_op_attr(cd->rqstp, p, fh);
903 *p++ = xdr_one; /* yes, a file handle follows */
904 p = encode_fh(p, fh);
911 * Encode a directory entry. This one works for both normal readdir
913 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
914 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
916 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
920 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
921 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
923 encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
924 loff_t offset, u64 ino, unsigned int d_type, int plus)
926 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
928 __be32 *p = cd->buffer;
929 caddr_t curr_page_addr = NULL;
931 int slen; /* string (name) length */
932 int elen; /* estimated entry length in words */
933 int num_entry_words = 0; /* actual number of words */
936 u64 offset64 = offset;
938 if (unlikely(cd->offset1)) {
939 /* we ended up with offset on a page boundary */
940 *cd->offset = htonl(offset64 >> 32);
941 *cd->offset1 = htonl(offset64 & 0xffffffff);
944 xdr_encode_hyper(cd->offset, offset64);
950 dprintk("encode_entry(%.*s @%ld%s)\n",
951 namlen, name, (long) offset, plus? " plus" : "");
954 /* truncate filename if too long */
955 namlen = min(namlen, NFS3_MAXNAMLEN);
957 slen = XDR_QUADLEN(namlen);
958 elen = slen + NFS3_ENTRY_BAGGAGE
959 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
961 if (cd->buflen < elen) {
962 cd->common.err = nfserr_toosmall;
966 /* determine which page in rq_respages[] we are currently filling */
967 for (page = cd->rqstp->rq_respages + 1;
968 page < cd->rqstp->rq_next_page; page++) {
969 curr_page_addr = page_address(*page);
971 if (((caddr_t)cd->buffer >= curr_page_addr) &&
972 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
976 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
977 /* encode entry in current page */
979 p = encode_entry_baggage(cd, p, name, namlen, ino);
982 p = encode_entryplus_baggage(cd, p, name, namlen, ino);
983 num_entry_words = p - cd->buffer;
984 } else if (*(page+1) != NULL) {
985 /* temporarily encode entry into next page, then move back to
986 * current and next page in rq_respages[] */
990 /* grab next page for temporary storage of entry */
991 p1 = tmp = page_address(*(page+1));
993 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
996 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
998 /* determine entry word length and lengths to go in pages */
999 num_entry_words = p1 - tmp;
1000 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
1001 if ((num_entry_words << 2) < len1) {
1002 /* the actual number of words in the entry is less
1003 * than elen and can still fit in the current page
1005 memmove(p, tmp, num_entry_words << 2);
1006 p += num_entry_words;
1009 cd->offset = cd->buffer + (cd->offset - tmp);
1011 unsigned int offset_r = (cd->offset - tmp) << 2;
1013 /* update pointer to offset location.
1014 * This is a 64bit quantity, so we need to
1015 * deal with 3 cases:
1016 * - entirely in first page
1017 * - entirely in second page
1018 * - 4 bytes in each page
1020 if (offset_r + 8 <= len1) {
1021 cd->offset = p + (cd->offset - tmp);
1022 } else if (offset_r >= len1) {
1023 cd->offset -= len1 >> 2;
1025 /* sitting on the fence */
1026 BUG_ON(offset_r != len1 - 4);
1027 cd->offset = p + (cd->offset - tmp);
1031 len2 = (num_entry_words << 2) - len1;
1033 /* move from temp page to current and next pages */
1034 memmove(p, tmp, len1);
1035 memmove(tmp, (caddr_t)tmp+len1, len2);
1037 p = tmp + (len2 >> 2);
1041 cd->common.err = nfserr_toosmall;
1045 cd->buflen -= num_entry_words;
1047 cd->common.err = nfs_ok;
1053 nfs3svc_encode_entry(void *cd, const char *name,
1054 int namlen, loff_t offset, u64 ino, unsigned int d_type)
1056 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1060 nfs3svc_encode_entry_plus(void *cd, const char *name,
1061 int namlen, loff_t offset, u64 ino,
1062 unsigned int d_type)
1064 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1069 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
1071 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
1072 struct kstatfs *s = &resp->stats;
1073 u64 bs = s->f_bsize;
1075 *p++ = resp->status;
1076 *p++ = xdr_zero; /* no post_op_attr */
1078 if (resp->status == 0) {
1079 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1080 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1081 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1082 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1083 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1084 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1085 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1087 return xdr_ressize_check(rqstp, p);
1092 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
1094 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1096 *p++ = resp->status;
1097 *p++ = xdr_zero; /* no post_op_attr */
1099 if (resp->status == 0) {
1100 *p++ = htonl(resp->f_rtmax);
1101 *p++ = htonl(resp->f_rtpref);
1102 *p++ = htonl(resp->f_rtmult);
1103 *p++ = htonl(resp->f_wtmax);
1104 *p++ = htonl(resp->f_wtpref);
1105 *p++ = htonl(resp->f_wtmult);
1106 *p++ = htonl(resp->f_dtpref);
1107 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1110 *p++ = htonl(resp->f_properties);
1113 return xdr_ressize_check(rqstp, p);
1118 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
1120 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1122 *p++ = resp->status;
1123 *p++ = xdr_zero; /* no post_op_attr */
1125 if (resp->status == 0) {
1126 *p++ = htonl(resp->p_link_max);
1127 *p++ = htonl(resp->p_name_max);
1128 *p++ = htonl(resp->p_no_trunc);
1129 *p++ = htonl(resp->p_chown_restricted);
1130 *p++ = htonl(resp->p_case_insensitive);
1131 *p++ = htonl(resp->p_case_preserving);
1134 return xdr_ressize_check(rqstp, p);
1139 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
1141 struct nfsd3_commitres *resp = rqstp->rq_resp;
1143 *p++ = resp->status;
1144 p = encode_wcc_data(rqstp, p, &resp->fh);
1145 /* Write verifier */
1146 if (resp->status == 0) {
1147 *p++ = resp->verf[0];
1148 *p++ = resp->verf[1];
1150 return xdr_ressize_check(rqstp, p);
1154 * XDR release functions
1157 nfs3svc_release_fhandle(struct svc_rqst *rqstp)
1159 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1165 nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
1167 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;