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/vs_tag.h>
26 #define NFSDDBG_FACILITY NFSDDBG_XDR
30 * Mapping of S_IF* types to NFS file types
32 static u32 nfs3_ftypes[] = {
33 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
34 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
35 NF3REG, NF3BAD, NF3LNK, NF3BAD,
36 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
40 * XDR functions for basic NFS types
43 encode_time3(__be32 *p, struct timespec *time)
45 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
50 decode_time3(__be32 *p, struct timespec *time)
52 time->tv_sec = ntohl(*p++);
53 time->tv_nsec = ntohl(*p++);
58 decode_fh(__be32 *p, struct svc_fh *fhp)
61 fh_init(fhp, NFS3_FHSIZE);
63 if (size > NFS3_FHSIZE)
66 memcpy(&fhp->fh_handle.fh_base, p, size);
67 fhp->fh_handle.fh_size = size;
68 return p + XDR_QUADLEN(size);
71 /* Helper function for NFSv3 ACL code */
72 __be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
74 return decode_fh(p, fhp);
78 encode_fh(__be32 *p, struct svc_fh *fhp)
80 unsigned int size = fhp->fh_handle.fh_size;
82 if (size) p[XDR_QUADLEN(size)-1]=0;
83 memcpy(p, &fhp->fh_handle.fh_base, size);
84 return p + XDR_QUADLEN(size);
88 * Decode a file name and make sure that the path contains
89 * no slashes or null bytes.
92 decode_filename(__be32 *p, char **namp, int *lenp)
97 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
98 for (i = 0, name = *namp; i < *lenp; i++, name++) {
99 if (*name == '\0' || *name == '/')
108 decode_sattr3(__be32 *p, struct iattr *iap)
117 iap->ia_valid |= ATTR_MODE;
118 iap->ia_mode = ntohl(*p++);
121 iap->ia_valid |= ATTR_UID;
125 iap->ia_valid |= ATTR_GID;
128 iap->ia_uid = INOTAG_UID(DX_TAG_NFSD, uid, gid);
129 iap->ia_gid = INOTAG_GID(DX_TAG_NFSD, uid, gid);
130 iap->ia_tag = INOTAG_TAG(DX_TAG_NFSD, uid, gid, 0);
134 iap->ia_valid |= ATTR_SIZE;
135 p = xdr_decode_hyper(p, &newsize);
136 if (newsize <= NFS_OFFSET_MAX)
137 iap->ia_size = newsize;
139 iap->ia_size = NFS_OFFSET_MAX;
141 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
142 iap->ia_valid |= ATTR_ATIME;
143 } else if (tmp == 2) { /* set to client time */
144 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
145 iap->ia_atime.tv_sec = ntohl(*p++);
146 iap->ia_atime.tv_nsec = ntohl(*p++);
148 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
149 iap->ia_valid |= ATTR_MTIME;
150 } else if (tmp == 2) { /* set to client time */
151 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
152 iap->ia_mtime.tv_sec = ntohl(*p++);
153 iap->ia_mtime.tv_nsec = ntohl(*p++);
159 encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
162 struct dentry *dentry = fhp->fh_dentry;
163 struct timespec time;
165 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
166 *p++ = htonl((u32) stat->mode);
167 *p++ = htonl((u32) stat->nlink);
168 *p++ = htonl((u32) nfsd_ruid(rqstp,
169 TAGINO_UID(DX_TAG(dentry->d_inode), stat->uid, stat->tag)));
170 *p++ = htonl((u32) nfsd_rgid(rqstp,
171 TAGINO_GID(DX_TAG(dentry->d_inode), stat->gid, stat->tag)));
172 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
173 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
175 p = xdr_encode_hyper(p, (u64) stat->size);
177 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
178 *p++ = htonl((u32) MAJOR(stat->rdev));
179 *p++ = htonl((u32) MINOR(stat->rdev));
180 if (is_fsid(fhp, rqstp->rq_reffh))
181 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
183 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat->dev));
184 p = xdr_encode_hyper(p, (u64) stat->ino);
185 p = encode_time3(p, &stat->atime);
186 lease_get_mtime(dentry->d_inode, &time);
187 p = encode_time3(p, &time);
188 p = encode_time3(p, &stat->ctime);
194 encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
196 struct inode *inode = fhp->fh_dentry->d_inode;
198 /* Attributes to follow */
201 *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
202 *p++ = htonl((u32) fhp->fh_post_mode);
203 *p++ = htonl((u32) fhp->fh_post_nlink);
204 *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
205 *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
206 if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
207 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
209 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
211 p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
212 *p++ = fhp->fh_post_rdev[0];
213 *p++ = fhp->fh_post_rdev[1];
214 if (is_fsid(fhp, rqstp->rq_reffh))
215 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
217 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
218 p = xdr_encode_hyper(p, (u64) inode->i_ino);
219 p = encode_time3(p, &fhp->fh_post_atime);
220 p = encode_time3(p, &fhp->fh_post_mtime);
221 p = encode_time3(p, &fhp->fh_post_ctime);
227 * Encode post-operation attributes.
228 * The inode may be NULL if the call failed because of a stale file
229 * handle. In this case, no attributes are returned.
232 encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
234 struct dentry *dentry = fhp->fh_dentry;
235 if (dentry && dentry->d_inode != NULL) {
239 err = vfs_getattr(fhp->fh_export->ex_mnt, dentry, &stat);
241 *p++ = xdr_one; /* attributes follow */
242 return encode_fattr3(rqstp, p, fhp, &stat);
249 /* Helper for NFSv3 ACLs */
251 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
253 return encode_post_op_attr(rqstp, p, fhp);
257 * Enocde weak cache consistency data
260 encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
262 struct dentry *dentry = fhp->fh_dentry;
264 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
265 if (fhp->fh_pre_saved) {
267 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
268 p = encode_time3(p, &fhp->fh_pre_mtime);
269 p = encode_time3(p, &fhp->fh_pre_ctime);
273 return encode_saved_post_attr(rqstp, p, fhp);
275 /* no pre- or post-attrs */
277 return encode_post_op_attr(rqstp, p, fhp);
282 * XDR decode functions
285 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
287 if (!(p = decode_fh(p, &args->fh)))
289 return xdr_argsize_check(rqstp, p);
293 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
294 struct nfsd3_sattrargs *args)
296 if (!(p = decode_fh(p, &args->fh))
297 || !(p = decode_sattr3(p, &args->attrs)))
300 if ((args->check_guard = ntohl(*p++)) != 0) {
301 struct timespec time;
302 p = decode_time3(p, &time);
303 args->guardtime = time.tv_sec;
306 return xdr_argsize_check(rqstp, p);
310 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
311 struct nfsd3_diropargs *args)
313 if (!(p = decode_fh(p, &args->fh))
314 || !(p = decode_filename(p, &args->name, &args->len)))
317 return xdr_argsize_check(rqstp, p);
321 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
322 struct nfsd3_accessargs *args)
324 if (!(p = decode_fh(p, &args->fh)))
326 args->access = ntohl(*p++);
328 return xdr_argsize_check(rqstp, p);
332 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
333 struct nfsd3_readargs *args)
337 u32 max_blocksize = svc_max_payload(rqstp);
339 if (!(p = decode_fh(p, &args->fh))
340 || !(p = xdr_decode_hyper(p, &args->offset)))
343 len = args->count = ntohl(*p++);
345 if (len > max_blocksize)
348 /* set up the kvec */
351 pn = rqstp->rq_resused++;
352 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
353 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
354 len -= rqstp->rq_vec[v].iov_len;
358 return xdr_argsize_check(rqstp, p);
362 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
363 struct nfsd3_writeargs *args)
365 unsigned int len, v, hdr;
366 u32 max_blocksize = svc_max_payload(rqstp);
368 if (!(p = decode_fh(p, &args->fh))
369 || !(p = xdr_decode_hyper(p, &args->offset)))
372 args->count = ntohl(*p++);
373 args->stable = ntohl(*p++);
374 len = args->len = ntohl(*p++);
376 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
377 if (rqstp->rq_arg.len < hdr ||
378 rqstp->rq_arg.len - hdr < len)
381 rqstp->rq_vec[0].iov_base = (void*)p;
382 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
384 if (len > max_blocksize)
387 while (len > rqstp->rq_vec[v].iov_len) {
388 len -= rqstp->rq_vec[v].iov_len;
390 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
391 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
393 rqstp->rq_vec[v].iov_len = len;
396 return args->count == args->len && rqstp->rq_vec[0].iov_len > 0;
400 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
401 struct nfsd3_createargs *args)
403 if (!(p = decode_fh(p, &args->fh))
404 || !(p = decode_filename(p, &args->name, &args->len)))
407 switch (args->createmode = ntohl(*p++)) {
408 case NFS3_CREATE_UNCHECKED:
409 case NFS3_CREATE_GUARDED:
410 if (!(p = decode_sattr3(p, &args->attrs)))
413 case NFS3_CREATE_EXCLUSIVE:
421 return xdr_argsize_check(rqstp, p);
424 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
425 struct nfsd3_createargs *args)
427 if (!(p = decode_fh(p, &args->fh))
428 || !(p = decode_filename(p, &args->name, &args->len))
429 || !(p = decode_sattr3(p, &args->attrs)))
432 return xdr_argsize_check(rqstp, p);
436 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
437 struct nfsd3_symlinkargs *args)
444 if (!(p = decode_fh(p, &args->ffh))
445 || !(p = decode_filename(p, &args->fname, &args->flen))
446 || !(p = decode_sattr3(p, &args->attrs))
449 /* now decode the pathname, which might be larger than the first page.
450 * As we have to check for nul's anyway, we copy it into a new page
451 * This page appears in the rq_res.pages list, but as pages_len is always
452 * 0, it won't get in the way
455 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
458 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
460 /* first copy and check from the first page */
462 vec = &rqstp->rq_arg.head[0];
463 avail = vec->iov_len - (old - (char*)vec->iov_base);
464 while (len && avail && *old) {
469 /* now copy next page if there is one */
470 if (len && !avail && rqstp->rq_arg.page_len) {
471 avail = rqstp->rq_arg.page_len;
472 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
473 old = page_address(rqstp->rq_arg.pages[0]);
475 while (len && avail && *old) {
488 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
489 struct nfsd3_mknodargs *args)
491 if (!(p = decode_fh(p, &args->fh))
492 || !(p = decode_filename(p, &args->name, &args->len)))
495 args->ftype = ntohl(*p++);
497 if (args->ftype == NF3BLK || args->ftype == NF3CHR
498 || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
499 if (!(p = decode_sattr3(p, &args->attrs)))
503 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
504 args->major = ntohl(*p++);
505 args->minor = ntohl(*p++);
508 return xdr_argsize_check(rqstp, p);
512 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
513 struct nfsd3_renameargs *args)
515 if (!(p = decode_fh(p, &args->ffh))
516 || !(p = decode_filename(p, &args->fname, &args->flen))
517 || !(p = decode_fh(p, &args->tfh))
518 || !(p = decode_filename(p, &args->tname, &args->tlen)))
521 return xdr_argsize_check(rqstp, p);
525 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
526 struct nfsd3_readlinkargs *args)
528 if (!(p = decode_fh(p, &args->fh)))
531 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
533 return xdr_argsize_check(rqstp, p);
537 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
538 struct nfsd3_linkargs *args)
540 if (!(p = decode_fh(p, &args->ffh))
541 || !(p = decode_fh(p, &args->tfh))
542 || !(p = decode_filename(p, &args->tname, &args->tlen)))
545 return xdr_argsize_check(rqstp, p);
549 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
550 struct nfsd3_readdirargs *args)
552 if (!(p = decode_fh(p, &args->fh)))
554 p = xdr_decode_hyper(p, &args->cookie);
555 args->verf = p; p += 2;
557 args->count = ntohl(*p++);
559 if (args->count > PAGE_SIZE)
560 args->count = PAGE_SIZE;
563 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
565 return xdr_argsize_check(rqstp, p);
569 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
570 struct nfsd3_readdirargs *args)
573 u32 max_blocksize = svc_max_payload(rqstp);
575 if (!(p = decode_fh(p, &args->fh)))
577 p = xdr_decode_hyper(p, &args->cookie);
578 args->verf = p; p += 2;
579 args->dircount = ntohl(*p++);
580 args->count = ntohl(*p++);
582 len = (args->count > max_blocksize) ? max_blocksize :
587 pn = rqstp->rq_resused++;
589 args->buffer = page_address(rqstp->rq_respages[pn]);
593 return xdr_argsize_check(rqstp, p);
597 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
598 struct nfsd3_commitargs *args)
600 if (!(p = decode_fh(p, &args->fh)))
602 p = xdr_decode_hyper(p, &args->offset);
603 args->count = ntohl(*p++);
605 return xdr_argsize_check(rqstp, p);
609 * XDR encode functions
612 * There must be an encoding function for void results so svc_process
613 * will work properly.
616 nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
618 return xdr_ressize_check(rqstp, p);
623 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
624 struct nfsd3_attrstat *resp)
626 if (resp->status == 0)
627 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
628 return xdr_ressize_check(rqstp, p);
631 /* SETATTR, REMOVE, RMDIR */
633 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
634 struct nfsd3_attrstat *resp)
636 p = encode_wcc_data(rqstp, p, &resp->fh);
637 return xdr_ressize_check(rqstp, p);
642 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
643 struct nfsd3_diropres *resp)
645 if (resp->status == 0) {
646 p = encode_fh(p, &resp->fh);
647 p = encode_post_op_attr(rqstp, p, &resp->fh);
649 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
650 return xdr_ressize_check(rqstp, p);
655 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
656 struct nfsd3_accessres *resp)
658 p = encode_post_op_attr(rqstp, p, &resp->fh);
659 if (resp->status == 0)
660 *p++ = htonl(resp->access);
661 return xdr_ressize_check(rqstp, p);
666 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
667 struct nfsd3_readlinkres *resp)
669 p = encode_post_op_attr(rqstp, p, &resp->fh);
670 if (resp->status == 0) {
671 *p++ = htonl(resp->len);
672 xdr_ressize_check(rqstp, p);
673 rqstp->rq_res.page_len = resp->len;
675 /* need to pad the tail */
676 rqstp->rq_res.tail[0].iov_base = p;
678 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
682 return xdr_ressize_check(rqstp, p);
687 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
688 struct nfsd3_readres *resp)
690 p = encode_post_op_attr(rqstp, p, &resp->fh);
691 if (resp->status == 0) {
692 *p++ = htonl(resp->count);
693 *p++ = htonl(resp->eof);
694 *p++ = htonl(resp->count); /* xdr opaque count */
695 xdr_ressize_check(rqstp, p);
696 /* now update rqstp->rq_res to reflect data aswell */
697 rqstp->rq_res.page_len = resp->count;
698 if (resp->count & 3) {
699 /* need to pad the tail */
700 rqstp->rq_res.tail[0].iov_base = p;
702 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
706 return xdr_ressize_check(rqstp, p);
711 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
712 struct nfsd3_writeres *resp)
714 p = encode_wcc_data(rqstp, p, &resp->fh);
715 if (resp->status == 0) {
716 *p++ = htonl(resp->count);
717 *p++ = htonl(resp->committed);
718 *p++ = htonl(nfssvc_boot.tv_sec);
719 *p++ = htonl(nfssvc_boot.tv_usec);
721 return xdr_ressize_check(rqstp, p);
724 /* CREATE, MKDIR, SYMLINK, MKNOD */
726 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
727 struct nfsd3_diropres *resp)
729 if (resp->status == 0) {
731 p = encode_fh(p, &resp->fh);
732 p = encode_post_op_attr(rqstp, p, &resp->fh);
734 p = encode_wcc_data(rqstp, p, &resp->dirfh);
735 return xdr_ressize_check(rqstp, p);
740 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
741 struct nfsd3_renameres *resp)
743 p = encode_wcc_data(rqstp, p, &resp->ffh);
744 p = encode_wcc_data(rqstp, p, &resp->tfh);
745 return xdr_ressize_check(rqstp, p);
750 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
751 struct nfsd3_linkres *resp)
753 p = encode_post_op_attr(rqstp, p, &resp->fh);
754 p = encode_wcc_data(rqstp, p, &resp->tfh);
755 return xdr_ressize_check(rqstp, p);
760 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
761 struct nfsd3_readdirres *resp)
763 p = encode_post_op_attr(rqstp, p, &resp->fh);
765 if (resp->status == 0) {
766 /* stupid readdir cookie */
767 memcpy(p, resp->verf, 8); p += 2;
768 xdr_ressize_check(rqstp, p);
769 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
770 return 1; /*No room for trailer */
771 rqstp->rq_res.page_len = (resp->count) << 2;
773 /* add the 'tail' to the end of the 'head' page - page 0. */
774 rqstp->rq_res.tail[0].iov_base = p;
775 *p++ = 0; /* no more entries */
776 *p++ = htonl(resp->common.err == nfserr_eof);
777 rqstp->rq_res.tail[0].iov_len = 2<<2;
780 return xdr_ressize_check(rqstp, p);
784 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
785 int namlen, ino_t ino)
787 *p++ = xdr_one; /* mark entry present */
788 p = xdr_encode_hyper(p, ino); /* file id */
789 p = xdr_encode_array(p, name, namlen);/* name length & name */
791 cd->offset = p; /* remember pointer */
792 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
798 encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
801 p = encode_post_op_attr(cd->rqstp, p, fhp);
802 *p++ = xdr_one; /* yes, a file handle follows */
803 p = encode_fh(p, fhp);
809 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
810 const char *name, int namlen)
812 struct svc_export *exp;
813 struct dentry *dparent, *dchild;
816 dparent = cd->fh.fh_dentry;
817 exp = cd->fh.fh_export;
819 fh_init(fhp, NFS3_FHSIZE);
820 if (isdotent(name, namlen)) {
822 dchild = dget_parent(dparent);
823 if (dchild == dparent) {
824 /* filesystem root - cannot return filehandle for ".." */
829 dchild = dget(dparent);
831 dchild = lookup_one_len(name, dparent, namlen);
834 if (d_mountpoint(dchild) ||
835 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
843 * Encode a directory entry. This one works for both normal readdir
845 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
846 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
848 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
852 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
853 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
855 encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
856 loff_t offset, ino_t ino, unsigned int d_type, int plus)
858 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
860 __be32 *p = cd->buffer;
861 caddr_t curr_page_addr = NULL;
862 int pn; /* current page number */
863 int slen; /* string (name) length */
864 int elen; /* estimated entry length in words */
865 int num_entry_words = 0; /* actual number of words */
868 u64 offset64 = offset;
870 if (unlikely(cd->offset1)) {
871 /* we ended up with offset on a page boundary */
872 *cd->offset = htonl(offset64 >> 32);
873 *cd->offset1 = htonl(offset64 & 0xffffffff);
876 xdr_encode_hyper(cd->offset, offset64);
881 dprintk("encode_entry(%.*s @%ld%s)\n",
882 namlen, name, (long) offset, plus? " plus" : "");
885 /* truncate filename if too long */
886 if (namlen > NFS3_MAXNAMLEN)
887 namlen = NFS3_MAXNAMLEN;
889 slen = XDR_QUADLEN(namlen);
890 elen = slen + NFS3_ENTRY_BAGGAGE
891 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
893 if (cd->buflen < elen) {
894 cd->common.err = nfserr_toosmall;
898 /* determine which page in rq_respages[] we are currently filling */
899 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
900 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
902 if (((caddr_t)cd->buffer >= curr_page_addr) &&
903 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
907 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
908 /* encode entry in current page */
910 p = encode_entry_baggage(cd, p, name, namlen, ino);
912 /* throw in readdirplus baggage */
916 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
920 p = encode_entryplus_baggage(cd, p, &fh);
922 num_entry_words = p - cd->buffer;
923 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
924 /* temporarily encode entry into next page, then move back to
925 * current and next page in rq_respages[] */
929 /* grab next page for temporary storage of entry */
930 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
932 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
934 /* throw in readdirplus baggage */
938 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
939 /* zero out the filehandle */
943 p1 = encode_entryplus_baggage(cd, p1, &fh);
946 /* determine entry word length and lengths to go in pages */
947 num_entry_words = p1 - tmp;
948 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
949 if ((num_entry_words << 2) < len1) {
950 /* the actual number of words in the entry is less
951 * than elen and can still fit in the current page
953 memmove(p, tmp, num_entry_words << 2);
954 p += num_entry_words;
957 cd->offset = cd->buffer + (cd->offset - tmp);
959 unsigned int offset_r = (cd->offset - tmp) << 2;
961 /* update pointer to offset location.
962 * This is a 64bit quantity, so we need to
964 * - entirely in first page
965 * - entirely in second page
966 * - 4 bytes in each page
968 if (offset_r + 8 <= len1) {
969 cd->offset = p + (cd->offset - tmp);
970 } else if (offset_r >= len1) {
971 cd->offset -= len1 >> 2;
973 /* sitting on the fence */
974 BUG_ON(offset_r != len1 - 4);
975 cd->offset = p + (cd->offset - tmp);
979 len2 = (num_entry_words << 2) - len1;
981 /* move from temp page to current and next pages */
982 memmove(p, tmp, len1);
983 memmove(tmp, (caddr_t)tmp+len1, len2);
985 p = tmp + (len2 >> 2);
989 cd->common.err = nfserr_toosmall;
993 cd->buflen -= num_entry_words;
995 cd->common.err = nfs_ok;
1001 nfs3svc_encode_entry(void *cd, const char *name,
1002 int namlen, loff_t offset, u64 ino, unsigned int d_type)
1004 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1008 nfs3svc_encode_entry_plus(void *cd, const char *name,
1009 int namlen, loff_t offset, u64 ino,
1010 unsigned int d_type)
1012 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1017 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
1018 struct nfsd3_fsstatres *resp)
1020 struct kstatfs *s = &resp->stats;
1021 u64 bs = s->f_bsize;
1023 *p++ = xdr_zero; /* no post_op_attr */
1025 if (resp->status == 0) {
1026 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1027 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1028 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1029 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1030 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1031 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1032 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1034 return xdr_ressize_check(rqstp, p);
1039 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
1040 struct nfsd3_fsinfores *resp)
1042 *p++ = xdr_zero; /* no post_op_attr */
1044 if (resp->status == 0) {
1045 *p++ = htonl(resp->f_rtmax);
1046 *p++ = htonl(resp->f_rtpref);
1047 *p++ = htonl(resp->f_rtmult);
1048 *p++ = htonl(resp->f_wtmax);
1049 *p++ = htonl(resp->f_wtpref);
1050 *p++ = htonl(resp->f_wtmult);
1051 *p++ = htonl(resp->f_dtpref);
1052 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1055 *p++ = htonl(resp->f_properties);
1058 return xdr_ressize_check(rqstp, p);
1063 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
1064 struct nfsd3_pathconfres *resp)
1066 *p++ = xdr_zero; /* no post_op_attr */
1068 if (resp->status == 0) {
1069 *p++ = htonl(resp->p_link_max);
1070 *p++ = htonl(resp->p_name_max);
1071 *p++ = htonl(resp->p_no_trunc);
1072 *p++ = htonl(resp->p_chown_restricted);
1073 *p++ = htonl(resp->p_case_insensitive);
1074 *p++ = htonl(resp->p_case_preserving);
1077 return xdr_ressize_check(rqstp, p);
1082 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
1083 struct nfsd3_commitres *resp)
1085 p = encode_wcc_data(rqstp, p, &resp->fh);
1086 /* Write verifier */
1087 if (resp->status == 0) {
1088 *p++ = htonl(nfssvc_boot.tv_sec);
1089 *p++ = htonl(nfssvc_boot.tv_usec);
1091 return xdr_ressize_check(rqstp, p);
1095 * XDR release functions
1098 nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
1099 struct nfsd3_attrstat *resp)
1106 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
1107 struct nfsd3_fhandle_pair *resp)