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_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,
181 XIDINO_UID(stat.uid, stat.xid)));
182 *p++ = htonl((u32) nfsd_rgid(rqstp,
183 XIDINO_GID(stat.gid, stat.xid)));
184 if (S_ISLNK(stat.mode) && stat.size > NFS3_MAXPATHLEN) {
185 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
187 p = xdr_encode_hyper(p, (u64) stat.size);
189 p = xdr_encode_hyper(p, ((u64)stat.blocks) << 9);
190 *p++ = htonl((u32) MAJOR(stat.rdev));
191 *p++ = htonl((u32) MINOR(stat.rdev));
192 if (is_fsid(fhp, rqstp->rq_reffh))
193 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
195 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat.dev));
196 p = xdr_encode_hyper(p, (u64) stat.ino);
197 p = encode_time3(p, &stat.atime);
198 lease_get_mtime(dentry->d_inode, &time);
199 p = encode_time3(p, &time);
200 p = encode_time3(p, &stat.ctime);
206 encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
208 struct inode *inode = fhp->fh_dentry->d_inode;
210 /* Attributes to follow */
213 *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
214 *p++ = htonl((u32) fhp->fh_post_mode);
215 *p++ = htonl((u32) fhp->fh_post_nlink);
216 *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
217 *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
218 if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
219 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
221 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
223 p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
224 *p++ = fhp->fh_post_rdev[0];
225 *p++ = fhp->fh_post_rdev[1];
226 if (is_fsid(fhp, rqstp->rq_reffh))
227 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
229 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
230 p = xdr_encode_hyper(p, (u64) inode->i_ino);
231 p = encode_time3(p, &fhp->fh_post_atime);
232 p = encode_time3(p, &fhp->fh_post_mtime);
233 p = encode_time3(p, &fhp->fh_post_ctime);
239 * Encode post-operation attributes.
240 * The inode may be NULL if the call failed because of a stale file
241 * handle. In this case, no attributes are returned.
244 encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
246 struct dentry *dentry = fhp->fh_dentry;
247 if (dentry && dentry->d_inode != NULL) {
248 *p++ = xdr_one; /* attributes follow */
249 return encode_fattr3(rqstp, p, fhp);
256 * Enocde weak cache consistency data
259 encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
261 struct dentry *dentry = fhp->fh_dentry;
263 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
264 if (fhp->fh_pre_saved) {
266 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
267 p = encode_time3(p, &fhp->fh_pre_mtime);
268 p = encode_time3(p, &fhp->fh_pre_ctime);
272 return encode_saved_post_attr(rqstp, p, fhp);
274 /* no pre- or post-attrs */
276 return encode_post_op_attr(rqstp, p, fhp);
281 * XDR decode functions
284 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
286 if (!(p = decode_fh(p, &args->fh)))
288 return xdr_argsize_check(rqstp, p);
292 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
293 struct nfsd3_sattrargs *args)
295 if (!(p = decode_fh(p, &args->fh))
296 || !(p = decode_sattr3(p, &args->attrs)))
299 if ((args->check_guard = ntohl(*p++)) != 0) {
300 struct timespec time;
301 p = decode_time3(p, &time);
302 args->guardtime = time.tv_sec;
305 return xdr_argsize_check(rqstp, p);
309 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
310 struct nfsd3_diropargs *args)
312 if (!(p = decode_fh(p, &args->fh))
313 || !(p = decode_filename(p, &args->name, &args->len)))
316 return xdr_argsize_check(rqstp, p);
320 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
321 struct nfsd3_accessargs *args)
323 if (!(p = decode_fh(p, &args->fh)))
325 args->access = ntohl(*p++);
327 return xdr_argsize_check(rqstp, p);
331 nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
332 struct nfsd3_readargs *args)
337 if (!(p = decode_fh(p, &args->fh))
338 || !(p = xdr_decode_hyper(p, &args->offset)))
341 len = args->count = ntohl(*p++);
343 if (len > NFSSVC_MAXBLKSIZE)
344 len = NFSSVC_MAXBLKSIZE;
346 /* set up the kvec */
349 pn = rqstp->rq_resused;
350 svc_take_page(rqstp);
351 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
352 args->vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
353 len -= args->vec[v].iov_len;
357 return xdr_argsize_check(rqstp, p);
361 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
362 struct nfsd3_writeargs *args)
364 unsigned int len, v, hdr;
366 if (!(p = decode_fh(p, &args->fh))
367 || !(p = xdr_decode_hyper(p, &args->offset)))
370 args->count = ntohl(*p++);
371 args->stable = ntohl(*p++);
372 len = args->len = ntohl(*p++);
374 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
375 if (rqstp->rq_arg.len < len + hdr)
378 args->vec[0].iov_base = (void*)p;
379 args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
381 if (len > NFSSVC_MAXBLKSIZE)
382 len = NFSSVC_MAXBLKSIZE;
384 while (len > args->vec[v].iov_len) {
385 len -= args->vec[v].iov_len;
387 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]);
388 args->vec[v].iov_len = PAGE_SIZE;
390 args->vec[v].iov_len = len;
393 return args->count == args->len && args->vec[0].iov_len > 0;
397 nfs3svc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
398 struct nfsd3_createargs *args)
400 if (!(p = decode_fh(p, &args->fh))
401 || !(p = decode_filename(p, &args->name, &args->len)))
404 switch (args->createmode = ntohl(*p++)) {
405 case NFS3_CREATE_UNCHECKED:
406 case NFS3_CREATE_GUARDED:
407 if (!(p = decode_sattr3(p, &args->attrs)))
410 case NFS3_CREATE_EXCLUSIVE:
418 return xdr_argsize_check(rqstp, p);
421 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, u32 *p,
422 struct nfsd3_createargs *args)
424 if (!(p = decode_fh(p, &args->fh))
425 || !(p = decode_filename(p, &args->name, &args->len))
426 || !(p = decode_sattr3(p, &args->attrs)))
429 return xdr_argsize_check(rqstp, p);
433 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
434 struct nfsd3_symlinkargs *args)
441 if (!(p = decode_fh(p, &args->ffh))
442 || !(p = decode_filename(p, &args->fname, &args->flen))
443 || !(p = decode_sattr3(p, &args->attrs))
446 /* now decode the pathname, which might be larger than the first page.
447 * As we have to check for nul's anyway, we copy it into a new page
448 * This page appears in the rq_res.pages list, but as pages_len is always
449 * 0, it won't get in the way
451 svc_take_page(rqstp);
453 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
455 args->tname = new = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
457 /* first copy and check from the first page */
459 vec = &rqstp->rq_arg.head[0];
460 avail = vec->iov_len - (old - (char*)vec->iov_base);
461 while (len && avail && *old) {
466 /* now copy next page if there is one */
467 if (len && !avail && rqstp->rq_arg.page_len) {
468 avail = rqstp->rq_arg.page_len;
469 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
470 old = page_address(rqstp->rq_arg.pages[0]);
472 while (len && avail && *old) {
485 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, u32 *p,
486 struct nfsd3_mknodargs *args)
488 if (!(p = decode_fh(p, &args->fh))
489 || !(p = decode_filename(p, &args->name, &args->len)))
492 args->ftype = ntohl(*p++);
494 if (args->ftype == NF3BLK || args->ftype == NF3CHR
495 || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
496 if (!(p = decode_sattr3(p, &args->attrs)))
500 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
501 args->major = ntohl(*p++);
502 args->minor = ntohl(*p++);
505 return xdr_argsize_check(rqstp, p);
509 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
510 struct nfsd3_renameargs *args)
512 if (!(p = decode_fh(p, &args->ffh))
513 || !(p = decode_filename(p, &args->fname, &args->flen))
514 || !(p = decode_fh(p, &args->tfh))
515 || !(p = decode_filename(p, &args->tname, &args->tlen)))
518 return xdr_argsize_check(rqstp, p);
522 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p,
523 struct nfsd3_readlinkargs *args)
525 if (!(p = decode_fh(p, &args->fh)))
527 svc_take_page(rqstp);
528 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
530 return xdr_argsize_check(rqstp, p);
534 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
535 struct nfsd3_linkargs *args)
537 if (!(p = decode_fh(p, &args->ffh))
538 || !(p = decode_fh(p, &args->tfh))
539 || !(p = decode_filename(p, &args->tname, &args->tlen)))
542 return xdr_argsize_check(rqstp, p);
546 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
547 struct nfsd3_readdirargs *args)
549 if (!(p = decode_fh(p, &args->fh)))
551 p = xdr_decode_hyper(p, &args->cookie);
552 args->verf = p; p += 2;
554 args->count = ntohl(*p++);
556 if (args->count > PAGE_SIZE)
557 args->count = PAGE_SIZE;
559 svc_take_page(rqstp);
560 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
562 return xdr_argsize_check(rqstp, p);
566 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, u32 *p,
567 struct nfsd3_readdirargs *args)
571 if (!(p = decode_fh(p, &args->fh)))
573 p = xdr_decode_hyper(p, &args->cookie);
574 args->verf = p; p += 2;
575 args->dircount = ntohl(*p++);
576 args->count = ntohl(*p++);
578 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);
606 * XDR encode functions
609 * There must be an encoding function for void results so svc_process
610 * will work properly.
613 nfs3svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
615 return xdr_ressize_check(rqstp, p);
620 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
621 struct nfsd3_attrstat *resp)
623 if (resp->status == 0)
624 p = encode_fattr3(rqstp, p, &resp->fh);
625 return xdr_ressize_check(rqstp, p);
628 /* SETATTR, REMOVE, RMDIR */
630 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, u32 *p,
631 struct nfsd3_attrstat *resp)
633 p = encode_wcc_data(rqstp, p, &resp->fh);
634 return xdr_ressize_check(rqstp, p);
639 nfs3svc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
640 struct nfsd3_diropres *resp)
642 if (resp->status == 0) {
643 p = encode_fh(p, &resp->fh);
644 p = encode_post_op_attr(rqstp, p, &resp->fh);
646 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
647 return xdr_ressize_check(rqstp, p);
652 nfs3svc_encode_accessres(struct svc_rqst *rqstp, u32 *p,
653 struct nfsd3_accessres *resp)
655 p = encode_post_op_attr(rqstp, p, &resp->fh);
656 if (resp->status == 0)
657 *p++ = htonl(resp->access);
658 return xdr_ressize_check(rqstp, p);
663 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
664 struct nfsd3_readlinkres *resp)
666 p = encode_post_op_attr(rqstp, p, &resp->fh);
667 if (resp->status == 0) {
668 *p++ = htonl(resp->len);
669 xdr_ressize_check(rqstp, p);
670 rqstp->rq_res.page_len = resp->len;
672 /* need to pad the tail */
673 rqstp->rq_restailpage = 0;
674 rqstp->rq_res.tail[0].iov_base = p;
676 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
680 return xdr_ressize_check(rqstp, p);
685 nfs3svc_encode_readres(struct svc_rqst *rqstp, u32 *p,
686 struct nfsd3_readres *resp)
688 p = encode_post_op_attr(rqstp, p, &resp->fh);
689 if (resp->status == 0) {
690 *p++ = htonl(resp->count);
691 *p++ = htonl(resp->eof);
692 *p++ = htonl(resp->count); /* xdr opaque count */
693 xdr_ressize_check(rqstp, p);
694 /* now update rqstp->rq_res to reflect data aswell */
695 rqstp->rq_res.page_len = resp->count;
696 if (resp->count & 3) {
697 /* need to pad the tail */
698 rqstp->rq_restailpage = 0;
699 rqstp->rq_res.tail[0].iov_base = p;
701 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
705 return xdr_ressize_check(rqstp, p);
710 nfs3svc_encode_writeres(struct svc_rqst *rqstp, u32 *p,
711 struct nfsd3_writeres *resp)
713 p = encode_wcc_data(rqstp, p, &resp->fh);
714 if (resp->status == 0) {
715 *p++ = htonl(resp->count);
716 *p++ = htonl(resp->committed);
717 *p++ = htonl(nfssvc_boot.tv_sec);
718 *p++ = htonl(nfssvc_boot.tv_usec);
720 return xdr_ressize_check(rqstp, p);
723 /* CREATE, MKDIR, SYMLINK, MKNOD */
725 nfs3svc_encode_createres(struct svc_rqst *rqstp, u32 *p,
726 struct nfsd3_diropres *resp)
728 if (resp->status == 0) {
730 p = encode_fh(p, &resp->fh);
731 p = encode_post_op_attr(rqstp, p, &resp->fh);
733 p = encode_wcc_data(rqstp, p, &resp->dirfh);
734 return xdr_ressize_check(rqstp, p);
739 nfs3svc_encode_renameres(struct svc_rqst *rqstp, u32 *p,
740 struct nfsd3_renameres *resp)
742 p = encode_wcc_data(rqstp, p, &resp->ffh);
743 p = encode_wcc_data(rqstp, p, &resp->tfh);
744 return xdr_ressize_check(rqstp, p);
749 nfs3svc_encode_linkres(struct svc_rqst *rqstp, u32 *p,
750 struct nfsd3_linkres *resp)
752 p = encode_post_op_attr(rqstp, p, &resp->fh);
753 p = encode_wcc_data(rqstp, p, &resp->tfh);
754 return xdr_ressize_check(rqstp, p);
759 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
760 struct nfsd3_readdirres *resp)
762 p = encode_post_op_attr(rqstp, p, &resp->fh);
764 if (resp->status == 0) {
765 /* stupid readdir cookie */
766 memcpy(p, resp->verf, 8); p += 2;
767 xdr_ressize_check(rqstp, p);
769 *p++ = 0; /* no more entries */
770 *p++ = htonl(resp->common.err == nfserr_eof);
771 rqstp->rq_res.page_len = (resp->count + 2) << 2;
774 return xdr_ressize_check(rqstp, p);
778 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
779 int namlen, ino_t ino)
781 *p++ = xdr_one; /* mark entry present */
782 p = xdr_encode_hyper(p, ino); /* file id */
783 p = xdr_encode_array(p, name, namlen);/* name length & name */
785 cd->offset = p; /* remember pointer */
786 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
792 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
795 p = encode_post_op_attr(cd->rqstp, p, fhp);
796 *p++ = xdr_one; /* yes, a file handle follows */
797 p = encode_fh(p, fhp);
803 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
804 const char *name, int namlen)
806 struct svc_export *exp;
807 struct dentry *dparent, *dchild;
810 dparent = cd->fh.fh_dentry;
811 exp = cd->fh.fh_export;
813 fh_init(fhp, NFS3_FHSIZE);
814 if (isdotent(name, namlen)) {
816 dchild = dget_parent(dparent);
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)