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>
25 #define NFSDDBG_FACILITY NFSDDBG_XDR
29 * Mapping of S_IF* types to NFS file types
31 static u32 nfs3_ftypes
[] = {
32 NF3NON
, NF3FIFO
, NF3CHR
, NF3BAD
,
33 NF3DIR
, NF3BAD
, NF3BLK
, NF3BAD
,
34 NF3REG
, NF3BAD
, NF3LNK
, NF3BAD
,
35 NF3SOCK
, NF3BAD
, NF3LNK
, NF3BAD
,
39 * XDR functions for basic NFS types
42 encode_time3(__be32
*p
, struct timespec
*time
)
44 *p
++ = htonl((u32
) time
->tv_sec
); *p
++ = htonl(time
->tv_nsec
);
49 decode_time3(__be32
*p
, struct timespec
*time
)
51 time
->tv_sec
= ntohl(*p
++);
52 time
->tv_nsec
= ntohl(*p
++);
57 decode_fh(__be32
*p
, struct svc_fh
*fhp
)
60 fh_init(fhp
, NFS3_FHSIZE
);
62 if (size
> NFS3_FHSIZE
)
65 memcpy(&fhp
->fh_handle
.fh_base
, p
, size
);
66 fhp
->fh_handle
.fh_size
= size
;
67 return p
+ XDR_QUADLEN(size
);
70 /* Helper function for NFSv3 ACL code */
71 __be32
*nfs3svc_decode_fh(__be32
*p
, struct svc_fh
*fhp
)
73 return decode_fh(p
, fhp
);
77 encode_fh(__be32
*p
, struct svc_fh
*fhp
)
79 unsigned int size
= fhp
->fh_handle
.fh_size
;
81 if (size
) p
[XDR_QUADLEN(size
)-1]=0;
82 memcpy(p
, &fhp
->fh_handle
.fh_base
, size
);
83 return p
+ XDR_QUADLEN(size
);
87 * Decode a file name and make sure that the path contains
88 * no slashes or null bytes.
91 decode_filename(__be32
*p
, char **namp
, unsigned int *lenp
)
96 if ((p
= xdr_decode_string_inplace(p
, namp
, lenp
, NFS3_MAXNAMLEN
)) != NULL
) {
97 for (i
= 0, name
= *namp
; i
< *lenp
; i
++, name
++) {
98 if (*name
== '\0' || *name
== '/')
107 decode_sattr3(__be32
*p
, struct iattr
*iap
)
114 iap
->ia_valid
|= ATTR_MODE
;
115 iap
->ia_mode
= ntohl(*p
++);
118 iap
->ia_valid
|= ATTR_UID
;
119 iap
->ia_uid
= ntohl(*p
++);
122 iap
->ia_valid
|= ATTR_GID
;
123 iap
->ia_gid
= ntohl(*p
++);
128 iap
->ia_valid
|= ATTR_SIZE
;
129 p
= xdr_decode_hyper(p
, &newsize
);
130 if (newsize
<= NFS_OFFSET_MAX
)
131 iap
->ia_size
= newsize
;
133 iap
->ia_size
= NFS_OFFSET_MAX
;
135 if ((tmp
= ntohl(*p
++)) == 1) { /* set to server time */
136 iap
->ia_valid
|= ATTR_ATIME
;
137 } else if (tmp
== 2) { /* set to client time */
138 iap
->ia_valid
|= ATTR_ATIME
| ATTR_ATIME_SET
;
139 iap
->ia_atime
.tv_sec
= ntohl(*p
++);
140 iap
->ia_atime
.tv_nsec
= ntohl(*p
++);
142 if ((tmp
= ntohl(*p
++)) == 1) { /* set to server time */
143 iap
->ia_valid
|= ATTR_MTIME
;
144 } else if (tmp
== 2) { /* set to client time */
145 iap
->ia_valid
|= ATTR_MTIME
| ATTR_MTIME_SET
;
146 iap
->ia_mtime
.tv_sec
= ntohl(*p
++);
147 iap
->ia_mtime
.tv_nsec
= ntohl(*p
++);
152 static __be32
*encode_fsid(__be32
*p
, struct svc_fh
*fhp
)
155 switch(fsid_source(fhp
)) {
158 p
= xdr_encode_hyper(p
, (u64
)huge_encode_dev
159 (fhp
->fh_dentry
->d_inode
->i_sb
->s_dev
));
161 case FSIDSOURCE_FSID
:
162 p
= xdr_encode_hyper(p
, (u64
) fhp
->fh_export
->ex_fsid
);
164 case FSIDSOURCE_UUID
:
165 f
= ((u64
*)fhp
->fh_export
->ex_uuid
)[0];
166 f
^= ((u64
*)fhp
->fh_export
->ex_uuid
)[1];
167 p
= xdr_encode_hyper(p
, f
);
174 encode_fattr3(struct svc_rqst
*rqstp
, __be32
*p
, struct svc_fh
*fhp
,
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
, stat
->uid
));
181 *p
++ = htonl((u32
) nfsd_rgid(rqstp
, stat
->gid
));
182 if (S_ISLNK(stat
->mode
) && stat
->size
> NFS3_MAXPATHLEN
) {
183 p
= xdr_encode_hyper(p
, (u64
) NFS3_MAXPATHLEN
);
185 p
= xdr_encode_hyper(p
, (u64
) stat
->size
);
187 p
= xdr_encode_hyper(p
, ((u64
)stat
->blocks
) << 9);
188 *p
++ = htonl((u32
) MAJOR(stat
->rdev
));
189 *p
++ = htonl((u32
) MINOR(stat
->rdev
));
190 p
= encode_fsid(p
, fhp
);
191 p
= xdr_encode_hyper(p
, stat
->ino
);
192 p
= encode_time3(p
, &stat
->atime
);
193 p
= encode_time3(p
, &stat
->mtime
);
194 p
= encode_time3(p
, &stat
->ctime
);
200 encode_saved_post_attr(struct svc_rqst
*rqstp
, __be32
*p
, struct svc_fh
*fhp
)
202 /* Attributes to follow */
204 return encode_fattr3(rqstp
, p
, fhp
, &fhp
->fh_post_attr
);
208 * Encode post-operation attributes.
209 * The inode may be NULL if the call failed because of a stale file
210 * handle. In this case, no attributes are returned.
213 encode_post_op_attr(struct svc_rqst
*rqstp
, __be32
*p
, struct svc_fh
*fhp
)
215 struct dentry
*dentry
= fhp
->fh_dentry
;
216 if (dentry
&& dentry
->d_inode
) {
220 err
= vfs_getattr(fhp
->fh_export
->ex_path
.mnt
, dentry
, &stat
);
222 *p
++ = xdr_one
; /* attributes follow */
223 lease_get_mtime(dentry
->d_inode
, &stat
.mtime
);
224 return encode_fattr3(rqstp
, p
, fhp
, &stat
);
231 /* Helper for NFSv3 ACLs */
233 nfs3svc_encode_post_op_attr(struct svc_rqst
*rqstp
, __be32
*p
, struct svc_fh
*fhp
)
235 return encode_post_op_attr(rqstp
, p
, fhp
);
239 * Enocde weak cache consistency data
242 encode_wcc_data(struct svc_rqst
*rqstp
, __be32
*p
, struct svc_fh
*fhp
)
244 struct dentry
*dentry
= fhp
->fh_dentry
;
246 if (dentry
&& dentry
->d_inode
&& fhp
->fh_post_saved
) {
247 if (fhp
->fh_pre_saved
) {
249 p
= xdr_encode_hyper(p
, (u64
) fhp
->fh_pre_size
);
250 p
= encode_time3(p
, &fhp
->fh_pre_mtime
);
251 p
= encode_time3(p
, &fhp
->fh_pre_ctime
);
255 return encode_saved_post_attr(rqstp
, p
, fhp
);
257 /* no pre- or post-attrs */
259 return encode_post_op_attr(rqstp
, p
, fhp
);
263 * Fill in the post_op attr for the wcc data
265 void fill_post_wcc(struct svc_fh
*fhp
)
269 if (fhp
->fh_post_saved
)
270 printk("nfsd: inode locked twice during operation.\n");
272 err
= vfs_getattr(fhp
->fh_export
->ex_path
.mnt
, fhp
->fh_dentry
,
275 fhp
->fh_post_saved
= 0;
277 fhp
->fh_post_saved
= 1;
281 * XDR decode functions
284 nfs3svc_decode_fhandle(struct svc_rqst
*rqstp
, __be32
*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
, __be32
*p
,
293 struct nfsd3_sattrargs
*args
)
295 if (!(p
= decode_fh(p
, &args
->fh
)))
297 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
, __be32
*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
, __be32
*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
, __be32
*p
,
332 struct nfsd3_readargs
*args
)
336 u32 max_blocksize
= svc_max_payload(rqstp
);
338 if (!(p
= decode_fh(p
, &args
->fh
)))
340 p
= xdr_decode_hyper(p
, &args
->offset
);
342 len
= args
->count
= ntohl(*p
++);
344 if (len
> max_blocksize
)
347 /* set up the kvec */
350 pn
= rqstp
->rq_resused
++;
351 rqstp
->rq_vec
[v
].iov_base
= page_address(rqstp
->rq_respages
[pn
]);
352 rqstp
->rq_vec
[v
].iov_len
= len
< PAGE_SIZE
? len
: PAGE_SIZE
;
353 len
-= rqstp
->rq_vec
[v
].iov_len
;
357 return xdr_argsize_check(rqstp
, p
);
361 nfs3svc_decode_writeargs(struct svc_rqst
*rqstp
, __be32
*p
,
362 struct nfsd3_writeargs
*args
)
364 unsigned int len
, v
, hdr
, dlen
;
365 u32 max_blocksize
= svc_max_payload(rqstp
);
367 if (!(p
= decode_fh(p
, &args
->fh
)))
369 p
= xdr_decode_hyper(p
, &args
->offset
);
371 args
->count
= ntohl(*p
++);
372 args
->stable
= ntohl(*p
++);
373 len
= args
->len
= ntohl(*p
++);
375 * The count must equal the amount of data passed.
377 if (args
->count
!= args
->len
)
381 * Check to make sure that we got the right number of
384 hdr
= (void*)p
- rqstp
->rq_arg
.head
[0].iov_base
;
385 dlen
= rqstp
->rq_arg
.head
[0].iov_len
+ rqstp
->rq_arg
.page_len
388 * Round the length of the data which was specified up to
389 * the next multiple of XDR units and then compare that
390 * against the length which was actually received.
392 if (dlen
!= XDR_QUADLEN(len
)*4)
395 if (args
->count
> max_blocksize
) {
396 args
->count
= max_blocksize
;
397 len
= args
->len
= max_blocksize
;
399 rqstp
->rq_vec
[0].iov_base
= (void*)p
;
400 rqstp
->rq_vec
[0].iov_len
= rqstp
->rq_arg
.head
[0].iov_len
- hdr
;
402 while (len
> rqstp
->rq_vec
[v
].iov_len
) {
403 len
-= rqstp
->rq_vec
[v
].iov_len
;
405 rqstp
->rq_vec
[v
].iov_base
= page_address(rqstp
->rq_pages
[v
]);
406 rqstp
->rq_vec
[v
].iov_len
= PAGE_SIZE
;
408 rqstp
->rq_vec
[v
].iov_len
= len
;
414 nfs3svc_decode_createargs(struct svc_rqst
*rqstp
, __be32
*p
,
415 struct nfsd3_createargs
*args
)
417 if (!(p
= decode_fh(p
, &args
->fh
))
418 || !(p
= decode_filename(p
, &args
->name
, &args
->len
)))
421 switch (args
->createmode
= ntohl(*p
++)) {
422 case NFS3_CREATE_UNCHECKED
:
423 case NFS3_CREATE_GUARDED
:
424 p
= decode_sattr3(p
, &args
->attrs
);
426 case NFS3_CREATE_EXCLUSIVE
:
434 return xdr_argsize_check(rqstp
, p
);
437 nfs3svc_decode_mkdirargs(struct svc_rqst
*rqstp
, __be32
*p
,
438 struct nfsd3_createargs
*args
)
440 if (!(p
= decode_fh(p
, &args
->fh
)) ||
441 !(p
= decode_filename(p
, &args
->name
, &args
->len
)))
443 p
= decode_sattr3(p
, &args
->attrs
);
445 return xdr_argsize_check(rqstp
, p
);
449 nfs3svc_decode_symlinkargs(struct svc_rqst
*rqstp
, __be32
*p
,
450 struct nfsd3_symlinkargs
*args
)
452 unsigned int len
, avail
;
456 if (!(p
= decode_fh(p
, &args
->ffh
)) ||
457 !(p
= decode_filename(p
, &args
->fname
, &args
->flen
))
460 p
= decode_sattr3(p
, &args
->attrs
);
462 /* now decode the pathname, which might be larger than the first page.
463 * As we have to check for nul's anyway, we copy it into a new page
464 * This page appears in the rq_res.pages list, but as pages_len is always
465 * 0, it won't get in the way
468 if (len
== 0 || len
> NFS3_MAXPATHLEN
|| len
>= PAGE_SIZE
)
471 page_address(rqstp
->rq_respages
[rqstp
->rq_resused
++]);
473 /* first copy and check from the first page */
475 vec
= &rqstp
->rq_arg
.head
[0];
476 avail
= vec
->iov_len
- (old
- (char*)vec
->iov_base
);
477 while (len
&& avail
&& *old
) {
482 /* now copy next page if there is one */
483 if (len
&& !avail
&& rqstp
->rq_arg
.page_len
) {
484 avail
= rqstp
->rq_arg
.page_len
;
485 if (avail
> PAGE_SIZE
)
487 old
= page_address(rqstp
->rq_arg
.pages
[0]);
489 while (len
&& avail
&& *old
) {
502 nfs3svc_decode_mknodargs(struct svc_rqst
*rqstp
, __be32
*p
,
503 struct nfsd3_mknodargs
*args
)
505 if (!(p
= decode_fh(p
, &args
->fh
))
506 || !(p
= decode_filename(p
, &args
->name
, &args
->len
)))
509 args
->ftype
= ntohl(*p
++);
511 if (args
->ftype
== NF3BLK
|| args
->ftype
== NF3CHR
512 || args
->ftype
== NF3SOCK
|| args
->ftype
== NF3FIFO
)
513 p
= decode_sattr3(p
, &args
->attrs
);
515 if (args
->ftype
== NF3BLK
|| args
->ftype
== NF3CHR
) {
516 args
->major
= ntohl(*p
++);
517 args
->minor
= ntohl(*p
++);
520 return xdr_argsize_check(rqstp
, p
);
524 nfs3svc_decode_renameargs(struct svc_rqst
*rqstp
, __be32
*p
,
525 struct nfsd3_renameargs
*args
)
527 if (!(p
= decode_fh(p
, &args
->ffh
))
528 || !(p
= decode_filename(p
, &args
->fname
, &args
->flen
))
529 || !(p
= decode_fh(p
, &args
->tfh
))
530 || !(p
= decode_filename(p
, &args
->tname
, &args
->tlen
)))
533 return xdr_argsize_check(rqstp
, p
);
537 nfs3svc_decode_readlinkargs(struct svc_rqst
*rqstp
, __be32
*p
,
538 struct nfsd3_readlinkargs
*args
)
540 if (!(p
= decode_fh(p
, &args
->fh
)))
543 page_address(rqstp
->rq_respages
[rqstp
->rq_resused
++]);
545 return xdr_argsize_check(rqstp
, p
);
549 nfs3svc_decode_linkargs(struct svc_rqst
*rqstp
, __be32
*p
,
550 struct nfsd3_linkargs
*args
)
552 if (!(p
= decode_fh(p
, &args
->ffh
))
553 || !(p
= decode_fh(p
, &args
->tfh
))
554 || !(p
= decode_filename(p
, &args
->tname
, &args
->tlen
)))
557 return xdr_argsize_check(rqstp
, p
);
561 nfs3svc_decode_readdirargs(struct svc_rqst
*rqstp
, __be32
*p
,
562 struct nfsd3_readdirargs
*args
)
564 if (!(p
= decode_fh(p
, &args
->fh
)))
566 p
= xdr_decode_hyper(p
, &args
->cookie
);
567 args
->verf
= p
; p
+= 2;
569 args
->count
= ntohl(*p
++);
571 if (args
->count
> PAGE_SIZE
)
572 args
->count
= PAGE_SIZE
;
575 page_address(rqstp
->rq_respages
[rqstp
->rq_resused
++]);
577 return xdr_argsize_check(rqstp
, p
);
581 nfs3svc_decode_readdirplusargs(struct svc_rqst
*rqstp
, __be32
*p
,
582 struct nfsd3_readdirargs
*args
)
585 u32 max_blocksize
= svc_max_payload(rqstp
);
587 if (!(p
= decode_fh(p
, &args
->fh
)))
589 p
= xdr_decode_hyper(p
, &args
->cookie
);
590 args
->verf
= p
; p
+= 2;
591 args
->dircount
= ntohl(*p
++);
592 args
->count
= ntohl(*p
++);
594 len
= (args
->count
> max_blocksize
) ? max_blocksize
:
599 pn
= rqstp
->rq_resused
++;
601 args
->buffer
= page_address(rqstp
->rq_respages
[pn
]);
605 return xdr_argsize_check(rqstp
, p
);
609 nfs3svc_decode_commitargs(struct svc_rqst
*rqstp
, __be32
*p
,
610 struct nfsd3_commitargs
*args
)
612 if (!(p
= decode_fh(p
, &args
->fh
)))
614 p
= xdr_decode_hyper(p
, &args
->offset
);
615 args
->count
= ntohl(*p
++);
617 return xdr_argsize_check(rqstp
, p
);
621 * XDR encode functions
624 * There must be an encoding function for void results so svc_process
625 * will work properly.
628 nfs3svc_encode_voidres(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
630 return xdr_ressize_check(rqstp
, p
);
635 nfs3svc_encode_attrstat(struct svc_rqst
*rqstp
, __be32
*p
,
636 struct nfsd3_attrstat
*resp
)
638 if (resp
->status
== 0) {
639 lease_get_mtime(resp
->fh
.fh_dentry
->d_inode
,
641 p
= encode_fattr3(rqstp
, p
, &resp
->fh
, &resp
->stat
);
643 return xdr_ressize_check(rqstp
, p
);
646 /* SETATTR, REMOVE, RMDIR */
648 nfs3svc_encode_wccstat(struct svc_rqst
*rqstp
, __be32
*p
,
649 struct nfsd3_attrstat
*resp
)
651 p
= encode_wcc_data(rqstp
, p
, &resp
->fh
);
652 return xdr_ressize_check(rqstp
, p
);
657 nfs3svc_encode_diropres(struct svc_rqst
*rqstp
, __be32
*p
,
658 struct nfsd3_diropres
*resp
)
660 if (resp
->status
== 0) {
661 p
= encode_fh(p
, &resp
->fh
);
662 p
= encode_post_op_attr(rqstp
, p
, &resp
->fh
);
664 p
= encode_post_op_attr(rqstp
, p
, &resp
->dirfh
);
665 return xdr_ressize_check(rqstp
, p
);
670 nfs3svc_encode_accessres(struct svc_rqst
*rqstp
, __be32
*p
,
671 struct nfsd3_accessres
*resp
)
673 p
= encode_post_op_attr(rqstp
, p
, &resp
->fh
);
674 if (resp
->status
== 0)
675 *p
++ = htonl(resp
->access
);
676 return xdr_ressize_check(rqstp
, p
);
681 nfs3svc_encode_readlinkres(struct svc_rqst
*rqstp
, __be32
*p
,
682 struct nfsd3_readlinkres
*resp
)
684 p
= encode_post_op_attr(rqstp
, p
, &resp
->fh
);
685 if (resp
->status
== 0) {
686 *p
++ = htonl(resp
->len
);
687 xdr_ressize_check(rqstp
, p
);
688 rqstp
->rq_res
.page_len
= resp
->len
;
690 /* need to pad the tail */
691 rqstp
->rq_res
.tail
[0].iov_base
= p
;
693 rqstp
->rq_res
.tail
[0].iov_len
= 4 - (resp
->len
&3);
697 return xdr_ressize_check(rqstp
, p
);
702 nfs3svc_encode_readres(struct svc_rqst
*rqstp
, __be32
*p
,
703 struct nfsd3_readres
*resp
)
705 p
= encode_post_op_attr(rqstp
, p
, &resp
->fh
);
706 if (resp
->status
== 0) {
707 *p
++ = htonl(resp
->count
);
708 *p
++ = htonl(resp
->eof
);
709 *p
++ = htonl(resp
->count
); /* xdr opaque count */
710 xdr_ressize_check(rqstp
, p
);
711 /* now update rqstp->rq_res to reflect data aswell */
712 rqstp
->rq_res
.page_len
= resp
->count
;
713 if (resp
->count
& 3) {
714 /* need to pad the tail */
715 rqstp
->rq_res
.tail
[0].iov_base
= p
;
717 rqstp
->rq_res
.tail
[0].iov_len
= 4 - (resp
->count
& 3);
721 return xdr_ressize_check(rqstp
, p
);
726 nfs3svc_encode_writeres(struct svc_rqst
*rqstp
, __be32
*p
,
727 struct nfsd3_writeres
*resp
)
729 p
= encode_wcc_data(rqstp
, p
, &resp
->fh
);
730 if (resp
->status
== 0) {
731 *p
++ = htonl(resp
->count
);
732 *p
++ = htonl(resp
->committed
);
733 *p
++ = htonl(nfssvc_boot
.tv_sec
);
734 *p
++ = htonl(nfssvc_boot
.tv_usec
);
736 return xdr_ressize_check(rqstp
, p
);
739 /* CREATE, MKDIR, SYMLINK, MKNOD */
741 nfs3svc_encode_createres(struct svc_rqst
*rqstp
, __be32
*p
,
742 struct nfsd3_diropres
*resp
)
744 if (resp
->status
== 0) {
746 p
= encode_fh(p
, &resp
->fh
);
747 p
= encode_post_op_attr(rqstp
, p
, &resp
->fh
);
749 p
= encode_wcc_data(rqstp
, p
, &resp
->dirfh
);
750 return xdr_ressize_check(rqstp
, p
);
755 nfs3svc_encode_renameres(struct svc_rqst
*rqstp
, __be32
*p
,
756 struct nfsd3_renameres
*resp
)
758 p
= encode_wcc_data(rqstp
, p
, &resp
->ffh
);
759 p
= encode_wcc_data(rqstp
, p
, &resp
->tfh
);
760 return xdr_ressize_check(rqstp
, p
);
765 nfs3svc_encode_linkres(struct svc_rqst
*rqstp
, __be32
*p
,
766 struct nfsd3_linkres
*resp
)
768 p
= encode_post_op_attr(rqstp
, p
, &resp
->fh
);
769 p
= encode_wcc_data(rqstp
, p
, &resp
->tfh
);
770 return xdr_ressize_check(rqstp
, p
);
775 nfs3svc_encode_readdirres(struct svc_rqst
*rqstp
, __be32
*p
,
776 struct nfsd3_readdirres
*resp
)
778 p
= encode_post_op_attr(rqstp
, p
, &resp
->fh
);
780 if (resp
->status
== 0) {
781 /* stupid readdir cookie */
782 memcpy(p
, resp
->verf
, 8); p
+= 2;
783 xdr_ressize_check(rqstp
, p
);
784 if (rqstp
->rq_res
.head
[0].iov_len
+ (2<<2) > PAGE_SIZE
)
785 return 1; /*No room for trailer */
786 rqstp
->rq_res
.page_len
= (resp
->count
) << 2;
788 /* add the 'tail' to the end of the 'head' page - page 0. */
789 rqstp
->rq_res
.tail
[0].iov_base
= p
;
790 *p
++ = 0; /* no more entries */
791 *p
++ = htonl(resp
->common
.err
== nfserr_eof
);
792 rqstp
->rq_res
.tail
[0].iov_len
= 2<<2;
795 return xdr_ressize_check(rqstp
, p
);
799 encode_entry_baggage(struct nfsd3_readdirres
*cd
, __be32
*p
, const char *name
,
802 *p
++ = xdr_one
; /* mark entry present */
803 p
= xdr_encode_hyper(p
, ino
); /* file id */
804 p
= xdr_encode_array(p
, name
, namlen
);/* name length & name */
806 cd
->offset
= p
; /* remember pointer */
807 p
= xdr_encode_hyper(p
, NFS_OFFSET_MAX
);/* offset of next entry */
813 encode_entryplus_baggage(struct nfsd3_readdirres
*cd
, __be32
*p
,
816 p
= encode_post_op_attr(cd
->rqstp
, p
, fhp
);
817 *p
++ = xdr_one
; /* yes, a file handle follows */
818 p
= encode_fh(p
, fhp
);
824 compose_entry_fh(struct nfsd3_readdirres
*cd
, struct svc_fh
*fhp
,
825 const char *name
, int namlen
)
827 struct svc_export
*exp
;
828 struct dentry
*dparent
, *dchild
;
831 dparent
= cd
->fh
.fh_dentry
;
832 exp
= cd
->fh
.fh_export
;
834 fh_init(fhp
, NFS3_FHSIZE
);
835 if (isdotent(name
, namlen
)) {
837 dchild
= dget_parent(dparent
);
838 if (dchild
== dparent
) {
839 /* filesystem root - cannot return filehandle for ".." */
844 dchild
= dget(dparent
);
846 dchild
= lookup_one_len(name
, dparent
, namlen
);
849 if (d_mountpoint(dchild
) ||
850 fh_compose(fhp
, exp
, dchild
, &cd
->fh
) != 0 ||
858 * Encode a directory entry. This one works for both normal readdir
860 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
861 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
863 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
867 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
868 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
870 encode_entry(struct readdir_cd
*ccd
, const char *name
, int namlen
,
871 loff_t offset
, u64 ino
, unsigned int d_type
, int plus
)
873 struct nfsd3_readdirres
*cd
= container_of(ccd
, struct nfsd3_readdirres
,
875 __be32
*p
= cd
->buffer
;
876 caddr_t curr_page_addr
= NULL
;
877 int pn
; /* current page number */
878 int slen
; /* string (name) length */
879 int elen
; /* estimated entry length in words */
880 int num_entry_words
= 0; /* actual number of words */
883 u64 offset64
= offset
;
885 if (unlikely(cd
->offset1
)) {
886 /* we ended up with offset on a page boundary */
887 *cd
->offset
= htonl(offset64
>> 32);
888 *cd
->offset1
= htonl(offset64
& 0xffffffff);
891 xdr_encode_hyper(cd
->offset
, offset64
);
896 dprintk("encode_entry(%.*s @%ld%s)\n",
897 namlen, name, (long) offset, plus? " plus" : "");
900 /* truncate filename if too long */
901 if (namlen
> NFS3_MAXNAMLEN
)
902 namlen
= NFS3_MAXNAMLEN
;
904 slen
= XDR_QUADLEN(namlen
);
905 elen
= slen
+ NFS3_ENTRY_BAGGAGE
906 + (plus
? NFS3_ENTRYPLUS_BAGGAGE
: 0);
908 if (cd
->buflen
< elen
) {
909 cd
->common
.err
= nfserr_toosmall
;
913 /* determine which page in rq_respages[] we are currently filling */
914 for (pn
=1; pn
< cd
->rqstp
->rq_resused
; pn
++) {
915 curr_page_addr
= page_address(cd
->rqstp
->rq_respages
[pn
]);
917 if (((caddr_t
)cd
->buffer
>= curr_page_addr
) &&
918 ((caddr_t
)cd
->buffer
< curr_page_addr
+ PAGE_SIZE
))
922 if ((caddr_t
)(cd
->buffer
+ elen
) < (curr_page_addr
+ PAGE_SIZE
)) {
923 /* encode entry in current page */
925 p
= encode_entry_baggage(cd
, p
, name
, namlen
, ino
);
927 /* throw in readdirplus baggage */
931 if (compose_entry_fh(cd
, &fh
, name
, namlen
) > 0) {
935 p
= encode_entryplus_baggage(cd
, p
, &fh
);
937 num_entry_words
= p
- cd
->buffer
;
938 } else if (cd
->rqstp
->rq_respages
[pn
+1] != NULL
) {
939 /* temporarily encode entry into next page, then move back to
940 * current and next page in rq_respages[] */
944 /* grab next page for temporary storage of entry */
945 p1
= tmp
= page_address(cd
->rqstp
->rq_respages
[pn
+1]);
947 p1
= encode_entry_baggage(cd
, p1
, name
, namlen
, ino
);
949 /* throw in readdirplus baggage */
953 if (compose_entry_fh(cd
, &fh
, name
, namlen
) > 0) {
954 /* zero out the filehandle */
958 p1
= encode_entryplus_baggage(cd
, p1
, &fh
);
961 /* determine entry word length and lengths to go in pages */
962 num_entry_words
= p1
- tmp
;
963 len1
= curr_page_addr
+ PAGE_SIZE
- (caddr_t
)cd
->buffer
;
964 if ((num_entry_words
<< 2) < len1
) {
965 /* the actual number of words in the entry is less
966 * than elen and can still fit in the current page
968 memmove(p
, tmp
, num_entry_words
<< 2);
969 p
+= num_entry_words
;
972 cd
->offset
= cd
->buffer
+ (cd
->offset
- tmp
);
974 unsigned int offset_r
= (cd
->offset
- tmp
) << 2;
976 /* update pointer to offset location.
977 * This is a 64bit quantity, so we need to
979 * - entirely in first page
980 * - entirely in second page
981 * - 4 bytes in each page
983 if (offset_r
+ 8 <= len1
) {
984 cd
->offset
= p
+ (cd
->offset
- tmp
);
985 } else if (offset_r
>= len1
) {
986 cd
->offset
-= len1
>> 2;
988 /* sitting on the fence */
989 BUG_ON(offset_r
!= len1
- 4);
990 cd
->offset
= p
+ (cd
->offset
- tmp
);
994 len2
= (num_entry_words
<< 2) - len1
;
996 /* move from temp page to current and next pages */
997 memmove(p
, tmp
, len1
);
998 memmove(tmp
, (caddr_t
)tmp
+len1
, len2
);
1000 p
= tmp
+ (len2
>> 2);
1004 cd
->common
.err
= nfserr_toosmall
;
1008 cd
->buflen
-= num_entry_words
;
1010 cd
->common
.err
= nfs_ok
;
1016 nfs3svc_encode_entry(void *cd
, const char *name
,
1017 int namlen
, loff_t offset
, u64 ino
, unsigned int d_type
)
1019 return encode_entry(cd
, name
, namlen
, offset
, ino
, d_type
, 0);
1023 nfs3svc_encode_entry_plus(void *cd
, const char *name
,
1024 int namlen
, loff_t offset
, u64 ino
,
1025 unsigned int d_type
)
1027 return encode_entry(cd
, name
, namlen
, offset
, ino
, d_type
, 1);
1032 nfs3svc_encode_fsstatres(struct svc_rqst
*rqstp
, __be32
*p
,
1033 struct nfsd3_fsstatres
*resp
)
1035 struct kstatfs
*s
= &resp
->stats
;
1036 u64 bs
= s
->f_bsize
;
1038 *p
++ = xdr_zero
; /* no post_op_attr */
1040 if (resp
->status
== 0) {
1041 p
= xdr_encode_hyper(p
, bs
* s
->f_blocks
); /* total bytes */
1042 p
= xdr_encode_hyper(p
, bs
* s
->f_bfree
); /* free bytes */
1043 p
= xdr_encode_hyper(p
, bs
* s
->f_bavail
); /* user available bytes */
1044 p
= xdr_encode_hyper(p
, s
->f_files
); /* total inodes */
1045 p
= xdr_encode_hyper(p
, s
->f_ffree
); /* free inodes */
1046 p
= xdr_encode_hyper(p
, s
->f_ffree
); /* user available inodes */
1047 *p
++ = htonl(resp
->invarsec
); /* mean unchanged time */
1049 return xdr_ressize_check(rqstp
, p
);
1054 nfs3svc_encode_fsinfores(struct svc_rqst
*rqstp
, __be32
*p
,
1055 struct nfsd3_fsinfores
*resp
)
1057 *p
++ = xdr_zero
; /* no post_op_attr */
1059 if (resp
->status
== 0) {
1060 *p
++ = htonl(resp
->f_rtmax
);
1061 *p
++ = htonl(resp
->f_rtpref
);
1062 *p
++ = htonl(resp
->f_rtmult
);
1063 *p
++ = htonl(resp
->f_wtmax
);
1064 *p
++ = htonl(resp
->f_wtpref
);
1065 *p
++ = htonl(resp
->f_wtmult
);
1066 *p
++ = htonl(resp
->f_dtpref
);
1067 p
= xdr_encode_hyper(p
, resp
->f_maxfilesize
);
1070 *p
++ = htonl(resp
->f_properties
);
1073 return xdr_ressize_check(rqstp
, p
);
1078 nfs3svc_encode_pathconfres(struct svc_rqst
*rqstp
, __be32
*p
,
1079 struct nfsd3_pathconfres
*resp
)
1081 *p
++ = xdr_zero
; /* no post_op_attr */
1083 if (resp
->status
== 0) {
1084 *p
++ = htonl(resp
->p_link_max
);
1085 *p
++ = htonl(resp
->p_name_max
);
1086 *p
++ = htonl(resp
->p_no_trunc
);
1087 *p
++ = htonl(resp
->p_chown_restricted
);
1088 *p
++ = htonl(resp
->p_case_insensitive
);
1089 *p
++ = htonl(resp
->p_case_preserving
);
1092 return xdr_ressize_check(rqstp
, p
);
1097 nfs3svc_encode_commitres(struct svc_rqst
*rqstp
, __be32
*p
,
1098 struct nfsd3_commitres
*resp
)
1100 p
= encode_wcc_data(rqstp
, p
, &resp
->fh
);
1101 /* Write verifier */
1102 if (resp
->status
== 0) {
1103 *p
++ = htonl(nfssvc_boot
.tv_sec
);
1104 *p
++ = htonl(nfssvc_boot
.tv_usec
);
1106 return xdr_ressize_check(rqstp
, p
);
1110 * XDR release functions
1113 nfs3svc_release_fhandle(struct svc_rqst
*rqstp
, __be32
*p
,
1114 struct nfsd3_attrstat
*resp
)
1121 nfs3svc_release_fhandle2(struct svc_rqst
*rqstp
, __be32
*p
,
1122 struct nfsd3_fhandle_pair
*resp
)