blob: a9b848edbd2e714d09382e3601d987058d9f8bca [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
Chuck Lever25a08662010-12-14 14:54:30 +000064
65/*
66 * While encoding arguments, set up the reply buffer in advance to
67 * receive reply data directly into the page cache.
68 */
69static void prepare_reply_buffer(struct rpc_rqst *req, struct page **pages,
70 unsigned int base, unsigned int len,
71 unsigned int bufsize)
72{
73 struct rpc_auth *auth = req->rq_cred->cr_auth;
74 unsigned int replen;
75
76 replen = RPC_REPHDRSIZE + auth->au_rslack + bufsize;
77 xdr_inline_pages(&req->rq_rcv_buf, replen << 2, pages, base, len);
78}
79
Chuck Leverf796f8b2010-12-14 14:55:10 +000080/*
81 * Handle decode buffer overflows out-of-line.
82 */
83static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
84{
85 dprintk("NFS: %s prematurely hit the end of our receive buffer. "
86 "Remaining buffer length is %tu words.\n",
87 func, xdr->end - xdr->p);
88}
89
Chuck Lever25a08662010-12-14 14:54:30 +000090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
Chuck Lever25a08662010-12-14 14:54:30 +000092 * Encode/decode NFSv2 basic data types
93 *
94 * Basic NFSv2 data types are defined in section 2.3 of RFC 1094:
95 * "NFS: Network File System Protocol Specification".
96 *
97 * Not all basic data types have their own encoding and decoding
98 * functions. For run-time efficiency, some data types are encoded
99 * or decoded inline.
100 */
101
102/*
Chuck Leverf796f8b2010-12-14 14:55:10 +0000103 * typedef opaque nfsdata<>;
104 */
105static int decode_nfsdata(struct xdr_stream *xdr, struct nfs_readres *result)
106{
107 u32 recvd, count;
108 size_t hdrlen;
109 __be32 *p;
110
111 p = xdr_inline_decode(xdr, 4);
112 if (unlikely(p == NULL))
113 goto out_overflow;
114 count = be32_to_cpup(p);
115 hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
116 recvd = xdr->buf->len - hdrlen;
117 if (unlikely(count > recvd))
118 goto out_cheating;
119out:
120 xdr_read_pages(xdr, count);
121 result->eof = 0; /* NFSv2 does not pass EOF flag on the wire. */
122 result->count = count;
123 return count;
124out_cheating:
125 dprintk("NFS: server cheating in read result: "
126 "count %u > recvd %u\n", count, recvd);
127 count = recvd;
128 goto out;
129out_overflow:
130 print_overflow_msg(__func__, xdr);
131 return -EIO;
132}
133
134/*
135 * enum stat {
136 * NFS_OK = 0,
137 * NFSERR_PERM = 1,
138 * NFSERR_NOENT = 2,
139 * NFSERR_IO = 5,
140 * NFSERR_NXIO = 6,
141 * NFSERR_ACCES = 13,
142 * NFSERR_EXIST = 17,
143 * NFSERR_NODEV = 19,
144 * NFSERR_NOTDIR = 20,
145 * NFSERR_ISDIR = 21,
146 * NFSERR_FBIG = 27,
147 * NFSERR_NOSPC = 28,
148 * NFSERR_ROFS = 30,
149 * NFSERR_NAMETOOLONG = 63,
150 * NFSERR_NOTEMPTY = 66,
151 * NFSERR_DQUOT = 69,
152 * NFSERR_STALE = 70,
153 * NFSERR_WFLUSH = 99
154 * };
155 */
156static int decode_stat(struct xdr_stream *xdr, enum nfs_stat *status)
157{
158 __be32 *p;
159
160 p = xdr_inline_decode(xdr, 4);
161 if (unlikely(p == NULL))
162 goto out_overflow;
163 *status = be32_to_cpup(p);
164 return 0;
165out_overflow:
166 print_overflow_msg(__func__, xdr);
167 return -EIO;
168}
169
170/*
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000171 * 2.3.2. ftype
172 *
173 * enum ftype {
174 * NFNON = 0,
175 * NFREG = 1,
176 * NFDIR = 2,
177 * NFBLK = 3,
178 * NFCHR = 4,
179 * NFLNK = 5
180 * };
181 *
182 */
183static __be32 *xdr_decode_ftype(__be32 *p, u32 *type)
184{
185 *type = be32_to_cpup(p++);
186 if (unlikely(*type > NF2FIFO))
187 *type = NFBAD;
188 return p;
189}
190
191/*
Chuck Lever25a08662010-12-14 14:54:30 +0000192 * 2.3.3. fhandle
193 *
194 * typedef opaque fhandle[FHSIZE];
195 */
196static void encode_fhandle(struct xdr_stream *xdr, const struct nfs_fh *fh)
197{
198 __be32 *p;
199
200 BUG_ON(fh->size != NFS2_FHSIZE);
201 p = xdr_reserve_space(xdr, NFS2_FHSIZE);
202 memcpy(p, fh->data, NFS2_FHSIZE);
203}
204
Chuck Leverf796f8b2010-12-14 14:55:10 +0000205static int decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
206{
207 __be32 *p;
208
209 p = xdr_inline_decode(xdr, NFS2_FHSIZE);
210 if (unlikely(p == NULL))
211 goto out_overflow;
212 fh->size = NFS2_FHSIZE;
213 memcpy(fh->data, p, NFS2_FHSIZE);
214 return 0;
215out_overflow:
216 print_overflow_msg(__func__, xdr);
217 return -EIO;
218}
219
Chuck Lever25a08662010-12-14 14:54:30 +0000220/*
Chuck Lever282ac2a2010-12-14 14:54:50 +0000221 * 2.3.4. timeval
222 *
223 * struct timeval {
224 * unsigned int seconds;
225 * unsigned int useconds;
226 * };
227 */
228static __be32 *xdr_encode_time(__be32 *p, const struct timespec *timep)
229{
230 *p++ = cpu_to_be32(timep->tv_sec);
231 if (timep->tv_nsec != 0)
232 *p++ = cpu_to_be32(timep->tv_nsec / NSEC_PER_USEC);
233 else
234 *p++ = cpu_to_be32(0);
235 return p;
236}
237
238/*
239 * Passing the invalid value useconds=1000000 is a Sun convention for
240 * "set to current server time". It's needed to make permissions checks
241 * for the "touch" program across v2 mounts to Solaris and Irix servers
242 * work correctly. See description of sattr in section 6.1 of "NFS
243 * Illustrated" by Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5.
244 */
245static __be32 *xdr_encode_current_server_time(__be32 *p,
246 const struct timespec *timep)
247{
248 *p++ = cpu_to_be32(timep->tv_sec);
249 *p++ = cpu_to_be32(1000000);
250 return p;
251}
252
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000253static __be32 *xdr_decode_time(__be32 *p, struct timespec *timep)
254{
255 timep->tv_sec = be32_to_cpup(p++);
256 timep->tv_nsec = be32_to_cpup(p++) * NSEC_PER_USEC;
257 return p;
258}
259
Chuck Lever282ac2a2010-12-14 14:54:50 +0000260/*
Chuck Leverf796f8b2010-12-14 14:55:10 +0000261 * 2.3.5. fattr
262 *
263 * struct fattr {
264 * ftype type;
265 * unsigned int mode;
266 * unsigned int nlink;
267 * unsigned int uid;
268 * unsigned int gid;
269 * unsigned int size;
270 * unsigned int blocksize;
271 * unsigned int rdev;
272 * unsigned int blocks;
273 * unsigned int fsid;
274 * unsigned int fileid;
275 * timeval atime;
276 * timeval mtime;
277 * timeval ctime;
278 * };
279 *
280 */
281static int decode_fattr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
282{
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000283 u32 rdev, type;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000284 __be32 *p;
285
286 p = xdr_inline_decode(xdr, NFS_fattr_sz << 2);
287 if (unlikely(p == NULL))
288 goto out_overflow;
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000289
290 fattr->valid |= NFS_ATTR_FATTR_V2;
291
292 p = xdr_decode_ftype(p, &type);
293
294 fattr->mode = be32_to_cpup(p++);
295 fattr->nlink = be32_to_cpup(p++);
296 fattr->uid = be32_to_cpup(p++);
297 fattr->gid = be32_to_cpup(p++);
298 fattr->size = be32_to_cpup(p++);
299 fattr->du.nfs2.blocksize = be32_to_cpup(p++);
300
301 rdev = be32_to_cpup(p++);
302 fattr->rdev = new_decode_dev(rdev);
303 if (type == (u32)NFCHR && rdev == (u32)NFS2_FIFO_DEV) {
304 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
305 fattr->rdev = 0;
306 }
307
308 fattr->du.nfs2.blocks = be32_to_cpup(p++);
309 fattr->fsid.major = be32_to_cpup(p++);
310 fattr->fsid.minor = 0;
311 fattr->fileid = be32_to_cpup(p++);
312
313 p = xdr_decode_time(p, &fattr->atime);
314 p = xdr_decode_time(p, &fattr->mtime);
315 xdr_decode_time(p, &fattr->ctime);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000316 return 0;
317out_overflow:
318 print_overflow_msg(__func__, xdr);
319 return -EIO;
320}
321
322/*
Chuck Lever25a08662010-12-14 14:54:30 +0000323 * 2.3.6. sattr
324 *
325 * struct sattr {
326 * unsigned int mode;
327 * unsigned int uid;
328 * unsigned int gid;
329 * unsigned int size;
330 * timeval atime;
331 * timeval mtime;
332 * };
333 */
334
335#define NFS2_SATTR_NOT_SET (0xffffffff)
336
337static __be32 *xdr_time_not_set(__be32 *p)
338{
339 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
340 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
341 return p;
342}
343
344static void encode_sattr(struct xdr_stream *xdr, const struct iattr *attr)
345{
346 __be32 *p;
347
348 p = xdr_reserve_space(xdr, NFS_sattr_sz << 2);
349
350 if (attr->ia_valid & ATTR_MODE)
351 *p++ = cpu_to_be32(attr->ia_mode);
352 else
353 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
354 if (attr->ia_valid & ATTR_UID)
355 *p++ = cpu_to_be32(attr->ia_uid);
356 else
357 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
358 if (attr->ia_valid & ATTR_GID)
359 *p++ = cpu_to_be32(attr->ia_gid);
360 else
361 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
362 if (attr->ia_valid & ATTR_SIZE)
363 *p++ = cpu_to_be32((u32)attr->ia_size);
364 else
365 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
366
367 if (attr->ia_valid & ATTR_ATIME_SET)
368 p = xdr_encode_time(p, &attr->ia_atime);
369 else if (attr->ia_valid & ATTR_ATIME)
370 p = xdr_encode_current_server_time(p, &attr->ia_atime);
371 else
372 p = xdr_time_not_set(p);
373 if (attr->ia_valid & ATTR_MTIME_SET)
374 xdr_encode_time(p, &attr->ia_mtime);
375 else if (attr->ia_valid & ATTR_MTIME)
376 xdr_encode_current_server_time(p, &attr->ia_mtime);
377 else
378 xdr_time_not_set(p);
379}
380
381/*
382 * 2.3.7. filename
383 *
384 * typedef string filename<MAXNAMLEN>;
385 */
386static void encode_filename(struct xdr_stream *xdr,
387 const char *name, u32 length)
388{
389 __be32 *p;
390
391 BUG_ON(length > NFS2_MAXNAMLEN);
392 p = xdr_reserve_space(xdr, 4 + length);
393 xdr_encode_opaque(p, name, length);
394}
395
Chuck Leverf796f8b2010-12-14 14:55:10 +0000396static int decode_filename_inline(struct xdr_stream *xdr,
397 const char **name, u32 *length)
398{
399 __be32 *p;
400 u32 count;
401
402 p = xdr_inline_decode(xdr, 4);
403 if (unlikely(p == NULL))
404 goto out_overflow;
405 count = be32_to_cpup(p);
406 if (count > NFS3_MAXNAMLEN)
407 goto out_nametoolong;
408 p = xdr_inline_decode(xdr, count);
409 if (unlikely(p == NULL))
410 goto out_overflow;
411 *name = (const char *)p;
412 *length = count;
413 return 0;
414out_nametoolong:
415 dprintk("NFS: returned filename too long: %u\n", count);
416 return -ENAMETOOLONG;
417out_overflow:
418 print_overflow_msg(__func__, xdr);
419 return -EIO;
420}
421
Chuck Lever25a08662010-12-14 14:54:30 +0000422/*
423 * 2.3.8. path
424 *
425 * typedef string path<MAXPATHLEN>;
426 */
427static void encode_path(struct xdr_stream *xdr, struct page **pages, u32 length)
428{
429 __be32 *p;
430
431 BUG_ON(length > NFS2_MAXPATHLEN);
432 p = xdr_reserve_space(xdr, 4);
433 *p = cpu_to_be32(length);
434 xdr_write_pages(xdr, pages, 0, length);
435}
436
Chuck Leverf796f8b2010-12-14 14:55:10 +0000437static int decode_path(struct xdr_stream *xdr)
438{
439 u32 length, recvd;
440 size_t hdrlen;
441 __be32 *p;
442
443 p = xdr_inline_decode(xdr, 4);
444 if (unlikely(p == NULL))
445 goto out_overflow;
446 length = be32_to_cpup(p);
447 if (unlikely(length >= xdr->buf->page_len || length > NFS_MAXPATHLEN))
448 goto out_size;
449 hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
450 recvd = xdr->buf->len - hdrlen;
451 if (unlikely(length > recvd))
452 goto out_cheating;
453
454 xdr_read_pages(xdr, length);
455 xdr_terminate_string(xdr->buf, length);
456 return 0;
457out_size:
458 dprintk("NFS: returned pathname too long: %u\n", length);
459 return -ENAMETOOLONG;
460out_cheating:
461 dprintk("NFS: server cheating in pathname result: "
462 "length %u > received %u\n", length, recvd);
463 return -EIO;
464out_overflow:
465 print_overflow_msg(__func__, xdr);
466 return -EIO;
467}
468
469/*
470 * 2.3.9. attrstat
471 *
472 * union attrstat switch (stat status) {
473 * case NFS_OK:
474 * fattr attributes;
475 * default:
476 * void;
477 * };
478 */
479static int decode_attrstat(struct xdr_stream *xdr, struct nfs_fattr *result)
480{
481 enum nfs_stat status;
482 int error;
483
484 error = decode_stat(xdr, &status);
485 if (unlikely(error))
486 goto out;
487 if (status != NFS_OK)
488 goto out_default;
489 error = decode_fattr(xdr, result);
490out:
491 return error;
492out_default:
493 return nfs_stat_to_errno(status);
494}
495
Chuck Lever25a08662010-12-14 14:54:30 +0000496/*
497 * 2.3.10. diropargs
498 *
499 * struct diropargs {
500 * fhandle dir;
501 * filename name;
502 * };
503 */
504static void encode_diropargs(struct xdr_stream *xdr, const struct nfs_fh *fh,
505 const char *name, u32 length)
506{
507 encode_fhandle(xdr, fh);
508 encode_filename(xdr, name, length);
509}
510
Chuck Leverf796f8b2010-12-14 14:55:10 +0000511/*
512 * 2.3.11. diropres
513 *
514 * union diropres switch (stat status) {
515 * case NFS_OK:
516 * struct {
517 * fhandle file;
518 * fattr attributes;
519 * } diropok;
520 * default:
521 * void;
522 * };
523 */
524static int decode_diropok(struct xdr_stream *xdr, struct nfs_diropok *result)
525{
526 int error;
527
528 error = decode_fhandle(xdr, result->fh);
529 if (unlikely(error))
530 goto out;
531 error = decode_fattr(xdr, result->fattr);
532out:
533 return error;
534}
535
536static int decode_diropres(struct xdr_stream *xdr, struct nfs_diropok *result)
537{
538 enum nfs_stat status;
539 int error;
540
541 error = decode_stat(xdr, &status);
542 if (unlikely(error))
543 goto out;
544 if (status != NFS_OK)
545 goto out_default;
546 error = decode_diropok(xdr, result);
547out:
548 return error;
549out_default:
550 return nfs_stat_to_errno(status);
551}
552
Chuck Lever25a08662010-12-14 14:54:30 +0000553
554/*
Chuck Lever2d70f532010-12-14 14:54:40 +0000555 * NFSv2 XDR encode functions
556 *
557 * NFSv2 argument types are defined in section 2.2 of RFC 1094:
558 * "NFS: Network File System Protocol Specification".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Chuck Lever25a08662010-12-14 14:54:30 +0000561static int nfs2_xdr_enc_fhandle(struct rpc_rqst *req, __be32 *p,
562 const struct nfs_fh *fh)
563{
564 struct xdr_stream xdr;
565
566 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
567 encode_fhandle(&xdr, fh);
568 return 0;
569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/*
Chuck Lever25a08662010-12-14 14:54:30 +0000572 * 2.2.3. sattrargs
573 *
574 * struct sattrargs {
575 * fhandle file;
576 * sattr attributes;
577 * };
578 */
579static int nfs2_xdr_enc_sattrargs(struct rpc_rqst *req, __be32 *p,
580 const struct nfs_sattrargs *args)
581{
582 struct xdr_stream xdr;
583
584 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
585 encode_fhandle(&xdr, args->fh);
586 encode_sattr(&xdr, args->sattr);
587 return 0;
588}
589
Chuck Lever25a08662010-12-14 14:54:30 +0000590static int nfs2_xdr_enc_diropargs(struct rpc_rqst *req, __be32 *p,
591 const struct nfs_diropargs *args)
592{
593 struct xdr_stream xdr;
594
595 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
596 encode_diropargs(&xdr, args->fh, args->name, args->len);
597 return 0;
598}
599
Chuck Lever25a08662010-12-14 14:54:30 +0000600static int nfs2_xdr_enc_readlinkargs(struct rpc_rqst *req, __be32 *p,
601 const struct nfs_readlinkargs *args)
602{
603 struct xdr_stream xdr;
604
605 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
606 encode_fhandle(&xdr, args->fh);
607 prepare_reply_buffer(req, args->pages, args->pgbase,
608 args->pglen, NFS_readlinkres_sz);
609 return 0;
610}
611
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400612/*
Chuck Lever25a08662010-12-14 14:54:30 +0000613 * 2.2.7. readargs
614 *
615 * struct readargs {
616 * fhandle file;
617 * unsigned offset;
618 * unsigned count;
619 * unsigned totalcount;
620 * };
621 */
622static void encode_readargs(struct xdr_stream *xdr,
623 const struct nfs_readargs *args)
624{
625 u32 offset = args->offset;
626 u32 count = args->count;
627 __be32 *p;
628
629 encode_fhandle(xdr, args->fh);
630
631 p = xdr_reserve_space(xdr, 4 + 4 + 4);
632 *p++ = cpu_to_be32(offset);
633 *p++ = cpu_to_be32(count);
634 *p = cpu_to_be32(count);
635}
636
637static int nfs2_xdr_enc_readargs(struct rpc_rqst *req, __be32 *p,
638 const struct nfs_readargs *args)
639{
640 struct xdr_stream xdr;
641
642 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
643 encode_readargs(&xdr, args);
644 prepare_reply_buffer(req, args->pages, args->pgbase,
645 args->count, NFS_readres_sz);
646 req->rq_rcv_buf.flags |= XDRBUF_READ;
647 return 0;
648}
649
650/*
Chuck Lever25a08662010-12-14 14:54:30 +0000651 * 2.2.9. writeargs
652 *
653 * struct writeargs {
654 * fhandle file;
655 * unsigned beginoffset;
656 * unsigned offset;
657 * unsigned totalcount;
658 * nfsdata data;
659 * };
660 */
661static void encode_writeargs(struct xdr_stream *xdr,
662 const struct nfs_writeargs *args)
663{
664 u32 offset = args->offset;
665 u32 count = args->count;
666 __be32 *p;
667
668 encode_fhandle(xdr, args->fh);
669
670 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
671 *p++ = cpu_to_be32(offset);
672 *p++ = cpu_to_be32(offset);
673 *p++ = cpu_to_be32(count);
674
675 /* nfsdata */
676 *p = cpu_to_be32(count);
677 xdr_write_pages(xdr, args->pages, args->pgbase, count);
678}
679
680static int nfs2_xdr_enc_writeargs(struct rpc_rqst *req, __be32 *p,
681 const struct nfs_writeargs *args)
682{
683 struct xdr_stream xdr;
684
685 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
686 encode_writeargs(&xdr, args);
687 xdr.buf->flags |= XDRBUF_WRITE;
688 return 0;
689}
690
691/*
Chuck Lever25a08662010-12-14 14:54:30 +0000692 * 2.2.10. createargs
693 *
694 * struct createargs {
695 * diropargs where;
696 * sattr attributes;
697 * };
698 */
699static int nfs2_xdr_enc_createargs(struct rpc_rqst *req, __be32 *p,
700 const struct nfs_createargs *args)
701{
702 struct xdr_stream xdr;
703
704 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
705 encode_diropargs(&xdr, args->fh, args->name, args->len);
706 encode_sattr(&xdr, args->sattr);
707 return 0;
708}
709
710static int nfs2_xdr_enc_removeargs(struct rpc_rqst *req, __be32 *p,
711 const struct nfs_removeargs *args)
712{
713 struct xdr_stream xdr;
714
715 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
716 encode_diropargs(&xdr, args->fh, args->name.name, args->name.len);
717 return 0;
718}
719
720/*
Chuck Lever25a08662010-12-14 14:54:30 +0000721 * 2.2.12. renameargs
722 *
723 * struct renameargs {
724 * diropargs from;
725 * diropargs to;
726 * };
727 */
728static int nfs2_xdr_enc_renameargs(struct rpc_rqst *req, __be32 *p,
729 const struct nfs_renameargs *args)
730{
731 const struct qstr *old = args->old_name;
732 const struct qstr *new = args->new_name;
733 struct xdr_stream xdr;
734
735 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
736 encode_diropargs(&xdr, args->old_dir, old->name, old->len);
737 encode_diropargs(&xdr, args->new_dir, new->name, new->len);
738 return 0;
739}
740
741/*
Chuck Lever25a08662010-12-14 14:54:30 +0000742 * 2.2.13. linkargs
743 *
744 * struct linkargs {
745 * fhandle from;
746 * diropargs to;
747 * };
748 */
749static int nfs2_xdr_enc_linkargs(struct rpc_rqst *req, __be32 *p,
750 const struct nfs_linkargs *args)
751{
752 struct xdr_stream xdr;
753
754 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
755 encode_fhandle(&xdr, args->fromfh);
756 encode_diropargs(&xdr, args->tofh, args->toname, args->tolen);
757 return 0;
758}
759
760/*
Chuck Lever25a08662010-12-14 14:54:30 +0000761 * 2.2.14. symlinkargs
762 *
763 * struct symlinkargs {
764 * diropargs from;
765 * path to;
766 * sattr attributes;
767 * };
768 */
769static int nfs2_xdr_enc_symlinkargs(struct rpc_rqst *req, __be32 *p,
770 const struct nfs_symlinkargs *args)
771{
772 struct xdr_stream xdr;
773
774 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
775 encode_diropargs(&xdr, args->fromfh, args->fromname, args->fromlen);
776 encode_path(&xdr, args->pages, args->pathlen);
777 encode_sattr(&xdr, args->sattr);
778 return 0;
779}
780
781/*
Chuck Lever25a08662010-12-14 14:54:30 +0000782 * 2.2.17. readdirargs
783 *
784 * struct readdirargs {
785 * fhandle dir;
786 * nfscookie cookie;
787 * unsigned count;
788 * };
789 */
790static void encode_readdirargs(struct xdr_stream *xdr,
791 const struct nfs_readdirargs *args)
792{
793 __be32 *p;
794
795 encode_fhandle(xdr, args->fh);
796
797 p = xdr_reserve_space(xdr, 4 + 4);
798 *p++ = cpu_to_be32(args->cookie);
799 *p = cpu_to_be32(args->count);
800}
801
802static int nfs2_xdr_enc_readdirargs(struct rpc_rqst *req, __be32 *p,
803 const struct nfs_readdirargs *args)
804{
805 struct xdr_stream xdr;
806
807 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
808 encode_readdirargs(&xdr, args);
809 prepare_reply_buffer(req, args->pages, 0,
810 args->count, NFS_readdirres_sz);
811 return 0;
812}
813
814/*
Chuck Lever661ad422010-12-14 14:55:20 +0000815 * NFSv2 XDR decode functions
816 *
817 * NFSv2 result types are defined in section 2.2 of RFC 1094:
818 * "NFS: Network File System Protocol Specification".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Chuck Leverf796f8b2010-12-14 14:55:10 +0000821static int nfs2_xdr_dec_stat(struct rpc_rqst *req, __be32 *p,
822 void *__unused)
823{
824 struct xdr_stream xdr;
825 enum nfs_stat status;
826 int error;
827
828 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
829 error = decode_stat(&xdr, &status);
830 if (unlikely(error))
831 goto out;
832 if (status != NFS_OK)
833 goto out_default;
834out:
835 return error;
836out_default:
837 return nfs_stat_to_errno(status);
838}
839
Chuck Leverf796f8b2010-12-14 14:55:10 +0000840static int nfs2_xdr_dec_attrstat(struct rpc_rqst *req, __be32 *p,
841 struct nfs_fattr *result)
842{
843 struct xdr_stream xdr;
844
845 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
846 return decode_attrstat(&xdr, result);
847}
848
Chuck Leverf796f8b2010-12-14 14:55:10 +0000849static int nfs2_xdr_dec_diropres(struct rpc_rqst *req, __be32 *p,
850 struct nfs_diropok *result)
851{
852 struct xdr_stream xdr;
853
854 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
855 return decode_diropres(&xdr, result);
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/*
Chuck Leverf796f8b2010-12-14 14:55:10 +0000859 * 2.2.6. readlinkres
860 *
861 * union readlinkres switch (stat status) {
862 * case NFS_OK:
863 * path data;
864 * default:
865 * void;
866 * };
867 */
868static int nfs2_xdr_dec_readlinkres(struct rpc_rqst *req, __be32 *p,
869 void *__unused)
870{
871 struct xdr_stream xdr;
872 enum nfs_stat status;
873 int error;
874
875 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
876 error = decode_stat(&xdr, &status);
877 if (unlikely(error))
878 goto out;
879 if (status != NFS_OK)
880 goto out_default;
881 error = decode_path(&xdr);
882out:
883 return error;
884out_default:
885 return nfs_stat_to_errno(status);
886}
887
888/*
889 * 2.2.7. readres
890 *
891 * union readres switch (stat status) {
892 * case NFS_OK:
893 * fattr attributes;
894 * nfsdata data;
895 * default:
896 * void;
897 * };
898 */
899static int nfs2_xdr_dec_readres(struct rpc_rqst *req, __be32 *p,
900 struct nfs_readres *result)
901{
902 struct xdr_stream xdr;
903 enum nfs_stat status;
904 int error;
905
906 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
907 error = decode_stat(&xdr, &status);
908 if (unlikely(error))
909 goto out;
910 if (status != NFS_OK)
911 goto out_default;
912 error = decode_fattr(&xdr, result->fattr);
913 if (unlikely(error))
914 goto out;
915 error = decode_nfsdata(&xdr, result);
916out:
917 return error;
918out_default:
919 return nfs_stat_to_errno(status);
920}
921
Chuck Leverf796f8b2010-12-14 14:55:10 +0000922static int nfs2_xdr_dec_writeres(struct rpc_rqst *req, __be32 *p,
923 struct nfs_writeres *result)
924{
925 struct xdr_stream xdr;
926
927 /* All NFSv2 writes are "file sync" writes */
928 result->verf->committed = NFS_FILE_SYNC;
929
930 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
931 return decode_attrstat(&xdr, result->fattr);
932}
933
934/**
935 * nfs2_decode_dirent - Decode a single NFSv2 directory entry stored in
936 * the local page cache.
937 * @xdr: XDR stream where entry resides
938 * @entry: buffer to fill in with entry data
Chuck Leverf796f8b2010-12-14 14:55:10 +0000939 * @plus: boolean indicating whether this should be a readdirplus entry
940 *
Chuck Lever573c4e12010-12-14 14:58:11 +0000941 * Returns zero if successful, otherwise a negative errno value is
942 * returned.
Chuck Leverf796f8b2010-12-14 14:55:10 +0000943 *
944 * This function is not invoked during READDIR reply decoding, but
945 * rather whenever an application invokes the getdents(2) system call
946 * on a directory already in our cache.
947 *
948 * 2.2.17. entry
949 *
950 * struct entry {
951 * unsigned fileid;
952 * filename name;
953 * nfscookie cookie;
954 * entry *nextentry;
955 * };
956 */
Chuck Lever573c4e12010-12-14 14:58:11 +0000957int nfs2_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
958 int plus)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000959{
960 __be32 *p;
961 int error;
962
963 p = xdr_inline_decode(xdr, 4);
964 if (unlikely(p == NULL))
965 goto out_overflow;
966 if (*p++ == xdr_zero) {
967 p = xdr_inline_decode(xdr, 4);
968 if (unlikely(p == NULL))
969 goto out_overflow;
970 if (*p++ == xdr_zero)
Chuck Lever573c4e12010-12-14 14:58:11 +0000971 return -EAGAIN;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000972 entry->eof = 1;
Chuck Lever573c4e12010-12-14 14:58:11 +0000973 return -EBADCOOKIE;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000974 }
975
976 p = xdr_inline_decode(xdr, 4);
977 if (unlikely(p == NULL))
978 goto out_overflow;
979 entry->ino = be32_to_cpup(p);
980
981 error = decode_filename_inline(xdr, &entry->name, &entry->len);
982 if (unlikely(error))
Chuck Lever573c4e12010-12-14 14:58:11 +0000983 return error;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000984
985 /*
986 * The type (size and byte order) of nfscookie isn't defined in
987 * RFC 1094. This implementation assumes that it's an XDR uint32.
988 */
989 entry->prev_cookie = entry->cookie;
990 p = xdr_inline_decode(xdr, 4);
991 if (unlikely(p == NULL))
992 goto out_overflow;
993 entry->cookie = be32_to_cpup(p);
994
995 entry->d_type = DT_UNKNOWN;
996
997 /* Peek at the next entry to see if we're at EOD */
998 p = xdr_inline_peek(xdr, 4 + 4);
999 entry->eof = 0;
1000 if (p != NULL)
1001 entry->eof = (p[0] == xdr_zero) && (p[1] != xdr_zero);
Chuck Lever573c4e12010-12-14 14:58:11 +00001002 return 0;
Chuck Leverf796f8b2010-12-14 14:55:10 +00001003
1004out_overflow:
1005 print_overflow_msg(__func__, xdr);
Chuck Lever573c4e12010-12-14 14:58:11 +00001006 return -EAGAIN;
Chuck Leverf796f8b2010-12-14 14:55:10 +00001007}
1008
1009/*
1010 * 2.2.17. readdirres
1011 *
1012 * union readdirres switch (stat status) {
1013 * case NFS_OK:
1014 * struct {
1015 * entry *entries;
1016 * bool eof;
1017 * } readdirok;
1018 * default:
1019 * void;
1020 * };
1021 *
1022 * Read the directory contents into the page cache, but don't
1023 * touch them. The actual decoding is done by nfs2_decode_dirent()
1024 * during subsequent nfs_readdir() calls.
1025 */
1026static int decode_readdirok(struct xdr_stream *xdr)
1027{
1028 u32 recvd, pglen;
1029 size_t hdrlen;
1030
1031 pglen = xdr->buf->page_len;
1032 hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
1033 recvd = xdr->buf->len - hdrlen;
1034 if (unlikely(pglen > recvd))
1035 goto out_cheating;
1036out:
1037 xdr_read_pages(xdr, pglen);
1038 return pglen;
1039out_cheating:
1040 dprintk("NFS: server cheating in readdir result: "
1041 "pglen %u > recvd %u\n", pglen, recvd);
1042 pglen = recvd;
1043 goto out;
1044}
1045
1046static int nfs2_xdr_dec_readdirres(struct rpc_rqst *req, __be32 *p,
1047 void *__unused)
1048{
1049 struct xdr_stream xdr;
1050 enum nfs_stat status;
1051 int error;
1052
1053 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
1054 error = decode_stat(&xdr, &status);
1055 if (unlikely(error))
1056 goto out;
1057 if (status != NFS_OK)
1058 goto out_default;
1059 error = decode_readdirok(&xdr);
1060out:
1061 return error;
1062out_default:
1063 return nfs_stat_to_errno(status);
1064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/*
Chuck Leverf796f8b2010-12-14 14:55:10 +00001067 * 2.2.18. statfsres
1068 *
1069 * union statfsres (stat status) {
1070 * case NFS_OK:
1071 * struct {
1072 * unsigned tsize;
1073 * unsigned bsize;
1074 * unsigned blocks;
1075 * unsigned bfree;
1076 * unsigned bavail;
1077 * } info;
1078 * default:
1079 * void;
1080 * };
1081 */
1082static int decode_info(struct xdr_stream *xdr, struct nfs2_fsstat *result)
1083{
1084 __be32 *p;
1085
1086 p = xdr_inline_decode(xdr, NFS_info_sz << 2);
1087 if (unlikely(p == NULL))
1088 goto out_overflow;
1089 result->tsize = be32_to_cpup(p++);
1090 result->bsize = be32_to_cpup(p++);
1091 result->blocks = be32_to_cpup(p++);
1092 result->bfree = be32_to_cpup(p++);
1093 result->bavail = be32_to_cpup(p);
1094 return 0;
1095out_overflow:
1096 print_overflow_msg(__func__, xdr);
1097 return -EIO;
1098}
1099
1100static int nfs2_xdr_dec_statfsres(struct rpc_rqst *req, __be32 *p,
1101 struct nfs2_fsstat *result)
1102{
1103 struct xdr_stream xdr;
1104 enum nfs_stat status;
1105 int error;
1106
1107 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
1108 error = decode_stat(&xdr, &status);
1109 if (unlikely(error))
1110 goto out;
1111 if (status != NFS_OK)
1112 goto out_default;
1113 error = decode_info(&xdr, result);
1114out:
1115 return error;
1116out_default:
1117 return nfs_stat_to_errno(status);
1118}
1119
1120
1121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * We need to translate between nfs status return values and
1123 * the local errno values which may not be the same.
1124 */
Chuck Lever85828492010-12-14 14:55:00 +00001125static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int stat;
1127 int errno;
1128} nfs_errtbl[] = {
1129 { NFS_OK, 0 },
Benny Halevy856dff32008-03-31 17:39:06 +03001130 { NFSERR_PERM, -EPERM },
1131 { NFSERR_NOENT, -ENOENT },
1132 { NFSERR_IO, -errno_NFSERR_IO},
1133 { NFSERR_NXIO, -ENXIO },
1134/* { NFSERR_EAGAIN, -EAGAIN }, */
1135 { NFSERR_ACCES, -EACCES },
1136 { NFSERR_EXIST, -EEXIST },
1137 { NFSERR_XDEV, -EXDEV },
1138 { NFSERR_NODEV, -ENODEV },
1139 { NFSERR_NOTDIR, -ENOTDIR },
1140 { NFSERR_ISDIR, -EISDIR },
1141 { NFSERR_INVAL, -EINVAL },
1142 { NFSERR_FBIG, -EFBIG },
1143 { NFSERR_NOSPC, -ENOSPC },
1144 { NFSERR_ROFS, -EROFS },
1145 { NFSERR_MLINK, -EMLINK },
1146 { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
1147 { NFSERR_NOTEMPTY, -ENOTEMPTY },
1148 { NFSERR_DQUOT, -EDQUOT },
1149 { NFSERR_STALE, -ESTALE },
1150 { NFSERR_REMOTE, -EREMOTE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151#ifdef EWFLUSH
Benny Halevy856dff32008-03-31 17:39:06 +03001152 { NFSERR_WFLUSH, -EWFLUSH },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153#endif
Benny Halevy856dff32008-03-31 17:39:06 +03001154 { NFSERR_BADHANDLE, -EBADHANDLE },
1155 { NFSERR_NOT_SYNC, -ENOTSYNC },
1156 { NFSERR_BAD_COOKIE, -EBADCOOKIE },
1157 { NFSERR_NOTSUPP, -ENOTSUPP },
1158 { NFSERR_TOOSMALL, -ETOOSMALL },
Trond Myklebustfdcb4572010-02-08 09:32:40 -05001159 { NFSERR_SERVERFAULT, -EREMOTEIO },
Benny Halevy856dff32008-03-31 17:39:06 +03001160 { NFSERR_BADTYPE, -EBADTYPE },
1161 { NFSERR_JUKEBOX, -EJUKEBOX },
1162 { -1, -EIO }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163};
1164
Chuck Lever85828492010-12-14 14:55:00 +00001165/**
1166 * nfs_stat_to_errno - convert an NFS status code to a local errno
1167 * @status: NFS status code to convert
1168 *
1169 * Returns a local errno value, or -EIO if the NFS status code is
1170 * not recognized. This function is used jointly by NFSv2 and NFSv3.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Chuck Lever85828492010-12-14 14:55:00 +00001172int nfs_stat_to_errno(enum nfs_stat status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 int i;
1175
1176 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
Chuck Lever85828492010-12-14 14:55:00 +00001177 if (nfs_errtbl[i].stat == (int)status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return nfs_errtbl[i].errno;
1179 }
Chuck Lever85828492010-12-14 14:55:00 +00001180 dprintk("NFS: Unrecognized nfs status value: %u\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return nfs_errtbl[i].errno;
1182}
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184#define PROC(proc, argtype, restype, timer) \
1185[NFSPROC_##proc] = { \
1186 .p_proc = NFSPROC_##proc, \
Chuck Lever25a08662010-12-14 14:54:30 +00001187 .p_encode = (kxdrproc_t)nfs2_xdr_enc_##argtype, \
Chuck Leverf796f8b2010-12-14 14:55:10 +00001188 .p_decode = (kxdrproc_t)nfs2_xdr_dec_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -04001189 .p_arglen = NFS_##argtype##_sz, \
1190 .p_replen = NFS_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -05001191 .p_timer = timer, \
1192 .p_statidx = NFSPROC_##proc, \
1193 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195struct rpc_procinfo nfs_procedures[] = {
Chuck Lever7d93bd712010-12-14 14:57:42 +00001196 PROC(GETATTR, fhandle, attrstat, 1),
1197 PROC(SETATTR, sattrargs, attrstat, 0),
1198 PROC(LOOKUP, diropargs, diropres, 2),
1199 PROC(READLINK, readlinkargs, readlinkres, 3),
1200 PROC(READ, readargs, readres, 3),
1201 PROC(WRITE, writeargs, writeres, 4),
1202 PROC(CREATE, createargs, diropres, 0),
1203 PROC(REMOVE, removeargs, stat, 0),
1204 PROC(RENAME, renameargs, stat, 0),
1205 PROC(LINK, linkargs, stat, 0),
1206 PROC(SYMLINK, symlinkargs, stat, 0),
1207 PROC(MKDIR, createargs, diropres, 0),
1208 PROC(RMDIR, diropargs, stat, 0),
1209 PROC(READDIR, readdirargs, readdirres, 3),
1210 PROC(STATFS, fhandle, statfsres, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211};
1212
1213struct rpc_version nfs_version2 = {
1214 .number = 2,
Tobias Klausere8c96f82006-03-24 03:15:34 -08001215 .nrprocs = ARRAY_SIZE(nfs_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 .procs = nfs_procedures
1217};