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