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