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)
131 iap->ia_valid |= ATTR_MODE;
132 iap->ia_mode = ntohl(*p++);
135 iap->ia_valid |= ATTR_UID;
139 iap->ia_valid |= ATTR_GID;
142 iap->ia_uid = INOXID_UID(1, uid, gid);
143 iap->ia_gid = INOXID_GID(1, uid, gid);
144 iap->ia_xid = INOXID_XID(1, uid, gid, 0);
148 iap->ia_valid |= ATTR_SIZE;
149 p = xdr_decode_hyper(p, &newsize);
150 if (newsize <= NFS_OFFSET_MAX)
151 iap->ia_size = newsize;
153 iap->ia_size = NFS_OFFSET_MAX;
155 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
156 iap->ia_valid |= ATTR_ATIME;
157 } else if (tmp == 2) { /* set to client time */
158 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
159 iap->ia_atime.tv_sec = ntohl(*p++);
160 iap->ia_atime.tv_nsec = ntohl(*p++);
162 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
163 iap->ia_valid |= ATTR_MTIME;
164 } else if (tmp == 2) { /* set to client time */
165 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
166 iap->ia_mtime.tv_sec = ntohl(*p++);
167 iap->ia_mtime.tv_nsec = ntohl(*p++);
173 encode_fattr3(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
175 struct vfsmount *mnt = fhp->fh_export->ex_mnt;
176 struct dentry *dentry = fhp->fh_dentry;
178 struct timespec time;
180 vfs_getattr(mnt, dentry, &stat);
182 *p++ = htonl(nfs3_ftypes[(stat.mode & S_IFMT) >> 12]);
183 *p++ = htonl((u32) stat.mode);
184 *p++ = htonl((u32) stat.nlink);
185 *p++ = htonl((u32) nfsd_ruid(rqstp,
186 XIDINO_UID(XID_TAG(dentry->d_inode), stat.uid, stat.xid)));
187 *p++ = htonl((u32) nfsd_rgid(rqstp,
188 XIDINO_GID(XID_TAG(dentry->d_inode), stat.gid, stat.xid)));
189 if (S_ISLNK(stat.mode) && stat.size > NFS3_MAXPATHLEN) {
190 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
192 p = xdr_encode_hyper(p, (u64) stat.size);
194 p = xdr_encode_hyper(p, ((u64)stat.blocks) << 9);
195 *p++ = htonl((u32) MAJOR(stat.rdev));
196 *p++ = htonl((u32) MINOR(stat.rdev));
197 if (is_fsid(fhp, rqstp->rq_reffh))
198 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
200 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat.dev));
201 p = xdr_encode_hyper(p, (u64) stat.ino);
202 p = encode_time3(p, &stat.atime);
203 lease_get_mtime(dentry->d_inode, &time);
204 p = encode_time3(p, &time);
205 p = encode_time3(p, &stat.ctime);
211 encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
213 struct inode *inode = fhp->fh_dentry->d_inode;
215 /* Attributes to follow */
218 *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
219 *p++ = htonl((u32) fhp->fh_post_mode);
220 *p++ = htonl((u32) fhp->fh_post_nlink);
221 *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
222 *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
223 if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
224 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
226 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
228 p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
229 *p++ = fhp->fh_post_rdev[0];
230 *p++ = fhp->fh_post_rdev[1];
231 if (is_fsid(fhp, rqstp->rq_reffh))
232 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
234 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
235 p = xdr_encode_hyper(p, (u64) inode->i_ino);
236 p = encode_time3(p, &fhp->fh_post_atime);
237 p = encode_time3(p, &fhp->fh_post_mtime);
238 p = encode_time3(p, &fhp->fh_post_ctime);
244 * Encode post-operation attributes.
245 * The inode may be NULL if the call failed because of a stale file
246 * handle. In this case, no attributes are returned.
249 encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
251 struct dentry *dentry = fhp->fh_dentry;
252 if (dentry && dentry->d_inode != NULL) {
253 *p++ = xdr_one; /* attributes follow */
254 return encode_fattr3(rqstp, p, fhp);
261 * Enocde weak cache consistency data
264 encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
266 struct dentry *dentry = fhp->fh_dentry;
268 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
269 if (fhp->fh_pre_saved) {
271 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
272 p = encode_time3(p, &fhp->fh_pre_mtime);
273 p = encode_time3(p, &fhp->fh_pre_ctime);
277 return encode_saved_post_attr(rqstp, p, fhp);
279 /* no pre- or post-attrs */
281 return encode_post_op_attr(rqstp, p, fhp);
286 * XDR decode functions
289 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
291 if (!(p = decode_fh(p, &args->fh)))
293 return xdr_argsize_check(rqstp, p);
297 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
298 struct nfsd3_sattrargs *args)
300 if (!(p = decode_fh(p, &args->fh))
301 || !(p = decode_sattr3(p, &args->attrs)))
304 if ((args->check_guard = ntohl(*p++)) != 0) {
305 struct timespec time;
306 p = decode_time3(p, &time);
307 args->guardtime = time.tv_sec;
310 return xdr_argsize_check(rqstp, p);
314 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
315 struct nfsd3_diropargs *args)
317 if (!(p = decode_fh(p, &args->fh))
318 || !(p = decode_filename(p, &args->name, &args->len)))
321 return xdr_argsize_check(rqstp, p);
325 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
326 struct nfsd3_accessargs *args)
328 if (!(p = decode_fh(p, &args->fh)))
330 args->access = ntohl(*p++);
332 return xdr_argsize_check(rqstp, p);
336 nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
337 struct nfsd3_readargs *args)
342 if (!(p = decode_fh(p, &args->fh))
343 || !(p = xdr_decode_hyper(p, &args->offset)))
346 len = args->count = ntohl(*p++);
348 if (len > NFSSVC_MAXBLKSIZE)
349 len = NFSSVC_MAXBLKSIZE;
351 /* set up the kvec */
354 pn = rqstp->rq_resused;
355 svc_take_page(rqstp);
356 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
357 args->vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
358 len -= args->vec[v].iov_len;
362 return xdr_argsize_check(rqstp, p);
366 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
367 struct nfsd3_writeargs *args)
369 unsigned int len, v, hdr;
371 if (!(p = decode_fh(p, &args->fh))
372 || !(p = xdr_decode_hyper(p, &args->offset)))
375 args->count = ntohl(*p++);
376 args->stable = ntohl(*p++);
377 len = args->len = ntohl(*p++);
379 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
380 if (rqstp->rq_arg.len < len + hdr)
383 args->vec[0].iov_base = (void*)p;
384 args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
386 if (len > NFSSVC_MAXBLKSIZE)
387 len = NFSSVC_MAXBLKSIZE;
389 while (len > args->vec[v].iov_len) {
390 len -= args->vec[v].iov_len;
392 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]);
393 args->vec[v].iov_len = PAGE_SIZE;
395 args->vec[v].iov_len = len;
398 return args->count == args->len && args->vec[0].iov_len > 0;
402 nfs3svc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
403 struct nfsd3_createargs *args)
405 if (!(p = decode_fh(p, &args->fh))
406 || !(p = decode_filename(p, &args->name, &args->len)))
409 switch (args->createmode = ntohl(*p++)) {
410 case NFS3_CREATE_UNCHECKED:
411 case NFS3_CREATE_GUARDED:
412 if (!(p = decode_sattr3(p, &args->attrs)))
415 case NFS3_CREATE_EXCLUSIVE:
423 return xdr_argsize_check(rqstp, p);
426 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, u32 *p,
427 struct nfsd3_createargs *args)
429 if (!(p = decode_fh(p, &args->fh))
430 || !(p = decode_filename(p, &args->name, &args->len))
431 || !(p = decode_sattr3(p, &args->attrs)))
434 return xdr_argsize_check(rqstp, p);
438 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
439 struct nfsd3_symlinkargs *args)
446 if (!(p = decode_fh(p, &args->ffh))
447 || !(p = decode_filename(p, &args->fname, &args->flen))
448 || !(p = decode_sattr3(p, &args->attrs))
451 /* now decode the pathname, which might be larger than the first page.
452 * As we have to check for nul's anyway, we copy it into a new page
453 * This page appears in the rq_res.pages list, but as pages_len is always
454 * 0, it won't get in the way
456 svc_take_page(rqstp);
458 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
460 args->tname = new = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
462 /* first copy and check from the first page */
464 vec = &rqstp->rq_arg.head[0];
465 avail = vec->iov_len - (old - (char*)vec->iov_base);
466 while (len && avail && *old) {
471 /* now copy next page if there is one */
472 if (len && !avail && rqstp->rq_arg.page_len) {
473 avail = rqstp->rq_arg.page_len;
474 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
475 old = page_address(rqstp->rq_arg.pages[0]);
477 while (len && avail && *old) {
490 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, u32 *p,
491 struct nfsd3_mknodargs *args)
493 if (!(p = decode_fh(p, &args->fh))
494 || !(p = decode_filename(p, &args->name, &args->len)))
497 args->ftype = ntohl(*p++);
499 if (args->ftype == NF3BLK || args->ftype == NF3CHR
500 || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
501 if (!(p = decode_sattr3(p, &args->attrs)))
505 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
506 args->major = ntohl(*p++);
507 args->minor = ntohl(*p++);
510 return xdr_argsize_check(rqstp, p);
514 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
515 struct nfsd3_renameargs *args)
517 if (!(p = decode_fh(p, &args->ffh))
518 || !(p = decode_filename(p, &args->fname, &args->flen))
519 || !(p = decode_fh(p, &args->tfh))
520 || !(p = decode_filename(p, &args->tname, &args->tlen)))
523 return xdr_argsize_check(rqstp, p);
527 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p,
528 struct nfsd3_readlinkargs *args)
530 if (!(p = decode_fh(p, &args->fh)))
532 svc_take_page(rqstp);
533 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
535 return xdr_argsize_check(rqstp, p);
539 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
540 struct nfsd3_linkargs *args)
542 if (!(p = decode_fh(p, &args->ffh))
543 || !(p = decode_fh(p, &args->tfh))
544 || !(p = decode_filename(p, &args->tname, &args->tlen)))
547 return xdr_argsize_check(rqstp, p);
551 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
552 struct nfsd3_readdirargs *args)
554 if (!(p = decode_fh(p, &args->fh)))
556 p = xdr_decode_hyper(p, &args->cookie);
557 args->verf = p; p += 2;
559 args->count = ntohl(*p++);
561 if (args->count > PAGE_SIZE)
562 args->count = PAGE_SIZE;
564 svc_take_page(rqstp);
565 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
567 return xdr_argsize_check(rqstp, p);
571 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, u32 *p,
572 struct nfsd3_readdirargs *args)
576 if (!(p = decode_fh(p, &args->fh)))
578 p = xdr_decode_hyper(p, &args->cookie);
579 args->verf = p; p += 2;
580 args->dircount = ntohl(*p++);
581 args->count = ntohl(*p++);
583 len = (args->count > NFSSVC_MAXBLKSIZE) ? NFSSVC_MAXBLKSIZE :
588 pn = rqstp->rq_resused;
589 svc_take_page(rqstp);
591 args->buffer = page_address(rqstp->rq_respages[pn]);
595 return xdr_argsize_check(rqstp, p);
599 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, u32 *p,
600 struct nfsd3_commitargs *args)
602 if (!(p = decode_fh(p, &args->fh)))
604 p = xdr_decode_hyper(p, &args->offset);
605 args->count = ntohl(*p++);
607 return xdr_argsize_check(rqstp, p);
611 * XDR encode functions
614 * There must be an encoding function for void results so svc_process
615 * will work properly.
618 nfs3svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
620 return xdr_ressize_check(rqstp, p);
625 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
626 struct nfsd3_attrstat *resp)
628 if (resp->status == 0)
629 p = encode_fattr3(rqstp, p, &resp->fh);
630 return xdr_ressize_check(rqstp, p);
633 /* SETATTR, REMOVE, RMDIR */
635 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, u32 *p,
636 struct nfsd3_attrstat *resp)
638 p = encode_wcc_data(rqstp, p, &resp->fh);
639 return xdr_ressize_check(rqstp, p);
644 nfs3svc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
645 struct nfsd3_diropres *resp)
647 if (resp->status == 0) {
648 p = encode_fh(p, &resp->fh);
649 p = encode_post_op_attr(rqstp, p, &resp->fh);
651 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
652 return xdr_ressize_check(rqstp, p);
657 nfs3svc_encode_accessres(struct svc_rqst *rqstp, u32 *p,
658 struct nfsd3_accessres *resp)
660 p = encode_post_op_attr(rqstp, p, &resp->fh);
661 if (resp->status == 0)
662 *p++ = htonl(resp->access);
663 return xdr_ressize_check(rqstp, p);
668 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
669 struct nfsd3_readlinkres *resp)
671 p = encode_post_op_attr(rqstp, p, &resp->fh);
672 if (resp->status == 0) {
673 *p++ = htonl(resp->len);
674 xdr_ressize_check(rqstp, p);
675 rqstp->rq_res.page_len = resp->len;
677 /* need to pad the tail */
678 rqstp->rq_restailpage = 0;
679 rqstp->rq_res.tail[0].iov_base = p;
681 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
685 return xdr_ressize_check(rqstp, p);
690 nfs3svc_encode_readres(struct svc_rqst *rqstp, u32 *p,
691 struct nfsd3_readres *resp)
693 p = encode_post_op_attr(rqstp, p, &resp->fh);
694 if (resp->status == 0) {
695 *p++ = htonl(resp->count);
696 *p++ = htonl(resp->eof);
697 *p++ = htonl(resp->count); /* xdr opaque count */
698 xdr_ressize_check(rqstp, p);
699 /* now update rqstp->rq_res to reflect data aswell */
700 rqstp->rq_res.page_len = resp->count;
701 if (resp->count & 3) {
702 /* need to pad the tail */
703 rqstp->rq_restailpage = 0;
704 rqstp->rq_res.tail[0].iov_base = p;
706 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
710 return xdr_ressize_check(rqstp, p);
715 nfs3svc_encode_writeres(struct svc_rqst *rqstp, u32 *p,
716 struct nfsd3_writeres *resp)
718 p = encode_wcc_data(rqstp, p, &resp->fh);
719 if (resp->status == 0) {
720 *p++ = htonl(resp->count);
721 *p++ = htonl(resp->committed);
722 *p++ = htonl(nfssvc_boot.tv_sec);
723 *p++ = htonl(nfssvc_boot.tv_usec);
725 return xdr_ressize_check(rqstp, p);
728 /* CREATE, MKDIR, SYMLINK, MKNOD */
730 nfs3svc_encode_createres(struct svc_rqst *rqstp, u32 *p,
731 struct nfsd3_diropres *resp)
733 if (resp->status == 0) {
735 p = encode_fh(p, &resp->fh);
736 p = encode_post_op_attr(rqstp, p, &resp->fh);
738 p = encode_wcc_data(rqstp, p, &resp->dirfh);
739 return xdr_ressize_check(rqstp, p);
744 nfs3svc_encode_renameres(struct svc_rqst *rqstp, u32 *p,
745 struct nfsd3_renameres *resp)
747 p = encode_wcc_data(rqstp, p, &resp->ffh);
748 p = encode_wcc_data(rqstp, p, &resp->tfh);
749 return xdr_ressize_check(rqstp, p);
754 nfs3svc_encode_linkres(struct svc_rqst *rqstp, u32 *p,
755 struct nfsd3_linkres *resp)
757 p = encode_post_op_attr(rqstp, p, &resp->fh);
758 p = encode_wcc_data(rqstp, p, &resp->tfh);
759 return xdr_ressize_check(rqstp, p);
764 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
765 struct nfsd3_readdirres *resp)
767 p = encode_post_op_attr(rqstp, p, &resp->fh);
769 if (resp->status == 0) {
770 /* stupid readdir cookie */
771 memcpy(p, resp->verf, 8); p += 2;
772 xdr_ressize_check(rqstp, p);
774 *p++ = 0; /* no more entries */
775 *p++ = htonl(resp->common.err == nfserr_eof);
776 rqstp->rq_res.page_len = (resp->count + 2) << 2;
779 return xdr_ressize_check(rqstp, p);
783 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
784 int namlen, ino_t ino)
786 *p++ = xdr_one; /* mark entry present */
787 p = xdr_encode_hyper(p, ino); /* file id */
788 p = xdr_encode_array(p, name, namlen);/* name length & name */
790 cd->offset = p; /* remember pointer */
791 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
797 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
800 p = encode_post_op_attr(cd->rqstp, p, fhp);
801 *p++ = xdr_one; /* yes, a file handle follows */
802 p = encode_fh(p, fhp);
808 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
809 const char *name, int namlen)
811 struct svc_export *exp;
812 struct dentry *dparent, *dchild;
815 dparent = cd->fh.fh_dentry;
816 exp = cd->fh.fh_export;
818 fh_init(fhp, NFS3_FHSIZE);
819 if (isdotent(name, namlen)) {
821 dchild = dget_parent(dparent);
823 dchild = dget(dparent);
825 dchild = lookup_one_len(name, dparent, namlen);
828 if (d_mountpoint(dchild) ||
829 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
837 * Encode a directory entry. This one works for both normal readdir
839 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
840 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
842 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
846 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
847 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
849 encode_entry(struct readdir_cd *ccd, const char *name,
850 int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
852 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
855 caddr_t curr_page_addr = NULL;
856 int pn; /* current page number */
857 int slen; /* string (name) length */
858 int elen; /* estimated entry length in words */
859 int num_entry_words = 0; /* actual number of words */
862 u64 offset64 = offset;
864 if (unlikely(cd->offset1)) {
865 /* we ended up with offset on a page boundary */
866 *cd->offset = htonl(offset64 >> 32);
867 *cd->offset1 = htonl(offset64 & 0xffffffff);
870 xdr_encode_hyper(cd->offset, (u64) offset);
875 dprintk("encode_entry(%.*s @%ld%s)\n",
876 namlen, name, (long) offset, plus? " plus" : "");
879 /* truncate filename if too long */
880 if (namlen > NFS3_MAXNAMLEN)
881 namlen = NFS3_MAXNAMLEN;
883 slen = XDR_QUADLEN(namlen);
884 elen = slen + NFS3_ENTRY_BAGGAGE
885 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
887 if (cd->buflen < elen) {
888 cd->common.err = nfserr_toosmall;
892 /* determine which page in rq_respages[] we are currently filling */
893 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
894 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
896 if (((caddr_t)cd->buffer >= curr_page_addr) &&
897 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
901 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
902 /* encode entry in current page */
904 p = encode_entry_baggage(cd, p, name, namlen, ino);
906 /* throw in readdirplus baggage */
910 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
914 p = encode_entryplus_baggage(cd, p, &fh);
916 num_entry_words = p - cd->buffer;
917 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
918 /* temporarily encode entry into next page, then move back to
919 * current and next page in rq_respages[] */
923 /* grab next page for temporary storage of entry */
924 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
926 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
928 /* throw in readdirplus baggage */
932 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
933 /* zero out the filehandle */
937 p1 = encode_entryplus_baggage(cd, p1, &fh);
940 /* determine entry word length and lengths to go in pages */
941 num_entry_words = p1 - tmp;
942 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
943 if ((num_entry_words << 2) < len1) {
944 /* the actual number of words in the entry is less
945 * than elen and can still fit in the current page
947 memmove(p, tmp, num_entry_words << 2);
948 p += num_entry_words;
951 cd->offset = cd->buffer + (cd->offset - tmp);
953 unsigned int offset_r = (cd->offset - tmp) << 2;
955 /* update pointer to offset location.
956 * This is a 64bit quantity, so we need to
958 * - entirely in first page
959 * - entirely in second page
960 * - 4 bytes in each page
962 if (offset_r + 8 <= len1) {
963 cd->offset = p + (cd->offset - tmp);
964 } else if (offset_r >= len1) {
965 cd->offset -= len1 >> 2;
967 /* sitting on the fence */
968 BUG_ON(offset_r != len1 - 4);
969 cd->offset = p + (cd->offset - tmp);
973 len2 = (num_entry_words << 2) - len1;
975 /* move from temp page to current and next pages */
976 memmove(p, tmp, len1);
977 memmove(tmp, (caddr_t)tmp+len1, len2);
979 p = tmp + (len2 >> 2);
983 cd->common.err = nfserr_toosmall;
987 cd->buflen -= num_entry_words;
989 cd->common.err = nfs_ok;
995 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
996 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
998 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1002 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
1003 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1005 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1010 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1011 struct nfsd3_fsstatres *resp)
1013 struct kstatfs *s = &resp->stats;
1014 u64 bs = s->f_bsize;
1016 *p++ = xdr_zero; /* no post_op_attr */
1018 if (resp->status == 0) {
1019 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1020 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1021 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1022 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1023 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1024 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1025 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1027 return xdr_ressize_check(rqstp, p);
1032 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1033 struct nfsd3_fsinfores *resp)
1035 *p++ = xdr_zero; /* no post_op_attr */
1037 if (resp->status == 0) {
1038 *p++ = htonl(resp->f_rtmax);
1039 *p++ = htonl(resp->f_rtpref);
1040 *p++ = htonl(resp->f_rtmult);
1041 *p++ = htonl(resp->f_wtmax);
1042 *p++ = htonl(resp->f_wtpref);
1043 *p++ = htonl(resp->f_wtmult);
1044 *p++ = htonl(resp->f_dtpref);
1045 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1048 *p++ = htonl(resp->f_properties);
1051 return xdr_ressize_check(rqstp, p);
1056 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1057 struct nfsd3_pathconfres *resp)
1059 *p++ = xdr_zero; /* no post_op_attr */
1061 if (resp->status == 0) {
1062 *p++ = htonl(resp->p_link_max);
1063 *p++ = htonl(resp->p_name_max);
1064 *p++ = htonl(resp->p_no_trunc);
1065 *p++ = htonl(resp->p_chown_restricted);
1066 *p++ = htonl(resp->p_case_insensitive);
1067 *p++ = htonl(resp->p_case_preserving);
1070 return xdr_ressize_check(rqstp, p);
1075 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1076 struct nfsd3_commitres *resp)
1078 p = encode_wcc_data(rqstp, p, &resp->fh);
1079 /* Write verifier */
1080 if (resp->status == 0) {
1081 *p++ = htonl(nfssvc_boot.tv_sec);
1082 *p++ = htonl(nfssvc_boot.tv_usec);
1084 return xdr_ressize_check(rqstp, p);
1088 * XDR release functions
1091 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1092 struct nfsd3_attrstat *resp)
1099 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1100 struct nfsd3_fhandle_pair *resp)