xref: /openbmc/linux/fs/nfs/nfs3xdr.c (revision 431f6eb3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/nfs/nfs3xdr.c
4  *
5  * XDR functions to encode/decode NFSv3 RPC arguments and results.
6  *
7  * Copyright (C) 1996, 1997 Olaf Kirch
8  */
9 
10 #include <linux/param.h>
11 #include <linux/time.h>
12 #include <linux/mm.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/in.h>
16 #include <linux/pagemap.h>
17 #include <linux/proc_fs.h>
18 #include <linux/kdev_t.h>
19 #include <linux/sunrpc/clnt.h>
20 #include <linux/nfs.h>
21 #include <linux/nfs3.h>
22 #include <linux/nfs_fs.h>
23 #include <linux/nfsacl.h>
24 #include "internal.h"
25 
26 #define NFSDBG_FACILITY		NFSDBG_XDR
27 
28 /* 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 NFS3_fhandle_sz		(1+16)
36 #define NFS3_fh_sz		(NFS3_fhandle_sz)	/* shorthand */
37 #define NFS3_sattr_sz		(15)
38 #define NFS3_filename_sz	(1+(NFS3_MAXNAMLEN>>2))
39 #define NFS3_path_sz		(1+(NFS3_MAXPATHLEN>>2))
40 #define NFS3_fattr_sz		(21)
41 #define NFS3_cookieverf_sz	(NFS3_COOKIEVERFSIZE>>2)
42 #define NFS3_wcc_attr_sz	(6)
43 #define NFS3_pre_op_attr_sz	(1+NFS3_wcc_attr_sz)
44 #define NFS3_post_op_attr_sz	(1+NFS3_fattr_sz)
45 #define NFS3_wcc_data_sz	(NFS3_pre_op_attr_sz+NFS3_post_op_attr_sz)
46 #define NFS3_diropargs_sz	(NFS3_fh_sz+NFS3_filename_sz)
47 
48 #define NFS3_getattrargs_sz	(NFS3_fh_sz)
49 #define NFS3_setattrargs_sz	(NFS3_fh_sz+NFS3_sattr_sz+3)
50 #define NFS3_lookupargs_sz	(NFS3_fh_sz+NFS3_filename_sz)
51 #define NFS3_accessargs_sz	(NFS3_fh_sz+1)
52 #define NFS3_readlinkargs_sz	(NFS3_fh_sz)
53 #define NFS3_readargs_sz	(NFS3_fh_sz+3)
54 #define NFS3_writeargs_sz	(NFS3_fh_sz+5)
55 #define NFS3_createargs_sz	(NFS3_diropargs_sz+NFS3_sattr_sz)
56 #define NFS3_mkdirargs_sz	(NFS3_diropargs_sz+NFS3_sattr_sz)
57 #define NFS3_symlinkargs_sz	(NFS3_diropargs_sz+1+NFS3_sattr_sz)
58 #define NFS3_mknodargs_sz	(NFS3_diropargs_sz+2+NFS3_sattr_sz)
59 #define NFS3_removeargs_sz	(NFS3_fh_sz+NFS3_filename_sz)
60 #define NFS3_renameargs_sz	(NFS3_diropargs_sz+NFS3_diropargs_sz)
61 #define NFS3_linkargs_sz		(NFS3_fh_sz+NFS3_diropargs_sz)
62 #define NFS3_readdirargs_sz	(NFS3_fh_sz+NFS3_cookieverf_sz+3)
63 #define NFS3_readdirplusargs_sz	(NFS3_fh_sz+NFS3_cookieverf_sz+4)
64 #define NFS3_commitargs_sz	(NFS3_fh_sz+3)
65 
66 #define NFS3_getattrres_sz	(1+NFS3_fattr_sz)
67 #define NFS3_setattrres_sz	(1+NFS3_wcc_data_sz)
68 #define NFS3_removeres_sz	(NFS3_setattrres_sz)
69 #define NFS3_lookupres_sz	(1+NFS3_fh_sz+(2 * NFS3_post_op_attr_sz))
70 #define NFS3_accessres_sz	(1+NFS3_post_op_attr_sz+1)
71 #define NFS3_readlinkres_sz	(1+NFS3_post_op_attr_sz+1)
72 #define NFS3_readres_sz		(1+NFS3_post_op_attr_sz+3)
73 #define NFS3_writeres_sz	(1+NFS3_wcc_data_sz+4)
74 #define NFS3_createres_sz	(1+NFS3_fh_sz+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
75 #define NFS3_renameres_sz	(1+(2 * NFS3_wcc_data_sz))
76 #define NFS3_linkres_sz		(1+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
77 #define NFS3_readdirres_sz	(1+NFS3_post_op_attr_sz+2)
78 #define NFS3_fsstatres_sz	(1+NFS3_post_op_attr_sz+13)
79 #define NFS3_fsinfores_sz	(1+NFS3_post_op_attr_sz+12)
80 #define NFS3_pathconfres_sz	(1+NFS3_post_op_attr_sz+6)
81 #define NFS3_commitres_sz	(1+NFS3_wcc_data_sz+2)
82 
83 #define ACL3_getaclargs_sz	(NFS3_fh_sz+1)
84 #define ACL3_setaclargs_sz	(NFS3_fh_sz+1+ \
85 				XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
86 #define ACL3_getaclres_sz	(1+NFS3_post_op_attr_sz+1+ \
87 				XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
88 #define ACL3_setaclres_sz	(1+NFS3_post_op_attr_sz)
89 
90 static int nfs3_stat_to_errno(enum nfs_stat);
91 
92 /*
93  * Map file type to S_IFMT bits
94  */
95 static const umode_t nfs_type2fmt[] = {
96 	[NF3BAD] = 0,
97 	[NF3REG] = S_IFREG,
98 	[NF3DIR] = S_IFDIR,
99 	[NF3BLK] = S_IFBLK,
100 	[NF3CHR] = S_IFCHR,
101 	[NF3LNK] = S_IFLNK,
102 	[NF3SOCK] = S_IFSOCK,
103 	[NF3FIFO] = S_IFIFO,
104 };
105 
106 /*
107  * While encoding arguments, set up the reply buffer in advance to
108  * receive reply data directly into the page cache.
109  */
110 static void prepare_reply_buffer(struct rpc_rqst *req, struct page **pages,
111 				 unsigned int base, unsigned int len,
112 				 unsigned int bufsize)
113 {
114 	struct rpc_auth	*auth = req->rq_cred->cr_auth;
115 	unsigned int replen;
116 
117 	replen = RPC_REPHDRSIZE + auth->au_rslack + bufsize;
118 	xdr_inline_pages(&req->rq_rcv_buf, replen << 2, pages, base, len);
119 }
120 
121 /*
122  * Handle decode buffer overflows out-of-line.
123  */
124 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
125 {
126 	dprintk("NFS: %s prematurely hit the end of our receive buffer. "
127 		"Remaining buffer length is %tu words.\n",
128 		func, xdr->end - xdr->p);
129 }
130 
131 
132 /*
133  * Encode/decode NFSv3 basic data types
134  *
135  * Basic NFSv3 data types are defined in section 2.5 of RFC 1813:
136  * "NFS Version 3 Protocol Specification".
137  *
138  * Not all basic data types have their own encoding and decoding
139  * functions.  For run-time efficiency, some data types are encoded
140  * or decoded inline.
141  */
142 
143 static void encode_uint32(struct xdr_stream *xdr, u32 value)
144 {
145 	__be32 *p = xdr_reserve_space(xdr, 4);
146 	*p = cpu_to_be32(value);
147 }
148 
149 static int decode_uint32(struct xdr_stream *xdr, u32 *value)
150 {
151 	__be32 *p;
152 
153 	p = xdr_inline_decode(xdr, 4);
154 	if (unlikely(p == NULL))
155 		goto out_overflow;
156 	*value = be32_to_cpup(p);
157 	return 0;
158 out_overflow:
159 	print_overflow_msg(__func__, xdr);
160 	return -EIO;
161 }
162 
163 static int decode_uint64(struct xdr_stream *xdr, u64 *value)
164 {
165 	__be32 *p;
166 
167 	p = xdr_inline_decode(xdr, 8);
168 	if (unlikely(p == NULL))
169 		goto out_overflow;
170 	xdr_decode_hyper(p, value);
171 	return 0;
172 out_overflow:
173 	print_overflow_msg(__func__, xdr);
174 	return -EIO;
175 }
176 
177 /*
178  * fileid3
179  *
180  *	typedef uint64 fileid3;
181  */
182 static __be32 *xdr_decode_fileid3(__be32 *p, u64 *fileid)
183 {
184 	return xdr_decode_hyper(p, fileid);
185 }
186 
187 static int decode_fileid3(struct xdr_stream *xdr, u64 *fileid)
188 {
189 	return decode_uint64(xdr, fileid);
190 }
191 
192 /*
193  * filename3
194  *
195  *	typedef string filename3<>;
196  */
197 static void encode_filename3(struct xdr_stream *xdr,
198 			     const char *name, u32 length)
199 {
200 	__be32 *p;
201 
202 	WARN_ON_ONCE(length > NFS3_MAXNAMLEN);
203 	p = xdr_reserve_space(xdr, 4 + length);
204 	xdr_encode_opaque(p, name, length);
205 }
206 
207 static int decode_inline_filename3(struct xdr_stream *xdr,
208 				   const char **name, u32 *length)
209 {
210 	__be32 *p;
211 	u32 count;
212 
213 	p = xdr_inline_decode(xdr, 4);
214 	if (unlikely(p == NULL))
215 		goto out_overflow;
216 	count = be32_to_cpup(p);
217 	if (count > NFS3_MAXNAMLEN)
218 		goto out_nametoolong;
219 	p = xdr_inline_decode(xdr, count);
220 	if (unlikely(p == NULL))
221 		goto out_overflow;
222 	*name = (const char *)p;
223 	*length = count;
224 	return 0;
225 
226 out_nametoolong:
227 	dprintk("NFS: returned filename too long: %u\n", count);
228 	return -ENAMETOOLONG;
229 out_overflow:
230 	print_overflow_msg(__func__, xdr);
231 	return -EIO;
232 }
233 
234 /*
235  * nfspath3
236  *
237  *	typedef string nfspath3<>;
238  */
239 static void encode_nfspath3(struct xdr_stream *xdr, struct page **pages,
240 			    const u32 length)
241 {
242 	encode_uint32(xdr, length);
243 	xdr_write_pages(xdr, pages, 0, length);
244 }
245 
246 static int decode_nfspath3(struct xdr_stream *xdr)
247 {
248 	u32 recvd, count;
249 	__be32 *p;
250 
251 	p = xdr_inline_decode(xdr, 4);
252 	if (unlikely(p == NULL))
253 		goto out_overflow;
254 	count = be32_to_cpup(p);
255 	if (unlikely(count >= xdr->buf->page_len || count > NFS3_MAXPATHLEN))
256 		goto out_nametoolong;
257 	recvd = xdr_read_pages(xdr, count);
258 	if (unlikely(count > recvd))
259 		goto out_cheating;
260 	xdr_terminate_string(xdr->buf, count);
261 	return 0;
262 
263 out_nametoolong:
264 	dprintk("NFS: returned pathname too long: %u\n", count);
265 	return -ENAMETOOLONG;
266 out_cheating:
267 	dprintk("NFS: server cheating in pathname result: "
268 		"count %u > recvd %u\n", count, recvd);
269 	return -EIO;
270 out_overflow:
271 	print_overflow_msg(__func__, xdr);
272 	return -EIO;
273 }
274 
275 /*
276  * cookie3
277  *
278  *	typedef uint64 cookie3
279  */
280 static __be32 *xdr_encode_cookie3(__be32 *p, u64 cookie)
281 {
282 	return xdr_encode_hyper(p, cookie);
283 }
284 
285 static int decode_cookie3(struct xdr_stream *xdr, u64 *cookie)
286 {
287 	return decode_uint64(xdr, cookie);
288 }
289 
290 /*
291  * cookieverf3
292  *
293  *	typedef opaque cookieverf3[NFS3_COOKIEVERFSIZE];
294  */
295 static __be32 *xdr_encode_cookieverf3(__be32 *p, const __be32 *verifier)
296 {
297 	memcpy(p, verifier, NFS3_COOKIEVERFSIZE);
298 	return p + XDR_QUADLEN(NFS3_COOKIEVERFSIZE);
299 }
300 
301 static int decode_cookieverf3(struct xdr_stream *xdr, __be32 *verifier)
302 {
303 	__be32 *p;
304 
305 	p = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
306 	if (unlikely(p == NULL))
307 		goto out_overflow;
308 	memcpy(verifier, p, NFS3_COOKIEVERFSIZE);
309 	return 0;
310 out_overflow:
311 	print_overflow_msg(__func__, xdr);
312 	return -EIO;
313 }
314 
315 /*
316  * createverf3
317  *
318  *	typedef opaque createverf3[NFS3_CREATEVERFSIZE];
319  */
320 static void encode_createverf3(struct xdr_stream *xdr, const __be32 *verifier)
321 {
322 	__be32 *p;
323 
324 	p = xdr_reserve_space(xdr, NFS3_CREATEVERFSIZE);
325 	memcpy(p, verifier, NFS3_CREATEVERFSIZE);
326 }
327 
328 static int decode_writeverf3(struct xdr_stream *xdr, struct nfs_write_verifier *verifier)
329 {
330 	__be32 *p;
331 
332 	p = xdr_inline_decode(xdr, NFS3_WRITEVERFSIZE);
333 	if (unlikely(p == NULL))
334 		goto out_overflow;
335 	memcpy(verifier->data, p, NFS3_WRITEVERFSIZE);
336 	return 0;
337 out_overflow:
338 	print_overflow_msg(__func__, xdr);
339 	return -EIO;
340 }
341 
342 /*
343  * size3
344  *
345  *	typedef uint64 size3;
346  */
347 static __be32 *xdr_decode_size3(__be32 *p, u64 *size)
348 {
349 	return xdr_decode_hyper(p, size);
350 }
351 
352 /*
353  * nfsstat3
354  *
355  *	enum nfsstat3 {
356  *		NFS3_OK = 0,
357  *		...
358  *	}
359  */
360 #define NFS3_OK		NFS_OK
361 
362 static int decode_nfsstat3(struct xdr_stream *xdr, enum nfs_stat *status)
363 {
364 	__be32 *p;
365 
366 	p = xdr_inline_decode(xdr, 4);
367 	if (unlikely(p == NULL))
368 		goto out_overflow;
369 	*status = be32_to_cpup(p);
370 	return 0;
371 out_overflow:
372 	print_overflow_msg(__func__, xdr);
373 	return -EIO;
374 }
375 
376 /*
377  * ftype3
378  *
379  *	enum ftype3 {
380  *		NF3REG	= 1,
381  *		NF3DIR	= 2,
382  *		NF3BLK	= 3,
383  *		NF3CHR	= 4,
384  *		NF3LNK	= 5,
385  *		NF3SOCK	= 6,
386  *		NF3FIFO	= 7
387  *	};
388  */
389 static void encode_ftype3(struct xdr_stream *xdr, const u32 type)
390 {
391 	encode_uint32(xdr, type);
392 }
393 
394 static __be32 *xdr_decode_ftype3(__be32 *p, umode_t *mode)
395 {
396 	u32 type;
397 
398 	type = be32_to_cpup(p++);
399 	if (type > NF3FIFO)
400 		type = NF3NON;
401 	*mode = nfs_type2fmt[type];
402 	return p;
403 }
404 
405 /*
406  * specdata3
407  *
408  *     struct specdata3 {
409  *             uint32  specdata1;
410  *             uint32  specdata2;
411  *     };
412  */
413 static void encode_specdata3(struct xdr_stream *xdr, const dev_t rdev)
414 {
415 	__be32 *p;
416 
417 	p = xdr_reserve_space(xdr, 8);
418 	*p++ = cpu_to_be32(MAJOR(rdev));
419 	*p = cpu_to_be32(MINOR(rdev));
420 }
421 
422 static __be32 *xdr_decode_specdata3(__be32 *p, dev_t *rdev)
423 {
424 	unsigned int major, minor;
425 
426 	major = be32_to_cpup(p++);
427 	minor = be32_to_cpup(p++);
428 	*rdev = MKDEV(major, minor);
429 	if (MAJOR(*rdev) != major || MINOR(*rdev) != minor)
430 		*rdev = 0;
431 	return p;
432 }
433 
434 /*
435  * nfs_fh3
436  *
437  *	struct nfs_fh3 {
438  *		opaque       data<NFS3_FHSIZE>;
439  *	};
440  */
441 static void encode_nfs_fh3(struct xdr_stream *xdr, const struct nfs_fh *fh)
442 {
443 	__be32 *p;
444 
445 	WARN_ON_ONCE(fh->size > NFS3_FHSIZE);
446 	p = xdr_reserve_space(xdr, 4 + fh->size);
447 	xdr_encode_opaque(p, fh->data, fh->size);
448 }
449 
450 static int decode_nfs_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
451 {
452 	u32 length;
453 	__be32 *p;
454 
455 	p = xdr_inline_decode(xdr, 4);
456 	if (unlikely(p == NULL))
457 		goto out_overflow;
458 	length = be32_to_cpup(p++);
459 	if (unlikely(length > NFS3_FHSIZE))
460 		goto out_toobig;
461 	p = xdr_inline_decode(xdr, length);
462 	if (unlikely(p == NULL))
463 		goto out_overflow;
464 	fh->size = length;
465 	memcpy(fh->data, p, length);
466 	return 0;
467 out_toobig:
468 	dprintk("NFS: file handle size (%u) too big\n", length);
469 	return -E2BIG;
470 out_overflow:
471 	print_overflow_msg(__func__, xdr);
472 	return -EIO;
473 }
474 
475 static void zero_nfs_fh3(struct nfs_fh *fh)
476 {
477 	memset(fh, 0, sizeof(*fh));
478 }
479 
480 /*
481  * nfstime3
482  *
483  *	struct nfstime3 {
484  *		uint32	seconds;
485  *		uint32	nseconds;
486  *	};
487  */
488 static __be32 *xdr_encode_nfstime3(__be32 *p, const struct timespec *timep)
489 {
490 	*p++ = cpu_to_be32(timep->tv_sec);
491 	*p++ = cpu_to_be32(timep->tv_nsec);
492 	return p;
493 }
494 
495 static __be32 *xdr_decode_nfstime3(__be32 *p, struct timespec *timep)
496 {
497 	timep->tv_sec = be32_to_cpup(p++);
498 	timep->tv_nsec = be32_to_cpup(p++);
499 	return p;
500 }
501 
502 /*
503  * sattr3
504  *
505  *	enum time_how {
506  *		DONT_CHANGE		= 0,
507  *		SET_TO_SERVER_TIME	= 1,
508  *		SET_TO_CLIENT_TIME	= 2
509  *	};
510  *
511  *	union set_mode3 switch (bool set_it) {
512  *	case TRUE:
513  *		mode3	mode;
514  *	default:
515  *		void;
516  *	};
517  *
518  *	union set_uid3 switch (bool set_it) {
519  *	case TRUE:
520  *		uid3	uid;
521  *	default:
522  *		void;
523  *	};
524  *
525  *	union set_gid3 switch (bool set_it) {
526  *	case TRUE:
527  *		gid3	gid;
528  *	default:
529  *		void;
530  *	};
531  *
532  *	union set_size3 switch (bool set_it) {
533  *	case TRUE:
534  *		size3	size;
535  *	default:
536  *		void;
537  *	};
538  *
539  *	union set_atime switch (time_how set_it) {
540  *	case SET_TO_CLIENT_TIME:
541  *		nfstime3	atime;
542  *	default:
543  *		void;
544  *	};
545  *
546  *	union set_mtime switch (time_how set_it) {
547  *	case SET_TO_CLIENT_TIME:
548  *		nfstime3  mtime;
549  *	default:
550  *		void;
551  *	};
552  *
553  *	struct sattr3 {
554  *		set_mode3	mode;
555  *		set_uid3	uid;
556  *		set_gid3	gid;
557  *		set_size3	size;
558  *		set_atime	atime;
559  *		set_mtime	mtime;
560  *	};
561  */
562 static void encode_sattr3(struct xdr_stream *xdr, const struct iattr *attr)
563 {
564 	struct timespec ts;
565 	u32 nbytes;
566 	__be32 *p;
567 
568 	/*
569 	 * In order to make only a single xdr_reserve_space() call,
570 	 * pre-compute the total number of bytes to be reserved.
571 	 * Six boolean values, one for each set_foo field, are always
572 	 * present in the encoded result, so start there.
573 	 */
574 	nbytes = 6 * 4;
575 	if (attr->ia_valid & ATTR_MODE)
576 		nbytes += 4;
577 	if (attr->ia_valid & ATTR_UID)
578 		nbytes += 4;
579 	if (attr->ia_valid & ATTR_GID)
580 		nbytes += 4;
581 	if (attr->ia_valid & ATTR_SIZE)
582 		nbytes += 8;
583 	if (attr->ia_valid & ATTR_ATIME_SET)
584 		nbytes += 8;
585 	if (attr->ia_valid & ATTR_MTIME_SET)
586 		nbytes += 8;
587 	p = xdr_reserve_space(xdr, nbytes);
588 
589 	if (attr->ia_valid & ATTR_MODE) {
590 		*p++ = xdr_one;
591 		*p++ = cpu_to_be32(attr->ia_mode & S_IALLUGO);
592 	} else
593 		*p++ = xdr_zero;
594 
595 	if (attr->ia_valid & ATTR_UID) {
596 		*p++ = xdr_one;
597 		*p++ = cpu_to_be32(from_kuid(&init_user_ns, attr->ia_uid));
598 	} else
599 		*p++ = xdr_zero;
600 
601 	if (attr->ia_valid & ATTR_GID) {
602 		*p++ = xdr_one;
603 		*p++ = cpu_to_be32(from_kgid(&init_user_ns, attr->ia_gid));
604 	} else
605 		*p++ = xdr_zero;
606 
607 	if (attr->ia_valid & ATTR_SIZE) {
608 		*p++ = xdr_one;
609 		p = xdr_encode_hyper(p, (u64)attr->ia_size);
610 	} else
611 		*p++ = xdr_zero;
612 
613 	if (attr->ia_valid & ATTR_ATIME_SET) {
614 		struct timespec ts;
615 		*p++ = xdr_two;
616 		ts = timespec64_to_timespec(attr->ia_atime);
617 		p = xdr_encode_nfstime3(p, &ts);
618 	} else if (attr->ia_valid & ATTR_ATIME) {
619 		*p++ = xdr_one;
620 	} else
621 		*p++ = xdr_zero;
622 
623 	if (attr->ia_valid & ATTR_MTIME_SET) {
624 		*p++ = xdr_two;
625 		ts = timespec64_to_timespec(attr->ia_mtime);
626 		xdr_encode_nfstime3(p, &ts);
627 	} else if (attr->ia_valid & ATTR_MTIME) {
628 		*p = xdr_one;
629 	} else
630 		*p = xdr_zero;
631 }
632 
633 /*
634  * fattr3
635  *
636  *	struct fattr3 {
637  *		ftype3		type;
638  *		mode3		mode;
639  *		uint32		nlink;
640  *		uid3		uid;
641  *		gid3		gid;
642  *		size3		size;
643  *		size3		used;
644  *		specdata3	rdev;
645  *		uint64		fsid;
646  *		fileid3		fileid;
647  *		nfstime3	atime;
648  *		nfstime3	mtime;
649  *		nfstime3	ctime;
650  *	};
651  */
652 static int decode_fattr3(struct xdr_stream *xdr, struct nfs_fattr *fattr)
653 {
654 	umode_t fmode;
655 	__be32 *p;
656 
657 	p = xdr_inline_decode(xdr, NFS3_fattr_sz << 2);
658 	if (unlikely(p == NULL))
659 		goto out_overflow;
660 
661 	p = xdr_decode_ftype3(p, &fmode);
662 
663 	fattr->mode = (be32_to_cpup(p++) & ~S_IFMT) | fmode;
664 	fattr->nlink = be32_to_cpup(p++);
665 	fattr->uid = make_kuid(&init_user_ns, be32_to_cpup(p++));
666 	if (!uid_valid(fattr->uid))
667 		goto out_uid;
668 	fattr->gid = make_kgid(&init_user_ns, be32_to_cpup(p++));
669 	if (!gid_valid(fattr->gid))
670 		goto out_gid;
671 
672 	p = xdr_decode_size3(p, &fattr->size);
673 	p = xdr_decode_size3(p, &fattr->du.nfs3.used);
674 	p = xdr_decode_specdata3(p, &fattr->rdev);
675 
676 	p = xdr_decode_hyper(p, &fattr->fsid.major);
677 	fattr->fsid.minor = 0;
678 
679 	p = xdr_decode_fileid3(p, &fattr->fileid);
680 	p = xdr_decode_nfstime3(p, &fattr->atime);
681 	p = xdr_decode_nfstime3(p, &fattr->mtime);
682 	xdr_decode_nfstime3(p, &fattr->ctime);
683 	fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
684 
685 	fattr->valid |= NFS_ATTR_FATTR_V3;
686 	return 0;
687 out_uid:
688 	dprintk("NFS: returned invalid uid\n");
689 	return -EINVAL;
690 out_gid:
691 	dprintk("NFS: returned invalid gid\n");
692 	return -EINVAL;
693 out_overflow:
694 	print_overflow_msg(__func__, xdr);
695 	return -EIO;
696 }
697 
698 /*
699  * post_op_attr
700  *
701  *	union post_op_attr switch (bool attributes_follow) {
702  *	case TRUE:
703  *		fattr3	attributes;
704  *	case FALSE:
705  *		void;
706  *	};
707  */
708 static int decode_post_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
709 {
710 	__be32 *p;
711 
712 	p = xdr_inline_decode(xdr, 4);
713 	if (unlikely(p == NULL))
714 		goto out_overflow;
715 	if (*p != xdr_zero)
716 		return decode_fattr3(xdr, fattr);
717 	return 0;
718 out_overflow:
719 	print_overflow_msg(__func__, xdr);
720 	return -EIO;
721 }
722 
723 /*
724  * wcc_attr
725  *	struct wcc_attr {
726  *		size3		size;
727  *		nfstime3	mtime;
728  *		nfstime3	ctime;
729  *	};
730  */
731 static int decode_wcc_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
732 {
733 	__be32 *p;
734 
735 	p = xdr_inline_decode(xdr, NFS3_wcc_attr_sz << 2);
736 	if (unlikely(p == NULL))
737 		goto out_overflow;
738 
739 	fattr->valid |= NFS_ATTR_FATTR_PRESIZE
740 		| NFS_ATTR_FATTR_PRECHANGE
741 		| NFS_ATTR_FATTR_PREMTIME
742 		| NFS_ATTR_FATTR_PRECTIME;
743 
744 	p = xdr_decode_size3(p, &fattr->pre_size);
745 	p = xdr_decode_nfstime3(p, &fattr->pre_mtime);
746 	xdr_decode_nfstime3(p, &fattr->pre_ctime);
747 	fattr->pre_change_attr = nfs_timespec_to_change_attr(&fattr->pre_ctime);
748 
749 	return 0;
750 out_overflow:
751 	print_overflow_msg(__func__, xdr);
752 	return -EIO;
753 }
754 
755 /*
756  * pre_op_attr
757  *	union pre_op_attr switch (bool attributes_follow) {
758  *	case TRUE:
759  *		wcc_attr	attributes;
760  *	case FALSE:
761  *		void;
762  *	};
763  *
764  * wcc_data
765  *
766  *	struct wcc_data {
767  *		pre_op_attr	before;
768  *		post_op_attr	after;
769  *	};
770  */
771 static int decode_pre_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
772 {
773 	__be32 *p;
774 
775 	p = xdr_inline_decode(xdr, 4);
776 	if (unlikely(p == NULL))
777 		goto out_overflow;
778 	if (*p != xdr_zero)
779 		return decode_wcc_attr(xdr, fattr);
780 	return 0;
781 out_overflow:
782 	print_overflow_msg(__func__, xdr);
783 	return -EIO;
784 }
785 
786 static int decode_wcc_data(struct xdr_stream *xdr, struct nfs_fattr *fattr)
787 {
788 	int error;
789 
790 	error = decode_pre_op_attr(xdr, fattr);
791 	if (unlikely(error))
792 		goto out;
793 	error = decode_post_op_attr(xdr, fattr);
794 out:
795 	return error;
796 }
797 
798 /*
799  * post_op_fh3
800  *
801  *	union post_op_fh3 switch (bool handle_follows) {
802  *	case TRUE:
803  *		nfs_fh3  handle;
804  *	case FALSE:
805  *		void;
806  *	};
807  */
808 static int decode_post_op_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
809 {
810 	__be32 *p = xdr_inline_decode(xdr, 4);
811 	if (unlikely(p == NULL))
812 		goto out_overflow;
813 	if (*p != xdr_zero)
814 		return decode_nfs_fh3(xdr, fh);
815 	zero_nfs_fh3(fh);
816 	return 0;
817 out_overflow:
818 	print_overflow_msg(__func__, xdr);
819 	return -EIO;
820 }
821 
822 /*
823  * diropargs3
824  *
825  *	struct diropargs3 {
826  *		nfs_fh3		dir;
827  *		filename3	name;
828  *	};
829  */
830 static void encode_diropargs3(struct xdr_stream *xdr, const struct nfs_fh *fh,
831 			      const char *name, u32 length)
832 {
833 	encode_nfs_fh3(xdr, fh);
834 	encode_filename3(xdr, name, length);
835 }
836 
837 
838 /*
839  * NFSv3 XDR encode functions
840  *
841  * NFSv3 argument types are defined in section 3.3 of RFC 1813:
842  * "NFS Version 3 Protocol Specification".
843  */
844 
845 /*
846  * 3.3.1  GETATTR3args
847  *
848  *	struct GETATTR3args {
849  *		nfs_fh3  object;
850  *	};
851  */
852 static void nfs3_xdr_enc_getattr3args(struct rpc_rqst *req,
853 				      struct xdr_stream *xdr,
854 				      const void *data)
855 {
856 	const struct nfs_fh *fh = data;
857 
858 	encode_nfs_fh3(xdr, fh);
859 }
860 
861 /*
862  * 3.3.2  SETATTR3args
863  *
864  *	union sattrguard3 switch (bool check) {
865  *	case TRUE:
866  *		nfstime3  obj_ctime;
867  *	case FALSE:
868  *		void;
869  *	};
870  *
871  *	struct SETATTR3args {
872  *		nfs_fh3		object;
873  *		sattr3		new_attributes;
874  *		sattrguard3	guard;
875  *	};
876  */
877 static void encode_sattrguard3(struct xdr_stream *xdr,
878 			       const struct nfs3_sattrargs *args)
879 {
880 	__be32 *p;
881 
882 	if (args->guard) {
883 		p = xdr_reserve_space(xdr, 4 + 8);
884 		*p++ = xdr_one;
885 		xdr_encode_nfstime3(p, &args->guardtime);
886 	} else {
887 		p = xdr_reserve_space(xdr, 4);
888 		*p = xdr_zero;
889 	}
890 }
891 
892 static void nfs3_xdr_enc_setattr3args(struct rpc_rqst *req,
893 				      struct xdr_stream *xdr,
894 				      const void *data)
895 {
896 	const struct nfs3_sattrargs *args = data;
897 	encode_nfs_fh3(xdr, args->fh);
898 	encode_sattr3(xdr, args->sattr);
899 	encode_sattrguard3(xdr, args);
900 }
901 
902 /*
903  * 3.3.3  LOOKUP3args
904  *
905  *	struct LOOKUP3args {
906  *		diropargs3  what;
907  *	};
908  */
909 static void nfs3_xdr_enc_lookup3args(struct rpc_rqst *req,
910 				     struct xdr_stream *xdr,
911 				     const void *data)
912 {
913 	const struct nfs3_diropargs *args = data;
914 
915 	encode_diropargs3(xdr, args->fh, args->name, args->len);
916 }
917 
918 /*
919  * 3.3.4  ACCESS3args
920  *
921  *	struct ACCESS3args {
922  *		nfs_fh3		object;
923  *		uint32		access;
924  *	};
925  */
926 static void encode_access3args(struct xdr_stream *xdr,
927 			       const struct nfs3_accessargs *args)
928 {
929 	encode_nfs_fh3(xdr, args->fh);
930 	encode_uint32(xdr, args->access);
931 }
932 
933 static void nfs3_xdr_enc_access3args(struct rpc_rqst *req,
934 				     struct xdr_stream *xdr,
935 				     const void *data)
936 {
937 	const struct nfs3_accessargs *args = data;
938 
939 	encode_access3args(xdr, args);
940 }
941 
942 /*
943  * 3.3.5  READLINK3args
944  *
945  *	struct READLINK3args {
946  *		nfs_fh3	symlink;
947  *	};
948  */
949 static void nfs3_xdr_enc_readlink3args(struct rpc_rqst *req,
950 				       struct xdr_stream *xdr,
951 				       const void *data)
952 {
953 	const struct nfs3_readlinkargs *args = data;
954 
955 	encode_nfs_fh3(xdr, args->fh);
956 	prepare_reply_buffer(req, args->pages, args->pgbase,
957 					args->pglen, NFS3_readlinkres_sz);
958 }
959 
960 /*
961  * 3.3.6  READ3args
962  *
963  *	struct READ3args {
964  *		nfs_fh3		file;
965  *		offset3		offset;
966  *		count3		count;
967  *	};
968  */
969 static void encode_read3args(struct xdr_stream *xdr,
970 			     const struct nfs_pgio_args *args)
971 {
972 	__be32 *p;
973 
974 	encode_nfs_fh3(xdr, args->fh);
975 
976 	p = xdr_reserve_space(xdr, 8 + 4);
977 	p = xdr_encode_hyper(p, args->offset);
978 	*p = cpu_to_be32(args->count);
979 }
980 
981 static void nfs3_xdr_enc_read3args(struct rpc_rqst *req,
982 				   struct xdr_stream *xdr,
983 				   const void *data)
984 {
985 	const struct nfs_pgio_args *args = data;
986 
987 	encode_read3args(xdr, args);
988 	prepare_reply_buffer(req, args->pages, args->pgbase,
989 					args->count, NFS3_readres_sz);
990 	req->rq_rcv_buf.flags |= XDRBUF_READ;
991 }
992 
993 /*
994  * 3.3.7  WRITE3args
995  *
996  *	enum stable_how {
997  *		UNSTABLE  = 0,
998  *		DATA_SYNC = 1,
999  *		FILE_SYNC = 2
1000  *	};
1001  *
1002  *	struct WRITE3args {
1003  *		nfs_fh3		file;
1004  *		offset3		offset;
1005  *		count3		count;
1006  *		stable_how	stable;
1007  *		opaque		data<>;
1008  *	};
1009  */
1010 static void encode_write3args(struct xdr_stream *xdr,
1011 			      const struct nfs_pgio_args *args)
1012 {
1013 	__be32 *p;
1014 
1015 	encode_nfs_fh3(xdr, args->fh);
1016 
1017 	p = xdr_reserve_space(xdr, 8 + 4 + 4 + 4);
1018 	p = xdr_encode_hyper(p, args->offset);
1019 	*p++ = cpu_to_be32(args->count);
1020 	*p++ = cpu_to_be32(args->stable);
1021 	*p = cpu_to_be32(args->count);
1022 	xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1023 }
1024 
1025 static void nfs3_xdr_enc_write3args(struct rpc_rqst *req,
1026 				    struct xdr_stream *xdr,
1027 				    const void *data)
1028 {
1029 	const struct nfs_pgio_args *args = data;
1030 
1031 	encode_write3args(xdr, args);
1032 	xdr->buf->flags |= XDRBUF_WRITE;
1033 }
1034 
1035 /*
1036  * 3.3.8  CREATE3args
1037  *
1038  *	enum createmode3 {
1039  *		UNCHECKED = 0,
1040  *		GUARDED   = 1,
1041  *		EXCLUSIVE = 2
1042  *	};
1043  *
1044  *	union createhow3 switch (createmode3 mode) {
1045  *	case UNCHECKED:
1046  *	case GUARDED:
1047  *		sattr3       obj_attributes;
1048  *	case EXCLUSIVE:
1049  *		createverf3  verf;
1050  *	};
1051  *
1052  *	struct CREATE3args {
1053  *		diropargs3	where;
1054  *		createhow3	how;
1055  *	};
1056  */
1057 static void encode_createhow3(struct xdr_stream *xdr,
1058 			      const struct nfs3_createargs *args)
1059 {
1060 	encode_uint32(xdr, args->createmode);
1061 	switch (args->createmode) {
1062 	case NFS3_CREATE_UNCHECKED:
1063 	case NFS3_CREATE_GUARDED:
1064 		encode_sattr3(xdr, args->sattr);
1065 		break;
1066 	case NFS3_CREATE_EXCLUSIVE:
1067 		encode_createverf3(xdr, args->verifier);
1068 		break;
1069 	default:
1070 		BUG();
1071 	}
1072 }
1073 
1074 static void nfs3_xdr_enc_create3args(struct rpc_rqst *req,
1075 				     struct xdr_stream *xdr,
1076 				     const void *data)
1077 {
1078 	const struct nfs3_createargs *args = data;
1079 
1080 	encode_diropargs3(xdr, args->fh, args->name, args->len);
1081 	encode_createhow3(xdr, args);
1082 }
1083 
1084 /*
1085  * 3.3.9  MKDIR3args
1086  *
1087  *	struct MKDIR3args {
1088  *		diropargs3	where;
1089  *		sattr3		attributes;
1090  *	};
1091  */
1092 static void nfs3_xdr_enc_mkdir3args(struct rpc_rqst *req,
1093 				    struct xdr_stream *xdr,
1094 				    const void *data)
1095 {
1096 	const struct nfs3_mkdirargs *args = data;
1097 
1098 	encode_diropargs3(xdr, args->fh, args->name, args->len);
1099 	encode_sattr3(xdr, args->sattr);
1100 }
1101 
1102 /*
1103  * 3.3.10  SYMLINK3args
1104  *
1105  *	struct symlinkdata3 {
1106  *		sattr3		symlink_attributes;
1107  *		nfspath3	symlink_data;
1108  *	};
1109  *
1110  *	struct SYMLINK3args {
1111  *		diropargs3	where;
1112  *		symlinkdata3	symlink;
1113  *	};
1114  */
1115 static void encode_symlinkdata3(struct xdr_stream *xdr,
1116 				const void *data)
1117 {
1118 	const struct nfs3_symlinkargs *args = data;
1119 
1120 	encode_sattr3(xdr, args->sattr);
1121 	encode_nfspath3(xdr, args->pages, args->pathlen);
1122 }
1123 
1124 static void nfs3_xdr_enc_symlink3args(struct rpc_rqst *req,
1125 				      struct xdr_stream *xdr,
1126 				      const void *data)
1127 {
1128 	const struct nfs3_symlinkargs *args = data;
1129 
1130 	encode_diropargs3(xdr, args->fromfh, args->fromname, args->fromlen);
1131 	encode_symlinkdata3(xdr, args);
1132 	xdr->buf->flags |= XDRBUF_WRITE;
1133 }
1134 
1135 /*
1136  * 3.3.11  MKNOD3args
1137  *
1138  *	struct devicedata3 {
1139  *		sattr3		dev_attributes;
1140  *		specdata3	spec;
1141  *	};
1142  *
1143  *	union mknoddata3 switch (ftype3 type) {
1144  *	case NF3CHR:
1145  *	case NF3BLK:
1146  *		devicedata3	device;
1147  *	case NF3SOCK:
1148  *	case NF3FIFO:
1149  *		sattr3		pipe_attributes;
1150  *	default:
1151  *		void;
1152  *	};
1153  *
1154  *	struct MKNOD3args {
1155  *		diropargs3	where;
1156  *		mknoddata3	what;
1157  *	};
1158  */
1159 static void encode_devicedata3(struct xdr_stream *xdr,
1160 			       const struct nfs3_mknodargs *args)
1161 {
1162 	encode_sattr3(xdr, args->sattr);
1163 	encode_specdata3(xdr, args->rdev);
1164 }
1165 
1166 static void encode_mknoddata3(struct xdr_stream *xdr,
1167 			      const struct nfs3_mknodargs *args)
1168 {
1169 	encode_ftype3(xdr, args->type);
1170 	switch (args->type) {
1171 	case NF3CHR:
1172 	case NF3BLK:
1173 		encode_devicedata3(xdr, args);
1174 		break;
1175 	case NF3SOCK:
1176 	case NF3FIFO:
1177 		encode_sattr3(xdr, args->sattr);
1178 		break;
1179 	case NF3REG:
1180 	case NF3DIR:
1181 		break;
1182 	default:
1183 		BUG();
1184 	}
1185 }
1186 
1187 static void nfs3_xdr_enc_mknod3args(struct rpc_rqst *req,
1188 				    struct xdr_stream *xdr,
1189 				    const void *data)
1190 {
1191 	const struct nfs3_mknodargs *args = data;
1192 
1193 	encode_diropargs3(xdr, args->fh, args->name, args->len);
1194 	encode_mknoddata3(xdr, args);
1195 }
1196 
1197 /*
1198  * 3.3.12  REMOVE3args
1199  *
1200  *	struct REMOVE3args {
1201  *		diropargs3  object;
1202  *	};
1203  */
1204 static void nfs3_xdr_enc_remove3args(struct rpc_rqst *req,
1205 				     struct xdr_stream *xdr,
1206 				     const void *data)
1207 {
1208 	const struct nfs_removeargs *args = data;
1209 
1210 	encode_diropargs3(xdr, args->fh, args->name.name, args->name.len);
1211 }
1212 
1213 /*
1214  * 3.3.14  RENAME3args
1215  *
1216  *	struct RENAME3args {
1217  *		diropargs3	from;
1218  *		diropargs3	to;
1219  *	};
1220  */
1221 static void nfs3_xdr_enc_rename3args(struct rpc_rqst *req,
1222 				     struct xdr_stream *xdr,
1223 				     const void *data)
1224 {
1225 	const struct nfs_renameargs *args = data;
1226 	const struct qstr *old = args->old_name;
1227 	const struct qstr *new = args->new_name;
1228 
1229 	encode_diropargs3(xdr, args->old_dir, old->name, old->len);
1230 	encode_diropargs3(xdr, args->new_dir, new->name, new->len);
1231 }
1232 
1233 /*
1234  * 3.3.15  LINK3args
1235  *
1236  *	struct LINK3args {
1237  *		nfs_fh3		file;
1238  *		diropargs3	link;
1239  *	};
1240  */
1241 static void nfs3_xdr_enc_link3args(struct rpc_rqst *req,
1242 				   struct xdr_stream *xdr,
1243 				   const void *data)
1244 {
1245 	const struct nfs3_linkargs *args = data;
1246 
1247 	encode_nfs_fh3(xdr, args->fromfh);
1248 	encode_diropargs3(xdr, args->tofh, args->toname, args->tolen);
1249 }
1250 
1251 /*
1252  * 3.3.16  READDIR3args
1253  *
1254  *	struct READDIR3args {
1255  *		nfs_fh3		dir;
1256  *		cookie3		cookie;
1257  *		cookieverf3	cookieverf;
1258  *		count3		count;
1259  *	};
1260  */
1261 static void encode_readdir3args(struct xdr_stream *xdr,
1262 				const struct nfs3_readdirargs *args)
1263 {
1264 	__be32 *p;
1265 
1266 	encode_nfs_fh3(xdr, args->fh);
1267 
1268 	p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4);
1269 	p = xdr_encode_cookie3(p, args->cookie);
1270 	p = xdr_encode_cookieverf3(p, args->verf);
1271 	*p = cpu_to_be32(args->count);
1272 }
1273 
1274 static void nfs3_xdr_enc_readdir3args(struct rpc_rqst *req,
1275 				      struct xdr_stream *xdr,
1276 				      const void *data)
1277 {
1278 	const struct nfs3_readdirargs *args = data;
1279 
1280 	encode_readdir3args(xdr, args);
1281 	prepare_reply_buffer(req, args->pages, 0,
1282 				args->count, NFS3_readdirres_sz);
1283 }
1284 
1285 /*
1286  * 3.3.17  READDIRPLUS3args
1287  *
1288  *	struct READDIRPLUS3args {
1289  *		nfs_fh3		dir;
1290  *		cookie3		cookie;
1291  *		cookieverf3	cookieverf;
1292  *		count3		dircount;
1293  *		count3		maxcount;
1294  *	};
1295  */
1296 static void encode_readdirplus3args(struct xdr_stream *xdr,
1297 				    const struct nfs3_readdirargs *args)
1298 {
1299 	__be32 *p;
1300 
1301 	encode_nfs_fh3(xdr, args->fh);
1302 
1303 	p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4 + 4);
1304 	p = xdr_encode_cookie3(p, args->cookie);
1305 	p = xdr_encode_cookieverf3(p, args->verf);
1306 
1307 	/*
1308 	 * readdirplus: need dircount + buffer size.
1309 	 * We just make sure we make dircount big enough
1310 	 */
1311 	*p++ = cpu_to_be32(args->count >> 3);
1312 
1313 	*p = cpu_to_be32(args->count);
1314 }
1315 
1316 static void nfs3_xdr_enc_readdirplus3args(struct rpc_rqst *req,
1317 					  struct xdr_stream *xdr,
1318 					  const void *data)
1319 {
1320 	const struct nfs3_readdirargs *args = data;
1321 
1322 	encode_readdirplus3args(xdr, args);
1323 	prepare_reply_buffer(req, args->pages, 0,
1324 				args->count, NFS3_readdirres_sz);
1325 }
1326 
1327 /*
1328  * 3.3.21  COMMIT3args
1329  *
1330  *	struct COMMIT3args {
1331  *		nfs_fh3		file;
1332  *		offset3		offset;
1333  *		count3		count;
1334  *	};
1335  */
1336 static void encode_commit3args(struct xdr_stream *xdr,
1337 			       const struct nfs_commitargs *args)
1338 {
1339 	__be32 *p;
1340 
1341 	encode_nfs_fh3(xdr, args->fh);
1342 
1343 	p = xdr_reserve_space(xdr, 8 + 4);
1344 	p = xdr_encode_hyper(p, args->offset);
1345 	*p = cpu_to_be32(args->count);
1346 }
1347 
1348 static void nfs3_xdr_enc_commit3args(struct rpc_rqst *req,
1349 				     struct xdr_stream *xdr,
1350 				     const void *data)
1351 {
1352 	const struct nfs_commitargs *args = data;
1353 
1354 	encode_commit3args(xdr, args);
1355 }
1356 
1357 #ifdef CONFIG_NFS_V3_ACL
1358 
1359 static void nfs3_xdr_enc_getacl3args(struct rpc_rqst *req,
1360 				     struct xdr_stream *xdr,
1361 				     const void *data)
1362 {
1363 	const struct nfs3_getaclargs *args = data;
1364 
1365 	encode_nfs_fh3(xdr, args->fh);
1366 	encode_uint32(xdr, args->mask);
1367 	if (args->mask & (NFS_ACL | NFS_DFACL)) {
1368 		prepare_reply_buffer(req, args->pages, 0,
1369 					NFSACL_MAXPAGES << PAGE_SHIFT,
1370 					ACL3_getaclres_sz);
1371 		req->rq_rcv_buf.flags |= XDRBUF_SPARSE_PAGES;
1372 	}
1373 }
1374 
1375 static void nfs3_xdr_enc_setacl3args(struct rpc_rqst *req,
1376 				     struct xdr_stream *xdr,
1377 				     const void *data)
1378 {
1379 	const struct nfs3_setaclargs *args = data;
1380 	unsigned int base;
1381 	int error;
1382 
1383 	encode_nfs_fh3(xdr, NFS_FH(args->inode));
1384 	encode_uint32(xdr, args->mask);
1385 
1386 	base = req->rq_slen;
1387 	if (args->npages != 0)
1388 		xdr_write_pages(xdr, args->pages, 0, args->len);
1389 	else
1390 		xdr_reserve_space(xdr, args->len);
1391 
1392 	error = nfsacl_encode(xdr->buf, base, args->inode,
1393 			    (args->mask & NFS_ACL) ?
1394 			    args->acl_access : NULL, 1, 0);
1395 	/* FIXME: this is just broken */
1396 	BUG_ON(error < 0);
1397 	error = nfsacl_encode(xdr->buf, base + error, args->inode,
1398 			    (args->mask & NFS_DFACL) ?
1399 			    args->acl_default : NULL, 1,
1400 			    NFS_ACL_DEFAULT);
1401 	BUG_ON(error < 0);
1402 }
1403 
1404 #endif  /* CONFIG_NFS_V3_ACL */
1405 
1406 /*
1407  * NFSv3 XDR decode functions
1408  *
1409  * NFSv3 result types are defined in section 3.3 of RFC 1813:
1410  * "NFS Version 3 Protocol Specification".
1411  */
1412 
1413 /*
1414  * 3.3.1  GETATTR3res
1415  *
1416  *	struct GETATTR3resok {
1417  *		fattr3		obj_attributes;
1418  *	};
1419  *
1420  *	union GETATTR3res switch (nfsstat3 status) {
1421  *	case NFS3_OK:
1422  *		GETATTR3resok  resok;
1423  *	default:
1424  *		void;
1425  *	};
1426  */
1427 static int nfs3_xdr_dec_getattr3res(struct rpc_rqst *req,
1428 				    struct xdr_stream *xdr,
1429 				    void *result)
1430 {
1431 	enum nfs_stat status;
1432 	int error;
1433 
1434 	error = decode_nfsstat3(xdr, &status);
1435 	if (unlikely(error))
1436 		goto out;
1437 	if (status != NFS3_OK)
1438 		goto out_default;
1439 	error = decode_fattr3(xdr, result);
1440 out:
1441 	return error;
1442 out_default:
1443 	return nfs3_stat_to_errno(status);
1444 }
1445 
1446 /*
1447  * 3.3.2  SETATTR3res
1448  *
1449  *	struct SETATTR3resok {
1450  *		wcc_data  obj_wcc;
1451  *	};
1452  *
1453  *	struct SETATTR3resfail {
1454  *		wcc_data  obj_wcc;
1455  *	};
1456  *
1457  *	union SETATTR3res switch (nfsstat3 status) {
1458  *	case NFS3_OK:
1459  *		SETATTR3resok   resok;
1460  *	default:
1461  *		SETATTR3resfail resfail;
1462  *	};
1463  */
1464 static int nfs3_xdr_dec_setattr3res(struct rpc_rqst *req,
1465 				    struct xdr_stream *xdr,
1466 				    void *result)
1467 {
1468 	enum nfs_stat status;
1469 	int error;
1470 
1471 	error = decode_nfsstat3(xdr, &status);
1472 	if (unlikely(error))
1473 		goto out;
1474 	error = decode_wcc_data(xdr, result);
1475 	if (unlikely(error))
1476 		goto out;
1477 	if (status != NFS3_OK)
1478 		goto out_status;
1479 out:
1480 	return error;
1481 out_status:
1482 	return nfs3_stat_to_errno(status);
1483 }
1484 
1485 /*
1486  * 3.3.3  LOOKUP3res
1487  *
1488  *	struct LOOKUP3resok {
1489  *		nfs_fh3		object;
1490  *		post_op_attr	obj_attributes;
1491  *		post_op_attr	dir_attributes;
1492  *	};
1493  *
1494  *	struct LOOKUP3resfail {
1495  *		post_op_attr	dir_attributes;
1496  *	};
1497  *
1498  *	union LOOKUP3res switch (nfsstat3 status) {
1499  *	case NFS3_OK:
1500  *		LOOKUP3resok	resok;
1501  *	default:
1502  *		LOOKUP3resfail	resfail;
1503  *	};
1504  */
1505 static int nfs3_xdr_dec_lookup3res(struct rpc_rqst *req,
1506 				   struct xdr_stream *xdr,
1507 				   void *data)
1508 {
1509 	struct nfs3_diropres *result = data;
1510 	enum nfs_stat status;
1511 	int error;
1512 
1513 	error = decode_nfsstat3(xdr, &status);
1514 	if (unlikely(error))
1515 		goto out;
1516 	if (status != NFS3_OK)
1517 		goto out_default;
1518 	error = decode_nfs_fh3(xdr, result->fh);
1519 	if (unlikely(error))
1520 		goto out;
1521 	error = decode_post_op_attr(xdr, result->fattr);
1522 	if (unlikely(error))
1523 		goto out;
1524 	error = decode_post_op_attr(xdr, result->dir_attr);
1525 out:
1526 	return error;
1527 out_default:
1528 	error = decode_post_op_attr(xdr, result->dir_attr);
1529 	if (unlikely(error))
1530 		goto out;
1531 	return nfs3_stat_to_errno(status);
1532 }
1533 
1534 /*
1535  * 3.3.4  ACCESS3res
1536  *
1537  *	struct ACCESS3resok {
1538  *		post_op_attr	obj_attributes;
1539  *		uint32		access;
1540  *	};
1541  *
1542  *	struct ACCESS3resfail {
1543  *		post_op_attr	obj_attributes;
1544  *	};
1545  *
1546  *	union ACCESS3res switch (nfsstat3 status) {
1547  *	case NFS3_OK:
1548  *		ACCESS3resok	resok;
1549  *	default:
1550  *		ACCESS3resfail	resfail;
1551  *	};
1552  */
1553 static int nfs3_xdr_dec_access3res(struct rpc_rqst *req,
1554 				   struct xdr_stream *xdr,
1555 				   void *data)
1556 {
1557 	struct nfs3_accessres *result = data;
1558 	enum nfs_stat status;
1559 	int error;
1560 
1561 	error = decode_nfsstat3(xdr, &status);
1562 	if (unlikely(error))
1563 		goto out;
1564 	error = decode_post_op_attr(xdr, result->fattr);
1565 	if (unlikely(error))
1566 		goto out;
1567 	if (status != NFS3_OK)
1568 		goto out_default;
1569 	error = decode_uint32(xdr, &result->access);
1570 out:
1571 	return error;
1572 out_default:
1573 	return nfs3_stat_to_errno(status);
1574 }
1575 
1576 /*
1577  * 3.3.5  READLINK3res
1578  *
1579  *	struct READLINK3resok {
1580  *		post_op_attr	symlink_attributes;
1581  *		nfspath3	data;
1582  *	};
1583  *
1584  *	struct READLINK3resfail {
1585  *		post_op_attr	symlink_attributes;
1586  *	};
1587  *
1588  *	union READLINK3res switch (nfsstat3 status) {
1589  *	case NFS3_OK:
1590  *		READLINK3resok	resok;
1591  *	default:
1592  *		READLINK3resfail resfail;
1593  *	};
1594  */
1595 static int nfs3_xdr_dec_readlink3res(struct rpc_rqst *req,
1596 				     struct xdr_stream *xdr,
1597 				     void *result)
1598 {
1599 	enum nfs_stat status;
1600 	int error;
1601 
1602 	error = decode_nfsstat3(xdr, &status);
1603 	if (unlikely(error))
1604 		goto out;
1605 	error = decode_post_op_attr(xdr, result);
1606 	if (unlikely(error))
1607 		goto out;
1608 	if (status != NFS3_OK)
1609 		goto out_default;
1610 	error = decode_nfspath3(xdr);
1611 out:
1612 	return error;
1613 out_default:
1614 	return nfs3_stat_to_errno(status);
1615 }
1616 
1617 /*
1618  * 3.3.6  READ3res
1619  *
1620  *	struct READ3resok {
1621  *		post_op_attr	file_attributes;
1622  *		count3		count;
1623  *		bool		eof;
1624  *		opaque		data<>;
1625  *	};
1626  *
1627  *	struct READ3resfail {
1628  *		post_op_attr	file_attributes;
1629  *	};
1630  *
1631  *	union READ3res switch (nfsstat3 status) {
1632  *	case NFS3_OK:
1633  *		READ3resok	resok;
1634  *	default:
1635  *		READ3resfail	resfail;
1636  *	};
1637  */
1638 static int decode_read3resok(struct xdr_stream *xdr,
1639 			     struct nfs_pgio_res *result)
1640 {
1641 	u32 eof, count, ocount, recvd;
1642 	__be32 *p;
1643 
1644 	p = xdr_inline_decode(xdr, 4 + 4 + 4);
1645 	if (unlikely(p == NULL))
1646 		goto out_overflow;
1647 	count = be32_to_cpup(p++);
1648 	eof = be32_to_cpup(p++);
1649 	ocount = be32_to_cpup(p++);
1650 	if (unlikely(ocount != count))
1651 		goto out_mismatch;
1652 	recvd = xdr_read_pages(xdr, count);
1653 	if (unlikely(count > recvd))
1654 		goto out_cheating;
1655 out:
1656 	result->eof = eof;
1657 	result->count = count;
1658 	return count;
1659 out_mismatch:
1660 	dprintk("NFS: READ count doesn't match length of opaque: "
1661 		"count %u != ocount %u\n", count, ocount);
1662 	return -EIO;
1663 out_cheating:
1664 	dprintk("NFS: server cheating in read result: "
1665 		"count %u > recvd %u\n", count, recvd);
1666 	count = recvd;
1667 	eof = 0;
1668 	goto out;
1669 out_overflow:
1670 	print_overflow_msg(__func__, xdr);
1671 	return -EIO;
1672 }
1673 
1674 static int nfs3_xdr_dec_read3res(struct rpc_rqst *req, struct xdr_stream *xdr,
1675 				 void *data)
1676 {
1677 	struct nfs_pgio_res *result = data;
1678 	enum nfs_stat status;
1679 	int error;
1680 
1681 	error = decode_nfsstat3(xdr, &status);
1682 	if (unlikely(error))
1683 		goto out;
1684 	error = decode_post_op_attr(xdr, result->fattr);
1685 	if (unlikely(error))
1686 		goto out;
1687 	result->op_status = status;
1688 	if (status != NFS3_OK)
1689 		goto out_status;
1690 	error = decode_read3resok(xdr, result);
1691 out:
1692 	return error;
1693 out_status:
1694 	return nfs3_stat_to_errno(status);
1695 }
1696 
1697 /*
1698  * 3.3.7  WRITE3res
1699  *
1700  *	enum stable_how {
1701  *		UNSTABLE  = 0,
1702  *		DATA_SYNC = 1,
1703  *		FILE_SYNC = 2
1704  *	};
1705  *
1706  *	struct WRITE3resok {
1707  *		wcc_data	file_wcc;
1708  *		count3		count;
1709  *		stable_how	committed;
1710  *		writeverf3	verf;
1711  *	};
1712  *
1713  *	struct WRITE3resfail {
1714  *		wcc_data	file_wcc;
1715  *	};
1716  *
1717  *	union WRITE3res switch (nfsstat3 status) {
1718  *	case NFS3_OK:
1719  *		WRITE3resok	resok;
1720  *	default:
1721  *		WRITE3resfail	resfail;
1722  *	};
1723  */
1724 static int decode_write3resok(struct xdr_stream *xdr,
1725 			      struct nfs_pgio_res *result)
1726 {
1727 	__be32 *p;
1728 
1729 	p = xdr_inline_decode(xdr, 4 + 4);
1730 	if (unlikely(p == NULL))
1731 		goto out_overflow;
1732 	result->count = be32_to_cpup(p++);
1733 	result->verf->committed = be32_to_cpup(p++);
1734 	if (unlikely(result->verf->committed > NFS_FILE_SYNC))
1735 		goto out_badvalue;
1736 	if (decode_writeverf3(xdr, &result->verf->verifier))
1737 		goto out_eio;
1738 	return result->count;
1739 out_badvalue:
1740 	dprintk("NFS: bad stable_how value: %u\n", result->verf->committed);
1741 	return -EIO;
1742 out_overflow:
1743 	print_overflow_msg(__func__, xdr);
1744 out_eio:
1745 	return -EIO;
1746 }
1747 
1748 static int nfs3_xdr_dec_write3res(struct rpc_rqst *req, struct xdr_stream *xdr,
1749 				  void *data)
1750 {
1751 	struct nfs_pgio_res *result = data;
1752 	enum nfs_stat status;
1753 	int error;
1754 
1755 	error = decode_nfsstat3(xdr, &status);
1756 	if (unlikely(error))
1757 		goto out;
1758 	error = decode_wcc_data(xdr, result->fattr);
1759 	if (unlikely(error))
1760 		goto out;
1761 	result->op_status = status;
1762 	if (status != NFS3_OK)
1763 		goto out_status;
1764 	error = decode_write3resok(xdr, result);
1765 out:
1766 	return error;
1767 out_status:
1768 	return nfs3_stat_to_errno(status);
1769 }
1770 
1771 /*
1772  * 3.3.8  CREATE3res
1773  *
1774  *	struct CREATE3resok {
1775  *		post_op_fh3	obj;
1776  *		post_op_attr	obj_attributes;
1777  *		wcc_data	dir_wcc;
1778  *	};
1779  *
1780  *	struct CREATE3resfail {
1781  *		wcc_data	dir_wcc;
1782  *	};
1783  *
1784  *	union CREATE3res switch (nfsstat3 status) {
1785  *	case NFS3_OK:
1786  *		CREATE3resok	resok;
1787  *	default:
1788  *		CREATE3resfail	resfail;
1789  *	};
1790  */
1791 static int decode_create3resok(struct xdr_stream *xdr,
1792 			       struct nfs3_diropres *result)
1793 {
1794 	int error;
1795 
1796 	error = decode_post_op_fh3(xdr, result->fh);
1797 	if (unlikely(error))
1798 		goto out;
1799 	error = decode_post_op_attr(xdr, result->fattr);
1800 	if (unlikely(error))
1801 		goto out;
1802 	/* The server isn't required to return a file handle.
1803 	 * If it didn't, force the client to perform a LOOKUP
1804 	 * to determine the correct file handle and attribute
1805 	 * values for the new object. */
1806 	if (result->fh->size == 0)
1807 		result->fattr->valid = 0;
1808 	error = decode_wcc_data(xdr, result->dir_attr);
1809 out:
1810 	return error;
1811 }
1812 
1813 static int nfs3_xdr_dec_create3res(struct rpc_rqst *req,
1814 				   struct xdr_stream *xdr,
1815 				   void *data)
1816 {
1817 	struct nfs3_diropres *result = data;
1818 	enum nfs_stat status;
1819 	int error;
1820 
1821 	error = decode_nfsstat3(xdr, &status);
1822 	if (unlikely(error))
1823 		goto out;
1824 	if (status != NFS3_OK)
1825 		goto out_default;
1826 	error = decode_create3resok(xdr, result);
1827 out:
1828 	return error;
1829 out_default:
1830 	error = decode_wcc_data(xdr, result->dir_attr);
1831 	if (unlikely(error))
1832 		goto out;
1833 	return nfs3_stat_to_errno(status);
1834 }
1835 
1836 /*
1837  * 3.3.12  REMOVE3res
1838  *
1839  *	struct REMOVE3resok {
1840  *		wcc_data    dir_wcc;
1841  *	};
1842  *
1843  *	struct REMOVE3resfail {
1844  *		wcc_data    dir_wcc;
1845  *	};
1846  *
1847  *	union REMOVE3res switch (nfsstat3 status) {
1848  *	case NFS3_OK:
1849  *		REMOVE3resok   resok;
1850  *	default:
1851  *		REMOVE3resfail resfail;
1852  *	};
1853  */
1854 static int nfs3_xdr_dec_remove3res(struct rpc_rqst *req,
1855 				   struct xdr_stream *xdr,
1856 				   void *data)
1857 {
1858 	struct nfs_removeres *result = data;
1859 	enum nfs_stat status;
1860 	int error;
1861 
1862 	error = decode_nfsstat3(xdr, &status);
1863 	if (unlikely(error))
1864 		goto out;
1865 	error = decode_wcc_data(xdr, result->dir_attr);
1866 	if (unlikely(error))
1867 		goto out;
1868 	if (status != NFS3_OK)
1869 		goto out_status;
1870 out:
1871 	return error;
1872 out_status:
1873 	return nfs3_stat_to_errno(status);
1874 }
1875 
1876 /*
1877  * 3.3.14  RENAME3res
1878  *
1879  *	struct RENAME3resok {
1880  *		wcc_data	fromdir_wcc;
1881  *		wcc_data	todir_wcc;
1882  *	};
1883  *
1884  *	struct RENAME3resfail {
1885  *		wcc_data	fromdir_wcc;
1886  *		wcc_data	todir_wcc;
1887  *	};
1888  *
1889  *	union RENAME3res switch (nfsstat3 status) {
1890  *	case NFS3_OK:
1891  *		RENAME3resok   resok;
1892  *	default:
1893  *		RENAME3resfail resfail;
1894  *	};
1895  */
1896 static int nfs3_xdr_dec_rename3res(struct rpc_rqst *req,
1897 				   struct xdr_stream *xdr,
1898 				   void *data)
1899 {
1900 	struct nfs_renameres *result = data;
1901 	enum nfs_stat status;
1902 	int error;
1903 
1904 	error = decode_nfsstat3(xdr, &status);
1905 	if (unlikely(error))
1906 		goto out;
1907 	error = decode_wcc_data(xdr, result->old_fattr);
1908 	if (unlikely(error))
1909 		goto out;
1910 	error = decode_wcc_data(xdr, result->new_fattr);
1911 	if (unlikely(error))
1912 		goto out;
1913 	if (status != NFS3_OK)
1914 		goto out_status;
1915 out:
1916 	return error;
1917 out_status:
1918 	return nfs3_stat_to_errno(status);
1919 }
1920 
1921 /*
1922  * 3.3.15  LINK3res
1923  *
1924  *	struct LINK3resok {
1925  *		post_op_attr	file_attributes;
1926  *		wcc_data	linkdir_wcc;
1927  *	};
1928  *
1929  *	struct LINK3resfail {
1930  *		post_op_attr	file_attributes;
1931  *		wcc_data	linkdir_wcc;
1932  *	};
1933  *
1934  *	union LINK3res switch (nfsstat3 status) {
1935  *	case NFS3_OK:
1936  *		LINK3resok	resok;
1937  *	default:
1938  *		LINK3resfail	resfail;
1939  *	};
1940  */
1941 static int nfs3_xdr_dec_link3res(struct rpc_rqst *req, struct xdr_stream *xdr,
1942 				 void *data)
1943 {
1944 	struct nfs3_linkres *result = data;
1945 	enum nfs_stat status;
1946 	int error;
1947 
1948 	error = decode_nfsstat3(xdr, &status);
1949 	if (unlikely(error))
1950 		goto out;
1951 	error = decode_post_op_attr(xdr, result->fattr);
1952 	if (unlikely(error))
1953 		goto out;
1954 	error = decode_wcc_data(xdr, result->dir_attr);
1955 	if (unlikely(error))
1956 		goto out;
1957 	if (status != NFS3_OK)
1958 		goto out_status;
1959 out:
1960 	return error;
1961 out_status:
1962 	return nfs3_stat_to_errno(status);
1963 }
1964 
1965 /**
1966  * nfs3_decode_dirent - Decode a single NFSv3 directory entry stored in
1967  *			the local page cache
1968  * @xdr: XDR stream where entry resides
1969  * @entry: buffer to fill in with entry data
1970  * @plus: boolean indicating whether this should be a readdirplus entry
1971  *
1972  * Returns zero if successful, otherwise a negative errno value is
1973  * returned.
1974  *
1975  * This function is not invoked during READDIR reply decoding, but
1976  * rather whenever an application invokes the getdents(2) system call
1977  * on a directory already in our cache.
1978  *
1979  * 3.3.16  entry3
1980  *
1981  *	struct entry3 {
1982  *		fileid3		fileid;
1983  *		filename3	name;
1984  *		cookie3		cookie;
1985  *		fhandle3	filehandle;
1986  *		post_op_attr3	attributes;
1987  *		entry3		*nextentry;
1988  *	};
1989  *
1990  * 3.3.17  entryplus3
1991  *	struct entryplus3 {
1992  *		fileid3		fileid;
1993  *		filename3	name;
1994  *		cookie3		cookie;
1995  *		post_op_attr	name_attributes;
1996  *		post_op_fh3	name_handle;
1997  *		entryplus3	*nextentry;
1998  *	};
1999  */
2000 int nfs3_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
2001 		       bool plus)
2002 {
2003 	struct nfs_entry old = *entry;
2004 	__be32 *p;
2005 	int error;
2006 	u64 new_cookie;
2007 
2008 	p = xdr_inline_decode(xdr, 4);
2009 	if (unlikely(p == NULL))
2010 		goto out_overflow;
2011 	if (*p == xdr_zero) {
2012 		p = xdr_inline_decode(xdr, 4);
2013 		if (unlikely(p == NULL))
2014 			goto out_overflow;
2015 		if (*p == xdr_zero)
2016 			return -EAGAIN;
2017 		entry->eof = 1;
2018 		return -EBADCOOKIE;
2019 	}
2020 
2021 	error = decode_fileid3(xdr, &entry->ino);
2022 	if (unlikely(error))
2023 		return error;
2024 
2025 	error = decode_inline_filename3(xdr, &entry->name, &entry->len);
2026 	if (unlikely(error))
2027 		return error;
2028 
2029 	error = decode_cookie3(xdr, &new_cookie);
2030 	if (unlikely(error))
2031 		return error;
2032 
2033 	entry->d_type = DT_UNKNOWN;
2034 
2035 	if (plus) {
2036 		entry->fattr->valid = 0;
2037 		error = decode_post_op_attr(xdr, entry->fattr);
2038 		if (unlikely(error))
2039 			return error;
2040 		if (entry->fattr->valid & NFS_ATTR_FATTR_V3)
2041 			entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
2042 
2043 		if (entry->fattr->fileid != entry->ino) {
2044 			entry->fattr->mounted_on_fileid = entry->ino;
2045 			entry->fattr->valid |= NFS_ATTR_FATTR_MOUNTED_ON_FILEID;
2046 		}
2047 
2048 		/* In fact, a post_op_fh3: */
2049 		p = xdr_inline_decode(xdr, 4);
2050 		if (unlikely(p == NULL))
2051 			goto out_overflow;
2052 		if (*p != xdr_zero) {
2053 			error = decode_nfs_fh3(xdr, entry->fh);
2054 			if (unlikely(error)) {
2055 				if (error == -E2BIG)
2056 					goto out_truncated;
2057 				return error;
2058 			}
2059 		} else
2060 			zero_nfs_fh3(entry->fh);
2061 	}
2062 
2063 	entry->prev_cookie = entry->cookie;
2064 	entry->cookie = new_cookie;
2065 
2066 	return 0;
2067 
2068 out_overflow:
2069 	print_overflow_msg(__func__, xdr);
2070 	return -EAGAIN;
2071 out_truncated:
2072 	dprintk("NFS: directory entry contains invalid file handle\n");
2073 	*entry = old;
2074 	return -EAGAIN;
2075 }
2076 
2077 /*
2078  * 3.3.16  READDIR3res
2079  *
2080  *	struct dirlist3 {
2081  *		entry3		*entries;
2082  *		bool		eof;
2083  *	};
2084  *
2085  *	struct READDIR3resok {
2086  *		post_op_attr	dir_attributes;
2087  *		cookieverf3	cookieverf;
2088  *		dirlist3	reply;
2089  *	};
2090  *
2091  *	struct READDIR3resfail {
2092  *		post_op_attr	dir_attributes;
2093  *	};
2094  *
2095  *	union READDIR3res switch (nfsstat3 status) {
2096  *	case NFS3_OK:
2097  *		READDIR3resok	resok;
2098  *	default:
2099  *		READDIR3resfail	resfail;
2100  *	};
2101  *
2102  * Read the directory contents into the page cache, but otherwise
2103  * don't touch them.  The actual decoding is done by nfs3_decode_entry()
2104  * during subsequent nfs_readdir() calls.
2105  */
2106 static int decode_dirlist3(struct xdr_stream *xdr)
2107 {
2108 	return xdr_read_pages(xdr, xdr->buf->page_len);
2109 }
2110 
2111 static int decode_readdir3resok(struct xdr_stream *xdr,
2112 				struct nfs3_readdirres *result)
2113 {
2114 	int error;
2115 
2116 	error = decode_post_op_attr(xdr, result->dir_attr);
2117 	if (unlikely(error))
2118 		goto out;
2119 	/* XXX: do we need to check if result->verf != NULL ? */
2120 	error = decode_cookieverf3(xdr, result->verf);
2121 	if (unlikely(error))
2122 		goto out;
2123 	error = decode_dirlist3(xdr);
2124 out:
2125 	return error;
2126 }
2127 
2128 static int nfs3_xdr_dec_readdir3res(struct rpc_rqst *req,
2129 				    struct xdr_stream *xdr,
2130 				    void *data)
2131 {
2132 	struct nfs3_readdirres *result = data;
2133 	enum nfs_stat status;
2134 	int error;
2135 
2136 	error = decode_nfsstat3(xdr, &status);
2137 	if (unlikely(error))
2138 		goto out;
2139 	if (status != NFS3_OK)
2140 		goto out_default;
2141 	error = decode_readdir3resok(xdr, result);
2142 out:
2143 	return error;
2144 out_default:
2145 	error = decode_post_op_attr(xdr, result->dir_attr);
2146 	if (unlikely(error))
2147 		goto out;
2148 	return nfs3_stat_to_errno(status);
2149 }
2150 
2151 /*
2152  * 3.3.18  FSSTAT3res
2153  *
2154  *	struct FSSTAT3resok {
2155  *		post_op_attr	obj_attributes;
2156  *		size3		tbytes;
2157  *		size3		fbytes;
2158  *		size3		abytes;
2159  *		size3		tfiles;
2160  *		size3		ffiles;
2161  *		size3		afiles;
2162  *		uint32		invarsec;
2163  *	};
2164  *
2165  *	struct FSSTAT3resfail {
2166  *		post_op_attr	obj_attributes;
2167  *	};
2168  *
2169  *	union FSSTAT3res switch (nfsstat3 status) {
2170  *	case NFS3_OK:
2171  *		FSSTAT3resok	resok;
2172  *	default:
2173  *		FSSTAT3resfail	resfail;
2174  *	};
2175  */
2176 static int decode_fsstat3resok(struct xdr_stream *xdr,
2177 			       struct nfs_fsstat *result)
2178 {
2179 	__be32 *p;
2180 
2181 	p = xdr_inline_decode(xdr, 8 * 6 + 4);
2182 	if (unlikely(p == NULL))
2183 		goto out_overflow;
2184 	p = xdr_decode_size3(p, &result->tbytes);
2185 	p = xdr_decode_size3(p, &result->fbytes);
2186 	p = xdr_decode_size3(p, &result->abytes);
2187 	p = xdr_decode_size3(p, &result->tfiles);
2188 	p = xdr_decode_size3(p, &result->ffiles);
2189 	xdr_decode_size3(p, &result->afiles);
2190 	/* ignore invarsec */
2191 	return 0;
2192 out_overflow:
2193 	print_overflow_msg(__func__, xdr);
2194 	return -EIO;
2195 }
2196 
2197 static int nfs3_xdr_dec_fsstat3res(struct rpc_rqst *req,
2198 				   struct xdr_stream *xdr,
2199 				   void *data)
2200 {
2201 	struct nfs_fsstat *result = data;
2202 	enum nfs_stat status;
2203 	int error;
2204 
2205 	error = decode_nfsstat3(xdr, &status);
2206 	if (unlikely(error))
2207 		goto out;
2208 	error = decode_post_op_attr(xdr, result->fattr);
2209 	if (unlikely(error))
2210 		goto out;
2211 	if (status != NFS3_OK)
2212 		goto out_status;
2213 	error = decode_fsstat3resok(xdr, result);
2214 out:
2215 	return error;
2216 out_status:
2217 	return nfs3_stat_to_errno(status);
2218 }
2219 
2220 /*
2221  * 3.3.19  FSINFO3res
2222  *
2223  *	struct FSINFO3resok {
2224  *		post_op_attr	obj_attributes;
2225  *		uint32		rtmax;
2226  *		uint32		rtpref;
2227  *		uint32		rtmult;
2228  *		uint32		wtmax;
2229  *		uint32		wtpref;
2230  *		uint32		wtmult;
2231  *		uint32		dtpref;
2232  *		size3		maxfilesize;
2233  *		nfstime3	time_delta;
2234  *		uint32		properties;
2235  *	};
2236  *
2237  *	struct FSINFO3resfail {
2238  *		post_op_attr	obj_attributes;
2239  *	};
2240  *
2241  *	union FSINFO3res switch (nfsstat3 status) {
2242  *	case NFS3_OK:
2243  *		FSINFO3resok	resok;
2244  *	default:
2245  *		FSINFO3resfail	resfail;
2246  *	};
2247  */
2248 static int decode_fsinfo3resok(struct xdr_stream *xdr,
2249 			       struct nfs_fsinfo *result)
2250 {
2251 	__be32 *p;
2252 
2253 	p = xdr_inline_decode(xdr, 4 * 7 + 8 + 8 + 4);
2254 	if (unlikely(p == NULL))
2255 		goto out_overflow;
2256 	result->rtmax  = be32_to_cpup(p++);
2257 	result->rtpref = be32_to_cpup(p++);
2258 	result->rtmult = be32_to_cpup(p++);
2259 	result->wtmax  = be32_to_cpup(p++);
2260 	result->wtpref = be32_to_cpup(p++);
2261 	result->wtmult = be32_to_cpup(p++);
2262 	result->dtpref = be32_to_cpup(p++);
2263 	p = xdr_decode_size3(p, &result->maxfilesize);
2264 	xdr_decode_nfstime3(p, &result->time_delta);
2265 
2266 	/* ignore properties */
2267 	result->lease_time = 0;
2268 	return 0;
2269 out_overflow:
2270 	print_overflow_msg(__func__, xdr);
2271 	return -EIO;
2272 }
2273 
2274 static int nfs3_xdr_dec_fsinfo3res(struct rpc_rqst *req,
2275 				   struct xdr_stream *xdr,
2276 				   void *data)
2277 {
2278 	struct nfs_fsinfo *result = data;
2279 	enum nfs_stat status;
2280 	int error;
2281 
2282 	error = decode_nfsstat3(xdr, &status);
2283 	if (unlikely(error))
2284 		goto out;
2285 	error = decode_post_op_attr(xdr, result->fattr);
2286 	if (unlikely(error))
2287 		goto out;
2288 	if (status != NFS3_OK)
2289 		goto out_status;
2290 	error = decode_fsinfo3resok(xdr, result);
2291 out:
2292 	return error;
2293 out_status:
2294 	return nfs3_stat_to_errno(status);
2295 }
2296 
2297 /*
2298  * 3.3.20  PATHCONF3res
2299  *
2300  *	struct PATHCONF3resok {
2301  *		post_op_attr	obj_attributes;
2302  *		uint32		linkmax;
2303  *		uint32		name_max;
2304  *		bool		no_trunc;
2305  *		bool		chown_restricted;
2306  *		bool		case_insensitive;
2307  *		bool		case_preserving;
2308  *	};
2309  *
2310  *	struct PATHCONF3resfail {
2311  *		post_op_attr	obj_attributes;
2312  *	};
2313  *
2314  *	union PATHCONF3res switch (nfsstat3 status) {
2315  *	case NFS3_OK:
2316  *		PATHCONF3resok	resok;
2317  *	default:
2318  *		PATHCONF3resfail resfail;
2319  *	};
2320  */
2321 static int decode_pathconf3resok(struct xdr_stream *xdr,
2322 				 struct nfs_pathconf *result)
2323 {
2324 	__be32 *p;
2325 
2326 	p = xdr_inline_decode(xdr, 4 * 6);
2327 	if (unlikely(p == NULL))
2328 		goto out_overflow;
2329 	result->max_link = be32_to_cpup(p++);
2330 	result->max_namelen = be32_to_cpup(p);
2331 	/* ignore remaining fields */
2332 	return 0;
2333 out_overflow:
2334 	print_overflow_msg(__func__, xdr);
2335 	return -EIO;
2336 }
2337 
2338 static int nfs3_xdr_dec_pathconf3res(struct rpc_rqst *req,
2339 				     struct xdr_stream *xdr,
2340 				     void *data)
2341 {
2342 	struct nfs_pathconf *result = data;
2343 	enum nfs_stat status;
2344 	int error;
2345 
2346 	error = decode_nfsstat3(xdr, &status);
2347 	if (unlikely(error))
2348 		goto out;
2349 	error = decode_post_op_attr(xdr, result->fattr);
2350 	if (unlikely(error))
2351 		goto out;
2352 	if (status != NFS3_OK)
2353 		goto out_status;
2354 	error = decode_pathconf3resok(xdr, result);
2355 out:
2356 	return error;
2357 out_status:
2358 	return nfs3_stat_to_errno(status);
2359 }
2360 
2361 /*
2362  * 3.3.21  COMMIT3res
2363  *
2364  *	struct COMMIT3resok {
2365  *		wcc_data	file_wcc;
2366  *		writeverf3	verf;
2367  *	};
2368  *
2369  *	struct COMMIT3resfail {
2370  *		wcc_data	file_wcc;
2371  *	};
2372  *
2373  *	union COMMIT3res switch (nfsstat3 status) {
2374  *	case NFS3_OK:
2375  *		COMMIT3resok	resok;
2376  *	default:
2377  *		COMMIT3resfail	resfail;
2378  *	};
2379  */
2380 static int nfs3_xdr_dec_commit3res(struct rpc_rqst *req,
2381 				   struct xdr_stream *xdr,
2382 				   void *data)
2383 {
2384 	struct nfs_commitres *result = data;
2385 	enum nfs_stat status;
2386 	int error;
2387 
2388 	error = decode_nfsstat3(xdr, &status);
2389 	if (unlikely(error))
2390 		goto out;
2391 	error = decode_wcc_data(xdr, result->fattr);
2392 	if (unlikely(error))
2393 		goto out;
2394 	result->op_status = status;
2395 	if (status != NFS3_OK)
2396 		goto out_status;
2397 	error = decode_writeverf3(xdr, &result->verf->verifier);
2398 out:
2399 	return error;
2400 out_status:
2401 	return nfs3_stat_to_errno(status);
2402 }
2403 
2404 #ifdef CONFIG_NFS_V3_ACL
2405 
2406 static inline int decode_getacl3resok(struct xdr_stream *xdr,
2407 				      struct nfs3_getaclres *result)
2408 {
2409 	struct posix_acl **acl;
2410 	unsigned int *aclcnt;
2411 	size_t hdrlen;
2412 	int error;
2413 
2414 	error = decode_post_op_attr(xdr, result->fattr);
2415 	if (unlikely(error))
2416 		goto out;
2417 	error = decode_uint32(xdr, &result->mask);
2418 	if (unlikely(error))
2419 		goto out;
2420 	error = -EINVAL;
2421 	if (result->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
2422 		goto out;
2423 
2424 	hdrlen = xdr_stream_pos(xdr);
2425 
2426 	acl = NULL;
2427 	if (result->mask & NFS_ACL)
2428 		acl = &result->acl_access;
2429 	aclcnt = NULL;
2430 	if (result->mask & NFS_ACLCNT)
2431 		aclcnt = &result->acl_access_count;
2432 	error = nfsacl_decode(xdr->buf, hdrlen, aclcnt, acl);
2433 	if (unlikely(error <= 0))
2434 		goto out;
2435 
2436 	acl = NULL;
2437 	if (result->mask & NFS_DFACL)
2438 		acl = &result->acl_default;
2439 	aclcnt = NULL;
2440 	if (result->mask & NFS_DFACLCNT)
2441 		aclcnt = &result->acl_default_count;
2442 	error = nfsacl_decode(xdr->buf, hdrlen + error, aclcnt, acl);
2443 	if (unlikely(error <= 0))
2444 		return error;
2445 	error = 0;
2446 out:
2447 	return error;
2448 }
2449 
2450 static int nfs3_xdr_dec_getacl3res(struct rpc_rqst *req,
2451 				   struct xdr_stream *xdr,
2452 				   void *result)
2453 {
2454 	enum nfs_stat status;
2455 	int error;
2456 
2457 	error = decode_nfsstat3(xdr, &status);
2458 	if (unlikely(error))
2459 		goto out;
2460 	if (status != NFS3_OK)
2461 		goto out_default;
2462 	error = decode_getacl3resok(xdr, result);
2463 out:
2464 	return error;
2465 out_default:
2466 	return nfs3_stat_to_errno(status);
2467 }
2468 
2469 static int nfs3_xdr_dec_setacl3res(struct rpc_rqst *req,
2470 				   struct xdr_stream *xdr,
2471 				   void *result)
2472 {
2473 	enum nfs_stat status;
2474 	int error;
2475 
2476 	error = decode_nfsstat3(xdr, &status);
2477 	if (unlikely(error))
2478 		goto out;
2479 	if (status != NFS3_OK)
2480 		goto out_default;
2481 	error = decode_post_op_attr(xdr, result);
2482 out:
2483 	return error;
2484 out_default:
2485 	return nfs3_stat_to_errno(status);
2486 }
2487 
2488 #endif  /* CONFIG_NFS_V3_ACL */
2489 
2490 
2491 /*
2492  * We need to translate between nfs status return values and
2493  * the local errno values which may not be the same.
2494  */
2495 static const struct {
2496 	int stat;
2497 	int errno;
2498 } nfs_errtbl[] = {
2499 	{ NFS_OK,		0		},
2500 	{ NFSERR_PERM,		-EPERM		},
2501 	{ NFSERR_NOENT,		-ENOENT		},
2502 	{ NFSERR_IO,		-errno_NFSERR_IO},
2503 	{ NFSERR_NXIO,		-ENXIO		},
2504 /*	{ NFSERR_EAGAIN,	-EAGAIN		}, */
2505 	{ NFSERR_ACCES,		-EACCES		},
2506 	{ NFSERR_EXIST,		-EEXIST		},
2507 	{ NFSERR_XDEV,		-EXDEV		},
2508 	{ NFSERR_NODEV,		-ENODEV		},
2509 	{ NFSERR_NOTDIR,	-ENOTDIR	},
2510 	{ NFSERR_ISDIR,		-EISDIR		},
2511 	{ NFSERR_INVAL,		-EINVAL		},
2512 	{ NFSERR_FBIG,		-EFBIG		},
2513 	{ NFSERR_NOSPC,		-ENOSPC		},
2514 	{ NFSERR_ROFS,		-EROFS		},
2515 	{ NFSERR_MLINK,		-EMLINK		},
2516 	{ NFSERR_NAMETOOLONG,	-ENAMETOOLONG	},
2517 	{ NFSERR_NOTEMPTY,	-ENOTEMPTY	},
2518 	{ NFSERR_DQUOT,		-EDQUOT		},
2519 	{ NFSERR_STALE,		-ESTALE		},
2520 	{ NFSERR_REMOTE,	-EREMOTE	},
2521 #ifdef EWFLUSH
2522 	{ NFSERR_WFLUSH,	-EWFLUSH	},
2523 #endif
2524 	{ NFSERR_BADHANDLE,	-EBADHANDLE	},
2525 	{ NFSERR_NOT_SYNC,	-ENOTSYNC	},
2526 	{ NFSERR_BAD_COOKIE,	-EBADCOOKIE	},
2527 	{ NFSERR_NOTSUPP,	-ENOTSUPP	},
2528 	{ NFSERR_TOOSMALL,	-ETOOSMALL	},
2529 	{ NFSERR_SERVERFAULT,	-EREMOTEIO	},
2530 	{ NFSERR_BADTYPE,	-EBADTYPE	},
2531 	{ NFSERR_JUKEBOX,	-EJUKEBOX	},
2532 	{ -1,			-EIO		}
2533 };
2534 
2535 /**
2536  * nfs3_stat_to_errno - convert an NFS status code to a local errno
2537  * @status: NFS status code to convert
2538  *
2539  * Returns a local errno value, or -EIO if the NFS status code is
2540  * not recognized.  This function is used jointly by NFSv2 and NFSv3.
2541  */
2542 static int nfs3_stat_to_errno(enum nfs_stat status)
2543 {
2544 	int i;
2545 
2546 	for (i = 0; nfs_errtbl[i].stat != -1; i++) {
2547 		if (nfs_errtbl[i].stat == (int)status)
2548 			return nfs_errtbl[i].errno;
2549 	}
2550 	dprintk("NFS: Unrecognized nfs status value: %u\n", status);
2551 	return nfs_errtbl[i].errno;
2552 }
2553 
2554 
2555 #define PROC(proc, argtype, restype, timer)				\
2556 [NFS3PROC_##proc] = {							\
2557 	.p_proc      = NFS3PROC_##proc,					\
2558 	.p_encode    = nfs3_xdr_enc_##argtype##3args,			\
2559 	.p_decode    = nfs3_xdr_dec_##restype##3res,			\
2560 	.p_arglen    = NFS3_##argtype##args_sz,				\
2561 	.p_replen    = NFS3_##restype##res_sz,				\
2562 	.p_timer     = timer,						\
2563 	.p_statidx   = NFS3PROC_##proc,					\
2564 	.p_name      = #proc,						\
2565 	}
2566 
2567 const struct rpc_procinfo nfs3_procedures[] = {
2568 	PROC(GETATTR,		getattr,	getattr,	1),
2569 	PROC(SETATTR,		setattr,	setattr,	0),
2570 	PROC(LOOKUP,		lookup,		lookup,		2),
2571 	PROC(ACCESS,		access,		access,		1),
2572 	PROC(READLINK,		readlink,	readlink,	3),
2573 	PROC(READ,		read,		read,		3),
2574 	PROC(WRITE,		write,		write,		4),
2575 	PROC(CREATE,		create,		create,		0),
2576 	PROC(MKDIR,		mkdir,		create,		0),
2577 	PROC(SYMLINK,		symlink,	create,		0),
2578 	PROC(MKNOD,		mknod,		create,		0),
2579 	PROC(REMOVE,		remove,		remove,		0),
2580 	PROC(RMDIR,		lookup,		setattr,	0),
2581 	PROC(RENAME,		rename,		rename,		0),
2582 	PROC(LINK,		link,		link,		0),
2583 	PROC(READDIR,		readdir,	readdir,	3),
2584 	PROC(READDIRPLUS,	readdirplus,	readdir,	3),
2585 	PROC(FSSTAT,		getattr,	fsstat,		0),
2586 	PROC(FSINFO,		getattr,	fsinfo,		0),
2587 	PROC(PATHCONF,		getattr,	pathconf,	0),
2588 	PROC(COMMIT,		commit,		commit,		5),
2589 };
2590 
2591 static unsigned int nfs_version3_counts[ARRAY_SIZE(nfs3_procedures)];
2592 const struct rpc_version nfs_version3 = {
2593 	.number			= 3,
2594 	.nrprocs		= ARRAY_SIZE(nfs3_procedures),
2595 	.procs			= nfs3_procedures,
2596 	.counts			= nfs_version3_counts,
2597 };
2598 
2599 #ifdef CONFIG_NFS_V3_ACL
2600 static const struct rpc_procinfo nfs3_acl_procedures[] = {
2601 	[ACLPROC3_GETACL] = {
2602 		.p_proc = ACLPROC3_GETACL,
2603 		.p_encode = nfs3_xdr_enc_getacl3args,
2604 		.p_decode = nfs3_xdr_dec_getacl3res,
2605 		.p_arglen = ACL3_getaclargs_sz,
2606 		.p_replen = ACL3_getaclres_sz,
2607 		.p_timer = 1,
2608 		.p_name = "GETACL",
2609 	},
2610 	[ACLPROC3_SETACL] = {
2611 		.p_proc = ACLPROC3_SETACL,
2612 		.p_encode = nfs3_xdr_enc_setacl3args,
2613 		.p_decode = nfs3_xdr_dec_setacl3res,
2614 		.p_arglen = ACL3_setaclargs_sz,
2615 		.p_replen = ACL3_setaclres_sz,
2616 		.p_timer = 0,
2617 		.p_name = "SETACL",
2618 	},
2619 };
2620 
2621 static unsigned int nfs3_acl_counts[ARRAY_SIZE(nfs3_acl_procedures)];
2622 const struct rpc_version nfsacl_version3 = {
2623 	.number			= 3,
2624 	.nrprocs		= ARRAY_SIZE(nfs3_acl_procedures),
2625 	.procs			= nfs3_acl_procedures,
2626 	.counts			= nfs3_acl_counts,
2627 };
2628 #endif  /* CONFIG_NFS_V3_ACL */
2629