2 * linux/fs/nfsd/nfs3xdr.c
4 * XDR support for nfsd/protocol version 3.
6 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
8 * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
11 #include <linux/types.h>
12 #include <linux/time.h>
13 #include <linux/nfs3.h>
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <linux/dcache.h>
17 #include <linux/namei.h>
19 #include <linux/vfs.h>
20 #include <linux/sunrpc/xdr.h>
21 #include <linux/sunrpc/svc.h>
22 #include <linux/nfsd/nfsd.h>
23 #include <linux/nfsd/xdr3.h>
24 #include <linux/nfsacl.h>
26 #define NFSDDBG_FACILITY NFSDDBG_XDR
28 #ifdef NFSD_OPTIMIZE_SPACE
34 * Mapping of S_IF* types to NFS file types
36 static u32 nfs3_ftypes[] = {
37 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
38 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
39 NF3REG, NF3BAD, NF3LNK, NF3BAD,
40 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
44 * XDR functions for basic NFS types
47 encode_time3(u32 *p, struct timespec *time)
49 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
54 decode_time3(u32 *p, struct timespec *time)
56 time->tv_sec = ntohl(*p++);
57 time->tv_nsec = ntohl(*p++);
62 decode_fh(u32 *p, struct svc_fh *fhp)
65 fh_init(fhp, NFS3_FHSIZE);
67 if (size > NFS3_FHSIZE)
70 memcpy(&fhp->fh_handle.fh_base, p, size);
71 fhp->fh_handle.fh_size = size;
72 return p + XDR_QUADLEN(size);
76 encode_fh(u32 *p, struct svc_fh *fhp)
78 unsigned int size = fhp->fh_handle.fh_size;
80 if (size) p[XDR_QUADLEN(size)-1]=0;
81 memcpy(p, &fhp->fh_handle.fh_base, size);
82 return p + XDR_QUADLEN(size);
86 * Decode a file name and make sure that the path contains
87 * no slashes or null bytes.
90 decode_filename(u32 *p, char **namp, int *lenp)
95 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
96 for (i = 0, name = *namp; i < *lenp; i++, name++) {
97 if (*name == '\0' || *name == '/')
106 decode_pathname(u32 *p, char **namp, int *lenp)
111 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXPATHLEN)) != NULL) {
112 for (i = 0, name = *namp; i < *lenp; i++, name++) {
122 decode_sattr3(u32 *p, struct iattr *iap)
129 iap->ia_valid |= ATTR_MODE;
130 iap->ia_mode = ntohl(*p++);
133 iap->ia_valid |= ATTR_UID;
134 iap->ia_uid = ntohl(*p++);
137 iap->ia_valid |= ATTR_GID;
138 iap->ia_gid = ntohl(*p++);
143 iap->ia_valid |= ATTR_SIZE;
144 p = xdr_decode_hyper(p, &newsize);
145 if (newsize <= NFS_OFFSET_MAX)
146 iap->ia_size = newsize;
148 iap->ia_size = NFS_OFFSET_MAX;
150 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
151 iap->ia_valid |= ATTR_ATIME;
152 } else if (tmp == 2) { /* set to client time */
153 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
154 iap->ia_atime.tv_sec = ntohl(*p++);
155 iap->ia_atime.tv_nsec = ntohl(*p++);
157 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
158 iap->ia_valid |= ATTR_MTIME;
159 } else if (tmp == 2) { /* set to client time */
160 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
161 iap->ia_mtime.tv_sec = ntohl(*p++);
162 iap->ia_mtime.tv_nsec = ntohl(*p++);
168 encode_fattr3(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
170 struct vfsmount *mnt = fhp->fh_export->ex_mnt;
171 struct dentry *dentry = fhp->fh_dentry;
173 struct timespec time;
175 vfs_getattr(mnt, dentry, &stat);
177 *p++ = htonl(nfs3_ftypes[(stat.mode & S_IFMT) >> 12]);
178 *p++ = htonl((u32) stat.mode);
179 *p++ = htonl((u32) stat.nlink);
180 *p++ = htonl((u32) nfsd_ruid(rqstp, stat.uid));
181 *p++ = htonl((u32) nfsd_rgid(rqstp, stat.gid));
182 if (S_ISLNK(stat.mode) && stat.size > NFS3_MAXPATHLEN) {
183 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
185 p = xdr_encode_hyper(p, (u64) stat.size);
187 p = xdr_encode_hyper(p, ((u64)stat.blocks) << 9);
188 *p++ = htonl((u32) MAJOR(stat.rdev));
189 *p++ = htonl((u32) MINOR(stat.rdev));
190 if (is_fsid(fhp, rqstp->rq_reffh))
191 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
193 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat.dev));
194 p = xdr_encode_hyper(p, (u64) stat.ino);
195 p = encode_time3(p, &stat.atime);
196 lease_get_mtime(dentry->d_inode, &time);
197 p = encode_time3(p, &time);
198 p = encode_time3(p, &stat.ctime);
204 encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
206 struct inode *inode = fhp->fh_dentry->d_inode;
208 /* Attributes to follow */
211 *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
212 *p++ = htonl((u32) fhp->fh_post_mode);
213 *p++ = htonl((u32) fhp->fh_post_nlink);
214 *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
215 *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
216 if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
217 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
219 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
221 p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
222 *p++ = fhp->fh_post_rdev[0];
223 *p++ = fhp->fh_post_rdev[1];
224 if (is_fsid(fhp, rqstp->rq_reffh))
225 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
227 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
228 p = xdr_encode_hyper(p, (u64) inode->i_ino);
229 p = encode_time3(p, &fhp->fh_post_atime);
230 p = encode_time3(p, &fhp->fh_post_mtime);
231 p = encode_time3(p, &fhp->fh_post_ctime);
237 * Encode post-operation attributes.
238 * The inode may be NULL if the call failed because of a stale file
239 * handle. In this case, no attributes are returned.
242 encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
244 struct dentry *dentry = fhp->fh_dentry;
245 if (dentry && dentry->d_inode != NULL) {
246 *p++ = xdr_one; /* attributes follow */
247 return encode_fattr3(rqstp, p, fhp);
254 * Enocde weak cache consistency data
257 encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
259 struct dentry *dentry = fhp->fh_dentry;
261 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
262 if (fhp->fh_pre_saved) {
264 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
265 p = encode_time3(p, &fhp->fh_pre_mtime);
266 p = encode_time3(p, &fhp->fh_pre_ctime);
270 return encode_saved_post_attr(rqstp, p, fhp);
272 /* no pre- or post-attrs */
274 return encode_post_op_attr(rqstp, p, fhp);
279 * XDR decode functions
282 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
284 if (!(p = decode_fh(p, &args->fh)))
286 return xdr_argsize_check(rqstp, p);
290 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
291 struct nfsd3_sattrargs *args)
293 if (!(p = decode_fh(p, &args->fh))
294 || !(p = decode_sattr3(p, &args->attrs)))
297 if ((args->check_guard = ntohl(*p++)) != 0) {
298 struct timespec time;
299 p = decode_time3(p, &time);
300 args->guardtime = time.tv_sec;
303 return xdr_argsize_check(rqstp, p);
307 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
308 struct nfsd3_diropargs *args)
310 if (!(p = decode_fh(p, &args->fh))
311 || !(p = decode_filename(p, &args->name, &args->len)))
314 return xdr_argsize_check(rqstp, p);
318 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
319 struct nfsd3_accessargs *args)
321 if (!(p = decode_fh(p, &args->fh)))
323 args->access = ntohl(*p++);
325 return xdr_argsize_check(rqstp, p);
329 nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
330 struct nfsd3_readargs *args)
335 if (!(p = decode_fh(p, &args->fh))
336 || !(p = xdr_decode_hyper(p, &args->offset)))
339 len = args->count = ntohl(*p++);
341 if (len > NFSSVC_MAXBLKSIZE)
342 len = NFSSVC_MAXBLKSIZE;
344 /* set up the kvec */
347 pn = rqstp->rq_resused;
348 svc_take_page(rqstp);
349 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
350 args->vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
351 len -= args->vec[v].iov_len;
355 return xdr_argsize_check(rqstp, p);
359 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
360 struct nfsd3_writeargs *args)
362 unsigned int len, v, hdr;
364 if (!(p = decode_fh(p, &args->fh))
365 || !(p = xdr_decode_hyper(p, &args->offset)))
368 args->count = ntohl(*p++);
369 args->stable = ntohl(*p++);
370 len = args->len = ntohl(*p++);
372 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
373 if (rqstp->rq_arg.len < len + hdr)
376 args->vec[0].iov_base = (void*)p;
377 args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
379 if (len > NFSSVC_MAXBLKSIZE)
380 len = NFSSVC_MAXBLKSIZE;
382 while (len > args->vec[v].iov_len) {
383 len -= args->vec[v].iov_len;
385 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]);
386 args->vec[v].iov_len = PAGE_SIZE;
388 args->vec[v].iov_len = len;
391 return args->count == args->len && args->vec[0].iov_len > 0;
395 nfs3svc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
396 struct nfsd3_createargs *args)
398 if (!(p = decode_fh(p, &args->fh))
399 || !(p = decode_filename(p, &args->name, &args->len)))
402 switch (args->createmode = ntohl(*p++)) {
403 case NFS3_CREATE_UNCHECKED:
404 case NFS3_CREATE_GUARDED:
405 if (!(p = decode_sattr3(p, &args->attrs)))
408 case NFS3_CREATE_EXCLUSIVE:
416 return xdr_argsize_check(rqstp, p);
419 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, u32 *p,
420 struct nfsd3_createargs *args)
422 if (!(p = decode_fh(p, &args->fh))
423 || !(p = decode_filename(p, &args->name, &args->len))
424 || !(p = decode_sattr3(p, &args->attrs)))
427 return xdr_argsize_check(rqstp, p);
431 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
432 struct nfsd3_symlinkargs *args)
439 if (!(p = decode_fh(p, &args->ffh))
440 || !(p = decode_filename(p, &args->fname, &args->flen))
441 || !(p = decode_sattr3(p, &args->attrs))
444 /* now decode the pathname, which might be larger than the first page.
445 * As we have to check for nul's anyway, we copy it into a new page
446 * This page appears in the rq_res.pages list, but as pages_len is always
447 * 0, it won't get in the way
449 svc_take_page(rqstp);
451 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
453 args->tname = new = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
455 /* first copy and check from the first page */
457 vec = &rqstp->rq_arg.head[0];
458 avail = vec->iov_len - (old - (char*)vec->iov_base);
459 while (len && avail && *old) {
464 /* now copy next page if there is one */
465 if (len && !avail && rqstp->rq_arg.page_len) {
466 avail = rqstp->rq_arg.page_len;
467 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
468 old = page_address(rqstp->rq_arg.pages[0]);
470 while (len && avail && *old) {
483 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, u32 *p,
484 struct nfsd3_mknodargs *args)
486 if (!(p = decode_fh(p, &args->fh))
487 || !(p = decode_filename(p, &args->name, &args->len)))
490 args->ftype = ntohl(*p++);
492 if (args->ftype == NF3BLK || args->ftype == NF3CHR
493 || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
494 if (!(p = decode_sattr3(p, &args->attrs)))
498 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
499 args->major = ntohl(*p++);
500 args->minor = ntohl(*p++);
503 return xdr_argsize_check(rqstp, p);
507 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
508 struct nfsd3_renameargs *args)
510 if (!(p = decode_fh(p, &args->ffh))
511 || !(p = decode_filename(p, &args->fname, &args->flen))
512 || !(p = decode_fh(p, &args->tfh))
513 || !(p = decode_filename(p, &args->tname, &args->tlen)))
516 return xdr_argsize_check(rqstp, p);
520 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p,
521 struct nfsd3_readlinkargs *args)
523 if (!(p = decode_fh(p, &args->fh)))
525 svc_take_page(rqstp);
526 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
528 return xdr_argsize_check(rqstp, p);
532 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
533 struct nfsd3_linkargs *args)
535 if (!(p = decode_fh(p, &args->ffh))
536 || !(p = decode_fh(p, &args->tfh))
537 || !(p = decode_filename(p, &args->tname, &args->tlen)))
540 return xdr_argsize_check(rqstp, p);
544 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
545 struct nfsd3_readdirargs *args)
547 if (!(p = decode_fh(p, &args->fh)))
549 p = xdr_decode_hyper(p, &args->cookie);
550 args->verf = p; p += 2;
552 args->count = ntohl(*p++);
554 if (args->count > PAGE_SIZE)
555 args->count = PAGE_SIZE;
557 svc_take_page(rqstp);
558 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
560 return xdr_argsize_check(rqstp, p);
564 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, u32 *p,
565 struct nfsd3_readdirargs *args)
569 if (!(p = decode_fh(p, &args->fh)))
571 p = xdr_decode_hyper(p, &args->cookie);
572 args->verf = p; p += 2;
573 args->dircount = ntohl(*p++);
574 args->count = ntohl(*p++);
576 len = (args->count > NFSSVC_MAXBLKSIZE) ? NFSSVC_MAXBLKSIZE :
583 pn = rqstp->rq_resused;
584 svc_take_page(rqstp);
586 args->buffer = page_address(rqstp->rq_respages[pn]);
590 return xdr_argsize_check(rqstp, p);
594 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, u32 *p,
595 struct nfsd3_commitargs *args)
597 if (!(p = decode_fh(p, &args->fh)))
599 p = xdr_decode_hyper(p, &args->offset);
600 args->count = ntohl(*p++);
602 return xdr_argsize_check(rqstp, p);
605 #ifdef CONFIG_NFSD_ACL
607 nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, u32 *p,
608 struct nfsd3_getaclargs *args)
610 if (!(p = decode_fh(p, &args->fh)))
612 args->mask = ntohl(*p); p++;
614 return xdr_argsize_check(rqstp, p);
616 #endif /* CONFIG_NFSD_ACL */
618 #ifdef CONFIG_NFSD_ACL
620 nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, u32 *p,
621 struct nfsd3_setaclargs *args)
623 struct iovec *head = rqstp->rq_arg.head;
627 if (!(p = decode_fh(p, &args->fh)))
629 args->mask = ntohl(*p++);
630 if (args->mask & ~(NFS3_ACL|NFS3_ACLCNT|NFS3_DFACL|NFS3_DFACLCNT) ||
631 !xdr_argsize_check(rqstp, p))
634 base = (char *)p - (char *)head->iov_base;
635 n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
636 (args->mask & NFS3_ACL) ?
637 &args->acl_access : NULL);
639 n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
640 (args->mask & NFS3_DFACL) ?
641 &args->acl_default : NULL);
644 #endif /* CONFIG_NFSD_ACL */
647 * XDR encode functions
650 * There must be an encoding function for void results so svc_process
651 * will work properly.
654 nfs3svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
656 return xdr_ressize_check(rqstp, p);
661 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
662 struct nfsd3_attrstat *resp)
664 if (resp->status == 0)
665 p = encode_fattr3(rqstp, p, &resp->fh);
666 return xdr_ressize_check(rqstp, p);
669 /* SETATTR, REMOVE, RMDIR */
671 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, u32 *p,
672 struct nfsd3_attrstat *resp)
674 p = encode_wcc_data(rqstp, p, &resp->fh);
675 return xdr_ressize_check(rqstp, p);
680 nfs3svc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
681 struct nfsd3_diropres *resp)
683 if (resp->status == 0) {
684 p = encode_fh(p, &resp->fh);
685 p = encode_post_op_attr(rqstp, p, &resp->fh);
687 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
688 return xdr_ressize_check(rqstp, p);
693 nfs3svc_encode_accessres(struct svc_rqst *rqstp, u32 *p,
694 struct nfsd3_accessres *resp)
696 p = encode_post_op_attr(rqstp, p, &resp->fh);
697 if (resp->status == 0)
698 *p++ = htonl(resp->access);
699 return xdr_ressize_check(rqstp, p);
704 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
705 struct nfsd3_readlinkres *resp)
707 p = encode_post_op_attr(rqstp, p, &resp->fh);
708 if (resp->status == 0) {
709 *p++ = htonl(resp->len);
710 xdr_ressize_check(rqstp, p);
711 rqstp->rq_res.page_len = resp->len;
713 /* need to pad the tail */
714 rqstp->rq_restailpage = 0;
715 rqstp->rq_res.tail[0].iov_base = p;
717 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
721 return xdr_ressize_check(rqstp, p);
726 nfs3svc_encode_readres(struct svc_rqst *rqstp, u32 *p,
727 struct nfsd3_readres *resp)
729 p = encode_post_op_attr(rqstp, p, &resp->fh);
730 if (resp->status == 0) {
731 *p++ = htonl(resp->count);
732 *p++ = htonl(resp->eof);
733 *p++ = htonl(resp->count); /* xdr opaque count */
734 xdr_ressize_check(rqstp, p);
735 /* now update rqstp->rq_res to reflect data aswell */
736 rqstp->rq_res.page_len = resp->count;
737 if (resp->count & 3) {
738 /* need to pad the tail */
739 rqstp->rq_restailpage = 0;
740 rqstp->rq_res.tail[0].iov_base = p;
742 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
746 return xdr_ressize_check(rqstp, p);
751 nfs3svc_encode_writeres(struct svc_rqst *rqstp, u32 *p,
752 struct nfsd3_writeres *resp)
754 p = encode_wcc_data(rqstp, p, &resp->fh);
755 if (resp->status == 0) {
756 *p++ = htonl(resp->count);
757 *p++ = htonl(resp->committed);
758 *p++ = htonl(nfssvc_boot.tv_sec);
759 *p++ = htonl(nfssvc_boot.tv_usec);
761 return xdr_ressize_check(rqstp, p);
764 /* CREATE, MKDIR, SYMLINK, MKNOD */
766 nfs3svc_encode_createres(struct svc_rqst *rqstp, u32 *p,
767 struct nfsd3_diropres *resp)
769 if (resp->status == 0) {
771 p = encode_fh(p, &resp->fh);
772 p = encode_post_op_attr(rqstp, p, &resp->fh);
774 p = encode_wcc_data(rqstp, p, &resp->dirfh);
775 return xdr_ressize_check(rqstp, p);
780 nfs3svc_encode_renameres(struct svc_rqst *rqstp, u32 *p,
781 struct nfsd3_renameres *resp)
783 p = encode_wcc_data(rqstp, p, &resp->ffh);
784 p = encode_wcc_data(rqstp, p, &resp->tfh);
785 return xdr_ressize_check(rqstp, p);
790 nfs3svc_encode_linkres(struct svc_rqst *rqstp, u32 *p,
791 struct nfsd3_linkres *resp)
793 p = encode_post_op_attr(rqstp, p, &resp->fh);
794 p = encode_wcc_data(rqstp, p, &resp->tfh);
795 return xdr_ressize_check(rqstp, p);
800 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
801 struct nfsd3_readdirres *resp)
803 p = encode_post_op_attr(rqstp, p, &resp->fh);
805 if (resp->status == 0) {
806 /* stupid readdir cookie */
807 memcpy(p, resp->verf, 8); p += 2;
808 xdr_ressize_check(rqstp, p);
809 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
810 return 1; /*No room for trailer */
811 rqstp->rq_res.page_len = (resp->count) << 2;
813 /* add the 'tail' to the end of the 'head' page - page 0. */
814 rqstp->rq_restailpage = 0;
815 rqstp->rq_res.tail[0].iov_base = p;
816 *p++ = 0; /* no more entries */
817 *p++ = htonl(resp->common.err == nfserr_eof);
818 rqstp->rq_res.tail[0].iov_len = 2<<2;
821 return xdr_ressize_check(rqstp, p);
825 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
826 int namlen, ino_t ino)
828 *p++ = xdr_one; /* mark entry present */
829 p = xdr_encode_hyper(p, ino); /* file id */
830 p = xdr_encode_array(p, name, namlen);/* name length & name */
832 cd->offset = p; /* remember pointer */
833 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
839 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
842 p = encode_post_op_attr(cd->rqstp, p, fhp);
843 *p++ = xdr_one; /* yes, a file handle follows */
844 p = encode_fh(p, fhp);
850 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
851 const char *name, int namlen)
853 struct svc_export *exp;
854 struct dentry *dparent, *dchild;
857 dparent = cd->fh.fh_dentry;
858 exp = cd->fh.fh_export;
860 fh_init(fhp, NFS3_FHSIZE);
861 if (isdotent(name, namlen)) {
863 dchild = dget_parent(dparent);
865 dchild = dget(dparent);
867 dchild = lookup_one_len(name, dparent, namlen);
870 if (d_mountpoint(dchild) ||
871 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
879 * Encode a directory entry. This one works for both normal readdir
881 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
882 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
884 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
888 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
889 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
891 encode_entry(struct readdir_cd *ccd, const char *name,
892 int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
894 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
897 caddr_t curr_page_addr = NULL;
898 int pn; /* current page number */
899 int slen; /* string (name) length */
900 int elen; /* estimated entry length in words */
901 int num_entry_words = 0; /* actual number of words */
904 u64 offset64 = offset;
906 if (unlikely(cd->offset1)) {
907 /* we ended up with offset on a page boundary */
908 *cd->offset = htonl(offset64 >> 32);
909 *cd->offset1 = htonl(offset64 & 0xffffffff);
912 xdr_encode_hyper(cd->offset, (u64) offset);
917 dprintk("encode_entry(%.*s @%ld%s)\n",
918 namlen, name, (long) offset, plus? " plus" : "");
921 /* truncate filename if too long */
922 if (namlen > NFS3_MAXNAMLEN)
923 namlen = NFS3_MAXNAMLEN;
925 slen = XDR_QUADLEN(namlen);
926 elen = slen + NFS3_ENTRY_BAGGAGE
927 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
929 if (cd->buflen < elen) {
930 cd->common.err = nfserr_toosmall;
934 /* determine which page in rq_respages[] we are currently filling */
935 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
936 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
938 if (((caddr_t)cd->buffer >= curr_page_addr) &&
939 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
943 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
944 /* encode entry in current page */
946 p = encode_entry_baggage(cd, p, name, namlen, ino);
948 /* throw in readdirplus baggage */
952 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
956 p = encode_entryplus_baggage(cd, p, &fh);
958 num_entry_words = p - cd->buffer;
959 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
960 /* temporarily encode entry into next page, then move back to
961 * current and next page in rq_respages[] */
965 /* grab next page for temporary storage of entry */
966 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
968 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
970 /* throw in readdirplus baggage */
974 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
975 /* zero out the filehandle */
979 p1 = encode_entryplus_baggage(cd, p1, &fh);
982 /* determine entry word length and lengths to go in pages */
983 num_entry_words = p1 - tmp;
984 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
985 if ((num_entry_words << 2) < len1) {
986 /* the actual number of words in the entry is less
987 * than elen and can still fit in the current page
989 memmove(p, tmp, num_entry_words << 2);
990 p += num_entry_words;
993 cd->offset = cd->buffer + (cd->offset - tmp);
995 unsigned int offset_r = (cd->offset - tmp) << 2;
997 /* update pointer to offset location.
998 * This is a 64bit quantity, so we need to
1000 * - entirely in first page
1001 * - entirely in second page
1002 * - 4 bytes in each page
1004 if (offset_r + 8 <= len1) {
1005 cd->offset = p + (cd->offset - tmp);
1006 } else if (offset_r >= len1) {
1007 cd->offset -= len1 >> 2;
1009 /* sitting on the fence */
1010 BUG_ON(offset_r != len1 - 4);
1011 cd->offset = p + (cd->offset - tmp);
1015 len2 = (num_entry_words << 2) - len1;
1017 /* move from temp page to current and next pages */
1018 memmove(p, tmp, len1);
1019 memmove(tmp, (caddr_t)tmp+len1, len2);
1021 p = tmp + (len2 >> 2);
1025 cd->common.err = nfserr_toosmall;
1029 cd->buflen -= num_entry_words;
1031 cd->common.err = nfs_ok;
1037 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
1038 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1040 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1044 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
1045 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1047 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1052 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1053 struct nfsd3_fsstatres *resp)
1055 struct kstatfs *s = &resp->stats;
1056 u64 bs = s->f_bsize;
1058 *p++ = xdr_zero; /* no post_op_attr */
1060 if (resp->status == 0) {
1061 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1062 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1063 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1064 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1065 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1066 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1067 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1069 return xdr_ressize_check(rqstp, p);
1074 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1075 struct nfsd3_fsinfores *resp)
1077 *p++ = xdr_zero; /* no post_op_attr */
1079 if (resp->status == 0) {
1080 *p++ = htonl(resp->f_rtmax);
1081 *p++ = htonl(resp->f_rtpref);
1082 *p++ = htonl(resp->f_rtmult);
1083 *p++ = htonl(resp->f_wtmax);
1084 *p++ = htonl(resp->f_wtpref);
1085 *p++ = htonl(resp->f_wtmult);
1086 *p++ = htonl(resp->f_dtpref);
1087 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1090 *p++ = htonl(resp->f_properties);
1093 return xdr_ressize_check(rqstp, p);
1098 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1099 struct nfsd3_pathconfres *resp)
1101 *p++ = xdr_zero; /* no post_op_attr */
1103 if (resp->status == 0) {
1104 *p++ = htonl(resp->p_link_max);
1105 *p++ = htonl(resp->p_name_max);
1106 *p++ = htonl(resp->p_no_trunc);
1107 *p++ = htonl(resp->p_chown_restricted);
1108 *p++ = htonl(resp->p_case_insensitive);
1109 *p++ = htonl(resp->p_case_preserving);
1112 return xdr_ressize_check(rqstp, p);
1117 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1118 struct nfsd3_commitres *resp)
1120 p = encode_wcc_data(rqstp, p, &resp->fh);
1121 /* Write verifier */
1122 if (resp->status == 0) {
1123 *p++ = htonl(nfssvc_boot.tv_sec);
1124 *p++ = htonl(nfssvc_boot.tv_usec);
1126 return xdr_ressize_check(rqstp, p);
1129 #ifdef CONFIG_NFSD_ACL
1132 nfs3svc_encode_getaclres(struct svc_rqst *rqstp, u32 *p,
1133 struct nfsd3_getaclres *resp)
1135 struct dentry *dentry = resp->fh.fh_dentry;
1137 p = encode_post_op_attr(rqstp, p, &resp->fh);
1138 if (resp->status == 0 && dentry && dentry->d_inode) {
1139 struct inode *inode = dentry->d_inode;
1140 int w = nfsacl_size(
1141 (resp->mask & NFS3_ACL) ? resp->acl_access : NULL,
1142 (resp->mask & NFS3_DFACL) ? resp->acl_default : NULL);
1143 struct iovec *head = rqstp->rq_res.head;
1147 *p++ = htonl(resp->mask);
1148 if (!xdr_ressize_check(rqstp, p))
1150 base = (char *)p - (char *)head->iov_base;
1152 rqstp->rq_res.page_len = w;
1154 if (!svc_take_res_page(rqstp))
1159 n = nfsacl_encode(&rqstp->rq_res, base, inode,
1161 resp->mask & NFS3_ACL, 0);
1163 n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
1165 resp->mask & NFS3_DFACL,
1170 if (!xdr_ressize_check(rqstp, p))
1175 #endif /* CONFIG_NFSD_ACL */
1177 #ifdef CONFIG_NFSD_ACL
1180 nfs3svc_encode_setaclres(struct svc_rqst *rqstp, u32 *p,
1181 struct nfsd3_attrstat *resp)
1183 p = encode_post_op_attr(rqstp, p, &resp->fh);
1185 return xdr_ressize_check(rqstp, p);
1187 #endif /* CONFIG_NFSD_ACL */
1190 * XDR release functions
1193 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1194 struct nfsd3_attrstat *resp)
1201 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1202 struct nfsd3_fhandle_pair *resp)
1209 #ifdef CONFIG_NFSD_ACL
1211 nfs3svc_release_getacl(struct svc_rqst *rqstp, u32 *p,
1212 struct nfsd3_getaclres *resp)
1215 posix_acl_release(resp->acl_access);
1216 posix_acl_release(resp->acl_default);
1219 #endif /* CONFIG_NFSD_ACL */