1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/nfs/callback_xdr.c
5 * Copyright (C) 2004 Trond Myklebust
7 * NFSv4 callback encode/decode procedures
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>
20 #include "nfs4session.h"
22 #define CB_OP_TAGLEN_MAXSZ (512)
23 #define CB_OP_HDR_RES_MAXSZ (2 * 4) // opcode, status
24 #define CB_OP_GETATTR_BITMAP_MAXSZ (4 * 4) // bitmap length, 3 bitmaps
25 #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
26 CB_OP_GETATTR_BITMAP_MAXSZ + \
27 /* change, size, ctime, mtime */\
29 #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
31 #if defined(CONFIG_NFS_V4_1)
32 #define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
33 #define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
34 #define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
35 NFS4_MAX_SESSIONID_LEN + \
36 (1 + 3) * 4) // seqid, 3 slotids
37 #define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
38 #define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
39 #define CB_OP_NOTIFY_LOCK_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
40 #endif /* CONFIG_NFS_V4_1 */
41 #ifdef CONFIG_NFS_V4_2
42 #define CB_OP_OFFLOAD_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
43 #endif /* CONFIG_NFS_V4_2 */
45 #define NFSDBG_FACILITY NFSDBG_CALLBACK
47 /* Internal error code */
48 #define NFS4ERR_RESOURCE_HDR 11050
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
*,
58 static struct callback_op callback_ops
[];
60 static __be32
nfs4_callback_null(struct svc_rqst
*rqstp
)
62 return htonl(NFS4_OK
);
65 static int nfs4_decode_void(struct svc_rqst
*rqstp
, __be32
*p
)
67 return xdr_argsize_check(rqstp
, p
);
70 static int nfs4_encode_void(struct svc_rqst
*rqstp
, __be32
*p
)
72 return xdr_ressize_check(rqstp
, p
);
75 static __be32
*read_buf(struct xdr_stream
*xdr
, size_t nbytes
)
79 p
= xdr_inline_decode(xdr
, nbytes
);
80 if (unlikely(p
== NULL
))
81 printk(KERN_WARNING
"NFS: NFSv4 callback reply buffer overflowed!\n");
85 static __be32
decode_string(struct xdr_stream
*xdr
, unsigned int *len
,
86 const char **str
, size_t maxlen
)
90 err
= xdr_stream_decode_opaque_inline(xdr
, (void **)str
, maxlen
);
92 return cpu_to_be32(NFS4ERR_RESOURCE
);
97 static __be32
decode_fh(struct xdr_stream
*xdr
, struct nfs_fh
*fh
)
101 p
= read_buf(xdr
, 4);
102 if (unlikely(p
== NULL
))
103 return htonl(NFS4ERR_RESOURCE
);
104 fh
->size
= ntohl(*p
);
105 if (fh
->size
> NFS4_FHSIZE
)
106 return htonl(NFS4ERR_BADHANDLE
);
107 p
= read_buf(xdr
, fh
->size
);
108 if (unlikely(p
== NULL
))
109 return htonl(NFS4ERR_RESOURCE
);
110 memcpy(&fh
->data
[0], p
, fh
->size
);
111 memset(&fh
->data
[fh
->size
], 0, sizeof(fh
->data
) - fh
->size
);
115 static __be32
decode_bitmap(struct xdr_stream
*xdr
, uint32_t *bitmap
)
118 unsigned int attrlen
;
120 p
= read_buf(xdr
, 4);
121 if (unlikely(p
== NULL
))
122 return htonl(NFS4ERR_RESOURCE
);
124 p
= read_buf(xdr
, attrlen
<< 2);
125 if (unlikely(p
== NULL
))
126 return htonl(NFS4ERR_RESOURCE
);
127 if (likely(attrlen
> 0))
128 bitmap
[0] = ntohl(*p
++);
130 bitmap
[1] = ntohl(*p
);
134 static __be32
decode_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
138 p
= read_buf(xdr
, NFS4_STATEID_SIZE
);
139 if (unlikely(p
== NULL
))
140 return htonl(NFS4ERR_RESOURCE
);
141 memcpy(stateid
->data
, p
, NFS4_STATEID_SIZE
);
145 static __be32
decode_delegation_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
147 stateid
->type
= NFS4_DELEGATION_STATEID_TYPE
;
148 return decode_stateid(xdr
, stateid
);
151 static __be32
decode_compound_hdr_arg(struct xdr_stream
*xdr
, struct cb_compound_hdr_arg
*hdr
)
156 status
= decode_string(xdr
, &hdr
->taglen
, &hdr
->tag
, CB_OP_TAGLEN_MAXSZ
);
157 if (unlikely(status
!= 0))
159 p
= read_buf(xdr
, 12);
160 if (unlikely(p
== NULL
))
161 return htonl(NFS4ERR_RESOURCE
);
162 hdr
->minorversion
= ntohl(*p
++);
163 /* Check for minor version support */
164 if (hdr
->minorversion
<= NFS4_MAX_MINOR_VERSION
) {
165 hdr
->cb_ident
= ntohl(*p
++); /* ignored by v4.1 and v4.2 */
167 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
168 "illegal minor version %u!\n",
169 __func__
, hdr
->minorversion
);
170 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
172 hdr
->nops
= ntohl(*p
);
176 static __be32
decode_op_hdr(struct xdr_stream
*xdr
, unsigned int *op
)
179 p
= read_buf(xdr
, 4);
180 if (unlikely(p
== NULL
))
181 return htonl(NFS4ERR_RESOURCE_HDR
);
186 static __be32
decode_getattr_args(struct svc_rqst
*rqstp
,
187 struct xdr_stream
*xdr
, void *argp
)
189 struct cb_getattrargs
*args
= argp
;
192 status
= decode_fh(xdr
, &args
->fh
);
193 if (unlikely(status
!= 0))
195 return decode_bitmap(xdr
, args
->bitmap
);
198 static __be32
decode_recall_args(struct svc_rqst
*rqstp
,
199 struct xdr_stream
*xdr
, void *argp
)
201 struct cb_recallargs
*args
= argp
;
205 status
= decode_delegation_stateid(xdr
, &args
->stateid
);
206 if (unlikely(status
!= 0))
208 p
= read_buf(xdr
, 4);
209 if (unlikely(p
== NULL
))
210 return htonl(NFS4ERR_RESOURCE
);
211 args
->truncate
= ntohl(*p
);
212 return decode_fh(xdr
, &args
->fh
);
215 #if defined(CONFIG_NFS_V4_1)
216 static __be32
decode_layout_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
218 stateid
->type
= NFS4_LAYOUT_STATEID_TYPE
;
219 return decode_stateid(xdr
, stateid
);
222 static __be32
decode_layoutrecall_args(struct svc_rqst
*rqstp
,
223 struct xdr_stream
*xdr
, void *argp
)
225 struct cb_layoutrecallargs
*args
= argp
;
230 p
= read_buf(xdr
, 4 * sizeof(uint32_t));
231 if (unlikely(p
== NULL
))
232 return htonl(NFS4ERR_BADXDR
);
234 args
->cbl_layout_type
= ntohl(*p
++);
235 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
236 * as it is unusable and ignored with the other types.
238 iomode
= ntohl(*p
++);
239 args
->cbl_layoutchanged
= ntohl(*p
++);
240 args
->cbl_recall_type
= ntohl(*p
++);
242 if (args
->cbl_recall_type
== RETURN_FILE
) {
243 args
->cbl_range
.iomode
= iomode
;
244 status
= decode_fh(xdr
, &args
->cbl_fh
);
245 if (unlikely(status
!= 0))
248 p
= read_buf(xdr
, 2 * sizeof(uint64_t));
249 if (unlikely(p
== NULL
))
250 return htonl(NFS4ERR_BADXDR
);
251 p
= xdr_decode_hyper(p
, &args
->cbl_range
.offset
);
252 p
= xdr_decode_hyper(p
, &args
->cbl_range
.length
);
253 return decode_layout_stateid(xdr
, &args
->cbl_stateid
);
254 } else if (args
->cbl_recall_type
== RETURN_FSID
) {
255 p
= read_buf(xdr
, 2 * sizeof(uint64_t));
256 if (unlikely(p
== NULL
))
257 return htonl(NFS4ERR_BADXDR
);
258 p
= xdr_decode_hyper(p
, &args
->cbl_fsid
.major
);
259 p
= xdr_decode_hyper(p
, &args
->cbl_fsid
.minor
);
260 } else if (args
->cbl_recall_type
!= RETURN_ALL
)
261 return htonl(NFS4ERR_BADXDR
);
266 __be32
decode_devicenotify_args(struct svc_rqst
*rqstp
,
267 struct xdr_stream
*xdr
,
270 struct cb_devicenotifyargs
*args
= argp
;
277 /* Num of device notifications */
278 p
= read_buf(xdr
, sizeof(uint32_t));
279 if (unlikely(p
== NULL
)) {
280 status
= htonl(NFS4ERR_BADXDR
);
286 if (n
> ULONG_MAX
/ sizeof(*args
->devs
)) {
287 status
= htonl(NFS4ERR_BADXDR
);
291 args
->devs
= kmalloc_array(n
, sizeof(*args
->devs
), GFP_KERNEL
);
293 status
= htonl(NFS4ERR_DELAY
);
297 /* Decode each dev notification */
298 for (i
= 0; i
< n
; i
++) {
299 struct cb_devicenotifyitem
*dev
= &args
->devs
[i
];
301 p
= read_buf(xdr
, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE
);
302 if (unlikely(p
== NULL
)) {
303 status
= htonl(NFS4ERR_BADXDR
);
307 tmp
= ntohl(*p
++); /* bitmap size */
309 status
= htonl(NFS4ERR_INVAL
);
312 dev
->cbd_notify_type
= ntohl(*p
++);
313 if (dev
->cbd_notify_type
!= NOTIFY_DEVICEID4_CHANGE
&&
314 dev
->cbd_notify_type
!= NOTIFY_DEVICEID4_DELETE
) {
315 status
= htonl(NFS4ERR_INVAL
);
319 tmp
= ntohl(*p
++); /* opaque size */
320 if (((dev
->cbd_notify_type
== NOTIFY_DEVICEID4_CHANGE
) &&
321 (tmp
!= NFS4_DEVICEID4_SIZE
+ 8)) ||
322 ((dev
->cbd_notify_type
== NOTIFY_DEVICEID4_DELETE
) &&
323 (tmp
!= NFS4_DEVICEID4_SIZE
+ 4))) {
324 status
= htonl(NFS4ERR_INVAL
);
327 dev
->cbd_layout_type
= ntohl(*p
++);
328 memcpy(dev
->cbd_dev_id
.data
, p
, NFS4_DEVICEID4_SIZE
);
329 p
+= XDR_QUADLEN(NFS4_DEVICEID4_SIZE
);
331 if (dev
->cbd_layout_type
== NOTIFY_DEVICEID4_CHANGE
) {
332 p
= read_buf(xdr
, sizeof(uint32_t));
333 if (unlikely(p
== NULL
)) {
334 status
= htonl(NFS4ERR_BADXDR
);
337 dev
->cbd_immediate
= ntohl(*p
++);
339 dev
->cbd_immediate
= 0;
344 dprintk("%s: type %d layout 0x%x immediate %d\n",
345 __func__
, dev
->cbd_notify_type
, dev
->cbd_layout_type
,
349 dprintk("%s: status %d ndevs %d\n",
350 __func__
, ntohl(status
), args
->ndevs
);
357 static __be32
decode_sessionid(struct xdr_stream
*xdr
,
358 struct nfs4_sessionid
*sid
)
362 p
= read_buf(xdr
, NFS4_MAX_SESSIONID_LEN
);
363 if (unlikely(p
== NULL
))
364 return htonl(NFS4ERR_RESOURCE
);
366 memcpy(sid
->data
, p
, NFS4_MAX_SESSIONID_LEN
);
370 static __be32
decode_rc_list(struct xdr_stream
*xdr
,
371 struct referring_call_list
*rc_list
)
377 status
= decode_sessionid(xdr
, &rc_list
->rcl_sessionid
);
381 status
= htonl(NFS4ERR_RESOURCE
);
382 p
= read_buf(xdr
, sizeof(uint32_t));
383 if (unlikely(p
== NULL
))
386 rc_list
->rcl_nrefcalls
= ntohl(*p
++);
387 if (rc_list
->rcl_nrefcalls
) {
389 rc_list
->rcl_nrefcalls
* 2 * sizeof(uint32_t));
390 if (unlikely(p
== NULL
))
392 rc_list
->rcl_refcalls
= kmalloc_array(rc_list
->rcl_nrefcalls
,
393 sizeof(*rc_list
->rcl_refcalls
),
395 if (unlikely(rc_list
->rcl_refcalls
== NULL
))
397 for (i
= 0; i
< rc_list
->rcl_nrefcalls
; i
++) {
398 rc_list
->rcl_refcalls
[i
].rc_sequenceid
= ntohl(*p
++);
399 rc_list
->rcl_refcalls
[i
].rc_slotid
= ntohl(*p
++);
408 static __be32
decode_cb_sequence_args(struct svc_rqst
*rqstp
,
409 struct xdr_stream
*xdr
,
412 struct cb_sequenceargs
*args
= argp
;
417 status
= decode_sessionid(xdr
, &args
->csa_sessionid
);
421 p
= read_buf(xdr
, 5 * sizeof(uint32_t));
422 if (unlikely(p
== NULL
))
423 return htonl(NFS4ERR_RESOURCE
);
425 args
->csa_addr
= svc_addr(rqstp
);
426 args
->csa_sequenceid
= ntohl(*p
++);
427 args
->csa_slotid
= ntohl(*p
++);
428 args
->csa_highestslotid
= ntohl(*p
++);
429 args
->csa_cachethis
= ntohl(*p
++);
430 args
->csa_nrclists
= ntohl(*p
++);
431 args
->csa_rclists
= NULL
;
432 if (args
->csa_nrclists
) {
433 args
->csa_rclists
= kmalloc_array(args
->csa_nrclists
,
434 sizeof(*args
->csa_rclists
),
436 if (unlikely(args
->csa_rclists
== NULL
))
437 return htonl(NFS4ERR_RESOURCE
);
439 for (i
= 0; i
< args
->csa_nrclists
; i
++) {
440 status
= decode_rc_list(xdr
, &args
->csa_rclists
[i
]);
442 args
->csa_nrclists
= i
;
450 for (i
= 0; i
< args
->csa_nrclists
; i
++)
451 kfree(args
->csa_rclists
[i
].rcl_refcalls
);
452 kfree(args
->csa_rclists
);
456 static __be32
decode_recallany_args(struct svc_rqst
*rqstp
,
457 struct xdr_stream
*xdr
,
460 struct cb_recallanyargs
*args
= argp
;
464 p
= read_buf(xdr
, 4);
465 if (unlikely(p
== NULL
))
466 return htonl(NFS4ERR_BADXDR
);
467 args
->craa_objs_to_keep
= ntohl(*p
++);
468 status
= decode_bitmap(xdr
, bitmap
);
469 if (unlikely(status
))
471 args
->craa_type_mask
= bitmap
[0];
476 static __be32
decode_recallslot_args(struct svc_rqst
*rqstp
,
477 struct xdr_stream
*xdr
,
480 struct cb_recallslotargs
*args
= argp
;
483 p
= read_buf(xdr
, 4);
484 if (unlikely(p
== NULL
))
485 return htonl(NFS4ERR_BADXDR
);
486 args
->crsa_target_highest_slotid
= ntohl(*p
++);
490 static __be32
decode_lockowner(struct xdr_stream
*xdr
, struct cb_notify_lock_args
*args
)
495 p
= read_buf(xdr
, 12);
496 if (unlikely(p
== NULL
))
497 return htonl(NFS4ERR_BADXDR
);
499 p
= xdr_decode_hyper(p
, &args
->cbnl_owner
.clientid
);
500 len
= be32_to_cpu(*p
);
502 p
= read_buf(xdr
, len
);
503 if (unlikely(p
== NULL
))
504 return htonl(NFS4ERR_BADXDR
);
506 /* Only try to decode if the length is right */
508 p
+= 2; /* skip "lock id:" */
509 args
->cbnl_owner
.s_dev
= be32_to_cpu(*p
++);
510 xdr_decode_hyper(p
, &args
->cbnl_owner
.id
);
511 args
->cbnl_valid
= true;
513 args
->cbnl_owner
.s_dev
= 0;
514 args
->cbnl_owner
.id
= 0;
515 args
->cbnl_valid
= false;
520 static __be32
decode_notify_lock_args(struct svc_rqst
*rqstp
,
521 struct xdr_stream
*xdr
, void *argp
)
523 struct cb_notify_lock_args
*args
= argp
;
526 status
= decode_fh(xdr
, &args
->cbnl_fh
);
527 if (unlikely(status
!= 0))
529 return decode_lockowner(xdr
, args
);
532 #endif /* CONFIG_NFS_V4_1 */
533 #ifdef CONFIG_NFS_V4_2
534 static __be32
decode_write_response(struct xdr_stream
*xdr
,
535 struct cb_offloadargs
*args
)
539 /* skip the always zero field */
540 p
= read_buf(xdr
, 4);
545 /* decode count, stable_how, verifier */
546 p
= xdr_inline_decode(xdr
, 8 + 4);
549 p
= xdr_decode_hyper(p
, &args
->wr_count
);
550 args
->wr_writeverf
.committed
= be32_to_cpup(p
);
551 p
= xdr_inline_decode(xdr
, NFS4_VERIFIER_SIZE
);
553 memcpy(&args
->wr_writeverf
.verifier
.data
[0], p
,
558 return htonl(NFS4ERR_RESOURCE
);
561 static __be32
decode_offload_args(struct svc_rqst
*rqstp
,
562 struct xdr_stream
*xdr
,
565 struct cb_offloadargs
*args
= data
;
570 status
= decode_fh(xdr
, &args
->coa_fh
);
571 if (unlikely(status
!= 0))
575 status
= decode_stateid(xdr
, &args
->coa_stateid
);
576 if (unlikely(status
!= 0))
580 p
= read_buf(xdr
, 4);
583 args
->error
= ntohl(*p
++);
585 status
= decode_write_response(xdr
, args
);
586 if (unlikely(status
!= 0))
589 p
= xdr_inline_decode(xdr
, 8);
592 p
= xdr_decode_hyper(p
, &args
->wr_count
);
596 return htonl(NFS4ERR_RESOURCE
);
598 #endif /* CONFIG_NFS_V4_2 */
599 static __be32
encode_string(struct xdr_stream
*xdr
, unsigned int len
, const char *str
)
601 if (unlikely(xdr_stream_encode_opaque(xdr
, str
, len
) < 0))
602 return cpu_to_be32(NFS4ERR_RESOURCE
);
606 static __be32
encode_attr_bitmap(struct xdr_stream
*xdr
, const uint32_t *bitmap
, size_t sz
)
608 if (xdr_stream_encode_uint32_array(xdr
, bitmap
, sz
) < 0)
609 return cpu_to_be32(NFS4ERR_RESOURCE
);
613 static __be32
encode_attr_change(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t change
)
617 if (!(bitmap
[0] & FATTR4_WORD0_CHANGE
))
619 p
= xdr_reserve_space(xdr
, 8);
621 return htonl(NFS4ERR_RESOURCE
);
622 p
= xdr_encode_hyper(p
, change
);
626 static __be32
encode_attr_size(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t size
)
630 if (!(bitmap
[0] & FATTR4_WORD0_SIZE
))
632 p
= xdr_reserve_space(xdr
, 8);
634 return htonl(NFS4ERR_RESOURCE
);
635 p
= xdr_encode_hyper(p
, size
);
639 static __be32
encode_attr_time(struct xdr_stream
*xdr
, const struct timespec
*time
)
643 p
= xdr_reserve_space(xdr
, 12);
645 return htonl(NFS4ERR_RESOURCE
);
646 p
= xdr_encode_hyper(p
, time
->tv_sec
);
647 *p
= htonl(time
->tv_nsec
);
651 static __be32
encode_attr_ctime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
653 if (!(bitmap
[1] & FATTR4_WORD1_TIME_METADATA
))
655 return encode_attr_time(xdr
,time
);
658 static __be32
encode_attr_mtime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
660 if (!(bitmap
[1] & FATTR4_WORD1_TIME_MODIFY
))
662 return encode_attr_time(xdr
,time
);
665 static __be32
encode_compound_hdr_res(struct xdr_stream
*xdr
, struct cb_compound_hdr_res
*hdr
)
669 hdr
->status
= xdr_reserve_space(xdr
, 4);
670 if (unlikely(hdr
->status
== NULL
))
671 return htonl(NFS4ERR_RESOURCE
);
672 status
= encode_string(xdr
, hdr
->taglen
, hdr
->tag
);
673 if (unlikely(status
!= 0))
675 hdr
->nops
= xdr_reserve_space(xdr
, 4);
676 if (unlikely(hdr
->nops
== NULL
))
677 return htonl(NFS4ERR_RESOURCE
);
681 static __be32
encode_op_hdr(struct xdr_stream
*xdr
, uint32_t op
, __be32 res
)
685 p
= xdr_reserve_space(xdr
, 8);
686 if (unlikely(p
== NULL
))
687 return htonl(NFS4ERR_RESOURCE_HDR
);
693 static __be32
encode_getattr_res(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
,
696 const struct cb_getattrres
*res
= resp
;
697 __be32
*savep
= NULL
;
698 __be32 status
= res
->status
;
700 if (unlikely(status
!= 0))
702 status
= encode_attr_bitmap(xdr
, res
->bitmap
, ARRAY_SIZE(res
->bitmap
));
703 if (unlikely(status
!= 0))
705 status
= cpu_to_be32(NFS4ERR_RESOURCE
);
706 savep
= xdr_reserve_space(xdr
, sizeof(*savep
));
707 if (unlikely(!savep
))
709 status
= encode_attr_change(xdr
, res
->bitmap
, res
->change_attr
);
710 if (unlikely(status
!= 0))
712 status
= encode_attr_size(xdr
, res
->bitmap
, res
->size
);
713 if (unlikely(status
!= 0))
715 status
= encode_attr_ctime(xdr
, res
->bitmap
, &res
->ctime
);
716 if (unlikely(status
!= 0))
718 status
= encode_attr_mtime(xdr
, res
->bitmap
, &res
->mtime
);
719 *savep
= htonl((unsigned int)((char *)xdr
->p
- (char *)(savep
+1)));
724 #if defined(CONFIG_NFS_V4_1)
726 static __be32
encode_sessionid(struct xdr_stream
*xdr
,
727 const struct nfs4_sessionid
*sid
)
731 p
= xdr_reserve_space(xdr
, NFS4_MAX_SESSIONID_LEN
);
732 if (unlikely(p
== NULL
))
733 return htonl(NFS4ERR_RESOURCE
);
735 memcpy(p
, sid
, NFS4_MAX_SESSIONID_LEN
);
739 static __be32
encode_cb_sequence_res(struct svc_rqst
*rqstp
,
740 struct xdr_stream
*xdr
,
743 const struct cb_sequenceres
*res
= resp
;
745 __be32 status
= res
->csr_status
;
747 if (unlikely(status
!= 0))
750 status
= encode_sessionid(xdr
, &res
->csr_sessionid
);
754 p
= xdr_reserve_space(xdr
, 4 * sizeof(uint32_t));
755 if (unlikely(p
== NULL
))
756 return htonl(NFS4ERR_RESOURCE
);
758 *p
++ = htonl(res
->csr_sequenceid
);
759 *p
++ = htonl(res
->csr_slotid
);
760 *p
++ = htonl(res
->csr_highestslotid
);
761 *p
++ = htonl(res
->csr_target_highestslotid
);
766 preprocess_nfs41_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
768 if (op_nr
== OP_CB_SEQUENCE
) {
770 return htonl(NFS4ERR_SEQUENCE_POS
);
773 return htonl(NFS4ERR_OP_NOT_IN_SESSION
);
780 case OP_CB_RECALL_ANY
:
781 case OP_CB_RECALL_SLOT
:
782 case OP_CB_LAYOUTRECALL
:
783 case OP_CB_NOTIFY_DEVICEID
:
784 case OP_CB_NOTIFY_LOCK
:
785 *op
= &callback_ops
[op_nr
];
789 case OP_CB_PUSH_DELEG
:
790 case OP_CB_RECALLABLE_OBJ_AVAIL
:
791 case OP_CB_WANTS_CANCELLED
:
792 return htonl(NFS4ERR_NOTSUPP
);
795 return htonl(NFS4ERR_OP_ILLEGAL
);
798 return htonl(NFS_OK
);
801 static void nfs4_callback_free_slot(struct nfs4_session
*session
,
802 struct nfs4_slot
*slot
)
804 struct nfs4_slot_table
*tbl
= &session
->bc_slot_table
;
806 spin_lock(&tbl
->slot_tbl_lock
);
808 * Let the state manager know callback processing done.
809 * A single slot, so highest used slotid is either 0 or -1
811 nfs4_free_slot(tbl
, slot
);
812 spin_unlock(&tbl
->slot_tbl_lock
);
815 static void nfs4_cb_free_slot(struct cb_process_state
*cps
)
818 nfs4_callback_free_slot(cps
->clp
->cl_session
, cps
->slot
);
823 #else /* CONFIG_NFS_V4_1 */
826 preprocess_nfs41_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
828 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
831 static void nfs4_cb_free_slot(struct cb_process_state
*cps
)
834 #endif /* CONFIG_NFS_V4_1 */
836 #ifdef CONFIG_NFS_V4_2
838 preprocess_nfs42_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
840 __be32 status
= preprocess_nfs41_op(nop
, op_nr
, op
);
841 if (status
!= htonl(NFS4ERR_OP_ILLEGAL
))
844 if (op_nr
== OP_CB_OFFLOAD
) {
845 *op
= &callback_ops
[op_nr
];
846 return htonl(NFS_OK
);
848 return htonl(NFS4ERR_NOTSUPP
);
849 return htonl(NFS4ERR_OP_ILLEGAL
);
851 #else /* CONFIG_NFS_V4_2 */
853 preprocess_nfs42_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
855 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
857 #endif /* CONFIG_NFS_V4_2 */
860 preprocess_nfs4_op(unsigned int op_nr
, struct callback_op
**op
)
865 *op
= &callback_ops
[op_nr
];
868 return htonl(NFS4ERR_OP_ILLEGAL
);
871 return htonl(NFS_OK
);
874 static __be32
process_op(int nop
, struct svc_rqst
*rqstp
,
875 struct xdr_stream
*xdr_in
, void *argp
,
876 struct xdr_stream
*xdr_out
, void *resp
,
877 struct cb_process_state
*cps
)
879 struct callback_op
*op
= &callback_ops
[0];
885 status
= decode_op_hdr(xdr_in
, &op_nr
);
886 if (unlikely(status
))
889 switch (cps
->minorversion
) {
891 status
= preprocess_nfs4_op(op_nr
, &op
);
894 status
= preprocess_nfs41_op(nop
, op_nr
, &op
);
897 status
= preprocess_nfs42_op(nop
, op_nr
, &op
);
900 status
= htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
903 if (status
== htonl(NFS4ERR_OP_ILLEGAL
))
904 op_nr
= OP_CB_ILLEGAL
;
908 if (cps
->drc_status
) {
909 status
= cps
->drc_status
;
913 maxlen
= xdr_out
->end
- xdr_out
->p
;
914 if (maxlen
> 0 && maxlen
< PAGE_SIZE
) {
915 status
= op
->decode_args(rqstp
, xdr_in
, argp
);
916 if (likely(status
== 0))
917 status
= op
->process_op(argp
, resp
, cps
);
919 status
= htonl(NFS4ERR_RESOURCE
);
922 res
= encode_op_hdr(xdr_out
, op_nr
, status
);
925 if (op
->encode_res
!= NULL
&& status
== 0)
926 status
= op
->encode_res(rqstp
, xdr_out
, resp
);
931 * Decode, process and encode a COMPOUND
933 static __be32
nfs4_callback_compound(struct svc_rqst
*rqstp
)
935 struct cb_compound_hdr_arg hdr_arg
= { 0 };
936 struct cb_compound_hdr_res hdr_res
= { NULL
};
937 struct xdr_stream xdr_in
, xdr_out
;
939 struct cb_process_state cps
= {
942 .net
= SVC_NET(rqstp
),
944 unsigned int nops
= 0;
946 xdr_init_decode(&xdr_in
, &rqstp
->rq_arg
, rqstp
->rq_arg
.head
[0].iov_base
);
948 p
= (__be32
*)((char *)rqstp
->rq_res
.head
[0].iov_base
+ rqstp
->rq_res
.head
[0].iov_len
);
949 xdr_init_encode(&xdr_out
, &rqstp
->rq_res
, p
);
951 status
= decode_compound_hdr_arg(&xdr_in
, &hdr_arg
);
952 if (status
== htonl(NFS4ERR_RESOURCE
))
953 return rpc_garbage_args
;
955 if (hdr_arg
.minorversion
== 0) {
956 cps
.clp
= nfs4_find_client_ident(SVC_NET(rqstp
), hdr_arg
.cb_ident
);
957 if (!cps
.clp
|| !check_gss_callback_principal(cps
.clp
, rqstp
)) {
959 nfs_put_client(cps
.clp
);
960 goto out_invalidcred
;
964 cps
.minorversion
= hdr_arg
.minorversion
;
965 hdr_res
.taglen
= hdr_arg
.taglen
;
966 hdr_res
.tag
= hdr_arg
.tag
;
967 if (encode_compound_hdr_res(&xdr_out
, &hdr_res
) != 0) {
969 nfs_put_client(cps
.clp
);
970 return rpc_system_err
;
972 while (status
== 0 && nops
!= hdr_arg
.nops
) {
973 status
= process_op(nops
, rqstp
, &xdr_in
,
974 rqstp
->rq_argp
, &xdr_out
, rqstp
->rq_resp
,
979 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
980 * resource error in cb_compound status without returning op */
981 if (unlikely(status
== htonl(NFS4ERR_RESOURCE_HDR
))) {
982 status
= htonl(NFS4ERR_RESOURCE
);
986 *hdr_res
.status
= status
;
987 *hdr_res
.nops
= htonl(nops
);
988 nfs4_cb_free_slot(&cps
);
989 nfs_put_client(cps
.clp
);
993 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
994 return rpc_autherr_badcred
;
998 * Define NFS4 callback COMPOUND ops.
1000 static struct callback_op callback_ops
[] = {
1002 .res_maxsize
= CB_OP_HDR_RES_MAXSZ
,
1005 .process_op
= nfs4_callback_getattr
,
1006 .decode_args
= decode_getattr_args
,
1007 .encode_res
= encode_getattr_res
,
1008 .res_maxsize
= CB_OP_GETATTR_RES_MAXSZ
,
1011 .process_op
= nfs4_callback_recall
,
1012 .decode_args
= decode_recall_args
,
1013 .res_maxsize
= CB_OP_RECALL_RES_MAXSZ
,
1015 #if defined(CONFIG_NFS_V4_1)
1016 [OP_CB_LAYOUTRECALL
] = {
1017 .process_op
= nfs4_callback_layoutrecall
,
1018 .decode_args
= decode_layoutrecall_args
,
1019 .res_maxsize
= CB_OP_LAYOUTRECALL_RES_MAXSZ
,
1021 [OP_CB_NOTIFY_DEVICEID
] = {
1022 .process_op
= nfs4_callback_devicenotify
,
1023 .decode_args
= decode_devicenotify_args
,
1024 .res_maxsize
= CB_OP_DEVICENOTIFY_RES_MAXSZ
,
1026 [OP_CB_SEQUENCE
] = {
1027 .process_op
= nfs4_callback_sequence
,
1028 .decode_args
= decode_cb_sequence_args
,
1029 .encode_res
= encode_cb_sequence_res
,
1030 .res_maxsize
= CB_OP_SEQUENCE_RES_MAXSZ
,
1032 [OP_CB_RECALL_ANY
] = {
1033 .process_op
= nfs4_callback_recallany
,
1034 .decode_args
= decode_recallany_args
,
1035 .res_maxsize
= CB_OP_RECALLANY_RES_MAXSZ
,
1037 [OP_CB_RECALL_SLOT
] = {
1038 .process_op
= nfs4_callback_recallslot
,
1039 .decode_args
= decode_recallslot_args
,
1040 .res_maxsize
= CB_OP_RECALLSLOT_RES_MAXSZ
,
1042 [OP_CB_NOTIFY_LOCK
] = {
1043 .process_op
= nfs4_callback_notify_lock
,
1044 .decode_args
= decode_notify_lock_args
,
1045 .res_maxsize
= CB_OP_NOTIFY_LOCK_RES_MAXSZ
,
1047 #endif /* CONFIG_NFS_V4_1 */
1048 #ifdef CONFIG_NFS_V4_2
1050 .process_op
= nfs4_callback_offload
,
1051 .decode_args
= decode_offload_args
,
1052 .res_maxsize
= CB_OP_OFFLOAD_RES_MAXSZ
,
1054 #endif /* CONFIG_NFS_V4_2 */
1058 * Define NFS4 callback procedures
1060 static const struct svc_procedure nfs4_callback_procedures1
[] = {
1062 .pc_func
= nfs4_callback_null
,
1063 .pc_decode
= nfs4_decode_void
,
1064 .pc_encode
= nfs4_encode_void
,
1068 .pc_func
= nfs4_callback_compound
,
1069 .pc_encode
= nfs4_encode_void
,
1072 .pc_xdrressize
= NFS4_CALLBACK_BUFSIZE
,
1076 static unsigned int nfs4_callback_count1
[ARRAY_SIZE(nfs4_callback_procedures1
)];
1077 const struct svc_version nfs4_callback_version1
= {
1079 .vs_nproc
= ARRAY_SIZE(nfs4_callback_procedures1
),
1080 .vs_proc
= nfs4_callback_procedures1
,
1081 .vs_count
= nfs4_callback_count1
,
1082 .vs_xdrsize
= NFS4_CALLBACK_XDRSIZE
,
1083 .vs_dispatch
= NULL
,
1085 .vs_need_cong_ctrl
= true,
1088 static unsigned int nfs4_callback_count4
[ARRAY_SIZE(nfs4_callback_procedures1
)];
1089 const struct svc_version nfs4_callback_version4
= {
1091 .vs_nproc
= ARRAY_SIZE(nfs4_callback_procedures1
),
1092 .vs_proc
= nfs4_callback_procedures1
,
1093 .vs_count
= nfs4_callback_count4
,
1094 .vs_xdrsize
= NFS4_CALLBACK_XDRSIZE
,
1095 .vs_dispatch
= NULL
,
1097 .vs_need_cong_ctrl
= true,