xref: /openbmc/linux/fs/nfs/nfs42xdr.c (revision bb05a617f06b7a882e19c4f475b8e37f14d9ceac)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
4  */
5 #ifndef __LINUX_FS_NFS_NFS4_2XDR_H
6 #define __LINUX_FS_NFS_NFS4_2XDR_H
7 
8 #include "nfs42.h"
9 
10 /* Not limited by NFS itself, limited by the generic xattr code */
11 #define nfs4_xattr_name_maxsz   XDR_QUADLEN(XATTR_NAME_MAX)
12 
13 #define encode_fallocate_maxsz		(encode_stateid_maxsz + \
14 					 2 /* offset */ + \
15 					 2 /* length */)
16 #define NFS42_WRITE_RES_SIZE		(1 /* wr_callback_id size */ +\
17 					 XDR_QUADLEN(NFS4_STATEID_SIZE) + \
18 					 2 /* wr_count */ + \
19 					 1 /* wr_committed */ + \
20 					 XDR_QUADLEN(NFS4_VERIFIER_SIZE))
21 #define encode_allocate_maxsz		(op_encode_hdr_maxsz + \
22 					 encode_fallocate_maxsz)
23 #define decode_allocate_maxsz		(op_decode_hdr_maxsz)
24 #define encode_copy_maxsz		(op_encode_hdr_maxsz +          \
25 					 XDR_QUADLEN(NFS4_STATEID_SIZE) + \
26 					 XDR_QUADLEN(NFS4_STATEID_SIZE) + \
27 					 2 + 2 + 2 + 1 + 1 + 1 +\
28 					 1 + /* One cnr_source_server */\
29 					 1 + /* nl4_type */ \
30 					 1 + XDR_QUADLEN(NFS4_OPAQUE_LIMIT))
31 #define decode_copy_maxsz		(op_decode_hdr_maxsz + \
32 					 NFS42_WRITE_RES_SIZE + \
33 					 1 /* cr_consecutive */ + \
34 					 1 /* cr_synchronous */)
35 #define encode_offload_cancel_maxsz	(op_encode_hdr_maxsz + \
36 					 XDR_QUADLEN(NFS4_STATEID_SIZE))
37 #define decode_offload_cancel_maxsz	(op_decode_hdr_maxsz)
38 #define encode_copy_notify_maxsz	(op_encode_hdr_maxsz + \
39 					 XDR_QUADLEN(NFS4_STATEID_SIZE) + \
40 					 1 + /* nl4_type */ \
41 					 1 + XDR_QUADLEN(NFS4_OPAQUE_LIMIT))
42 #define decode_copy_notify_maxsz	(op_decode_hdr_maxsz + \
43 					 3 + /* cnr_lease_time */\
44 					 XDR_QUADLEN(NFS4_STATEID_SIZE) + \
45 					 1 + /* Support 1 cnr_source_server */\
46 					 1 + /* nl4_type */ \
47 					 1 + XDR_QUADLEN(NFS4_OPAQUE_LIMIT))
48 #define encode_deallocate_maxsz		(op_encode_hdr_maxsz + \
49 					 encode_fallocate_maxsz)
50 #define decode_deallocate_maxsz		(op_decode_hdr_maxsz)
51 #define encode_read_plus_maxsz		(op_encode_hdr_maxsz + \
52 					 encode_stateid_maxsz + 3)
53 #define NFS42_READ_PLUS_DATA_SEGMENT_SIZE \
54 					(1 /* data_content4 */ + \
55 					 2 /* data_info4.di_offset */ + \
56 					 1 /* data_info4.di_length */)
57 #define decode_read_plus_maxsz		(op_decode_hdr_maxsz + \
58 					 1 /* rpr_eof */ + \
59 					 1 /* rpr_contents count */ + \
60 					 NFS42_READ_PLUS_DATA_SEGMENT_SIZE)
61 #define encode_seek_maxsz		(op_encode_hdr_maxsz + \
62 					 encode_stateid_maxsz + \
63 					 2 /* offset */ + \
64 					 1 /* whence */)
65 #define decode_seek_maxsz		(op_decode_hdr_maxsz + \
66 					 1 /* eof */ + \
67 					 1 /* whence */ + \
68 					 2 /* offset */ + \
69 					 2 /* length */)
70 #define encode_io_info_maxsz		4
71 #define encode_layoutstats_maxsz	(op_decode_hdr_maxsz + \
72 					2 /* offset */ + \
73 					2 /* length */ + \
74 					encode_stateid_maxsz + \
75 					encode_io_info_maxsz + \
76 					encode_io_info_maxsz + \
77 					1 /* opaque devaddr4 length */ + \
78 					XDR_QUADLEN(PNFS_LAYOUTSTATS_MAXSIZE))
79 #define decode_layoutstats_maxsz	(op_decode_hdr_maxsz)
80 #define encode_device_error_maxsz	(XDR_QUADLEN(NFS4_DEVICEID4_SIZE) + \
81 					1 /* status */ + 1 /* opnum */)
82 #define encode_layouterror_maxsz	(op_decode_hdr_maxsz + \
83 					2 /* offset */ + \
84 					2 /* length */ + \
85 					encode_stateid_maxsz + \
86 					1 /* Array size */ + \
87 					encode_device_error_maxsz)
88 #define decode_layouterror_maxsz	(op_decode_hdr_maxsz)
89 #define encode_clone_maxsz		(encode_stateid_maxsz + \
90 					encode_stateid_maxsz + \
91 					2 /* src offset */ + \
92 					2 /* dst offset */ + \
93 					2 /* count */)
94 #define decode_clone_maxsz		(op_decode_hdr_maxsz)
95 #define encode_getxattr_maxsz		(op_encode_hdr_maxsz + 1 + \
96 					 nfs4_xattr_name_maxsz)
97 #define decode_getxattr_maxsz		(op_decode_hdr_maxsz + 1 + pagepad_maxsz)
98 #define encode_setxattr_maxsz		(op_encode_hdr_maxsz + \
99 					 1 + nfs4_xattr_name_maxsz + 1)
100 #define decode_setxattr_maxsz		(op_decode_hdr_maxsz + decode_change_info_maxsz)
101 #define encode_listxattrs_maxsz		(op_encode_hdr_maxsz + 2 + 1)
102 #define decode_listxattrs_maxsz		(op_decode_hdr_maxsz + 2 + 1 + 1 + 1)
103 #define encode_removexattr_maxsz	(op_encode_hdr_maxsz + 1 + \
104 					 nfs4_xattr_name_maxsz)
105 #define decode_removexattr_maxsz	(op_decode_hdr_maxsz + \
106 					 decode_change_info_maxsz)
107 
108 #define NFS4_enc_allocate_sz		(compound_encode_hdr_maxsz + \
109 					 encode_sequence_maxsz + \
110 					 encode_putfh_maxsz + \
111 					 encode_allocate_maxsz + \
112 					 encode_getattr_maxsz)
113 #define NFS4_dec_allocate_sz		(compound_decode_hdr_maxsz + \
114 					 decode_sequence_maxsz + \
115 					 decode_putfh_maxsz + \
116 					 decode_allocate_maxsz + \
117 					 decode_getattr_maxsz)
118 #define NFS4_enc_copy_sz		(compound_encode_hdr_maxsz + \
119 					 encode_sequence_maxsz + \
120 					 encode_putfh_maxsz + \
121 					 encode_savefh_maxsz + \
122 					 encode_putfh_maxsz + \
123 					 encode_copy_maxsz + \
124 					 encode_commit_maxsz)
125 #define NFS4_dec_copy_sz		(compound_decode_hdr_maxsz + \
126 					 decode_sequence_maxsz + \
127 					 decode_putfh_maxsz + \
128 					 decode_savefh_maxsz + \
129 					 decode_putfh_maxsz + \
130 					 decode_copy_maxsz + \
131 					 decode_commit_maxsz)
132 #define NFS4_enc_offload_cancel_sz	(compound_encode_hdr_maxsz + \
133 					 encode_sequence_maxsz + \
134 					 encode_putfh_maxsz + \
135 					 encode_offload_cancel_maxsz)
136 #define NFS4_dec_offload_cancel_sz	(compound_decode_hdr_maxsz + \
137 					 decode_sequence_maxsz + \
138 					 decode_putfh_maxsz + \
139 					 decode_offload_cancel_maxsz)
140 #define NFS4_enc_copy_notify_sz		(compound_encode_hdr_maxsz + \
141 					 encode_putfh_maxsz + \
142 					 encode_copy_notify_maxsz)
143 #define NFS4_dec_copy_notify_sz		(compound_decode_hdr_maxsz + \
144 					 decode_putfh_maxsz + \
145 					 decode_copy_notify_maxsz)
146 #define NFS4_enc_deallocate_sz		(compound_encode_hdr_maxsz + \
147 					 encode_sequence_maxsz + \
148 					 encode_putfh_maxsz + \
149 					 encode_deallocate_maxsz + \
150 					 encode_getattr_maxsz)
151 #define NFS4_dec_deallocate_sz		(compound_decode_hdr_maxsz + \
152 					 decode_sequence_maxsz + \
153 					 decode_putfh_maxsz + \
154 					 decode_deallocate_maxsz + \
155 					 decode_getattr_maxsz)
156 #define NFS4_enc_read_plus_sz		(compound_encode_hdr_maxsz + \
157 					 encode_sequence_maxsz + \
158 					 encode_putfh_maxsz + \
159 					 encode_read_plus_maxsz)
160 #define NFS4_dec_read_plus_sz		(compound_decode_hdr_maxsz + \
161 					 decode_sequence_maxsz + \
162 					 decode_putfh_maxsz + \
163 					 decode_read_plus_maxsz)
164 #define NFS4_enc_seek_sz		(compound_encode_hdr_maxsz + \
165 					 encode_sequence_maxsz + \
166 					 encode_putfh_maxsz + \
167 					 encode_seek_maxsz)
168 #define NFS4_dec_seek_sz		(compound_decode_hdr_maxsz + \
169 					 decode_sequence_maxsz + \
170 					 decode_putfh_maxsz + \
171 					 decode_seek_maxsz)
172 #define NFS4_enc_layoutstats_sz		(compound_encode_hdr_maxsz + \
173 					 encode_sequence_maxsz + \
174 					 encode_putfh_maxsz + \
175 					 PNFS_LAYOUTSTATS_MAXDEV * encode_layoutstats_maxsz)
176 #define NFS4_dec_layoutstats_sz		(compound_decode_hdr_maxsz + \
177 					 decode_sequence_maxsz + \
178 					 decode_putfh_maxsz + \
179 					 PNFS_LAYOUTSTATS_MAXDEV * decode_layoutstats_maxsz)
180 #define NFS4_enc_layouterror_sz		(compound_encode_hdr_maxsz + \
181 					 encode_sequence_maxsz + \
182 					 encode_putfh_maxsz + \
183 					 NFS42_LAYOUTERROR_MAX * \
184 					 encode_layouterror_maxsz)
185 #define NFS4_dec_layouterror_sz		(compound_decode_hdr_maxsz + \
186 					 decode_sequence_maxsz + \
187 					 decode_putfh_maxsz + \
188 					 NFS42_LAYOUTERROR_MAX * \
189 					 decode_layouterror_maxsz)
190 #define NFS4_enc_clone_sz		(compound_encode_hdr_maxsz + \
191 					 encode_sequence_maxsz + \
192 					 encode_putfh_maxsz + \
193 					 encode_savefh_maxsz + \
194 					 encode_putfh_maxsz + \
195 					 encode_clone_maxsz + \
196 					 encode_getattr_maxsz)
197 #define NFS4_dec_clone_sz		(compound_decode_hdr_maxsz + \
198 					 decode_sequence_maxsz + \
199 					 decode_putfh_maxsz + \
200 					 decode_savefh_maxsz + \
201 					 decode_putfh_maxsz + \
202 					 decode_clone_maxsz + \
203 					 decode_getattr_maxsz)
204 #define NFS4_enc_getxattr_sz		(compound_encode_hdr_maxsz + \
205 					 encode_sequence_maxsz + \
206 					 encode_putfh_maxsz + \
207 					 encode_getxattr_maxsz)
208 #define NFS4_dec_getxattr_sz		(compound_decode_hdr_maxsz + \
209 					 decode_sequence_maxsz + \
210 					 decode_putfh_maxsz + \
211 					 decode_getxattr_maxsz)
212 #define NFS4_enc_setxattr_sz		(compound_encode_hdr_maxsz + \
213 					 encode_sequence_maxsz + \
214 					 encode_putfh_maxsz + \
215 					 encode_setxattr_maxsz + \
216 					 encode_getattr_maxsz)
217 #define NFS4_dec_setxattr_sz		(compound_decode_hdr_maxsz + \
218 					 decode_sequence_maxsz + \
219 					 decode_putfh_maxsz + \
220 					 decode_setxattr_maxsz + \
221 					 decode_getattr_maxsz)
222 #define NFS4_enc_listxattrs_sz		(compound_encode_hdr_maxsz + \
223 					 encode_sequence_maxsz + \
224 					 encode_putfh_maxsz + \
225 					 encode_listxattrs_maxsz)
226 #define NFS4_dec_listxattrs_sz		(compound_decode_hdr_maxsz + \
227 					 decode_sequence_maxsz + \
228 					 decode_putfh_maxsz + \
229 					 decode_listxattrs_maxsz)
230 #define NFS4_enc_removexattr_sz		(compound_encode_hdr_maxsz + \
231 					 encode_sequence_maxsz + \
232 					 encode_putfh_maxsz + \
233 					 encode_removexattr_maxsz)
234 #define NFS4_dec_removexattr_sz		(compound_decode_hdr_maxsz + \
235 					 decode_sequence_maxsz + \
236 					 decode_putfh_maxsz + \
237 					 decode_removexattr_maxsz)
238 
239 /*
240  * These values specify the maximum amount of data that is not
241  * associated with the extended attribute name or extended
242  * attribute list in the SETXATTR, GETXATTR and LISTXATTR
243  * respectively.
244  */
245 const u32 nfs42_maxsetxattr_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
246 					compound_encode_hdr_maxsz +
247 					encode_sequence_maxsz +
248 					encode_putfh_maxsz + 1 +
249 					nfs4_xattr_name_maxsz)
250 					* XDR_UNIT);
251 
252 const u32 nfs42_maxgetxattr_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
253 					compound_decode_hdr_maxsz +
254 					decode_sequence_maxsz +
255 					decode_putfh_maxsz + 1) * XDR_UNIT);
256 
257 const u32 nfs42_maxlistxattrs_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
258 					compound_decode_hdr_maxsz +
259 					decode_sequence_maxsz +
260 					decode_putfh_maxsz + 3) * XDR_UNIT);
261 
262 static void encode_fallocate(struct xdr_stream *xdr,
263 			     const struct nfs42_falloc_args *args)
264 {
265 	encode_nfs4_stateid(xdr, &args->falloc_stateid);
266 	encode_uint64(xdr, args->falloc_offset);
267 	encode_uint64(xdr, args->falloc_length);
268 }
269 
270 static void encode_allocate(struct xdr_stream *xdr,
271 			    const struct nfs42_falloc_args *args,
272 			    struct compound_hdr *hdr)
273 {
274 	encode_op_hdr(xdr, OP_ALLOCATE, decode_allocate_maxsz, hdr);
275 	encode_fallocate(xdr, args);
276 }
277 
278 static void encode_nl4_server(struct xdr_stream *xdr,
279 			      const struct nl4_server *ns)
280 {
281 	encode_uint32(xdr, ns->nl4_type);
282 	switch (ns->nl4_type) {
283 	case NL4_NAME:
284 	case NL4_URL:
285 		encode_string(xdr, ns->u.nl4_str_sz, ns->u.nl4_str);
286 		break;
287 	case NL4_NETADDR:
288 		encode_string(xdr, ns->u.nl4_addr.netid_len,
289 			      ns->u.nl4_addr.netid);
290 		encode_string(xdr, ns->u.nl4_addr.addr_len,
291 			      ns->u.nl4_addr.addr);
292 		break;
293 	default:
294 		WARN_ON_ONCE(1);
295 	}
296 }
297 
298 static void encode_copy(struct xdr_stream *xdr,
299 			const struct nfs42_copy_args *args,
300 			struct compound_hdr *hdr)
301 {
302 	encode_op_hdr(xdr, OP_COPY, decode_copy_maxsz, hdr);
303 	encode_nfs4_stateid(xdr, &args->src_stateid);
304 	encode_nfs4_stateid(xdr, &args->dst_stateid);
305 
306 	encode_uint64(xdr, args->src_pos);
307 	encode_uint64(xdr, args->dst_pos);
308 	encode_uint64(xdr, args->count);
309 
310 	encode_uint32(xdr, 1); /* consecutive = true */
311 	encode_uint32(xdr, args->sync);
312 	if (args->cp_src == NULL) { /* intra-ssc */
313 		encode_uint32(xdr, 0); /* no src server list */
314 		return;
315 	}
316 	encode_uint32(xdr, 1); /* supporting 1 server */
317 	encode_nl4_server(xdr, args->cp_src);
318 }
319 
320 static void encode_copy_commit(struct xdr_stream *xdr,
321 			  const struct nfs42_copy_args *args,
322 			  struct compound_hdr *hdr)
323 {
324 	__be32 *p;
325 
326 	encode_op_hdr(xdr, OP_COMMIT, decode_commit_maxsz, hdr);
327 	p = reserve_space(xdr, 12);
328 	p = xdr_encode_hyper(p, args->dst_pos);
329 	*p = cpu_to_be32(args->count);
330 }
331 
332 static void encode_offload_cancel(struct xdr_stream *xdr,
333 				  const struct nfs42_offload_status_args *args,
334 				  struct compound_hdr *hdr)
335 {
336 	encode_op_hdr(xdr, OP_OFFLOAD_CANCEL, decode_offload_cancel_maxsz, hdr);
337 	encode_nfs4_stateid(xdr, &args->osa_stateid);
338 }
339 
340 static void encode_copy_notify(struct xdr_stream *xdr,
341 			       const struct nfs42_copy_notify_args *args,
342 			       struct compound_hdr *hdr)
343 {
344 	encode_op_hdr(xdr, OP_COPY_NOTIFY, decode_copy_notify_maxsz, hdr);
345 	encode_nfs4_stateid(xdr, &args->cna_src_stateid);
346 	encode_nl4_server(xdr, &args->cna_dst);
347 }
348 
349 static void encode_deallocate(struct xdr_stream *xdr,
350 			      const struct nfs42_falloc_args *args,
351 			      struct compound_hdr *hdr)
352 {
353 	encode_op_hdr(xdr, OP_DEALLOCATE, decode_deallocate_maxsz, hdr);
354 	encode_fallocate(xdr, args);
355 }
356 
357 static void encode_read_plus(struct xdr_stream *xdr,
358 			     const struct nfs_pgio_args *args,
359 			     struct compound_hdr *hdr)
360 {
361 	encode_op_hdr(xdr, OP_READ_PLUS, decode_read_plus_maxsz, hdr);
362 	encode_nfs4_stateid(xdr, &args->stateid);
363 	encode_uint64(xdr, args->offset);
364 	encode_uint32(xdr, args->count);
365 }
366 
367 static void encode_seek(struct xdr_stream *xdr,
368 			const struct nfs42_seek_args *args,
369 			struct compound_hdr *hdr)
370 {
371 	encode_op_hdr(xdr, OP_SEEK, decode_seek_maxsz, hdr);
372 	encode_nfs4_stateid(xdr, &args->sa_stateid);
373 	encode_uint64(xdr, args->sa_offset);
374 	encode_uint32(xdr, args->sa_what);
375 }
376 
377 static void encode_layoutstats(struct xdr_stream *xdr,
378 			       const struct nfs42_layoutstat_args *args,
379 			       struct nfs42_layoutstat_devinfo *devinfo,
380 			       struct compound_hdr *hdr)
381 {
382 	__be32 *p;
383 
384 	encode_op_hdr(xdr, OP_LAYOUTSTATS, decode_layoutstats_maxsz, hdr);
385 	p = reserve_space(xdr, 8 + 8);
386 	p = xdr_encode_hyper(p, devinfo->offset);
387 	p = xdr_encode_hyper(p, devinfo->length);
388 	encode_nfs4_stateid(xdr, &args->stateid);
389 	p = reserve_space(xdr, 4*8 + NFS4_DEVICEID4_SIZE + 4);
390 	p = xdr_encode_hyper(p, devinfo->read_count);
391 	p = xdr_encode_hyper(p, devinfo->read_bytes);
392 	p = xdr_encode_hyper(p, devinfo->write_count);
393 	p = xdr_encode_hyper(p, devinfo->write_bytes);
394 	p = xdr_encode_opaque_fixed(p, devinfo->dev_id.data,
395 			NFS4_DEVICEID4_SIZE);
396 	/* Encode layoutupdate4 */
397 	*p++ = cpu_to_be32(devinfo->layout_type);
398 	if (devinfo->ld_private.ops)
399 		devinfo->ld_private.ops->encode(xdr, args,
400 				&devinfo->ld_private);
401 	else
402 		encode_uint32(xdr, 0);
403 }
404 
405 static void encode_clone(struct xdr_stream *xdr,
406 			 const struct nfs42_clone_args *args,
407 			 struct compound_hdr *hdr)
408 {
409 	__be32 *p;
410 
411 	encode_op_hdr(xdr, OP_CLONE, decode_clone_maxsz, hdr);
412 	encode_nfs4_stateid(xdr, &args->src_stateid);
413 	encode_nfs4_stateid(xdr, &args->dst_stateid);
414 	p = reserve_space(xdr, 3*8);
415 	p = xdr_encode_hyper(p, args->src_offset);
416 	p = xdr_encode_hyper(p, args->dst_offset);
417 	xdr_encode_hyper(p, args->count);
418 }
419 
420 static void encode_device_error(struct xdr_stream *xdr,
421 				const struct nfs42_device_error *error)
422 {
423 	__be32 *p;
424 
425 	p = reserve_space(xdr, NFS4_DEVICEID4_SIZE + 2*4);
426 	p = xdr_encode_opaque_fixed(p, error->dev_id.data,
427 			NFS4_DEVICEID4_SIZE);
428 	*p++ = cpu_to_be32(error->status);
429 	*p = cpu_to_be32(error->opnum);
430 }
431 
432 static void encode_layouterror(struct xdr_stream *xdr,
433 			       const struct nfs42_layout_error *args,
434 			       struct compound_hdr *hdr)
435 {
436 	__be32 *p;
437 
438 	encode_op_hdr(xdr, OP_LAYOUTERROR, decode_layouterror_maxsz, hdr);
439 	p = reserve_space(xdr, 8 + 8);
440 	p = xdr_encode_hyper(p, args->offset);
441 	p = xdr_encode_hyper(p, args->length);
442 	encode_nfs4_stateid(xdr, &args->stateid);
443 	p = reserve_space(xdr, 4);
444 	*p = cpu_to_be32(1);
445 	encode_device_error(xdr, &args->errors[0]);
446 }
447 
448 static void encode_setxattr(struct xdr_stream *xdr,
449 			    const struct nfs42_setxattrargs *arg,
450 			    struct compound_hdr *hdr)
451 {
452 	__be32 *p;
453 
454 	BUILD_BUG_ON(XATTR_CREATE != SETXATTR4_CREATE);
455 	BUILD_BUG_ON(XATTR_REPLACE != SETXATTR4_REPLACE);
456 
457 	encode_op_hdr(xdr, OP_SETXATTR, decode_setxattr_maxsz, hdr);
458 	p = reserve_space(xdr, 4);
459 	*p = cpu_to_be32(arg->xattr_flags);
460 	encode_string(xdr, strlen(arg->xattr_name), arg->xattr_name);
461 	p = reserve_space(xdr, 4);
462 	*p = cpu_to_be32(arg->xattr_len);
463 	if (arg->xattr_len)
464 		xdr_write_pages(xdr, arg->xattr_pages, 0, arg->xattr_len);
465 }
466 
467 static void encode_getxattr(struct xdr_stream *xdr, const char *name,
468 			    struct compound_hdr *hdr)
469 {
470 	encode_op_hdr(xdr, OP_GETXATTR, decode_getxattr_maxsz, hdr);
471 	encode_string(xdr, strlen(name), name);
472 }
473 
474 static void encode_removexattr(struct xdr_stream *xdr, const char *name,
475 			       struct compound_hdr *hdr)
476 {
477 	encode_op_hdr(xdr, OP_REMOVEXATTR, decode_removexattr_maxsz, hdr);
478 	encode_string(xdr, strlen(name), name);
479 }
480 
481 static void encode_listxattrs(struct xdr_stream *xdr,
482 			     const struct nfs42_listxattrsargs *arg,
483 			     struct compound_hdr *hdr)
484 {
485 	__be32 *p;
486 
487 	encode_op_hdr(xdr, OP_LISTXATTRS, decode_listxattrs_maxsz, hdr);
488 
489 	p = reserve_space(xdr, 12);
490 	if (unlikely(!p))
491 		return;
492 
493 	p = xdr_encode_hyper(p, arg->cookie);
494 	/*
495 	 * RFC 8276 says to specify the full max length of the LISTXATTRS
496 	 * XDR reply. Count is set to the XDR length of the names array
497 	 * plus the EOF marker. So, add the cookie and the names count.
498 	 */
499 	*p = cpu_to_be32(arg->count + 8 + 4);
500 }
501 
502 /*
503  * Encode ALLOCATE request
504  */
505 static void nfs4_xdr_enc_allocate(struct rpc_rqst *req,
506 				  struct xdr_stream *xdr,
507 				  const void *data)
508 {
509 	const struct nfs42_falloc_args *args = data;
510 	struct compound_hdr hdr = {
511 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
512 	};
513 
514 	encode_compound_hdr(xdr, req, &hdr);
515 	encode_sequence(xdr, &args->seq_args, &hdr);
516 	encode_putfh(xdr, args->falloc_fh, &hdr);
517 	encode_allocate(xdr, args, &hdr);
518 	encode_getfattr(xdr, args->falloc_bitmask, &hdr);
519 	encode_nops(&hdr);
520 }
521 
522 /*
523  * Encode COPY request
524  */
525 static void nfs4_xdr_enc_copy(struct rpc_rqst *req,
526 			      struct xdr_stream *xdr,
527 			      const void *data)
528 {
529 	const struct nfs42_copy_args *args = data;
530 	struct compound_hdr hdr = {
531 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
532 	};
533 
534 	encode_compound_hdr(xdr, req, &hdr);
535 	encode_sequence(xdr, &args->seq_args, &hdr);
536 	encode_putfh(xdr, args->src_fh, &hdr);
537 	encode_savefh(xdr, &hdr);
538 	encode_putfh(xdr, args->dst_fh, &hdr);
539 	encode_copy(xdr, args, &hdr);
540 	if (args->sync)
541 		encode_copy_commit(xdr, args, &hdr);
542 	encode_nops(&hdr);
543 }
544 
545 /*
546  * Encode OFFLOAD_CANEL request
547  */
548 static void nfs4_xdr_enc_offload_cancel(struct rpc_rqst *req,
549 					struct xdr_stream *xdr,
550 					const void *data)
551 {
552 	const struct nfs42_offload_status_args *args = data;
553 	struct compound_hdr hdr = {
554 		.minorversion = nfs4_xdr_minorversion(&args->osa_seq_args),
555 	};
556 
557 	encode_compound_hdr(xdr, req, &hdr);
558 	encode_sequence(xdr, &args->osa_seq_args, &hdr);
559 	encode_putfh(xdr, args->osa_src_fh, &hdr);
560 	encode_offload_cancel(xdr, args, &hdr);
561 	encode_nops(&hdr);
562 }
563 
564 /*
565  * Encode COPY_NOTIFY request
566  */
567 static void nfs4_xdr_enc_copy_notify(struct rpc_rqst *req,
568 				     struct xdr_stream *xdr,
569 				     const void *data)
570 {
571 	const struct nfs42_copy_notify_args *args = data;
572 	struct compound_hdr hdr = {
573 		.minorversion = nfs4_xdr_minorversion(&args->cna_seq_args),
574 	};
575 
576 	encode_compound_hdr(xdr, req, &hdr);
577 	encode_sequence(xdr, &args->cna_seq_args, &hdr);
578 	encode_putfh(xdr, args->cna_src_fh, &hdr);
579 	encode_copy_notify(xdr, args, &hdr);
580 	encode_nops(&hdr);
581 }
582 
583 /*
584  * Encode DEALLOCATE request
585  */
586 static void nfs4_xdr_enc_deallocate(struct rpc_rqst *req,
587 				    struct xdr_stream *xdr,
588 				    const void *data)
589 {
590 	const struct nfs42_falloc_args *args = data;
591 	struct compound_hdr hdr = {
592 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
593 	};
594 
595 	encode_compound_hdr(xdr, req, &hdr);
596 	encode_sequence(xdr, &args->seq_args, &hdr);
597 	encode_putfh(xdr, args->falloc_fh, &hdr);
598 	encode_deallocate(xdr, args, &hdr);
599 	encode_getfattr(xdr, args->falloc_bitmask, &hdr);
600 	encode_nops(&hdr);
601 }
602 
603 /*
604  * Encode READ_PLUS request
605  */
606 static void nfs4_xdr_enc_read_plus(struct rpc_rqst *req,
607 				   struct xdr_stream *xdr,
608 				   const void *data)
609 {
610 	const struct nfs_pgio_args *args = data;
611 	struct compound_hdr hdr = {
612 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
613 	};
614 
615 	encode_compound_hdr(xdr, req, &hdr);
616 	encode_sequence(xdr, &args->seq_args, &hdr);
617 	encode_putfh(xdr, args->fh, &hdr);
618 	encode_read_plus(xdr, args, &hdr);
619 
620 	rpc_prepare_reply_pages(req, args->pages, args->pgbase,
621 				args->count, hdr.replen);
622 	encode_nops(&hdr);
623 }
624 
625 /*
626  * Encode SEEK request
627  */
628 static void nfs4_xdr_enc_seek(struct rpc_rqst *req,
629 			      struct xdr_stream *xdr,
630 			      const void *data)
631 {
632 	const struct nfs42_seek_args *args = data;
633 	struct compound_hdr hdr = {
634 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
635 	};
636 
637 	encode_compound_hdr(xdr, req, &hdr);
638 	encode_sequence(xdr, &args->seq_args, &hdr);
639 	encode_putfh(xdr, args->sa_fh, &hdr);
640 	encode_seek(xdr, args, &hdr);
641 	encode_nops(&hdr);
642 }
643 
644 /*
645  * Encode LAYOUTSTATS request
646  */
647 static void nfs4_xdr_enc_layoutstats(struct rpc_rqst *req,
648 				     struct xdr_stream *xdr,
649 				     const void *data)
650 {
651 	const struct nfs42_layoutstat_args *args = data;
652 	int i;
653 
654 	struct compound_hdr hdr = {
655 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
656 	};
657 
658 	encode_compound_hdr(xdr, req, &hdr);
659 	encode_sequence(xdr, &args->seq_args, &hdr);
660 	encode_putfh(xdr, args->fh, &hdr);
661 	WARN_ON(args->num_dev > PNFS_LAYOUTSTATS_MAXDEV);
662 	for (i = 0; i < args->num_dev; i++)
663 		encode_layoutstats(xdr, args, &args->devinfo[i], &hdr);
664 	encode_nops(&hdr);
665 }
666 
667 /*
668  * Encode CLONE request
669  */
670 static void nfs4_xdr_enc_clone(struct rpc_rqst *req,
671 			       struct xdr_stream *xdr,
672 			       const void *data)
673 {
674 	const struct nfs42_clone_args *args = data;
675 	struct compound_hdr hdr = {
676 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
677 	};
678 
679 	encode_compound_hdr(xdr, req, &hdr);
680 	encode_sequence(xdr, &args->seq_args, &hdr);
681 	encode_putfh(xdr, args->src_fh, &hdr);
682 	encode_savefh(xdr, &hdr);
683 	encode_putfh(xdr, args->dst_fh, &hdr);
684 	encode_clone(xdr, args, &hdr);
685 	encode_getfattr(xdr, args->dst_bitmask, &hdr);
686 	encode_nops(&hdr);
687 }
688 
689 /*
690  * Encode LAYOUTERROR request
691  */
692 static void nfs4_xdr_enc_layouterror(struct rpc_rqst *req,
693 				     struct xdr_stream *xdr,
694 				     const void *data)
695 {
696 	const struct nfs42_layouterror_args *args = data;
697 	struct compound_hdr hdr = {
698 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
699 	};
700 	int i;
701 
702 	encode_compound_hdr(xdr, req, &hdr);
703 	encode_sequence(xdr, &args->seq_args, &hdr);
704 	encode_putfh(xdr, NFS_FH(args->inode), &hdr);
705 	for (i = 0; i < args->num_errors; i++)
706 		encode_layouterror(xdr, &args->errors[i], &hdr);
707 	encode_nops(&hdr);
708 }
709 
710 /*
711  * Encode SETXATTR request
712  */
713 static void nfs4_xdr_enc_setxattr(struct rpc_rqst *req, struct xdr_stream *xdr,
714 				  const void *data)
715 {
716 	const struct nfs42_setxattrargs *args = data;
717 	struct compound_hdr hdr = {
718 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
719 	};
720 
721 	encode_compound_hdr(xdr, req, &hdr);
722 	encode_sequence(xdr, &args->seq_args, &hdr);
723 	encode_putfh(xdr, args->fh, &hdr);
724 	encode_setxattr(xdr, args, &hdr);
725 	encode_getfattr(xdr, args->bitmask, &hdr);
726 	encode_nops(&hdr);
727 }
728 
729 /*
730  * Encode GETXATTR request
731  */
732 static void nfs4_xdr_enc_getxattr(struct rpc_rqst *req, struct xdr_stream *xdr,
733 				  const void *data)
734 {
735 	const struct nfs42_getxattrargs *args = data;
736 	struct compound_hdr hdr = {
737 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
738 	};
739 	uint32_t replen;
740 
741 	encode_compound_hdr(xdr, req, &hdr);
742 	encode_sequence(xdr, &args->seq_args, &hdr);
743 	encode_putfh(xdr, args->fh, &hdr);
744 	replen = hdr.replen + op_decode_hdr_maxsz + 1;
745 	encode_getxattr(xdr, args->xattr_name, &hdr);
746 
747 	rpc_prepare_reply_pages(req, args->xattr_pages, 0, args->xattr_len,
748 				replen);
749 
750 	encode_nops(&hdr);
751 }
752 
753 /*
754  * Encode LISTXATTR request
755  */
756 static void nfs4_xdr_enc_listxattrs(struct rpc_rqst *req,
757 				    struct xdr_stream *xdr, const void *data)
758 {
759 	const struct nfs42_listxattrsargs *args = data;
760 	struct compound_hdr hdr = {
761 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
762 	};
763 	uint32_t replen;
764 
765 	encode_compound_hdr(xdr, req, &hdr);
766 	encode_sequence(xdr, &args->seq_args, &hdr);
767 	encode_putfh(xdr, args->fh, &hdr);
768 	replen = hdr.replen + op_decode_hdr_maxsz + 2 + 1;
769 	encode_listxattrs(xdr, args, &hdr);
770 
771 	rpc_prepare_reply_pages(req, args->xattr_pages, 0, args->count, replen);
772 
773 	encode_nops(&hdr);
774 }
775 
776 /*
777  * Encode REMOVEXATTR request
778  */
779 static void nfs4_xdr_enc_removexattr(struct rpc_rqst *req,
780 				     struct xdr_stream *xdr, const void *data)
781 {
782 	const struct nfs42_removexattrargs *args = data;
783 	struct compound_hdr hdr = {
784 		.minorversion = nfs4_xdr_minorversion(&args->seq_args),
785 	};
786 
787 	encode_compound_hdr(xdr, req, &hdr);
788 	encode_sequence(xdr, &args->seq_args, &hdr);
789 	encode_putfh(xdr, args->fh, &hdr);
790 	encode_removexattr(xdr, args->xattr_name, &hdr);
791 	encode_nops(&hdr);
792 }
793 
794 static int decode_allocate(struct xdr_stream *xdr, struct nfs42_falloc_res *res)
795 {
796 	return decode_op_hdr(xdr, OP_ALLOCATE);
797 }
798 
799 static int decode_write_response(struct xdr_stream *xdr,
800 				 struct nfs42_write_res *res)
801 {
802 	__be32 *p;
803 	int status, count;
804 
805 	p = xdr_inline_decode(xdr, 4);
806 	if (unlikely(!p))
807 		return -EIO;
808 	count = be32_to_cpup(p);
809 	if (count > 1)
810 		return -EREMOTEIO;
811 	else if (count == 1) {
812 		status = decode_opaque_fixed(xdr, &res->stateid,
813 				NFS4_STATEID_SIZE);
814 		if (unlikely(status))
815 			return -EIO;
816 	}
817 	p = xdr_inline_decode(xdr, 8 + 4);
818 	if (unlikely(!p))
819 		return -EIO;
820 	p = xdr_decode_hyper(p, &res->count);
821 	res->verifier.committed = be32_to_cpup(p);
822 	return decode_verifier(xdr, &res->verifier.verifier);
823 }
824 
825 static int decode_nl4_server(struct xdr_stream *xdr, struct nl4_server *ns)
826 {
827 	struct nfs42_netaddr *naddr;
828 	uint32_t dummy;
829 	char *dummy_str;
830 	__be32 *p;
831 	int status;
832 
833 	/* nl_type */
834 	p = xdr_inline_decode(xdr, 4);
835 	if (unlikely(!p))
836 		return -EIO;
837 	ns->nl4_type = be32_to_cpup(p);
838 	switch (ns->nl4_type) {
839 	case NL4_NAME:
840 	case NL4_URL:
841 		status = decode_opaque_inline(xdr, &dummy, &dummy_str);
842 		if (unlikely(status))
843 			return status;
844 		if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
845 			return -EIO;
846 		memcpy(&ns->u.nl4_str, dummy_str, dummy);
847 		ns->u.nl4_str_sz = dummy;
848 		break;
849 	case NL4_NETADDR:
850 		naddr = &ns->u.nl4_addr;
851 
852 		/* netid string */
853 		status = decode_opaque_inline(xdr, &dummy, &dummy_str);
854 		if (unlikely(status))
855 			return status;
856 		if (unlikely(dummy > RPCBIND_MAXNETIDLEN))
857 			return -EIO;
858 		naddr->netid_len = dummy;
859 		memcpy(naddr->netid, dummy_str, naddr->netid_len);
860 
861 		/* uaddr string */
862 		status = decode_opaque_inline(xdr, &dummy, &dummy_str);
863 		if (unlikely(status))
864 			return status;
865 		if (unlikely(dummy > RPCBIND_MAXUADDRLEN))
866 			return -EIO;
867 		naddr->addr_len = dummy;
868 		memcpy(naddr->addr, dummy_str, naddr->addr_len);
869 		break;
870 	default:
871 		WARN_ON_ONCE(1);
872 		return -EIO;
873 	}
874 	return 0;
875 }
876 
877 static int decode_copy_requirements(struct xdr_stream *xdr,
878 				    struct nfs42_copy_res *res) {
879 	__be32 *p;
880 
881 	p = xdr_inline_decode(xdr, 4 + 4);
882 	if (unlikely(!p))
883 		return -EIO;
884 
885 	res->consecutive = be32_to_cpup(p++);
886 	res->synchronous = be32_to_cpup(p++);
887 	return 0;
888 }
889 
890 static int decode_copy(struct xdr_stream *xdr, struct nfs42_copy_res *res)
891 {
892 	int status;
893 
894 	status = decode_op_hdr(xdr, OP_COPY);
895 	if (status == NFS4ERR_OFFLOAD_NO_REQS) {
896 		status = decode_copy_requirements(xdr, res);
897 		if (status)
898 			return status;
899 		return NFS4ERR_OFFLOAD_NO_REQS;
900 	} else if (status)
901 		return status;
902 
903 	status = decode_write_response(xdr, &res->write_res);
904 	if (status)
905 		return status;
906 
907 	return decode_copy_requirements(xdr, res);
908 }
909 
910 static int decode_offload_cancel(struct xdr_stream *xdr,
911 				 struct nfs42_offload_status_res *res)
912 {
913 	return decode_op_hdr(xdr, OP_OFFLOAD_CANCEL);
914 }
915 
916 static int decode_copy_notify(struct xdr_stream *xdr,
917 			      struct nfs42_copy_notify_res *res)
918 {
919 	__be32 *p;
920 	int status, count;
921 
922 	status = decode_op_hdr(xdr, OP_COPY_NOTIFY);
923 	if (status)
924 		return status;
925 	/* cnr_lease_time */
926 	p = xdr_inline_decode(xdr, 12);
927 	if (unlikely(!p))
928 		return -EIO;
929 	p = xdr_decode_hyper(p, &res->cnr_lease_time.seconds);
930 	res->cnr_lease_time.nseconds = be32_to_cpup(p);
931 
932 	status = decode_opaque_fixed(xdr, &res->cnr_stateid, NFS4_STATEID_SIZE);
933 	if (unlikely(status))
934 		return -EIO;
935 
936 	/* number of source addresses */
937 	p = xdr_inline_decode(xdr, 4);
938 	if (unlikely(!p))
939 		return -EIO;
940 
941 	count = be32_to_cpup(p);
942 	if (count > 1)
943 		pr_warn("NFS: %s: nsvr %d > Supported. Use first servers\n",
944 			 __func__, count);
945 
946 	status = decode_nl4_server(xdr, &res->cnr_src);
947 	if (unlikely(status))
948 		return -EIO;
949 	return 0;
950 }
951 
952 static int decode_deallocate(struct xdr_stream *xdr, struct nfs42_falloc_res *res)
953 {
954 	return decode_op_hdr(xdr, OP_DEALLOCATE);
955 }
956 
957 struct read_plus_segment {
958 	enum data_content4 type;
959 	uint64_t offset;
960 	union {
961 		struct {
962 			uint64_t length;
963 		} hole;
964 
965 		struct {
966 			uint32_t length;
967 			unsigned int from;
968 		} data;
969 	};
970 };
971 
972 static inline uint64_t read_plus_segment_length(struct read_plus_segment *seg)
973 {
974 	return seg->type == NFS4_CONTENT_DATA ? seg->data.length : seg->hole.length;
975 }
976 
977 static int decode_read_plus_segment(struct xdr_stream *xdr,
978 				    struct read_plus_segment *seg)
979 {
980 	__be32 *p;
981 
982 	p = xdr_inline_decode(xdr, 4);
983 	if (!p)
984 		return -EIO;
985 	seg->type = be32_to_cpup(p++);
986 
987 	p = xdr_inline_decode(xdr, seg->type == NFS4_CONTENT_DATA ? 12 : 16);
988 	if (!p)
989 		return -EIO;
990 	p = xdr_decode_hyper(p, &seg->offset);
991 
992 	if (seg->type == NFS4_CONTENT_DATA) {
993 		struct xdr_buf buf;
994 		uint32_t len = be32_to_cpup(p);
995 
996 		seg->data.length = len;
997 		seg->data.from = xdr_stream_pos(xdr);
998 
999 		if (!xdr_stream_subsegment(xdr, &buf, xdr_align_size(len)))
1000 			return -EIO;
1001 	} else if (seg->type == NFS4_CONTENT_HOLE) {
1002 		xdr_decode_hyper(p, &seg->hole.length);
1003 	} else
1004 		return -EINVAL;
1005 	return 0;
1006 }
1007 
1008 static int process_read_plus_segment(struct xdr_stream *xdr,
1009 				     struct nfs_pgio_args *args,
1010 				     struct nfs_pgio_res *res,
1011 				     struct read_plus_segment *seg)
1012 {
1013 	unsigned long offset = seg->offset;
1014 	unsigned long length = read_plus_segment_length(seg);
1015 	unsigned int bufpos;
1016 
1017 	if (offset + length < args->offset)
1018 		return 0;
1019 	else if (offset > args->offset + args->count) {
1020 		res->eof = 0;
1021 		return 0;
1022 	} else if (offset < args->offset) {
1023 		length -= (args->offset - offset);
1024 		offset = args->offset;
1025 	} else if (offset + length > args->offset + args->count) {
1026 		length = (args->offset + args->count) - offset;
1027 		res->eof = 0;
1028 	}
1029 
1030 	bufpos = xdr->buf->head[0].iov_len + (offset - args->offset);
1031 	if (seg->type == NFS4_CONTENT_HOLE)
1032 		return xdr_stream_zero(xdr, bufpos, length);
1033 	else
1034 		return xdr_stream_move_subsegment(xdr, seg->data.from, bufpos, length);
1035 }
1036 
1037 static int decode_read_plus(struct xdr_stream *xdr, struct nfs_pgio_res *res)
1038 {
1039 	struct nfs_pgio_header *hdr =
1040 		container_of(res, struct nfs_pgio_header, res);
1041 	struct nfs_pgio_args *args = &hdr->args;
1042 	uint32_t segments;
1043 	struct read_plus_segment *segs;
1044 	int status, i;
1045 	__be32 *p;
1046 
1047 	status = decode_op_hdr(xdr, OP_READ_PLUS);
1048 	if (status)
1049 		return status;
1050 
1051 	p = xdr_inline_decode(xdr, 4 + 4);
1052 	if (unlikely(!p))
1053 		return -EIO;
1054 
1055 	res->count = 0;
1056 	res->eof = be32_to_cpup(p++);
1057 	segments = be32_to_cpup(p++);
1058 	if (segments == 0)
1059 		return 0;
1060 
1061 	segs = kmalloc_array(segments, sizeof(*segs), GFP_KERNEL);
1062 	if (!segs)
1063 		return -ENOMEM;
1064 
1065 	for (i = 0; i < segments; i++) {
1066 		status = decode_read_plus_segment(xdr, &segs[i]);
1067 		if (status < 0)
1068 			goto out;
1069 	}
1070 
1071 	xdr_set_pagelen(xdr, xdr_align_size(args->count));
1072 	for (i = segments; i > 0; i--)
1073 		res->count += process_read_plus_segment(xdr, args, res, &segs[i-1]);
1074 	status = 0;
1075 
1076 out:
1077 	kfree(segs);
1078 	return status;
1079 }
1080 
1081 static int decode_seek(struct xdr_stream *xdr, struct nfs42_seek_res *res)
1082 {
1083 	int status;
1084 	__be32 *p;
1085 
1086 	status = decode_op_hdr(xdr, OP_SEEK);
1087 	if (status)
1088 		return status;
1089 
1090 	p = xdr_inline_decode(xdr, 4 + 8);
1091 	if (unlikely(!p))
1092 		return -EIO;
1093 
1094 	res->sr_eof = be32_to_cpup(p++);
1095 	p = xdr_decode_hyper(p, &res->sr_offset);
1096 	return 0;
1097 }
1098 
1099 static int decode_layoutstats(struct xdr_stream *xdr)
1100 {
1101 	return decode_op_hdr(xdr, OP_LAYOUTSTATS);
1102 }
1103 
1104 static int decode_clone(struct xdr_stream *xdr)
1105 {
1106 	return decode_op_hdr(xdr, OP_CLONE);
1107 }
1108 
1109 static int decode_layouterror(struct xdr_stream *xdr)
1110 {
1111 	return decode_op_hdr(xdr, OP_LAYOUTERROR);
1112 }
1113 
1114 static int decode_setxattr(struct xdr_stream *xdr,
1115 			   struct nfs4_change_info *cinfo)
1116 {
1117 	int status;
1118 
1119 	status = decode_op_hdr(xdr, OP_SETXATTR);
1120 	if (status)
1121 		goto out;
1122 	status = decode_change_info(xdr, cinfo);
1123 out:
1124 	return status;
1125 }
1126 
1127 static int decode_getxattr(struct xdr_stream *xdr,
1128 			   struct nfs42_getxattrres *res,
1129 			   struct rpc_rqst *req)
1130 {
1131 	int status;
1132 	__be32 *p;
1133 	u32 len, rdlen;
1134 
1135 	status = decode_op_hdr(xdr, OP_GETXATTR);
1136 	if (status)
1137 		return status;
1138 
1139 	p = xdr_inline_decode(xdr, 4);
1140 	if (unlikely(!p))
1141 		return -EIO;
1142 
1143 	len = be32_to_cpup(p);
1144 
1145 	/*
1146 	 * Only check against the page length here. The actual
1147 	 * requested length may be smaller, but that is only
1148 	 * checked against after possibly caching a valid reply.
1149 	 */
1150 	if (len > req->rq_rcv_buf.page_len)
1151 		return -ERANGE;
1152 
1153 	res->xattr_len = len;
1154 
1155 	if (len > 0) {
1156 		rdlen = xdr_read_pages(xdr, len);
1157 		if (rdlen < len)
1158 			return -EIO;
1159 	}
1160 
1161 	return 0;
1162 }
1163 
1164 static int decode_removexattr(struct xdr_stream *xdr,
1165 			   struct nfs4_change_info *cinfo)
1166 {
1167 	int status;
1168 
1169 	status = decode_op_hdr(xdr, OP_REMOVEXATTR);
1170 	if (status)
1171 		goto out;
1172 
1173 	status = decode_change_info(xdr, cinfo);
1174 out:
1175 	return status;
1176 }
1177 
1178 static int decode_listxattrs(struct xdr_stream *xdr,
1179 			    struct nfs42_listxattrsres *res)
1180 {
1181 	int status;
1182 	__be32 *p;
1183 	u32 count, len, ulen;
1184 	size_t left, copied;
1185 	char *buf;
1186 
1187 	status = decode_op_hdr(xdr, OP_LISTXATTRS);
1188 	if (status) {
1189 		/*
1190 		 * Special case: for LISTXATTRS, NFS4ERR_TOOSMALL
1191 		 * should be translated to ERANGE.
1192 		 */
1193 		if (status == -ETOOSMALL)
1194 			status = -ERANGE;
1195 		/*
1196 		 * Special case: for LISTXATTRS, NFS4ERR_NOXATTR
1197 		 * should be translated to success with zero-length reply.
1198 		 */
1199 		if (status == -ENODATA) {
1200 			res->eof = true;
1201 			status = 0;
1202 		}
1203 		goto out;
1204 	}
1205 
1206 	p = xdr_inline_decode(xdr, 8);
1207 	if (unlikely(!p))
1208 		return -EIO;
1209 
1210 	xdr_decode_hyper(p, &res->cookie);
1211 
1212 	p = xdr_inline_decode(xdr, 4);
1213 	if (unlikely(!p))
1214 		return -EIO;
1215 
1216 	left = res->xattr_len;
1217 	buf = res->xattr_buf;
1218 
1219 	count = be32_to_cpup(p);
1220 	copied = 0;
1221 
1222 	/*
1223 	 * We have asked for enough room to encode the maximum number
1224 	 * of possible attribute names, so everything should fit.
1225 	 *
1226 	 * But, don't rely on that assumption. Just decode entries
1227 	 * until they don't fit anymore, just in case the server did
1228 	 * something odd.
1229 	 */
1230 	while (count--) {
1231 		p = xdr_inline_decode(xdr, 4);
1232 		if (unlikely(!p))
1233 			return -EIO;
1234 
1235 		len = be32_to_cpup(p);
1236 		if (len > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
1237 			status = -ERANGE;
1238 			goto out;
1239 		}
1240 
1241 		p = xdr_inline_decode(xdr, len);
1242 		if (unlikely(!p))
1243 			return -EIO;
1244 
1245 		ulen = len + XATTR_USER_PREFIX_LEN + 1;
1246 		if (buf) {
1247 			if (ulen > left) {
1248 				status = -ERANGE;
1249 				goto out;
1250 			}
1251 
1252 			memcpy(buf, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
1253 			memcpy(buf + XATTR_USER_PREFIX_LEN, p, len);
1254 
1255 			buf[ulen - 1] = 0;
1256 			buf += ulen;
1257 			left -= ulen;
1258 		}
1259 		copied += ulen;
1260 	}
1261 
1262 	p = xdr_inline_decode(xdr, 4);
1263 	if (unlikely(!p))
1264 		return -EIO;
1265 
1266 	res->eof = be32_to_cpup(p);
1267 	res->copied = copied;
1268 
1269 out:
1270 	if (status == -ERANGE && res->xattr_len == XATTR_LIST_MAX)
1271 		status = -E2BIG;
1272 
1273 	return status;
1274 }
1275 
1276 /*
1277  * Decode ALLOCATE request
1278  */
1279 static int nfs4_xdr_dec_allocate(struct rpc_rqst *rqstp,
1280 				 struct xdr_stream *xdr,
1281 				 void *data)
1282 {
1283 	struct nfs42_falloc_res *res = data;
1284 	struct compound_hdr hdr;
1285 	int status;
1286 
1287 	status = decode_compound_hdr(xdr, &hdr);
1288 	if (status)
1289 		goto out;
1290 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1291 	if (status)
1292 		goto out;
1293 	status = decode_putfh(xdr);
1294 	if (status)
1295 		goto out;
1296 	status = decode_allocate(xdr, res);
1297 	if (status)
1298 		goto out;
1299 	decode_getfattr(xdr, res->falloc_fattr, res->falloc_server);
1300 out:
1301 	return status;
1302 }
1303 
1304 /*
1305  * Decode COPY response
1306  */
1307 static int nfs4_xdr_dec_copy(struct rpc_rqst *rqstp,
1308 			     struct xdr_stream *xdr,
1309 			     void *data)
1310 {
1311 	struct nfs42_copy_res *res = data;
1312 	struct compound_hdr hdr;
1313 	int status;
1314 
1315 	status = decode_compound_hdr(xdr, &hdr);
1316 	if (status)
1317 		goto out;
1318 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1319 	if (status)
1320 		goto out;
1321 	status = decode_putfh(xdr);
1322 	if (status)
1323 		goto out;
1324 	status = decode_savefh(xdr);
1325 	if (status)
1326 		goto out;
1327 	status = decode_putfh(xdr);
1328 	if (status)
1329 		goto out;
1330 	status = decode_copy(xdr, res);
1331 	if (status)
1332 		goto out;
1333 	if (res->commit_res.verf)
1334 		status = decode_commit(xdr, &res->commit_res);
1335 out:
1336 	return status;
1337 }
1338 
1339 /*
1340  * Decode OFFLOAD_CANCEL response
1341  */
1342 static int nfs4_xdr_dec_offload_cancel(struct rpc_rqst *rqstp,
1343 				       struct xdr_stream *xdr,
1344 				       void *data)
1345 {
1346 	struct nfs42_offload_status_res *res = data;
1347 	struct compound_hdr hdr;
1348 	int status;
1349 
1350 	status = decode_compound_hdr(xdr, &hdr);
1351 	if (status)
1352 		goto out;
1353 	status = decode_sequence(xdr, &res->osr_seq_res, rqstp);
1354 	if (status)
1355 		goto out;
1356 	status = decode_putfh(xdr);
1357 	if (status)
1358 		goto out;
1359 	status = decode_offload_cancel(xdr, res);
1360 
1361 out:
1362 	return status;
1363 }
1364 
1365 /*
1366  * Decode COPY_NOTIFY response
1367  */
1368 static int nfs4_xdr_dec_copy_notify(struct rpc_rqst *rqstp,
1369 				    struct xdr_stream *xdr,
1370 				    void *data)
1371 {
1372 	struct nfs42_copy_notify_res *res = data;
1373 	struct compound_hdr hdr;
1374 	int status;
1375 
1376 	status = decode_compound_hdr(xdr, &hdr);
1377 	if (status)
1378 		goto out;
1379 	status = decode_sequence(xdr, &res->cnr_seq_res, rqstp);
1380 	if (status)
1381 		goto out;
1382 	status = decode_putfh(xdr);
1383 	if (status)
1384 		goto out;
1385 	status = decode_copy_notify(xdr, res);
1386 
1387 out:
1388 	return status;
1389 }
1390 
1391 /*
1392  * Decode DEALLOCATE request
1393  */
1394 static int nfs4_xdr_dec_deallocate(struct rpc_rqst *rqstp,
1395 				   struct xdr_stream *xdr,
1396 				   void *data)
1397 {
1398 	struct nfs42_falloc_res *res = data;
1399 	struct compound_hdr hdr;
1400 	int status;
1401 
1402 	status = decode_compound_hdr(xdr, &hdr);
1403 	if (status)
1404 		goto out;
1405 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1406 	if (status)
1407 		goto out;
1408 	status = decode_putfh(xdr);
1409 	if (status)
1410 		goto out;
1411 	status = decode_deallocate(xdr, res);
1412 	if (status)
1413 		goto out;
1414 	decode_getfattr(xdr, res->falloc_fattr, res->falloc_server);
1415 out:
1416 	return status;
1417 }
1418 
1419 /*
1420  * Decode READ_PLUS request
1421  */
1422 static int nfs4_xdr_dec_read_plus(struct rpc_rqst *rqstp,
1423 				  struct xdr_stream *xdr,
1424 				  void *data)
1425 {
1426 	struct nfs_pgio_res *res = data;
1427 	struct compound_hdr hdr;
1428 	int status;
1429 
1430 	xdr_set_scratch_buffer(xdr, res->scratch, sizeof(res->scratch));
1431 
1432 	status = decode_compound_hdr(xdr, &hdr);
1433 	if (status)
1434 		goto out;
1435 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1436 	if (status)
1437 		goto out;
1438 	status = decode_putfh(xdr);
1439 	if (status)
1440 		goto out;
1441 	status = decode_read_plus(xdr, res);
1442 	if (!status)
1443 		status = res->count;
1444 out:
1445 	return status;
1446 }
1447 
1448 /*
1449  * Decode SEEK request
1450  */
1451 static int nfs4_xdr_dec_seek(struct rpc_rqst *rqstp,
1452 			     struct xdr_stream *xdr,
1453 			     void *data)
1454 {
1455 	struct nfs42_seek_res *res = data;
1456 	struct compound_hdr hdr;
1457 	int status;
1458 
1459 	status = decode_compound_hdr(xdr, &hdr);
1460 	if (status)
1461 		goto out;
1462 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1463 	if (status)
1464 		goto out;
1465 	status = decode_putfh(xdr);
1466 	if (status)
1467 		goto out;
1468 	status = decode_seek(xdr, res);
1469 out:
1470 	return status;
1471 }
1472 
1473 /*
1474  * Decode LAYOUTSTATS request
1475  */
1476 static int nfs4_xdr_dec_layoutstats(struct rpc_rqst *rqstp,
1477 				    struct xdr_stream *xdr,
1478 				    void *data)
1479 {
1480 	struct nfs42_layoutstat_res *res = data;
1481 	struct compound_hdr hdr;
1482 	int status, i;
1483 
1484 	status = decode_compound_hdr(xdr, &hdr);
1485 	if (status)
1486 		goto out;
1487 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1488 	if (status)
1489 		goto out;
1490 	status = decode_putfh(xdr);
1491 	if (status)
1492 		goto out;
1493 	WARN_ON(res->num_dev > PNFS_LAYOUTSTATS_MAXDEV);
1494 	for (i = 0; i < res->num_dev; i++) {
1495 		status = decode_layoutstats(xdr);
1496 		if (status)
1497 			goto out;
1498 	}
1499 out:
1500 	res->rpc_status = status;
1501 	return status;
1502 }
1503 
1504 /*
1505  * Decode CLONE request
1506  */
1507 static int nfs4_xdr_dec_clone(struct rpc_rqst *rqstp,
1508 			      struct xdr_stream *xdr,
1509 			      void *data)
1510 {
1511 	struct nfs42_clone_res *res = data;
1512 	struct compound_hdr hdr;
1513 	int status;
1514 
1515 	status = decode_compound_hdr(xdr, &hdr);
1516 	if (status)
1517 		goto out;
1518 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1519 	if (status)
1520 		goto out;
1521 	status = decode_putfh(xdr);
1522 	if (status)
1523 		goto out;
1524 	status = decode_savefh(xdr);
1525 	if (status)
1526 		goto out;
1527 	status = decode_putfh(xdr);
1528 	if (status)
1529 		goto out;
1530 	status = decode_clone(xdr);
1531 	if (status)
1532 		goto out;
1533 	decode_getfattr(xdr, res->dst_fattr, res->server);
1534 out:
1535 	res->rpc_status = status;
1536 	return status;
1537 }
1538 
1539 /*
1540  * Decode LAYOUTERROR request
1541  */
1542 static int nfs4_xdr_dec_layouterror(struct rpc_rqst *rqstp,
1543 				    struct xdr_stream *xdr,
1544 				    void *data)
1545 {
1546 	struct nfs42_layouterror_res *res = data;
1547 	struct compound_hdr hdr;
1548 	int status, i;
1549 
1550 	status = decode_compound_hdr(xdr, &hdr);
1551 	if (status)
1552 		goto out;
1553 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1554 	if (status)
1555 		goto out;
1556 	status = decode_putfh(xdr);
1557 
1558 	for (i = 0; i < res->num_errors && status == 0; i++)
1559 		status = decode_layouterror(xdr);
1560 out:
1561 	res->rpc_status = status;
1562 	return status;
1563 }
1564 
1565 /*
1566  * Decode SETXATTR request
1567  */
1568 static int nfs4_xdr_dec_setxattr(struct rpc_rqst *req, struct xdr_stream *xdr,
1569 				 void *data)
1570 {
1571 	struct nfs42_setxattrres *res = data;
1572 	struct compound_hdr hdr;
1573 	int status;
1574 
1575 	status = decode_compound_hdr(xdr, &hdr);
1576 	if (status)
1577 		goto out;
1578 	status = decode_sequence(xdr, &res->seq_res, req);
1579 	if (status)
1580 		goto out;
1581 	status = decode_putfh(xdr);
1582 	if (status)
1583 		goto out;
1584 	status = decode_setxattr(xdr, &res->cinfo);
1585 	if (status)
1586 		goto out;
1587 	status = decode_getfattr(xdr, res->fattr, res->server);
1588 out:
1589 	return status;
1590 }
1591 
1592 /*
1593  * Decode GETXATTR request
1594  */
1595 static int nfs4_xdr_dec_getxattr(struct rpc_rqst *rqstp,
1596 				 struct xdr_stream *xdr, void *data)
1597 {
1598 	struct nfs42_getxattrres *res = data;
1599 	struct compound_hdr hdr;
1600 	int status;
1601 
1602 	status = decode_compound_hdr(xdr, &hdr);
1603 	if (status)
1604 		goto out;
1605 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1606 	if (status)
1607 		goto out;
1608 	status = decode_putfh(xdr);
1609 	if (status)
1610 		goto out;
1611 	status = decode_getxattr(xdr, res, rqstp);
1612 out:
1613 	return status;
1614 }
1615 
1616 /*
1617  * Decode LISTXATTR request
1618  */
1619 static int nfs4_xdr_dec_listxattrs(struct rpc_rqst *rqstp,
1620 				   struct xdr_stream *xdr, void *data)
1621 {
1622 	struct nfs42_listxattrsres *res = data;
1623 	struct compound_hdr hdr;
1624 	int status;
1625 
1626 	xdr_set_scratch_page(xdr, res->scratch);
1627 
1628 	status = decode_compound_hdr(xdr, &hdr);
1629 	if (status)
1630 		goto out;
1631 	status = decode_sequence(xdr, &res->seq_res, rqstp);
1632 	if (status)
1633 		goto out;
1634 	status = decode_putfh(xdr);
1635 	if (status)
1636 		goto out;
1637 	status = decode_listxattrs(xdr, res);
1638 out:
1639 	return status;
1640 }
1641 
1642 /*
1643  * Decode REMOVEXATTR request
1644  */
1645 static int nfs4_xdr_dec_removexattr(struct rpc_rqst *req,
1646 				    struct xdr_stream *xdr, void *data)
1647 {
1648 	struct nfs42_removexattrres *res = data;
1649 	struct compound_hdr hdr;
1650 	int status;
1651 
1652 	status = decode_compound_hdr(xdr, &hdr);
1653 	if (status)
1654 		goto out;
1655 	status = decode_sequence(xdr, &res->seq_res, req);
1656 	if (status)
1657 		goto out;
1658 	status = decode_putfh(xdr);
1659 	if (status)
1660 		goto out;
1661 
1662 	status = decode_removexattr(xdr, &res->cinfo);
1663 out:
1664 	return status;
1665 }
1666 #endif /* __LINUX_FS_NFS_NFS4_2XDR_H */
1667