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);
812 if (dchild == dparent) {
813 /* filesystem root - cannot return filehandle for ".." */
818 dchild = dget(dparent);
820 dchild = lookup_one_len(name, dparent, namlen);
823 if (d_mountpoint(dchild) ||
824 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
832 * Encode a directory entry. This one works for both normal readdir
834 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
835 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
837 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
841 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
842 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
844 encode_entry(struct readdir_cd *ccd, const char *name,
845 int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
847 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
850 caddr_t curr_page_addr = NULL;
851 int pn; /* current page number */
852 int slen; /* string (name) length */
853 int elen; /* estimated entry length in words */
854 int num_entry_words = 0; /* actual number of words */
857 u64 offset64 = offset;
859 if (unlikely(cd->offset1)) {
860 /* we ended up with offset on a page boundary */
861 *cd->offset = htonl(offset64 >> 32);
862 *cd->offset1 = htonl(offset64 & 0xffffffff);
865 xdr_encode_hyper(cd->offset, (u64) offset);
870 dprintk("encode_entry(%.*s @%ld%s)\n",
871 namlen, name, (long) offset, plus? " plus" : "");
874 /* truncate filename if too long */
875 if (namlen > NFS3_MAXNAMLEN)
876 namlen = NFS3_MAXNAMLEN;
878 slen = XDR_QUADLEN(namlen);
879 elen = slen + NFS3_ENTRY_BAGGAGE
880 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
882 if (cd->buflen < elen) {
883 cd->common.err = nfserr_toosmall;
887 /* determine which page in rq_respages[] we are currently filling */
888 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
889 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
891 if (((caddr_t)cd->buffer >= curr_page_addr) &&
892 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
896 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
897 /* encode entry in current page */
899 p = encode_entry_baggage(cd, p, name, namlen, ino);
901 /* throw in readdirplus baggage */
905 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
909 p = encode_entryplus_baggage(cd, p, &fh);
911 num_entry_words = p - cd->buffer;
912 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
913 /* temporarily encode entry into next page, then move back to
914 * current and next page in rq_respages[] */
918 /* grab next page for temporary storage of entry */
919 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
921 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
923 /* throw in readdirplus baggage */
927 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
928 /* zero out the filehandle */
932 p1 = encode_entryplus_baggage(cd, p1, &fh);
935 /* determine entry word length and lengths to go in pages */
936 num_entry_words = p1 - tmp;
937 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
938 if ((num_entry_words << 2) < len1) {
939 /* the actual number of words in the entry is less
940 * than elen and can still fit in the current page
942 memmove(p, tmp, num_entry_words << 2);
943 p += num_entry_words;
946 cd->offset = cd->buffer + (cd->offset - tmp);
948 unsigned int offset_r = (cd->offset - tmp) << 2;
950 /* update pointer to offset location.
951 * This is a 64bit quantity, so we need to
953 * - entirely in first page
954 * - entirely in second page
955 * - 4 bytes in each page
957 if (offset_r + 8 <= len1) {
958 cd->offset = p + (cd->offset - tmp);
959 } else if (offset_r >= len1) {
960 cd->offset -= len1 >> 2;
962 /* sitting on the fence */
963 BUG_ON(offset_r != len1 - 4);
964 cd->offset = p + (cd->offset - tmp);
968 len2 = (num_entry_words << 2) - len1;
970 /* move from temp page to current and next pages */
971 memmove(p, tmp, len1);
972 memmove(tmp, (caddr_t)tmp+len1, len2);
974 p = tmp + (len2 >> 2);
978 cd->common.err = nfserr_toosmall;
982 cd->buflen -= num_entry_words;
984 cd->common.err = nfs_ok;
990 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
991 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
993 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
997 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
998 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1000 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1005 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1006 struct nfsd3_fsstatres *resp)
1008 struct kstatfs *s = &resp->stats;
1009 u64 bs = s->f_bsize;
1011 *p++ = xdr_zero; /* no post_op_attr */
1013 if (resp->status == 0) {
1014 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1015 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1016 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1017 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1018 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1019 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1020 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1022 return xdr_ressize_check(rqstp, p);
1027 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1028 struct nfsd3_fsinfores *resp)
1030 *p++ = xdr_zero; /* no post_op_attr */
1032 if (resp->status == 0) {
1033 *p++ = htonl(resp->f_rtmax);
1034 *p++ = htonl(resp->f_rtpref);
1035 *p++ = htonl(resp->f_rtmult);
1036 *p++ = htonl(resp->f_wtmax);
1037 *p++ = htonl(resp->f_wtpref);
1038 *p++ = htonl(resp->f_wtmult);
1039 *p++ = htonl(resp->f_dtpref);
1040 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1043 *p++ = htonl(resp->f_properties);
1046 return xdr_ressize_check(rqstp, p);
1051 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1052 struct nfsd3_pathconfres *resp)
1054 *p++ = xdr_zero; /* no post_op_attr */
1056 if (resp->status == 0) {
1057 *p++ = htonl(resp->p_link_max);
1058 *p++ = htonl(resp->p_name_max);
1059 *p++ = htonl(resp->p_no_trunc);
1060 *p++ = htonl(resp->p_chown_restricted);
1061 *p++ = htonl(resp->p_case_insensitive);
1062 *p++ = htonl(resp->p_case_preserving);
1065 return xdr_ressize_check(rqstp, p);
1070 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1071 struct nfsd3_commitres *resp)
1073 p = encode_wcc_data(rqstp, p, &resp->fh);
1074 /* Write verifier */
1075 if (resp->status == 0) {
1076 *p++ = htonl(nfssvc_boot.tv_sec);
1077 *p++ = htonl(nfssvc_boot.tv_usec);
1079 return xdr_ressize_check(rqstp, p);
1083 * XDR release functions
1086 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1087 struct nfsd3_attrstat *resp)
1094 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1095 struct nfsd3_fhandle_pair *resp)