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)
65 * Common NFS XDR functions as inlines
67 static inline __be32 *
68 xdr_encode_fhandle(__be32 *p, const struct nfs_fh *fhandle)
70 memcpy(p, fhandle->data, NFS2_FHSIZE);
71 return p + XDR_QUADLEN(NFS2_FHSIZE);
74 static inline __be32 *
75 xdr_decode_fhandle(__be32 *p, struct nfs_fh *fhandle)
77 /* NFSv2 handles have a fixed length */
78 fhandle->size = NFS2_FHSIZE;
79 memcpy(fhandle->data, p, NFS2_FHSIZE);
80 return p + XDR_QUADLEN(NFS2_FHSIZE);
84 xdr_encode_time(__be32 *p, struct timespec *timep)
86 *p++ = htonl(timep->tv_sec);
87 /* Convert nanoseconds into microseconds */
88 *p++ = htonl(timep->tv_nsec ? timep->tv_nsec / 1000 : 0);
93 xdr_encode_current_server_time(__be32 *p, struct timespec *timep)
96 * Passing the invalid value useconds=1000000 is a
97 * Sun convention for "set to current server time".
98 * It's needed to make permissions checks for the
99 * "touch" program across v2 mounts to Solaris and
100 * Irix boxes work correctly. See description of
101 * sattr in section 6.1 of "NFS Illustrated" by
102 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
104 *p++ = htonl(timep->tv_sec);
105 *p++ = htonl(1000000);
109 static inline __be32*
110 xdr_decode_time(__be32 *p, struct timespec *timep)
112 timep->tv_sec = ntohl(*p++);
113 /* Convert microseconds into nanoseconds */
114 timep->tv_nsec = ntohl(*p++) * 1000;
119 xdr_decode_fattr(__be32 *p, struct nfs_fattr *fattr)
123 fattr->mode = ntohl(*p++);
124 fattr->nlink = ntohl(*p++);
125 fattr->uid = ntohl(*p++);
126 fattr->gid = ntohl(*p++);
127 fattr->size = ntohl(*p++);
128 fattr->du.nfs2.blocksize = ntohl(*p++);
130 fattr->du.nfs2.blocks = ntohl(*p++);
131 fattr->fsid.major = ntohl(*p++);
132 fattr->fsid.minor = 0;
133 fattr->fileid = ntohl(*p++);
134 p = xdr_decode_time(p, &fattr->atime);
135 p = xdr_decode_time(p, &fattr->mtime);
136 p = xdr_decode_time(p, &fattr->ctime);
137 fattr->valid |= NFS_ATTR_FATTR_V2;
138 fattr->rdev = new_decode_dev(rdev);
139 if (type == NFCHR && rdev == NFS2_FIFO_DEV) {
140 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
146 static inline __be32 *
147 xdr_encode_sattr(__be32 *p, struct iattr *attr)
149 const __be32 not_set = __constant_htonl(0xFFFFFFFF);
151 *p++ = (attr->ia_valid & ATTR_MODE) ? htonl(attr->ia_mode) : not_set;
152 *p++ = (attr->ia_valid & ATTR_UID) ? htonl(attr->ia_uid) : not_set;
153 *p++ = (attr->ia_valid & ATTR_GID) ? htonl(attr->ia_gid) : not_set;
154 *p++ = (attr->ia_valid & ATTR_SIZE) ? htonl(attr->ia_size) : not_set;
156 if (attr->ia_valid & ATTR_ATIME_SET) {
157 p = xdr_encode_time(p, &attr->ia_atime);
158 } else if (attr->ia_valid & ATTR_ATIME) {
159 p = xdr_encode_current_server_time(p, &attr->ia_atime);
165 if (attr->ia_valid & ATTR_MTIME_SET) {
166 p = xdr_encode_time(p, &attr->ia_mtime);
167 } else if (attr->ia_valid & ATTR_MTIME) {
168 p = xdr_encode_current_server_time(p, &attr->ia_mtime);
177 * NFS encode functions
180 * Encode file handle argument
181 * GETATTR, READLINK, STATFS
184 nfs_xdr_fhandle(struct rpc_rqst *req, __be32 *p, struct nfs_fh *fh)
186 p = xdr_encode_fhandle(p, fh);
187 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
192 * Encode SETATTR arguments
195 nfs_xdr_sattrargs(struct rpc_rqst *req, __be32 *p, struct nfs_sattrargs *args)
197 p = xdr_encode_fhandle(p, args->fh);
198 p = xdr_encode_sattr(p, args->sattr);
199 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
204 * Encode directory ops argument
208 nfs_xdr_diropargs(struct rpc_rqst *req, __be32 *p, struct nfs_diropargs *args)
210 p = xdr_encode_fhandle(p, args->fh);
211 p = xdr_encode_array(p, args->name, args->len);
212 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
217 * Encode REMOVE argument
220 nfs_xdr_removeargs(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
222 p = xdr_encode_fhandle(p, args->fh);
223 p = xdr_encode_array(p, args->name.name, args->name.len);
224 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
229 * Arguments to a READ call. Since we read data directly into the page
230 * cache, we also set up the reply iovec here so that iov[1] points
231 * exactly to the page we want to fetch.
234 nfs_xdr_readargs(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
236 struct rpc_auth *auth = req->rq_cred->cr_auth;
238 u32 offset = (u32)args->offset;
239 u32 count = args->count;
241 p = xdr_encode_fhandle(p, args->fh);
242 *p++ = htonl(offset);
245 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
247 /* Inline the page array */
248 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readres_sz) << 2;
249 xdr_inline_pages(&req->rq_rcv_buf, replen,
250 args->pages, args->pgbase, count);
251 req->rq_rcv_buf.flags |= XDRBUF_READ;
259 nfs_xdr_readres(struct rpc_rqst *req, __be32 *p, struct nfs_readres *res)
261 struct kvec *iov = req->rq_rcv_buf.head;
266 if ((status = ntohl(*p++)))
267 return nfs_stat_to_errno(status);
268 p = xdr_decode_fattr(p, res->fattr);
272 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
273 if (iov->iov_len < hdrlen) {
274 dprintk("NFS: READ reply header overflowed:"
275 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
276 return -errno_NFSERR_IO;
277 } else if (iov->iov_len != hdrlen) {
278 dprintk("NFS: READ header is short. iovec will be shifted.\n");
279 xdr_shift_buf(&req->rq_rcv_buf, iov->iov_len - hdrlen);
282 recvd = req->rq_rcv_buf.len - hdrlen;
284 dprintk("NFS: server cheating in read reply: "
285 "count %u > recvd %u\n", count, recvd);
289 dprintk("RPC: readres OK count %u\n", count);
290 if (count < res->count)
298 * Write arguments. Splice the buffer to be written into the iovec.
301 nfs_xdr_writeargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
303 struct xdr_buf *sndbuf = &req->rq_snd_buf;
304 u32 offset = (u32)args->offset;
305 u32 count = args->count;
307 p = xdr_encode_fhandle(p, args->fh);
308 *p++ = htonl(offset);
309 *p++ = htonl(offset);
312 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
314 /* Copy the page array */
315 xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
316 sndbuf->flags |= XDRBUF_WRITE;
321 * Encode create arguments
325 nfs_xdr_createargs(struct rpc_rqst *req, __be32 *p, struct nfs_createargs *args)
327 p = xdr_encode_fhandle(p, args->fh);
328 p = xdr_encode_array(p, args->name, args->len);
329 p = xdr_encode_sattr(p, args->sattr);
330 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
335 * Encode RENAME arguments
338 nfs_xdr_renameargs(struct rpc_rqst *req, __be32 *p, struct nfs_renameargs *args)
340 p = xdr_encode_fhandle(p, args->old_dir);
341 p = xdr_encode_array(p, args->old_name->name, args->old_name->len);
342 p = xdr_encode_fhandle(p, args->new_dir);
343 p = xdr_encode_array(p, args->new_name->name, args->new_name->len);
344 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
349 * Encode LINK arguments
352 nfs_xdr_linkargs(struct rpc_rqst *req, __be32 *p, struct nfs_linkargs *args)
354 p = xdr_encode_fhandle(p, args->fromfh);
355 p = xdr_encode_fhandle(p, args->tofh);
356 p = xdr_encode_array(p, args->toname, args->tolen);
357 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
362 * Encode SYMLINK arguments
365 nfs_xdr_symlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_symlinkargs *args)
367 struct xdr_buf *sndbuf = &req->rq_snd_buf;
370 p = xdr_encode_fhandle(p, args->fromfh);
371 p = xdr_encode_array(p, args->fromname, args->fromlen);
372 *p++ = htonl(args->pathlen);
373 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
375 xdr_encode_pages(sndbuf, args->pages, 0, args->pathlen);
378 * xdr_encode_pages may have added a few bytes to ensure the
379 * pathname ends on a 4-byte boundary. Start encoding the
380 * attributes after the pad bytes.
382 pad = sndbuf->tail->iov_len;
385 p = xdr_encode_sattr(p, args->sattr);
386 sndbuf->len += xdr_adjust_iovec(sndbuf->tail, p) - pad;
391 * Encode arguments to readdir call
394 nfs_xdr_readdirargs(struct rpc_rqst *req, __be32 *p, struct nfs_readdirargs *args)
396 struct rpc_auth *auth = req->rq_cred->cr_auth;
398 u32 count = args->count;
400 p = xdr_encode_fhandle(p, args->fh);
401 *p++ = htonl(args->cookie);
402 *p++ = htonl(count); /* see above */
403 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
405 /* Inline the page array */
406 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readdirres_sz) << 2;
407 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
412 * Decode the result of a readdir call.
413 * We're not really decoding anymore, we just leave the buffer untouched
414 * and only check that it is syntactically correct.
415 * The real decoding happens in nfs_decode_entry below, called directly
416 * from nfs_readdir for each entry.
419 nfs_xdr_readdirres(struct rpc_rqst *req, __be32 *p, void *dummy)
421 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
422 struct kvec *iov = rcvbuf->head;
425 unsigned int pglen, recvd;
428 if ((status = ntohl(*p++)))
429 return nfs_stat_to_errno(status);
431 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
432 if (iov->iov_len < hdrlen) {
433 dprintk("NFS: READDIR reply header overflowed:"
434 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
435 return -errno_NFSERR_IO;
436 } else if (iov->iov_len != hdrlen) {
437 dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
438 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
441 pglen = rcvbuf->page_len;
442 recvd = rcvbuf->len - hdrlen;
445 page = rcvbuf->pages;
449 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
451 dprintk("nfs: %s: prematurely hit end of receive buffer. "
452 "Remaining buffer length is %tu words.\n",
453 func, xdr->end - xdr->p);
457 nfs_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry, struct nfs_server *server, int plus)
460 p = xdr_inline_decode(xdr, 4);
464 p = xdr_inline_decode(xdr, 4);
468 return ERR_PTR(-EAGAIN);
470 return ERR_PTR(-EBADCOOKIE);
473 p = xdr_inline_decode(xdr, 8);
477 entry->ino = ntohl(*p++);
478 entry->len = ntohl(*p++);
480 p = xdr_inline_decode(xdr, entry->len + 4);
483 entry->name = (const char *) p;
484 p += XDR_QUADLEN(entry->len);
485 entry->prev_cookie = entry->cookie;
486 entry->cookie = ntohl(*p++);
488 entry->d_type = DT_UNKNOWN;
493 print_overflow_msg(__func__, xdr);
494 return ERR_PTR(-EAGAIN);
498 * NFS XDR decode functions
501 * Decode simple status reply
504 nfs_xdr_stat(struct rpc_rqst *req, __be32 *p, void *dummy)
508 if ((status = ntohl(*p++)) != 0)
509 status = nfs_stat_to_errno(status);
514 * Decode attrstat reply
515 * GETATTR, SETATTR, WRITE
518 nfs_xdr_attrstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
522 if ((status = ntohl(*p++)))
523 return nfs_stat_to_errno(status);
524 xdr_decode_fattr(p, fattr);
529 * Decode diropres reply
530 * LOOKUP, CREATE, MKDIR
533 nfs_xdr_diropres(struct rpc_rqst *req, __be32 *p, struct nfs_diropok *res)
537 if ((status = ntohl(*p++)))
538 return nfs_stat_to_errno(status);
539 p = xdr_decode_fhandle(p, res->fh);
540 xdr_decode_fattr(p, res->fattr);
545 * Encode READLINK args
548 nfs_xdr_readlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_readlinkargs *args)
550 struct rpc_auth *auth = req->rq_cred->cr_auth;
553 p = xdr_encode_fhandle(p, args->fh);
554 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
556 /* Inline the page array */
557 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readlinkres_sz) << 2;
558 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
563 * Decode READLINK reply
566 nfs_xdr_readlinkres(struct rpc_rqst *req, __be32 *p, void *dummy)
568 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
569 struct kvec *iov = rcvbuf->head;
574 if ((status = ntohl(*p++)))
575 return nfs_stat_to_errno(status);
576 /* Convert length of symlink */
578 if (len >= rcvbuf->page_len) {
579 dprintk("nfs: server returned giant symlink!\n");
580 return -ENAMETOOLONG;
582 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
583 if (iov->iov_len < hdrlen) {
584 dprintk("NFS: READLINK reply header overflowed:"
585 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
586 return -errno_NFSERR_IO;
587 } else if (iov->iov_len != hdrlen) {
588 dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
589 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
591 recvd = req->rq_rcv_buf.len - hdrlen;
593 dprintk("NFS: server cheating in readlink reply: "
594 "count %u > recvd %u\n", len, recvd);
598 xdr_terminate_string(rcvbuf, len);
606 nfs_xdr_writeres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
608 res->verf->committed = NFS_FILE_SYNC;
609 return nfs_xdr_attrstat(req, p, res->fattr);
613 * Decode STATFS reply
616 nfs_xdr_statfsres(struct rpc_rqst *req, __be32 *p, struct nfs2_fsstat *res)
620 if ((status = ntohl(*p++)))
621 return nfs_stat_to_errno(status);
623 res->tsize = ntohl(*p++);
624 res->bsize = ntohl(*p++);
625 res->blocks = ntohl(*p++);
626 res->bfree = ntohl(*p++);
627 res->bavail = ntohl(*p++);
632 * We need to translate between nfs status return values and
633 * the local errno values which may not be the same.
640 { NFSERR_PERM, -EPERM },
641 { NFSERR_NOENT, -ENOENT },
642 { NFSERR_IO, -errno_NFSERR_IO},
643 { NFSERR_NXIO, -ENXIO },
644 /* { NFSERR_EAGAIN, -EAGAIN }, */
645 { NFSERR_ACCES, -EACCES },
646 { NFSERR_EXIST, -EEXIST },
647 { NFSERR_XDEV, -EXDEV },
648 { NFSERR_NODEV, -ENODEV },
649 { NFSERR_NOTDIR, -ENOTDIR },
650 { NFSERR_ISDIR, -EISDIR },
651 { NFSERR_INVAL, -EINVAL },
652 { NFSERR_FBIG, -EFBIG },
653 { NFSERR_NOSPC, -ENOSPC },
654 { NFSERR_ROFS, -EROFS },
655 { NFSERR_MLINK, -EMLINK },
656 { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
657 { NFSERR_NOTEMPTY, -ENOTEMPTY },
658 { NFSERR_DQUOT, -EDQUOT },
659 { NFSERR_STALE, -ESTALE },
660 { NFSERR_REMOTE, -EREMOTE },
662 { NFSERR_WFLUSH, -EWFLUSH },
664 { NFSERR_BADHANDLE, -EBADHANDLE },
665 { NFSERR_NOT_SYNC, -ENOTSYNC },
666 { NFSERR_BAD_COOKIE, -EBADCOOKIE },
667 { NFSERR_NOTSUPP, -ENOTSUPP },
668 { NFSERR_TOOSMALL, -ETOOSMALL },
669 { NFSERR_SERVERFAULT, -EREMOTEIO },
670 { NFSERR_BADTYPE, -EBADTYPE },
671 { NFSERR_JUKEBOX, -EJUKEBOX },
676 * Convert an NFS error code to a local one.
677 * This one is used jointly by NFSv2 and NFSv3.
680 nfs_stat_to_errno(int stat)
684 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
685 if (nfs_errtbl[i].stat == stat)
686 return nfs_errtbl[i].errno;
688 dprintk("nfs_stat_to_errno: bad nfs status return value: %d\n", stat);
689 return nfs_errtbl[i].errno;
692 #define PROC(proc, argtype, restype, timer) \
693 [NFSPROC_##proc] = { \
694 .p_proc = NFSPROC_##proc, \
695 .p_encode = (kxdrproc_t) nfs_xdr_##argtype, \
696 .p_decode = (kxdrproc_t) nfs_xdr_##restype, \
697 .p_arglen = NFS_##argtype##_sz, \
698 .p_replen = NFS_##restype##_sz, \
700 .p_statidx = NFSPROC_##proc, \
703 struct rpc_procinfo nfs_procedures[] = {
704 PROC(GETATTR, fhandle, attrstat, 1),
705 PROC(SETATTR, sattrargs, attrstat, 0),
706 PROC(LOOKUP, diropargs, diropres, 2),
707 PROC(READLINK, readlinkargs, readlinkres, 3),
708 PROC(READ, readargs, readres, 3),
709 PROC(WRITE, writeargs, writeres, 4),
710 PROC(CREATE, createargs, diropres, 0),
711 PROC(REMOVE, removeargs, stat, 0),
712 PROC(RENAME, renameargs, stat, 0),
713 PROC(LINK, linkargs, stat, 0),
714 PROC(SYMLINK, symlinkargs, stat, 0),
715 PROC(MKDIR, createargs, diropres, 0),
716 PROC(RMDIR, diropargs, stat, 0),
717 PROC(READDIR, readdirargs, readdirres, 3),
718 PROC(STATFS, fhandle, statfsres, 0),
721 struct rpc_version nfs_version2 = {
723 .nrprocs = ARRAY_SIZE(nfs_procedures),
724 .procs = nfs_procedures