blob: 7bc2da8efd4a352a962d8aac259cd6279fe3f6eb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/nfs2xdr.c
3 *
4 * XDR functions to encode/decode NFS RPC arguments and results.
5 *
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
10 */
11
12#include <linux/param.h>
13#include <linux/time.h>
14#include <linux/mm.h>
15#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/string.h>
18#include <linux/in.h>
19#include <linux/pagemap.h>
20#include <linux/proc_fs.h>
21#include <linux/sunrpc/clnt.h>
22#include <linux/nfs.h>
23#include <linux/nfs2.h>
24#include <linux/nfs_fs.h>
Trond Myklebust816724e2006-06-24 08:41:41 -040025#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define NFSDBG_FACILITY NFSDBG_XDR
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Mapping from NFS error code to "errno" error code. */
30#define errno_NFSERR_IO EIO
31
32/*
33 * Declare the space requirements for NFS arguments and replies as
34 * number of 32bit-words
35 */
36#define NFS_fhandle_sz (8)
37#define NFS_sattr_sz (8)
38#define NFS_filename_sz (1+(NFS2_MAXNAMLEN>>2))
39#define NFS_path_sz (1+(NFS2_MAXPATHLEN>>2))
40#define NFS_fattr_sz (17)
41#define NFS_info_sz (5)
42#define NFS_entry_sz (NFS_filename_sz+3)
43
44#define NFS_diropargs_sz (NFS_fhandle_sz+NFS_filename_sz)
Trond Myklebust4fdc17b2007-07-14 15:39:57 -040045#define NFS_removeargs_sz (NFS_fhandle_sz+NFS_filename_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define NFS_sattrargs_sz (NFS_fhandle_sz+NFS_sattr_sz)
47#define NFS_readlinkargs_sz (NFS_fhandle_sz)
48#define NFS_readargs_sz (NFS_fhandle_sz+3)
49#define NFS_writeargs_sz (NFS_fhandle_sz+4)
50#define NFS_createargs_sz (NFS_diropargs_sz+NFS_sattr_sz)
51#define NFS_renameargs_sz (NFS_diropargs_sz+NFS_diropargs_sz)
52#define NFS_linkargs_sz (NFS_fhandle_sz+NFS_diropargs_sz)
Chuck Lever94a6d752006-08-22 20:06:23 -040053#define NFS_symlinkargs_sz (NFS_diropargs_sz+1+NFS_sattr_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define NFS_readdirargs_sz (NFS_fhandle_sz+2)
55
56#define NFS_attrstat_sz (1+NFS_fattr_sz)
57#define NFS_diropres_sz (1+NFS_fhandle_sz+NFS_fattr_sz)
58#define NFS_readlinkres_sz (2)
59#define NFS_readres_sz (1+NFS_fattr_sz+1)
60#define NFS_writeres_sz (NFS_attrstat_sz)
61#define NFS_stat_sz (1)
62#define NFS_readdirres_sz (1)
63#define NFS_statfsres_sz (1+NFS_info_sz)
64
65/*
66 * Common NFS XDR functions as inlines
67 */
Al Viro9d787a72006-10-19 23:28:47 -070068static inline __be32 *
Trond Myklebust4fdc17b2007-07-14 15:39:57 -040069xdr_encode_fhandle(__be32 *p, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 memcpy(p, fhandle->data, NFS2_FHSIZE);
72 return p + XDR_QUADLEN(NFS2_FHSIZE);
73}
74
Al Viro9d787a72006-10-19 23:28:47 -070075static inline __be32 *
76xdr_decode_fhandle(__be32 *p, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 /* NFSv2 handles have a fixed length */
79 fhandle->size = NFS2_FHSIZE;
80 memcpy(fhandle->data, p, NFS2_FHSIZE);
81 return p + XDR_QUADLEN(NFS2_FHSIZE);
82}
83
Al Viro9d787a72006-10-19 23:28:47 -070084static inline __be32*
85xdr_encode_time(__be32 *p, struct timespec *timep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 *p++ = htonl(timep->tv_sec);
88 /* Convert nanoseconds into microseconds */
89 *p++ = htonl(timep->tv_nsec ? timep->tv_nsec / 1000 : 0);
90 return p;
91}
92
Al Viro9d787a72006-10-19 23:28:47 -070093static inline __be32*
94xdr_encode_current_server_time(__be32 *p, struct timespec *timep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 /*
97 * Passing the invalid value useconds=1000000 is a
98 * Sun convention for "set to current server time".
99 * It's needed to make permissions checks for the
100 * "touch" program across v2 mounts to Solaris and
101 * Irix boxes work correctly. See description of
102 * sattr in section 6.1 of "NFS Illustrated" by
103 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
104 */
105 *p++ = htonl(timep->tv_sec);
106 *p++ = htonl(1000000);
107 return p;
108}
109
Al Viro9d787a72006-10-19 23:28:47 -0700110static inline __be32*
111xdr_decode_time(__be32 *p, struct timespec *timep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 timep->tv_sec = ntohl(*p++);
114 /* Convert microseconds into nanoseconds */
115 timep->tv_nsec = ntohl(*p++) * 1000;
116 return p;
117}
118
Al Viro9d787a72006-10-19 23:28:47 -0700119static __be32 *
120xdr_decode_fattr(__be32 *p, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Trond Myklebustbca79472009-03-11 14:10:26 -0400122 u32 rdev, type;
123 type = ntohl(*p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 fattr->mode = ntohl(*p++);
125 fattr->nlink = ntohl(*p++);
126 fattr->uid = ntohl(*p++);
127 fattr->gid = ntohl(*p++);
128 fattr->size = ntohl(*p++);
129 fattr->du.nfs2.blocksize = ntohl(*p++);
130 rdev = ntohl(*p++);
131 fattr->du.nfs2.blocks = ntohl(*p++);
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -0400132 fattr->fsid.major = ntohl(*p++);
133 fattr->fsid.minor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 fattr->fileid = ntohl(*p++);
135 p = xdr_decode_time(p, &fattr->atime);
136 p = xdr_decode_time(p, &fattr->mtime);
137 p = xdr_decode_time(p, &fattr->ctime);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400138 fattr->valid |= NFS_ATTR_FATTR_V2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 fattr->rdev = new_decode_dev(rdev);
Trond Myklebustbca79472009-03-11 14:10:26 -0400140 if (type == NFCHR && rdev == NFS2_FIFO_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
142 fattr->rdev = 0;
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return p;
145}
146
Al Viro9d787a72006-10-19 23:28:47 -0700147static inline __be32 *
148xdr_encode_sattr(__be32 *p, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Al Viro9d787a72006-10-19 23:28:47 -0700150 const __be32 not_set = __constant_htonl(0xFFFFFFFF);
Trond Myklebusteadb8c12006-01-03 09:55:54 +0100151
152 *p++ = (attr->ia_valid & ATTR_MODE) ? htonl(attr->ia_mode) : not_set;
153 *p++ = (attr->ia_valid & ATTR_UID) ? htonl(attr->ia_uid) : not_set;
154 *p++ = (attr->ia_valid & ATTR_GID) ? htonl(attr->ia_gid) : not_set;
155 *p++ = (attr->ia_valid & ATTR_SIZE) ? htonl(attr->ia_size) : not_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (attr->ia_valid & ATTR_ATIME_SET) {
158 p = xdr_encode_time(p, &attr->ia_atime);
159 } else if (attr->ia_valid & ATTR_ATIME) {
160 p = xdr_encode_current_server_time(p, &attr->ia_atime);
161 } else {
Trond Myklebusteadb8c12006-01-03 09:55:54 +0100162 *p++ = not_set;
163 *p++ = not_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
166 if (attr->ia_valid & ATTR_MTIME_SET) {
167 p = xdr_encode_time(p, &attr->ia_mtime);
168 } else if (attr->ia_valid & ATTR_MTIME) {
169 p = xdr_encode_current_server_time(p, &attr->ia_mtime);
170 } else {
Trond Myklebusteadb8c12006-01-03 09:55:54 +0100171 *p++ = not_set;
172 *p++ = not_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 return p;
175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/*
178 * NFS encode functions
179 */
180/*
181 * Encode file handle argument
182 * GETATTR, READLINK, STATFS
183 */
184static int
Al Viro9d787a72006-10-19 23:28:47 -0700185nfs_xdr_fhandle(struct rpc_rqst *req, __be32 *p, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 p = xdr_encode_fhandle(p, fh);
188 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
189 return 0;
190}
191
192/*
193 * Encode SETATTR arguments
194 */
195static int
Al Viro9d787a72006-10-19 23:28:47 -0700196nfs_xdr_sattrargs(struct rpc_rqst *req, __be32 *p, struct nfs_sattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 p = xdr_encode_fhandle(p, args->fh);
199 p = xdr_encode_sattr(p, args->sattr);
200 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
201 return 0;
202}
203
204/*
205 * Encode directory ops argument
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400206 * LOOKUP, RMDIR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
208static int
Al Viro9d787a72006-10-19 23:28:47 -0700209nfs_xdr_diropargs(struct rpc_rqst *req, __be32 *p, struct nfs_diropargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 p = xdr_encode_fhandle(p, args->fh);
212 p = xdr_encode_array(p, args->name, args->len);
213 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
214 return 0;
215}
216
217/*
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400218 * Encode REMOVE argument
219 */
220static int
221nfs_xdr_removeargs(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
222{
223 p = xdr_encode_fhandle(p, args->fh);
224 p = xdr_encode_array(p, args->name.name, args->name.len);
225 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
226 return 0;
227}
228
229/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * Arguments to a READ call. Since we read data directly into the page
231 * cache, we also set up the reply iovec here so that iov[1] points
232 * exactly to the page we want to fetch.
233 */
234static int
Al Viro9d787a72006-10-19 23:28:47 -0700235nfs_xdr_readargs(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400237 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 unsigned int replen;
239 u32 offset = (u32)args->offset;
240 u32 count = args->count;
241
242 p = xdr_encode_fhandle(p, args->fh);
243 *p++ = htonl(offset);
244 *p++ = htonl(count);
245 *p++ = htonl(count);
246 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
247
248 /* Inline the page array */
249 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readres_sz) << 2;
250 xdr_inline_pages(&req->rq_rcv_buf, replen,
251 args->pages, args->pgbase, count);
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -0400252 req->rq_rcv_buf.flags |= XDRBUF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254}
255
256/*
257 * Decode READ reply
258 */
259static int
Al Viro9d787a72006-10-19 23:28:47 -0700260nfs_xdr_readres(struct rpc_rqst *req, __be32 *p, struct nfs_readres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct kvec *iov = req->rq_rcv_buf.head;
Chuck Lever6232dbb2007-10-26 13:31:52 -0400263 size_t hdrlen;
264 u32 count, recvd;
265 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300268 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 p = xdr_decode_fattr(p, res->fattr);
270
271 count = ntohl(*p++);
272 res->eof = 0;
273 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
274 if (iov->iov_len < hdrlen) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400275 dprintk("NFS: READ reply header overflowed:"
Chuck Lever6232dbb2007-10-26 13:31:52 -0400276 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return -errno_NFSERR_IO;
278 } else if (iov->iov_len != hdrlen) {
279 dprintk("NFS: READ header is short. iovec will be shifted.\n");
280 xdr_shift_buf(&req->rq_rcv_buf, iov->iov_len - hdrlen);
281 }
282
283 recvd = req->rq_rcv_buf.len - hdrlen;
284 if (count > recvd) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400285 dprintk("NFS: server cheating in read reply: "
Chuck Lever6232dbb2007-10-26 13:31:52 -0400286 "count %u > recvd %u\n", count, recvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 count = recvd;
288 }
289
Chuck Lever6232dbb2007-10-26 13:31:52 -0400290 dprintk("RPC: readres OK count %u\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (count < res->count)
292 res->count = count;
293
294 return count;
295}
296
297
298/*
299 * Write arguments. Splice the buffer to be written into the iovec.
300 */
301static int
Al Viro9d787a72006-10-19 23:28:47 -0700302nfs_xdr_writeargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct xdr_buf *sndbuf = &req->rq_snd_buf;
305 u32 offset = (u32)args->offset;
306 u32 count = args->count;
307
308 p = xdr_encode_fhandle(p, args->fh);
309 *p++ = htonl(offset);
310 *p++ = htonl(offset);
311 *p++ = htonl(count);
312 *p++ = htonl(count);
313 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
314
315 /* Copy the page array */
316 xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -0400317 sndbuf->flags |= XDRBUF_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319}
320
321/*
322 * Encode create arguments
323 * CREATE, MKDIR
324 */
325static int
Al Viro9d787a72006-10-19 23:28:47 -0700326nfs_xdr_createargs(struct rpc_rqst *req, __be32 *p, struct nfs_createargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 p = xdr_encode_fhandle(p, args->fh);
329 p = xdr_encode_array(p, args->name, args->len);
330 p = xdr_encode_sattr(p, args->sattr);
331 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
332 return 0;
333}
334
335/*
336 * Encode RENAME arguments
337 */
338static int
Al Viro9d787a72006-10-19 23:28:47 -0700339nfs_xdr_renameargs(struct rpc_rqst *req, __be32 *p, struct nfs_renameargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 p = xdr_encode_fhandle(p, args->fromfh);
342 p = xdr_encode_array(p, args->fromname, args->fromlen);
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);
346 return 0;
347}
348
349/*
350 * Encode LINK arguments
351 */
352static int
Al Viro9d787a72006-10-19 23:28:47 -0700353nfs_xdr_linkargs(struct rpc_rqst *req, __be32 *p, struct nfs_linkargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 p = xdr_encode_fhandle(p, args->fromfh);
356 p = xdr_encode_fhandle(p, args->tofh);
357 p = xdr_encode_array(p, args->toname, args->tolen);
358 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
359 return 0;
360}
361
362/*
363 * Encode SYMLINK arguments
364 */
365static int
Al Viro9d787a72006-10-19 23:28:47 -0700366nfs_xdr_symlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_symlinkargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Chuck Lever94a6d752006-08-22 20:06:23 -0400368 struct xdr_buf *sndbuf = &req->rq_snd_buf;
369 size_t pad;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 p = xdr_encode_fhandle(p, args->fromfh);
372 p = xdr_encode_array(p, args->fromname, args->fromlen);
Chuck Lever94a6d752006-08-22 20:06:23 -0400373 *p++ = htonl(args->pathlen);
374 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
375
376 xdr_encode_pages(sndbuf, args->pages, 0, args->pathlen);
377
378 /*
379 * xdr_encode_pages may have added a few bytes to ensure the
380 * pathname ends on a 4-byte boundary. Start encoding the
381 * attributes after the pad bytes.
382 */
383 pad = sndbuf->tail->iov_len;
384 if (pad > 0)
385 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 p = xdr_encode_sattr(p, args->sattr);
Chuck Lever94a6d752006-08-22 20:06:23 -0400387 sndbuf->len += xdr_adjust_iovec(sndbuf->tail, p) - pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389}
390
391/*
392 * Encode arguments to readdir call
393 */
394static int
Al Viro9d787a72006-10-19 23:28:47 -0700395nfs_xdr_readdirargs(struct rpc_rqst *req, __be32 *p, struct nfs_readdirargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 struct rpc_task *task = req->rq_task;
Trond Myklebust1be27f32007-06-27 14:29:04 -0400398 struct rpc_auth *auth = task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 unsigned int replen;
400 u32 count = args->count;
401
402 p = xdr_encode_fhandle(p, args->fh);
403 *p++ = htonl(args->cookie);
404 *p++ = htonl(count); /* see above */
405 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
406
407 /* Inline the page array */
408 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readdirres_sz) << 2;
409 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
410 return 0;
411}
412
413/*
414 * Decode the result of a readdir call.
415 * We're not really decoding anymore, we just leave the buffer untouched
416 * and only check that it is syntactically correct.
417 * The real decoding happens in nfs_decode_entry below, called directly
418 * from nfs_readdir for each entry.
419 */
420static int
Al Viro9d787a72006-10-19 23:28:47 -0700421nfs_xdr_readdirres(struct rpc_rqst *req, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
424 struct kvec *iov = rcvbuf->head;
425 struct page **page;
Chuck Lever6232dbb2007-10-26 13:31:52 -0400426 size_t hdrlen;
427 unsigned int pglen, recvd;
428 u32 len;
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500429 int status, nr = 0;
Al Viro9d787a72006-10-19 23:28:47 -0700430 __be32 *end, *entry, *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300433 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
436 if (iov->iov_len < hdrlen) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400437 dprintk("NFS: READDIR reply header overflowed:"
Chuck Lever6232dbb2007-10-26 13:31:52 -0400438 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -errno_NFSERR_IO;
440 } else if (iov->iov_len != hdrlen) {
441 dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
442 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
443 }
444
445 pglen = rcvbuf->page_len;
446 recvd = rcvbuf->len - hdrlen;
447 if (pglen > recvd)
448 pglen = recvd;
449 page = rcvbuf->pages;
Al Viro9d787a72006-10-19 23:28:47 -0700450 kaddr = p = kmap_atomic(*page, KM_USER0);
451 end = (__be32 *)((char *)p + pglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 entry = p;
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500453
454 /* Make sure the packet actually has a value_follows and EOF entry */
455 if ((entry + 1) > end)
456 goto short_pkt;
457
458 for (; *p++; nr++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (p + 2 > end)
460 goto short_pkt;
461 p++; /* fileid */
462 len = ntohl(*p++);
463 p += XDR_QUADLEN(len) + 1; /* name plus cookie */
464 if (len > NFS2_MAXNAMLEN) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400465 dprintk("NFS: giant filename in readdir (len 0x%x)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 len);
467 goto err_unmap;
468 }
469 if (p + 2 > end)
470 goto short_pkt;
471 entry = p;
472 }
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500473
474 /*
475 * Apparently some server sends responses that are a valid size, but
476 * contain no entries, and have value_follows==0 and EOF==0. For
477 * those, just set the EOF marker.
478 */
479 if (!nr && entry[1] == 0) {
480 dprintk("NFS: readdir reply truncated!\n");
481 entry[1] = 1;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 out:
484 kunmap_atomic(kaddr, KM_USER0);
485 return nr;
486 short_pkt:
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500487 /*
488 * When we get a short packet there are 2 possibilities. We can
489 * return an error, or fix up the response to look like a valid
490 * response and return what we have so far. If there are no
491 * entries and the packet was short, then return -EIO. If there
492 * are valid entries in the response, return them and pretend that
493 * the call was successful, but incomplete. The caller can retry the
494 * readdir starting at the last cookie.
495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 entry[0] = entry[1] = 0;
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500497 if (!nr)
498 nr = -errno_NFSERR_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto out;
500err_unmap:
501 nr = -errno_NFSERR_IO;
502 goto out;
503}
504
Al Viro0dbb4c62006-10-19 23:28:49 -0700505__be32 *
506nfs_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 if (!*p++) {
509 if (!*p)
510 return ERR_PTR(-EAGAIN);
511 entry->eof = 1;
512 return ERR_PTR(-EBADCOOKIE);
513 }
514
515 entry->ino = ntohl(*p++);
516 entry->len = ntohl(*p++);
517 entry->name = (const char *) p;
518 p += XDR_QUADLEN(entry->len);
519 entry->prev_cookie = entry->cookie;
520 entry->cookie = ntohl(*p++);
521 entry->eof = !p[0] && p[1];
522
523 return p;
524}
525
526/*
527 * NFS XDR decode functions
528 */
529/*
530 * Decode simple status reply
531 */
532static int
Al Viro9d787a72006-10-19 23:28:47 -0700533nfs_xdr_stat(struct rpc_rqst *req, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 int status;
536
537 if ((status = ntohl(*p++)) != 0)
Benny Halevy856dff32008-03-31 17:39:06 +0300538 status = nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return status;
540}
541
542/*
543 * Decode attrstat reply
544 * GETATTR, SETATTR, WRITE
545 */
546static int
Al Viro9d787a72006-10-19 23:28:47 -0700547nfs_xdr_attrstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 int status;
550
551 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300552 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 xdr_decode_fattr(p, fattr);
554 return 0;
555}
556
557/*
558 * Decode diropres reply
559 * LOOKUP, CREATE, MKDIR
560 */
561static int
Al Viro9d787a72006-10-19 23:28:47 -0700562nfs_xdr_diropres(struct rpc_rqst *req, __be32 *p, struct nfs_diropok *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 int status;
565
566 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300567 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 p = xdr_decode_fhandle(p, res->fh);
569 xdr_decode_fattr(p, res->fattr);
570 return 0;
571}
572
573/*
574 * Encode READLINK args
575 */
576static int
Al Viro9d787a72006-10-19 23:28:47 -0700577nfs_xdr_readlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_readlinkargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400579 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 unsigned int replen;
581
582 p = xdr_encode_fhandle(p, args->fh);
583 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
584
585 /* Inline the page array */
586 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readlinkres_sz) << 2;
587 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
588 return 0;
589}
590
591/*
592 * Decode READLINK reply
593 */
594static int
Al Viro9d787a72006-10-19 23:28:47 -0700595nfs_xdr_readlinkres(struct rpc_rqst *req, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
598 struct kvec *iov = rcvbuf->head;
Chuck Lever6232dbb2007-10-26 13:31:52 -0400599 size_t hdrlen;
600 u32 len, recvd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 char *kaddr;
602 int status;
603
604 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300605 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* Convert length of symlink */
607 len = ntohl(*p++);
Chuck Lever6232dbb2007-10-26 13:31:52 -0400608 if (len >= rcvbuf->page_len) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400609 dprintk("nfs: server returned giant symlink!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return -ENAMETOOLONG;
611 }
612 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
613 if (iov->iov_len < hdrlen) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400614 dprintk("NFS: READLINK reply header overflowed:"
Chuck Lever6232dbb2007-10-26 13:31:52 -0400615 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -errno_NFSERR_IO;
617 } else if (iov->iov_len != hdrlen) {
618 dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
619 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
620 }
621 recvd = req->rq_rcv_buf.len - hdrlen;
622 if (recvd < len) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400623 dprintk("NFS: server cheating in readlink reply: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 "count %u > recvd %u\n", len, recvd);
625 return -EIO;
626 }
627
628 /* NULL terminate the string we got */
629 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
630 kaddr[len+rcvbuf->page_base] = '\0';
631 kunmap_atomic(kaddr, KM_USER0);
632 return 0;
633}
634
635/*
636 * Decode WRITE reply
637 */
638static int
Al Viro9d787a72006-10-19 23:28:47 -0700639nfs_xdr_writeres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 res->verf->committed = NFS_FILE_SYNC;
642 return nfs_xdr_attrstat(req, p, res->fattr);
643}
644
645/*
646 * Decode STATFS reply
647 */
648static int
Al Viro9d787a72006-10-19 23:28:47 -0700649nfs_xdr_statfsres(struct rpc_rqst *req, __be32 *p, struct nfs2_fsstat *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 int status;
652
653 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300654 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 res->tsize = ntohl(*p++);
657 res->bsize = ntohl(*p++);
658 res->blocks = ntohl(*p++);
659 res->bfree = ntohl(*p++);
660 res->bavail = ntohl(*p++);
661 return 0;
662}
663
664/*
665 * We need to translate between nfs status return values and
666 * the local errno values which may not be the same.
667 */
668static struct {
669 int stat;
670 int errno;
671} nfs_errtbl[] = {
672 { NFS_OK, 0 },
Benny Halevy856dff32008-03-31 17:39:06 +0300673 { NFSERR_PERM, -EPERM },
674 { NFSERR_NOENT, -ENOENT },
675 { NFSERR_IO, -errno_NFSERR_IO},
676 { NFSERR_NXIO, -ENXIO },
677/* { NFSERR_EAGAIN, -EAGAIN }, */
678 { NFSERR_ACCES, -EACCES },
679 { NFSERR_EXIST, -EEXIST },
680 { NFSERR_XDEV, -EXDEV },
681 { NFSERR_NODEV, -ENODEV },
682 { NFSERR_NOTDIR, -ENOTDIR },
683 { NFSERR_ISDIR, -EISDIR },
684 { NFSERR_INVAL, -EINVAL },
685 { NFSERR_FBIG, -EFBIG },
686 { NFSERR_NOSPC, -ENOSPC },
687 { NFSERR_ROFS, -EROFS },
688 { NFSERR_MLINK, -EMLINK },
689 { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
690 { NFSERR_NOTEMPTY, -ENOTEMPTY },
691 { NFSERR_DQUOT, -EDQUOT },
692 { NFSERR_STALE, -ESTALE },
693 { NFSERR_REMOTE, -EREMOTE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#ifdef EWFLUSH
Benny Halevy856dff32008-03-31 17:39:06 +0300695 { NFSERR_WFLUSH, -EWFLUSH },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#endif
Benny Halevy856dff32008-03-31 17:39:06 +0300697 { NFSERR_BADHANDLE, -EBADHANDLE },
698 { NFSERR_NOT_SYNC, -ENOTSYNC },
699 { NFSERR_BAD_COOKIE, -EBADCOOKIE },
700 { NFSERR_NOTSUPP, -ENOTSUPP },
701 { NFSERR_TOOSMALL, -ETOOSMALL },
Trond Myklebustfdcb4572010-02-08 09:32:40 -0500702 { NFSERR_SERVERFAULT, -EREMOTEIO },
Benny Halevy856dff32008-03-31 17:39:06 +0300703 { NFSERR_BADTYPE, -EBADTYPE },
704 { NFSERR_JUKEBOX, -EJUKEBOX },
705 { -1, -EIO }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706};
707
708/*
709 * Convert an NFS error code to a local one.
710 * This one is used jointly by NFSv2 and NFSv3.
711 */
712int
713nfs_stat_to_errno(int stat)
714{
715 int i;
716
717 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
718 if (nfs_errtbl[i].stat == stat)
719 return nfs_errtbl[i].errno;
720 }
Chuck Leverfe82a182007-09-11 18:01:10 -0400721 dprintk("nfs_stat_to_errno: bad nfs status return value: %d\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return nfs_errtbl[i].errno;
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725#define PROC(proc, argtype, restype, timer) \
726[NFSPROC_##proc] = { \
727 .p_proc = NFSPROC_##proc, \
728 .p_encode = (kxdrproc_t) nfs_xdr_##argtype, \
729 .p_decode = (kxdrproc_t) nfs_xdr_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -0400730 .p_arglen = NFS_##argtype##_sz, \
731 .p_replen = NFS_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -0500732 .p_timer = timer, \
733 .p_statidx = NFSPROC_##proc, \
734 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736struct rpc_procinfo nfs_procedures[] = {
737 PROC(GETATTR, fhandle, attrstat, 1),
738 PROC(SETATTR, sattrargs, attrstat, 0),
739 PROC(LOOKUP, diropargs, diropres, 2),
740 PROC(READLINK, readlinkargs, readlinkres, 3),
741 PROC(READ, readargs, readres, 3),
742 PROC(WRITE, writeargs, writeres, 4),
743 PROC(CREATE, createargs, diropres, 0),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400744 PROC(REMOVE, removeargs, stat, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 PROC(RENAME, renameargs, stat, 0),
746 PROC(LINK, linkargs, stat, 0),
747 PROC(SYMLINK, symlinkargs, stat, 0),
748 PROC(MKDIR, createargs, diropres, 0),
749 PROC(RMDIR, diropargs, stat, 0),
750 PROC(READDIR, readdirargs, readdirres, 3),
751 PROC(STATFS, fhandle, statfsres, 0),
752};
753
754struct rpc_version nfs_version2 = {
755 .number = 2,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800756 .nrprocs = ARRAY_SIZE(nfs_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .procs = nfs_procedures
758};