xref: /linux/fs/nfs/nfs2xdr.c (revision 8f8d5745bb520c76b81abef4a2cb3023d0313bfd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/nfs/nfs2xdr.c
4  *
5  * XDR functions to encode/decode NFS RPC arguments and results.
6  *
7  * Copyright (C) 1992, 1993, 1994  Rick Sladkey
8  * Copyright (C) 1996 Olaf Kirch
9  * 04 Aug 1998  Ion Badulescu <ionut@cs.columbia.edu>
10  * 		FIFO's need special handling in NFSv2
11  */
12 
13 #include <linux/param.h>
14 #include <linux/time.h>
15 #include <linux/mm.h>
16 #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>
25 #include "nfstrace.h"
26 #include "internal.h"
27 
28 #define NFSDBG_FACILITY		NFSDBG_XDR
29 
30 /* Mapping from NFS error code to "errno" error code. */
31 #define errno_NFSERR_IO		EIO
32 
33 /*
34  * Declare the space requirements for NFS arguments and replies as
35  * number of 32bit-words
36  */
37 #define NFS_fhandle_sz		(8)
38 #define NFS_sattr_sz		(8)
39 #define NFS_filename_sz		(1+(NFS2_MAXNAMLEN>>2))
40 #define NFS_path_sz		(1+(NFS2_MAXPATHLEN>>2))
41 #define NFS_fattr_sz		(17)
42 #define NFS_info_sz		(5)
43 #define NFS_entry_sz		(NFS_filename_sz+3)
44 
45 #define NFS_diropargs_sz	(NFS_fhandle_sz+NFS_filename_sz)
46 #define NFS_removeargs_sz	(NFS_fhandle_sz+NFS_filename_sz)
47 #define NFS_sattrargs_sz	(NFS_fhandle_sz+NFS_sattr_sz)
48 #define NFS_readlinkargs_sz	(NFS_fhandle_sz)
49 #define NFS_readargs_sz		(NFS_fhandle_sz+3)
50 #define NFS_writeargs_sz	(NFS_fhandle_sz+4)
51 #define NFS_createargs_sz	(NFS_diropargs_sz+NFS_sattr_sz)
52 #define NFS_renameargs_sz	(NFS_diropargs_sz+NFS_diropargs_sz)
53 #define NFS_linkargs_sz		(NFS_fhandle_sz+NFS_diropargs_sz)
54 #define NFS_symlinkargs_sz	(NFS_diropargs_sz+1+NFS_sattr_sz)
55 #define NFS_readdirargs_sz	(NFS_fhandle_sz+2)
56 
57 #define NFS_attrstat_sz		(1+NFS_fattr_sz)
58 #define NFS_diropres_sz		(1+NFS_fhandle_sz+NFS_fattr_sz)
59 #define NFS_readlinkres_sz	(2+1)
60 #define NFS_readres_sz		(1+NFS_fattr_sz+1+1)
61 #define NFS_writeres_sz         (NFS_attrstat_sz)
62 #define NFS_stat_sz		(1)
63 #define NFS_readdirres_sz	(1+1)
64 #define NFS_statfsres_sz	(1+NFS_info_sz)
65 
66 static int nfs_stat_to_errno(enum nfs_stat);
67 
68 /*
69  * Encode/decode NFSv2 basic data types
70  *
71  * Basic NFSv2 data types are defined in section 2.3 of RFC 1094:
72  * "NFS: Network File System Protocol Specification".
73  *
74  * Not all basic data types have their own encoding and decoding
75  * functions.  For run-time efficiency, some data types are encoded
76  * or decoded inline.
77  */
78 
79 /*
80  *	typedef opaque	nfsdata<>;
81  */
82 static int decode_nfsdata(struct xdr_stream *xdr, struct nfs_pgio_res *result)
83 {
84 	u32 recvd, count;
85 	__be32 *p;
86 
87 	p = xdr_inline_decode(xdr, 4);
88 	if (unlikely(!p))
89 		return -EIO;
90 	count = be32_to_cpup(p);
91 	recvd = xdr_read_pages(xdr, count);
92 	if (unlikely(count > recvd))
93 		goto out_cheating;
94 out:
95 	result->eof = 0;	/* NFSv2 does not pass EOF flag on the wire. */
96 	result->count = count;
97 	return count;
98 out_cheating:
99 	dprintk("NFS: server cheating in read result: "
100 		"count %u > recvd %u\n", count, recvd);
101 	count = recvd;
102 	goto out;
103 }
104 
105 /*
106  *	enum stat {
107  *		NFS_OK = 0,
108  *		NFSERR_PERM = 1,
109  *		NFSERR_NOENT = 2,
110  *		NFSERR_IO = 5,
111  *		NFSERR_NXIO = 6,
112  *		NFSERR_ACCES = 13,
113  *		NFSERR_EXIST = 17,
114  *		NFSERR_NODEV = 19,
115  *		NFSERR_NOTDIR = 20,
116  *		NFSERR_ISDIR = 21,
117  *		NFSERR_FBIG = 27,
118  *		NFSERR_NOSPC = 28,
119  *		NFSERR_ROFS = 30,
120  *		NFSERR_NAMETOOLONG = 63,
121  *		NFSERR_NOTEMPTY = 66,
122  *		NFSERR_DQUOT = 69,
123  *		NFSERR_STALE = 70,
124  *		NFSERR_WFLUSH = 99
125  *	};
126  */
127 static int decode_stat(struct xdr_stream *xdr, enum nfs_stat *status)
128 {
129 	__be32 *p;
130 
131 	p = xdr_inline_decode(xdr, 4);
132 	if (unlikely(!p))
133 		return -EIO;
134 	if (unlikely(*p != cpu_to_be32(NFS_OK)))
135 		goto out_status;
136 	*status = 0;
137 	return 0;
138 out_status:
139 	*status = be32_to_cpup(p);
140 	trace_nfs_xdr_status((int)*status);
141 	return 0;
142 }
143 
144 /*
145  * 2.3.2.  ftype
146  *
147  *	enum ftype {
148  *		NFNON = 0,
149  *		NFREG = 1,
150  *		NFDIR = 2,
151  *		NFBLK = 3,
152  *		NFCHR = 4,
153  *		NFLNK = 5
154  *	};
155  *
156  */
157 static __be32 *xdr_decode_ftype(__be32 *p, u32 *type)
158 {
159 	*type = be32_to_cpup(p++);
160 	if (unlikely(*type > NF2FIFO))
161 		*type = NFBAD;
162 	return p;
163 }
164 
165 /*
166  * 2.3.3.  fhandle
167  *
168  *	typedef opaque fhandle[FHSIZE];
169  */
170 static void encode_fhandle(struct xdr_stream *xdr, const struct nfs_fh *fh)
171 {
172 	__be32 *p;
173 
174 	p = xdr_reserve_space(xdr, NFS2_FHSIZE);
175 	memcpy(p, fh->data, NFS2_FHSIZE);
176 }
177 
178 static int decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
179 {
180 	__be32 *p;
181 
182 	p = xdr_inline_decode(xdr, NFS2_FHSIZE);
183 	if (unlikely(!p))
184 		return -EIO;
185 	fh->size = NFS2_FHSIZE;
186 	memcpy(fh->data, p, NFS2_FHSIZE);
187 	return 0;
188 }
189 
190 /*
191  * 2.3.4.  timeval
192  *
193  *	struct timeval {
194  *		unsigned int seconds;
195  *		unsigned int useconds;
196  *	};
197  */
198 static __be32 *xdr_encode_time(__be32 *p, const struct timespec *timep)
199 {
200 	*p++ = cpu_to_be32(timep->tv_sec);
201 	if (timep->tv_nsec != 0)
202 		*p++ = cpu_to_be32(timep->tv_nsec / NSEC_PER_USEC);
203 	else
204 		*p++ = cpu_to_be32(0);
205 	return p;
206 }
207 
208 /*
209  * Passing the invalid value useconds=1000000 is a Sun convention for
210  * "set to current server time".  It's needed to make permissions checks
211  * for the "touch" program across v2 mounts to Solaris and Irix servers
212  * work correctly.  See description of sattr in section 6.1 of "NFS
213  * Illustrated" by Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5.
214  */
215 static __be32 *xdr_encode_current_server_time(__be32 *p,
216 					      const struct timespec *timep)
217 {
218 	*p++ = cpu_to_be32(timep->tv_sec);
219 	*p++ = cpu_to_be32(1000000);
220 	return p;
221 }
222 
223 static __be32 *xdr_decode_time(__be32 *p, struct timespec *timep)
224 {
225 	timep->tv_sec = be32_to_cpup(p++);
226 	timep->tv_nsec = be32_to_cpup(p++) * NSEC_PER_USEC;
227 	return p;
228 }
229 
230 /*
231  * 2.3.5.  fattr
232  *
233  *	struct fattr {
234  *		ftype		type;
235  *		unsigned int	mode;
236  *		unsigned int	nlink;
237  *		unsigned int	uid;
238  *		unsigned int	gid;
239  *		unsigned int	size;
240  *		unsigned int	blocksize;
241  *		unsigned int	rdev;
242  *		unsigned int	blocks;
243  *		unsigned int	fsid;
244  *		unsigned int	fileid;
245  *		timeval		atime;
246  *		timeval		mtime;
247  *		timeval		ctime;
248  *	};
249  *
250  */
251 static int decode_fattr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
252 {
253 	u32 rdev, type;
254 	__be32 *p;
255 
256 	p = xdr_inline_decode(xdr, NFS_fattr_sz << 2);
257 	if (unlikely(!p))
258 		return -EIO;
259 
260 	fattr->valid |= NFS_ATTR_FATTR_V2;
261 
262 	p = xdr_decode_ftype(p, &type);
263 
264 	fattr->mode = be32_to_cpup(p++);
265 	fattr->nlink = be32_to_cpup(p++);
266 	fattr->uid = make_kuid(&init_user_ns, be32_to_cpup(p++));
267 	if (!uid_valid(fattr->uid))
268 		goto out_uid;
269 	fattr->gid = make_kgid(&init_user_ns, be32_to_cpup(p++));
270 	if (!gid_valid(fattr->gid))
271 		goto out_gid;
272 
273 	fattr->size = be32_to_cpup(p++);
274 	fattr->du.nfs2.blocksize = be32_to_cpup(p++);
275 
276 	rdev = be32_to_cpup(p++);
277 	fattr->rdev = new_decode_dev(rdev);
278 	if (type == (u32)NFCHR && rdev == (u32)NFS2_FIFO_DEV) {
279 		fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
280 		fattr->rdev = 0;
281 	}
282 
283 	fattr->du.nfs2.blocks = be32_to_cpup(p++);
284 	fattr->fsid.major = be32_to_cpup(p++);
285 	fattr->fsid.minor = 0;
286 	fattr->fileid = be32_to_cpup(p++);
287 
288 	p = xdr_decode_time(p, &fattr->atime);
289 	p = xdr_decode_time(p, &fattr->mtime);
290 	xdr_decode_time(p, &fattr->ctime);
291 	fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
292 
293 	return 0;
294 out_uid:
295 	dprintk("NFS: returned invalid uid\n");
296 	return -EINVAL;
297 out_gid:
298 	dprintk("NFS: returned invalid gid\n");
299 	return -EINVAL;
300 }
301 
302 /*
303  * 2.3.6.  sattr
304  *
305  *	struct sattr {
306  *		unsigned int	mode;
307  *		unsigned int	uid;
308  *		unsigned int	gid;
309  *		unsigned int	size;
310  *		timeval		atime;
311  *		timeval		mtime;
312  *	};
313  */
314 
315 #define NFS2_SATTR_NOT_SET	(0xffffffff)
316 
317 static __be32 *xdr_time_not_set(__be32 *p)
318 {
319 	*p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
320 	*p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
321 	return p;
322 }
323 
324 static void encode_sattr(struct xdr_stream *xdr, const struct iattr *attr)
325 {
326 	struct timespec ts;
327 	__be32 *p;
328 
329 	p = xdr_reserve_space(xdr, NFS_sattr_sz << 2);
330 
331 	if (attr->ia_valid & ATTR_MODE)
332 		*p++ = cpu_to_be32(attr->ia_mode);
333 	else
334 		*p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
335 	if (attr->ia_valid & ATTR_UID)
336 		*p++ = cpu_to_be32(from_kuid(&init_user_ns, attr->ia_uid));
337 	else
338 		*p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
339 	if (attr->ia_valid & ATTR_GID)
340 		*p++ = cpu_to_be32(from_kgid(&init_user_ns, attr->ia_gid));
341 	else
342 		*p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
343 	if (attr->ia_valid & ATTR_SIZE)
344 		*p++ = cpu_to_be32((u32)attr->ia_size);
345 	else
346 		*p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
347 
348 	if (attr->ia_valid & ATTR_ATIME_SET) {
349 		ts = timespec64_to_timespec(attr->ia_atime);
350 		p = xdr_encode_time(p, &ts);
351 	} else if (attr->ia_valid & ATTR_ATIME) {
352 		ts = timespec64_to_timespec(attr->ia_atime);
353 		p = xdr_encode_current_server_time(p, &ts);
354 	} else
355 		p = xdr_time_not_set(p);
356 	if (attr->ia_valid & ATTR_MTIME_SET) {
357 		ts = timespec64_to_timespec(attr->ia_atime);
358 		xdr_encode_time(p, &ts);
359 	} else if (attr->ia_valid & ATTR_MTIME) {
360 		ts = timespec64_to_timespec(attr->ia_mtime);
361 		xdr_encode_current_server_time(p, &ts);
362 	} else
363 		xdr_time_not_set(p);
364 }
365 
366 /*
367  * 2.3.7.  filename
368  *
369  *	typedef string filename<MAXNAMLEN>;
370  */
371 static void encode_filename(struct xdr_stream *xdr,
372 			    const char *name, u32 length)
373 {
374 	__be32 *p;
375 
376 	WARN_ON_ONCE(length > NFS2_MAXNAMLEN);
377 	p = xdr_reserve_space(xdr, 4 + length);
378 	xdr_encode_opaque(p, name, length);
379 }
380 
381 static int decode_filename_inline(struct xdr_stream *xdr,
382 				  const char **name, u32 *length)
383 {
384 	__be32 *p;
385 	u32 count;
386 
387 	p = xdr_inline_decode(xdr, 4);
388 	if (unlikely(!p))
389 		return -EIO;
390 	count = be32_to_cpup(p);
391 	if (count > NFS3_MAXNAMLEN)
392 		goto out_nametoolong;
393 	p = xdr_inline_decode(xdr, count);
394 	if (unlikely(!p))
395 		return -EIO;
396 	*name = (const char *)p;
397 	*length = count;
398 	return 0;
399 out_nametoolong:
400 	dprintk("NFS: returned filename too long: %u\n", count);
401 	return -ENAMETOOLONG;
402 }
403 
404 /*
405  * 2.3.8.  path
406  *
407  *	typedef string path<MAXPATHLEN>;
408  */
409 static void encode_path(struct xdr_stream *xdr, struct page **pages, u32 length)
410 {
411 	__be32 *p;
412 
413 	p = xdr_reserve_space(xdr, 4);
414 	*p = cpu_to_be32(length);
415 	xdr_write_pages(xdr, pages, 0, length);
416 }
417 
418 static int decode_path(struct xdr_stream *xdr)
419 {
420 	u32 length, recvd;
421 	__be32 *p;
422 
423 	p = xdr_inline_decode(xdr, 4);
424 	if (unlikely(!p))
425 		return -EIO;
426 	length = be32_to_cpup(p);
427 	if (unlikely(length >= xdr->buf->page_len || length > NFS_MAXPATHLEN))
428 		goto out_size;
429 	recvd = xdr_read_pages(xdr, length);
430 	if (unlikely(length > recvd))
431 		goto out_cheating;
432 	xdr_terminate_string(xdr->buf, length);
433 	return 0;
434 out_size:
435 	dprintk("NFS: returned pathname too long: %u\n", length);
436 	return -ENAMETOOLONG;
437 out_cheating:
438 	dprintk("NFS: server cheating in pathname result: "
439 		"length %u > received %u\n", length, recvd);
440 	return -EIO;
441 }
442 
443 /*
444  * 2.3.9.  attrstat
445  *
446  *	union attrstat switch (stat status) {
447  *	case NFS_OK:
448  *		fattr attributes;
449  *	default:
450  *		void;
451  *	};
452  */
453 static int decode_attrstat(struct xdr_stream *xdr, struct nfs_fattr *result,
454 			   __u32 *op_status)
455 {
456 	enum nfs_stat status;
457 	int error;
458 
459 	error = decode_stat(xdr, &status);
460 	if (unlikely(error))
461 		goto out;
462 	if (op_status)
463 		*op_status = status;
464 	if (status != NFS_OK)
465 		goto out_default;
466 	error = decode_fattr(xdr, result);
467 out:
468 	return error;
469 out_default:
470 	return nfs_stat_to_errno(status);
471 }
472 
473 /*
474  * 2.3.10.  diropargs
475  *
476  *	struct diropargs {
477  *		fhandle  dir;
478  *		filename name;
479  *	};
480  */
481 static void encode_diropargs(struct xdr_stream *xdr, const struct nfs_fh *fh,
482 			     const char *name, u32 length)
483 {
484 	encode_fhandle(xdr, fh);
485 	encode_filename(xdr, name, length);
486 }
487 
488 /*
489  * 2.3.11.  diropres
490  *
491  *	union diropres switch (stat status) {
492  *	case NFS_OK:
493  *		struct {
494  *			fhandle file;
495  *			fattr   attributes;
496  *		} diropok;
497  *	default:
498  *		void;
499  *	};
500  */
501 static int decode_diropok(struct xdr_stream *xdr, struct nfs_diropok *result)
502 {
503 	int error;
504 
505 	error = decode_fhandle(xdr, result->fh);
506 	if (unlikely(error))
507 		goto out;
508 	error = decode_fattr(xdr, result->fattr);
509 out:
510 	return error;
511 }
512 
513 static int decode_diropres(struct xdr_stream *xdr, struct nfs_diropok *result)
514 {
515 	enum nfs_stat status;
516 	int error;
517 
518 	error = decode_stat(xdr, &status);
519 	if (unlikely(error))
520 		goto out;
521 	if (status != NFS_OK)
522 		goto out_default;
523 	error = decode_diropok(xdr, result);
524 out:
525 	return error;
526 out_default:
527 	return nfs_stat_to_errno(status);
528 }
529 
530 
531 /*
532  * NFSv2 XDR encode functions
533  *
534  * NFSv2 argument types are defined in section 2.2 of RFC 1094:
535  * "NFS: Network File System Protocol Specification".
536  */
537 
538 static void nfs2_xdr_enc_fhandle(struct rpc_rqst *req,
539 				 struct xdr_stream *xdr,
540 				 const void *data)
541 {
542 	const struct nfs_fh *fh = data;
543 
544 	encode_fhandle(xdr, fh);
545 }
546 
547 /*
548  * 2.2.3.  sattrargs
549  *
550  *	struct sattrargs {
551  *		fhandle file;
552  *		sattr attributes;
553  *	};
554  */
555 static void nfs2_xdr_enc_sattrargs(struct rpc_rqst *req,
556 				   struct xdr_stream *xdr,
557 				   const void *data)
558 {
559 	const struct nfs_sattrargs *args = data;
560 
561 	encode_fhandle(xdr, args->fh);
562 	encode_sattr(xdr, args->sattr);
563 }
564 
565 static void nfs2_xdr_enc_diropargs(struct rpc_rqst *req,
566 				   struct xdr_stream *xdr,
567 				   const void *data)
568 {
569 	const struct nfs_diropargs *args = data;
570 
571 	encode_diropargs(xdr, args->fh, args->name, args->len);
572 }
573 
574 static void nfs2_xdr_enc_readlinkargs(struct rpc_rqst *req,
575 				      struct xdr_stream *xdr,
576 				      const void *data)
577 {
578 	const struct nfs_readlinkargs *args = data;
579 
580 	encode_fhandle(xdr, args->fh);
581 	rpc_prepare_reply_pages(req, args->pages, args->pgbase,
582 				args->pglen, NFS_readlinkres_sz);
583 }
584 
585 /*
586  * 2.2.7.  readargs
587  *
588  *	struct readargs {
589  *		fhandle file;
590  *		unsigned offset;
591  *		unsigned count;
592  *		unsigned totalcount;
593  *	};
594  */
595 static void encode_readargs(struct xdr_stream *xdr,
596 			    const struct nfs_pgio_args *args)
597 {
598 	u32 offset = args->offset;
599 	u32 count = args->count;
600 	__be32 *p;
601 
602 	encode_fhandle(xdr, args->fh);
603 
604 	p = xdr_reserve_space(xdr, 4 + 4 + 4);
605 	*p++ = cpu_to_be32(offset);
606 	*p++ = cpu_to_be32(count);
607 	*p = cpu_to_be32(count);
608 }
609 
610 static void nfs2_xdr_enc_readargs(struct rpc_rqst *req,
611 				  struct xdr_stream *xdr,
612 				  const void *data)
613 {
614 	const struct nfs_pgio_args *args = data;
615 
616 	encode_readargs(xdr, args);
617 	rpc_prepare_reply_pages(req, args->pages, args->pgbase,
618 				args->count, NFS_readres_sz);
619 	req->rq_rcv_buf.flags |= XDRBUF_READ;
620 }
621 
622 /*
623  * 2.2.9.  writeargs
624  *
625  *	struct writeargs {
626  *		fhandle file;
627  *		unsigned beginoffset;
628  *		unsigned offset;
629  *		unsigned totalcount;
630  *		nfsdata data;
631  *	};
632  */
633 static void encode_writeargs(struct xdr_stream *xdr,
634 			     const struct nfs_pgio_args *args)
635 {
636 	u32 offset = args->offset;
637 	u32 count = args->count;
638 	__be32 *p;
639 
640 	encode_fhandle(xdr, args->fh);
641 
642 	p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
643 	*p++ = cpu_to_be32(offset);
644 	*p++ = cpu_to_be32(offset);
645 	*p++ = cpu_to_be32(count);
646 
647 	/* nfsdata */
648 	*p = cpu_to_be32(count);
649 	xdr_write_pages(xdr, args->pages, args->pgbase, count);
650 }
651 
652 static void nfs2_xdr_enc_writeargs(struct rpc_rqst *req,
653 				   struct xdr_stream *xdr,
654 				   const void *data)
655 {
656 	const struct nfs_pgio_args *args = data;
657 
658 	encode_writeargs(xdr, args);
659 	xdr->buf->flags |= XDRBUF_WRITE;
660 }
661 
662 /*
663  * 2.2.10.  createargs
664  *
665  *	struct createargs {
666  *		diropargs where;
667  *		sattr attributes;
668  *	};
669  */
670 static void nfs2_xdr_enc_createargs(struct rpc_rqst *req,
671 				    struct xdr_stream *xdr,
672 				    const void *data)
673 {
674 	const struct nfs_createargs *args = data;
675 
676 	encode_diropargs(xdr, args->fh, args->name, args->len);
677 	encode_sattr(xdr, args->sattr);
678 }
679 
680 static void nfs2_xdr_enc_removeargs(struct rpc_rqst *req,
681 				    struct xdr_stream *xdr,
682 				    const void *data)
683 {
684 	const struct nfs_removeargs *args = data;
685 
686 	encode_diropargs(xdr, args->fh, args->name.name, args->name.len);
687 }
688 
689 /*
690  * 2.2.12.  renameargs
691  *
692  *	struct renameargs {
693  *		diropargs from;
694  *		diropargs to;
695  *	};
696  */
697 static void nfs2_xdr_enc_renameargs(struct rpc_rqst *req,
698 				    struct xdr_stream *xdr,
699 				    const void *data)
700 {
701 	const struct nfs_renameargs *args = data;
702 	const struct qstr *old = args->old_name;
703 	const struct qstr *new = args->new_name;
704 
705 	encode_diropargs(xdr, args->old_dir, old->name, old->len);
706 	encode_diropargs(xdr, args->new_dir, new->name, new->len);
707 }
708 
709 /*
710  * 2.2.13.  linkargs
711  *
712  *	struct linkargs {
713  *		fhandle from;
714  *		diropargs to;
715  *	};
716  */
717 static void nfs2_xdr_enc_linkargs(struct rpc_rqst *req,
718 				  struct xdr_stream *xdr,
719 				  const void *data)
720 {
721 	const struct nfs_linkargs *args = data;
722 
723 	encode_fhandle(xdr, args->fromfh);
724 	encode_diropargs(xdr, args->tofh, args->toname, args->tolen);
725 }
726 
727 /*
728  * 2.2.14.  symlinkargs
729  *
730  *	struct symlinkargs {
731  *		diropargs from;
732  *		path to;
733  *		sattr attributes;
734  *	};
735  */
736 static void nfs2_xdr_enc_symlinkargs(struct rpc_rqst *req,
737 				     struct xdr_stream *xdr,
738 				     const void *data)
739 {
740 	const struct nfs_symlinkargs *args = data;
741 
742 	encode_diropargs(xdr, args->fromfh, args->fromname, args->fromlen);
743 	encode_path(xdr, args->pages, args->pathlen);
744 	encode_sattr(xdr, args->sattr);
745 }
746 
747 /*
748  * 2.2.17.  readdirargs
749  *
750  *	struct readdirargs {
751  *		fhandle dir;
752  *		nfscookie cookie;
753  *		unsigned count;
754  *	};
755  */
756 static void encode_readdirargs(struct xdr_stream *xdr,
757 			       const struct nfs_readdirargs *args)
758 {
759 	__be32 *p;
760 
761 	encode_fhandle(xdr, args->fh);
762 
763 	p = xdr_reserve_space(xdr, 4 + 4);
764 	*p++ = cpu_to_be32(args->cookie);
765 	*p = cpu_to_be32(args->count);
766 }
767 
768 static void nfs2_xdr_enc_readdirargs(struct rpc_rqst *req,
769 				     struct xdr_stream *xdr,
770 				     const void *data)
771 {
772 	const struct nfs_readdirargs *args = data;
773 
774 	encode_readdirargs(xdr, args);
775 	rpc_prepare_reply_pages(req, args->pages, 0,
776 				args->count, NFS_readdirres_sz);
777 }
778 
779 /*
780  * NFSv2 XDR decode functions
781  *
782  * NFSv2 result types are defined in section 2.2 of RFC 1094:
783  * "NFS: Network File System Protocol Specification".
784  */
785 
786 static int nfs2_xdr_dec_stat(struct rpc_rqst *req, struct xdr_stream *xdr,
787 			     void *__unused)
788 {
789 	enum nfs_stat status;
790 	int error;
791 
792 	error = decode_stat(xdr, &status);
793 	if (unlikely(error))
794 		goto out;
795 	if (status != NFS_OK)
796 		goto out_default;
797 out:
798 	return error;
799 out_default:
800 	return nfs_stat_to_errno(status);
801 }
802 
803 static int nfs2_xdr_dec_attrstat(struct rpc_rqst *req, struct xdr_stream *xdr,
804 				 void *result)
805 {
806 	return decode_attrstat(xdr, result, NULL);
807 }
808 
809 static int nfs2_xdr_dec_diropres(struct rpc_rqst *req, struct xdr_stream *xdr,
810 				 void *result)
811 {
812 	return decode_diropres(xdr, result);
813 }
814 
815 /*
816  * 2.2.6.  readlinkres
817  *
818  *	union readlinkres switch (stat status) {
819  *	case NFS_OK:
820  *		path data;
821  *	default:
822  *		void;
823  *	};
824  */
825 static int nfs2_xdr_dec_readlinkres(struct rpc_rqst *req,
826 				    struct xdr_stream *xdr, void *__unused)
827 {
828 	enum nfs_stat status;
829 	int error;
830 
831 	error = decode_stat(xdr, &status);
832 	if (unlikely(error))
833 		goto out;
834 	if (status != NFS_OK)
835 		goto out_default;
836 	error = decode_path(xdr);
837 out:
838 	return error;
839 out_default:
840 	return nfs_stat_to_errno(status);
841 }
842 
843 /*
844  * 2.2.7.  readres
845  *
846  *	union readres switch (stat status) {
847  *	case NFS_OK:
848  *		fattr attributes;
849  *		nfsdata data;
850  *	default:
851  *		void;
852  *	};
853  */
854 static int nfs2_xdr_dec_readres(struct rpc_rqst *req, struct xdr_stream *xdr,
855 				void *data)
856 {
857 	struct nfs_pgio_res *result = data;
858 	enum nfs_stat status;
859 	int error;
860 
861 	error = decode_stat(xdr, &status);
862 	if (unlikely(error))
863 		goto out;
864 	result->op_status = status;
865 	if (status != NFS_OK)
866 		goto out_default;
867 	error = decode_fattr(xdr, result->fattr);
868 	if (unlikely(error))
869 		goto out;
870 	error = decode_nfsdata(xdr, result);
871 out:
872 	return error;
873 out_default:
874 	return nfs_stat_to_errno(status);
875 }
876 
877 static int nfs2_xdr_dec_writeres(struct rpc_rqst *req, struct xdr_stream *xdr,
878 				 void *data)
879 {
880 	struct nfs_pgio_res *result = data;
881 
882 	/* All NFSv2 writes are "file sync" writes */
883 	result->verf->committed = NFS_FILE_SYNC;
884 	return decode_attrstat(xdr, result->fattr, &result->op_status);
885 }
886 
887 /**
888  * nfs2_decode_dirent - Decode a single NFSv2 directory entry stored in
889  *                      the local page cache.
890  * @xdr: XDR stream where entry resides
891  * @entry: buffer to fill in with entry data
892  * @plus: boolean indicating whether this should be a readdirplus entry
893  *
894  * Returns zero if successful, otherwise a negative errno value is
895  * returned.
896  *
897  * This function is not invoked during READDIR reply decoding, but
898  * rather whenever an application invokes the getdents(2) system call
899  * on a directory already in our cache.
900  *
901  * 2.2.17.  entry
902  *
903  *	struct entry {
904  *		unsigned	fileid;
905  *		filename	name;
906  *		nfscookie	cookie;
907  *		entry		*nextentry;
908  *	};
909  */
910 int nfs2_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
911 		       bool plus)
912 {
913 	__be32 *p;
914 	int error;
915 
916 	p = xdr_inline_decode(xdr, 4);
917 	if (unlikely(!p))
918 		return -EAGAIN;
919 	if (*p++ == xdr_zero) {
920 		p = xdr_inline_decode(xdr, 4);
921 		if (unlikely(!p))
922 			return -EAGAIN;
923 		if (*p++ == xdr_zero)
924 			return -EAGAIN;
925 		entry->eof = 1;
926 		return -EBADCOOKIE;
927 	}
928 
929 	p = xdr_inline_decode(xdr, 4);
930 	if (unlikely(!p))
931 		return -EAGAIN;
932 	entry->ino = be32_to_cpup(p);
933 
934 	error = decode_filename_inline(xdr, &entry->name, &entry->len);
935 	if (unlikely(error))
936 		return error;
937 
938 	/*
939 	 * The type (size and byte order) of nfscookie isn't defined in
940 	 * RFC 1094.  This implementation assumes that it's an XDR uint32.
941 	 */
942 	entry->prev_cookie = entry->cookie;
943 	p = xdr_inline_decode(xdr, 4);
944 	if (unlikely(!p))
945 		return -EAGAIN;
946 	entry->cookie = be32_to_cpup(p);
947 
948 	entry->d_type = DT_UNKNOWN;
949 
950 	return 0;
951 }
952 
953 /*
954  * 2.2.17.  readdirres
955  *
956  *	union readdirres switch (stat status) {
957  *	case NFS_OK:
958  *		struct {
959  *			entry *entries;
960  *			bool eof;
961  *		} readdirok;
962  *	default:
963  *		void;
964  *	};
965  *
966  * Read the directory contents into the page cache, but don't
967  * touch them.  The actual decoding is done by nfs2_decode_dirent()
968  * during subsequent nfs_readdir() calls.
969  */
970 static int decode_readdirok(struct xdr_stream *xdr)
971 {
972 	return xdr_read_pages(xdr, xdr->buf->page_len);
973 }
974 
975 static int nfs2_xdr_dec_readdirres(struct rpc_rqst *req,
976 				   struct xdr_stream *xdr, void *__unused)
977 {
978 	enum nfs_stat status;
979 	int error;
980 
981 	error = decode_stat(xdr, &status);
982 	if (unlikely(error))
983 		goto out;
984 	if (status != NFS_OK)
985 		goto out_default;
986 	error = decode_readdirok(xdr);
987 out:
988 	return error;
989 out_default:
990 	return nfs_stat_to_errno(status);
991 }
992 
993 /*
994  * 2.2.18.  statfsres
995  *
996  *	union statfsres (stat status) {
997  *	case NFS_OK:
998  *		struct {
999  *			unsigned tsize;
1000  *			unsigned bsize;
1001  *			unsigned blocks;
1002  *			unsigned bfree;
1003  *			unsigned bavail;
1004  *		} info;
1005  *	default:
1006  *		void;
1007  *	};
1008  */
1009 static int decode_info(struct xdr_stream *xdr, struct nfs2_fsstat *result)
1010 {
1011 	__be32 *p;
1012 
1013 	p = xdr_inline_decode(xdr, NFS_info_sz << 2);
1014 	if (unlikely(!p))
1015 		return -EIO;
1016 	result->tsize  = be32_to_cpup(p++);
1017 	result->bsize  = be32_to_cpup(p++);
1018 	result->blocks = be32_to_cpup(p++);
1019 	result->bfree  = be32_to_cpup(p++);
1020 	result->bavail = be32_to_cpup(p);
1021 	return 0;
1022 }
1023 
1024 static int nfs2_xdr_dec_statfsres(struct rpc_rqst *req, struct xdr_stream *xdr,
1025 				  void *result)
1026 {
1027 	enum nfs_stat status;
1028 	int error;
1029 
1030 	error = decode_stat(xdr, &status);
1031 	if (unlikely(error))
1032 		goto out;
1033 	if (status != NFS_OK)
1034 		goto out_default;
1035 	error = decode_info(xdr, result);
1036 out:
1037 	return error;
1038 out_default:
1039 	return nfs_stat_to_errno(status);
1040 }
1041 
1042 
1043 /*
1044  * We need to translate between nfs status return values and
1045  * the local errno values which may not be the same.
1046  */
1047 static const struct {
1048 	int stat;
1049 	int errno;
1050 } nfs_errtbl[] = {
1051 	{ NFS_OK,		0		},
1052 	{ NFSERR_PERM,		-EPERM		},
1053 	{ NFSERR_NOENT,		-ENOENT		},
1054 	{ NFSERR_IO,		-errno_NFSERR_IO},
1055 	{ NFSERR_NXIO,		-ENXIO		},
1056 /*	{ NFSERR_EAGAIN,	-EAGAIN		}, */
1057 	{ NFSERR_ACCES,		-EACCES		},
1058 	{ NFSERR_EXIST,		-EEXIST		},
1059 	{ NFSERR_XDEV,		-EXDEV		},
1060 	{ NFSERR_NODEV,		-ENODEV		},
1061 	{ NFSERR_NOTDIR,	-ENOTDIR	},
1062 	{ NFSERR_ISDIR,		-EISDIR		},
1063 	{ NFSERR_INVAL,		-EINVAL		},
1064 	{ NFSERR_FBIG,		-EFBIG		},
1065 	{ NFSERR_NOSPC,		-ENOSPC		},
1066 	{ NFSERR_ROFS,		-EROFS		},
1067 	{ NFSERR_MLINK,		-EMLINK		},
1068 	{ NFSERR_NAMETOOLONG,	-ENAMETOOLONG	},
1069 	{ NFSERR_NOTEMPTY,	-ENOTEMPTY	},
1070 	{ NFSERR_DQUOT,		-EDQUOT		},
1071 	{ NFSERR_STALE,		-ESTALE		},
1072 	{ NFSERR_REMOTE,	-EREMOTE	},
1073 #ifdef EWFLUSH
1074 	{ NFSERR_WFLUSH,	-EWFLUSH	},
1075 #endif
1076 	{ NFSERR_BADHANDLE,	-EBADHANDLE	},
1077 	{ NFSERR_NOT_SYNC,	-ENOTSYNC	},
1078 	{ NFSERR_BAD_COOKIE,	-EBADCOOKIE	},
1079 	{ NFSERR_NOTSUPP,	-ENOTSUPP	},
1080 	{ NFSERR_TOOSMALL,	-ETOOSMALL	},
1081 	{ NFSERR_SERVERFAULT,	-EREMOTEIO	},
1082 	{ NFSERR_BADTYPE,	-EBADTYPE	},
1083 	{ NFSERR_JUKEBOX,	-EJUKEBOX	},
1084 	{ -1,			-EIO		}
1085 };
1086 
1087 /**
1088  * nfs_stat_to_errno - convert an NFS status code to a local errno
1089  * @status: NFS status code to convert
1090  *
1091  * Returns a local errno value, or -EIO if the NFS status code is
1092  * not recognized.  This function is used jointly by NFSv2 and NFSv3.
1093  */
1094 static int nfs_stat_to_errno(enum nfs_stat status)
1095 {
1096 	int i;
1097 
1098 	for (i = 0; nfs_errtbl[i].stat != -1; i++) {
1099 		if (nfs_errtbl[i].stat == (int)status)
1100 			return nfs_errtbl[i].errno;
1101 	}
1102 	dprintk("NFS: Unrecognized nfs status value: %u\n", status);
1103 	return nfs_errtbl[i].errno;
1104 }
1105 
1106 #define PROC(proc, argtype, restype, timer)				\
1107 [NFSPROC_##proc] = {							\
1108 	.p_proc	    =  NFSPROC_##proc,					\
1109 	.p_encode   =  nfs2_xdr_enc_##argtype,				\
1110 	.p_decode   =  nfs2_xdr_dec_##restype,				\
1111 	.p_arglen   =  NFS_##argtype##_sz,				\
1112 	.p_replen   =  NFS_##restype##_sz,				\
1113 	.p_timer    =  timer,						\
1114 	.p_statidx  =  NFSPROC_##proc,					\
1115 	.p_name     =  #proc,						\
1116 	}
1117 const struct rpc_procinfo nfs_procedures[] = {
1118 	PROC(GETATTR,	fhandle,	attrstat,	1),
1119 	PROC(SETATTR,	sattrargs,	attrstat,	0),
1120 	PROC(LOOKUP,	diropargs,	diropres,	2),
1121 	PROC(READLINK,	readlinkargs,	readlinkres,	3),
1122 	PROC(READ,	readargs,	readres,	3),
1123 	PROC(WRITE,	writeargs,	writeres,	4),
1124 	PROC(CREATE,	createargs,	diropres,	0),
1125 	PROC(REMOVE,	removeargs,	stat,		0),
1126 	PROC(RENAME,	renameargs,	stat,		0),
1127 	PROC(LINK,	linkargs,	stat,		0),
1128 	PROC(SYMLINK,	symlinkargs,	stat,		0),
1129 	PROC(MKDIR,	createargs,	diropres,	0),
1130 	PROC(RMDIR,	diropargs,	stat,		0),
1131 	PROC(READDIR,	readdirargs,	readdirres,	3),
1132 	PROC(STATFS,	fhandle,	statfsres,	0),
1133 };
1134 
1135 static unsigned int nfs_version2_counts[ARRAY_SIZE(nfs_procedures)];
1136 const struct rpc_version nfs_version2 = {
1137 	.number			= 2,
1138 	.nrprocs		= ARRAY_SIZE(nfs_procedures),
1139 	.procs			= nfs_procedures,
1140 	.counts			= nfs_version2_counts,
1141 };
1142