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