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/slab.h>
16 #include <linux/utsname.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
20 #include <linux/pagemap.h>
21 #include <linux/proc_fs.h>
22 #include <linux/sunrpc/clnt.h>
23 #include <linux/nfs.h>
24 #include <linux/nfs2.h>
25 #include <linux/nfs_fs.h>
27 #define NFSDBG_FACILITY NFSDBG_XDR
28 /* #define NFS_PARANOIA 1 */
30 extern int nfs_stat_to_errno(int stat);
32 /* Mapping from NFS error code to "errno" error code. */
33 #define errno_NFSERR_IO EIO
36 * Declare the space requirements for NFS arguments and replies as
37 * number of 32bit-words
39 #define NFS_fhandle_sz (8)
40 #define NFS_sattr_sz (8)
41 #define NFS_filename_sz (1+(NFS2_MAXNAMLEN>>2))
42 #define NFS_path_sz (1+(NFS2_MAXPATHLEN>>2))
43 #define NFS_fattr_sz (17)
44 #define NFS_info_sz (5)
45 #define NFS_entry_sz (NFS_filename_sz+3)
47 #define NFS_diropargs_sz (NFS_fhandle_sz+NFS_filename_sz)
48 #define NFS_sattrargs_sz (NFS_fhandle_sz+NFS_sattr_sz)
49 #define NFS_readlinkargs_sz (NFS_fhandle_sz)
50 #define NFS_readargs_sz (NFS_fhandle_sz+3)
51 #define NFS_writeargs_sz (NFS_fhandle_sz+4)
52 #define NFS_createargs_sz (NFS_diropargs_sz+NFS_sattr_sz)
53 #define NFS_renameargs_sz (NFS_diropargs_sz+NFS_diropargs_sz)
54 #define NFS_linkargs_sz (NFS_fhandle_sz+NFS_diropargs_sz)
55 #define NFS_symlinkargs_sz (NFS_diropargs_sz+NFS_path_sz+NFS_sattr_sz)
56 #define NFS_readdirargs_sz (NFS_fhandle_sz+2)
58 #define NFS_attrstat_sz (1+NFS_fattr_sz)
59 #define NFS_diropres_sz (1+NFS_fhandle_sz+NFS_fattr_sz)
60 #define NFS_readlinkres_sz (2)
61 #define NFS_readres_sz (1+NFS_fattr_sz+1)
62 #define NFS_writeres_sz (NFS_attrstat_sz)
63 #define NFS_stat_sz (1)
64 #define NFS_readdirres_sz (1)
65 #define NFS_statfsres_sz (1+NFS_info_sz)
68 * Common NFS XDR functions as inlines
71 xdr_encode_fhandle(u32 *p, struct nfs_fh *fhandle)
73 memcpy(p, fhandle->data, NFS2_FHSIZE);
74 return p + XDR_QUADLEN(NFS2_FHSIZE);
78 xdr_decode_fhandle(u32 *p, struct nfs_fh *fhandle)
80 /* NFSv2 handles have a fixed length */
81 fhandle->size = NFS2_FHSIZE;
82 memcpy(fhandle->data, p, NFS2_FHSIZE);
83 return p + XDR_QUADLEN(NFS2_FHSIZE);
87 xdr_encode_time(u32 *p, struct timespec *timep)
89 *p++ = htonl(timep->tv_sec);
90 /* Convert nanoseconds into microseconds */
91 *p++ = htonl(timep->tv_nsec ? timep->tv_nsec / 1000 : 0);
96 xdr_encode_current_server_time(u32 *p, struct timespec *timep)
99 * Passing the invalid value useconds=1000000 is a
100 * Sun convention for "set to current server time".
101 * It's needed to make permissions checks for the
102 * "touch" program across v2 mounts to Solaris and
103 * Irix boxes work correctly. See description of
104 * sattr in section 6.1 of "NFS Illustrated" by
105 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
107 *p++ = htonl(timep->tv_sec);
108 *p++ = htonl(1000000);
113 xdr_decode_time(u32 *p, struct timespec *timep)
115 timep->tv_sec = ntohl(*p++);
116 /* Convert microseconds into nanoseconds */
117 timep->tv_nsec = ntohl(*p++) * 1000;
122 xdr_decode_fattr(u32 *p, struct nfs_fattr *fattr)
125 fattr->type = (enum nfs_ftype) ntohl(*p++);
126 fattr->mode = ntohl(*p++);
127 fattr->nlink = ntohl(*p++);
128 fattr->uid = ntohl(*p++);
129 fattr->gid = ntohl(*p++);
130 fattr->size = ntohl(*p++);
131 fattr->du.nfs2.blocksize = ntohl(*p++);
133 fattr->du.nfs2.blocks = ntohl(*p++);
134 fattr->fsid_u.nfs3 = ntohl(*p++);
135 fattr->fileid = ntohl(*p++);
136 p = xdr_decode_time(p, &fattr->atime);
137 p = xdr_decode_time(p, &fattr->mtime);
138 p = xdr_decode_time(p, &fattr->ctime);
139 fattr->valid |= NFS_ATTR_FATTR;
140 fattr->rdev = new_decode_dev(rdev);
141 if (fattr->type == NFCHR && rdev == NFS2_FIFO_DEV) {
142 fattr->type = NFFIFO;
143 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
149 #define SATTR(p, attr, flag, field) \
150 *p++ = (attr->ia_valid & flag) ? htonl(attr->field) : ~(u32) 0
152 xdr_encode_sattr(u32 *p, struct iattr *attr)
154 SATTR(p, attr, ATTR_MODE, ia_mode);
155 SATTR(p, attr, ATTR_UID, ia_uid);
156 SATTR(p, attr, ATTR_GID, ia_gid);
157 SATTR(p, attr, ATTR_SIZE, ia_size);
159 if (attr->ia_valid & ATTR_ATIME_SET) {
160 p = xdr_encode_time(p, &attr->ia_atime);
161 } else if (attr->ia_valid & ATTR_ATIME) {
162 p = xdr_encode_current_server_time(p, &attr->ia_atime);
168 if (attr->ia_valid & ATTR_MTIME_SET) {
169 p = xdr_encode_time(p, &attr->ia_mtime);
170 } else if (attr->ia_valid & ATTR_MTIME) {
171 p = xdr_encode_current_server_time(p, &attr->ia_mtime);
181 * NFS encode functions
184 * Encode file handle argument
185 * GETATTR, READLINK, STATFS
188 nfs_xdr_fhandle(struct rpc_rqst *req, u32 *p, struct nfs_fh *fh)
190 p = xdr_encode_fhandle(p, fh);
191 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
196 * Encode SETATTR arguments
199 nfs_xdr_sattrargs(struct rpc_rqst *req, u32 *p, struct nfs_sattrargs *args)
201 p = xdr_encode_fhandle(p, args->fh);
202 p = xdr_encode_sattr(p, args->sattr);
203 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
208 * Encode directory ops argument
209 * LOOKUP, REMOVE, RMDIR
212 nfs_xdr_diropargs(struct rpc_rqst *req, u32 *p, struct nfs_diropargs *args)
214 p = xdr_encode_fhandle(p, args->fh);
215 p = xdr_encode_array(p, args->name, args->len);
216 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
221 * Arguments to a READ call. Since we read data directly into the page
222 * cache, we also set up the reply iovec here so that iov[1] points
223 * exactly to the page we want to fetch.
226 nfs_xdr_readargs(struct rpc_rqst *req, u32 *p, struct nfs_readargs *args)
228 struct rpc_auth *auth = req->rq_task->tk_auth;
230 u32 offset = (u32)args->offset;
231 u32 count = args->count;
233 p = xdr_encode_fhandle(p, args->fh);
234 *p++ = htonl(offset);
237 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
239 /* Inline the page array */
240 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readres_sz) << 2;
241 xdr_inline_pages(&req->rq_rcv_buf, replen,
242 args->pages, args->pgbase, count);
250 nfs_xdr_readres(struct rpc_rqst *req, u32 *p, struct nfs_readres *res)
252 struct kvec *iov = req->rq_rcv_buf.head;
253 int status, count, recvd, hdrlen;
255 if ((status = ntohl(*p++)))
256 return -nfs_stat_to_errno(status);
257 p = xdr_decode_fattr(p, res->fattr);
261 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
262 if (iov->iov_len < hdrlen) {
263 printk(KERN_WARNING "NFS: READ reply header overflowed:"
264 "length %d > %Zu\n", hdrlen, iov->iov_len);
265 return -errno_NFSERR_IO;
266 } else if (iov->iov_len != hdrlen) {
267 dprintk("NFS: READ header is short. iovec will be shifted.\n");
268 xdr_shift_buf(&req->rq_rcv_buf, iov->iov_len - hdrlen);
271 recvd = req->rq_rcv_buf.len - hdrlen;
273 printk(KERN_WARNING "NFS: server cheating in read reply: "
274 "count %d > recvd %d\n", count, recvd);
278 dprintk("RPC: readres OK count %d\n", count);
279 if (count < res->count)
287 * Write arguments. Splice the buffer to be written into the iovec.
290 nfs_xdr_writeargs(struct rpc_rqst *req, u32 *p, struct nfs_writeargs *args)
292 struct xdr_buf *sndbuf = &req->rq_snd_buf;
293 u32 offset = (u32)args->offset;
294 u32 count = args->count;
296 p = xdr_encode_fhandle(p, args->fh);
297 *p++ = htonl(offset);
298 *p++ = htonl(offset);
301 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
303 /* Copy the page array */
304 xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
309 * Encode create arguments
313 nfs_xdr_createargs(struct rpc_rqst *req, u32 *p, struct nfs_createargs *args)
315 p = xdr_encode_fhandle(p, args->fh);
316 p = xdr_encode_array(p, args->name, args->len);
317 p = xdr_encode_sattr(p, args->sattr);
318 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
323 * Encode RENAME arguments
326 nfs_xdr_renameargs(struct rpc_rqst *req, u32 *p, struct nfs_renameargs *args)
328 p = xdr_encode_fhandle(p, args->fromfh);
329 p = xdr_encode_array(p, args->fromname, args->fromlen);
330 p = xdr_encode_fhandle(p, args->tofh);
331 p = xdr_encode_array(p, args->toname, args->tolen);
332 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
337 * Encode LINK arguments
340 nfs_xdr_linkargs(struct rpc_rqst *req, u32 *p, struct nfs_linkargs *args)
342 p = xdr_encode_fhandle(p, args->fromfh);
343 p = xdr_encode_fhandle(p, args->tofh);
344 p = xdr_encode_array(p, args->toname, args->tolen);
345 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
350 * Encode SYMLINK arguments
353 nfs_xdr_symlinkargs(struct rpc_rqst *req, u32 *p, struct nfs_symlinkargs *args)
355 p = xdr_encode_fhandle(p, args->fromfh);
356 p = xdr_encode_array(p, args->fromname, args->fromlen);
357 p = xdr_encode_array(p, args->topath, args->tolen);
358 p = xdr_encode_sattr(p, args->sattr);
359 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
364 * Encode arguments to readdir call
367 nfs_xdr_readdirargs(struct rpc_rqst *req, u32 *p, struct nfs_readdirargs *args)
369 struct rpc_task *task = req->rq_task;
370 struct rpc_auth *auth = task->tk_auth;
372 u32 count = args->count;
374 p = xdr_encode_fhandle(p, args->fh);
375 *p++ = htonl(args->cookie);
376 *p++ = htonl(count); /* see above */
377 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
379 /* Inline the page array */
380 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readdirres_sz) << 2;
381 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
386 * Decode the result of a readdir call.
387 * We're not really decoding anymore, we just leave the buffer untouched
388 * and only check that it is syntactically correct.
389 * The real decoding happens in nfs_decode_entry below, called directly
390 * from nfs_readdir for each entry.
393 nfs_xdr_readdirres(struct rpc_rqst *req, u32 *p, void *dummy)
395 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
396 struct kvec *iov = rcvbuf->head;
400 unsigned int len, pglen;
401 u32 *end, *entry, *kaddr;
403 if ((status = ntohl(*p++)))
404 return -nfs_stat_to_errno(status);
406 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
407 if (iov->iov_len < hdrlen) {
408 printk(KERN_WARNING "NFS: READDIR reply header overflowed:"
409 "length %d > %Zu\n", hdrlen, iov->iov_len);
410 return -errno_NFSERR_IO;
411 } else if (iov->iov_len != hdrlen) {
412 dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
413 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
416 pglen = rcvbuf->page_len;
417 recvd = rcvbuf->len - hdrlen;
420 page = rcvbuf->pages;
421 kaddr = p = (u32 *)kmap_atomic(*page, KM_USER0);
422 end = (u32 *)((char *)p + pglen);
424 for (nr = 0; *p++; nr++) {
429 p += XDR_QUADLEN(len) + 1; /* name plus cookie */
430 if (len > NFS2_MAXNAMLEN) {
431 printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)!\n",
439 if (!nr && (entry[0] != 0 || entry[1] == 0))
442 kunmap_atomic(kaddr, KM_USER0);
445 entry[0] = entry[1] = 0;
446 /* truncate listing ? */
448 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
453 nr = -errno_NFSERR_IO;
458 nfs_decode_dirent(u32 *p, struct nfs_entry *entry, int plus)
462 return ERR_PTR(-EAGAIN);
464 return ERR_PTR(-EBADCOOKIE);
467 entry->ino = ntohl(*p++);
468 entry->len = ntohl(*p++);
469 entry->name = (const char *) p;
470 p += XDR_QUADLEN(entry->len);
471 entry->prev_cookie = entry->cookie;
472 entry->cookie = ntohl(*p++);
473 entry->eof = !p[0] && p[1];
479 * NFS XDR decode functions
482 * Decode simple status reply
485 nfs_xdr_stat(struct rpc_rqst *req, u32 *p, void *dummy)
489 if ((status = ntohl(*p++)) != 0)
490 status = -nfs_stat_to_errno(status);
495 * Decode attrstat reply
496 * GETATTR, SETATTR, WRITE
499 nfs_xdr_attrstat(struct rpc_rqst *req, u32 *p, struct nfs_fattr *fattr)
503 if ((status = ntohl(*p++)))
504 return -nfs_stat_to_errno(status);
505 xdr_decode_fattr(p, fattr);
510 * Decode diropres reply
511 * LOOKUP, CREATE, MKDIR
514 nfs_xdr_diropres(struct rpc_rqst *req, u32 *p, struct nfs_diropok *res)
518 if ((status = ntohl(*p++)))
519 return -nfs_stat_to_errno(status);
520 p = xdr_decode_fhandle(p, res->fh);
521 xdr_decode_fattr(p, res->fattr);
526 * Encode READLINK args
529 nfs_xdr_readlinkargs(struct rpc_rqst *req, u32 *p, struct nfs_readlinkargs *args)
531 struct rpc_auth *auth = req->rq_task->tk_auth;
534 p = xdr_encode_fhandle(p, args->fh);
535 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
537 /* Inline the page array */
538 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readlinkres_sz) << 2;
539 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
544 * Decode READLINK reply
547 nfs_xdr_readlinkres(struct rpc_rqst *req, u32 *p, void *dummy)
549 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
550 struct kvec *iov = rcvbuf->head;
551 int hdrlen, len, recvd;
555 if ((status = ntohl(*p++)))
556 return -nfs_stat_to_errno(status);
557 /* Convert length of symlink */
559 if (len >= rcvbuf->page_len || len <= 0) {
560 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
561 return -ENAMETOOLONG;
563 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
564 if (iov->iov_len < hdrlen) {
565 printk(KERN_WARNING "NFS: READLINK reply header overflowed:"
566 "length %d > %Zu\n", hdrlen, iov->iov_len);
567 return -errno_NFSERR_IO;
568 } else if (iov->iov_len != hdrlen) {
569 dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
570 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
572 recvd = req->rq_rcv_buf.len - hdrlen;
574 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
575 "count %u > recvd %u\n", len, recvd);
579 /* NULL terminate the string we got */
580 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
581 kaddr[len+rcvbuf->page_base] = '\0';
582 kunmap_atomic(kaddr, KM_USER0);
590 nfs_xdr_writeres(struct rpc_rqst *req, u32 *p, struct nfs_writeres *res)
592 res->verf->committed = NFS_FILE_SYNC;
593 return nfs_xdr_attrstat(req, p, res->fattr);
597 * Decode STATFS reply
600 nfs_xdr_statfsres(struct rpc_rqst *req, u32 *p, struct nfs2_fsstat *res)
604 if ((status = ntohl(*p++)))
605 return -nfs_stat_to_errno(status);
607 res->tsize = ntohl(*p++);
608 res->bsize = ntohl(*p++);
609 res->blocks = ntohl(*p++);
610 res->bfree = ntohl(*p++);
611 res->bavail = ntohl(*p++);
616 * We need to translate between nfs status return values and
617 * the local errno values which may not be the same.
624 { NFSERR_PERM, EPERM },
625 { NFSERR_NOENT, ENOENT },
626 { NFSERR_IO, errno_NFSERR_IO },
627 { NFSERR_NXIO, ENXIO },
628 /* { NFSERR_EAGAIN, EAGAIN }, */
629 { NFSERR_ACCES, EACCES },
630 { NFSERR_EXIST, EEXIST },
631 { NFSERR_XDEV, EXDEV },
632 { NFSERR_NODEV, ENODEV },
633 { NFSERR_NOTDIR, ENOTDIR },
634 { NFSERR_ISDIR, EISDIR },
635 { NFSERR_INVAL, EINVAL },
636 { NFSERR_FBIG, EFBIG },
637 { NFSERR_NOSPC, ENOSPC },
638 { NFSERR_ROFS, EROFS },
639 { NFSERR_MLINK, EMLINK },
640 { NFSERR_NAMETOOLONG, ENAMETOOLONG },
641 { NFSERR_NOTEMPTY, ENOTEMPTY },
642 { NFSERR_DQUOT, EDQUOT },
643 { NFSERR_STALE, ESTALE },
644 { NFSERR_REMOTE, EREMOTE },
646 { NFSERR_WFLUSH, EWFLUSH },
648 { NFSERR_BADHANDLE, EBADHANDLE },
649 { NFSERR_NOT_SYNC, ENOTSYNC },
650 { NFSERR_BAD_COOKIE, EBADCOOKIE },
651 { NFSERR_NOTSUPP, ENOTSUPP },
652 { NFSERR_TOOSMALL, ETOOSMALL },
653 { NFSERR_SERVERFAULT, ESERVERFAULT },
654 { NFSERR_BADTYPE, EBADTYPE },
655 { NFSERR_JUKEBOX, EJUKEBOX },
660 * Convert an NFS error code to a local one.
661 * This one is used jointly by NFSv2 and NFSv3.
664 nfs_stat_to_errno(int stat)
668 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
669 if (nfs_errtbl[i].stat == stat)
670 return nfs_errtbl[i].errno;
672 printk(KERN_ERR "nfs_stat_to_errno: bad nfs status return value: %d\n", stat);
673 return nfs_errtbl[i].errno;
677 # define MAX(a, b) (((a) > (b))? (a) : (b))
680 #define PROC(proc, argtype, restype, timer) \
681 [NFSPROC_##proc] = { \
682 .p_proc = NFSPROC_##proc, \
683 .p_encode = (kxdrproc_t) nfs_xdr_##argtype, \
684 .p_decode = (kxdrproc_t) nfs_xdr_##restype, \
685 .p_bufsiz = MAX(NFS_##argtype##_sz,NFS_##restype##_sz) << 2, \
688 struct rpc_procinfo nfs_procedures[] = {
689 PROC(GETATTR, fhandle, attrstat, 1),
690 PROC(SETATTR, sattrargs, attrstat, 0),
691 PROC(LOOKUP, diropargs, diropres, 2),
692 PROC(READLINK, readlinkargs, readlinkres, 3),
693 PROC(READ, readargs, readres, 3),
694 PROC(WRITE, writeargs, writeres, 4),
695 PROC(CREATE, createargs, diropres, 0),
696 PROC(REMOVE, diropargs, stat, 0),
697 PROC(RENAME, renameargs, stat, 0),
698 PROC(LINK, linkargs, stat, 0),
699 PROC(SYMLINK, symlinkargs, stat, 0),
700 PROC(MKDIR, createargs, diropres, 0),
701 PROC(RMDIR, diropargs, stat, 0),
702 PROC(READDIR, readdirargs, readdirres, 3),
703 PROC(STATFS, fhandle, statfsres, 0),
706 struct rpc_version nfs_version2 = {
708 .nrprocs = sizeof(nfs_procedures)/sizeof(nfs_procedures[0]),
709 .procs = nfs_procedures