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);
174 dprintk("%s: minorversion %d nops %d\n", __func__,
175 hdr->minorversion, hdr->nops);
179 static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
182 p = read_buf(xdr, 4);
183 if (unlikely(p == NULL))
184 return htonl(NFS4ERR_RESOURCE_HDR);
189 static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
193 status = decode_fh(xdr, &args->fh);
194 if (unlikely(status != 0))
196 status = decode_bitmap(xdr, args->bitmap);
198 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
202 static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
207 status = decode_delegation_stateid(xdr, &args->stateid);
208 if (unlikely(status != 0))
210 p = read_buf(xdr, 4);
211 if (unlikely(p == NULL)) {
212 status = htonl(NFS4ERR_RESOURCE);
215 args->truncate = ntohl(*p);
216 status = decode_fh(xdr, &args->fh);
218 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
222 #if defined(CONFIG_NFS_V4_1)
223 static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
225 stateid->type = NFS4_LAYOUT_STATEID_TYPE;
226 return decode_stateid(xdr, stateid);
229 static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
230 struct xdr_stream *xdr,
231 struct cb_layoutrecallargs *args)
237 p = read_buf(xdr, 4 * sizeof(uint32_t));
238 if (unlikely(p == NULL)) {
239 status = htonl(NFS4ERR_BADXDR);
243 args->cbl_layout_type = ntohl(*p++);
244 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
245 * as it is unusable and ignored with the other types.
247 iomode = ntohl(*p++);
248 args->cbl_layoutchanged = ntohl(*p++);
249 args->cbl_recall_type = ntohl(*p++);
251 if (args->cbl_recall_type == RETURN_FILE) {
252 args->cbl_range.iomode = iomode;
253 status = decode_fh(xdr, &args->cbl_fh);
254 if (unlikely(status != 0))
257 p = read_buf(xdr, 2 * sizeof(uint64_t));
258 if (unlikely(p == NULL)) {
259 status = htonl(NFS4ERR_BADXDR);
262 p = xdr_decode_hyper(p, &args->cbl_range.offset);
263 p = xdr_decode_hyper(p, &args->cbl_range.length);
264 status = decode_layout_stateid(xdr, &args->cbl_stateid);
265 if (unlikely(status != 0))
267 } else if (args->cbl_recall_type == RETURN_FSID) {
268 p = read_buf(xdr, 2 * sizeof(uint64_t));
269 if (unlikely(p == NULL)) {
270 status = htonl(NFS4ERR_BADXDR);
273 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
274 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
275 } else if (args->cbl_recall_type != RETURN_ALL) {
276 status = htonl(NFS4ERR_BADXDR);
279 dprintk("%s: ltype 0x%x iomode %d changed %d recall_type %d\n",
281 args->cbl_layout_type, iomode,
282 args->cbl_layoutchanged, args->cbl_recall_type);
284 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
289 __be32 decode_devicenotify_args(struct svc_rqst *rqstp,
290 struct xdr_stream *xdr,
291 struct cb_devicenotifyargs *args)
299 /* Num of device notifications */
300 p = read_buf(xdr, sizeof(uint32_t));
301 if (unlikely(p == NULL)) {
302 status = htonl(NFS4ERR_BADXDR);
308 if (n > ULONG_MAX / sizeof(*args->devs)) {
309 status = htonl(NFS4ERR_BADXDR);
313 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
315 status = htonl(NFS4ERR_DELAY);
319 /* Decode each dev notification */
320 for (i = 0; i < n; i++) {
321 struct cb_devicenotifyitem *dev = &args->devs[i];
323 p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
324 if (unlikely(p == NULL)) {
325 status = htonl(NFS4ERR_BADXDR);
329 tmp = ntohl(*p++); /* bitmap size */
331 status = htonl(NFS4ERR_INVAL);
334 dev->cbd_notify_type = ntohl(*p++);
335 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
336 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
337 status = htonl(NFS4ERR_INVAL);
341 tmp = ntohl(*p++); /* opaque size */
342 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
343 (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
344 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
345 (tmp != NFS4_DEVICEID4_SIZE + 4))) {
346 status = htonl(NFS4ERR_INVAL);
349 dev->cbd_layout_type = ntohl(*p++);
350 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
351 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
353 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
354 p = read_buf(xdr, sizeof(uint32_t));
355 if (unlikely(p == NULL)) {
356 status = htonl(NFS4ERR_BADXDR);
359 dev->cbd_immediate = ntohl(*p++);
361 dev->cbd_immediate = 0;
366 dprintk("%s: type %d layout 0x%x immediate %d\n",
367 __func__, dev->cbd_notify_type, dev->cbd_layout_type,
371 dprintk("%s: status %d ndevs %d\n",
372 __func__, ntohl(status), args->ndevs);
379 static __be32 decode_sessionid(struct xdr_stream *xdr,
380 struct nfs4_sessionid *sid)
384 p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
385 if (unlikely(p == NULL))
386 return htonl(NFS4ERR_RESOURCE);
388 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
392 static __be32 decode_rc_list(struct xdr_stream *xdr,
393 struct referring_call_list *rc_list)
399 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
403 status = htonl(NFS4ERR_RESOURCE);
404 p = read_buf(xdr, sizeof(uint32_t));
405 if (unlikely(p == NULL))
408 rc_list->rcl_nrefcalls = ntohl(*p++);
409 if (rc_list->rcl_nrefcalls) {
411 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
412 if (unlikely(p == NULL))
414 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
415 sizeof(*rc_list->rcl_refcalls),
417 if (unlikely(rc_list->rcl_refcalls == NULL))
419 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
420 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
421 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
430 static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
431 struct xdr_stream *xdr,
432 struct cb_sequenceargs *args)
438 status = decode_sessionid(xdr, &args->csa_sessionid);
442 status = htonl(NFS4ERR_RESOURCE);
443 p = read_buf(xdr, 5 * sizeof(uint32_t));
444 if (unlikely(p == NULL))
447 args->csa_addr = svc_addr(rqstp);
448 args->csa_sequenceid = ntohl(*p++);
449 args->csa_slotid = ntohl(*p++);
450 args->csa_highestslotid = ntohl(*p++);
451 args->csa_cachethis = ntohl(*p++);
452 args->csa_nrclists = ntohl(*p++);
453 args->csa_rclists = NULL;
454 if (args->csa_nrclists) {
455 args->csa_rclists = kmalloc_array(args->csa_nrclists,
456 sizeof(*args->csa_rclists),
458 if (unlikely(args->csa_rclists == NULL))
461 for (i = 0; i < args->csa_nrclists; i++) {
462 status = decode_rc_list(xdr, &args->csa_rclists[i]);
464 args->csa_nrclists = i;
471 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
472 "highestslotid %u cachethis %d nrclists %u\n",
474 ((u32 *)&args->csa_sessionid)[0],
475 ((u32 *)&args->csa_sessionid)[1],
476 ((u32 *)&args->csa_sessionid)[2],
477 ((u32 *)&args->csa_sessionid)[3],
478 args->csa_sequenceid, args->csa_slotid,
479 args->csa_highestslotid, args->csa_cachethis,
482 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
486 for (i = 0; i < args->csa_nrclists; i++)
487 kfree(args->csa_rclists[i].rcl_refcalls);
488 kfree(args->csa_rclists);
492 static __be32 decode_recallany_args(struct svc_rqst *rqstp,
493 struct xdr_stream *xdr,
494 struct cb_recallanyargs *args)
499 p = read_buf(xdr, 4);
500 if (unlikely(p == NULL))
501 return htonl(NFS4ERR_BADXDR);
502 args->craa_objs_to_keep = ntohl(*p++);
503 status = decode_bitmap(xdr, bitmap);
504 if (unlikely(status))
506 args->craa_type_mask = bitmap[0];
511 static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
512 struct xdr_stream *xdr,
513 struct cb_recallslotargs *args)
517 p = read_buf(xdr, 4);
518 if (unlikely(p == NULL))
519 return htonl(NFS4ERR_BADXDR);
520 args->crsa_target_highest_slotid = ntohl(*p++);
524 static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
529 p = read_buf(xdr, 12);
530 if (unlikely(p == NULL))
531 return htonl(NFS4ERR_BADXDR);
533 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
534 len = be32_to_cpu(*p);
536 p = read_buf(xdr, len);
537 if (unlikely(p == NULL))
538 return htonl(NFS4ERR_BADXDR);
540 /* Only try to decode if the length is right */
542 p += 2; /* skip "lock id:" */
543 args->cbnl_owner.s_dev = be32_to_cpu(*p++);
544 xdr_decode_hyper(p, &args->cbnl_owner.id);
545 args->cbnl_valid = true;
547 args->cbnl_owner.s_dev = 0;
548 args->cbnl_owner.id = 0;
549 args->cbnl_valid = false;
554 static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_notify_lock_args *args)
558 status = decode_fh(xdr, &args->cbnl_fh);
559 if (unlikely(status != 0))
561 status = decode_lockowner(xdr, args);
563 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
567 #endif /* CONFIG_NFS_V4_1 */
569 static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
571 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
572 return cpu_to_be32(NFS4ERR_RESOURCE);
576 #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
577 #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
578 static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
583 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
584 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
586 p = xdr_reserve_space(xdr, 16);
587 if (unlikely(p == NULL))
588 return htonl(NFS4ERR_RESOURCE);
592 } else if (bm[0] != 0) {
593 p = xdr_reserve_space(xdr, 12);
594 if (unlikely(p == NULL))
595 return htonl(NFS4ERR_RESOURCE);
599 p = xdr_reserve_space(xdr, 8);
600 if (unlikely(p == NULL))
601 return htonl(NFS4ERR_RESOURCE);
608 static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
612 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
614 p = xdr_reserve_space(xdr, 8);
616 return htonl(NFS4ERR_RESOURCE);
617 p = xdr_encode_hyper(p, change);
621 static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
625 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
627 p = xdr_reserve_space(xdr, 8);
629 return htonl(NFS4ERR_RESOURCE);
630 p = xdr_encode_hyper(p, size);
634 static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
638 p = xdr_reserve_space(xdr, 12);
640 return htonl(NFS4ERR_RESOURCE);
641 p = xdr_encode_hyper(p, time->tv_sec);
642 *p = htonl(time->tv_nsec);
646 static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
648 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
650 return encode_attr_time(xdr,time);
653 static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
655 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
657 return encode_attr_time(xdr,time);
660 static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
664 hdr->status = xdr_reserve_space(xdr, 4);
665 if (unlikely(hdr->status == NULL))
666 return htonl(NFS4ERR_RESOURCE);
667 status = encode_string(xdr, hdr->taglen, hdr->tag);
668 if (unlikely(status != 0))
670 hdr->nops = xdr_reserve_space(xdr, 4);
671 if (unlikely(hdr->nops == NULL))
672 return htonl(NFS4ERR_RESOURCE);
676 static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
680 p = xdr_reserve_space(xdr, 8);
681 if (unlikely(p == NULL))
682 return htonl(NFS4ERR_RESOURCE_HDR);
688 static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
690 __be32 *savep = NULL;
691 __be32 status = res->status;
693 if (unlikely(status != 0))
695 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
696 if (unlikely(status != 0))
698 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
699 if (unlikely(status != 0))
701 status = encode_attr_size(xdr, res->bitmap, res->size);
702 if (unlikely(status != 0))
704 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
705 if (unlikely(status != 0))
707 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
708 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
710 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
714 #if defined(CONFIG_NFS_V4_1)
716 static __be32 encode_sessionid(struct xdr_stream *xdr,
717 const struct nfs4_sessionid *sid)
721 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
722 if (unlikely(p == NULL))
723 return htonl(NFS4ERR_RESOURCE);
725 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
729 static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
730 struct xdr_stream *xdr,
731 const struct cb_sequenceres *res)
734 __be32 status = res->csr_status;
736 if (unlikely(status != 0))
739 status = encode_sessionid(xdr, &res->csr_sessionid);
743 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
744 if (unlikely(p == NULL))
745 return htonl(NFS4ERR_RESOURCE);
747 *p++ = htonl(res->csr_sequenceid);
748 *p++ = htonl(res->csr_slotid);
749 *p++ = htonl(res->csr_highestslotid);
750 *p++ = htonl(res->csr_target_highestslotid);
752 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
757 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
759 if (op_nr == OP_CB_SEQUENCE) {
761 return htonl(NFS4ERR_SEQUENCE_POS);
764 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
771 case OP_CB_RECALL_ANY:
772 case OP_CB_RECALL_SLOT:
773 case OP_CB_LAYOUTRECALL:
774 case OP_CB_NOTIFY_DEVICEID:
775 case OP_CB_NOTIFY_LOCK:
776 *op = &callback_ops[op_nr];
780 case OP_CB_PUSH_DELEG:
781 case OP_CB_RECALLABLE_OBJ_AVAIL:
782 case OP_CB_WANTS_CANCELLED:
783 return htonl(NFS4ERR_NOTSUPP);
786 return htonl(NFS4ERR_OP_ILLEGAL);
789 return htonl(NFS_OK);
792 static void nfs4_callback_free_slot(struct nfs4_session *session,
793 struct nfs4_slot *slot)
795 struct nfs4_slot_table *tbl = &session->bc_slot_table;
797 spin_lock(&tbl->slot_tbl_lock);
799 * Let the state manager know callback processing done.
800 * A single slot, so highest used slotid is either 0 or -1
802 nfs4_free_slot(tbl, slot);
803 nfs4_slot_tbl_drain_complete(tbl);
804 spin_unlock(&tbl->slot_tbl_lock);
807 static void nfs4_cb_free_slot(struct cb_process_state *cps)
810 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
815 #else /* CONFIG_NFS_V4_1 */
818 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
820 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
823 static void nfs4_cb_free_slot(struct cb_process_state *cps)
826 #endif /* CONFIG_NFS_V4_1 */
828 #ifdef CONFIG_NFS_V4_2
830 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
832 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
833 if (status != htonl(NFS4ERR_OP_ILLEGAL))
836 if (op_nr == OP_CB_OFFLOAD)
837 return htonl(NFS4ERR_NOTSUPP);
838 return htonl(NFS4ERR_OP_ILLEGAL);
840 #else /* CONFIG_NFS_V4_2 */
842 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
844 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
846 #endif /* CONFIG_NFS_V4_2 */
849 preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
854 *op = &callback_ops[op_nr];
857 return htonl(NFS4ERR_OP_ILLEGAL);
860 return htonl(NFS_OK);
863 static __be32 process_op(int nop, struct svc_rqst *rqstp,
864 struct xdr_stream *xdr_in, void *argp,
865 struct xdr_stream *xdr_out, void *resp,
866 struct cb_process_state *cps)
868 struct callback_op *op = &callback_ops[0];
874 dprintk("%s: start\n", __func__);
875 status = decode_op_hdr(xdr_in, &op_nr);
876 if (unlikely(status))
879 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
880 __func__, cps->minorversion, nop, op_nr);
882 switch (cps->minorversion) {
884 status = preprocess_nfs4_op(op_nr, &op);
887 status = preprocess_nfs41_op(nop, op_nr, &op);
890 status = preprocess_nfs42_op(nop, op_nr, &op);
893 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
896 if (status == htonl(NFS4ERR_OP_ILLEGAL))
897 op_nr = OP_CB_ILLEGAL;
901 if (cps->drc_status) {
902 status = cps->drc_status;
906 maxlen = xdr_out->end - xdr_out->p;
907 if (maxlen > 0 && maxlen < PAGE_SIZE) {
908 status = op->decode_args(rqstp, xdr_in, argp);
909 if (likely(status == 0))
910 status = op->process_op(argp, resp, cps);
912 status = htonl(NFS4ERR_RESOURCE);
915 res = encode_op_hdr(xdr_out, op_nr, status);
918 if (op->encode_res != NULL && status == 0)
919 status = op->encode_res(rqstp, xdr_out, resp);
920 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
925 * Decode, process and encode a COMPOUND
927 static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
929 struct cb_compound_hdr_arg hdr_arg = { 0 };
930 struct cb_compound_hdr_res hdr_res = { NULL };
931 struct xdr_stream xdr_in, xdr_out;
933 struct cb_process_state cps = {
936 .net = SVC_NET(rqstp),
938 unsigned int nops = 0;
940 dprintk("%s: start\n", __func__);
942 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
944 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
945 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
947 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
948 if (status == htonl(NFS4ERR_RESOURCE))
949 return rpc_garbage_args;
951 if (hdr_arg.minorversion == 0) {
952 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
953 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
954 goto out_invalidcred;
957 cps.minorversion = hdr_arg.minorversion;
958 hdr_res.taglen = hdr_arg.taglen;
959 hdr_res.tag = hdr_arg.tag;
960 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
961 return rpc_system_err;
963 while (status == 0 && nops != hdr_arg.nops) {
964 status = process_op(nops, rqstp, &xdr_in,
965 argp, &xdr_out, resp, &cps);
969 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
970 * resource error in cb_compound status without returning op */
971 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
972 status = htonl(NFS4ERR_RESOURCE);
976 *hdr_res.status = status;
977 *hdr_res.nops = htonl(nops);
978 nfs4_cb_free_slot(&cps);
979 nfs_put_client(cps.clp);
980 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
984 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
985 return rpc_autherr_badcred;
989 * Define NFS4 callback COMPOUND ops.
991 static struct callback_op callback_ops[] = {
993 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
996 .process_op = (callback_process_op_t)nfs4_callback_getattr,
997 .decode_args = (callback_decode_arg_t)decode_getattr_args,
998 .encode_res = (callback_encode_res_t)encode_getattr_res,
999 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
1002 .process_op = (callback_process_op_t)nfs4_callback_recall,
1003 .decode_args = (callback_decode_arg_t)decode_recall_args,
1004 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
1006 #if defined(CONFIG_NFS_V4_1)
1007 [OP_CB_LAYOUTRECALL] = {
1008 .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
1010 (callback_decode_arg_t)decode_layoutrecall_args,
1011 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
1013 [OP_CB_NOTIFY_DEVICEID] = {
1014 .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
1016 (callback_decode_arg_t)decode_devicenotify_args,
1017 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
1019 [OP_CB_SEQUENCE] = {
1020 .process_op = (callback_process_op_t)nfs4_callback_sequence,
1021 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
1022 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
1023 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
1025 [OP_CB_RECALL_ANY] = {
1026 .process_op = (callback_process_op_t)nfs4_callback_recallany,
1027 .decode_args = (callback_decode_arg_t)decode_recallany_args,
1028 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
1030 [OP_CB_RECALL_SLOT] = {
1031 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
1032 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
1033 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
1035 [OP_CB_NOTIFY_LOCK] = {
1036 .process_op = (callback_process_op_t)nfs4_callback_notify_lock,
1037 .decode_args = (callback_decode_arg_t)decode_notify_lock_args,
1038 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
1040 #endif /* CONFIG_NFS_V4_1 */
1044 * Define NFS4 callback procedures
1046 static struct svc_procedure nfs4_callback_procedures1[] = {
1048 .pc_func = nfs4_callback_null,
1049 .pc_decode = (kxdrproc_t)nfs4_decode_void,
1050 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1054 .pc_func = nfs4_callback_compound,
1055 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1058 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1062 struct svc_version nfs4_callback_version1 = {
1064 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1065 .vs_proc = nfs4_callback_procedures1,
1066 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1067 .vs_dispatch = NULL,
1069 .vs_need_cong_ctrl = true,
1072 struct svc_version nfs4_callback_version4 = {
1074 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1075 .vs_proc = nfs4_callback_procedures1,
1076 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1077 .vs_dispatch = NULL,
1079 .vs_need_cong_ctrl = true,