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
47 __be32 (*process_op
)(void *, void *, struct cb_process_state
*);
48 __be32 (*decode_args
)(struct svc_rqst
*, struct xdr_stream
*, void *);
49 __be32 (*encode_res
)(struct svc_rqst
*, struct xdr_stream
*,
54 static struct callback_op callback_ops
[];
56 static __be32
nfs4_callback_null(struct svc_rqst
*rqstp
)
58 return htonl(NFS4_OK
);
61 static int nfs4_decode_void(struct svc_rqst
*rqstp
, __be32
*p
)
63 return xdr_argsize_check(rqstp
, p
);
66 static int nfs4_encode_void(struct svc_rqst
*rqstp
, __be32
*p
)
68 return xdr_ressize_check(rqstp
, p
);
71 static __be32
*read_buf(struct xdr_stream
*xdr
, size_t nbytes
)
75 p
= xdr_inline_decode(xdr
, nbytes
);
76 if (unlikely(p
== NULL
))
77 printk(KERN_WARNING
"NFS: NFSv4 callback reply buffer overflowed!\n");
81 static __be32
decode_string(struct xdr_stream
*xdr
, unsigned int *len
,
82 const char **str
, size_t maxlen
)
86 err
= xdr_stream_decode_opaque_inline(xdr
, (void **)str
, maxlen
);
88 return cpu_to_be32(NFS4ERR_RESOURCE
);
93 static __be32
decode_fh(struct xdr_stream
*xdr
, struct nfs_fh
*fh
)
98 if (unlikely(p
== NULL
))
99 return htonl(NFS4ERR_RESOURCE
);
100 fh
->size
= ntohl(*p
);
101 if (fh
->size
> NFS4_FHSIZE
)
102 return htonl(NFS4ERR_BADHANDLE
);
103 p
= read_buf(xdr
, fh
->size
);
104 if (unlikely(p
== NULL
))
105 return htonl(NFS4ERR_RESOURCE
);
106 memcpy(&fh
->data
[0], p
, fh
->size
);
107 memset(&fh
->data
[fh
->size
], 0, sizeof(fh
->data
) - fh
->size
);
111 static __be32
decode_bitmap(struct xdr_stream
*xdr
, uint32_t *bitmap
)
114 unsigned int attrlen
;
116 p
= read_buf(xdr
, 4);
117 if (unlikely(p
== NULL
))
118 return htonl(NFS4ERR_RESOURCE
);
120 p
= read_buf(xdr
, attrlen
<< 2);
121 if (unlikely(p
== NULL
))
122 return htonl(NFS4ERR_RESOURCE
);
123 if (likely(attrlen
> 0))
124 bitmap
[0] = ntohl(*p
++);
126 bitmap
[1] = ntohl(*p
);
130 static __be32
decode_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
134 p
= read_buf(xdr
, NFS4_STATEID_SIZE
);
135 if (unlikely(p
== NULL
))
136 return htonl(NFS4ERR_RESOURCE
);
137 memcpy(stateid
->data
, p
, NFS4_STATEID_SIZE
);
141 static __be32
decode_delegation_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
143 stateid
->type
= NFS4_DELEGATION_STATEID_TYPE
;
144 return decode_stateid(xdr
, stateid
);
147 static __be32
decode_compound_hdr_arg(struct xdr_stream
*xdr
, struct cb_compound_hdr_arg
*hdr
)
152 status
= decode_string(xdr
, &hdr
->taglen
, &hdr
->tag
, CB_OP_TAGLEN_MAXSZ
);
153 if (unlikely(status
!= 0))
155 p
= read_buf(xdr
, 12);
156 if (unlikely(p
== NULL
))
157 return htonl(NFS4ERR_RESOURCE
);
158 hdr
->minorversion
= ntohl(*p
++);
159 /* Check for minor version support */
160 if (hdr
->minorversion
<= NFS4_MAX_MINOR_VERSION
) {
161 hdr
->cb_ident
= ntohl(*p
++); /* ignored by v4.1 and v4.2 */
163 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
164 "illegal minor version %u!\n",
165 __func__
, hdr
->minorversion
);
166 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
168 hdr
->nops
= ntohl(*p
);
172 static __be32
decode_op_hdr(struct xdr_stream
*xdr
, unsigned int *op
)
175 p
= read_buf(xdr
, 4);
176 if (unlikely(p
== NULL
))
177 return htonl(NFS4ERR_RESOURCE_HDR
);
182 static __be32
decode_getattr_args(struct svc_rqst
*rqstp
,
183 struct xdr_stream
*xdr
, void *argp
)
185 struct cb_getattrargs
*args
= argp
;
188 status
= decode_fh(xdr
, &args
->fh
);
189 if (unlikely(status
!= 0))
191 return decode_bitmap(xdr
, args
->bitmap
);
194 static __be32
decode_recall_args(struct svc_rqst
*rqstp
,
195 struct xdr_stream
*xdr
, void *argp
)
197 struct cb_recallargs
*args
= argp
;
201 status
= decode_delegation_stateid(xdr
, &args
->stateid
);
202 if (unlikely(status
!= 0))
204 p
= read_buf(xdr
, 4);
205 if (unlikely(p
== NULL
))
206 return htonl(NFS4ERR_RESOURCE
);
207 args
->truncate
= ntohl(*p
);
208 return decode_fh(xdr
, &args
->fh
);
211 #if defined(CONFIG_NFS_V4_1)
212 static __be32
decode_layout_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
214 stateid
->type
= NFS4_LAYOUT_STATEID_TYPE
;
215 return decode_stateid(xdr
, stateid
);
218 static __be32
decode_layoutrecall_args(struct svc_rqst
*rqstp
,
219 struct xdr_stream
*xdr
, void *argp
)
221 struct cb_layoutrecallargs
*args
= argp
;
226 p
= read_buf(xdr
, 4 * sizeof(uint32_t));
227 if (unlikely(p
== NULL
))
228 return htonl(NFS4ERR_BADXDR
);
230 args
->cbl_layout_type
= ntohl(*p
++);
231 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
232 * as it is unusable and ignored with the other types.
234 iomode
= ntohl(*p
++);
235 args
->cbl_layoutchanged
= ntohl(*p
++);
236 args
->cbl_recall_type
= ntohl(*p
++);
238 if (args
->cbl_recall_type
== RETURN_FILE
) {
239 args
->cbl_range
.iomode
= iomode
;
240 status
= decode_fh(xdr
, &args
->cbl_fh
);
241 if (unlikely(status
!= 0))
244 p
= read_buf(xdr
, 2 * sizeof(uint64_t));
245 if (unlikely(p
== NULL
))
246 return htonl(NFS4ERR_BADXDR
);
247 p
= xdr_decode_hyper(p
, &args
->cbl_range
.offset
);
248 p
= xdr_decode_hyper(p
, &args
->cbl_range
.length
);
249 return decode_layout_stateid(xdr
, &args
->cbl_stateid
);
250 } else if (args
->cbl_recall_type
== RETURN_FSID
) {
251 p
= read_buf(xdr
, 2 * sizeof(uint64_t));
252 if (unlikely(p
== NULL
))
253 return htonl(NFS4ERR_BADXDR
);
254 p
= xdr_decode_hyper(p
, &args
->cbl_fsid
.major
);
255 p
= xdr_decode_hyper(p
, &args
->cbl_fsid
.minor
);
256 } else if (args
->cbl_recall_type
!= RETURN_ALL
)
257 return htonl(NFS4ERR_BADXDR
);
262 __be32
decode_devicenotify_args(struct svc_rqst
*rqstp
,
263 struct xdr_stream
*xdr
,
266 struct cb_devicenotifyargs
*args
= argp
;
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
,
408 struct cb_sequenceargs
*args
= argp
;
413 status
= decode_sessionid(xdr
, &args
->csa_sessionid
);
417 p
= read_buf(xdr
, 5 * sizeof(uint32_t));
418 if (unlikely(p
== NULL
))
419 return htonl(NFS4ERR_RESOURCE
);
421 args
->csa_addr
= svc_addr(rqstp
);
422 args
->csa_sequenceid
= ntohl(*p
++);
423 args
->csa_slotid
= ntohl(*p
++);
424 args
->csa_highestslotid
= ntohl(*p
++);
425 args
->csa_cachethis
= ntohl(*p
++);
426 args
->csa_nrclists
= ntohl(*p
++);
427 args
->csa_rclists
= NULL
;
428 if (args
->csa_nrclists
) {
429 args
->csa_rclists
= kmalloc_array(args
->csa_nrclists
,
430 sizeof(*args
->csa_rclists
),
432 if (unlikely(args
->csa_rclists
== NULL
))
433 return htonl(NFS4ERR_RESOURCE
);
435 for (i
= 0; i
< args
->csa_nrclists
; i
++) {
436 status
= decode_rc_list(xdr
, &args
->csa_rclists
[i
]);
438 args
->csa_nrclists
= i
;
446 for (i
= 0; i
< args
->csa_nrclists
; i
++)
447 kfree(args
->csa_rclists
[i
].rcl_refcalls
);
448 kfree(args
->csa_rclists
);
452 static __be32
decode_recallany_args(struct svc_rqst
*rqstp
,
453 struct xdr_stream
*xdr
,
456 struct cb_recallanyargs
*args
= argp
;
460 p
= read_buf(xdr
, 4);
461 if (unlikely(p
== NULL
))
462 return htonl(NFS4ERR_BADXDR
);
463 args
->craa_objs_to_keep
= ntohl(*p
++);
464 status
= decode_bitmap(xdr
, bitmap
);
465 if (unlikely(status
))
467 args
->craa_type_mask
= bitmap
[0];
472 static __be32
decode_recallslot_args(struct svc_rqst
*rqstp
,
473 struct xdr_stream
*xdr
,
476 struct cb_recallslotargs
*args
= argp
;
479 p
= read_buf(xdr
, 4);
480 if (unlikely(p
== NULL
))
481 return htonl(NFS4ERR_BADXDR
);
482 args
->crsa_target_highest_slotid
= ntohl(*p
++);
486 static __be32
decode_lockowner(struct xdr_stream
*xdr
, struct cb_notify_lock_args
*args
)
491 p
= read_buf(xdr
, 12);
492 if (unlikely(p
== NULL
))
493 return htonl(NFS4ERR_BADXDR
);
495 p
= xdr_decode_hyper(p
, &args
->cbnl_owner
.clientid
);
496 len
= be32_to_cpu(*p
);
498 p
= read_buf(xdr
, len
);
499 if (unlikely(p
== NULL
))
500 return htonl(NFS4ERR_BADXDR
);
502 /* Only try to decode if the length is right */
504 p
+= 2; /* skip "lock id:" */
505 args
->cbnl_owner
.s_dev
= be32_to_cpu(*p
++);
506 xdr_decode_hyper(p
, &args
->cbnl_owner
.id
);
507 args
->cbnl_valid
= true;
509 args
->cbnl_owner
.s_dev
= 0;
510 args
->cbnl_owner
.id
= 0;
511 args
->cbnl_valid
= false;
516 static __be32
decode_notify_lock_args(struct svc_rqst
*rqstp
,
517 struct xdr_stream
*xdr
, void *argp
)
519 struct cb_notify_lock_args
*args
= argp
;
522 status
= decode_fh(xdr
, &args
->cbnl_fh
);
523 if (unlikely(status
!= 0))
525 return decode_lockowner(xdr
, args
);
528 #endif /* CONFIG_NFS_V4_1 */
530 static __be32
encode_string(struct xdr_stream
*xdr
, unsigned int len
, const char *str
)
532 if (unlikely(xdr_stream_encode_opaque(xdr
, str
, len
) < 0))
533 return cpu_to_be32(NFS4ERR_RESOURCE
);
537 #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
538 #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
539 static __be32
encode_attr_bitmap(struct xdr_stream
*xdr
, const uint32_t *bitmap
, __be32
**savep
)
544 bm
[0] = htonl(bitmap
[0] & CB_SUPPORTED_ATTR0
);
545 bm
[1] = htonl(bitmap
[1] & CB_SUPPORTED_ATTR1
);
547 p
= xdr_reserve_space(xdr
, 16);
548 if (unlikely(p
== NULL
))
549 return htonl(NFS4ERR_RESOURCE
);
553 } else if (bm
[0] != 0) {
554 p
= xdr_reserve_space(xdr
, 12);
555 if (unlikely(p
== NULL
))
556 return htonl(NFS4ERR_RESOURCE
);
560 p
= xdr_reserve_space(xdr
, 8);
561 if (unlikely(p
== NULL
))
562 return htonl(NFS4ERR_RESOURCE
);
569 static __be32
encode_attr_change(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t change
)
573 if (!(bitmap
[0] & FATTR4_WORD0_CHANGE
))
575 p
= xdr_reserve_space(xdr
, 8);
577 return htonl(NFS4ERR_RESOURCE
);
578 p
= xdr_encode_hyper(p
, change
);
582 static __be32
encode_attr_size(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t size
)
586 if (!(bitmap
[0] & FATTR4_WORD0_SIZE
))
588 p
= xdr_reserve_space(xdr
, 8);
590 return htonl(NFS4ERR_RESOURCE
);
591 p
= xdr_encode_hyper(p
, size
);
595 static __be32
encode_attr_time(struct xdr_stream
*xdr
, const struct timespec
*time
)
599 p
= xdr_reserve_space(xdr
, 12);
601 return htonl(NFS4ERR_RESOURCE
);
602 p
= xdr_encode_hyper(p
, time
->tv_sec
);
603 *p
= htonl(time
->tv_nsec
);
607 static __be32
encode_attr_ctime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
609 if (!(bitmap
[1] & FATTR4_WORD1_TIME_METADATA
))
611 return encode_attr_time(xdr
,time
);
614 static __be32
encode_attr_mtime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
616 if (!(bitmap
[1] & FATTR4_WORD1_TIME_MODIFY
))
618 return encode_attr_time(xdr
,time
);
621 static __be32
encode_compound_hdr_res(struct xdr_stream
*xdr
, struct cb_compound_hdr_res
*hdr
)
625 hdr
->status
= xdr_reserve_space(xdr
, 4);
626 if (unlikely(hdr
->status
== NULL
))
627 return htonl(NFS4ERR_RESOURCE
);
628 status
= encode_string(xdr
, hdr
->taglen
, hdr
->tag
);
629 if (unlikely(status
!= 0))
631 hdr
->nops
= xdr_reserve_space(xdr
, 4);
632 if (unlikely(hdr
->nops
== NULL
))
633 return htonl(NFS4ERR_RESOURCE
);
637 static __be32
encode_op_hdr(struct xdr_stream
*xdr
, uint32_t op
, __be32 res
)
641 p
= xdr_reserve_space(xdr
, 8);
642 if (unlikely(p
== NULL
))
643 return htonl(NFS4ERR_RESOURCE_HDR
);
649 static __be32
encode_getattr_res(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
,
652 const struct cb_getattrres
*res
= resp
;
653 __be32
*savep
= NULL
;
654 __be32 status
= res
->status
;
656 if (unlikely(status
!= 0))
658 status
= encode_attr_bitmap(xdr
, res
->bitmap
, &savep
);
659 if (unlikely(status
!= 0))
661 status
= encode_attr_change(xdr
, res
->bitmap
, res
->change_attr
);
662 if (unlikely(status
!= 0))
664 status
= encode_attr_size(xdr
, res
->bitmap
, res
->size
);
665 if (unlikely(status
!= 0))
667 status
= encode_attr_ctime(xdr
, res
->bitmap
, &res
->ctime
);
668 if (unlikely(status
!= 0))
670 status
= encode_attr_mtime(xdr
, res
->bitmap
, &res
->mtime
);
671 *savep
= htonl((unsigned int)((char *)xdr
->p
- (char *)(savep
+1)));
676 #if defined(CONFIG_NFS_V4_1)
678 static __be32
encode_sessionid(struct xdr_stream
*xdr
,
679 const struct nfs4_sessionid
*sid
)
683 p
= xdr_reserve_space(xdr
, NFS4_MAX_SESSIONID_LEN
);
684 if (unlikely(p
== NULL
))
685 return htonl(NFS4ERR_RESOURCE
);
687 memcpy(p
, sid
, NFS4_MAX_SESSIONID_LEN
);
691 static __be32
encode_cb_sequence_res(struct svc_rqst
*rqstp
,
692 struct xdr_stream
*xdr
,
695 const struct cb_sequenceres
*res
= resp
;
697 __be32 status
= res
->csr_status
;
699 if (unlikely(status
!= 0))
702 status
= encode_sessionid(xdr
, &res
->csr_sessionid
);
706 p
= xdr_reserve_space(xdr
, 4 * sizeof(uint32_t));
707 if (unlikely(p
== NULL
))
708 return htonl(NFS4ERR_RESOURCE
);
710 *p
++ = htonl(res
->csr_sequenceid
);
711 *p
++ = htonl(res
->csr_slotid
);
712 *p
++ = htonl(res
->csr_highestslotid
);
713 *p
++ = htonl(res
->csr_target_highestslotid
);
718 preprocess_nfs41_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
720 if (op_nr
== OP_CB_SEQUENCE
) {
722 return htonl(NFS4ERR_SEQUENCE_POS
);
725 return htonl(NFS4ERR_OP_NOT_IN_SESSION
);
732 case OP_CB_RECALL_ANY
:
733 case OP_CB_RECALL_SLOT
:
734 case OP_CB_LAYOUTRECALL
:
735 case OP_CB_NOTIFY_DEVICEID
:
736 case OP_CB_NOTIFY_LOCK
:
737 *op
= &callback_ops
[op_nr
];
741 case OP_CB_PUSH_DELEG
:
742 case OP_CB_RECALLABLE_OBJ_AVAIL
:
743 case OP_CB_WANTS_CANCELLED
:
744 return htonl(NFS4ERR_NOTSUPP
);
747 return htonl(NFS4ERR_OP_ILLEGAL
);
750 return htonl(NFS_OK
);
753 static void nfs4_callback_free_slot(struct nfs4_session
*session
,
754 struct nfs4_slot
*slot
)
756 struct nfs4_slot_table
*tbl
= &session
->bc_slot_table
;
758 spin_lock(&tbl
->slot_tbl_lock
);
760 * Let the state manager know callback processing done.
761 * A single slot, so highest used slotid is either 0 or -1
763 nfs4_free_slot(tbl
, slot
);
764 spin_unlock(&tbl
->slot_tbl_lock
);
767 static void nfs4_cb_free_slot(struct cb_process_state
*cps
)
770 nfs4_callback_free_slot(cps
->clp
->cl_session
, cps
->slot
);
775 #else /* CONFIG_NFS_V4_1 */
778 preprocess_nfs41_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
780 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
783 static void nfs4_cb_free_slot(struct cb_process_state
*cps
)
786 #endif /* CONFIG_NFS_V4_1 */
788 #ifdef CONFIG_NFS_V4_2
790 preprocess_nfs42_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
792 __be32 status
= preprocess_nfs41_op(nop
, op_nr
, op
);
793 if (status
!= htonl(NFS4ERR_OP_ILLEGAL
))
796 if (op_nr
== OP_CB_OFFLOAD
)
797 return htonl(NFS4ERR_NOTSUPP
);
798 return htonl(NFS4ERR_OP_ILLEGAL
);
800 #else /* CONFIG_NFS_V4_2 */
802 preprocess_nfs42_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
804 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
806 #endif /* CONFIG_NFS_V4_2 */
809 preprocess_nfs4_op(unsigned int op_nr
, struct callback_op
**op
)
814 *op
= &callback_ops
[op_nr
];
817 return htonl(NFS4ERR_OP_ILLEGAL
);
820 return htonl(NFS_OK
);
823 static __be32
process_op(int nop
, struct svc_rqst
*rqstp
,
824 struct xdr_stream
*xdr_in
, void *argp
,
825 struct xdr_stream
*xdr_out
, void *resp
,
826 struct cb_process_state
*cps
)
828 struct callback_op
*op
= &callback_ops
[0];
834 status
= decode_op_hdr(xdr_in
, &op_nr
);
835 if (unlikely(status
))
838 switch (cps
->minorversion
) {
840 status
= preprocess_nfs4_op(op_nr
, &op
);
843 status
= preprocess_nfs41_op(nop
, op_nr
, &op
);
846 status
= preprocess_nfs42_op(nop
, op_nr
, &op
);
849 status
= htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
852 if (status
== htonl(NFS4ERR_OP_ILLEGAL
))
853 op_nr
= OP_CB_ILLEGAL
;
857 if (cps
->drc_status
) {
858 status
= cps
->drc_status
;
862 maxlen
= xdr_out
->end
- xdr_out
->p
;
863 if (maxlen
> 0 && maxlen
< PAGE_SIZE
) {
864 status
= op
->decode_args(rqstp
, xdr_in
, argp
);
865 if (likely(status
== 0))
866 status
= op
->process_op(argp
, resp
, cps
);
868 status
= htonl(NFS4ERR_RESOURCE
);
871 res
= encode_op_hdr(xdr_out
, op_nr
, status
);
874 if (op
->encode_res
!= NULL
&& status
== 0)
875 status
= op
->encode_res(rqstp
, xdr_out
, resp
);
880 * Decode, process and encode a COMPOUND
882 static __be32
nfs4_callback_compound(struct svc_rqst
*rqstp
)
884 struct cb_compound_hdr_arg hdr_arg
= { 0 };
885 struct cb_compound_hdr_res hdr_res
= { NULL
};
886 struct xdr_stream xdr_in
, xdr_out
;
888 struct cb_process_state cps
= {
891 .net
= SVC_NET(rqstp
),
893 unsigned int nops
= 0;
895 xdr_init_decode(&xdr_in
, &rqstp
->rq_arg
, rqstp
->rq_arg
.head
[0].iov_base
);
897 p
= (__be32
*)((char *)rqstp
->rq_res
.head
[0].iov_base
+ rqstp
->rq_res
.head
[0].iov_len
);
898 xdr_init_encode(&xdr_out
, &rqstp
->rq_res
, p
);
900 status
= decode_compound_hdr_arg(&xdr_in
, &hdr_arg
);
901 if (status
== htonl(NFS4ERR_RESOURCE
))
902 return rpc_garbage_args
;
904 if (hdr_arg
.minorversion
== 0) {
905 cps
.clp
= nfs4_find_client_ident(SVC_NET(rqstp
), hdr_arg
.cb_ident
);
906 if (!cps
.clp
|| !check_gss_callback_principal(cps
.clp
, rqstp
))
907 goto out_invalidcred
;
910 cps
.minorversion
= hdr_arg
.minorversion
;
911 hdr_res
.taglen
= hdr_arg
.taglen
;
912 hdr_res
.tag
= hdr_arg
.tag
;
913 if (encode_compound_hdr_res(&xdr_out
, &hdr_res
) != 0)
914 return rpc_system_err
;
916 while (status
== 0 && nops
!= hdr_arg
.nops
) {
917 status
= process_op(nops
, rqstp
, &xdr_in
,
918 rqstp
->rq_argp
, &xdr_out
, rqstp
->rq_resp
,
923 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
924 * resource error in cb_compound status without returning op */
925 if (unlikely(status
== htonl(NFS4ERR_RESOURCE_HDR
))) {
926 status
= htonl(NFS4ERR_RESOURCE
);
930 *hdr_res
.status
= status
;
931 *hdr_res
.nops
= htonl(nops
);
932 nfs4_cb_free_slot(&cps
);
933 nfs_put_client(cps
.clp
);
937 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
938 return rpc_autherr_badcred
;
942 * Define NFS4 callback COMPOUND ops.
944 static struct callback_op callback_ops
[] = {
946 .res_maxsize
= CB_OP_HDR_RES_MAXSZ
,
949 .process_op
= nfs4_callback_getattr
,
950 .decode_args
= decode_getattr_args
,
951 .encode_res
= encode_getattr_res
,
952 .res_maxsize
= CB_OP_GETATTR_RES_MAXSZ
,
955 .process_op
= nfs4_callback_recall
,
956 .decode_args
= decode_recall_args
,
957 .res_maxsize
= CB_OP_RECALL_RES_MAXSZ
,
959 #if defined(CONFIG_NFS_V4_1)
960 [OP_CB_LAYOUTRECALL
] = {
961 .process_op
= nfs4_callback_layoutrecall
,
962 .decode_args
= decode_layoutrecall_args
,
963 .res_maxsize
= CB_OP_LAYOUTRECALL_RES_MAXSZ
,
965 [OP_CB_NOTIFY_DEVICEID
] = {
966 .process_op
= nfs4_callback_devicenotify
,
967 .decode_args
= decode_devicenotify_args
,
968 .res_maxsize
= CB_OP_DEVICENOTIFY_RES_MAXSZ
,
971 .process_op
= nfs4_callback_sequence
,
972 .decode_args
= decode_cb_sequence_args
,
973 .encode_res
= encode_cb_sequence_res
,
974 .res_maxsize
= CB_OP_SEQUENCE_RES_MAXSZ
,
976 [OP_CB_RECALL_ANY
] = {
977 .process_op
= nfs4_callback_recallany
,
978 .decode_args
= decode_recallany_args
,
979 .res_maxsize
= CB_OP_RECALLANY_RES_MAXSZ
,
981 [OP_CB_RECALL_SLOT
] = {
982 .process_op
= nfs4_callback_recallslot
,
983 .decode_args
= decode_recallslot_args
,
984 .res_maxsize
= CB_OP_RECALLSLOT_RES_MAXSZ
,
986 [OP_CB_NOTIFY_LOCK
] = {
987 .process_op
= nfs4_callback_notify_lock
,
988 .decode_args
= decode_notify_lock_args
,
989 .res_maxsize
= CB_OP_NOTIFY_LOCK_RES_MAXSZ
,
991 #endif /* CONFIG_NFS_V4_1 */
995 * Define NFS4 callback procedures
997 static const struct svc_procedure nfs4_callback_procedures1
[] = {
999 .pc_func
= nfs4_callback_null
,
1000 .pc_decode
= nfs4_decode_void
,
1001 .pc_encode
= nfs4_encode_void
,
1005 .pc_func
= nfs4_callback_compound
,
1006 .pc_encode
= nfs4_encode_void
,
1009 .pc_xdrressize
= NFS4_CALLBACK_BUFSIZE
,
1013 static unsigned int nfs4_callback_count1
[ARRAY_SIZE(nfs4_callback_procedures1
)];
1014 const struct svc_version nfs4_callback_version1
= {
1016 .vs_nproc
= ARRAY_SIZE(nfs4_callback_procedures1
),
1017 .vs_proc
= nfs4_callback_procedures1
,
1018 .vs_count
= nfs4_callback_count1
,
1019 .vs_xdrsize
= NFS4_CALLBACK_XDRSIZE
,
1020 .vs_dispatch
= NULL
,
1022 .vs_need_cong_ctrl
= true,
1025 static unsigned int nfs4_callback_count4
[ARRAY_SIZE(nfs4_callback_procedures1
)];
1026 const struct svc_version nfs4_callback_version4
= {
1028 .vs_nproc
= ARRAY_SIZE(nfs4_callback_procedures1
),
1029 .vs_proc
= nfs4_callback_procedures1
,
1030 .vs_count
= nfs4_callback_count4
,
1031 .vs_xdrsize
= NFS4_CALLBACK_XDRSIZE
,
1032 .vs_dispatch
= NULL
,
1034 .vs_need_cong_ctrl
= true,