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>
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 (!fhp->fh_no_wcc && 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)
262 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
265 if (fhp->fh_no_wcc || 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_change = nfsd4_change_attribute(&stat, inode);
278 fhp->fh_pre_mtime = stat.mtime;
279 fhp->fh_pre_ctime = stat.ctime;
280 fhp->fh_pre_size = stat.size;
281 fhp->fh_pre_saved = true;
285 * Fill in the post_op attr for the wcc data
287 void fill_post_wcc(struct svc_fh *fhp)
289 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
290 struct inode *inode = d_inode(fhp->fh_dentry);
296 if (fhp->fh_post_saved)
297 printk("nfsd: inode locked twice during operation.\n");
299 err = fh_getattr(fhp, &fhp->fh_post_attr);
301 fhp->fh_post_saved = false;
302 fhp->fh_post_attr.ctime = inode->i_ctime;
304 fhp->fh_post_saved = true;
306 fhp->fh_post_change =
307 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
311 * XDR decode functions
315 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p)
317 struct nfsd_fhandle *args = rqstp->rq_argp;
319 p = decode_fh(p, &args->fh);
322 return xdr_argsize_check(rqstp, p);
326 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
328 struct nfsd3_sattrargs *args = rqstp->rq_argp;
330 p = decode_fh(p, &args->fh);
333 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
335 if ((args->check_guard = ntohl(*p++)) != 0) {
336 struct timespec64 time;
337 p = decode_time3(p, &time);
338 args->guardtime = time.tv_sec;
341 return xdr_argsize_check(rqstp, p);
345 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
347 struct nfsd3_diropargs *args = rqstp->rq_argp;
349 if (!(p = decode_fh(p, &args->fh))
350 || !(p = decode_filename(p, &args->name, &args->len)))
353 return xdr_argsize_check(rqstp, p);
357 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
359 struct nfsd3_accessargs *args = rqstp->rq_argp;
361 p = decode_fh(p, &args->fh);
364 args->access = ntohl(*p++);
366 return xdr_argsize_check(rqstp, p);
370 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
372 struct nfsd3_readargs *args = rqstp->rq_argp;
375 u32 max_blocksize = svc_max_payload(rqstp);
377 p = decode_fh(p, &args->fh);
380 p = xdr_decode_hyper(p, &args->offset);
382 args->count = ntohl(*p++);
383 len = min(args->count, max_blocksize);
385 /* set up the kvec */
388 struct page *p = *(rqstp->rq_next_page++);
390 rqstp->rq_vec[v].iov_base = page_address(p);
391 rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
392 len -= rqstp->rq_vec[v].iov_len;
396 return xdr_argsize_check(rqstp, p);
400 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
402 struct nfsd3_writeargs *args = rqstp->rq_argp;
403 unsigned int len, hdr, dlen;
404 u32 max_blocksize = svc_max_payload(rqstp);
405 struct kvec *head = rqstp->rq_arg.head;
406 struct kvec *tail = rqstp->rq_arg.tail;
408 p = decode_fh(p, &args->fh);
411 p = xdr_decode_hyper(p, &args->offset);
413 args->count = ntohl(*p++);
414 args->stable = ntohl(*p++);
415 len = args->len = ntohl(*p++);
416 if ((void *)p > head->iov_base + head->iov_len)
419 * The count must equal the amount of data passed.
421 if (args->count != args->len)
425 * Check to make sure that we got the right number of
428 hdr = (void*)p - head->iov_base;
429 dlen = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len - hdr;
431 * Round the length of the data which was specified up to
432 * the next multiple of XDR units and then compare that
433 * against the length which was actually received.
434 * Note that when RPCSEC/GSS (for example) is used, the
435 * data buffer can be padded so dlen might be larger
436 * than required. It must never be smaller.
438 if (dlen < XDR_QUADLEN(len)*4)
441 if (args->count > max_blocksize) {
442 args->count = max_blocksize;
443 len = args->len = max_blocksize;
446 args->first.iov_base = (void *)p;
447 args->first.iov_len = head->iov_len - hdr;
452 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
454 struct nfsd3_createargs *args = rqstp->rq_argp;
456 if (!(p = decode_fh(p, &args->fh))
457 || !(p = decode_filename(p, &args->name, &args->len)))
460 switch (args->createmode = ntohl(*p++)) {
461 case NFS3_CREATE_UNCHECKED:
462 case NFS3_CREATE_GUARDED:
463 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
465 case NFS3_CREATE_EXCLUSIVE:
473 return xdr_argsize_check(rqstp, p);
477 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
479 struct nfsd3_createargs *args = rqstp->rq_argp;
481 if (!(p = decode_fh(p, &args->fh)) ||
482 !(p = decode_filename(p, &args->name, &args->len)))
484 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
486 return xdr_argsize_check(rqstp, p);
490 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
492 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
493 char *base = (char *)p;
496 if (!(p = decode_fh(p, &args->ffh)) ||
497 !(p = decode_filename(p, &args->fname, &args->flen)))
499 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
501 args->tlen = ntohl(*p++);
503 args->first.iov_base = p;
504 args->first.iov_len = rqstp->rq_arg.head[0].iov_len;
505 args->first.iov_len -= (char *)p - base;
507 dlen = args->first.iov_len + rqstp->rq_arg.page_len +
508 rqstp->rq_arg.tail[0].iov_len;
509 if (dlen < XDR_QUADLEN(args->tlen) << 2)
515 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
517 struct nfsd3_mknodargs *args = rqstp->rq_argp;
519 if (!(p = decode_fh(p, &args->fh))
520 || !(p = decode_filename(p, &args->name, &args->len)))
523 args->ftype = ntohl(*p++);
525 if (args->ftype == NF3BLK || args->ftype == NF3CHR
526 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
527 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
529 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
530 args->major = ntohl(*p++);
531 args->minor = ntohl(*p++);
534 return xdr_argsize_check(rqstp, p);
538 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
540 struct nfsd3_renameargs *args = rqstp->rq_argp;
542 if (!(p = decode_fh(p, &args->ffh))
543 || !(p = decode_filename(p, &args->fname, &args->flen))
544 || !(p = decode_fh(p, &args->tfh))
545 || !(p = decode_filename(p, &args->tname, &args->tlen)))
548 return xdr_argsize_check(rqstp, p);
552 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p)
554 struct nfsd3_readlinkargs *args = rqstp->rq_argp;
556 p = decode_fh(p, &args->fh);
559 args->buffer = page_address(*(rqstp->rq_next_page++));
561 return xdr_argsize_check(rqstp, p);
565 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
567 struct nfsd3_linkargs *args = rqstp->rq_argp;
569 if (!(p = decode_fh(p, &args->ffh))
570 || !(p = decode_fh(p, &args->tfh))
571 || !(p = decode_filename(p, &args->tname, &args->tlen)))
574 return xdr_argsize_check(rqstp, p);
578 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
580 struct nfsd3_readdirargs *args = rqstp->rq_argp;
582 u32 max_blocksize = svc_max_payload(rqstp);
584 p = decode_fh(p, &args->fh);
587 p = xdr_decode_hyper(p, &args->cookie);
588 args->verf = p; p += 2;
590 args->count = ntohl(*p++);
591 len = args->count = min_t(u32, args->count, max_blocksize);
594 struct page *p = *(rqstp->rq_next_page++);
596 args->buffer = page_address(p);
600 return xdr_argsize_check(rqstp, p);
604 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
606 struct nfsd3_readdirargs *args = rqstp->rq_argp;
608 u32 max_blocksize = svc_max_payload(rqstp);
610 p = decode_fh(p, &args->fh);
613 p = xdr_decode_hyper(p, &args->cookie);
614 args->verf = p; p += 2;
615 args->dircount = ntohl(*p++);
616 args->count = ntohl(*p++);
618 len = args->count = min(args->count, max_blocksize);
620 struct page *p = *(rqstp->rq_next_page++);
622 args->buffer = page_address(p);
626 return xdr_argsize_check(rqstp, p);
630 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
632 struct nfsd3_commitargs *args = rqstp->rq_argp;
633 p = decode_fh(p, &args->fh);
636 p = xdr_decode_hyper(p, &args->offset);
637 args->count = ntohl(*p++);
639 return xdr_argsize_check(rqstp, p);
643 * XDR encode functions
648 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p)
650 struct nfsd3_attrstat *resp = rqstp->rq_resp;
653 if (resp->status == 0) {
654 lease_get_mtime(d_inode(resp->fh.fh_dentry),
656 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
658 return xdr_ressize_check(rqstp, p);
661 /* SETATTR, REMOVE, RMDIR */
663 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
665 struct nfsd3_attrstat *resp = rqstp->rq_resp;
668 p = encode_wcc_data(rqstp, p, &resp->fh);
669 return xdr_ressize_check(rqstp, p);
674 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
676 struct nfsd3_diropres *resp = rqstp->rq_resp;
679 if (resp->status == 0) {
680 p = encode_fh(p, &resp->fh);
681 p = encode_post_op_attr(rqstp, p, &resp->fh);
683 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
684 return xdr_ressize_check(rqstp, p);
689 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
691 struct nfsd3_accessres *resp = rqstp->rq_resp;
694 p = encode_post_op_attr(rqstp, p, &resp->fh);
695 if (resp->status == 0)
696 *p++ = htonl(resp->access);
697 return xdr_ressize_check(rqstp, p);
702 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
704 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
705 struct kvec *head = rqstp->rq_res.head;
708 p = encode_post_op_attr(rqstp, p, &resp->fh);
709 if (resp->status == 0) {
710 *p++ = htonl(resp->len);
711 xdr_ressize_check(rqstp, p);
712 rqstp->rq_res.page_len = resp->len;
714 /* need to pad the tail */
715 rqstp->rq_res.tail[0].iov_base = p;
717 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
719 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len))
723 return xdr_ressize_check(rqstp, p);
728 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
730 struct nfsd3_readres *resp = rqstp->rq_resp;
731 struct kvec *head = rqstp->rq_res.head;
734 p = encode_post_op_attr(rqstp, p, &resp->fh);
735 if (resp->status == 0) {
736 *p++ = htonl(resp->count);
737 *p++ = htonl(resp->eof);
738 *p++ = htonl(resp->count); /* xdr opaque count */
739 xdr_ressize_check(rqstp, p);
740 /* now update rqstp->rq_res to reflect data as well */
741 rqstp->rq_res.page_len = resp->count;
742 if (resp->count & 3) {
743 /* need to pad the tail */
744 rqstp->rq_res.tail[0].iov_base = p;
746 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
748 if (svc_encode_result_payload(rqstp, head->iov_len,
753 return xdr_ressize_check(rqstp, p);
758 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
760 struct nfsd3_writeres *resp = rqstp->rq_resp;
763 p = encode_wcc_data(rqstp, p, &resp->fh);
764 if (resp->status == 0) {
765 *p++ = htonl(resp->count);
766 *p++ = htonl(resp->committed);
767 *p++ = resp->verf[0];
768 *p++ = resp->verf[1];
770 return xdr_ressize_check(rqstp, p);
773 /* CREATE, MKDIR, SYMLINK, MKNOD */
775 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
777 struct nfsd3_diropres *resp = rqstp->rq_resp;
780 if (resp->status == 0) {
782 p = encode_fh(p, &resp->fh);
783 p = encode_post_op_attr(rqstp, p, &resp->fh);
785 p = encode_wcc_data(rqstp, p, &resp->dirfh);
786 return xdr_ressize_check(rqstp, p);
791 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
793 struct nfsd3_renameres *resp = rqstp->rq_resp;
796 p = encode_wcc_data(rqstp, p, &resp->ffh);
797 p = encode_wcc_data(rqstp, p, &resp->tfh);
798 return xdr_ressize_check(rqstp, p);
803 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
805 struct nfsd3_linkres *resp = rqstp->rq_resp;
808 p = encode_post_op_attr(rqstp, p, &resp->fh);
809 p = encode_wcc_data(rqstp, p, &resp->tfh);
810 return xdr_ressize_check(rqstp, p);
815 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
817 struct nfsd3_readdirres *resp = rqstp->rq_resp;
820 p = encode_post_op_attr(rqstp, p, &resp->fh);
822 if (resp->status == 0) {
823 /* stupid readdir cookie */
824 memcpy(p, resp->verf, 8); p += 2;
825 xdr_ressize_check(rqstp, p);
826 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
827 return 1; /*No room for trailer */
828 rqstp->rq_res.page_len = (resp->count) << 2;
830 /* add the 'tail' to the end of the 'head' page - page 0. */
831 rqstp->rq_res.tail[0].iov_base = p;
832 *p++ = 0; /* no more entries */
833 *p++ = htonl(resp->common.err == nfserr_eof);
834 rqstp->rq_res.tail[0].iov_len = 2<<2;
837 return xdr_ressize_check(rqstp, p);
841 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
844 *p++ = xdr_one; /* mark entry present */
845 p = xdr_encode_hyper(p, ino); /* file id */
846 p = xdr_encode_array(p, name, namlen);/* name length & name */
848 cd->offset = p; /* remember pointer */
849 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
855 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
856 const char *name, int namlen, u64 ino)
858 struct svc_export *exp;
859 struct dentry *dparent, *dchild;
860 __be32 rv = nfserr_noent;
862 dparent = cd->fh.fh_dentry;
863 exp = cd->fh.fh_export;
865 if (isdotent(name, namlen)) {
867 dchild = dget_parent(dparent);
868 /* filesystem root - cannot return filehandle for ".." */
869 if (dchild == dparent)
872 dchild = dget(dparent);
874 dchild = lookup_positive_unlocked(name, dparent, namlen);
877 if (d_mountpoint(dchild))
879 if (dchild->d_inode->i_ino != ino)
881 rv = fh_compose(fhp, exp, dchild, &cd->fh);
887 static __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, int namlen, u64 ino)
889 struct svc_fh *fh = &cd->scratch;
892 fh_init(fh, NFS3_FHSIZE);
893 err = compose_entry_fh(cd, fh, name, namlen, ino);
899 p = encode_post_op_attr(cd->rqstp, p, fh);
900 *p++ = xdr_one; /* yes, a file handle follows */
901 p = encode_fh(p, fh);
908 * Encode a directory entry. This one works for both normal readdir
910 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
911 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
913 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
917 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
918 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
920 encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
921 loff_t offset, u64 ino, unsigned int d_type, int plus)
923 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
925 __be32 *p = cd->buffer;
926 caddr_t curr_page_addr = NULL;
928 int slen; /* string (name) length */
929 int elen; /* estimated entry length in words */
930 int num_entry_words = 0; /* actual number of words */
933 u64 offset64 = offset;
935 if (unlikely(cd->offset1)) {
936 /* we ended up with offset on a page boundary */
937 *cd->offset = htonl(offset64 >> 32);
938 *cd->offset1 = htonl(offset64 & 0xffffffff);
941 xdr_encode_hyper(cd->offset, offset64);
947 dprintk("encode_entry(%.*s @%ld%s)\n",
948 namlen, name, (long) offset, plus? " plus" : "");
951 /* truncate filename if too long */
952 namlen = min(namlen, NFS3_MAXNAMLEN);
954 slen = XDR_QUADLEN(namlen);
955 elen = slen + NFS3_ENTRY_BAGGAGE
956 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
958 if (cd->buflen < elen) {
959 cd->common.err = nfserr_toosmall;
963 /* determine which page in rq_respages[] we are currently filling */
964 for (page = cd->rqstp->rq_respages + 1;
965 page < cd->rqstp->rq_next_page; page++) {
966 curr_page_addr = page_address(*page);
968 if (((caddr_t)cd->buffer >= curr_page_addr) &&
969 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
973 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
974 /* encode entry in current page */
976 p = encode_entry_baggage(cd, p, name, namlen, ino);
979 p = encode_entryplus_baggage(cd, p, name, namlen, ino);
980 num_entry_words = p - cd->buffer;
981 } else if (*(page+1) != NULL) {
982 /* temporarily encode entry into next page, then move back to
983 * current and next page in rq_respages[] */
987 /* grab next page for temporary storage of entry */
988 p1 = tmp = page_address(*(page+1));
990 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
993 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
995 /* determine entry word length and lengths to go in pages */
996 num_entry_words = p1 - tmp;
997 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
998 if ((num_entry_words << 2) < len1) {
999 /* the actual number of words in the entry is less
1000 * than elen and can still fit in the current page
1002 memmove(p, tmp, num_entry_words << 2);
1003 p += num_entry_words;
1006 cd->offset = cd->buffer + (cd->offset - tmp);
1008 unsigned int offset_r = (cd->offset - tmp) << 2;
1010 /* update pointer to offset location.
1011 * This is a 64bit quantity, so we need to
1012 * deal with 3 cases:
1013 * - entirely in first page
1014 * - entirely in second page
1015 * - 4 bytes in each page
1017 if (offset_r + 8 <= len1) {
1018 cd->offset = p + (cd->offset - tmp);
1019 } else if (offset_r >= len1) {
1020 cd->offset -= len1 >> 2;
1022 /* sitting on the fence */
1023 BUG_ON(offset_r != len1 - 4);
1024 cd->offset = p + (cd->offset - tmp);
1028 len2 = (num_entry_words << 2) - len1;
1030 /* move from temp page to current and next pages */
1031 memmove(p, tmp, len1);
1032 memmove(tmp, (caddr_t)tmp+len1, len2);
1034 p = tmp + (len2 >> 2);
1038 cd->common.err = nfserr_toosmall;
1042 cd->buflen -= num_entry_words;
1044 cd->common.err = nfs_ok;
1050 nfs3svc_encode_entry(void *cd, const char *name,
1051 int namlen, loff_t offset, u64 ino, unsigned int d_type)
1053 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1057 nfs3svc_encode_entry_plus(void *cd, const char *name,
1058 int namlen, loff_t offset, u64 ino,
1059 unsigned int d_type)
1061 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1066 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
1068 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
1069 struct kstatfs *s = &resp->stats;
1070 u64 bs = s->f_bsize;
1072 *p++ = resp->status;
1073 *p++ = xdr_zero; /* no post_op_attr */
1075 if (resp->status == 0) {
1076 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1077 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1078 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1079 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1080 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1081 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1082 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1084 return xdr_ressize_check(rqstp, p);
1089 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
1091 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1093 *p++ = resp->status;
1094 *p++ = xdr_zero; /* no post_op_attr */
1096 if (resp->status == 0) {
1097 *p++ = htonl(resp->f_rtmax);
1098 *p++ = htonl(resp->f_rtpref);
1099 *p++ = htonl(resp->f_rtmult);
1100 *p++ = htonl(resp->f_wtmax);
1101 *p++ = htonl(resp->f_wtpref);
1102 *p++ = htonl(resp->f_wtmult);
1103 *p++ = htonl(resp->f_dtpref);
1104 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1107 *p++ = htonl(resp->f_properties);
1110 return xdr_ressize_check(rqstp, p);
1115 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
1117 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1119 *p++ = resp->status;
1120 *p++ = xdr_zero; /* no post_op_attr */
1122 if (resp->status == 0) {
1123 *p++ = htonl(resp->p_link_max);
1124 *p++ = htonl(resp->p_name_max);
1125 *p++ = htonl(resp->p_no_trunc);
1126 *p++ = htonl(resp->p_chown_restricted);
1127 *p++ = htonl(resp->p_case_insensitive);
1128 *p++ = htonl(resp->p_case_preserving);
1131 return xdr_ressize_check(rqstp, p);
1136 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
1138 struct nfsd3_commitres *resp = rqstp->rq_resp;
1140 *p++ = resp->status;
1141 p = encode_wcc_data(rqstp, p, &resp->fh);
1142 /* Write verifier */
1143 if (resp->status == 0) {
1144 *p++ = resp->verf[0];
1145 *p++ = resp->verf[1];
1147 return xdr_ressize_check(rqstp, p);
1151 * XDR release functions
1154 nfs3svc_release_fhandle(struct svc_rqst *rqstp)
1156 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1162 nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
1164 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;