2 * linux/fs/nfs/callback_xdr.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFSv4 callback encode/decode procedures
8 #include <linux/kernel.h>
9 #include <linux/sunrpc/svc.h>
10 #include <linux/nfs4.h>
11 #include <linux/nfs_fs.h>
12 #include <linux/ratelimit.h>
13 #include <linux/printk.h>
14 #include <linux/slab.h>
15 #include <linux/sunrpc/bc_xprt.h>
19 #include "nfs4session.h"
21 #define CB_OP_TAGLEN_MAXSZ (512)
22 #define CB_OP_HDR_RES_MAXSZ (2 * 4) // opcode, status
23 #define CB_OP_GETATTR_BITMAP_MAXSZ (4 * 4) // bitmap length, 3 bitmaps
24 #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
25 CB_OP_GETATTR_BITMAP_MAXSZ + \
26 /* change, size, ctime, mtime */\
28 #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
30 #if defined(CONFIG_NFS_V4_1)
31 #define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
32 #define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
33 #define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
34 NFS4_MAX_SESSIONID_LEN + \
35 (1 + 3) * 4) // seqid, 3 slotids
36 #define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
37 #define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
38 #define CB_OP_NOTIFY_LOCK_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
39 #endif /* CONFIG_NFS_V4_1 */
41 #define NFSDBG_FACILITY NFSDBG_CALLBACK
43 /* Internal error code */
44 #define NFS4ERR_RESOURCE_HDR 11050
46 typedef __be32 (*callback_process_op_t
)(void *, void *,
47 struct cb_process_state
*);
48 typedef __be32 (*callback_decode_arg_t
)(struct svc_rqst
*, struct xdr_stream
*, void *);
49 typedef __be32 (*callback_encode_res_t
)(struct svc_rqst
*, struct xdr_stream
*, void *);
53 callback_process_op_t process_op
;
54 callback_decode_arg_t decode_args
;
55 callback_encode_res_t encode_res
;
59 static struct callback_op callback_ops
[];
61 static __be32
nfs4_callback_null(struct svc_rqst
*rqstp
, void *argp
, void *resp
)
63 return htonl(NFS4_OK
);
66 static int nfs4_decode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
68 return xdr_argsize_check(rqstp
, p
);
71 static int nfs4_encode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
73 return xdr_ressize_check(rqstp
, p
);
76 static __be32
*read_buf(struct xdr_stream
*xdr
, size_t nbytes
)
80 p
= xdr_inline_decode(xdr
, nbytes
);
81 if (unlikely(p
== NULL
))
82 printk(KERN_WARNING
"NFS: NFSv4 callback reply buffer overflowed!\n");
86 static __be32
decode_string(struct xdr_stream
*xdr
, unsigned int *len
,
87 const char **str
, size_t maxlen
)
91 err
= xdr_stream_decode_opaque_inline(xdr
, (void **)str
, maxlen
);
93 return cpu_to_be32(NFS4ERR_RESOURCE
);
98 static __be32
decode_fh(struct xdr_stream
*xdr
, struct nfs_fh
*fh
)
102 p
= read_buf(xdr
, 4);
103 if (unlikely(p
== NULL
))
104 return htonl(NFS4ERR_RESOURCE
);
105 fh
->size
= ntohl(*p
);
106 if (fh
->size
> NFS4_FHSIZE
)
107 return htonl(NFS4ERR_BADHANDLE
);
108 p
= read_buf(xdr
, fh
->size
);
109 if (unlikely(p
== NULL
))
110 return htonl(NFS4ERR_RESOURCE
);
111 memcpy(&fh
->data
[0], p
, fh
->size
);
112 memset(&fh
->data
[fh
->size
], 0, sizeof(fh
->data
) - fh
->size
);
116 static __be32
decode_bitmap(struct xdr_stream
*xdr
, uint32_t *bitmap
)
119 unsigned int attrlen
;
121 p
= read_buf(xdr
, 4);
122 if (unlikely(p
== NULL
))
123 return htonl(NFS4ERR_RESOURCE
);
125 p
= read_buf(xdr
, attrlen
<< 2);
126 if (unlikely(p
== NULL
))
127 return htonl(NFS4ERR_RESOURCE
);
128 if (likely(attrlen
> 0))
129 bitmap
[0] = ntohl(*p
++);
131 bitmap
[1] = ntohl(*p
);
135 static __be32
decode_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
139 p
= read_buf(xdr
, NFS4_STATEID_SIZE
);
140 if (unlikely(p
== NULL
))
141 return htonl(NFS4ERR_RESOURCE
);
142 memcpy(stateid
->data
, p
, NFS4_STATEID_SIZE
);
146 static __be32
decode_delegation_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
148 stateid
->type
= NFS4_DELEGATION_STATEID_TYPE
;
149 return decode_stateid(xdr
, stateid
);
152 static __be32
decode_compound_hdr_arg(struct xdr_stream
*xdr
, struct cb_compound_hdr_arg
*hdr
)
157 status
= decode_string(xdr
, &hdr
->taglen
, &hdr
->tag
, CB_OP_TAGLEN_MAXSZ
);
158 if (unlikely(status
!= 0))
160 p
= read_buf(xdr
, 12);
161 if (unlikely(p
== NULL
))
162 return htonl(NFS4ERR_RESOURCE
);
163 hdr
->minorversion
= ntohl(*p
++);
164 /* Check for minor version support */
165 if (hdr
->minorversion
<= NFS4_MAX_MINOR_VERSION
) {
166 hdr
->cb_ident
= ntohl(*p
++); /* ignored by v4.1 and v4.2 */
168 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
169 "illegal minor version %u!\n",
170 __func__
, hdr
->minorversion
);
171 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
173 hdr
->nops
= ntohl(*p
);
177 static __be32
decode_op_hdr(struct xdr_stream
*xdr
, unsigned int *op
)
180 p
= read_buf(xdr
, 4);
181 if (unlikely(p
== NULL
))
182 return htonl(NFS4ERR_RESOURCE_HDR
);
187 static __be32
decode_getattr_args(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, struct cb_getattrargs
*args
)
191 status
= decode_fh(xdr
, &args
->fh
);
192 if (unlikely(status
!= 0))
194 return decode_bitmap(xdr
, args
->bitmap
);
197 static __be32
decode_recall_args(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, struct cb_recallargs
*args
)
202 status
= decode_delegation_stateid(xdr
, &args
->stateid
);
203 if (unlikely(status
!= 0))
205 p
= read_buf(xdr
, 4);
206 if (unlikely(p
== NULL
))
207 return htonl(NFS4ERR_RESOURCE
);
208 args
->truncate
= ntohl(*p
);
209 return decode_fh(xdr
, &args
->fh
);
212 #if defined(CONFIG_NFS_V4_1)
213 static __be32
decode_layout_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
215 stateid
->type
= NFS4_LAYOUT_STATEID_TYPE
;
216 return decode_stateid(xdr
, stateid
);
219 static __be32
decode_layoutrecall_args(struct svc_rqst
*rqstp
,
220 struct xdr_stream
*xdr
,
221 struct cb_layoutrecallargs
*args
)
227 p
= read_buf(xdr
, 4 * sizeof(uint32_t));
228 if (unlikely(p
== NULL
))
229 return htonl(NFS4ERR_BADXDR
);
231 args
->cbl_layout_type
= ntohl(*p
++);
232 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
233 * as it is unusable and ignored with the other types.
235 iomode
= ntohl(*p
++);
236 args
->cbl_layoutchanged
= ntohl(*p
++);
237 args
->cbl_recall_type
= ntohl(*p
++);
239 if (args
->cbl_recall_type
== RETURN_FILE
) {
240 args
->cbl_range
.iomode
= iomode
;
241 status
= decode_fh(xdr
, &args
->cbl_fh
);
242 if (unlikely(status
!= 0))
245 p
= read_buf(xdr
, 2 * sizeof(uint64_t));
246 if (unlikely(p
== NULL
))
247 return htonl(NFS4ERR_BADXDR
);
248 p
= xdr_decode_hyper(p
, &args
->cbl_range
.offset
);
249 p
= xdr_decode_hyper(p
, &args
->cbl_range
.length
);
250 return decode_layout_stateid(xdr
, &args
->cbl_stateid
);
251 } else if (args
->cbl_recall_type
== RETURN_FSID
) {
252 p
= read_buf(xdr
, 2 * sizeof(uint64_t));
253 if (unlikely(p
== NULL
))
254 return htonl(NFS4ERR_BADXDR
);
255 p
= xdr_decode_hyper(p
, &args
->cbl_fsid
.major
);
256 p
= xdr_decode_hyper(p
, &args
->cbl_fsid
.minor
);
257 } else if (args
->cbl_recall_type
!= RETURN_ALL
)
258 return htonl(NFS4ERR_BADXDR
);
263 __be32
decode_devicenotify_args(struct svc_rqst
*rqstp
,
264 struct xdr_stream
*xdr
,
265 struct cb_devicenotifyargs
*args
)
273 /* Num of device notifications */
274 p
= read_buf(xdr
, sizeof(uint32_t));
275 if (unlikely(p
== NULL
)) {
276 status
= htonl(NFS4ERR_BADXDR
);
282 if (n
> ULONG_MAX
/ sizeof(*args
->devs
)) {
283 status
= htonl(NFS4ERR_BADXDR
);
287 args
->devs
= kmalloc_array(n
, sizeof(*args
->devs
), GFP_KERNEL
);
289 status
= htonl(NFS4ERR_DELAY
);
293 /* Decode each dev notification */
294 for (i
= 0; i
< n
; i
++) {
295 struct cb_devicenotifyitem
*dev
= &args
->devs
[i
];
297 p
= read_buf(xdr
, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE
);
298 if (unlikely(p
== NULL
)) {
299 status
= htonl(NFS4ERR_BADXDR
);
303 tmp
= ntohl(*p
++); /* bitmap size */
305 status
= htonl(NFS4ERR_INVAL
);
308 dev
->cbd_notify_type
= ntohl(*p
++);
309 if (dev
->cbd_notify_type
!= NOTIFY_DEVICEID4_CHANGE
&&
310 dev
->cbd_notify_type
!= NOTIFY_DEVICEID4_DELETE
) {
311 status
= htonl(NFS4ERR_INVAL
);
315 tmp
= ntohl(*p
++); /* opaque size */
316 if (((dev
->cbd_notify_type
== NOTIFY_DEVICEID4_CHANGE
) &&
317 (tmp
!= NFS4_DEVICEID4_SIZE
+ 8)) ||
318 ((dev
->cbd_notify_type
== NOTIFY_DEVICEID4_DELETE
) &&
319 (tmp
!= NFS4_DEVICEID4_SIZE
+ 4))) {
320 status
= htonl(NFS4ERR_INVAL
);
323 dev
->cbd_layout_type
= ntohl(*p
++);
324 memcpy(dev
->cbd_dev_id
.data
, p
, NFS4_DEVICEID4_SIZE
);
325 p
+= XDR_QUADLEN(NFS4_DEVICEID4_SIZE
);
327 if (dev
->cbd_layout_type
== NOTIFY_DEVICEID4_CHANGE
) {
328 p
= read_buf(xdr
, sizeof(uint32_t));
329 if (unlikely(p
== NULL
)) {
330 status
= htonl(NFS4ERR_BADXDR
);
333 dev
->cbd_immediate
= ntohl(*p
++);
335 dev
->cbd_immediate
= 0;
340 dprintk("%s: type %d layout 0x%x immediate %d\n",
341 __func__
, dev
->cbd_notify_type
, dev
->cbd_layout_type
,
345 dprintk("%s: status %d ndevs %d\n",
346 __func__
, ntohl(status
), args
->ndevs
);
353 static __be32
decode_sessionid(struct xdr_stream
*xdr
,
354 struct nfs4_sessionid
*sid
)
358 p
= read_buf(xdr
, NFS4_MAX_SESSIONID_LEN
);
359 if (unlikely(p
== NULL
))
360 return htonl(NFS4ERR_RESOURCE
);
362 memcpy(sid
->data
, p
, NFS4_MAX_SESSIONID_LEN
);
366 static __be32
decode_rc_list(struct xdr_stream
*xdr
,
367 struct referring_call_list
*rc_list
)
373 status
= decode_sessionid(xdr
, &rc_list
->rcl_sessionid
);
377 status
= htonl(NFS4ERR_RESOURCE
);
378 p
= read_buf(xdr
, sizeof(uint32_t));
379 if (unlikely(p
== NULL
))
382 rc_list
->rcl_nrefcalls
= ntohl(*p
++);
383 if (rc_list
->rcl_nrefcalls
) {
385 rc_list
->rcl_nrefcalls
* 2 * sizeof(uint32_t));
386 if (unlikely(p
== NULL
))
388 rc_list
->rcl_refcalls
= kmalloc_array(rc_list
->rcl_nrefcalls
,
389 sizeof(*rc_list
->rcl_refcalls
),
391 if (unlikely(rc_list
->rcl_refcalls
== NULL
))
393 for (i
= 0; i
< rc_list
->rcl_nrefcalls
; i
++) {
394 rc_list
->rcl_refcalls
[i
].rc_sequenceid
= ntohl(*p
++);
395 rc_list
->rcl_refcalls
[i
].rc_slotid
= ntohl(*p
++);
404 static __be32
decode_cb_sequence_args(struct svc_rqst
*rqstp
,
405 struct xdr_stream
*xdr
,
406 struct cb_sequenceargs
*args
)
412 status
= decode_sessionid(xdr
, &args
->csa_sessionid
);
416 p
= read_buf(xdr
, 5 * sizeof(uint32_t));
417 if (unlikely(p
== NULL
))
418 return htonl(NFS4ERR_RESOURCE
);
420 args
->csa_addr
= svc_addr(rqstp
);
421 args
->csa_sequenceid
= ntohl(*p
++);
422 args
->csa_slotid
= ntohl(*p
++);
423 args
->csa_highestslotid
= ntohl(*p
++);
424 args
->csa_cachethis
= ntohl(*p
++);
425 args
->csa_nrclists
= ntohl(*p
++);
426 args
->csa_rclists
= NULL
;
427 if (args
->csa_nrclists
) {
428 args
->csa_rclists
= kmalloc_array(args
->csa_nrclists
,
429 sizeof(*args
->csa_rclists
),
431 if (unlikely(args
->csa_rclists
== NULL
))
432 return htonl(NFS4ERR_RESOURCE
);
434 for (i
= 0; i
< args
->csa_nrclists
; i
++) {
435 status
= decode_rc_list(xdr
, &args
->csa_rclists
[i
]);
437 args
->csa_nrclists
= i
;
445 for (i
= 0; i
< args
->csa_nrclists
; i
++)
446 kfree(args
->csa_rclists
[i
].rcl_refcalls
);
447 kfree(args
->csa_rclists
);
451 static __be32
decode_recallany_args(struct svc_rqst
*rqstp
,
452 struct xdr_stream
*xdr
,
453 struct cb_recallanyargs
*args
)
458 p
= read_buf(xdr
, 4);
459 if (unlikely(p
== NULL
))
460 return htonl(NFS4ERR_BADXDR
);
461 args
->craa_objs_to_keep
= ntohl(*p
++);
462 status
= decode_bitmap(xdr
, bitmap
);
463 if (unlikely(status
))
465 args
->craa_type_mask
= bitmap
[0];
470 static __be32
decode_recallslot_args(struct svc_rqst
*rqstp
,
471 struct xdr_stream
*xdr
,
472 struct cb_recallslotargs
*args
)
476 p
= read_buf(xdr
, 4);
477 if (unlikely(p
== NULL
))
478 return htonl(NFS4ERR_BADXDR
);
479 args
->crsa_target_highest_slotid
= ntohl(*p
++);
483 static __be32
decode_lockowner(struct xdr_stream
*xdr
, struct cb_notify_lock_args
*args
)
488 p
= read_buf(xdr
, 12);
489 if (unlikely(p
== NULL
))
490 return htonl(NFS4ERR_BADXDR
);
492 p
= xdr_decode_hyper(p
, &args
->cbnl_owner
.clientid
);
493 len
= be32_to_cpu(*p
);
495 p
= read_buf(xdr
, len
);
496 if (unlikely(p
== NULL
))
497 return htonl(NFS4ERR_BADXDR
);
499 /* Only try to decode if the length is right */
501 p
+= 2; /* skip "lock id:" */
502 args
->cbnl_owner
.s_dev
= be32_to_cpu(*p
++);
503 xdr_decode_hyper(p
, &args
->cbnl_owner
.id
);
504 args
->cbnl_valid
= true;
506 args
->cbnl_owner
.s_dev
= 0;
507 args
->cbnl_owner
.id
= 0;
508 args
->cbnl_valid
= false;
513 static __be32
decode_notify_lock_args(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, struct cb_notify_lock_args
*args
)
517 status
= decode_fh(xdr
, &args
->cbnl_fh
);
518 if (unlikely(status
!= 0))
520 return decode_lockowner(xdr
, args
);
523 #endif /* CONFIG_NFS_V4_1 */
525 static __be32
encode_string(struct xdr_stream
*xdr
, unsigned int len
, const char *str
)
527 if (unlikely(xdr_stream_encode_opaque(xdr
, str
, len
) < 0))
528 return cpu_to_be32(NFS4ERR_RESOURCE
);
532 #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
533 #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
534 static __be32
encode_attr_bitmap(struct xdr_stream
*xdr
, const uint32_t *bitmap
, __be32
**savep
)
539 bm
[0] = htonl(bitmap
[0] & CB_SUPPORTED_ATTR0
);
540 bm
[1] = htonl(bitmap
[1] & CB_SUPPORTED_ATTR1
);
542 p
= xdr_reserve_space(xdr
, 16);
543 if (unlikely(p
== NULL
))
544 return htonl(NFS4ERR_RESOURCE
);
548 } else if (bm
[0] != 0) {
549 p
= xdr_reserve_space(xdr
, 12);
550 if (unlikely(p
== NULL
))
551 return htonl(NFS4ERR_RESOURCE
);
555 p
= xdr_reserve_space(xdr
, 8);
556 if (unlikely(p
== NULL
))
557 return htonl(NFS4ERR_RESOURCE
);
564 static __be32
encode_attr_change(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t change
)
568 if (!(bitmap
[0] & FATTR4_WORD0_CHANGE
))
570 p
= xdr_reserve_space(xdr
, 8);
572 return htonl(NFS4ERR_RESOURCE
);
573 p
= xdr_encode_hyper(p
, change
);
577 static __be32
encode_attr_size(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t size
)
581 if (!(bitmap
[0] & FATTR4_WORD0_SIZE
))
583 p
= xdr_reserve_space(xdr
, 8);
585 return htonl(NFS4ERR_RESOURCE
);
586 p
= xdr_encode_hyper(p
, size
);
590 static __be32
encode_attr_time(struct xdr_stream
*xdr
, const struct timespec
*time
)
594 p
= xdr_reserve_space(xdr
, 12);
596 return htonl(NFS4ERR_RESOURCE
);
597 p
= xdr_encode_hyper(p
, time
->tv_sec
);
598 *p
= htonl(time
->tv_nsec
);
602 static __be32
encode_attr_ctime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
604 if (!(bitmap
[1] & FATTR4_WORD1_TIME_METADATA
))
606 return encode_attr_time(xdr
,time
);
609 static __be32
encode_attr_mtime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
611 if (!(bitmap
[1] & FATTR4_WORD1_TIME_MODIFY
))
613 return encode_attr_time(xdr
,time
);
616 static __be32
encode_compound_hdr_res(struct xdr_stream
*xdr
, struct cb_compound_hdr_res
*hdr
)
620 hdr
->status
= xdr_reserve_space(xdr
, 4);
621 if (unlikely(hdr
->status
== NULL
))
622 return htonl(NFS4ERR_RESOURCE
);
623 status
= encode_string(xdr
, hdr
->taglen
, hdr
->tag
);
624 if (unlikely(status
!= 0))
626 hdr
->nops
= xdr_reserve_space(xdr
, 4);
627 if (unlikely(hdr
->nops
== NULL
))
628 return htonl(NFS4ERR_RESOURCE
);
632 static __be32
encode_op_hdr(struct xdr_stream
*xdr
, uint32_t op
, __be32 res
)
636 p
= xdr_reserve_space(xdr
, 8);
637 if (unlikely(p
== NULL
))
638 return htonl(NFS4ERR_RESOURCE_HDR
);
644 static __be32
encode_getattr_res(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, const struct cb_getattrres
*res
)
646 __be32
*savep
= NULL
;
647 __be32 status
= res
->status
;
649 if (unlikely(status
!= 0))
651 status
= encode_attr_bitmap(xdr
, res
->bitmap
, &savep
);
652 if (unlikely(status
!= 0))
654 status
= encode_attr_change(xdr
, res
->bitmap
, res
->change_attr
);
655 if (unlikely(status
!= 0))
657 status
= encode_attr_size(xdr
, res
->bitmap
, res
->size
);
658 if (unlikely(status
!= 0))
660 status
= encode_attr_ctime(xdr
, res
->bitmap
, &res
->ctime
);
661 if (unlikely(status
!= 0))
663 status
= encode_attr_mtime(xdr
, res
->bitmap
, &res
->mtime
);
664 *savep
= htonl((unsigned int)((char *)xdr
->p
- (char *)(savep
+1)));
669 #if defined(CONFIG_NFS_V4_1)
671 static __be32
encode_sessionid(struct xdr_stream
*xdr
,
672 const struct nfs4_sessionid
*sid
)
676 p
= xdr_reserve_space(xdr
, NFS4_MAX_SESSIONID_LEN
);
677 if (unlikely(p
== NULL
))
678 return htonl(NFS4ERR_RESOURCE
);
680 memcpy(p
, sid
, NFS4_MAX_SESSIONID_LEN
);
684 static __be32
encode_cb_sequence_res(struct svc_rqst
*rqstp
,
685 struct xdr_stream
*xdr
,
686 const struct cb_sequenceres
*res
)
689 __be32 status
= res
->csr_status
;
691 if (unlikely(status
!= 0))
694 status
= encode_sessionid(xdr
, &res
->csr_sessionid
);
698 p
= xdr_reserve_space(xdr
, 4 * sizeof(uint32_t));
699 if (unlikely(p
== NULL
))
700 return htonl(NFS4ERR_RESOURCE
);
702 *p
++ = htonl(res
->csr_sequenceid
);
703 *p
++ = htonl(res
->csr_slotid
);
704 *p
++ = htonl(res
->csr_highestslotid
);
705 *p
++ = htonl(res
->csr_target_highestslotid
);
710 preprocess_nfs41_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
712 if (op_nr
== OP_CB_SEQUENCE
) {
714 return htonl(NFS4ERR_SEQUENCE_POS
);
717 return htonl(NFS4ERR_OP_NOT_IN_SESSION
);
724 case OP_CB_RECALL_ANY
:
725 case OP_CB_RECALL_SLOT
:
726 case OP_CB_LAYOUTRECALL
:
727 case OP_CB_NOTIFY_DEVICEID
:
728 case OP_CB_NOTIFY_LOCK
:
729 *op
= &callback_ops
[op_nr
];
733 case OP_CB_PUSH_DELEG
:
734 case OP_CB_RECALLABLE_OBJ_AVAIL
:
735 case OP_CB_WANTS_CANCELLED
:
736 return htonl(NFS4ERR_NOTSUPP
);
739 return htonl(NFS4ERR_OP_ILLEGAL
);
742 return htonl(NFS_OK
);
745 static void nfs4_callback_free_slot(struct nfs4_session
*session
,
746 struct nfs4_slot
*slot
)
748 struct nfs4_slot_table
*tbl
= &session
->bc_slot_table
;
750 spin_lock(&tbl
->slot_tbl_lock
);
752 * Let the state manager know callback processing done.
753 * A single slot, so highest used slotid is either 0 or -1
755 nfs4_free_slot(tbl
, slot
);
756 spin_unlock(&tbl
->slot_tbl_lock
);
759 static void nfs4_cb_free_slot(struct cb_process_state
*cps
)
762 nfs4_callback_free_slot(cps
->clp
->cl_session
, cps
->slot
);
767 #else /* CONFIG_NFS_V4_1 */
770 preprocess_nfs41_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
772 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
775 static void nfs4_cb_free_slot(struct cb_process_state
*cps
)
778 #endif /* CONFIG_NFS_V4_1 */
780 #ifdef CONFIG_NFS_V4_2
782 preprocess_nfs42_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
784 __be32 status
= preprocess_nfs41_op(nop
, op_nr
, op
);
785 if (status
!= htonl(NFS4ERR_OP_ILLEGAL
))
788 if (op_nr
== OP_CB_OFFLOAD
)
789 return htonl(NFS4ERR_NOTSUPP
);
790 return htonl(NFS4ERR_OP_ILLEGAL
);
792 #else /* CONFIG_NFS_V4_2 */
794 preprocess_nfs42_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
796 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
798 #endif /* CONFIG_NFS_V4_2 */
801 preprocess_nfs4_op(unsigned int op_nr
, struct callback_op
**op
)
806 *op
= &callback_ops
[op_nr
];
809 return htonl(NFS4ERR_OP_ILLEGAL
);
812 return htonl(NFS_OK
);
815 static __be32
process_op(int nop
, struct svc_rqst
*rqstp
,
816 struct xdr_stream
*xdr_in
, void *argp
,
817 struct xdr_stream
*xdr_out
, void *resp
,
818 struct cb_process_state
*cps
)
820 struct callback_op
*op
= &callback_ops
[0];
826 status
= decode_op_hdr(xdr_in
, &op_nr
);
827 if (unlikely(status
))
830 switch (cps
->minorversion
) {
832 status
= preprocess_nfs4_op(op_nr
, &op
);
835 status
= preprocess_nfs41_op(nop
, op_nr
, &op
);
838 status
= preprocess_nfs42_op(nop
, op_nr
, &op
);
841 status
= htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
844 if (status
== htonl(NFS4ERR_OP_ILLEGAL
))
845 op_nr
= OP_CB_ILLEGAL
;
849 if (cps
->drc_status
) {
850 status
= cps
->drc_status
;
854 maxlen
= xdr_out
->end
- xdr_out
->p
;
855 if (maxlen
> 0 && maxlen
< PAGE_SIZE
) {
856 status
= op
->decode_args(rqstp
, xdr_in
, argp
);
857 if (likely(status
== 0))
858 status
= op
->process_op(argp
, resp
, cps
);
860 status
= htonl(NFS4ERR_RESOURCE
);
863 res
= encode_op_hdr(xdr_out
, op_nr
, status
);
866 if (op
->encode_res
!= NULL
&& status
== 0)
867 status
= op
->encode_res(rqstp
, xdr_out
, resp
);
872 * Decode, process and encode a COMPOUND
874 static __be32
nfs4_callback_compound(struct svc_rqst
*rqstp
, void *argp
, void *resp
)
876 struct cb_compound_hdr_arg hdr_arg
= { 0 };
877 struct cb_compound_hdr_res hdr_res
= { NULL
};
878 struct xdr_stream xdr_in
, xdr_out
;
880 struct cb_process_state cps
= {
883 .net
= SVC_NET(rqstp
),
885 unsigned int nops
= 0;
887 xdr_init_decode(&xdr_in
, &rqstp
->rq_arg
, rqstp
->rq_arg
.head
[0].iov_base
);
889 p
= (__be32
*)((char *)rqstp
->rq_res
.head
[0].iov_base
+ rqstp
->rq_res
.head
[0].iov_len
);
890 xdr_init_encode(&xdr_out
, &rqstp
->rq_res
, p
);
892 status
= decode_compound_hdr_arg(&xdr_in
, &hdr_arg
);
893 if (status
== htonl(NFS4ERR_RESOURCE
))
894 return rpc_garbage_args
;
896 if (hdr_arg
.minorversion
== 0) {
897 cps
.clp
= nfs4_find_client_ident(SVC_NET(rqstp
), hdr_arg
.cb_ident
);
898 if (!cps
.clp
|| !check_gss_callback_principal(cps
.clp
, rqstp
))
899 goto out_invalidcred
;
902 cps
.minorversion
= hdr_arg
.minorversion
;
903 hdr_res
.taglen
= hdr_arg
.taglen
;
904 hdr_res
.tag
= hdr_arg
.tag
;
905 if (encode_compound_hdr_res(&xdr_out
, &hdr_res
) != 0)
906 return rpc_system_err
;
908 while (status
== 0 && nops
!= hdr_arg
.nops
) {
909 status
= process_op(nops
, rqstp
, &xdr_in
,
910 argp
, &xdr_out
, resp
, &cps
);
914 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
915 * resource error in cb_compound status without returning op */
916 if (unlikely(status
== htonl(NFS4ERR_RESOURCE_HDR
))) {
917 status
= htonl(NFS4ERR_RESOURCE
);
921 *hdr_res
.status
= status
;
922 *hdr_res
.nops
= htonl(nops
);
923 nfs4_cb_free_slot(&cps
);
924 nfs_put_client(cps
.clp
);
928 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
929 return rpc_autherr_badcred
;
933 * Define NFS4 callback COMPOUND ops.
935 static struct callback_op callback_ops
[] = {
937 .res_maxsize
= CB_OP_HDR_RES_MAXSZ
,
940 .process_op
= (callback_process_op_t
)nfs4_callback_getattr
,
941 .decode_args
= (callback_decode_arg_t
)decode_getattr_args
,
942 .encode_res
= (callback_encode_res_t
)encode_getattr_res
,
943 .res_maxsize
= CB_OP_GETATTR_RES_MAXSZ
,
946 .process_op
= (callback_process_op_t
)nfs4_callback_recall
,
947 .decode_args
= (callback_decode_arg_t
)decode_recall_args
,
948 .res_maxsize
= CB_OP_RECALL_RES_MAXSZ
,
950 #if defined(CONFIG_NFS_V4_1)
951 [OP_CB_LAYOUTRECALL
] = {
952 .process_op
= (callback_process_op_t
)nfs4_callback_layoutrecall
,
954 (callback_decode_arg_t
)decode_layoutrecall_args
,
955 .res_maxsize
= CB_OP_LAYOUTRECALL_RES_MAXSZ
,
957 [OP_CB_NOTIFY_DEVICEID
] = {
958 .process_op
= (callback_process_op_t
)nfs4_callback_devicenotify
,
960 (callback_decode_arg_t
)decode_devicenotify_args
,
961 .res_maxsize
= CB_OP_DEVICENOTIFY_RES_MAXSZ
,
964 .process_op
= (callback_process_op_t
)nfs4_callback_sequence
,
965 .decode_args
= (callback_decode_arg_t
)decode_cb_sequence_args
,
966 .encode_res
= (callback_encode_res_t
)encode_cb_sequence_res
,
967 .res_maxsize
= CB_OP_SEQUENCE_RES_MAXSZ
,
969 [OP_CB_RECALL_ANY
] = {
970 .process_op
= (callback_process_op_t
)nfs4_callback_recallany
,
971 .decode_args
= (callback_decode_arg_t
)decode_recallany_args
,
972 .res_maxsize
= CB_OP_RECALLANY_RES_MAXSZ
,
974 [OP_CB_RECALL_SLOT
] = {
975 .process_op
= (callback_process_op_t
)nfs4_callback_recallslot
,
976 .decode_args
= (callback_decode_arg_t
)decode_recallslot_args
,
977 .res_maxsize
= CB_OP_RECALLSLOT_RES_MAXSZ
,
979 [OP_CB_NOTIFY_LOCK
] = {
980 .process_op
= (callback_process_op_t
)nfs4_callback_notify_lock
,
981 .decode_args
= (callback_decode_arg_t
)decode_notify_lock_args
,
982 .res_maxsize
= CB_OP_NOTIFY_LOCK_RES_MAXSZ
,
984 #endif /* CONFIG_NFS_V4_1 */
988 * Define NFS4 callback procedures
990 static struct svc_procedure nfs4_callback_procedures1
[] = {
992 .pc_func
= nfs4_callback_null
,
993 .pc_decode
= (kxdrproc_t
)nfs4_decode_void
,
994 .pc_encode
= (kxdrproc_t
)nfs4_encode_void
,
998 .pc_func
= nfs4_callback_compound
,
999 .pc_encode
= (kxdrproc_t
)nfs4_encode_void
,
1002 .pc_xdrressize
= NFS4_CALLBACK_BUFSIZE
,
1006 struct svc_version nfs4_callback_version1
= {
1008 .vs_nproc
= ARRAY_SIZE(nfs4_callback_procedures1
),
1009 .vs_proc
= nfs4_callback_procedures1
,
1010 .vs_xdrsize
= NFS4_CALLBACK_XDRSIZE
,
1011 .vs_dispatch
= NULL
,
1013 .vs_need_cong_ctrl
= true,
1016 struct svc_version nfs4_callback_version4
= {
1018 .vs_nproc
= ARRAY_SIZE(nfs4_callback_procedures1
),
1019 .vs_proc
= nfs4_callback_procedures1
,
1020 .vs_xdrsize
= NFS4_CALLBACK_XDRSIZE
,
1021 .vs_dispatch
= NULL
,
1023 .vs_need_cong_ctrl
= true,