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
94 static const umode_t nfs_type2fmt[] = {
101 [NF3SOCK] = S_IFSOCK,
106 * Common NFS XDR functions as inlines
108 static inline __be32 *
109 xdr_encode_fhandle(__be32 *p, const struct nfs_fh *fh)
111 return xdr_encode_array(p, fh->data, fh->size);
114 static inline __be32 *
115 xdr_decode_fhandle(__be32 *p, struct nfs_fh *fh)
117 if ((fh->size = ntohl(*p++)) <= NFS3_FHSIZE) {
118 memcpy(fh->data, p, fh->size);
119 return p + XDR_QUADLEN(fh->size);
125 * Encode/decode time.
127 static inline __be32 *
128 xdr_encode_time3(__be32 *p, struct timespec *timep)
130 *p++ = htonl(timep->tv_sec);
131 *p++ = htonl(timep->tv_nsec);
135 static inline __be32 *
136 xdr_decode_time3(__be32 *p, struct timespec *timep)
138 timep->tv_sec = ntohl(*p++);
139 timep->tv_nsec = ntohl(*p++);
144 xdr_decode_fattr(__be32 *p, struct nfs_fattr *fattr)
146 unsigned int type, major, minor;
152 fmode = nfs_type2fmt[type];
153 fattr->mode = (ntohl(*p++) & ~S_IFMT) | fmode;
154 fattr->nlink = ntohl(*p++);
155 fattr->uid = ntohl(*p++);
156 fattr->gid = ntohl(*p++);
157 p = xdr_decode_hyper(p, &fattr->size);
158 p = xdr_decode_hyper(p, &fattr->du.nfs3.used);
160 /* Turn remote device info into Linux-specific dev_t */
163 fattr->rdev = MKDEV(major, minor);
164 if (MAJOR(fattr->rdev) != major || MINOR(fattr->rdev) != minor)
167 p = xdr_decode_hyper(p, &fattr->fsid.major);
168 fattr->fsid.minor = 0;
169 p = xdr_decode_hyper(p, &fattr->fileid);
170 p = xdr_decode_time3(p, &fattr->atime);
171 p = xdr_decode_time3(p, &fattr->mtime);
172 p = xdr_decode_time3(p, &fattr->ctime);
174 /* Update the mode bits */
175 fattr->valid |= NFS_ATTR_FATTR_V3;
179 static inline __be32 *
180 xdr_encode_sattr(__be32 *p, struct iattr *attr)
182 if (attr->ia_valid & ATTR_MODE) {
184 *p++ = htonl(attr->ia_mode & S_IALLUGO);
188 if (attr->ia_valid & ATTR_UID) {
190 *p++ = htonl(attr->ia_uid);
194 if (attr->ia_valid & ATTR_GID) {
196 *p++ = htonl(attr->ia_gid);
200 if (attr->ia_valid & ATTR_SIZE) {
202 p = xdr_encode_hyper(p, (__u64) attr->ia_size);
206 if (attr->ia_valid & ATTR_ATIME_SET) {
208 p = xdr_encode_time3(p, &attr->ia_atime);
209 } else if (attr->ia_valid & ATTR_ATIME) {
214 if (attr->ia_valid & ATTR_MTIME_SET) {
216 p = xdr_encode_time3(p, &attr->ia_mtime);
217 } else if (attr->ia_valid & ATTR_MTIME) {
225 static inline __be32 *
226 xdr_decode_wcc_attr(__be32 *p, struct nfs_fattr *fattr)
228 p = xdr_decode_hyper(p, &fattr->pre_size);
229 p = xdr_decode_time3(p, &fattr->pre_mtime);
230 p = xdr_decode_time3(p, &fattr->pre_ctime);
231 fattr->valid |= NFS_ATTR_FATTR_PRESIZE
232 | NFS_ATTR_FATTR_PREMTIME
233 | NFS_ATTR_FATTR_PRECTIME;
237 static inline __be32 *
238 xdr_decode_post_op_attr(__be32 *p, struct nfs_fattr *fattr)
241 p = xdr_decode_fattr(p, fattr);
245 static inline __be32 *
246 xdr_decode_pre_op_attr(__be32 *p, struct nfs_fattr *fattr)
249 return xdr_decode_wcc_attr(p, fattr);
254 static inline __be32 *
255 xdr_decode_wcc_data(__be32 *p, struct nfs_fattr *fattr)
257 p = xdr_decode_pre_op_attr(p, fattr);
258 return xdr_decode_post_op_attr(p, fattr);
262 * NFS encode functions
266 * Encode file handle argument
269 nfs3_xdr_fhandle(struct rpc_rqst *req, __be32 *p, struct nfs_fh *fh)
271 p = xdr_encode_fhandle(p, fh);
272 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
277 * Encode SETATTR arguments
280 nfs3_xdr_sattrargs(struct rpc_rqst *req, __be32 *p, struct nfs3_sattrargs *args)
282 p = xdr_encode_fhandle(p, args->fh);
283 p = xdr_encode_sattr(p, args->sattr);
284 *p++ = htonl(args->guard);
286 p = xdr_encode_time3(p, &args->guardtime);
287 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
292 * Encode directory ops argument
295 nfs3_xdr_diropargs(struct rpc_rqst *req, __be32 *p, struct nfs3_diropargs *args)
297 p = xdr_encode_fhandle(p, args->fh);
298 p = xdr_encode_array(p, args->name, args->len);
299 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
304 * Encode REMOVE argument
307 nfs3_xdr_removeargs(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
309 p = xdr_encode_fhandle(p, args->fh);
310 p = xdr_encode_array(p, args->name.name, args->name.len);
311 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
316 * Encode access() argument
319 nfs3_xdr_accessargs(struct rpc_rqst *req, __be32 *p, struct nfs3_accessargs *args)
321 p = xdr_encode_fhandle(p, args->fh);
322 *p++ = htonl(args->access);
323 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
328 * Arguments to a READ call. Since we read data directly into the page
329 * cache, we also set up the reply iovec here so that iov[1] points
330 * exactly to the page we want to fetch.
333 nfs3_xdr_readargs(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
335 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
337 u32 count = args->count;
339 p = xdr_encode_fhandle(p, args->fh);
340 p = xdr_encode_hyper(p, args->offset);
342 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
344 /* Inline the page array */
345 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS3_readres_sz) << 2;
346 xdr_inline_pages(&req->rq_rcv_buf, replen,
347 args->pages, args->pgbase, count);
348 req->rq_rcv_buf.flags |= XDRBUF_READ;
353 * Write arguments. Splice the buffer to be written into the iovec.
356 nfs3_xdr_writeargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
358 struct xdr_buf *sndbuf = &req->rq_snd_buf;
359 u32 count = args->count;
361 p = xdr_encode_fhandle(p, args->fh);
362 p = xdr_encode_hyper(p, args->offset);
364 *p++ = htonl(args->stable);
366 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
368 /* Copy the page array */
369 xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
370 sndbuf->flags |= XDRBUF_WRITE;
375 * Encode CREATE arguments
378 nfs3_xdr_createargs(struct rpc_rqst *req, __be32 *p, struct nfs3_createargs *args)
380 p = xdr_encode_fhandle(p, args->fh);
381 p = xdr_encode_array(p, args->name, args->len);
383 *p++ = htonl(args->createmode);
384 if (args->createmode == NFS3_CREATE_EXCLUSIVE) {
385 *p++ = args->verifier[0];
386 *p++ = args->verifier[1];
388 p = xdr_encode_sattr(p, args->sattr);
390 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
395 * Encode MKDIR arguments
398 nfs3_xdr_mkdirargs(struct rpc_rqst *req, __be32 *p, struct nfs3_mkdirargs *args)
400 p = xdr_encode_fhandle(p, args->fh);
401 p = xdr_encode_array(p, args->name, args->len);
402 p = xdr_encode_sattr(p, args->sattr);
403 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
408 * Encode SYMLINK arguments
411 nfs3_xdr_symlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs3_symlinkargs *args)
413 p = xdr_encode_fhandle(p, args->fromfh);
414 p = xdr_encode_array(p, args->fromname, args->fromlen);
415 p = xdr_encode_sattr(p, args->sattr);
416 *p++ = htonl(args->pathlen);
417 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
420 xdr_encode_pages(&req->rq_snd_buf, args->pages, 0, args->pathlen);
425 * Encode MKNOD arguments
428 nfs3_xdr_mknodargs(struct rpc_rqst *req, __be32 *p, struct nfs3_mknodargs *args)
430 p = xdr_encode_fhandle(p, args->fh);
431 p = xdr_encode_array(p, args->name, args->len);
432 *p++ = htonl(args->type);
433 p = xdr_encode_sattr(p, args->sattr);
434 if (args->type == NF3CHR || args->type == NF3BLK) {
435 *p++ = htonl(MAJOR(args->rdev));
436 *p++ = htonl(MINOR(args->rdev));
439 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
444 * Encode RENAME arguments
447 nfs3_xdr_renameargs(struct rpc_rqst *req, __be32 *p, struct nfs3_renameargs *args)
449 p = xdr_encode_fhandle(p, args->fromfh);
450 p = xdr_encode_array(p, args->fromname, args->fromlen);
451 p = xdr_encode_fhandle(p, args->tofh);
452 p = xdr_encode_array(p, args->toname, args->tolen);
453 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
458 * Encode LINK arguments
461 nfs3_xdr_linkargs(struct rpc_rqst *req, __be32 *p, struct nfs3_linkargs *args)
463 p = xdr_encode_fhandle(p, args->fromfh);
464 p = xdr_encode_fhandle(p, args->tofh);
465 p = xdr_encode_array(p, args->toname, args->tolen);
466 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
471 * Encode arguments to readdir call
474 nfs3_xdr_readdirargs(struct rpc_rqst *req, __be32 *p, struct nfs3_readdirargs *args)
476 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
478 u32 count = args->count;
480 p = xdr_encode_fhandle(p, args->fh);
481 p = xdr_encode_hyper(p, args->cookie);
482 *p++ = args->verf[0];
483 *p++ = args->verf[1];
485 /* readdirplus: need dircount + buffer size.
486 * We just make sure we make dircount big enough */
487 *p++ = htonl(count >> 3);
490 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
492 /* Inline the page array */
493 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS3_readdirres_sz) << 2;
494 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
499 * Decode the result of a readdir call.
500 * We just check for syntactical correctness.
503 nfs3_xdr_readdirres(struct rpc_rqst *req, __be32 *p, struct nfs3_readdirres *res)
505 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
506 struct kvec *iov = rcvbuf->head;
509 u32 len, recvd, pglen;
511 __be32 *entry, *end, *kaddr;
513 status = ntohl(*p++);
514 /* Decode post_op_attrs */
515 p = xdr_decode_post_op_attr(p, res->dir_attr);
517 return nfs_stat_to_errno(status);
518 /* Decode verifier cookie */
526 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
527 if (iov->iov_len < hdrlen) {
528 dprintk("NFS: READDIR reply header overflowed:"
529 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
530 return -errno_NFSERR_IO;
531 } else if (iov->iov_len != hdrlen) {
532 dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
533 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
536 pglen = rcvbuf->page_len;
537 recvd = rcvbuf->len - hdrlen;
540 page = rcvbuf->pages;
541 kaddr = p = kmap_atomic(*page, KM_USER0);
542 end = (__be32 *)((char *)p + pglen);
545 /* Make sure the packet actually has a value_follows and EOF entry */
546 if ((entry + 1) > end)
552 p += 2; /* inode # */
553 len = ntohl(*p++); /* string length */
554 p += XDR_QUADLEN(len) + 2; /* name + cookie */
555 if (len > NFS3_MAXNAMLEN) {
556 dprintk("NFS: giant filename in readdir (len 0x%x)!\n",
575 if (len > NFS3_FHSIZE) {
576 dprintk("NFS: giant filehandle in "
577 "readdir (len 0x%x)!\n", len);
580 p += XDR_QUADLEN(len);
590 * Apparently some server sends responses that are a valid size, but
591 * contain no entries, and have value_follows==0 and EOF==0. For
592 * those, just set the EOF marker.
594 if (!nr && entry[1] == 0) {
595 dprintk("NFS: readdir reply truncated!\n");
599 kunmap_atomic(kaddr, KM_USER0);
603 * When we get a short packet there are 2 possibilities. We can
604 * return an error, or fix up the response to look like a valid
605 * response and return what we have so far. If there are no
606 * entries and the packet was short, then return -EIO. If there
607 * are valid entries in the response, return them and pretend that
608 * the call was successful, but incomplete. The caller can retry the
609 * readdir starting at the last cookie.
611 entry[0] = entry[1] = 0;
613 nr = -errno_NFSERR_IO;
616 nr = -errno_NFSERR_IO;
621 nfs3_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
623 struct nfs_entry old = *entry;
627 return ERR_PTR(-EAGAIN);
629 return ERR_PTR(-EBADCOOKIE);
632 p = xdr_decode_hyper(p, &entry->ino);
633 entry->len = ntohl(*p++);
634 entry->name = (const char *) p;
635 p += XDR_QUADLEN(entry->len);
636 entry->prev_cookie = entry->cookie;
637 p = xdr_decode_hyper(p, &entry->cookie);
640 entry->fattr->valid = 0;
641 p = xdr_decode_post_op_attr(p, entry->fattr);
642 /* In fact, a post_op_fh3: */
644 p = xdr_decode_fhandle(p, entry->fh);
645 /* Ugh -- server reply was truncated */
647 dprintk("NFS: FH truncated\n");
649 return ERR_PTR(-EAGAIN);
652 memset((u8*)(entry->fh), 0, sizeof(*entry->fh));
655 entry->eof = !p[0] && p[1];
660 * Encode COMMIT arguments
663 nfs3_xdr_commitargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
665 p = xdr_encode_fhandle(p, args->fh);
666 p = xdr_encode_hyper(p, args->offset);
667 *p++ = htonl(args->count);
668 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
672 #ifdef CONFIG_NFS_V3_ACL
674 * Encode GETACL arguments
677 nfs3_xdr_getaclargs(struct rpc_rqst *req, __be32 *p,
678 struct nfs3_getaclargs *args)
680 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
683 p = xdr_encode_fhandle(p, args->fh);
684 *p++ = htonl(args->mask);
685 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
687 if (args->mask & (NFS_ACL | NFS_DFACL)) {
688 /* Inline the page array */
689 replen = (RPC_REPHDRSIZE + auth->au_rslack +
690 ACL3_getaclres_sz) << 2;
691 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0,
692 NFSACL_MAXPAGES << PAGE_SHIFT);
698 * Encode SETACL arguments
701 nfs3_xdr_setaclargs(struct rpc_rqst *req, __be32 *p,
702 struct nfs3_setaclargs *args)
704 struct xdr_buf *buf = &req->rq_snd_buf;
708 p = xdr_encode_fhandle(p, NFS_FH(args->inode));
709 *p++ = htonl(args->mask);
710 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
713 if (args->npages != 0)
714 xdr_encode_pages(buf, args->pages, 0, args->len);
716 req->rq_slen += args->len;
718 err = nfsacl_encode(buf, base, args->inode,
719 (args->mask & NFS_ACL) ?
720 args->acl_access : NULL, 1, 0);
722 err = nfsacl_encode(buf, base + err, args->inode,
723 (args->mask & NFS_DFACL) ?
724 args->acl_default : NULL, 1,
726 return (err > 0) ? 0 : err;
728 #endif /* CONFIG_NFS_V3_ACL */
731 * NFS XDR decode functions
735 * Decode attrstat reply.
738 nfs3_xdr_attrstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
742 if ((status = ntohl(*p++)))
743 return nfs_stat_to_errno(status);
744 xdr_decode_fattr(p, fattr);
749 * Decode status+wcc_data reply
750 * SATTR, REMOVE, RMDIR
753 nfs3_xdr_wccstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
757 if ((status = ntohl(*p++)))
758 status = nfs_stat_to_errno(status);
759 xdr_decode_wcc_data(p, fattr);
764 nfs3_xdr_removeres(struct rpc_rqst *req, __be32 *p, struct nfs_removeres *res)
766 return nfs3_xdr_wccstat(req, p, &res->dir_attr);
770 * Decode LOOKUP reply
773 nfs3_xdr_lookupres(struct rpc_rqst *req, __be32 *p, struct nfs3_diropres *res)
777 if ((status = ntohl(*p++))) {
778 status = nfs_stat_to_errno(status);
780 if (!(p = xdr_decode_fhandle(p, res->fh)))
781 return -errno_NFSERR_IO;
782 p = xdr_decode_post_op_attr(p, res->fattr);
784 xdr_decode_post_op_attr(p, res->dir_attr);
789 * Decode ACCESS reply
792 nfs3_xdr_accessres(struct rpc_rqst *req, __be32 *p, struct nfs3_accessres *res)
794 int status = ntohl(*p++);
796 p = xdr_decode_post_op_attr(p, res->fattr);
798 return nfs_stat_to_errno(status);
799 res->access = ntohl(*p++);
804 nfs3_xdr_readlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs3_readlinkargs *args)
806 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
809 p = xdr_encode_fhandle(p, args->fh);
810 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
812 /* Inline the page array */
813 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS3_readlinkres_sz) << 2;
814 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
819 * Decode READLINK reply
822 nfs3_xdr_readlinkres(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
824 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
825 struct kvec *iov = rcvbuf->head;
831 status = ntohl(*p++);
832 p = xdr_decode_post_op_attr(p, fattr);
835 return nfs_stat_to_errno(status);
837 /* Convert length of symlink */
839 if (len >= rcvbuf->page_len) {
840 dprintk("nfs: server returned giant symlink!\n");
841 return -ENAMETOOLONG;
844 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
845 if (iov->iov_len < hdrlen) {
846 dprintk("NFS: READLINK reply header overflowed:"
847 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
848 return -errno_NFSERR_IO;
849 } else if (iov->iov_len != hdrlen) {
850 dprintk("NFS: READLINK header is short. "
851 "iovec will be shifted.\n");
852 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
854 recvd = req->rq_rcv_buf.len - hdrlen;
856 dprintk("NFS: server cheating in readlink reply: "
857 "count %u > recvd %u\n", len, recvd);
861 /* NULL terminate the string we got */
862 kaddr = (char*)kmap_atomic(rcvbuf->pages[0], KM_USER0);
863 kaddr[len+rcvbuf->page_base] = '\0';
864 kunmap_atomic(kaddr, KM_USER0);
872 nfs3_xdr_readres(struct rpc_rqst *req, __be32 *p, struct nfs_readres *res)
874 struct kvec *iov = req->rq_rcv_buf.head;
876 u32 count, ocount, recvd;
879 status = ntohl(*p++);
880 p = xdr_decode_post_op_attr(p, res->fattr);
883 return nfs_stat_to_errno(status);
885 /* Decode reply count and EOF flag. NFSv3 is somewhat redundant
886 * in that it puts the count both in the res struct and in the
887 * opaque data count. */
889 res->eof = ntohl(*p++);
890 ocount = ntohl(*p++);
892 if (ocount != count) {
893 dprintk("NFS: READ count doesn't match RPC opaque count.\n");
894 return -errno_NFSERR_IO;
897 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
898 if (iov->iov_len < hdrlen) {
899 dprintk("NFS: READ reply header overflowed:"
900 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
901 return -errno_NFSERR_IO;
902 } else if (iov->iov_len != hdrlen) {
903 dprintk("NFS: READ header is short. iovec will be shifted.\n");
904 xdr_shift_buf(&req->rq_rcv_buf, iov->iov_len - hdrlen);
907 recvd = req->rq_rcv_buf.len - hdrlen;
909 dprintk("NFS: server cheating in read reply: "
910 "count %u > recvd %u\n", count, recvd);
915 if (count < res->count)
922 * Decode WRITE response
925 nfs3_xdr_writeres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
929 status = ntohl(*p++);
930 p = xdr_decode_wcc_data(p, res->fattr);
933 return nfs_stat_to_errno(status);
935 res->count = ntohl(*p++);
936 res->verf->committed = (enum nfs3_stable_how)ntohl(*p++);
937 res->verf->verifier[0] = *p++;
938 res->verf->verifier[1] = *p++;
944 * Decode a CREATE response
947 nfs3_xdr_createres(struct rpc_rqst *req, __be32 *p, struct nfs3_diropres *res)
951 status = ntohl(*p++);
954 if (!(p = xdr_decode_fhandle(p, res->fh)))
955 return -errno_NFSERR_IO;
956 p = xdr_decode_post_op_attr(p, res->fattr);
958 memset(res->fh, 0, sizeof(*res->fh));
959 /* Do decode post_op_attr but set it to NULL */
960 p = xdr_decode_post_op_attr(p, res->fattr);
961 res->fattr->valid = 0;
964 status = nfs_stat_to_errno(status);
966 p = xdr_decode_wcc_data(p, res->dir_attr);
971 * Decode RENAME reply
974 nfs3_xdr_renameres(struct rpc_rqst *req, __be32 *p, struct nfs3_renameres *res)
978 if ((status = ntohl(*p++)) != 0)
979 status = nfs_stat_to_errno(status);
980 p = xdr_decode_wcc_data(p, res->fromattr);
981 p = xdr_decode_wcc_data(p, res->toattr);
989 nfs3_xdr_linkres(struct rpc_rqst *req, __be32 *p, struct nfs3_linkres *res)
993 if ((status = ntohl(*p++)) != 0)
994 status = nfs_stat_to_errno(status);
995 p = xdr_decode_post_op_attr(p, res->fattr);
996 p = xdr_decode_wcc_data(p, res->dir_attr);
1001 * Decode FSSTAT reply
1004 nfs3_xdr_fsstatres(struct rpc_rqst *req, __be32 *p, struct nfs_fsstat *res)
1008 status = ntohl(*p++);
1010 p = xdr_decode_post_op_attr(p, res->fattr);
1012 return nfs_stat_to_errno(status);
1014 p = xdr_decode_hyper(p, &res->tbytes);
1015 p = xdr_decode_hyper(p, &res->fbytes);
1016 p = xdr_decode_hyper(p, &res->abytes);
1017 p = xdr_decode_hyper(p, &res->tfiles);
1018 p = xdr_decode_hyper(p, &res->ffiles);
1019 p = xdr_decode_hyper(p, &res->afiles);
1021 /* ignore invarsec */
1026 * Decode FSINFO reply
1029 nfs3_xdr_fsinfores(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *res)
1033 status = ntohl(*p++);
1035 p = xdr_decode_post_op_attr(p, res->fattr);
1037 return nfs_stat_to_errno(status);
1039 res->rtmax = ntohl(*p++);
1040 res->rtpref = ntohl(*p++);
1041 res->rtmult = ntohl(*p++);
1042 res->wtmax = ntohl(*p++);
1043 res->wtpref = ntohl(*p++);
1044 res->wtmult = ntohl(*p++);
1045 res->dtpref = ntohl(*p++);
1046 p = xdr_decode_hyper(p, &res->maxfilesize);
1048 /* ignore time_delta and properties */
1049 res->lease_time = 0;
1054 * Decode PATHCONF reply
1057 nfs3_xdr_pathconfres(struct rpc_rqst *req, __be32 *p, struct nfs_pathconf *res)
1061 status = ntohl(*p++);
1063 p = xdr_decode_post_op_attr(p, res->fattr);
1065 return nfs_stat_to_errno(status);
1066 res->max_link = ntohl(*p++);
1067 res->max_namelen = ntohl(*p++);
1069 /* ignore remaining fields */
1074 * Decode COMMIT reply
1077 nfs3_xdr_commitres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
1081 status = ntohl(*p++);
1082 p = xdr_decode_wcc_data(p, res->fattr);
1084 return nfs_stat_to_errno(status);
1086 res->verf->verifier[0] = *p++;
1087 res->verf->verifier[1] = *p++;
1091 #ifdef CONFIG_NFS_V3_ACL
1093 * Decode GETACL reply
1096 nfs3_xdr_getaclres(struct rpc_rqst *req, __be32 *p,
1097 struct nfs3_getaclres *res)
1099 struct xdr_buf *buf = &req->rq_rcv_buf;
1100 int status = ntohl(*p++);
1101 struct posix_acl **acl;
1102 unsigned int *aclcnt;
1106 return nfs_stat_to_errno(status);
1107 p = xdr_decode_post_op_attr(p, res->fattr);
1108 res->mask = ntohl(*p++);
1109 if (res->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
1111 base = (char *)p - (char *)req->rq_rcv_buf.head->iov_base;
1113 acl = (res->mask & NFS_ACL) ? &res->acl_access : NULL;
1114 aclcnt = (res->mask & NFS_ACLCNT) ? &res->acl_access_count : NULL;
1115 err = nfsacl_decode(buf, base, aclcnt, acl);
1117 acl = (res->mask & NFS_DFACL) ? &res->acl_default : NULL;
1118 aclcnt = (res->mask & NFS_DFACLCNT) ? &res->acl_default_count : NULL;
1120 err = nfsacl_decode(buf, base + err, aclcnt, acl);
1121 return (err > 0) ? 0 : err;
1125 * Decode setacl reply.
1128 nfs3_xdr_setaclres(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
1130 int status = ntohl(*p++);
1133 return nfs_stat_to_errno(status);
1134 xdr_decode_post_op_attr(p, fattr);
1137 #endif /* CONFIG_NFS_V3_ACL */
1139 #define PROC(proc, argtype, restype, timer) \
1140 [NFS3PROC_##proc] = { \
1141 .p_proc = NFS3PROC_##proc, \
1142 .p_encode = (kxdrproc_t) nfs3_xdr_##argtype, \
1143 .p_decode = (kxdrproc_t) nfs3_xdr_##restype, \
1144 .p_arglen = NFS3_##argtype##_sz, \
1145 .p_replen = NFS3_##restype##_sz, \
1147 .p_statidx = NFS3PROC_##proc, \
1151 struct rpc_procinfo nfs3_procedures[] = {
1152 PROC(GETATTR, fhandle, attrstat, 1),
1153 PROC(SETATTR, sattrargs, wccstat, 0),
1154 PROC(LOOKUP, diropargs, lookupres, 2),
1155 PROC(ACCESS, accessargs, accessres, 1),
1156 PROC(READLINK, readlinkargs, readlinkres, 3),
1157 PROC(READ, readargs, readres, 3),
1158 PROC(WRITE, writeargs, writeres, 4),
1159 PROC(CREATE, createargs, createres, 0),
1160 PROC(MKDIR, mkdirargs, createres, 0),
1161 PROC(SYMLINK, symlinkargs, createres, 0),
1162 PROC(MKNOD, mknodargs, createres, 0),
1163 PROC(REMOVE, removeargs, removeres, 0),
1164 PROC(RMDIR, diropargs, wccstat, 0),
1165 PROC(RENAME, renameargs, renameres, 0),
1166 PROC(LINK, linkargs, linkres, 0),
1167 PROC(READDIR, readdirargs, readdirres, 3),
1168 PROC(READDIRPLUS, readdirargs, readdirres, 3),
1169 PROC(FSSTAT, fhandle, fsstatres, 0),
1170 PROC(FSINFO, fhandle, fsinfores, 0),
1171 PROC(PATHCONF, fhandle, pathconfres, 0),
1172 PROC(COMMIT, commitargs, commitres, 5),
1175 struct rpc_version nfs_version3 = {
1177 .nrprocs = ARRAY_SIZE(nfs3_procedures),
1178 .procs = nfs3_procedures
1181 #ifdef CONFIG_NFS_V3_ACL
1182 static struct rpc_procinfo nfs3_acl_procedures[] = {
1183 [ACLPROC3_GETACL] = {
1184 .p_proc = ACLPROC3_GETACL,
1185 .p_encode = (kxdrproc_t) nfs3_xdr_getaclargs,
1186 .p_decode = (kxdrproc_t) nfs3_xdr_getaclres,
1187 .p_arglen = ACL3_getaclargs_sz,
1188 .p_replen = ACL3_getaclres_sz,
1192 [ACLPROC3_SETACL] = {
1193 .p_proc = ACLPROC3_SETACL,
1194 .p_encode = (kxdrproc_t) nfs3_xdr_setaclargs,
1195 .p_decode = (kxdrproc_t) nfs3_xdr_setaclres,
1196 .p_arglen = ACL3_setaclargs_sz,
1197 .p_replen = ACL3_setaclres_sz,
1203 struct rpc_version nfsacl_version3 = {
1205 .nrprocs = sizeof(nfs3_acl_procedures)/
1206 sizeof(nfs3_acl_procedures[0]),
1207 .procs = nfs3_acl_procedures,
1209 #endif /* CONFIG_NFS_V3_ACL */