2 * linux/fs/nfs/callback_xdr.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFSv4 callback encode/decode procedures
8 #include <linux/config.h>
9 #include <linux/kernel.h>
10 #include <linux/sunrpc/svc.h>
11 #include <linux/nfs4.h>
12 #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 #define NFSDBG_FACILITY NFSDBG_CALLBACK
25 typedef unsigned (*callback_process_op_t
)(void *, void *);
26 typedef unsigned (*callback_decode_arg_t
)(struct svc_rqst
*, struct xdr_stream
*, void *);
27 typedef unsigned (*callback_encode_res_t
)(struct svc_rqst
*, struct xdr_stream
*, void *);
31 callback_process_op_t process_op
;
32 callback_decode_arg_t decode_args
;
33 callback_encode_res_t encode_res
;
37 static struct callback_op callback_ops
[];
39 static int nfs4_callback_null(struct svc_rqst
*rqstp
, void *argp
, void *resp
)
41 return htonl(NFS4_OK
);
44 static int nfs4_decode_void(struct svc_rqst
*rqstp
, uint32_t *p
, void *dummy
)
46 return xdr_argsize_check(rqstp
, p
);
49 static int nfs4_encode_void(struct svc_rqst
*rqstp
, uint32_t *p
, void *dummy
)
51 return xdr_ressize_check(rqstp
, p
);
54 static uint32_t *read_buf(struct xdr_stream
*xdr
, int nbytes
)
58 p
= xdr_inline_decode(xdr
, nbytes
);
59 if (unlikely(p
== NULL
))
60 printk(KERN_WARNING
"NFSv4 callback reply buffer overflowed!\n");
64 static unsigned decode_string(struct xdr_stream
*xdr
, unsigned int *len
, const char **str
)
69 if (unlikely(p
== NULL
))
70 return htonl(NFS4ERR_RESOURCE
);
74 p
= read_buf(xdr
, *len
);
75 if (unlikely(p
== NULL
))
76 return htonl(NFS4ERR_RESOURCE
);
77 *str
= (const char *)p
;
84 static unsigned decode_fh(struct xdr_stream
*xdr
, struct nfs_fh
*fh
)
89 if (unlikely(p
== NULL
))
90 return htonl(NFS4ERR_RESOURCE
);
92 if (fh
->size
> NFS4_FHSIZE
)
93 return htonl(NFS4ERR_BADHANDLE
);
94 p
= read_buf(xdr
, fh
->size
);
95 if (unlikely(p
== NULL
))
96 return htonl(NFS4ERR_RESOURCE
);
97 memcpy(&fh
->data
[0], p
, fh
->size
);
98 memset(&fh
->data
[fh
->size
], 0, sizeof(fh
->data
) - fh
->size
);
102 static unsigned decode_bitmap(struct xdr_stream
*xdr
, uint32_t *bitmap
)
105 unsigned int attrlen
;
107 p
= read_buf(xdr
, 4);
108 if (unlikely(p
== NULL
))
109 return htonl(NFS4ERR_RESOURCE
);
111 p
= read_buf(xdr
, attrlen
<< 2);
112 if (unlikely(p
== NULL
))
113 return htonl(NFS4ERR_RESOURCE
);
114 if (likely(attrlen
> 0))
115 bitmap
[0] = ntohl(*p
++);
117 bitmap
[1] = ntohl(*p
);
121 static unsigned decode_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
125 p
= read_buf(xdr
, 16);
126 if (unlikely(p
== NULL
))
127 return htonl(NFS4ERR_RESOURCE
);
128 memcpy(stateid
->data
, p
, 16);
132 static unsigned decode_compound_hdr_arg(struct xdr_stream
*xdr
, struct cb_compound_hdr_arg
*hdr
)
135 unsigned int minor_version
;
138 status
= decode_string(xdr
, &hdr
->taglen
, &hdr
->tag
);
139 if (unlikely(status
!= 0))
141 /* We do not like overly long tags! */
142 if (hdr
->taglen
> CB_OP_TAGLEN_MAXSZ
-12 || hdr
->taglen
< 0) {
143 printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
144 __FUNCTION__
, hdr
->taglen
);
145 return htonl(NFS4ERR_RESOURCE
);
147 p
= read_buf(xdr
, 12);
148 if (unlikely(p
== NULL
))
149 return htonl(NFS4ERR_RESOURCE
);
150 minor_version
= ntohl(*p
++);
151 /* Check minor version is zero. */
152 if (minor_version
!= 0) {
153 printk(KERN_WARNING
"%s: NFSv4 server callback with illegal minor version %u!\n",
154 __FUNCTION__
, minor_version
);
155 return htonl(NFS4ERR_MINOR_VERS_MISMATCH
);
157 hdr
->callback_ident
= ntohl(*p
++);
158 hdr
->nops
= ntohl(*p
);
162 static unsigned decode_op_hdr(struct xdr_stream
*xdr
, unsigned int *op
)
165 p
= read_buf(xdr
, 4);
166 if (unlikely(p
== NULL
))
167 return htonl(NFS4ERR_RESOURCE
);
172 static unsigned decode_getattr_args(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, struct cb_getattrargs
*args
)
176 status
= decode_fh(xdr
, &args
->fh
);
177 if (unlikely(status
!= 0))
179 args
->addr
= &rqstp
->rq_addr
;
180 status
= decode_bitmap(xdr
, args
->bitmap
);
182 dprintk("%s: exit with status = %d\n", __FUNCTION__
, status
);
186 static unsigned decode_recall_args(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, struct cb_recallargs
*args
)
191 args
->addr
= &rqstp
->rq_addr
;
192 status
= decode_stateid(xdr
, &args
->stateid
);
193 if (unlikely(status
!= 0))
195 p
= read_buf(xdr
, 4);
196 if (unlikely(p
== NULL
)) {
197 status
= htonl(NFS4ERR_RESOURCE
);
200 args
->truncate
= ntohl(*p
);
201 status
= decode_fh(xdr
, &args
->fh
);
203 dprintk("%s: exit with status = %d\n", __FUNCTION__
, status
);
207 static unsigned encode_string(struct xdr_stream
*xdr
, unsigned int len
, const char *str
)
211 p
= xdr_reserve_space(xdr
, 4 + len
);
212 if (unlikely(p
== NULL
))
213 return htonl(NFS4ERR_RESOURCE
);
214 xdr_encode_opaque(p
, str
, len
);
218 #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
219 #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
220 static unsigned encode_attr_bitmap(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint32_t **savep
)
225 bm
[0] = htonl(bitmap
[0] & CB_SUPPORTED_ATTR0
);
226 bm
[1] = htonl(bitmap
[1] & CB_SUPPORTED_ATTR1
);
228 p
= xdr_reserve_space(xdr
, 16);
229 if (unlikely(p
== NULL
))
230 return htonl(NFS4ERR_RESOURCE
);
234 } else if (bm
[0] != 0) {
235 p
= xdr_reserve_space(xdr
, 12);
236 if (unlikely(p
== NULL
))
237 return htonl(NFS4ERR_RESOURCE
);
241 p
= xdr_reserve_space(xdr
, 8);
242 if (unlikely(p
== NULL
))
243 return htonl(NFS4ERR_RESOURCE
);
250 static unsigned encode_attr_change(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t change
)
254 if (!(bitmap
[0] & FATTR4_WORD0_CHANGE
))
256 p
= xdr_reserve_space(xdr
, 8);
257 if (unlikely(p
== 0))
258 return htonl(NFS4ERR_RESOURCE
);
259 p
= xdr_encode_hyper(p
, change
);
263 static unsigned encode_attr_size(struct xdr_stream
*xdr
, const uint32_t *bitmap
, uint64_t size
)
267 if (!(bitmap
[0] & FATTR4_WORD0_SIZE
))
269 p
= xdr_reserve_space(xdr
, 8);
270 if (unlikely(p
== 0))
271 return htonl(NFS4ERR_RESOURCE
);
272 p
= xdr_encode_hyper(p
, size
);
276 static unsigned encode_attr_time(struct xdr_stream
*xdr
, const struct timespec
*time
)
280 p
= xdr_reserve_space(xdr
, 12);
281 if (unlikely(p
== 0))
282 return htonl(NFS4ERR_RESOURCE
);
283 p
= xdr_encode_hyper(p
, time
->tv_sec
);
284 *p
= htonl(time
->tv_nsec
);
288 static unsigned encode_attr_ctime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
290 if (!(bitmap
[1] & FATTR4_WORD1_TIME_METADATA
))
292 return encode_attr_time(xdr
,time
);
295 static unsigned encode_attr_mtime(struct xdr_stream
*xdr
, const uint32_t *bitmap
, const struct timespec
*time
)
297 if (!(bitmap
[1] & FATTR4_WORD1_TIME_MODIFY
))
299 return encode_attr_time(xdr
,time
);
302 static unsigned encode_compound_hdr_res(struct xdr_stream
*xdr
, struct cb_compound_hdr_res
*hdr
)
306 hdr
->status
= xdr_reserve_space(xdr
, 4);
307 if (unlikely(hdr
->status
== NULL
))
308 return htonl(NFS4ERR_RESOURCE
);
309 status
= encode_string(xdr
, hdr
->taglen
, hdr
->tag
);
310 if (unlikely(status
!= 0))
312 hdr
->nops
= xdr_reserve_space(xdr
, 4);
313 if (unlikely(hdr
->nops
== NULL
))
314 return htonl(NFS4ERR_RESOURCE
);
318 static unsigned encode_op_hdr(struct xdr_stream
*xdr
, uint32_t op
, uint32_t res
)
322 p
= xdr_reserve_space(xdr
, 8);
323 if (unlikely(p
== NULL
))
324 return htonl(NFS4ERR_RESOURCE
);
330 static unsigned encode_getattr_res(struct svc_rqst
*rqstp
, struct xdr_stream
*xdr
, const struct cb_getattrres
*res
)
333 unsigned status
= res
->status
;
335 if (unlikely(status
!= 0))
337 status
= encode_attr_bitmap(xdr
, res
->bitmap
, &savep
);
338 if (unlikely(status
!= 0))
340 status
= encode_attr_change(xdr
, res
->bitmap
, res
->change_attr
);
341 if (unlikely(status
!= 0))
343 status
= encode_attr_size(xdr
, res
->bitmap
, res
->size
);
344 if (unlikely(status
!= 0))
346 status
= encode_attr_ctime(xdr
, res
->bitmap
, &res
->ctime
);
347 if (unlikely(status
!= 0))
349 status
= encode_attr_mtime(xdr
, res
->bitmap
, &res
->mtime
);
350 *savep
= htonl((unsigned int)((char *)xdr
->p
- (char *)(savep
+1)));
352 dprintk("%s: exit with status = %d\n", __FUNCTION__
, status
);
356 static unsigned process_op(struct svc_rqst
*rqstp
,
357 struct xdr_stream
*xdr_in
, void *argp
,
358 struct xdr_stream
*xdr_out
, void *resp
)
360 struct callback_op
*op
;
362 unsigned int status
= 0;
366 dprintk("%s: start\n", __FUNCTION__
);
367 status
= decode_op_hdr(xdr_in
, &op_nr
);
368 if (unlikely(status
!= 0)) {
369 op_nr
= OP_CB_ILLEGAL
;
370 op
= &callback_ops
[0];
371 } else if (unlikely(op_nr
!= OP_CB_GETATTR
&& op_nr
!= OP_CB_RECALL
)) {
372 op_nr
= OP_CB_ILLEGAL
;
373 op
= &callback_ops
[0];
374 status
= htonl(NFS4ERR_OP_ILLEGAL
);
376 op
= &callback_ops
[op_nr
];
378 maxlen
= xdr_out
->end
- xdr_out
->p
;
379 if (maxlen
> 0 && maxlen
< PAGE_SIZE
) {
380 if (likely(status
== 0 && op
->decode_args
!= NULL
))
381 status
= op
->decode_args(rqstp
, xdr_in
, argp
);
382 if (likely(status
== 0 && op
->process_op
!= NULL
))
383 status
= op
->process_op(argp
, resp
);
385 status
= htonl(NFS4ERR_RESOURCE
);
387 res
= encode_op_hdr(xdr_out
, op_nr
, status
);
390 if (op
->encode_res
!= NULL
&& status
== 0)
391 status
= op
->encode_res(rqstp
, xdr_out
, resp
);
392 dprintk("%s: done, status = %d\n", __FUNCTION__
, status
);
397 * Decode, process and encode a COMPOUND
399 static int nfs4_callback_compound(struct svc_rqst
*rqstp
, void *argp
, void *resp
)
401 struct cb_compound_hdr_arg hdr_arg
;
402 struct cb_compound_hdr_res hdr_res
;
403 struct xdr_stream xdr_in
, xdr_out
;
406 unsigned int nops
= 1;
408 dprintk("%s: start\n", __FUNCTION__
);
410 xdr_init_decode(&xdr_in
, &rqstp
->rq_arg
, rqstp
->rq_arg
.head
[0].iov_base
);
412 p
= (uint32_t*)((char *)rqstp
->rq_res
.head
[0].iov_base
+ rqstp
->rq_res
.head
[0].iov_len
);
413 rqstp
->rq_res
.head
[0].iov_len
= PAGE_SIZE
;
414 xdr_init_encode(&xdr_out
, &rqstp
->rq_res
, p
);
416 decode_compound_hdr_arg(&xdr_in
, &hdr_arg
);
417 hdr_res
.taglen
= hdr_arg
.taglen
;
418 hdr_res
.tag
= hdr_arg
.tag
;
419 encode_compound_hdr_res(&xdr_out
, &hdr_res
);
422 status
= process_op(rqstp
, &xdr_in
, argp
, &xdr_out
, resp
);
425 if (nops
== hdr_arg
.nops
)
429 *hdr_res
.status
= status
;
430 *hdr_res
.nops
= htonl(nops
);
431 dprintk("%s: done, status = %u\n", __FUNCTION__
, status
);
436 * Define NFS4 callback COMPOUND ops.
438 static struct callback_op callback_ops
[] = {
440 .res_maxsize
= CB_OP_HDR_RES_MAXSZ
,
443 .process_op
= (callback_process_op_t
)nfs4_callback_getattr
,
444 .decode_args
= (callback_decode_arg_t
)decode_getattr_args
,
445 .encode_res
= (callback_encode_res_t
)encode_getattr_res
,
446 .res_maxsize
= CB_OP_GETATTR_RES_MAXSZ
,
449 .process_op
= (callback_process_op_t
)nfs4_callback_recall
,
450 .decode_args
= (callback_decode_arg_t
)decode_recall_args
,
451 .res_maxsize
= CB_OP_RECALL_RES_MAXSZ
,
456 * Define NFS4 callback procedures
458 static struct svc_procedure nfs4_callback_procedures1
[] = {
460 .pc_func
= nfs4_callback_null
,
461 .pc_decode
= (kxdrproc_t
)nfs4_decode_void
,
462 .pc_encode
= (kxdrproc_t
)nfs4_encode_void
,
466 .pc_func
= nfs4_callback_compound
,
467 .pc_encode
= (kxdrproc_t
)nfs4_encode_void
,
470 .pc_xdrressize
= NFS4_CALLBACK_BUFSIZE
,
474 struct svc_version nfs4_callback_version1
= {
476 .vs_nproc
= ARRAY_SIZE(nfs4_callback_procedures1
),
477 .vs_proc
= nfs4_callback_procedures1
,
478 .vs_xdrsize
= NFS4_CALLBACK_XDRSIZE
,