blob: 73a5a5ea2976680559bc59293ccb554598557a80 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/nfs/callback_xdr.c
4 *
5 * Copyright (C) 2004 Trond Myklebust
6 *
7 * NFSv4 callback encode/decode procedures
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/sunrpc/svc.h>
11#include <linux/nfs4.h>
12#include <linux/nfs_fs.h>
Trond Myklebust9a3ba432012-03-12 18:01:48 -040013#include <linux/ratelimit.h>
14#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Andy Adamsonc36fca52011-01-06 02:04:32 +000016#include <linux/sunrpc/bc_xprt.h>
Trond Myklebust4ce79712005-06-22 17:16:21 +000017#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "callback.h"
Andy Adamsonc36fca52011-01-06 02:04:32 +000019#include "internal.h"
Trond Myklebust76e697b2012-11-26 14:20:49 -050020#include "nfs4session.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Kinglong Mee45724e82015-09-24 20:57:58 +080022#define CB_OP_TAGLEN_MAXSZ (512)
23#define CB_OP_HDR_RES_MAXSZ (2 * 4) // opcode, status
24#define CB_OP_GETATTR_BITMAP_MAXSZ (4 * 4) // bitmap length, 3 bitmaps
25#define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
26 CB_OP_GETATTR_BITMAP_MAXSZ + \
27 /* change, size, ctime, mtime */\
28 (2 + 2 + 3 + 3) * 4)
29#define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Benny Halevy4aece6a2009-04-01 09:23:26 -040031#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +000032#define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Marc Eshel1be56832011-05-22 19:47:09 +030033#define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Benny Halevy4aece6a2009-04-01 09:23:26 -040034#define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
Kinglong Mee45724e82015-09-24 20:57:58 +080035 NFS4_MAX_SESSIONID_LEN + \
36 (1 + 3) * 4) // seqid, 3 slotids
Alexandros Batsakis31f09602009-12-05 13:27:02 -050037#define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -050038#define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Jeff Laytondb783682016-09-17 18:17:36 -040039#define CB_OP_NOTIFY_LOCK_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Benny Halevy4aece6a2009-04-01 09:23:26 -040040#endif /* CONFIG_NFS_V4_1 */
Olga Kornievskaia5178a122018-07-09 15:13:28 -040041#ifdef CONFIG_NFS_V4_2
42#define CB_OP_OFFLOAD_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
43#endif /* CONFIG_NFS_V4_2 */
Benny Halevy4aece6a2009-04-01 09:23:26 -040044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define NFSDBG_FACILITY NFSDBG_CALLBACK
46
Andy Adamson31d2b432010-01-14 17:45:04 -050047/* Internal error code */
48#define NFS4ERR_RESOURCE_HDR 11050
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct callback_op {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +020051 __be32 (*process_op)(void *, void *, struct cb_process_state *);
52 __be32 (*decode_args)(struct svc_rqst *, struct xdr_stream *, void *);
53 __be32 (*encode_res)(struct svc_rqst *, struct xdr_stream *,
54 const void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 long res_maxsize;
56};
57
58static struct callback_op callback_ops[];
59
Christoph Hellwiga6beb732017-05-08 17:35:49 +020060static __be32 nfs4_callback_null(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 return htonl(NFS4_OK);
63}
64
Christoph Hellwig026fec72017-05-08 19:01:48 +020065static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 return xdr_argsize_check(rqstp, p);
68}
69
Christoph Hellwig63f8de32017-05-08 19:42:02 +020070static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 return xdr_ressize_check(rqstp, p);
73}
74
Trond Myklebustc065eee2017-02-19 16:08:28 -050075static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len,
76 const char **str, size_t maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Trond Myklebustc065eee2017-02-19 16:08:28 -050078 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Trond Myklebustc065eee2017-02-19 16:08:28 -050080 err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen);
81 if (err < 0)
82 return cpu_to_be32(NFS4ERR_RESOURCE);
83 *len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 0;
85}
86
Al Viroe6f684f2006-10-19 23:28:50 -070087static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Al Viro5704fde2006-10-19 23:28:51 -070089 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Chuck Levereb72f482019-02-11 11:24:21 -050091 p = xdr_inline_decode(xdr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (unlikely(p == NULL))
93 return htonl(NFS4ERR_RESOURCE);
94 fh->size = ntohl(*p);
95 if (fh->size > NFS4_FHSIZE)
96 return htonl(NFS4ERR_BADHANDLE);
Chuck Levereb72f482019-02-11 11:24:21 -050097 p = xdr_inline_decode(xdr, fh->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (unlikely(p == NULL))
99 return htonl(NFS4ERR_RESOURCE);
100 memcpy(&fh->data[0], p, fh->size);
101 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
102 return 0;
103}
104
Al Viroe6f684f2006-10-19 23:28:50 -0700105static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Al Viro5704fde2006-10-19 23:28:51 -0700107 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned int attrlen;
109
Chuck Levereb72f482019-02-11 11:24:21 -0500110 p = xdr_inline_decode(xdr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (unlikely(p == NULL))
112 return htonl(NFS4ERR_RESOURCE);
113 attrlen = ntohl(*p);
Chuck Levereb72f482019-02-11 11:24:21 -0500114 p = xdr_inline_decode(xdr, attrlen << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (unlikely(p == NULL))
116 return htonl(NFS4ERR_RESOURCE);
117 if (likely(attrlen > 0))
118 bitmap[0] = ntohl(*p++);
119 if (attrlen > 1)
120 bitmap[1] = ntohl(*p);
121 return 0;
122}
123
Al Viroe6f684f2006-10-19 23:28:50 -0700124static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Al Viro5704fde2006-10-19 23:28:51 -0700126 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Chuck Levereb72f482019-02-11 11:24:21 -0500128 p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (unlikely(p == NULL))
130 return htonl(NFS4ERR_RESOURCE);
Trond Myklebust93b717f2016-05-16 17:42:43 -0400131 memcpy(stateid->data, p, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 0;
133}
134
Trond Myklebust93b717f2016-05-16 17:42:43 -0400135static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
136{
137 stateid->type = NFS4_DELEGATION_STATEID_TYPE;
138 return decode_stateid(xdr, stateid);
139}
140
Al Viroe6f684f2006-10-19 23:28:50 -0700141static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Al Viro5704fde2006-10-19 23:28:51 -0700143 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700144 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Trond Myklebustc065eee2017-02-19 16:08:28 -0500146 status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (unlikely(status != 0))
148 return status;
Chuck Levereb72f482019-02-11 11:24:21 -0500149 p = xdr_inline_decode(xdr, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (unlikely(p == NULL))
151 return htonl(NFS4ERR_RESOURCE);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400152 hdr->minorversion = ntohl(*p++);
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400153 /* Check for minor version support */
154 if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
Steve Dickson42c2c422013-05-22 12:50:38 -0400155 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
Benny Halevy48a9e2d2009-04-01 09:23:20 -0400156 } else {
Trond Myklebust9a3ba432012-03-12 18:01:48 -0400157 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400158 "illegal minor version %u!\n",
159 __func__, hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 hdr->nops = ntohl(*p);
163 return 0;
164}
165
Al Viroe6f684f2006-10-19 23:28:50 -0700166static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Al Viro5704fde2006-10-19 23:28:51 -0700168 __be32 *p;
Chuck Levereb72f482019-02-11 11:24:21 -0500169 p = xdr_inline_decode(xdr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500171 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *op = ntohl(*p);
173 return 0;
174}
175
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200176static __be32 decode_getattr_args(struct svc_rqst *rqstp,
177 struct xdr_stream *xdr, void *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200179 struct cb_getattrargs *args = argp;
Al Viroe6f684f2006-10-19 23:28:50 -0700180 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 status = decode_fh(xdr, &args->fh);
183 if (unlikely(status != 0))
Anna Schumaker56938bb2017-04-07 14:14:58 -0400184 return status;
185 return decode_bitmap(xdr, args->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200188static __be32 decode_recall_args(struct svc_rqst *rqstp,
189 struct xdr_stream *xdr, void *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200191 struct cb_recallargs *args = argp;
Al Viro5704fde2006-10-19 23:28:51 -0700192 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700193 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Trond Myklebust93b717f2016-05-16 17:42:43 -0400195 status = decode_delegation_stateid(xdr, &args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (unlikely(status != 0))
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400197 return status;
Chuck Levereb72f482019-02-11 11:24:21 -0500198 p = xdr_inline_decode(xdr, 4);
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400199 if (unlikely(p == NULL))
200 return htonl(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 args->truncate = ntohl(*p);
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400202 return decode_fh(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Benny Halevy4aece6a2009-04-01 09:23:26 -0400205#if defined(CONFIG_NFS_V4_1)
Trond Myklebust93b717f2016-05-16 17:42:43 -0400206static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
207{
208 stateid->type = NFS4_LAYOUT_STATEID_TYPE;
209 return decode_stateid(xdr, stateid);
210}
Benny Halevy4aece6a2009-04-01 09:23:26 -0400211
Fred Isamanf2a62562011-01-06 11:36:29 +0000212static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200213 struct xdr_stream *xdr, void *argp)
Fred Isamanf2a62562011-01-06 11:36:29 +0000214{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200215 struct cb_layoutrecallargs *args = argp;
Fred Isamanf2a62562011-01-06 11:36:29 +0000216 __be32 *p;
217 __be32 status = 0;
218 uint32_t iomode;
219
Chuck Levereb72f482019-02-11 11:24:21 -0500220 p = xdr_inline_decode(xdr, 4 * sizeof(uint32_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400221 if (unlikely(p == NULL))
222 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000223
224 args->cbl_layout_type = ntohl(*p++);
225 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300226 * as it is unusable and ignored with the other types.
Fred Isamanf2a62562011-01-06 11:36:29 +0000227 */
228 iomode = ntohl(*p++);
229 args->cbl_layoutchanged = ntohl(*p++);
230 args->cbl_recall_type = ntohl(*p++);
231
232 if (args->cbl_recall_type == RETURN_FILE) {
233 args->cbl_range.iomode = iomode;
234 status = decode_fh(xdr, &args->cbl_fh);
235 if (unlikely(status != 0))
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400236 return status;
Fred Isamanf2a62562011-01-06 11:36:29 +0000237
Chuck Levereb72f482019-02-11 11:24:21 -0500238 p = xdr_inline_decode(xdr, 2 * sizeof(uint64_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400239 if (unlikely(p == NULL))
240 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000241 p = xdr_decode_hyper(p, &args->cbl_range.offset);
242 p = xdr_decode_hyper(p, &args->cbl_range.length);
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400243 return decode_layout_stateid(xdr, &args->cbl_stateid);
Fred Isamanf2a62562011-01-06 11:36:29 +0000244 } else if (args->cbl_recall_type == RETURN_FSID) {
Chuck Levereb72f482019-02-11 11:24:21 -0500245 p = xdr_inline_decode(xdr, 2 * sizeof(uint64_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400246 if (unlikely(p == NULL))
247 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000248 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
249 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400250 } else if (args->cbl_recall_type != RETURN_ALL)
251 return htonl(NFS4ERR_BADXDR);
252 return 0;
Fred Isamanf2a62562011-01-06 11:36:29 +0000253}
254
Marc Eshel1be56832011-05-22 19:47:09 +0300255static
256__be32 decode_devicenotify_args(struct svc_rqst *rqstp,
257 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200258 void *argp)
Marc Eshel1be56832011-05-22 19:47:09 +0300259{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200260 struct cb_devicenotifyargs *args = argp;
Marc Eshel1be56832011-05-22 19:47:09 +0300261 __be32 *p;
262 __be32 status = 0;
263 u32 tmp;
264 int n, i;
265 args->ndevs = 0;
266
267 /* Num of device notifications */
Chuck Levereb72f482019-02-11 11:24:21 -0500268 p = xdr_inline_decode(xdr, sizeof(uint32_t));
Marc Eshel1be56832011-05-22 19:47:09 +0300269 if (unlikely(p == NULL)) {
270 status = htonl(NFS4ERR_BADXDR);
271 goto out;
272 }
273 n = ntohl(*p++);
274 if (n <= 0)
275 goto out;
Dan Carpenter363e0df02012-01-12 10:16:14 +0300276 if (n > ULONG_MAX / sizeof(*args->devs)) {
277 status = htonl(NFS4ERR_BADXDR);
278 goto out;
279 }
Marc Eshel1be56832011-05-22 19:47:09 +0300280
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500281 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
Marc Eshel1be56832011-05-22 19:47:09 +0300282 if (!args->devs) {
283 status = htonl(NFS4ERR_DELAY);
284 goto out;
285 }
286
287 /* Decode each dev notification */
288 for (i = 0; i < n; i++) {
289 struct cb_devicenotifyitem *dev = &args->devs[i];
290
Chuck Levereb72f482019-02-11 11:24:21 -0500291 p = xdr_inline_decode(xdr, (4 * sizeof(uint32_t)) +
292 NFS4_DEVICEID4_SIZE);
Marc Eshel1be56832011-05-22 19:47:09 +0300293 if (unlikely(p == NULL)) {
294 status = htonl(NFS4ERR_BADXDR);
295 goto err;
296 }
297
298 tmp = ntohl(*p++); /* bitmap size */
299 if (tmp != 1) {
300 status = htonl(NFS4ERR_INVAL);
301 goto err;
302 }
303 dev->cbd_notify_type = ntohl(*p++);
304 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
305 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
306 status = htonl(NFS4ERR_INVAL);
307 goto err;
308 }
309
310 tmp = ntohl(*p++); /* opaque size */
311 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
312 (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
313 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
314 (tmp != NFS4_DEVICEID4_SIZE + 4))) {
315 status = htonl(NFS4ERR_INVAL);
316 goto err;
317 }
318 dev->cbd_layout_type = ntohl(*p++);
319 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
320 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
321
322 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
Chuck Levereb72f482019-02-11 11:24:21 -0500323 p = xdr_inline_decode(xdr, sizeof(uint32_t));
Marc Eshel1be56832011-05-22 19:47:09 +0300324 if (unlikely(p == NULL)) {
325 status = htonl(NFS4ERR_BADXDR);
326 goto err;
327 }
328 dev->cbd_immediate = ntohl(*p++);
329 } else {
330 dev->cbd_immediate = 0;
331 }
332
333 args->ndevs++;
334
335 dprintk("%s: type %d layout 0x%x immediate %d\n",
336 __func__, dev->cbd_notify_type, dev->cbd_layout_type,
337 dev->cbd_immediate);
338 }
339out:
340 dprintk("%s: status %d ndevs %d\n",
341 __func__, ntohl(status), args->ndevs);
342 return status;
343err:
344 kfree(args->devs);
345 goto out;
346}
347
Andy Adamson9733f0d2010-01-22 12:03:08 -0500348static __be32 decode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400349 struct nfs4_sessionid *sid)
350{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500351 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400352
Chuck Levereb72f482019-02-11 11:24:21 -0500353 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400354 if (unlikely(p == NULL))
Joe Perchesa419aef2009-08-18 11:18:35 -0700355 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400356
Kinglong Mee590184a2015-09-24 20:57:37 +0800357 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400358 return 0;
359}
360
Andy Adamson9733f0d2010-01-22 12:03:08 -0500361static __be32 decode_rc_list(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400362 struct referring_call_list *rc_list)
363{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500364 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400365 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500366 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400367
368 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
369 if (status)
370 goto out;
371
372 status = htonl(NFS4ERR_RESOURCE);
Chuck Levereb72f482019-02-11 11:24:21 -0500373 p = xdr_inline_decode(xdr, sizeof(uint32_t));
Benny Halevy4aece6a2009-04-01 09:23:26 -0400374 if (unlikely(p == NULL))
375 goto out;
376
377 rc_list->rcl_nrefcalls = ntohl(*p++);
378 if (rc_list->rcl_nrefcalls) {
Chuck Levereb72f482019-02-11 11:24:21 -0500379 p = xdr_inline_decode(xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400380 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
381 if (unlikely(p == NULL))
382 goto out;
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500383 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400384 sizeof(*rc_list->rcl_refcalls),
385 GFP_KERNEL);
386 if (unlikely(rc_list->rcl_refcalls == NULL))
387 goto out;
388 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
389 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
390 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
391 }
392 }
393 status = 0;
394
395out:
396 return status;
397}
398
Andy Adamson9733f0d2010-01-22 12:03:08 -0500399static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400400 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200401 void *argp)
Benny Halevy4aece6a2009-04-01 09:23:26 -0400402{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200403 struct cb_sequenceargs *args = argp;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500404 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400405 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500406 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400407
408 status = decode_sessionid(xdr, &args->csa_sessionid);
409 if (status)
Anna Schumaker17965492017-04-07 14:15:01 -0400410 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400411
Chuck Levereb72f482019-02-11 11:24:21 -0500412 p = xdr_inline_decode(xdr, 5 * sizeof(uint32_t));
Benny Halevy4aece6a2009-04-01 09:23:26 -0400413 if (unlikely(p == NULL))
Anna Schumaker17965492017-04-07 14:15:01 -0400414 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400415
Ricardo Labiaga65fc64e2009-04-01 09:23:30 -0400416 args->csa_addr = svc_addr(rqstp);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400417 args->csa_sequenceid = ntohl(*p++);
418 args->csa_slotid = ntohl(*p++);
419 args->csa_highestslotid = ntohl(*p++);
420 args->csa_cachethis = ntohl(*p++);
421 args->csa_nrclists = ntohl(*p++);
422 args->csa_rclists = NULL;
423 if (args->csa_nrclists) {
Dan Carpenter0439f312012-06-12 10:37:08 +0300424 args->csa_rclists = kmalloc_array(args->csa_nrclists,
425 sizeof(*args->csa_rclists),
426 GFP_KERNEL);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400427 if (unlikely(args->csa_rclists == NULL))
Anna Schumaker17965492017-04-07 14:15:01 -0400428 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400429
430 for (i = 0; i < args->csa_nrclists; i++) {
431 status = decode_rc_list(xdr, &args->csa_rclists[i]);
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500432 if (status) {
433 args->csa_nrclists = i;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400434 goto out_free;
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500435 }
Benny Halevy4aece6a2009-04-01 09:23:26 -0400436 }
437 }
Anna Schumaker17965492017-04-07 14:15:01 -0400438 return 0;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400439
440out_free:
441 for (i = 0; i < args->csa_nrclists; i++)
442 kfree(args->csa_rclists[i].rcl_refcalls);
443 kfree(args->csa_rclists);
Anna Schumaker17965492017-04-07 14:15:01 -0400444 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400445}
446
Andy Adamson9733f0d2010-01-22 12:03:08 -0500447static __be32 decode_recallany_args(struct svc_rqst *rqstp,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500448 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200449 void *argp)
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500450{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200451 struct cb_recallanyargs *args = argp;
Peng Taod743c3c2011-10-23 20:22:38 -0700452 uint32_t bitmap[2];
453 __be32 *p, status;
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500454
Chuck Levereb72f482019-02-11 11:24:21 -0500455 p = xdr_inline_decode(xdr, 4);
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500456 if (unlikely(p == NULL))
457 return htonl(NFS4ERR_BADXDR);
458 args->craa_objs_to_keep = ntohl(*p++);
Peng Taod743c3c2011-10-23 20:22:38 -0700459 status = decode_bitmap(xdr, bitmap);
460 if (unlikely(status))
461 return status;
462 args->craa_type_mask = bitmap[0];
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500463
464 return 0;
465}
466
Andy Adamson9733f0d2010-01-22 12:03:08 -0500467static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500468 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200469 void *argp)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500470{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200471 struct cb_recallslotargs *args = argp;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500472 __be32 *p;
473
Chuck Levereb72f482019-02-11 11:24:21 -0500474 p = xdr_inline_decode(xdr, 4);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500475 if (unlikely(p == NULL))
476 return htonl(NFS4ERR_BADXDR);
Trond Myklebustd5fb4ce2012-11-20 20:24:02 -0500477 args->crsa_target_highest_slotid = ntohl(*p++);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500478 return 0;
479}
480
Jeff Laytondb783682016-09-17 18:17:36 -0400481static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
482{
483 __be32 *p;
484 unsigned int len;
485
Chuck Levereb72f482019-02-11 11:24:21 -0500486 p = xdr_inline_decode(xdr, 12);
Jeff Laytondb783682016-09-17 18:17:36 -0400487 if (unlikely(p == NULL))
488 return htonl(NFS4ERR_BADXDR);
489
490 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
491 len = be32_to_cpu(*p);
492
Chuck Levereb72f482019-02-11 11:24:21 -0500493 p = xdr_inline_decode(xdr, len);
Jeff Laytondb783682016-09-17 18:17:36 -0400494 if (unlikely(p == NULL))
495 return htonl(NFS4ERR_BADXDR);
496
497 /* Only try to decode if the length is right */
498 if (len == 20) {
499 p += 2; /* skip "lock id:" */
500 args->cbnl_owner.s_dev = be32_to_cpu(*p++);
501 xdr_decode_hyper(p, &args->cbnl_owner.id);
502 args->cbnl_valid = true;
503 } else {
504 args->cbnl_owner.s_dev = 0;
505 args->cbnl_owner.id = 0;
506 args->cbnl_valid = false;
507 }
508 return 0;
509}
510
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200511static __be32 decode_notify_lock_args(struct svc_rqst *rqstp,
512 struct xdr_stream *xdr, void *argp)
Jeff Laytondb783682016-09-17 18:17:36 -0400513{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200514 struct cb_notify_lock_args *args = argp;
Jeff Laytondb783682016-09-17 18:17:36 -0400515 __be32 status;
516
517 status = decode_fh(xdr, &args->cbnl_fh);
518 if (unlikely(status != 0))
Anna Schumaker535ece22017-04-07 14:15:02 -0400519 return status;
520 return decode_lockowner(xdr, args);
Jeff Laytondb783682016-09-17 18:17:36 -0400521}
522
Benny Halevy4aece6a2009-04-01 09:23:26 -0400523#endif /* CONFIG_NFS_V4_1 */
Olga Kornievskaia5178a122018-07-09 15:13:28 -0400524#ifdef CONFIG_NFS_V4_2
525static __be32 decode_write_response(struct xdr_stream *xdr,
526 struct cb_offloadargs *args)
527{
528 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400529
Olga Kornievskaia5178a122018-07-09 15:13:28 -0400530 /* skip the always zero field */
Chuck Levereb72f482019-02-11 11:24:21 -0500531 p = xdr_inline_decode(xdr, 4);
Olga Kornievskaia5178a122018-07-09 15:13:28 -0400532 if (unlikely(!p))
533 goto out;
534 p++;
535
536 /* decode count, stable_how, verifier */
537 p = xdr_inline_decode(xdr, 8 + 4);
538 if (unlikely(!p))
539 goto out;
540 p = xdr_decode_hyper(p, &args->wr_count);
541 args->wr_writeverf.committed = be32_to_cpup(p);
542 p = xdr_inline_decode(xdr, NFS4_VERIFIER_SIZE);
543 if (likely(p)) {
544 memcpy(&args->wr_writeverf.verifier.data[0], p,
545 NFS4_VERIFIER_SIZE);
546 return 0;
547 }
548out:
549 return htonl(NFS4ERR_RESOURCE);
550}
551
552static __be32 decode_offload_args(struct svc_rqst *rqstp,
553 struct xdr_stream *xdr,
554 void *data)
555{
556 struct cb_offloadargs *args = data;
557 __be32 *p;
558 __be32 status;
559
560 /* decode fh */
561 status = decode_fh(xdr, &args->coa_fh);
562 if (unlikely(status != 0))
563 return status;
564
565 /* decode stateid */
566 status = decode_stateid(xdr, &args->coa_stateid);
567 if (unlikely(status != 0))
568 return status;
569
570 /* decode status */
Chuck Levereb72f482019-02-11 11:24:21 -0500571 p = xdr_inline_decode(xdr, 4);
Olga Kornievskaia5178a122018-07-09 15:13:28 -0400572 if (unlikely(!p))
573 goto out;
574 args->error = ntohl(*p++);
575 if (!args->error) {
576 status = decode_write_response(xdr, args);
577 if (unlikely(status != 0))
578 return status;
579 } else {
580 p = xdr_inline_decode(xdr, 8);
581 if (unlikely(!p))
582 goto out;
583 p = xdr_decode_hyper(p, &args->wr_count);
584 }
585 return 0;
586out:
587 return htonl(NFS4ERR_RESOURCE);
588}
589#endif /* CONFIG_NFS_V4_2 */
Al Viroe6f684f2006-10-19 23:28:50 -0700590static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Trond Myklebustab6e9aa2017-02-19 16:08:26 -0500592 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
593 return cpu_to_be32(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595}
596
Trond Myklebust8b064942018-03-20 17:03:12 -0400597static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, size_t sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Trond Myklebust8b064942018-03-20 17:03:12 -0400599 if (xdr_stream_encode_uint32_array(xdr, bitmap, sz) < 0)
600 return cpu_to_be32(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602}
603
Al Viroe6f684f2006-10-19 23:28:50 -0700604static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Al Viro5704fde2006-10-19 23:28:51 -0700606 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
609 return 0;
610 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800611 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return htonl(NFS4ERR_RESOURCE);
613 p = xdr_encode_hyper(p, change);
614 return 0;
615}
616
Al Viroe6f684f2006-10-19 23:28:50 -0700617static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Al Viro5704fde2006-10-19 23:28:51 -0700619 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
622 return 0;
623 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800624 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return htonl(NFS4ERR_RESOURCE);
626 p = xdr_encode_hyper(p, size);
627 return 0;
628}
629
Al Viroe6f684f2006-10-19 23:28:50 -0700630static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Al Viro5704fde2006-10-19 23:28:51 -0700632 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 p = xdr_reserve_space(xdr, 12);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800635 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return htonl(NFS4ERR_RESOURCE);
637 p = xdr_encode_hyper(p, time->tv_sec);
638 *p = htonl(time->tv_nsec);
639 return 0;
640}
641
Al Viroe6f684f2006-10-19 23:28:50 -0700642static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
645 return 0;
646 return encode_attr_time(xdr,time);
647}
648
Al Viroe6f684f2006-10-19 23:28:50 -0700649static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
652 return 0;
653 return encode_attr_time(xdr,time);
654}
655
Al Viroe6f684f2006-10-19 23:28:50 -0700656static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Al Viroe6f684f2006-10-19 23:28:50 -0700658 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 hdr->status = xdr_reserve_space(xdr, 4);
661 if (unlikely(hdr->status == NULL))
662 return htonl(NFS4ERR_RESOURCE);
663 status = encode_string(xdr, hdr->taglen, hdr->tag);
664 if (unlikely(status != 0))
665 return status;
666 hdr->nops = xdr_reserve_space(xdr, 4);
667 if (unlikely(hdr->nops == NULL))
668 return htonl(NFS4ERR_RESOURCE);
669 return 0;
670}
671
Al Viroe6f684f2006-10-19 23:28:50 -0700672static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Al Viro5704fde2006-10-19 23:28:51 -0700674 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 p = xdr_reserve_space(xdr, 8);
677 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500678 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 *p++ = htonl(op);
680 *p = res;
681 return 0;
682}
683
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200684static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr,
685 const void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200687 const struct cb_getattrres *res = resp;
Al Viro5704fde2006-10-19 23:28:51 -0700688 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700689 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (unlikely(status != 0))
692 goto out;
Trond Myklebust8b064942018-03-20 17:03:12 -0400693 status = encode_attr_bitmap(xdr, res->bitmap, ARRAY_SIZE(res->bitmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (unlikely(status != 0))
695 goto out;
Trond Myklebust8b064942018-03-20 17:03:12 -0400696 status = cpu_to_be32(NFS4ERR_RESOURCE);
697 savep = xdr_reserve_space(xdr, sizeof(*savep));
698 if (unlikely(!savep))
699 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
701 if (unlikely(status != 0))
702 goto out;
703 status = encode_attr_size(xdr, res->bitmap, res->size);
704 if (unlikely(status != 0))
705 goto out;
706 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
707 if (unlikely(status != 0))
708 goto out;
709 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
710 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
711out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return status;
713}
714
Benny Halevy34bc47c92009-04-01 09:23:22 -0400715#if defined(CONFIG_NFS_V4_1)
716
Andy Adamson9733f0d2010-01-22 12:03:08 -0500717static __be32 encode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400718 const struct nfs4_sessionid *sid)
719{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500720 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400721
Kinglong Mee590184a2015-09-24 20:57:37 +0800722 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400723 if (unlikely(p == NULL))
724 return htonl(NFS4ERR_RESOURCE);
725
Kinglong Mee590184a2015-09-24 20:57:37 +0800726 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400727 return 0;
728}
729
Andy Adamson9733f0d2010-01-22 12:03:08 -0500730static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400731 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200732 const void *resp)
Benny Halevy4aece6a2009-04-01 09:23:26 -0400733{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200734 const struct cb_sequenceres *res = resp;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500735 __be32 *p;
Dan Carpentere216c8c2012-06-12 10:37:39 +0300736 __be32 status = res->csr_status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400737
738 if (unlikely(status != 0))
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400739 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400740
Kinglong Meee0a63c02015-09-24 20:58:38 +0800741 status = encode_sessionid(xdr, &res->csr_sessionid);
742 if (status)
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400743 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400744
745 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
746 if (unlikely(p == NULL))
747 return htonl(NFS4ERR_RESOURCE);
748
749 *p++ = htonl(res->csr_sequenceid);
750 *p++ = htonl(res->csr_slotid);
751 *p++ = htonl(res->csr_highestslotid);
752 *p++ = htonl(res->csr_target_highestslotid);
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400753 return 0;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400754}
755
Benny Halevy34bc47c92009-04-01 09:23:22 -0400756static __be32
757preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
758{
Benny Halevy281fe152009-04-01 09:23:27 -0400759 if (op_nr == OP_CB_SEQUENCE) {
760 if (nop != 0)
761 return htonl(NFS4ERR_SEQUENCE_POS);
762 } else {
763 if (nop == 0)
764 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
765 }
766
Benny Halevy34bc47c92009-04-01 09:23:22 -0400767 switch (op_nr) {
768 case OP_CB_GETATTR:
769 case OP_CB_RECALL:
Benny Halevy4aece6a2009-04-01 09:23:26 -0400770 case OP_CB_SEQUENCE:
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500771 case OP_CB_RECALL_ANY:
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500772 case OP_CB_RECALL_SLOT:
Fred Isamanf2a62562011-01-06 11:36:29 +0000773 case OP_CB_LAYOUTRECALL:
Marc Eshel1be56832011-05-22 19:47:09 +0300774 case OP_CB_NOTIFY_DEVICEID:
Jeff Laytondb783682016-09-17 18:17:36 -0400775 case OP_CB_NOTIFY_LOCK:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400776 *op = &callback_ops[op_nr];
777 break;
778
Benny Halevy34bc47c92009-04-01 09:23:22 -0400779 case OP_CB_NOTIFY:
780 case OP_CB_PUSH_DELEG:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400781 case OP_CB_RECALLABLE_OBJ_AVAIL:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400782 case OP_CB_WANTS_CANCELLED:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400783 return htonl(NFS4ERR_NOTSUPP);
784
785 default:
786 return htonl(NFS4ERR_OP_ILLEGAL);
787 }
788
789 return htonl(NFS_OK);
790}
791
Trond Myklebust810d82e2016-01-23 15:18:18 -0500792static void nfs4_callback_free_slot(struct nfs4_session *session,
793 struct nfs4_slot *slot)
Andy Adamson42acd022011-01-06 02:04:34 +0000794{
795 struct nfs4_slot_table *tbl = &session->bc_slot_table;
796
797 spin_lock(&tbl->slot_tbl_lock);
798 /*
799 * Let the state manager know callback processing done.
800 * A single slot, so highest used slotid is either 0 or -1
801 */
Trond Myklebust810d82e2016-01-23 15:18:18 -0500802 nfs4_free_slot(tbl, slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000803 spin_unlock(&tbl->slot_tbl_lock);
804}
805
Trond Myklebust55a67392011-08-02 14:46:29 -0400806static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000807{
Trond Myklebust810d82e2016-01-23 15:18:18 -0500808 if (cps->slot) {
809 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
810 cps->slot = NULL;
811 }
Andy Adamson42acd022011-01-06 02:04:34 +0000812}
813
Benny Halevy34bc47c92009-04-01 09:23:22 -0400814#else /* CONFIG_NFS_V4_1 */
815
816static __be32
817preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
818{
819 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
820}
821
Trond Myklebust55a67392011-08-02 14:46:29 -0400822static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000823{
824}
Benny Halevy34bc47c92009-04-01 09:23:22 -0400825#endif /* CONFIG_NFS_V4_1 */
826
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400827#ifdef CONFIG_NFS_V4_2
828static __be32
829preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
830{
831 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
832 if (status != htonl(NFS4ERR_OP_ILLEGAL))
833 return status;
834
Olga Kornievskaia5178a122018-07-09 15:13:28 -0400835 if (op_nr == OP_CB_OFFLOAD) {
836 *op = &callback_ops[op_nr];
837 return htonl(NFS_OK);
838 } else
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400839 return htonl(NFS4ERR_NOTSUPP);
840 return htonl(NFS4ERR_OP_ILLEGAL);
841}
842#else /* CONFIG_NFS_V4_2 */
843static __be32
844preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
845{
846 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
847}
848#endif /* CONFIG_NFS_V4_2 */
849
Benny Halevy34bc47c92009-04-01 09:23:22 -0400850static __be32
851preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
852{
853 switch (op_nr) {
854 case OP_CB_GETATTR:
855 case OP_CB_RECALL:
856 *op = &callback_ops[op_nr];
857 break;
858 default:
859 return htonl(NFS4ERR_OP_ILLEGAL);
860 }
861
862 return htonl(NFS_OK);
863}
864
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400865static __be32 process_op(int nop, struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 struct xdr_stream *xdr_in, void *argp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000867 struct xdr_stream *xdr_out, void *resp,
868 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500870 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500871 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400872 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700874 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500877 if (unlikely(status))
878 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400880 switch (cps->minorversion) {
881 case 0:
882 status = preprocess_nfs4_op(op_nr, &op);
883 break;
884 case 1:
885 status = preprocess_nfs41_op(nop, op_nr, &op);
886 break;
887 case 2:
888 status = preprocess_nfs42_op(nop, op_nr, &op);
889 break;
890 default:
891 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
892 }
893
Benny Halevy34bc47c92009-04-01 09:23:22 -0400894 if (status == htonl(NFS4ERR_OP_ILLEGAL))
895 op_nr = OP_CB_ILLEGAL;
Andy Adamsonb92b3012010-01-14 17:45:05 -0500896 if (status)
897 goto encode_hdr;
Andy Adamson31d2b432010-01-14 17:45:04 -0500898
Andy Adamsonc36fca52011-01-06 02:04:32 +0000899 if (cps->drc_status) {
900 status = cps->drc_status;
Andy Adamson49110962010-01-14 17:45:08 -0500901 goto encode_hdr;
902 }
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 maxlen = xdr_out->end - xdr_out->p;
905 if (maxlen > 0 && maxlen < PAGE_SIZE) {
Andy Adamsone95e60d2010-01-14 17:45:06 -0500906 status = op->decode_args(rqstp, xdr_in, argp);
907 if (likely(status == 0))
Andy Adamsonc36fca52011-01-06 02:04:32 +0000908 status = op->process_op(argp, resp, cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 } else
910 status = htonl(NFS4ERR_RESOURCE);
911
Andy Adamsonb92b3012010-01-14 17:45:05 -0500912encode_hdr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 res = encode_op_hdr(xdr_out, op_nr, status);
Andy Adamson31d2b432010-01-14 17:45:04 -0500914 if (unlikely(res))
915 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (op->encode_res != NULL && status == 0)
917 status = op->encode_res(rqstp, xdr_out, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return status;
919}
920
921/*
922 * Decode, process and encode a COMPOUND
923 */
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200924static __be32 nfs4_callback_compound(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400926 struct cb_compound_hdr_arg hdr_arg = { 0 };
927 struct cb_compound_hdr_res hdr_res = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct xdr_stream xdr_in, xdr_out;
Andy Adamsonc36fca52011-01-06 02:04:32 +0000929 __be32 *p, status;
930 struct cb_process_state cps = {
931 .drc_status = 0,
932 .clp = NULL,
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400933 .net = SVC_NET(rqstp),
Andy Adamsonc36fca52011-01-06 02:04:32 +0000934 };
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400935 unsigned int nops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500937 xdr_init_decode(&xdr_in, &rqstp->rq_arg,
938 rqstp->rq_arg.head[0].iov_base, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Al Viro5704fde2006-10-19 23:28:51 -0700940 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Chuck Lever0ccc61b2019-02-11 11:24:05 -0500941 xdr_init_encode(&xdr_out, &rqstp->rq_res, p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400943 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
Vaishali Thakkar4ed0d832015-06-10 20:15:29 +0530944 if (status == htonl(NFS4ERR_RESOURCE))
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400945 return rpc_garbage_args;
946
Andy Adamsonc36fca52011-01-06 02:04:32 +0000947 if (hdr_arg.minorversion == 0) {
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400948 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
Olga Kornievskaia32cd3ee2018-07-26 16:04:47 -0400949 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp)) {
950 if (cps.clp)
951 nfs_put_client(cps.clp);
Chuck Levera4e187d2016-06-29 13:55:22 -0400952 goto out_invalidcred;
Olga Kornievskaia32cd3ee2018-07-26 16:04:47 -0400953 }
Andy Adamson778be232011-01-25 15:38:01 +0000954 }
Andy Adamsonc36fca52011-01-06 02:04:32 +0000955
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400956 cps.minorversion = hdr_arg.minorversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 hdr_res.taglen = hdr_arg.taglen;
958 hdr_res.tag = hdr_arg.tag;
Olga Kornievskaia32cd3ee2018-07-26 16:04:47 -0400959 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) {
960 if (cps.clp)
961 nfs_put_client(cps.clp);
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400962 return rpc_system_err;
Olga Kornievskaia32cd3ee2018-07-26 16:04:47 -0400963 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400964 while (status == 0 && nops != hdr_arg.nops) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400965 status = process_op(nops, rqstp, &xdr_in,
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200966 rqstp->rq_argp, &xdr_out, rqstp->rq_resp,
967 &cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 nops++;
969 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400970
Andy Adamson31d2b432010-01-14 17:45:04 -0500971 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
972 * resource error in cb_compound status without returning op */
973 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
974 status = htonl(NFS4ERR_RESOURCE);
975 nops--;
976 }
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *hdr_res.status = status;
979 *hdr_res.nops = htonl(nops);
Trond Myklebust55a67392011-08-02 14:46:29 -0400980 nfs4_cb_free_slot(&cps);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000981 nfs_put_client(cps.clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return rpc_success;
Chuck Levera4e187d2016-06-29 13:55:22 -0400983
984out_invalidcred:
985 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
Trond Myklebust83dd59a2019-04-09 11:46:14 -0400986 return svc_return_autherr(rqstp, rpc_autherr_badcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989/*
990 * Define NFS4 callback COMPOUND ops.
991 */
992static struct callback_op callback_ops[] = {
993 [0] = {
994 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
995 },
996 [OP_CB_GETATTR] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200997 .process_op = nfs4_callback_getattr,
998 .decode_args = decode_getattr_args,
999 .encode_res = encode_getattr_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
1001 },
1002 [OP_CB_RECALL] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001003 .process_op = nfs4_callback_recall,
1004 .decode_args = decode_recall_args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -04001006 },
1007#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +00001008 [OP_CB_LAYOUTRECALL] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001009 .process_op = nfs4_callback_layoutrecall,
1010 .decode_args = decode_layoutrecall_args,
Fred Isamanf2a62562011-01-06 11:36:29 +00001011 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
1012 },
Marc Eshel1be56832011-05-22 19:47:09 +03001013 [OP_CB_NOTIFY_DEVICEID] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001014 .process_op = nfs4_callback_devicenotify,
1015 .decode_args = decode_devicenotify_args,
Marc Eshel1be56832011-05-22 19:47:09 +03001016 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
1017 },
Benny Halevy4aece6a2009-04-01 09:23:26 -04001018 [OP_CB_SEQUENCE] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001019 .process_op = nfs4_callback_sequence,
1020 .decode_args = decode_cb_sequence_args,
1021 .encode_res = encode_cb_sequence_res,
Benny Halevy4aece6a2009-04-01 09:23:26 -04001022 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
1023 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -05001024 [OP_CB_RECALL_ANY] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001025 .process_op = nfs4_callback_recallany,
1026 .decode_args = decode_recallany_args,
Alexandros Batsakis31f09602009-12-05 13:27:02 -05001027 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
1028 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001029 [OP_CB_RECALL_SLOT] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001030 .process_op = nfs4_callback_recallslot,
1031 .decode_args = decode_recallslot_args,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001032 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
1033 },
Jeff Laytondb783682016-09-17 18:17:36 -04001034 [OP_CB_NOTIFY_LOCK] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001035 .process_op = nfs4_callback_notify_lock,
1036 .decode_args = decode_notify_lock_args,
Jeff Laytondb783682016-09-17 18:17:36 -04001037 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
1038 },
Benny Halevy4aece6a2009-04-01 09:23:26 -04001039#endif /* CONFIG_NFS_V4_1 */
Olga Kornievskaia5178a122018-07-09 15:13:28 -04001040#ifdef CONFIG_NFS_V4_2
1041 [OP_CB_OFFLOAD] = {
1042 .process_op = nfs4_callback_offload,
1043 .decode_args = decode_offload_args,
1044 .res_maxsize = CB_OP_OFFLOAD_RES_MAXSZ,
1045 },
1046#endif /* CONFIG_NFS_V4_2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047};
1048
1049/*
1050 * Define NFS4 callback procedures
1051 */
Christoph Hellwig860bda22017-05-12 16:11:49 +02001052static const struct svc_procedure nfs4_callback_procedures1[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 [CB_NULL] = {
1054 .pc_func = nfs4_callback_null,
Christoph Hellwig026fec72017-05-08 19:01:48 +02001055 .pc_decode = nfs4_decode_void,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001056 .pc_encode = nfs4_encode_void,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 .pc_xdrressize = 1,
1058 },
1059 [CB_COMPOUND] = {
1060 .pc_func = nfs4_callback_compound,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001061 .pc_encode = nfs4_encode_void,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 .pc_argsize = 256,
1063 .pc_ressize = 256,
1064 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1065 }
1066};
1067
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001068static unsigned int nfs4_callback_count1[ARRAY_SIZE(nfs4_callback_procedures1)];
Christoph Hellwige9679182017-05-12 16:21:37 +02001069const struct svc_version nfs4_callback_version1 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 .vs_vers = 1,
1071 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1072 .vs_proc = nfs4_callback_procedures1,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001073 .vs_count = nfs4_callback_count1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1075 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001076 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001077 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078};
1079
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001080static unsigned int nfs4_callback_count4[ARRAY_SIZE(nfs4_callback_procedures1)];
Christoph Hellwige9679182017-05-12 16:21:37 +02001081const struct svc_version nfs4_callback_version4 = {
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001082 .vs_vers = 4,
1083 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1084 .vs_proc = nfs4_callback_procedures1,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001085 .vs_count = nfs4_callback_count4,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001086 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1087 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001088 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001089 .vs_need_cong_ctrl = true,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001090};