xref: /openbmc/linux/fs/nfs/nfs4xdr.c (revision 6774def6)
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/errno.h>
42 #include <linux/string.h>
43 #include <linux/in.h>
44 #include <linux/pagemap.h>
45 #include <linux/proc_fs.h>
46 #include <linux/kdev_t.h>
47 #include <linux/module.h>
48 #include <linux/utsname.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/sunrpc/msg_prot.h>
51 #include <linux/sunrpc/gss_api.h>
52 #include <linux/nfs.h>
53 #include <linux/nfs4.h>
54 #include <linux/nfs_fs.h>
55 #include <linux/nfs_idmap.h>
56 
57 #include "nfs4_fs.h"
58 #include "internal.h"
59 #include "nfs4session.h"
60 #include "pnfs.h"
61 #include "netns.h"
62 
63 #define NFSDBG_FACILITY		NFSDBG_XDR
64 
65 /* Mapping from NFS error code to "errno" error code. */
66 #define errno_NFSERR_IO		EIO
67 
68 static int nfs4_stat_to_errno(int);
69 
70 /* NFSv4 COMPOUND tags are only wanted for debugging purposes */
71 #ifdef DEBUG
72 #define NFS4_MAXTAGLEN		20
73 #else
74 #define NFS4_MAXTAGLEN		0
75 #endif
76 
77 /* lock,open owner id:
78  * we currently use size 2 (u64) out of (NFS4_OPAQUE_LIMIT  >> 2)
79  */
80 #define open_owner_id_maxsz	(1 + 2 + 1 + 1 + 2)
81 #define lock_owner_id_maxsz	(1 + 1 + 4)
82 #define decode_lockowner_maxsz	(1 + XDR_QUADLEN(IDMAP_NAMESZ))
83 #define compound_encode_hdr_maxsz	(3 + (NFS4_MAXTAGLEN >> 2))
84 #define compound_decode_hdr_maxsz	(3 + (NFS4_MAXTAGLEN >> 2))
85 #define op_encode_hdr_maxsz	(1)
86 #define op_decode_hdr_maxsz	(2)
87 #define encode_stateid_maxsz	(XDR_QUADLEN(NFS4_STATEID_SIZE))
88 #define decode_stateid_maxsz	(XDR_QUADLEN(NFS4_STATEID_SIZE))
89 #define encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
90 #define decode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
91 #define encode_putfh_maxsz	(op_encode_hdr_maxsz + 1 + \
92 				(NFS4_FHSIZE >> 2))
93 #define decode_putfh_maxsz	(op_decode_hdr_maxsz)
94 #define encode_putrootfh_maxsz	(op_encode_hdr_maxsz)
95 #define decode_putrootfh_maxsz	(op_decode_hdr_maxsz)
96 #define encode_getfh_maxsz      (op_encode_hdr_maxsz)
97 #define decode_getfh_maxsz      (op_decode_hdr_maxsz + 1 + \
98 				((3+NFS4_FHSIZE) >> 2))
99 #define nfs4_fattr_bitmap_maxsz 4
100 #define encode_getattr_maxsz    (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
101 #define nfs4_name_maxsz		(1 + ((3 + NFS4_MAXNAMLEN) >> 2))
102 #define nfs4_path_maxsz		(1 + ((3 + NFS4_MAXPATHLEN) >> 2))
103 #define nfs4_owner_maxsz	(1 + XDR_QUADLEN(IDMAP_NAMESZ))
104 #define nfs4_group_maxsz	(1 + XDR_QUADLEN(IDMAP_NAMESZ))
105 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
106 /* PI(4 bytes) + LFS(4 bytes) + 1(for null terminator?) + MAXLABELLEN */
107 #define	nfs4_label_maxsz	(4 + 4 + 1 + XDR_QUADLEN(NFS4_MAXLABELLEN))
108 #else
109 #define	nfs4_label_maxsz	0
110 #endif
111 /* We support only one layout type per file system */
112 #define decode_mdsthreshold_maxsz (1 + 1 + nfs4_fattr_bitmap_maxsz + 1 + 8)
113 /* This is based on getfattr, which uses the most attributes: */
114 #define nfs4_fattr_value_maxsz	(1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
115 				3 + 3 + 3 + nfs4_owner_maxsz + \
116 				nfs4_group_maxsz + nfs4_label_maxsz + \
117 				 decode_mdsthreshold_maxsz))
118 #define nfs4_fattr_maxsz	(nfs4_fattr_bitmap_maxsz + \
119 				nfs4_fattr_value_maxsz)
120 #define decode_getattr_maxsz    (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
121 #define encode_attrs_maxsz	(nfs4_fattr_bitmap_maxsz + \
122 				 1 + 2 + 1 + \
123 				nfs4_owner_maxsz + \
124 				nfs4_group_maxsz + \
125 				nfs4_label_maxsz + \
126 				4 + 4)
127 #define encode_savefh_maxsz     (op_encode_hdr_maxsz)
128 #define decode_savefh_maxsz     (op_decode_hdr_maxsz)
129 #define encode_restorefh_maxsz  (op_encode_hdr_maxsz)
130 #define decode_restorefh_maxsz  (op_decode_hdr_maxsz)
131 #define encode_fsinfo_maxsz	(encode_getattr_maxsz)
132 /* The 5 accounts for the PNFS attributes, and assumes that at most three
133  * layout types will be returned.
134  */
135 #define decode_fsinfo_maxsz	(op_decode_hdr_maxsz + \
136 				 nfs4_fattr_bitmap_maxsz + 4 + 8 + 5)
137 #define encode_renew_maxsz	(op_encode_hdr_maxsz + 3)
138 #define decode_renew_maxsz	(op_decode_hdr_maxsz)
139 #define encode_setclientid_maxsz \
140 				(op_encode_hdr_maxsz + \
141 				XDR_QUADLEN(NFS4_VERIFIER_SIZE) + \
142 				XDR_QUADLEN(NFS4_SETCLIENTID_NAMELEN) + \
143 				1 /* sc_prog */ + \
144 				XDR_QUADLEN(RPCBIND_MAXNETIDLEN) + \
145 				XDR_QUADLEN(RPCBIND_MAXUADDRLEN) + \
146 				1) /* sc_cb_ident */
147 #define decode_setclientid_maxsz \
148 				(op_decode_hdr_maxsz + \
149 				2 + \
150 				1024) /* large value for CLID_INUSE */
151 #define encode_setclientid_confirm_maxsz \
152 				(op_encode_hdr_maxsz + \
153 				3 + (NFS4_VERIFIER_SIZE >> 2))
154 #define decode_setclientid_confirm_maxsz \
155 				(op_decode_hdr_maxsz)
156 #define encode_lookup_maxsz	(op_encode_hdr_maxsz + nfs4_name_maxsz)
157 #define decode_lookup_maxsz	(op_decode_hdr_maxsz)
158 #define encode_share_access_maxsz \
159 				(2)
160 #define encode_createmode_maxsz	(1 + encode_attrs_maxsz + encode_verifier_maxsz)
161 #define encode_opentype_maxsz	(1 + encode_createmode_maxsz)
162 #define encode_claim_null_maxsz	(1 + nfs4_name_maxsz)
163 #define encode_open_maxsz	(op_encode_hdr_maxsz + \
164 				2 + encode_share_access_maxsz + 2 + \
165 				open_owner_id_maxsz + \
166 				encode_opentype_maxsz + \
167 				encode_claim_null_maxsz)
168 #define decode_ace_maxsz	(3 + nfs4_owner_maxsz)
169 #define decode_delegation_maxsz	(1 + decode_stateid_maxsz + 1 + \
170 				decode_ace_maxsz)
171 #define decode_change_info_maxsz	(5)
172 #define decode_open_maxsz	(op_decode_hdr_maxsz + \
173 				decode_stateid_maxsz + \
174 				decode_change_info_maxsz + 1 + \
175 				nfs4_fattr_bitmap_maxsz + \
176 				decode_delegation_maxsz)
177 #define encode_open_confirm_maxsz \
178 				(op_encode_hdr_maxsz + \
179 				 encode_stateid_maxsz + 1)
180 #define decode_open_confirm_maxsz \
181 				(op_decode_hdr_maxsz + \
182 				 decode_stateid_maxsz)
183 #define encode_open_downgrade_maxsz \
184 				(op_encode_hdr_maxsz + \
185 				 encode_stateid_maxsz + 1 + \
186 				 encode_share_access_maxsz)
187 #define decode_open_downgrade_maxsz \
188 				(op_decode_hdr_maxsz + \
189 				 decode_stateid_maxsz)
190 #define encode_close_maxsz	(op_encode_hdr_maxsz + \
191 				 1 + encode_stateid_maxsz)
192 #define decode_close_maxsz	(op_decode_hdr_maxsz + \
193 				 decode_stateid_maxsz)
194 #define encode_setattr_maxsz	(op_encode_hdr_maxsz + \
195 				 encode_stateid_maxsz + \
196 				 encode_attrs_maxsz)
197 #define decode_setattr_maxsz	(op_decode_hdr_maxsz + \
198 				 nfs4_fattr_bitmap_maxsz)
199 #define encode_read_maxsz	(op_encode_hdr_maxsz + \
200 				 encode_stateid_maxsz + 3)
201 #define decode_read_maxsz	(op_decode_hdr_maxsz + 2)
202 #define encode_readdir_maxsz	(op_encode_hdr_maxsz + \
203 				 2 + encode_verifier_maxsz + 5 + \
204 				nfs4_label_maxsz)
205 #define decode_readdir_maxsz	(op_decode_hdr_maxsz + \
206 				 decode_verifier_maxsz)
207 #define encode_readlink_maxsz	(op_encode_hdr_maxsz)
208 #define decode_readlink_maxsz	(op_decode_hdr_maxsz + 1)
209 #define encode_write_maxsz	(op_encode_hdr_maxsz + \
210 				 encode_stateid_maxsz + 4)
211 #define decode_write_maxsz	(op_decode_hdr_maxsz + \
212 				 2 + decode_verifier_maxsz)
213 #define encode_commit_maxsz	(op_encode_hdr_maxsz + 3)
214 #define decode_commit_maxsz	(op_decode_hdr_maxsz + \
215 				 decode_verifier_maxsz)
216 #define encode_remove_maxsz	(op_encode_hdr_maxsz + \
217 				nfs4_name_maxsz)
218 #define decode_remove_maxsz	(op_decode_hdr_maxsz + \
219 				 decode_change_info_maxsz)
220 #define encode_rename_maxsz	(op_encode_hdr_maxsz + \
221 				2 * nfs4_name_maxsz)
222 #define decode_rename_maxsz	(op_decode_hdr_maxsz + \
223 				 decode_change_info_maxsz + \
224 				 decode_change_info_maxsz)
225 #define encode_link_maxsz	(op_encode_hdr_maxsz + \
226 				nfs4_name_maxsz)
227 #define decode_link_maxsz	(op_decode_hdr_maxsz + decode_change_info_maxsz)
228 #define encode_lockowner_maxsz	(7)
229 #define encode_lock_maxsz	(op_encode_hdr_maxsz + \
230 				 7 + \
231 				 1 + encode_stateid_maxsz + 1 + \
232 				 encode_lockowner_maxsz)
233 #define decode_lock_denied_maxsz \
234 				(8 + decode_lockowner_maxsz)
235 #define decode_lock_maxsz	(op_decode_hdr_maxsz + \
236 				 decode_lock_denied_maxsz)
237 #define encode_lockt_maxsz	(op_encode_hdr_maxsz + 5 + \
238 				encode_lockowner_maxsz)
239 #define decode_lockt_maxsz	(op_decode_hdr_maxsz + \
240 				 decode_lock_denied_maxsz)
241 #define encode_locku_maxsz	(op_encode_hdr_maxsz + 3 + \
242 				 encode_stateid_maxsz + \
243 				 4)
244 #define decode_locku_maxsz	(op_decode_hdr_maxsz + \
245 				 decode_stateid_maxsz)
246 #define encode_release_lockowner_maxsz \
247 				(op_encode_hdr_maxsz + \
248 				 encode_lockowner_maxsz)
249 #define decode_release_lockowner_maxsz \
250 				(op_decode_hdr_maxsz)
251 #define encode_access_maxsz	(op_encode_hdr_maxsz + 1)
252 #define decode_access_maxsz	(op_decode_hdr_maxsz + 2)
253 #define encode_symlink_maxsz	(op_encode_hdr_maxsz + \
254 				1 + nfs4_name_maxsz + \
255 				1 + \
256 				nfs4_fattr_maxsz)
257 #define decode_symlink_maxsz	(op_decode_hdr_maxsz + 8)
258 #define encode_create_maxsz	(op_encode_hdr_maxsz + \
259 				1 + 2 + nfs4_name_maxsz + \
260 				encode_attrs_maxsz)
261 #define decode_create_maxsz	(op_decode_hdr_maxsz + \
262 				decode_change_info_maxsz + \
263 				nfs4_fattr_bitmap_maxsz)
264 #define encode_statfs_maxsz	(encode_getattr_maxsz)
265 #define decode_statfs_maxsz	(decode_getattr_maxsz)
266 #define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
267 #define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
268 #define encode_getacl_maxsz	(encode_getattr_maxsz)
269 #define decode_getacl_maxsz	(op_decode_hdr_maxsz + \
270 				 nfs4_fattr_bitmap_maxsz + 1)
271 #define encode_setacl_maxsz	(op_encode_hdr_maxsz + \
272 				 encode_stateid_maxsz + 3)
273 #define decode_setacl_maxsz	(decode_setattr_maxsz)
274 #define encode_fs_locations_maxsz \
275 				(encode_getattr_maxsz)
276 #define decode_fs_locations_maxsz \
277 				(0)
278 #define encode_secinfo_maxsz	(op_encode_hdr_maxsz + nfs4_name_maxsz)
279 #define decode_secinfo_maxsz	(op_decode_hdr_maxsz + 1 + ((NFS_MAX_SECFLAVORS * (16 + GSS_OID_MAX_LEN)) / 4))
280 
281 #if defined(CONFIG_NFS_V4_1)
282 #define NFS4_MAX_MACHINE_NAME_LEN (64)
283 #define IMPL_NAME_LIMIT (sizeof(utsname()->sysname) + sizeof(utsname()->release) + \
284 			 sizeof(utsname()->version) + sizeof(utsname()->machine) + 8)
285 
286 #define encode_exchange_id_maxsz (op_encode_hdr_maxsz + \
287 				encode_verifier_maxsz + \
288 				1 /* co_ownerid.len */ + \
289 				XDR_QUADLEN(NFS4_EXCHANGE_ID_LEN) + \
290 				1 /* flags */ + \
291 				1 /* spa_how */ + \
292 				/* max is SP4_MACH_CRED (for now) */ + \
293 				1 + NFS4_OP_MAP_NUM_WORDS + \
294 				1 + NFS4_OP_MAP_NUM_WORDS + \
295 				1 /* implementation id array of size 1 */ + \
296 				1 /* nii_domain */ + \
297 				XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + \
298 				1 /* nii_name */ + \
299 				XDR_QUADLEN(IMPL_NAME_LIMIT) + \
300 				3 /* nii_date */)
301 #define decode_exchange_id_maxsz (op_decode_hdr_maxsz + \
302 				2 /* eir_clientid */ + \
303 				1 /* eir_sequenceid */ + \
304 				1 /* eir_flags */ + \
305 				1 /* spr_how */ + \
306 				  /* max is SP4_MACH_CRED (for now) */ + \
307 				1 + NFS4_OP_MAP_NUM_WORDS + \
308 				1 + NFS4_OP_MAP_NUM_WORDS + \
309 				2 /* eir_server_owner.so_minor_id */ + \
310 				/* eir_server_owner.so_major_id<> */ \
311 				XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 + \
312 				/* eir_server_scope<> */ \
313 				XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 + \
314 				1 /* eir_server_impl_id array length */ + \
315 				1 /* nii_domain */ + \
316 				XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + \
317 				1 /* nii_name */ + \
318 				XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + \
319 				3 /* nii_date */)
320 #define encode_channel_attrs_maxsz  (6 + 1 /* ca_rdma_ird.len (0) */)
321 #define decode_channel_attrs_maxsz  (6 + \
322 				     1 /* ca_rdma_ird.len */ + \
323 				     1 /* ca_rdma_ird */)
324 #define encode_create_session_maxsz  (op_encode_hdr_maxsz + \
325 				     2 /* csa_clientid */ + \
326 				     1 /* csa_sequence */ + \
327 				     1 /* csa_flags */ + \
328 				     encode_channel_attrs_maxsz + \
329 				     encode_channel_attrs_maxsz + \
330 				     1 /* csa_cb_program */ + \
331 				     1 /* csa_sec_parms.len (1) */ + \
332 				     1 /* cb_secflavor (AUTH_SYS) */ + \
333 				     1 /* stamp */ + \
334 				     1 /* machinename.len */ + \
335 				     XDR_QUADLEN(NFS4_MAX_MACHINE_NAME_LEN) + \
336 				     1 /* uid */ + \
337 				     1 /* gid */ + \
338 				     1 /* gids.len (0) */)
339 #define decode_create_session_maxsz  (op_decode_hdr_maxsz +	\
340 				     XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
341 				     1 /* csr_sequence */ + \
342 				     1 /* csr_flags */ + \
343 				     decode_channel_attrs_maxsz + \
344 				     decode_channel_attrs_maxsz)
345 #define encode_bind_conn_to_session_maxsz  (op_encode_hdr_maxsz + \
346 				     /* bctsa_sessid */ \
347 				     XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
348 				     1 /* bctsa_dir */ + \
349 				     1 /* bctsa_use_conn_in_rdma_mode */)
350 #define decode_bind_conn_to_session_maxsz  (op_decode_hdr_maxsz +	\
351 				     /* bctsr_sessid */ \
352 				     XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
353 				     1 /* bctsr_dir */ + \
354 				     1 /* bctsr_use_conn_in_rdma_mode */)
355 #define encode_destroy_session_maxsz    (op_encode_hdr_maxsz + 4)
356 #define decode_destroy_session_maxsz    (op_decode_hdr_maxsz)
357 #define encode_destroy_clientid_maxsz   (op_encode_hdr_maxsz + 2)
358 #define decode_destroy_clientid_maxsz   (op_decode_hdr_maxsz)
359 #define encode_sequence_maxsz	(op_encode_hdr_maxsz + \
360 				XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 4)
361 #define decode_sequence_maxsz	(op_decode_hdr_maxsz + \
362 				XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5)
363 #define encode_reclaim_complete_maxsz	(op_encode_hdr_maxsz + 4)
364 #define decode_reclaim_complete_maxsz	(op_decode_hdr_maxsz + 4)
365 #define encode_getdeviceinfo_maxsz (op_encode_hdr_maxsz + \
366 				XDR_QUADLEN(NFS4_DEVICEID4_SIZE) + \
367 				1 /* layout type */ + \
368 				1 /* maxcount */ + \
369 				1 /* bitmap size */ + \
370 				1 /* notification bitmap length */ + \
371 				1 /* notification bitmap, word 0 */)
372 #define decode_getdeviceinfo_maxsz (op_decode_hdr_maxsz + \
373 				1 /* layout type */ + \
374 				1 /* opaque devaddr4 length */ + \
375 				  /* devaddr4 payload is read into page */ \
376 				1 /* notification bitmap length */ + \
377 				1 /* notification bitmap, word 0 */)
378 #define encode_layoutget_maxsz	(op_encode_hdr_maxsz + 10 + \
379 				encode_stateid_maxsz)
380 #define decode_layoutget_maxsz	(op_decode_hdr_maxsz + 8 + \
381 				decode_stateid_maxsz + \
382 				XDR_QUADLEN(PNFS_LAYOUT_MAXSIZE))
383 #define encode_layoutcommit_maxsz (op_encode_hdr_maxsz +          \
384 				2 /* offset */ + \
385 				2 /* length */ + \
386 				1 /* reclaim */ + \
387 				encode_stateid_maxsz + \
388 				1 /* new offset (true) */ + \
389 				2 /* last byte written */ + \
390 				1 /* nt_timechanged (false) */ + \
391 				1 /* layoutupdate4 layout type */ + \
392 				1 /* layoutupdate4 opaqueue len */)
393 				  /* the actual content of layoutupdate4 should
394 				     be allocated by drivers and spliced in
395 				     using xdr_write_pages */
396 #define decode_layoutcommit_maxsz (op_decode_hdr_maxsz + 3)
397 #define encode_layoutreturn_maxsz (8 + op_encode_hdr_maxsz + \
398 				encode_stateid_maxsz + \
399 				1 /* FIXME: opaque lrf_body always empty at the moment */)
400 #define decode_layoutreturn_maxsz (op_decode_hdr_maxsz + \
401 				1 + decode_stateid_maxsz)
402 #define encode_secinfo_no_name_maxsz (op_encode_hdr_maxsz + 1)
403 #define decode_secinfo_no_name_maxsz decode_secinfo_maxsz
404 #define encode_test_stateid_maxsz	(op_encode_hdr_maxsz + 2 + \
405 					 XDR_QUADLEN(NFS4_STATEID_SIZE))
406 #define decode_test_stateid_maxsz	(op_decode_hdr_maxsz + 2 + 1)
407 #define encode_free_stateid_maxsz	(op_encode_hdr_maxsz + 1 + \
408 					 XDR_QUADLEN(NFS4_STATEID_SIZE))
409 #define decode_free_stateid_maxsz	(op_decode_hdr_maxsz)
410 #else /* CONFIG_NFS_V4_1 */
411 #define encode_sequence_maxsz	0
412 #define decode_sequence_maxsz	0
413 #endif /* CONFIG_NFS_V4_1 */
414 
415 #define NFS4_enc_compound_sz	(1024)  /* XXX: large enough? */
416 #define NFS4_dec_compound_sz	(1024)  /* XXX: large enough? */
417 #define NFS4_enc_read_sz	(compound_encode_hdr_maxsz + \
418 				encode_sequence_maxsz + \
419 				encode_putfh_maxsz + \
420 				encode_read_maxsz)
421 #define NFS4_dec_read_sz	(compound_decode_hdr_maxsz + \
422 				decode_sequence_maxsz + \
423 				decode_putfh_maxsz + \
424 				decode_read_maxsz)
425 #define NFS4_enc_readlink_sz	(compound_encode_hdr_maxsz + \
426 				encode_sequence_maxsz + \
427 				encode_putfh_maxsz + \
428 				encode_readlink_maxsz)
429 #define NFS4_dec_readlink_sz	(compound_decode_hdr_maxsz + \
430 				decode_sequence_maxsz + \
431 				decode_putfh_maxsz + \
432 				decode_readlink_maxsz)
433 #define NFS4_enc_readdir_sz	(compound_encode_hdr_maxsz + \
434 				encode_sequence_maxsz + \
435 				encode_putfh_maxsz + \
436 				encode_readdir_maxsz)
437 #define NFS4_dec_readdir_sz	(compound_decode_hdr_maxsz + \
438 				decode_sequence_maxsz + \
439 				decode_putfh_maxsz + \
440 				decode_readdir_maxsz)
441 #define NFS4_enc_write_sz	(compound_encode_hdr_maxsz + \
442 				encode_sequence_maxsz + \
443 				encode_putfh_maxsz + \
444 				encode_write_maxsz + \
445 				encode_getattr_maxsz)
446 #define NFS4_dec_write_sz	(compound_decode_hdr_maxsz + \
447 				decode_sequence_maxsz + \
448 				decode_putfh_maxsz + \
449 				decode_write_maxsz + \
450 				decode_getattr_maxsz)
451 #define NFS4_enc_commit_sz	(compound_encode_hdr_maxsz + \
452 				encode_sequence_maxsz + \
453 				encode_putfh_maxsz + \
454 				encode_commit_maxsz)
455 #define NFS4_dec_commit_sz	(compound_decode_hdr_maxsz + \
456 				decode_sequence_maxsz + \
457 				decode_putfh_maxsz + \
458 				decode_commit_maxsz)
459 #define NFS4_enc_open_sz        (compound_encode_hdr_maxsz + \
460 				encode_sequence_maxsz + \
461 				encode_putfh_maxsz + \
462 				encode_open_maxsz + \
463 				encode_access_maxsz + \
464 				encode_getfh_maxsz + \
465 				encode_getattr_maxsz)
466 #define NFS4_dec_open_sz        (compound_decode_hdr_maxsz + \
467 				decode_sequence_maxsz + \
468 				decode_putfh_maxsz + \
469 				decode_open_maxsz + \
470 				decode_access_maxsz + \
471 				decode_getfh_maxsz + \
472 				decode_getattr_maxsz)
473 #define NFS4_enc_open_confirm_sz \
474 				(compound_encode_hdr_maxsz + \
475 				 encode_putfh_maxsz + \
476 				 encode_open_confirm_maxsz)
477 #define NFS4_dec_open_confirm_sz \
478 				(compound_decode_hdr_maxsz + \
479 				 decode_putfh_maxsz + \
480 				 decode_open_confirm_maxsz)
481 #define NFS4_enc_open_noattr_sz	(compound_encode_hdr_maxsz + \
482 					encode_sequence_maxsz + \
483 					encode_putfh_maxsz + \
484 					encode_open_maxsz + \
485 					encode_access_maxsz + \
486 					encode_getattr_maxsz)
487 #define NFS4_dec_open_noattr_sz	(compound_decode_hdr_maxsz + \
488 					decode_sequence_maxsz + \
489 					decode_putfh_maxsz + \
490 					decode_open_maxsz + \
491 					decode_access_maxsz + \
492 					decode_getattr_maxsz)
493 #define NFS4_enc_open_downgrade_sz \
494 				(compound_encode_hdr_maxsz + \
495 				 encode_sequence_maxsz + \
496 				 encode_putfh_maxsz + \
497 				 encode_open_downgrade_maxsz + \
498 				 encode_getattr_maxsz)
499 #define NFS4_dec_open_downgrade_sz \
500 				(compound_decode_hdr_maxsz + \
501 				 decode_sequence_maxsz + \
502 				 decode_putfh_maxsz + \
503 				 decode_open_downgrade_maxsz + \
504 				 decode_getattr_maxsz)
505 #define NFS4_enc_close_sz	(compound_encode_hdr_maxsz + \
506 				 encode_sequence_maxsz + \
507 				 encode_putfh_maxsz + \
508 				 encode_close_maxsz + \
509 				 encode_getattr_maxsz)
510 #define NFS4_dec_close_sz	(compound_decode_hdr_maxsz + \
511 				 decode_sequence_maxsz + \
512 				 decode_putfh_maxsz + \
513 				 decode_close_maxsz + \
514 				 decode_getattr_maxsz)
515 #define NFS4_enc_setattr_sz	(compound_encode_hdr_maxsz + \
516 				 encode_sequence_maxsz + \
517 				 encode_putfh_maxsz + \
518 				 encode_setattr_maxsz + \
519 				 encode_getattr_maxsz)
520 #define NFS4_dec_setattr_sz	(compound_decode_hdr_maxsz + \
521 				 decode_sequence_maxsz + \
522 				 decode_putfh_maxsz + \
523 				 decode_setattr_maxsz + \
524 				 decode_getattr_maxsz)
525 #define NFS4_enc_fsinfo_sz	(compound_encode_hdr_maxsz + \
526 				encode_sequence_maxsz + \
527 				encode_putfh_maxsz + \
528 				encode_fsinfo_maxsz)
529 #define NFS4_dec_fsinfo_sz	(compound_decode_hdr_maxsz + \
530 				decode_sequence_maxsz + \
531 				decode_putfh_maxsz + \
532 				decode_fsinfo_maxsz)
533 #define NFS4_enc_renew_sz	(compound_encode_hdr_maxsz + \
534 				encode_renew_maxsz)
535 #define NFS4_dec_renew_sz	(compound_decode_hdr_maxsz + \
536 				decode_renew_maxsz)
537 #define NFS4_enc_setclientid_sz	(compound_encode_hdr_maxsz + \
538 				encode_setclientid_maxsz)
539 #define NFS4_dec_setclientid_sz	(compound_decode_hdr_maxsz + \
540 				decode_setclientid_maxsz)
541 #define NFS4_enc_setclientid_confirm_sz \
542 				(compound_encode_hdr_maxsz + \
543 				encode_setclientid_confirm_maxsz)
544 #define NFS4_dec_setclientid_confirm_sz \
545 				(compound_decode_hdr_maxsz + \
546 				decode_setclientid_confirm_maxsz)
547 #define NFS4_enc_lock_sz        (compound_encode_hdr_maxsz + \
548 				encode_sequence_maxsz + \
549 				encode_putfh_maxsz + \
550 				encode_lock_maxsz)
551 #define NFS4_dec_lock_sz        (compound_decode_hdr_maxsz + \
552 				decode_sequence_maxsz + \
553 				decode_putfh_maxsz + \
554 				decode_lock_maxsz)
555 #define NFS4_enc_lockt_sz       (compound_encode_hdr_maxsz + \
556 				encode_sequence_maxsz + \
557 				encode_putfh_maxsz + \
558 				encode_lockt_maxsz)
559 #define NFS4_dec_lockt_sz       (compound_decode_hdr_maxsz + \
560 				 decode_sequence_maxsz + \
561 				 decode_putfh_maxsz + \
562 				 decode_lockt_maxsz)
563 #define NFS4_enc_locku_sz       (compound_encode_hdr_maxsz + \
564 				encode_sequence_maxsz + \
565 				encode_putfh_maxsz + \
566 				encode_locku_maxsz)
567 #define NFS4_dec_locku_sz       (compound_decode_hdr_maxsz + \
568 				decode_sequence_maxsz + \
569 				decode_putfh_maxsz + \
570 				decode_locku_maxsz)
571 #define NFS4_enc_release_lockowner_sz \
572 				(compound_encode_hdr_maxsz + \
573 				 encode_lockowner_maxsz)
574 #define NFS4_dec_release_lockowner_sz \
575 				(compound_decode_hdr_maxsz + \
576 				 decode_lockowner_maxsz)
577 #define NFS4_enc_access_sz	(compound_encode_hdr_maxsz + \
578 				encode_sequence_maxsz + \
579 				encode_putfh_maxsz + \
580 				encode_access_maxsz + \
581 				encode_getattr_maxsz)
582 #define NFS4_dec_access_sz	(compound_decode_hdr_maxsz + \
583 				decode_sequence_maxsz + \
584 				decode_putfh_maxsz + \
585 				decode_access_maxsz + \
586 				decode_getattr_maxsz)
587 #define NFS4_enc_getattr_sz	(compound_encode_hdr_maxsz + \
588 				encode_sequence_maxsz + \
589 				encode_putfh_maxsz + \
590 				encode_getattr_maxsz + \
591 				encode_renew_maxsz)
592 #define NFS4_dec_getattr_sz	(compound_decode_hdr_maxsz + \
593 				decode_sequence_maxsz + \
594 				decode_putfh_maxsz + \
595 				decode_getattr_maxsz + \
596 				decode_renew_maxsz)
597 #define NFS4_enc_lookup_sz	(compound_encode_hdr_maxsz + \
598 				encode_sequence_maxsz + \
599 				encode_putfh_maxsz + \
600 				encode_lookup_maxsz + \
601 				encode_getattr_maxsz + \
602 				encode_getfh_maxsz)
603 #define NFS4_dec_lookup_sz	(compound_decode_hdr_maxsz + \
604 				decode_sequence_maxsz + \
605 				decode_putfh_maxsz + \
606 				decode_lookup_maxsz + \
607 				decode_getattr_maxsz + \
608 				decode_getfh_maxsz)
609 #define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
610 				encode_sequence_maxsz + \
611 				encode_putrootfh_maxsz + \
612 				encode_getattr_maxsz + \
613 				encode_getfh_maxsz)
614 #define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
615 				decode_sequence_maxsz + \
616 				decode_putrootfh_maxsz + \
617 				decode_getattr_maxsz + \
618 				decode_getfh_maxsz)
619 #define NFS4_enc_remove_sz	(compound_encode_hdr_maxsz + \
620 				encode_sequence_maxsz + \
621 				encode_putfh_maxsz + \
622 				encode_remove_maxsz)
623 #define NFS4_dec_remove_sz	(compound_decode_hdr_maxsz + \
624 				decode_sequence_maxsz + \
625 				decode_putfh_maxsz + \
626 				decode_remove_maxsz)
627 #define NFS4_enc_rename_sz	(compound_encode_hdr_maxsz + \
628 				encode_sequence_maxsz + \
629 				encode_putfh_maxsz + \
630 				encode_savefh_maxsz + \
631 				encode_putfh_maxsz + \
632 				encode_rename_maxsz)
633 #define NFS4_dec_rename_sz	(compound_decode_hdr_maxsz + \
634 				decode_sequence_maxsz + \
635 				decode_putfh_maxsz + \
636 				decode_savefh_maxsz + \
637 				decode_putfh_maxsz + \
638 				decode_rename_maxsz)
639 #define NFS4_enc_link_sz	(compound_encode_hdr_maxsz + \
640 				encode_sequence_maxsz + \
641 				encode_putfh_maxsz + \
642 				encode_savefh_maxsz + \
643 				encode_putfh_maxsz + \
644 				encode_link_maxsz + \
645 				encode_restorefh_maxsz + \
646 				encode_getattr_maxsz)
647 #define NFS4_dec_link_sz	(compound_decode_hdr_maxsz + \
648 				decode_sequence_maxsz + \
649 				decode_putfh_maxsz + \
650 				decode_savefh_maxsz + \
651 				decode_putfh_maxsz + \
652 				decode_link_maxsz + \
653 				decode_restorefh_maxsz + \
654 				decode_getattr_maxsz)
655 #define NFS4_enc_symlink_sz	(compound_encode_hdr_maxsz + \
656 				encode_sequence_maxsz + \
657 				encode_putfh_maxsz + \
658 				encode_symlink_maxsz + \
659 				encode_getattr_maxsz + \
660 				encode_getfh_maxsz)
661 #define NFS4_dec_symlink_sz	(compound_decode_hdr_maxsz + \
662 				decode_sequence_maxsz + \
663 				decode_putfh_maxsz + \
664 				decode_symlink_maxsz + \
665 				decode_getattr_maxsz + \
666 				decode_getfh_maxsz)
667 #define NFS4_enc_create_sz	(compound_encode_hdr_maxsz + \
668 				encode_sequence_maxsz + \
669 				encode_putfh_maxsz + \
670 				encode_create_maxsz + \
671 				encode_getfh_maxsz + \
672 				encode_getattr_maxsz)
673 #define NFS4_dec_create_sz	(compound_decode_hdr_maxsz + \
674 				decode_sequence_maxsz + \
675 				decode_putfh_maxsz + \
676 				decode_create_maxsz + \
677 				decode_getfh_maxsz + \
678 				decode_getattr_maxsz)
679 #define NFS4_enc_pathconf_sz	(compound_encode_hdr_maxsz + \
680 				encode_sequence_maxsz + \
681 				encode_putfh_maxsz + \
682 				encode_getattr_maxsz)
683 #define NFS4_dec_pathconf_sz	(compound_decode_hdr_maxsz + \
684 				decode_sequence_maxsz + \
685 				decode_putfh_maxsz + \
686 				decode_getattr_maxsz)
687 #define NFS4_enc_statfs_sz	(compound_encode_hdr_maxsz + \
688 				encode_sequence_maxsz + \
689 				encode_putfh_maxsz + \
690 				encode_statfs_maxsz)
691 #define NFS4_dec_statfs_sz	(compound_decode_hdr_maxsz + \
692 				decode_sequence_maxsz + \
693 				decode_putfh_maxsz + \
694 				decode_statfs_maxsz)
695 #define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
696 				encode_sequence_maxsz + \
697 				encode_putfh_maxsz + \
698 				encode_getattr_maxsz)
699 #define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
700 				decode_sequence_maxsz + \
701 				decode_putfh_maxsz + \
702 				decode_getattr_maxsz)
703 #define NFS4_enc_delegreturn_sz	(compound_encode_hdr_maxsz + \
704 				encode_sequence_maxsz + \
705 				encode_putfh_maxsz + \
706 				encode_delegreturn_maxsz + \
707 				encode_getattr_maxsz)
708 #define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
709 				decode_sequence_maxsz + \
710 				decode_delegreturn_maxsz + \
711 				decode_getattr_maxsz)
712 #define NFS4_enc_getacl_sz	(compound_encode_hdr_maxsz + \
713 				encode_sequence_maxsz + \
714 				encode_putfh_maxsz + \
715 				encode_getacl_maxsz)
716 #define NFS4_dec_getacl_sz	(compound_decode_hdr_maxsz + \
717 				decode_sequence_maxsz + \
718 				decode_putfh_maxsz + \
719 				decode_getacl_maxsz)
720 #define NFS4_enc_setacl_sz	(compound_encode_hdr_maxsz + \
721 				encode_sequence_maxsz + \
722 				encode_putfh_maxsz + \
723 				encode_setacl_maxsz)
724 #define NFS4_dec_setacl_sz	(compound_decode_hdr_maxsz + \
725 				decode_sequence_maxsz + \
726 				decode_putfh_maxsz + \
727 				decode_setacl_maxsz)
728 #define NFS4_enc_fs_locations_sz \
729 				(compound_encode_hdr_maxsz + \
730 				 encode_sequence_maxsz + \
731 				 encode_putfh_maxsz + \
732 				 encode_lookup_maxsz + \
733 				 encode_fs_locations_maxsz + \
734 				 encode_renew_maxsz)
735 #define NFS4_dec_fs_locations_sz \
736 				(compound_decode_hdr_maxsz + \
737 				 decode_sequence_maxsz + \
738 				 decode_putfh_maxsz + \
739 				 decode_lookup_maxsz + \
740 				 decode_fs_locations_maxsz + \
741 				 decode_renew_maxsz)
742 #define NFS4_enc_secinfo_sz 	(compound_encode_hdr_maxsz + \
743 				encode_sequence_maxsz + \
744 				encode_putfh_maxsz + \
745 				encode_secinfo_maxsz)
746 #define NFS4_dec_secinfo_sz	(compound_decode_hdr_maxsz + \
747 				decode_sequence_maxsz + \
748 				decode_putfh_maxsz + \
749 				decode_secinfo_maxsz)
750 #define NFS4_enc_fsid_present_sz \
751 				(compound_encode_hdr_maxsz + \
752 				 encode_sequence_maxsz + \
753 				 encode_putfh_maxsz + \
754 				 encode_getfh_maxsz + \
755 				 encode_renew_maxsz)
756 #define NFS4_dec_fsid_present_sz \
757 				(compound_decode_hdr_maxsz + \
758 				 decode_sequence_maxsz + \
759 				 decode_putfh_maxsz + \
760 				 decode_getfh_maxsz + \
761 				 decode_renew_maxsz)
762 #if defined(CONFIG_NFS_V4_1)
763 #define NFS4_enc_bind_conn_to_session_sz \
764 				(compound_encode_hdr_maxsz + \
765 				 encode_bind_conn_to_session_maxsz)
766 #define NFS4_dec_bind_conn_to_session_sz \
767 				(compound_decode_hdr_maxsz + \
768 				 decode_bind_conn_to_session_maxsz)
769 #define NFS4_enc_exchange_id_sz \
770 				(compound_encode_hdr_maxsz + \
771 				 encode_exchange_id_maxsz)
772 #define NFS4_dec_exchange_id_sz \
773 				(compound_decode_hdr_maxsz + \
774 				 decode_exchange_id_maxsz)
775 #define NFS4_enc_create_session_sz \
776 				(compound_encode_hdr_maxsz + \
777 				 encode_create_session_maxsz)
778 #define NFS4_dec_create_session_sz \
779 				(compound_decode_hdr_maxsz + \
780 				 decode_create_session_maxsz)
781 #define NFS4_enc_destroy_session_sz	(compound_encode_hdr_maxsz + \
782 					 encode_destroy_session_maxsz)
783 #define NFS4_dec_destroy_session_sz	(compound_decode_hdr_maxsz + \
784 					 decode_destroy_session_maxsz)
785 #define NFS4_enc_destroy_clientid_sz	(compound_encode_hdr_maxsz + \
786 					 encode_destroy_clientid_maxsz)
787 #define NFS4_dec_destroy_clientid_sz	(compound_decode_hdr_maxsz + \
788 					 decode_destroy_clientid_maxsz)
789 #define NFS4_enc_sequence_sz \
790 				(compound_decode_hdr_maxsz + \
791 				 encode_sequence_maxsz)
792 #define NFS4_dec_sequence_sz \
793 				(compound_decode_hdr_maxsz + \
794 				 decode_sequence_maxsz)
795 #define NFS4_enc_get_lease_time_sz	(compound_encode_hdr_maxsz + \
796 					 encode_sequence_maxsz + \
797 					 encode_putrootfh_maxsz + \
798 					 encode_fsinfo_maxsz)
799 #define NFS4_dec_get_lease_time_sz	(compound_decode_hdr_maxsz + \
800 					 decode_sequence_maxsz + \
801 					 decode_putrootfh_maxsz + \
802 					 decode_fsinfo_maxsz)
803 #define NFS4_enc_reclaim_complete_sz	(compound_encode_hdr_maxsz + \
804 					 encode_sequence_maxsz + \
805 					 encode_reclaim_complete_maxsz)
806 #define NFS4_dec_reclaim_complete_sz	(compound_decode_hdr_maxsz + \
807 					 decode_sequence_maxsz + \
808 					 decode_reclaim_complete_maxsz)
809 #define NFS4_enc_getdeviceinfo_sz (compound_encode_hdr_maxsz +    \
810 				encode_sequence_maxsz +\
811 				encode_getdeviceinfo_maxsz)
812 #define NFS4_dec_getdeviceinfo_sz (compound_decode_hdr_maxsz +    \
813 				decode_sequence_maxsz + \
814 				decode_getdeviceinfo_maxsz)
815 #define NFS4_enc_layoutget_sz	(compound_encode_hdr_maxsz + \
816 				encode_sequence_maxsz + \
817 				encode_putfh_maxsz +        \
818 				encode_layoutget_maxsz)
819 #define NFS4_dec_layoutget_sz	(compound_decode_hdr_maxsz + \
820 				decode_sequence_maxsz + \
821 				decode_putfh_maxsz +        \
822 				decode_layoutget_maxsz)
823 #define NFS4_enc_layoutcommit_sz (compound_encode_hdr_maxsz + \
824 				encode_sequence_maxsz +\
825 				encode_putfh_maxsz + \
826 				encode_layoutcommit_maxsz + \
827 				encode_getattr_maxsz)
828 #define NFS4_dec_layoutcommit_sz (compound_decode_hdr_maxsz + \
829 				decode_sequence_maxsz + \
830 				decode_putfh_maxsz + \
831 				decode_layoutcommit_maxsz + \
832 				decode_getattr_maxsz)
833 #define NFS4_enc_layoutreturn_sz (compound_encode_hdr_maxsz + \
834 				encode_sequence_maxsz + \
835 				encode_putfh_maxsz + \
836 				encode_layoutreturn_maxsz)
837 #define NFS4_dec_layoutreturn_sz (compound_decode_hdr_maxsz + \
838 				decode_sequence_maxsz + \
839 				decode_putfh_maxsz + \
840 				decode_layoutreturn_maxsz)
841 #define NFS4_enc_secinfo_no_name_sz	(compound_encode_hdr_maxsz + \
842 					encode_sequence_maxsz + \
843 					encode_putrootfh_maxsz +\
844 					encode_secinfo_no_name_maxsz)
845 #define NFS4_dec_secinfo_no_name_sz	(compound_decode_hdr_maxsz + \
846 					decode_sequence_maxsz + \
847 					decode_putrootfh_maxsz + \
848 					decode_secinfo_no_name_maxsz)
849 #define NFS4_enc_test_stateid_sz	(compound_encode_hdr_maxsz + \
850 					 encode_sequence_maxsz + \
851 					 encode_test_stateid_maxsz)
852 #define NFS4_dec_test_stateid_sz	(compound_decode_hdr_maxsz + \
853 					 decode_sequence_maxsz + \
854 					 decode_test_stateid_maxsz)
855 #define NFS4_enc_free_stateid_sz	(compound_encode_hdr_maxsz + \
856 					 encode_sequence_maxsz + \
857 					 encode_free_stateid_maxsz)
858 #define NFS4_dec_free_stateid_sz	(compound_decode_hdr_maxsz + \
859 					 decode_sequence_maxsz + \
860 					 decode_free_stateid_maxsz)
861 
862 const u32 nfs41_maxwrite_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
863 				      compound_encode_hdr_maxsz +
864 				      encode_sequence_maxsz +
865 				      encode_putfh_maxsz +
866 				      encode_getattr_maxsz) *
867 				     XDR_UNIT);
868 
869 const u32 nfs41_maxread_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
870 				     compound_decode_hdr_maxsz +
871 				     decode_sequence_maxsz +
872 				     decode_putfh_maxsz) *
873 				    XDR_UNIT);
874 
875 const u32 nfs41_maxgetdevinfo_overhead = ((RPC_MAX_REPHEADER_WITH_AUTH +
876 					   compound_decode_hdr_maxsz +
877 					   decode_sequence_maxsz) *
878 					  XDR_UNIT);
879 EXPORT_SYMBOL_GPL(nfs41_maxgetdevinfo_overhead);
880 #endif /* CONFIG_NFS_V4_1 */
881 
882 static const umode_t nfs_type2fmt[] = {
883 	[NF4BAD] = 0,
884 	[NF4REG] = S_IFREG,
885 	[NF4DIR] = S_IFDIR,
886 	[NF4BLK] = S_IFBLK,
887 	[NF4CHR] = S_IFCHR,
888 	[NF4LNK] = S_IFLNK,
889 	[NF4SOCK] = S_IFSOCK,
890 	[NF4FIFO] = S_IFIFO,
891 	[NF4ATTRDIR] = 0,
892 	[NF4NAMEDATTR] = 0,
893 };
894 
895 struct compound_hdr {
896 	int32_t		status;
897 	uint32_t	nops;
898 	__be32 *	nops_p;
899 	uint32_t	taglen;
900 	char *		tag;
901 	uint32_t	replen;		/* expected reply words */
902 	u32		minorversion;
903 };
904 
905 static __be32 *reserve_space(struct xdr_stream *xdr, size_t nbytes)
906 {
907 	__be32 *p = xdr_reserve_space(xdr, nbytes);
908 	BUG_ON(!p);
909 	return p;
910 }
911 
912 static void encode_opaque_fixed(struct xdr_stream *xdr, const void *buf, size_t len)
913 {
914 	__be32 *p;
915 
916 	p = xdr_reserve_space(xdr, len);
917 	xdr_encode_opaque_fixed(p, buf, len);
918 }
919 
920 static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
921 {
922 	__be32 *p;
923 
924 	p = reserve_space(xdr, 4 + len);
925 	xdr_encode_opaque(p, str, len);
926 }
927 
928 static void encode_uint32(struct xdr_stream *xdr, u32 n)
929 {
930 	__be32 *p;
931 
932 	p = reserve_space(xdr, 4);
933 	*p = cpu_to_be32(n);
934 }
935 
936 static void encode_uint64(struct xdr_stream *xdr, u64 n)
937 {
938 	__be32 *p;
939 
940 	p = reserve_space(xdr, 8);
941 	xdr_encode_hyper(p, n);
942 }
943 
944 static void encode_nfs4_seqid(struct xdr_stream *xdr,
945 		const struct nfs_seqid *seqid)
946 {
947 	encode_uint32(xdr, seqid->sequence->counter);
948 }
949 
950 static void encode_compound_hdr(struct xdr_stream *xdr,
951 				struct rpc_rqst *req,
952 				struct compound_hdr *hdr)
953 {
954 	__be32 *p;
955 	struct rpc_auth *auth = req->rq_cred->cr_auth;
956 
957 	/* initialize running count of expected bytes in reply.
958 	 * NOTE: the replied tag SHOULD be the same is the one sent,
959 	 * but this is not required as a MUST for the server to do so. */
960 	hdr->replen = RPC_REPHDRSIZE + auth->au_rslack + 3 + hdr->taglen;
961 
962 	WARN_ON_ONCE(hdr->taglen > NFS4_MAXTAGLEN);
963 	encode_string(xdr, hdr->taglen, hdr->tag);
964 	p = reserve_space(xdr, 8);
965 	*p++ = cpu_to_be32(hdr->minorversion);
966 	hdr->nops_p = p;
967 	*p = cpu_to_be32(hdr->nops);
968 }
969 
970 static void encode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 op,
971 		uint32_t replen,
972 		struct compound_hdr *hdr)
973 {
974 	encode_uint32(xdr, op);
975 	hdr->nops++;
976 	hdr->replen += replen;
977 }
978 
979 static void encode_nops(struct compound_hdr *hdr)
980 {
981 	WARN_ON_ONCE(hdr->nops > NFS4_MAX_OPS);
982 	*hdr->nops_p = htonl(hdr->nops);
983 }
984 
985 static void encode_nfs4_stateid(struct xdr_stream *xdr, const nfs4_stateid *stateid)
986 {
987 	encode_opaque_fixed(xdr, stateid, NFS4_STATEID_SIZE);
988 }
989 
990 static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
991 {
992 	encode_opaque_fixed(xdr, verf->data, NFS4_VERIFIER_SIZE);
993 }
994 
995 static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap,
996 				const struct nfs4_label *label,
997 				const struct nfs_server *server)
998 {
999 	char owner_name[IDMAP_NAMESZ];
1000 	char owner_group[IDMAP_NAMESZ];
1001 	int owner_namelen = 0;
1002 	int owner_grouplen = 0;
1003 	__be32 *p;
1004 	unsigned i;
1005 	uint32_t len = 0;
1006 	uint32_t bmval_len;
1007 	uint32_t bmval[3] = { 0 };
1008 
1009 	/*
1010 	 * We reserve enough space to write the entire attribute buffer at once.
1011 	 * In the worst-case, this would be
1012 	 * 16(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
1013 	 * = 40 bytes, plus any contribution from variable-length fields
1014 	 *            such as owner/group.
1015 	 */
1016 	if (iap->ia_valid & ATTR_SIZE) {
1017 		bmval[0] |= FATTR4_WORD0_SIZE;
1018 		len += 8;
1019 	}
1020 	if (iap->ia_valid & ATTR_MODE) {
1021 		bmval[1] |= FATTR4_WORD1_MODE;
1022 		len += 4;
1023 	}
1024 	if (iap->ia_valid & ATTR_UID) {
1025 		owner_namelen = nfs_map_uid_to_name(server, iap->ia_uid, owner_name, IDMAP_NAMESZ);
1026 		if (owner_namelen < 0) {
1027 			dprintk("nfs: couldn't resolve uid %d to string\n",
1028 					from_kuid(&init_user_ns, iap->ia_uid));
1029 			/* XXX */
1030 			strcpy(owner_name, "nobody");
1031 			owner_namelen = sizeof("nobody") - 1;
1032 			/* goto out; */
1033 		}
1034 		bmval[1] |= FATTR4_WORD1_OWNER;
1035 		len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
1036 	}
1037 	if (iap->ia_valid & ATTR_GID) {
1038 		owner_grouplen = nfs_map_gid_to_group(server, iap->ia_gid, owner_group, IDMAP_NAMESZ);
1039 		if (owner_grouplen < 0) {
1040 			dprintk("nfs: couldn't resolve gid %d to string\n",
1041 					from_kgid(&init_user_ns, iap->ia_gid));
1042 			strcpy(owner_group, "nobody");
1043 			owner_grouplen = sizeof("nobody") - 1;
1044 			/* goto out; */
1045 		}
1046 		bmval[1] |= FATTR4_WORD1_OWNER_GROUP;
1047 		len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
1048 	}
1049 	if (iap->ia_valid & ATTR_ATIME_SET) {
1050 		bmval[1] |= FATTR4_WORD1_TIME_ACCESS_SET;
1051 		len += 16;
1052 	} else if (iap->ia_valid & ATTR_ATIME) {
1053 		bmval[1] |= FATTR4_WORD1_TIME_ACCESS_SET;
1054 		len += 4;
1055 	}
1056 	if (iap->ia_valid & ATTR_MTIME_SET) {
1057 		bmval[1] |= FATTR4_WORD1_TIME_MODIFY_SET;
1058 		len += 16;
1059 	} else if (iap->ia_valid & ATTR_MTIME) {
1060 		bmval[1] |= FATTR4_WORD1_TIME_MODIFY_SET;
1061 		len += 4;
1062 	}
1063 	if (label) {
1064 		len += 4 + 4 + 4 + (XDR_QUADLEN(label->len) << 2);
1065 		bmval[2] |= FATTR4_WORD2_SECURITY_LABEL;
1066 	}
1067 
1068 	if (bmval[2] != 0)
1069 		bmval_len = 3;
1070 	else if (bmval[1] != 0)
1071 		bmval_len = 2;
1072 	else
1073 		bmval_len = 1;
1074 
1075 	p = reserve_space(xdr, 4 + (bmval_len << 2) + 4 + len);
1076 
1077 	*p++ = cpu_to_be32(bmval_len);
1078 	for (i = 0; i < bmval_len; i++)
1079 		*p++ = cpu_to_be32(bmval[i]);
1080 	*p++ = cpu_to_be32(len);
1081 
1082 	if (bmval[0] & FATTR4_WORD0_SIZE)
1083 		p = xdr_encode_hyper(p, iap->ia_size);
1084 	if (bmval[1] & FATTR4_WORD1_MODE)
1085 		*p++ = cpu_to_be32(iap->ia_mode & S_IALLUGO);
1086 	if (bmval[1] & FATTR4_WORD1_OWNER)
1087 		p = xdr_encode_opaque(p, owner_name, owner_namelen);
1088 	if (bmval[1] & FATTR4_WORD1_OWNER_GROUP)
1089 		p = xdr_encode_opaque(p, owner_group, owner_grouplen);
1090 	if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
1091 		if (iap->ia_valid & ATTR_ATIME_SET) {
1092 			*p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
1093 			p = xdr_encode_hyper(p, (s64)iap->ia_atime.tv_sec);
1094 			*p++ = cpu_to_be32(iap->ia_atime.tv_nsec);
1095 		} else
1096 			*p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
1097 	}
1098 	if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
1099 		if (iap->ia_valid & ATTR_MTIME_SET) {
1100 			*p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
1101 			p = xdr_encode_hyper(p, (s64)iap->ia_mtime.tv_sec);
1102 			*p++ = cpu_to_be32(iap->ia_mtime.tv_nsec);
1103 		} else
1104 			*p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
1105 	}
1106 	if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
1107 		*p++ = cpu_to_be32(label->lfs);
1108 		*p++ = cpu_to_be32(label->pi);
1109 		*p++ = cpu_to_be32(label->len);
1110 		p = xdr_encode_opaque_fixed(p, label->label, label->len);
1111 	}
1112 
1113 /* out: */
1114 }
1115 
1116 static void encode_access(struct xdr_stream *xdr, u32 access, struct compound_hdr *hdr)
1117 {
1118 	encode_op_hdr(xdr, OP_ACCESS, decode_access_maxsz, hdr);
1119 	encode_uint32(xdr, access);
1120 }
1121 
1122 static void encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1123 {
1124 	encode_op_hdr(xdr, OP_CLOSE, decode_close_maxsz, hdr);
1125 	encode_nfs4_seqid(xdr, arg->seqid);
1126 	encode_nfs4_stateid(xdr, arg->stateid);
1127 }
1128 
1129 static void encode_commit(struct xdr_stream *xdr, const struct nfs_commitargs *args, struct compound_hdr *hdr)
1130 {
1131 	__be32 *p;
1132 
1133 	encode_op_hdr(xdr, OP_COMMIT, decode_commit_maxsz, hdr);
1134 	p = reserve_space(xdr, 12);
1135 	p = xdr_encode_hyper(p, args->offset);
1136 	*p = cpu_to_be32(args->count);
1137 }
1138 
1139 static void encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create, struct compound_hdr *hdr)
1140 {
1141 	__be32 *p;
1142 
1143 	encode_op_hdr(xdr, OP_CREATE, decode_create_maxsz, hdr);
1144 	encode_uint32(xdr, create->ftype);
1145 
1146 	switch (create->ftype) {
1147 	case NF4LNK:
1148 		p = reserve_space(xdr, 4);
1149 		*p = cpu_to_be32(create->u.symlink.len);
1150 		xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
1151 		break;
1152 
1153 	case NF4BLK: case NF4CHR:
1154 		p = reserve_space(xdr, 8);
1155 		*p++ = cpu_to_be32(create->u.device.specdata1);
1156 		*p = cpu_to_be32(create->u.device.specdata2);
1157 		break;
1158 
1159 	default:
1160 		break;
1161 	}
1162 
1163 	encode_string(xdr, create->name->len, create->name->name);
1164 	encode_attrs(xdr, create->attrs, create->label, create->server);
1165 }
1166 
1167 static void encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap, struct compound_hdr *hdr)
1168 {
1169 	__be32 *p;
1170 
1171 	encode_op_hdr(xdr, OP_GETATTR, decode_getattr_maxsz, hdr);
1172 	p = reserve_space(xdr, 8);
1173 	*p++ = cpu_to_be32(1);
1174 	*p = cpu_to_be32(bitmap);
1175 }
1176 
1177 static void encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1, struct compound_hdr *hdr)
1178 {
1179 	__be32 *p;
1180 
1181 	encode_op_hdr(xdr, OP_GETATTR, decode_getattr_maxsz, hdr);
1182 	p = reserve_space(xdr, 12);
1183 	*p++ = cpu_to_be32(2);
1184 	*p++ = cpu_to_be32(bm0);
1185 	*p = cpu_to_be32(bm1);
1186 }
1187 
1188 static void
1189 encode_getattr_three(struct xdr_stream *xdr,
1190 		     uint32_t bm0, uint32_t bm1, uint32_t bm2,
1191 		     struct compound_hdr *hdr)
1192 {
1193 	__be32 *p;
1194 
1195 	encode_op_hdr(xdr, OP_GETATTR, decode_getattr_maxsz, hdr);
1196 	if (bm2) {
1197 		p = reserve_space(xdr, 16);
1198 		*p++ = cpu_to_be32(3);
1199 		*p++ = cpu_to_be32(bm0);
1200 		*p++ = cpu_to_be32(bm1);
1201 		*p = cpu_to_be32(bm2);
1202 	} else if (bm1) {
1203 		p = reserve_space(xdr, 12);
1204 		*p++ = cpu_to_be32(2);
1205 		*p++ = cpu_to_be32(bm0);
1206 		*p = cpu_to_be32(bm1);
1207 	} else {
1208 		p = reserve_space(xdr, 8);
1209 		*p++ = cpu_to_be32(1);
1210 		*p = cpu_to_be32(bm0);
1211 	}
1212 }
1213 
1214 static void encode_getfattr(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1215 {
1216 	encode_getattr_three(xdr, bitmask[0] & nfs4_fattr_bitmap[0],
1217 			   bitmask[1] & nfs4_fattr_bitmap[1],
1218 			   bitmask[2] & nfs4_fattr_bitmap[2],
1219 			   hdr);
1220 }
1221 
1222 static void encode_getfattr_open(struct xdr_stream *xdr, const u32 *bitmask,
1223 				 const u32 *open_bitmap,
1224 				 struct compound_hdr *hdr)
1225 {
1226 	encode_getattr_three(xdr,
1227 			     bitmask[0] & open_bitmap[0],
1228 			     bitmask[1] & open_bitmap[1],
1229 			     bitmask[2] & open_bitmap[2],
1230 			     hdr);
1231 }
1232 
1233 static void encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1234 {
1235 	encode_getattr_three(xdr,
1236 			     bitmask[0] & nfs4_fsinfo_bitmap[0],
1237 			     bitmask[1] & nfs4_fsinfo_bitmap[1],
1238 			     bitmask[2] & nfs4_fsinfo_bitmap[2],
1239 			     hdr);
1240 }
1241 
1242 static void encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1243 {
1244 	encode_getattr_two(xdr, bitmask[0] & nfs4_fs_locations_bitmap[0],
1245 			   bitmask[1] & nfs4_fs_locations_bitmap[1], hdr);
1246 }
1247 
1248 static void encode_getfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1249 {
1250 	encode_op_hdr(xdr, OP_GETFH, decode_getfh_maxsz, hdr);
1251 }
1252 
1253 static void encode_link(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1254 {
1255 	encode_op_hdr(xdr, OP_LINK, decode_link_maxsz, hdr);
1256 	encode_string(xdr, name->len, name->name);
1257 }
1258 
1259 static inline int nfs4_lock_type(struct file_lock *fl, int block)
1260 {
1261 	if (fl->fl_type == F_RDLCK)
1262 		return block ? NFS4_READW_LT : NFS4_READ_LT;
1263 	return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
1264 }
1265 
1266 static inline uint64_t nfs4_lock_length(struct file_lock *fl)
1267 {
1268 	if (fl->fl_end == OFFSET_MAX)
1269 		return ~(uint64_t)0;
1270 	return fl->fl_end - fl->fl_start + 1;
1271 }
1272 
1273 static void encode_lockowner(struct xdr_stream *xdr, const struct nfs_lowner *lowner)
1274 {
1275 	__be32 *p;
1276 
1277 	p = reserve_space(xdr, 32);
1278 	p = xdr_encode_hyper(p, lowner->clientid);
1279 	*p++ = cpu_to_be32(20);
1280 	p = xdr_encode_opaque_fixed(p, "lock id:", 8);
1281 	*p++ = cpu_to_be32(lowner->s_dev);
1282 	xdr_encode_hyper(p, lowner->id);
1283 }
1284 
1285 /*
1286  * opcode,type,reclaim,offset,length,new_lock_owner = 32
1287  * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
1288  */
1289 static void encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args, struct compound_hdr *hdr)
1290 {
1291 	__be32 *p;
1292 
1293 	encode_op_hdr(xdr, OP_LOCK, decode_lock_maxsz, hdr);
1294 	p = reserve_space(xdr, 28);
1295 	*p++ = cpu_to_be32(nfs4_lock_type(args->fl, args->block));
1296 	*p++ = cpu_to_be32(args->reclaim);
1297 	p = xdr_encode_hyper(p, args->fl->fl_start);
1298 	p = xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1299 	*p = cpu_to_be32(args->new_lock_owner);
1300 	if (args->new_lock_owner){
1301 		encode_nfs4_seqid(xdr, args->open_seqid);
1302 		encode_nfs4_stateid(xdr, args->open_stateid);
1303 		encode_nfs4_seqid(xdr, args->lock_seqid);
1304 		encode_lockowner(xdr, &args->lock_owner);
1305 	}
1306 	else {
1307 		encode_nfs4_stateid(xdr, args->lock_stateid);
1308 		encode_nfs4_seqid(xdr, args->lock_seqid);
1309 	}
1310 }
1311 
1312 static void encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args, struct compound_hdr *hdr)
1313 {
1314 	__be32 *p;
1315 
1316 	encode_op_hdr(xdr, OP_LOCKT, decode_lockt_maxsz, hdr);
1317 	p = reserve_space(xdr, 20);
1318 	*p++ = cpu_to_be32(nfs4_lock_type(args->fl, 0));
1319 	p = xdr_encode_hyper(p, args->fl->fl_start);
1320 	p = xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1321 	encode_lockowner(xdr, &args->lock_owner);
1322 }
1323 
1324 static void encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args, struct compound_hdr *hdr)
1325 {
1326 	__be32 *p;
1327 
1328 	encode_op_hdr(xdr, OP_LOCKU, decode_locku_maxsz, hdr);
1329 	encode_uint32(xdr, nfs4_lock_type(args->fl, 0));
1330 	encode_nfs4_seqid(xdr, args->seqid);
1331 	encode_nfs4_stateid(xdr, args->stateid);
1332 	p = reserve_space(xdr, 16);
1333 	p = xdr_encode_hyper(p, args->fl->fl_start);
1334 	xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1335 }
1336 
1337 static void encode_release_lockowner(struct xdr_stream *xdr, const struct nfs_lowner *lowner, struct compound_hdr *hdr)
1338 {
1339 	encode_op_hdr(xdr, OP_RELEASE_LOCKOWNER, decode_release_lockowner_maxsz, hdr);
1340 	encode_lockowner(xdr, lowner);
1341 }
1342 
1343 static void encode_lookup(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1344 {
1345 	encode_op_hdr(xdr, OP_LOOKUP, decode_lookup_maxsz, hdr);
1346 	encode_string(xdr, name->len, name->name);
1347 }
1348 
1349 static void encode_share_access(struct xdr_stream *xdr, fmode_t fmode)
1350 {
1351 	__be32 *p;
1352 
1353 	p = reserve_space(xdr, 8);
1354 	switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1355 	case FMODE_READ:
1356 		*p++ = cpu_to_be32(NFS4_SHARE_ACCESS_READ);
1357 		break;
1358 	case FMODE_WRITE:
1359 		*p++ = cpu_to_be32(NFS4_SHARE_ACCESS_WRITE);
1360 		break;
1361 	case FMODE_READ|FMODE_WRITE:
1362 		*p++ = cpu_to_be32(NFS4_SHARE_ACCESS_BOTH);
1363 		break;
1364 	default:
1365 		*p++ = cpu_to_be32(0);
1366 	}
1367 	*p = cpu_to_be32(0);		/* for linux, share_deny = 0 always */
1368 }
1369 
1370 static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1371 {
1372 	__be32 *p;
1373  /*
1374  * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
1375  * owner 4 = 32
1376  */
1377 	encode_nfs4_seqid(xdr, arg->seqid);
1378 	encode_share_access(xdr, arg->fmode);
1379 	p = reserve_space(xdr, 36);
1380 	p = xdr_encode_hyper(p, arg->clientid);
1381 	*p++ = cpu_to_be32(24);
1382 	p = xdr_encode_opaque_fixed(p, "open id:", 8);
1383 	*p++ = cpu_to_be32(arg->server->s_dev);
1384 	*p++ = cpu_to_be32(arg->id.uniquifier);
1385 	xdr_encode_hyper(p, arg->id.create_time);
1386 }
1387 
1388 static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1389 {
1390 	struct iattr dummy;
1391 	__be32 *p;
1392 
1393 	p = reserve_space(xdr, 4);
1394 	switch(arg->createmode) {
1395 	case NFS4_CREATE_UNCHECKED:
1396 		*p = cpu_to_be32(NFS4_CREATE_UNCHECKED);
1397 		encode_attrs(xdr, arg->u.attrs, arg->label, arg->server);
1398 		break;
1399 	case NFS4_CREATE_GUARDED:
1400 		*p = cpu_to_be32(NFS4_CREATE_GUARDED);
1401 		encode_attrs(xdr, arg->u.attrs, arg->label, arg->server);
1402 		break;
1403 	case NFS4_CREATE_EXCLUSIVE:
1404 		*p = cpu_to_be32(NFS4_CREATE_EXCLUSIVE);
1405 		encode_nfs4_verifier(xdr, &arg->u.verifier);
1406 		break;
1407 	case NFS4_CREATE_EXCLUSIVE4_1:
1408 		*p = cpu_to_be32(NFS4_CREATE_EXCLUSIVE4_1);
1409 		encode_nfs4_verifier(xdr, &arg->u.verifier);
1410 		dummy.ia_valid = 0;
1411 		encode_attrs(xdr, &dummy, arg->label, arg->server);
1412 	}
1413 }
1414 
1415 static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1416 {
1417 	__be32 *p;
1418 
1419 	p = reserve_space(xdr, 4);
1420 	switch (arg->open_flags & O_CREAT) {
1421 	case 0:
1422 		*p = cpu_to_be32(NFS4_OPEN_NOCREATE);
1423 		break;
1424 	default:
1425 		*p = cpu_to_be32(NFS4_OPEN_CREATE);
1426 		encode_createmode(xdr, arg);
1427 	}
1428 }
1429 
1430 static inline void encode_delegation_type(struct xdr_stream *xdr, fmode_t delegation_type)
1431 {
1432 	__be32 *p;
1433 
1434 	p = reserve_space(xdr, 4);
1435 	switch (delegation_type) {
1436 	case 0:
1437 		*p = cpu_to_be32(NFS4_OPEN_DELEGATE_NONE);
1438 		break;
1439 	case FMODE_READ:
1440 		*p = cpu_to_be32(NFS4_OPEN_DELEGATE_READ);
1441 		break;
1442 	case FMODE_WRITE|FMODE_READ:
1443 		*p = cpu_to_be32(NFS4_OPEN_DELEGATE_WRITE);
1444 		break;
1445 	default:
1446 		BUG();
1447 	}
1448 }
1449 
1450 static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
1451 {
1452 	__be32 *p;
1453 
1454 	p = reserve_space(xdr, 4);
1455 	*p = cpu_to_be32(NFS4_OPEN_CLAIM_NULL);
1456 	encode_string(xdr, name->len, name->name);
1457 }
1458 
1459 static inline void encode_claim_previous(struct xdr_stream *xdr, fmode_t type)
1460 {
1461 	__be32 *p;
1462 
1463 	p = reserve_space(xdr, 4);
1464 	*p = cpu_to_be32(NFS4_OPEN_CLAIM_PREVIOUS);
1465 	encode_delegation_type(xdr, type);
1466 }
1467 
1468 static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
1469 {
1470 	__be32 *p;
1471 
1472 	p = reserve_space(xdr, 4);
1473 	*p = cpu_to_be32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
1474 	encode_nfs4_stateid(xdr, stateid);
1475 	encode_string(xdr, name->len, name->name);
1476 }
1477 
1478 static inline void encode_claim_fh(struct xdr_stream *xdr)
1479 {
1480 	__be32 *p;
1481 
1482 	p = reserve_space(xdr, 4);
1483 	*p = cpu_to_be32(NFS4_OPEN_CLAIM_FH);
1484 }
1485 
1486 static inline void encode_claim_delegate_cur_fh(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1487 {
1488 	__be32 *p;
1489 
1490 	p = reserve_space(xdr, 4);
1491 	*p = cpu_to_be32(NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1492 	encode_nfs4_stateid(xdr, stateid);
1493 }
1494 
1495 static void encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg, struct compound_hdr *hdr)
1496 {
1497 	encode_op_hdr(xdr, OP_OPEN, decode_open_maxsz, hdr);
1498 	encode_openhdr(xdr, arg);
1499 	encode_opentype(xdr, arg);
1500 	switch (arg->claim) {
1501 	case NFS4_OPEN_CLAIM_NULL:
1502 		encode_claim_null(xdr, arg->name);
1503 		break;
1504 	case NFS4_OPEN_CLAIM_PREVIOUS:
1505 		encode_claim_previous(xdr, arg->u.delegation_type);
1506 		break;
1507 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1508 		encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
1509 		break;
1510 	case NFS4_OPEN_CLAIM_FH:
1511 		encode_claim_fh(xdr);
1512 		break;
1513 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1514 		encode_claim_delegate_cur_fh(xdr, &arg->u.delegation);
1515 		break;
1516 	default:
1517 		BUG();
1518 	}
1519 }
1520 
1521 static void encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg, struct compound_hdr *hdr)
1522 {
1523 	encode_op_hdr(xdr, OP_OPEN_CONFIRM, decode_open_confirm_maxsz, hdr);
1524 	encode_nfs4_stateid(xdr, arg->stateid);
1525 	encode_nfs4_seqid(xdr, arg->seqid);
1526 }
1527 
1528 static void encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1529 {
1530 	encode_op_hdr(xdr, OP_OPEN_DOWNGRADE, decode_open_downgrade_maxsz, hdr);
1531 	encode_nfs4_stateid(xdr, arg->stateid);
1532 	encode_nfs4_seqid(xdr, arg->seqid);
1533 	encode_share_access(xdr, arg->fmode);
1534 }
1535 
1536 static void
1537 encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh, struct compound_hdr *hdr)
1538 {
1539 	encode_op_hdr(xdr, OP_PUTFH, decode_putfh_maxsz, hdr);
1540 	encode_string(xdr, fh->size, fh->data);
1541 }
1542 
1543 static void encode_putrootfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1544 {
1545 	encode_op_hdr(xdr, OP_PUTROOTFH, decode_putrootfh_maxsz, hdr);
1546 }
1547 
1548 static void encode_read(struct xdr_stream *xdr, const struct nfs_pgio_args *args,
1549 			struct compound_hdr *hdr)
1550 {
1551 	__be32 *p;
1552 
1553 	encode_op_hdr(xdr, OP_READ, decode_read_maxsz, hdr);
1554 	encode_nfs4_stateid(xdr, &args->stateid);
1555 
1556 	p = reserve_space(xdr, 12);
1557 	p = xdr_encode_hyper(p, args->offset);
1558 	*p = cpu_to_be32(args->count);
1559 }
1560 
1561 static void encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req, struct compound_hdr *hdr)
1562 {
1563 	uint32_t attrs[3] = {
1564 		FATTR4_WORD0_RDATTR_ERROR,
1565 		FATTR4_WORD1_MOUNTED_ON_FILEID,
1566 	};
1567 	uint32_t dircount = readdir->count >> 1;
1568 	__be32 *p, verf[2];
1569 	uint32_t attrlen = 0;
1570 	unsigned int i;
1571 
1572 	if (readdir->plus) {
1573 		attrs[0] |= FATTR4_WORD0_TYPE|FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE|
1574 			FATTR4_WORD0_FSID|FATTR4_WORD0_FILEHANDLE|FATTR4_WORD0_FILEID;
1575 		attrs[1] |= FATTR4_WORD1_MODE|FATTR4_WORD1_NUMLINKS|FATTR4_WORD1_OWNER|
1576 			FATTR4_WORD1_OWNER_GROUP|FATTR4_WORD1_RAWDEV|
1577 			FATTR4_WORD1_SPACE_USED|FATTR4_WORD1_TIME_ACCESS|
1578 			FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1579 		attrs[2] |= FATTR4_WORD2_SECURITY_LABEL;
1580 		dircount >>= 1;
1581 	}
1582 	/* Use mounted_on_fileid only if the server supports it */
1583 	if (!(readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID))
1584 		attrs[0] |= FATTR4_WORD0_FILEID;
1585 	for (i = 0; i < ARRAY_SIZE(attrs); i++) {
1586 		attrs[i] &= readdir->bitmask[i];
1587 		if (attrs[i] != 0)
1588 			attrlen = i+1;
1589 	}
1590 
1591 	encode_op_hdr(xdr, OP_READDIR, decode_readdir_maxsz, hdr);
1592 	encode_uint64(xdr, readdir->cookie);
1593 	encode_nfs4_verifier(xdr, &readdir->verifier);
1594 	p = reserve_space(xdr, 12 + (attrlen << 2));
1595 	*p++ = cpu_to_be32(dircount);
1596 	*p++ = cpu_to_be32(readdir->count);
1597 	*p++ = cpu_to_be32(attrlen);
1598 	for (i = 0; i < attrlen; i++)
1599 		*p++ = cpu_to_be32(attrs[i]);
1600 	memcpy(verf, readdir->verifier.data, sizeof(verf));
1601 
1602 	dprintk("%s: cookie = %llu, verifier = %08x:%08x, bitmap = %08x:%08x:%08x\n",
1603 			__func__,
1604 			(unsigned long long)readdir->cookie,
1605 			verf[0], verf[1],
1606 			attrs[0] & readdir->bitmask[0],
1607 			attrs[1] & readdir->bitmask[1],
1608 			attrs[2] & readdir->bitmask[2]);
1609 }
1610 
1611 static void encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req, struct compound_hdr *hdr)
1612 {
1613 	encode_op_hdr(xdr, OP_READLINK, decode_readlink_maxsz, hdr);
1614 }
1615 
1616 static void encode_remove(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1617 {
1618 	encode_op_hdr(xdr, OP_REMOVE, decode_remove_maxsz, hdr);
1619 	encode_string(xdr, name->len, name->name);
1620 }
1621 
1622 static void encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname, struct compound_hdr *hdr)
1623 {
1624 	encode_op_hdr(xdr, OP_RENAME, decode_rename_maxsz, hdr);
1625 	encode_string(xdr, oldname->len, oldname->name);
1626 	encode_string(xdr, newname->len, newname->name);
1627 }
1628 
1629 static void encode_renew(struct xdr_stream *xdr, clientid4 clid,
1630 			 struct compound_hdr *hdr)
1631 {
1632 	encode_op_hdr(xdr, OP_RENEW, decode_renew_maxsz, hdr);
1633 	encode_uint64(xdr, clid);
1634 }
1635 
1636 static void
1637 encode_restorefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1638 {
1639 	encode_op_hdr(xdr, OP_RESTOREFH, decode_restorefh_maxsz, hdr);
1640 }
1641 
1642 static void
1643 encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg, struct compound_hdr *hdr)
1644 {
1645 	__be32 *p;
1646 
1647 	encode_op_hdr(xdr, OP_SETATTR, decode_setacl_maxsz, hdr);
1648 	encode_nfs4_stateid(xdr, &zero_stateid);
1649 	p = reserve_space(xdr, 2*4);
1650 	*p++ = cpu_to_be32(1);
1651 	*p = cpu_to_be32(FATTR4_WORD0_ACL);
1652 	p = reserve_space(xdr, 4);
1653 	*p = cpu_to_be32(arg->acl_len);
1654 	xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1655 }
1656 
1657 static void
1658 encode_savefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1659 {
1660 	encode_op_hdr(xdr, OP_SAVEFH, decode_savefh_maxsz, hdr);
1661 }
1662 
1663 static void encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server, struct compound_hdr *hdr)
1664 {
1665 	encode_op_hdr(xdr, OP_SETATTR, decode_setattr_maxsz, hdr);
1666 	encode_nfs4_stateid(xdr, &arg->stateid);
1667 	encode_attrs(xdr, arg->iap, arg->label, server);
1668 }
1669 
1670 static void encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid, struct compound_hdr *hdr)
1671 {
1672 	__be32 *p;
1673 
1674 	encode_op_hdr(xdr, OP_SETCLIENTID, decode_setclientid_maxsz, hdr);
1675 	encode_nfs4_verifier(xdr, setclientid->sc_verifier);
1676 
1677 	encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1678 	p = reserve_space(xdr, 4);
1679 	*p = cpu_to_be32(setclientid->sc_prog);
1680 	encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1681 	encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1682 	p = reserve_space(xdr, 4);
1683 	*p = cpu_to_be32(setclientid->sc_cb_ident);
1684 }
1685 
1686 static void encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_setclientid_res *arg, struct compound_hdr *hdr)
1687 {
1688 	encode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM,
1689 			decode_setclientid_confirm_maxsz, hdr);
1690 	encode_uint64(xdr, arg->clientid);
1691 	encode_nfs4_verifier(xdr, &arg->confirm);
1692 }
1693 
1694 static void encode_write(struct xdr_stream *xdr, const struct nfs_pgio_args *args,
1695 			 struct compound_hdr *hdr)
1696 {
1697 	__be32 *p;
1698 
1699 	encode_op_hdr(xdr, OP_WRITE, decode_write_maxsz, hdr);
1700 	encode_nfs4_stateid(xdr, &args->stateid);
1701 
1702 	p = reserve_space(xdr, 16);
1703 	p = xdr_encode_hyper(p, args->offset);
1704 	*p++ = cpu_to_be32(args->stable);
1705 	*p = cpu_to_be32(args->count);
1706 
1707 	xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1708 }
1709 
1710 static void encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid, struct compound_hdr *hdr)
1711 {
1712 	encode_op_hdr(xdr, OP_DELEGRETURN, decode_delegreturn_maxsz, hdr);
1713 	encode_nfs4_stateid(xdr, stateid);
1714 }
1715 
1716 static void encode_secinfo(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1717 {
1718 	encode_op_hdr(xdr, OP_SECINFO, decode_secinfo_maxsz, hdr);
1719 	encode_string(xdr, name->len, name->name);
1720 }
1721 
1722 #if defined(CONFIG_NFS_V4_1)
1723 /* NFSv4.1 operations */
1724 static void encode_bind_conn_to_session(struct xdr_stream *xdr,
1725 				   struct nfs4_session *session,
1726 				   struct compound_hdr *hdr)
1727 {
1728 	__be32 *p;
1729 
1730 	encode_op_hdr(xdr, OP_BIND_CONN_TO_SESSION,
1731 		decode_bind_conn_to_session_maxsz, hdr);
1732 	encode_opaque_fixed(xdr, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1733 	p = xdr_reserve_space(xdr, 8);
1734 	*p++ = cpu_to_be32(NFS4_CDFC4_BACK_OR_BOTH);
1735 	*p = 0;	/* use_conn_in_rdma_mode = False */
1736 }
1737 
1738 static void encode_op_map(struct xdr_stream *xdr, struct nfs4_op_map *op_map)
1739 {
1740 	unsigned int i;
1741 	encode_uint32(xdr, NFS4_OP_MAP_NUM_WORDS);
1742 	for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++)
1743 		encode_uint32(xdr, op_map->u.words[i]);
1744 }
1745 
1746 static void encode_exchange_id(struct xdr_stream *xdr,
1747 			       struct nfs41_exchange_id_args *args,
1748 			       struct compound_hdr *hdr)
1749 {
1750 	__be32 *p;
1751 	char impl_name[IMPL_NAME_LIMIT];
1752 	int len = 0;
1753 
1754 	encode_op_hdr(xdr, OP_EXCHANGE_ID, decode_exchange_id_maxsz, hdr);
1755 	encode_nfs4_verifier(xdr, args->verifier);
1756 
1757 	encode_string(xdr, args->id_len, args->id);
1758 
1759 	encode_uint32(xdr, args->flags);
1760 	encode_uint32(xdr, args->state_protect.how);
1761 
1762 	switch (args->state_protect.how) {
1763 	case SP4_NONE:
1764 		break;
1765 	case SP4_MACH_CRED:
1766 		encode_op_map(xdr, &args->state_protect.enforce);
1767 		encode_op_map(xdr, &args->state_protect.allow);
1768 		break;
1769 	default:
1770 		WARN_ON_ONCE(1);
1771 		break;
1772 	}
1773 
1774 	if (send_implementation_id &&
1775 	    sizeof(CONFIG_NFS_V4_1_IMPLEMENTATION_ID_DOMAIN) > 1 &&
1776 	    sizeof(CONFIG_NFS_V4_1_IMPLEMENTATION_ID_DOMAIN)
1777 		<= sizeof(impl_name) + 1)
1778 		len = snprintf(impl_name, sizeof(impl_name), "%s %s %s %s",
1779 			       utsname()->sysname, utsname()->release,
1780 			       utsname()->version, utsname()->machine);
1781 
1782 	if (len > 0) {
1783 		encode_uint32(xdr, 1);	/* implementation id array length=1 */
1784 
1785 		encode_string(xdr,
1786 			sizeof(CONFIG_NFS_V4_1_IMPLEMENTATION_ID_DOMAIN) - 1,
1787 			CONFIG_NFS_V4_1_IMPLEMENTATION_ID_DOMAIN);
1788 		encode_string(xdr, len, impl_name);
1789 		/* just send zeros for nii_date - the date is in nii_name */
1790 		p = reserve_space(xdr, 12);
1791 		p = xdr_encode_hyper(p, 0);
1792 		*p = cpu_to_be32(0);
1793 	} else
1794 		encode_uint32(xdr, 0);	/* implementation id array length=0 */
1795 }
1796 
1797 static void encode_create_session(struct xdr_stream *xdr,
1798 				  struct nfs41_create_session_args *args,
1799 				  struct compound_hdr *hdr)
1800 {
1801 	__be32 *p;
1802 	char machine_name[NFS4_MAX_MACHINE_NAME_LEN];
1803 	uint32_t len;
1804 	struct nfs_client *clp = args->client;
1805 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
1806 	u32 max_resp_sz_cached;
1807 
1808 	/*
1809 	 * Assumes OPEN is the biggest non-idempotent compound.
1810 	 * 2 is the verifier.
1811 	 */
1812 	max_resp_sz_cached = (NFS4_dec_open_sz + RPC_REPHDRSIZE +
1813 			      RPC_MAX_AUTH_SIZE + 2) * XDR_UNIT;
1814 
1815 	len = scnprintf(machine_name, sizeof(machine_name), "%s",
1816 			clp->cl_ipaddr);
1817 
1818 	encode_op_hdr(xdr, OP_CREATE_SESSION, decode_create_session_maxsz, hdr);
1819 	p = reserve_space(xdr, 16 + 2*28 + 20 + len + 12);
1820 	p = xdr_encode_hyper(p, clp->cl_clientid);
1821 	*p++ = cpu_to_be32(clp->cl_seqid);			/*Sequence id */
1822 	*p++ = cpu_to_be32(args->flags);			/*flags */
1823 
1824 	/* Fore Channel */
1825 	*p++ = cpu_to_be32(0);				/* header padding size */
1826 	*p++ = cpu_to_be32(args->fc_attrs.max_rqst_sz);	/* max req size */
1827 	*p++ = cpu_to_be32(args->fc_attrs.max_resp_sz);	/* max resp size */
1828 	*p++ = cpu_to_be32(max_resp_sz_cached);		/* Max resp sz cached */
1829 	*p++ = cpu_to_be32(args->fc_attrs.max_ops);	/* max operations */
1830 	*p++ = cpu_to_be32(args->fc_attrs.max_reqs);	/* max requests */
1831 	*p++ = cpu_to_be32(0);				/* rdmachannel_attrs */
1832 
1833 	/* Back Channel */
1834 	*p++ = cpu_to_be32(0);				/* header padding size */
1835 	*p++ = cpu_to_be32(args->bc_attrs.max_rqst_sz);	/* max req size */
1836 	*p++ = cpu_to_be32(args->bc_attrs.max_resp_sz);	/* max resp size */
1837 	*p++ = cpu_to_be32(args->bc_attrs.max_resp_sz_cached);	/* Max resp sz cached */
1838 	*p++ = cpu_to_be32(args->bc_attrs.max_ops);	/* max operations */
1839 	*p++ = cpu_to_be32(args->bc_attrs.max_reqs);	/* max requests */
1840 	*p++ = cpu_to_be32(0);				/* rdmachannel_attrs */
1841 
1842 	*p++ = cpu_to_be32(args->cb_program);		/* cb_program */
1843 	*p++ = cpu_to_be32(1);
1844 	*p++ = cpu_to_be32(RPC_AUTH_UNIX);			/* auth_sys */
1845 
1846 	/* authsys_parms rfc1831 */
1847 	*p++ = cpu_to_be32(nn->boot_time.tv_nsec);	/* stamp */
1848 	p = xdr_encode_opaque(p, machine_name, len);
1849 	*p++ = cpu_to_be32(0);				/* UID */
1850 	*p++ = cpu_to_be32(0);				/* GID */
1851 	*p = cpu_to_be32(0);				/* No more gids */
1852 }
1853 
1854 static void encode_destroy_session(struct xdr_stream *xdr,
1855 				   struct nfs4_session *session,
1856 				   struct compound_hdr *hdr)
1857 {
1858 	encode_op_hdr(xdr, OP_DESTROY_SESSION, decode_destroy_session_maxsz, hdr);
1859 	encode_opaque_fixed(xdr, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1860 }
1861 
1862 static void encode_destroy_clientid(struct xdr_stream *xdr,
1863 				   uint64_t clientid,
1864 				   struct compound_hdr *hdr)
1865 {
1866 	encode_op_hdr(xdr, OP_DESTROY_CLIENTID, decode_destroy_clientid_maxsz, hdr);
1867 	encode_uint64(xdr, clientid);
1868 }
1869 
1870 static void encode_reclaim_complete(struct xdr_stream *xdr,
1871 				    struct nfs41_reclaim_complete_args *args,
1872 				    struct compound_hdr *hdr)
1873 {
1874 	encode_op_hdr(xdr, OP_RECLAIM_COMPLETE, decode_reclaim_complete_maxsz, hdr);
1875 	encode_uint32(xdr, args->one_fs);
1876 }
1877 #endif /* CONFIG_NFS_V4_1 */
1878 
1879 static void encode_sequence(struct xdr_stream *xdr,
1880 			    const struct nfs4_sequence_args *args,
1881 			    struct compound_hdr *hdr)
1882 {
1883 #if defined(CONFIG_NFS_V4_1)
1884 	struct nfs4_session *session;
1885 	struct nfs4_slot_table *tp;
1886 	struct nfs4_slot *slot = args->sa_slot;
1887 	__be32 *p;
1888 
1889 	tp = slot->table;
1890 	session = tp->session;
1891 	if (!session)
1892 		return;
1893 
1894 	encode_op_hdr(xdr, OP_SEQUENCE, decode_sequence_maxsz, hdr);
1895 
1896 	/*
1897 	 * Sessionid + seqid + slotid + max slotid + cache_this
1898 	 */
1899 	dprintk("%s: sessionid=%u:%u:%u:%u seqid=%d slotid=%d "
1900 		"max_slotid=%d cache_this=%d\n",
1901 		__func__,
1902 		((u32 *)session->sess_id.data)[0],
1903 		((u32 *)session->sess_id.data)[1],
1904 		((u32 *)session->sess_id.data)[2],
1905 		((u32 *)session->sess_id.data)[3],
1906 		slot->seq_nr, slot->slot_nr,
1907 		tp->highest_used_slotid, args->sa_cache_this);
1908 	p = reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 16);
1909 	p = xdr_encode_opaque_fixed(p, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1910 	*p++ = cpu_to_be32(slot->seq_nr);
1911 	*p++ = cpu_to_be32(slot->slot_nr);
1912 	*p++ = cpu_to_be32(tp->highest_used_slotid);
1913 	*p = cpu_to_be32(args->sa_cache_this);
1914 #endif /* CONFIG_NFS_V4_1 */
1915 }
1916 
1917 #ifdef CONFIG_NFS_V4_1
1918 static void
1919 encode_getdeviceinfo(struct xdr_stream *xdr,
1920 		     const struct nfs4_getdeviceinfo_args *args,
1921 		     struct compound_hdr *hdr)
1922 {
1923 	__be32 *p;
1924 
1925 	encode_op_hdr(xdr, OP_GETDEVICEINFO, decode_getdeviceinfo_maxsz, hdr);
1926 	p = reserve_space(xdr, NFS4_DEVICEID4_SIZE + 4 + 4);
1927 	p = xdr_encode_opaque_fixed(p, args->pdev->dev_id.data,
1928 				    NFS4_DEVICEID4_SIZE);
1929 	*p++ = cpu_to_be32(args->pdev->layout_type);
1930 	*p++ = cpu_to_be32(args->pdev->maxcount);	/* gdia_maxcount */
1931 
1932 	p = reserve_space(xdr, 4 + 4);
1933 	*p++ = cpu_to_be32(1);			/* bitmap length */
1934 	*p++ = cpu_to_be32(NOTIFY_DEVICEID4_CHANGE | NOTIFY_DEVICEID4_DELETE);
1935 }
1936 
1937 static void
1938 encode_layoutget(struct xdr_stream *xdr,
1939 		      const struct nfs4_layoutget_args *args,
1940 		      struct compound_hdr *hdr)
1941 {
1942 	__be32 *p;
1943 
1944 	encode_op_hdr(xdr, OP_LAYOUTGET, decode_layoutget_maxsz, hdr);
1945 	p = reserve_space(xdr, 36);
1946 	*p++ = cpu_to_be32(0);     /* Signal layout available */
1947 	*p++ = cpu_to_be32(args->type);
1948 	*p++ = cpu_to_be32(args->range.iomode);
1949 	p = xdr_encode_hyper(p, args->range.offset);
1950 	p = xdr_encode_hyper(p, args->range.length);
1951 	p = xdr_encode_hyper(p, args->minlength);
1952 	encode_nfs4_stateid(xdr, &args->stateid);
1953 	encode_uint32(xdr, args->maxcount);
1954 
1955 	dprintk("%s: 1st type:0x%x iomode:%d off:%lu len:%lu mc:%d\n",
1956 		__func__,
1957 		args->type,
1958 		args->range.iomode,
1959 		(unsigned long)args->range.offset,
1960 		(unsigned long)args->range.length,
1961 		args->maxcount);
1962 }
1963 
1964 static int
1965 encode_layoutcommit(struct xdr_stream *xdr,
1966 		    struct inode *inode,
1967 		    struct nfs4_layoutcommit_args *args,
1968 		    struct compound_hdr *hdr)
1969 {
1970 	__be32 *p;
1971 
1972 	dprintk("%s: lbw: %llu type: %d\n", __func__, args->lastbytewritten,
1973 		NFS_SERVER(args->inode)->pnfs_curr_ld->id);
1974 
1975 	encode_op_hdr(xdr, OP_LAYOUTCOMMIT, decode_layoutcommit_maxsz, hdr);
1976 	p = reserve_space(xdr, 20);
1977 	/* Only whole file layouts */
1978 	p = xdr_encode_hyper(p, 0); /* offset */
1979 	p = xdr_encode_hyper(p, args->lastbytewritten + 1);	/* length */
1980 	*p = cpu_to_be32(0); /* reclaim */
1981 	encode_nfs4_stateid(xdr, &args->stateid);
1982 	p = reserve_space(xdr, 20);
1983 	*p++ = cpu_to_be32(1); /* newoffset = TRUE */
1984 	p = xdr_encode_hyper(p, args->lastbytewritten);
1985 	*p++ = cpu_to_be32(0); /* Never send time_modify_changed */
1986 	*p++ = cpu_to_be32(NFS_SERVER(args->inode)->pnfs_curr_ld->id);/* type */
1987 
1988 	if (NFS_SERVER(inode)->pnfs_curr_ld->encode_layoutcommit) {
1989 		NFS_SERVER(inode)->pnfs_curr_ld->encode_layoutcommit(
1990 			NFS_I(inode)->layout, xdr, args);
1991 	} else {
1992 		encode_uint32(xdr, args->layoutupdate_len);
1993 		if (args->layoutupdate_pages) {
1994 			xdr_write_pages(xdr, args->layoutupdate_pages, 0,
1995 					args->layoutupdate_len);
1996 		}
1997 	}
1998 
1999 	return 0;
2000 }
2001 
2002 static void
2003 encode_layoutreturn(struct xdr_stream *xdr,
2004 		    const struct nfs4_layoutreturn_args *args,
2005 		    struct compound_hdr *hdr)
2006 {
2007 	__be32 *p;
2008 
2009 	encode_op_hdr(xdr, OP_LAYOUTRETURN, decode_layoutreturn_maxsz, hdr);
2010 	p = reserve_space(xdr, 16);
2011 	*p++ = cpu_to_be32(0);		/* reclaim. always 0 for now */
2012 	*p++ = cpu_to_be32(args->layout_type);
2013 	*p++ = cpu_to_be32(IOMODE_ANY);
2014 	*p = cpu_to_be32(RETURN_FILE);
2015 	p = reserve_space(xdr, 16);
2016 	p = xdr_encode_hyper(p, 0);
2017 	p = xdr_encode_hyper(p, NFS4_MAX_UINT64);
2018 	spin_lock(&args->inode->i_lock);
2019 	encode_nfs4_stateid(xdr, &args->stateid);
2020 	spin_unlock(&args->inode->i_lock);
2021 	if (NFS_SERVER(args->inode)->pnfs_curr_ld->encode_layoutreturn) {
2022 		NFS_SERVER(args->inode)->pnfs_curr_ld->encode_layoutreturn(
2023 			NFS_I(args->inode)->layout, xdr, args);
2024 	} else
2025 		encode_uint32(xdr, 0);
2026 }
2027 
2028 static int
2029 encode_secinfo_no_name(struct xdr_stream *xdr,
2030 		       const struct nfs41_secinfo_no_name_args *args,
2031 		       struct compound_hdr *hdr)
2032 {
2033 	encode_op_hdr(xdr, OP_SECINFO_NO_NAME, decode_secinfo_no_name_maxsz, hdr);
2034 	encode_uint32(xdr, args->style);
2035 	return 0;
2036 }
2037 
2038 static void encode_test_stateid(struct xdr_stream *xdr,
2039 				struct nfs41_test_stateid_args *args,
2040 				struct compound_hdr *hdr)
2041 {
2042 	encode_op_hdr(xdr, OP_TEST_STATEID, decode_test_stateid_maxsz, hdr);
2043 	encode_uint32(xdr, 1);
2044 	encode_nfs4_stateid(xdr, args->stateid);
2045 }
2046 
2047 static void encode_free_stateid(struct xdr_stream *xdr,
2048 				struct nfs41_free_stateid_args *args,
2049 				struct compound_hdr *hdr)
2050 {
2051 	encode_op_hdr(xdr, OP_FREE_STATEID, decode_free_stateid_maxsz, hdr);
2052 	encode_nfs4_stateid(xdr, &args->stateid);
2053 }
2054 #endif /* CONFIG_NFS_V4_1 */
2055 
2056 /*
2057  * END OF "GENERIC" ENCODE ROUTINES.
2058  */
2059 
2060 static u32 nfs4_xdr_minorversion(const struct nfs4_sequence_args *args)
2061 {
2062 #if defined(CONFIG_NFS_V4_1)
2063 	struct nfs4_session *session = args->sa_slot->table->session;
2064 	if (session)
2065 		return session->clp->cl_mvops->minor_version;
2066 #endif /* CONFIG_NFS_V4_1 */
2067 	return 0;
2068 }
2069 
2070 /*
2071  * Encode an ACCESS request
2072  */
2073 static void nfs4_xdr_enc_access(struct rpc_rqst *req, struct xdr_stream *xdr,
2074 				const struct nfs4_accessargs *args)
2075 {
2076 	struct compound_hdr hdr = {
2077 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2078 	};
2079 
2080 	encode_compound_hdr(xdr, req, &hdr);
2081 	encode_sequence(xdr, &args->seq_args, &hdr);
2082 	encode_putfh(xdr, args->fh, &hdr);
2083 	encode_access(xdr, args->access, &hdr);
2084 	encode_getfattr(xdr, args->bitmask, &hdr);
2085 	encode_nops(&hdr);
2086 }
2087 
2088 /*
2089  * Encode LOOKUP request
2090  */
2091 static void nfs4_xdr_enc_lookup(struct rpc_rqst *req, struct xdr_stream *xdr,
2092 				const struct nfs4_lookup_arg *args)
2093 {
2094 	struct compound_hdr hdr = {
2095 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2096 	};
2097 
2098 	encode_compound_hdr(xdr, req, &hdr);
2099 	encode_sequence(xdr, &args->seq_args, &hdr);
2100 	encode_putfh(xdr, args->dir_fh, &hdr);
2101 	encode_lookup(xdr, args->name, &hdr);
2102 	encode_getfh(xdr, &hdr);
2103 	encode_getfattr(xdr, args->bitmask, &hdr);
2104 	encode_nops(&hdr);
2105 }
2106 
2107 /*
2108  * Encode LOOKUP_ROOT request
2109  */
2110 static void nfs4_xdr_enc_lookup_root(struct rpc_rqst *req,
2111 				     struct xdr_stream *xdr,
2112 				     const struct nfs4_lookup_root_arg *args)
2113 {
2114 	struct compound_hdr hdr = {
2115 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2116 	};
2117 
2118 	encode_compound_hdr(xdr, req, &hdr);
2119 	encode_sequence(xdr, &args->seq_args, &hdr);
2120 	encode_putrootfh(xdr, &hdr);
2121 	encode_getfh(xdr, &hdr);
2122 	encode_getfattr(xdr, args->bitmask, &hdr);
2123 	encode_nops(&hdr);
2124 }
2125 
2126 /*
2127  * Encode REMOVE request
2128  */
2129 static void nfs4_xdr_enc_remove(struct rpc_rqst *req, struct xdr_stream *xdr,
2130 				const struct nfs_removeargs *args)
2131 {
2132 	struct compound_hdr hdr = {
2133 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2134 	};
2135 
2136 	encode_compound_hdr(xdr, req, &hdr);
2137 	encode_sequence(xdr, &args->seq_args, &hdr);
2138 	encode_putfh(xdr, args->fh, &hdr);
2139 	encode_remove(xdr, &args->name, &hdr);
2140 	encode_nops(&hdr);
2141 }
2142 
2143 /*
2144  * Encode RENAME request
2145  */
2146 static void nfs4_xdr_enc_rename(struct rpc_rqst *req, struct xdr_stream *xdr,
2147 				const struct nfs_renameargs *args)
2148 {
2149 	struct compound_hdr hdr = {
2150 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2151 	};
2152 
2153 	encode_compound_hdr(xdr, req, &hdr);
2154 	encode_sequence(xdr, &args->seq_args, &hdr);
2155 	encode_putfh(xdr, args->old_dir, &hdr);
2156 	encode_savefh(xdr, &hdr);
2157 	encode_putfh(xdr, args->new_dir, &hdr);
2158 	encode_rename(xdr, args->old_name, args->new_name, &hdr);
2159 	encode_nops(&hdr);
2160 }
2161 
2162 /*
2163  * Encode LINK request
2164  */
2165 static void nfs4_xdr_enc_link(struct rpc_rqst *req, struct xdr_stream *xdr,
2166 			     const struct nfs4_link_arg *args)
2167 {
2168 	struct compound_hdr hdr = {
2169 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2170 	};
2171 
2172 	encode_compound_hdr(xdr, req, &hdr);
2173 	encode_sequence(xdr, &args->seq_args, &hdr);
2174 	encode_putfh(xdr, args->fh, &hdr);
2175 	encode_savefh(xdr, &hdr);
2176 	encode_putfh(xdr, args->dir_fh, &hdr);
2177 	encode_link(xdr, args->name, &hdr);
2178 	encode_restorefh(xdr, &hdr);
2179 	encode_getfattr(xdr, args->bitmask, &hdr);
2180 	encode_nops(&hdr);
2181 }
2182 
2183 /*
2184  * Encode CREATE request
2185  */
2186 static void nfs4_xdr_enc_create(struct rpc_rqst *req, struct xdr_stream *xdr,
2187 				const struct nfs4_create_arg *args)
2188 {
2189 	struct compound_hdr hdr = {
2190 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2191 	};
2192 
2193 	encode_compound_hdr(xdr, req, &hdr);
2194 	encode_sequence(xdr, &args->seq_args, &hdr);
2195 	encode_putfh(xdr, args->dir_fh, &hdr);
2196 	encode_create(xdr, args, &hdr);
2197 	encode_getfh(xdr, &hdr);
2198 	encode_getfattr(xdr, args->bitmask, &hdr);
2199 	encode_nops(&hdr);
2200 }
2201 
2202 /*
2203  * Encode SYMLINK request
2204  */
2205 static void nfs4_xdr_enc_symlink(struct rpc_rqst *req, struct xdr_stream *xdr,
2206 				 const struct nfs4_create_arg *args)
2207 {
2208 	nfs4_xdr_enc_create(req, xdr, args);
2209 }
2210 
2211 /*
2212  * Encode GETATTR request
2213  */
2214 static void nfs4_xdr_enc_getattr(struct rpc_rqst *req, struct xdr_stream *xdr,
2215 				 const struct nfs4_getattr_arg *args)
2216 {
2217 	struct compound_hdr hdr = {
2218 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2219 	};
2220 
2221 	encode_compound_hdr(xdr, req, &hdr);
2222 	encode_sequence(xdr, &args->seq_args, &hdr);
2223 	encode_putfh(xdr, args->fh, &hdr);
2224 	encode_getfattr(xdr, args->bitmask, &hdr);
2225 	encode_nops(&hdr);
2226 }
2227 
2228 /*
2229  * Encode a CLOSE request
2230  */
2231 static void nfs4_xdr_enc_close(struct rpc_rqst *req, struct xdr_stream *xdr,
2232 			       struct nfs_closeargs *args)
2233 {
2234 	struct compound_hdr hdr = {
2235 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2236 	};
2237 
2238 	encode_compound_hdr(xdr, req, &hdr);
2239 	encode_sequence(xdr, &args->seq_args, &hdr);
2240 	encode_putfh(xdr, args->fh, &hdr);
2241 	encode_close(xdr, args, &hdr);
2242 	encode_getfattr(xdr, args->bitmask, &hdr);
2243 	encode_nops(&hdr);
2244 }
2245 
2246 /*
2247  * Encode an OPEN request
2248  */
2249 static void nfs4_xdr_enc_open(struct rpc_rqst *req, struct xdr_stream *xdr,
2250 			      struct nfs_openargs *args)
2251 {
2252 	struct compound_hdr hdr = {
2253 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2254 	};
2255 
2256 	encode_compound_hdr(xdr, req, &hdr);
2257 	encode_sequence(xdr, &args->seq_args, &hdr);
2258 	encode_putfh(xdr, args->fh, &hdr);
2259 	encode_open(xdr, args, &hdr);
2260 	encode_getfh(xdr, &hdr);
2261 	if (args->access)
2262 		encode_access(xdr, args->access, &hdr);
2263 	encode_getfattr_open(xdr, args->bitmask, args->open_bitmap, &hdr);
2264 	encode_nops(&hdr);
2265 }
2266 
2267 /*
2268  * Encode an OPEN_CONFIRM request
2269  */
2270 static void nfs4_xdr_enc_open_confirm(struct rpc_rqst *req,
2271 				      struct xdr_stream *xdr,
2272 				      struct nfs_open_confirmargs *args)
2273 {
2274 	struct compound_hdr hdr = {
2275 		.nops   = 0,
2276 	};
2277 
2278 	encode_compound_hdr(xdr, req, &hdr);
2279 	encode_putfh(xdr, args->fh, &hdr);
2280 	encode_open_confirm(xdr, args, &hdr);
2281 	encode_nops(&hdr);
2282 }
2283 
2284 /*
2285  * Encode an OPEN request with no attributes.
2286  */
2287 static void nfs4_xdr_enc_open_noattr(struct rpc_rqst *req,
2288 				     struct xdr_stream *xdr,
2289 				     struct nfs_openargs *args)
2290 {
2291 	struct compound_hdr hdr = {
2292 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2293 	};
2294 
2295 	encode_compound_hdr(xdr, req, &hdr);
2296 	encode_sequence(xdr, &args->seq_args, &hdr);
2297 	encode_putfh(xdr, args->fh, &hdr);
2298 	encode_open(xdr, args, &hdr);
2299 	if (args->access)
2300 		encode_access(xdr, args->access, &hdr);
2301 	encode_getfattr_open(xdr, args->bitmask, args->open_bitmap, &hdr);
2302 	encode_nops(&hdr);
2303 }
2304 
2305 /*
2306  * Encode an OPEN_DOWNGRADE request
2307  */
2308 static void nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req,
2309 					struct xdr_stream *xdr,
2310 					struct nfs_closeargs *args)
2311 {
2312 	struct compound_hdr hdr = {
2313 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2314 	};
2315 
2316 	encode_compound_hdr(xdr, req, &hdr);
2317 	encode_sequence(xdr, &args->seq_args, &hdr);
2318 	encode_putfh(xdr, args->fh, &hdr);
2319 	encode_open_downgrade(xdr, args, &hdr);
2320 	encode_getfattr(xdr, args->bitmask, &hdr);
2321 	encode_nops(&hdr);
2322 }
2323 
2324 /*
2325  * Encode a LOCK request
2326  */
2327 static void nfs4_xdr_enc_lock(struct rpc_rqst *req, struct xdr_stream *xdr,
2328 			      struct nfs_lock_args *args)
2329 {
2330 	struct compound_hdr hdr = {
2331 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2332 	};
2333 
2334 	encode_compound_hdr(xdr, req, &hdr);
2335 	encode_sequence(xdr, &args->seq_args, &hdr);
2336 	encode_putfh(xdr, args->fh, &hdr);
2337 	encode_lock(xdr, args, &hdr);
2338 	encode_nops(&hdr);
2339 }
2340 
2341 /*
2342  * Encode a LOCKT request
2343  */
2344 static void nfs4_xdr_enc_lockt(struct rpc_rqst *req, struct xdr_stream *xdr,
2345 			       struct nfs_lockt_args *args)
2346 {
2347 	struct compound_hdr hdr = {
2348 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2349 	};
2350 
2351 	encode_compound_hdr(xdr, req, &hdr);
2352 	encode_sequence(xdr, &args->seq_args, &hdr);
2353 	encode_putfh(xdr, args->fh, &hdr);
2354 	encode_lockt(xdr, args, &hdr);
2355 	encode_nops(&hdr);
2356 }
2357 
2358 /*
2359  * Encode a LOCKU request
2360  */
2361 static void nfs4_xdr_enc_locku(struct rpc_rqst *req, struct xdr_stream *xdr,
2362 			       struct nfs_locku_args *args)
2363 {
2364 	struct compound_hdr hdr = {
2365 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2366 	};
2367 
2368 	encode_compound_hdr(xdr, req, &hdr);
2369 	encode_sequence(xdr, &args->seq_args, &hdr);
2370 	encode_putfh(xdr, args->fh, &hdr);
2371 	encode_locku(xdr, args, &hdr);
2372 	encode_nops(&hdr);
2373 }
2374 
2375 static void nfs4_xdr_enc_release_lockowner(struct rpc_rqst *req,
2376 					   struct xdr_stream *xdr,
2377 					struct nfs_release_lockowner_args *args)
2378 {
2379 	struct compound_hdr hdr = {
2380 		.minorversion = 0,
2381 	};
2382 
2383 	encode_compound_hdr(xdr, req, &hdr);
2384 	encode_release_lockowner(xdr, &args->lock_owner, &hdr);
2385 	encode_nops(&hdr);
2386 }
2387 
2388 /*
2389  * Encode a READLINK request
2390  */
2391 static void nfs4_xdr_enc_readlink(struct rpc_rqst *req, struct xdr_stream *xdr,
2392 				  const struct nfs4_readlink *args)
2393 {
2394 	struct compound_hdr hdr = {
2395 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2396 	};
2397 
2398 	encode_compound_hdr(xdr, req, &hdr);
2399 	encode_sequence(xdr, &args->seq_args, &hdr);
2400 	encode_putfh(xdr, args->fh, &hdr);
2401 	encode_readlink(xdr, args, req, &hdr);
2402 
2403 	xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2, args->pages,
2404 			args->pgbase, args->pglen);
2405 	encode_nops(&hdr);
2406 }
2407 
2408 /*
2409  * Encode a READDIR request
2410  */
2411 static void nfs4_xdr_enc_readdir(struct rpc_rqst *req, struct xdr_stream *xdr,
2412 				 const struct nfs4_readdir_arg *args)
2413 {
2414 	struct compound_hdr hdr = {
2415 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2416 	};
2417 
2418 	encode_compound_hdr(xdr, req, &hdr);
2419 	encode_sequence(xdr, &args->seq_args, &hdr);
2420 	encode_putfh(xdr, args->fh, &hdr);
2421 	encode_readdir(xdr, args, req, &hdr);
2422 
2423 	xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2, args->pages,
2424 			 args->pgbase, args->count);
2425 	dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
2426 			__func__, hdr.replen << 2, args->pages,
2427 			args->pgbase, args->count);
2428 	encode_nops(&hdr);
2429 }
2430 
2431 /*
2432  * Encode a READ request
2433  */
2434 static void nfs4_xdr_enc_read(struct rpc_rqst *req, struct xdr_stream *xdr,
2435 			      struct nfs_pgio_args *args)
2436 {
2437 	struct compound_hdr hdr = {
2438 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2439 	};
2440 
2441 	encode_compound_hdr(xdr, req, &hdr);
2442 	encode_sequence(xdr, &args->seq_args, &hdr);
2443 	encode_putfh(xdr, args->fh, &hdr);
2444 	encode_read(xdr, args, &hdr);
2445 
2446 	xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2,
2447 			 args->pages, args->pgbase, args->count);
2448 	req->rq_rcv_buf.flags |= XDRBUF_READ;
2449 	encode_nops(&hdr);
2450 }
2451 
2452 /*
2453  * Encode an SETATTR request
2454  */
2455 static void nfs4_xdr_enc_setattr(struct rpc_rqst *req, struct xdr_stream *xdr,
2456 				 struct nfs_setattrargs *args)
2457 {
2458 	struct compound_hdr hdr = {
2459 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2460 	};
2461 
2462 	encode_compound_hdr(xdr, req, &hdr);
2463 	encode_sequence(xdr, &args->seq_args, &hdr);
2464 	encode_putfh(xdr, args->fh, &hdr);
2465 	encode_setattr(xdr, args, args->server, &hdr);
2466 	encode_getfattr(xdr, args->bitmask, &hdr);
2467 	encode_nops(&hdr);
2468 }
2469 
2470 /*
2471  * Encode a GETACL request
2472  */
2473 static void nfs4_xdr_enc_getacl(struct rpc_rqst *req, struct xdr_stream *xdr,
2474 				struct nfs_getaclargs *args)
2475 {
2476 	struct compound_hdr hdr = {
2477 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2478 	};
2479 	uint32_t replen;
2480 
2481 	encode_compound_hdr(xdr, req, &hdr);
2482 	encode_sequence(xdr, &args->seq_args, &hdr);
2483 	encode_putfh(xdr, args->fh, &hdr);
2484 	replen = hdr.replen + op_decode_hdr_maxsz + 1;
2485 	encode_getattr_two(xdr, FATTR4_WORD0_ACL, 0, &hdr);
2486 
2487 	xdr_inline_pages(&req->rq_rcv_buf, replen << 2,
2488 		args->acl_pages, args->acl_pgbase, args->acl_len);
2489 
2490 	encode_nops(&hdr);
2491 }
2492 
2493 /*
2494  * Encode a WRITE request
2495  */
2496 static void nfs4_xdr_enc_write(struct rpc_rqst *req, struct xdr_stream *xdr,
2497 			       struct nfs_pgio_args *args)
2498 {
2499 	struct compound_hdr hdr = {
2500 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2501 	};
2502 
2503 	encode_compound_hdr(xdr, req, &hdr);
2504 	encode_sequence(xdr, &args->seq_args, &hdr);
2505 	encode_putfh(xdr, args->fh, &hdr);
2506 	encode_write(xdr, args, &hdr);
2507 	req->rq_snd_buf.flags |= XDRBUF_WRITE;
2508 	if (args->bitmask)
2509 		encode_getfattr(xdr, args->bitmask, &hdr);
2510 	encode_nops(&hdr);
2511 }
2512 
2513 /*
2514  *  a COMMIT request
2515  */
2516 static void nfs4_xdr_enc_commit(struct rpc_rqst *req, struct xdr_stream *xdr,
2517 				struct nfs_commitargs *args)
2518 {
2519 	struct compound_hdr hdr = {
2520 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2521 	};
2522 
2523 	encode_compound_hdr(xdr, req, &hdr);
2524 	encode_sequence(xdr, &args->seq_args, &hdr);
2525 	encode_putfh(xdr, args->fh, &hdr);
2526 	encode_commit(xdr, args, &hdr);
2527 	encode_nops(&hdr);
2528 }
2529 
2530 /*
2531  * FSINFO request
2532  */
2533 static void nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, struct xdr_stream *xdr,
2534 				struct nfs4_fsinfo_arg *args)
2535 {
2536 	struct compound_hdr hdr = {
2537 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2538 	};
2539 
2540 	encode_compound_hdr(xdr, req, &hdr);
2541 	encode_sequence(xdr, &args->seq_args, &hdr);
2542 	encode_putfh(xdr, args->fh, &hdr);
2543 	encode_fsinfo(xdr, args->bitmask, &hdr);
2544 	encode_nops(&hdr);
2545 }
2546 
2547 /*
2548  * a PATHCONF request
2549  */
2550 static void nfs4_xdr_enc_pathconf(struct rpc_rqst *req, struct xdr_stream *xdr,
2551 				  const struct nfs4_pathconf_arg *args)
2552 {
2553 	struct compound_hdr hdr = {
2554 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2555 	};
2556 
2557 	encode_compound_hdr(xdr, req, &hdr);
2558 	encode_sequence(xdr, &args->seq_args, &hdr);
2559 	encode_putfh(xdr, args->fh, &hdr);
2560 	encode_getattr_one(xdr, args->bitmask[0] & nfs4_pathconf_bitmap[0],
2561 			   &hdr);
2562 	encode_nops(&hdr);
2563 }
2564 
2565 /*
2566  * a STATFS request
2567  */
2568 static void nfs4_xdr_enc_statfs(struct rpc_rqst *req, struct xdr_stream *xdr,
2569 				const struct nfs4_statfs_arg *args)
2570 {
2571 	struct compound_hdr hdr = {
2572 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2573 	};
2574 
2575 	encode_compound_hdr(xdr, req, &hdr);
2576 	encode_sequence(xdr, &args->seq_args, &hdr);
2577 	encode_putfh(xdr, args->fh, &hdr);
2578 	encode_getattr_two(xdr, args->bitmask[0] & nfs4_statfs_bitmap[0],
2579 			   args->bitmask[1] & nfs4_statfs_bitmap[1], &hdr);
2580 	encode_nops(&hdr);
2581 }
2582 
2583 /*
2584  * GETATTR_BITMAP request
2585  */
2586 static void nfs4_xdr_enc_server_caps(struct rpc_rqst *req,
2587 				     struct xdr_stream *xdr,
2588 				     struct nfs4_server_caps_arg *args)
2589 {
2590 	struct compound_hdr hdr = {
2591 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2592 	};
2593 
2594 	encode_compound_hdr(xdr, req, &hdr);
2595 	encode_sequence(xdr, &args->seq_args, &hdr);
2596 	encode_putfh(xdr, args->fhandle, &hdr);
2597 	encode_getattr_one(xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
2598 			   FATTR4_WORD0_FH_EXPIRE_TYPE|
2599 			   FATTR4_WORD0_LINK_SUPPORT|
2600 			   FATTR4_WORD0_SYMLINK_SUPPORT|
2601 			   FATTR4_WORD0_ACLSUPPORT, &hdr);
2602 	encode_nops(&hdr);
2603 }
2604 
2605 /*
2606  * a RENEW request
2607  */
2608 static void nfs4_xdr_enc_renew(struct rpc_rqst *req, struct xdr_stream *xdr,
2609 			       struct nfs_client *clp)
2610 {
2611 	struct compound_hdr hdr = {
2612 		.nops	= 0,
2613 	};
2614 
2615 	encode_compound_hdr(xdr, req, &hdr);
2616 	encode_renew(xdr, clp->cl_clientid, &hdr);
2617 	encode_nops(&hdr);
2618 }
2619 
2620 /*
2621  * a SETCLIENTID request
2622  */
2623 static void nfs4_xdr_enc_setclientid(struct rpc_rqst *req,
2624 				     struct xdr_stream *xdr,
2625 				     struct nfs4_setclientid *sc)
2626 {
2627 	struct compound_hdr hdr = {
2628 		.nops	= 0,
2629 	};
2630 
2631 	encode_compound_hdr(xdr, req, &hdr);
2632 	encode_setclientid(xdr, sc, &hdr);
2633 	encode_nops(&hdr);
2634 }
2635 
2636 /*
2637  * a SETCLIENTID_CONFIRM request
2638  */
2639 static void nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req,
2640 					     struct xdr_stream *xdr,
2641 					     struct nfs4_setclientid_res *arg)
2642 {
2643 	struct compound_hdr hdr = {
2644 		.nops	= 0,
2645 	};
2646 
2647 	encode_compound_hdr(xdr, req, &hdr);
2648 	encode_setclientid_confirm(xdr, arg, &hdr);
2649 	encode_nops(&hdr);
2650 }
2651 
2652 /*
2653  * DELEGRETURN request
2654  */
2655 static void nfs4_xdr_enc_delegreturn(struct rpc_rqst *req,
2656 				     struct xdr_stream *xdr,
2657 				     const struct nfs4_delegreturnargs *args)
2658 {
2659 	struct compound_hdr hdr = {
2660 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2661 	};
2662 
2663 	encode_compound_hdr(xdr, req, &hdr);
2664 	encode_sequence(xdr, &args->seq_args, &hdr);
2665 	encode_putfh(xdr, args->fhandle, &hdr);
2666 	encode_getfattr(xdr, args->bitmask, &hdr);
2667 	encode_delegreturn(xdr, args->stateid, &hdr);
2668 	encode_nops(&hdr);
2669 }
2670 
2671 /*
2672  * Encode FS_LOCATIONS request
2673  */
2674 static void nfs4_xdr_enc_fs_locations(struct rpc_rqst *req,
2675 				      struct xdr_stream *xdr,
2676 				      struct nfs4_fs_locations_arg *args)
2677 {
2678 	struct compound_hdr hdr = {
2679 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2680 	};
2681 	uint32_t replen;
2682 
2683 	encode_compound_hdr(xdr, req, &hdr);
2684 	encode_sequence(xdr, &args->seq_args, &hdr);
2685 	if (args->migration) {
2686 		encode_putfh(xdr, args->fh, &hdr);
2687 		replen = hdr.replen;
2688 		encode_fs_locations(xdr, args->bitmask, &hdr);
2689 		if (args->renew)
2690 			encode_renew(xdr, args->clientid, &hdr);
2691 	} else {
2692 		encode_putfh(xdr, args->dir_fh, &hdr);
2693 		encode_lookup(xdr, args->name, &hdr);
2694 		replen = hdr.replen;
2695 		encode_fs_locations(xdr, args->bitmask, &hdr);
2696 	}
2697 
2698 	/* Set up reply kvec to capture returned fs_locations array. */
2699 	xdr_inline_pages(&req->rq_rcv_buf, replen << 2, &args->page,
2700 			0, PAGE_SIZE);
2701 	encode_nops(&hdr);
2702 }
2703 
2704 /*
2705  * Encode SECINFO request
2706  */
2707 static void nfs4_xdr_enc_secinfo(struct rpc_rqst *req,
2708 				struct xdr_stream *xdr,
2709 				struct nfs4_secinfo_arg *args)
2710 {
2711 	struct compound_hdr hdr = {
2712 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2713 	};
2714 
2715 	encode_compound_hdr(xdr, req, &hdr);
2716 	encode_sequence(xdr, &args->seq_args, &hdr);
2717 	encode_putfh(xdr, args->dir_fh, &hdr);
2718 	encode_secinfo(xdr, args->name, &hdr);
2719 	encode_nops(&hdr);
2720 }
2721 
2722 /*
2723  * Encode FSID_PRESENT request
2724  */
2725 static void nfs4_xdr_enc_fsid_present(struct rpc_rqst *req,
2726 				      struct xdr_stream *xdr,
2727 				      struct nfs4_fsid_present_arg *args)
2728 {
2729 	struct compound_hdr hdr = {
2730 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2731 	};
2732 
2733 	encode_compound_hdr(xdr, req, &hdr);
2734 	encode_sequence(xdr, &args->seq_args, &hdr);
2735 	encode_putfh(xdr, args->fh, &hdr);
2736 	encode_getfh(xdr, &hdr);
2737 	if (args->renew)
2738 		encode_renew(xdr, args->clientid, &hdr);
2739 	encode_nops(&hdr);
2740 }
2741 
2742 #if defined(CONFIG_NFS_V4_1)
2743 /*
2744  * BIND_CONN_TO_SESSION request
2745  */
2746 static void nfs4_xdr_enc_bind_conn_to_session(struct rpc_rqst *req,
2747 				struct xdr_stream *xdr,
2748 				struct nfs_client *clp)
2749 {
2750 	struct compound_hdr hdr = {
2751 		.minorversion = clp->cl_mvops->minor_version,
2752 	};
2753 
2754 	encode_compound_hdr(xdr, req, &hdr);
2755 	encode_bind_conn_to_session(xdr, clp->cl_session, &hdr);
2756 	encode_nops(&hdr);
2757 }
2758 
2759 /*
2760  * EXCHANGE_ID request
2761  */
2762 static void nfs4_xdr_enc_exchange_id(struct rpc_rqst *req,
2763 				     struct xdr_stream *xdr,
2764 				     struct nfs41_exchange_id_args *args)
2765 {
2766 	struct compound_hdr hdr = {
2767 		.minorversion = args->client->cl_mvops->minor_version,
2768 	};
2769 
2770 	encode_compound_hdr(xdr, req, &hdr);
2771 	encode_exchange_id(xdr, args, &hdr);
2772 	encode_nops(&hdr);
2773 }
2774 
2775 /*
2776  * a CREATE_SESSION request
2777  */
2778 static void nfs4_xdr_enc_create_session(struct rpc_rqst *req,
2779 					struct xdr_stream *xdr,
2780 					struct nfs41_create_session_args *args)
2781 {
2782 	struct compound_hdr hdr = {
2783 		.minorversion = args->client->cl_mvops->minor_version,
2784 	};
2785 
2786 	encode_compound_hdr(xdr, req, &hdr);
2787 	encode_create_session(xdr, args, &hdr);
2788 	encode_nops(&hdr);
2789 }
2790 
2791 /*
2792  * a DESTROY_SESSION request
2793  */
2794 static void nfs4_xdr_enc_destroy_session(struct rpc_rqst *req,
2795 					 struct xdr_stream *xdr,
2796 					 struct nfs4_session *session)
2797 {
2798 	struct compound_hdr hdr = {
2799 		.minorversion = session->clp->cl_mvops->minor_version,
2800 	};
2801 
2802 	encode_compound_hdr(xdr, req, &hdr);
2803 	encode_destroy_session(xdr, session, &hdr);
2804 	encode_nops(&hdr);
2805 }
2806 
2807 /*
2808  * a DESTROY_CLIENTID request
2809  */
2810 static void nfs4_xdr_enc_destroy_clientid(struct rpc_rqst *req,
2811 					 struct xdr_stream *xdr,
2812 					 struct nfs_client *clp)
2813 {
2814 	struct compound_hdr hdr = {
2815 		.minorversion = clp->cl_mvops->minor_version,
2816 	};
2817 
2818 	encode_compound_hdr(xdr, req, &hdr);
2819 	encode_destroy_clientid(xdr, clp->cl_clientid, &hdr);
2820 	encode_nops(&hdr);
2821 }
2822 
2823 /*
2824  * a SEQUENCE request
2825  */
2826 static void nfs4_xdr_enc_sequence(struct rpc_rqst *req, struct xdr_stream *xdr,
2827 				  struct nfs4_sequence_args *args)
2828 {
2829 	struct compound_hdr hdr = {
2830 		.minorversion = nfs4_xdr_minorversion(args),
2831 	};
2832 
2833 	encode_compound_hdr(xdr, req, &hdr);
2834 	encode_sequence(xdr, args, &hdr);
2835 	encode_nops(&hdr);
2836 }
2837 
2838 /*
2839  * a GET_LEASE_TIME request
2840  */
2841 static void nfs4_xdr_enc_get_lease_time(struct rpc_rqst *req,
2842 					struct xdr_stream *xdr,
2843 					struct nfs4_get_lease_time_args *args)
2844 {
2845 	struct compound_hdr hdr = {
2846 		.minorversion = nfs4_xdr_minorversion(&args->la_seq_args),
2847 	};
2848 	const u32 lease_bitmap[3] = { FATTR4_WORD0_LEASE_TIME };
2849 
2850 	encode_compound_hdr(xdr, req, &hdr);
2851 	encode_sequence(xdr, &args->la_seq_args, &hdr);
2852 	encode_putrootfh(xdr, &hdr);
2853 	encode_fsinfo(xdr, lease_bitmap, &hdr);
2854 	encode_nops(&hdr);
2855 }
2856 
2857 /*
2858  * a RECLAIM_COMPLETE request
2859  */
2860 static void nfs4_xdr_enc_reclaim_complete(struct rpc_rqst *req,
2861 					  struct xdr_stream *xdr,
2862 				struct nfs41_reclaim_complete_args *args)
2863 {
2864 	struct compound_hdr hdr = {
2865 		.minorversion = nfs4_xdr_minorversion(&args->seq_args)
2866 	};
2867 
2868 	encode_compound_hdr(xdr, req, &hdr);
2869 	encode_sequence(xdr, &args->seq_args, &hdr);
2870 	encode_reclaim_complete(xdr, args, &hdr);
2871 	encode_nops(&hdr);
2872 }
2873 
2874 /*
2875  * Encode GETDEVICEINFO request
2876  */
2877 static void nfs4_xdr_enc_getdeviceinfo(struct rpc_rqst *req,
2878 				       struct xdr_stream *xdr,
2879 				       struct nfs4_getdeviceinfo_args *args)
2880 {
2881 	struct compound_hdr hdr = {
2882 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2883 	};
2884 
2885 	encode_compound_hdr(xdr, req, &hdr);
2886 	encode_sequence(xdr, &args->seq_args, &hdr);
2887 	encode_getdeviceinfo(xdr, args, &hdr);
2888 
2889 	/* set up reply kvec. Subtract notification bitmap max size (2)
2890 	 * so that notification bitmap is put in xdr_buf tail */
2891 	xdr_inline_pages(&req->rq_rcv_buf, (hdr.replen - 2) << 2,
2892 			 args->pdev->pages, args->pdev->pgbase,
2893 			 args->pdev->pglen);
2894 
2895 	encode_nops(&hdr);
2896 }
2897 
2898 /*
2899  *  Encode LAYOUTGET request
2900  */
2901 static void nfs4_xdr_enc_layoutget(struct rpc_rqst *req,
2902 				   struct xdr_stream *xdr,
2903 				   struct nfs4_layoutget_args *args)
2904 {
2905 	struct compound_hdr hdr = {
2906 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2907 	};
2908 
2909 	encode_compound_hdr(xdr, req, &hdr);
2910 	encode_sequence(xdr, &args->seq_args, &hdr);
2911 	encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2912 	encode_layoutget(xdr, args, &hdr);
2913 
2914 	xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2,
2915 	    args->layout.pages, 0, args->layout.pglen);
2916 
2917 	encode_nops(&hdr);
2918 }
2919 
2920 /*
2921  *  Encode LAYOUTCOMMIT request
2922  */
2923 static void nfs4_xdr_enc_layoutcommit(struct rpc_rqst *req,
2924 				      struct xdr_stream *xdr,
2925 				      struct nfs4_layoutcommit_args *args)
2926 {
2927 	struct nfs4_layoutcommit_data *data =
2928 		container_of(args, struct nfs4_layoutcommit_data, args);
2929 	struct compound_hdr hdr = {
2930 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2931 	};
2932 
2933 	encode_compound_hdr(xdr, req, &hdr);
2934 	encode_sequence(xdr, &args->seq_args, &hdr);
2935 	encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2936 	encode_layoutcommit(xdr, data->args.inode, args, &hdr);
2937 	encode_getfattr(xdr, args->bitmask, &hdr);
2938 	encode_nops(&hdr);
2939 }
2940 
2941 /*
2942  * Encode LAYOUTRETURN request
2943  */
2944 static void nfs4_xdr_enc_layoutreturn(struct rpc_rqst *req,
2945 				      struct xdr_stream *xdr,
2946 				      struct nfs4_layoutreturn_args *args)
2947 {
2948 	struct compound_hdr hdr = {
2949 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2950 	};
2951 
2952 	encode_compound_hdr(xdr, req, &hdr);
2953 	encode_sequence(xdr, &args->seq_args, &hdr);
2954 	encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2955 	encode_layoutreturn(xdr, args, &hdr);
2956 	encode_nops(&hdr);
2957 }
2958 
2959 /*
2960  * Encode SECINFO_NO_NAME request
2961  */
2962 static int nfs4_xdr_enc_secinfo_no_name(struct rpc_rqst *req,
2963 					struct xdr_stream *xdr,
2964 					struct nfs41_secinfo_no_name_args *args)
2965 {
2966 	struct compound_hdr hdr = {
2967 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2968 	};
2969 
2970 	encode_compound_hdr(xdr, req, &hdr);
2971 	encode_sequence(xdr, &args->seq_args, &hdr);
2972 	encode_putrootfh(xdr, &hdr);
2973 	encode_secinfo_no_name(xdr, args, &hdr);
2974 	encode_nops(&hdr);
2975 	return 0;
2976 }
2977 
2978 /*
2979  *  Encode TEST_STATEID request
2980  */
2981 static void nfs4_xdr_enc_test_stateid(struct rpc_rqst *req,
2982 				      struct xdr_stream *xdr,
2983 				      struct nfs41_test_stateid_args *args)
2984 {
2985 	struct compound_hdr hdr = {
2986 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
2987 	};
2988 
2989 	encode_compound_hdr(xdr, req, &hdr);
2990 	encode_sequence(xdr, &args->seq_args, &hdr);
2991 	encode_test_stateid(xdr, args, &hdr);
2992 	encode_nops(&hdr);
2993 }
2994 
2995 /*
2996  *  Encode FREE_STATEID request
2997  */
2998 static void nfs4_xdr_enc_free_stateid(struct rpc_rqst *req,
2999 				     struct xdr_stream *xdr,
3000 				     struct nfs41_free_stateid_args *args)
3001 {
3002 	struct compound_hdr hdr = {
3003 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
3004 	};
3005 
3006 	encode_compound_hdr(xdr, req, &hdr);
3007 	encode_sequence(xdr, &args->seq_args, &hdr);
3008 	encode_free_stateid(xdr, args, &hdr);
3009 	encode_nops(&hdr);
3010 }
3011 #endif /* CONFIG_NFS_V4_1 */
3012 
3013 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
3014 {
3015 	dprintk("nfs: %s: prematurely hit end of receive buffer. "
3016 		"Remaining buffer length is %tu words.\n",
3017 		func, xdr->end - xdr->p);
3018 }
3019 
3020 static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
3021 {
3022 	__be32 *p;
3023 
3024 	p = xdr_inline_decode(xdr, 4);
3025 	if (unlikely(!p))
3026 		goto out_overflow;
3027 	*len = be32_to_cpup(p);
3028 	p = xdr_inline_decode(xdr, *len);
3029 	if (unlikely(!p))
3030 		goto out_overflow;
3031 	*string = (char *)p;
3032 	return 0;
3033 out_overflow:
3034 	print_overflow_msg(__func__, xdr);
3035 	return -EIO;
3036 }
3037 
3038 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
3039 {
3040 	__be32 *p;
3041 
3042 	p = xdr_inline_decode(xdr, 8);
3043 	if (unlikely(!p))
3044 		goto out_overflow;
3045 	hdr->status = be32_to_cpup(p++);
3046 	hdr->taglen = be32_to_cpup(p);
3047 
3048 	p = xdr_inline_decode(xdr, hdr->taglen + 4);
3049 	if (unlikely(!p))
3050 		goto out_overflow;
3051 	hdr->tag = (char *)p;
3052 	p += XDR_QUADLEN(hdr->taglen);
3053 	hdr->nops = be32_to_cpup(p);
3054 	if (unlikely(hdr->nops < 1))
3055 		return nfs4_stat_to_errno(hdr->status);
3056 	return 0;
3057 out_overflow:
3058 	print_overflow_msg(__func__, xdr);
3059 	return -EIO;
3060 }
3061 
3062 static bool __decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected,
3063 		int *nfs_retval)
3064 {
3065 	__be32 *p;
3066 	uint32_t opnum;
3067 	int32_t nfserr;
3068 
3069 	p = xdr_inline_decode(xdr, 8);
3070 	if (unlikely(!p))
3071 		goto out_overflow;
3072 	opnum = be32_to_cpup(p++);
3073 	if (unlikely(opnum != expected))
3074 		goto out_bad_operation;
3075 	nfserr = be32_to_cpup(p);
3076 	if (nfserr == NFS_OK)
3077 		*nfs_retval = 0;
3078 	else
3079 		*nfs_retval = nfs4_stat_to_errno(nfserr);
3080 	return true;
3081 out_bad_operation:
3082 	dprintk("nfs: Server returned operation"
3083 		" %d but we issued a request for %d\n",
3084 			opnum, expected);
3085 	*nfs_retval = -EREMOTEIO;
3086 	return false;
3087 out_overflow:
3088 	print_overflow_msg(__func__, xdr);
3089 	*nfs_retval = -EIO;
3090 	return false;
3091 }
3092 
3093 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
3094 {
3095 	int retval;
3096 
3097 	__decode_op_hdr(xdr, expected, &retval);
3098 	return retval;
3099 }
3100 
3101 /* Dummy routine */
3102 static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
3103 {
3104 	__be32 *p;
3105 	unsigned int strlen;
3106 	char *str;
3107 
3108 	p = xdr_inline_decode(xdr, 12);
3109 	if (likely(p))
3110 		return decode_opaque_inline(xdr, &strlen, &str);
3111 	print_overflow_msg(__func__, xdr);
3112 	return -EIO;
3113 }
3114 
3115 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
3116 {
3117 	uint32_t bmlen;
3118 	__be32 *p;
3119 
3120 	p = xdr_inline_decode(xdr, 4);
3121 	if (unlikely(!p))
3122 		goto out_overflow;
3123 	bmlen = be32_to_cpup(p);
3124 
3125 	bitmap[0] = bitmap[1] = bitmap[2] = 0;
3126 	p = xdr_inline_decode(xdr, (bmlen << 2));
3127 	if (unlikely(!p))
3128 		goto out_overflow;
3129 	if (bmlen > 0) {
3130 		bitmap[0] = be32_to_cpup(p++);
3131 		if (bmlen > 1) {
3132 			bitmap[1] = be32_to_cpup(p++);
3133 			if (bmlen > 2)
3134 				bitmap[2] = be32_to_cpup(p);
3135 		}
3136 	}
3137 	return 0;
3138 out_overflow:
3139 	print_overflow_msg(__func__, xdr);
3140 	return -EIO;
3141 }
3142 
3143 static int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, unsigned int *savep)
3144 {
3145 	__be32 *p;
3146 
3147 	p = xdr_inline_decode(xdr, 4);
3148 	if (unlikely(!p))
3149 		goto out_overflow;
3150 	*attrlen = be32_to_cpup(p);
3151 	*savep = xdr_stream_pos(xdr);
3152 	return 0;
3153 out_overflow:
3154 	print_overflow_msg(__func__, xdr);
3155 	return -EIO;
3156 }
3157 
3158 static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
3159 {
3160 	if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
3161 		int ret;
3162 		ret = decode_attr_bitmap(xdr, bitmask);
3163 		if (unlikely(ret < 0))
3164 			return ret;
3165 		bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
3166 	} else
3167 		bitmask[0] = bitmask[1] = bitmask[2] = 0;
3168 	dprintk("%s: bitmask=%08x:%08x:%08x\n", __func__,
3169 		bitmask[0], bitmask[1], bitmask[2]);
3170 	return 0;
3171 }
3172 
3173 static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
3174 {
3175 	__be32 *p;
3176 	int ret = 0;
3177 
3178 	*type = 0;
3179 	if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
3180 		return -EIO;
3181 	if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
3182 		p = xdr_inline_decode(xdr, 4);
3183 		if (unlikely(!p))
3184 			goto out_overflow;
3185 		*type = be32_to_cpup(p);
3186 		if (*type < NF4REG || *type > NF4NAMEDATTR) {
3187 			dprintk("%s: bad type %d\n", __func__, *type);
3188 			return -EIO;
3189 		}
3190 		bitmap[0] &= ~FATTR4_WORD0_TYPE;
3191 		ret = NFS_ATTR_FATTR_TYPE;
3192 	}
3193 	dprintk("%s: type=0%o\n", __func__, nfs_type2fmt[*type]);
3194 	return ret;
3195 out_overflow:
3196 	print_overflow_msg(__func__, xdr);
3197 	return -EIO;
3198 }
3199 
3200 static int decode_attr_fh_expire_type(struct xdr_stream *xdr,
3201 				      uint32_t *bitmap, uint32_t *type)
3202 {
3203 	__be32 *p;
3204 
3205 	*type = 0;
3206 	if (unlikely(bitmap[0] & (FATTR4_WORD0_FH_EXPIRE_TYPE - 1U)))
3207 		return -EIO;
3208 	if (likely(bitmap[0] & FATTR4_WORD0_FH_EXPIRE_TYPE)) {
3209 		p = xdr_inline_decode(xdr, 4);
3210 		if (unlikely(!p))
3211 			goto out_overflow;
3212 		*type = be32_to_cpup(p);
3213 		bitmap[0] &= ~FATTR4_WORD0_FH_EXPIRE_TYPE;
3214 	}
3215 	dprintk("%s: expire type=0x%x\n", __func__, *type);
3216 	return 0;
3217 out_overflow:
3218 	print_overflow_msg(__func__, xdr);
3219 	return -EIO;
3220 }
3221 
3222 static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
3223 {
3224 	__be32 *p;
3225 	int ret = 0;
3226 
3227 	*change = 0;
3228 	if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
3229 		return -EIO;
3230 	if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
3231 		p = xdr_inline_decode(xdr, 8);
3232 		if (unlikely(!p))
3233 			goto out_overflow;
3234 		xdr_decode_hyper(p, change);
3235 		bitmap[0] &= ~FATTR4_WORD0_CHANGE;
3236 		ret = NFS_ATTR_FATTR_CHANGE;
3237 	}
3238 	dprintk("%s: change attribute=%Lu\n", __func__,
3239 			(unsigned long long)*change);
3240 	return ret;
3241 out_overflow:
3242 	print_overflow_msg(__func__, xdr);
3243 	return -EIO;
3244 }
3245 
3246 static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
3247 {
3248 	__be32 *p;
3249 	int ret = 0;
3250 
3251 	*size = 0;
3252 	if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
3253 		return -EIO;
3254 	if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
3255 		p = xdr_inline_decode(xdr, 8);
3256 		if (unlikely(!p))
3257 			goto out_overflow;
3258 		xdr_decode_hyper(p, size);
3259 		bitmap[0] &= ~FATTR4_WORD0_SIZE;
3260 		ret = NFS_ATTR_FATTR_SIZE;
3261 	}
3262 	dprintk("%s: file size=%Lu\n", __func__, (unsigned long long)*size);
3263 	return ret;
3264 out_overflow:
3265 	print_overflow_msg(__func__, xdr);
3266 	return -EIO;
3267 }
3268 
3269 static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3270 {
3271 	__be32 *p;
3272 
3273 	*res = 0;
3274 	if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
3275 		return -EIO;
3276 	if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
3277 		p = xdr_inline_decode(xdr, 4);
3278 		if (unlikely(!p))
3279 			goto out_overflow;
3280 		*res = be32_to_cpup(p);
3281 		bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
3282 	}
3283 	dprintk("%s: link support=%s\n", __func__, *res == 0 ? "false" : "true");
3284 	return 0;
3285 out_overflow:
3286 	print_overflow_msg(__func__, xdr);
3287 	return -EIO;
3288 }
3289 
3290 static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3291 {
3292 	__be32 *p;
3293 
3294 	*res = 0;
3295 	if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
3296 		return -EIO;
3297 	if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
3298 		p = xdr_inline_decode(xdr, 4);
3299 		if (unlikely(!p))
3300 			goto out_overflow;
3301 		*res = be32_to_cpup(p);
3302 		bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
3303 	}
3304 	dprintk("%s: symlink support=%s\n", __func__, *res == 0 ? "false" : "true");
3305 	return 0;
3306 out_overflow:
3307 	print_overflow_msg(__func__, xdr);
3308 	return -EIO;
3309 }
3310 
3311 static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
3312 {
3313 	__be32 *p;
3314 	int ret = 0;
3315 
3316 	fsid->major = 0;
3317 	fsid->minor = 0;
3318 	if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
3319 		return -EIO;
3320 	if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
3321 		p = xdr_inline_decode(xdr, 16);
3322 		if (unlikely(!p))
3323 			goto out_overflow;
3324 		p = xdr_decode_hyper(p, &fsid->major);
3325 		xdr_decode_hyper(p, &fsid->minor);
3326 		bitmap[0] &= ~FATTR4_WORD0_FSID;
3327 		ret = NFS_ATTR_FATTR_FSID;
3328 	}
3329 	dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __func__,
3330 			(unsigned long long)fsid->major,
3331 			(unsigned long long)fsid->minor);
3332 	return ret;
3333 out_overflow:
3334 	print_overflow_msg(__func__, xdr);
3335 	return -EIO;
3336 }
3337 
3338 static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3339 {
3340 	__be32 *p;
3341 
3342 	*res = 60;
3343 	if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
3344 		return -EIO;
3345 	if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
3346 		p = xdr_inline_decode(xdr, 4);
3347 		if (unlikely(!p))
3348 			goto out_overflow;
3349 		*res = be32_to_cpup(p);
3350 		bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
3351 	}
3352 	dprintk("%s: file size=%u\n", __func__, (unsigned int)*res);
3353 	return 0;
3354 out_overflow:
3355 	print_overflow_msg(__func__, xdr);
3356 	return -EIO;
3357 }
3358 
3359 static int decode_attr_error(struct xdr_stream *xdr, uint32_t *bitmap, int32_t *res)
3360 {
3361 	__be32 *p;
3362 
3363 	if (unlikely(bitmap[0] & (FATTR4_WORD0_RDATTR_ERROR - 1U)))
3364 		return -EIO;
3365 	if (likely(bitmap[0] & FATTR4_WORD0_RDATTR_ERROR)) {
3366 		p = xdr_inline_decode(xdr, 4);
3367 		if (unlikely(!p))
3368 			goto out_overflow;
3369 		bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
3370 		*res = -be32_to_cpup(p);
3371 	}
3372 	return 0;
3373 out_overflow:
3374 	print_overflow_msg(__func__, xdr);
3375 	return -EIO;
3376 }
3377 
3378 static int decode_attr_filehandle(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fh *fh)
3379 {
3380 	__be32 *p;
3381 	int len;
3382 
3383 	if (fh != NULL)
3384 		memset(fh, 0, sizeof(*fh));
3385 
3386 	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEHANDLE - 1U)))
3387 		return -EIO;
3388 	if (likely(bitmap[0] & FATTR4_WORD0_FILEHANDLE)) {
3389 		p = xdr_inline_decode(xdr, 4);
3390 		if (unlikely(!p))
3391 			goto out_overflow;
3392 		len = be32_to_cpup(p);
3393 		if (len > NFS4_FHSIZE)
3394 			return -EIO;
3395 		p = xdr_inline_decode(xdr, len);
3396 		if (unlikely(!p))
3397 			goto out_overflow;
3398 		if (fh != NULL) {
3399 			memcpy(fh->data, p, len);
3400 			fh->size = len;
3401 		}
3402 		bitmap[0] &= ~FATTR4_WORD0_FILEHANDLE;
3403 	}
3404 	return 0;
3405 out_overflow:
3406 	print_overflow_msg(__func__, xdr);
3407 	return -EIO;
3408 }
3409 
3410 static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3411 {
3412 	__be32 *p;
3413 
3414 	*res = 0;
3415 	if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
3416 		return -EIO;
3417 	if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
3418 		p = xdr_inline_decode(xdr, 4);
3419 		if (unlikely(!p))
3420 			goto out_overflow;
3421 		*res = be32_to_cpup(p);
3422 		bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
3423 	}
3424 	dprintk("%s: ACLs supported=%u\n", __func__, (unsigned int)*res);
3425 	return 0;
3426 out_overflow:
3427 	print_overflow_msg(__func__, xdr);
3428 	return -EIO;
3429 }
3430 
3431 static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
3432 {
3433 	__be32 *p;
3434 	int ret = 0;
3435 
3436 	*fileid = 0;
3437 	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
3438 		return -EIO;
3439 	if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
3440 		p = xdr_inline_decode(xdr, 8);
3441 		if (unlikely(!p))
3442 			goto out_overflow;
3443 		xdr_decode_hyper(p, fileid);
3444 		bitmap[0] &= ~FATTR4_WORD0_FILEID;
3445 		ret = NFS_ATTR_FATTR_FILEID;
3446 	}
3447 	dprintk("%s: fileid=%Lu\n", __func__, (unsigned long long)*fileid);
3448 	return ret;
3449 out_overflow:
3450 	print_overflow_msg(__func__, xdr);
3451 	return -EIO;
3452 }
3453 
3454 static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
3455 {
3456 	__be32 *p;
3457 	int ret = 0;
3458 
3459 	*fileid = 0;
3460 	if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
3461 		return -EIO;
3462 	if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
3463 		p = xdr_inline_decode(xdr, 8);
3464 		if (unlikely(!p))
3465 			goto out_overflow;
3466 		xdr_decode_hyper(p, fileid);
3467 		bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
3468 		ret = NFS_ATTR_FATTR_MOUNTED_ON_FILEID;
3469 	}
3470 	dprintk("%s: fileid=%Lu\n", __func__, (unsigned long long)*fileid);
3471 	return ret;
3472 out_overflow:
3473 	print_overflow_msg(__func__, xdr);
3474 	return -EIO;
3475 }
3476 
3477 static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3478 {
3479 	__be32 *p;
3480 	int status = 0;
3481 
3482 	*res = 0;
3483 	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
3484 		return -EIO;
3485 	if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
3486 		p = xdr_inline_decode(xdr, 8);
3487 		if (unlikely(!p))
3488 			goto out_overflow;
3489 		xdr_decode_hyper(p, res);
3490 		bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
3491 	}
3492 	dprintk("%s: files avail=%Lu\n", __func__, (unsigned long long)*res);
3493 	return status;
3494 out_overflow:
3495 	print_overflow_msg(__func__, xdr);
3496 	return -EIO;
3497 }
3498 
3499 static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3500 {
3501 	__be32 *p;
3502 	int status = 0;
3503 
3504 	*res = 0;
3505 	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
3506 		return -EIO;
3507 	if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
3508 		p = xdr_inline_decode(xdr, 8);
3509 		if (unlikely(!p))
3510 			goto out_overflow;
3511 		xdr_decode_hyper(p, res);
3512 		bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
3513 	}
3514 	dprintk("%s: files free=%Lu\n", __func__, (unsigned long long)*res);
3515 	return status;
3516 out_overflow:
3517 	print_overflow_msg(__func__, xdr);
3518 	return -EIO;
3519 }
3520 
3521 static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3522 {
3523 	__be32 *p;
3524 	int status = 0;
3525 
3526 	*res = 0;
3527 	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
3528 		return -EIO;
3529 	if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
3530 		p = xdr_inline_decode(xdr, 8);
3531 		if (unlikely(!p))
3532 			goto out_overflow;
3533 		xdr_decode_hyper(p, res);
3534 		bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
3535 	}
3536 	dprintk("%s: files total=%Lu\n", __func__, (unsigned long long)*res);
3537 	return status;
3538 out_overflow:
3539 	print_overflow_msg(__func__, xdr);
3540 	return -EIO;
3541 }
3542 
3543 static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
3544 {
3545 	u32 n;
3546 	__be32 *p;
3547 	int status = 0;
3548 
3549 	p = xdr_inline_decode(xdr, 4);
3550 	if (unlikely(!p))
3551 		goto out_overflow;
3552 	n = be32_to_cpup(p);
3553 	if (n == 0)
3554 		goto root_path;
3555 	dprintk("pathname4: ");
3556 	if (n > NFS4_PATHNAME_MAXCOMPONENTS) {
3557 		dprintk("cannot parse %d components in path\n", n);
3558 		goto out_eio;
3559 	}
3560 	for (path->ncomponents = 0; path->ncomponents < n; path->ncomponents++) {
3561 		struct nfs4_string *component = &path->components[path->ncomponents];
3562 		status = decode_opaque_inline(xdr, &component->len, &component->data);
3563 		if (unlikely(status != 0))
3564 			goto out_eio;
3565 		ifdebug (XDR)
3566 			pr_cont("%s%.*s ",
3567 				(path->ncomponents != n ? "/ " : ""),
3568 				component->len, component->data);
3569 	}
3570 out:
3571 	return status;
3572 root_path:
3573 /* a root pathname is sent as a zero component4 */
3574 	path->ncomponents = 1;
3575 	path->components[0].len=0;
3576 	path->components[0].data=NULL;
3577 	dprintk("pathname4: /\n");
3578 	goto out;
3579 out_eio:
3580 	dprintk(" status %d", status);
3581 	status = -EIO;
3582 	goto out;
3583 out_overflow:
3584 	print_overflow_msg(__func__, xdr);
3585 	return -EIO;
3586 }
3587 
3588 static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
3589 {
3590 	int n;
3591 	__be32 *p;
3592 	int status = -EIO;
3593 
3594 	if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
3595 		goto out;
3596 	status = 0;
3597 	if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
3598 		goto out;
3599 	status = -EIO;
3600 	/* Ignore borken servers that return unrequested attrs */
3601 	if (unlikely(res == NULL))
3602 		goto out;
3603 	dprintk("%s: fsroot:\n", __func__);
3604 	status = decode_pathname(xdr, &res->fs_path);
3605 	if (unlikely(status != 0))
3606 		goto out;
3607 	p = xdr_inline_decode(xdr, 4);
3608 	if (unlikely(!p))
3609 		goto out_overflow;
3610 	n = be32_to_cpup(p);
3611 	if (n <= 0)
3612 		goto out_eio;
3613 	for (res->nlocations = 0; res->nlocations < n; res->nlocations++) {
3614 		u32 m;
3615 		struct nfs4_fs_location *loc;
3616 
3617 		if (res->nlocations == NFS4_FS_LOCATIONS_MAXENTRIES)
3618 			break;
3619 		loc = &res->locations[res->nlocations];
3620 		p = xdr_inline_decode(xdr, 4);
3621 		if (unlikely(!p))
3622 			goto out_overflow;
3623 		m = be32_to_cpup(p);
3624 
3625 		dprintk("%s: servers:\n", __func__);
3626 		for (loc->nservers = 0; loc->nservers < m; loc->nservers++) {
3627 			struct nfs4_string *server;
3628 
3629 			if (loc->nservers == NFS4_FS_LOCATION_MAXSERVERS) {
3630 				unsigned int i;
3631 				dprintk("%s: using first %u of %u servers "
3632 					"returned for location %u\n",
3633 						__func__,
3634 						NFS4_FS_LOCATION_MAXSERVERS,
3635 						m, res->nlocations);
3636 				for (i = loc->nservers; i < m; i++) {
3637 					unsigned int len;
3638 					char *data;
3639 					status = decode_opaque_inline(xdr, &len, &data);
3640 					if (unlikely(status != 0))
3641 						goto out_eio;
3642 				}
3643 				break;
3644 			}
3645 			server = &loc->servers[loc->nservers];
3646 			status = decode_opaque_inline(xdr, &server->len, &server->data);
3647 			if (unlikely(status != 0))
3648 				goto out_eio;
3649 			dprintk("%s ", server->data);
3650 		}
3651 		status = decode_pathname(xdr, &loc->rootpath);
3652 		if (unlikely(status != 0))
3653 			goto out_eio;
3654 	}
3655 	if (res->nlocations != 0)
3656 		status = NFS_ATTR_FATTR_V4_LOCATIONS;
3657 out:
3658 	dprintk("%s: fs_locations done, error = %d\n", __func__, status);
3659 	return status;
3660 out_overflow:
3661 	print_overflow_msg(__func__, xdr);
3662 out_eio:
3663 	status = -EIO;
3664 	goto out;
3665 }
3666 
3667 static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3668 {
3669 	__be32 *p;
3670 	int status = 0;
3671 
3672 	*res = 0;
3673 	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
3674 		return -EIO;
3675 	if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
3676 		p = xdr_inline_decode(xdr, 8);
3677 		if (unlikely(!p))
3678 			goto out_overflow;
3679 		xdr_decode_hyper(p, res);
3680 		bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
3681 	}
3682 	dprintk("%s: maxfilesize=%Lu\n", __func__, (unsigned long long)*res);
3683 	return status;
3684 out_overflow:
3685 	print_overflow_msg(__func__, xdr);
3686 	return -EIO;
3687 }
3688 
3689 static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
3690 {
3691 	__be32 *p;
3692 	int status = 0;
3693 
3694 	*maxlink = 1;
3695 	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
3696 		return -EIO;
3697 	if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
3698 		p = xdr_inline_decode(xdr, 4);
3699 		if (unlikely(!p))
3700 			goto out_overflow;
3701 		*maxlink = be32_to_cpup(p);
3702 		bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
3703 	}
3704 	dprintk("%s: maxlink=%u\n", __func__, *maxlink);
3705 	return status;
3706 out_overflow:
3707 	print_overflow_msg(__func__, xdr);
3708 	return -EIO;
3709 }
3710 
3711 static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
3712 {
3713 	__be32 *p;
3714 	int status = 0;
3715 
3716 	*maxname = 1024;
3717 	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
3718 		return -EIO;
3719 	if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
3720 		p = xdr_inline_decode(xdr, 4);
3721 		if (unlikely(!p))
3722 			goto out_overflow;
3723 		*maxname = be32_to_cpup(p);
3724 		bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
3725 	}
3726 	dprintk("%s: maxname=%u\n", __func__, *maxname);
3727 	return status;
3728 out_overflow:
3729 	print_overflow_msg(__func__, xdr);
3730 	return -EIO;
3731 }
3732 
3733 static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3734 {
3735 	__be32 *p;
3736 	int status = 0;
3737 
3738 	*res = 1024;
3739 	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
3740 		return -EIO;
3741 	if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
3742 		uint64_t maxread;
3743 		p = xdr_inline_decode(xdr, 8);
3744 		if (unlikely(!p))
3745 			goto out_overflow;
3746 		xdr_decode_hyper(p, &maxread);
3747 		if (maxread > 0x7FFFFFFF)
3748 			maxread = 0x7FFFFFFF;
3749 		*res = (uint32_t)maxread;
3750 		bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
3751 	}
3752 	dprintk("%s: maxread=%lu\n", __func__, (unsigned long)*res);
3753 	return status;
3754 out_overflow:
3755 	print_overflow_msg(__func__, xdr);
3756 	return -EIO;
3757 }
3758 
3759 static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3760 {
3761 	__be32 *p;
3762 	int status = 0;
3763 
3764 	*res = 1024;
3765 	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
3766 		return -EIO;
3767 	if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
3768 		uint64_t maxwrite;
3769 		p = xdr_inline_decode(xdr, 8);
3770 		if (unlikely(!p))
3771 			goto out_overflow;
3772 		xdr_decode_hyper(p, &maxwrite);
3773 		if (maxwrite > 0x7FFFFFFF)
3774 			maxwrite = 0x7FFFFFFF;
3775 		*res = (uint32_t)maxwrite;
3776 		bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
3777 	}
3778 	dprintk("%s: maxwrite=%lu\n", __func__, (unsigned long)*res);
3779 	return status;
3780 out_overflow:
3781 	print_overflow_msg(__func__, xdr);
3782 	return -EIO;
3783 }
3784 
3785 static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, umode_t *mode)
3786 {
3787 	uint32_t tmp;
3788 	__be32 *p;
3789 	int ret = 0;
3790 
3791 	*mode = 0;
3792 	if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
3793 		return -EIO;
3794 	if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
3795 		p = xdr_inline_decode(xdr, 4);
3796 		if (unlikely(!p))
3797 			goto out_overflow;
3798 		tmp = be32_to_cpup(p);
3799 		*mode = tmp & ~S_IFMT;
3800 		bitmap[1] &= ~FATTR4_WORD1_MODE;
3801 		ret = NFS_ATTR_FATTR_MODE;
3802 	}
3803 	dprintk("%s: file mode=0%o\n", __func__, (unsigned int)*mode);
3804 	return ret;
3805 out_overflow:
3806 	print_overflow_msg(__func__, xdr);
3807 	return -EIO;
3808 }
3809 
3810 static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
3811 {
3812 	__be32 *p;
3813 	int ret = 0;
3814 
3815 	*nlink = 1;
3816 	if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
3817 		return -EIO;
3818 	if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
3819 		p = xdr_inline_decode(xdr, 4);
3820 		if (unlikely(!p))
3821 			goto out_overflow;
3822 		*nlink = be32_to_cpup(p);
3823 		bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
3824 		ret = NFS_ATTR_FATTR_NLINK;
3825 	}
3826 	dprintk("%s: nlink=%u\n", __func__, (unsigned int)*nlink);
3827 	return ret;
3828 out_overflow:
3829 	print_overflow_msg(__func__, xdr);
3830 	return -EIO;
3831 }
3832 
3833 static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap,
3834 		const struct nfs_server *server, kuid_t *uid,
3835 		struct nfs4_string *owner_name)
3836 {
3837 	uint32_t len;
3838 	__be32 *p;
3839 	int ret = 0;
3840 
3841 	*uid = make_kuid(&init_user_ns, -2);
3842 	if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
3843 		return -EIO;
3844 	if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
3845 		p = xdr_inline_decode(xdr, 4);
3846 		if (unlikely(!p))
3847 			goto out_overflow;
3848 		len = be32_to_cpup(p);
3849 		p = xdr_inline_decode(xdr, len);
3850 		if (unlikely(!p))
3851 			goto out_overflow;
3852 		if (owner_name != NULL) {
3853 			owner_name->data = kmemdup(p, len, GFP_NOWAIT);
3854 			if (owner_name->data != NULL) {
3855 				owner_name->len = len;
3856 				ret = NFS_ATTR_FATTR_OWNER_NAME;
3857 			}
3858 		} else if (len < XDR_MAX_NETOBJ) {
3859 			if (nfs_map_name_to_uid(server, (char *)p, len, uid) == 0)
3860 				ret = NFS_ATTR_FATTR_OWNER;
3861 			else
3862 				dprintk("%s: nfs_map_name_to_uid failed!\n",
3863 						__func__);
3864 		} else
3865 			dprintk("%s: name too long (%u)!\n",
3866 					__func__, len);
3867 		bitmap[1] &= ~FATTR4_WORD1_OWNER;
3868 	}
3869 	dprintk("%s: uid=%d\n", __func__, (int)from_kuid(&init_user_ns, *uid));
3870 	return ret;
3871 out_overflow:
3872 	print_overflow_msg(__func__, xdr);
3873 	return -EIO;
3874 }
3875 
3876 static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap,
3877 		const struct nfs_server *server, kgid_t *gid,
3878 		struct nfs4_string *group_name)
3879 {
3880 	uint32_t len;
3881 	__be32 *p;
3882 	int ret = 0;
3883 
3884 	*gid = make_kgid(&init_user_ns, -2);
3885 	if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
3886 		return -EIO;
3887 	if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
3888 		p = xdr_inline_decode(xdr, 4);
3889 		if (unlikely(!p))
3890 			goto out_overflow;
3891 		len = be32_to_cpup(p);
3892 		p = xdr_inline_decode(xdr, len);
3893 		if (unlikely(!p))
3894 			goto out_overflow;
3895 		if (group_name != NULL) {
3896 			group_name->data = kmemdup(p, len, GFP_NOWAIT);
3897 			if (group_name->data != NULL) {
3898 				group_name->len = len;
3899 				ret = NFS_ATTR_FATTR_GROUP_NAME;
3900 			}
3901 		} else if (len < XDR_MAX_NETOBJ) {
3902 			if (nfs_map_group_to_gid(server, (char *)p, len, gid) == 0)
3903 				ret = NFS_ATTR_FATTR_GROUP;
3904 			else
3905 				dprintk("%s: nfs_map_group_to_gid failed!\n",
3906 						__func__);
3907 		} else
3908 			dprintk("%s: name too long (%u)!\n",
3909 					__func__, len);
3910 		bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
3911 	}
3912 	dprintk("%s: gid=%d\n", __func__, (int)from_kgid(&init_user_ns, *gid));
3913 	return ret;
3914 out_overflow:
3915 	print_overflow_msg(__func__, xdr);
3916 	return -EIO;
3917 }
3918 
3919 static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
3920 {
3921 	uint32_t major = 0, minor = 0;
3922 	__be32 *p;
3923 	int ret = 0;
3924 
3925 	*rdev = MKDEV(0,0);
3926 	if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
3927 		return -EIO;
3928 	if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
3929 		dev_t tmp;
3930 
3931 		p = xdr_inline_decode(xdr, 8);
3932 		if (unlikely(!p))
3933 			goto out_overflow;
3934 		major = be32_to_cpup(p++);
3935 		minor = be32_to_cpup(p);
3936 		tmp = MKDEV(major, minor);
3937 		if (MAJOR(tmp) == major && MINOR(tmp) == minor)
3938 			*rdev = tmp;
3939 		bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
3940 		ret = NFS_ATTR_FATTR_RDEV;
3941 	}
3942 	dprintk("%s: rdev=(0x%x:0x%x)\n", __func__, major, minor);
3943 	return ret;
3944 out_overflow:
3945 	print_overflow_msg(__func__, xdr);
3946 	return -EIO;
3947 }
3948 
3949 static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3950 {
3951 	__be32 *p;
3952 	int status = 0;
3953 
3954 	*res = 0;
3955 	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
3956 		return -EIO;
3957 	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
3958 		p = xdr_inline_decode(xdr, 8);
3959 		if (unlikely(!p))
3960 			goto out_overflow;
3961 		xdr_decode_hyper(p, res);
3962 		bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
3963 	}
3964 	dprintk("%s: space avail=%Lu\n", __func__, (unsigned long long)*res);
3965 	return status;
3966 out_overflow:
3967 	print_overflow_msg(__func__, xdr);
3968 	return -EIO;
3969 }
3970 
3971 static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3972 {
3973 	__be32 *p;
3974 	int status = 0;
3975 
3976 	*res = 0;
3977 	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
3978 		return -EIO;
3979 	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
3980 		p = xdr_inline_decode(xdr, 8);
3981 		if (unlikely(!p))
3982 			goto out_overflow;
3983 		xdr_decode_hyper(p, res);
3984 		bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
3985 	}
3986 	dprintk("%s: space free=%Lu\n", __func__, (unsigned long long)*res);
3987 	return status;
3988 out_overflow:
3989 	print_overflow_msg(__func__, xdr);
3990 	return -EIO;
3991 }
3992 
3993 static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3994 {
3995 	__be32 *p;
3996 	int status = 0;
3997 
3998 	*res = 0;
3999 	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
4000 		return -EIO;
4001 	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
4002 		p = xdr_inline_decode(xdr, 8);
4003 		if (unlikely(!p))
4004 			goto out_overflow;
4005 		xdr_decode_hyper(p, res);
4006 		bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
4007 	}
4008 	dprintk("%s: space total=%Lu\n", __func__, (unsigned long long)*res);
4009 	return status;
4010 out_overflow:
4011 	print_overflow_msg(__func__, xdr);
4012 	return -EIO;
4013 }
4014 
4015 static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
4016 {
4017 	__be32 *p;
4018 	int ret = 0;
4019 
4020 	*used = 0;
4021 	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
4022 		return -EIO;
4023 	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
4024 		p = xdr_inline_decode(xdr, 8);
4025 		if (unlikely(!p))
4026 			goto out_overflow;
4027 		xdr_decode_hyper(p, used);
4028 		bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
4029 		ret = NFS_ATTR_FATTR_SPACE_USED;
4030 	}
4031 	dprintk("%s: space used=%Lu\n", __func__,
4032 			(unsigned long long)*used);
4033 	return ret;
4034 out_overflow:
4035 	print_overflow_msg(__func__, xdr);
4036 	return -EIO;
4037 }
4038 
4039 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
4040 {
4041 	__be32 *p;
4042 	uint64_t sec;
4043 	uint32_t nsec;
4044 
4045 	p = xdr_inline_decode(xdr, 12);
4046 	if (unlikely(!p))
4047 		goto out_overflow;
4048 	p = xdr_decode_hyper(p, &sec);
4049 	nsec = be32_to_cpup(p);
4050 	time->tv_sec = (time_t)sec;
4051 	time->tv_nsec = (long)nsec;
4052 	return 0;
4053 out_overflow:
4054 	print_overflow_msg(__func__, xdr);
4055 	return -EIO;
4056 }
4057 
4058 static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
4059 {
4060 	int status = 0;
4061 
4062 	time->tv_sec = 0;
4063 	time->tv_nsec = 0;
4064 	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
4065 		return -EIO;
4066 	if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
4067 		status = decode_attr_time(xdr, time);
4068 		if (status == 0)
4069 			status = NFS_ATTR_FATTR_ATIME;
4070 		bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
4071 	}
4072 	dprintk("%s: atime=%ld\n", __func__, (long)time->tv_sec);
4073 	return status;
4074 }
4075 
4076 static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
4077 {
4078 	int status = 0;
4079 
4080 	time->tv_sec = 0;
4081 	time->tv_nsec = 0;
4082 	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
4083 		return -EIO;
4084 	if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
4085 		status = decode_attr_time(xdr, time);
4086 		if (status == 0)
4087 			status = NFS_ATTR_FATTR_CTIME;
4088 		bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
4089 	}
4090 	dprintk("%s: ctime=%ld\n", __func__, (long)time->tv_sec);
4091 	return status;
4092 }
4093 
4094 static int decode_attr_time_delta(struct xdr_stream *xdr, uint32_t *bitmap,
4095 				  struct timespec *time)
4096 {
4097 	int status = 0;
4098 
4099 	time->tv_sec = 0;
4100 	time->tv_nsec = 0;
4101 	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_DELTA - 1U)))
4102 		return -EIO;
4103 	if (likely(bitmap[1] & FATTR4_WORD1_TIME_DELTA)) {
4104 		status = decode_attr_time(xdr, time);
4105 		bitmap[1] &= ~FATTR4_WORD1_TIME_DELTA;
4106 	}
4107 	dprintk("%s: time_delta=%ld %ld\n", __func__, (long)time->tv_sec,
4108 		(long)time->tv_nsec);
4109 	return status;
4110 }
4111 
4112 static int decode_attr_security_label(struct xdr_stream *xdr, uint32_t *bitmap,
4113 					struct nfs4_label *label)
4114 {
4115 	uint32_t pi = 0;
4116 	uint32_t lfs = 0;
4117 	__u32 len;
4118 	__be32 *p;
4119 	int status = 0;
4120 
4121 	if (unlikely(bitmap[2] & (FATTR4_WORD2_SECURITY_LABEL - 1U)))
4122 		return -EIO;
4123 	if (likely(bitmap[2] & FATTR4_WORD2_SECURITY_LABEL)) {
4124 		p = xdr_inline_decode(xdr, 4);
4125 		if (unlikely(!p))
4126 			goto out_overflow;
4127 		lfs = be32_to_cpup(p++);
4128 		p = xdr_inline_decode(xdr, 4);
4129 		if (unlikely(!p))
4130 			goto out_overflow;
4131 		pi = be32_to_cpup(p++);
4132 		p = xdr_inline_decode(xdr, 4);
4133 		if (unlikely(!p))
4134 			goto out_overflow;
4135 		len = be32_to_cpup(p++);
4136 		p = xdr_inline_decode(xdr, len);
4137 		if (unlikely(!p))
4138 			goto out_overflow;
4139 		if (len < NFS4_MAXLABELLEN) {
4140 			if (label) {
4141 				memcpy(label->label, p, len);
4142 				label->len = len;
4143 				label->pi = pi;
4144 				label->lfs = lfs;
4145 				status = NFS_ATTR_FATTR_V4_SECURITY_LABEL;
4146 			}
4147 			bitmap[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
4148 		} else
4149 			printk(KERN_WARNING "%s: label too long (%u)!\n",
4150 					__func__, len);
4151 	}
4152 	if (label && label->label)
4153 		dprintk("%s: label=%s, len=%d, PI=%d, LFS=%d\n", __func__,
4154 			(char *)label->label, label->len, label->pi, label->lfs);
4155 	return status;
4156 
4157 out_overflow:
4158 	print_overflow_msg(__func__, xdr);
4159 	return -EIO;
4160 }
4161 
4162 static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
4163 {
4164 	int status = 0;
4165 
4166 	time->tv_sec = 0;
4167 	time->tv_nsec = 0;
4168 	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
4169 		return -EIO;
4170 	if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
4171 		status = decode_attr_time(xdr, time);
4172 		if (status == 0)
4173 			status = NFS_ATTR_FATTR_MTIME;
4174 		bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
4175 	}
4176 	dprintk("%s: mtime=%ld\n", __func__, (long)time->tv_sec);
4177 	return status;
4178 }
4179 
4180 static int verify_attr_len(struct xdr_stream *xdr, unsigned int savep, uint32_t attrlen)
4181 {
4182 	unsigned int attrwords = XDR_QUADLEN(attrlen);
4183 	unsigned int nwords = (xdr_stream_pos(xdr) - savep) >> 2;
4184 
4185 	if (unlikely(attrwords != nwords)) {
4186 		dprintk("%s: server returned incorrect attribute length: "
4187 			"%u %c %u\n",
4188 				__func__,
4189 				attrwords << 2,
4190 				(attrwords < nwords) ? '<' : '>',
4191 				nwords << 2);
4192 		return -EIO;
4193 	}
4194 	return 0;
4195 }
4196 
4197 static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
4198 {
4199 	__be32 *p;
4200 
4201 	p = xdr_inline_decode(xdr, 20);
4202 	if (unlikely(!p))
4203 		goto out_overflow;
4204 	cinfo->atomic = be32_to_cpup(p++);
4205 	p = xdr_decode_hyper(p, &cinfo->before);
4206 	xdr_decode_hyper(p, &cinfo->after);
4207 	return 0;
4208 out_overflow:
4209 	print_overflow_msg(__func__, xdr);
4210 	return -EIO;
4211 }
4212 
4213 static int decode_access(struct xdr_stream *xdr, u32 *supported, u32 *access)
4214 {
4215 	__be32 *p;
4216 	uint32_t supp, acc;
4217 	int status;
4218 
4219 	status = decode_op_hdr(xdr, OP_ACCESS);
4220 	if (status)
4221 		return status;
4222 	p = xdr_inline_decode(xdr, 8);
4223 	if (unlikely(!p))
4224 		goto out_overflow;
4225 	supp = be32_to_cpup(p++);
4226 	acc = be32_to_cpup(p);
4227 	*supported = supp;
4228 	*access = acc;
4229 	return 0;
4230 out_overflow:
4231 	print_overflow_msg(__func__, xdr);
4232 	return -EIO;
4233 }
4234 
4235 static int decode_opaque_fixed(struct xdr_stream *xdr, void *buf, size_t len)
4236 {
4237 	__be32 *p;
4238 
4239 	p = xdr_inline_decode(xdr, len);
4240 	if (likely(p)) {
4241 		memcpy(buf, p, len);
4242 		return 0;
4243 	}
4244 	print_overflow_msg(__func__, xdr);
4245 	return -EIO;
4246 }
4247 
4248 static int decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
4249 {
4250 	return decode_opaque_fixed(xdr, stateid, NFS4_STATEID_SIZE);
4251 }
4252 
4253 static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
4254 {
4255 	int status;
4256 
4257 	status = decode_op_hdr(xdr, OP_CLOSE);
4258 	if (status != -EIO)
4259 		nfs_increment_open_seqid(status, res->seqid);
4260 	if (!status)
4261 		status = decode_stateid(xdr, &res->stateid);
4262 	return status;
4263 }
4264 
4265 static int decode_verifier(struct xdr_stream *xdr, void *verifier)
4266 {
4267 	return decode_opaque_fixed(xdr, verifier, NFS4_VERIFIER_SIZE);
4268 }
4269 
4270 static int decode_write_verifier(struct xdr_stream *xdr, struct nfs_write_verifier *verifier)
4271 {
4272 	return decode_opaque_fixed(xdr, verifier->data, NFS4_VERIFIER_SIZE);
4273 }
4274 
4275 static int decode_commit(struct xdr_stream *xdr, struct nfs_commitres *res)
4276 {
4277 	int status;
4278 
4279 	status = decode_op_hdr(xdr, OP_COMMIT);
4280 	if (!status)
4281 		status = decode_write_verifier(xdr, &res->verf->verifier);
4282 	return status;
4283 }
4284 
4285 static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
4286 {
4287 	__be32 *p;
4288 	uint32_t bmlen;
4289 	int status;
4290 
4291 	status = decode_op_hdr(xdr, OP_CREATE);
4292 	if (status)
4293 		return status;
4294 	if ((status = decode_change_info(xdr, cinfo)))
4295 		return status;
4296 	p = xdr_inline_decode(xdr, 4);
4297 	if (unlikely(!p))
4298 		goto out_overflow;
4299 	bmlen = be32_to_cpup(p);
4300 	p = xdr_inline_decode(xdr, bmlen << 2);
4301 	if (likely(p))
4302 		return 0;
4303 out_overflow:
4304 	print_overflow_msg(__func__, xdr);
4305 	return -EIO;
4306 }
4307 
4308 static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
4309 {
4310 	unsigned int savep;
4311 	uint32_t attrlen, bitmap[3] = {0};
4312 	int status;
4313 
4314 	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4315 		goto xdr_error;
4316 	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4317 		goto xdr_error;
4318 	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4319 		goto xdr_error;
4320 	if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
4321 		goto xdr_error;
4322 	if ((status = decode_attr_fh_expire_type(xdr, bitmap,
4323 						 &res->fh_expire_type)) != 0)
4324 		goto xdr_error;
4325 	if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
4326 		goto xdr_error;
4327 	if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
4328 		goto xdr_error;
4329 	if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
4330 		goto xdr_error;
4331 	status = verify_attr_len(xdr, savep, attrlen);
4332 xdr_error:
4333 	dprintk("%s: xdr returned %d!\n", __func__, -status);
4334 	return status;
4335 }
4336 
4337 static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
4338 {
4339 	unsigned int savep;
4340 	uint32_t attrlen, bitmap[3] = {0};
4341 	int status;
4342 
4343 	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4344 		goto xdr_error;
4345 	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4346 		goto xdr_error;
4347 	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4348 		goto xdr_error;
4349 
4350 	if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
4351 		goto xdr_error;
4352 	if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
4353 		goto xdr_error;
4354 	if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
4355 		goto xdr_error;
4356 	if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
4357 		goto xdr_error;
4358 	if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
4359 		goto xdr_error;
4360 	if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
4361 		goto xdr_error;
4362 
4363 	status = verify_attr_len(xdr, savep, attrlen);
4364 xdr_error:
4365 	dprintk("%s: xdr returned %d!\n", __func__, -status);
4366 	return status;
4367 }
4368 
4369 static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
4370 {
4371 	unsigned int savep;
4372 	uint32_t attrlen, bitmap[3] = {0};
4373 	int status;
4374 
4375 	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4376 		goto xdr_error;
4377 	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4378 		goto xdr_error;
4379 	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4380 		goto xdr_error;
4381 
4382 	if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
4383 		goto xdr_error;
4384 	if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
4385 		goto xdr_error;
4386 
4387 	status = verify_attr_len(xdr, savep, attrlen);
4388 xdr_error:
4389 	dprintk("%s: xdr returned %d!\n", __func__, -status);
4390 	return status;
4391 }
4392 
4393 static int decode_threshold_hint(struct xdr_stream *xdr,
4394 				  uint32_t *bitmap,
4395 				  uint64_t *res,
4396 				  uint32_t hint_bit)
4397 {
4398 	__be32 *p;
4399 
4400 	*res = 0;
4401 	if (likely(bitmap[0] & hint_bit)) {
4402 		p = xdr_inline_decode(xdr, 8);
4403 		if (unlikely(!p))
4404 			goto out_overflow;
4405 		xdr_decode_hyper(p, res);
4406 	}
4407 	return 0;
4408 out_overflow:
4409 	print_overflow_msg(__func__, xdr);
4410 	return -EIO;
4411 }
4412 
4413 static int decode_first_threshold_item4(struct xdr_stream *xdr,
4414 					struct nfs4_threshold *res)
4415 {
4416 	__be32 *p;
4417 	unsigned int savep;
4418 	uint32_t bitmap[3] = {0,}, attrlen;
4419 	int status;
4420 
4421 	/* layout type */
4422 	p = xdr_inline_decode(xdr, 4);
4423 	if (unlikely(!p)) {
4424 		print_overflow_msg(__func__, xdr);
4425 		return -EIO;
4426 	}
4427 	res->l_type = be32_to_cpup(p);
4428 
4429 	/* thi_hintset bitmap */
4430 	status = decode_attr_bitmap(xdr, bitmap);
4431 	if (status < 0)
4432 		goto xdr_error;
4433 
4434 	/* thi_hintlist length */
4435 	status = decode_attr_length(xdr, &attrlen, &savep);
4436 	if (status < 0)
4437 		goto xdr_error;
4438 	/* thi_hintlist */
4439 	status = decode_threshold_hint(xdr, bitmap, &res->rd_sz, THRESHOLD_RD);
4440 	if (status < 0)
4441 		goto xdr_error;
4442 	status = decode_threshold_hint(xdr, bitmap, &res->wr_sz, THRESHOLD_WR);
4443 	if (status < 0)
4444 		goto xdr_error;
4445 	status = decode_threshold_hint(xdr, bitmap, &res->rd_io_sz,
4446 				       THRESHOLD_RD_IO);
4447 	if (status < 0)
4448 		goto xdr_error;
4449 	status = decode_threshold_hint(xdr, bitmap, &res->wr_io_sz,
4450 				       THRESHOLD_WR_IO);
4451 	if (status < 0)
4452 		goto xdr_error;
4453 
4454 	status = verify_attr_len(xdr, savep, attrlen);
4455 	res->bm = bitmap[0];
4456 
4457 	dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
4458 		 __func__, res->bm, res->rd_sz, res->wr_sz, res->rd_io_sz,
4459 		res->wr_io_sz);
4460 xdr_error:
4461 	dprintk("%s ret=%d!\n", __func__, status);
4462 	return status;
4463 }
4464 
4465 /*
4466  * Thresholds on pNFS direct I/O vrs MDS I/O
4467  */
4468 static int decode_attr_mdsthreshold(struct xdr_stream *xdr,
4469 				    uint32_t *bitmap,
4470 				    struct nfs4_threshold *res)
4471 {
4472 	__be32 *p;
4473 	int status = 0;
4474 	uint32_t num;
4475 
4476 	if (unlikely(bitmap[2] & (FATTR4_WORD2_MDSTHRESHOLD - 1U)))
4477 		return -EIO;
4478 	if (bitmap[2] & FATTR4_WORD2_MDSTHRESHOLD) {
4479 		/* Did the server return an unrequested attribute? */
4480 		if (unlikely(res == NULL))
4481 			return -EREMOTEIO;
4482 		p = xdr_inline_decode(xdr, 4);
4483 		if (unlikely(!p))
4484 			goto out_overflow;
4485 		num = be32_to_cpup(p);
4486 		if (num == 0)
4487 			return 0;
4488 		if (num > 1)
4489 			printk(KERN_INFO "%s: Warning: Multiple pNFS layout "
4490 				"drivers per filesystem not supported\n",
4491 				__func__);
4492 
4493 		status = decode_first_threshold_item4(xdr, res);
4494 		bitmap[2] &= ~FATTR4_WORD2_MDSTHRESHOLD;
4495 	}
4496 	return status;
4497 out_overflow:
4498 	print_overflow_msg(__func__, xdr);
4499 	return -EIO;
4500 }
4501 
4502 static int decode_getfattr_attrs(struct xdr_stream *xdr, uint32_t *bitmap,
4503 		struct nfs_fattr *fattr, struct nfs_fh *fh,
4504 		struct nfs4_fs_locations *fs_loc, struct nfs4_label *label,
4505 		const struct nfs_server *server)
4506 {
4507 	int status;
4508 	umode_t fmode = 0;
4509 	uint32_t type;
4510 	int32_t err;
4511 
4512 	status = decode_attr_type(xdr, bitmap, &type);
4513 	if (status < 0)
4514 		goto xdr_error;
4515 	fattr->mode = 0;
4516 	if (status != 0) {
4517 		fattr->mode |= nfs_type2fmt[type];
4518 		fattr->valid |= status;
4519 	}
4520 
4521 	status = decode_attr_change(xdr, bitmap, &fattr->change_attr);
4522 	if (status < 0)
4523 		goto xdr_error;
4524 	fattr->valid |= status;
4525 
4526 	status = decode_attr_size(xdr, bitmap, &fattr->size);
4527 	if (status < 0)
4528 		goto xdr_error;
4529 	fattr->valid |= status;
4530 
4531 	status = decode_attr_fsid(xdr, bitmap, &fattr->fsid);
4532 	if (status < 0)
4533 		goto xdr_error;
4534 	fattr->valid |= status;
4535 
4536 	err = 0;
4537 	status = decode_attr_error(xdr, bitmap, &err);
4538 	if (status < 0)
4539 		goto xdr_error;
4540 
4541 	status = decode_attr_filehandle(xdr, bitmap, fh);
4542 	if (status < 0)
4543 		goto xdr_error;
4544 
4545 	status = decode_attr_fileid(xdr, bitmap, &fattr->fileid);
4546 	if (status < 0)
4547 		goto xdr_error;
4548 	fattr->valid |= status;
4549 
4550 	status = decode_attr_fs_locations(xdr, bitmap, fs_loc);
4551 	if (status < 0)
4552 		goto xdr_error;
4553 	fattr->valid |= status;
4554 
4555 	status = decode_attr_mode(xdr, bitmap, &fmode);
4556 	if (status < 0)
4557 		goto xdr_error;
4558 	if (status != 0) {
4559 		fattr->mode |= fmode;
4560 		fattr->valid |= status;
4561 	}
4562 
4563 	status = decode_attr_nlink(xdr, bitmap, &fattr->nlink);
4564 	if (status < 0)
4565 		goto xdr_error;
4566 	fattr->valid |= status;
4567 
4568 	status = decode_attr_owner(xdr, bitmap, server, &fattr->uid, fattr->owner_name);
4569 	if (status < 0)
4570 		goto xdr_error;
4571 	fattr->valid |= status;
4572 
4573 	status = decode_attr_group(xdr, bitmap, server, &fattr->gid, fattr->group_name);
4574 	if (status < 0)
4575 		goto xdr_error;
4576 	fattr->valid |= status;
4577 
4578 	status = decode_attr_rdev(xdr, bitmap, &fattr->rdev);
4579 	if (status < 0)
4580 		goto xdr_error;
4581 	fattr->valid |= status;
4582 
4583 	status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used);
4584 	if (status < 0)
4585 		goto xdr_error;
4586 	fattr->valid |= status;
4587 
4588 	status = decode_attr_time_access(xdr, bitmap, &fattr->atime);
4589 	if (status < 0)
4590 		goto xdr_error;
4591 	fattr->valid |= status;
4592 
4593 	status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime);
4594 	if (status < 0)
4595 		goto xdr_error;
4596 	fattr->valid |= status;
4597 
4598 	status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime);
4599 	if (status < 0)
4600 		goto xdr_error;
4601 	fattr->valid |= status;
4602 
4603 	status = decode_attr_mounted_on_fileid(xdr, bitmap, &fattr->mounted_on_fileid);
4604 	if (status < 0)
4605 		goto xdr_error;
4606 	fattr->valid |= status;
4607 
4608 	status = decode_attr_mdsthreshold(xdr, bitmap, fattr->mdsthreshold);
4609 	if (status < 0)
4610 		goto xdr_error;
4611 
4612 	if (label) {
4613 		status = decode_attr_security_label(xdr, bitmap, label);
4614 		if (status < 0)
4615 			goto xdr_error;
4616 		fattr->valid |= status;
4617 	}
4618 
4619 xdr_error:
4620 	dprintk("%s: xdr returned %d\n", __func__, -status);
4621 	return status;
4622 }
4623 
4624 static int decode_getfattr_generic(struct xdr_stream *xdr, struct nfs_fattr *fattr,
4625 		struct nfs_fh *fh, struct nfs4_fs_locations *fs_loc,
4626 		struct nfs4_label *label, const struct nfs_server *server)
4627 {
4628 	unsigned int savep;
4629 	uint32_t attrlen,
4630 		 bitmap[3] = {0};
4631 	int status;
4632 
4633 	status = decode_op_hdr(xdr, OP_GETATTR);
4634 	if (status < 0)
4635 		goto xdr_error;
4636 
4637 	status = decode_attr_bitmap(xdr, bitmap);
4638 	if (status < 0)
4639 		goto xdr_error;
4640 
4641 	status = decode_attr_length(xdr, &attrlen, &savep);
4642 	if (status < 0)
4643 		goto xdr_error;
4644 
4645 	status = decode_getfattr_attrs(xdr, bitmap, fattr, fh, fs_loc,
4646 					label, server);
4647 	if (status < 0)
4648 		goto xdr_error;
4649 
4650 	status = verify_attr_len(xdr, savep, attrlen);
4651 xdr_error:
4652 	dprintk("%s: xdr returned %d\n", __func__, -status);
4653 	return status;
4654 }
4655 
4656 static int decode_getfattr_label(struct xdr_stream *xdr, struct nfs_fattr *fattr,
4657 		struct nfs4_label *label, const struct nfs_server *server)
4658 {
4659 	return decode_getfattr_generic(xdr, fattr, NULL, NULL, label, server);
4660 }
4661 
4662 static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr,
4663 		const struct nfs_server *server)
4664 {
4665 	return decode_getfattr_generic(xdr, fattr, NULL, NULL, NULL, server);
4666 }
4667 
4668 /*
4669  * Decode potentially multiple layout types. Currently we only support
4670  * one layout driver per file system.
4671  */
4672 static int decode_first_pnfs_layout_type(struct xdr_stream *xdr,
4673 					 uint32_t *layouttype)
4674 {
4675 	__be32 *p;
4676 	int num;
4677 
4678 	p = xdr_inline_decode(xdr, 4);
4679 	if (unlikely(!p))
4680 		goto out_overflow;
4681 	num = be32_to_cpup(p);
4682 
4683 	/* pNFS is not supported by the underlying file system */
4684 	if (num == 0) {
4685 		*layouttype = 0;
4686 		return 0;
4687 	}
4688 	if (num > 1)
4689 		printk(KERN_INFO "NFS: %s: Warning: Multiple pNFS layout "
4690 			"drivers per filesystem not supported\n", __func__);
4691 
4692 	/* Decode and set first layout type, move xdr->p past unused types */
4693 	p = xdr_inline_decode(xdr, num * 4);
4694 	if (unlikely(!p))
4695 		goto out_overflow;
4696 	*layouttype = be32_to_cpup(p);
4697 	return 0;
4698 out_overflow:
4699 	print_overflow_msg(__func__, xdr);
4700 	return -EIO;
4701 }
4702 
4703 /*
4704  * The type of file system exported.
4705  * Note we must ensure that layouttype is set in any non-error case.
4706  */
4707 static int decode_attr_pnfstype(struct xdr_stream *xdr, uint32_t *bitmap,
4708 				uint32_t *layouttype)
4709 {
4710 	int status = 0;
4711 
4712 	dprintk("%s: bitmap is %x\n", __func__, bitmap[1]);
4713 	if (unlikely(bitmap[1] & (FATTR4_WORD1_FS_LAYOUT_TYPES - 1U)))
4714 		return -EIO;
4715 	if (bitmap[1] & FATTR4_WORD1_FS_LAYOUT_TYPES) {
4716 		status = decode_first_pnfs_layout_type(xdr, layouttype);
4717 		bitmap[1] &= ~FATTR4_WORD1_FS_LAYOUT_TYPES;
4718 	} else
4719 		*layouttype = 0;
4720 	return status;
4721 }
4722 
4723 /*
4724  * The prefered block size for layout directed io
4725  */
4726 static int decode_attr_layout_blksize(struct xdr_stream *xdr, uint32_t *bitmap,
4727 				      uint32_t *res)
4728 {
4729 	__be32 *p;
4730 
4731 	dprintk("%s: bitmap is %x\n", __func__, bitmap[2]);
4732 	*res = 0;
4733 	if (bitmap[2] & FATTR4_WORD2_LAYOUT_BLKSIZE) {
4734 		p = xdr_inline_decode(xdr, 4);
4735 		if (unlikely(!p)) {
4736 			print_overflow_msg(__func__, xdr);
4737 			return -EIO;
4738 		}
4739 		*res = be32_to_cpup(p);
4740 		bitmap[2] &= ~FATTR4_WORD2_LAYOUT_BLKSIZE;
4741 	}
4742 	return 0;
4743 }
4744 
4745 static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
4746 {
4747 	unsigned int savep;
4748 	uint32_t attrlen, bitmap[3];
4749 	int status;
4750 
4751 	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4752 		goto xdr_error;
4753 	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4754 		goto xdr_error;
4755 	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4756 		goto xdr_error;
4757 
4758 	fsinfo->rtmult = fsinfo->wtmult = 512;	/* ??? */
4759 
4760 	if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
4761 		goto xdr_error;
4762 	if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
4763 		goto xdr_error;
4764 	if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
4765 		goto xdr_error;
4766 	fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
4767 	if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
4768 		goto xdr_error;
4769 	fsinfo->wtpref = fsinfo->wtmax;
4770 	status = decode_attr_time_delta(xdr, bitmap, &fsinfo->time_delta);
4771 	if (status != 0)
4772 		goto xdr_error;
4773 	status = decode_attr_pnfstype(xdr, bitmap, &fsinfo->layouttype);
4774 	if (status != 0)
4775 		goto xdr_error;
4776 	status = decode_attr_layout_blksize(xdr, bitmap, &fsinfo->blksize);
4777 	if (status)
4778 		goto xdr_error;
4779 
4780 	status = verify_attr_len(xdr, savep, attrlen);
4781 xdr_error:
4782 	dprintk("%s: xdr returned %d!\n", __func__, -status);
4783 	return status;
4784 }
4785 
4786 static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
4787 {
4788 	__be32 *p;
4789 	uint32_t len;
4790 	int status;
4791 
4792 	/* Zero handle first to allow comparisons */
4793 	memset(fh, 0, sizeof(*fh));
4794 
4795 	status = decode_op_hdr(xdr, OP_GETFH);
4796 	if (status)
4797 		return status;
4798 
4799 	p = xdr_inline_decode(xdr, 4);
4800 	if (unlikely(!p))
4801 		goto out_overflow;
4802 	len = be32_to_cpup(p);
4803 	if (len > NFS4_FHSIZE)
4804 		return -EIO;
4805 	fh->size = len;
4806 	p = xdr_inline_decode(xdr, len);
4807 	if (unlikely(!p))
4808 		goto out_overflow;
4809 	memcpy(fh->data, p, len);
4810 	return 0;
4811 out_overflow:
4812 	print_overflow_msg(__func__, xdr);
4813 	return -EIO;
4814 }
4815 
4816 static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
4817 {
4818 	int status;
4819 
4820 	status = decode_op_hdr(xdr, OP_LINK);
4821 	if (status)
4822 		return status;
4823 	return decode_change_info(xdr, cinfo);
4824 }
4825 
4826 /*
4827  * We create the owner, so we know a proper owner.id length is 4.
4828  */
4829 static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
4830 {
4831 	uint64_t offset, length, clientid;
4832 	__be32 *p;
4833 	uint32_t namelen, type;
4834 
4835 	p = xdr_inline_decode(xdr, 32); /* read 32 bytes */
4836 	if (unlikely(!p))
4837 		goto out_overflow;
4838 	p = xdr_decode_hyper(p, &offset); /* read 2 8-byte long words */
4839 	p = xdr_decode_hyper(p, &length);
4840 	type = be32_to_cpup(p++); /* 4 byte read */
4841 	if (fl != NULL) { /* manipulate file lock */
4842 		fl->fl_start = (loff_t)offset;
4843 		fl->fl_end = fl->fl_start + (loff_t)length - 1;
4844 		if (length == ~(uint64_t)0)
4845 			fl->fl_end = OFFSET_MAX;
4846 		fl->fl_type = F_WRLCK;
4847 		if (type & 1)
4848 			fl->fl_type = F_RDLCK;
4849 		fl->fl_pid = 0;
4850 	}
4851 	p = xdr_decode_hyper(p, &clientid); /* read 8 bytes */
4852 	namelen = be32_to_cpup(p); /* read 4 bytes */  /* have read all 32 bytes now */
4853 	p = xdr_inline_decode(xdr, namelen); /* variable size field */
4854 	if (likely(p))
4855 		return -NFS4ERR_DENIED;
4856 out_overflow:
4857 	print_overflow_msg(__func__, xdr);
4858 	return -EIO;
4859 }
4860 
4861 static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
4862 {
4863 	int status;
4864 
4865 	status = decode_op_hdr(xdr, OP_LOCK);
4866 	if (status == -EIO)
4867 		goto out;
4868 	if (status == 0) {
4869 		status = decode_stateid(xdr, &res->stateid);
4870 		if (unlikely(status))
4871 			goto out;
4872 	} else if (status == -NFS4ERR_DENIED)
4873 		status = decode_lock_denied(xdr, NULL);
4874 	if (res->open_seqid != NULL)
4875 		nfs_increment_open_seqid(status, res->open_seqid);
4876 	nfs_increment_lock_seqid(status, res->lock_seqid);
4877 out:
4878 	return status;
4879 }
4880 
4881 static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
4882 {
4883 	int status;
4884 	status = decode_op_hdr(xdr, OP_LOCKT);
4885 	if (status == -NFS4ERR_DENIED)
4886 		return decode_lock_denied(xdr, res->denied);
4887 	return status;
4888 }
4889 
4890 static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
4891 {
4892 	int status;
4893 
4894 	status = decode_op_hdr(xdr, OP_LOCKU);
4895 	if (status != -EIO)
4896 		nfs_increment_lock_seqid(status, res->seqid);
4897 	if (status == 0)
4898 		status = decode_stateid(xdr, &res->stateid);
4899 	return status;
4900 }
4901 
4902 static int decode_release_lockowner(struct xdr_stream *xdr)
4903 {
4904 	return decode_op_hdr(xdr, OP_RELEASE_LOCKOWNER);
4905 }
4906 
4907 static int decode_lookup(struct xdr_stream *xdr)
4908 {
4909 	return decode_op_hdr(xdr, OP_LOOKUP);
4910 }
4911 
4912 /* This is too sick! */
4913 static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
4914 {
4915 	__be32 *p;
4916 	uint32_t limit_type, nblocks, blocksize;
4917 
4918 	p = xdr_inline_decode(xdr, 12);
4919 	if (unlikely(!p))
4920 		goto out_overflow;
4921 	limit_type = be32_to_cpup(p++);
4922 	switch (limit_type) {
4923 	case 1:
4924 		xdr_decode_hyper(p, maxsize);
4925 		break;
4926 	case 2:
4927 		nblocks = be32_to_cpup(p++);
4928 		blocksize = be32_to_cpup(p);
4929 		*maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
4930 	}
4931 	return 0;
4932 out_overflow:
4933 	print_overflow_msg(__func__, xdr);
4934 	return -EIO;
4935 }
4936 
4937 static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
4938 {
4939 	__be32 *p;
4940 	uint32_t delegation_type;
4941 	int status;
4942 
4943 	p = xdr_inline_decode(xdr, 4);
4944 	if (unlikely(!p))
4945 		goto out_overflow;
4946 	delegation_type = be32_to_cpup(p);
4947 	if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
4948 		res->delegation_type = 0;
4949 		return 0;
4950 	}
4951 	status = decode_stateid(xdr, &res->delegation);
4952 	if (unlikely(status))
4953 		return status;
4954 	p = xdr_inline_decode(xdr, 4);
4955 	if (unlikely(!p))
4956 		goto out_overflow;
4957 	res->do_recall = be32_to_cpup(p);
4958 
4959 	switch (delegation_type) {
4960 	case NFS4_OPEN_DELEGATE_READ:
4961 		res->delegation_type = FMODE_READ;
4962 		break;
4963 	case NFS4_OPEN_DELEGATE_WRITE:
4964 		res->delegation_type = FMODE_WRITE|FMODE_READ;
4965 		if (decode_space_limit(xdr, &res->maxsize) < 0)
4966 				return -EIO;
4967 	}
4968 	return decode_ace(xdr, NULL, res->server->nfs_client);
4969 out_overflow:
4970 	print_overflow_msg(__func__, xdr);
4971 	return -EIO;
4972 }
4973 
4974 static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
4975 {
4976 	__be32 *p;
4977 	uint32_t savewords, bmlen, i;
4978 	int status;
4979 
4980 	if (!__decode_op_hdr(xdr, OP_OPEN, &status))
4981 		return status;
4982 	nfs_increment_open_seqid(status, res->seqid);
4983 	if (status)
4984 		return status;
4985 	status = decode_stateid(xdr, &res->stateid);
4986 	if (unlikely(status))
4987 		return status;
4988 
4989 	decode_change_info(xdr, &res->cinfo);
4990 
4991 	p = xdr_inline_decode(xdr, 8);
4992 	if (unlikely(!p))
4993 		goto out_overflow;
4994 	res->rflags = be32_to_cpup(p++);
4995 	bmlen = be32_to_cpup(p);
4996 	if (bmlen > 10)
4997 		goto xdr_error;
4998 
4999 	p = xdr_inline_decode(xdr, bmlen << 2);
5000 	if (unlikely(!p))
5001 		goto out_overflow;
5002 	savewords = min_t(uint32_t, bmlen, NFS4_BITMAP_SIZE);
5003 	for (i = 0; i < savewords; ++i)
5004 		res->attrset[i] = be32_to_cpup(p++);
5005 	for (; i < NFS4_BITMAP_SIZE; i++)
5006 		res->attrset[i] = 0;
5007 
5008 	return decode_delegation(xdr, res);
5009 xdr_error:
5010 	dprintk("%s: Bitmap too large! Length = %u\n", __func__, bmlen);
5011 	return -EIO;
5012 out_overflow:
5013 	print_overflow_msg(__func__, xdr);
5014 	return -EIO;
5015 }
5016 
5017 static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
5018 {
5019 	int status;
5020 
5021 	status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
5022 	if (status != -EIO)
5023 		nfs_increment_open_seqid(status, res->seqid);
5024 	if (!status)
5025 		status = decode_stateid(xdr, &res->stateid);
5026 	return status;
5027 }
5028 
5029 static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
5030 {
5031 	int status;
5032 
5033 	status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
5034 	if (status != -EIO)
5035 		nfs_increment_open_seqid(status, res->seqid);
5036 	if (!status)
5037 		status = decode_stateid(xdr, &res->stateid);
5038 	return status;
5039 }
5040 
5041 static int decode_putfh(struct xdr_stream *xdr)
5042 {
5043 	return decode_op_hdr(xdr, OP_PUTFH);
5044 }
5045 
5046 static int decode_putrootfh(struct xdr_stream *xdr)
5047 {
5048 	return decode_op_hdr(xdr, OP_PUTROOTFH);
5049 }
5050 
5051 static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req,
5052 		       struct nfs_pgio_res *res)
5053 {
5054 	__be32 *p;
5055 	uint32_t count, eof, recvd;
5056 	int status;
5057 
5058 	status = decode_op_hdr(xdr, OP_READ);
5059 	if (status)
5060 		return status;
5061 	p = xdr_inline_decode(xdr, 8);
5062 	if (unlikely(!p))
5063 		goto out_overflow;
5064 	eof = be32_to_cpup(p++);
5065 	count = be32_to_cpup(p);
5066 	recvd = xdr_read_pages(xdr, count);
5067 	if (count > recvd) {
5068 		dprintk("NFS: server cheating in read reply: "
5069 				"count %u > recvd %u\n", count, recvd);
5070 		count = recvd;
5071 		eof = 0;
5072 	}
5073 	res->eof = eof;
5074 	res->count = count;
5075 	return 0;
5076 out_overflow:
5077 	print_overflow_msg(__func__, xdr);
5078 	return -EIO;
5079 }
5080 
5081 static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
5082 {
5083 	int		status;
5084 	__be32		verf[2];
5085 
5086 	status = decode_op_hdr(xdr, OP_READDIR);
5087 	if (!status)
5088 		status = decode_verifier(xdr, readdir->verifier.data);
5089 	if (unlikely(status))
5090 		return status;
5091 	memcpy(verf, readdir->verifier.data, sizeof(verf));
5092 	dprintk("%s: verifier = %08x:%08x\n",
5093 			__func__, verf[0], verf[1]);
5094 	return xdr_read_pages(xdr, xdr->buf->page_len);
5095 }
5096 
5097 static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
5098 {
5099 	struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
5100 	u32 len, recvd;
5101 	__be32 *p;
5102 	int status;
5103 
5104 	status = decode_op_hdr(xdr, OP_READLINK);
5105 	if (status)
5106 		return status;
5107 
5108 	/* Convert length of symlink */
5109 	p = xdr_inline_decode(xdr, 4);
5110 	if (unlikely(!p))
5111 		goto out_overflow;
5112 	len = be32_to_cpup(p);
5113 	if (len >= rcvbuf->page_len || len <= 0) {
5114 		dprintk("nfs: server returned giant symlink!\n");
5115 		return -ENAMETOOLONG;
5116 	}
5117 	recvd = xdr_read_pages(xdr, len);
5118 	if (recvd < len) {
5119 		dprintk("NFS: server cheating in readlink reply: "
5120 				"count %u > recvd %u\n", len, recvd);
5121 		return -EIO;
5122 	}
5123 	/*
5124 	 * The XDR encode routine has set things up so that
5125 	 * the link text will be copied directly into the
5126 	 * buffer.  We just have to do overflow-checking,
5127 	 * and and null-terminate the text (the VFS expects
5128 	 * null-termination).
5129 	 */
5130 	xdr_terminate_string(rcvbuf, len);
5131 	return 0;
5132 out_overflow:
5133 	print_overflow_msg(__func__, xdr);
5134 	return -EIO;
5135 }
5136 
5137 static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
5138 {
5139 	int status;
5140 
5141 	status = decode_op_hdr(xdr, OP_REMOVE);
5142 	if (status)
5143 		goto out;
5144 	status = decode_change_info(xdr, cinfo);
5145 out:
5146 	return status;
5147 }
5148 
5149 static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
5150 	      struct nfs4_change_info *new_cinfo)
5151 {
5152 	int status;
5153 
5154 	status = decode_op_hdr(xdr, OP_RENAME);
5155 	if (status)
5156 		goto out;
5157 	if ((status = decode_change_info(xdr, old_cinfo)))
5158 		goto out;
5159 	status = decode_change_info(xdr, new_cinfo);
5160 out:
5161 	return status;
5162 }
5163 
5164 static int decode_renew(struct xdr_stream *xdr)
5165 {
5166 	return decode_op_hdr(xdr, OP_RENEW);
5167 }
5168 
5169 static int
5170 decode_restorefh(struct xdr_stream *xdr)
5171 {
5172 	return decode_op_hdr(xdr, OP_RESTOREFH);
5173 }
5174 
5175 static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
5176 			 struct nfs_getaclres *res)
5177 {
5178 	unsigned int savep;
5179 	uint32_t attrlen,
5180 		 bitmap[3] = {0};
5181 	int status;
5182 	unsigned int pg_offset;
5183 
5184 	res->acl_len = 0;
5185 	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
5186 		goto out;
5187 
5188 	xdr_enter_page(xdr, xdr->buf->page_len);
5189 
5190 	/* Calculate the offset of the page data */
5191 	pg_offset = xdr->buf->head[0].iov_len;
5192 
5193 	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
5194 		goto out;
5195 	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
5196 		goto out;
5197 
5198 	if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
5199 		return -EIO;
5200 	if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
5201 
5202 		/* The bitmap (xdr len + bitmaps) and the attr xdr len words
5203 		 * are stored with the acl data to handle the problem of
5204 		 * variable length bitmaps.*/
5205 		res->acl_data_offset = xdr_stream_pos(xdr) - pg_offset;
5206 		res->acl_len = attrlen;
5207 
5208 		/* Check for receive buffer overflow */
5209 		if (res->acl_len > (xdr->nwords << 2) ||
5210 		    res->acl_len + res->acl_data_offset > xdr->buf->page_len) {
5211 			res->acl_flags |= NFS4_ACL_TRUNC;
5212 			dprintk("NFS: acl reply: attrlen %u > page_len %u\n",
5213 					attrlen, xdr->nwords << 2);
5214 		}
5215 	} else
5216 		status = -EOPNOTSUPP;
5217 
5218 out:
5219 	return status;
5220 }
5221 
5222 static int
5223 decode_savefh(struct xdr_stream *xdr)
5224 {
5225 	return decode_op_hdr(xdr, OP_SAVEFH);
5226 }
5227 
5228 static int decode_setattr(struct xdr_stream *xdr)
5229 {
5230 	__be32 *p;
5231 	uint32_t bmlen;
5232 	int status;
5233 
5234 	status = decode_op_hdr(xdr, OP_SETATTR);
5235 	if (status)
5236 		return status;
5237 	p = xdr_inline_decode(xdr, 4);
5238 	if (unlikely(!p))
5239 		goto out_overflow;
5240 	bmlen = be32_to_cpup(p);
5241 	p = xdr_inline_decode(xdr, bmlen << 2);
5242 	if (likely(p))
5243 		return 0;
5244 out_overflow:
5245 	print_overflow_msg(__func__, xdr);
5246 	return -EIO;
5247 }
5248 
5249 static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_setclientid_res *res)
5250 {
5251 	__be32 *p;
5252 	uint32_t opnum;
5253 	int32_t nfserr;
5254 
5255 	p = xdr_inline_decode(xdr, 8);
5256 	if (unlikely(!p))
5257 		goto out_overflow;
5258 	opnum = be32_to_cpup(p++);
5259 	if (opnum != OP_SETCLIENTID) {
5260 		dprintk("nfs: decode_setclientid: Server returned operation"
5261 			" %d\n", opnum);
5262 		return -EIO;
5263 	}
5264 	nfserr = be32_to_cpup(p);
5265 	if (nfserr == NFS_OK) {
5266 		p = xdr_inline_decode(xdr, 8 + NFS4_VERIFIER_SIZE);
5267 		if (unlikely(!p))
5268 			goto out_overflow;
5269 		p = xdr_decode_hyper(p, &res->clientid);
5270 		memcpy(res->confirm.data, p, NFS4_VERIFIER_SIZE);
5271 	} else if (nfserr == NFSERR_CLID_INUSE) {
5272 		uint32_t len;
5273 
5274 		/* skip netid string */
5275 		p = xdr_inline_decode(xdr, 4);
5276 		if (unlikely(!p))
5277 			goto out_overflow;
5278 		len = be32_to_cpup(p);
5279 		p = xdr_inline_decode(xdr, len);
5280 		if (unlikely(!p))
5281 			goto out_overflow;
5282 
5283 		/* skip uaddr string */
5284 		p = xdr_inline_decode(xdr, 4);
5285 		if (unlikely(!p))
5286 			goto out_overflow;
5287 		len = be32_to_cpup(p);
5288 		p = xdr_inline_decode(xdr, len);
5289 		if (unlikely(!p))
5290 			goto out_overflow;
5291 		return -NFSERR_CLID_INUSE;
5292 	} else
5293 		return nfs4_stat_to_errno(nfserr);
5294 
5295 	return 0;
5296 out_overflow:
5297 	print_overflow_msg(__func__, xdr);
5298 	return -EIO;
5299 }
5300 
5301 static int decode_setclientid_confirm(struct xdr_stream *xdr)
5302 {
5303 	return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
5304 }
5305 
5306 static int decode_write(struct xdr_stream *xdr, struct nfs_pgio_res *res)
5307 {
5308 	__be32 *p;
5309 	int status;
5310 
5311 	status = decode_op_hdr(xdr, OP_WRITE);
5312 	if (status)
5313 		return status;
5314 
5315 	p = xdr_inline_decode(xdr, 8);
5316 	if (unlikely(!p))
5317 		goto out_overflow;
5318 	res->count = be32_to_cpup(p++);
5319 	res->verf->committed = be32_to_cpup(p++);
5320 	return decode_write_verifier(xdr, &res->verf->verifier);
5321 out_overflow:
5322 	print_overflow_msg(__func__, xdr);
5323 	return -EIO;
5324 }
5325 
5326 static int decode_delegreturn(struct xdr_stream *xdr)
5327 {
5328 	return decode_op_hdr(xdr, OP_DELEGRETURN);
5329 }
5330 
5331 static int decode_secinfo_gss(struct xdr_stream *xdr,
5332 			      struct nfs4_secinfo4 *flavor)
5333 {
5334 	u32 oid_len;
5335 	__be32 *p;
5336 
5337 	p = xdr_inline_decode(xdr, 4);
5338 	if (unlikely(!p))
5339 		goto out_overflow;
5340 	oid_len = be32_to_cpup(p);
5341 	if (oid_len > GSS_OID_MAX_LEN)
5342 		goto out_err;
5343 
5344 	p = xdr_inline_decode(xdr, oid_len);
5345 	if (unlikely(!p))
5346 		goto out_overflow;
5347 	memcpy(flavor->flavor_info.oid.data, p, oid_len);
5348 	flavor->flavor_info.oid.len = oid_len;
5349 
5350 	p = xdr_inline_decode(xdr, 8);
5351 	if (unlikely(!p))
5352 		goto out_overflow;
5353 	flavor->flavor_info.qop = be32_to_cpup(p++);
5354 	flavor->flavor_info.service = be32_to_cpup(p);
5355 
5356 	return 0;
5357 
5358 out_overflow:
5359 	print_overflow_msg(__func__, xdr);
5360 	return -EIO;
5361 out_err:
5362 	return -EINVAL;
5363 }
5364 
5365 static int decode_secinfo_common(struct xdr_stream *xdr, struct nfs4_secinfo_res *res)
5366 {
5367 	struct nfs4_secinfo4 *sec_flavor;
5368 	unsigned int i, num_flavors;
5369 	int status;
5370 	__be32 *p;
5371 
5372 	p = xdr_inline_decode(xdr, 4);
5373 	if (unlikely(!p))
5374 		goto out_overflow;
5375 
5376 	res->flavors->num_flavors = 0;
5377 	num_flavors = be32_to_cpup(p);
5378 
5379 	for (i = 0; i < num_flavors; i++) {
5380 		sec_flavor = &res->flavors->flavors[i];
5381 		if ((char *)&sec_flavor[1] - (char *)res->flavors > PAGE_SIZE)
5382 			break;
5383 
5384 		p = xdr_inline_decode(xdr, 4);
5385 		if (unlikely(!p))
5386 			goto out_overflow;
5387 		sec_flavor->flavor = be32_to_cpup(p);
5388 
5389 		if (sec_flavor->flavor == RPC_AUTH_GSS) {
5390 			status = decode_secinfo_gss(xdr, sec_flavor);
5391 			if (status)
5392 				goto out;
5393 		}
5394 		res->flavors->num_flavors++;
5395 	}
5396 
5397 	status = 0;
5398 out:
5399 	return status;
5400 out_overflow:
5401 	print_overflow_msg(__func__, xdr);
5402 	return -EIO;
5403 }
5404 
5405 static int decode_secinfo(struct xdr_stream *xdr, struct nfs4_secinfo_res *res)
5406 {
5407 	int status = decode_op_hdr(xdr, OP_SECINFO);
5408 	if (status)
5409 		return status;
5410 	return decode_secinfo_common(xdr, res);
5411 }
5412 
5413 #if defined(CONFIG_NFS_V4_1)
5414 static int decode_secinfo_no_name(struct xdr_stream *xdr, struct nfs4_secinfo_res *res)
5415 {
5416 	int status = decode_op_hdr(xdr, OP_SECINFO_NO_NAME);
5417 	if (status)
5418 		return status;
5419 	return decode_secinfo_common(xdr, res);
5420 }
5421 
5422 static int decode_op_map(struct xdr_stream *xdr, struct nfs4_op_map *op_map)
5423 {
5424 	__be32 *p;
5425 	uint32_t bitmap_words;
5426 	unsigned int i;
5427 
5428 	p = xdr_inline_decode(xdr, 4);
5429 	bitmap_words = be32_to_cpup(p++);
5430 	if (bitmap_words > NFS4_OP_MAP_NUM_WORDS)
5431 		return -EIO;
5432 	p = xdr_inline_decode(xdr, 4 * bitmap_words);
5433 	for (i = 0; i < bitmap_words; i++)
5434 		op_map->u.words[i] = be32_to_cpup(p++);
5435 
5436 	return 0;
5437 }
5438 
5439 static int decode_exchange_id(struct xdr_stream *xdr,
5440 			      struct nfs41_exchange_id_res *res)
5441 {
5442 	__be32 *p;
5443 	uint32_t dummy;
5444 	char *dummy_str;
5445 	int status;
5446 	uint32_t impl_id_count;
5447 
5448 	status = decode_op_hdr(xdr, OP_EXCHANGE_ID);
5449 	if (status)
5450 		return status;
5451 
5452 	p = xdr_inline_decode(xdr, 8);
5453 	if (unlikely(!p))
5454 		goto out_overflow;
5455 	xdr_decode_hyper(p, &res->clientid);
5456 	p = xdr_inline_decode(xdr, 12);
5457 	if (unlikely(!p))
5458 		goto out_overflow;
5459 	res->seqid = be32_to_cpup(p++);
5460 	res->flags = be32_to_cpup(p++);
5461 
5462 	res->state_protect.how = be32_to_cpup(p);
5463 	switch (res->state_protect.how) {
5464 	case SP4_NONE:
5465 		break;
5466 	case SP4_MACH_CRED:
5467 		status = decode_op_map(xdr, &res->state_protect.enforce);
5468 		if (status)
5469 			return status;
5470 		status = decode_op_map(xdr, &res->state_protect.allow);
5471 		if (status)
5472 			return status;
5473 		break;
5474 	default:
5475 		WARN_ON_ONCE(1);
5476 		return -EIO;
5477 	}
5478 
5479 	/* server_owner4.so_minor_id */
5480 	p = xdr_inline_decode(xdr, 8);
5481 	if (unlikely(!p))
5482 		goto out_overflow;
5483 	p = xdr_decode_hyper(p, &res->server_owner->minor_id);
5484 
5485 	/* server_owner4.so_major_id */
5486 	status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5487 	if (unlikely(status))
5488 		return status;
5489 	if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
5490 		return -EIO;
5491 	memcpy(res->server_owner->major_id, dummy_str, dummy);
5492 	res->server_owner->major_id_sz = dummy;
5493 
5494 	/* server_scope4 */
5495 	status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5496 	if (unlikely(status))
5497 		return status;
5498 	if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
5499 		return -EIO;
5500 	memcpy(res->server_scope->server_scope, dummy_str, dummy);
5501 	res->server_scope->server_scope_sz = dummy;
5502 
5503 	/* Implementation Id */
5504 	p = xdr_inline_decode(xdr, 4);
5505 	if (unlikely(!p))
5506 		goto out_overflow;
5507 	impl_id_count = be32_to_cpup(p++);
5508 
5509 	if (impl_id_count) {
5510 		/* nii_domain */
5511 		status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5512 		if (unlikely(status))
5513 			return status;
5514 		if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
5515 			return -EIO;
5516 		memcpy(res->impl_id->domain, dummy_str, dummy);
5517 
5518 		/* nii_name */
5519 		status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5520 		if (unlikely(status))
5521 			return status;
5522 		if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
5523 			return -EIO;
5524 		memcpy(res->impl_id->name, dummy_str, dummy);
5525 
5526 		/* nii_date */
5527 		p = xdr_inline_decode(xdr, 12);
5528 		if (unlikely(!p))
5529 			goto out_overflow;
5530 		p = xdr_decode_hyper(p, &res->impl_id->date.seconds);
5531 		res->impl_id->date.nseconds = be32_to_cpup(p);
5532 
5533 		/* if there's more than one entry, ignore the rest */
5534 	}
5535 	return 0;
5536 out_overflow:
5537 	print_overflow_msg(__func__, xdr);
5538 	return -EIO;
5539 }
5540 
5541 static int decode_chan_attrs(struct xdr_stream *xdr,
5542 			     struct nfs4_channel_attrs *attrs)
5543 {
5544 	__be32 *p;
5545 	u32 nr_attrs, val;
5546 
5547 	p = xdr_inline_decode(xdr, 28);
5548 	if (unlikely(!p))
5549 		goto out_overflow;
5550 	val = be32_to_cpup(p++);	/* headerpadsz */
5551 	if (val)
5552 		return -EINVAL;		/* no support for header padding yet */
5553 	attrs->max_rqst_sz = be32_to_cpup(p++);
5554 	attrs->max_resp_sz = be32_to_cpup(p++);
5555 	attrs->max_resp_sz_cached = be32_to_cpup(p++);
5556 	attrs->max_ops = be32_to_cpup(p++);
5557 	attrs->max_reqs = be32_to_cpup(p++);
5558 	nr_attrs = be32_to_cpup(p);
5559 	if (unlikely(nr_attrs > 1)) {
5560 		printk(KERN_WARNING "NFS: %s: Invalid rdma channel attrs "
5561 			"count %u\n", __func__, nr_attrs);
5562 		return -EINVAL;
5563 	}
5564 	if (nr_attrs == 1) {
5565 		p = xdr_inline_decode(xdr, 4); /* skip rdma_attrs */
5566 		if (unlikely(!p))
5567 			goto out_overflow;
5568 	}
5569 	return 0;
5570 out_overflow:
5571 	print_overflow_msg(__func__, xdr);
5572 	return -EIO;
5573 }
5574 
5575 static int decode_sessionid(struct xdr_stream *xdr, struct nfs4_sessionid *sid)
5576 {
5577 	return decode_opaque_fixed(xdr, sid->data, NFS4_MAX_SESSIONID_LEN);
5578 }
5579 
5580 static int decode_bind_conn_to_session(struct xdr_stream *xdr,
5581 				struct nfs41_bind_conn_to_session_res *res)
5582 {
5583 	__be32 *p;
5584 	int status;
5585 
5586 	status = decode_op_hdr(xdr, OP_BIND_CONN_TO_SESSION);
5587 	if (!status)
5588 		status = decode_sessionid(xdr, &res->session->sess_id);
5589 	if (unlikely(status))
5590 		return status;
5591 
5592 	/* dir flags, rdma mode bool */
5593 	p = xdr_inline_decode(xdr, 8);
5594 	if (unlikely(!p))
5595 		goto out_overflow;
5596 
5597 	res->dir = be32_to_cpup(p++);
5598 	if (res->dir == 0 || res->dir > NFS4_CDFS4_BOTH)
5599 		return -EIO;
5600 	if (be32_to_cpup(p) == 0)
5601 		res->use_conn_in_rdma_mode = false;
5602 	else
5603 		res->use_conn_in_rdma_mode = true;
5604 
5605 	return 0;
5606 out_overflow:
5607 	print_overflow_msg(__func__, xdr);
5608 	return -EIO;
5609 }
5610 
5611 static int decode_create_session(struct xdr_stream *xdr,
5612 				 struct nfs41_create_session_res *res)
5613 {
5614 	__be32 *p;
5615 	int status;
5616 	struct nfs_client *clp = res->client;
5617 	struct nfs4_session *session = clp->cl_session;
5618 
5619 	status = decode_op_hdr(xdr, OP_CREATE_SESSION);
5620 	if (!status)
5621 		status = decode_sessionid(xdr, &session->sess_id);
5622 	if (unlikely(status))
5623 		return status;
5624 
5625 	/* seqid, flags */
5626 	p = xdr_inline_decode(xdr, 8);
5627 	if (unlikely(!p))
5628 		goto out_overflow;
5629 	clp->cl_seqid = be32_to_cpup(p++);
5630 	session->flags = be32_to_cpup(p);
5631 
5632 	/* Channel attributes */
5633 	status = decode_chan_attrs(xdr, &session->fc_attrs);
5634 	if (!status)
5635 		status = decode_chan_attrs(xdr, &session->bc_attrs);
5636 	return status;
5637 out_overflow:
5638 	print_overflow_msg(__func__, xdr);
5639 	return -EIO;
5640 }
5641 
5642 static int decode_destroy_session(struct xdr_stream *xdr, void *dummy)
5643 {
5644 	return decode_op_hdr(xdr, OP_DESTROY_SESSION);
5645 }
5646 
5647 static int decode_destroy_clientid(struct xdr_stream *xdr, void *dummy)
5648 {
5649 	return decode_op_hdr(xdr, OP_DESTROY_CLIENTID);
5650 }
5651 
5652 static int decode_reclaim_complete(struct xdr_stream *xdr, void *dummy)
5653 {
5654 	return decode_op_hdr(xdr, OP_RECLAIM_COMPLETE);
5655 }
5656 #endif /* CONFIG_NFS_V4_1 */
5657 
5658 static int decode_sequence(struct xdr_stream *xdr,
5659 			   struct nfs4_sequence_res *res,
5660 			   struct rpc_rqst *rqstp)
5661 {
5662 #if defined(CONFIG_NFS_V4_1)
5663 	struct nfs4_session *session;
5664 	struct nfs4_sessionid id;
5665 	u32 dummy;
5666 	int status;
5667 	__be32 *p;
5668 
5669 	if (res->sr_slot == NULL)
5670 		return 0;
5671 	if (!res->sr_slot->table->session)
5672 		return 0;
5673 
5674 	status = decode_op_hdr(xdr, OP_SEQUENCE);
5675 	if (!status)
5676 		status = decode_sessionid(xdr, &id);
5677 	if (unlikely(status))
5678 		goto out_err;
5679 
5680 	/*
5681 	 * If the server returns different values for sessionID, slotID or
5682 	 * sequence number, the server is looney tunes.
5683 	 */
5684 	status = -EREMOTEIO;
5685 	session = res->sr_slot->table->session;
5686 
5687 	if (memcmp(id.data, session->sess_id.data,
5688 		   NFS4_MAX_SESSIONID_LEN)) {
5689 		dprintk("%s Invalid session id\n", __func__);
5690 		goto out_err;
5691 	}
5692 
5693 	p = xdr_inline_decode(xdr, 20);
5694 	if (unlikely(!p))
5695 		goto out_overflow;
5696 
5697 	/* seqid */
5698 	dummy = be32_to_cpup(p++);
5699 	if (dummy != res->sr_slot->seq_nr) {
5700 		dprintk("%s Invalid sequence number\n", __func__);
5701 		goto out_err;
5702 	}
5703 	/* slot id */
5704 	dummy = be32_to_cpup(p++);
5705 	if (dummy != res->sr_slot->slot_nr) {
5706 		dprintk("%s Invalid slot id\n", __func__);
5707 		goto out_err;
5708 	}
5709 	/* highest slot id */
5710 	res->sr_highest_slotid = be32_to_cpup(p++);
5711 	/* target highest slot id */
5712 	res->sr_target_highest_slotid = be32_to_cpup(p++);
5713 	/* result flags */
5714 	res->sr_status_flags = be32_to_cpup(p);
5715 	status = 0;
5716 out_err:
5717 	res->sr_status = status;
5718 	return status;
5719 out_overflow:
5720 	print_overflow_msg(__func__, xdr);
5721 	status = -EIO;
5722 	goto out_err;
5723 #else  /* CONFIG_NFS_V4_1 */
5724 	return 0;
5725 #endif /* CONFIG_NFS_V4_1 */
5726 }
5727 
5728 #if defined(CONFIG_NFS_V4_1)
5729 static int decode_getdeviceinfo(struct xdr_stream *xdr,
5730 				struct pnfs_device *pdev)
5731 {
5732 	__be32 *p;
5733 	uint32_t len, type;
5734 	int status;
5735 
5736 	status = decode_op_hdr(xdr, OP_GETDEVICEINFO);
5737 	if (status) {
5738 		if (status == -ETOOSMALL) {
5739 			p = xdr_inline_decode(xdr, 4);
5740 			if (unlikely(!p))
5741 				goto out_overflow;
5742 			pdev->mincount = be32_to_cpup(p);
5743 			dprintk("%s: Min count too small. mincnt = %u\n",
5744 				__func__, pdev->mincount);
5745 		}
5746 		return status;
5747 	}
5748 
5749 	p = xdr_inline_decode(xdr, 8);
5750 	if (unlikely(!p))
5751 		goto out_overflow;
5752 	type = be32_to_cpup(p++);
5753 	if (type != pdev->layout_type) {
5754 		dprintk("%s: layout mismatch req: %u pdev: %u\n",
5755 			__func__, pdev->layout_type, type);
5756 		return -EINVAL;
5757 	}
5758 	/*
5759 	 * Get the length of the opaque device_addr4. xdr_read_pages places
5760 	 * the opaque device_addr4 in the xdr_buf->pages (pnfs_device->pages)
5761 	 * and places the remaining xdr data in xdr_buf->tail
5762 	 */
5763 	pdev->mincount = be32_to_cpup(p);
5764 	if (xdr_read_pages(xdr, pdev->mincount) != pdev->mincount)
5765 		goto out_overflow;
5766 
5767 	/* Parse notification bitmap, verifying that it is zero. */
5768 	p = xdr_inline_decode(xdr, 4);
5769 	if (unlikely(!p))
5770 		goto out_overflow;
5771 	len = be32_to_cpup(p);
5772 	if (len) {
5773 		uint32_t i;
5774 
5775 		p = xdr_inline_decode(xdr, 4 * len);
5776 		if (unlikely(!p))
5777 			goto out_overflow;
5778 
5779 		if (be32_to_cpup(p++) &
5780 		    ~(NOTIFY_DEVICEID4_CHANGE | NOTIFY_DEVICEID4_DELETE)) {
5781 			dprintk("%s: unsupported notification\n",
5782 				__func__);
5783 		}
5784 
5785 		for (i = 1; i < len; i++) {
5786 			if (be32_to_cpup(p++)) {
5787 				dprintk("%s: unsupported notification\n",
5788 					__func__);
5789 				return -EIO;
5790 			}
5791 		}
5792 	}
5793 	return 0;
5794 out_overflow:
5795 	print_overflow_msg(__func__, xdr);
5796 	return -EIO;
5797 }
5798 
5799 static int decode_layoutget(struct xdr_stream *xdr, struct rpc_rqst *req,
5800 			    struct nfs4_layoutget_res *res)
5801 {
5802 	__be32 *p;
5803 	int status;
5804 	u32 layout_count;
5805 	u32 recvd;
5806 
5807 	status = decode_op_hdr(xdr, OP_LAYOUTGET);
5808 	if (status)
5809 		return status;
5810 	p = xdr_inline_decode(xdr, 4);
5811 	if (unlikely(!p))
5812 		goto out_overflow;
5813 	res->return_on_close = be32_to_cpup(p);
5814 	decode_stateid(xdr, &res->stateid);
5815 	p = xdr_inline_decode(xdr, 4);
5816 	if (unlikely(!p))
5817 		goto out_overflow;
5818 	layout_count = be32_to_cpup(p);
5819 	if (!layout_count) {
5820 		dprintk("%s: server responded with empty layout array\n",
5821 			__func__);
5822 		return -EINVAL;
5823 	}
5824 
5825 	p = xdr_inline_decode(xdr, 28);
5826 	if (unlikely(!p))
5827 		goto out_overflow;
5828 	p = xdr_decode_hyper(p, &res->range.offset);
5829 	p = xdr_decode_hyper(p, &res->range.length);
5830 	res->range.iomode = be32_to_cpup(p++);
5831 	res->type = be32_to_cpup(p++);
5832 	res->layoutp->len = be32_to_cpup(p);
5833 
5834 	dprintk("%s roff:%lu rlen:%lu riomode:%d, lo_type:0x%x, lo.len:%d\n",
5835 		__func__,
5836 		(unsigned long)res->range.offset,
5837 		(unsigned long)res->range.length,
5838 		res->range.iomode,
5839 		res->type,
5840 		res->layoutp->len);
5841 
5842 	recvd = xdr_read_pages(xdr, res->layoutp->len);
5843 	if (res->layoutp->len > recvd) {
5844 		dprintk("NFS: server cheating in layoutget reply: "
5845 				"layout len %u > recvd %u\n",
5846 				res->layoutp->len, recvd);
5847 		return -EINVAL;
5848 	}
5849 
5850 	if (layout_count > 1) {
5851 		/* We only handle a length one array at the moment.  Any
5852 		 * further entries are just ignored.  Note that this means
5853 		 * the client may see a response that is less than the
5854 		 * minimum it requested.
5855 		 */
5856 		dprintk("%s: server responded with %d layouts, dropping tail\n",
5857 			__func__, layout_count);
5858 	}
5859 
5860 	return 0;
5861 out_overflow:
5862 	print_overflow_msg(__func__, xdr);
5863 	return -EIO;
5864 }
5865 
5866 static int decode_layoutreturn(struct xdr_stream *xdr,
5867 			       struct nfs4_layoutreturn_res *res)
5868 {
5869 	__be32 *p;
5870 	int status;
5871 
5872 	status = decode_op_hdr(xdr, OP_LAYOUTRETURN);
5873 	if (status)
5874 		return status;
5875 	p = xdr_inline_decode(xdr, 4);
5876 	if (unlikely(!p))
5877 		goto out_overflow;
5878 	res->lrs_present = be32_to_cpup(p);
5879 	if (res->lrs_present)
5880 		status = decode_stateid(xdr, &res->stateid);
5881 	return status;
5882 out_overflow:
5883 	print_overflow_msg(__func__, xdr);
5884 	return -EIO;
5885 }
5886 
5887 static int decode_layoutcommit(struct xdr_stream *xdr,
5888 			       struct rpc_rqst *req,
5889 			       struct nfs4_layoutcommit_res *res)
5890 {
5891 	__be32 *p;
5892 	__u32 sizechanged;
5893 	int status;
5894 
5895 	status = decode_op_hdr(xdr, OP_LAYOUTCOMMIT);
5896 	res->status = status;
5897 	if (status)
5898 		return status;
5899 
5900 	p = xdr_inline_decode(xdr, 4);
5901 	if (unlikely(!p))
5902 		goto out_overflow;
5903 	sizechanged = be32_to_cpup(p);
5904 
5905 	if (sizechanged) {
5906 		/* throw away new size */
5907 		p = xdr_inline_decode(xdr, 8);
5908 		if (unlikely(!p))
5909 			goto out_overflow;
5910 	}
5911 	return 0;
5912 out_overflow:
5913 	print_overflow_msg(__func__, xdr);
5914 	return -EIO;
5915 }
5916 
5917 static int decode_test_stateid(struct xdr_stream *xdr,
5918 			       struct nfs41_test_stateid_res *res)
5919 {
5920 	__be32 *p;
5921 	int status;
5922 	int num_res;
5923 
5924 	status = decode_op_hdr(xdr, OP_TEST_STATEID);
5925 	if (status)
5926 		return status;
5927 
5928 	p = xdr_inline_decode(xdr, 4);
5929 	if (unlikely(!p))
5930 		goto out_overflow;
5931 	num_res = be32_to_cpup(p++);
5932 	if (num_res != 1)
5933 		goto out;
5934 
5935 	p = xdr_inline_decode(xdr, 4);
5936 	if (unlikely(!p))
5937 		goto out_overflow;
5938 	res->status = be32_to_cpup(p++);
5939 
5940 	return status;
5941 out_overflow:
5942 	print_overflow_msg(__func__, xdr);
5943 out:
5944 	return -EIO;
5945 }
5946 
5947 static int decode_free_stateid(struct xdr_stream *xdr,
5948 			       struct nfs41_free_stateid_res *res)
5949 {
5950 	res->status = decode_op_hdr(xdr, OP_FREE_STATEID);
5951 	return res->status;
5952 }
5953 #endif /* CONFIG_NFS_V4_1 */
5954 
5955 /*
5956  * END OF "GENERIC" DECODE ROUTINES.
5957  */
5958 
5959 /*
5960  * Decode OPEN_DOWNGRADE response
5961  */
5962 static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp,
5963 				       struct xdr_stream *xdr,
5964 				       struct nfs_closeres *res)
5965 {
5966 	struct compound_hdr hdr;
5967 	int status;
5968 
5969 	status = decode_compound_hdr(xdr, &hdr);
5970 	if (status)
5971 		goto out;
5972 	status = decode_sequence(xdr, &res->seq_res, rqstp);
5973 	if (status)
5974 		goto out;
5975 	status = decode_putfh(xdr);
5976 	if (status)
5977 		goto out;
5978 	status = decode_open_downgrade(xdr, res);
5979 	if (status != 0)
5980 		goto out;
5981 	decode_getfattr(xdr, res->fattr, res->server);
5982 out:
5983 	return status;
5984 }
5985 
5986 /*
5987  * Decode ACCESS response
5988  */
5989 static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5990 			       struct nfs4_accessres *res)
5991 {
5992 	struct compound_hdr hdr;
5993 	int status;
5994 
5995 	status = decode_compound_hdr(xdr, &hdr);
5996 	if (status)
5997 		goto out;
5998 	status = decode_sequence(xdr, &res->seq_res, rqstp);
5999 	if (status)
6000 		goto out;
6001 	status = decode_putfh(xdr);
6002 	if (status != 0)
6003 		goto out;
6004 	status = decode_access(xdr, &res->supported, &res->access);
6005 	if (status != 0)
6006 		goto out;
6007 	decode_getfattr(xdr, res->fattr, res->server);
6008 out:
6009 	return status;
6010 }
6011 
6012 /*
6013  * Decode LOOKUP response
6014  */
6015 static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6016 			       struct nfs4_lookup_res *res)
6017 {
6018 	struct compound_hdr hdr;
6019 	int status;
6020 
6021 	status = decode_compound_hdr(xdr, &hdr);
6022 	if (status)
6023 		goto out;
6024 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6025 	if (status)
6026 		goto out;
6027 	status = decode_putfh(xdr);
6028 	if (status)
6029 		goto out;
6030 	status = decode_lookup(xdr);
6031 	if (status)
6032 		goto out;
6033 	status = decode_getfh(xdr, res->fh);
6034 	if (status)
6035 		goto out;
6036 	status = decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6037 out:
6038 	return status;
6039 }
6040 
6041 /*
6042  * Decode LOOKUP_ROOT response
6043  */
6044 static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp,
6045 				    struct xdr_stream *xdr,
6046 				    struct nfs4_lookup_res *res)
6047 {
6048 	struct compound_hdr hdr;
6049 	int status;
6050 
6051 	status = decode_compound_hdr(xdr, &hdr);
6052 	if (status)
6053 		goto out;
6054 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6055 	if (status)
6056 		goto out;
6057 	status = decode_putrootfh(xdr);
6058 	if (status)
6059 		goto out;
6060 	status = decode_getfh(xdr, res->fh);
6061 	if (status == 0)
6062 		status = decode_getfattr_label(xdr, res->fattr,
6063 						res->label, res->server);
6064 out:
6065 	return status;
6066 }
6067 
6068 /*
6069  * Decode REMOVE response
6070  */
6071 static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6072 			       struct nfs_removeres *res)
6073 {
6074 	struct compound_hdr hdr;
6075 	int status;
6076 
6077 	status = decode_compound_hdr(xdr, &hdr);
6078 	if (status)
6079 		goto out;
6080 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6081 	if (status)
6082 		goto out;
6083 	status = decode_putfh(xdr);
6084 	if (status)
6085 		goto out;
6086 	status = decode_remove(xdr, &res->cinfo);
6087 out:
6088 	return status;
6089 }
6090 
6091 /*
6092  * Decode RENAME response
6093  */
6094 static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6095 			       struct nfs_renameres *res)
6096 {
6097 	struct compound_hdr hdr;
6098 	int status;
6099 
6100 	status = decode_compound_hdr(xdr, &hdr);
6101 	if (status)
6102 		goto out;
6103 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6104 	if (status)
6105 		goto out;
6106 	status = decode_putfh(xdr);
6107 	if (status)
6108 		goto out;
6109 	status = decode_savefh(xdr);
6110 	if (status)
6111 		goto out;
6112 	status = decode_putfh(xdr);
6113 	if (status)
6114 		goto out;
6115 	status = decode_rename(xdr, &res->old_cinfo, &res->new_cinfo);
6116 out:
6117 	return status;
6118 }
6119 
6120 /*
6121  * Decode LINK response
6122  */
6123 static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6124 			     struct nfs4_link_res *res)
6125 {
6126 	struct compound_hdr hdr;
6127 	int status;
6128 
6129 	status = decode_compound_hdr(xdr, &hdr);
6130 	if (status)
6131 		goto out;
6132 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6133 	if (status)
6134 		goto out;
6135 	status = decode_putfh(xdr);
6136 	if (status)
6137 		goto out;
6138 	status = decode_savefh(xdr);
6139 	if (status)
6140 		goto out;
6141 	status = decode_putfh(xdr);
6142 	if (status)
6143 		goto out;
6144 	status = decode_link(xdr, &res->cinfo);
6145 	if (status)
6146 		goto out;
6147 	/*
6148 	 * Note order: OP_LINK leaves the directory as the current
6149 	 *             filehandle.
6150 	 */
6151 	status = decode_restorefh(xdr);
6152 	if (status)
6153 		goto out;
6154 	decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6155 out:
6156 	return status;
6157 }
6158 
6159 /*
6160  * Decode CREATE response
6161  */
6162 static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6163 			       struct nfs4_create_res *res)
6164 {
6165 	struct compound_hdr hdr;
6166 	int status;
6167 
6168 	status = decode_compound_hdr(xdr, &hdr);
6169 	if (status)
6170 		goto out;
6171 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6172 	if (status)
6173 		goto out;
6174 	status = decode_putfh(xdr);
6175 	if (status)
6176 		goto out;
6177 	status = decode_create(xdr, &res->dir_cinfo);
6178 	if (status)
6179 		goto out;
6180 	status = decode_getfh(xdr, res->fh);
6181 	if (status)
6182 		goto out;
6183 	decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6184 out:
6185 	return status;
6186 }
6187 
6188 /*
6189  * Decode SYMLINK response
6190  */
6191 static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6192 				struct nfs4_create_res *res)
6193 {
6194 	return nfs4_xdr_dec_create(rqstp, xdr, res);
6195 }
6196 
6197 /*
6198  * Decode GETATTR response
6199  */
6200 static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6201 				struct nfs4_getattr_res *res)
6202 {
6203 	struct compound_hdr hdr;
6204 	int status;
6205 
6206 	status = decode_compound_hdr(xdr, &hdr);
6207 	if (status)
6208 		goto out;
6209 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6210 	if (status)
6211 		goto out;
6212 	status = decode_putfh(xdr);
6213 	if (status)
6214 		goto out;
6215 	status = decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6216 out:
6217 	return status;
6218 }
6219 
6220 /*
6221  * Encode an SETACL request
6222  */
6223 static void nfs4_xdr_enc_setacl(struct rpc_rqst *req, struct xdr_stream *xdr,
6224 				struct nfs_setaclargs *args)
6225 {
6226 	struct compound_hdr hdr = {
6227 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
6228 	};
6229 
6230 	encode_compound_hdr(xdr, req, &hdr);
6231 	encode_sequence(xdr, &args->seq_args, &hdr);
6232 	encode_putfh(xdr, args->fh, &hdr);
6233 	encode_setacl(xdr, args, &hdr);
6234 	encode_nops(&hdr);
6235 }
6236 
6237 /*
6238  * Decode SETACL response
6239  */
6240 static int
6241 nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6242 		    struct nfs_setaclres *res)
6243 {
6244 	struct compound_hdr hdr;
6245 	int status;
6246 
6247 	status = decode_compound_hdr(xdr, &hdr);
6248 	if (status)
6249 		goto out;
6250 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6251 	if (status)
6252 		goto out;
6253 	status = decode_putfh(xdr);
6254 	if (status)
6255 		goto out;
6256 	status = decode_setattr(xdr);
6257 out:
6258 	return status;
6259 }
6260 
6261 /*
6262  * Decode GETACL response
6263  */
6264 static int
6265 nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6266 		    struct nfs_getaclres *res)
6267 {
6268 	struct compound_hdr hdr;
6269 	int status;
6270 
6271 	if (res->acl_scratch != NULL) {
6272 		void *p = page_address(res->acl_scratch);
6273 		xdr_set_scratch_buffer(xdr, p, PAGE_SIZE);
6274 	}
6275 	status = decode_compound_hdr(xdr, &hdr);
6276 	if (status)
6277 		goto out;
6278 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6279 	if (status)
6280 		goto out;
6281 	status = decode_putfh(xdr);
6282 	if (status)
6283 		goto out;
6284 	status = decode_getacl(xdr, rqstp, res);
6285 
6286 out:
6287 	return status;
6288 }
6289 
6290 /*
6291  * Decode CLOSE response
6292  */
6293 static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6294 			      struct nfs_closeres *res)
6295 {
6296 	struct compound_hdr hdr;
6297 	int status;
6298 
6299 	status = decode_compound_hdr(xdr, &hdr);
6300 	if (status)
6301 		goto out;
6302 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6303 	if (status)
6304 		goto out;
6305 	status = decode_putfh(xdr);
6306 	if (status)
6307 		goto out;
6308 	status = decode_close(xdr, res);
6309 	if (status != 0)
6310 		goto out;
6311 	/*
6312 	 * Note: Server may do delete on close for this file
6313 	 * 	in which case the getattr call will fail with
6314 	 * 	an ESTALE error. Shouldn't be a problem,
6315 	 * 	though, since fattr->valid will remain unset.
6316 	 */
6317 	decode_getfattr(xdr, res->fattr, res->server);
6318 out:
6319 	return status;
6320 }
6321 
6322 /*
6323  * Decode OPEN response
6324  */
6325 static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6326 			     struct nfs_openres *res)
6327 {
6328 	struct compound_hdr hdr;
6329 	int status;
6330 
6331 	status = decode_compound_hdr(xdr, &hdr);
6332 	if (status)
6333 		goto out;
6334 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6335 	if (status)
6336 		goto out;
6337 	status = decode_putfh(xdr);
6338 	if (status)
6339 		goto out;
6340 	status = decode_open(xdr, res);
6341 	if (status)
6342 		goto out;
6343 	status = decode_getfh(xdr, &res->fh);
6344 	if (status)
6345 		goto out;
6346 	if (res->access_request)
6347 		decode_access(xdr, &res->access_supported, &res->access_result);
6348 	decode_getfattr_label(xdr, res->f_attr, res->f_label, res->server);
6349 out:
6350 	return status;
6351 }
6352 
6353 /*
6354  * Decode OPEN_CONFIRM response
6355  */
6356 static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp,
6357 				     struct xdr_stream *xdr,
6358 				     struct nfs_open_confirmres *res)
6359 {
6360 	struct compound_hdr hdr;
6361 	int status;
6362 
6363 	status = decode_compound_hdr(xdr, &hdr);
6364 	if (status)
6365 		goto out;
6366 	status = decode_putfh(xdr);
6367 	if (status)
6368 		goto out;
6369 	status = decode_open_confirm(xdr, res);
6370 out:
6371 	return status;
6372 }
6373 
6374 /*
6375  * Decode OPEN response
6376  */
6377 static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp,
6378 				    struct xdr_stream *xdr,
6379 				    struct nfs_openres *res)
6380 {
6381 	struct compound_hdr hdr;
6382 	int status;
6383 
6384 	status = decode_compound_hdr(xdr, &hdr);
6385 	if (status)
6386 		goto out;
6387 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6388 	if (status)
6389 		goto out;
6390 	status = decode_putfh(xdr);
6391 	if (status)
6392 		goto out;
6393 	status = decode_open(xdr, res);
6394 	if (status)
6395 		goto out;
6396 	if (res->access_request)
6397 		decode_access(xdr, &res->access_supported, &res->access_result);
6398 	decode_getfattr(xdr, res->f_attr, res->server);
6399 out:
6400 	return status;
6401 }
6402 
6403 /*
6404  * Decode SETATTR response
6405  */
6406 static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp,
6407 				struct xdr_stream *xdr,
6408 				struct nfs_setattrres *res)
6409 {
6410 	struct compound_hdr hdr;
6411 	int status;
6412 
6413 	status = decode_compound_hdr(xdr, &hdr);
6414 	if (status)
6415 		goto out;
6416 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6417 	if (status)
6418 		goto out;
6419 	status = decode_putfh(xdr);
6420 	if (status)
6421 		goto out;
6422 	status = decode_setattr(xdr);
6423 	if (status)
6424 		goto out;
6425 	decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6426 out:
6427 	return status;
6428 }
6429 
6430 /*
6431  * Decode LOCK response
6432  */
6433 static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6434 			     struct nfs_lock_res *res)
6435 {
6436 	struct compound_hdr hdr;
6437 	int status;
6438 
6439 	status = decode_compound_hdr(xdr, &hdr);
6440 	if (status)
6441 		goto out;
6442 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6443 	if (status)
6444 		goto out;
6445 	status = decode_putfh(xdr);
6446 	if (status)
6447 		goto out;
6448 	status = decode_lock(xdr, res);
6449 out:
6450 	return status;
6451 }
6452 
6453 /*
6454  * Decode LOCKT response
6455  */
6456 static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6457 			      struct nfs_lockt_res *res)
6458 {
6459 	struct compound_hdr hdr;
6460 	int status;
6461 
6462 	status = decode_compound_hdr(xdr, &hdr);
6463 	if (status)
6464 		goto out;
6465 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6466 	if (status)
6467 		goto out;
6468 	status = decode_putfh(xdr);
6469 	if (status)
6470 		goto out;
6471 	status = decode_lockt(xdr, res);
6472 out:
6473 	return status;
6474 }
6475 
6476 /*
6477  * Decode LOCKU response
6478  */
6479 static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6480 			      struct nfs_locku_res *res)
6481 {
6482 	struct compound_hdr hdr;
6483 	int status;
6484 
6485 	status = decode_compound_hdr(xdr, &hdr);
6486 	if (status)
6487 		goto out;
6488 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6489 	if (status)
6490 		goto out;
6491 	status = decode_putfh(xdr);
6492 	if (status)
6493 		goto out;
6494 	status = decode_locku(xdr, res);
6495 out:
6496 	return status;
6497 }
6498 
6499 static int nfs4_xdr_dec_release_lockowner(struct rpc_rqst *rqstp,
6500 					  struct xdr_stream *xdr, void *dummy)
6501 {
6502 	struct compound_hdr hdr;
6503 	int status;
6504 
6505 	status = decode_compound_hdr(xdr, &hdr);
6506 	if (!status)
6507 		status = decode_release_lockowner(xdr);
6508 	return status;
6509 }
6510 
6511 /*
6512  * Decode READLINK response
6513  */
6514 static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp,
6515 				 struct xdr_stream *xdr,
6516 				 struct nfs4_readlink_res *res)
6517 {
6518 	struct compound_hdr hdr;
6519 	int status;
6520 
6521 	status = decode_compound_hdr(xdr, &hdr);
6522 	if (status)
6523 		goto out;
6524 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6525 	if (status)
6526 		goto out;
6527 	status = decode_putfh(xdr);
6528 	if (status)
6529 		goto out;
6530 	status = decode_readlink(xdr, rqstp);
6531 out:
6532 	return status;
6533 }
6534 
6535 /*
6536  * Decode READDIR response
6537  */
6538 static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6539 				struct nfs4_readdir_res *res)
6540 {
6541 	struct compound_hdr hdr;
6542 	int status;
6543 
6544 	status = decode_compound_hdr(xdr, &hdr);
6545 	if (status)
6546 		goto out;
6547 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6548 	if (status)
6549 		goto out;
6550 	status = decode_putfh(xdr);
6551 	if (status)
6552 		goto out;
6553 	status = decode_readdir(xdr, rqstp, res);
6554 out:
6555 	return status;
6556 }
6557 
6558 /*
6559  * Decode Read response
6560  */
6561 static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6562 			     struct nfs_pgio_res *res)
6563 {
6564 	struct compound_hdr hdr;
6565 	int status;
6566 
6567 	status = decode_compound_hdr(xdr, &hdr);
6568 	if (status)
6569 		goto out;
6570 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6571 	if (status)
6572 		goto out;
6573 	status = decode_putfh(xdr);
6574 	if (status)
6575 		goto out;
6576 	status = decode_read(xdr, rqstp, res);
6577 	if (!status)
6578 		status = res->count;
6579 out:
6580 	return status;
6581 }
6582 
6583 /*
6584  * Decode WRITE response
6585  */
6586 static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6587 			      struct nfs_pgio_res *res)
6588 {
6589 	struct compound_hdr hdr;
6590 	int status;
6591 
6592 	status = decode_compound_hdr(xdr, &hdr);
6593 	if (status)
6594 		goto out;
6595 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6596 	if (status)
6597 		goto out;
6598 	status = decode_putfh(xdr);
6599 	if (status)
6600 		goto out;
6601 	status = decode_write(xdr, res);
6602 	if (status)
6603 		goto out;
6604 	if (res->fattr)
6605 		decode_getfattr(xdr, res->fattr, res->server);
6606 	if (!status)
6607 		status = res->count;
6608 out:
6609 	return status;
6610 }
6611 
6612 /*
6613  * Decode COMMIT response
6614  */
6615 static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6616 			       struct nfs_commitres *res)
6617 {
6618 	struct compound_hdr hdr;
6619 	int status;
6620 
6621 	status = decode_compound_hdr(xdr, &hdr);
6622 	if (status)
6623 		goto out;
6624 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6625 	if (status)
6626 		goto out;
6627 	status = decode_putfh(xdr);
6628 	if (status)
6629 		goto out;
6630 	status = decode_commit(xdr, res);
6631 out:
6632 	return status;
6633 }
6634 
6635 /*
6636  * Decode FSINFO response
6637  */
6638 static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, struct xdr_stream *xdr,
6639 			       struct nfs4_fsinfo_res *res)
6640 {
6641 	struct compound_hdr hdr;
6642 	int status;
6643 
6644 	status = decode_compound_hdr(xdr, &hdr);
6645 	if (!status)
6646 		status = decode_sequence(xdr, &res->seq_res, req);
6647 	if (!status)
6648 		status = decode_putfh(xdr);
6649 	if (!status)
6650 		status = decode_fsinfo(xdr, res->fsinfo);
6651 	return status;
6652 }
6653 
6654 /*
6655  * Decode PATHCONF response
6656  */
6657 static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, struct xdr_stream *xdr,
6658 				 struct nfs4_pathconf_res *res)
6659 {
6660 	struct compound_hdr hdr;
6661 	int status;
6662 
6663 	status = decode_compound_hdr(xdr, &hdr);
6664 	if (!status)
6665 		status = decode_sequence(xdr, &res->seq_res, req);
6666 	if (!status)
6667 		status = decode_putfh(xdr);
6668 	if (!status)
6669 		status = decode_pathconf(xdr, res->pathconf);
6670 	return status;
6671 }
6672 
6673 /*
6674  * Decode STATFS response
6675  */
6676 static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, struct xdr_stream *xdr,
6677 			       struct nfs4_statfs_res *res)
6678 {
6679 	struct compound_hdr hdr;
6680 	int status;
6681 
6682 	status = decode_compound_hdr(xdr, &hdr);
6683 	if (!status)
6684 		status = decode_sequence(xdr, &res->seq_res, req);
6685 	if (!status)
6686 		status = decode_putfh(xdr);
6687 	if (!status)
6688 		status = decode_statfs(xdr, res->fsstat);
6689 	return status;
6690 }
6691 
6692 /*
6693  * Decode GETATTR_BITMAP response
6694  */
6695 static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req,
6696 				    struct xdr_stream *xdr,
6697 				    struct nfs4_server_caps_res *res)
6698 {
6699 	struct compound_hdr hdr;
6700 	int status;
6701 
6702 	status = decode_compound_hdr(xdr, &hdr);
6703 	if (status)
6704 		goto out;
6705 	status = decode_sequence(xdr, &res->seq_res, req);
6706 	if (status)
6707 		goto out;
6708 	status = decode_putfh(xdr);
6709 	if (status)
6710 		goto out;
6711 	status = decode_server_caps(xdr, res);
6712 out:
6713 	return status;
6714 }
6715 
6716 /*
6717  * Decode RENEW response
6718  */
6719 static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6720 			      void *__unused)
6721 {
6722 	struct compound_hdr hdr;
6723 	int status;
6724 
6725 	status = decode_compound_hdr(xdr, &hdr);
6726 	if (!status)
6727 		status = decode_renew(xdr);
6728 	return status;
6729 }
6730 
6731 /*
6732  * Decode SETCLIENTID response
6733  */
6734 static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req,
6735 				    struct xdr_stream *xdr,
6736 				    struct nfs4_setclientid_res *res)
6737 {
6738 	struct compound_hdr hdr;
6739 	int status;
6740 
6741 	status = decode_compound_hdr(xdr, &hdr);
6742 	if (!status)
6743 		status = decode_setclientid(xdr, res);
6744 	return status;
6745 }
6746 
6747 /*
6748  * Decode SETCLIENTID_CONFIRM response
6749  */
6750 static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req,
6751 					    struct xdr_stream *xdr)
6752 {
6753 	struct compound_hdr hdr;
6754 	int status;
6755 
6756 	status = decode_compound_hdr(xdr, &hdr);
6757 	if (!status)
6758 		status = decode_setclientid_confirm(xdr);
6759 	return status;
6760 }
6761 
6762 /*
6763  * Decode DELEGRETURN response
6764  */
6765 static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp,
6766 				    struct xdr_stream *xdr,
6767 				    struct nfs4_delegreturnres *res)
6768 {
6769 	struct compound_hdr hdr;
6770 	int status;
6771 
6772 	status = decode_compound_hdr(xdr, &hdr);
6773 	if (status)
6774 		goto out;
6775 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6776 	if (status)
6777 		goto out;
6778 	status = decode_putfh(xdr);
6779 	if (status != 0)
6780 		goto out;
6781 	status = decode_getfattr(xdr, res->fattr, res->server);
6782 	if (status != 0)
6783 		goto out;
6784 	status = decode_delegreturn(xdr);
6785 out:
6786 	return status;
6787 }
6788 
6789 /*
6790  * Decode FS_LOCATIONS response
6791  */
6792 static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req,
6793 				     struct xdr_stream *xdr,
6794 				     struct nfs4_fs_locations_res *res)
6795 {
6796 	struct compound_hdr hdr;
6797 	int status;
6798 
6799 	status = decode_compound_hdr(xdr, &hdr);
6800 	if (status)
6801 		goto out;
6802 	status = decode_sequence(xdr, &res->seq_res, req);
6803 	if (status)
6804 		goto out;
6805 	status = decode_putfh(xdr);
6806 	if (status)
6807 		goto out;
6808 	if (res->migration) {
6809 		xdr_enter_page(xdr, PAGE_SIZE);
6810 		status = decode_getfattr_generic(xdr,
6811 					&res->fs_locations->fattr,
6812 					 NULL, res->fs_locations,
6813 					 NULL, res->fs_locations->server);
6814 		if (status)
6815 			goto out;
6816 		if (res->renew)
6817 			status = decode_renew(xdr);
6818 	} else {
6819 		status = decode_lookup(xdr);
6820 		if (status)
6821 			goto out;
6822 		xdr_enter_page(xdr, PAGE_SIZE);
6823 		status = decode_getfattr_generic(xdr,
6824 					&res->fs_locations->fattr,
6825 					 NULL, res->fs_locations,
6826 					 NULL, res->fs_locations->server);
6827 	}
6828 out:
6829 	return status;
6830 }
6831 
6832 /*
6833  * Decode SECINFO response
6834  */
6835 static int nfs4_xdr_dec_secinfo(struct rpc_rqst *rqstp,
6836 				struct xdr_stream *xdr,
6837 				struct nfs4_secinfo_res *res)
6838 {
6839 	struct compound_hdr hdr;
6840 	int status;
6841 
6842 	status = decode_compound_hdr(xdr, &hdr);
6843 	if (status)
6844 		goto out;
6845 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6846 	if (status)
6847 		goto out;
6848 	status = decode_putfh(xdr);
6849 	if (status)
6850 		goto out;
6851 	status = decode_secinfo(xdr, res);
6852 out:
6853 	return status;
6854 }
6855 
6856 /*
6857  * Decode FSID_PRESENT response
6858  */
6859 static int nfs4_xdr_dec_fsid_present(struct rpc_rqst *rqstp,
6860 				     struct xdr_stream *xdr,
6861 				     struct nfs4_fsid_present_res *res)
6862 {
6863 	struct compound_hdr hdr;
6864 	int status;
6865 
6866 	status = decode_compound_hdr(xdr, &hdr);
6867 	if (status)
6868 		goto out;
6869 	status = decode_sequence(xdr, &res->seq_res, rqstp);
6870 	if (status)
6871 		goto out;
6872 	status = decode_putfh(xdr);
6873 	if (status)
6874 		goto out;
6875 	status = decode_getfh(xdr, res->fh);
6876 	if (status)
6877 		goto out;
6878 	if (res->renew)
6879 		status = decode_renew(xdr);
6880 out:
6881 	return status;
6882 }
6883 
6884 #if defined(CONFIG_NFS_V4_1)
6885 /*
6886  * Decode BIND_CONN_TO_SESSION response
6887  */
6888 static int nfs4_xdr_dec_bind_conn_to_session(struct rpc_rqst *rqstp,
6889 					struct xdr_stream *xdr,
6890 					void *res)
6891 {
6892 	struct compound_hdr hdr;
6893 	int status;
6894 
6895 	status = decode_compound_hdr(xdr, &hdr);
6896 	if (!status)
6897 		status = decode_bind_conn_to_session(xdr, res);
6898 	return status;
6899 }
6900 
6901 /*
6902  * Decode EXCHANGE_ID response
6903  */
6904 static int nfs4_xdr_dec_exchange_id(struct rpc_rqst *rqstp,
6905 				    struct xdr_stream *xdr,
6906 				    void *res)
6907 {
6908 	struct compound_hdr hdr;
6909 	int status;
6910 
6911 	status = decode_compound_hdr(xdr, &hdr);
6912 	if (!status)
6913 		status = decode_exchange_id(xdr, res);
6914 	return status;
6915 }
6916 
6917 /*
6918  * Decode CREATE_SESSION response
6919  */
6920 static int nfs4_xdr_dec_create_session(struct rpc_rqst *rqstp,
6921 				       struct xdr_stream *xdr,
6922 				       struct nfs41_create_session_res *res)
6923 {
6924 	struct compound_hdr hdr;
6925 	int status;
6926 
6927 	status = decode_compound_hdr(xdr, &hdr);
6928 	if (!status)
6929 		status = decode_create_session(xdr, res);
6930 	return status;
6931 }
6932 
6933 /*
6934  * Decode DESTROY_SESSION response
6935  */
6936 static int nfs4_xdr_dec_destroy_session(struct rpc_rqst *rqstp,
6937 					struct xdr_stream *xdr,
6938 					void *res)
6939 {
6940 	struct compound_hdr hdr;
6941 	int status;
6942 
6943 	status = decode_compound_hdr(xdr, &hdr);
6944 	if (!status)
6945 		status = decode_destroy_session(xdr, res);
6946 	return status;
6947 }
6948 
6949 /*
6950  * Decode DESTROY_CLIENTID response
6951  */
6952 static int nfs4_xdr_dec_destroy_clientid(struct rpc_rqst *rqstp,
6953 					struct xdr_stream *xdr,
6954 					void *res)
6955 {
6956 	struct compound_hdr hdr;
6957 	int status;
6958 
6959 	status = decode_compound_hdr(xdr, &hdr);
6960 	if (!status)
6961 		status = decode_destroy_clientid(xdr, res);
6962 	return status;
6963 }
6964 
6965 /*
6966  * Decode SEQUENCE response
6967  */
6968 static int nfs4_xdr_dec_sequence(struct rpc_rqst *rqstp,
6969 				 struct xdr_stream *xdr,
6970 				 struct nfs4_sequence_res *res)
6971 {
6972 	struct compound_hdr hdr;
6973 	int status;
6974 
6975 	status = decode_compound_hdr(xdr, &hdr);
6976 	if (!status)
6977 		status = decode_sequence(xdr, res, rqstp);
6978 	return status;
6979 }
6980 
6981 /*
6982  * Decode GET_LEASE_TIME response
6983  */
6984 static int nfs4_xdr_dec_get_lease_time(struct rpc_rqst *rqstp,
6985 				       struct xdr_stream *xdr,
6986 				       struct nfs4_get_lease_time_res *res)
6987 {
6988 	struct compound_hdr hdr;
6989 	int status;
6990 
6991 	status = decode_compound_hdr(xdr, &hdr);
6992 	if (!status)
6993 		status = decode_sequence(xdr, &res->lr_seq_res, rqstp);
6994 	if (!status)
6995 		status = decode_putrootfh(xdr);
6996 	if (!status)
6997 		status = decode_fsinfo(xdr, res->lr_fsinfo);
6998 	return status;
6999 }
7000 
7001 /*
7002  * Decode RECLAIM_COMPLETE response
7003  */
7004 static int nfs4_xdr_dec_reclaim_complete(struct rpc_rqst *rqstp,
7005 					 struct xdr_stream *xdr,
7006 					 struct nfs41_reclaim_complete_res *res)
7007 {
7008 	struct compound_hdr hdr;
7009 	int status;
7010 
7011 	status = decode_compound_hdr(xdr, &hdr);
7012 	if (!status)
7013 		status = decode_sequence(xdr, &res->seq_res, rqstp);
7014 	if (!status)
7015 		status = decode_reclaim_complete(xdr, NULL);
7016 	return status;
7017 }
7018 
7019 /*
7020  * Decode GETDEVINFO response
7021  */
7022 static int nfs4_xdr_dec_getdeviceinfo(struct rpc_rqst *rqstp,
7023 				      struct xdr_stream *xdr,
7024 				      struct nfs4_getdeviceinfo_res *res)
7025 {
7026 	struct compound_hdr hdr;
7027 	int status;
7028 
7029 	status = decode_compound_hdr(xdr, &hdr);
7030 	if (status != 0)
7031 		goto out;
7032 	status = decode_sequence(xdr, &res->seq_res, rqstp);
7033 	if (status != 0)
7034 		goto out;
7035 	status = decode_getdeviceinfo(xdr, res->pdev);
7036 out:
7037 	return status;
7038 }
7039 
7040 /*
7041  * Decode LAYOUTGET response
7042  */
7043 static int nfs4_xdr_dec_layoutget(struct rpc_rqst *rqstp,
7044 				  struct xdr_stream *xdr,
7045 				  struct nfs4_layoutget_res *res)
7046 {
7047 	struct compound_hdr hdr;
7048 	int status;
7049 
7050 	status = decode_compound_hdr(xdr, &hdr);
7051 	if (status)
7052 		goto out;
7053 	status = decode_sequence(xdr, &res->seq_res, rqstp);
7054 	if (status)
7055 		goto out;
7056 	status = decode_putfh(xdr);
7057 	if (status)
7058 		goto out;
7059 	status = decode_layoutget(xdr, rqstp, res);
7060 out:
7061 	return status;
7062 }
7063 
7064 /*
7065  * Decode LAYOUTRETURN response
7066  */
7067 static int nfs4_xdr_dec_layoutreturn(struct rpc_rqst *rqstp,
7068 				     struct xdr_stream *xdr,
7069 				     struct nfs4_layoutreturn_res *res)
7070 {
7071 	struct compound_hdr hdr;
7072 	int status;
7073 
7074 	status = decode_compound_hdr(xdr, &hdr);
7075 	if (status)
7076 		goto out;
7077 	status = decode_sequence(xdr, &res->seq_res, rqstp);
7078 	if (status)
7079 		goto out;
7080 	status = decode_putfh(xdr);
7081 	if (status)
7082 		goto out;
7083 	status = decode_layoutreturn(xdr, res);
7084 out:
7085 	return status;
7086 }
7087 
7088 /*
7089  * Decode LAYOUTCOMMIT response
7090  */
7091 static int nfs4_xdr_dec_layoutcommit(struct rpc_rqst *rqstp,
7092 				     struct xdr_stream *xdr,
7093 				     struct nfs4_layoutcommit_res *res)
7094 {
7095 	struct compound_hdr hdr;
7096 	int status;
7097 
7098 	status = decode_compound_hdr(xdr, &hdr);
7099 	if (status)
7100 		goto out;
7101 	status = decode_sequence(xdr, &res->seq_res, rqstp);
7102 	if (status)
7103 		goto out;
7104 	status = decode_putfh(xdr);
7105 	if (status)
7106 		goto out;
7107 	status = decode_layoutcommit(xdr, rqstp, res);
7108 	if (status)
7109 		goto out;
7110 	decode_getfattr(xdr, res->fattr, res->server);
7111 out:
7112 	return status;
7113 }
7114 
7115 /*
7116  * Decode SECINFO_NO_NAME response
7117  */
7118 static int nfs4_xdr_dec_secinfo_no_name(struct rpc_rqst *rqstp,
7119 					struct xdr_stream *xdr,
7120 					struct nfs4_secinfo_res *res)
7121 {
7122 	struct compound_hdr hdr;
7123 	int status;
7124 
7125 	status = decode_compound_hdr(xdr, &hdr);
7126 	if (status)
7127 		goto out;
7128 	status = decode_sequence(xdr, &res->seq_res, rqstp);
7129 	if (status)
7130 		goto out;
7131 	status = decode_putrootfh(xdr);
7132 	if (status)
7133 		goto out;
7134 	status = decode_secinfo_no_name(xdr, res);
7135 out:
7136 	return status;
7137 }
7138 
7139 /*
7140  * Decode TEST_STATEID response
7141  */
7142 static int nfs4_xdr_dec_test_stateid(struct rpc_rqst *rqstp,
7143 				     struct xdr_stream *xdr,
7144 				     struct nfs41_test_stateid_res *res)
7145 {
7146 	struct compound_hdr hdr;
7147 	int status;
7148 
7149 	status = decode_compound_hdr(xdr, &hdr);
7150 	if (status)
7151 		goto out;
7152 	status = decode_sequence(xdr, &res->seq_res, rqstp);
7153 	if (status)
7154 		goto out;
7155 	status = decode_test_stateid(xdr, res);
7156 out:
7157 	return status;
7158 }
7159 
7160 /*
7161  * Decode FREE_STATEID response
7162  */
7163 static int nfs4_xdr_dec_free_stateid(struct rpc_rqst *rqstp,
7164 				     struct xdr_stream *xdr,
7165 				     struct nfs41_free_stateid_res *res)
7166 {
7167 	struct compound_hdr hdr;
7168 	int status;
7169 
7170 	status = decode_compound_hdr(xdr, &hdr);
7171 	if (status)
7172 		goto out;
7173 	status = decode_sequence(xdr, &res->seq_res, rqstp);
7174 	if (status)
7175 		goto out;
7176 	status = decode_free_stateid(xdr, res);
7177 out:
7178 	return status;
7179 }
7180 #endif /* CONFIG_NFS_V4_1 */
7181 
7182 /**
7183  * nfs4_decode_dirent - Decode a single NFSv4 directory entry stored in
7184  *                      the local page cache.
7185  * @xdr: XDR stream where entry resides
7186  * @entry: buffer to fill in with entry data
7187  * @plus: boolean indicating whether this should be a readdirplus entry
7188  *
7189  * Returns zero if successful, otherwise a negative errno value is
7190  * returned.
7191  *
7192  * This function is not invoked during READDIR reply decoding, but
7193  * rather whenever an application invokes the getdents(2) system call
7194  * on a directory already in our cache.
7195  */
7196 int nfs4_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
7197 		       int plus)
7198 {
7199 	unsigned int savep;
7200 	uint32_t bitmap[3] = {0};
7201 	uint32_t len;
7202 	__be32 *p = xdr_inline_decode(xdr, 4);
7203 	if (unlikely(!p))
7204 		goto out_overflow;
7205 	if (*p == xdr_zero) {
7206 		p = xdr_inline_decode(xdr, 4);
7207 		if (unlikely(!p))
7208 			goto out_overflow;
7209 		if (*p == xdr_zero)
7210 			return -EAGAIN;
7211 		entry->eof = 1;
7212 		return -EBADCOOKIE;
7213 	}
7214 
7215 	p = xdr_inline_decode(xdr, 12);
7216 	if (unlikely(!p))
7217 		goto out_overflow;
7218 	entry->prev_cookie = entry->cookie;
7219 	p = xdr_decode_hyper(p, &entry->cookie);
7220 	entry->len = be32_to_cpup(p);
7221 
7222 	p = xdr_inline_decode(xdr, entry->len);
7223 	if (unlikely(!p))
7224 		goto out_overflow;
7225 	entry->name = (const char *) p;
7226 
7227 	/*
7228 	 * In case the server doesn't return an inode number,
7229 	 * we fake one here.  (We don't use inode number 0,
7230 	 * since glibc seems to choke on it...)
7231 	 */
7232 	entry->ino = 1;
7233 	entry->fattr->valid = 0;
7234 
7235 	if (decode_attr_bitmap(xdr, bitmap) < 0)
7236 		goto out_overflow;
7237 
7238 	if (decode_attr_length(xdr, &len, &savep) < 0)
7239 		goto out_overflow;
7240 
7241 	if (decode_getfattr_attrs(xdr, bitmap, entry->fattr, entry->fh,
7242 			NULL, entry->label, entry->server) < 0)
7243 		goto out_overflow;
7244 	if (entry->fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
7245 		entry->ino = entry->fattr->mounted_on_fileid;
7246 	else if (entry->fattr->valid & NFS_ATTR_FATTR_FILEID)
7247 		entry->ino = entry->fattr->fileid;
7248 
7249 	entry->d_type = DT_UNKNOWN;
7250 	if (entry->fattr->valid & NFS_ATTR_FATTR_TYPE)
7251 		entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
7252 
7253 	return 0;
7254 
7255 out_overflow:
7256 	print_overflow_msg(__func__, xdr);
7257 	return -EAGAIN;
7258 }
7259 
7260 /*
7261  * We need to translate between nfs status return values and
7262  * the local errno values which may not be the same.
7263  */
7264 static struct {
7265 	int stat;
7266 	int errno;
7267 } nfs_errtbl[] = {
7268 	{ NFS4_OK,		0		},
7269 	{ NFS4ERR_PERM,		-EPERM		},
7270 	{ NFS4ERR_NOENT,	-ENOENT		},
7271 	{ NFS4ERR_IO,		-errno_NFSERR_IO},
7272 	{ NFS4ERR_NXIO,		-ENXIO		},
7273 	{ NFS4ERR_ACCESS,	-EACCES		},
7274 	{ NFS4ERR_EXIST,	-EEXIST		},
7275 	{ NFS4ERR_XDEV,		-EXDEV		},
7276 	{ NFS4ERR_NOTDIR,	-ENOTDIR	},
7277 	{ NFS4ERR_ISDIR,	-EISDIR		},
7278 	{ NFS4ERR_INVAL,	-EINVAL		},
7279 	{ NFS4ERR_FBIG,		-EFBIG		},
7280 	{ NFS4ERR_NOSPC,	-ENOSPC		},
7281 	{ NFS4ERR_ROFS,		-EROFS		},
7282 	{ NFS4ERR_MLINK,	-EMLINK		},
7283 	{ NFS4ERR_NAMETOOLONG,	-ENAMETOOLONG	},
7284 	{ NFS4ERR_NOTEMPTY,	-ENOTEMPTY	},
7285 	{ NFS4ERR_DQUOT,	-EDQUOT		},
7286 	{ NFS4ERR_STALE,	-ESTALE		},
7287 	{ NFS4ERR_BADHANDLE,	-EBADHANDLE	},
7288 	{ NFS4ERR_BAD_COOKIE,	-EBADCOOKIE	},
7289 	{ NFS4ERR_NOTSUPP,	-ENOTSUPP	},
7290 	{ NFS4ERR_TOOSMALL,	-ETOOSMALL	},
7291 	{ NFS4ERR_SERVERFAULT,	-EREMOTEIO	},
7292 	{ NFS4ERR_BADTYPE,	-EBADTYPE	},
7293 	{ NFS4ERR_LOCKED,	-EAGAIN		},
7294 	{ NFS4ERR_SYMLINK,	-ELOOP		},
7295 	{ NFS4ERR_OP_ILLEGAL,	-EOPNOTSUPP	},
7296 	{ NFS4ERR_DEADLOCK,	-EDEADLK	},
7297 	{ -1,			-EIO		}
7298 };
7299 
7300 /*
7301  * Convert an NFS error code to a local one.
7302  * This one is used jointly by NFSv2 and NFSv3.
7303  */
7304 static int
7305 nfs4_stat_to_errno(int stat)
7306 {
7307 	int i;
7308 	for (i = 0; nfs_errtbl[i].stat != -1; i++) {
7309 		if (nfs_errtbl[i].stat == stat)
7310 			return nfs_errtbl[i].errno;
7311 	}
7312 	if (stat <= 10000 || stat > 10100) {
7313 		/* The server is looney tunes. */
7314 		return -EREMOTEIO;
7315 	}
7316 	/* If we cannot translate the error, the recovery routines should
7317 	 * handle it.
7318 	 * Note: remaining NFSv4 error codes have values > 10000, so should
7319 	 * not conflict with native Linux error codes.
7320 	 */
7321 	return -stat;
7322 }
7323 
7324 #ifdef CONFIG_NFS_V4_2
7325 #include "nfs42xdr.c"
7326 #endif /* CONFIG_NFS_V4_2 */
7327 
7328 #define PROC(proc, argtype, restype)				\
7329 [NFSPROC4_CLNT_##proc] = {					\
7330 	.p_proc   = NFSPROC4_COMPOUND,				\
7331 	.p_encode = (kxdreproc_t)nfs4_xdr_##argtype,		\
7332 	.p_decode = (kxdrdproc_t)nfs4_xdr_##restype,		\
7333 	.p_arglen = NFS4_##argtype##_sz,			\
7334 	.p_replen = NFS4_##restype##_sz,			\
7335 	.p_statidx = NFSPROC4_CLNT_##proc,			\
7336 	.p_name   = #proc,					\
7337 }
7338 
7339 struct rpc_procinfo	nfs4_procedures[] = {
7340 	PROC(READ,		enc_read,		dec_read),
7341 	PROC(WRITE,		enc_write,		dec_write),
7342 	PROC(COMMIT,		enc_commit,		dec_commit),
7343 	PROC(OPEN,		enc_open,		dec_open),
7344 	PROC(OPEN_CONFIRM,	enc_open_confirm,	dec_open_confirm),
7345 	PROC(OPEN_NOATTR,	enc_open_noattr,	dec_open_noattr),
7346 	PROC(OPEN_DOWNGRADE,	enc_open_downgrade,	dec_open_downgrade),
7347 	PROC(CLOSE,		enc_close,		dec_close),
7348 	PROC(SETATTR,		enc_setattr,		dec_setattr),
7349 	PROC(FSINFO,		enc_fsinfo,		dec_fsinfo),
7350 	PROC(RENEW,		enc_renew,		dec_renew),
7351 	PROC(SETCLIENTID,	enc_setclientid,	dec_setclientid),
7352 	PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
7353 	PROC(LOCK,		enc_lock,		dec_lock),
7354 	PROC(LOCKT,		enc_lockt,		dec_lockt),
7355 	PROC(LOCKU,		enc_locku,		dec_locku),
7356 	PROC(ACCESS,		enc_access,		dec_access),
7357 	PROC(GETATTR,		enc_getattr,		dec_getattr),
7358 	PROC(LOOKUP,		enc_lookup,		dec_lookup),
7359 	PROC(LOOKUP_ROOT,	enc_lookup_root,	dec_lookup_root),
7360 	PROC(REMOVE,		enc_remove,		dec_remove),
7361 	PROC(RENAME,		enc_rename,		dec_rename),
7362 	PROC(LINK,		enc_link,		dec_link),
7363 	PROC(SYMLINK,		enc_symlink,		dec_symlink),
7364 	PROC(CREATE,		enc_create,		dec_create),
7365 	PROC(PATHCONF,		enc_pathconf,		dec_pathconf),
7366 	PROC(STATFS,		enc_statfs,		dec_statfs),
7367 	PROC(READLINK,		enc_readlink,		dec_readlink),
7368 	PROC(READDIR,		enc_readdir,		dec_readdir),
7369 	PROC(SERVER_CAPS,	enc_server_caps,	dec_server_caps),
7370 	PROC(DELEGRETURN,	enc_delegreturn,	dec_delegreturn),
7371 	PROC(GETACL,		enc_getacl,		dec_getacl),
7372 	PROC(SETACL,		enc_setacl,		dec_setacl),
7373 	PROC(FS_LOCATIONS,	enc_fs_locations,	dec_fs_locations),
7374 	PROC(RELEASE_LOCKOWNER,	enc_release_lockowner,	dec_release_lockowner),
7375 	PROC(SECINFO,		enc_secinfo,		dec_secinfo),
7376 	PROC(FSID_PRESENT,	enc_fsid_present,	dec_fsid_present),
7377 #if defined(CONFIG_NFS_V4_1)
7378 	PROC(EXCHANGE_ID,	enc_exchange_id,	dec_exchange_id),
7379 	PROC(CREATE_SESSION,	enc_create_session,	dec_create_session),
7380 	PROC(DESTROY_SESSION,	enc_destroy_session,	dec_destroy_session),
7381 	PROC(SEQUENCE,		enc_sequence,		dec_sequence),
7382 	PROC(GET_LEASE_TIME,	enc_get_lease_time,	dec_get_lease_time),
7383 	PROC(RECLAIM_COMPLETE,	enc_reclaim_complete,	dec_reclaim_complete),
7384 	PROC(GETDEVICEINFO,	enc_getdeviceinfo,	dec_getdeviceinfo),
7385 	PROC(LAYOUTGET,		enc_layoutget,		dec_layoutget),
7386 	PROC(LAYOUTCOMMIT,	enc_layoutcommit,	dec_layoutcommit),
7387 	PROC(LAYOUTRETURN,	enc_layoutreturn,	dec_layoutreturn),
7388 	PROC(SECINFO_NO_NAME,	enc_secinfo_no_name,	dec_secinfo_no_name),
7389 	PROC(TEST_STATEID,	enc_test_stateid,	dec_test_stateid),
7390 	PROC(FREE_STATEID,	enc_free_stateid,	dec_free_stateid),
7391 	PROC(BIND_CONN_TO_SESSION,
7392 			enc_bind_conn_to_session, dec_bind_conn_to_session),
7393 	PROC(DESTROY_CLIENTID,	enc_destroy_clientid,	dec_destroy_clientid),
7394 #endif /* CONFIG_NFS_V4_1 */
7395 #ifdef CONFIG_NFS_V4_2
7396 	PROC(SEEK,		enc_seek,		dec_seek),
7397 #endif /* CONFIG_NFS_V4_2 */
7398 };
7399 
7400 const struct rpc_version nfs_version4 = {
7401 	.number			= 4,
7402 	.nrprocs		= ARRAY_SIZE(nfs4_procedures),
7403 	.procs			= nfs4_procedures
7404 };
7405 
7406 /*
7407  * Local variables:
7408  *  c-basic-offset: 8
7409  * End:
7410  */
7411