1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/nfs/callback_xdr.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2004 Trond Myklebust 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * NFSv4 callback encode/decode procedures 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds #include <linux/kernel.h> 101da177e4SLinus Torvalds #include <linux/sunrpc/svc.h> 111da177e4SLinus Torvalds #include <linux/nfs4.h> 121da177e4SLinus Torvalds #include <linux/nfs_fs.h> 139a3ba432STrond Myklebust #include <linux/ratelimit.h> 149a3ba432STrond Myklebust #include <linux/printk.h> 155a0e3ad6STejun Heo #include <linux/slab.h> 16c36fca52SAndy Adamson #include <linux/sunrpc/bc_xprt.h> 174ce79717STrond Myklebust #include "nfs4_fs.h" 181da177e4SLinus Torvalds #include "callback.h" 19c36fca52SAndy Adamson #include "internal.h" 2076e697baSTrond Myklebust #include "nfs4session.h" 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #define CB_OP_TAGLEN_MAXSZ (512) 2345724e8aSKinglong Mee #define CB_OP_HDR_RES_MAXSZ (2 * 4) // opcode, status 2445724e8aSKinglong Mee #define CB_OP_GETATTR_BITMAP_MAXSZ (4 * 4) // bitmap length, 3 bitmaps 251da177e4SLinus Torvalds #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \ 261da177e4SLinus Torvalds CB_OP_GETATTR_BITMAP_MAXSZ + \ 2745724e8aSKinglong Mee /* change, size, ctime, mtime */\ 2845724e8aSKinglong Mee (2 + 2 + 3 + 3) * 4) 291da177e4SLinus Torvalds #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 301da177e4SLinus Torvalds 314aece6a1SBenny Halevy #if defined(CONFIG_NFS_V4_1) 32f2a62561SFred Isaman #define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 331be5683bSMarc Eshel #define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 344aece6a1SBenny Halevy #define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \ 3545724e8aSKinglong Mee NFS4_MAX_SESSIONID_LEN + \ 3645724e8aSKinglong Mee (1 + 3) * 4) // seqid, 3 slotids 3731f09607SAlexandros Batsakis #define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 38b9efa1b2SAndy Adamson #define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 39db783688SJeff Layton #define CB_OP_NOTIFY_LOCK_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 404aece6a1SBenny Halevy #endif /* CONFIG_NFS_V4_1 */ 414aece6a1SBenny Halevy 421da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_CALLBACK 431da177e4SLinus Torvalds 4431d2b435SAndy Adamson /* Internal error code */ 4531d2b435SAndy Adamson #define NFS4ERR_RESOURCE_HDR 11050 4631d2b435SAndy Adamson 471da177e4SLinus Torvalds struct callback_op { 48f4dac4adSChristoph Hellwig __be32 (*process_op)(void *, void *, struct cb_process_state *); 49f4dac4adSChristoph Hellwig __be32 (*decode_args)(struct svc_rqst *, struct xdr_stream *, void *); 50f4dac4adSChristoph Hellwig __be32 (*encode_res)(struct svc_rqst *, struct xdr_stream *, 51f4dac4adSChristoph Hellwig const void *); 521da177e4SLinus Torvalds long res_maxsize; 531da177e4SLinus Torvalds }; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds static struct callback_op callback_ops[]; 561da177e4SLinus Torvalds 57a6beb732SChristoph Hellwig static __be32 nfs4_callback_null(struct svc_rqst *rqstp) 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds return htonl(NFS4_OK); 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 62026fec7eSChristoph Hellwig static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p) 631da177e4SLinus Torvalds { 641da177e4SLinus Torvalds return xdr_argsize_check(rqstp, p); 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds 6763f8de37SChristoph Hellwig static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds return xdr_ressize_check(rqstp, p); 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds 72b60475c9SJeff Layton static __be32 *read_buf(struct xdr_stream *xdr, size_t nbytes) 731da177e4SLinus Torvalds { 745704fdebSAl Viro __be32 *p; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds p = xdr_inline_decode(xdr, nbytes); 771da177e4SLinus Torvalds if (unlikely(p == NULL)) 78756b9b37STrond Myklebust printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n"); 791da177e4SLinus Torvalds return p; 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 82c065eeeaSTrond Myklebust static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, 83c065eeeaSTrond Myklebust const char **str, size_t maxlen) 841da177e4SLinus Torvalds { 85c065eeeaSTrond Myklebust ssize_t err; 861da177e4SLinus Torvalds 87c065eeeaSTrond Myklebust err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen); 88c065eeeaSTrond Myklebust if (err < 0) 89c065eeeaSTrond Myklebust return cpu_to_be32(NFS4ERR_RESOURCE); 90c065eeeaSTrond Myklebust *len = err; 911da177e4SLinus Torvalds return 0; 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds 94e6f684f6SAl Viro static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh) 951da177e4SLinus Torvalds { 965704fdebSAl Viro __be32 *p; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds p = read_buf(xdr, 4); 991da177e4SLinus Torvalds if (unlikely(p == NULL)) 1001da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 1011da177e4SLinus Torvalds fh->size = ntohl(*p); 1021da177e4SLinus Torvalds if (fh->size > NFS4_FHSIZE) 1031da177e4SLinus Torvalds return htonl(NFS4ERR_BADHANDLE); 1041da177e4SLinus Torvalds p = read_buf(xdr, fh->size); 1051da177e4SLinus Torvalds if (unlikely(p == NULL)) 1061da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 1071da177e4SLinus Torvalds memcpy(&fh->data[0], p, fh->size); 1081da177e4SLinus Torvalds memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size); 1091da177e4SLinus Torvalds return 0; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 112e6f684f6SAl Viro static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap) 1131da177e4SLinus Torvalds { 1145704fdebSAl Viro __be32 *p; 1151da177e4SLinus Torvalds unsigned int attrlen; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds p = read_buf(xdr, 4); 1181da177e4SLinus Torvalds if (unlikely(p == NULL)) 1191da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 1201da177e4SLinus Torvalds attrlen = ntohl(*p); 1211da177e4SLinus Torvalds p = read_buf(xdr, attrlen << 2); 1221da177e4SLinus Torvalds if (unlikely(p == NULL)) 1231da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 1241da177e4SLinus Torvalds if (likely(attrlen > 0)) 1251da177e4SLinus Torvalds bitmap[0] = ntohl(*p++); 1261da177e4SLinus Torvalds if (attrlen > 1) 1271da177e4SLinus Torvalds bitmap[1] = ntohl(*p); 1281da177e4SLinus Torvalds return 0; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 131e6f684f6SAl Viro static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 1321da177e4SLinus Torvalds { 1335704fdebSAl Viro __be32 *p; 1341da177e4SLinus Torvalds 1352d2f24adSTrond Myklebust p = read_buf(xdr, NFS4_STATEID_SIZE); 1361da177e4SLinus Torvalds if (unlikely(p == NULL)) 1371da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 13893b717fdSTrond Myklebust memcpy(stateid->data, p, NFS4_STATEID_SIZE); 1391da177e4SLinus Torvalds return 0; 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 14293b717fdSTrond Myklebust static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 14393b717fdSTrond Myklebust { 14493b717fdSTrond Myklebust stateid->type = NFS4_DELEGATION_STATEID_TYPE; 14593b717fdSTrond Myklebust return decode_stateid(xdr, stateid); 14693b717fdSTrond Myklebust } 14793b717fdSTrond Myklebust 148e6f684f6SAl Viro static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr) 1491da177e4SLinus Torvalds { 1505704fdebSAl Viro __be32 *p; 151e6f684f6SAl Viro __be32 status; 1521da177e4SLinus Torvalds 153c065eeeaSTrond Myklebust status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ); 1541da177e4SLinus Torvalds if (unlikely(status != 0)) 1551da177e4SLinus Torvalds return status; 1561da177e4SLinus Torvalds p = read_buf(xdr, 12); 1571da177e4SLinus Torvalds if (unlikely(p == NULL)) 1581da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 159b8f2ef84SBenny Halevy hdr->minorversion = ntohl(*p++); 160459de2edSBryan Schumaker /* Check for minor version support */ 161459de2edSBryan Schumaker if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) { 16242c2c424SSteve Dickson hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */ 16348a9e2d2SBenny Halevy } else { 1649a3ba432STrond Myklebust pr_warn_ratelimited("NFS: %s: NFSv4 server callback with " 165b8f2ef84SBenny Halevy "illegal minor version %u!\n", 166b8f2ef84SBenny Halevy __func__, hdr->minorversion); 1671da177e4SLinus Torvalds return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds hdr->nops = ntohl(*p); 1701da177e4SLinus Torvalds return 0; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 173e6f684f6SAl Viro static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op) 1741da177e4SLinus Torvalds { 1755704fdebSAl Viro __be32 *p; 1761da177e4SLinus Torvalds p = read_buf(xdr, 4); 1771da177e4SLinus Torvalds if (unlikely(p == NULL)) 17831d2b435SAndy Adamson return htonl(NFS4ERR_RESOURCE_HDR); 1791da177e4SLinus Torvalds *op = ntohl(*p); 1801da177e4SLinus Torvalds return 0; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 183f4dac4adSChristoph Hellwig static __be32 decode_getattr_args(struct svc_rqst *rqstp, 184f4dac4adSChristoph Hellwig struct xdr_stream *xdr, void *argp) 1851da177e4SLinus Torvalds { 186f4dac4adSChristoph Hellwig struct cb_getattrargs *args = argp; 187e6f684f6SAl Viro __be32 status; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds status = decode_fh(xdr, &args->fh); 1901da177e4SLinus Torvalds if (unlikely(status != 0)) 1911da177e4SLinus Torvalds return status; 19256938bb7SAnna Schumaker return decode_bitmap(xdr, args->bitmap); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 195f4dac4adSChristoph Hellwig static __be32 decode_recall_args(struct svc_rqst *rqstp, 196f4dac4adSChristoph Hellwig struct xdr_stream *xdr, void *argp) 1971da177e4SLinus Torvalds { 198f4dac4adSChristoph Hellwig struct cb_recallargs *args = argp; 1995704fdebSAl Viro __be32 *p; 200e6f684f6SAl Viro __be32 status; 2011da177e4SLinus Torvalds 20293b717fdSTrond Myklebust status = decode_delegation_stateid(xdr, &args->stateid); 2031da177e4SLinus Torvalds if (unlikely(status != 0)) 2043873bc50SAlexey Dobriyan return status; 205135a4ea0SAnna Schumaker p = read_buf(xdr, 4); 206135a4ea0SAnna Schumaker if (unlikely(p == NULL)) 207135a4ea0SAnna Schumaker return htonl(NFS4ERR_RESOURCE); 208135a4ea0SAnna Schumaker args->truncate = ntohl(*p); 209135a4ea0SAnna Schumaker return decode_fh(xdr, &args->fh); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2124aece6a1SBenny Halevy #if defined(CONFIG_NFS_V4_1) 21393b717fdSTrond Myklebust static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 21493b717fdSTrond Myklebust { 21593b717fdSTrond Myklebust stateid->type = NFS4_LAYOUT_STATEID_TYPE; 21693b717fdSTrond Myklebust return decode_stateid(xdr, stateid); 21793b717fdSTrond Myklebust } 2184aece6a1SBenny Halevy 219f2a62561SFred Isaman static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp, 220f4dac4adSChristoph Hellwig struct xdr_stream *xdr, void *argp) 221f2a62561SFred Isaman { 222f4dac4adSChristoph Hellwig struct cb_layoutrecallargs *args = argp; 223f2a62561SFred Isaman __be32 *p; 224f2a62561SFred Isaman __be32 status = 0; 225f2a62561SFred Isaman uint32_t iomode; 226f2a62561SFred Isaman 227f2a62561SFred Isaman p = read_buf(xdr, 4 * sizeof(uint32_t)); 228c79d56d2SAnna Schumaker if (unlikely(p == NULL)) 229c79d56d2SAnna Schumaker return htonl(NFS4ERR_BADXDR); 230f2a62561SFred Isaman 231f2a62561SFred Isaman args->cbl_layout_type = ntohl(*p++); 232f2a62561SFred Isaman /* Depite the spec's xdr, iomode really belongs in the FILE switch, 23325985edcSLucas De Marchi * as it is unusable and ignored with the other types. 234f2a62561SFred Isaman */ 235f2a62561SFred Isaman iomode = ntohl(*p++); 236f2a62561SFred Isaman args->cbl_layoutchanged = ntohl(*p++); 237f2a62561SFred Isaman args->cbl_recall_type = ntohl(*p++); 238f2a62561SFred Isaman 239f2a62561SFred Isaman if (args->cbl_recall_type == RETURN_FILE) { 240f2a62561SFred Isaman args->cbl_range.iomode = iomode; 241f2a62561SFred Isaman status = decode_fh(xdr, &args->cbl_fh); 242f2a62561SFred Isaman if (unlikely(status != 0)) 243c79d56d2SAnna Schumaker return status; 244f2a62561SFred Isaman 245f2a62561SFred Isaman p = read_buf(xdr, 2 * sizeof(uint64_t)); 246c79d56d2SAnna Schumaker if (unlikely(p == NULL)) 247c79d56d2SAnna Schumaker return htonl(NFS4ERR_BADXDR); 248f2a62561SFred Isaman p = xdr_decode_hyper(p, &args->cbl_range.offset); 249f2a62561SFred Isaman p = xdr_decode_hyper(p, &args->cbl_range.length); 250c79d56d2SAnna Schumaker return decode_layout_stateid(xdr, &args->cbl_stateid); 251f2a62561SFred Isaman } else if (args->cbl_recall_type == RETURN_FSID) { 252f2a62561SFred Isaman p = read_buf(xdr, 2 * sizeof(uint64_t)); 253c79d56d2SAnna Schumaker if (unlikely(p == NULL)) 254c79d56d2SAnna Schumaker return htonl(NFS4ERR_BADXDR); 255f2a62561SFred Isaman p = xdr_decode_hyper(p, &args->cbl_fsid.major); 256f2a62561SFred Isaman p = xdr_decode_hyper(p, &args->cbl_fsid.minor); 257c79d56d2SAnna Schumaker } else if (args->cbl_recall_type != RETURN_ALL) 258c79d56d2SAnna Schumaker return htonl(NFS4ERR_BADXDR); 259c79d56d2SAnna Schumaker return 0; 260f2a62561SFred Isaman } 261f2a62561SFred Isaman 2621be5683bSMarc Eshel static 2631be5683bSMarc Eshel __be32 decode_devicenotify_args(struct svc_rqst *rqstp, 2641be5683bSMarc Eshel struct xdr_stream *xdr, 265f4dac4adSChristoph Hellwig void *argp) 2661be5683bSMarc Eshel { 267f4dac4adSChristoph Hellwig struct cb_devicenotifyargs *args = argp; 2681be5683bSMarc Eshel __be32 *p; 2691be5683bSMarc Eshel __be32 status = 0; 2701be5683bSMarc Eshel u32 tmp; 2711be5683bSMarc Eshel int n, i; 2721be5683bSMarc Eshel args->ndevs = 0; 2731be5683bSMarc Eshel 2741be5683bSMarc Eshel /* Num of device notifications */ 2751be5683bSMarc Eshel p = read_buf(xdr, sizeof(uint32_t)); 2761be5683bSMarc Eshel if (unlikely(p == NULL)) { 2771be5683bSMarc Eshel status = htonl(NFS4ERR_BADXDR); 2781be5683bSMarc Eshel goto out; 2791be5683bSMarc Eshel } 2801be5683bSMarc Eshel n = ntohl(*p++); 2811be5683bSMarc Eshel if (n <= 0) 2821be5683bSMarc Eshel goto out; 283363e0df0SDan Carpenter if (n > ULONG_MAX / sizeof(*args->devs)) { 284363e0df0SDan Carpenter status = htonl(NFS4ERR_BADXDR); 285363e0df0SDan Carpenter goto out; 286363e0df0SDan Carpenter } 2871be5683bSMarc Eshel 288a4f743a6STrond Myklebust args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL); 2891be5683bSMarc Eshel if (!args->devs) { 2901be5683bSMarc Eshel status = htonl(NFS4ERR_DELAY); 2911be5683bSMarc Eshel goto out; 2921be5683bSMarc Eshel } 2931be5683bSMarc Eshel 2941be5683bSMarc Eshel /* Decode each dev notification */ 2951be5683bSMarc Eshel for (i = 0; i < n; i++) { 2961be5683bSMarc Eshel struct cb_devicenotifyitem *dev = &args->devs[i]; 2971be5683bSMarc Eshel 2981be5683bSMarc Eshel p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE); 2991be5683bSMarc Eshel if (unlikely(p == NULL)) { 3001be5683bSMarc Eshel status = htonl(NFS4ERR_BADXDR); 3011be5683bSMarc Eshel goto err; 3021be5683bSMarc Eshel } 3031be5683bSMarc Eshel 3041be5683bSMarc Eshel tmp = ntohl(*p++); /* bitmap size */ 3051be5683bSMarc Eshel if (tmp != 1) { 3061be5683bSMarc Eshel status = htonl(NFS4ERR_INVAL); 3071be5683bSMarc Eshel goto err; 3081be5683bSMarc Eshel } 3091be5683bSMarc Eshel dev->cbd_notify_type = ntohl(*p++); 3101be5683bSMarc Eshel if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE && 3111be5683bSMarc Eshel dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) { 3121be5683bSMarc Eshel status = htonl(NFS4ERR_INVAL); 3131be5683bSMarc Eshel goto err; 3141be5683bSMarc Eshel } 3151be5683bSMarc Eshel 3161be5683bSMarc Eshel tmp = ntohl(*p++); /* opaque size */ 3171be5683bSMarc Eshel if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) && 3181be5683bSMarc Eshel (tmp != NFS4_DEVICEID4_SIZE + 8)) || 3191be5683bSMarc Eshel ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) && 3201be5683bSMarc Eshel (tmp != NFS4_DEVICEID4_SIZE + 4))) { 3211be5683bSMarc Eshel status = htonl(NFS4ERR_INVAL); 3221be5683bSMarc Eshel goto err; 3231be5683bSMarc Eshel } 3241be5683bSMarc Eshel dev->cbd_layout_type = ntohl(*p++); 3251be5683bSMarc Eshel memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE); 3261be5683bSMarc Eshel p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE); 3271be5683bSMarc Eshel 3281be5683bSMarc Eshel if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) { 3291be5683bSMarc Eshel p = read_buf(xdr, sizeof(uint32_t)); 3301be5683bSMarc Eshel if (unlikely(p == NULL)) { 3311be5683bSMarc Eshel status = htonl(NFS4ERR_BADXDR); 3321be5683bSMarc Eshel goto err; 3331be5683bSMarc Eshel } 3341be5683bSMarc Eshel dev->cbd_immediate = ntohl(*p++); 3351be5683bSMarc Eshel } else { 3361be5683bSMarc Eshel dev->cbd_immediate = 0; 3371be5683bSMarc Eshel } 3381be5683bSMarc Eshel 3391be5683bSMarc Eshel args->ndevs++; 3401be5683bSMarc Eshel 3411be5683bSMarc Eshel dprintk("%s: type %d layout 0x%x immediate %d\n", 3421be5683bSMarc Eshel __func__, dev->cbd_notify_type, dev->cbd_layout_type, 3431be5683bSMarc Eshel dev->cbd_immediate); 3441be5683bSMarc Eshel } 3451be5683bSMarc Eshel out: 3461be5683bSMarc Eshel dprintk("%s: status %d ndevs %d\n", 3471be5683bSMarc Eshel __func__, ntohl(status), args->ndevs); 3481be5683bSMarc Eshel return status; 3491be5683bSMarc Eshel err: 3501be5683bSMarc Eshel kfree(args->devs); 3511be5683bSMarc Eshel goto out; 3521be5683bSMarc Eshel } 3531be5683bSMarc Eshel 3549733f0d9SAndy Adamson static __be32 decode_sessionid(struct xdr_stream *xdr, 3554aece6a1SBenny Halevy struct nfs4_sessionid *sid) 3564aece6a1SBenny Halevy { 3579733f0d9SAndy Adamson __be32 *p; 3584aece6a1SBenny Halevy 359590184a6SKinglong Mee p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN); 3604aece6a1SBenny Halevy if (unlikely(p == NULL)) 361a419aef8SJoe Perches return htonl(NFS4ERR_RESOURCE); 3624aece6a1SBenny Halevy 363590184a6SKinglong Mee memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN); 3644aece6a1SBenny Halevy return 0; 3654aece6a1SBenny Halevy } 3664aece6a1SBenny Halevy 3679733f0d9SAndy Adamson static __be32 decode_rc_list(struct xdr_stream *xdr, 3684aece6a1SBenny Halevy struct referring_call_list *rc_list) 3694aece6a1SBenny Halevy { 3709733f0d9SAndy Adamson __be32 *p; 3714aece6a1SBenny Halevy int i; 3729733f0d9SAndy Adamson __be32 status; 3734aece6a1SBenny Halevy 3744aece6a1SBenny Halevy status = decode_sessionid(xdr, &rc_list->rcl_sessionid); 3754aece6a1SBenny Halevy if (status) 3764aece6a1SBenny Halevy goto out; 3774aece6a1SBenny Halevy 3784aece6a1SBenny Halevy status = htonl(NFS4ERR_RESOURCE); 3794aece6a1SBenny Halevy p = read_buf(xdr, sizeof(uint32_t)); 3804aece6a1SBenny Halevy if (unlikely(p == NULL)) 3814aece6a1SBenny Halevy goto out; 3824aece6a1SBenny Halevy 3834aece6a1SBenny Halevy rc_list->rcl_nrefcalls = ntohl(*p++); 3844aece6a1SBenny Halevy if (rc_list->rcl_nrefcalls) { 3854aece6a1SBenny Halevy p = read_buf(xdr, 3864aece6a1SBenny Halevy rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t)); 3874aece6a1SBenny Halevy if (unlikely(p == NULL)) 3884aece6a1SBenny Halevy goto out; 389a4f743a6STrond Myklebust rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls, 3904aece6a1SBenny Halevy sizeof(*rc_list->rcl_refcalls), 3914aece6a1SBenny Halevy GFP_KERNEL); 3924aece6a1SBenny Halevy if (unlikely(rc_list->rcl_refcalls == NULL)) 3934aece6a1SBenny Halevy goto out; 3944aece6a1SBenny Halevy for (i = 0; i < rc_list->rcl_nrefcalls; i++) { 3954aece6a1SBenny Halevy rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++); 3964aece6a1SBenny Halevy rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++); 3974aece6a1SBenny Halevy } 3984aece6a1SBenny Halevy } 3994aece6a1SBenny Halevy status = 0; 4004aece6a1SBenny Halevy 4014aece6a1SBenny Halevy out: 4024aece6a1SBenny Halevy return status; 4034aece6a1SBenny Halevy } 4044aece6a1SBenny Halevy 4059733f0d9SAndy Adamson static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp, 4064aece6a1SBenny Halevy struct xdr_stream *xdr, 407f4dac4adSChristoph Hellwig void *argp) 4084aece6a1SBenny Halevy { 409f4dac4adSChristoph Hellwig struct cb_sequenceargs *args = argp; 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, 455f4dac4adSChristoph Hellwig void *argp) 45631f09607SAlexandros Batsakis { 457f4dac4adSChristoph Hellwig struct cb_recallanyargs *args = argp; 458d743c3c9SPeng Tao uint32_t bitmap[2]; 459d743c3c9SPeng Tao __be32 *p, status; 46031f09607SAlexandros Batsakis 46131f09607SAlexandros Batsakis p = read_buf(xdr, 4); 46231f09607SAlexandros Batsakis if (unlikely(p == NULL)) 46331f09607SAlexandros Batsakis return htonl(NFS4ERR_BADXDR); 46431f09607SAlexandros Batsakis args->craa_objs_to_keep = ntohl(*p++); 465d743c3c9SPeng Tao status = decode_bitmap(xdr, bitmap); 466d743c3c9SPeng Tao if (unlikely(status)) 467d743c3c9SPeng Tao return status; 468d743c3c9SPeng Tao args->craa_type_mask = bitmap[0]; 46931f09607SAlexandros Batsakis 47031f09607SAlexandros Batsakis return 0; 47131f09607SAlexandros Batsakis } 47231f09607SAlexandros Batsakis 4739733f0d9SAndy Adamson static __be32 decode_recallslot_args(struct svc_rqst *rqstp, 474b9efa1b2SAndy Adamson struct xdr_stream *xdr, 475f4dac4adSChristoph Hellwig void *argp) 476b9efa1b2SAndy Adamson { 477f4dac4adSChristoph Hellwig struct cb_recallslotargs *args = argp; 478b9efa1b2SAndy Adamson __be32 *p; 479b9efa1b2SAndy Adamson 480b9efa1b2SAndy Adamson p = read_buf(xdr, 4); 481b9efa1b2SAndy Adamson if (unlikely(p == NULL)) 482b9efa1b2SAndy Adamson return htonl(NFS4ERR_BADXDR); 483d5fb4ce3STrond Myklebust args->crsa_target_highest_slotid = ntohl(*p++); 484b9efa1b2SAndy Adamson return 0; 485b9efa1b2SAndy Adamson } 486b9efa1b2SAndy Adamson 487db783688SJeff Layton static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args) 488db783688SJeff Layton { 489db783688SJeff Layton __be32 *p; 490db783688SJeff Layton unsigned int len; 491db783688SJeff Layton 492db783688SJeff Layton p = read_buf(xdr, 12); 493db783688SJeff Layton if (unlikely(p == NULL)) 494db783688SJeff Layton return htonl(NFS4ERR_BADXDR); 495db783688SJeff Layton 496db783688SJeff Layton p = xdr_decode_hyper(p, &args->cbnl_owner.clientid); 497db783688SJeff Layton len = be32_to_cpu(*p); 498db783688SJeff Layton 499db783688SJeff Layton p = read_buf(xdr, len); 500db783688SJeff Layton if (unlikely(p == NULL)) 501db783688SJeff Layton return htonl(NFS4ERR_BADXDR); 502db783688SJeff Layton 503db783688SJeff Layton /* Only try to decode if the length is right */ 504db783688SJeff Layton if (len == 20) { 505db783688SJeff Layton p += 2; /* skip "lock id:" */ 506db783688SJeff Layton args->cbnl_owner.s_dev = be32_to_cpu(*p++); 507db783688SJeff Layton xdr_decode_hyper(p, &args->cbnl_owner.id); 508db783688SJeff Layton args->cbnl_valid = true; 509db783688SJeff Layton } else { 510db783688SJeff Layton args->cbnl_owner.s_dev = 0; 511db783688SJeff Layton args->cbnl_owner.id = 0; 512db783688SJeff Layton args->cbnl_valid = false; 513db783688SJeff Layton } 514db783688SJeff Layton return 0; 515db783688SJeff Layton } 516db783688SJeff Layton 517f4dac4adSChristoph Hellwig static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, 518f4dac4adSChristoph Hellwig struct xdr_stream *xdr, void *argp) 519db783688SJeff Layton { 520f4dac4adSChristoph Hellwig struct cb_notify_lock_args *args = argp; 521db783688SJeff Layton __be32 status; 522db783688SJeff Layton 523db783688SJeff Layton status = decode_fh(xdr, &args->cbnl_fh); 524db783688SJeff Layton if (unlikely(status != 0)) 525db783688SJeff Layton return status; 526535ece2bSAnna Schumaker return decode_lockowner(xdr, args); 527db783688SJeff Layton } 528db783688SJeff Layton 5294aece6a1SBenny Halevy #endif /* CONFIG_NFS_V4_1 */ 5304aece6a1SBenny Halevy 531e6f684f6SAl Viro static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str) 5321da177e4SLinus Torvalds { 533ab6e9aafSTrond Myklebust if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0)) 534ab6e9aafSTrond Myklebust return cpu_to_be32(NFS4ERR_RESOURCE); 5351da177e4SLinus Torvalds return 0; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) 5391da177e4SLinus Torvalds #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) 5405704fdebSAl Viro static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep) 5411da177e4SLinus Torvalds { 5425704fdebSAl Viro __be32 bm[2]; 5435704fdebSAl Viro __be32 *p; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0); 5461da177e4SLinus Torvalds bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1); 5471da177e4SLinus Torvalds if (bm[1] != 0) { 5481da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 16); 5491da177e4SLinus Torvalds if (unlikely(p == NULL)) 5501da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5511da177e4SLinus Torvalds *p++ = htonl(2); 5521da177e4SLinus Torvalds *p++ = bm[0]; 5531da177e4SLinus Torvalds *p++ = bm[1]; 5541da177e4SLinus Torvalds } else if (bm[0] != 0) { 5551da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 12); 5561da177e4SLinus Torvalds if (unlikely(p == NULL)) 5571da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5581da177e4SLinus Torvalds *p++ = htonl(1); 5591da177e4SLinus Torvalds *p++ = bm[0]; 5601da177e4SLinus Torvalds } else { 5611da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 8); 5621da177e4SLinus Torvalds if (unlikely(p == NULL)) 5631da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5641da177e4SLinus Torvalds *p++ = htonl(0); 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds *savep = p; 5671da177e4SLinus Torvalds return 0; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 570e6f684f6SAl Viro static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change) 5711da177e4SLinus Torvalds { 5725704fdebSAl Viro __be32 *p; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds if (!(bitmap[0] & FATTR4_WORD0_CHANGE)) 5751da177e4SLinus Torvalds return 0; 5761da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 8); 57790dc7d27SHarvey Harrison if (unlikely(!p)) 5781da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5791da177e4SLinus Torvalds p = xdr_encode_hyper(p, change); 5801da177e4SLinus Torvalds return 0; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 583e6f684f6SAl Viro static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size) 5841da177e4SLinus Torvalds { 5855704fdebSAl Viro __be32 *p; 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds if (!(bitmap[0] & FATTR4_WORD0_SIZE)) 5881da177e4SLinus Torvalds return 0; 5891da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 8); 59090dc7d27SHarvey Harrison if (unlikely(!p)) 5911da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 5921da177e4SLinus Torvalds p = xdr_encode_hyper(p, size); 5931da177e4SLinus Torvalds return 0; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 596e6f684f6SAl Viro static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time) 5971da177e4SLinus Torvalds { 5985704fdebSAl Viro __be32 *p; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 12); 60190dc7d27SHarvey Harrison if (unlikely(!p)) 6021da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 6031da177e4SLinus Torvalds p = xdr_encode_hyper(p, time->tv_sec); 6041da177e4SLinus Torvalds *p = htonl(time->tv_nsec); 6051da177e4SLinus Torvalds return 0; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 608e6f684f6SAl Viro static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time) 6091da177e4SLinus Torvalds { 6101da177e4SLinus Torvalds if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) 6111da177e4SLinus Torvalds return 0; 6121da177e4SLinus Torvalds return encode_attr_time(xdr,time); 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 615e6f684f6SAl Viro static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time) 6161da177e4SLinus Torvalds { 6171da177e4SLinus Torvalds if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) 6181da177e4SLinus Torvalds return 0; 6191da177e4SLinus Torvalds return encode_attr_time(xdr,time); 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 622e6f684f6SAl Viro static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr) 6231da177e4SLinus Torvalds { 624e6f684f6SAl Viro __be32 status; 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds hdr->status = xdr_reserve_space(xdr, 4); 6271da177e4SLinus Torvalds if (unlikely(hdr->status == NULL)) 6281da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 6291da177e4SLinus Torvalds status = encode_string(xdr, hdr->taglen, hdr->tag); 6301da177e4SLinus Torvalds if (unlikely(status != 0)) 6311da177e4SLinus Torvalds return status; 6321da177e4SLinus Torvalds hdr->nops = xdr_reserve_space(xdr, 4); 6331da177e4SLinus Torvalds if (unlikely(hdr->nops == NULL)) 6341da177e4SLinus Torvalds return htonl(NFS4ERR_RESOURCE); 6351da177e4SLinus Torvalds return 0; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 638e6f684f6SAl Viro static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res) 6391da177e4SLinus Torvalds { 6405704fdebSAl Viro __be32 *p; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds p = xdr_reserve_space(xdr, 8); 6431da177e4SLinus Torvalds if (unlikely(p == NULL)) 64431d2b435SAndy Adamson return htonl(NFS4ERR_RESOURCE_HDR); 6451da177e4SLinus Torvalds *p++ = htonl(op); 6461da177e4SLinus Torvalds *p = res; 6471da177e4SLinus Torvalds return 0; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 650f4dac4adSChristoph Hellwig static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, 651f4dac4adSChristoph Hellwig const void *resp) 6521da177e4SLinus Torvalds { 653f4dac4adSChristoph Hellwig const struct cb_getattrres *res = resp; 6545704fdebSAl Viro __be32 *savep = NULL; 655e6f684f6SAl Viro __be32 status = res->status; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds if (unlikely(status != 0)) 6581da177e4SLinus Torvalds goto out; 6591da177e4SLinus Torvalds status = encode_attr_bitmap(xdr, res->bitmap, &savep); 6601da177e4SLinus Torvalds if (unlikely(status != 0)) 6611da177e4SLinus Torvalds goto out; 6621da177e4SLinus Torvalds status = encode_attr_change(xdr, res->bitmap, res->change_attr); 6631da177e4SLinus Torvalds if (unlikely(status != 0)) 6641da177e4SLinus Torvalds goto out; 6651da177e4SLinus Torvalds status = encode_attr_size(xdr, res->bitmap, res->size); 6661da177e4SLinus Torvalds if (unlikely(status != 0)) 6671da177e4SLinus Torvalds goto out; 6681da177e4SLinus Torvalds status = encode_attr_ctime(xdr, res->bitmap, &res->ctime); 6691da177e4SLinus Torvalds if (unlikely(status != 0)) 6701da177e4SLinus Torvalds goto out; 6711da177e4SLinus Torvalds status = encode_attr_mtime(xdr, res->bitmap, &res->mtime); 6721da177e4SLinus Torvalds *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1))); 6731da177e4SLinus Torvalds out: 6741da177e4SLinus Torvalds return status; 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 67734bc47c9SBenny Halevy #if defined(CONFIG_NFS_V4_1) 67834bc47c9SBenny Halevy 6799733f0d9SAndy Adamson static __be32 encode_sessionid(struct xdr_stream *xdr, 6804aece6a1SBenny Halevy const struct nfs4_sessionid *sid) 6814aece6a1SBenny Halevy { 6829733f0d9SAndy Adamson __be32 *p; 6834aece6a1SBenny Halevy 684590184a6SKinglong Mee p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN); 6854aece6a1SBenny Halevy if (unlikely(p == NULL)) 6864aece6a1SBenny Halevy return htonl(NFS4ERR_RESOURCE); 6874aece6a1SBenny Halevy 688590184a6SKinglong Mee memcpy(p, sid, NFS4_MAX_SESSIONID_LEN); 6894aece6a1SBenny Halevy return 0; 6904aece6a1SBenny Halevy } 6914aece6a1SBenny Halevy 6929733f0d9SAndy Adamson static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp, 6934aece6a1SBenny Halevy struct xdr_stream *xdr, 694f4dac4adSChristoph Hellwig const void *resp) 6954aece6a1SBenny Halevy { 696f4dac4adSChristoph Hellwig const struct cb_sequenceres *res = resp; 6979733f0d9SAndy Adamson __be32 *p; 698e216c8c7SDan Carpenter __be32 status = res->csr_status; 6994aece6a1SBenny Halevy 7004aece6a1SBenny Halevy if (unlikely(status != 0)) 7013d0bfaa6SAnna Schumaker return status; 7024aece6a1SBenny Halevy 703e0a63c0bSKinglong Mee status = encode_sessionid(xdr, &res->csr_sessionid); 704e0a63c0bSKinglong Mee if (status) 7053d0bfaa6SAnna Schumaker return status; 7064aece6a1SBenny Halevy 7074aece6a1SBenny Halevy p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t)); 7084aece6a1SBenny Halevy if (unlikely(p == NULL)) 7094aece6a1SBenny Halevy return htonl(NFS4ERR_RESOURCE); 7104aece6a1SBenny Halevy 7114aece6a1SBenny Halevy *p++ = htonl(res->csr_sequenceid); 7124aece6a1SBenny Halevy *p++ = htonl(res->csr_slotid); 7134aece6a1SBenny Halevy *p++ = htonl(res->csr_highestslotid); 7144aece6a1SBenny Halevy *p++ = htonl(res->csr_target_highestslotid); 7153d0bfaa6SAnna Schumaker return 0; 7164aece6a1SBenny Halevy } 7174aece6a1SBenny Halevy 71834bc47c9SBenny Halevy static __be32 71934bc47c9SBenny Halevy preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) 72034bc47c9SBenny Halevy { 721281fe15dSBenny Halevy if (op_nr == OP_CB_SEQUENCE) { 722281fe15dSBenny Halevy if (nop != 0) 723281fe15dSBenny Halevy return htonl(NFS4ERR_SEQUENCE_POS); 724281fe15dSBenny Halevy } else { 725281fe15dSBenny Halevy if (nop == 0) 726281fe15dSBenny Halevy return htonl(NFS4ERR_OP_NOT_IN_SESSION); 727281fe15dSBenny Halevy } 728281fe15dSBenny Halevy 72934bc47c9SBenny Halevy switch (op_nr) { 73034bc47c9SBenny Halevy case OP_CB_GETATTR: 73134bc47c9SBenny Halevy case OP_CB_RECALL: 7324aece6a1SBenny Halevy case OP_CB_SEQUENCE: 73331f09607SAlexandros Batsakis case OP_CB_RECALL_ANY: 734b9efa1b2SAndy Adamson case OP_CB_RECALL_SLOT: 735f2a62561SFred Isaman case OP_CB_LAYOUTRECALL: 7361be5683bSMarc Eshel case OP_CB_NOTIFY_DEVICEID: 737db783688SJeff Layton case OP_CB_NOTIFY_LOCK: 73834bc47c9SBenny Halevy *op = &callback_ops[op_nr]; 73934bc47c9SBenny Halevy break; 74034bc47c9SBenny Halevy 74134bc47c9SBenny Halevy case OP_CB_NOTIFY: 74234bc47c9SBenny Halevy case OP_CB_PUSH_DELEG: 74334bc47c9SBenny Halevy case OP_CB_RECALLABLE_OBJ_AVAIL: 74434bc47c9SBenny Halevy case OP_CB_WANTS_CANCELLED: 74534bc47c9SBenny Halevy return htonl(NFS4ERR_NOTSUPP); 74634bc47c9SBenny Halevy 74734bc47c9SBenny Halevy default: 74834bc47c9SBenny Halevy return htonl(NFS4ERR_OP_ILLEGAL); 74934bc47c9SBenny Halevy } 75034bc47c9SBenny Halevy 75134bc47c9SBenny Halevy return htonl(NFS_OK); 75234bc47c9SBenny Halevy } 75334bc47c9SBenny Halevy 754810d82e6STrond Myklebust static void nfs4_callback_free_slot(struct nfs4_session *session, 755810d82e6STrond Myklebust struct nfs4_slot *slot) 75642acd021SAndy Adamson { 75742acd021SAndy Adamson struct nfs4_slot_table *tbl = &session->bc_slot_table; 75842acd021SAndy Adamson 75942acd021SAndy Adamson spin_lock(&tbl->slot_tbl_lock); 76042acd021SAndy Adamson /* 76142acd021SAndy Adamson * Let the state manager know callback processing done. 76242acd021SAndy Adamson * A single slot, so highest used slotid is either 0 or -1 76342acd021SAndy Adamson */ 764810d82e6STrond Myklebust nfs4_free_slot(tbl, slot); 76542acd021SAndy Adamson spin_unlock(&tbl->slot_tbl_lock); 76642acd021SAndy Adamson } 76742acd021SAndy Adamson 76855a67399STrond Myklebust static void nfs4_cb_free_slot(struct cb_process_state *cps) 76942acd021SAndy Adamson { 770810d82e6STrond Myklebust if (cps->slot) { 771810d82e6STrond Myklebust nfs4_callback_free_slot(cps->clp->cl_session, cps->slot); 772810d82e6STrond Myklebust cps->slot = NULL; 773810d82e6STrond Myklebust } 77442acd021SAndy Adamson } 77542acd021SAndy Adamson 77634bc47c9SBenny Halevy #else /* CONFIG_NFS_V4_1 */ 77734bc47c9SBenny Halevy 77834bc47c9SBenny Halevy static __be32 77934bc47c9SBenny Halevy preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) 78034bc47c9SBenny Halevy { 78134bc47c9SBenny Halevy return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 78234bc47c9SBenny Halevy } 78334bc47c9SBenny Halevy 78455a67399STrond Myklebust static void nfs4_cb_free_slot(struct cb_process_state *cps) 78542acd021SAndy Adamson { 78642acd021SAndy Adamson } 78734bc47c9SBenny Halevy #endif /* CONFIG_NFS_V4_1 */ 78834bc47c9SBenny Halevy 7896b140b85SBryan Schumaker #ifdef CONFIG_NFS_V4_2 7906b140b85SBryan Schumaker static __be32 7916b140b85SBryan Schumaker preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op) 7926b140b85SBryan Schumaker { 7936b140b85SBryan Schumaker __be32 status = preprocess_nfs41_op(nop, op_nr, op); 7946b140b85SBryan Schumaker if (status != htonl(NFS4ERR_OP_ILLEGAL)) 7956b140b85SBryan Schumaker return status; 7966b140b85SBryan Schumaker 7976b140b85SBryan Schumaker if (op_nr == OP_CB_OFFLOAD) 7986b140b85SBryan Schumaker return htonl(NFS4ERR_NOTSUPP); 7996b140b85SBryan Schumaker return htonl(NFS4ERR_OP_ILLEGAL); 8006b140b85SBryan Schumaker } 8016b140b85SBryan Schumaker #else /* CONFIG_NFS_V4_2 */ 8026b140b85SBryan Schumaker static __be32 8036b140b85SBryan Schumaker preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op) 8046b140b85SBryan Schumaker { 8056b140b85SBryan Schumaker return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 8066b140b85SBryan Schumaker } 8076b140b85SBryan Schumaker #endif /* CONFIG_NFS_V4_2 */ 8086b140b85SBryan Schumaker 80934bc47c9SBenny Halevy static __be32 81034bc47c9SBenny Halevy preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op) 81134bc47c9SBenny Halevy { 81234bc47c9SBenny Halevy switch (op_nr) { 81334bc47c9SBenny Halevy case OP_CB_GETATTR: 81434bc47c9SBenny Halevy case OP_CB_RECALL: 81534bc47c9SBenny Halevy *op = &callback_ops[op_nr]; 81634bc47c9SBenny Halevy break; 81734bc47c9SBenny Halevy default: 81834bc47c9SBenny Halevy return htonl(NFS4ERR_OP_ILLEGAL); 81934bc47c9SBenny Halevy } 82034bc47c9SBenny Halevy 82134bc47c9SBenny Halevy return htonl(NFS_OK); 82234bc47c9SBenny Halevy } 82334bc47c9SBenny Halevy 824459de2edSBryan Schumaker static __be32 process_op(int nop, struct svc_rqst *rqstp, 8251da177e4SLinus Torvalds struct xdr_stream *xdr_in, void *argp, 826c36fca52SAndy Adamson struct xdr_stream *xdr_out, void *resp, 827c36fca52SAndy Adamson struct cb_process_state *cps) 8281da177e4SLinus Torvalds { 829a162a6b8STrond Myklebust struct callback_op *op = &callback_ops[0]; 83031d2b435SAndy Adamson unsigned int op_nr; 83134bc47c9SBenny Halevy __be32 status; 8321da177e4SLinus Torvalds long maxlen; 833e6f684f6SAl Viro __be32 res; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds status = decode_op_hdr(xdr_in, &op_nr); 83631d2b435SAndy Adamson if (unlikely(status)) 83731d2b435SAndy Adamson return status; 8381da177e4SLinus Torvalds 8396b140b85SBryan Schumaker switch (cps->minorversion) { 8406b140b85SBryan Schumaker case 0: 8416b140b85SBryan Schumaker status = preprocess_nfs4_op(op_nr, &op); 8426b140b85SBryan Schumaker break; 8436b140b85SBryan Schumaker case 1: 8446b140b85SBryan Schumaker status = preprocess_nfs41_op(nop, op_nr, &op); 8456b140b85SBryan Schumaker break; 8466b140b85SBryan Schumaker case 2: 8476b140b85SBryan Schumaker status = preprocess_nfs42_op(nop, op_nr, &op); 8486b140b85SBryan Schumaker break; 8496b140b85SBryan Schumaker default: 8506b140b85SBryan Schumaker status = htonl(NFS4ERR_MINOR_VERS_MISMATCH); 8516b140b85SBryan Schumaker } 8526b140b85SBryan Schumaker 85334bc47c9SBenny Halevy if (status == htonl(NFS4ERR_OP_ILLEGAL)) 85434bc47c9SBenny Halevy op_nr = OP_CB_ILLEGAL; 855b92b3019SAndy Adamson if (status) 856b92b3019SAndy Adamson goto encode_hdr; 85731d2b435SAndy Adamson 858c36fca52SAndy Adamson if (cps->drc_status) { 859c36fca52SAndy Adamson status = cps->drc_status; 8604911096fSAndy Adamson goto encode_hdr; 8614911096fSAndy Adamson } 8624911096fSAndy Adamson 8631da177e4SLinus Torvalds maxlen = xdr_out->end - xdr_out->p; 8641da177e4SLinus Torvalds if (maxlen > 0 && maxlen < PAGE_SIZE) { 8651da177e4SLinus Torvalds status = op->decode_args(rqstp, xdr_in, argp); 866e95e60daSAndy Adamson if (likely(status == 0)) 867c36fca52SAndy Adamson status = op->process_op(argp, resp, cps); 8681da177e4SLinus Torvalds } else 8691da177e4SLinus Torvalds status = htonl(NFS4ERR_RESOURCE); 8701da177e4SLinus Torvalds 871b92b3019SAndy Adamson encode_hdr: 8721da177e4SLinus Torvalds res = encode_op_hdr(xdr_out, op_nr, status); 87331d2b435SAndy Adamson if (unlikely(res)) 87431d2b435SAndy Adamson return res; 8751da177e4SLinus Torvalds if (op->encode_res != NULL && status == 0) 8761da177e4SLinus Torvalds status = op->encode_res(rqstp, xdr_out, resp); 8771da177e4SLinus Torvalds return status; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds /* 8811da177e4SLinus Torvalds * Decode, process and encode a COMPOUND 8821da177e4SLinus Torvalds */ 883a6beb732SChristoph Hellwig static __be32 nfs4_callback_compound(struct svc_rqst *rqstp) 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 896756b9b37STrond Myklebust xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base); 8971da177e4SLinus Torvalds 8985704fdebSAl Viro p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len); 8991da177e4SLinus Torvalds xdr_init_encode(&xdr_out, &rqstp->rq_res, p); 9001da177e4SLinus Torvalds 9013a6258e1STrond Myklebust status = decode_compound_hdr_arg(&xdr_in, &hdr_arg); 9024ed0d83dSVaishali Thakkar if (status == htonl(NFS4ERR_RESOURCE)) 9033a6258e1STrond Myklebust return rpc_garbage_args; 9043a6258e1STrond Myklebust 905c36fca52SAndy Adamson if (hdr_arg.minorversion == 0) { 9069695c705SStanislav Kinsbursky cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident); 907778be232SAndy Adamson if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp)) 908a4e187d8SChuck Lever goto out_invalidcred; 909778be232SAndy Adamson } 910c36fca52SAndy Adamson 911459de2edSBryan Schumaker cps.minorversion = hdr_arg.minorversion; 9121da177e4SLinus Torvalds hdr_res.taglen = hdr_arg.taglen; 9131da177e4SLinus Torvalds hdr_res.tag = hdr_arg.tag; 9143a6258e1STrond Myklebust if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) 9153a6258e1STrond Myklebust return rpc_system_err; 9161da177e4SLinus Torvalds 9173a6258e1STrond Myklebust while (status == 0 && nops != hdr_arg.nops) { 918459de2edSBryan Schumaker status = process_op(nops, rqstp, &xdr_in, 919a6beb732SChristoph Hellwig rqstp->rq_argp, &xdr_out, rqstp->rq_resp, 920a6beb732SChristoph Hellwig &cps); 9211da177e4SLinus Torvalds nops++; 9221da177e4SLinus Torvalds } 9233a6258e1STrond Myklebust 92431d2b435SAndy Adamson /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return 92531d2b435SAndy Adamson * resource error in cb_compound status without returning op */ 92631d2b435SAndy Adamson if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) { 92731d2b435SAndy Adamson status = htonl(NFS4ERR_RESOURCE); 92831d2b435SAndy Adamson nops--; 92931d2b435SAndy Adamson } 93031d2b435SAndy Adamson 9311da177e4SLinus Torvalds *hdr_res.status = status; 9321da177e4SLinus Torvalds *hdr_res.nops = htonl(nops); 93355a67399STrond Myklebust nfs4_cb_free_slot(&cps); 934c36fca52SAndy Adamson nfs_put_client(cps.clp); 9351da177e4SLinus Torvalds return rpc_success; 936a4e187d8SChuck Lever 937a4e187d8SChuck Lever out_invalidcred: 938a4e187d8SChuck Lever pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n"); 939a4e187d8SChuck Lever return rpc_autherr_badcred; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* 9431da177e4SLinus Torvalds * Define NFS4 callback COMPOUND ops. 9441da177e4SLinus Torvalds */ 9451da177e4SLinus Torvalds static struct callback_op callback_ops[] = { 9461da177e4SLinus Torvalds [0] = { 9471da177e4SLinus Torvalds .res_maxsize = CB_OP_HDR_RES_MAXSZ, 9481da177e4SLinus Torvalds }, 9491da177e4SLinus Torvalds [OP_CB_GETATTR] = { 950f4dac4adSChristoph Hellwig .process_op = nfs4_callback_getattr, 951f4dac4adSChristoph Hellwig .decode_args = decode_getattr_args, 952f4dac4adSChristoph Hellwig .encode_res = encode_getattr_res, 9531da177e4SLinus Torvalds .res_maxsize = CB_OP_GETATTR_RES_MAXSZ, 9541da177e4SLinus Torvalds }, 9551da177e4SLinus Torvalds [OP_CB_RECALL] = { 956f4dac4adSChristoph Hellwig .process_op = nfs4_callback_recall, 957f4dac4adSChristoph Hellwig .decode_args = decode_recall_args, 9581da177e4SLinus Torvalds .res_maxsize = CB_OP_RECALL_RES_MAXSZ, 9594aece6a1SBenny Halevy }, 9604aece6a1SBenny Halevy #if defined(CONFIG_NFS_V4_1) 961f2a62561SFred Isaman [OP_CB_LAYOUTRECALL] = { 962f4dac4adSChristoph Hellwig .process_op = nfs4_callback_layoutrecall, 963f4dac4adSChristoph Hellwig .decode_args = decode_layoutrecall_args, 964f2a62561SFred Isaman .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ, 965f2a62561SFred Isaman }, 9661be5683bSMarc Eshel [OP_CB_NOTIFY_DEVICEID] = { 967f4dac4adSChristoph Hellwig .process_op = nfs4_callback_devicenotify, 968f4dac4adSChristoph Hellwig .decode_args = decode_devicenotify_args, 9691be5683bSMarc Eshel .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ, 9701be5683bSMarc Eshel }, 9714aece6a1SBenny Halevy [OP_CB_SEQUENCE] = { 972f4dac4adSChristoph Hellwig .process_op = nfs4_callback_sequence, 973f4dac4adSChristoph Hellwig .decode_args = decode_cb_sequence_args, 974f4dac4adSChristoph Hellwig .encode_res = encode_cb_sequence_res, 9754aece6a1SBenny Halevy .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ, 9764aece6a1SBenny Halevy }, 97731f09607SAlexandros Batsakis [OP_CB_RECALL_ANY] = { 978f4dac4adSChristoph Hellwig .process_op = nfs4_callback_recallany, 979f4dac4adSChristoph Hellwig .decode_args = decode_recallany_args, 98031f09607SAlexandros Batsakis .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ, 98131f09607SAlexandros Batsakis }, 982b9efa1b2SAndy Adamson [OP_CB_RECALL_SLOT] = { 983f4dac4adSChristoph Hellwig .process_op = nfs4_callback_recallslot, 984f4dac4adSChristoph Hellwig .decode_args = decode_recallslot_args, 985b9efa1b2SAndy Adamson .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ, 986b9efa1b2SAndy Adamson }, 987db783688SJeff Layton [OP_CB_NOTIFY_LOCK] = { 988f4dac4adSChristoph Hellwig .process_op = nfs4_callback_notify_lock, 989f4dac4adSChristoph Hellwig .decode_args = decode_notify_lock_args, 990db783688SJeff Layton .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ, 991db783688SJeff Layton }, 9924aece6a1SBenny Halevy #endif /* CONFIG_NFS_V4_1 */ 9931da177e4SLinus Torvalds }; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds /* 9961da177e4SLinus Torvalds * Define NFS4 callback procedures 9971da177e4SLinus Torvalds */ 998860bda29SChristoph Hellwig static const struct svc_procedure nfs4_callback_procedures1[] = { 9991da177e4SLinus Torvalds [CB_NULL] = { 10001da177e4SLinus Torvalds .pc_func = nfs4_callback_null, 1001026fec7eSChristoph Hellwig .pc_decode = nfs4_decode_void, 100263f8de37SChristoph Hellwig .pc_encode = nfs4_encode_void, 10031da177e4SLinus Torvalds .pc_xdrressize = 1, 10041da177e4SLinus Torvalds }, 10051da177e4SLinus Torvalds [CB_COMPOUND] = { 10061da177e4SLinus Torvalds .pc_func = nfs4_callback_compound, 100763f8de37SChristoph Hellwig .pc_encode = nfs4_encode_void, 10081da177e4SLinus Torvalds .pc_argsize = 256, 10091da177e4SLinus Torvalds .pc_ressize = 256, 10101da177e4SLinus Torvalds .pc_xdrressize = NFS4_CALLBACK_BUFSIZE, 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds }; 10131da177e4SLinus Torvalds 10147fd38af9SChristoph Hellwig static unsigned int nfs4_callback_count1[ARRAY_SIZE(nfs4_callback_procedures1)]; 1015e9679189SChristoph Hellwig const struct svc_version nfs4_callback_version1 = { 10161da177e4SLinus Torvalds .vs_vers = 1, 10171da177e4SLinus Torvalds .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), 10181da177e4SLinus Torvalds .vs_proc = nfs4_callback_procedures1, 10197fd38af9SChristoph Hellwig .vs_count = nfs4_callback_count1, 10201da177e4SLinus Torvalds .vs_xdrsize = NFS4_CALLBACK_XDRSIZE, 10211da177e4SLinus Torvalds .vs_dispatch = NULL, 102205a45a2dSJeff Layton .vs_hidden = true, 10235283b03eSJeff Layton .vs_need_cong_ctrl = true, 10241da177e4SLinus Torvalds }; 10251da177e4SLinus Torvalds 10267fd38af9SChristoph Hellwig static unsigned int nfs4_callback_count4[ARRAY_SIZE(nfs4_callback_procedures1)]; 1027e9679189SChristoph Hellwig const struct svc_version nfs4_callback_version4 = { 102807bccc2dSAlexandros Batsakis .vs_vers = 4, 102907bccc2dSAlexandros Batsakis .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), 103007bccc2dSAlexandros Batsakis .vs_proc = nfs4_callback_procedures1, 10317fd38af9SChristoph Hellwig .vs_count = nfs4_callback_count4, 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