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);
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_sattr3(u32 *p, struct iattr *iap)
115 iap->ia_valid |= ATTR_MODE;
116 iap->ia_mode = ntohl(*p++);
119 iap->ia_valid |= ATTR_UID;
123 iap->ia_valid |= ATTR_GID;
126 iap->ia_uid = INOXID_UID(XID_TAG_NFSD, uid, gid);
127 iap->ia_gid = INOXID_GID(XID_TAG_NFSD, uid, gid);
128 iap->ia_xid = INOXID_XID(XID_TAG_NFSD, uid, gid, 0);
132 iap->ia_valid |= ATTR_SIZE;
133 p = xdr_decode_hyper(p, &newsize);
134 if (newsize <= NFS_OFFSET_MAX)
135 iap->ia_size = newsize;
137 iap->ia_size = NFS_OFFSET_MAX;
139 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
140 iap->ia_valid |= ATTR_ATIME;
141 } else if (tmp == 2) { /* set to client time */
142 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
143 iap->ia_atime.tv_sec = ntohl(*p++);
144 iap->ia_atime.tv_nsec = ntohl(*p++);
146 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
147 iap->ia_valid |= ATTR_MTIME;
148 } else if (tmp == 2) { /* set to client time */
149 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
150 iap->ia_mtime.tv_sec = ntohl(*p++);
151 iap->ia_mtime.tv_nsec = ntohl(*p++);
157 encode_fattr3(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
159 struct vfsmount *mnt = fhp->fh_export->ex_mnt;
160 struct dentry *dentry = fhp->fh_dentry;
162 struct timespec time;
164 vfs_getattr(mnt, dentry, &stat);
166 *p++ = htonl(nfs3_ftypes[(stat.mode & S_IFMT) >> 12]);
167 *p++ = htonl((u32) stat.mode);
168 *p++ = htonl((u32) stat.nlink);
169 *p++ = htonl((u32) nfsd_ruid(rqstp,
170 XIDINO_UID(XID_TAG(dentry->d_inode), stat.uid, stat.xid)));
171 *p++ = htonl((u32) nfsd_rgid(rqstp,
172 XIDINO_GID(XID_TAG(dentry->d_inode), stat.gid, stat.xid)));
173 if (S_ISLNK(stat.mode) && stat.size > NFS3_MAXPATHLEN) {
174 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
176 p = xdr_encode_hyper(p, (u64) stat.size);
178 p = xdr_encode_hyper(p, ((u64)stat.blocks) << 9);
179 *p++ = htonl((u32) MAJOR(stat.rdev));
180 *p++ = htonl((u32) MINOR(stat.rdev));
181 if (is_fsid(fhp, rqstp->rq_reffh))
182 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
184 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat.dev));
185 p = xdr_encode_hyper(p, (u64) stat.ino);
186 p = encode_time3(p, &stat.atime);
187 lease_get_mtime(dentry->d_inode, &time);
188 p = encode_time3(p, &time);
189 p = encode_time3(p, &stat.ctime);
195 encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
197 struct inode *inode = fhp->fh_dentry->d_inode;
199 /* Attributes to follow */
202 *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
203 *p++ = htonl((u32) fhp->fh_post_mode);
204 *p++ = htonl((u32) fhp->fh_post_nlink);
205 *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
206 *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
207 if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
208 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
210 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
212 p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
213 *p++ = fhp->fh_post_rdev[0];
214 *p++ = fhp->fh_post_rdev[1];
215 if (is_fsid(fhp, rqstp->rq_reffh))
216 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
218 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
219 p = xdr_encode_hyper(p, (u64) inode->i_ino);
220 p = encode_time3(p, &fhp->fh_post_atime);
221 p = encode_time3(p, &fhp->fh_post_mtime);
222 p = encode_time3(p, &fhp->fh_post_ctime);
228 * Encode post-operation attributes.
229 * The inode may be NULL if the call failed because of a stale file
230 * handle. In this case, no attributes are returned.
233 encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
235 struct dentry *dentry = fhp->fh_dentry;
236 if (dentry && dentry->d_inode != NULL) {
237 *p++ = xdr_one; /* attributes follow */
238 return encode_fattr3(rqstp, p, fhp);
245 * Enocde weak cache consistency data
248 encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
250 struct dentry *dentry = fhp->fh_dentry;
252 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
253 if (fhp->fh_pre_saved) {
255 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
256 p = encode_time3(p, &fhp->fh_pre_mtime);
257 p = encode_time3(p, &fhp->fh_pre_ctime);
261 return encode_saved_post_attr(rqstp, p, fhp);
263 /* no pre- or post-attrs */
265 return encode_post_op_attr(rqstp, p, fhp);
270 * XDR decode functions
273 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
275 if (!(p = decode_fh(p, &args->fh)))
277 return xdr_argsize_check(rqstp, p);
281 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
282 struct nfsd3_sattrargs *args)
284 if (!(p = decode_fh(p, &args->fh))
285 || !(p = decode_sattr3(p, &args->attrs)))
288 if ((args->check_guard = ntohl(*p++)) != 0) {
289 struct timespec time;
290 p = decode_time3(p, &time);
291 args->guardtime = time.tv_sec;
294 return xdr_argsize_check(rqstp, p);
298 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
299 struct nfsd3_diropargs *args)
301 if (!(p = decode_fh(p, &args->fh))
302 || !(p = decode_filename(p, &args->name, &args->len)))
305 return xdr_argsize_check(rqstp, p);
309 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
310 struct nfsd3_accessargs *args)
312 if (!(p = decode_fh(p, &args->fh)))
314 args->access = ntohl(*p++);
316 return xdr_argsize_check(rqstp, p);
320 nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
321 struct nfsd3_readargs *args)
326 if (!(p = decode_fh(p, &args->fh))
327 || !(p = xdr_decode_hyper(p, &args->offset)))
330 len = args->count = ntohl(*p++);
332 if (len > NFSSVC_MAXBLKSIZE)
333 len = NFSSVC_MAXBLKSIZE;
335 /* set up the kvec */
338 pn = rqstp->rq_resused;
339 svc_take_page(rqstp);
340 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
341 args->vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
342 len -= args->vec[v].iov_len;
346 return xdr_argsize_check(rqstp, p);
350 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
351 struct nfsd3_writeargs *args)
353 unsigned int len, v, hdr;
355 if (!(p = decode_fh(p, &args->fh))
356 || !(p = xdr_decode_hyper(p, &args->offset)))
359 args->count = ntohl(*p++);
360 args->stable = ntohl(*p++);
361 len = args->len = ntohl(*p++);
363 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
364 if (rqstp->rq_arg.len < len + hdr)
367 args->vec[0].iov_base = (void*)p;
368 args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
370 if (len > NFSSVC_MAXBLKSIZE)
371 len = NFSSVC_MAXBLKSIZE;
373 while (len > args->vec[v].iov_len) {
374 len -= args->vec[v].iov_len;
376 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]);
377 args->vec[v].iov_len = PAGE_SIZE;
379 args->vec[v].iov_len = len;
382 return args->count == args->len && args->vec[0].iov_len > 0;
386 nfs3svc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
387 struct nfsd3_createargs *args)
389 if (!(p = decode_fh(p, &args->fh))
390 || !(p = decode_filename(p, &args->name, &args->len)))
393 switch (args->createmode = ntohl(*p++)) {
394 case NFS3_CREATE_UNCHECKED:
395 case NFS3_CREATE_GUARDED:
396 if (!(p = decode_sattr3(p, &args->attrs)))
399 case NFS3_CREATE_EXCLUSIVE:
407 return xdr_argsize_check(rqstp, p);
410 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, u32 *p,
411 struct nfsd3_createargs *args)
413 if (!(p = decode_fh(p, &args->fh))
414 || !(p = decode_filename(p, &args->name, &args->len))
415 || !(p = decode_sattr3(p, &args->attrs)))
418 return xdr_argsize_check(rqstp, p);
422 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
423 struct nfsd3_symlinkargs *args)
430 if (!(p = decode_fh(p, &args->ffh))
431 || !(p = decode_filename(p, &args->fname, &args->flen))
432 || !(p = decode_sattr3(p, &args->attrs))
435 /* now decode the pathname, which might be larger than the first page.
436 * As we have to check for nul's anyway, we copy it into a new page
437 * This page appears in the rq_res.pages list, but as pages_len is always
438 * 0, it won't get in the way
440 svc_take_page(rqstp);
442 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
444 args->tname = new = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
446 /* first copy and check from the first page */
448 vec = &rqstp->rq_arg.head[0];
449 avail = vec->iov_len - (old - (char*)vec->iov_base);
450 while (len && avail && *old) {
455 /* now copy next page if there is one */
456 if (len && !avail && rqstp->rq_arg.page_len) {
457 avail = rqstp->rq_arg.page_len;
458 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
459 old = page_address(rqstp->rq_arg.pages[0]);
461 while (len && avail && *old) {
474 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, u32 *p,
475 struct nfsd3_mknodargs *args)
477 if (!(p = decode_fh(p, &args->fh))
478 || !(p = decode_filename(p, &args->name, &args->len)))
481 args->ftype = ntohl(*p++);
483 if (args->ftype == NF3BLK || args->ftype == NF3CHR
484 || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
485 if (!(p = decode_sattr3(p, &args->attrs)))
489 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
490 args->major = ntohl(*p++);
491 args->minor = ntohl(*p++);
494 return xdr_argsize_check(rqstp, p);
498 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
499 struct nfsd3_renameargs *args)
501 if (!(p = decode_fh(p, &args->ffh))
502 || !(p = decode_filename(p, &args->fname, &args->flen))
503 || !(p = decode_fh(p, &args->tfh))
504 || !(p = decode_filename(p, &args->tname, &args->tlen)))
507 return xdr_argsize_check(rqstp, p);
511 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p,
512 struct nfsd3_readlinkargs *args)
514 if (!(p = decode_fh(p, &args->fh)))
516 svc_take_page(rqstp);
517 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
519 return xdr_argsize_check(rqstp, p);
523 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
524 struct nfsd3_linkargs *args)
526 if (!(p = decode_fh(p, &args->ffh))
527 || !(p = decode_fh(p, &args->tfh))
528 || !(p = decode_filename(p, &args->tname, &args->tlen)))
531 return xdr_argsize_check(rqstp, p);
535 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
536 struct nfsd3_readdirargs *args)
538 if (!(p = decode_fh(p, &args->fh)))
540 p = xdr_decode_hyper(p, &args->cookie);
541 args->verf = p; p += 2;
543 args->count = ntohl(*p++);
545 if (args->count > PAGE_SIZE)
546 args->count = PAGE_SIZE;
548 svc_take_page(rqstp);
549 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
551 return xdr_argsize_check(rqstp, p);
555 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, u32 *p,
556 struct nfsd3_readdirargs *args)
560 if (!(p = decode_fh(p, &args->fh)))
562 p = xdr_decode_hyper(p, &args->cookie);
563 args->verf = p; p += 2;
564 args->dircount = ntohl(*p++);
565 args->count = ntohl(*p++);
567 len = (args->count > NFSSVC_MAXBLKSIZE) ? NFSSVC_MAXBLKSIZE :
572 pn = rqstp->rq_resused;
573 svc_take_page(rqstp);
575 args->buffer = page_address(rqstp->rq_respages[pn]);
579 return xdr_argsize_check(rqstp, p);
583 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, u32 *p,
584 struct nfsd3_commitargs *args)
586 if (!(p = decode_fh(p, &args->fh)))
588 p = xdr_decode_hyper(p, &args->offset);
589 args->count = ntohl(*p++);
591 return xdr_argsize_check(rqstp, p);
595 * XDR encode functions
598 * There must be an encoding function for void results so svc_process
599 * will work properly.
602 nfs3svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
604 return xdr_ressize_check(rqstp, p);
609 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
610 struct nfsd3_attrstat *resp)
612 if (resp->status == 0)
613 p = encode_fattr3(rqstp, p, &resp->fh);
614 return xdr_ressize_check(rqstp, p);
617 /* SETATTR, REMOVE, RMDIR */
619 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, u32 *p,
620 struct nfsd3_attrstat *resp)
622 p = encode_wcc_data(rqstp, p, &resp->fh);
623 return xdr_ressize_check(rqstp, p);
628 nfs3svc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
629 struct nfsd3_diropres *resp)
631 if (resp->status == 0) {
632 p = encode_fh(p, &resp->fh);
633 p = encode_post_op_attr(rqstp, p, &resp->fh);
635 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
636 return xdr_ressize_check(rqstp, p);
641 nfs3svc_encode_accessres(struct svc_rqst *rqstp, u32 *p,
642 struct nfsd3_accessres *resp)
644 p = encode_post_op_attr(rqstp, p, &resp->fh);
645 if (resp->status == 0)
646 *p++ = htonl(resp->access);
647 return xdr_ressize_check(rqstp, p);
652 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
653 struct nfsd3_readlinkres *resp)
655 p = encode_post_op_attr(rqstp, p, &resp->fh);
656 if (resp->status == 0) {
657 *p++ = htonl(resp->len);
658 xdr_ressize_check(rqstp, p);
659 rqstp->rq_res.page_len = resp->len;
661 /* need to pad the tail */
662 rqstp->rq_restailpage = 0;
663 rqstp->rq_res.tail[0].iov_base = p;
665 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
669 return xdr_ressize_check(rqstp, p);
674 nfs3svc_encode_readres(struct svc_rqst *rqstp, u32 *p,
675 struct nfsd3_readres *resp)
677 p = encode_post_op_attr(rqstp, p, &resp->fh);
678 if (resp->status == 0) {
679 *p++ = htonl(resp->count);
680 *p++ = htonl(resp->eof);
681 *p++ = htonl(resp->count); /* xdr opaque count */
682 xdr_ressize_check(rqstp, p);
683 /* now update rqstp->rq_res to reflect data aswell */
684 rqstp->rq_res.page_len = resp->count;
685 if (resp->count & 3) {
686 /* need to pad the tail */
687 rqstp->rq_restailpage = 0;
688 rqstp->rq_res.tail[0].iov_base = p;
690 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
694 return xdr_ressize_check(rqstp, p);
699 nfs3svc_encode_writeres(struct svc_rqst *rqstp, u32 *p,
700 struct nfsd3_writeres *resp)
702 p = encode_wcc_data(rqstp, p, &resp->fh);
703 if (resp->status == 0) {
704 *p++ = htonl(resp->count);
705 *p++ = htonl(resp->committed);
706 *p++ = htonl(nfssvc_boot.tv_sec);
707 *p++ = htonl(nfssvc_boot.tv_usec);
709 return xdr_ressize_check(rqstp, p);
712 /* CREATE, MKDIR, SYMLINK, MKNOD */
714 nfs3svc_encode_createres(struct svc_rqst *rqstp, u32 *p,
715 struct nfsd3_diropres *resp)
717 if (resp->status == 0) {
719 p = encode_fh(p, &resp->fh);
720 p = encode_post_op_attr(rqstp, p, &resp->fh);
722 p = encode_wcc_data(rqstp, p, &resp->dirfh);
723 return xdr_ressize_check(rqstp, p);
728 nfs3svc_encode_renameres(struct svc_rqst *rqstp, u32 *p,
729 struct nfsd3_renameres *resp)
731 p = encode_wcc_data(rqstp, p, &resp->ffh);
732 p = encode_wcc_data(rqstp, p, &resp->tfh);
733 return xdr_ressize_check(rqstp, p);
738 nfs3svc_encode_linkres(struct svc_rqst *rqstp, u32 *p,
739 struct nfsd3_linkres *resp)
741 p = encode_post_op_attr(rqstp, p, &resp->fh);
742 p = encode_wcc_data(rqstp, p, &resp->tfh);
743 return xdr_ressize_check(rqstp, p);
748 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
749 struct nfsd3_readdirres *resp)
751 p = encode_post_op_attr(rqstp, p, &resp->fh);
753 if (resp->status == 0) {
754 /* stupid readdir cookie */
755 memcpy(p, resp->verf, 8); p += 2;
756 xdr_ressize_check(rqstp, p);
757 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
758 return 1; /*No room for trailer */
759 rqstp->rq_res.page_len = (resp->count) << 2;
761 /* add the 'tail' to the end of the 'head' page - page 0. */
762 rqstp->rq_restailpage = 0;
763 rqstp->rq_res.tail[0].iov_base = p;
764 *p++ = 0; /* no more entries */
765 *p++ = htonl(resp->common.err == nfserr_eof);
766 rqstp->rq_res.tail[0].iov_len = 2<<2;
769 return xdr_ressize_check(rqstp, p);
773 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
774 int namlen, ino_t ino)
776 *p++ = xdr_one; /* mark entry present */
777 p = xdr_encode_hyper(p, ino); /* file id */
778 p = xdr_encode_array(p, name, namlen);/* name length & name */
780 cd->offset = p; /* remember pointer */
781 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
787 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
790 p = encode_post_op_attr(cd->rqstp, p, fhp);
791 *p++ = xdr_one; /* yes, a file handle follows */
792 p = encode_fh(p, fhp);
798 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
799 const char *name, int namlen)
801 struct svc_export *exp;
802 struct dentry *dparent, *dchild;
805 dparent = cd->fh.fh_dentry;
806 exp = cd->fh.fh_export;
808 fh_init(fhp, NFS3_FHSIZE);
809 if (isdotent(name, namlen)) {
811 dchild = dget_parent(dparent);
813 dchild = dget(dparent);
815 dchild = lookup_one_len(name, dparent, namlen);
818 if (d_mountpoint(dchild) ||
819 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
827 * Encode a directory entry. This one works for both normal readdir
829 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
830 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
832 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
836 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
837 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
839 encode_entry(struct readdir_cd *ccd, const char *name,
840 int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
842 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
845 caddr_t curr_page_addr = NULL;
846 int pn; /* current page number */
847 int slen; /* string (name) length */
848 int elen; /* estimated entry length in words */
849 int num_entry_words = 0; /* actual number of words */
852 u64 offset64 = offset;
854 if (unlikely(cd->offset1)) {
855 /* we ended up with offset on a page boundary */
856 *cd->offset = htonl(offset64 >> 32);
857 *cd->offset1 = htonl(offset64 & 0xffffffff);
860 xdr_encode_hyper(cd->offset, (u64) offset);
865 dprintk("encode_entry(%.*s @%ld%s)\n",
866 namlen, name, (long) offset, plus? " plus" : "");
869 /* truncate filename if too long */
870 if (namlen > NFS3_MAXNAMLEN)
871 namlen = NFS3_MAXNAMLEN;
873 slen = XDR_QUADLEN(namlen);
874 elen = slen + NFS3_ENTRY_BAGGAGE
875 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
877 if (cd->buflen < elen) {
878 cd->common.err = nfserr_toosmall;
882 /* determine which page in rq_respages[] we are currently filling */
883 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
884 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
886 if (((caddr_t)cd->buffer >= curr_page_addr) &&
887 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
891 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
892 /* encode entry in current page */
894 p = encode_entry_baggage(cd, p, name, namlen, ino);
896 /* throw in readdirplus baggage */
900 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
904 p = encode_entryplus_baggage(cd, p, &fh);
906 num_entry_words = p - cd->buffer;
907 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
908 /* temporarily encode entry into next page, then move back to
909 * current and next page in rq_respages[] */
913 /* grab next page for temporary storage of entry */
914 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
916 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
918 /* throw in readdirplus baggage */
922 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
923 /* zero out the filehandle */
927 p1 = encode_entryplus_baggage(cd, p1, &fh);
930 /* determine entry word length and lengths to go in pages */
931 num_entry_words = p1 - tmp;
932 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
933 if ((num_entry_words << 2) < len1) {
934 /* the actual number of words in the entry is less
935 * than elen and can still fit in the current page
937 memmove(p, tmp, num_entry_words << 2);
938 p += num_entry_words;
941 cd->offset = cd->buffer + (cd->offset - tmp);
943 unsigned int offset_r = (cd->offset - tmp) << 2;
945 /* update pointer to offset location.
946 * This is a 64bit quantity, so we need to
948 * - entirely in first page
949 * - entirely in second page
950 * - 4 bytes in each page
952 if (offset_r + 8 <= len1) {
953 cd->offset = p + (cd->offset - tmp);
954 } else if (offset_r >= len1) {
955 cd->offset -= len1 >> 2;
957 /* sitting on the fence */
958 BUG_ON(offset_r != len1 - 4);
959 cd->offset = p + (cd->offset - tmp);
963 len2 = (num_entry_words << 2) - len1;
965 /* move from temp page to current and next pages */
966 memmove(p, tmp, len1);
967 memmove(tmp, (caddr_t)tmp+len1, len2);
969 p = tmp + (len2 >> 2);
973 cd->common.err = nfserr_toosmall;
977 cd->buflen -= num_entry_words;
979 cd->common.err = nfs_ok;
985 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
986 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
988 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
992 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
993 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
995 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1000 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1001 struct nfsd3_fsstatres *resp)
1003 struct kstatfs *s = &resp->stats;
1004 u64 bs = s->f_bsize;
1006 *p++ = xdr_zero; /* no post_op_attr */
1008 if (resp->status == 0) {
1009 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1010 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1011 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1012 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1013 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1014 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1015 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1017 return xdr_ressize_check(rqstp, p);
1022 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1023 struct nfsd3_fsinfores *resp)
1025 *p++ = xdr_zero; /* no post_op_attr */
1027 if (resp->status == 0) {
1028 *p++ = htonl(resp->f_rtmax);
1029 *p++ = htonl(resp->f_rtpref);
1030 *p++ = htonl(resp->f_rtmult);
1031 *p++ = htonl(resp->f_wtmax);
1032 *p++ = htonl(resp->f_wtpref);
1033 *p++ = htonl(resp->f_wtmult);
1034 *p++ = htonl(resp->f_dtpref);
1035 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1038 *p++ = htonl(resp->f_properties);
1041 return xdr_ressize_check(rqstp, p);
1046 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1047 struct nfsd3_pathconfres *resp)
1049 *p++ = xdr_zero; /* no post_op_attr */
1051 if (resp->status == 0) {
1052 *p++ = htonl(resp->p_link_max);
1053 *p++ = htonl(resp->p_name_max);
1054 *p++ = htonl(resp->p_no_trunc);
1055 *p++ = htonl(resp->p_chown_restricted);
1056 *p++ = htonl(resp->p_case_insensitive);
1057 *p++ = htonl(resp->p_case_preserving);
1060 return xdr_ressize_check(rqstp, p);
1065 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1066 struct nfsd3_commitres *resp)
1068 p = encode_wcc_data(rqstp, p, &resp->fh);
1069 /* Write verifier */
1070 if (resp->status == 0) {
1071 *p++ = htonl(nfssvc_boot.tv_sec);
1072 *p++ = htonl(nfssvc_boot.tv_usec);
1074 return xdr_ressize_check(rqstp, p);
1078 * XDR release functions
1081 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1082 struct nfsd3_attrstat *resp)
1089 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1090 struct nfsd3_fhandle_pair *resp)