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, const char **str)
91 if (unlikely(p == NULL))
92 return htonl(NFS4ERR_RESOURCE);
96 p = read_buf(xdr, *len);
97 if (unlikely(p == NULL))
98 return htonl(NFS4ERR_RESOURCE);
99 *str = (const char *)p;
106 static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
110 p = read_buf(xdr, 4);
111 if (unlikely(p == NULL))
112 return htonl(NFS4ERR_RESOURCE);
113 fh->size = ntohl(*p);
114 if (fh->size > NFS4_FHSIZE)
115 return htonl(NFS4ERR_BADHANDLE);
116 p = read_buf(xdr, fh->size);
117 if (unlikely(p == NULL))
118 return htonl(NFS4ERR_RESOURCE);
119 memcpy(&fh->data[0], p, fh->size);
120 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
124 static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
127 unsigned int attrlen;
129 p = read_buf(xdr, 4);
130 if (unlikely(p == NULL))
131 return htonl(NFS4ERR_RESOURCE);
133 p = read_buf(xdr, attrlen << 2);
134 if (unlikely(p == NULL))
135 return htonl(NFS4ERR_RESOURCE);
136 if (likely(attrlen > 0))
137 bitmap[0] = ntohl(*p++);
139 bitmap[1] = ntohl(*p);
143 static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
147 p = read_buf(xdr, NFS4_STATEID_SIZE);
148 if (unlikely(p == NULL))
149 return htonl(NFS4ERR_RESOURCE);
150 memcpy(stateid->data, p, NFS4_STATEID_SIZE);
154 static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
156 stateid->type = NFS4_DELEGATION_STATEID_TYPE;
157 return decode_stateid(xdr, stateid);
160 static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
165 status = decode_string(xdr, &hdr->taglen, &hdr->tag);
166 if (unlikely(status != 0))
168 /* We do not like overly long tags! */
169 if (hdr->taglen > CB_OP_TAGLEN_MAXSZ) {
170 printk("NFS: NFSv4 CALLBACK %s: client sent tag of length %u\n",
171 __func__, hdr->taglen);
172 return htonl(NFS4ERR_RESOURCE);
174 p = read_buf(xdr, 12);
175 if (unlikely(p == NULL))
176 return htonl(NFS4ERR_RESOURCE);
177 hdr->minorversion = ntohl(*p++);
178 /* Check for minor version support */
179 if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
180 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
182 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
183 "illegal minor version %u!\n",
184 __func__, hdr->minorversion);
185 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
187 hdr->nops = ntohl(*p);
188 dprintk("%s: minorversion %d nops %d\n", __func__,
189 hdr->minorversion, hdr->nops);
193 static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
196 p = read_buf(xdr, 4);
197 if (unlikely(p == NULL))
198 return htonl(NFS4ERR_RESOURCE_HDR);
203 static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
207 status = decode_fh(xdr, &args->fh);
208 if (unlikely(status != 0))
210 status = decode_bitmap(xdr, args->bitmap);
212 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
216 static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
221 status = decode_delegation_stateid(xdr, &args->stateid);
222 if (unlikely(status != 0))
224 p = read_buf(xdr, 4);
225 if (unlikely(p == NULL)) {
226 status = htonl(NFS4ERR_RESOURCE);
229 args->truncate = ntohl(*p);
230 status = decode_fh(xdr, &args->fh);
232 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
236 #if defined(CONFIG_NFS_V4_1)
237 static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
239 stateid->type = NFS4_LAYOUT_STATEID_TYPE;
240 return decode_stateid(xdr, stateid);
243 static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
244 struct xdr_stream *xdr,
245 struct cb_layoutrecallargs *args)
251 p = read_buf(xdr, 4 * sizeof(uint32_t));
252 if (unlikely(p == NULL)) {
253 status = htonl(NFS4ERR_BADXDR);
257 args->cbl_layout_type = ntohl(*p++);
258 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
259 * as it is unusable and ignored with the other types.
261 iomode = ntohl(*p++);
262 args->cbl_layoutchanged = ntohl(*p++);
263 args->cbl_recall_type = ntohl(*p++);
265 if (args->cbl_recall_type == RETURN_FILE) {
266 args->cbl_range.iomode = iomode;
267 status = decode_fh(xdr, &args->cbl_fh);
268 if (unlikely(status != 0))
271 p = read_buf(xdr, 2 * sizeof(uint64_t));
272 if (unlikely(p == NULL)) {
273 status = htonl(NFS4ERR_BADXDR);
276 p = xdr_decode_hyper(p, &args->cbl_range.offset);
277 p = xdr_decode_hyper(p, &args->cbl_range.length);
278 status = decode_layout_stateid(xdr, &args->cbl_stateid);
279 if (unlikely(status != 0))
281 } else if (args->cbl_recall_type == RETURN_FSID) {
282 p = read_buf(xdr, 2 * sizeof(uint64_t));
283 if (unlikely(p == NULL)) {
284 status = htonl(NFS4ERR_BADXDR);
287 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
288 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
289 } else if (args->cbl_recall_type != RETURN_ALL) {
290 status = htonl(NFS4ERR_BADXDR);
293 dprintk("%s: ltype 0x%x iomode %d changed %d recall_type %d\n",
295 args->cbl_layout_type, iomode,
296 args->cbl_layoutchanged, args->cbl_recall_type);
298 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
303 __be32 decode_devicenotify_args(struct svc_rqst *rqstp,
304 struct xdr_stream *xdr,
305 struct cb_devicenotifyargs *args)
313 /* Num of device notifications */
314 p = read_buf(xdr, sizeof(uint32_t));
315 if (unlikely(p == NULL)) {
316 status = htonl(NFS4ERR_BADXDR);
323 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
325 status = htonl(NFS4ERR_DELAY);
329 /* Decode each dev notification */
330 for (i = 0; i < n; i++) {
331 struct cb_devicenotifyitem *dev = &args->devs[i];
333 p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
334 if (unlikely(p == NULL)) {
335 status = htonl(NFS4ERR_BADXDR);
339 tmp = ntohl(*p++); /* bitmap size */
341 status = htonl(NFS4ERR_INVAL);
344 dev->cbd_notify_type = ntohl(*p++);
345 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
346 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
347 status = htonl(NFS4ERR_INVAL);
351 tmp = ntohl(*p++); /* opaque size */
352 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
353 (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
354 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
355 (tmp != NFS4_DEVICEID4_SIZE + 4))) {
356 status = htonl(NFS4ERR_INVAL);
359 dev->cbd_layout_type = ntohl(*p++);
360 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
361 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
363 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
364 p = read_buf(xdr, sizeof(uint32_t));
365 if (unlikely(p == NULL)) {
366 status = htonl(NFS4ERR_BADXDR);
369 dev->cbd_immediate = ntohl(*p++);
371 dev->cbd_immediate = 0;
376 dprintk("%s: type %d layout 0x%x immediate %d\n",
377 __func__, dev->cbd_notify_type, dev->cbd_layout_type,
381 dprintk("%s: status %d ndevs %d\n",
382 __func__, ntohl(status), args->ndevs);
389 static __be32 decode_sessionid(struct xdr_stream *xdr,
390 struct nfs4_sessionid *sid)
394 p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
395 if (unlikely(p == NULL))
396 return htonl(NFS4ERR_RESOURCE);
398 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
402 static __be32 decode_rc_list(struct xdr_stream *xdr,
403 struct referring_call_list *rc_list)
409 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
413 status = htonl(NFS4ERR_RESOURCE);
414 p = read_buf(xdr, sizeof(uint32_t));
415 if (unlikely(p == NULL))
418 rc_list->rcl_nrefcalls = ntohl(*p++);
419 if (rc_list->rcl_nrefcalls) {
421 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
422 if (unlikely(p == NULL))
424 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
425 sizeof(*rc_list->rcl_refcalls),
427 if (unlikely(rc_list->rcl_refcalls == NULL))
429 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
430 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
431 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
440 static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
441 struct xdr_stream *xdr,
442 struct cb_sequenceargs *args)
448 status = decode_sessionid(xdr, &args->csa_sessionid);
452 status = htonl(NFS4ERR_RESOURCE);
453 p = read_buf(xdr, 5 * sizeof(uint32_t));
454 if (unlikely(p == NULL))
457 args->csa_addr = svc_addr(rqstp);
458 args->csa_sequenceid = ntohl(*p++);
459 args->csa_slotid = ntohl(*p++);
460 args->csa_highestslotid = ntohl(*p++);
461 args->csa_cachethis = ntohl(*p++);
462 args->csa_nrclists = ntohl(*p++);
463 args->csa_rclists = NULL;
464 if (args->csa_nrclists) {
465 args->csa_rclists = kmalloc_array(args->csa_nrclists,
466 sizeof(*args->csa_rclists),
468 if (unlikely(args->csa_rclists == NULL))
471 for (i = 0; i < args->csa_nrclists; i++) {
472 status = decode_rc_list(xdr, &args->csa_rclists[i]);
474 args->csa_nrclists = i;
481 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
482 "highestslotid %u cachethis %d nrclists %u\n",
484 ((u32 *)&args->csa_sessionid)[0],
485 ((u32 *)&args->csa_sessionid)[1],
486 ((u32 *)&args->csa_sessionid)[2],
487 ((u32 *)&args->csa_sessionid)[3],
488 args->csa_sequenceid, args->csa_slotid,
489 args->csa_highestslotid, args->csa_cachethis,
492 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
496 for (i = 0; i < args->csa_nrclists; i++)
497 kfree(args->csa_rclists[i].rcl_refcalls);
498 kfree(args->csa_rclists);
502 static __be32 decode_recallany_args(struct svc_rqst *rqstp,
503 struct xdr_stream *xdr,
504 struct cb_recallanyargs *args)
509 p = read_buf(xdr, 4);
510 if (unlikely(p == NULL))
511 return htonl(NFS4ERR_BADXDR);
512 args->craa_objs_to_keep = ntohl(*p++);
513 status = decode_bitmap(xdr, bitmap);
514 if (unlikely(status))
516 args->craa_type_mask = bitmap[0];
521 static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
522 struct xdr_stream *xdr,
523 struct cb_recallslotargs *args)
527 p = read_buf(xdr, 4);
528 if (unlikely(p == NULL))
529 return htonl(NFS4ERR_BADXDR);
530 args->crsa_target_highest_slotid = ntohl(*p++);
534 static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
539 p = read_buf(xdr, 12);
540 if (unlikely(p == NULL))
541 return htonl(NFS4ERR_BADXDR);
543 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
544 len = be32_to_cpu(*p);
546 p = read_buf(xdr, len);
547 if (unlikely(p == NULL))
548 return htonl(NFS4ERR_BADXDR);
550 /* Only try to decode if the length is right */
552 p += 2; /* skip "lock id:" */
553 args->cbnl_owner.s_dev = be32_to_cpu(*p++);
554 xdr_decode_hyper(p, &args->cbnl_owner.id);
555 args->cbnl_valid = true;
557 args->cbnl_owner.s_dev = 0;
558 args->cbnl_owner.id = 0;
559 args->cbnl_valid = false;
564 static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_notify_lock_args *args)
568 status = decode_fh(xdr, &args->cbnl_fh);
569 if (unlikely(status != 0))
571 status = decode_lockowner(xdr, args);
573 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
577 #endif /* CONFIG_NFS_V4_1 */
579 static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
583 p = xdr_reserve_space(xdr, 4 + len);
584 if (unlikely(p == NULL))
585 return htonl(NFS4ERR_RESOURCE);
586 xdr_encode_opaque(p, str, len);
590 #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
591 #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
592 static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
597 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
598 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
600 p = xdr_reserve_space(xdr, 16);
601 if (unlikely(p == NULL))
602 return htonl(NFS4ERR_RESOURCE);
606 } else if (bm[0] != 0) {
607 p = xdr_reserve_space(xdr, 12);
608 if (unlikely(p == NULL))
609 return htonl(NFS4ERR_RESOURCE);
613 p = xdr_reserve_space(xdr, 8);
614 if (unlikely(p == NULL))
615 return htonl(NFS4ERR_RESOURCE);
622 static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
626 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
628 p = xdr_reserve_space(xdr, 8);
630 return htonl(NFS4ERR_RESOURCE);
631 p = xdr_encode_hyper(p, change);
635 static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
639 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
641 p = xdr_reserve_space(xdr, 8);
643 return htonl(NFS4ERR_RESOURCE);
644 p = xdr_encode_hyper(p, size);
648 static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
652 p = xdr_reserve_space(xdr, 12);
654 return htonl(NFS4ERR_RESOURCE);
655 p = xdr_encode_hyper(p, time->tv_sec);
656 *p = htonl(time->tv_nsec);
660 static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
662 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
664 return encode_attr_time(xdr,time);
667 static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
669 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
671 return encode_attr_time(xdr,time);
674 static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
678 hdr->status = xdr_reserve_space(xdr, 4);
679 if (unlikely(hdr->status == NULL))
680 return htonl(NFS4ERR_RESOURCE);
681 status = encode_string(xdr, hdr->taglen, hdr->tag);
682 if (unlikely(status != 0))
684 hdr->nops = xdr_reserve_space(xdr, 4);
685 if (unlikely(hdr->nops == NULL))
686 return htonl(NFS4ERR_RESOURCE);
690 static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
694 p = xdr_reserve_space(xdr, 8);
695 if (unlikely(p == NULL))
696 return htonl(NFS4ERR_RESOURCE_HDR);
702 static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
704 __be32 *savep = NULL;
705 __be32 status = res->status;
707 if (unlikely(status != 0))
709 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
710 if (unlikely(status != 0))
712 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
713 if (unlikely(status != 0))
715 status = encode_attr_size(xdr, res->bitmap, res->size);
716 if (unlikely(status != 0))
718 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
719 if (unlikely(status != 0))
721 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
722 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
724 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
728 #if defined(CONFIG_NFS_V4_1)
730 static __be32 encode_sessionid(struct xdr_stream *xdr,
731 const struct nfs4_sessionid *sid)
735 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
736 if (unlikely(p == NULL))
737 return htonl(NFS4ERR_RESOURCE);
739 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
743 static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
744 struct xdr_stream *xdr,
745 const struct cb_sequenceres *res)
748 __be32 status = res->csr_status;
750 if (unlikely(status != 0))
753 status = encode_sessionid(xdr, &res->csr_sessionid);
757 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
758 if (unlikely(p == NULL))
759 return htonl(NFS4ERR_RESOURCE);
761 *p++ = htonl(res->csr_sequenceid);
762 *p++ = htonl(res->csr_slotid);
763 *p++ = htonl(res->csr_highestslotid);
764 *p++ = htonl(res->csr_target_highestslotid);
766 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
771 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
773 if (op_nr == OP_CB_SEQUENCE) {
775 return htonl(NFS4ERR_SEQUENCE_POS);
778 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
785 case OP_CB_RECALL_ANY:
786 case OP_CB_RECALL_SLOT:
787 case OP_CB_LAYOUTRECALL:
788 case OP_CB_NOTIFY_DEVICEID:
789 case OP_CB_NOTIFY_LOCK:
790 *op = &callback_ops[op_nr];
794 case OP_CB_PUSH_DELEG:
795 case OP_CB_RECALLABLE_OBJ_AVAIL:
796 case OP_CB_WANTS_CANCELLED:
797 return htonl(NFS4ERR_NOTSUPP);
800 return htonl(NFS4ERR_OP_ILLEGAL);
803 return htonl(NFS_OK);
806 static void nfs4_callback_free_slot(struct nfs4_session *session,
807 struct nfs4_slot *slot)
809 struct nfs4_slot_table *tbl = &session->bc_slot_table;
811 spin_lock(&tbl->slot_tbl_lock);
813 * Let the state manager know callback processing done.
814 * A single slot, so highest used slotid is either 0 or -1
816 nfs4_free_slot(tbl, slot);
817 nfs4_slot_tbl_drain_complete(tbl);
818 spin_unlock(&tbl->slot_tbl_lock);
821 static void nfs4_cb_free_slot(struct cb_process_state *cps)
824 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
829 #else /* CONFIG_NFS_V4_1 */
832 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
834 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
837 static void nfs4_cb_free_slot(struct cb_process_state *cps)
840 #endif /* CONFIG_NFS_V4_1 */
842 #ifdef CONFIG_NFS_V4_2
844 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
846 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
847 if (status != htonl(NFS4ERR_OP_ILLEGAL))
850 if (op_nr == OP_CB_OFFLOAD)
851 return htonl(NFS4ERR_NOTSUPP);
852 return htonl(NFS4ERR_OP_ILLEGAL);
854 #else /* CONFIG_NFS_V4_2 */
856 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
858 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
860 #endif /* CONFIG_NFS_V4_2 */
863 preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
868 *op = &callback_ops[op_nr];
871 return htonl(NFS4ERR_OP_ILLEGAL);
874 return htonl(NFS_OK);
877 static __be32 process_op(int nop, struct svc_rqst *rqstp,
878 struct xdr_stream *xdr_in, void *argp,
879 struct xdr_stream *xdr_out, void *resp,
880 struct cb_process_state *cps)
882 struct callback_op *op = &callback_ops[0];
888 dprintk("%s: start\n", __func__);
889 status = decode_op_hdr(xdr_in, &op_nr);
890 if (unlikely(status))
893 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
894 __func__, cps->minorversion, nop, op_nr);
896 switch (cps->minorversion) {
898 status = preprocess_nfs4_op(op_nr, &op);
901 status = preprocess_nfs41_op(nop, op_nr, &op);
904 status = preprocess_nfs42_op(nop, op_nr, &op);
907 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
910 if (status == htonl(NFS4ERR_OP_ILLEGAL))
911 op_nr = OP_CB_ILLEGAL;
915 if (cps->drc_status) {
916 status = cps->drc_status;
920 maxlen = xdr_out->end - xdr_out->p;
921 if (maxlen > 0 && maxlen < PAGE_SIZE) {
922 status = op->decode_args(rqstp, xdr_in, argp);
923 if (likely(status == 0))
924 status = op->process_op(argp, resp, cps);
926 status = htonl(NFS4ERR_RESOURCE);
929 res = encode_op_hdr(xdr_out, op_nr, status);
932 if (op->encode_res != NULL && status == 0)
933 status = op->encode_res(rqstp, xdr_out, resp);
934 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
939 * Decode, process and encode a COMPOUND
941 static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
943 struct cb_compound_hdr_arg hdr_arg = { 0 };
944 struct cb_compound_hdr_res hdr_res = { NULL };
945 struct xdr_stream xdr_in, xdr_out;
947 struct cb_process_state cps = {
950 .net = SVC_NET(rqstp),
952 unsigned int nops = 0;
954 dprintk("%s: start\n", __func__);
956 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
958 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
959 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
961 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
962 if (status == htonl(NFS4ERR_RESOURCE))
963 return rpc_garbage_args;
965 if (hdr_arg.minorversion == 0) {
966 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
967 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp)) {
969 nfs_put_client(cps.clp);
970 goto out_invalidcred;
974 cps.minorversion = hdr_arg.minorversion;
975 hdr_res.taglen = hdr_arg.taglen;
976 hdr_res.tag = hdr_arg.tag;
977 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) {
979 nfs_put_client(cps.clp);
980 return rpc_system_err;
982 while (status == 0 && nops != hdr_arg.nops) {
983 status = process_op(nops, rqstp, &xdr_in,
984 argp, &xdr_out, resp, &cps);
988 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
989 * resource error in cb_compound status without returning op */
990 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
991 status = htonl(NFS4ERR_RESOURCE);
995 *hdr_res.status = status;
996 *hdr_res.nops = htonl(nops);
997 nfs4_cb_free_slot(&cps);
998 nfs_put_client(cps.clp);
999 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
1003 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
1004 return rpc_autherr_badcred;
1008 * Define NFS4 callback COMPOUND ops.
1010 static struct callback_op callback_ops[] = {
1012 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
1015 .process_op = (callback_process_op_t)nfs4_callback_getattr,
1016 .decode_args = (callback_decode_arg_t)decode_getattr_args,
1017 .encode_res = (callback_encode_res_t)encode_getattr_res,
1018 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
1021 .process_op = (callback_process_op_t)nfs4_callback_recall,
1022 .decode_args = (callback_decode_arg_t)decode_recall_args,
1023 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
1025 #if defined(CONFIG_NFS_V4_1)
1026 [OP_CB_LAYOUTRECALL] = {
1027 .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
1029 (callback_decode_arg_t)decode_layoutrecall_args,
1030 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
1032 [OP_CB_NOTIFY_DEVICEID] = {
1033 .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
1035 (callback_decode_arg_t)decode_devicenotify_args,
1036 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
1038 [OP_CB_SEQUENCE] = {
1039 .process_op = (callback_process_op_t)nfs4_callback_sequence,
1040 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
1041 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
1042 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
1044 [OP_CB_RECALL_ANY] = {
1045 .process_op = (callback_process_op_t)nfs4_callback_recallany,
1046 .decode_args = (callback_decode_arg_t)decode_recallany_args,
1047 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
1049 [OP_CB_RECALL_SLOT] = {
1050 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
1051 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
1052 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
1054 [OP_CB_NOTIFY_LOCK] = {
1055 .process_op = (callback_process_op_t)nfs4_callback_notify_lock,
1056 .decode_args = (callback_decode_arg_t)decode_notify_lock_args,
1057 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
1059 #endif /* CONFIG_NFS_V4_1 */
1063 * Define NFS4 callback procedures
1065 static struct svc_procedure nfs4_callback_procedures1[] = {
1067 .pc_func = nfs4_callback_null,
1068 .pc_decode = (kxdrproc_t)nfs4_decode_void,
1069 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1073 .pc_func = nfs4_callback_compound,
1074 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1077 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1081 struct svc_version nfs4_callback_version1 = {
1083 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1084 .vs_proc = nfs4_callback_procedures1,
1085 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1086 .vs_dispatch = NULL,
1090 struct svc_version nfs4_callback_version4 = {
1092 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1093 .vs_proc = nfs4_callback_procedures1,
1094 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1095 .vs_dispatch = NULL,