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>
16 #define CB_OP_TAGLEN_MAXSZ (512)
17 #define CB_OP_HDR_RES_MAXSZ (2 + CB_OP_TAGLEN_MAXSZ)
18 #define CB_OP_GETATTR_BITMAP_MAXSZ (4)
19 #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
20 CB_OP_GETATTR_BITMAP_MAXSZ + \
22 #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
24 #define NFSDBG_FACILITY NFSDBG_CALLBACK
26 typedef unsigned (*callback_process_op_t)(void *, void *);
27 typedef unsigned (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
28 typedef unsigned (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
32 callback_process_op_t process_op;
33 callback_decode_arg_t decode_args;
34 callback_encode_res_t encode_res;
38 static struct callback_op callback_ops[];
40 static int nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
42 return htonl(NFS4_OK);
45 static int nfs4_decode_void(struct svc_rqst *rqstp, uint32_t *p, void *dummy)
47 return xdr_argsize_check(rqstp, p);
50 static int nfs4_encode_void(struct svc_rqst *rqstp, uint32_t *p, void *dummy)
52 return xdr_ressize_check(rqstp, p);
55 static uint32_t *read_buf(struct xdr_stream *xdr, int nbytes)
59 p = xdr_inline_decode(xdr, nbytes);
60 if (unlikely(p == NULL))
61 printk(KERN_WARNING "NFSv4 callback reply buffer overflowed!\n");
65 static unsigned decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
70 if (unlikely(p == NULL))
71 return htonl(NFS4ERR_RESOURCE);
75 p = read_buf(xdr, *len);
76 if (unlikely(p == NULL))
77 return htonl(NFS4ERR_RESOURCE);
78 *str = (const char *)p;
85 static unsigned decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
90 if (unlikely(p == NULL))
91 return htonl(NFS4ERR_RESOURCE);
93 if (fh->size > NFS4_FHSIZE)
94 return htonl(NFS4ERR_BADHANDLE);
95 p = read_buf(xdr, fh->size);
96 if (unlikely(p == NULL))
97 return htonl(NFS4ERR_RESOURCE);
98 memcpy(&fh->data[0], p, fh->size);
99 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
103 static unsigned decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
106 unsigned int attrlen;
108 p = read_buf(xdr, 4);
109 if (unlikely(p == NULL))
110 return htonl(NFS4ERR_RESOURCE);
112 p = read_buf(xdr, attrlen << 2);
113 if (unlikely(p == NULL))
114 return htonl(NFS4ERR_RESOURCE);
115 if (likely(attrlen > 0))
116 bitmap[0] = ntohl(*p++);
118 bitmap[1] = ntohl(*p);
122 static unsigned decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
126 p = read_buf(xdr, 16);
127 if (unlikely(p == NULL))
128 return htonl(NFS4ERR_RESOURCE);
129 memcpy(stateid->data, p, 16);
133 static unsigned decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
136 unsigned int minor_version;
139 status = decode_string(xdr, &hdr->taglen, &hdr->tag);
140 if (unlikely(status != 0))
142 /* We do not like overly long tags! */
143 if (hdr->taglen > CB_OP_TAGLEN_MAXSZ-12 || hdr->taglen < 0) {
144 printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
145 __FUNCTION__, hdr->taglen);
146 return htonl(NFS4ERR_RESOURCE);
148 p = read_buf(xdr, 12);
149 if (unlikely(p == NULL))
150 return htonl(NFS4ERR_RESOURCE);
151 minor_version = ntohl(*p++);
152 /* Check minor version is zero. */
153 if (minor_version != 0) {
154 printk(KERN_WARNING "%s: NFSv4 server callback with illegal minor version %u!\n",
155 __FUNCTION__, minor_version);
156 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
158 hdr->callback_ident = ntohl(*p++);
159 hdr->nops = ntohl(*p);
163 static unsigned decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
166 p = read_buf(xdr, 4);
167 if (unlikely(p == NULL))
168 return htonl(NFS4ERR_RESOURCE);
173 static unsigned decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
177 status = decode_fh(xdr, &args->fh);
178 if (unlikely(status != 0))
180 args->addr = &rqstp->rq_addr;
181 status = decode_bitmap(xdr, args->bitmap);
183 dprintk("%s: exit with status = %d\n", __FUNCTION__, status);
187 static unsigned decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
192 args->addr = &rqstp->rq_addr;
193 status = decode_stateid(xdr, &args->stateid);
194 if (unlikely(status != 0))
196 p = read_buf(xdr, 4);
197 if (unlikely(p == NULL)) {
198 status = htonl(NFS4ERR_RESOURCE);
201 args->truncate = ntohl(*p);
202 status = decode_fh(xdr, &args->fh);
204 dprintk("%s: exit with status = %d\n", __FUNCTION__, status);
208 static unsigned encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
212 p = xdr_reserve_space(xdr, 4 + len);
213 if (unlikely(p == NULL))
214 return htonl(NFS4ERR_RESOURCE);
215 xdr_encode_opaque(p, str, len);
219 #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
220 #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
221 static unsigned encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, uint32_t **savep)
226 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
227 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
229 p = xdr_reserve_space(xdr, 16);
230 if (unlikely(p == NULL))
231 return htonl(NFS4ERR_RESOURCE);
235 } else if (bm[0] != 0) {
236 p = xdr_reserve_space(xdr, 12);
237 if (unlikely(p == NULL))
238 return htonl(NFS4ERR_RESOURCE);
242 p = xdr_reserve_space(xdr, 8);
243 if (unlikely(p == NULL))
244 return htonl(NFS4ERR_RESOURCE);
251 static unsigned encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
255 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
257 p = xdr_reserve_space(xdr, 8);
258 if (unlikely(p == 0))
259 return htonl(NFS4ERR_RESOURCE);
260 p = xdr_encode_hyper(p, change);
264 static unsigned encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
268 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
270 p = xdr_reserve_space(xdr, 8);
271 if (unlikely(p == 0))
272 return htonl(NFS4ERR_RESOURCE);
273 p = xdr_encode_hyper(p, size);
277 static unsigned encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
281 p = xdr_reserve_space(xdr, 12);
282 if (unlikely(p == 0))
283 return htonl(NFS4ERR_RESOURCE);
284 p = xdr_encode_hyper(p, time->tv_sec);
285 *p = htonl(time->tv_nsec);
289 static unsigned encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
291 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
293 return encode_attr_time(xdr,time);
296 static unsigned encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
298 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
300 return encode_attr_time(xdr,time);
303 static unsigned encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
307 hdr->status = xdr_reserve_space(xdr, 4);
308 if (unlikely(hdr->status == NULL))
309 return htonl(NFS4ERR_RESOURCE);
310 status = encode_string(xdr, hdr->taglen, hdr->tag);
311 if (unlikely(status != 0))
313 hdr->nops = xdr_reserve_space(xdr, 4);
314 if (unlikely(hdr->nops == NULL))
315 return htonl(NFS4ERR_RESOURCE);
319 static unsigned encode_op_hdr(struct xdr_stream *xdr, uint32_t op, uint32_t res)
323 p = xdr_reserve_space(xdr, 8);
324 if (unlikely(p == NULL))
325 return htonl(NFS4ERR_RESOURCE);
331 static unsigned encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
333 uint32_t *savep = NULL;
334 unsigned status = res->status;
336 if (unlikely(status != 0))
338 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
339 if (unlikely(status != 0))
341 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
342 if (unlikely(status != 0))
344 status = encode_attr_size(xdr, res->bitmap, res->size);
345 if (unlikely(status != 0))
347 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
348 if (unlikely(status != 0))
350 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
351 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
353 dprintk("%s: exit with status = %d\n", __FUNCTION__, status);
357 static unsigned process_op(struct svc_rqst *rqstp,
358 struct xdr_stream *xdr_in, void *argp,
359 struct xdr_stream *xdr_out, void *resp)
361 struct callback_op *op = &callback_ops[0];
362 unsigned int op_nr = OP_CB_ILLEGAL;
363 unsigned int status = 0;
367 dprintk("%s: start\n", __FUNCTION__);
368 status = decode_op_hdr(xdr_in, &op_nr);
369 if (likely(status == 0)) {
373 op = &callback_ops[op_nr];
376 op_nr = OP_CB_ILLEGAL;
377 op = &callback_ops[0];
378 status = htonl(NFS4ERR_OP_ILLEGAL);
382 maxlen = xdr_out->end - xdr_out->p;
383 if (maxlen > 0 && maxlen < PAGE_SIZE) {
384 if (likely(status == 0 && op->decode_args != NULL))
385 status = op->decode_args(rqstp, xdr_in, argp);
386 if (likely(status == 0 && op->process_op != NULL))
387 status = op->process_op(argp, resp);
389 status = htonl(NFS4ERR_RESOURCE);
391 res = encode_op_hdr(xdr_out, op_nr, status);
394 if (op->encode_res != NULL && status == 0)
395 status = op->encode_res(rqstp, xdr_out, resp);
396 dprintk("%s: done, status = %d\n", __FUNCTION__, status);
401 * Decode, process and encode a COMPOUND
403 static int nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
405 struct cb_compound_hdr_arg hdr_arg;
406 struct cb_compound_hdr_res hdr_res;
407 struct xdr_stream xdr_in, xdr_out;
410 unsigned int nops = 1;
412 dprintk("%s: start\n", __FUNCTION__);
414 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
416 p = (uint32_t*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
417 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
419 decode_compound_hdr_arg(&xdr_in, &hdr_arg);
420 hdr_res.taglen = hdr_arg.taglen;
421 hdr_res.tag = hdr_arg.tag;
423 encode_compound_hdr_res(&xdr_out, &hdr_res);
426 status = process_op(rqstp, &xdr_in, argp, &xdr_out, resp);
429 if (nops == hdr_arg.nops)
433 *hdr_res.status = status;
434 *hdr_res.nops = htonl(nops);
435 dprintk("%s: done, status = %u\n", __FUNCTION__, status);
440 * Define NFS4 callback COMPOUND ops.
442 static struct callback_op callback_ops[] = {
444 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
447 .process_op = (callback_process_op_t)nfs4_callback_getattr,
448 .decode_args = (callback_decode_arg_t)decode_getattr_args,
449 .encode_res = (callback_encode_res_t)encode_getattr_res,
450 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
453 .process_op = (callback_process_op_t)nfs4_callback_recall,
454 .decode_args = (callback_decode_arg_t)decode_recall_args,
455 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
460 * Define NFS4 callback procedures
462 static struct svc_procedure nfs4_callback_procedures1[] = {
464 .pc_func = nfs4_callback_null,
465 .pc_decode = (kxdrproc_t)nfs4_decode_void,
466 .pc_encode = (kxdrproc_t)nfs4_encode_void,
470 .pc_func = nfs4_callback_compound,
471 .pc_encode = (kxdrproc_t)nfs4_encode_void,
474 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
478 struct svc_version nfs4_callback_version1 = {
480 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
481 .vs_proc = nfs4_callback_procedures1,
482 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,