1/*
2 *  fs/nfs/nfs4xdr.c
3 *
4 *  Client-side XDR for NFSv4.
5 *
6 *  Copyright (c) 2002 The Regents of the University of Michigan.
7 *  All rights reserved.
8 *
9 *  Kendrick Smith <kmsmith@umich.edu>
10 *  Andy Adamson   <andros@umich.edu>
11 *
12 *  Redistribution and use in source and binary forms, with or without
13 *  modification, are permitted provided that the following conditions
14 *  are met:
15 *
16 *  1. Redistributions of source code must retain the above copyright
17 *     notice, this list of conditions and the following disclaimer.
18 *  2. Redistributions in binary form must reproduce the above copyright
19 *     notice, this list of conditions and the following disclaimer in the
20 *     documentation and/or other materials provided with the distribution.
21 *  3. Neither the name of the University nor the names of its
22 *     contributors may be used to endorse or promote products derived
23 *     from this software without specific prior written permission.
24 *
25 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/param.h>
39#include <linux/time.h>
40#include <linux/mm.h>
41#include <linux/slab.h>
42#include <linux/utsname.h>
43#include <linux/errno.h>
44#include <linux/string.h>
45#include <linux/in.h>
46#include <linux/pagemap.h>
47#include <linux/proc_fs.h>
48#include <linux/kdev_t.h>
49#include <linux/sunrpc/clnt.h>
50#include <linux/nfs.h>
51#include <linux/nfs4.h>
52#include <linux/nfs_fs.h>
53#include <linux/nfs_idmap.h>
54#include "nfs4_fs.h"
55
56#define NFSDBG_FACILITY		NFSDBG_XDR
57
58/* Mapping from NFS error code to "errno" error code. */
59#define errno_NFSERR_IO		EIO
60
61static int nfs4_stat_to_errno(int);
62
63/* NFSv4 COMPOUND tags are only wanted for debugging purposes */
64#ifdef DEBUG
65#define NFS4_MAXTAGLEN		20
66#else
67#define NFS4_MAXTAGLEN		0
68#endif
69
70/* lock,open owner id:
71 * we currently use size 1 (u32) out of (NFS4_OPAQUE_LIMIT  >> 2)
72 */
73#define owner_id_maxsz          (1 + 1)
74#define compound_encode_hdr_maxsz	(3 + (NFS4_MAXTAGLEN >> 2))
75#define compound_decode_hdr_maxsz	(3 + (NFS4_MAXTAGLEN >> 2))
76#define op_encode_hdr_maxsz	(1)
77#define op_decode_hdr_maxsz	(2)
78#define encode_putfh_maxsz	(op_encode_hdr_maxsz + 1 + \
79				(NFS4_FHSIZE >> 2))
80#define decode_putfh_maxsz	(op_decode_hdr_maxsz)
81#define encode_putrootfh_maxsz	(op_encode_hdr_maxsz)
82#define decode_putrootfh_maxsz	(op_decode_hdr_maxsz)
83#define encode_getfh_maxsz      (op_encode_hdr_maxsz)
84#define decode_getfh_maxsz      (op_decode_hdr_maxsz + 1 + \
85				((3+NFS4_FHSIZE) >> 2))
86#define nfs4_fattr_bitmap_maxsz 3
87#define encode_getattr_maxsz    (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
88#define nfs4_name_maxsz		(1 + ((3 + NFS4_MAXNAMLEN) >> 2))
89#define nfs4_path_maxsz		(1 + ((3 + NFS4_MAXPATHLEN) >> 2))
90/* This is based on getfattr, which uses the most attributes: */
91#define nfs4_fattr_value_maxsz	(1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
92				3 + 3 + 3 + 2 * nfs4_name_maxsz))
93#define nfs4_fattr_maxsz	(nfs4_fattr_bitmap_maxsz + \
94				nfs4_fattr_value_maxsz)
95#define decode_getattr_maxsz    (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
96#define encode_savefh_maxsz     (op_encode_hdr_maxsz)
97#define decode_savefh_maxsz     (op_decode_hdr_maxsz)
98#define encode_restorefh_maxsz  (op_encode_hdr_maxsz)
99#define decode_restorefh_maxsz  (op_decode_hdr_maxsz)
100#define encode_fsinfo_maxsz	(op_encode_hdr_maxsz + 2)
101#define decode_fsinfo_maxsz	(op_decode_hdr_maxsz + 11)
102#define encode_renew_maxsz	(op_encode_hdr_maxsz + 3)
103#define decode_renew_maxsz	(op_decode_hdr_maxsz)
104#define encode_setclientid_maxsz \
105				(op_encode_hdr_maxsz + \
106				4 /*server->ip_addr*/ + \
107				1 /*Netid*/ + \
108				6 /*uaddr*/ + \
109				6 + (NFS4_VERIFIER_SIZE >> 2))
110#define decode_setclientid_maxsz \
111				(op_decode_hdr_maxsz + \
112				2 + \
113				1024) /* large value for CLID_INUSE */
114#define encode_setclientid_confirm_maxsz \
115				(op_encode_hdr_maxsz + \
116				3 + (NFS4_VERIFIER_SIZE >> 2))
117#define decode_setclientid_confirm_maxsz \
118				(op_decode_hdr_maxsz)
119#define encode_lookup_maxsz	(op_encode_hdr_maxsz + \
120				1 + ((3 + NFS4_FHSIZE) >> 2))
121#define encode_remove_maxsz	(op_encode_hdr_maxsz + \
122				nfs4_name_maxsz)
123#define encode_rename_maxsz	(op_encode_hdr_maxsz + \
124				2 * nfs4_name_maxsz)
125#define decode_rename_maxsz	(op_decode_hdr_maxsz + 5 + 5)
126#define encode_link_maxsz	(op_encode_hdr_maxsz + \
127				nfs4_name_maxsz)
128#define decode_link_maxsz	(op_decode_hdr_maxsz + 5)
129#define encode_symlink_maxsz	(op_encode_hdr_maxsz + \
130				1 + nfs4_name_maxsz + \
131				1 + \
132				nfs4_fattr_maxsz)
133#define decode_symlink_maxsz	(op_decode_hdr_maxsz + 8)
134#define encode_create_maxsz	(op_encode_hdr_maxsz + \
135				2 + nfs4_name_maxsz + \
136				nfs4_fattr_maxsz)
137#define decode_create_maxsz	(op_decode_hdr_maxsz + 8)
138#define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
139#define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
140#define NFS4_enc_compound_sz	(1024)
141#define NFS4_dec_compound_sz	(1024)
142#define NFS4_enc_read_sz	(compound_encode_hdr_maxsz + \
143				encode_putfh_maxsz + \
144				op_encode_hdr_maxsz + 7)
145#define NFS4_dec_read_sz	(compound_decode_hdr_maxsz + \
146				decode_putfh_maxsz + \
147				op_decode_hdr_maxsz + 2)
148#define NFS4_enc_readlink_sz	(compound_encode_hdr_maxsz + \
149				encode_putfh_maxsz + \
150				op_encode_hdr_maxsz)
151#define NFS4_dec_readlink_sz	(compound_decode_hdr_maxsz + \
152				decode_putfh_maxsz + \
153				op_decode_hdr_maxsz)
154#define NFS4_enc_readdir_sz	(compound_encode_hdr_maxsz + \
155				encode_putfh_maxsz + \
156				op_encode_hdr_maxsz + 9)
157#define NFS4_dec_readdir_sz	(compound_decode_hdr_maxsz + \
158				decode_putfh_maxsz + \
159				op_decode_hdr_maxsz + 2)
160#define NFS4_enc_write_sz	(compound_encode_hdr_maxsz + \
161				encode_putfh_maxsz + \
162				op_encode_hdr_maxsz + 8 + \
163				encode_getattr_maxsz)
164#define NFS4_dec_write_sz	(compound_decode_hdr_maxsz + \
165				decode_putfh_maxsz + \
166				op_decode_hdr_maxsz + 4 + \
167				decode_getattr_maxsz)
168#define NFS4_enc_commit_sz	(compound_encode_hdr_maxsz + \
169				encode_putfh_maxsz + \
170				op_encode_hdr_maxsz + 3 + \
171				encode_getattr_maxsz)
172#define NFS4_dec_commit_sz	(compound_decode_hdr_maxsz + \
173				decode_putfh_maxsz + \
174				op_decode_hdr_maxsz + 2 + \
175				decode_getattr_maxsz)
176#define NFS4_enc_open_sz        (compound_encode_hdr_maxsz + \
177                                encode_putfh_maxsz + \
178                                op_encode_hdr_maxsz + \
179                                13 + 3 + 2 + 64 + \
180                                encode_getattr_maxsz + \
181                                encode_getfh_maxsz)
182#define NFS4_dec_open_sz        (compound_decode_hdr_maxsz + \
183                                decode_putfh_maxsz + \
184                                op_decode_hdr_maxsz + 4 + 5 + 2 + 3 + \
185                                decode_getattr_maxsz + \
186                                decode_getfh_maxsz)
187#define NFS4_enc_open_confirm_sz      \
188                                (compound_encode_hdr_maxsz + \
189                                encode_putfh_maxsz + \
190                                op_encode_hdr_maxsz + 5)
191#define NFS4_dec_open_confirm_sz        (compound_decode_hdr_maxsz + \
192                                        decode_putfh_maxsz + \
193                                        op_decode_hdr_maxsz + 4)
194#define NFS4_enc_open_noattr_sz	(compound_encode_hdr_maxsz + \
195					encode_putfh_maxsz + \
196					op_encode_hdr_maxsz + \
197					11)
198#define NFS4_dec_open_noattr_sz	(compound_decode_hdr_maxsz + \
199					decode_putfh_maxsz + \
200					op_decode_hdr_maxsz + \
201					4 + 5 + 2 + 3)
202#define NFS4_enc_open_downgrade_sz \
203				(compound_encode_hdr_maxsz + \
204                                encode_putfh_maxsz + \
205                                op_encode_hdr_maxsz + 7 + \
206				encode_getattr_maxsz)
207#define NFS4_dec_open_downgrade_sz \
208				(compound_decode_hdr_maxsz + \
209                                decode_putfh_maxsz + \
210                                op_decode_hdr_maxsz + 4 + \
211				decode_getattr_maxsz)
212#define NFS4_enc_close_sz       (compound_encode_hdr_maxsz + \
213                                encode_putfh_maxsz + \
214                                op_encode_hdr_maxsz + 5 + \
215				encode_getattr_maxsz)
216#define NFS4_dec_close_sz       (compound_decode_hdr_maxsz + \
217                                decode_putfh_maxsz + \
218                                op_decode_hdr_maxsz + 4 + \
219				decode_getattr_maxsz)
220#define NFS4_enc_setattr_sz     (compound_encode_hdr_maxsz + \
221                                encode_putfh_maxsz + \
222                                op_encode_hdr_maxsz + 4 + \
223                                nfs4_fattr_maxsz + \
224                                encode_getattr_maxsz)
225#define NFS4_dec_setattr_sz     (compound_decode_hdr_maxsz + \
226                                decode_putfh_maxsz + \
227                                op_decode_hdr_maxsz + 3 + \
228                                nfs4_fattr_maxsz)
229#define NFS4_enc_fsinfo_sz	(compound_encode_hdr_maxsz + \
230				encode_putfh_maxsz + \
231				encode_fsinfo_maxsz)
232#define NFS4_dec_fsinfo_sz	(compound_decode_hdr_maxsz + \
233				decode_putfh_maxsz + \
234				decode_fsinfo_maxsz)
235#define NFS4_enc_renew_sz	(compound_encode_hdr_maxsz + \
236				encode_renew_maxsz)
237#define NFS4_dec_renew_sz	(compound_decode_hdr_maxsz + \
238				decode_renew_maxsz)
239#define NFS4_enc_setclientid_sz	(compound_encode_hdr_maxsz + \
240				encode_setclientid_maxsz)
241#define NFS4_dec_setclientid_sz	(compound_decode_hdr_maxsz + \
242				decode_setclientid_maxsz)
243#define NFS4_enc_setclientid_confirm_sz \
244				(compound_encode_hdr_maxsz + \
245				encode_setclientid_confirm_maxsz + \
246				encode_putrootfh_maxsz + \
247				encode_fsinfo_maxsz)
248#define NFS4_dec_setclientid_confirm_sz \
249				(compound_decode_hdr_maxsz + \
250				decode_setclientid_confirm_maxsz + \
251				decode_putrootfh_maxsz + \
252				decode_fsinfo_maxsz)
253#define NFS4_enc_lock_sz        (compound_encode_hdr_maxsz + \
254				encode_putfh_maxsz + \
255				encode_getattr_maxsz + \
256				op_encode_hdr_maxsz + \
257				1 + 1 + 2 + 2 + \
258				1 + 4 + 1 + 2 + \
259				owner_id_maxsz)
260#define NFS4_dec_lock_sz        (compound_decode_hdr_maxsz + \
261				decode_putfh_maxsz + \
262				decode_getattr_maxsz + \
263				op_decode_hdr_maxsz + \
264				2 + 2 + 1 + 2 + \
265				owner_id_maxsz)
266#define NFS4_enc_lockt_sz       (compound_encode_hdr_maxsz + \
267				encode_putfh_maxsz + \
268				encode_getattr_maxsz + \
269				op_encode_hdr_maxsz + \
270				1 + 2 + 2 + 2 + \
271				owner_id_maxsz)
272#define NFS4_dec_lockt_sz       (NFS4_dec_lock_sz)
273#define NFS4_enc_locku_sz       (compound_encode_hdr_maxsz + \
274				encode_putfh_maxsz + \
275				encode_getattr_maxsz + \
276				op_encode_hdr_maxsz + \
277				1 + 1 + 4 + 2 + 2)
278#define NFS4_dec_locku_sz       (compound_decode_hdr_maxsz + \
279				decode_putfh_maxsz + \
280				decode_getattr_maxsz + \
281				op_decode_hdr_maxsz + 4)
282#define NFS4_enc_access_sz	(compound_encode_hdr_maxsz + \
283				encode_putfh_maxsz + \
284				op_encode_hdr_maxsz + 1)
285#define NFS4_dec_access_sz	(compound_decode_hdr_maxsz + \
286				decode_putfh_maxsz + \
287				op_decode_hdr_maxsz + 2)
288#define NFS4_enc_getattr_sz	(compound_encode_hdr_maxsz + \
289				encode_putfh_maxsz + \
290				encode_getattr_maxsz)
291#define NFS4_dec_getattr_sz	(compound_decode_hdr_maxsz + \
292				decode_putfh_maxsz + \
293				decode_getattr_maxsz)
294#define NFS4_enc_lookup_sz	(compound_encode_hdr_maxsz + \
295				encode_putfh_maxsz + \
296				encode_lookup_maxsz + \
297				encode_getattr_maxsz + \
298				encode_getfh_maxsz)
299#define NFS4_dec_lookup_sz	(compound_decode_hdr_maxsz + \
300				decode_putfh_maxsz + \
301				op_decode_hdr_maxsz + \
302				decode_getattr_maxsz + \
303				decode_getfh_maxsz)
304#define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
305				encode_putrootfh_maxsz + \
306				encode_getattr_maxsz + \
307				encode_getfh_maxsz)
308#define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
309				decode_putrootfh_maxsz + \
310				decode_getattr_maxsz + \
311				decode_getfh_maxsz)
312#define NFS4_enc_remove_sz	(compound_encode_hdr_maxsz + \
313				encode_putfh_maxsz + \
314				encode_remove_maxsz + \
315				encode_getattr_maxsz)
316#define NFS4_dec_remove_sz	(compound_decode_hdr_maxsz + \
317				decode_putfh_maxsz + \
318				op_decode_hdr_maxsz + 5 + \
319				decode_getattr_maxsz)
320#define NFS4_enc_rename_sz	(compound_encode_hdr_maxsz + \
321				encode_putfh_maxsz + \
322				encode_savefh_maxsz + \
323				encode_putfh_maxsz + \
324				encode_rename_maxsz + \
325				encode_getattr_maxsz + \
326				encode_restorefh_maxsz + \
327				encode_getattr_maxsz)
328#define NFS4_dec_rename_sz	(compound_decode_hdr_maxsz + \
329				decode_putfh_maxsz + \
330				decode_savefh_maxsz + \
331				decode_putfh_maxsz + \
332				decode_rename_maxsz + \
333				decode_getattr_maxsz + \
334				decode_restorefh_maxsz + \
335				decode_getattr_maxsz)
336#define NFS4_enc_link_sz	(compound_encode_hdr_maxsz + \
337				encode_putfh_maxsz + \
338				encode_savefh_maxsz + \
339				encode_putfh_maxsz + \
340				encode_link_maxsz + \
341				decode_getattr_maxsz + \
342				encode_restorefh_maxsz + \
343				decode_getattr_maxsz)
344#define NFS4_dec_link_sz	(compound_decode_hdr_maxsz + \
345				decode_putfh_maxsz + \
346				decode_savefh_maxsz + \
347				decode_putfh_maxsz + \
348				decode_link_maxsz + \
349				decode_getattr_maxsz + \
350				decode_restorefh_maxsz + \
351				decode_getattr_maxsz)
352#define NFS4_enc_symlink_sz	(compound_encode_hdr_maxsz + \
353				encode_putfh_maxsz + \
354				encode_symlink_maxsz + \
355				encode_getattr_maxsz + \
356				encode_getfh_maxsz)
357#define NFS4_dec_symlink_sz	(compound_decode_hdr_maxsz + \
358				decode_putfh_maxsz + \
359				decode_symlink_maxsz + \
360				decode_getattr_maxsz + \
361				decode_getfh_maxsz)
362#define NFS4_enc_create_sz	(compound_encode_hdr_maxsz + \
363				encode_putfh_maxsz + \
364				encode_savefh_maxsz + \
365				encode_create_maxsz + \
366				encode_getfh_maxsz + \
367				encode_getattr_maxsz + \
368				encode_restorefh_maxsz + \
369				encode_getattr_maxsz)
370#define NFS4_dec_create_sz	(compound_decode_hdr_maxsz + \
371				decode_putfh_maxsz + \
372				decode_savefh_maxsz + \
373				decode_create_maxsz + \
374				decode_getfh_maxsz + \
375				decode_getattr_maxsz + \
376				decode_restorefh_maxsz + \
377				decode_getattr_maxsz)
378#define NFS4_enc_pathconf_sz	(compound_encode_hdr_maxsz + \
379				encode_putfh_maxsz + \
380				encode_getattr_maxsz)
381#define NFS4_dec_pathconf_sz	(compound_decode_hdr_maxsz + \
382				decode_putfh_maxsz + \
383				decode_getattr_maxsz)
384#define NFS4_enc_statfs_sz	(compound_encode_hdr_maxsz + \
385				encode_putfh_maxsz + \
386				encode_getattr_maxsz)
387#define NFS4_dec_statfs_sz	(compound_decode_hdr_maxsz + \
388				decode_putfh_maxsz + \
389				op_decode_hdr_maxsz + 12)
390#define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
391				encode_putfh_maxsz + \
392				encode_getattr_maxsz)
393#define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
394				decode_putfh_maxsz + \
395				decode_getattr_maxsz)
396#define NFS4_enc_delegreturn_sz	(compound_encode_hdr_maxsz + \
397				encode_putfh_maxsz + \
398				encode_delegreturn_maxsz + \
399				encode_getattr_maxsz)
400#define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
401				decode_delegreturn_maxsz + \
402				decode_getattr_maxsz)
403#define NFS4_enc_getacl_sz	(compound_encode_hdr_maxsz + \
404				encode_putfh_maxsz + \
405				encode_getattr_maxsz)
406#define NFS4_dec_getacl_sz	(compound_decode_hdr_maxsz + \
407				decode_putfh_maxsz + \
408				op_decode_hdr_maxsz + \
409				nfs4_fattr_bitmap_maxsz + 1)
410#define NFS4_enc_setacl_sz	(compound_encode_hdr_maxsz + \
411				encode_putfh_maxsz + \
412				op_encode_hdr_maxsz + 4 + \
413				nfs4_fattr_bitmap_maxsz + 1)
414#define NFS4_dec_setacl_sz	(compound_decode_hdr_maxsz + \
415				decode_putfh_maxsz + \
416				op_decode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
417#define NFS4_enc_fs_locations_sz \
418				(compound_encode_hdr_maxsz + \
419				 encode_putfh_maxsz + \
420				 encode_getattr_maxsz)
421#define NFS4_dec_fs_locations_sz \
422				(compound_decode_hdr_maxsz + \
423				 decode_putfh_maxsz + \
424				 op_decode_hdr_maxsz + \
425				 nfs4_fattr_bitmap_maxsz)
426
427static struct {
428	unsigned int	mode;
429	unsigned int	nfs2type;
430} nfs_type2fmt[] = {
431	{ 0,		NFNON	     },
432	{ S_IFREG,	NFREG	     },
433	{ S_IFDIR,	NFDIR	     },
434	{ S_IFBLK,	NFBLK	     },
435	{ S_IFCHR,	NFCHR	     },
436	{ S_IFLNK,	NFLNK	     },
437	{ S_IFSOCK,	NFSOCK	     },
438	{ S_IFIFO,	NFFIFO	     },
439	{ 0,		NFNON	     },
440	{ 0,		NFNON	     },
441};
442
443struct compound_hdr {
444	int32_t		status;
445	uint32_t	nops;
446	uint32_t	taglen;
447	char *		tag;
448};
449
450/*
451 * START OF "GENERIC" ENCODE ROUTINES.
452 *   These may look a little ugly since they are imported from a "generic"
453 * set of XDR encode/decode routines which are intended to be shared by
454 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
455 *
456 * If the pain of reading these is too great, it should be a straightforward
457 * task to translate them into Linux-specific versions which are more
458 * consistent with the style used in NFSv2/v3...
459 */
460#define WRITE32(n)               *p++ = htonl(n)
461#define WRITE64(n)               do {				\
462	*p++ = htonl((uint32_t)((n) >> 32));				\
463	*p++ = htonl((uint32_t)(n));					\
464} while (0)
465#define WRITEMEM(ptr,nbytes)     do {				\
466	p = xdr_encode_opaque_fixed(p, ptr, nbytes);		\
467} while (0)
468
469#define RESERVE_SPACE(nbytes)	do {				\
470	p = xdr_reserve_space(xdr, nbytes);			\
471	if (!p) printk("RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
472	BUG_ON(!p);						\
473} while (0)
474
475static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
476{
477	__be32 *p;
478
479	p = xdr_reserve_space(xdr, 4 + len);
480	BUG_ON(p == NULL);
481	xdr_encode_opaque(p, str, len);
482}
483
484static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
485{
486	__be32 *p;
487
488	dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
489	BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
490	RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
491	WRITE32(hdr->taglen);
492	WRITEMEM(hdr->tag, hdr->taglen);
493	WRITE32(NFS4_MINOR_VERSION);
494	WRITE32(hdr->nops);
495	return 0;
496}
497
498static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
499{
500	__be32 *p;
501
502	p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
503	BUG_ON(p == NULL);
504	xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
505}
506
507static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
508{
509	char owner_name[IDMAP_NAMESZ];
510	char owner_group[IDMAP_NAMESZ];
511	int owner_namelen = 0;
512	int owner_grouplen = 0;
513	__be32 *p;
514	__be32 *q;
515	int len;
516	uint32_t bmval0 = 0;
517	uint32_t bmval1 = 0;
518	int status;
519
520	/*
521	 * We reserve enough space to write the entire attribute buffer at once.
522	 * In the worst-case, this would be
523	 *   12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
524	 *          = 36 bytes, plus any contribution from variable-length fields
525	 *            such as owner/group.
526	 */
527	len = 16;
528
529	/* Sigh */
530	if (iap->ia_valid & ATTR_SIZE)
531		len += 8;
532	if (iap->ia_valid & ATTR_MODE)
533		len += 4;
534	if (iap->ia_valid & ATTR_UID) {
535		owner_namelen = nfs_map_uid_to_name(server->nfs_client, iap->ia_uid, owner_name);
536		if (owner_namelen < 0) {
537			printk(KERN_WARNING "nfs: couldn't resolve uid %d to string\n",
538			       iap->ia_uid);
539			strcpy(owner_name, "nobody");
540			owner_namelen = sizeof("nobody") - 1;
541			/* goto out; */
542		}
543		len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
544	}
545	if (iap->ia_valid & ATTR_GID) {
546		owner_grouplen = nfs_map_gid_to_group(server->nfs_client, iap->ia_gid, owner_group);
547		if (owner_grouplen < 0) {
548			printk(KERN_WARNING "nfs4: couldn't resolve gid %d to string\n",
549			       iap->ia_gid);
550			strcpy(owner_group, "nobody");
551			owner_grouplen = sizeof("nobody") - 1;
552			/* goto out; */
553		}
554		len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
555	}
556	if (iap->ia_valid & ATTR_ATIME_SET)
557		len += 16;
558	else if (iap->ia_valid & ATTR_ATIME)
559		len += 4;
560	if (iap->ia_valid & ATTR_MTIME_SET)
561		len += 16;
562	else if (iap->ia_valid & ATTR_MTIME)
563		len += 4;
564	RESERVE_SPACE(len);
565
566	/*
567	 * We write the bitmap length now, but leave the bitmap and the attribute
568	 * buffer length to be backfilled at the end of this routine.
569	 */
570	WRITE32(2);
571	q = p;
572	p += 3;
573
574	if (iap->ia_valid & ATTR_SIZE) {
575		bmval0 |= FATTR4_WORD0_SIZE;
576		WRITE64(iap->ia_size);
577	}
578	if (iap->ia_valid & ATTR_MODE) {
579		bmval1 |= FATTR4_WORD1_MODE;
580		WRITE32(iap->ia_mode & S_IALLUGO);
581	}
582	if (iap->ia_valid & ATTR_UID) {
583		bmval1 |= FATTR4_WORD1_OWNER;
584		WRITE32(owner_namelen);
585		WRITEMEM(owner_name, owner_namelen);
586	}
587	if (iap->ia_valid & ATTR_GID) {
588		bmval1 |= FATTR4_WORD1_OWNER_GROUP;
589		WRITE32(owner_grouplen);
590		WRITEMEM(owner_group, owner_grouplen);
591	}
592	if (iap->ia_valid & ATTR_ATIME_SET) {
593		bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
594		WRITE32(NFS4_SET_TO_CLIENT_TIME);
595		WRITE32(0);
596		WRITE32(iap->ia_mtime.tv_sec);
597		WRITE32(iap->ia_mtime.tv_nsec);
598	}
599	else if (iap->ia_valid & ATTR_ATIME) {
600		bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
601		WRITE32(NFS4_SET_TO_SERVER_TIME);
602	}
603	if (iap->ia_valid & ATTR_MTIME_SET) {
604		bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
605		WRITE32(NFS4_SET_TO_CLIENT_TIME);
606		WRITE32(0);
607		WRITE32(iap->ia_mtime.tv_sec);
608		WRITE32(iap->ia_mtime.tv_nsec);
609	}
610	else if (iap->ia_valid & ATTR_MTIME) {
611		bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
612		WRITE32(NFS4_SET_TO_SERVER_TIME);
613	}
614
615	/*
616	 * Now we backfill the bitmap and the attribute buffer length.
617	 */
618	if (len != ((char *)p - (char *)q) + 4) {
619		printk ("encode_attr: Attr length calculation error! %u != %Zu\n",
620				len, ((char *)p - (char *)q) + 4);
621		BUG();
622	}
623	len = (char *)p - (char *)q - 12;
624	*q++ = htonl(bmval0);
625	*q++ = htonl(bmval1);
626	*q++ = htonl(len);
627
628	status = 0;
629/* out: */
630	return status;
631}
632
633static int encode_access(struct xdr_stream *xdr, u32 access)
634{
635	__be32 *p;
636
637	RESERVE_SPACE(8);
638	WRITE32(OP_ACCESS);
639	WRITE32(access);
640
641	return 0;
642}
643
644static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
645{
646	__be32 *p;
647
648	RESERVE_SPACE(8+NFS4_STATEID_SIZE);
649	WRITE32(OP_CLOSE);
650	WRITE32(arg->seqid->sequence->counter);
651	WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
652
653	return 0;
654}
655
656static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
657{
658	__be32 *p;
659
660        RESERVE_SPACE(16);
661        WRITE32(OP_COMMIT);
662        WRITE64(args->offset);
663        WRITE32(args->count);
664
665        return 0;
666}
667
668static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
669{
670	__be32 *p;
671
672	RESERVE_SPACE(8);
673	WRITE32(OP_CREATE);
674	WRITE32(create->ftype);
675
676	switch (create->ftype) {
677	case NF4LNK:
678		RESERVE_SPACE(4);
679		WRITE32(create->u.symlink.len);
680		xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
681		break;
682
683	case NF4BLK: case NF4CHR:
684		RESERVE_SPACE(8);
685		WRITE32(create->u.device.specdata1);
686		WRITE32(create->u.device.specdata2);
687		break;
688
689	default:
690		break;
691	}
692
693	RESERVE_SPACE(4 + create->name->len);
694	WRITE32(create->name->len);
695	WRITEMEM(create->name->name, create->name->len);
696
697	return encode_attrs(xdr, create->attrs, create->server);
698}
699
700static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
701{
702        __be32 *p;
703
704        RESERVE_SPACE(12);
705        WRITE32(OP_GETATTR);
706        WRITE32(1);
707        WRITE32(bitmap);
708        return 0;
709}
710
711static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
712{
713        __be32 *p;
714
715        RESERVE_SPACE(16);
716        WRITE32(OP_GETATTR);
717        WRITE32(2);
718        WRITE32(bm0);
719        WRITE32(bm1);
720        return 0;
721}
722
723static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
724{
725	return encode_getattr_two(xdr,
726			bitmask[0] & nfs4_fattr_bitmap[0],
727			bitmask[1] & nfs4_fattr_bitmap[1]);
728}
729
730static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
731{
732	return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
733			bitmask[1] & nfs4_fsinfo_bitmap[1]);
734}
735
736static int encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask)
737{
738	return encode_getattr_two(xdr,
739				  bitmask[0] & nfs4_fs_locations_bitmap[0],
740				  bitmask[1] & nfs4_fs_locations_bitmap[1]);
741}
742
743static int encode_getfh(struct xdr_stream *xdr)
744{
745	__be32 *p;
746
747	RESERVE_SPACE(4);
748	WRITE32(OP_GETFH);
749
750	return 0;
751}
752
753static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
754{
755	__be32 *p;
756
757	RESERVE_SPACE(8 + name->len);
758	WRITE32(OP_LINK);
759	WRITE32(name->len);
760	WRITEMEM(name->name, name->len);
761
762	return 0;
763}
764
765static inline int nfs4_lock_type(struct file_lock *fl, int block)
766{
767	if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
768		return block ? NFS4_READW_LT : NFS4_READ_LT;
769	return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
770}
771
772static inline uint64_t nfs4_lock_length(struct file_lock *fl)
773{
774	if (fl->fl_end == OFFSET_MAX)
775		return ~(uint64_t)0;
776	return fl->fl_end - fl->fl_start + 1;
777}
778
779/*
780 * opcode,type,reclaim,offset,length,new_lock_owner = 32
781 * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
782 */
783static int encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args)
784{
785	__be32 *p;
786
787	RESERVE_SPACE(32);
788	WRITE32(OP_LOCK);
789	WRITE32(nfs4_lock_type(args->fl, args->block));
790	WRITE32(args->reclaim);
791	WRITE64(args->fl->fl_start);
792	WRITE64(nfs4_lock_length(args->fl));
793	WRITE32(args->new_lock_owner);
794	if (args->new_lock_owner){
795		RESERVE_SPACE(4+NFS4_STATEID_SIZE+20);
796		WRITE32(args->open_seqid->sequence->counter);
797		WRITEMEM(args->open_stateid->data, NFS4_STATEID_SIZE);
798		WRITE32(args->lock_seqid->sequence->counter);
799		WRITE64(args->lock_owner.clientid);
800		WRITE32(4);
801		WRITE32(args->lock_owner.id);
802	}
803	else {
804		RESERVE_SPACE(NFS4_STATEID_SIZE+4);
805		WRITEMEM(args->lock_stateid->data, NFS4_STATEID_SIZE);
806		WRITE32(args->lock_seqid->sequence->counter);
807	}
808
809	return 0;
810}
811
812static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args)
813{
814	__be32 *p;
815
816	RESERVE_SPACE(40);
817	WRITE32(OP_LOCKT);
818	WRITE32(nfs4_lock_type(args->fl, 0));
819	WRITE64(args->fl->fl_start);
820	WRITE64(nfs4_lock_length(args->fl));
821	WRITE64(args->lock_owner.clientid);
822	WRITE32(4);
823	WRITE32(args->lock_owner.id);
824
825	return 0;
826}
827
828static int encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args)
829{
830	__be32 *p;
831
832	RESERVE_SPACE(12+NFS4_STATEID_SIZE+16);
833	WRITE32(OP_LOCKU);
834	WRITE32(nfs4_lock_type(args->fl, 0));
835	WRITE32(args->seqid->sequence->counter);
836	WRITEMEM(args->stateid->data, NFS4_STATEID_SIZE);
837	WRITE64(args->fl->fl_start);
838	WRITE64(nfs4_lock_length(args->fl));
839
840	return 0;
841}
842
843static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
844{
845	int len = name->len;
846	__be32 *p;
847
848	RESERVE_SPACE(8 + len);
849	WRITE32(OP_LOOKUP);
850	WRITE32(len);
851	WRITEMEM(name->name, len);
852
853	return 0;
854}
855
856static void encode_share_access(struct xdr_stream *xdr, int open_flags)
857{
858	__be32 *p;
859
860	RESERVE_SPACE(8);
861	switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
862		case FMODE_READ:
863			WRITE32(NFS4_SHARE_ACCESS_READ);
864			break;
865		case FMODE_WRITE:
866			WRITE32(NFS4_SHARE_ACCESS_WRITE);
867			break;
868		case FMODE_READ|FMODE_WRITE:
869			WRITE32(NFS4_SHARE_ACCESS_BOTH);
870			break;
871		default:
872			BUG();
873	}
874	WRITE32(0);		/* for linux, share_deny = 0 always */
875}
876
877static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
878{
879	__be32 *p;
880 /*
881 * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
882 * owner 4 = 32
883 */
884	RESERVE_SPACE(8);
885	WRITE32(OP_OPEN);
886	WRITE32(arg->seqid->sequence->counter);
887	encode_share_access(xdr, arg->open_flags);
888	RESERVE_SPACE(16);
889	WRITE64(arg->clientid);
890	WRITE32(4);
891	WRITE32(arg->id);
892}
893
894static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
895{
896	__be32 *p;
897
898	RESERVE_SPACE(4);
899	switch(arg->open_flags & O_EXCL) {
900		case 0:
901			WRITE32(NFS4_CREATE_UNCHECKED);
902			encode_attrs(xdr, arg->u.attrs, arg->server);
903			break;
904		default:
905			WRITE32(NFS4_CREATE_EXCLUSIVE);
906			encode_nfs4_verifier(xdr, &arg->u.verifier);
907	}
908}
909
910static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
911{
912	__be32 *p;
913
914	RESERVE_SPACE(4);
915	switch (arg->open_flags & O_CREAT) {
916		case 0:
917			WRITE32(NFS4_OPEN_NOCREATE);
918			break;
919		default:
920			BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
921			WRITE32(NFS4_OPEN_CREATE);
922			encode_createmode(xdr, arg);
923	}
924}
925
926static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
927{
928	__be32 *p;
929
930	RESERVE_SPACE(4);
931	switch (delegation_type) {
932		case 0:
933			WRITE32(NFS4_OPEN_DELEGATE_NONE);
934			break;
935		case FMODE_READ:
936			WRITE32(NFS4_OPEN_DELEGATE_READ);
937			break;
938		case FMODE_WRITE|FMODE_READ:
939			WRITE32(NFS4_OPEN_DELEGATE_WRITE);
940			break;
941		default:
942			BUG();
943	}
944}
945
946static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
947{
948	__be32 *p;
949
950	RESERVE_SPACE(4);
951	WRITE32(NFS4_OPEN_CLAIM_NULL);
952	encode_string(xdr, name->len, name->name);
953}
954
955static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
956{
957	__be32 *p;
958
959	RESERVE_SPACE(4);
960	WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
961	encode_delegation_type(xdr, type);
962}
963
964static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
965{
966	__be32 *p;
967
968	RESERVE_SPACE(4+NFS4_STATEID_SIZE);
969	WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
970	WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
971	encode_string(xdr, name->len, name->name);
972}
973
974static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
975{
976	encode_openhdr(xdr, arg);
977	encode_opentype(xdr, arg);
978	switch (arg->claim) {
979		case NFS4_OPEN_CLAIM_NULL:
980			encode_claim_null(xdr, arg->name);
981			break;
982		case NFS4_OPEN_CLAIM_PREVIOUS:
983			encode_claim_previous(xdr, arg->u.delegation_type);
984			break;
985		case NFS4_OPEN_CLAIM_DELEGATE_CUR:
986			encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
987			break;
988		default:
989			BUG();
990	}
991	return 0;
992}
993
994static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
995{
996	__be32 *p;
997
998	RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
999	WRITE32(OP_OPEN_CONFIRM);
1000	WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
1001	WRITE32(arg->seqid->sequence->counter);
1002
1003	return 0;
1004}
1005
1006static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
1007{
1008	__be32 *p;
1009
1010	RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
1011	WRITE32(OP_OPEN_DOWNGRADE);
1012	WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
1013	WRITE32(arg->seqid->sequence->counter);
1014	encode_share_access(xdr, arg->open_flags);
1015	return 0;
1016}
1017
1018static int
1019encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
1020{
1021	int len = fh->size;
1022	__be32 *p;
1023
1024	RESERVE_SPACE(8 + len);
1025	WRITE32(OP_PUTFH);
1026	WRITE32(len);
1027	WRITEMEM(fh->data, len);
1028
1029	return 0;
1030}
1031
1032static int encode_putrootfh(struct xdr_stream *xdr)
1033{
1034        __be32 *p;
1035
1036        RESERVE_SPACE(4);
1037        WRITE32(OP_PUTROOTFH);
1038
1039        return 0;
1040}
1041
1042static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
1043{
1044	nfs4_stateid stateid;
1045	__be32 *p;
1046
1047	RESERVE_SPACE(NFS4_STATEID_SIZE);
1048	if (ctx->state != NULL) {
1049		nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
1050		WRITEMEM(stateid.data, NFS4_STATEID_SIZE);
1051	} else
1052		WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
1053}
1054
1055static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
1056{
1057	__be32 *p;
1058
1059	RESERVE_SPACE(4);
1060	WRITE32(OP_READ);
1061
1062	encode_stateid(xdr, args->context);
1063
1064	RESERVE_SPACE(12);
1065	WRITE64(args->offset);
1066	WRITE32(args->count);
1067
1068	return 0;
1069}
1070
1071static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
1072{
1073	struct rpc_auth *auth = req->rq_task->tk_auth;
1074	uint32_t attrs[2] = {
1075		FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1076		FATTR4_WORD1_MOUNTED_ON_FILEID,
1077	};
1078	int replen;
1079	__be32 *p;
1080
1081	RESERVE_SPACE(12+NFS4_VERIFIER_SIZE+20);
1082	WRITE32(OP_READDIR);
1083	WRITE64(readdir->cookie);
1084	WRITEMEM(readdir->verifier.data, NFS4_VERIFIER_SIZE);
1085	WRITE32(readdir->count >> 1);  /* We're not doing readdirplus */
1086	WRITE32(readdir->count);
1087	WRITE32(2);
1088	/* Switch to mounted_on_fileid if the server supports it */
1089	if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1090		attrs[0] &= ~FATTR4_WORD0_FILEID;
1091	else
1092		attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1093	WRITE32(attrs[0] & readdir->bitmask[0]);
1094	WRITE32(attrs[1] & readdir->bitmask[1]);
1095	dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
1096			__FUNCTION__,
1097			(unsigned long long)readdir->cookie,
1098			((u32 *)readdir->verifier.data)[0],
1099			((u32 *)readdir->verifier.data)[1],
1100			attrs[0] & readdir->bitmask[0],
1101			attrs[1] & readdir->bitmask[1]);
1102
1103	/* set up reply kvec
1104	 *    toplevel_status + taglen + rescount + OP_PUTFH + status
1105	 *      + OP_READDIR + status + verifer(2)  = 9
1106	 */
1107	replen = (RPC_REPHDRSIZE + auth->au_rslack + 9) << 2;
1108	xdr_inline_pages(&req->rq_rcv_buf, replen, readdir->pages,
1109			 readdir->pgbase, readdir->count);
1110	dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1111			__FUNCTION__, replen, readdir->pages,
1112			readdir->pgbase, readdir->count);
1113
1114	return 0;
1115}
1116
1117static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1118{
1119	struct rpc_auth *auth = req->rq_task->tk_auth;
1120	unsigned int replen;
1121	__be32 *p;
1122
1123	RESERVE_SPACE(4);
1124	WRITE32(OP_READLINK);
1125
1126	/* set up reply kvec
1127	 *    toplevel_status + taglen + rescount + OP_PUTFH + status
1128	 *      + OP_READLINK + status + string length = 8
1129	 */
1130	replen = (RPC_REPHDRSIZE + auth->au_rslack + 8) << 2;
1131	xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages,
1132			readlink->pgbase, readlink->pglen);
1133
1134	return 0;
1135}
1136
1137static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1138{
1139	__be32 *p;
1140
1141	RESERVE_SPACE(8 + name->len);
1142	WRITE32(OP_REMOVE);
1143	WRITE32(name->len);
1144	WRITEMEM(name->name, name->len);
1145
1146	return 0;
1147}
1148
1149static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1150{
1151	__be32 *p;
1152
1153	RESERVE_SPACE(8 + oldname->len);
1154	WRITE32(OP_RENAME);
1155	WRITE32(oldname->len);
1156	WRITEMEM(oldname->name, oldname->len);
1157
1158	RESERVE_SPACE(4 + newname->len);
1159	WRITE32(newname->len);
1160	WRITEMEM(newname->name, newname->len);
1161
1162	return 0;
1163}
1164
1165static int encode_renew(struct xdr_stream *xdr, const struct nfs_client *client_stateid)
1166{
1167	__be32 *p;
1168
1169	RESERVE_SPACE(12);
1170	WRITE32(OP_RENEW);
1171	WRITE64(client_stateid->cl_clientid);
1172
1173	return 0;
1174}
1175
1176static int
1177encode_restorefh(struct xdr_stream *xdr)
1178{
1179	__be32 *p;
1180
1181	RESERVE_SPACE(4);
1182	WRITE32(OP_RESTOREFH);
1183
1184	return 0;
1185}
1186
1187static int
1188encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1189{
1190	__be32 *p;
1191
1192	RESERVE_SPACE(4+NFS4_STATEID_SIZE);
1193	WRITE32(OP_SETATTR);
1194	WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
1195	RESERVE_SPACE(2*4);
1196	WRITE32(1);
1197	WRITE32(FATTR4_WORD0_ACL);
1198	if (arg->acl_len % 4)
1199		return -EINVAL;
1200	RESERVE_SPACE(4);
1201	WRITE32(arg->acl_len);
1202	xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1203	return 0;
1204}
1205
1206static int
1207encode_savefh(struct xdr_stream *xdr)
1208{
1209	__be32 *p;
1210
1211	RESERVE_SPACE(4);
1212	WRITE32(OP_SAVEFH);
1213
1214	return 0;
1215}
1216
1217static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1218{
1219	int status;
1220	__be32 *p;
1221
1222        RESERVE_SPACE(4+NFS4_STATEID_SIZE);
1223        WRITE32(OP_SETATTR);
1224	WRITEMEM(arg->stateid.data, NFS4_STATEID_SIZE);
1225
1226        if ((status = encode_attrs(xdr, arg->iap, server)))
1227		return status;
1228
1229        return 0;
1230}
1231
1232static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1233{
1234	__be32 *p;
1235
1236	RESERVE_SPACE(4 + NFS4_VERIFIER_SIZE);
1237	WRITE32(OP_SETCLIENTID);
1238	WRITEMEM(setclientid->sc_verifier->data, NFS4_VERIFIER_SIZE);
1239
1240	encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1241	RESERVE_SPACE(4);
1242	WRITE32(setclientid->sc_prog);
1243	encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1244	encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1245	RESERVE_SPACE(4);
1246	WRITE32(setclientid->sc_cb_ident);
1247
1248	return 0;
1249}
1250
1251static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs_client *client_state)
1252{
1253        __be32 *p;
1254
1255        RESERVE_SPACE(12 + NFS4_VERIFIER_SIZE);
1256        WRITE32(OP_SETCLIENTID_CONFIRM);
1257        WRITE64(client_state->cl_clientid);
1258        WRITEMEM(client_state->cl_confirm.data, NFS4_VERIFIER_SIZE);
1259
1260        return 0;
1261}
1262
1263static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1264{
1265	__be32 *p;
1266
1267	RESERVE_SPACE(4);
1268	WRITE32(OP_WRITE);
1269
1270	encode_stateid(xdr, args->context);
1271
1272	RESERVE_SPACE(16);
1273	WRITE64(args->offset);
1274	WRITE32(args->stable);
1275	WRITE32(args->count);
1276
1277	xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1278
1279	return 0;
1280}
1281
1282static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1283{
1284	__be32 *p;
1285
1286	RESERVE_SPACE(4+NFS4_STATEID_SIZE);
1287
1288	WRITE32(OP_DELEGRETURN);
1289	WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
1290	return 0;
1291
1292}
1293/*
1294 * END OF "GENERIC" ENCODE ROUTINES.
1295 */
1296
1297/*
1298 * Encode an ACCESS request
1299 */
1300static int nfs4_xdr_enc_access(struct rpc_rqst *req, __be32 *p, const struct nfs4_accessargs *args)
1301{
1302	struct xdr_stream xdr;
1303	struct compound_hdr hdr = {
1304		.nops = 2,
1305	};
1306	int status;
1307
1308	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1309	encode_compound_hdr(&xdr, &hdr);
1310	if ((status = encode_putfh(&xdr, args->fh)) == 0)
1311		status = encode_access(&xdr, args->access);
1312	return status;
1313}
1314
1315/*
1316 * Encode LOOKUP request
1317 */
1318static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_arg *args)
1319{
1320	struct xdr_stream xdr;
1321	struct compound_hdr hdr = {
1322		.nops = 4,
1323	};
1324	int status;
1325
1326	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1327	encode_compound_hdr(&xdr, &hdr);
1328	if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1329		goto out;
1330	if ((status = encode_lookup(&xdr, args->name)) != 0)
1331		goto out;
1332	if ((status = encode_getfh(&xdr)) != 0)
1333		goto out;
1334	status = encode_getfattr(&xdr, args->bitmask);
1335out:
1336	return status;
1337}
1338
1339/*
1340 * Encode LOOKUP_ROOT request
1341 */
1342static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_root_arg *args)
1343{
1344	struct xdr_stream xdr;
1345	struct compound_hdr hdr = {
1346		.nops = 3,
1347	};
1348	int status;
1349
1350	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1351	encode_compound_hdr(&xdr, &hdr);
1352	if ((status = encode_putrootfh(&xdr)) != 0)
1353		goto out;
1354	if ((status = encode_getfh(&xdr)) == 0)
1355		status = encode_getfattr(&xdr, args->bitmask);
1356out:
1357	return status;
1358}
1359
1360/*
1361 * Encode REMOVE request
1362 */
1363static int nfs4_xdr_enc_remove(struct rpc_rqst *req, __be32 *p, const struct nfs4_remove_arg *args)
1364{
1365	struct xdr_stream xdr;
1366	struct compound_hdr hdr = {
1367		.nops = 3,
1368	};
1369	int status;
1370
1371	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1372	encode_compound_hdr(&xdr, &hdr);
1373	if ((status = encode_putfh(&xdr, args->fh)) != 0)
1374		goto out;
1375	if ((status = encode_remove(&xdr, args->name)) != 0)
1376		goto out;
1377	status = encode_getfattr(&xdr, args->bitmask);
1378out:
1379	return status;
1380}
1381
1382/*
1383 * Encode RENAME request
1384 */
1385static int nfs4_xdr_enc_rename(struct rpc_rqst *req, __be32 *p, const struct nfs4_rename_arg *args)
1386{
1387	struct xdr_stream xdr;
1388	struct compound_hdr hdr = {
1389		.nops = 7,
1390	};
1391	int status;
1392
1393	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1394	encode_compound_hdr(&xdr, &hdr);
1395	if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1396		goto out;
1397	if ((status = encode_savefh(&xdr)) != 0)
1398		goto out;
1399	if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1400		goto out;
1401	if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
1402		goto out;
1403	if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1404		goto out;
1405	if ((status = encode_restorefh(&xdr)) != 0)
1406		goto out;
1407	status = encode_getfattr(&xdr, args->bitmask);
1408out:
1409	return status;
1410}
1411
1412/*
1413 * Encode LINK request
1414 */
1415static int nfs4_xdr_enc_link(struct rpc_rqst *req, __be32 *p, const struct nfs4_link_arg *args)
1416{
1417	struct xdr_stream xdr;
1418	struct compound_hdr hdr = {
1419		.nops = 7,
1420	};
1421	int status;
1422
1423	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1424	encode_compound_hdr(&xdr, &hdr);
1425	if ((status = encode_putfh(&xdr, args->fh)) != 0)
1426		goto out;
1427	if ((status = encode_savefh(&xdr)) != 0)
1428		goto out;
1429	if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1430		goto out;
1431	if ((status = encode_link(&xdr, args->name)) != 0)
1432		goto out;
1433	if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1434		goto out;
1435	if ((status = encode_restorefh(&xdr)) != 0)
1436		goto out;
1437	status = encode_getfattr(&xdr, args->bitmask);
1438out:
1439	return status;
1440}
1441
1442/*
1443 * Encode CREATE request
1444 */
1445static int nfs4_xdr_enc_create(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
1446{
1447	struct xdr_stream xdr;
1448	struct compound_hdr hdr = {
1449		.nops = 7,
1450	};
1451	int status;
1452
1453	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1454	encode_compound_hdr(&xdr, &hdr);
1455	if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1456		goto out;
1457	if ((status = encode_savefh(&xdr)) != 0)
1458		goto out;
1459	if ((status = encode_create(&xdr, args)) != 0)
1460		goto out;
1461	if ((status = encode_getfh(&xdr)) != 0)
1462		goto out;
1463	if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1464		goto out;
1465	if ((status = encode_restorefh(&xdr)) != 0)
1466		goto out;
1467	status = encode_getfattr(&xdr, args->bitmask);
1468out:
1469	return status;
1470}
1471
1472/*
1473 * Encode SYMLINK request
1474 */
1475static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
1476{
1477	return nfs4_xdr_enc_create(req, p, args);
1478}
1479
1480/*
1481 * Encode GETATTR request
1482 */
1483static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, __be32 *p, const struct nfs4_getattr_arg *args)
1484{
1485	struct xdr_stream xdr;
1486	struct compound_hdr hdr = {
1487		.nops = 2,
1488	};
1489	int status;
1490
1491	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1492	encode_compound_hdr(&xdr, &hdr);
1493	if ((status = encode_putfh(&xdr, args->fh)) == 0)
1494		status = encode_getfattr(&xdr, args->bitmask);
1495	return status;
1496}
1497
1498/*
1499 * Encode a CLOSE request
1500 */
1501static int nfs4_xdr_enc_close(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
1502{
1503        struct xdr_stream xdr;
1504        struct compound_hdr hdr = {
1505                .nops   = 3,
1506        };
1507        int status;
1508
1509        xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1510        encode_compound_hdr(&xdr, &hdr);
1511        status = encode_putfh(&xdr, args->fh);
1512        if(status)
1513                goto out;
1514        status = encode_close(&xdr, args);
1515	if (status != 0)
1516		goto out;
1517	status = encode_getfattr(&xdr, args->bitmask);
1518out:
1519        return status;
1520}
1521
1522/*
1523 * Encode an OPEN request
1524 */
1525static int nfs4_xdr_enc_open(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
1526{
1527	struct xdr_stream xdr;
1528	struct compound_hdr hdr = {
1529		.nops = 7,
1530	};
1531	int status;
1532
1533	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1534	encode_compound_hdr(&xdr, &hdr);
1535	status = encode_putfh(&xdr, args->fh);
1536	if (status)
1537		goto out;
1538	status = encode_savefh(&xdr);
1539	if (status)
1540		goto out;
1541	status = encode_open(&xdr, args);
1542	if (status)
1543		goto out;
1544	status = encode_getfh(&xdr);
1545	if (status)
1546		goto out;
1547	status = encode_getfattr(&xdr, args->bitmask);
1548	if (status)
1549		goto out;
1550	status = encode_restorefh(&xdr);
1551	if (status)
1552		goto out;
1553	status = encode_getfattr(&xdr, args->bitmask);
1554out:
1555	return status;
1556}
1557
1558/*
1559 * Encode an OPEN_CONFIRM request
1560 */
1561static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_open_confirmargs *args)
1562{
1563	struct xdr_stream xdr;
1564	struct compound_hdr hdr = {
1565		.nops   = 2,
1566	};
1567	int status;
1568
1569	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1570	encode_compound_hdr(&xdr, &hdr);
1571	status = encode_putfh(&xdr, args->fh);
1572	if(status)
1573		goto out;
1574	status = encode_open_confirm(&xdr, args);
1575out:
1576	return status;
1577}
1578
1579/*
1580 * Encode an OPEN request with no attributes.
1581 */
1582static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
1583{
1584	struct xdr_stream xdr;
1585	struct compound_hdr hdr = {
1586		.nops   = 3,
1587	};
1588	int status;
1589
1590	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1591	encode_compound_hdr(&xdr, &hdr);
1592	status = encode_putfh(&xdr, args->fh);
1593	if (status)
1594		goto out;
1595	status = encode_open(&xdr, args);
1596	if (status)
1597		goto out;
1598	status = encode_getfattr(&xdr, args->bitmask);
1599out:
1600	return status;
1601}
1602
1603/*
1604 * Encode an OPEN_DOWNGRADE request
1605 */
1606static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
1607{
1608	struct xdr_stream xdr;
1609	struct compound_hdr hdr = {
1610		.nops	= 3,
1611	};
1612	int status;
1613
1614	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1615	encode_compound_hdr(&xdr, &hdr);
1616	status = encode_putfh(&xdr, args->fh);
1617	if (status)
1618		goto out;
1619	status = encode_open_downgrade(&xdr, args);
1620	if (status != 0)
1621		goto out;
1622	status = encode_getfattr(&xdr, args->bitmask);
1623out:
1624	return status;
1625}
1626
1627/*
1628 * Encode a LOCK request
1629 */
1630static int nfs4_xdr_enc_lock(struct rpc_rqst *req, __be32 *p, struct nfs_lock_args *args)
1631{
1632	struct xdr_stream xdr;
1633	struct compound_hdr hdr = {
1634		.nops   = 2,
1635	};
1636	int status;
1637
1638	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1639	encode_compound_hdr(&xdr, &hdr);
1640	status = encode_putfh(&xdr, args->fh);
1641	if(status)
1642		goto out;
1643	status = encode_lock(&xdr, args);
1644out:
1645	return status;
1646}
1647
1648/*
1649 * Encode a LOCKT request
1650 */
1651static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, __be32 *p, struct nfs_lockt_args *args)
1652{
1653	struct xdr_stream xdr;
1654	struct compound_hdr hdr = {
1655		.nops   = 2,
1656	};
1657	int status;
1658
1659	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1660	encode_compound_hdr(&xdr, &hdr);
1661	status = encode_putfh(&xdr, args->fh);
1662	if(status)
1663		goto out;
1664	status = encode_lockt(&xdr, args);
1665out:
1666	return status;
1667}
1668
1669/*
1670 * Encode a LOCKU request
1671 */
1672static int nfs4_xdr_enc_locku(struct rpc_rqst *req, __be32 *p, struct nfs_locku_args *args)
1673{
1674	struct xdr_stream xdr;
1675	struct compound_hdr hdr = {
1676		.nops   = 2,
1677	};
1678	int status;
1679
1680	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1681	encode_compound_hdr(&xdr, &hdr);
1682	status = encode_putfh(&xdr, args->fh);
1683	if(status)
1684		goto out;
1685	status = encode_locku(&xdr, args);
1686out:
1687	return status;
1688}
1689
1690/*
1691 * Encode a READLINK request
1692 */
1693static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_readlink *args)
1694{
1695	struct xdr_stream xdr;
1696	struct compound_hdr hdr = {
1697		.nops = 2,
1698	};
1699	int status;
1700
1701	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1702	encode_compound_hdr(&xdr, &hdr);
1703	status = encode_putfh(&xdr, args->fh);
1704	if(status)
1705		goto out;
1706	status = encode_readlink(&xdr, args, req);
1707out:
1708	return status;
1709}
1710
1711/*
1712 * Encode a READDIR request
1713 */
1714static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, __be32 *p, const struct nfs4_readdir_arg *args)
1715{
1716	struct xdr_stream xdr;
1717	struct compound_hdr hdr = {
1718		.nops = 2,
1719	};
1720	int status;
1721
1722	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1723	encode_compound_hdr(&xdr, &hdr);
1724	status = encode_putfh(&xdr, args->fh);
1725	if(status)
1726		goto out;
1727	status = encode_readdir(&xdr, args, req);
1728out:
1729	return status;
1730}
1731
1732/*
1733 * Encode a READ request
1734 */
1735static int nfs4_xdr_enc_read(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
1736{
1737	struct rpc_auth	*auth = req->rq_task->tk_auth;
1738	struct xdr_stream xdr;
1739	struct compound_hdr hdr = {
1740		.nops = 2,
1741	};
1742	int replen, status;
1743
1744	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1745	encode_compound_hdr(&xdr, &hdr);
1746	status = encode_putfh(&xdr, args->fh);
1747	if (status)
1748		goto out;
1749	status = encode_read(&xdr, args);
1750	if (status)
1751		goto out;
1752
1753	/* set up reply kvec
1754	 *    toplevel status + taglen=0 + rescount + OP_PUTFH + status
1755	 *       + OP_READ + status + eof + datalen = 9
1756	 */
1757	replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1758	xdr_inline_pages(&req->rq_rcv_buf, replen,
1759			 args->pages, args->pgbase, args->count);
1760out:
1761	return status;
1762}
1763
1764/*
1765 * Encode an SETATTR request
1766 */
1767static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, __be32 *p, struct nfs_setattrargs *args)
1768
1769{
1770        struct xdr_stream xdr;
1771        struct compound_hdr hdr = {
1772                .nops   = 3,
1773        };
1774        int status;
1775
1776        xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1777        encode_compound_hdr(&xdr, &hdr);
1778        status = encode_putfh(&xdr, args->fh);
1779        if(status)
1780                goto out;
1781        status = encode_setattr(&xdr, args, args->server);
1782        if(status)
1783                goto out;
1784	status = encode_getfattr(&xdr, args->bitmask);
1785out:
1786        return status;
1787}
1788
1789/*
1790 * Encode a GETACL request
1791 */
1792static int
1793nfs4_xdr_enc_getacl(struct rpc_rqst *req, __be32 *p,
1794		struct nfs_getaclargs *args)
1795{
1796	struct xdr_stream xdr;
1797	struct rpc_auth *auth = req->rq_task->tk_auth;
1798	struct compound_hdr hdr = {
1799		.nops   = 2,
1800	};
1801	int replen, status;
1802
1803	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1804	encode_compound_hdr(&xdr, &hdr);
1805	status = encode_putfh(&xdr, args->fh);
1806	if (status)
1807		goto out;
1808	status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1809	/* set up reply buffer: */
1810	replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1811	xdr_inline_pages(&req->rq_rcv_buf, replen,
1812		args->acl_pages, args->acl_pgbase, args->acl_len);
1813out:
1814	return status;
1815}
1816
1817/*
1818 * Encode a WRITE request
1819 */
1820static int nfs4_xdr_enc_write(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
1821{
1822	struct xdr_stream xdr;
1823	struct compound_hdr hdr = {
1824		.nops = 3,
1825	};
1826	int status;
1827
1828	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1829	encode_compound_hdr(&xdr, &hdr);
1830	status = encode_putfh(&xdr, args->fh);
1831	if (status)
1832		goto out;
1833	status = encode_write(&xdr, args);
1834	if (status)
1835		goto out;
1836	status = encode_getfattr(&xdr, args->bitmask);
1837out:
1838	return status;
1839}
1840
1841/*
1842 *  a COMMIT request
1843 */
1844static int nfs4_xdr_enc_commit(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
1845{
1846	struct xdr_stream xdr;
1847	struct compound_hdr hdr = {
1848		.nops = 3,
1849	};
1850	int status;
1851
1852	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1853	encode_compound_hdr(&xdr, &hdr);
1854	status = encode_putfh(&xdr, args->fh);
1855	if (status)
1856		goto out;
1857	status = encode_commit(&xdr, args);
1858	if (status)
1859		goto out;
1860	status = encode_getfattr(&xdr, args->bitmask);
1861out:
1862	return status;
1863}
1864
1865/*
1866 * FSINFO request
1867 */
1868static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs4_fsinfo_arg *args)
1869{
1870	struct xdr_stream xdr;
1871	struct compound_hdr hdr = {
1872		.nops	= 2,
1873	};
1874	int status;
1875
1876	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1877	encode_compound_hdr(&xdr, &hdr);
1878	status = encode_putfh(&xdr, args->fh);
1879	if (!status)
1880		status = encode_fsinfo(&xdr, args->bitmask);
1881	return status;
1882}
1883
1884/*
1885 * a PATHCONF request
1886 */
1887static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, __be32 *p, const struct nfs4_pathconf_arg *args)
1888{
1889	struct xdr_stream xdr;
1890	struct compound_hdr hdr = {
1891		.nops = 2,
1892	};
1893	int status;
1894
1895	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1896	encode_compound_hdr(&xdr, &hdr);
1897	status = encode_putfh(&xdr, args->fh);
1898	if (!status)
1899		status = encode_getattr_one(&xdr,
1900				args->bitmask[0] & nfs4_pathconf_bitmap[0]);
1901	return status;
1902}
1903
1904/*
1905 * a STATFS request
1906 */
1907static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, __be32 *p, const struct nfs4_statfs_arg *args)
1908{
1909	struct xdr_stream xdr;
1910	struct compound_hdr hdr = {
1911		.nops = 2,
1912	};
1913	int status;
1914
1915	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1916	encode_compound_hdr(&xdr, &hdr);
1917	status = encode_putfh(&xdr, args->fh);
1918	if (status == 0)
1919		status = encode_getattr_two(&xdr,
1920				args->bitmask[0] & nfs4_statfs_bitmap[0],
1921				args->bitmask[1] & nfs4_statfs_bitmap[1]);
1922	return status;
1923}
1924
1925/*
1926 * GETATTR_BITMAP request
1927 */
1928static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, __be32 *p, const struct nfs_fh *fhandle)
1929{
1930	struct xdr_stream xdr;
1931	struct compound_hdr hdr = {
1932		.nops = 2,
1933	};
1934	int status;
1935
1936	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1937	encode_compound_hdr(&xdr, &hdr);
1938	status = encode_putfh(&xdr, fhandle);
1939	if (status == 0)
1940		status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
1941				FATTR4_WORD0_LINK_SUPPORT|
1942				FATTR4_WORD0_SYMLINK_SUPPORT|
1943				FATTR4_WORD0_ACLSUPPORT);
1944	return status;
1945}
1946
1947/*
1948 * a RENEW request
1949 */
1950static int nfs4_xdr_enc_renew(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
1951{
1952	struct xdr_stream xdr;
1953	struct compound_hdr hdr = {
1954		.nops	= 1,
1955	};
1956
1957	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1958	encode_compound_hdr(&xdr, &hdr);
1959	return encode_renew(&xdr, clp);
1960}
1961
1962/*
1963 * a SETCLIENTID request
1964 */
1965static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, __be32 *p, struct nfs4_setclientid *sc)
1966{
1967	struct xdr_stream xdr;
1968	struct compound_hdr hdr = {
1969		.nops	= 1,
1970	};
1971
1972	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1973	encode_compound_hdr(&xdr, &hdr);
1974	return encode_setclientid(&xdr, sc);
1975}
1976
1977/*
1978 * a SETCLIENTID_CONFIRM request
1979 */
1980static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
1981{
1982	struct xdr_stream xdr;
1983	struct compound_hdr hdr = {
1984		.nops	= 3,
1985	};
1986	const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
1987	int status;
1988
1989	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1990	encode_compound_hdr(&xdr, &hdr);
1991	status = encode_setclientid_confirm(&xdr, clp);
1992	if (!status)
1993		status = encode_putrootfh(&xdr);
1994	if (!status)
1995		status = encode_fsinfo(&xdr, lease_bitmap);
1996	return status;
1997}
1998
1999/*
2000 * DELEGRETURN request
2001 */
2002static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, __be32 *p, const struct nfs4_delegreturnargs *args)
2003{
2004	struct xdr_stream xdr;
2005	struct compound_hdr hdr = {
2006		.nops = 3,
2007	};
2008	int status;
2009
2010	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2011	encode_compound_hdr(&xdr, &hdr);
2012	status = encode_putfh(&xdr, args->fhandle);
2013	if (status != 0)
2014		goto out;
2015	status = encode_delegreturn(&xdr, args->stateid);
2016	if (status != 0)
2017		goto out;
2018	status = encode_getfattr(&xdr, args->bitmask);
2019out:
2020	return status;
2021}
2022
2023/*
2024 * Encode FS_LOCATIONS request
2025 */
2026static int nfs4_xdr_enc_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations_arg *args)
2027{
2028	struct xdr_stream xdr;
2029	struct compound_hdr hdr = {
2030		.nops = 3,
2031	};
2032	struct rpc_auth *auth = req->rq_task->tk_auth;
2033	int replen;
2034	int status;
2035
2036	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2037	encode_compound_hdr(&xdr, &hdr);
2038	if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
2039		goto out;
2040	if ((status = encode_lookup(&xdr, args->name)) != 0)
2041		goto out;
2042	if ((status = encode_fs_locations(&xdr, args->bitmask)) != 0)
2043		goto out;
2044	/* set up reply
2045	 *   toplevel_status + OP_PUTFH + status
2046	 *   + OP_LOOKUP + status + OP_GETATTR + status = 7
2047	 */
2048	replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
2049	xdr_inline_pages(&req->rq_rcv_buf, replen, &args->page,
2050			0, PAGE_SIZE);
2051out:
2052	return status;
2053}
2054
2055/*
2056 * START OF "GENERIC" DECODE ROUTINES.
2057 *   These may look a little ugly since they are imported from a "generic"
2058 * set of XDR encode/decode routines which are intended to be shared by
2059 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2060 *
2061 * If the pain of reading these is too great, it should be a straightforward
2062 * task to translate them into Linux-specific versions which are more
2063 * consistent with the style used in NFSv2/v3...
2064 */
2065#define READ32(x)         (x) = ntohl(*p++)
2066#define READ64(x)         do {			\
2067	(x) = (u64)ntohl(*p++) << 32;		\
2068	(x) |= ntohl(*p++);			\
2069} while (0)
2070#define READTIME(x)       do {			\
2071	p++;					\
2072	(x.tv_sec) = ntohl(*p++);		\
2073	(x.tv_nsec) = ntohl(*p++);		\
2074} while (0)
2075#define COPYMEM(x,nbytes) do {			\
2076	memcpy((x), p, nbytes);			\
2077	p += XDR_QUADLEN(nbytes);		\
2078} while (0)
2079
2080#define READ_BUF(nbytes)  do { \
2081	p = xdr_inline_decode(xdr, nbytes); \
2082	if (unlikely(!p)) { \
2083		printk(KERN_INFO "%s: prematurely hit end of receive" \
2084				" buffer\n", __FUNCTION__); \
2085		printk(KERN_INFO "%s: xdr->p=%p, bytes=%u, xdr->end=%p\n", \
2086				__FUNCTION__, xdr->p, nbytes, xdr->end); \
2087		return -EIO; \
2088	} \
2089} while (0)
2090
2091static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
2092{
2093	__be32 *p;
2094
2095	READ_BUF(4);
2096	READ32(*len);
2097	READ_BUF(*len);
2098	*string = (char *)p;
2099	return 0;
2100}
2101
2102static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2103{
2104	__be32 *p;
2105
2106	READ_BUF(8);
2107	READ32(hdr->status);
2108	READ32(hdr->taglen);
2109
2110	READ_BUF(hdr->taglen + 4);
2111	hdr->tag = (char *)p;
2112	p += XDR_QUADLEN(hdr->taglen);
2113	READ32(hdr->nops);
2114	return 0;
2115}
2116
2117static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2118{
2119	__be32 *p;
2120	uint32_t opnum;
2121	int32_t nfserr;
2122
2123	READ_BUF(8);
2124	READ32(opnum);
2125	if (opnum != expected) {
2126		printk(KERN_NOTICE
2127				"nfs4_decode_op_hdr: Server returned operation"
2128			       	" %d but we issued a request for %d\n",
2129				opnum, expected);
2130		return -EIO;
2131	}
2132	READ32(nfserr);
2133	if (nfserr != NFS_OK)
2134		return -nfs4_stat_to_errno(nfserr);
2135	return 0;
2136}
2137
2138/* Dummy routine */
2139static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
2140{
2141	__be32 *p;
2142	unsigned int strlen;
2143	char *str;
2144
2145	READ_BUF(12);
2146	return decode_opaque_inline(xdr, &strlen, &str);
2147}
2148
2149static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2150{
2151	uint32_t bmlen;
2152	__be32 *p;
2153
2154	READ_BUF(4);
2155	READ32(bmlen);
2156
2157	bitmap[0] = bitmap[1] = 0;
2158	READ_BUF((bmlen << 2));
2159	if (bmlen > 0) {
2160		READ32(bitmap[0]);
2161		if (bmlen > 1)
2162			READ32(bitmap[1]);
2163	}
2164	return 0;
2165}
2166
2167static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, __be32 **savep)
2168{
2169	__be32 *p;
2170
2171	READ_BUF(4);
2172	READ32(*attrlen);
2173	*savep = xdr->p;
2174	return 0;
2175}
2176
2177static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2178{
2179	if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2180		decode_attr_bitmap(xdr, bitmask);
2181		bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2182	} else
2183		bitmask[0] = bitmask[1] = 0;
2184	dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2185	return 0;
2186}
2187
2188static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2189{
2190	__be32 *p;
2191
2192	*type = 0;
2193	if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2194		return -EIO;
2195	if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2196		READ_BUF(4);
2197		READ32(*type);
2198		if (*type < NF4REG || *type > NF4NAMEDATTR) {
2199			dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2200			return -EIO;
2201		}
2202		bitmap[0] &= ~FATTR4_WORD0_TYPE;
2203	}
2204	dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2205	return 0;
2206}
2207
2208static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2209{
2210	__be32 *p;
2211
2212	*change = 0;
2213	if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2214		return -EIO;
2215	if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2216		READ_BUF(8);
2217		READ64(*change);
2218		bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2219	}
2220	dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2221			(unsigned long long)*change);
2222	return 0;
2223}
2224
2225static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2226{
2227	__be32 *p;
2228
2229	*size = 0;
2230	if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2231		return -EIO;
2232	if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2233		READ_BUF(8);
2234		READ64(*size);
2235		bitmap[0] &= ~FATTR4_WORD0_SIZE;
2236	}
2237	dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2238	return 0;
2239}
2240
2241static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2242{
2243	__be32 *p;
2244
2245	*res = 0;
2246	if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2247		return -EIO;
2248	if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2249		READ_BUF(4);
2250		READ32(*res);
2251		bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2252	}
2253	dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2254	return 0;
2255}
2256
2257static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2258{
2259	__be32 *p;
2260
2261	*res = 0;
2262	if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2263		return -EIO;
2264	if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2265		READ_BUF(4);
2266		READ32(*res);
2267		bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2268	}
2269	dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2270	return 0;
2271}
2272
2273static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
2274{
2275	__be32 *p;
2276
2277	fsid->major = 0;
2278	fsid->minor = 0;
2279	if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2280		return -EIO;
2281	if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2282		READ_BUF(16);
2283		READ64(fsid->major);
2284		READ64(fsid->minor);
2285		bitmap[0] &= ~FATTR4_WORD0_FSID;
2286	}
2287	dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2288			(unsigned long long)fsid->major,
2289			(unsigned long long)fsid->minor);
2290	return 0;
2291}
2292
2293static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2294{
2295	__be32 *p;
2296
2297	*res = 60;
2298	if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2299		return -EIO;
2300	if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2301		READ_BUF(4);
2302		READ32(*res);
2303		bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2304	}
2305	dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2306	return 0;
2307}
2308
2309static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2310{
2311	__be32 *p;
2312
2313	*res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2314	if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2315		return -EIO;
2316	if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2317		READ_BUF(4);
2318		READ32(*res);
2319		bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2320	}
2321	dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2322	return 0;
2323}
2324
2325static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2326{
2327	__be32 *p;
2328
2329	*fileid = 0;
2330	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2331		return -EIO;
2332	if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2333		READ_BUF(8);
2334		READ64(*fileid);
2335		bitmap[0] &= ~FATTR4_WORD0_FILEID;
2336	}
2337	dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2338	return 0;
2339}
2340
2341static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2342{
2343	__be32 *p;
2344
2345	*fileid = 0;
2346	if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
2347		return -EIO;
2348	if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
2349		READ_BUF(8);
2350		READ64(*fileid);
2351		bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
2352	}
2353	dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2354	return 0;
2355}
2356
2357static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2358{
2359	__be32 *p;
2360	int status = 0;
2361
2362	*res = 0;
2363	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2364		return -EIO;
2365	if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2366		READ_BUF(8);
2367		READ64(*res);
2368		bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2369	}
2370	dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2371	return status;
2372}
2373
2374static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2375{
2376	__be32 *p;
2377	int status = 0;
2378
2379	*res = 0;
2380	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2381		return -EIO;
2382	if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2383		READ_BUF(8);
2384		READ64(*res);
2385		bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2386	}
2387	dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2388	return status;
2389}
2390
2391static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2392{
2393	__be32 *p;
2394	int status = 0;
2395
2396	*res = 0;
2397	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2398		return -EIO;
2399	if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2400		READ_BUF(8);
2401		READ64(*res);
2402		bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2403	}
2404	dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2405	return status;
2406}
2407
2408static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
2409{
2410	int n;
2411	__be32 *p;
2412	int status = 0;
2413
2414	READ_BUF(4);
2415	READ32(n);
2416	if (n < 0)
2417		goto out_eio;
2418	if (n == 0)
2419		goto root_path;
2420	dprintk("path ");
2421	path->ncomponents = 0;
2422	while (path->ncomponents < n) {
2423		struct nfs4_string *component = &path->components[path->ncomponents];
2424		status = decode_opaque_inline(xdr, &component->len, &component->data);
2425		if (unlikely(status != 0))
2426			goto out_eio;
2427		if (path->ncomponents != n)
2428			dprintk("/");
2429		dprintk("%s", component->data);
2430		if (path->ncomponents < NFS4_PATHNAME_MAXCOMPONENTS)
2431			path->ncomponents++;
2432		else {
2433			dprintk("cannot parse %d components in path\n", n);
2434			goto out_eio;
2435		}
2436	}
2437out:
2438	dprintk("\n");
2439	return status;
2440root_path:
2441/* a root pathname is sent as a zero component4 */
2442	path->ncomponents = 1;
2443	path->components[0].len=0;
2444	path->components[0].data=NULL;
2445	dprintk("path /\n");
2446	goto out;
2447out_eio:
2448	dprintk(" status %d", status);
2449	status = -EIO;
2450	goto out;
2451}
2452
2453static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
2454{
2455	int n;
2456	__be32 *p;
2457	int status = -EIO;
2458
2459	if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
2460		goto out;
2461	status = 0;
2462	if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
2463		goto out;
2464	dprintk("%s: fsroot ", __FUNCTION__);
2465	status = decode_pathname(xdr, &res->fs_path);
2466	if (unlikely(status != 0))
2467		goto out;
2468	READ_BUF(4);
2469	READ32(n);
2470	if (n <= 0)
2471		goto out_eio;
2472	res->nlocations = 0;
2473	while (res->nlocations < n) {
2474		int m;
2475		struct nfs4_fs_location *loc = &res->locations[res->nlocations];
2476
2477		READ_BUF(4);
2478		READ32(m);
2479		if (m <= 0)
2480			goto out_eio;
2481
2482		loc->nservers = 0;
2483		dprintk("%s: servers ", __FUNCTION__);
2484		while (loc->nservers < m) {
2485			struct nfs4_string *server = &loc->servers[loc->nservers];
2486			status = decode_opaque_inline(xdr, &server->len, &server->data);
2487			if (unlikely(status != 0))
2488				goto out_eio;
2489			dprintk("%s ", server->data);
2490			if (loc->nservers < NFS4_FS_LOCATION_MAXSERVERS)
2491				loc->nservers++;
2492			else {
2493				int i;
2494				dprintk("%s: using first %d of %d servers returned for location %d\n", __FUNCTION__, NFS4_FS_LOCATION_MAXSERVERS, m, res->nlocations);
2495				for (i = loc->nservers; i < m; i++) {
2496					unsigned int len;
2497					char *data;
2498					status = decode_opaque_inline(xdr, &len, &data);
2499					if (unlikely(status != 0))
2500						goto out_eio;
2501				}
2502			}
2503		}
2504		status = decode_pathname(xdr, &loc->rootpath);
2505		if (unlikely(status != 0))
2506			goto out_eio;
2507		if (res->nlocations < NFS4_FS_LOCATIONS_MAXENTRIES)
2508			res->nlocations++;
2509	}
2510out:
2511	dprintk("%s: fs_locations done, error = %d\n", __FUNCTION__, status);
2512	return status;
2513out_eio:
2514	status = -EIO;
2515	goto out;
2516}
2517
2518static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2519{
2520	__be32 *p;
2521	int status = 0;
2522
2523	*res = 0;
2524	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2525		return -EIO;
2526	if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2527		READ_BUF(8);
2528		READ64(*res);
2529		bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2530	}
2531	dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2532	return status;
2533}
2534
2535static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2536{
2537	__be32 *p;
2538	int status = 0;
2539
2540	*maxlink = 1;
2541	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2542		return -EIO;
2543	if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2544		READ_BUF(4);
2545		READ32(*maxlink);
2546		bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2547	}
2548	dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2549	return status;
2550}
2551
2552static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2553{
2554	__be32 *p;
2555	int status = 0;
2556
2557	*maxname = 1024;
2558	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2559		return -EIO;
2560	if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2561		READ_BUF(4);
2562		READ32(*maxname);
2563		bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2564	}
2565	dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2566	return status;
2567}
2568
2569static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2570{
2571	__be32 *p;
2572	int status = 0;
2573
2574	*res = 1024;
2575	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2576		return -EIO;
2577	if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2578		uint64_t maxread;
2579		READ_BUF(8);
2580		READ64(maxread);
2581		if (maxread > 0x7FFFFFFF)
2582			maxread = 0x7FFFFFFF;
2583		*res = (uint32_t)maxread;
2584		bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2585	}
2586	dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2587	return status;
2588}
2589
2590static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2591{
2592	__be32 *p;
2593	int status = 0;
2594
2595	*res = 1024;
2596	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2597		return -EIO;
2598	if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2599		uint64_t maxwrite;
2600		READ_BUF(8);
2601		READ64(maxwrite);
2602		if (maxwrite > 0x7FFFFFFF)
2603			maxwrite = 0x7FFFFFFF;
2604		*res = (uint32_t)maxwrite;
2605		bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2606	}
2607	dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2608	return status;
2609}
2610
2611static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2612{
2613	__be32 *p;
2614
2615	*mode = 0;
2616	if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2617		return -EIO;
2618	if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2619		READ_BUF(4);
2620		READ32(*mode);
2621		*mode &= ~S_IFMT;
2622		bitmap[1] &= ~FATTR4_WORD1_MODE;
2623	}
2624	dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2625	return 0;
2626}
2627
2628static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2629{
2630	__be32 *p;
2631
2632	*nlink = 1;
2633	if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2634		return -EIO;
2635	if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2636		READ_BUF(4);
2637		READ32(*nlink);
2638		bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2639	}
2640	dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2641	return 0;
2642}
2643
2644static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, uint32_t *uid)
2645{
2646	uint32_t len;
2647	__be32 *p;
2648
2649	*uid = -2;
2650	if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2651		return -EIO;
2652	if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2653		READ_BUF(4);
2654		READ32(len);
2655		READ_BUF(len);
2656		if (len < XDR_MAX_NETOBJ) {
2657			if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2658				dprintk("%s: nfs_map_name_to_uid failed!\n",
2659						__FUNCTION__);
2660		} else
2661			printk(KERN_WARNING "%s: name too long (%u)!\n",
2662					__FUNCTION__, len);
2663		bitmap[1] &= ~FATTR4_WORD1_OWNER;
2664	}
2665	dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2666	return 0;
2667}
2668
2669static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, uint32_t *gid)
2670{
2671	uint32_t len;
2672	__be32 *p;
2673
2674	*gid = -2;
2675	if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2676		return -EIO;
2677	if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2678		READ_BUF(4);
2679		READ32(len);
2680		READ_BUF(len);
2681		if (len < XDR_MAX_NETOBJ) {
2682			if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2683				dprintk("%s: nfs_map_group_to_gid failed!\n",
2684						__FUNCTION__);
2685		} else
2686			printk(KERN_WARNING "%s: name too long (%u)!\n",
2687					__FUNCTION__, len);
2688		bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2689	}
2690	dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2691	return 0;
2692}
2693
2694static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2695{
2696	uint32_t major = 0, minor = 0;
2697	__be32 *p;
2698
2699	*rdev = MKDEV(0,0);
2700	if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2701		return -EIO;
2702	if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2703		dev_t tmp;
2704
2705		READ_BUF(8);
2706		READ32(major);
2707		READ32(minor);
2708		tmp = MKDEV(major, minor);
2709		if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2710			*rdev = tmp;
2711		bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2712	}
2713	dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2714	return 0;
2715}
2716
2717static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2718{
2719	__be32 *p;
2720	int status = 0;
2721
2722	*res = 0;
2723	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2724		return -EIO;
2725	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2726		READ_BUF(8);
2727		READ64(*res);
2728		bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2729	}
2730	dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2731	return status;
2732}
2733
2734static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2735{
2736	__be32 *p;
2737	int status = 0;
2738
2739	*res = 0;
2740	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2741		return -EIO;
2742	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2743		READ_BUF(8);
2744		READ64(*res);
2745		bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2746	}
2747	dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2748	return status;
2749}
2750
2751static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2752{
2753	__be32 *p;
2754	int status = 0;
2755
2756	*res = 0;
2757	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2758		return -EIO;
2759	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2760		READ_BUF(8);
2761		READ64(*res);
2762		bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2763	}
2764	dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2765	return status;
2766}
2767
2768static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2769{
2770	__be32 *p;
2771
2772	*used = 0;
2773	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2774		return -EIO;
2775	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2776		READ_BUF(8);
2777		READ64(*used);
2778		bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2779	}
2780	dprintk("%s: space used=%Lu\n", __FUNCTION__,
2781			(unsigned long long)*used);
2782	return 0;
2783}
2784
2785static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2786{
2787	__be32 *p;
2788	uint64_t sec;
2789	uint32_t nsec;
2790
2791	READ_BUF(12);
2792	READ64(sec);
2793	READ32(nsec);
2794	time->tv_sec = (time_t)sec;
2795	time->tv_nsec = (long)nsec;
2796	return 0;
2797}
2798
2799static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2800{
2801	int status = 0;
2802
2803	time->tv_sec = 0;
2804	time->tv_nsec = 0;
2805	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2806		return -EIO;
2807	if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2808		status = decode_attr_time(xdr, time);
2809		bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2810	}
2811	dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2812	return status;
2813}
2814
2815static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2816{
2817	int status = 0;
2818
2819	time->tv_sec = 0;
2820	time->tv_nsec = 0;
2821	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2822		return -EIO;
2823	if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2824		status = decode_attr_time(xdr, time);
2825		bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2826	}
2827	dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2828	return status;
2829}
2830
2831static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2832{
2833	int status = 0;
2834
2835	time->tv_sec = 0;
2836	time->tv_nsec = 0;
2837	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2838		return -EIO;
2839	if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2840		status = decode_attr_time(xdr, time);
2841		bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2842	}
2843	dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2844	return status;
2845}
2846
2847static int verify_attr_len(struct xdr_stream *xdr, __be32 *savep, uint32_t attrlen)
2848{
2849	unsigned int attrwords = XDR_QUADLEN(attrlen);
2850	unsigned int nwords = xdr->p - savep;
2851
2852	if (unlikely(attrwords != nwords)) {
2853		printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2854				__FUNCTION__,
2855				attrwords << 2,
2856				(attrwords < nwords) ? '<' : '>',
2857				nwords << 2);
2858		return -EIO;
2859	}
2860	return 0;
2861}
2862
2863static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2864{
2865	__be32 *p;
2866
2867	READ_BUF(20);
2868	READ32(cinfo->atomic);
2869	READ64(cinfo->before);
2870	READ64(cinfo->after);
2871	return 0;
2872}
2873
2874static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2875{
2876	__be32 *p;
2877	uint32_t supp, acc;
2878	int status;
2879
2880	status = decode_op_hdr(xdr, OP_ACCESS);
2881	if (status)
2882		return status;
2883	READ_BUF(8);
2884	READ32(supp);
2885	READ32(acc);
2886	access->supported = supp;
2887	access->access = acc;
2888	return 0;
2889}
2890
2891static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2892{
2893	__be32 *p;
2894	int status;
2895
2896	status = decode_op_hdr(xdr, OP_CLOSE);
2897	if (status)
2898		return status;
2899	READ_BUF(NFS4_STATEID_SIZE);
2900	COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
2901	return 0;
2902}
2903
2904static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
2905{
2906	__be32 *p;
2907	int status;
2908
2909	status = decode_op_hdr(xdr, OP_COMMIT);
2910	if (status)
2911		return status;
2912	READ_BUF(8);
2913	COPYMEM(res->verf->verifier, 8);
2914	return 0;
2915}
2916
2917static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2918{
2919	__be32 *p;
2920	uint32_t bmlen;
2921	int status;
2922
2923	status = decode_op_hdr(xdr, OP_CREATE);
2924	if (status)
2925		return status;
2926	if ((status = decode_change_info(xdr, cinfo)))
2927		return status;
2928	READ_BUF(4);
2929	READ32(bmlen);
2930	READ_BUF(bmlen << 2);
2931	return 0;
2932}
2933
2934static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
2935{
2936	__be32 *savep;
2937	uint32_t attrlen,
2938		 bitmap[2] = {0};
2939	int status;
2940
2941	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2942		goto xdr_error;
2943	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2944		goto xdr_error;
2945	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2946		goto xdr_error;
2947	if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
2948		goto xdr_error;
2949	if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
2950		goto xdr_error;
2951	if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
2952		goto xdr_error;
2953	if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
2954		goto xdr_error;
2955	status = verify_attr_len(xdr, savep, attrlen);
2956xdr_error:
2957	dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2958	return status;
2959}
2960
2961static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
2962{
2963	__be32 *savep;
2964	uint32_t attrlen,
2965		 bitmap[2] = {0};
2966	int status;
2967
2968	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2969		goto xdr_error;
2970	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2971		goto xdr_error;
2972	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2973		goto xdr_error;
2974
2975	if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
2976		goto xdr_error;
2977	if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
2978		goto xdr_error;
2979	if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
2980		goto xdr_error;
2981	if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
2982		goto xdr_error;
2983	if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
2984		goto xdr_error;
2985	if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
2986		goto xdr_error;
2987
2988	status = verify_attr_len(xdr, savep, attrlen);
2989xdr_error:
2990	dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2991	return status;
2992}
2993
2994static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
2995{
2996	__be32 *savep;
2997	uint32_t attrlen,
2998		 bitmap[2] = {0};
2999	int status;
3000
3001	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3002		goto xdr_error;
3003	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3004		goto xdr_error;
3005	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3006		goto xdr_error;
3007
3008	if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
3009		goto xdr_error;
3010	if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
3011		goto xdr_error;
3012
3013	status = verify_attr_len(xdr, savep, attrlen);
3014xdr_error:
3015	dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
3016	return status;
3017}
3018
3019static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
3020{
3021	__be32 *savep;
3022	uint32_t attrlen,
3023		 bitmap[2] = {0},
3024		 type;
3025	int status, fmode = 0;
3026	uint64_t fileid;
3027
3028	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3029		goto xdr_error;
3030	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3031		goto xdr_error;
3032
3033	fattr->bitmap[0] = bitmap[0];
3034	fattr->bitmap[1] = bitmap[1];
3035
3036	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3037		goto xdr_error;
3038
3039
3040	if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
3041		goto xdr_error;
3042	fattr->type = nfs_type2fmt[type].nfs2type;
3043	fmode = nfs_type2fmt[type].mode;
3044
3045	if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
3046		goto xdr_error;
3047	if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
3048		goto xdr_error;
3049	if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid)) != 0)
3050		goto xdr_error;
3051	if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
3052		goto xdr_error;
3053	if ((status = decode_attr_fs_locations(xdr, bitmap, container_of(fattr,
3054						struct nfs4_fs_locations,
3055						fattr))) != 0)
3056		goto xdr_error;
3057	if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
3058		goto xdr_error;
3059	fattr->mode |= fmode;
3060	if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
3061		goto xdr_error;
3062	if ((status = decode_attr_owner(xdr, bitmap, server->nfs_client, &fattr->uid)) != 0)
3063		goto xdr_error;
3064	if ((status = decode_attr_group(xdr, bitmap, server->nfs_client, &fattr->gid)) != 0)
3065		goto xdr_error;
3066	if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
3067		goto xdr_error;
3068	if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
3069		goto xdr_error;
3070	if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
3071		goto xdr_error;
3072	if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
3073		goto xdr_error;
3074	if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
3075		goto xdr_error;
3076	if ((status = decode_attr_mounted_on_fileid(xdr, bitmap, &fileid)) != 0)
3077		goto xdr_error;
3078	if (fattr->fileid == 0 && fileid != 0)
3079		fattr->fileid = fileid;
3080	if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
3081		fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
3082xdr_error:
3083	dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
3084	return status;
3085}
3086
3087
3088static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
3089{
3090	__be32 *savep;
3091	uint32_t attrlen, bitmap[2];
3092	int status;
3093
3094	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3095		goto xdr_error;
3096	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3097		goto xdr_error;
3098	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3099		goto xdr_error;
3100
3101	fsinfo->rtmult = fsinfo->wtmult = 512;	/* ??? */
3102
3103	if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
3104		goto xdr_error;
3105	if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
3106		goto xdr_error;
3107	if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
3108		goto xdr_error;
3109	fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
3110	if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
3111		goto xdr_error;
3112	fsinfo->wtpref = fsinfo->wtmax;
3113
3114	status = verify_attr_len(xdr, savep, attrlen);
3115xdr_error:
3116	dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
3117	return status;
3118}
3119
3120static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
3121{
3122	__be32 *p;
3123	uint32_t len;
3124	int status;
3125
3126	status = decode_op_hdr(xdr, OP_GETFH);
3127	if (status)
3128		return status;
3129	/* Zero handle first to allow comparisons */
3130	memset(fh, 0, sizeof(*fh));
3131
3132	READ_BUF(4);
3133	READ32(len);
3134	if (len > NFS4_FHSIZE)
3135		return -EIO;
3136	fh->size = len;
3137	READ_BUF(len);
3138	COPYMEM(fh->data, len);
3139	return 0;
3140}
3141
3142static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3143{
3144	int status;
3145
3146	status = decode_op_hdr(xdr, OP_LINK);
3147	if (status)
3148		return status;
3149	return decode_change_info(xdr, cinfo);
3150}
3151
3152/*
3153 * We create the owner, so we know a proper owner.id length is 4.
3154 */
3155static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
3156{
3157	uint64_t offset, length, clientid;
3158	__be32 *p;
3159	uint32_t namelen, type;
3160
3161	READ_BUF(32);
3162	READ64(offset);
3163	READ64(length);
3164	READ32(type);
3165	if (fl != NULL) {
3166		fl->fl_start = (loff_t)offset;
3167		fl->fl_end = fl->fl_start + (loff_t)length - 1;
3168		if (length == ~(uint64_t)0)
3169			fl->fl_end = OFFSET_MAX;
3170		fl->fl_type = F_WRLCK;
3171		if (type & 1)
3172			fl->fl_type = F_RDLCK;
3173		fl->fl_pid = 0;
3174	}
3175	READ64(clientid);
3176	READ32(namelen);
3177	READ_BUF(namelen);
3178	return -NFS4ERR_DENIED;
3179}
3180
3181static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
3182{
3183	__be32 *p;
3184	int status;
3185
3186	status = decode_op_hdr(xdr, OP_LOCK);
3187	if (status == 0) {
3188		READ_BUF(NFS4_STATEID_SIZE);
3189		COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
3190	} else if (status == -NFS4ERR_DENIED)
3191		return decode_lock_denied(xdr, NULL);
3192	return status;
3193}
3194
3195static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
3196{
3197	int status;
3198	status = decode_op_hdr(xdr, OP_LOCKT);
3199	if (status == -NFS4ERR_DENIED)
3200		return decode_lock_denied(xdr, res->denied);
3201	return status;
3202}
3203
3204static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
3205{
3206	__be32 *p;
3207	int status;
3208
3209	status = decode_op_hdr(xdr, OP_LOCKU);
3210	if (status == 0) {
3211		READ_BUF(NFS4_STATEID_SIZE);
3212		COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
3213	}
3214	return status;
3215}
3216
3217static int decode_lookup(struct xdr_stream *xdr)
3218{
3219	return decode_op_hdr(xdr, OP_LOOKUP);
3220}
3221
3222/* This is too sick! */
3223static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3224{
3225        __be32 *p;
3226	uint32_t limit_type, nblocks, blocksize;
3227
3228	READ_BUF(12);
3229	READ32(limit_type);
3230	switch (limit_type) {
3231		case 1:
3232			READ64(*maxsize);
3233			break;
3234		case 2:
3235			READ32(nblocks);
3236			READ32(blocksize);
3237			*maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
3238	}
3239	return 0;
3240}
3241
3242static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3243{
3244        __be32 *p;
3245        uint32_t delegation_type;
3246
3247	READ_BUF(4);
3248	READ32(delegation_type);
3249	if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3250		res->delegation_type = 0;
3251		return 0;
3252	}
3253	READ_BUF(NFS4_STATEID_SIZE+4);
3254	COPYMEM(res->delegation.data, NFS4_STATEID_SIZE);
3255	READ32(res->do_recall);
3256	switch (delegation_type) {
3257		case NFS4_OPEN_DELEGATE_READ:
3258			res->delegation_type = FMODE_READ;
3259			break;
3260		case NFS4_OPEN_DELEGATE_WRITE:
3261			res->delegation_type = FMODE_WRITE|FMODE_READ;
3262			if (decode_space_limit(xdr, &res->maxsize) < 0)
3263				return -EIO;
3264	}
3265	return decode_ace(xdr, NULL, res->server->nfs_client);
3266}
3267
3268static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3269{
3270        __be32 *p;
3271        uint32_t bmlen;
3272        int status;
3273
3274        status = decode_op_hdr(xdr, OP_OPEN);
3275        if (status)
3276                return status;
3277        READ_BUF(NFS4_STATEID_SIZE);
3278        COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
3279
3280        decode_change_info(xdr, &res->cinfo);
3281
3282        READ_BUF(8);
3283        READ32(res->rflags);
3284        READ32(bmlen);
3285        if (bmlen > 10)
3286                goto xdr_error;
3287
3288        READ_BUF(bmlen << 2);
3289        p += bmlen;
3290	return decode_delegation(xdr, res);
3291xdr_error:
3292	dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
3293	return -EIO;
3294}
3295
3296static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3297{
3298        __be32 *p;
3299	int status;
3300
3301        status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3302        if (status)
3303                return status;
3304        READ_BUF(NFS4_STATEID_SIZE);
3305        COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
3306        return 0;
3307}
3308
3309static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3310{
3311	__be32 *p;
3312	int status;
3313
3314	status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3315	if (status)
3316		return status;
3317	READ_BUF(NFS4_STATEID_SIZE);
3318	COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
3319	return 0;
3320}
3321
3322static int decode_putfh(struct xdr_stream *xdr)
3323{
3324	return decode_op_hdr(xdr, OP_PUTFH);
3325}
3326
3327static int decode_putrootfh(struct xdr_stream *xdr)
3328{
3329	return decode_op_hdr(xdr, OP_PUTROOTFH);
3330}
3331
3332static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3333{
3334	struct kvec *iov = req->rq_rcv_buf.head;
3335	__be32 *p;
3336	uint32_t count, eof, recvd, hdrlen;
3337	int status;
3338
3339	status = decode_op_hdr(xdr, OP_READ);
3340	if (status)
3341		return status;
3342	READ_BUF(8);
3343	READ32(eof);
3344	READ32(count);
3345	hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3346	recvd = req->rq_rcv_buf.len - hdrlen;
3347	if (count > recvd) {
3348		printk(KERN_WARNING "NFS: server cheating in read reply: "
3349				"count %u > recvd %u\n", count, recvd);
3350		count = recvd;
3351		eof = 0;
3352	}
3353	xdr_read_pages(xdr, count);
3354	res->eof = eof;
3355	res->count = count;
3356	return 0;
3357}
3358
3359static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3360{
3361	struct xdr_buf	*rcvbuf = &req->rq_rcv_buf;
3362	struct page	*page = *rcvbuf->pages;
3363	struct kvec	*iov = rcvbuf->head;
3364	unsigned int	nr, pglen = rcvbuf->page_len;
3365	__be32		*end, *entry, *p, *kaddr;
3366	uint32_t	len, attrlen, xlen;
3367	int 		hdrlen, recvd, status;
3368
3369	status = decode_op_hdr(xdr, OP_READDIR);
3370	if (status)
3371		return status;
3372	READ_BUF(8);
3373	COPYMEM(readdir->verifier.data, 8);
3374	dprintk("%s: verifier = 0x%x%x\n",
3375			__FUNCTION__,
3376			((u32 *)readdir->verifier.data)[0],
3377			((u32 *)readdir->verifier.data)[1]);
3378
3379
3380	hdrlen = (char *) p - (char *) iov->iov_base;
3381	recvd = rcvbuf->len - hdrlen;
3382	if (pglen > recvd)
3383		pglen = recvd;
3384	xdr_read_pages(xdr, pglen);
3385
3386	BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
3387	kaddr = p = kmap_atomic(page, KM_USER0);
3388	end = p + ((pglen + readdir->pgbase) >> 2);
3389	entry = p;
3390	for (nr = 0; *p++; nr++) {
3391		if (end - p < 3)
3392			goto short_pkt;
3393		dprintk("cookie = %Lu, ", *((unsigned long long *)p));
3394		p += 2;			/* cookie */
3395		len = ntohl(*p++);	/* filename length */
3396		if (len > NFS4_MAXNAMLEN) {
3397			printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
3398			goto err_unmap;
3399		}
3400		xlen = XDR_QUADLEN(len);
3401		if (end - p < xlen + 1)
3402			goto short_pkt;
3403		dprintk("filename = %*s\n", len, (char *)p);
3404		p += xlen;
3405		len = ntohl(*p++);	/* bitmap length */
3406		if (end - p < len + 1)
3407			goto short_pkt;
3408		p += len;
3409		attrlen = XDR_QUADLEN(ntohl(*p++));
3410		if (end - p < attrlen + 2)
3411			goto short_pkt;
3412		p += attrlen;		/* attributes */
3413		entry = p;
3414	}
3415	if (!nr && (entry[0] != 0 || entry[1] == 0))
3416		goto short_pkt;
3417out:
3418	kunmap_atomic(kaddr, KM_USER0);
3419	return 0;
3420short_pkt:
3421	dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
3422	entry[0] = entry[1] = 0;
3423	/* truncate listing ? */
3424	if (!nr) {
3425		printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
3426		entry[1] = 1;
3427	}
3428	goto out;
3429err_unmap:
3430	kunmap_atomic(kaddr, KM_USER0);
3431	return -errno_NFSERR_IO;
3432}
3433
3434static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3435{
3436	struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3437	struct kvec *iov = rcvbuf->head;
3438	int hdrlen, len, recvd;
3439	__be32 *p;
3440	char *kaddr;
3441	int status;
3442
3443	status = decode_op_hdr(xdr, OP_READLINK);
3444	if (status)
3445		return status;
3446
3447	/* Convert length of symlink */
3448	READ_BUF(4);
3449	READ32(len);
3450	if (len >= rcvbuf->page_len || len <= 0) {
3451		dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
3452		return -ENAMETOOLONG;
3453	}
3454	hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3455	recvd = req->rq_rcv_buf.len - hdrlen;
3456	if (recvd < len) {
3457		printk(KERN_WARNING "NFS: server cheating in readlink reply: "
3458				"count %u > recvd %u\n", len, recvd);
3459		return -EIO;
3460	}
3461	xdr_read_pages(xdr, len);
3462	/*
3463	 * The XDR encode routine has set things up so that
3464	 * the link text will be copied directly into the
3465	 * buffer.  We just have to do overflow-checking,
3466	 * and and null-terminate the text (the VFS expects
3467	 * null-termination).
3468	 */
3469	kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3470	kaddr[len+rcvbuf->page_base] = '\0';
3471	kunmap_atomic(kaddr, KM_USER0);
3472	return 0;
3473}
3474
3475static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3476{
3477	int status;
3478
3479	status = decode_op_hdr(xdr, OP_REMOVE);
3480	if (status)
3481		goto out;
3482	status = decode_change_info(xdr, cinfo);
3483out:
3484	return status;
3485}
3486
3487static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3488	      struct nfs4_change_info *new_cinfo)
3489{
3490	int status;
3491
3492	status = decode_op_hdr(xdr, OP_RENAME);
3493	if (status)
3494		goto out;
3495	if ((status = decode_change_info(xdr, old_cinfo)))
3496		goto out;
3497	status = decode_change_info(xdr, new_cinfo);
3498out:
3499	return status;
3500}
3501
3502static int decode_renew(struct xdr_stream *xdr)
3503{
3504	return decode_op_hdr(xdr, OP_RENEW);
3505}
3506
3507static int
3508decode_restorefh(struct xdr_stream *xdr)
3509{
3510	return decode_op_hdr(xdr, OP_RESTOREFH);
3511}
3512
3513static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3514		size_t *acl_len)
3515{
3516	__be32 *savep;
3517	uint32_t attrlen,
3518		 bitmap[2] = {0};
3519	struct kvec *iov = req->rq_rcv_buf.head;
3520	int status;
3521
3522	*acl_len = 0;
3523	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3524		goto out;
3525	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3526		goto out;
3527	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3528		goto out;
3529
3530	if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3531		return -EIO;
3532	if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
3533		int hdrlen, recvd;
3534
3535		/* We ignore &savep and don't do consistency checks on
3536		 * the attr length.  Let userspace figure it out.... */
3537		hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3538		recvd = req->rq_rcv_buf.len - hdrlen;
3539		if (attrlen > recvd) {
3540			printk(KERN_WARNING "NFS: server cheating in getattr"
3541					" acl reply: attrlen %u > recvd %u\n",
3542					attrlen, recvd);
3543			return -EINVAL;
3544		}
3545		xdr_read_pages(xdr, attrlen);
3546		*acl_len = attrlen;
3547	} else
3548		status = -EOPNOTSUPP;
3549
3550out:
3551	return status;
3552}
3553
3554static int
3555decode_savefh(struct xdr_stream *xdr)
3556{
3557	return decode_op_hdr(xdr, OP_SAVEFH);
3558}
3559
3560static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3561{
3562	__be32 *p;
3563	uint32_t bmlen;
3564	int status;
3565
3566
3567	status = decode_op_hdr(xdr, OP_SETATTR);
3568	if (status)
3569		return status;
3570	READ_BUF(4);
3571	READ32(bmlen);
3572	READ_BUF(bmlen << 2);
3573	return 0;
3574}
3575
3576static int decode_setclientid(struct xdr_stream *xdr, struct nfs_client *clp)
3577{
3578	__be32 *p;
3579	uint32_t opnum;
3580	int32_t nfserr;
3581
3582	READ_BUF(8);
3583	READ32(opnum);
3584	if (opnum != OP_SETCLIENTID) {
3585		printk(KERN_NOTICE
3586				"nfs4_decode_setclientid: Server returned operation"
3587			       	" %d\n", opnum);
3588		return -EIO;
3589	}
3590	READ32(nfserr);
3591	if (nfserr == NFS_OK) {
3592		READ_BUF(8 + NFS4_VERIFIER_SIZE);
3593		READ64(clp->cl_clientid);
3594		COPYMEM(clp->cl_confirm.data, NFS4_VERIFIER_SIZE);
3595	} else if (nfserr == NFSERR_CLID_INUSE) {
3596		uint32_t len;
3597
3598		/* skip netid string */
3599		READ_BUF(4);
3600		READ32(len);
3601		READ_BUF(len);
3602
3603		/* skip uaddr string */
3604		READ_BUF(4);
3605		READ32(len);
3606		READ_BUF(len);
3607		return -NFSERR_CLID_INUSE;
3608	} else
3609		return -nfs4_stat_to_errno(nfserr);
3610
3611	return 0;
3612}
3613
3614static int decode_setclientid_confirm(struct xdr_stream *xdr)
3615{
3616	return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3617}
3618
3619static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3620{
3621	__be32 *p;
3622	int status;
3623
3624	status = decode_op_hdr(xdr, OP_WRITE);
3625	if (status)
3626		return status;
3627
3628	READ_BUF(16);
3629	READ32(res->count);
3630	READ32(res->verf->committed);
3631	COPYMEM(res->verf->verifier, 8);
3632	return 0;
3633}
3634
3635static int decode_delegreturn(struct xdr_stream *xdr)
3636{
3637	return decode_op_hdr(xdr, OP_DELEGRETURN);
3638}
3639
3640/*
3641 * Decode OPEN_DOWNGRADE response
3642 */
3643static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
3644{
3645        struct xdr_stream xdr;
3646        struct compound_hdr hdr;
3647        int status;
3648
3649        xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3650        status = decode_compound_hdr(&xdr, &hdr);
3651        if (status)
3652                goto out;
3653        status = decode_putfh(&xdr);
3654        if (status)
3655                goto out;
3656        status = decode_open_downgrade(&xdr, res);
3657	if (status != 0)
3658		goto out;
3659	decode_getfattr(&xdr, res->fattr, res->server);
3660out:
3661        return status;
3662}
3663
3664/*
3665 * END OF "GENERIC" DECODE ROUTINES.
3666 */
3667
3668/*
3669 * Decode ACCESS response
3670 */
3671static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_accessres *res)
3672{
3673	struct xdr_stream xdr;
3674	struct compound_hdr hdr;
3675	int status;
3676
3677	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3678	if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3679		goto out;
3680	if ((status = decode_putfh(&xdr)) == 0)
3681		status = decode_access(&xdr, res);
3682out:
3683	return status;
3684}
3685
3686/*
3687 * Decode LOOKUP response
3688 */
3689static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
3690{
3691	struct xdr_stream xdr;
3692	struct compound_hdr hdr;
3693	int status;
3694
3695	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3696	if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3697		goto out;
3698	if ((status = decode_putfh(&xdr)) != 0)
3699		goto out;
3700	if ((status = decode_lookup(&xdr)) != 0)
3701		goto out;
3702	if ((status = decode_getfh(&xdr, res->fh)) != 0)
3703		goto out;
3704	status = decode_getfattr(&xdr, res->fattr, res->server);
3705out:
3706	return status;
3707}
3708
3709/*
3710 * Decode LOOKUP_ROOT response
3711 */
3712static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
3713{
3714	struct xdr_stream xdr;
3715	struct compound_hdr hdr;
3716	int status;
3717
3718	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3719	if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3720		goto out;
3721	if ((status = decode_putrootfh(&xdr)) != 0)
3722		goto out;
3723	if ((status = decode_getfh(&xdr, res->fh)) == 0)
3724		status = decode_getfattr(&xdr, res->fattr, res->server);
3725out:
3726	return status;
3727}
3728
3729/*
3730 * Decode REMOVE response
3731 */
3732static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_remove_res *res)
3733{
3734	struct xdr_stream xdr;
3735	struct compound_hdr hdr;
3736	int status;
3737
3738	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3739	if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3740		goto out;
3741	if ((status = decode_putfh(&xdr)) != 0)
3742		goto out;
3743	if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3744		goto out;
3745	decode_getfattr(&xdr, res->dir_attr, res->server);
3746out:
3747	return status;
3748}
3749
3750/*
3751 * Decode RENAME response
3752 */
3753static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_rename_res *res)
3754{
3755	struct xdr_stream xdr;
3756	struct compound_hdr hdr;
3757	int status;
3758
3759	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3760	if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3761		goto out;
3762	if ((status = decode_putfh(&xdr)) != 0)
3763		goto out;
3764	if ((status = decode_savefh(&xdr)) != 0)
3765		goto out;
3766	if ((status = decode_putfh(&xdr)) != 0)
3767		goto out;
3768	if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3769		goto out;
3770	/* Current FH is target directory */
3771	if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3772		goto out;
3773	if ((status = decode_restorefh(&xdr)) != 0)
3774		goto out;
3775	decode_getfattr(&xdr, res->old_fattr, res->server);
3776out:
3777	return status;
3778}
3779
3780/*
3781 * Decode LINK response
3782 */
3783static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_link_res *res)
3784{
3785	struct xdr_stream xdr;
3786	struct compound_hdr hdr;
3787	int status;
3788
3789	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3790	if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3791		goto out;
3792	if ((status = decode_putfh(&xdr)) != 0)
3793		goto out;
3794	if ((status = decode_savefh(&xdr)) != 0)
3795		goto out;
3796	if ((status = decode_putfh(&xdr)) != 0)
3797		goto out;
3798	if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3799		goto out;
3800	/*
3801	 * Note order: OP_LINK leaves the directory as the current
3802	 *             filehandle.
3803	 */
3804	if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3805		goto out;
3806	if ((status = decode_restorefh(&xdr)) != 0)
3807		goto out;
3808	decode_getfattr(&xdr, res->fattr, res->server);
3809out:
3810	return status;
3811}
3812
3813/*
3814 * Decode CREATE response
3815 */
3816static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
3817{
3818	struct xdr_stream xdr;
3819	struct compound_hdr hdr;
3820	int status;
3821
3822	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3823	if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3824		goto out;
3825	if ((status = decode_putfh(&xdr)) != 0)
3826		goto out;
3827	if ((status = decode_savefh(&xdr)) != 0)
3828		goto out;
3829	if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3830		goto out;
3831	if ((status = decode_getfh(&xdr, res->fh)) != 0)
3832		goto out;
3833	if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3834		goto out;
3835	if ((status = decode_restorefh(&xdr)) != 0)
3836		goto out;
3837	decode_getfattr(&xdr, res->dir_fattr, res->server);
3838out:
3839	return status;
3840}
3841
3842/*
3843 * Decode SYMLINK response
3844 */
3845static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
3846{
3847	return nfs4_xdr_dec_create(rqstp, p, res);
3848}
3849
3850/*
3851 * Decode GETATTR response
3852 */
3853static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_getattr_res *res)
3854{
3855	struct xdr_stream xdr;
3856	struct compound_hdr hdr;
3857	int status;
3858
3859	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3860	status = decode_compound_hdr(&xdr, &hdr);
3861	if (status)
3862		goto out;
3863	status = decode_putfh(&xdr);
3864	if (status)
3865		goto out;
3866	status = decode_getfattr(&xdr, res->fattr, res->server);
3867out:
3868	return status;
3869
3870}
3871
3872/*
3873 * Encode an SETACL request
3874 */
3875static int
3876nfs4_xdr_enc_setacl(struct rpc_rqst *req, __be32 *p, struct nfs_setaclargs *args)
3877{
3878        struct xdr_stream xdr;
3879        struct compound_hdr hdr = {
3880                .nops   = 2,
3881        };
3882        int status;
3883
3884        xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3885        encode_compound_hdr(&xdr, &hdr);
3886        status = encode_putfh(&xdr, args->fh);
3887        if (status)
3888                goto out;
3889        status = encode_setacl(&xdr, args);
3890out:
3891        return status;
3892}
3893/*
3894 * Decode SETACL response
3895 */
3896static int
3897nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, __be32 *p, void *res)
3898{
3899	struct xdr_stream xdr;
3900	struct compound_hdr hdr;
3901	int status;
3902
3903	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3904	status = decode_compound_hdr(&xdr, &hdr);
3905	if (status)
3906		goto out;
3907	status = decode_putfh(&xdr);
3908	if (status)
3909		goto out;
3910	status = decode_setattr(&xdr, res);
3911out:
3912	return status;
3913}
3914
3915/*
3916 * Decode GETACL response
3917 */
3918static int
3919nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, __be32 *p, size_t *acl_len)
3920{
3921	struct xdr_stream xdr;
3922	struct compound_hdr hdr;
3923	int status;
3924
3925	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3926	status = decode_compound_hdr(&xdr, &hdr);
3927	if (status)
3928		goto out;
3929	status = decode_putfh(&xdr);
3930	if (status)
3931		goto out;
3932	status = decode_getacl(&xdr, rqstp, acl_len);
3933
3934out:
3935	return status;
3936}
3937
3938/*
3939 * Decode CLOSE response
3940 */
3941static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
3942{
3943        struct xdr_stream xdr;
3944        struct compound_hdr hdr;
3945        int status;
3946
3947        xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3948        status = decode_compound_hdr(&xdr, &hdr);
3949        if (status)
3950                goto out;
3951        status = decode_putfh(&xdr);
3952        if (status)
3953                goto out;
3954        status = decode_close(&xdr, res);
3955	if (status != 0)
3956		goto out;
3957	/*
3958	 * Note: Server may do delete on close for this file
3959	 * 	in which case the getattr call will fail with
3960	 * 	an ESTALE error. Shouldn't be a problem,
3961	 * 	though, since fattr->valid will remain unset.
3962	 */
3963	decode_getfattr(&xdr, res->fattr, res->server);
3964out:
3965        return status;
3966}
3967
3968/*
3969 * Decode OPEN response
3970 */
3971static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
3972{
3973        struct xdr_stream xdr;
3974        struct compound_hdr hdr;
3975        int status;
3976
3977        xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3978        status = decode_compound_hdr(&xdr, &hdr);
3979        if (status)
3980                goto out;
3981        status = decode_putfh(&xdr);
3982        if (status)
3983                goto out;
3984        status = decode_savefh(&xdr);
3985	if (status)
3986		goto out;
3987        status = decode_open(&xdr, res);
3988        if (status)
3989                goto out;
3990	status = decode_getfh(&xdr, &res->fh);
3991        if (status)
3992		goto out;
3993	if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
3994		goto out;
3995	if ((status = decode_restorefh(&xdr)) != 0)
3996		goto out;
3997	decode_getfattr(&xdr, res->dir_attr, res->server);
3998out:
3999        return status;
4000}
4001
4002/*
4003 * Decode OPEN_CONFIRM response
4004 */
4005static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, __be32 *p, struct nfs_open_confirmres *res)
4006{
4007        struct xdr_stream xdr;
4008        struct compound_hdr hdr;
4009        int status;
4010
4011        xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4012        status = decode_compound_hdr(&xdr, &hdr);
4013        if (status)
4014                goto out;
4015        status = decode_putfh(&xdr);
4016        if (status)
4017                goto out;
4018        status = decode_open_confirm(&xdr, res);
4019out:
4020        return status;
4021}
4022
4023/*
4024 * Decode OPEN response
4025 */
4026static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
4027{
4028        struct xdr_stream xdr;
4029        struct compound_hdr hdr;
4030        int status;
4031
4032        xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4033        status = decode_compound_hdr(&xdr, &hdr);
4034        if (status)
4035                goto out;
4036        status = decode_putfh(&xdr);
4037        if (status)
4038                goto out;
4039        status = decode_open(&xdr, res);
4040        if (status)
4041                goto out;
4042	decode_getfattr(&xdr, res->f_attr, res->server);
4043out:
4044        return status;
4045}
4046
4047/*
4048 * Decode SETATTR response
4049 */
4050static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_setattrres *res)
4051{
4052        struct xdr_stream xdr;
4053        struct compound_hdr hdr;
4054        int status;
4055
4056        xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4057        status = decode_compound_hdr(&xdr, &hdr);
4058        if (status)
4059                goto out;
4060        status = decode_putfh(&xdr);
4061        if (status)
4062                goto out;
4063        status = decode_setattr(&xdr, res);
4064        if (status)
4065                goto out;
4066	status = decode_getfattr(&xdr, res->fattr, res->server);
4067	if (status == NFS4ERR_DELAY)
4068		status = 0;
4069out:
4070        return status;
4071}
4072
4073/*
4074 * Decode LOCK response
4075 */
4076static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lock_res *res)
4077{
4078	struct xdr_stream xdr;
4079	struct compound_hdr hdr;
4080	int status;
4081
4082	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4083	status = decode_compound_hdr(&xdr, &hdr);
4084	if (status)
4085		goto out;
4086	status = decode_putfh(&xdr);
4087	if (status)
4088		goto out;
4089	status = decode_lock(&xdr, res);
4090out:
4091	return status;
4092}
4093
4094/*
4095 * Decode LOCKT response
4096 */
4097static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lockt_res *res)
4098{
4099	struct xdr_stream xdr;
4100	struct compound_hdr hdr;
4101	int status;
4102
4103	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4104	status = decode_compound_hdr(&xdr, &hdr);
4105	if (status)
4106		goto out;
4107	status = decode_putfh(&xdr);
4108	if (status)
4109		goto out;
4110	status = decode_lockt(&xdr, res);
4111out:
4112	return status;
4113}
4114
4115/*
4116 * Decode LOCKU response
4117 */
4118static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, __be32 *p, struct nfs_locku_res *res)
4119{
4120	struct xdr_stream xdr;
4121	struct compound_hdr hdr;
4122	int status;
4123
4124	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4125	status = decode_compound_hdr(&xdr, &hdr);
4126	if (status)
4127		goto out;
4128	status = decode_putfh(&xdr);
4129	if (status)
4130		goto out;
4131	status = decode_locku(&xdr, res);
4132out:
4133	return status;
4134}
4135
4136/*
4137 * Decode READLINK response
4138 */
4139static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, __be32 *p, void *res)
4140{
4141	struct xdr_stream xdr;
4142	struct compound_hdr hdr;
4143	int status;
4144
4145	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4146	status = decode_compound_hdr(&xdr, &hdr);
4147	if (status)
4148		goto out;
4149	status = decode_putfh(&xdr);
4150	if (status)
4151		goto out;
4152	status = decode_readlink(&xdr, rqstp);
4153out:
4154	return status;
4155}
4156
4157/*
4158 * Decode READDIR response
4159 */
4160static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_readdir_res *res)
4161{
4162	struct xdr_stream xdr;
4163	struct compound_hdr hdr;
4164	int status;
4165
4166	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4167	status = decode_compound_hdr(&xdr, &hdr);
4168	if (status)
4169		goto out;
4170	status = decode_putfh(&xdr);
4171	if (status)
4172		goto out;
4173	status = decode_readdir(&xdr, rqstp, res);
4174out:
4175	return status;
4176}
4177
4178/*
4179 * Decode Read response
4180 */
4181static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, __be32 *p, struct nfs_readres *res)
4182{
4183	struct xdr_stream xdr;
4184	struct compound_hdr hdr;
4185	int status;
4186
4187	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4188	status = decode_compound_hdr(&xdr, &hdr);
4189	if (status)
4190		goto out;
4191	status = decode_putfh(&xdr);
4192	if (status)
4193		goto out;
4194	status = decode_read(&xdr, rqstp, res);
4195	if (!status)
4196		status = res->count;
4197out:
4198	return status;
4199}
4200
4201/*
4202 * Decode WRITE response
4203 */
4204static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
4205{
4206	struct xdr_stream xdr;
4207	struct compound_hdr hdr;
4208	int status;
4209
4210	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4211	status = decode_compound_hdr(&xdr, &hdr);
4212	if (status)
4213		goto out;
4214	status = decode_putfh(&xdr);
4215	if (status)
4216		goto out;
4217	status = decode_write(&xdr, res);
4218	if (status)
4219		goto out;
4220	decode_getfattr(&xdr, res->fattr, res->server);
4221	if (!status)
4222		status = res->count;
4223out:
4224	return status;
4225}
4226
4227/*
4228 * Decode COMMIT response
4229 */
4230static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
4231{
4232	struct xdr_stream xdr;
4233	struct compound_hdr hdr;
4234	int status;
4235
4236	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4237	status = decode_compound_hdr(&xdr, &hdr);
4238	if (status)
4239		goto out;
4240	status = decode_putfh(&xdr);
4241	if (status)
4242		goto out;
4243	status = decode_commit(&xdr, res);
4244	if (status)
4245		goto out;
4246	decode_getfattr(&xdr, res->fattr, res->server);
4247out:
4248	return status;
4249}
4250
4251/*
4252 * FSINFO request
4253 */
4254static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
4255{
4256	struct xdr_stream xdr;
4257	struct compound_hdr hdr;
4258	int status;
4259
4260	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4261	status = decode_compound_hdr(&xdr, &hdr);
4262	if (!status)
4263		status = decode_putfh(&xdr);
4264	if (!status)
4265		status = decode_fsinfo(&xdr, fsinfo);
4266	if (!status)
4267		status = -nfs4_stat_to_errno(hdr.status);
4268	return status;
4269}
4270
4271/*
4272 * PATHCONF request
4273 */
4274static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, __be32 *p, struct nfs_pathconf *pathconf)
4275{
4276	struct xdr_stream xdr;
4277	struct compound_hdr hdr;
4278	int status;
4279
4280	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4281	status = decode_compound_hdr(&xdr, &hdr);
4282	if (!status)
4283		status = decode_putfh(&xdr);
4284	if (!status)
4285		status = decode_pathconf(&xdr, pathconf);
4286	return status;
4287}
4288
4289/*
4290 * STATFS request
4291 */
4292static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, __be32 *p, struct nfs_fsstat *fsstat)
4293{
4294	struct xdr_stream xdr;
4295	struct compound_hdr hdr;
4296	int status;
4297
4298	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4299	status = decode_compound_hdr(&xdr, &hdr);
4300	if (!status)
4301		status = decode_putfh(&xdr);
4302	if (!status)
4303		status = decode_statfs(&xdr, fsstat);
4304	return status;
4305}
4306
4307/*
4308 * GETATTR_BITMAP request
4309 */
4310static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, __be32 *p, struct nfs4_server_caps_res *res)
4311{
4312	struct xdr_stream xdr;
4313	struct compound_hdr hdr;
4314	int status;
4315
4316	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4317	if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4318		goto out;
4319	if ((status = decode_putfh(&xdr)) != 0)
4320		goto out;
4321	status = decode_server_caps(&xdr, res);
4322out:
4323	return status;
4324}
4325
4326/*
4327 * Decode RENEW response
4328 */
4329static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, __be32 *p, void *dummy)
4330{
4331	struct xdr_stream xdr;
4332	struct compound_hdr hdr;
4333	int status;
4334
4335	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4336	status = decode_compound_hdr(&xdr, &hdr);
4337	if (!status)
4338		status = decode_renew(&xdr);
4339	return status;
4340}
4341
4342/*
4343 * a SETCLIENTID request
4344 */
4345static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, __be32 *p,
4346		struct nfs_client *clp)
4347{
4348	struct xdr_stream xdr;
4349	struct compound_hdr hdr;
4350	int status;
4351
4352	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4353	status = decode_compound_hdr(&xdr, &hdr);
4354	if (!status)
4355		status = decode_setclientid(&xdr, clp);
4356	if (!status)
4357		status = -nfs4_stat_to_errno(hdr.status);
4358	return status;
4359}
4360
4361/*
4362 * a SETCLIENTID_CONFIRM request
4363 */
4364static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
4365{
4366	struct xdr_stream xdr;
4367	struct compound_hdr hdr;
4368	int status;
4369
4370	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4371	status = decode_compound_hdr(&xdr, &hdr);
4372	if (!status)
4373		status = decode_setclientid_confirm(&xdr);
4374	if (!status)
4375		status = decode_putrootfh(&xdr);
4376	if (!status)
4377		status = decode_fsinfo(&xdr, fsinfo);
4378	if (!status)
4379		status = -nfs4_stat_to_errno(hdr.status);
4380	return status;
4381}
4382
4383/*
4384 * DELEGRETURN request
4385 */
4386static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_delegreturnres *res)
4387{
4388	struct xdr_stream xdr;
4389	struct compound_hdr hdr;
4390	int status;
4391
4392	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4393	status = decode_compound_hdr(&xdr, &hdr);
4394	if (status != 0)
4395		goto out;
4396	status = decode_putfh(&xdr);
4397	if (status != 0)
4398		goto out;
4399	status = decode_delegreturn(&xdr);
4400	decode_getfattr(&xdr, res->fattr, res->server);
4401out:
4402	return status;
4403}
4404
4405/*
4406 * FS_LOCATIONS request
4407 */
4408static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations *res)
4409{
4410	struct xdr_stream xdr;
4411	struct compound_hdr hdr;
4412	int status;
4413
4414	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4415	status = decode_compound_hdr(&xdr, &hdr);
4416	if (status != 0)
4417		goto out;
4418	if ((status = decode_putfh(&xdr)) != 0)
4419		goto out;
4420	if ((status = decode_lookup(&xdr)) != 0)
4421		goto out;
4422	xdr_enter_page(&xdr, PAGE_SIZE);
4423	status = decode_getfattr(&xdr, &res->fattr, res->server);
4424out:
4425	return status;
4426}
4427
4428__be32 *nfs4_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
4429{
4430	uint32_t bitmap[2] = {0};
4431	uint32_t len;
4432
4433	if (!*p++) {
4434		if (!*p)
4435			return ERR_PTR(-EAGAIN);
4436		entry->eof = 1;
4437		return ERR_PTR(-EBADCOOKIE);
4438	}
4439
4440	entry->prev_cookie = entry->cookie;
4441	p = xdr_decode_hyper(p, &entry->cookie);
4442	entry->len = ntohl(*p++);
4443	entry->name = (const char *) p;
4444	p += XDR_QUADLEN(entry->len);
4445
4446	/*
4447	 * In case the server doesn't return an inode number,
4448	 * we fake one here.  (We don't use inode number 0,
4449	 * since glibc seems to choke on it...)
4450	 */
4451	entry->ino = 1;
4452
4453	len = ntohl(*p++);		/* bitmap length */
4454	if (len-- > 0) {
4455		bitmap[0] = ntohl(*p++);
4456		if (len-- > 0) {
4457			bitmap[1] = ntohl(*p++);
4458			p += len;
4459		}
4460	}
4461	len = XDR_QUADLEN(ntohl(*p++));	/* attribute buffer length */
4462	if (len > 0) {
4463		if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4464			bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4465			/* Ignore the return value of rdattr_error for now */
4466			p++;
4467			len--;
4468		}
4469		if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4470			xdr_decode_hyper(p, &entry->ino);
4471		else if (bitmap[0] == FATTR4_WORD0_FILEID)
4472			xdr_decode_hyper(p, &entry->ino);
4473		p += len;
4474	}
4475
4476	entry->eof = !p[0] && p[1];
4477	return p;
4478}
4479
4480/*
4481 * We need to translate between nfs status return values and
4482 * the local errno values which may not be the same.
4483 */
4484static struct {
4485	int stat;
4486	int errno;
4487} nfs_errtbl[] = {
4488	{ NFS4_OK,		0		},
4489	{ NFS4ERR_PERM,		EPERM		},
4490	{ NFS4ERR_NOENT,	ENOENT		},
4491	{ NFS4ERR_IO,		errno_NFSERR_IO	},
4492	{ NFS4ERR_NXIO,		ENXIO		},
4493	{ NFS4ERR_ACCESS,	EACCES		},
4494	{ NFS4ERR_EXIST,	EEXIST		},
4495	{ NFS4ERR_XDEV,		EXDEV		},
4496	{ NFS4ERR_NOTDIR,	ENOTDIR		},
4497	{ NFS4ERR_ISDIR,	EISDIR		},
4498	{ NFS4ERR_INVAL,	EINVAL		},
4499	{ NFS4ERR_FBIG,		EFBIG		},
4500	{ NFS4ERR_NOSPC,	ENOSPC		},
4501	{ NFS4ERR_ROFS,		EROFS		},
4502	{ NFS4ERR_MLINK,	EMLINK		},
4503	{ NFS4ERR_NAMETOOLONG,	ENAMETOOLONG	},
4504	{ NFS4ERR_NOTEMPTY,	ENOTEMPTY	},
4505	{ NFS4ERR_DQUOT,	EDQUOT		},
4506	{ NFS4ERR_STALE,	ESTALE		},
4507	{ NFS4ERR_BADHANDLE,	EBADHANDLE	},
4508	{ NFS4ERR_BADOWNER,	EINVAL		},
4509	{ NFS4ERR_BADNAME,	EINVAL		},
4510	{ NFS4ERR_BAD_COOKIE,	EBADCOOKIE	},
4511	{ NFS4ERR_NOTSUPP,	ENOTSUPP	},
4512	{ NFS4ERR_TOOSMALL,	ETOOSMALL	},
4513	{ NFS4ERR_SERVERFAULT,	ESERVERFAULT	},
4514	{ NFS4ERR_BADTYPE,	EBADTYPE	},
4515	{ NFS4ERR_LOCKED,	EAGAIN		},
4516	{ NFS4ERR_RESOURCE,	EREMOTEIO	},
4517	{ NFS4ERR_SYMLINK,	ELOOP		},
4518	{ NFS4ERR_OP_ILLEGAL,	EOPNOTSUPP	},
4519	{ NFS4ERR_DEADLOCK,	EDEADLK		},
4520	{ NFS4ERR_WRONGSEC,	EPERM		},
4521	{ -1,			EIO		}
4522};
4523
4524/*
4525 * Convert an NFS error code to a local one.
4526 * This one is used jointly by NFSv2 and NFSv3.
4527 */
4528static int
4529nfs4_stat_to_errno(int stat)
4530{
4531	int i;
4532	for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4533		if (nfs_errtbl[i].stat == stat)
4534			return nfs_errtbl[i].errno;
4535	}
4536	if (stat <= 10000 || stat > 10100) {
4537		/* The server is looney tunes. */
4538		return ESERVERFAULT;
4539	}
4540	/* If we cannot translate the error, the recovery routines should
4541	 * handle it.
4542	 * Note: remaining NFSv4 error codes have values > 10000, so should
4543	 * not conflict with native Linux error codes.
4544	 */
4545	return stat;
4546}
4547
4548#define PROC(proc, argtype, restype)				\
4549[NFSPROC4_CLNT_##proc] = {					\
4550	.p_proc   = NFSPROC4_COMPOUND,				\
4551	.p_encode = (kxdrproc_t) nfs4_xdr_##argtype,		\
4552	.p_decode = (kxdrproc_t) nfs4_xdr_##restype,		\
4553	.p_arglen = NFS4_##argtype##_sz,			\
4554	.p_replen = NFS4_##restype##_sz,			\
4555	.p_statidx = NFSPROC4_CLNT_##proc,			\
4556	.p_name   = #proc,					\
4557    }
4558
4559struct rpc_procinfo	nfs4_procedures[] = {
4560  PROC(READ,		enc_read,	dec_read),
4561  PROC(WRITE,		enc_write,	dec_write),
4562  PROC(COMMIT,		enc_commit,	dec_commit),
4563  PROC(OPEN,		enc_open,	dec_open),
4564  PROC(OPEN_CONFIRM,	enc_open_confirm,	dec_open_confirm),
4565  PROC(OPEN_NOATTR,	enc_open_noattr,	dec_open_noattr),
4566  PROC(OPEN_DOWNGRADE,	enc_open_downgrade,	dec_open_downgrade),
4567  PROC(CLOSE,		enc_close,	dec_close),
4568  PROC(SETATTR,		enc_setattr,	dec_setattr),
4569  PROC(FSINFO,		enc_fsinfo,	dec_fsinfo),
4570  PROC(RENEW,		enc_renew,	dec_renew),
4571  PROC(SETCLIENTID,	enc_setclientid,	dec_setclientid),
4572  PROC(SETCLIENTID_CONFIRM,	enc_setclientid_confirm,	dec_setclientid_confirm),
4573  PROC(LOCK,            enc_lock,       dec_lock),
4574  PROC(LOCKT,           enc_lockt,      dec_lockt),
4575  PROC(LOCKU,           enc_locku,      dec_locku),
4576  PROC(ACCESS,		enc_access,	dec_access),
4577  PROC(GETATTR,		enc_getattr,	dec_getattr),
4578  PROC(LOOKUP,		enc_lookup,	dec_lookup),
4579  PROC(LOOKUP_ROOT,	enc_lookup_root,	dec_lookup_root),
4580  PROC(REMOVE,		enc_remove,	dec_remove),
4581  PROC(RENAME,		enc_rename,	dec_rename),
4582  PROC(LINK,		enc_link,	dec_link),
4583  PROC(SYMLINK,		enc_symlink,	dec_symlink),
4584  PROC(CREATE,		enc_create,	dec_create),
4585  PROC(PATHCONF,	enc_pathconf,	dec_pathconf),
4586  PROC(STATFS,		enc_statfs,	dec_statfs),
4587  PROC(READLINK,	enc_readlink,	dec_readlink),
4588  PROC(READDIR,		enc_readdir,	dec_readdir),
4589  PROC(SERVER_CAPS,	enc_server_caps, dec_server_caps),
4590  PROC(DELEGRETURN,	enc_delegreturn, dec_delegreturn),
4591  PROC(GETACL,		enc_getacl,	dec_getacl),
4592  PROC(SETACL,		enc_setacl,	dec_setacl),
4593  PROC(FS_LOCATIONS,	enc_fs_locations, dec_fs_locations),
4594};
4595
4596struct rpc_version		nfs_version4 = {
4597	.number			= 4,
4598	.nrprocs		= ARRAY_SIZE(nfs4_procedures),
4599	.procs			= nfs4_procedures
4600};
4601
4602/*
4603 * Local variables:
4604 *  c-basic-offset: 8
4605 * End:
4606 */
4607