xref: /linux/fs/nfs/callback_xdr.c (revision 189f164e573e18d9f8876dbd3ad8fcbe11f93037)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/nfs/callback_xdr.c
4  *
5  * Copyright (C) 2004 Trond Myklebust
6  *
7  * NFSv4 callback encode/decode procedures
8  */
9 #include <linux/kernel.h>
10 #include <linux/sunrpc/svc.h>
11 #include <linux/nfs4.h>
12 #include <linux/nfs_fs.h>
13 #include <linux/ratelimit.h>
14 #include <linux/printk.h>
15 #include <linux/slab.h>
16 #include <linux/sunrpc/bc_xprt.h>
17 #include "nfs4_fs.h"
18 #include "callback.h"
19 #include "internal.h"
20 #include "nfs4session.h"
21 #include "nfs4trace.h"
22 
23 #define CB_OP_TAGLEN_MAXSZ		(512)
24 #define CB_OP_HDR_RES_MAXSZ		(2 * 4) // opcode, status
25 #define CB_OP_GETATTR_BITMAP_MAXSZ	(4 * 4) // bitmap length, 3 bitmaps
26 #define CB_OP_GETATTR_RES_MAXSZ		(CB_OP_HDR_RES_MAXSZ + \
27 					 CB_OP_GETATTR_BITMAP_MAXSZ + \
28 					 /* change, size, atime, ctime,
29 					  * mtime, deleg_atime, deleg_mtime */\
30 					 (2 + 2 + 3 + 3 + 3 + 3 + 3) * 4)
31 #define CB_OP_RECALL_RES_MAXSZ		(CB_OP_HDR_RES_MAXSZ)
32 
33 #define CB_OP_LAYOUTRECALL_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
34 #define CB_OP_DEVICENOTIFY_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
35 #define CB_OP_SEQUENCE_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ + \
36 					 NFS4_MAX_SESSIONID_LEN + \
37 					 (1 + 3) * 4) // seqid, 3 slotids
38 #define CB_OP_RECALLANY_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
39 #define CB_OP_RECALLSLOT_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
40 #define CB_OP_NOTIFY_LOCK_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
41 #ifdef CONFIG_NFS_V4_2
42 #define CB_OP_OFFLOAD_RES_MAXSZ		(CB_OP_HDR_RES_MAXSZ)
43 #endif /* CONFIG_NFS_V4_2 */
44 
45 #define NFSDBG_FACILITY NFSDBG_CALLBACK
46 
47 /* Internal error code */
48 #define NFS4ERR_RESOURCE_HDR	11050
49 
50 struct callback_op {
51 	__be32 (*process_op)(void *, void *, struct cb_process_state *);
52 	__be32 (*decode_args)(struct svc_rqst *, struct xdr_stream *, void *);
53 	__be32 (*encode_res)(struct svc_rqst *, struct xdr_stream *,
54 			const void *);
55 	long res_maxsize;
56 };
57 
58 static struct callback_op callback_ops[];
59 
60 static __be32 nfs4_callback_null(struct svc_rqst *rqstp)
61 {
62 	return htonl(NFS4_OK);
63 }
64 
65 /*
66  * svc_process_common() looks for an XDR encoder to know when
67  * not to drop a Reply.
68  */
69 static bool nfs4_encode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
70 {
71 	return true;
72 }
73 
74 static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len,
75 		const char **str, size_t maxlen)
76 {
77 	ssize_t err;
78 
79 	err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen);
80 	if (err < 0)
81 		return cpu_to_be32(NFS4ERR_RESOURCE);
82 	*len = err;
83 	return 0;
84 }
85 
86 static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
87 {
88 	__be32 *p;
89 
90 	p = xdr_inline_decode(xdr, 4);
91 	if (unlikely(p == NULL))
92 		return htonl(NFS4ERR_RESOURCE);
93 	fh->size = ntohl(*p);
94 	if (fh->size > NFS4_FHSIZE)
95 		return htonl(NFS4ERR_BADHANDLE);
96 	p = xdr_inline_decode(xdr, fh->size);
97 	if (unlikely(p == NULL))
98 		return htonl(NFS4ERR_RESOURCE);
99 	memcpy(&fh->data[0], p, fh->size);
100 	memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
101 	return 0;
102 }
103 
104 static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
105 {
106 	__be32 *p;
107 	unsigned int attrlen;
108 
109 	p = xdr_inline_decode(xdr, 4);
110 	if (unlikely(p == NULL))
111 		return htonl(NFS4ERR_RESOURCE);
112 	attrlen = ntohl(*p);
113 	p = xdr_inline_decode(xdr, attrlen << 2);
114 	if (unlikely(p == NULL))
115 		return htonl(NFS4ERR_RESOURCE);
116 	if (likely(attrlen > 0))
117 		bitmap[0] = ntohl(*p++);
118 	if (attrlen > 1)
119 		bitmap[1] = ntohl(*p++);
120 	if (attrlen > 2)
121 		bitmap[2] = ntohl(*p);
122 	return 0;
123 }
124 
125 static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
126 {
127 	__be32 *p;
128 
129 	p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE);
130 	if (unlikely(p == NULL))
131 		return htonl(NFS4ERR_RESOURCE);
132 	memcpy(stateid->data, p, NFS4_STATEID_SIZE);
133 	return 0;
134 }
135 
136 static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
137 {
138 	stateid->type = NFS4_DELEGATION_STATEID_TYPE;
139 	return decode_stateid(xdr, stateid);
140 }
141 
142 static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
143 {
144 	__be32 *p;
145 	__be32 status;
146 
147 	status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ);
148 	if (unlikely(status != 0))
149 		return status;
150 	p = xdr_inline_decode(xdr, 12);
151 	if (unlikely(p == NULL))
152 		return htonl(NFS4ERR_RESOURCE);
153 	hdr->minorversion = ntohl(*p++);
154 	/* Check for minor version support */
155 	if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
156 		hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
157 	} else {
158 		pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
159 			"illegal minor version %u!\n",
160 			__func__, hdr->minorversion);
161 		return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
162 	}
163 	hdr->nops = ntohl(*p);
164 	return 0;
165 }
166 
167 static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
168 {
169 	__be32 *p;
170 	p = xdr_inline_decode(xdr, 4);
171 	if (unlikely(p == NULL))
172 		return htonl(NFS4ERR_RESOURCE_HDR);
173 	*op = ntohl(*p);
174 	return 0;
175 }
176 
177 static __be32 decode_getattr_args(struct svc_rqst *rqstp,
178 		struct xdr_stream *xdr, void *argp)
179 {
180 	struct cb_getattrargs *args = argp;
181 	__be32 status;
182 
183 	status = decode_fh(xdr, &args->fh);
184 	if (unlikely(status != 0))
185 		return status;
186 	return decode_bitmap(xdr, args->bitmap);
187 }
188 
189 static __be32 decode_recall_args(struct svc_rqst *rqstp,
190 		struct xdr_stream *xdr, void *argp)
191 {
192 	struct cb_recallargs *args = argp;
193 	__be32 *p;
194 	__be32 status;
195 
196 	status = decode_delegation_stateid(xdr, &args->stateid);
197 	if (unlikely(status != 0))
198 		return status;
199 	p = xdr_inline_decode(xdr, 4);
200 	if (unlikely(p == NULL))
201 		return htonl(NFS4ERR_RESOURCE);
202 	args->truncate = ntohl(*p);
203 	return decode_fh(xdr, &args->fh);
204 }
205 
206 static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
207 {
208 	stateid->type = NFS4_LAYOUT_STATEID_TYPE;
209 	return decode_stateid(xdr, stateid);
210 }
211 
212 static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
213 				       struct xdr_stream *xdr, void *argp)
214 {
215 	struct cb_layoutrecallargs *args = argp;
216 	__be32 *p;
217 	__be32 status = 0;
218 	uint32_t iomode;
219 
220 	p = xdr_inline_decode(xdr, 4 * sizeof(uint32_t));
221 	if (unlikely(p == NULL))
222 		return htonl(NFS4ERR_BADXDR);
223 
224 	args->cbl_layout_type = ntohl(*p++);
225 	/* Depite the spec's xdr, iomode really belongs in the FILE switch,
226 	 * as it is unusable and ignored with the other types.
227 	 */
228 	iomode = ntohl(*p++);
229 	args->cbl_layoutchanged = ntohl(*p++);
230 	args->cbl_recall_type = ntohl(*p++);
231 
232 	if (args->cbl_recall_type == RETURN_FILE) {
233 		args->cbl_range.iomode = iomode;
234 		status = decode_fh(xdr, &args->cbl_fh);
235 		if (unlikely(status != 0))
236 			return status;
237 
238 		p = xdr_inline_decode(xdr, 2 * sizeof(uint64_t));
239 		if (unlikely(p == NULL))
240 			return htonl(NFS4ERR_BADXDR);
241 		p = xdr_decode_hyper(p, &args->cbl_range.offset);
242 		p = xdr_decode_hyper(p, &args->cbl_range.length);
243 		return decode_layout_stateid(xdr, &args->cbl_stateid);
244 	} else if (args->cbl_recall_type == RETURN_FSID) {
245 		p = xdr_inline_decode(xdr, 2 * sizeof(uint64_t));
246 		if (unlikely(p == NULL))
247 			return htonl(NFS4ERR_BADXDR);
248 		p = xdr_decode_hyper(p, &args->cbl_fsid.major);
249 		p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
250 	} else if (args->cbl_recall_type != RETURN_ALL)
251 		return htonl(NFS4ERR_BADXDR);
252 	return 0;
253 }
254 
255 static
256 __be32 decode_devicenotify_args(struct svc_rqst *rqstp,
257 				struct xdr_stream *xdr,
258 				void *argp)
259 {
260 	struct cb_devicenotifyargs *args = argp;
261 	uint32_t tmp, n, i;
262 	__be32 *p;
263 	__be32 status = 0;
264 
265 	/* Num of device notifications */
266 	p = xdr_inline_decode(xdr, sizeof(uint32_t));
267 	if (unlikely(p == NULL)) {
268 		status = htonl(NFS4ERR_BADXDR);
269 		goto out;
270 	}
271 	n = ntohl(*p++);
272 	if (n == 0)
273 		goto out;
274 
275 	args->devs = kmalloc_objs(*args->devs, n);
276 	if (!args->devs) {
277 		status = htonl(NFS4ERR_DELAY);
278 		goto out;
279 	}
280 
281 	/* Decode each dev notification */
282 	for (i = 0; i < n; i++) {
283 		struct cb_devicenotifyitem *dev = &args->devs[i];
284 
285 		p = xdr_inline_decode(xdr, (4 * sizeof(uint32_t)) +
286 				      NFS4_DEVICEID4_SIZE);
287 		if (unlikely(p == NULL)) {
288 			status = htonl(NFS4ERR_BADXDR);
289 			goto err;
290 		}
291 
292 		tmp = ntohl(*p++);	/* bitmap size */
293 		if (tmp != 1) {
294 			status = htonl(NFS4ERR_INVAL);
295 			goto err;
296 		}
297 		dev->cbd_notify_type = ntohl(*p++);
298 		if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
299 		    dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
300 			status = htonl(NFS4ERR_INVAL);
301 			goto err;
302 		}
303 
304 		tmp = ntohl(*p++);	/* opaque size */
305 		if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
306 		     (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
307 		    ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
308 		     (tmp != NFS4_DEVICEID4_SIZE + 4))) {
309 			status = htonl(NFS4ERR_INVAL);
310 			goto err;
311 		}
312 		dev->cbd_layout_type = ntohl(*p++);
313 		memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
314 		p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
315 
316 		if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
317 			p = xdr_inline_decode(xdr, sizeof(uint32_t));
318 			if (unlikely(p == NULL)) {
319 				status = htonl(NFS4ERR_BADXDR);
320 				goto err;
321 			}
322 			dev->cbd_immediate = ntohl(*p++);
323 		} else {
324 			dev->cbd_immediate = 0;
325 		}
326 
327 		dprintk("%s: type %d layout 0x%x immediate %d\n",
328 			__func__, dev->cbd_notify_type, dev->cbd_layout_type,
329 			dev->cbd_immediate);
330 	}
331 	args->ndevs = n;
332 	dprintk("%s: ndevs %d\n", __func__, args->ndevs);
333 	return 0;
334 err:
335 	kfree(args->devs);
336 out:
337 	args->devs = NULL;
338 	args->ndevs = 0;
339 	dprintk("%s: status %d ndevs %d\n",
340 		__func__, ntohl(status), args->ndevs);
341 	return status;
342 }
343 
344 static __be32 decode_sessionid(struct xdr_stream *xdr,
345 				 struct nfs4_sessionid *sid)
346 {
347 	__be32 *p;
348 
349 	p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN);
350 	if (unlikely(p == NULL))
351 		return htonl(NFS4ERR_RESOURCE);
352 
353 	memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
354 	return 0;
355 }
356 
357 static __be32 decode_rc_list(struct xdr_stream *xdr,
358 			       struct referring_call_list *rc_list)
359 {
360 	__be32 *p;
361 	int i;
362 	__be32 status;
363 
364 	status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
365 	if (status)
366 		goto out;
367 
368 	status = htonl(NFS4ERR_RESOURCE);
369 	p = xdr_inline_decode(xdr, sizeof(uint32_t));
370 	if (unlikely(p == NULL))
371 		goto out;
372 
373 	rc_list->rcl_nrefcalls = ntohl(*p++);
374 	if (rc_list->rcl_nrefcalls) {
375 		if (unlikely(rc_list->rcl_nrefcalls > xdr->buf->len))
376 			goto out;
377 		p = xdr_inline_decode(xdr,
378 			     rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
379 		if (unlikely(p == NULL))
380 			goto out;
381 		rc_list->rcl_refcalls = kmalloc_objs(*rc_list->rcl_refcalls,
382 						     rc_list->rcl_nrefcalls);
383 		if (unlikely(rc_list->rcl_refcalls == NULL))
384 			goto out;
385 		for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
386 			rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
387 			rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
388 		}
389 	}
390 	status = 0;
391 
392 out:
393 	return status;
394 }
395 
396 static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
397 					struct xdr_stream *xdr,
398 					void *argp)
399 {
400 	struct cb_sequenceargs *args = argp;
401 	__be32 *p;
402 	int i;
403 	__be32 status;
404 
405 	status = decode_sessionid(xdr, &args->csa_sessionid);
406 	if (status)
407 		return status;
408 
409 	p = xdr_inline_decode(xdr, 5 * sizeof(uint32_t));
410 	if (unlikely(p == NULL))
411 		return htonl(NFS4ERR_RESOURCE);
412 
413 	args->csa_addr = svc_addr(rqstp);
414 	args->csa_sequenceid = ntohl(*p++);
415 	args->csa_slotid = ntohl(*p++);
416 	args->csa_highestslotid = ntohl(*p++);
417 	args->csa_cachethis = ntohl(*p++);
418 	args->csa_nrclists = ntohl(*p++);
419 	args->csa_rclists = NULL;
420 	if (args->csa_nrclists) {
421 		args->csa_rclists = kmalloc_objs(*args->csa_rclists,
422 						 args->csa_nrclists);
423 		if (unlikely(args->csa_rclists == NULL))
424 			return htonl(NFS4ERR_RESOURCE);
425 
426 		for (i = 0; i < args->csa_nrclists; i++) {
427 			status = decode_rc_list(xdr, &args->csa_rclists[i]);
428 			if (status) {
429 				args->csa_nrclists = i;
430 				goto out_free;
431 			}
432 		}
433 	}
434 	return 0;
435 
436 out_free:
437 	for (i = 0; i < args->csa_nrclists; i++)
438 		kfree(args->csa_rclists[i].rcl_refcalls);
439 	kfree(args->csa_rclists);
440 	return status;
441 }
442 
443 static __be32 decode_recallany_args(struct svc_rqst *rqstp,
444 				      struct xdr_stream *xdr,
445 				      void *argp)
446 {
447 	struct cb_recallanyargs *args = argp;
448 	uint32_t bitmap[3];
449 	__be32 *p, status;
450 
451 	p = xdr_inline_decode(xdr, 4);
452 	if (unlikely(p == NULL))
453 		return htonl(NFS4ERR_BADXDR);
454 	args->craa_objs_to_keep = ntohl(*p++);
455 	status = decode_bitmap(xdr, bitmap);
456 	if (unlikely(status))
457 		return status;
458 	args->craa_type_mask = bitmap[0];
459 
460 	return 0;
461 }
462 
463 static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
464 					struct xdr_stream *xdr,
465 					void *argp)
466 {
467 	struct cb_recallslotargs *args = argp;
468 	__be32 *p;
469 
470 	p = xdr_inline_decode(xdr, 4);
471 	if (unlikely(p == NULL))
472 		return htonl(NFS4ERR_BADXDR);
473 	args->crsa_target_highest_slotid = ntohl(*p++);
474 	return 0;
475 }
476 
477 static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
478 {
479 	__be32		*p;
480 	unsigned int	len;
481 
482 	p = xdr_inline_decode(xdr, 12);
483 	if (unlikely(p == NULL))
484 		return htonl(NFS4ERR_BADXDR);
485 
486 	p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
487 	len = be32_to_cpu(*p);
488 
489 	p = xdr_inline_decode(xdr, len);
490 	if (unlikely(p == NULL))
491 		return htonl(NFS4ERR_BADXDR);
492 
493 	/* Only try to decode if the length is right */
494 	if (len == 20) {
495 		p += 2;	/* skip "lock id:" */
496 		args->cbnl_owner.s_dev = be32_to_cpu(*p++);
497 		xdr_decode_hyper(p, &args->cbnl_owner.id);
498 		args->cbnl_valid = true;
499 	} else {
500 		args->cbnl_owner.s_dev = 0;
501 		args->cbnl_owner.id = 0;
502 		args->cbnl_valid = false;
503 	}
504 	return 0;
505 }
506 
507 static __be32 decode_notify_lock_args(struct svc_rqst *rqstp,
508 		struct xdr_stream *xdr, void *argp)
509 {
510 	struct cb_notify_lock_args *args = argp;
511 	__be32 status;
512 
513 	status = decode_fh(xdr, &args->cbnl_fh);
514 	if (unlikely(status != 0))
515 		return status;
516 	return decode_lockowner(xdr, args);
517 }
518 
519 #ifdef CONFIG_NFS_V4_2
520 static __be32 decode_write_response(struct xdr_stream *xdr,
521 					struct cb_offloadargs *args)
522 {
523 	__be32 *p;
524 
525 	/* skip the always zero field */
526 	p = xdr_inline_decode(xdr, 4);
527 	if (unlikely(!p))
528 		goto out;
529 	p++;
530 
531 	/* decode count, stable_how, verifier */
532 	p = xdr_inline_decode(xdr, 8 + 4);
533 	if (unlikely(!p))
534 		goto out;
535 	p = xdr_decode_hyper(p, &args->wr_count);
536 	args->wr_writeverf.committed = be32_to_cpup(p);
537 	p = xdr_inline_decode(xdr, NFS4_VERIFIER_SIZE);
538 	if (likely(p)) {
539 		memcpy(&args->wr_writeverf.verifier.data[0], p,
540 			NFS4_VERIFIER_SIZE);
541 		return 0;
542 	}
543 out:
544 	return htonl(NFS4ERR_RESOURCE);
545 }
546 
547 static __be32 decode_offload_args(struct svc_rqst *rqstp,
548 					struct xdr_stream *xdr,
549 					void *data)
550 {
551 	struct cb_offloadargs *args = data;
552 	__be32 *p;
553 	__be32 status;
554 
555 	/* decode fh */
556 	status = decode_fh(xdr, &args->coa_fh);
557 	if (unlikely(status != 0))
558 		return status;
559 
560 	/* decode stateid */
561 	status = decode_stateid(xdr, &args->coa_stateid);
562 	if (unlikely(status != 0))
563 		return status;
564 
565 	/* decode status */
566 	p = xdr_inline_decode(xdr, 4);
567 	if (unlikely(!p))
568 		goto out;
569 	args->error = ntohl(*p++);
570 	if (!args->error) {
571 		status = decode_write_response(xdr, args);
572 		if (unlikely(status != 0))
573 			return status;
574 	} else {
575 		p = xdr_inline_decode(xdr, 8);
576 		if (unlikely(!p))
577 			goto out;
578 		p = xdr_decode_hyper(p, &args->wr_count);
579 	}
580 	return 0;
581 out:
582 	return htonl(NFS4ERR_RESOURCE);
583 }
584 #endif /* CONFIG_NFS_V4_2 */
585 static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
586 {
587 	if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
588 		return cpu_to_be32(NFS4ERR_RESOURCE);
589 	return 0;
590 }
591 
592 static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, size_t sz)
593 {
594 	if (xdr_stream_encode_uint32_array(xdr, bitmap, sz) < 0)
595 		return cpu_to_be32(NFS4ERR_RESOURCE);
596 	return 0;
597 }
598 
599 static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
600 {
601 	__be32 *p;
602 
603 	if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
604 		return 0;
605 	p = xdr_reserve_space(xdr, 8);
606 	if (unlikely(!p))
607 		return htonl(NFS4ERR_RESOURCE);
608 	p = xdr_encode_hyper(p, change);
609 	return 0;
610 }
611 
612 static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
613 {
614 	__be32 *p;
615 
616 	if (!(bitmap[0] & FATTR4_WORD0_SIZE))
617 		return 0;
618 	p = xdr_reserve_space(xdr, 8);
619 	if (unlikely(!p))
620 		return htonl(NFS4ERR_RESOURCE);
621 	p = xdr_encode_hyper(p, size);
622 	return 0;
623 }
624 
625 static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec64 *time)
626 {
627 	__be32 *p;
628 
629 	p = xdr_reserve_space(xdr, 12);
630 	if (unlikely(!p))
631 		return htonl(NFS4ERR_RESOURCE);
632 	p = xdr_encode_hyper(p, time->tv_sec);
633 	*p = htonl(time->tv_nsec);
634 	return 0;
635 }
636 
637 static __be32 encode_attr_atime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time)
638 {
639 	if (!(bitmap[1] & FATTR4_WORD1_TIME_ACCESS))
640 		return 0;
641 	return encode_attr_time(xdr,time);
642 }
643 
644 static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time)
645 {
646 	if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
647 		return 0;
648 	return encode_attr_time(xdr,time);
649 }
650 
651 static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time)
652 {
653 	if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
654 		return 0;
655 	return encode_attr_time(xdr,time);
656 }
657 
658 static __be32 encode_attr_delegatime(struct xdr_stream *xdr,
659 				     const uint32_t *bitmap,
660 				     const struct timespec64 *time)
661 {
662 	if (!(bitmap[2] & FATTR4_WORD2_TIME_DELEG_ACCESS))
663 		return 0;
664 	return encode_attr_time(xdr,time);
665 }
666 
667 static __be32 encode_attr_delegmtime(struct xdr_stream *xdr,
668 				     const uint32_t *bitmap,
669 				     const struct timespec64 *time)
670 {
671 	if (!(bitmap[2] & FATTR4_WORD2_TIME_DELEG_MODIFY))
672 		return 0;
673 	return encode_attr_time(xdr,time);
674 }
675 
676 static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
677 {
678 	__be32 status;
679 
680 	hdr->status = xdr_reserve_space(xdr, 4);
681 	if (unlikely(hdr->status == NULL))
682 		return htonl(NFS4ERR_RESOURCE);
683 	status = encode_string(xdr, hdr->taglen, hdr->tag);
684 	if (unlikely(status != 0))
685 		return status;
686 	hdr->nops = xdr_reserve_space(xdr, 4);
687 	if (unlikely(hdr->nops == NULL))
688 		return htonl(NFS4ERR_RESOURCE);
689 	return 0;
690 }
691 
692 static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
693 {
694 	__be32 *p;
695 
696 	p = xdr_reserve_space(xdr, 8);
697 	if (unlikely(p == NULL))
698 		return htonl(NFS4ERR_RESOURCE_HDR);
699 	*p++ = htonl(op);
700 	*p = res;
701 	return 0;
702 }
703 
704 static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr,
705 		const void *resp)
706 {
707 	const struct cb_getattrres *res = resp;
708 	__be32 *savep = NULL;
709 	__be32 status = res->status;
710 
711 	if (unlikely(status != 0))
712 		goto out;
713 	status = encode_attr_bitmap(xdr, res->bitmap, ARRAY_SIZE(res->bitmap));
714 	if (unlikely(status != 0))
715 		goto out;
716 	status = cpu_to_be32(NFS4ERR_RESOURCE);
717 	savep = xdr_reserve_space(xdr, sizeof(*savep));
718 	if (unlikely(!savep))
719 		goto out;
720 	status = encode_attr_change(xdr, res->bitmap, res->change_attr);
721 	if (unlikely(status != 0))
722 		goto out;
723 	status = encode_attr_size(xdr, res->bitmap, res->size);
724 	if (unlikely(status != 0))
725 		goto out;
726 	status = encode_attr_atime(xdr, res->bitmap, &res->atime);
727 	if (unlikely(status != 0))
728 		goto out;
729 	status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
730 	if (unlikely(status != 0))
731 		goto out;
732 	status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
733 	if (unlikely(status != 0))
734 		goto out;
735 	status = encode_attr_delegatime(xdr, res->bitmap, &res->atime);
736 	if (unlikely(status != 0))
737 		goto out;
738 	status = encode_attr_delegmtime(xdr, res->bitmap, &res->mtime);
739 	*savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
740 out:
741 	return status;
742 }
743 
744 static __be32 encode_sessionid(struct xdr_stream *xdr,
745 				 const struct nfs4_sessionid *sid)
746 {
747 	__be32 *p;
748 
749 	p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
750 	if (unlikely(p == NULL))
751 		return htonl(NFS4ERR_RESOURCE);
752 
753 	memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
754 	return 0;
755 }
756 
757 static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
758 				       struct xdr_stream *xdr,
759 				       const void *resp)
760 {
761 	const struct cb_sequenceres *res = resp;
762 	__be32 *p;
763 	__be32 status = res->csr_status;
764 
765 	if (unlikely(status != 0))
766 		return status;
767 
768 	status = encode_sessionid(xdr, &res->csr_sessionid);
769 	if (status)
770 		return status;
771 
772 	p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
773 	if (unlikely(p == NULL))
774 		return htonl(NFS4ERR_RESOURCE);
775 
776 	*p++ = htonl(res->csr_sequenceid);
777 	*p++ = htonl(res->csr_slotid);
778 	*p++ = htonl(res->csr_highestslotid);
779 	*p++ = htonl(res->csr_target_highestslotid);
780 	return 0;
781 }
782 
783 static __be32
784 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
785 {
786 	if (op_nr == OP_CB_SEQUENCE) {
787 		if (nop != 0)
788 			return htonl(NFS4ERR_SEQUENCE_POS);
789 	} else {
790 		if (nop == 0)
791 			return htonl(NFS4ERR_OP_NOT_IN_SESSION);
792 	}
793 
794 	switch (op_nr) {
795 	case OP_CB_GETATTR:
796 	case OP_CB_RECALL:
797 	case OP_CB_SEQUENCE:
798 	case OP_CB_RECALL_ANY:
799 	case OP_CB_RECALL_SLOT:
800 	case OP_CB_LAYOUTRECALL:
801 	case OP_CB_NOTIFY_DEVICEID:
802 	case OP_CB_NOTIFY_LOCK:
803 		*op = &callback_ops[op_nr];
804 		break;
805 
806 	case OP_CB_NOTIFY:
807 	case OP_CB_PUSH_DELEG:
808 	case OP_CB_RECALLABLE_OBJ_AVAIL:
809 	case OP_CB_WANTS_CANCELLED:
810 		return htonl(NFS4ERR_NOTSUPP);
811 
812 	default:
813 		return htonl(NFS4ERR_OP_ILLEGAL);
814 	}
815 
816 	return htonl(NFS_OK);
817 }
818 
819 static void nfs4_callback_free_slot(struct nfs4_session *session,
820 		struct nfs4_slot *slot)
821 {
822 	struct nfs4_slot_table *tbl = &session->bc_slot_table;
823 
824 	spin_lock(&tbl->slot_tbl_lock);
825 	/*
826 	 * Let the state manager know callback processing done.
827 	 * A single slot, so highest used slotid is either 0 or -1
828 	 */
829 	nfs4_free_slot(tbl, slot);
830 	spin_unlock(&tbl->slot_tbl_lock);
831 }
832 
833 static void nfs4_cb_free_slot(struct cb_process_state *cps)
834 {
835 	if (cps->slot) {
836 		nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
837 		cps->slot = NULL;
838 	}
839 }
840 
841 #ifdef CONFIG_NFS_V4_2
842 static __be32
843 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
844 {
845 	__be32 status = preprocess_nfs41_op(nop, op_nr, op);
846 	if (status != htonl(NFS4ERR_OP_ILLEGAL))
847 		return status;
848 
849 	if (op_nr == OP_CB_OFFLOAD) {
850 		*op = &callback_ops[op_nr];
851 		return htonl(NFS_OK);
852 	} else
853 		return htonl(NFS4ERR_NOTSUPP);
854 	return htonl(NFS4ERR_OP_ILLEGAL);
855 }
856 #else /* CONFIG_NFS_V4_2 */
857 static __be32
858 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
859 {
860 	return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
861 }
862 #endif /* CONFIG_NFS_V4_2 */
863 
864 static __be32
865 preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
866 {
867 	switch (op_nr) {
868 	case OP_CB_GETATTR:
869 	case OP_CB_RECALL:
870 		*op = &callback_ops[op_nr];
871 		break;
872 	default:
873 		return htonl(NFS4ERR_OP_ILLEGAL);
874 	}
875 
876 	return htonl(NFS_OK);
877 }
878 
879 static __be32 process_op(int nop, struct svc_rqst *rqstp,
880 			 struct cb_process_state *cps)
881 {
882 	struct xdr_stream *xdr_out = &rqstp->rq_res_stream;
883 	struct callback_op *op = &callback_ops[0];
884 	unsigned int op_nr;
885 	__be32 status;
886 	long maxlen;
887 	__be32 res;
888 
889 	status = decode_op_hdr(&rqstp->rq_arg_stream, &op_nr);
890 	if (unlikely(status))
891 		return status;
892 
893 	switch (cps->minorversion) {
894 	case 0:
895 		status = preprocess_nfs4_op(op_nr, &op);
896 		break;
897 	case 1:
898 		status = preprocess_nfs41_op(nop, op_nr, &op);
899 		break;
900 	case 2:
901 		status = preprocess_nfs42_op(nop, op_nr, &op);
902 		break;
903 	default:
904 		status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
905 	}
906 
907 	if (status == htonl(NFS4ERR_OP_ILLEGAL))
908 		op_nr = OP_CB_ILLEGAL;
909 	if (status)
910 		goto encode_hdr;
911 
912 	if (cps->drc_status) {
913 		status = cps->drc_status;
914 		goto encode_hdr;
915 	}
916 
917 	maxlen = xdr_out->end - xdr_out->p;
918 	if (maxlen > 0 && maxlen < PAGE_SIZE) {
919 		status = op->decode_args(rqstp, &rqstp->rq_arg_stream,
920 					 rqstp->rq_argp);
921 		if (likely(status == 0))
922 			status = op->process_op(rqstp->rq_argp, rqstp->rq_resp,
923 						cps);
924 	} else
925 		status = htonl(NFS4ERR_RESOURCE);
926 
927 encode_hdr:
928 	res = encode_op_hdr(xdr_out, op_nr, status);
929 	if (unlikely(res))
930 		return res;
931 	if (op->encode_res != NULL && status == 0)
932 		status = op->encode_res(rqstp, xdr_out, rqstp->rq_resp);
933 	return status;
934 }
935 
936 /*
937  * Decode, process and encode a COMPOUND
938  */
939 static __be32 nfs4_callback_compound(struct svc_rqst *rqstp)
940 {
941 	struct cb_compound_hdr_arg hdr_arg = { 0 };
942 	struct cb_compound_hdr_res hdr_res = { NULL };
943 	struct cb_process_state cps = {
944 		.drc_status = 0,
945 		.clp = NULL,
946 		.net = SVC_NET(rqstp),
947 	};
948 	unsigned int nops = 0;
949 	__be32 status;
950 
951 	status = decode_compound_hdr_arg(&rqstp->rq_arg_stream, &hdr_arg);
952 	if (status == htonl(NFS4ERR_RESOURCE))
953 		return rpc_garbage_args;
954 
955 	if (hdr_arg.minorversion == 0) {
956 		cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
957 		if (!cps.clp) {
958 			trace_nfs_cb_no_clp(rqstp->rq_xid, hdr_arg.cb_ident);
959 			goto out_invalidcred;
960 		}
961 		if (!check_gss_callback_principal(cps.clp, rqstp)) {
962 			trace_nfs_cb_badprinc(rqstp->rq_xid, hdr_arg.cb_ident);
963 			nfs_put_client(cps.clp);
964 			goto out_invalidcred;
965 		}
966 		svc_xprt_set_valid(rqstp->rq_xprt);
967 	}
968 
969 	cps.minorversion = hdr_arg.minorversion;
970 	hdr_res.taglen = hdr_arg.taglen;
971 	hdr_res.tag = hdr_arg.tag;
972 	if (encode_compound_hdr_res(&rqstp->rq_res_stream, &hdr_res) != 0) {
973 		if (cps.clp)
974 			nfs_put_client(cps.clp);
975 		return rpc_system_err;
976 	}
977 	while (status == 0 && nops != hdr_arg.nops) {
978 		status = process_op(nops, rqstp, &cps);
979 		nops++;
980 	}
981 
982 	/* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
983 	* resource error in cb_compound status without returning op */
984 	if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
985 		status = htonl(NFS4ERR_RESOURCE);
986 		nops--;
987 	}
988 
989 	if (svc_is_backchannel(rqstp) && cps.clp) {
990 		rqstp->bc_to_initval = cps.clp->cl_rpcclient->cl_timeout->to_initval;
991 		rqstp->bc_to_retries = cps.clp->cl_rpcclient->cl_timeout->to_retries;
992 	}
993 
994 	*hdr_res.status = status;
995 	*hdr_res.nops = htonl(nops);
996 	nfs4_cb_free_slot(&cps);
997 	nfs_put_client(cps.clp);
998 	return rpc_success;
999 
1000 out_invalidcred:
1001 	pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
1002 	rqstp->rq_auth_stat = rpc_autherr_badcred;
1003 	return rpc_success;
1004 }
1005 
1006 static int
1007 nfs_callback_dispatch(struct svc_rqst *rqstp)
1008 {
1009 	const struct svc_procedure *procp = rqstp->rq_procinfo;
1010 
1011 	*rqstp->rq_accept_statp = procp->pc_func(rqstp);
1012 	return 1;
1013 }
1014 
1015 /*
1016  * Define NFS4 callback COMPOUND ops.
1017  */
1018 static struct callback_op callback_ops[] = {
1019 	[0] = {
1020 		.res_maxsize = CB_OP_HDR_RES_MAXSZ,
1021 	},
1022 	[OP_CB_GETATTR] = {
1023 		.process_op = nfs4_callback_getattr,
1024 		.decode_args = decode_getattr_args,
1025 		.encode_res = encode_getattr_res,
1026 		.res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
1027 	},
1028 	[OP_CB_RECALL] = {
1029 		.process_op = nfs4_callback_recall,
1030 		.decode_args = decode_recall_args,
1031 		.res_maxsize = CB_OP_RECALL_RES_MAXSZ,
1032 	},
1033 	[OP_CB_LAYOUTRECALL] = {
1034 		.process_op = nfs4_callback_layoutrecall,
1035 		.decode_args = decode_layoutrecall_args,
1036 		.res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
1037 	},
1038 	[OP_CB_NOTIFY_DEVICEID] = {
1039 		.process_op = nfs4_callback_devicenotify,
1040 		.decode_args = decode_devicenotify_args,
1041 		.res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
1042 	},
1043 	[OP_CB_SEQUENCE] = {
1044 		.process_op = nfs4_callback_sequence,
1045 		.decode_args = decode_cb_sequence_args,
1046 		.encode_res = encode_cb_sequence_res,
1047 		.res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
1048 	},
1049 	[OP_CB_RECALL_ANY] = {
1050 		.process_op = nfs4_callback_recallany,
1051 		.decode_args = decode_recallany_args,
1052 		.res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
1053 	},
1054 	[OP_CB_RECALL_SLOT] = {
1055 		.process_op = nfs4_callback_recallslot,
1056 		.decode_args = decode_recallslot_args,
1057 		.res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
1058 	},
1059 	[OP_CB_NOTIFY_LOCK] = {
1060 		.process_op = nfs4_callback_notify_lock,
1061 		.decode_args = decode_notify_lock_args,
1062 		.res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
1063 	},
1064 #ifdef CONFIG_NFS_V4_2
1065 	[OP_CB_OFFLOAD] = {
1066 		.process_op = nfs4_callback_offload,
1067 		.decode_args = decode_offload_args,
1068 		.res_maxsize = CB_OP_OFFLOAD_RES_MAXSZ,
1069 	},
1070 #endif /* CONFIG_NFS_V4_2 */
1071 };
1072 
1073 /*
1074  * Define NFS4 callback procedures
1075  */
1076 static const struct svc_procedure nfs4_callback_procedures1[] = {
1077 	[CB_NULL] = {
1078 		.pc_func = nfs4_callback_null,
1079 		.pc_encode = nfs4_encode_void,
1080 		.pc_xdrressize = 1,
1081 		.pc_name = "NULL",
1082 	},
1083 	[CB_COMPOUND] = {
1084 		.pc_func = nfs4_callback_compound,
1085 		.pc_encode = nfs4_encode_void,
1086 		.pc_argsize = 256,
1087 		.pc_argzero = 256,
1088 		.pc_ressize = 256,
1089 		.pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1090 		.pc_name = "COMPOUND",
1091 	}
1092 };
1093 
1094 static DEFINE_PER_CPU_ALIGNED(unsigned long,
1095 			      nfs4_callback_count1[ARRAY_SIZE(nfs4_callback_procedures1)]);
1096 const struct svc_version nfs4_callback_version1 = {
1097 	.vs_vers = 1,
1098 	.vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1099 	.vs_proc = nfs4_callback_procedures1,
1100 	.vs_count = nfs4_callback_count1,
1101 	.vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1102 	.vs_dispatch = nfs_callback_dispatch,
1103 	.vs_hidden = true,
1104 	.vs_need_cong_ctrl = true,
1105 };
1106 
1107 static DEFINE_PER_CPU_ALIGNED(unsigned long,
1108 			      nfs4_callback_count4[ARRAY_SIZE(nfs4_callback_procedures1)]);
1109 const struct svc_version nfs4_callback_version4 = {
1110 	.vs_vers = 4,
1111 	.vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1112 	.vs_proc = nfs4_callback_procedures1,
1113 	.vs_count = nfs4_callback_count4,
1114 	.vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1115 	.vs_dispatch = nfs_callback_dispatch,
1116 	.vs_hidden = true,
1117 	.vs_need_cong_ctrl = true,
1118 };
1119