2 * linux/fs/nfs/nfs2xdr.c
4 * XDR functions to encode/decode NFS RPC arguments and results.
6 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
7 * Copyright (C) 1996 Olaf Kirch
8 * 04 Aug 1998 Ion Badulescu <ionut@cs.columbia.edu>
9 * FIFO's need special handling in NFSv2
12 #include <linux/param.h>
13 #include <linux/time.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
18 #include <linux/pagemap.h>
19 #include <linux/proc_fs.h>
20 #include <linux/sunrpc/clnt.h>
21 #include <linux/nfs.h>
22 #include <linux/nfs2.h>
23 #include <linux/nfs_fs.h>
26 #define NFSDBG_FACILITY NFSDBG_XDR
28 /* Mapping from NFS error code to "errno" error code. */
29 #define errno_NFSERR_IO EIO
32 * Declare the space requirements for NFS arguments and replies as
33 * number of 32bit-words
35 #define NFS_fhandle_sz (8)
36 #define NFS_sattr_sz (8)
37 #define NFS_filename_sz (1+(NFS2_MAXNAMLEN>>2))
38 #define NFS_path_sz (1+(NFS2_MAXPATHLEN>>2))
39 #define NFS_fattr_sz (17)
40 #define NFS_info_sz (5)
41 #define NFS_entry_sz (NFS_filename_sz+3)
43 #define NFS_diropargs_sz (NFS_fhandle_sz+NFS_filename_sz)
44 #define NFS_removeargs_sz (NFS_fhandle_sz+NFS_filename_sz)
45 #define NFS_sattrargs_sz (NFS_fhandle_sz+NFS_sattr_sz)
46 #define NFS_readlinkargs_sz (NFS_fhandle_sz)
47 #define NFS_readargs_sz (NFS_fhandle_sz+3)
48 #define NFS_writeargs_sz (NFS_fhandle_sz+4)
49 #define NFS_createargs_sz (NFS_diropargs_sz+NFS_sattr_sz)
50 #define NFS_renameargs_sz (NFS_diropargs_sz+NFS_diropargs_sz)
51 #define NFS_linkargs_sz (NFS_fhandle_sz+NFS_diropargs_sz)
52 #define NFS_symlinkargs_sz (NFS_diropargs_sz+1+NFS_sattr_sz)
53 #define NFS_readdirargs_sz (NFS_fhandle_sz+2)
55 #define NFS_attrstat_sz (1+NFS_fattr_sz)
56 #define NFS_diropres_sz (1+NFS_fhandle_sz+NFS_fattr_sz)
57 #define NFS_readlinkres_sz (2)
58 #define NFS_readres_sz (1+NFS_fattr_sz+1)
59 #define NFS_writeres_sz (NFS_attrstat_sz)
60 #define NFS_stat_sz (1)
61 #define NFS_readdirres_sz (1)
62 #define NFS_statfsres_sz (1+NFS_info_sz)
66 * While encoding arguments, set up the reply buffer in advance to
67 * receive reply data directly into the page cache.
69 static void prepare_reply_buffer(struct rpc_rqst *req, struct page **pages,
70 unsigned int base, unsigned int len,
73 struct rpc_auth *auth = req->rq_cred->cr_auth;
76 replen = RPC_REPHDRSIZE + auth->au_rslack + bufsize;
77 xdr_inline_pages(&req->rq_rcv_buf, replen << 2, pages, base, len);
81 * Handle decode buffer overflows out-of-line.
83 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
85 dprintk("NFS: %s prematurely hit the end of our receive buffer. "
86 "Remaining buffer length is %tu words.\n",
87 func, xdr->end - xdr->p);
92 * Common NFS XDR functions as inlines
95 xdr_decode_time(__be32 *p, struct timespec *timep)
97 timep->tv_sec = ntohl(*p++);
98 /* Convert microseconds into nanoseconds */
99 timep->tv_nsec = ntohl(*p++) * 1000;
104 xdr_decode_fattr(__be32 *p, struct nfs_fattr *fattr)
108 fattr->mode = ntohl(*p++);
109 fattr->nlink = ntohl(*p++);
110 fattr->uid = ntohl(*p++);
111 fattr->gid = ntohl(*p++);
112 fattr->size = ntohl(*p++);
113 fattr->du.nfs2.blocksize = ntohl(*p++);
115 fattr->du.nfs2.blocks = ntohl(*p++);
116 fattr->fsid.major = ntohl(*p++);
117 fattr->fsid.minor = 0;
118 fattr->fileid = ntohl(*p++);
119 p = xdr_decode_time(p, &fattr->atime);
120 p = xdr_decode_time(p, &fattr->mtime);
121 p = xdr_decode_time(p, &fattr->ctime);
122 fattr->valid |= NFS_ATTR_FATTR_V2;
123 fattr->rdev = new_decode_dev(rdev);
124 if (type == NFCHR && rdev == NFS2_FIFO_DEV) {
125 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
132 * Encode/decode NFSv2 basic data types
134 * Basic NFSv2 data types are defined in section 2.3 of RFC 1094:
135 * "NFS: Network File System Protocol Specification".
137 * Not all basic data types have their own encoding and decoding
138 * functions. For run-time efficiency, some data types are encoded
143 * typedef opaque nfsdata<>;
145 static int decode_nfsdata(struct xdr_stream *xdr, struct nfs_readres *result)
151 p = xdr_inline_decode(xdr, 4);
152 if (unlikely(p == NULL))
154 count = be32_to_cpup(p);
155 hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
156 recvd = xdr->buf->len - hdrlen;
157 if (unlikely(count > recvd))
160 xdr_read_pages(xdr, count);
161 result->eof = 0; /* NFSv2 does not pass EOF flag on the wire. */
162 result->count = count;
165 dprintk("NFS: server cheating in read result: "
166 "count %u > recvd %u\n", count, recvd);
170 print_overflow_msg(__func__, xdr);
184 * NFSERR_NOTDIR = 20,
189 * NFSERR_NAMETOOLONG = 63,
190 * NFSERR_NOTEMPTY = 66,
196 static int decode_stat(struct xdr_stream *xdr, enum nfs_stat *status)
200 p = xdr_inline_decode(xdr, 4);
201 if (unlikely(p == NULL))
203 *status = be32_to_cpup(p);
206 print_overflow_msg(__func__, xdr);
213 * typedef opaque fhandle[FHSIZE];
215 static void encode_fhandle(struct xdr_stream *xdr, const struct nfs_fh *fh)
219 BUG_ON(fh->size != NFS2_FHSIZE);
220 p = xdr_reserve_space(xdr, NFS2_FHSIZE);
221 memcpy(p, fh->data, NFS2_FHSIZE);
224 static int decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
228 p = xdr_inline_decode(xdr, NFS2_FHSIZE);
229 if (unlikely(p == NULL))
231 fh->size = NFS2_FHSIZE;
232 memcpy(fh->data, p, NFS2_FHSIZE);
235 print_overflow_msg(__func__, xdr);
243 * unsigned int seconds;
244 * unsigned int useconds;
247 static __be32 *xdr_encode_time(__be32 *p, const struct timespec *timep)
249 *p++ = cpu_to_be32(timep->tv_sec);
250 if (timep->tv_nsec != 0)
251 *p++ = cpu_to_be32(timep->tv_nsec / NSEC_PER_USEC);
253 *p++ = cpu_to_be32(0);
258 * Passing the invalid value useconds=1000000 is a Sun convention for
259 * "set to current server time". It's needed to make permissions checks
260 * for the "touch" program across v2 mounts to Solaris and Irix servers
261 * work correctly. See description of sattr in section 6.1 of "NFS
262 * Illustrated" by Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5.
264 static __be32 *xdr_encode_current_server_time(__be32 *p,
265 const struct timespec *timep)
267 *p++ = cpu_to_be32(timep->tv_sec);
268 *p++ = cpu_to_be32(1000000);
278 * unsigned int nlink;
282 * unsigned int blocksize;
284 * unsigned int blocks;
286 * unsigned int fileid;
293 static int decode_fattr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
297 p = xdr_inline_decode(xdr, NFS_fattr_sz << 2);
298 if (unlikely(p == NULL))
300 xdr_decode_fattr(p, fattr);
303 print_overflow_msg(__func__, xdr);
320 #define NFS2_SATTR_NOT_SET (0xffffffff)
322 static __be32 *xdr_time_not_set(__be32 *p)
324 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
325 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
329 static void encode_sattr(struct xdr_stream *xdr, const struct iattr *attr)
333 p = xdr_reserve_space(xdr, NFS_sattr_sz << 2);
335 if (attr->ia_valid & ATTR_MODE)
336 *p++ = cpu_to_be32(attr->ia_mode);
338 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
339 if (attr->ia_valid & ATTR_UID)
340 *p++ = cpu_to_be32(attr->ia_uid);
342 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
343 if (attr->ia_valid & ATTR_GID)
344 *p++ = cpu_to_be32(attr->ia_gid);
346 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
347 if (attr->ia_valid & ATTR_SIZE)
348 *p++ = cpu_to_be32((u32)attr->ia_size);
350 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
352 if (attr->ia_valid & ATTR_ATIME_SET)
353 p = xdr_encode_time(p, &attr->ia_atime);
354 else if (attr->ia_valid & ATTR_ATIME)
355 p = xdr_encode_current_server_time(p, &attr->ia_atime);
357 p = xdr_time_not_set(p);
358 if (attr->ia_valid & ATTR_MTIME_SET)
359 xdr_encode_time(p, &attr->ia_mtime);
360 else if (attr->ia_valid & ATTR_MTIME)
361 xdr_encode_current_server_time(p, &attr->ia_mtime);
369 * typedef string filename<MAXNAMLEN>;
371 static void encode_filename(struct xdr_stream *xdr,
372 const char *name, u32 length)
376 BUG_ON(length > NFS2_MAXNAMLEN);
377 p = xdr_reserve_space(xdr, 4 + length);
378 xdr_encode_opaque(p, name, length);
381 static int decode_filename_inline(struct xdr_stream *xdr,
382 const char **name, u32 *length)
387 p = xdr_inline_decode(xdr, 4);
388 if (unlikely(p == NULL))
390 count = be32_to_cpup(p);
391 if (count > NFS3_MAXNAMLEN)
392 goto out_nametoolong;
393 p = xdr_inline_decode(xdr, count);
394 if (unlikely(p == NULL))
396 *name = (const char *)p;
400 dprintk("NFS: returned filename too long: %u\n", count);
401 return -ENAMETOOLONG;
403 print_overflow_msg(__func__, xdr);
410 * typedef string path<MAXPATHLEN>;
412 static void encode_path(struct xdr_stream *xdr, struct page **pages, u32 length)
416 BUG_ON(length > NFS2_MAXPATHLEN);
417 p = xdr_reserve_space(xdr, 4);
418 *p = cpu_to_be32(length);
419 xdr_write_pages(xdr, pages, 0, length);
422 static int decode_path(struct xdr_stream *xdr)
428 p = xdr_inline_decode(xdr, 4);
429 if (unlikely(p == NULL))
431 length = be32_to_cpup(p);
432 if (unlikely(length >= xdr->buf->page_len || length > NFS_MAXPATHLEN))
434 hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
435 recvd = xdr->buf->len - hdrlen;
436 if (unlikely(length > recvd))
439 xdr_read_pages(xdr, length);
440 xdr_terminate_string(xdr->buf, length);
443 dprintk("NFS: returned pathname too long: %u\n", length);
444 return -ENAMETOOLONG;
446 dprintk("NFS: server cheating in pathname result: "
447 "length %u > received %u\n", length, recvd);
450 print_overflow_msg(__func__, xdr);
457 * union attrstat switch (stat status) {
464 static int decode_attrstat(struct xdr_stream *xdr, struct nfs_fattr *result)
466 enum nfs_stat status;
469 error = decode_stat(xdr, &status);
472 if (status != NFS_OK)
474 error = decode_fattr(xdr, result);
478 return nfs_stat_to_errno(status);
489 static void encode_diropargs(struct xdr_stream *xdr, const struct nfs_fh *fh,
490 const char *name, u32 length)
492 encode_fhandle(xdr, fh);
493 encode_filename(xdr, name, length);
499 * union diropres switch (stat status) {
509 static int decode_diropok(struct xdr_stream *xdr, struct nfs_diropok *result)
513 error = decode_fhandle(xdr, result->fh);
516 error = decode_fattr(xdr, result->fattr);
521 static int decode_diropres(struct xdr_stream *xdr, struct nfs_diropok *result)
523 enum nfs_stat status;
526 error = decode_stat(xdr, &status);
529 if (status != NFS_OK)
531 error = decode_diropok(xdr, result);
535 return nfs_stat_to_errno(status);
540 * NFSv2 XDR encode functions
542 * NFSv2 argument types are defined in section 2.2 of RFC 1094:
543 * "NFS: Network File System Protocol Specification".
546 static int nfs2_xdr_enc_fhandle(struct rpc_rqst *req, __be32 *p,
547 const struct nfs_fh *fh)
549 struct xdr_stream xdr;
551 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
552 encode_fhandle(&xdr, fh);
564 static int nfs2_xdr_enc_sattrargs(struct rpc_rqst *req, __be32 *p,
565 const struct nfs_sattrargs *args)
567 struct xdr_stream xdr;
569 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
570 encode_fhandle(&xdr, args->fh);
571 encode_sattr(&xdr, args->sattr);
575 static int nfs2_xdr_enc_diropargs(struct rpc_rqst *req, __be32 *p,
576 const struct nfs_diropargs *args)
578 struct xdr_stream xdr;
580 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
581 encode_diropargs(&xdr, args->fh, args->name, args->len);
585 static int nfs2_xdr_enc_readlinkargs(struct rpc_rqst *req, __be32 *p,
586 const struct nfs_readlinkargs *args)
588 struct xdr_stream xdr;
590 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
591 encode_fhandle(&xdr, args->fh);
592 prepare_reply_buffer(req, args->pages, args->pgbase,
593 args->pglen, NFS_readlinkres_sz);
604 * unsigned totalcount;
607 static void encode_readargs(struct xdr_stream *xdr,
608 const struct nfs_readargs *args)
610 u32 offset = args->offset;
611 u32 count = args->count;
614 encode_fhandle(xdr, args->fh);
616 p = xdr_reserve_space(xdr, 4 + 4 + 4);
617 *p++ = cpu_to_be32(offset);
618 *p++ = cpu_to_be32(count);
619 *p = cpu_to_be32(count);
622 static int nfs2_xdr_enc_readargs(struct rpc_rqst *req, __be32 *p,
623 const struct nfs_readargs *args)
625 struct xdr_stream xdr;
627 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
628 encode_readargs(&xdr, args);
629 prepare_reply_buffer(req, args->pages, args->pgbase,
630 args->count, NFS_readres_sz);
631 req->rq_rcv_buf.flags |= XDRBUF_READ;
640 * unsigned beginoffset;
642 * unsigned totalcount;
646 static void encode_writeargs(struct xdr_stream *xdr,
647 const struct nfs_writeargs *args)
649 u32 offset = args->offset;
650 u32 count = args->count;
653 encode_fhandle(xdr, args->fh);
655 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
656 *p++ = cpu_to_be32(offset);
657 *p++ = cpu_to_be32(offset);
658 *p++ = cpu_to_be32(count);
661 *p = cpu_to_be32(count);
662 xdr_write_pages(xdr, args->pages, args->pgbase, count);
665 static int nfs2_xdr_enc_writeargs(struct rpc_rqst *req, __be32 *p,
666 const struct nfs_writeargs *args)
668 struct xdr_stream xdr;
670 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
671 encode_writeargs(&xdr, args);
672 xdr.buf->flags |= XDRBUF_WRITE;
679 * struct createargs {
684 static int nfs2_xdr_enc_createargs(struct rpc_rqst *req, __be32 *p,
685 const struct nfs_createargs *args)
687 struct xdr_stream xdr;
689 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
690 encode_diropargs(&xdr, args->fh, args->name, args->len);
691 encode_sattr(&xdr, args->sattr);
695 static int nfs2_xdr_enc_removeargs(struct rpc_rqst *req, __be32 *p,
696 const struct nfs_removeargs *args)
698 struct xdr_stream xdr;
700 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
701 encode_diropargs(&xdr, args->fh, args->name.name, args->name.len);
708 * struct renameargs {
713 static int nfs2_xdr_enc_renameargs(struct rpc_rqst *req, __be32 *p,
714 const struct nfs_renameargs *args)
716 const struct qstr *old = args->old_name;
717 const struct qstr *new = args->new_name;
718 struct xdr_stream xdr;
720 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
721 encode_diropargs(&xdr, args->old_dir, old->name, old->len);
722 encode_diropargs(&xdr, args->new_dir, new->name, new->len);
734 static int nfs2_xdr_enc_linkargs(struct rpc_rqst *req, __be32 *p,
735 const struct nfs_linkargs *args)
737 struct xdr_stream xdr;
739 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
740 encode_fhandle(&xdr, args->fromfh);
741 encode_diropargs(&xdr, args->tofh, args->toname, args->tolen);
746 * 2.2.14. symlinkargs
748 * struct symlinkargs {
754 static int nfs2_xdr_enc_symlinkargs(struct rpc_rqst *req, __be32 *p,
755 const struct nfs_symlinkargs *args)
757 struct xdr_stream xdr;
759 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
760 encode_diropargs(&xdr, args->fromfh, args->fromname, args->fromlen);
761 encode_path(&xdr, args->pages, args->pathlen);
762 encode_sattr(&xdr, args->sattr);
767 * 2.2.17. readdirargs
769 * struct readdirargs {
775 static void encode_readdirargs(struct xdr_stream *xdr,
776 const struct nfs_readdirargs *args)
780 encode_fhandle(xdr, args->fh);
782 p = xdr_reserve_space(xdr, 4 + 4);
783 *p++ = cpu_to_be32(args->cookie);
784 *p = cpu_to_be32(args->count);
787 static int nfs2_xdr_enc_readdirargs(struct rpc_rqst *req, __be32 *p,
788 const struct nfs_readdirargs *args)
790 struct xdr_stream xdr;
792 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
793 encode_readdirargs(&xdr, args);
794 prepare_reply_buffer(req, args->pages, 0,
795 args->count, NFS_readdirres_sz);
800 * NFSv2 XDR decode functions
802 * NFSv2 result types are defined in section 2.2 of RFC 1094:
803 * "NFS: Network File System Protocol Specification".
806 static int nfs2_xdr_dec_stat(struct rpc_rqst *req, __be32 *p,
809 struct xdr_stream xdr;
810 enum nfs_stat status;
813 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
814 error = decode_stat(&xdr, &status);
817 if (status != NFS_OK)
822 return nfs_stat_to_errno(status);
825 static int nfs2_xdr_dec_attrstat(struct rpc_rqst *req, __be32 *p,
826 struct nfs_fattr *result)
828 struct xdr_stream xdr;
830 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
831 return decode_attrstat(&xdr, result);
834 static int nfs2_xdr_dec_diropres(struct rpc_rqst *req, __be32 *p,
835 struct nfs_diropok *result)
837 struct xdr_stream xdr;
839 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
840 return decode_diropres(&xdr, result);
846 * union readlinkres switch (stat status) {
853 static int nfs2_xdr_dec_readlinkres(struct rpc_rqst *req, __be32 *p,
856 struct xdr_stream xdr;
857 enum nfs_stat status;
860 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
861 error = decode_stat(&xdr, &status);
864 if (status != NFS_OK)
866 error = decode_path(&xdr);
870 return nfs_stat_to_errno(status);
876 * union readres switch (stat status) {
884 static int nfs2_xdr_dec_readres(struct rpc_rqst *req, __be32 *p,
885 struct nfs_readres *result)
887 struct xdr_stream xdr;
888 enum nfs_stat status;
891 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
892 error = decode_stat(&xdr, &status);
895 if (status != NFS_OK)
897 error = decode_fattr(&xdr, result->fattr);
900 error = decode_nfsdata(&xdr, result);
904 return nfs_stat_to_errno(status);
907 static int nfs2_xdr_dec_writeres(struct rpc_rqst *req, __be32 *p,
908 struct nfs_writeres *result)
910 struct xdr_stream xdr;
912 /* All NFSv2 writes are "file sync" writes */
913 result->verf->committed = NFS_FILE_SYNC;
915 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
916 return decode_attrstat(&xdr, result->fattr);
920 * nfs2_decode_dirent - Decode a single NFSv2 directory entry stored in
921 * the local page cache.
922 * @xdr: XDR stream where entry resides
923 * @entry: buffer to fill in with entry data
924 * @server: nfs_server data for this directory
925 * @plus: boolean indicating whether this should be a readdirplus entry
927 * Returns the position of the next item in the buffer, or an ERR_PTR.
929 * This function is not invoked during READDIR reply decoding, but
930 * rather whenever an application invokes the getdents(2) system call
931 * on a directory already in our cache.
942 __be32 *nfs2_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
943 struct nfs_server *server, int plus)
948 p = xdr_inline_decode(xdr, 4);
949 if (unlikely(p == NULL))
951 if (*p++ == xdr_zero) {
952 p = xdr_inline_decode(xdr, 4);
953 if (unlikely(p == NULL))
955 if (*p++ == xdr_zero)
956 return ERR_PTR(-EAGAIN);
958 return ERR_PTR(-EBADCOOKIE);
961 p = xdr_inline_decode(xdr, 4);
962 if (unlikely(p == NULL))
964 entry->ino = be32_to_cpup(p);
966 error = decode_filename_inline(xdr, &entry->name, &entry->len);
968 return ERR_PTR(error);
971 * The type (size and byte order) of nfscookie isn't defined in
972 * RFC 1094. This implementation assumes that it's an XDR uint32.
974 entry->prev_cookie = entry->cookie;
975 p = xdr_inline_decode(xdr, 4);
976 if (unlikely(p == NULL))
978 entry->cookie = be32_to_cpup(p);
980 entry->d_type = DT_UNKNOWN;
982 /* Peek at the next entry to see if we're at EOD */
983 p = xdr_inline_peek(xdr, 4 + 4);
986 entry->eof = (p[0] == xdr_zero) && (p[1] != xdr_zero);
990 print_overflow_msg(__func__, xdr);
991 return ERR_PTR(-EAGAIN);
997 * union readdirres switch (stat status) {
1007 * Read the directory contents into the page cache, but don't
1008 * touch them. The actual decoding is done by nfs2_decode_dirent()
1009 * during subsequent nfs_readdir() calls.
1011 static int decode_readdirok(struct xdr_stream *xdr)
1016 pglen = xdr->buf->page_len;
1017 hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
1018 recvd = xdr->buf->len - hdrlen;
1019 if (unlikely(pglen > recvd))
1022 xdr_read_pages(xdr, pglen);
1025 dprintk("NFS: server cheating in readdir result: "
1026 "pglen %u > recvd %u\n", pglen, recvd);
1031 static int nfs2_xdr_dec_readdirres(struct rpc_rqst *req, __be32 *p,
1034 struct xdr_stream xdr;
1035 enum nfs_stat status;
1038 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
1039 error = decode_stat(&xdr, &status);
1040 if (unlikely(error))
1042 if (status != NFS_OK)
1044 error = decode_readdirok(&xdr);
1048 return nfs_stat_to_errno(status);
1054 * union statfsres (stat status) {
1067 static int decode_info(struct xdr_stream *xdr, struct nfs2_fsstat *result)
1071 p = xdr_inline_decode(xdr, NFS_info_sz << 2);
1072 if (unlikely(p == NULL))
1074 result->tsize = be32_to_cpup(p++);
1075 result->bsize = be32_to_cpup(p++);
1076 result->blocks = be32_to_cpup(p++);
1077 result->bfree = be32_to_cpup(p++);
1078 result->bavail = be32_to_cpup(p);
1081 print_overflow_msg(__func__, xdr);
1085 static int nfs2_xdr_dec_statfsres(struct rpc_rqst *req, __be32 *p,
1086 struct nfs2_fsstat *result)
1088 struct xdr_stream xdr;
1089 enum nfs_stat status;
1092 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
1093 error = decode_stat(&xdr, &status);
1094 if (unlikely(error))
1096 if (status != NFS_OK)
1098 error = decode_info(&xdr, result);
1102 return nfs_stat_to_errno(status);
1107 * We need to translate between nfs status return values and
1108 * the local errno values which may not be the same.
1110 static const struct {
1115 { NFSERR_PERM, -EPERM },
1116 { NFSERR_NOENT, -ENOENT },
1117 { NFSERR_IO, -errno_NFSERR_IO},
1118 { NFSERR_NXIO, -ENXIO },
1119 /* { NFSERR_EAGAIN, -EAGAIN }, */
1120 { NFSERR_ACCES, -EACCES },
1121 { NFSERR_EXIST, -EEXIST },
1122 { NFSERR_XDEV, -EXDEV },
1123 { NFSERR_NODEV, -ENODEV },
1124 { NFSERR_NOTDIR, -ENOTDIR },
1125 { NFSERR_ISDIR, -EISDIR },
1126 { NFSERR_INVAL, -EINVAL },
1127 { NFSERR_FBIG, -EFBIG },
1128 { NFSERR_NOSPC, -ENOSPC },
1129 { NFSERR_ROFS, -EROFS },
1130 { NFSERR_MLINK, -EMLINK },
1131 { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
1132 { NFSERR_NOTEMPTY, -ENOTEMPTY },
1133 { NFSERR_DQUOT, -EDQUOT },
1134 { NFSERR_STALE, -ESTALE },
1135 { NFSERR_REMOTE, -EREMOTE },
1137 { NFSERR_WFLUSH, -EWFLUSH },
1139 { NFSERR_BADHANDLE, -EBADHANDLE },
1140 { NFSERR_NOT_SYNC, -ENOTSYNC },
1141 { NFSERR_BAD_COOKIE, -EBADCOOKIE },
1142 { NFSERR_NOTSUPP, -ENOTSUPP },
1143 { NFSERR_TOOSMALL, -ETOOSMALL },
1144 { NFSERR_SERVERFAULT, -EREMOTEIO },
1145 { NFSERR_BADTYPE, -EBADTYPE },
1146 { NFSERR_JUKEBOX, -EJUKEBOX },
1151 * nfs_stat_to_errno - convert an NFS status code to a local errno
1152 * @status: NFS status code to convert
1154 * Returns a local errno value, or -EIO if the NFS status code is
1155 * not recognized. This function is used jointly by NFSv2 and NFSv3.
1157 int nfs_stat_to_errno(enum nfs_stat status)
1161 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
1162 if (nfs_errtbl[i].stat == (int)status)
1163 return nfs_errtbl[i].errno;
1165 dprintk("NFS: Unrecognized nfs status value: %u\n", status);
1166 return nfs_errtbl[i].errno;
1169 #define PROC(proc, argtype, restype, timer) \
1170 [NFSPROC_##proc] = { \
1171 .p_proc = NFSPROC_##proc, \
1172 .p_encode = (kxdrproc_t)nfs2_xdr_enc_##argtype, \
1173 .p_decode = (kxdrproc_t)nfs2_xdr_dec_##restype, \
1174 .p_arglen = NFS_##argtype##_sz, \
1175 .p_replen = NFS_##restype##_sz, \
1177 .p_statidx = NFSPROC_##proc, \
1180 struct rpc_procinfo nfs_procedures[] = {
1181 PROC(GETATTR, fhandle, attrstat, 1),
1182 PROC(SETATTR, sattrargs, attrstat, 0),
1183 PROC(LOOKUP, diropargs, diropres, 2),
1184 PROC(READLINK, readlinkargs, readlinkres, 3),
1185 PROC(READ, readargs, readres, 3),
1186 PROC(WRITE, writeargs, writeres, 4),
1187 PROC(CREATE, createargs, diropres, 0),
1188 PROC(REMOVE, removeargs, stat, 0),
1189 PROC(RENAME, renameargs, stat, 0),
1190 PROC(LINK, linkargs, stat, 0),
1191 PROC(SYMLINK, symlinkargs, stat, 0),
1192 PROC(MKDIR, createargs, diropres, 0),
1193 PROC(RMDIR, diropargs, stat, 0),
1194 PROC(READDIR, readdirargs, readdirres, 3),
1195 PROC(STATFS, fhandle, statfsres, 0),
1198 struct rpc_version nfs_version2 = {
1200 .nrprocs = ARRAY_SIZE(nfs_procedures),
1201 .procs = nfs_procedures