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