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/vserver/xid.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);
75 /* Helper function for NFSv3 ACL code */
76 u32 *nfs3svc_decode_fh(u32 *p, struct svc_fh *fhp)
78 return decode_fh(p, fhp);
82 encode_fh(u32 *p, struct svc_fh *fhp)
84 unsigned int size = fhp->fh_handle.fh_size;
86 if (size) p[XDR_QUADLEN(size)-1]=0;
87 memcpy(p, &fhp->fh_handle.fh_base, size);
88 return p + XDR_QUADLEN(size);
92 * Decode a file name and make sure that the path contains
93 * no slashes or null bytes.
96 decode_filename(u32 *p, char **namp, int *lenp)
101 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
102 for (i = 0, name = *namp; i < *lenp; i++, name++) {
103 if (*name == '\0' || *name == '/')
112 decode_sattr3(u32 *p, struct iattr *iap)
121 iap->ia_valid |= ATTR_MODE;
122 iap->ia_mode = ntohl(*p++);
125 iap->ia_valid |= ATTR_UID;
129 iap->ia_valid |= ATTR_GID;
132 iap->ia_uid = INOXID_UID(XID_TAG_NFSD, uid, gid);
133 iap->ia_gid = INOXID_GID(XID_TAG_NFSD, uid, gid);
134 iap->ia_xid = INOXID_XID(XID_TAG_NFSD, uid, gid, 0);
138 iap->ia_valid |= ATTR_SIZE;
139 p = xdr_decode_hyper(p, &newsize);
140 if (newsize <= NFS_OFFSET_MAX)
141 iap->ia_size = newsize;
143 iap->ia_size = NFS_OFFSET_MAX;
145 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
146 iap->ia_valid |= ATTR_ATIME;
147 } else if (tmp == 2) { /* set to client time */
148 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
149 iap->ia_atime.tv_sec = ntohl(*p++);
150 iap->ia_atime.tv_nsec = ntohl(*p++);
152 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
153 iap->ia_valid |= ATTR_MTIME;
154 } else if (tmp == 2) { /* set to client time */
155 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
156 iap->ia_mtime.tv_sec = ntohl(*p++);
157 iap->ia_mtime.tv_nsec = ntohl(*p++);
163 encode_fattr3(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp,
166 struct dentry *dentry = fhp->fh_dentry;
167 struct timespec time;
169 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
170 *p++ = htonl((u32) stat->mode);
171 *p++ = htonl((u32) stat->nlink);
172 *p++ = htonl((u32) nfsd_ruid(rqstp,
173 XIDINO_UID(XID_TAG(dentry->d_inode), stat->uid, stat->xid)));
174 *p++ = htonl((u32) nfsd_rgid(rqstp,
175 XIDINO_GID(XID_TAG(dentry->d_inode), stat->gid, stat->xid)));
176 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
177 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
179 p = xdr_encode_hyper(p, (u64) stat->size);
181 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
182 *p++ = htonl((u32) MAJOR(stat->rdev));
183 *p++ = htonl((u32) MINOR(stat->rdev));
184 if (is_fsid(fhp, rqstp->rq_reffh))
185 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
187 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat->dev));
188 p = xdr_encode_hyper(p, (u64) stat->ino);
189 p = encode_time3(p, &stat->atime);
190 lease_get_mtime(dentry->d_inode, &time);
191 p = encode_time3(p, &time);
192 p = encode_time3(p, &stat->ctime);
198 encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
200 struct inode *inode = fhp->fh_dentry->d_inode;
202 /* Attributes to follow */
205 *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
206 *p++ = htonl((u32) fhp->fh_post_mode);
207 *p++ = htonl((u32) fhp->fh_post_nlink);
208 *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
209 *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
210 if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
211 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
213 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
215 p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
216 *p++ = fhp->fh_post_rdev[0];
217 *p++ = fhp->fh_post_rdev[1];
218 if (is_fsid(fhp, rqstp->rq_reffh))
219 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
221 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
222 p = xdr_encode_hyper(p, (u64) inode->i_ino);
223 p = encode_time3(p, &fhp->fh_post_atime);
224 p = encode_time3(p, &fhp->fh_post_mtime);
225 p = encode_time3(p, &fhp->fh_post_ctime);
231 * Encode post-operation attributes.
232 * The inode may be NULL if the call failed because of a stale file
233 * handle. In this case, no attributes are returned.
236 encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
238 struct dentry *dentry = fhp->fh_dentry;
239 if (dentry && dentry->d_inode != NULL) {
243 err = vfs_getattr(fhp->fh_export->ex_mnt, dentry, &stat);
245 *p++ = xdr_one; /* attributes follow */
246 return encode_fattr3(rqstp, p, fhp, &stat);
253 /* Helper for NFSv3 ACLs */
255 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
257 return encode_post_op_attr(rqstp, p, fhp);
261 * Enocde weak cache consistency data
264 encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
266 struct dentry *dentry = fhp->fh_dentry;
268 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
269 if (fhp->fh_pre_saved) {
271 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
272 p = encode_time3(p, &fhp->fh_pre_mtime);
273 p = encode_time3(p, &fhp->fh_pre_ctime);
277 return encode_saved_post_attr(rqstp, p, fhp);
279 /* no pre- or post-attrs */
281 return encode_post_op_attr(rqstp, p, fhp);
286 * XDR decode functions
289 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
291 if (!(p = decode_fh(p, &args->fh)))
293 return xdr_argsize_check(rqstp, p);
297 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
298 struct nfsd3_sattrargs *args)
300 if (!(p = decode_fh(p, &args->fh))
301 || !(p = decode_sattr3(p, &args->attrs)))
304 if ((args->check_guard = ntohl(*p++)) != 0) {
305 struct timespec time;
306 p = decode_time3(p, &time);
307 args->guardtime = time.tv_sec;
310 return xdr_argsize_check(rqstp, p);
314 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
315 struct nfsd3_diropargs *args)
317 if (!(p = decode_fh(p, &args->fh))
318 || !(p = decode_filename(p, &args->name, &args->len)))
321 return xdr_argsize_check(rqstp, p);
325 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
326 struct nfsd3_accessargs *args)
328 if (!(p = decode_fh(p, &args->fh)))
330 args->access = ntohl(*p++);
332 return xdr_argsize_check(rqstp, p);
336 nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
337 struct nfsd3_readargs *args)
342 if (!(p = decode_fh(p, &args->fh))
343 || !(p = xdr_decode_hyper(p, &args->offset)))
346 len = args->count = ntohl(*p++);
348 if (len > NFSSVC_MAXBLKSIZE)
349 len = NFSSVC_MAXBLKSIZE;
351 /* set up the kvec */
354 pn = rqstp->rq_resused;
355 svc_take_page(rqstp);
356 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
357 args->vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
358 len -= args->vec[v].iov_len;
362 return xdr_argsize_check(rqstp, p);
366 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
367 struct nfsd3_writeargs *args)
369 unsigned int len, v, hdr;
371 if (!(p = decode_fh(p, &args->fh))
372 || !(p = xdr_decode_hyper(p, &args->offset)))
375 args->count = ntohl(*p++);
376 args->stable = ntohl(*p++);
377 len = args->len = ntohl(*p++);
379 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
380 if (rqstp->rq_arg.len < hdr ||
381 rqstp->rq_arg.len - hdr < len)
384 args->vec[0].iov_base = (void*)p;
385 args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
387 if (len > NFSSVC_MAXBLKSIZE)
388 len = NFSSVC_MAXBLKSIZE;
390 while (len > args->vec[v].iov_len) {
391 len -= args->vec[v].iov_len;
393 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]);
394 args->vec[v].iov_len = PAGE_SIZE;
396 args->vec[v].iov_len = len;
399 return args->count == args->len && args->vec[0].iov_len > 0;
403 nfs3svc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
404 struct nfsd3_createargs *args)
406 if (!(p = decode_fh(p, &args->fh))
407 || !(p = decode_filename(p, &args->name, &args->len)))
410 switch (args->createmode = ntohl(*p++)) {
411 case NFS3_CREATE_UNCHECKED:
412 case NFS3_CREATE_GUARDED:
413 if (!(p = decode_sattr3(p, &args->attrs)))
416 case NFS3_CREATE_EXCLUSIVE:
424 return xdr_argsize_check(rqstp, p);
427 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, u32 *p,
428 struct nfsd3_createargs *args)
430 if (!(p = decode_fh(p, &args->fh))
431 || !(p = decode_filename(p, &args->name, &args->len))
432 || !(p = decode_sattr3(p, &args->attrs)))
435 return xdr_argsize_check(rqstp, p);
439 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
440 struct nfsd3_symlinkargs *args)
447 if (!(p = decode_fh(p, &args->ffh))
448 || !(p = decode_filename(p, &args->fname, &args->flen))
449 || !(p = decode_sattr3(p, &args->attrs))
452 /* now decode the pathname, which might be larger than the first page.
453 * As we have to check for nul's anyway, we copy it into a new page
454 * This page appears in the rq_res.pages list, but as pages_len is always
455 * 0, it won't get in the way
457 svc_take_page(rqstp);
459 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
461 args->tname = new = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
463 /* first copy and check from the first page */
465 vec = &rqstp->rq_arg.head[0];
466 avail = vec->iov_len - (old - (char*)vec->iov_base);
467 while (len && avail && *old) {
472 /* now copy next page if there is one */
473 if (len && !avail && rqstp->rq_arg.page_len) {
474 avail = rqstp->rq_arg.page_len;
475 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
476 old = page_address(rqstp->rq_arg.pages[0]);
478 while (len && avail && *old) {
491 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, u32 *p,
492 struct nfsd3_mknodargs *args)
494 if (!(p = decode_fh(p, &args->fh))
495 || !(p = decode_filename(p, &args->name, &args->len)))
498 args->ftype = ntohl(*p++);
500 if (args->ftype == NF3BLK || args->ftype == NF3CHR
501 || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
502 if (!(p = decode_sattr3(p, &args->attrs)))
506 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
507 args->major = ntohl(*p++);
508 args->minor = ntohl(*p++);
511 return xdr_argsize_check(rqstp, p);
515 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
516 struct nfsd3_renameargs *args)
518 if (!(p = decode_fh(p, &args->ffh))
519 || !(p = decode_filename(p, &args->fname, &args->flen))
520 || !(p = decode_fh(p, &args->tfh))
521 || !(p = decode_filename(p, &args->tname, &args->tlen)))
524 return xdr_argsize_check(rqstp, p);
528 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p,
529 struct nfsd3_readlinkargs *args)
531 if (!(p = decode_fh(p, &args->fh)))
533 svc_take_page(rqstp);
534 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
536 return xdr_argsize_check(rqstp, p);
540 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
541 struct nfsd3_linkargs *args)
543 if (!(p = decode_fh(p, &args->ffh))
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_readdirargs(struct svc_rqst *rqstp, u32 *p,
553 struct nfsd3_readdirargs *args)
555 if (!(p = decode_fh(p, &args->fh)))
557 p = xdr_decode_hyper(p, &args->cookie);
558 args->verf = p; p += 2;
560 args->count = ntohl(*p++);
562 if (args->count > PAGE_SIZE)
563 args->count = PAGE_SIZE;
565 svc_take_page(rqstp);
566 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
568 return xdr_argsize_check(rqstp, p);
572 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, u32 *p,
573 struct nfsd3_readdirargs *args)
577 if (!(p = decode_fh(p, &args->fh)))
579 p = xdr_decode_hyper(p, &args->cookie);
580 args->verf = p; p += 2;
581 args->dircount = ntohl(*p++);
582 args->count = ntohl(*p++);
584 len = (args->count > NFSSVC_MAXBLKSIZE) ? NFSSVC_MAXBLKSIZE :
589 pn = rqstp->rq_resused;
590 svc_take_page(rqstp);
592 args->buffer = page_address(rqstp->rq_respages[pn]);
596 return xdr_argsize_check(rqstp, p);
600 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, u32 *p,
601 struct nfsd3_commitargs *args)
603 if (!(p = decode_fh(p, &args->fh)))
605 p = xdr_decode_hyper(p, &args->offset);
606 args->count = ntohl(*p++);
608 return xdr_argsize_check(rqstp, p);
612 * XDR encode functions
615 * There must be an encoding function for void results so svc_process
616 * will work properly.
619 nfs3svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
621 return xdr_ressize_check(rqstp, p);
626 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
627 struct nfsd3_attrstat *resp)
629 if (resp->status == 0)
630 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
631 return xdr_ressize_check(rqstp, p);
634 /* SETATTR, REMOVE, RMDIR */
636 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, u32 *p,
637 struct nfsd3_attrstat *resp)
639 p = encode_wcc_data(rqstp, p, &resp->fh);
640 return xdr_ressize_check(rqstp, p);
645 nfs3svc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
646 struct nfsd3_diropres *resp)
648 if (resp->status == 0) {
649 p = encode_fh(p, &resp->fh);
650 p = encode_post_op_attr(rqstp, p, &resp->fh);
652 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
653 return xdr_ressize_check(rqstp, p);
658 nfs3svc_encode_accessres(struct svc_rqst *rqstp, u32 *p,
659 struct nfsd3_accessres *resp)
661 p = encode_post_op_attr(rqstp, p, &resp->fh);
662 if (resp->status == 0)
663 *p++ = htonl(resp->access);
664 return xdr_ressize_check(rqstp, p);
669 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
670 struct nfsd3_readlinkres *resp)
672 p = encode_post_op_attr(rqstp, p, &resp->fh);
673 if (resp->status == 0) {
674 *p++ = htonl(resp->len);
675 xdr_ressize_check(rqstp, p);
676 rqstp->rq_res.page_len = resp->len;
678 /* need to pad the tail */
679 rqstp->rq_restailpage = 0;
680 rqstp->rq_res.tail[0].iov_base = p;
682 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
686 return xdr_ressize_check(rqstp, p);
691 nfs3svc_encode_readres(struct svc_rqst *rqstp, u32 *p,
692 struct nfsd3_readres *resp)
694 p = encode_post_op_attr(rqstp, p, &resp->fh);
695 if (resp->status == 0) {
696 *p++ = htonl(resp->count);
697 *p++ = htonl(resp->eof);
698 *p++ = htonl(resp->count); /* xdr opaque count */
699 xdr_ressize_check(rqstp, p);
700 /* now update rqstp->rq_res to reflect data aswell */
701 rqstp->rq_res.page_len = resp->count;
702 if (resp->count & 3) {
703 /* need to pad the tail */
704 rqstp->rq_restailpage = 0;
705 rqstp->rq_res.tail[0].iov_base = p;
707 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
711 return xdr_ressize_check(rqstp, p);
716 nfs3svc_encode_writeres(struct svc_rqst *rqstp, u32 *p,
717 struct nfsd3_writeres *resp)
719 p = encode_wcc_data(rqstp, p, &resp->fh);
720 if (resp->status == 0) {
721 *p++ = htonl(resp->count);
722 *p++ = htonl(resp->committed);
723 *p++ = htonl(nfssvc_boot.tv_sec);
724 *p++ = htonl(nfssvc_boot.tv_usec);
726 return xdr_ressize_check(rqstp, p);
729 /* CREATE, MKDIR, SYMLINK, MKNOD */
731 nfs3svc_encode_createres(struct svc_rqst *rqstp, u32 *p,
732 struct nfsd3_diropres *resp)
734 if (resp->status == 0) {
736 p = encode_fh(p, &resp->fh);
737 p = encode_post_op_attr(rqstp, p, &resp->fh);
739 p = encode_wcc_data(rqstp, p, &resp->dirfh);
740 return xdr_ressize_check(rqstp, p);
745 nfs3svc_encode_renameres(struct svc_rqst *rqstp, u32 *p,
746 struct nfsd3_renameres *resp)
748 p = encode_wcc_data(rqstp, p, &resp->ffh);
749 p = encode_wcc_data(rqstp, p, &resp->tfh);
750 return xdr_ressize_check(rqstp, p);
755 nfs3svc_encode_linkres(struct svc_rqst *rqstp, u32 *p,
756 struct nfsd3_linkres *resp)
758 p = encode_post_op_attr(rqstp, p, &resp->fh);
759 p = encode_wcc_data(rqstp, p, &resp->tfh);
760 return xdr_ressize_check(rqstp, p);
765 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
766 struct nfsd3_readdirres *resp)
768 p = encode_post_op_attr(rqstp, p, &resp->fh);
770 if (resp->status == 0) {
771 /* stupid readdir cookie */
772 memcpy(p, resp->verf, 8); p += 2;
773 xdr_ressize_check(rqstp, p);
774 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
775 return 1; /*No room for trailer */
776 rqstp->rq_res.page_len = (resp->count) << 2;
778 /* add the 'tail' to the end of the 'head' page - page 0. */
779 rqstp->rq_restailpage = 0;
780 rqstp->rq_res.tail[0].iov_base = p;
781 *p++ = 0; /* no more entries */
782 *p++ = htonl(resp->common.err == nfserr_eof);
783 rqstp->rq_res.tail[0].iov_len = 2<<2;
786 return xdr_ressize_check(rqstp, p);
790 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
791 int namlen, ino_t ino)
793 *p++ = xdr_one; /* mark entry present */
794 p = xdr_encode_hyper(p, ino); /* file id */
795 p = xdr_encode_array(p, name, namlen);/* name length & name */
797 cd->offset = p; /* remember pointer */
798 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
804 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
807 p = encode_post_op_attr(cd->rqstp, p, fhp);
808 *p++ = xdr_one; /* yes, a file handle follows */
809 p = encode_fh(p, fhp);
815 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
816 const char *name, int namlen)
818 struct svc_export *exp;
819 struct dentry *dparent, *dchild;
822 dparent = cd->fh.fh_dentry;
823 exp = cd->fh.fh_export;
825 fh_init(fhp, NFS3_FHSIZE);
826 if (isdotent(name, namlen)) {
828 dchild = dget_parent(dparent);
829 if (dchild == dparent) {
830 /* filesystem root - cannot return filehandle for ".." */
835 dchild = dget(dparent);
837 dchild = lookup_one_len(name, dparent, namlen);
840 if (d_mountpoint(dchild) ||
841 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
849 * Encode a directory entry. This one works for both normal readdir
851 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
852 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
854 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
858 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
859 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
861 encode_entry(struct readdir_cd *ccd, const char *name,
862 int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
864 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
867 caddr_t curr_page_addr = NULL;
868 int pn; /* current page number */
869 int slen; /* string (name) length */
870 int elen; /* estimated entry length in words */
871 int num_entry_words = 0; /* actual number of words */
874 u64 offset64 = offset;
876 if (unlikely(cd->offset1)) {
877 /* we ended up with offset on a page boundary */
878 *cd->offset = htonl(offset64 >> 32);
879 *cd->offset1 = htonl(offset64 & 0xffffffff);
882 xdr_encode_hyper(cd->offset, (u64) offset);
887 dprintk("encode_entry(%.*s @%ld%s)\n",
888 namlen, name, (long) offset, plus? " plus" : "");
891 /* truncate filename if too long */
892 if (namlen > NFS3_MAXNAMLEN)
893 namlen = NFS3_MAXNAMLEN;
895 slen = XDR_QUADLEN(namlen);
896 elen = slen + NFS3_ENTRY_BAGGAGE
897 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
899 if (cd->buflen < elen) {
900 cd->common.err = nfserr_toosmall;
904 /* determine which page in rq_respages[] we are currently filling */
905 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
906 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
908 if (((caddr_t)cd->buffer >= curr_page_addr) &&
909 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
913 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
914 /* encode entry in current page */
916 p = encode_entry_baggage(cd, p, name, namlen, ino);
918 /* throw in readdirplus baggage */
922 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
926 p = encode_entryplus_baggage(cd, p, &fh);
928 num_entry_words = p - cd->buffer;
929 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
930 /* temporarily encode entry into next page, then move back to
931 * current and next page in rq_respages[] */
935 /* grab next page for temporary storage of entry */
936 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
938 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
940 /* throw in readdirplus baggage */
944 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
945 /* zero out the filehandle */
949 p1 = encode_entryplus_baggage(cd, p1, &fh);
952 /* determine entry word length and lengths to go in pages */
953 num_entry_words = p1 - tmp;
954 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
955 if ((num_entry_words << 2) < len1) {
956 /* the actual number of words in the entry is less
957 * than elen and can still fit in the current page
959 memmove(p, tmp, num_entry_words << 2);
960 p += num_entry_words;
963 cd->offset = cd->buffer + (cd->offset - tmp);
965 unsigned int offset_r = (cd->offset - tmp) << 2;
967 /* update pointer to offset location.
968 * This is a 64bit quantity, so we need to
970 * - entirely in first page
971 * - entirely in second page
972 * - 4 bytes in each page
974 if (offset_r + 8 <= len1) {
975 cd->offset = p + (cd->offset - tmp);
976 } else if (offset_r >= len1) {
977 cd->offset -= len1 >> 2;
979 /* sitting on the fence */
980 BUG_ON(offset_r != len1 - 4);
981 cd->offset = p + (cd->offset - tmp);
985 len2 = (num_entry_words << 2) - len1;
987 /* move from temp page to current and next pages */
988 memmove(p, tmp, len1);
989 memmove(tmp, (caddr_t)tmp+len1, len2);
991 p = tmp + (len2 >> 2);
995 cd->common.err = nfserr_toosmall;
999 cd->buflen -= num_entry_words;
1001 cd->common.err = nfs_ok;
1007 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
1008 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1010 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1014 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
1015 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1017 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1022 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1023 struct nfsd3_fsstatres *resp)
1025 struct kstatfs *s = &resp->stats;
1026 u64 bs = s->f_bsize;
1028 *p++ = xdr_zero; /* no post_op_attr */
1030 if (resp->status == 0) {
1031 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1032 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1033 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1034 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1035 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1036 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1037 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1039 return xdr_ressize_check(rqstp, p);
1044 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1045 struct nfsd3_fsinfores *resp)
1047 *p++ = xdr_zero; /* no post_op_attr */
1049 if (resp->status == 0) {
1050 *p++ = htonl(resp->f_rtmax);
1051 *p++ = htonl(resp->f_rtpref);
1052 *p++ = htonl(resp->f_rtmult);
1053 *p++ = htonl(resp->f_wtmax);
1054 *p++ = htonl(resp->f_wtpref);
1055 *p++ = htonl(resp->f_wtmult);
1056 *p++ = htonl(resp->f_dtpref);
1057 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1060 *p++ = htonl(resp->f_properties);
1063 return xdr_ressize_check(rqstp, p);
1068 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1069 struct nfsd3_pathconfres *resp)
1071 *p++ = xdr_zero; /* no post_op_attr */
1073 if (resp->status == 0) {
1074 *p++ = htonl(resp->p_link_max);
1075 *p++ = htonl(resp->p_name_max);
1076 *p++ = htonl(resp->p_no_trunc);
1077 *p++ = htonl(resp->p_chown_restricted);
1078 *p++ = htonl(resp->p_case_insensitive);
1079 *p++ = htonl(resp->p_case_preserving);
1082 return xdr_ressize_check(rqstp, p);
1087 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1088 struct nfsd3_commitres *resp)
1090 p = encode_wcc_data(rqstp, p, &resp->fh);
1091 /* Write verifier */
1092 if (resp->status == 0) {
1093 *p++ = htonl(nfssvc_boot.tv_sec);
1094 *p++ = htonl(nfssvc_boot.tv_usec);
1096 return xdr_ressize_check(rqstp, p);
1100 * XDR release functions
1103 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1104 struct nfsd3_attrstat *resp)
1111 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1112 struct nfsd3_fhandle_pair *resp)