11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/callback_xdr.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2004 Trond Myklebust 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * NFSv4 callback encode/decode procedures 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds #include <linux/kernel.h> 91da177e4SLinus Torvalds #include <linux/sunrpc/svc.h> 101da177e4SLinus Torvalds #include <linux/nfs4.h> 111da177e4SLinus Torvalds #include <linux/nfs_fs.h> 129a3ba432STrond Myklebust #include <linux/ratelimit.h> 139a3ba432STrond Myklebust #include <linux/printk.h> 145a0e3ad6STejun Heo #include <linux/slab.h> 15c36fca52SAndy Adamson #include <linux/sunrpc/bc_xprt.h> 164ce79717STrond Myklebust #include "nfs4_fs.h" 171da177e4SLinus Torvalds #include "callback.h" 18c36fca52SAndy Adamson #include "internal.h" 1976e697baSTrond Myklebust #include "nfs4session.h" 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #define CB_OP_TAGLEN_MAXSZ (512) 2245724e8aSKinglong Mee #define CB_OP_HDR_RES_MAXSZ (2 * 4) // opcode, status 2345724e8aSKinglong Mee #define CB_OP_GETATTR_BITMAP_MAXSZ (4 * 4) // bitmap length, 3 bitmaps 241da177e4SLinus Torvalds #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \ 251da177e4SLinus Torvalds CB_OP_GETATTR_BITMAP_MAXSZ + \ 2645724e8aSKinglong Mee /* change, size, ctime, mtime */\ 2745724e8aSKinglong Mee (2 + 2 + 3 + 3) * 4) 281da177e4SLinus Torvalds #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 291da177e4SLinus Torvalds 304aece6a1SBenny Halevy #if defined(CONFIG_NFS_V4_1) 31f2a62561SFred Isaman #define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 321be5683bSMarc Eshel #define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 334aece6a1SBenny Halevy #define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \ 3445724e8aSKinglong Mee NFS4_MAX_SESSIONID_LEN + \ 3545724e8aSKinglong Mee (1 + 3) * 4) // seqid, 3 slotids 3631f09607SAlexandros Batsakis #define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 37b9efa1b2SAndy Adamson #define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 38db783688SJeff Layton #define CB_OP_NOTIFY_LOCK_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 394aece6a1SBenny Halevy #endif /* CONFIG_NFS_V4_1 */ 404aece6a1SBenny Halevy 411da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_CALLBACK 421da177e4SLinus Torvalds 4331d2b435SAndy Adamson /* Internal error code */ 4431d2b435SAndy Adamson #define NFS4ERR_RESOURCE_HDR 11050 4531d2b435SAndy Adamson 46c36fca52SAndy Adamson typedef __be32 (*callback_process_op_t)(void *, void *, 47c36fca52SAndy Adamson struct cb_process_state *); 48e6f684f6SAl Viro typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *); 49e6f684f6SAl Viro typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *); 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds struct callback_op { 531da177e4SLinus Torvalds callback_process_op_t process_op; 541da177e4SLinus Torvalds callback_decode_arg_t decode_args; 551da177e4SLinus Torvalds callback_encode_res_t encode_res; 561da177e4SLinus Torvalds long res_maxsize; 571da177e4SLinus Torvalds }; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds static struct callback_op callback_ops[]; 601da177e4SLinus Torvalds 617111c66eSAl Viro static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp) 621da177e4SLinus Torvalds { 631da177e4SLinus Torvalds return htonl(NFS4_OK); 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 665704fdebSAl Viro static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds return xdr_argsize_check(rqstp, p); 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds 715704fdebSAl Viro static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds return xdr_ressize_check(rqstp, p); 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 76b60475c9SJeff Layton static __be32 *read_buf(struct xdr_stream *xdr, size_t nbytes) 771da177e4SLinus Torvalds { 785704fdebSAl Viro __be32 *p; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds p = xdr_inline_decode(xdr, nbytes); 811da177e4SLinus Torvalds if (unlikely(p == NULL)) 82756b9b37STrond Myklebust printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n"); 831da177e4SLinus Torvalds return p; 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds 86c065eeeaSTrond Myklebust static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, 87c065eeeaSTrond Myklebust const char **str, size_t maxlen) 881da177e4SLinus Torvalds { 89c065eeeaSTrond Myklebust ssize_t err; 901da177e4SLinus Torvalds 91c065eeeaSTrond Myklebust err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen); 92c065eeeaSTrond Myklebust if (err < 0) 93c065eeeaSTrond Myklebust return cpu_to_be32(NFS4ERR_RESOURCE); 94c065eeeaSTrond Myklebust *len = err; 951da177e4SLinus Torvalds return 0; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 98e6f684f6SAl Viro static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh) 991da177e4SLinus Torvalds { 1005704fdebSAl Viro __be32 *p; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds p = read_buf(xdr, 4); 1031da177e4SLinus Torvalds if (unlikely(p == NULL)) 1041da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 1051da177e4SLinus Torvalds fh->size = ntohl(*p); 1061da177e4SLinus Torvalds if (fh->size > NFS4_FHSIZE) 1071da177e4SLinus Torvalds return htonl(NFS4ERR_BADHANDLE); 1081da177e4SLinus Torvalds p = read_buf(xdr, fh->size); 1091da177e4SLinus Torvalds if (unlikely(p == NULL)) 1101da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 1111da177e4SLinus Torvalds memcpy(&fh->data[0], p, fh->size); 1121da177e4SLinus Torvalds memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size); 1131da177e4SLinus Torvalds return 0; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 116e6f684f6SAl Viro static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap) 1171da177e4SLinus Torvalds { 1185704fdebSAl Viro __be32 *p; 1191da177e4SLinus Torvalds unsigned int attrlen; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds p = read_buf(xdr, 4); 1221da177e4SLinus Torvalds if (unlikely(p == NULL)) 1231da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 1241da177e4SLinus Torvalds attrlen = ntohl(*p); 1251da177e4SLinus Torvalds p = read_buf(xdr, attrlen << 2); 1261da177e4SLinus Torvalds if (unlikely(p == NULL)) 1271da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 1281da177e4SLinus Torvalds if (likely(attrlen > 0)) 1291da177e4SLinus Torvalds bitmap[0] = ntohl(*p++); 1301da177e4SLinus Torvalds if (attrlen > 1) 1311da177e4SLinus Torvalds bitmap[1] = ntohl(*p); 1321da177e4SLinus Torvalds return 0; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 135e6f684f6SAl Viro static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 1361da177e4SLinus Torvalds { 1375704fdebSAl Viro __be32 *p; 1381da177e4SLinus Torvalds 1392d2f24adSTrond Myklebust p = read_buf(xdr, NFS4_STATEID_SIZE); 1401da177e4SLinus Torvalds if (unlikely(p == NULL)) 1411da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 14293b717fdSTrond Myklebust memcpy(stateid->data, p, NFS4_STATEID_SIZE); 1431da177e4SLinus Torvalds return 0; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 14693b717fdSTrond Myklebust static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 14793b717fdSTrond Myklebust { 14893b717fdSTrond Myklebust stateid->type = NFS4_DELEGATION_STATEID_TYPE; 14993b717fdSTrond Myklebust return decode_stateid(xdr, stateid); 15093b717fdSTrond Myklebust } 15193b717fdSTrond Myklebust 152e6f684f6SAl Viro static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr) 1531da177e4SLinus Torvalds { 1545704fdebSAl Viro __be32 *p; 155e6f684f6SAl Viro __be32 status; 1561da177e4SLinus Torvalds 157c065eeeaSTrond Myklebust status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ); 1581da177e4SLinus Torvalds if (unlikely(status != 0)) 1591da177e4SLinus Torvalds return status; 1601da177e4SLinus Torvalds p = read_buf(xdr, 12); 1611da177e4SLinus Torvalds if (unlikely(p == NULL)) 1621da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 163b8f2ef84SBenny Halevy hdr->minorversion = ntohl(*p++); 164459de2edSBryan Schumaker /* Check for minor version support */ 165459de2edSBryan Schumaker if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) { 16642c2c424SSteve Dickson hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */ 16748a9e2d2SBenny Halevy } else { 1689a3ba432STrond Myklebust pr_warn_ratelimited("NFS: %s: NFSv4 server callback with " 169b8f2ef84SBenny Halevy "illegal minor version %u!\n", 170b8f2ef84SBenny Halevy __func__, hdr->minorversion); 1711da177e4SLinus Torvalds return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds hdr->nops = ntohl(*p); 174b8f2ef84SBenny Halevy dprintk("%s: minorversion %d nops %d\n", __func__, 175b8f2ef84SBenny Halevy hdr->minorversion, hdr->nops); 1761da177e4SLinus Torvalds return 0; 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 179e6f684f6SAl Viro static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op) 1801da177e4SLinus Torvalds { 1815704fdebSAl Viro __be32 *p; 1821da177e4SLinus Torvalds p = read_buf(xdr, 4); 1831da177e4SLinus Torvalds if (unlikely(p == NULL)) 18431d2b435SAndy Adamson return htonl(NFS4ERR_RESOURCE_HDR); 1851da177e4SLinus Torvalds *op = ntohl(*p); 1861da177e4SLinus Torvalds return 0; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 189e6f684f6SAl Viro static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args) 1901da177e4SLinus Torvalds { 191e6f684f6SAl Viro __be32 status; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds status = decode_fh(xdr, &args->fh); 1941da177e4SLinus Torvalds if (unlikely(status != 0)) 1951da177e4SLinus Torvalds return status; 19656938bb7SAnna Schumaker return decode_bitmap(xdr, args->bitmap); 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 199e6f684f6SAl Viro static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args) 2001da177e4SLinus Torvalds { 2015704fdebSAl Viro __be32 *p; 202e6f684f6SAl Viro __be32 status; 2031da177e4SLinus Torvalds 20493b717fdSTrond Myklebust status = decode_delegation_stateid(xdr, &args->stateid); 2051da177e4SLinus Torvalds if (unlikely(status != 0)) 2063873bc50SAlexey Dobriyan return status; 207135a4ea0SAnna Schumaker p = read_buf(xdr, 4); 208135a4ea0SAnna Schumaker if (unlikely(p == NULL)) 209135a4ea0SAnna Schumaker return htonl(NFS4ERR_RESOURCE); 210135a4ea0SAnna Schumaker args->truncate = ntohl(*p); 211135a4ea0SAnna Schumaker return decode_fh(xdr, &args->fh); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 2144aece6a1SBenny Halevy #if defined(CONFIG_NFS_V4_1) 21593b717fdSTrond Myklebust static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 21693b717fdSTrond Myklebust { 21793b717fdSTrond Myklebust stateid->type = NFS4_LAYOUT_STATEID_TYPE; 21893b717fdSTrond Myklebust return decode_stateid(xdr, stateid); 21993b717fdSTrond Myklebust } 2204aece6a1SBenny Halevy 221f2a62561SFred Isaman static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp, 222f2a62561SFred Isaman struct xdr_stream *xdr, 223f2a62561SFred Isaman struct cb_layoutrecallargs *args) 224f2a62561SFred Isaman { 225f2a62561SFred Isaman __be32 *p; 226f2a62561SFred Isaman __be32 status = 0; 227f2a62561SFred Isaman uint32_t iomode; 228f2a62561SFred Isaman 229f2a62561SFred Isaman p = read_buf(xdr, 4 * sizeof(uint32_t)); 230c79d56d2SAnna Schumaker if (unlikely(p == NULL)) 231c79d56d2SAnna Schumaker return htonl(NFS4ERR_BADXDR); 232f2a62561SFred Isaman 233f2a62561SFred Isaman args->cbl_layout_type = ntohl(*p++); 234f2a62561SFred Isaman /* Depite the spec's xdr, iomode really belongs in the FILE switch, 23525985edcSLucas De Marchi * as it is unusable and ignored with the other types. 236f2a62561SFred Isaman */ 237f2a62561SFred Isaman iomode = ntohl(*p++); 238f2a62561SFred Isaman args->cbl_layoutchanged = ntohl(*p++); 239f2a62561SFred Isaman args->cbl_recall_type = ntohl(*p++); 240f2a62561SFred Isaman 241f2a62561SFred Isaman if (args->cbl_recall_type == RETURN_FILE) { 242f2a62561SFred Isaman args->cbl_range.iomode = iomode; 243f2a62561SFred Isaman status = decode_fh(xdr, &args->cbl_fh); 244f2a62561SFred Isaman if (unlikely(status != 0)) 245c79d56d2SAnna Schumaker return status; 246f2a62561SFred Isaman 247f2a62561SFred Isaman p = read_buf(xdr, 2 * sizeof(uint64_t)); 248c79d56d2SAnna Schumaker if (unlikely(p == NULL)) 249c79d56d2SAnna Schumaker return htonl(NFS4ERR_BADXDR); 250f2a62561SFred Isaman p = xdr_decode_hyper(p, &args->cbl_range.offset); 251f2a62561SFred Isaman p = xdr_decode_hyper(p, &args->cbl_range.length); 252c79d56d2SAnna Schumaker return decode_layout_stateid(xdr, &args->cbl_stateid); 253f2a62561SFred Isaman } else if (args->cbl_recall_type == RETURN_FSID) { 254f2a62561SFred Isaman p = read_buf(xdr, 2 * sizeof(uint64_t)); 255c79d56d2SAnna Schumaker if (unlikely(p == NULL)) 256c79d56d2SAnna Schumaker return htonl(NFS4ERR_BADXDR); 257f2a62561SFred Isaman p = xdr_decode_hyper(p, &args->cbl_fsid.major); 258f2a62561SFred Isaman p = xdr_decode_hyper(p, &args->cbl_fsid.minor); 259c79d56d2SAnna Schumaker } else if (args->cbl_recall_type != RETURN_ALL) 260c79d56d2SAnna Schumaker return htonl(NFS4ERR_BADXDR); 261c79d56d2SAnna Schumaker return 0; 262f2a62561SFred Isaman } 263f2a62561SFred Isaman 2641be5683bSMarc Eshel static 2651be5683bSMarc Eshel __be32 decode_devicenotify_args(struct svc_rqst *rqstp, 2661be5683bSMarc Eshel struct xdr_stream *xdr, 2671be5683bSMarc Eshel struct cb_devicenotifyargs *args) 2681be5683bSMarc Eshel { 2691be5683bSMarc Eshel __be32 *p; 2701be5683bSMarc Eshel __be32 status = 0; 2711be5683bSMarc Eshel u32 tmp; 2721be5683bSMarc Eshel int n, i; 2731be5683bSMarc Eshel args->ndevs = 0; 2741be5683bSMarc Eshel 2751be5683bSMarc Eshel /* Num of device notifications */ 2761be5683bSMarc Eshel p = read_buf(xdr, sizeof(uint32_t)); 2771be5683bSMarc Eshel if (unlikely(p == NULL)) { 2781be5683bSMarc Eshel status = htonl(NFS4ERR_BADXDR); 2791be5683bSMarc Eshel goto out; 2801be5683bSMarc Eshel } 2811be5683bSMarc Eshel n = ntohl(*p++); 2821be5683bSMarc Eshel if (n <= 0) 2831be5683bSMarc Eshel goto out; 284363e0df0SDan Carpenter if (n > ULONG_MAX / sizeof(*args->devs)) { 285363e0df0SDan Carpenter status = htonl(NFS4ERR_BADXDR); 286363e0df0SDan Carpenter goto out; 287363e0df0SDan Carpenter } 2881be5683bSMarc Eshel 289a4f743a6STrond Myklebust args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL); 2901be5683bSMarc Eshel if (!args->devs) { 2911be5683bSMarc Eshel status = htonl(NFS4ERR_DELAY); 2921be5683bSMarc Eshel goto out; 2931be5683bSMarc Eshel } 2941be5683bSMarc Eshel 2951be5683bSMarc Eshel /* Decode each dev notification */ 2961be5683bSMarc Eshel for (i = 0; i < n; i++) { 2971be5683bSMarc Eshel struct cb_devicenotifyitem *dev = &args->devs[i]; 2981be5683bSMarc Eshel 2991be5683bSMarc Eshel p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE); 3001be5683bSMarc Eshel if (unlikely(p == NULL)) { 3011be5683bSMarc Eshel status = htonl(NFS4ERR_BADXDR); 3021be5683bSMarc Eshel goto err; 3031be5683bSMarc Eshel } 3041be5683bSMarc Eshel 3051be5683bSMarc Eshel tmp = ntohl(*p++); /* bitmap size */ 3061be5683bSMarc Eshel if (tmp != 1) { 3071be5683bSMarc Eshel status = htonl(NFS4ERR_INVAL); 3081be5683bSMarc Eshel goto err; 3091be5683bSMarc Eshel } 3101be5683bSMarc Eshel dev->cbd_notify_type = ntohl(*p++); 3111be5683bSMarc Eshel if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE && 3121be5683bSMarc Eshel dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) { 3131be5683bSMarc Eshel status = htonl(NFS4ERR_INVAL); 3141be5683bSMarc Eshel goto err; 3151be5683bSMarc Eshel } 3161be5683bSMarc Eshel 3171be5683bSMarc Eshel tmp = ntohl(*p++); /* opaque size */ 3181be5683bSMarc Eshel if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) && 3191be5683bSMarc Eshel (tmp != NFS4_DEVICEID4_SIZE + 8)) || 3201be5683bSMarc Eshel ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) && 3211be5683bSMarc Eshel (tmp != NFS4_DEVICEID4_SIZE + 4))) { 3221be5683bSMarc Eshel status = htonl(NFS4ERR_INVAL); 3231be5683bSMarc Eshel goto err; 3241be5683bSMarc Eshel } 3251be5683bSMarc Eshel dev->cbd_layout_type = ntohl(*p++); 3261be5683bSMarc Eshel memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE); 3271be5683bSMarc Eshel p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE); 3281be5683bSMarc Eshel 3291be5683bSMarc Eshel if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) { 3301be5683bSMarc Eshel p = read_buf(xdr, sizeof(uint32_t)); 3311be5683bSMarc Eshel if (unlikely(p == NULL)) { 3321be5683bSMarc Eshel status = htonl(NFS4ERR_BADXDR); 3331be5683bSMarc Eshel goto err; 3341be5683bSMarc Eshel } 3351be5683bSMarc Eshel dev->cbd_immediate = ntohl(*p++); 3361be5683bSMarc Eshel } else { 3371be5683bSMarc Eshel dev->cbd_immediate = 0; 3381be5683bSMarc Eshel } 3391be5683bSMarc Eshel 3401be5683bSMarc Eshel args->ndevs++; 3411be5683bSMarc Eshel 3421be5683bSMarc Eshel dprintk("%s: type %d layout 0x%x immediate %d\n", 3431be5683bSMarc Eshel __func__, dev->cbd_notify_type, dev->cbd_layout_type, 3441be5683bSMarc Eshel dev->cbd_immediate); 3451be5683bSMarc Eshel } 3461be5683bSMarc Eshel out: 3471be5683bSMarc Eshel dprintk("%s: status %d ndevs %d\n", 3481be5683bSMarc Eshel __func__, ntohl(status), args->ndevs); 3491be5683bSMarc Eshel return status; 3501be5683bSMarc Eshel err: 3511be5683bSMarc Eshel kfree(args->devs); 3521be5683bSMarc Eshel goto out; 3531be5683bSMarc Eshel } 3541be5683bSMarc Eshel 3559733f0d9SAndy Adamson static __be32 decode_sessionid(struct xdr_stream *xdr, 3564aece6a1SBenny Halevy struct nfs4_sessionid *sid) 3574aece6a1SBenny Halevy { 3589733f0d9SAndy Adamson __be32 *p; 3594aece6a1SBenny Halevy 360590184a6SKinglong Mee p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN); 3614aece6a1SBenny Halevy if (unlikely(p == NULL)) 362a419aef8SJoe Perches return htonl(NFS4ERR_RESOURCE); 3634aece6a1SBenny Halevy 364590184a6SKinglong Mee memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN); 3654aece6a1SBenny Halevy return 0; 3664aece6a1SBenny Halevy } 3674aece6a1SBenny Halevy 3689733f0d9SAndy Adamson static __be32 decode_rc_list(struct xdr_stream *xdr, 3694aece6a1SBenny Halevy struct referring_call_list *rc_list) 3704aece6a1SBenny Halevy { 3719733f0d9SAndy Adamson __be32 *p; 3724aece6a1SBenny Halevy int i; 3739733f0d9SAndy Adamson __be32 status; 3744aece6a1SBenny Halevy 3754aece6a1SBenny Halevy status = decode_sessionid(xdr, &rc_list->rcl_sessionid); 3764aece6a1SBenny Halevy if (status) 3774aece6a1SBenny Halevy goto out; 3784aece6a1SBenny Halevy 3794aece6a1SBenny Halevy status = htonl(NFS4ERR_RESOURCE); 3804aece6a1SBenny Halevy p = read_buf(xdr, sizeof(uint32_t)); 3814aece6a1SBenny Halevy if (unlikely(p == NULL)) 3824aece6a1SBenny Halevy goto out; 3834aece6a1SBenny Halevy 3844aece6a1SBenny Halevy rc_list->rcl_nrefcalls = ntohl(*p++); 3854aece6a1SBenny Halevy if (rc_list->rcl_nrefcalls) { 3864aece6a1SBenny Halevy p = read_buf(xdr, 3874aece6a1SBenny Halevy rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t)); 3884aece6a1SBenny Halevy if (unlikely(p == NULL)) 3894aece6a1SBenny Halevy goto out; 390a4f743a6STrond Myklebust rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls, 3914aece6a1SBenny Halevy sizeof(*rc_list->rcl_refcalls), 3924aece6a1SBenny Halevy GFP_KERNEL); 3934aece6a1SBenny Halevy if (unlikely(rc_list->rcl_refcalls == NULL)) 3944aece6a1SBenny Halevy goto out; 3954aece6a1SBenny Halevy for (i = 0; i < rc_list->rcl_nrefcalls; i++) { 3964aece6a1SBenny Halevy rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++); 3974aece6a1SBenny Halevy rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++); 3984aece6a1SBenny Halevy } 3994aece6a1SBenny Halevy } 4004aece6a1SBenny Halevy status = 0; 4014aece6a1SBenny Halevy 4024aece6a1SBenny Halevy out: 4034aece6a1SBenny Halevy return status; 4044aece6a1SBenny Halevy } 4054aece6a1SBenny Halevy 4069733f0d9SAndy Adamson static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp, 4074aece6a1SBenny Halevy struct xdr_stream *xdr, 4084aece6a1SBenny Halevy struct cb_sequenceargs *args) 4094aece6a1SBenny Halevy { 4109733f0d9SAndy Adamson __be32 *p; 4114aece6a1SBenny Halevy int i; 4129733f0d9SAndy Adamson __be32 status; 4134aece6a1SBenny Halevy 4144aece6a1SBenny Halevy status = decode_sessionid(xdr, &args->csa_sessionid); 4154aece6a1SBenny Halevy if (status) 4161796549aSAnna Schumaker return status; 4174aece6a1SBenny Halevy 4184aece6a1SBenny Halevy p = read_buf(xdr, 5 * sizeof(uint32_t)); 4194aece6a1SBenny Halevy if (unlikely(p == NULL)) 4201796549aSAnna Schumaker return htonl(NFS4ERR_RESOURCE); 4214aece6a1SBenny Halevy 42265fc64e5SRicardo Labiaga args->csa_addr = svc_addr(rqstp); 4234aece6a1SBenny Halevy args->csa_sequenceid = ntohl(*p++); 4244aece6a1SBenny Halevy args->csa_slotid = ntohl(*p++); 4254aece6a1SBenny Halevy args->csa_highestslotid = ntohl(*p++); 4264aece6a1SBenny Halevy args->csa_cachethis = ntohl(*p++); 4274aece6a1SBenny Halevy args->csa_nrclists = ntohl(*p++); 4284aece6a1SBenny Halevy args->csa_rclists = NULL; 4294aece6a1SBenny Halevy if (args->csa_nrclists) { 4300439f31cSDan Carpenter args->csa_rclists = kmalloc_array(args->csa_nrclists, 4314aece6a1SBenny Halevy sizeof(*args->csa_rclists), 4324aece6a1SBenny Halevy GFP_KERNEL); 4334aece6a1SBenny Halevy if (unlikely(args->csa_rclists == NULL)) 4341796549aSAnna Schumaker return htonl(NFS4ERR_RESOURCE); 4354aece6a1SBenny Halevy 4364aece6a1SBenny Halevy for (i = 0; i < args->csa_nrclists; i++) { 4374aece6a1SBenny Halevy status = decode_rc_list(xdr, &args->csa_rclists[i]); 438d8ba1f97STrond Myklebust if (status) { 439d8ba1f97STrond Myklebust args->csa_nrclists = i; 4404aece6a1SBenny Halevy goto out_free; 4414aece6a1SBenny Halevy } 4424aece6a1SBenny Halevy } 443d8ba1f97STrond Myklebust } 4441796549aSAnna Schumaker return 0; 4454aece6a1SBenny Halevy 4464aece6a1SBenny Halevy out_free: 4474aece6a1SBenny Halevy for (i = 0; i < args->csa_nrclists; i++) 4484aece6a1SBenny Halevy kfree(args->csa_rclists[i].rcl_refcalls); 4494aece6a1SBenny Halevy kfree(args->csa_rclists); 4501796549aSAnna Schumaker return status; 4514aece6a1SBenny Halevy } 4524aece6a1SBenny Halevy 4539733f0d9SAndy Adamson static __be32 decode_recallany_args(struct svc_rqst *rqstp, 45431f09607SAlexandros Batsakis struct xdr_stream *xdr, 45531f09607SAlexandros Batsakis struct cb_recallanyargs *args) 45631f09607SAlexandros Batsakis { 457d743c3c9SPeng Tao uint32_t bitmap[2]; 458d743c3c9SPeng Tao __be32 *p, status; 45931f09607SAlexandros Batsakis 46031f09607SAlexandros Batsakis p = read_buf(xdr, 4); 46131f09607SAlexandros Batsakis if (unlikely(p == NULL)) 46231f09607SAlexandros Batsakis return htonl(NFS4ERR_BADXDR); 46331f09607SAlexandros Batsakis args->craa_objs_to_keep = ntohl(*p++); 464d743c3c9SPeng Tao status = decode_bitmap(xdr, bitmap); 465d743c3c9SPeng Tao if (unlikely(status)) 466d743c3c9SPeng Tao return status; 467d743c3c9SPeng Tao args->craa_type_mask = bitmap[0]; 46831f09607SAlexandros Batsakis 46931f09607SAlexandros Batsakis return 0; 47031f09607SAlexandros Batsakis } 47131f09607SAlexandros Batsakis 4729733f0d9SAndy Adamson static __be32 decode_recallslot_args(struct svc_rqst *rqstp, 473b9efa1b2SAndy Adamson struct xdr_stream *xdr, 474b9efa1b2SAndy Adamson struct cb_recallslotargs *args) 475b9efa1b2SAndy Adamson { 476b9efa1b2SAndy Adamson __be32 *p; 477b9efa1b2SAndy Adamson 478b9efa1b2SAndy Adamson p = read_buf(xdr, 4); 479b9efa1b2SAndy Adamson if (unlikely(p == NULL)) 480b9efa1b2SAndy Adamson return htonl(NFS4ERR_BADXDR); 481d5fb4ce3STrond Myklebust args->crsa_target_highest_slotid = ntohl(*p++); 482b9efa1b2SAndy Adamson return 0; 483b9efa1b2SAndy Adamson } 484b9efa1b2SAndy Adamson 485db783688SJeff Layton static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args) 486db783688SJeff Layton { 487db783688SJeff Layton __be32 *p; 488db783688SJeff Layton unsigned int len; 489db783688SJeff Layton 490db783688SJeff Layton p = read_buf(xdr, 12); 491db783688SJeff Layton if (unlikely(p == NULL)) 492db783688SJeff Layton return htonl(NFS4ERR_BADXDR); 493db783688SJeff Layton 494db783688SJeff Layton p = xdr_decode_hyper(p, &args->cbnl_owner.clientid); 495db783688SJeff Layton len = be32_to_cpu(*p); 496db783688SJeff Layton 497db783688SJeff Layton p = read_buf(xdr, len); 498db783688SJeff Layton if (unlikely(p == NULL)) 499db783688SJeff Layton return htonl(NFS4ERR_BADXDR); 500db783688SJeff Layton 501db783688SJeff Layton /* Only try to decode if the length is right */ 502db783688SJeff Layton if (len == 20) { 503db783688SJeff Layton p += 2; /* skip "lock id:" */ 504db783688SJeff Layton args->cbnl_owner.s_dev = be32_to_cpu(*p++); 505db783688SJeff Layton xdr_decode_hyper(p, &args->cbnl_owner.id); 506db783688SJeff Layton args->cbnl_valid = true; 507db783688SJeff Layton } else { 508db783688SJeff Layton args->cbnl_owner.s_dev = 0; 509db783688SJeff Layton args->cbnl_owner.id = 0; 510db783688SJeff Layton args->cbnl_valid = false; 511db783688SJeff Layton } 512db783688SJeff Layton return 0; 513db783688SJeff Layton } 514db783688SJeff Layton 515db783688SJeff Layton static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_notify_lock_args *args) 516db783688SJeff Layton { 517db783688SJeff Layton __be32 status; 518db783688SJeff Layton 519db783688SJeff Layton status = decode_fh(xdr, &args->cbnl_fh); 520db783688SJeff Layton if (unlikely(status != 0)) 521db783688SJeff Layton return status; 522535ece2bSAnna Schumaker return decode_lockowner(xdr, args); 523db783688SJeff Layton } 524db783688SJeff Layton 5254aece6a1SBenny Halevy #endif /* CONFIG_NFS_V4_1 */ 5264aece6a1SBenny Halevy 527e6f684f6SAl Viro static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str) 5281da177e4SLinus Torvalds { 529ab6e9aafSTrond Myklebust if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0)) 530ab6e9aafSTrond Myklebust return cpu_to_be32(NFS4ERR_RESOURCE); 5311da177e4SLinus Torvalds return 0; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) 5351da177e4SLinus Torvalds #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) 5365704fdebSAl Viro static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep) 5371da177e4SLinus Torvalds { 5385704fdebSAl Viro __be32 bm[2]; 5395704fdebSAl Viro __be32 *p; 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0); 5421da177e4SLinus Torvalds bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1); 5431da177e4SLinus Torvalds if (bm[1] != 0) { 5441da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 16); 5451da177e4SLinus Torvalds if (unlikely(p == NULL)) 5461da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5471da177e4SLinus Torvalds *p++ = htonl(2); 5481da177e4SLinus Torvalds *p++ = bm[0]; 5491da177e4SLinus Torvalds *p++ = bm[1]; 5501da177e4SLinus Torvalds } else if (bm[0] != 0) { 5511da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 12); 5521da177e4SLinus Torvalds if (unlikely(p == NULL)) 5531da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5541da177e4SLinus Torvalds *p++ = htonl(1); 5551da177e4SLinus Torvalds *p++ = bm[0]; 5561da177e4SLinus Torvalds } else { 5571da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 8); 5581da177e4SLinus Torvalds if (unlikely(p == NULL)) 5591da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5601da177e4SLinus Torvalds *p++ = htonl(0); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds *savep = p; 5631da177e4SLinus Torvalds return 0; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 566e6f684f6SAl Viro static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change) 5671da177e4SLinus Torvalds { 5685704fdebSAl Viro __be32 *p; 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds if (!(bitmap[0] & FATTR4_WORD0_CHANGE)) 5711da177e4SLinus Torvalds return 0; 5721da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 8); 57390dc7d27SHarvey Harrison if (unlikely(!p)) 5741da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5751da177e4SLinus Torvalds p = xdr_encode_hyper(p, change); 5761da177e4SLinus Torvalds return 0; 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds 579e6f684f6SAl Viro static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size) 5801da177e4SLinus Torvalds { 5815704fdebSAl Viro __be32 *p; 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds if (!(bitmap[0] & FATTR4_WORD0_SIZE)) 5841da177e4SLinus Torvalds return 0; 5851da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 8); 58690dc7d27SHarvey Harrison if (unlikely(!p)) 5871da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5881da177e4SLinus Torvalds p = xdr_encode_hyper(p, size); 5891da177e4SLinus Torvalds return 0; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 592e6f684f6SAl Viro static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time) 5931da177e4SLinus Torvalds { 5945704fdebSAl Viro __be32 *p; 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 12); 59790dc7d27SHarvey Harrison if (unlikely(!p)) 5981da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5991da177e4SLinus Torvalds p = xdr_encode_hyper(p, time->tv_sec); 6001da177e4SLinus Torvalds *p = htonl(time->tv_nsec); 6011da177e4SLinus Torvalds return 0; 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds 604e6f684f6SAl Viro static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time) 6051da177e4SLinus Torvalds { 6061da177e4SLinus Torvalds if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) 6071da177e4SLinus Torvalds return 0; 6081da177e4SLinus Torvalds return encode_attr_time(xdr,time); 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 611e6f684f6SAl Viro static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time) 6121da177e4SLinus Torvalds { 6131da177e4SLinus Torvalds if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) 6141da177e4SLinus Torvalds return 0; 6151da177e4SLinus Torvalds return encode_attr_time(xdr,time); 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds 618e6f684f6SAl Viro static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr) 6191da177e4SLinus Torvalds { 620e6f684f6SAl Viro __be32 status; 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds hdr->status = xdr_reserve_space(xdr, 4); 6231da177e4SLinus Torvalds if (unlikely(hdr->status == NULL)) 6241da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 6251da177e4SLinus Torvalds status = encode_string(xdr, hdr->taglen, hdr->tag); 6261da177e4SLinus Torvalds if (unlikely(status != 0)) 6271da177e4SLinus Torvalds return status; 6281da177e4SLinus Torvalds hdr->nops = xdr_reserve_space(xdr, 4); 6291da177e4SLinus Torvalds if (unlikely(hdr->nops == NULL)) 6301da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 6311da177e4SLinus Torvalds return 0; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 634e6f684f6SAl Viro static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res) 6351da177e4SLinus Torvalds { 6365704fdebSAl Viro __be32 *p; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 8); 6391da177e4SLinus Torvalds if (unlikely(p == NULL)) 64031d2b435SAndy Adamson return htonl(NFS4ERR_RESOURCE_HDR); 6411da177e4SLinus Torvalds *p++ = htonl(op); 6421da177e4SLinus Torvalds *p = res; 6431da177e4SLinus Torvalds return 0; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 646e6f684f6SAl Viro static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res) 6471da177e4SLinus Torvalds { 6485704fdebSAl Viro __be32 *savep = NULL; 649e6f684f6SAl Viro __be32 status = res->status; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds if (unlikely(status != 0)) 6521da177e4SLinus Torvalds goto out; 6531da177e4SLinus Torvalds status = encode_attr_bitmap(xdr, res->bitmap, &savep); 6541da177e4SLinus Torvalds if (unlikely(status != 0)) 6551da177e4SLinus Torvalds goto out; 6561da177e4SLinus Torvalds status = encode_attr_change(xdr, res->bitmap, res->change_attr); 6571da177e4SLinus Torvalds if (unlikely(status != 0)) 6581da177e4SLinus Torvalds goto out; 6591da177e4SLinus Torvalds status = encode_attr_size(xdr, res->bitmap, res->size); 6601da177e4SLinus Torvalds if (unlikely(status != 0)) 6611da177e4SLinus Torvalds goto out; 6621da177e4SLinus Torvalds status = encode_attr_ctime(xdr, res->bitmap, &res->ctime); 6631da177e4SLinus Torvalds if (unlikely(status != 0)) 6641da177e4SLinus Torvalds goto out; 6651da177e4SLinus Torvalds status = encode_attr_mtime(xdr, res->bitmap, &res->mtime); 6661da177e4SLinus Torvalds *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1))); 6671da177e4SLinus Torvalds out: 6683110ff80SHarvey Harrison dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); 6691da177e4SLinus Torvalds return status; 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 67234bc47c9SBenny Halevy #if defined(CONFIG_NFS_V4_1) 67334bc47c9SBenny Halevy 6749733f0d9SAndy Adamson static __be32 encode_sessionid(struct xdr_stream *xdr, 6754aece6a1SBenny Halevy const struct nfs4_sessionid *sid) 6764aece6a1SBenny Halevy { 6779733f0d9SAndy Adamson __be32 *p; 6784aece6a1SBenny Halevy 679590184a6SKinglong Mee p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN); 6804aece6a1SBenny Halevy if (unlikely(p == NULL)) 6814aece6a1SBenny Halevy return htonl(NFS4ERR_RESOURCE); 6824aece6a1SBenny Halevy 683590184a6SKinglong Mee memcpy(p, sid, NFS4_MAX_SESSIONID_LEN); 6844aece6a1SBenny Halevy return 0; 6854aece6a1SBenny Halevy } 6864aece6a1SBenny Halevy 6879733f0d9SAndy Adamson static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp, 6884aece6a1SBenny Halevy struct xdr_stream *xdr, 6894aece6a1SBenny Halevy const struct cb_sequenceres *res) 6904aece6a1SBenny Halevy { 6919733f0d9SAndy Adamson __be32 *p; 692e216c8c7SDan Carpenter __be32 status = res->csr_status; 6934aece6a1SBenny Halevy 6944aece6a1SBenny Halevy if (unlikely(status != 0)) 6953d0bfaa6SAnna Schumaker return status; 6964aece6a1SBenny Halevy 697e0a63c0bSKinglong Mee status = encode_sessionid(xdr, &res->csr_sessionid); 698e0a63c0bSKinglong Mee if (status) 6993d0bfaa6SAnna Schumaker return status; 7004aece6a1SBenny Halevy 7014aece6a1SBenny Halevy p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t)); 7024aece6a1SBenny Halevy if (unlikely(p == NULL)) 7034aece6a1SBenny Halevy return htonl(NFS4ERR_RESOURCE); 7044aece6a1SBenny Halevy 7054aece6a1SBenny Halevy *p++ = htonl(res->csr_sequenceid); 7064aece6a1SBenny Halevy *p++ = htonl(res->csr_slotid); 7074aece6a1SBenny Halevy *p++ = htonl(res->csr_highestslotid); 7084aece6a1SBenny Halevy *p++ = htonl(res->csr_target_highestslotid); 7093d0bfaa6SAnna Schumaker return 0; 7104aece6a1SBenny Halevy } 7114aece6a1SBenny Halevy 71234bc47c9SBenny Halevy static __be32 71334bc47c9SBenny Halevy preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) 71434bc47c9SBenny Halevy { 715281fe15dSBenny Halevy if (op_nr == OP_CB_SEQUENCE) { 716281fe15dSBenny Halevy if (nop != 0) 717281fe15dSBenny Halevy return htonl(NFS4ERR_SEQUENCE_POS); 718281fe15dSBenny Halevy } else { 719281fe15dSBenny Halevy if (nop == 0) 720281fe15dSBenny Halevy return htonl(NFS4ERR_OP_NOT_IN_SESSION); 721281fe15dSBenny Halevy } 722281fe15dSBenny Halevy 72334bc47c9SBenny Halevy switch (op_nr) { 72434bc47c9SBenny Halevy case OP_CB_GETATTR: 72534bc47c9SBenny Halevy case OP_CB_RECALL: 7264aece6a1SBenny Halevy case OP_CB_SEQUENCE: 72731f09607SAlexandros Batsakis case OP_CB_RECALL_ANY: 728b9efa1b2SAndy Adamson case OP_CB_RECALL_SLOT: 729f2a62561SFred Isaman case OP_CB_LAYOUTRECALL: 7301be5683bSMarc Eshel case OP_CB_NOTIFY_DEVICEID: 731db783688SJeff Layton case OP_CB_NOTIFY_LOCK: 73234bc47c9SBenny Halevy *op = &callback_ops[op_nr]; 73334bc47c9SBenny Halevy break; 73434bc47c9SBenny Halevy 73534bc47c9SBenny Halevy case OP_CB_NOTIFY: 73634bc47c9SBenny Halevy case OP_CB_PUSH_DELEG: 73734bc47c9SBenny Halevy case OP_CB_RECALLABLE_OBJ_AVAIL: 73834bc47c9SBenny Halevy case OP_CB_WANTS_CANCELLED: 73934bc47c9SBenny Halevy return htonl(NFS4ERR_NOTSUPP); 74034bc47c9SBenny Halevy 74134bc47c9SBenny Halevy default: 74234bc47c9SBenny Halevy return htonl(NFS4ERR_OP_ILLEGAL); 74334bc47c9SBenny Halevy } 74434bc47c9SBenny Halevy 74534bc47c9SBenny Halevy return htonl(NFS_OK); 74634bc47c9SBenny Halevy } 74734bc47c9SBenny Halevy 748810d82e6STrond Myklebust static void nfs4_callback_free_slot(struct nfs4_session *session, 749810d82e6STrond Myklebust struct nfs4_slot *slot) 75042acd021SAndy Adamson { 75142acd021SAndy Adamson struct nfs4_slot_table *tbl = &session->bc_slot_table; 75242acd021SAndy Adamson 75342acd021SAndy Adamson spin_lock(&tbl->slot_tbl_lock); 75442acd021SAndy Adamson /* 75542acd021SAndy Adamson * Let the state manager know callback processing done. 75642acd021SAndy Adamson * A single slot, so highest used slotid is either 0 or -1 75742acd021SAndy Adamson */ 758810d82e6STrond Myklebust nfs4_free_slot(tbl, slot); 759774d5f14SAndy Adamson nfs4_slot_tbl_drain_complete(tbl); 76042acd021SAndy Adamson spin_unlock(&tbl->slot_tbl_lock); 76142acd021SAndy Adamson } 76242acd021SAndy Adamson 76355a67399STrond Myklebust static void nfs4_cb_free_slot(struct cb_process_state *cps) 76442acd021SAndy Adamson { 765810d82e6STrond Myklebust if (cps->slot) { 766810d82e6STrond Myklebust nfs4_callback_free_slot(cps->clp->cl_session, cps->slot); 767810d82e6STrond Myklebust cps->slot = NULL; 768810d82e6STrond Myklebust } 76942acd021SAndy Adamson } 77042acd021SAndy Adamson 77134bc47c9SBenny Halevy #else /* CONFIG_NFS_V4_1 */ 77234bc47c9SBenny Halevy 77334bc47c9SBenny Halevy static __be32 77434bc47c9SBenny Halevy preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) 77534bc47c9SBenny Halevy { 77634bc47c9SBenny Halevy return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 77734bc47c9SBenny Halevy } 77834bc47c9SBenny Halevy 77955a67399STrond Myklebust static void nfs4_cb_free_slot(struct cb_process_state *cps) 78042acd021SAndy Adamson { 78142acd021SAndy Adamson } 78234bc47c9SBenny Halevy #endif /* CONFIG_NFS_V4_1 */ 78334bc47c9SBenny Halevy 7846b140b85SBryan Schumaker #ifdef CONFIG_NFS_V4_2 7856b140b85SBryan Schumaker static __be32 7866b140b85SBryan Schumaker preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op) 7876b140b85SBryan Schumaker { 7886b140b85SBryan Schumaker __be32 status = preprocess_nfs41_op(nop, op_nr, op); 7896b140b85SBryan Schumaker if (status != htonl(NFS4ERR_OP_ILLEGAL)) 7906b140b85SBryan Schumaker return status; 7916b140b85SBryan Schumaker 7926b140b85SBryan Schumaker if (op_nr == OP_CB_OFFLOAD) 7936b140b85SBryan Schumaker return htonl(NFS4ERR_NOTSUPP); 7946b140b85SBryan Schumaker return htonl(NFS4ERR_OP_ILLEGAL); 7956b140b85SBryan Schumaker } 7966b140b85SBryan Schumaker #else /* CONFIG_NFS_V4_2 */ 7976b140b85SBryan Schumaker static __be32 7986b140b85SBryan Schumaker preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op) 7996b140b85SBryan Schumaker { 8006b140b85SBryan Schumaker return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 8016b140b85SBryan Schumaker } 8026b140b85SBryan Schumaker #endif /* CONFIG_NFS_V4_2 */ 8036b140b85SBryan Schumaker 80434bc47c9SBenny Halevy static __be32 80534bc47c9SBenny Halevy preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op) 80634bc47c9SBenny Halevy { 80734bc47c9SBenny Halevy switch (op_nr) { 80834bc47c9SBenny Halevy case OP_CB_GETATTR: 80934bc47c9SBenny Halevy case OP_CB_RECALL: 81034bc47c9SBenny Halevy *op = &callback_ops[op_nr]; 81134bc47c9SBenny Halevy break; 81234bc47c9SBenny Halevy default: 81334bc47c9SBenny Halevy return htonl(NFS4ERR_OP_ILLEGAL); 81434bc47c9SBenny Halevy } 81534bc47c9SBenny Halevy 81634bc47c9SBenny Halevy return htonl(NFS_OK); 81734bc47c9SBenny Halevy } 81834bc47c9SBenny Halevy 819459de2edSBryan Schumaker static __be32 process_op(int nop, struct svc_rqst *rqstp, 8201da177e4SLinus Torvalds struct xdr_stream *xdr_in, void *argp, 821c36fca52SAndy Adamson struct xdr_stream *xdr_out, void *resp, 822c36fca52SAndy Adamson struct cb_process_state *cps) 8231da177e4SLinus Torvalds { 824a162a6b8STrond Myklebust struct callback_op *op = &callback_ops[0]; 82531d2b435SAndy Adamson unsigned int op_nr; 82634bc47c9SBenny Halevy __be32 status; 8271da177e4SLinus Torvalds long maxlen; 828e6f684f6SAl Viro __be32 res; 8291da177e4SLinus Torvalds 8303110ff80SHarvey Harrison dprintk("%s: start\n", __func__); 8311da177e4SLinus Torvalds status = decode_op_hdr(xdr_in, &op_nr); 83231d2b435SAndy Adamson if (unlikely(status)) 83331d2b435SAndy Adamson return status; 8341da177e4SLinus Torvalds 83534bc47c9SBenny Halevy dprintk("%s: minorversion=%d nop=%d op_nr=%u\n", 836459de2edSBryan Schumaker __func__, cps->minorversion, nop, op_nr); 83734bc47c9SBenny Halevy 8386b140b85SBryan Schumaker switch (cps->minorversion) { 8396b140b85SBryan Schumaker case 0: 8406b140b85SBryan Schumaker status = preprocess_nfs4_op(op_nr, &op); 8416b140b85SBryan Schumaker break; 8426b140b85SBryan Schumaker case 1: 8436b140b85SBryan Schumaker status = preprocess_nfs41_op(nop, op_nr, &op); 8446b140b85SBryan Schumaker break; 8456b140b85SBryan Schumaker case 2: 8466b140b85SBryan Schumaker status = preprocess_nfs42_op(nop, op_nr, &op); 8476b140b85SBryan Schumaker break; 8486b140b85SBryan Schumaker default: 8496b140b85SBryan Schumaker status = htonl(NFS4ERR_MINOR_VERS_MISMATCH); 8506b140b85SBryan Schumaker } 8516b140b85SBryan Schumaker 85234bc47c9SBenny Halevy if (status == htonl(NFS4ERR_OP_ILLEGAL)) 85334bc47c9SBenny Halevy op_nr = OP_CB_ILLEGAL; 854b92b3019SAndy Adamson if (status) 855b92b3019SAndy Adamson goto encode_hdr; 85631d2b435SAndy Adamson 857c36fca52SAndy Adamson if (cps->drc_status) { 858c36fca52SAndy Adamson status = cps->drc_status; 8594911096fSAndy Adamson goto encode_hdr; 8604911096fSAndy Adamson } 8614911096fSAndy Adamson 8621da177e4SLinus Torvalds maxlen = xdr_out->end - xdr_out->p; 8631da177e4SLinus Torvalds if (maxlen > 0 && maxlen < PAGE_SIZE) { 8641da177e4SLinus Torvalds status = op->decode_args(rqstp, xdr_in, argp); 865e95e60daSAndy Adamson if (likely(status == 0)) 866c36fca52SAndy Adamson status = op->process_op(argp, resp, cps); 8671da177e4SLinus Torvalds } else 8681da177e4SLinus Torvalds status = htonl(NFS4ERR_RESOURCE); 8691da177e4SLinus Torvalds 870b92b3019SAndy Adamson encode_hdr: 8711da177e4SLinus Torvalds res = encode_op_hdr(xdr_out, op_nr, status); 87231d2b435SAndy Adamson if (unlikely(res)) 87331d2b435SAndy Adamson return res; 8741da177e4SLinus Torvalds if (op->encode_res != NULL && status == 0) 8751da177e4SLinus Torvalds status = op->encode_res(rqstp, xdr_out, resp); 8763110ff80SHarvey Harrison dprintk("%s: done, status = %d\n", __func__, ntohl(status)); 8771da177e4SLinus Torvalds return status; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds /* 8811da177e4SLinus Torvalds * Decode, process and encode a COMPOUND 8821da177e4SLinus Torvalds */ 8837111c66eSAl Viro static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp) 8841da177e4SLinus Torvalds { 8853a6258e1STrond Myklebust struct cb_compound_hdr_arg hdr_arg = { 0 }; 8863a6258e1STrond Myklebust struct cb_compound_hdr_res hdr_res = { NULL }; 8871da177e4SLinus Torvalds struct xdr_stream xdr_in, xdr_out; 888c36fca52SAndy Adamson __be32 *p, status; 889c36fca52SAndy Adamson struct cb_process_state cps = { 890c36fca52SAndy Adamson .drc_status = 0, 891c36fca52SAndy Adamson .clp = NULL, 8929695c705SStanislav Kinsbursky .net = SVC_NET(rqstp), 893c36fca52SAndy Adamson }; 8943a6258e1STrond Myklebust unsigned int nops = 0; 8951da177e4SLinus Torvalds 8963110ff80SHarvey Harrison dprintk("%s: start\n", __func__); 8971da177e4SLinus Torvalds 898756b9b37STrond Myklebust xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base); 8991da177e4SLinus Torvalds 9005704fdebSAl Viro p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len); 9011da177e4SLinus Torvalds xdr_init_encode(&xdr_out, &rqstp->rq_res, p); 9021da177e4SLinus Torvalds 9033a6258e1STrond Myklebust status = decode_compound_hdr_arg(&xdr_in, &hdr_arg); 9044ed0d83dSVaishali Thakkar if (status == htonl(NFS4ERR_RESOURCE)) 9053a6258e1STrond Myklebust return rpc_garbage_args; 9063a6258e1STrond Myklebust 907c36fca52SAndy Adamson if (hdr_arg.minorversion == 0) { 9089695c705SStanislav Kinsbursky cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident); 909778be232SAndy Adamson if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp)) 910a4e187d8SChuck Lever goto out_invalidcred; 911778be232SAndy Adamson } 912c36fca52SAndy Adamson 913459de2edSBryan Schumaker cps.minorversion = hdr_arg.minorversion; 9141da177e4SLinus Torvalds hdr_res.taglen = hdr_arg.taglen; 9151da177e4SLinus Torvalds hdr_res.tag = hdr_arg.tag; 9163a6258e1STrond Myklebust if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) 9173a6258e1STrond Myklebust return rpc_system_err; 9181da177e4SLinus Torvalds 9193a6258e1STrond Myklebust while (status == 0 && nops != hdr_arg.nops) { 920459de2edSBryan Schumaker status = process_op(nops, rqstp, &xdr_in, 921459de2edSBryan Schumaker argp, &xdr_out, resp, &cps); 9221da177e4SLinus Torvalds nops++; 9231da177e4SLinus Torvalds } 9243a6258e1STrond Myklebust 92531d2b435SAndy Adamson /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return 92631d2b435SAndy Adamson * resource error in cb_compound status without returning op */ 92731d2b435SAndy Adamson if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) { 92831d2b435SAndy Adamson status = htonl(NFS4ERR_RESOURCE); 92931d2b435SAndy Adamson nops--; 93031d2b435SAndy Adamson } 93131d2b435SAndy Adamson 9321da177e4SLinus Torvalds *hdr_res.status = status; 9331da177e4SLinus Torvalds *hdr_res.nops = htonl(nops); 93455a67399STrond Myklebust nfs4_cb_free_slot(&cps); 935c36fca52SAndy Adamson nfs_put_client(cps.clp); 9363110ff80SHarvey Harrison dprintk("%s: done, status = %u\n", __func__, ntohl(status)); 9371da177e4SLinus Torvalds return rpc_success; 938a4e187d8SChuck Lever 939a4e187d8SChuck Lever out_invalidcred: 940a4e187d8SChuck Lever pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n"); 941a4e187d8SChuck Lever return rpc_autherr_badcred; 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds /* 9451da177e4SLinus Torvalds * Define NFS4 callback COMPOUND ops. 9461da177e4SLinus Torvalds */ 9471da177e4SLinus Torvalds static struct callback_op callback_ops[] = { 9481da177e4SLinus Torvalds [0] = { 9491da177e4SLinus Torvalds .res_maxsize = CB_OP_HDR_RES_MAXSZ, 9501da177e4SLinus Torvalds }, 9511da177e4SLinus Torvalds [OP_CB_GETATTR] = { 9521da177e4SLinus Torvalds .process_op = (callback_process_op_t)nfs4_callback_getattr, 9531da177e4SLinus Torvalds .decode_args = (callback_decode_arg_t)decode_getattr_args, 9541da177e4SLinus Torvalds .encode_res = (callback_encode_res_t)encode_getattr_res, 9551da177e4SLinus Torvalds .res_maxsize = CB_OP_GETATTR_RES_MAXSZ, 9561da177e4SLinus Torvalds }, 9571da177e4SLinus Torvalds [OP_CB_RECALL] = { 9581da177e4SLinus Torvalds .process_op = (callback_process_op_t)nfs4_callback_recall, 9591da177e4SLinus Torvalds .decode_args = (callback_decode_arg_t)decode_recall_args, 9601da177e4SLinus Torvalds .res_maxsize = CB_OP_RECALL_RES_MAXSZ, 9614aece6a1SBenny Halevy }, 9624aece6a1SBenny Halevy #if defined(CONFIG_NFS_V4_1) 963f2a62561SFred Isaman [OP_CB_LAYOUTRECALL] = { 964f2a62561SFred Isaman .process_op = (callback_process_op_t)nfs4_callback_layoutrecall, 965f2a62561SFred Isaman .decode_args = 966f2a62561SFred Isaman (callback_decode_arg_t)decode_layoutrecall_args, 967f2a62561SFred Isaman .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ, 968f2a62561SFred Isaman }, 9691be5683bSMarc Eshel [OP_CB_NOTIFY_DEVICEID] = { 9701be5683bSMarc Eshel .process_op = (callback_process_op_t)nfs4_callback_devicenotify, 9711be5683bSMarc Eshel .decode_args = 9721be5683bSMarc Eshel (callback_decode_arg_t)decode_devicenotify_args, 9731be5683bSMarc Eshel .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ, 9741be5683bSMarc Eshel }, 9754aece6a1SBenny Halevy [OP_CB_SEQUENCE] = { 9764aece6a1SBenny Halevy .process_op = (callback_process_op_t)nfs4_callback_sequence, 9774aece6a1SBenny Halevy .decode_args = (callback_decode_arg_t)decode_cb_sequence_args, 9784aece6a1SBenny Halevy .encode_res = (callback_encode_res_t)encode_cb_sequence_res, 9794aece6a1SBenny Halevy .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ, 9804aece6a1SBenny Halevy }, 98131f09607SAlexandros Batsakis [OP_CB_RECALL_ANY] = { 98231f09607SAlexandros Batsakis .process_op = (callback_process_op_t)nfs4_callback_recallany, 98331f09607SAlexandros Batsakis .decode_args = (callback_decode_arg_t)decode_recallany_args, 98431f09607SAlexandros Batsakis .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ, 98531f09607SAlexandros Batsakis }, 986b9efa1b2SAndy Adamson [OP_CB_RECALL_SLOT] = { 987b9efa1b2SAndy Adamson .process_op = (callback_process_op_t)nfs4_callback_recallslot, 988b9efa1b2SAndy Adamson .decode_args = (callback_decode_arg_t)decode_recallslot_args, 989b9efa1b2SAndy Adamson .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ, 990b9efa1b2SAndy Adamson }, 991db783688SJeff Layton [OP_CB_NOTIFY_LOCK] = { 992db783688SJeff Layton .process_op = (callback_process_op_t)nfs4_callback_notify_lock, 993db783688SJeff Layton .decode_args = (callback_decode_arg_t)decode_notify_lock_args, 994db783688SJeff Layton .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ, 995db783688SJeff Layton }, 9964aece6a1SBenny Halevy #endif /* CONFIG_NFS_V4_1 */ 9971da177e4SLinus Torvalds }; 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds /* 10001da177e4SLinus Torvalds * Define NFS4 callback procedures 10011da177e4SLinus Torvalds */ 10021da177e4SLinus Torvalds static struct svc_procedure nfs4_callback_procedures1[] = { 10031da177e4SLinus Torvalds [CB_NULL] = { 10041da177e4SLinus Torvalds .pc_func = nfs4_callback_null, 10051da177e4SLinus Torvalds .pc_decode = (kxdrproc_t)nfs4_decode_void, 10061da177e4SLinus Torvalds .pc_encode = (kxdrproc_t)nfs4_encode_void, 10071da177e4SLinus Torvalds .pc_xdrressize = 1, 10081da177e4SLinus Torvalds }, 10091da177e4SLinus Torvalds [CB_COMPOUND] = { 10101da177e4SLinus Torvalds .pc_func = nfs4_callback_compound, 10111da177e4SLinus Torvalds .pc_encode = (kxdrproc_t)nfs4_encode_void, 10121da177e4SLinus Torvalds .pc_argsize = 256, 10131da177e4SLinus Torvalds .pc_ressize = 256, 10141da177e4SLinus Torvalds .pc_xdrressize = NFS4_CALLBACK_BUFSIZE, 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds }; 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds struct svc_version nfs4_callback_version1 = { 10191da177e4SLinus Torvalds .vs_vers = 1, 10201da177e4SLinus Torvalds .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), 10211da177e4SLinus Torvalds .vs_proc = nfs4_callback_procedures1, 10221da177e4SLinus Torvalds .vs_xdrsize = NFS4_CALLBACK_XDRSIZE, 10231da177e4SLinus Torvalds .vs_dispatch = NULL, 102405a45a2dSJeff Layton .vs_hidden = true, 10255283b03eSJeff Layton .vs_need_cong_ctrl = true, 10261da177e4SLinus Torvalds }; 10271da177e4SLinus Torvalds 102807bccc2dSAlexandros Batsakis struct svc_version nfs4_callback_version4 = { 102907bccc2dSAlexandros Batsakis .vs_vers = 4, 103007bccc2dSAlexandros Batsakis .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), 103107bccc2dSAlexandros Batsakis .vs_proc = nfs4_callback_procedures1, 103207bccc2dSAlexandros Batsakis .vs_xdrsize = NFS4_CALLBACK_XDRSIZE, 103307bccc2dSAlexandros Batsakis .vs_dispatch = NULL, 103405a45a2dSJeff Layton .vs_hidden = true, 10355283b03eSJeff Layton .vs_need_cong_ctrl = true, 103607bccc2dSAlexandros Batsakis }; 1037