2 * linux/fs/nfs/nfs3xdr.c
4 * XDR functions to encode/decode NFSv3 RPC arguments and results.
6 * Copyright (C) 1996, 1997 Olaf Kirch
9 #include <linux/param.h>
10 #include <linux/time.h>
12 #include <linux/slab.h>
13 #include <linux/utsname.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
17 #include <linux/pagemap.h>
18 #include <linux/proc_fs.h>
19 #include <linux/kdev_t.h>
20 #include <linux/sunrpc/clnt.h>
21 #include <linux/nfs.h>
22 #include <linux/nfs3.h>
23 #include <linux/nfs_fs.h>
24 #include <linux/nfsacl.h>
27 #define NFSDBG_FACILITY NFSDBG_XDR
29 /* Mapping from NFS error code to "errno" error code. */
30 #define errno_NFSERR_IO EIO
33 * Declare the space requirements for NFS arguments and replies as
34 * number of 32bit-words
36 #define NFS3_fhandle_sz (1+16)
37 #define NFS3_fh_sz (NFS3_fhandle_sz) /* shorthand */
38 #define NFS3_sattr_sz (15)
39 #define NFS3_filename_sz (1+(NFS3_MAXNAMLEN>>2))
40 #define NFS3_path_sz (1+(NFS3_MAXPATHLEN>>2))
41 #define NFS3_fattr_sz (21)
42 #define NFS3_wcc_attr_sz (6)
43 #define NFS3_pre_op_attr_sz (1+NFS3_wcc_attr_sz)
44 #define NFS3_post_op_attr_sz (1+NFS3_fattr_sz)
45 #define NFS3_wcc_data_sz (NFS3_pre_op_attr_sz+NFS3_post_op_attr_sz)
46 #define NFS3_fsstat_sz
47 #define NFS3_fsinfo_sz
48 #define NFS3_pathconf_sz
49 #define NFS3_entry_sz (NFS3_filename_sz+3)
51 #define NFS3_sattrargs_sz (NFS3_fh_sz+NFS3_sattr_sz+3)
52 #define NFS3_diropargs_sz (NFS3_fh_sz+NFS3_filename_sz)
53 #define NFS3_removeargs_sz (NFS3_fh_sz+NFS3_filename_sz)
54 #define NFS3_accessargs_sz (NFS3_fh_sz+1)
55 #define NFS3_readlinkargs_sz (NFS3_fh_sz)
56 #define NFS3_readargs_sz (NFS3_fh_sz+3)
57 #define NFS3_writeargs_sz (NFS3_fh_sz+5)
58 #define NFS3_createargs_sz (NFS3_diropargs_sz+NFS3_sattr_sz)
59 #define NFS3_mkdirargs_sz (NFS3_diropargs_sz+NFS3_sattr_sz)
60 #define NFS3_symlinkargs_sz (NFS3_diropargs_sz+1+NFS3_sattr_sz)
61 #define NFS3_mknodargs_sz (NFS3_diropargs_sz+2+NFS3_sattr_sz)
62 #define NFS3_renameargs_sz (NFS3_diropargs_sz+NFS3_diropargs_sz)
63 #define NFS3_linkargs_sz (NFS3_fh_sz+NFS3_diropargs_sz)
64 #define NFS3_readdirargs_sz (NFS3_fh_sz+2)
65 #define NFS3_commitargs_sz (NFS3_fh_sz+3)
67 #define NFS3_attrstat_sz (1+NFS3_fattr_sz)
68 #define NFS3_wccstat_sz (1+NFS3_wcc_data_sz)
69 #define NFS3_removeres_sz (NFS3_wccstat_sz)
70 #define NFS3_lookupres_sz (1+NFS3_fh_sz+(2 * NFS3_post_op_attr_sz))
71 #define NFS3_accessres_sz (1+NFS3_post_op_attr_sz+1)
72 #define NFS3_readlinkres_sz (1+NFS3_post_op_attr_sz+1)
73 #define NFS3_readres_sz (1+NFS3_post_op_attr_sz+3)
74 #define NFS3_writeres_sz (1+NFS3_wcc_data_sz+4)
75 #define NFS3_createres_sz (1+NFS3_fh_sz+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
76 #define NFS3_renameres_sz (1+(2 * NFS3_wcc_data_sz))
77 #define NFS3_linkres_sz (1+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
78 #define NFS3_readdirres_sz (1+NFS3_post_op_attr_sz+2)
79 #define NFS3_fsstatres_sz (1+NFS3_post_op_attr_sz+13)
80 #define NFS3_fsinfores_sz (1+NFS3_post_op_attr_sz+12)
81 #define NFS3_pathconfres_sz (1+NFS3_post_op_attr_sz+6)
82 #define NFS3_commitres_sz (1+NFS3_wcc_data_sz+2)
84 #define ACL3_getaclargs_sz (NFS3_fh_sz+1)
85 #define ACL3_setaclargs_sz (NFS3_fh_sz+1+ \
86 XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
87 #define ACL3_getaclres_sz (1+NFS3_post_op_attr_sz+1+ \
88 XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
89 #define ACL3_setaclres_sz (1+NFS3_post_op_attr_sz)
92 * Map file type to S_IFMT bits
96 unsigned int nfs2type
;
104 { S_IFSOCK
, NFSOCK
},
110 * Common NFS XDR functions as inlines
112 static inline __be32
*
113 xdr_encode_fhandle(__be32
*p
, const struct nfs_fh
*fh
)
115 return xdr_encode_array(p
, fh
->data
, fh
->size
);
118 static inline __be32
*
119 xdr_decode_fhandle(__be32
*p
, struct nfs_fh
*fh
)
121 if ((fh
->size
= ntohl(*p
++)) <= NFS3_FHSIZE
) {
122 memcpy(fh
->data
, p
, fh
->size
);
123 return p
+ XDR_QUADLEN(fh
->size
);
129 * Encode/decode time.
131 static inline __be32
*
132 xdr_encode_time3(__be32
*p
, struct timespec
*timep
)
134 *p
++ = htonl(timep
->tv_sec
);
135 *p
++ = htonl(timep
->tv_nsec
);
139 static inline __be32
*
140 xdr_decode_time3(__be32
*p
, struct timespec
*timep
)
142 timep
->tv_sec
= ntohl(*p
++);
143 timep
->tv_nsec
= ntohl(*p
++);
148 xdr_decode_fattr(__be32
*p
, struct nfs_fattr
*fattr
)
150 unsigned int type
, major
, minor
;
156 fmode
= nfs_type2fmt
[type
].mode
;
157 fattr
->type
= nfs_type2fmt
[type
].nfs2type
;
158 fattr
->mode
= (ntohl(*p
++) & ~S_IFMT
) | fmode
;
159 fattr
->nlink
= ntohl(*p
++);
160 fattr
->uid
= ntohl(*p
++);
161 fattr
->gid
= ntohl(*p
++);
162 p
= xdr_decode_hyper(p
, &fattr
->size
);
163 p
= xdr_decode_hyper(p
, &fattr
->du
.nfs3
.used
);
165 /* Turn remote device info into Linux-specific dev_t */
168 fattr
->rdev
= MKDEV(major
, minor
);
169 if (MAJOR(fattr
->rdev
) != major
|| MINOR(fattr
->rdev
) != minor
)
172 p
= xdr_decode_hyper(p
, &fattr
->fsid
.major
);
173 fattr
->fsid
.minor
= 0;
174 p
= xdr_decode_hyper(p
, &fattr
->fileid
);
175 p
= xdr_decode_time3(p
, &fattr
->atime
);
176 p
= xdr_decode_time3(p
, &fattr
->mtime
);
177 p
= xdr_decode_time3(p
, &fattr
->ctime
);
179 /* Update the mode bits */
180 fattr
->valid
|= (NFS_ATTR_FATTR
| NFS_ATTR_FATTR_V3
);
184 static inline __be32
*
185 xdr_encode_sattr(__be32
*p
, struct iattr
*attr
)
187 if (attr
->ia_valid
& ATTR_MODE
) {
189 *p
++ = htonl(attr
->ia_mode
& S_IALLUGO
);
193 if (attr
->ia_valid
& ATTR_UID
) {
195 *p
++ = htonl(attr
->ia_uid
);
199 if (attr
->ia_valid
& ATTR_GID
) {
201 *p
++ = htonl(attr
->ia_gid
);
205 if (attr
->ia_valid
& ATTR_SIZE
) {
207 p
= xdr_encode_hyper(p
, (__u64
) attr
->ia_size
);
211 if (attr
->ia_valid
& ATTR_ATIME_SET
) {
213 p
= xdr_encode_time3(p
, &attr
->ia_atime
);
214 } else if (attr
->ia_valid
& ATTR_ATIME
) {
219 if (attr
->ia_valid
& ATTR_MTIME_SET
) {
221 p
= xdr_encode_time3(p
, &attr
->ia_mtime
);
222 } else if (attr
->ia_valid
& ATTR_MTIME
) {
230 static inline __be32
*
231 xdr_decode_wcc_attr(__be32
*p
, struct nfs_fattr
*fattr
)
233 p
= xdr_decode_hyper(p
, &fattr
->pre_size
);
234 p
= xdr_decode_time3(p
, &fattr
->pre_mtime
);
235 p
= xdr_decode_time3(p
, &fattr
->pre_ctime
);
236 fattr
->valid
|= NFS_ATTR_WCC
;
240 static inline __be32
*
241 xdr_decode_post_op_attr(__be32
*p
, struct nfs_fattr
*fattr
)
244 p
= xdr_decode_fattr(p
, fattr
);
248 static inline __be32
*
249 xdr_decode_pre_op_attr(__be32
*p
, struct nfs_fattr
*fattr
)
252 return xdr_decode_wcc_attr(p
, fattr
);
257 static inline __be32
*
258 xdr_decode_wcc_data(__be32
*p
, struct nfs_fattr
*fattr
)
260 p
= xdr_decode_pre_op_attr(p
, fattr
);
261 return xdr_decode_post_op_attr(p
, fattr
);
265 * NFS encode functions
269 * Encode file handle argument
272 nfs3_xdr_fhandle(struct rpc_rqst
*req
, __be32
*p
, struct nfs_fh
*fh
)
274 p
= xdr_encode_fhandle(p
, fh
);
275 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
280 * Encode SETATTR arguments
283 nfs3_xdr_sattrargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_sattrargs
*args
)
285 p
= xdr_encode_fhandle(p
, args
->fh
);
286 p
= xdr_encode_sattr(p
, args
->sattr
);
287 *p
++ = htonl(args
->guard
);
289 p
= xdr_encode_time3(p
, &args
->guardtime
);
290 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
295 * Encode directory ops argument
298 nfs3_xdr_diropargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_diropargs
*args
)
300 p
= xdr_encode_fhandle(p
, args
->fh
);
301 p
= xdr_encode_array(p
, args
->name
, args
->len
);
302 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
307 * Encode REMOVE argument
310 nfs3_xdr_removeargs(struct rpc_rqst
*req
, __be32
*p
, const struct nfs_removeargs
*args
)
312 p
= xdr_encode_fhandle(p
, args
->fh
);
313 p
= xdr_encode_array(p
, args
->name
.name
, args
->name
.len
);
314 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
319 * Encode access() argument
322 nfs3_xdr_accessargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_accessargs
*args
)
324 p
= xdr_encode_fhandle(p
, args
->fh
);
325 *p
++ = htonl(args
->access
);
326 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
331 * Arguments to a READ call. Since we read data directly into the page
332 * cache, we also set up the reply iovec here so that iov[1] points
333 * exactly to the page we want to fetch.
336 nfs3_xdr_readargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs_readargs
*args
)
338 struct rpc_auth
*auth
= req
->rq_task
->tk_msg
.rpc_cred
->cr_auth
;
340 u32 count
= args
->count
;
342 p
= xdr_encode_fhandle(p
, args
->fh
);
343 p
= xdr_encode_hyper(p
, args
->offset
);
345 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
347 /* Inline the page array */
348 replen
= (RPC_REPHDRSIZE
+ auth
->au_rslack
+ NFS3_readres_sz
) << 2;
349 xdr_inline_pages(&req
->rq_rcv_buf
, replen
,
350 args
->pages
, args
->pgbase
, count
);
351 req
->rq_rcv_buf
.flags
|= XDRBUF_READ
;
356 * Write arguments. Splice the buffer to be written into the iovec.
359 nfs3_xdr_writeargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs_writeargs
*args
)
361 struct xdr_buf
*sndbuf
= &req
->rq_snd_buf
;
362 u32 count
= args
->count
;
364 p
= xdr_encode_fhandle(p
, args
->fh
);
365 p
= xdr_encode_hyper(p
, args
->offset
);
367 *p
++ = htonl(args
->stable
);
369 sndbuf
->len
= xdr_adjust_iovec(sndbuf
->head
, p
);
371 /* Copy the page array */
372 xdr_encode_pages(sndbuf
, args
->pages
, args
->pgbase
, count
);
373 sndbuf
->flags
|= XDRBUF_WRITE
;
378 * Encode CREATE arguments
381 nfs3_xdr_createargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_createargs
*args
)
383 p
= xdr_encode_fhandle(p
, args
->fh
);
384 p
= xdr_encode_array(p
, args
->name
, args
->len
);
386 *p
++ = htonl(args
->createmode
);
387 if (args
->createmode
== NFS3_CREATE_EXCLUSIVE
) {
388 *p
++ = args
->verifier
[0];
389 *p
++ = args
->verifier
[1];
391 p
= xdr_encode_sattr(p
, args
->sattr
);
393 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
398 * Encode MKDIR arguments
401 nfs3_xdr_mkdirargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_mkdirargs
*args
)
403 p
= xdr_encode_fhandle(p
, args
->fh
);
404 p
= xdr_encode_array(p
, args
->name
, args
->len
);
405 p
= xdr_encode_sattr(p
, args
->sattr
);
406 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
411 * Encode SYMLINK arguments
414 nfs3_xdr_symlinkargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_symlinkargs
*args
)
416 p
= xdr_encode_fhandle(p
, args
->fromfh
);
417 p
= xdr_encode_array(p
, args
->fromname
, args
->fromlen
);
418 p
= xdr_encode_sattr(p
, args
->sattr
);
419 *p
++ = htonl(args
->pathlen
);
420 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
423 xdr_encode_pages(&req
->rq_snd_buf
, args
->pages
, 0, args
->pathlen
);
428 * Encode MKNOD arguments
431 nfs3_xdr_mknodargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_mknodargs
*args
)
433 p
= xdr_encode_fhandle(p
, args
->fh
);
434 p
= xdr_encode_array(p
, args
->name
, args
->len
);
435 *p
++ = htonl(args
->type
);
436 p
= xdr_encode_sattr(p
, args
->sattr
);
437 if (args
->type
== NF3CHR
|| args
->type
== NF3BLK
) {
438 *p
++ = htonl(MAJOR(args
->rdev
));
439 *p
++ = htonl(MINOR(args
->rdev
));
442 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
447 * Encode RENAME arguments
450 nfs3_xdr_renameargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_renameargs
*args
)
452 p
= xdr_encode_fhandle(p
, args
->fromfh
);
453 p
= xdr_encode_array(p
, args
->fromname
, args
->fromlen
);
454 p
= xdr_encode_fhandle(p
, args
->tofh
);
455 p
= xdr_encode_array(p
, args
->toname
, args
->tolen
);
456 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
461 * Encode LINK arguments
464 nfs3_xdr_linkargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_linkargs
*args
)
466 p
= xdr_encode_fhandle(p
, args
->fromfh
);
467 p
= xdr_encode_fhandle(p
, args
->tofh
);
468 p
= xdr_encode_array(p
, args
->toname
, args
->tolen
);
469 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
474 * Encode arguments to readdir call
477 nfs3_xdr_readdirargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_readdirargs
*args
)
479 struct rpc_auth
*auth
= req
->rq_task
->tk_msg
.rpc_cred
->cr_auth
;
481 u32 count
= args
->count
;
483 p
= xdr_encode_fhandle(p
, args
->fh
);
484 p
= xdr_encode_hyper(p
, args
->cookie
);
485 *p
++ = args
->verf
[0];
486 *p
++ = args
->verf
[1];
488 /* readdirplus: need dircount + buffer size.
489 * We just make sure we make dircount big enough */
490 *p
++ = htonl(count
>> 3);
493 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
495 /* Inline the page array */
496 replen
= (RPC_REPHDRSIZE
+ auth
->au_rslack
+ NFS3_readdirres_sz
) << 2;
497 xdr_inline_pages(&req
->rq_rcv_buf
, replen
, args
->pages
, 0, count
);
502 * Decode the result of a readdir call.
503 * We just check for syntactical correctness.
506 nfs3_xdr_readdirres(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_readdirres
*res
)
508 struct xdr_buf
*rcvbuf
= &req
->rq_rcv_buf
;
509 struct kvec
*iov
= rcvbuf
->head
;
512 u32 len
, recvd
, pglen
;
514 __be32
*entry
, *end
, *kaddr
;
516 status
= ntohl(*p
++);
517 /* Decode post_op_attrs */
518 p
= xdr_decode_post_op_attr(p
, res
->dir_attr
);
520 return nfs_stat_to_errno(status
);
521 /* Decode verifier cookie */
529 hdrlen
= (u8
*) p
- (u8
*) iov
->iov_base
;
530 if (iov
->iov_len
< hdrlen
) {
531 dprintk("NFS: READDIR reply header overflowed:"
532 "length %Zu > %Zu\n", hdrlen
, iov
->iov_len
);
533 return -errno_NFSERR_IO
;
534 } else if (iov
->iov_len
!= hdrlen
) {
535 dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
536 xdr_shift_buf(rcvbuf
, iov
->iov_len
- hdrlen
);
539 pglen
= rcvbuf
->page_len
;
540 recvd
= rcvbuf
->len
- hdrlen
;
543 page
= rcvbuf
->pages
;
544 kaddr
= p
= kmap_atomic(*page
, KM_USER0
);
545 end
= (__be32
*)((char *)p
+ pglen
);
548 /* Make sure the packet actually has a value_follows and EOF entry */
549 if ((entry
+ 1) > end
)
555 p
+= 2; /* inode # */
556 len
= ntohl(*p
++); /* string length */
557 p
+= XDR_QUADLEN(len
) + 2; /* name + cookie */
558 if (len
> NFS3_MAXNAMLEN
) {
559 dprintk("NFS: giant filename in readdir (len 0x%x)!\n",
578 if (len
> NFS3_FHSIZE
) {
579 dprintk("NFS: giant filehandle in "
580 "readdir (len 0x%x)!\n", len
);
583 p
+= XDR_QUADLEN(len
);
593 * Apparently some server sends responses that are a valid size, but
594 * contain no entries, and have value_follows==0 and EOF==0. For
595 * those, just set the EOF marker.
597 if (!nr
&& entry
[1] == 0) {
598 dprintk("NFS: readdir reply truncated!\n");
602 kunmap_atomic(kaddr
, KM_USER0
);
606 * When we get a short packet there are 2 possibilities. We can
607 * return an error, or fix up the response to look like a valid
608 * response and return what we have so far. If there are no
609 * entries and the packet was short, then return -EIO. If there
610 * are valid entries in the response, return them and pretend that
611 * the call was successful, but incomplete. The caller can retry the
612 * readdir starting at the last cookie.
614 entry
[0] = entry
[1] = 0;
616 nr
= -errno_NFSERR_IO
;
619 nr
= -errno_NFSERR_IO
;
624 nfs3_decode_dirent(__be32
*p
, struct nfs_entry
*entry
, int plus
)
626 struct nfs_entry old
= *entry
;
630 return ERR_PTR(-EAGAIN
);
632 return ERR_PTR(-EBADCOOKIE
);
635 p
= xdr_decode_hyper(p
, &entry
->ino
);
636 entry
->len
= ntohl(*p
++);
637 entry
->name
= (const char *) p
;
638 p
+= XDR_QUADLEN(entry
->len
);
639 entry
->prev_cookie
= entry
->cookie
;
640 p
= xdr_decode_hyper(p
, &entry
->cookie
);
643 entry
->fattr
->valid
= 0;
644 p
= xdr_decode_post_op_attr(p
, entry
->fattr
);
645 /* In fact, a post_op_fh3: */
647 p
= xdr_decode_fhandle(p
, entry
->fh
);
648 /* Ugh -- server reply was truncated */
650 dprintk("NFS: FH truncated\n");
652 return ERR_PTR(-EAGAIN
);
655 memset((u8
*)(entry
->fh
), 0, sizeof(*entry
->fh
));
658 entry
->eof
= !p
[0] && p
[1];
663 * Encode COMMIT arguments
666 nfs3_xdr_commitargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs_writeargs
*args
)
668 p
= xdr_encode_fhandle(p
, args
->fh
);
669 p
= xdr_encode_hyper(p
, args
->offset
);
670 *p
++ = htonl(args
->count
);
671 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
675 #ifdef CONFIG_NFS_V3_ACL
677 * Encode GETACL arguments
680 nfs3_xdr_getaclargs(struct rpc_rqst
*req
, __be32
*p
,
681 struct nfs3_getaclargs
*args
)
683 struct rpc_auth
*auth
= req
->rq_task
->tk_msg
.rpc_cred
->cr_auth
;
686 p
= xdr_encode_fhandle(p
, args
->fh
);
687 *p
++ = htonl(args
->mask
);
688 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
690 if (args
->mask
& (NFS_ACL
| NFS_DFACL
)) {
691 /* Inline the page array */
692 replen
= (RPC_REPHDRSIZE
+ auth
->au_rslack
+
693 ACL3_getaclres_sz
) << 2;
694 xdr_inline_pages(&req
->rq_rcv_buf
, replen
, args
->pages
, 0,
695 NFSACL_MAXPAGES
<< PAGE_SHIFT
);
701 * Encode SETACL arguments
704 nfs3_xdr_setaclargs(struct rpc_rqst
*req
, __be32
*p
,
705 struct nfs3_setaclargs
*args
)
707 struct xdr_buf
*buf
= &req
->rq_snd_buf
;
711 p
= xdr_encode_fhandle(p
, NFS_FH(args
->inode
));
712 *p
++ = htonl(args
->mask
);
713 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
716 if (args
->npages
!= 0)
717 xdr_encode_pages(buf
, args
->pages
, 0, args
->len
);
719 req
->rq_slen
+= args
->len
;
721 err
= nfsacl_encode(buf
, base
, args
->inode
,
722 (args
->mask
& NFS_ACL
) ?
723 args
->acl_access
: NULL
, 1, 0);
725 err
= nfsacl_encode(buf
, base
+ err
, args
->inode
,
726 (args
->mask
& NFS_DFACL
) ?
727 args
->acl_default
: NULL
, 1,
729 return (err
> 0) ? 0 : err
;
731 #endif /* CONFIG_NFS_V3_ACL */
734 * NFS XDR decode functions
738 * Decode attrstat reply.
741 nfs3_xdr_attrstat(struct rpc_rqst
*req
, __be32
*p
, struct nfs_fattr
*fattr
)
745 if ((status
= ntohl(*p
++)))
746 return nfs_stat_to_errno(status
);
747 xdr_decode_fattr(p
, fattr
);
752 * Decode status+wcc_data reply
753 * SATTR, REMOVE, RMDIR
756 nfs3_xdr_wccstat(struct rpc_rqst
*req
, __be32
*p
, struct nfs_fattr
*fattr
)
760 if ((status
= ntohl(*p
++)))
761 status
= nfs_stat_to_errno(status
);
762 xdr_decode_wcc_data(p
, fattr
);
767 nfs3_xdr_removeres(struct rpc_rqst
*req
, __be32
*p
, struct nfs_removeres
*res
)
769 return nfs3_xdr_wccstat(req
, p
, &res
->dir_attr
);
773 * Decode LOOKUP reply
776 nfs3_xdr_lookupres(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_diropres
*res
)
780 if ((status
= ntohl(*p
++))) {
781 status
= nfs_stat_to_errno(status
);
783 if (!(p
= xdr_decode_fhandle(p
, res
->fh
)))
784 return -errno_NFSERR_IO
;
785 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
787 xdr_decode_post_op_attr(p
, res
->dir_attr
);
792 * Decode ACCESS reply
795 nfs3_xdr_accessres(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_accessres
*res
)
797 int status
= ntohl(*p
++);
799 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
801 return nfs_stat_to_errno(status
);
802 res
->access
= ntohl(*p
++);
807 nfs3_xdr_readlinkargs(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_readlinkargs
*args
)
809 struct rpc_auth
*auth
= req
->rq_task
->tk_msg
.rpc_cred
->cr_auth
;
812 p
= xdr_encode_fhandle(p
, args
->fh
);
813 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
815 /* Inline the page array */
816 replen
= (RPC_REPHDRSIZE
+ auth
->au_rslack
+ NFS3_readlinkres_sz
) << 2;
817 xdr_inline_pages(&req
->rq_rcv_buf
, replen
, args
->pages
, args
->pgbase
, args
->pglen
);
822 * Decode READLINK reply
825 nfs3_xdr_readlinkres(struct rpc_rqst
*req
, __be32
*p
, struct nfs_fattr
*fattr
)
827 struct xdr_buf
*rcvbuf
= &req
->rq_rcv_buf
;
828 struct kvec
*iov
= rcvbuf
->head
;
834 status
= ntohl(*p
++);
835 p
= xdr_decode_post_op_attr(p
, fattr
);
838 return nfs_stat_to_errno(status
);
840 /* Convert length of symlink */
842 if (len
>= rcvbuf
->page_len
) {
843 dprintk("nfs: server returned giant symlink!\n");
844 return -ENAMETOOLONG
;
847 hdrlen
= (u8
*) p
- (u8
*) iov
->iov_base
;
848 if (iov
->iov_len
< hdrlen
) {
849 dprintk("NFS: READLINK reply header overflowed:"
850 "length %Zu > %Zu\n", hdrlen
, iov
->iov_len
);
851 return -errno_NFSERR_IO
;
852 } else if (iov
->iov_len
!= hdrlen
) {
853 dprintk("NFS: READLINK header is short. "
854 "iovec will be shifted.\n");
855 xdr_shift_buf(rcvbuf
, iov
->iov_len
- hdrlen
);
857 recvd
= req
->rq_rcv_buf
.len
- hdrlen
;
859 dprintk("NFS: server cheating in readlink reply: "
860 "count %u > recvd %u\n", len
, recvd
);
864 /* NULL terminate the string we got */
865 kaddr
= (char*)kmap_atomic(rcvbuf
->pages
[0], KM_USER0
);
866 kaddr
[len
+rcvbuf
->page_base
] = '\0';
867 kunmap_atomic(kaddr
, KM_USER0
);
875 nfs3_xdr_readres(struct rpc_rqst
*req
, __be32
*p
, struct nfs_readres
*res
)
877 struct kvec
*iov
= req
->rq_rcv_buf
.head
;
879 u32 count
, ocount
, recvd
;
882 status
= ntohl(*p
++);
883 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
886 return nfs_stat_to_errno(status
);
888 /* Decode reply count and EOF flag. NFSv3 is somewhat redundant
889 * in that it puts the count both in the res struct and in the
890 * opaque data count. */
892 res
->eof
= ntohl(*p
++);
893 ocount
= ntohl(*p
++);
895 if (ocount
!= count
) {
896 dprintk("NFS: READ count doesn't match RPC opaque count.\n");
897 return -errno_NFSERR_IO
;
900 hdrlen
= (u8
*) p
- (u8
*) iov
->iov_base
;
901 if (iov
->iov_len
< hdrlen
) {
902 dprintk("NFS: READ reply header overflowed:"
903 "length %Zu > %Zu\n", hdrlen
, iov
->iov_len
);
904 return -errno_NFSERR_IO
;
905 } else if (iov
->iov_len
!= hdrlen
) {
906 dprintk("NFS: READ header is short. iovec will be shifted.\n");
907 xdr_shift_buf(&req
->rq_rcv_buf
, iov
->iov_len
- hdrlen
);
910 recvd
= req
->rq_rcv_buf
.len
- hdrlen
;
912 dprintk("NFS: server cheating in read reply: "
913 "count %u > recvd %u\n", count
, recvd
);
918 if (count
< res
->count
)
925 * Decode WRITE response
928 nfs3_xdr_writeres(struct rpc_rqst
*req
, __be32
*p
, struct nfs_writeres
*res
)
932 status
= ntohl(*p
++);
933 p
= xdr_decode_wcc_data(p
, res
->fattr
);
936 return nfs_stat_to_errno(status
);
938 res
->count
= ntohl(*p
++);
939 res
->verf
->committed
= (enum nfs3_stable_how
)ntohl(*p
++);
940 res
->verf
->verifier
[0] = *p
++;
941 res
->verf
->verifier
[1] = *p
++;
947 * Decode a CREATE response
950 nfs3_xdr_createres(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_diropres
*res
)
954 status
= ntohl(*p
++);
957 if (!(p
= xdr_decode_fhandle(p
, res
->fh
)))
958 return -errno_NFSERR_IO
;
959 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
961 memset(res
->fh
, 0, sizeof(*res
->fh
));
962 /* Do decode post_op_attr but set it to NULL */
963 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
964 res
->fattr
->valid
= 0;
967 status
= nfs_stat_to_errno(status
);
969 p
= xdr_decode_wcc_data(p
, res
->dir_attr
);
974 * Decode RENAME reply
977 nfs3_xdr_renameres(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_renameres
*res
)
981 if ((status
= ntohl(*p
++)) != 0)
982 status
= nfs_stat_to_errno(status
);
983 p
= xdr_decode_wcc_data(p
, res
->fromattr
);
984 p
= xdr_decode_wcc_data(p
, res
->toattr
);
992 nfs3_xdr_linkres(struct rpc_rqst
*req
, __be32
*p
, struct nfs3_linkres
*res
)
996 if ((status
= ntohl(*p
++)) != 0)
997 status
= nfs_stat_to_errno(status
);
998 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
999 p
= xdr_decode_wcc_data(p
, res
->dir_attr
);
1004 * Decode FSSTAT reply
1007 nfs3_xdr_fsstatres(struct rpc_rqst
*req
, __be32
*p
, struct nfs_fsstat
*res
)
1011 status
= ntohl(*p
++);
1013 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
1015 return nfs_stat_to_errno(status
);
1017 p
= xdr_decode_hyper(p
, &res
->tbytes
);
1018 p
= xdr_decode_hyper(p
, &res
->fbytes
);
1019 p
= xdr_decode_hyper(p
, &res
->abytes
);
1020 p
= xdr_decode_hyper(p
, &res
->tfiles
);
1021 p
= xdr_decode_hyper(p
, &res
->ffiles
);
1022 p
= xdr_decode_hyper(p
, &res
->afiles
);
1024 /* ignore invarsec */
1029 * Decode FSINFO reply
1032 nfs3_xdr_fsinfores(struct rpc_rqst
*req
, __be32
*p
, struct nfs_fsinfo
*res
)
1036 status
= ntohl(*p
++);
1038 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
1040 return nfs_stat_to_errno(status
);
1042 res
->rtmax
= ntohl(*p
++);
1043 res
->rtpref
= ntohl(*p
++);
1044 res
->rtmult
= ntohl(*p
++);
1045 res
->wtmax
= ntohl(*p
++);
1046 res
->wtpref
= ntohl(*p
++);
1047 res
->wtmult
= ntohl(*p
++);
1048 res
->dtpref
= ntohl(*p
++);
1049 p
= xdr_decode_hyper(p
, &res
->maxfilesize
);
1051 /* ignore time_delta and properties */
1052 res
->lease_time
= 0;
1057 * Decode PATHCONF reply
1060 nfs3_xdr_pathconfres(struct rpc_rqst
*req
, __be32
*p
, struct nfs_pathconf
*res
)
1064 status
= ntohl(*p
++);
1066 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
1068 return nfs_stat_to_errno(status
);
1069 res
->max_link
= ntohl(*p
++);
1070 res
->max_namelen
= ntohl(*p
++);
1072 /* ignore remaining fields */
1077 * Decode COMMIT reply
1080 nfs3_xdr_commitres(struct rpc_rqst
*req
, __be32
*p
, struct nfs_writeres
*res
)
1084 status
= ntohl(*p
++);
1085 p
= xdr_decode_wcc_data(p
, res
->fattr
);
1087 return nfs_stat_to_errno(status
);
1089 res
->verf
->verifier
[0] = *p
++;
1090 res
->verf
->verifier
[1] = *p
++;
1094 #ifdef CONFIG_NFS_V3_ACL
1096 * Decode GETACL reply
1099 nfs3_xdr_getaclres(struct rpc_rqst
*req
, __be32
*p
,
1100 struct nfs3_getaclres
*res
)
1102 struct xdr_buf
*buf
= &req
->rq_rcv_buf
;
1103 int status
= ntohl(*p
++);
1104 struct posix_acl
**acl
;
1105 unsigned int *aclcnt
;
1109 return nfs_stat_to_errno(status
);
1110 p
= xdr_decode_post_op_attr(p
, res
->fattr
);
1111 res
->mask
= ntohl(*p
++);
1112 if (res
->mask
& ~(NFS_ACL
|NFS_ACLCNT
|NFS_DFACL
|NFS_DFACLCNT
))
1114 base
= (char *)p
- (char *)req
->rq_rcv_buf
.head
->iov_base
;
1116 acl
= (res
->mask
& NFS_ACL
) ? &res
->acl_access
: NULL
;
1117 aclcnt
= (res
->mask
& NFS_ACLCNT
) ? &res
->acl_access_count
: NULL
;
1118 err
= nfsacl_decode(buf
, base
, aclcnt
, acl
);
1120 acl
= (res
->mask
& NFS_DFACL
) ? &res
->acl_default
: NULL
;
1121 aclcnt
= (res
->mask
& NFS_DFACLCNT
) ? &res
->acl_default_count
: NULL
;
1123 err
= nfsacl_decode(buf
, base
+ err
, aclcnt
, acl
);
1124 return (err
> 0) ? 0 : err
;
1128 * Decode setacl reply.
1131 nfs3_xdr_setaclres(struct rpc_rqst
*req
, __be32
*p
, struct nfs_fattr
*fattr
)
1133 int status
= ntohl(*p
++);
1136 return nfs_stat_to_errno(status
);
1137 xdr_decode_post_op_attr(p
, fattr
);
1140 #endif /* CONFIG_NFS_V3_ACL */
1142 #define PROC(proc, argtype, restype, timer) \
1143 [NFS3PROC_##proc] = { \
1144 .p_proc = NFS3PROC_##proc, \
1145 .p_encode = (kxdrproc_t) nfs3_xdr_##argtype, \
1146 .p_decode = (kxdrproc_t) nfs3_xdr_##restype, \
1147 .p_arglen = NFS3_##argtype##_sz, \
1148 .p_replen = NFS3_##restype##_sz, \
1150 .p_statidx = NFS3PROC_##proc, \
1154 struct rpc_procinfo nfs3_procedures
[] = {
1155 PROC(GETATTR
, fhandle
, attrstat
, 1),
1156 PROC(SETATTR
, sattrargs
, wccstat
, 0),
1157 PROC(LOOKUP
, diropargs
, lookupres
, 2),
1158 PROC(ACCESS
, accessargs
, accessres
, 1),
1159 PROC(READLINK
, readlinkargs
, readlinkres
, 3),
1160 PROC(READ
, readargs
, readres
, 3),
1161 PROC(WRITE
, writeargs
, writeres
, 4),
1162 PROC(CREATE
, createargs
, createres
, 0),
1163 PROC(MKDIR
, mkdirargs
, createres
, 0),
1164 PROC(SYMLINK
, symlinkargs
, createres
, 0),
1165 PROC(MKNOD
, mknodargs
, createres
, 0),
1166 PROC(REMOVE
, removeargs
, removeres
, 0),
1167 PROC(RMDIR
, diropargs
, wccstat
, 0),
1168 PROC(RENAME
, renameargs
, renameres
, 0),
1169 PROC(LINK
, linkargs
, linkres
, 0),
1170 PROC(READDIR
, readdirargs
, readdirres
, 3),
1171 PROC(READDIRPLUS
, readdirargs
, readdirres
, 3),
1172 PROC(FSSTAT
, fhandle
, fsstatres
, 0),
1173 PROC(FSINFO
, fhandle
, fsinfores
, 0),
1174 PROC(PATHCONF
, fhandle
, pathconfres
, 0),
1175 PROC(COMMIT
, commitargs
, commitres
, 5),
1178 struct rpc_version nfs_version3
= {
1180 .nrprocs
= ARRAY_SIZE(nfs3_procedures
),
1181 .procs
= nfs3_procedures
1184 #ifdef CONFIG_NFS_V3_ACL
1185 static struct rpc_procinfo nfs3_acl_procedures
[] = {
1186 [ACLPROC3_GETACL
] = {
1187 .p_proc
= ACLPROC3_GETACL
,
1188 .p_encode
= (kxdrproc_t
) nfs3_xdr_getaclargs
,
1189 .p_decode
= (kxdrproc_t
) nfs3_xdr_getaclres
,
1190 .p_arglen
= ACL3_getaclargs_sz
,
1191 .p_replen
= ACL3_getaclres_sz
,
1195 [ACLPROC3_SETACL
] = {
1196 .p_proc
= ACLPROC3_SETACL
,
1197 .p_encode
= (kxdrproc_t
) nfs3_xdr_setaclargs
,
1198 .p_decode
= (kxdrproc_t
) nfs3_xdr_setaclres
,
1199 .p_arglen
= ACL3_setaclargs_sz
,
1200 .p_replen
= ACL3_setaclres_sz
,
1206 struct rpc_version nfsacl_version3
= {
1208 .nrprocs
= sizeof(nfs3_acl_procedures
)/
1209 sizeof(nfs3_acl_procedures
[0]),
1210 .procs
= nfs3_acl_procedures
,
1212 #endif /* CONFIG_NFS_V3_ACL */