blob: 81cf142579167e9654b3332348e92e3eee48c37b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
16#include <linux/string.h>
17#include <linux/in.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>
Trond Myklebust816724e2006-06-24 08:41:41 -040024#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define NFSDBG_FACILITY NFSDBG_XDR
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* Mapping from NFS error code to "errno" error code. */
29#define errno_NFSERR_IO EIO
30
31/*
32 * Declare the space requirements for NFS arguments and replies as
33 * number of 32bit-words
34 */
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)
42
43#define NFS_diropargs_sz (NFS_fhandle_sz+NFS_filename_sz)
Trond Myklebust4fdc17b2007-07-14 15:39:57 -040044#define NFS_removeargs_sz (NFS_fhandle_sz+NFS_filename_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#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)
Chuck Lever94a6d752006-08-22 20:06:23 -040052#define NFS_symlinkargs_sz (NFS_diropargs_sz+1+NFS_sattr_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFS_readdirargs_sz (NFS_fhandle_sz+2)
54
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)
63
64/*
65 * Common NFS XDR functions as inlines
66 */
Al Viro9d787a72006-10-19 23:28:47 -070067static inline __be32 *
Trond Myklebust4fdc17b2007-07-14 15:39:57 -040068xdr_encode_fhandle(__be32 *p, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 memcpy(p, fhandle->data, NFS2_FHSIZE);
71 return p + XDR_QUADLEN(NFS2_FHSIZE);
72}
73
Al Viro9d787a72006-10-19 23:28:47 -070074static inline __be32 *
75xdr_decode_fhandle(__be32 *p, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
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);
81}
82
Al Viro9d787a72006-10-19 23:28:47 -070083static inline __be32*
84xdr_encode_time(__be32 *p, struct timespec *timep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 *p++ = htonl(timep->tv_sec);
87 /* Convert nanoseconds into microseconds */
88 *p++ = htonl(timep->tv_nsec ? timep->tv_nsec / 1000 : 0);
89 return p;
90}
91
Al Viro9d787a72006-10-19 23:28:47 -070092static inline __be32*
93xdr_encode_current_server_time(__be32 *p, struct timespec *timep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 /*
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
103 */
104 *p++ = htonl(timep->tv_sec);
105 *p++ = htonl(1000000);
106 return p;
107}
108
Al Viro9d787a72006-10-19 23:28:47 -0700109static inline __be32*
110xdr_decode_time(__be32 *p, struct timespec *timep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 timep->tv_sec = ntohl(*p++);
113 /* Convert microseconds into nanoseconds */
114 timep->tv_nsec = ntohl(*p++) * 1000;
115 return p;
116}
117
Al Viro9d787a72006-10-19 23:28:47 -0700118static __be32 *
119xdr_decode_fattr(__be32 *p, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Trond Myklebustbca79472009-03-11 14:10:26 -0400121 u32 rdev, type;
122 type = ntohl(*p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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++);
129 rdev = ntohl(*p++);
130 fattr->du.nfs2.blocks = ntohl(*p++);
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -0400131 fattr->fsid.major = ntohl(*p++);
132 fattr->fsid.minor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 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);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400137 fattr->valid |= NFS_ATTR_FATTR_V2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 fattr->rdev = new_decode_dev(rdev);
Trond Myklebustbca79472009-03-11 14:10:26 -0400139 if (type == NFCHR && rdev == NFS2_FIFO_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
141 fattr->rdev = 0;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return p;
144}
145
Al Viro9d787a72006-10-19 23:28:47 -0700146static inline __be32 *
147xdr_encode_sattr(__be32 *p, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Al Viro9d787a72006-10-19 23:28:47 -0700149 const __be32 not_set = __constant_htonl(0xFFFFFFFF);
Trond Myklebusteadb8c12006-01-03 09:55:54 +0100150
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
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);
160 } else {
Trond Myklebusteadb8c12006-01-03 09:55:54 +0100161 *p++ = not_set;
162 *p++ = not_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164
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);
169 } else {
Trond Myklebusteadb8c12006-01-03 09:55:54 +0100170 *p++ = not_set;
171 *p++ = not_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 return p;
174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/*
177 * NFS encode functions
178 */
179/*
180 * Encode file handle argument
181 * GETATTR, READLINK, STATFS
182 */
183static int
Al Viro9d787a72006-10-19 23:28:47 -0700184nfs_xdr_fhandle(struct rpc_rqst *req, __be32 *p, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 p = xdr_encode_fhandle(p, fh);
187 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
188 return 0;
189}
190
191/*
192 * Encode SETATTR arguments
193 */
194static int
Al Viro9d787a72006-10-19 23:28:47 -0700195nfs_xdr_sattrargs(struct rpc_rqst *req, __be32 *p, struct nfs_sattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
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);
200 return 0;
201}
202
203/*
204 * Encode directory ops argument
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400205 * LOOKUP, RMDIR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
207static int
Al Viro9d787a72006-10-19 23:28:47 -0700208nfs_xdr_diropargs(struct rpc_rqst *req, __be32 *p, struct nfs_diropargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
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);
213 return 0;
214}
215
216/*
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400217 * Encode REMOVE argument
218 */
219static int
220nfs_xdr_removeargs(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
221{
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);
225 return 0;
226}
227
228/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * 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.
232 */
233static int
Al Viro9d787a72006-10-19 23:28:47 -0700234nfs_xdr_readargs(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400236 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 unsigned int replen;
238 u32 offset = (u32)args->offset;
239 u32 count = args->count;
240
241 p = xdr_encode_fhandle(p, args->fh);
242 *p++ = htonl(offset);
243 *p++ = htonl(count);
244 *p++ = htonl(count);
245 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
246
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);
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -0400251 req->rq_rcv_buf.flags |= XDRBUF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253}
254
255/*
256 * Decode READ reply
257 */
258static int
Al Viro9d787a72006-10-19 23:28:47 -0700259nfs_xdr_readres(struct rpc_rqst *req, __be32 *p, struct nfs_readres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct kvec *iov = req->rq_rcv_buf.head;
Chuck Lever6232dbb2007-10-26 13:31:52 -0400262 size_t hdrlen;
263 u32 count, recvd;
264 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300267 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 p = xdr_decode_fattr(p, res->fattr);
269
270 count = ntohl(*p++);
271 res->eof = 0;
272 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
273 if (iov->iov_len < hdrlen) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400274 dprintk("NFS: READ reply header overflowed:"
Chuck Lever6232dbb2007-10-26 13:31:52 -0400275 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 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);
280 }
281
282 recvd = req->rq_rcv_buf.len - hdrlen;
283 if (count > recvd) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400284 dprintk("NFS: server cheating in read reply: "
Chuck Lever6232dbb2007-10-26 13:31:52 -0400285 "count %u > recvd %u\n", count, recvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 count = recvd;
287 }
288
Chuck Lever6232dbb2007-10-26 13:31:52 -0400289 dprintk("RPC: readres OK count %u\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (count < res->count)
291 res->count = count;
292
293 return count;
294}
295
296
297/*
298 * Write arguments. Splice the buffer to be written into the iovec.
299 */
300static int
Al Viro9d787a72006-10-19 23:28:47 -0700301nfs_xdr_writeargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct xdr_buf *sndbuf = &req->rq_snd_buf;
304 u32 offset = (u32)args->offset;
305 u32 count = args->count;
306
307 p = xdr_encode_fhandle(p, args->fh);
308 *p++ = htonl(offset);
309 *p++ = htonl(offset);
310 *p++ = htonl(count);
311 *p++ = htonl(count);
312 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
313
314 /* Copy the page array */
315 xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -0400316 sndbuf->flags |= XDRBUF_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318}
319
320/*
321 * Encode create arguments
322 * CREATE, MKDIR
323 */
324static int
Al Viro9d787a72006-10-19 23:28:47 -0700325nfs_xdr_createargs(struct rpc_rqst *req, __be32 *p, struct nfs_createargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
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);
331 return 0;
332}
333
334/*
335 * Encode RENAME arguments
336 */
337static int
Al Viro9d787a72006-10-19 23:28:47 -0700338nfs_xdr_renameargs(struct rpc_rqst *req, __be32 *p, struct nfs_renameargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 p = xdr_encode_fhandle(p, args->fromfh);
341 p = xdr_encode_array(p, args->fromname, args->fromlen);
342 p = xdr_encode_fhandle(p, args->tofh);
343 p = xdr_encode_array(p, args->toname, args->tolen);
344 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
345 return 0;
346}
347
348/*
349 * Encode LINK arguments
350 */
351static int
Al Viro9d787a72006-10-19 23:28:47 -0700352nfs_xdr_linkargs(struct rpc_rqst *req, __be32 *p, struct nfs_linkargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
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);
358 return 0;
359}
360
361/*
362 * Encode SYMLINK arguments
363 */
364static int
Al Viro9d787a72006-10-19 23:28:47 -0700365nfs_xdr_symlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_symlinkargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Chuck Lever94a6d752006-08-22 20:06:23 -0400367 struct xdr_buf *sndbuf = &req->rq_snd_buf;
368 size_t pad;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 p = xdr_encode_fhandle(p, args->fromfh);
371 p = xdr_encode_array(p, args->fromname, args->fromlen);
Chuck Lever94a6d752006-08-22 20:06:23 -0400372 *p++ = htonl(args->pathlen);
373 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
374
375 xdr_encode_pages(sndbuf, args->pages, 0, args->pathlen);
376
377 /*
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.
381 */
382 pad = sndbuf->tail->iov_len;
383 if (pad > 0)
384 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 p = xdr_encode_sattr(p, args->sattr);
Chuck Lever94a6d752006-08-22 20:06:23 -0400386 sndbuf->len += xdr_adjust_iovec(sndbuf->tail, p) - pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388}
389
390/*
391 * Encode arguments to readdir call
392 */
393static int
Al Viro9d787a72006-10-19 23:28:47 -0700394nfs_xdr_readdirargs(struct rpc_rqst *req, __be32 *p, struct nfs_readdirargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct rpc_task *task = req->rq_task;
Trond Myklebust1be27f32007-06-27 14:29:04 -0400397 struct rpc_auth *auth = task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 unsigned int replen;
399 u32 count = args->count;
400
401 p = xdr_encode_fhandle(p, args->fh);
402 *p++ = htonl(args->cookie);
403 *p++ = htonl(count); /* see above */
404 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
405
406 /* Inline the page array */
407 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readdirres_sz) << 2;
408 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
409 return 0;
410}
411
412/*
413 * Decode the result of a readdir call.
414 * We're not really decoding anymore, we just leave the buffer untouched
415 * and only check that it is syntactically correct.
416 * The real decoding happens in nfs_decode_entry below, called directly
417 * from nfs_readdir for each entry.
418 */
419static int
Al Viro9d787a72006-10-19 23:28:47 -0700420nfs_xdr_readdirres(struct rpc_rqst *req, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
423 struct kvec *iov = rcvbuf->head;
424 struct page **page;
Chuck Lever6232dbb2007-10-26 13:31:52 -0400425 size_t hdrlen;
426 unsigned int pglen, recvd;
427 u32 len;
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500428 int status, nr = 0;
Al Viro9d787a72006-10-19 23:28:47 -0700429 __be32 *end, *entry, *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300432 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
435 if (iov->iov_len < hdrlen) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400436 dprintk("NFS: READDIR reply header overflowed:"
Chuck Lever6232dbb2007-10-26 13:31:52 -0400437 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -errno_NFSERR_IO;
439 } else if (iov->iov_len != hdrlen) {
440 dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
441 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
442 }
443
444 pglen = rcvbuf->page_len;
445 recvd = rcvbuf->len - hdrlen;
446 if (pglen > recvd)
447 pglen = recvd;
448 page = rcvbuf->pages;
Al Viro9d787a72006-10-19 23:28:47 -0700449 kaddr = p = kmap_atomic(*page, KM_USER0);
450 end = (__be32 *)((char *)p + pglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 entry = p;
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500452
453 /* Make sure the packet actually has a value_follows and EOF entry */
454 if ((entry + 1) > end)
455 goto short_pkt;
456
457 for (; *p++; nr++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (p + 2 > end)
459 goto short_pkt;
460 p++; /* fileid */
461 len = ntohl(*p++);
462 p += XDR_QUADLEN(len) + 1; /* name plus cookie */
463 if (len > NFS2_MAXNAMLEN) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400464 dprintk("NFS: giant filename in readdir (len 0x%x)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 len);
466 goto err_unmap;
467 }
468 if (p + 2 > end)
469 goto short_pkt;
470 entry = p;
471 }
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500472
473 /*
474 * Apparently some server sends responses that are a valid size, but
475 * contain no entries, and have value_follows==0 and EOF==0. For
476 * those, just set the EOF marker.
477 */
478 if (!nr && entry[1] == 0) {
479 dprintk("NFS: readdir reply truncated!\n");
480 entry[1] = 1;
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 out:
483 kunmap_atomic(kaddr, KM_USER0);
484 return nr;
485 short_pkt:
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500486 /*
487 * When we get a short packet there are 2 possibilities. We can
488 * return an error, or fix up the response to look like a valid
489 * response and return what we have so far. If there are no
490 * entries and the packet was short, then return -EIO. If there
491 * are valid entries in the response, return them and pretend that
492 * the call was successful, but incomplete. The caller can retry the
493 * readdir starting at the last cookie.
494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 entry[0] = entry[1] = 0;
Jeff Laytoncaa02bd2008-02-22 14:49:59 -0500496 if (!nr)
497 nr = -errno_NFSERR_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto out;
499err_unmap:
500 nr = -errno_NFSERR_IO;
501 goto out;
502}
503
Al Viro0dbb4c62006-10-19 23:28:49 -0700504__be32 *
505nfs_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 if (!*p++) {
508 if (!*p)
509 return ERR_PTR(-EAGAIN);
510 entry->eof = 1;
511 return ERR_PTR(-EBADCOOKIE);
512 }
513
514 entry->ino = ntohl(*p++);
515 entry->len = ntohl(*p++);
516 entry->name = (const char *) p;
517 p += XDR_QUADLEN(entry->len);
518 entry->prev_cookie = entry->cookie;
519 entry->cookie = ntohl(*p++);
520 entry->eof = !p[0] && p[1];
521
522 return p;
523}
524
525/*
526 * NFS XDR decode functions
527 */
528/*
529 * Decode simple status reply
530 */
531static int
Al Viro9d787a72006-10-19 23:28:47 -0700532nfs_xdr_stat(struct rpc_rqst *req, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 int status;
535
536 if ((status = ntohl(*p++)) != 0)
Benny Halevy856dff32008-03-31 17:39:06 +0300537 status = nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return status;
539}
540
541/*
542 * Decode attrstat reply
543 * GETATTR, SETATTR, WRITE
544 */
545static int
Al Viro9d787a72006-10-19 23:28:47 -0700546nfs_xdr_attrstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 int status;
549
550 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300551 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 xdr_decode_fattr(p, fattr);
553 return 0;
554}
555
556/*
557 * Decode diropres reply
558 * LOOKUP, CREATE, MKDIR
559 */
560static int
Al Viro9d787a72006-10-19 23:28:47 -0700561nfs_xdr_diropres(struct rpc_rqst *req, __be32 *p, struct nfs_diropok *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 int status;
564
565 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300566 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 p = xdr_decode_fhandle(p, res->fh);
568 xdr_decode_fattr(p, res->fattr);
569 return 0;
570}
571
572/*
573 * Encode READLINK args
574 */
575static int
Al Viro9d787a72006-10-19 23:28:47 -0700576nfs_xdr_readlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_readlinkargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400578 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 unsigned int replen;
580
581 p = xdr_encode_fhandle(p, args->fh);
582 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
583
584 /* Inline the page array */
585 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readlinkres_sz) << 2;
586 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
587 return 0;
588}
589
590/*
591 * Decode READLINK reply
592 */
593static int
Al Viro9d787a72006-10-19 23:28:47 -0700594nfs_xdr_readlinkres(struct rpc_rqst *req, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
597 struct kvec *iov = rcvbuf->head;
Chuck Lever6232dbb2007-10-26 13:31:52 -0400598 size_t hdrlen;
599 u32 len, recvd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 char *kaddr;
601 int status;
602
603 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300604 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Convert length of symlink */
606 len = ntohl(*p++);
Chuck Lever6232dbb2007-10-26 13:31:52 -0400607 if (len >= rcvbuf->page_len) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400608 dprintk("nfs: server returned giant symlink!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -ENAMETOOLONG;
610 }
611 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
612 if (iov->iov_len < hdrlen) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400613 dprintk("NFS: READLINK reply header overflowed:"
Chuck Lever6232dbb2007-10-26 13:31:52 -0400614 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -errno_NFSERR_IO;
616 } else if (iov->iov_len != hdrlen) {
617 dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
618 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
619 }
620 recvd = req->rq_rcv_buf.len - hdrlen;
621 if (recvd < len) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400622 dprintk("NFS: server cheating in readlink reply: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 "count %u > recvd %u\n", len, recvd);
624 return -EIO;
625 }
626
627 /* NULL terminate the string we got */
628 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
629 kaddr[len+rcvbuf->page_base] = '\0';
630 kunmap_atomic(kaddr, KM_USER0);
631 return 0;
632}
633
634/*
635 * Decode WRITE reply
636 */
637static int
Al Viro9d787a72006-10-19 23:28:47 -0700638nfs_xdr_writeres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 res->verf->committed = NFS_FILE_SYNC;
641 return nfs_xdr_attrstat(req, p, res->fattr);
642}
643
644/*
645 * Decode STATFS reply
646 */
647static int
Al Viro9d787a72006-10-19 23:28:47 -0700648nfs_xdr_statfsres(struct rpc_rqst *req, __be32 *p, struct nfs2_fsstat *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 int status;
651
652 if ((status = ntohl(*p++)))
Benny Halevy856dff32008-03-31 17:39:06 +0300653 return nfs_stat_to_errno(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 res->tsize = ntohl(*p++);
656 res->bsize = ntohl(*p++);
657 res->blocks = ntohl(*p++);
658 res->bfree = ntohl(*p++);
659 res->bavail = ntohl(*p++);
660 return 0;
661}
662
663/*
664 * We need to translate between nfs status return values and
665 * the local errno values which may not be the same.
666 */
667static struct {
668 int stat;
669 int errno;
670} nfs_errtbl[] = {
671 { NFS_OK, 0 },
Benny Halevy856dff32008-03-31 17:39:06 +0300672 { NFSERR_PERM, -EPERM },
673 { NFSERR_NOENT, -ENOENT },
674 { NFSERR_IO, -errno_NFSERR_IO},
675 { NFSERR_NXIO, -ENXIO },
676/* { NFSERR_EAGAIN, -EAGAIN }, */
677 { NFSERR_ACCES, -EACCES },
678 { NFSERR_EXIST, -EEXIST },
679 { NFSERR_XDEV, -EXDEV },
680 { NFSERR_NODEV, -ENODEV },
681 { NFSERR_NOTDIR, -ENOTDIR },
682 { NFSERR_ISDIR, -EISDIR },
683 { NFSERR_INVAL, -EINVAL },
684 { NFSERR_FBIG, -EFBIG },
685 { NFSERR_NOSPC, -ENOSPC },
686 { NFSERR_ROFS, -EROFS },
687 { NFSERR_MLINK, -EMLINK },
688 { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
689 { NFSERR_NOTEMPTY, -ENOTEMPTY },
690 { NFSERR_DQUOT, -EDQUOT },
691 { NFSERR_STALE, -ESTALE },
692 { NFSERR_REMOTE, -EREMOTE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693#ifdef EWFLUSH
Benny Halevy856dff32008-03-31 17:39:06 +0300694 { NFSERR_WFLUSH, -EWFLUSH },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#endif
Benny Halevy856dff32008-03-31 17:39:06 +0300696 { NFSERR_BADHANDLE, -EBADHANDLE },
697 { NFSERR_NOT_SYNC, -ENOTSYNC },
698 { NFSERR_BAD_COOKIE, -EBADCOOKIE },
699 { NFSERR_NOTSUPP, -ENOTSUPP },
700 { NFSERR_TOOSMALL, -ETOOSMALL },
Trond Myklebustfdcb4572010-02-08 09:32:40 -0500701 { NFSERR_SERVERFAULT, -EREMOTEIO },
Benny Halevy856dff32008-03-31 17:39:06 +0300702 { NFSERR_BADTYPE, -EBADTYPE },
703 { NFSERR_JUKEBOX, -EJUKEBOX },
704 { -1, -EIO }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705};
706
707/*
708 * Convert an NFS error code to a local one.
709 * This one is used jointly by NFSv2 and NFSv3.
710 */
711int
712nfs_stat_to_errno(int stat)
713{
714 int i;
715
716 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
717 if (nfs_errtbl[i].stat == stat)
718 return nfs_errtbl[i].errno;
719 }
Chuck Leverfe82a182007-09-11 18:01:10 -0400720 dprintk("nfs_stat_to_errno: bad nfs status return value: %d\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return nfs_errtbl[i].errno;
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724#define PROC(proc, argtype, restype, timer) \
725[NFSPROC_##proc] = { \
726 .p_proc = NFSPROC_##proc, \
727 .p_encode = (kxdrproc_t) nfs_xdr_##argtype, \
728 .p_decode = (kxdrproc_t) nfs_xdr_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -0400729 .p_arglen = NFS_##argtype##_sz, \
730 .p_replen = NFS_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -0500731 .p_timer = timer, \
732 .p_statidx = NFSPROC_##proc, \
733 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735struct rpc_procinfo nfs_procedures[] = {
736 PROC(GETATTR, fhandle, attrstat, 1),
737 PROC(SETATTR, sattrargs, attrstat, 0),
738 PROC(LOOKUP, diropargs, diropres, 2),
739 PROC(READLINK, readlinkargs, readlinkres, 3),
740 PROC(READ, readargs, readres, 3),
741 PROC(WRITE, writeargs, writeres, 4),
742 PROC(CREATE, createargs, diropres, 0),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400743 PROC(REMOVE, removeargs, stat, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 PROC(RENAME, renameargs, stat, 0),
745 PROC(LINK, linkargs, stat, 0),
746 PROC(SYMLINK, symlinkargs, stat, 0),
747 PROC(MKDIR, createargs, diropres, 0),
748 PROC(RMDIR, diropargs, stat, 0),
749 PROC(READDIR, readdirargs, readdirres, 3),
750 PROC(STATFS, fhandle, statfsres, 0),
751};
752
753struct rpc_version nfs_version2 = {
754 .number = 2,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800755 .nrprocs = ARRAY_SIZE(nfs_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .procs = nfs_procedures
757};