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