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>
15 #define CB_OP_TAGLEN_MAXSZ (512)
16 #define CB_OP_HDR_RES_MAXSZ (2 + CB_OP_TAGLEN_MAXSZ)
17 #define CB_OP_GETATTR_BITMAP_MAXSZ (4)
18 #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
19 CB_OP_GETATTR_BITMAP_MAXSZ + \
21 #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
23 #if defined(CONFIG_NFS_V4_1)
24 #define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
26 #endif /* CONFIG_NFS_V4_1 */
28 #define NFSDBG_FACILITY NFSDBG_CALLBACK
30 typedef __be32 (*callback_process_op_t
)(void *, void *);
31 typedef __be32 (*callback_decode_arg_t
)(struct svc_rqst
*, struct xdr_stream
*, void *);
32 typedef __be32 (*callback_encode_res_t
)(struct svc_rqst
*, struct xdr_stream
*, void *);
36 callback_process_op_t process_op
;
37 callback_decode_arg_t decode_args
;
38 callback_encode_res_t encode_res
;
42 static struct callback_op callback_ops
[];
44 static __be32
nfs4_callback_null(struct svc_rqst
*rqstp
, void *argp
, void *resp
)
46 return htonl(NFS4_OK
);
49 static int nfs4_decode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
51 return xdr_argsize_check(rqstp
, p
);
54 static int nfs4_encode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
56 return xdr_ressize_check(rqstp
, p
);
59 static __be32
*read_buf(struct xdr_stream
*xdr
, int nbytes
)
63 p
= xdr_inline_decode(xdr
, nbytes
);
64 if (unlikely(p
== NULL
))
65 printk(KERN_WARNING
"NFSv4 callback reply buffer overflowed!\n");
69 static __be32
decode_string(struct xdr_stream
*xdr
, unsigned int *len
, const char **str
)
74 if (unlikely(p
== NULL
))
75 return htonl(NFS4ERR_RESOURCE
);
79 p
= read_buf(xdr
, *len
);
80 if (unlikely(p
== NULL
))
81 return htonl(NFS4ERR_RESOURCE
);
82 *str
= (const char *)p
;
89 static __be32
decode_fh(struct xdr_stream
*xdr
, struct nfs_fh
*fh
)
94 if (unlikely(p
== NULL
))
95 return htonl(NFS4ERR_RESOURCE
);
97 if (fh
->size
> NFS4_FHSIZE
)
98 return htonl(NFS4ERR_BADHANDLE
);
99 p
= read_buf(xdr
, fh
->size
);
100 if (unlikely(p
== NULL
))
101 return htonl(NFS4ERR_RESOURCE
);
102 memcpy(&fh
->data
[0], p
, fh
->size
);
103 memset(&fh
->data
[fh
->size
], 0, sizeof(fh
->data
) - fh
->size
);
107 static __be32
decode_bitmap(struct xdr_stream
*xdr
, uint32_t *bitmap
)
110 unsigned int attrlen
;
112 p
= read_buf(xdr
, 4);
113 if (unlikely(p
== NULL
))
114 return htonl(NFS4ERR_RESOURCE
);
116 p
= read_buf(xdr
, attrlen
<< 2);
117 if (unlikely(p
== NULL
))
118 return htonl(NFS4ERR_RESOURCE
);
119 if (likely(attrlen
> 0))
120 bitmap
[0] = ntohl(*p
++);
122 bitmap
[1] = ntohl(*p
);
126 static __be32
decode_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
130 p
= read_buf(xdr
, 16);
131 if (unlikely(p
== NULL
))
132 return htonl(NFS4ERR_RESOURCE
);
133 memcpy(stateid
->data
, p
, 16);
137 static __be32
decode_compound_hdr_arg(struct xdr_stream
*xdr
, struct cb_compound_hdr_arg
*hdr
)
142 status
= decode_string(xdr
, &hdr
->taglen
, &hdr
->tag
);
143 if (unlikely(status
!= 0))
145 /* We do not like overly long tags! */
146 if (hdr
->taglen
> CB_OP_TAGLEN_MAXSZ
- 12) {
147 printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
148 __func__
, hdr
->taglen
);
149 return htonl(NFS4ERR_RESOURCE
);
151 p
= read_buf(xdr
, 12);
152 if (unlikely(p
== NULL
))
153 return htonl(NFS4ERR_RESOURCE
);
154 hdr
->minorversion
= ntohl(*p
++);
155 /* Check minor version is zero or one. */
156 if (hdr
->minorversion
<= 1) {
157 p
++; /* skip callback_ident */
159 printk(KERN_WARNING
"%s: NFSv4 server callback with "
160 "illegal minor version %u!\n",
161 __func__
, hdr
->minorversion
);
162 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
164 hdr
->nops
= ntohl(*p
);
165 dprintk("%s: minorversion %d nops %d\n", __func__
,
166 hdr
->minorversion
, hdr
->nops
);
170 static __be32
decode_op_hdr(struct xdr_stream
*xdr
, unsigned int *op
)
173 p
= read_buf(xdr
, 4);
174 if (unlikely(p
== NULL
))
175 return htonl(NFS4ERR_RESOURCE
);
180 static __be32
decode_getattr_args(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, struct cb_getattrargs
*args
)
184 status
= decode_fh(xdr
, &args
->fh
);
185 if (unlikely(status
!= 0))
187 args
->addr
= svc_addr(rqstp
);
188 status
= decode_bitmap(xdr
, args
->bitmap
);
190 dprintk("%s: exit with status = %d\n", __func__
, ntohl(status
));
194 static __be32
decode_recall_args(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, struct cb_recallargs
*args
)
199 args
->addr
= svc_addr(rqstp
);
200 status
= decode_stateid(xdr
, &args
->stateid
);
201 if (unlikely(status
!= 0))
203 p
= read_buf(xdr
, 4);
204 if (unlikely(p
== NULL
)) {
205 status
= htonl(NFS4ERR_RESOURCE
);
208 args
->truncate
= ntohl(*p
);
209 status
= decode_fh(xdr
, &args
->fh
);
211 dprintk("%s: exit with status = %d\n", __func__
, ntohl(status
));
215 #if defined(CONFIG_NFS_V4_1)
217 static unsigned decode_sessionid(struct xdr_stream
*xdr
,
218 struct nfs4_sessionid
*sid
)
221 int len
= NFS4_MAX_SESSIONID_LEN
;
223 p
= read_buf(xdr
, len
);
224 if (unlikely(p
== NULL
))
225 return htonl(NFS4ERR_RESOURCE
);;
227 memcpy(sid
->data
, p
, len
);
231 static unsigned decode_rc_list(struct xdr_stream
*xdr
,
232 struct referring_call_list
*rc_list
)
238 status
= decode_sessionid(xdr
, &rc_list
->rcl_sessionid
);
242 status
= htonl(NFS4ERR_RESOURCE
);
243 p
= read_buf(xdr
, sizeof(uint32_t));
244 if (unlikely(p
== NULL
))
247 rc_list
->rcl_nrefcalls
= ntohl(*p
++);
248 if (rc_list
->rcl_nrefcalls
) {
250 rc_list
->rcl_nrefcalls
* 2 * sizeof(uint32_t));
251 if (unlikely(p
== NULL
))
253 rc_list
->rcl_refcalls
= kmalloc(rc_list
->rcl_nrefcalls
*
254 sizeof(*rc_list
->rcl_refcalls
),
256 if (unlikely(rc_list
->rcl_refcalls
== NULL
))
258 for (i
= 0; i
< rc_list
->rcl_nrefcalls
; i
++) {
259 rc_list
->rcl_refcalls
[i
].rc_sequenceid
= ntohl(*p
++);
260 rc_list
->rcl_refcalls
[i
].rc_slotid
= ntohl(*p
++);
269 static unsigned decode_cb_sequence_args(struct svc_rqst
*rqstp
,
270 struct xdr_stream
*xdr
,
271 struct cb_sequenceargs
*args
)
277 status
= decode_sessionid(xdr
, &args
->csa_sessionid
);
281 status
= htonl(NFS4ERR_RESOURCE
);
282 p
= read_buf(xdr
, 5 * sizeof(uint32_t));
283 if (unlikely(p
== NULL
))
286 args
->csa_addr
= svc_addr(rqstp
);
287 args
->csa_sequenceid
= ntohl(*p
++);
288 args
->csa_slotid
= ntohl(*p
++);
289 args
->csa_highestslotid
= ntohl(*p
++);
290 args
->csa_cachethis
= ntohl(*p
++);
291 args
->csa_nrclists
= ntohl(*p
++);
292 args
->csa_rclists
= NULL
;
293 if (args
->csa_nrclists
) {
294 args
->csa_rclists
= kmalloc(args
->csa_nrclists
*
295 sizeof(*args
->csa_rclists
),
297 if (unlikely(args
->csa_rclists
== NULL
))
300 for (i
= 0; i
< args
->csa_nrclists
; i
++) {
301 status
= decode_rc_list(xdr
, &args
->csa_rclists
[i
]);
308 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
309 "highestslotid %u cachethis %d nrclists %u\n",
311 ((u32
*)&args
->csa_sessionid
)[0],
312 ((u32
*)&args
->csa_sessionid
)[1],
313 ((u32
*)&args
->csa_sessionid
)[2],
314 ((u32
*)&args
->csa_sessionid
)[3],
315 args
->csa_sequenceid
, args
->csa_slotid
,
316 args
->csa_highestslotid
, args
->csa_cachethis
,
319 dprintk("%s: exit with status = %d\n", __func__
, ntohl(status
));
323 for (i
= 0; i
< args
->csa_nrclists
; i
++)
324 kfree(args
->csa_rclists
[i
].rcl_refcalls
);
325 kfree(args
->csa_rclists
);
329 #endif /* CONFIG_NFS_V4_1 */
331 static __be32
encode_string(struct xdr_stream
*xdr
, unsigned int len
, const char *str
)
335 p
= xdr_reserve_space(xdr
, 4 + len
);
336 if (unlikely(p
== NULL
))
337 return htonl(NFS4ERR_RESOURCE
);
338 xdr_encode_opaque(p
, str
, len
);
342 #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
343 #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
344 static __be32
encode_attr_bitmap(struct xdr_stream
*xdr
, const uint32_t *bitmap
, __be32
**savep
)
349 bm
[0] = htonl(bitmap
[0] & CB_SUPPORTED_ATTR0
);
350 bm
[1] = htonl(bitmap
[1] & CB_SUPPORTED_ATTR1
);
352 p
= xdr_reserve_space(xdr
, 16);
353 if (unlikely(p
== NULL
))
354 return htonl(NFS4ERR_RESOURCE
);
358 } else if (bm
[0] != 0) {
359 p
= xdr_reserve_space(xdr
, 12);
360 if (unlikely(p
== NULL
))
361 return htonl(NFS4ERR_RESOURCE
);
365 p
= xdr_reserve_space(xdr
, 8);
366 if (unlikely(p
== NULL
))
367 return htonl(NFS4ERR_RESOURCE
);
374 static __be32
encode_attr_change(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t change
)
378 if (!(bitmap
[0] & FATTR4_WORD0_CHANGE
))
380 p
= xdr_reserve_space(xdr
, 8);
382 return htonl(NFS4ERR_RESOURCE
);
383 p
= xdr_encode_hyper(p
, change
);
387 static __be32
encode_attr_size(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t size
)
391 if (!(bitmap
[0] & FATTR4_WORD0_SIZE
))
393 p
= xdr_reserve_space(xdr
, 8);
395 return htonl(NFS4ERR_RESOURCE
);
396 p
= xdr_encode_hyper(p
, size
);
400 static __be32
encode_attr_time(struct xdr_stream
*xdr
, const struct timespec
*time
)
404 p
= xdr_reserve_space(xdr
, 12);
406 return htonl(NFS4ERR_RESOURCE
);
407 p
= xdr_encode_hyper(p
, time
->tv_sec
);
408 *p
= htonl(time
->tv_nsec
);
412 static __be32
encode_attr_ctime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
414 if (!(bitmap
[1] & FATTR4_WORD1_TIME_METADATA
))
416 return encode_attr_time(xdr
,time
);
419 static __be32
encode_attr_mtime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
421 if (!(bitmap
[1] & FATTR4_WORD1_TIME_MODIFY
))
423 return encode_attr_time(xdr
,time
);
426 static __be32
encode_compound_hdr_res(struct xdr_stream
*xdr
, struct cb_compound_hdr_res
*hdr
)
430 hdr
->status
= xdr_reserve_space(xdr
, 4);
431 if (unlikely(hdr
->status
== NULL
))
432 return htonl(NFS4ERR_RESOURCE
);
433 status
= encode_string(xdr
, hdr
->taglen
, hdr
->tag
);
434 if (unlikely(status
!= 0))
436 hdr
->nops
= xdr_reserve_space(xdr
, 4);
437 if (unlikely(hdr
->nops
== NULL
))
438 return htonl(NFS4ERR_RESOURCE
);
442 static __be32
encode_op_hdr(struct xdr_stream
*xdr
, uint32_t op
, __be32 res
)
446 p
= xdr_reserve_space(xdr
, 8);
447 if (unlikely(p
== NULL
))
448 return htonl(NFS4ERR_RESOURCE
);
454 static __be32
encode_getattr_res(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, const struct cb_getattrres
*res
)
456 __be32
*savep
= NULL
;
457 __be32 status
= res
->status
;
459 if (unlikely(status
!= 0))
461 status
= encode_attr_bitmap(xdr
, res
->bitmap
, &savep
);
462 if (unlikely(status
!= 0))
464 status
= encode_attr_change(xdr
, res
->bitmap
, res
->change_attr
);
465 if (unlikely(status
!= 0))
467 status
= encode_attr_size(xdr
, res
->bitmap
, res
->size
);
468 if (unlikely(status
!= 0))
470 status
= encode_attr_ctime(xdr
, res
->bitmap
, &res
->ctime
);
471 if (unlikely(status
!= 0))
473 status
= encode_attr_mtime(xdr
, res
->bitmap
, &res
->mtime
);
474 *savep
= htonl((unsigned int)((char *)xdr
->p
- (char *)(savep
+1)));
476 dprintk("%s: exit with status = %d\n", __func__
, ntohl(status
));
480 #if defined(CONFIG_NFS_V4_1)
482 static unsigned encode_sessionid(struct xdr_stream
*xdr
,
483 const struct nfs4_sessionid
*sid
)
486 int len
= NFS4_MAX_SESSIONID_LEN
;
488 p
= xdr_reserve_space(xdr
, len
);
489 if (unlikely(p
== NULL
))
490 return htonl(NFS4ERR_RESOURCE
);
496 static unsigned encode_cb_sequence_res(struct svc_rqst
*rqstp
,
497 struct xdr_stream
*xdr
,
498 const struct cb_sequenceres
*res
)
501 unsigned status
= res
->csr_status
;
503 if (unlikely(status
!= 0))
506 encode_sessionid(xdr
, &res
->csr_sessionid
);
508 p
= xdr_reserve_space(xdr
, 4 * sizeof(uint32_t));
509 if (unlikely(p
== NULL
))
510 return htonl(NFS4ERR_RESOURCE
);
512 *p
++ = htonl(res
->csr_sequenceid
);
513 *p
++ = htonl(res
->csr_slotid
);
514 *p
++ = htonl(res
->csr_highestslotid
);
515 *p
++ = htonl(res
->csr_target_highestslotid
);
517 dprintk("%s: exit with status = %d\n", __func__
, ntohl(status
));
522 preprocess_nfs41_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
524 if (op_nr
== OP_CB_SEQUENCE
) {
526 return htonl(NFS4ERR_SEQUENCE_POS
);
529 return htonl(NFS4ERR_OP_NOT_IN_SESSION
);
536 *op
= &callback_ops
[op_nr
];
539 case OP_CB_LAYOUTRECALL
:
540 case OP_CB_NOTIFY_DEVICEID
:
542 case OP_CB_PUSH_DELEG
:
543 case OP_CB_RECALL_ANY
:
544 case OP_CB_RECALLABLE_OBJ_AVAIL
:
545 case OP_CB_RECALL_SLOT
:
546 case OP_CB_WANTS_CANCELLED
:
547 case OP_CB_NOTIFY_LOCK
:
548 return htonl(NFS4ERR_NOTSUPP
);
551 return htonl(NFS4ERR_OP_ILLEGAL
);
554 return htonl(NFS_OK
);
557 #else /* CONFIG_NFS_V4_1 */
560 preprocess_nfs41_op(int nop
, unsigned int op_nr
, struct callback_op
**op
)
562 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
565 #endif /* CONFIG_NFS_V4_1 */
568 preprocess_nfs4_op(unsigned int op_nr
, struct callback_op
**op
)
573 *op
= &callback_ops
[op_nr
];
576 return htonl(NFS4ERR_OP_ILLEGAL
);
579 return htonl(NFS_OK
);
582 static __be32
process_op(uint32_t minorversion
, int nop
,
583 struct svc_rqst
*rqstp
,
584 struct xdr_stream
*xdr_in
, void *argp
,
585 struct xdr_stream
*xdr_out
, void *resp
)
587 struct callback_op
*op
= &callback_ops
[0];
588 unsigned int op_nr
= OP_CB_ILLEGAL
;
593 dprintk("%s: start\n", __func__
);
594 status
= decode_op_hdr(xdr_in
, &op_nr
);
595 if (unlikely(status
)) {
596 status
= htonl(NFS4ERR_OP_ILLEGAL
);
600 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
601 __func__
, minorversion
, nop
, op_nr
);
603 status
= minorversion
? preprocess_nfs41_op(nop
, op_nr
, &op
) :
604 preprocess_nfs4_op(op_nr
, &op
);
605 if (status
== htonl(NFS4ERR_OP_ILLEGAL
))
606 op_nr
= OP_CB_ILLEGAL
;
608 maxlen
= xdr_out
->end
- xdr_out
->p
;
609 if (maxlen
> 0 && maxlen
< PAGE_SIZE
) {
610 if (likely(status
== 0 && op
->decode_args
!= NULL
))
611 status
= op
->decode_args(rqstp
, xdr_in
, argp
);
612 if (likely(status
== 0 && op
->process_op
!= NULL
))
613 status
= op
->process_op(argp
, resp
);
615 status
= htonl(NFS4ERR_RESOURCE
);
617 res
= encode_op_hdr(xdr_out
, op_nr
, status
);
620 if (op
->encode_res
!= NULL
&& status
== 0)
621 status
= op
->encode_res(rqstp
, xdr_out
, resp
);
622 dprintk("%s: done, status = %d\n", __func__
, ntohl(status
));
627 * Decode, process and encode a COMPOUND
629 static __be32
nfs4_callback_compound(struct svc_rqst
*rqstp
, void *argp
, void *resp
)
631 struct cb_compound_hdr_arg hdr_arg
= { 0 };
632 struct cb_compound_hdr_res hdr_res
= { NULL
};
633 struct xdr_stream xdr_in
, xdr_out
;
636 unsigned int nops
= 0;
638 dprintk("%s: start\n", __func__
);
640 xdr_init_decode(&xdr_in
, &rqstp
->rq_arg
, rqstp
->rq_arg
.head
[0].iov_base
);
642 p
= (__be32
*)((char *)rqstp
->rq_res
.head
[0].iov_base
+ rqstp
->rq_res
.head
[0].iov_len
);
643 xdr_init_encode(&xdr_out
, &rqstp
->rq_res
, p
);
645 status
= decode_compound_hdr_arg(&xdr_in
, &hdr_arg
);
646 if (status
== __constant_htonl(NFS4ERR_RESOURCE
))
647 return rpc_garbage_args
;
649 hdr_res
.taglen
= hdr_arg
.taglen
;
650 hdr_res
.tag
= hdr_arg
.tag
;
651 if (encode_compound_hdr_res(&xdr_out
, &hdr_res
) != 0)
652 return rpc_system_err
;
654 while (status
== 0 && nops
!= hdr_arg
.nops
) {
655 status
= process_op(hdr_arg
.minorversion
, nops
,
656 rqstp
, &xdr_in
, argp
, &xdr_out
, resp
);
660 *hdr_res
.status
= status
;
661 *hdr_res
.nops
= htonl(nops
);
662 dprintk("%s: done, status = %u\n", __func__
, ntohl(status
));
667 * Define NFS4 callback COMPOUND ops.
669 static struct callback_op callback_ops
[] = {
671 .res_maxsize
= CB_OP_HDR_RES_MAXSZ
,
674 .process_op
= (callback_process_op_t
)nfs4_callback_getattr
,
675 .decode_args
= (callback_decode_arg_t
)decode_getattr_args
,
676 .encode_res
= (callback_encode_res_t
)encode_getattr_res
,
677 .res_maxsize
= CB_OP_GETATTR_RES_MAXSZ
,
680 .process_op
= (callback_process_op_t
)nfs4_callback_recall
,
681 .decode_args
= (callback_decode_arg_t
)decode_recall_args
,
682 .res_maxsize
= CB_OP_RECALL_RES_MAXSZ
,
684 #if defined(CONFIG_NFS_V4_1)
686 .process_op
= (callback_process_op_t
)nfs4_callback_sequence
,
687 .decode_args
= (callback_decode_arg_t
)decode_cb_sequence_args
,
688 .encode_res
= (callback_encode_res_t
)encode_cb_sequence_res
,
689 .res_maxsize
= CB_OP_SEQUENCE_RES_MAXSZ
,
691 #endif /* CONFIG_NFS_V4_1 */
695 * Define NFS4 callback procedures
697 static struct svc_procedure nfs4_callback_procedures1
[] = {
699 .pc_func
= nfs4_callback_null
,
700 .pc_decode
= (kxdrproc_t
)nfs4_decode_void
,
701 .pc_encode
= (kxdrproc_t
)nfs4_encode_void
,
705 .pc_func
= nfs4_callback_compound
,
706 .pc_encode
= (kxdrproc_t
)nfs4_encode_void
,
709 .pc_xdrressize
= NFS4_CALLBACK_BUFSIZE
,
713 struct svc_version nfs4_callback_version1
= {
715 .vs_nproc
= ARRAY_SIZE(nfs4_callback_procedures1
),
716 .vs_proc
= nfs4_callback_procedures1
,
717 .vs_xdrsize
= NFS4_CALLBACK_XDRSIZE
,