blob: a87a562734077c221884edaf7962086af6e7a643 [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
Jeff Laytonb60475c2016-09-17 18:17:31 -040075static __be32 *read_buf(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Al Viro5704fde2006-10-19 23:28:51 -070077 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 p = xdr_inline_decode(xdr, nbytes);
80 if (unlikely(p == NULL))
Trond Myklebust756b9b32015-12-07 12:52:23 -080081 printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return p;
83}
84
Trond Myklebustc065eee2017-02-19 16:08:28 -050085static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len,
86 const char **str, size_t maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Trond Myklebustc065eee2017-02-19 16:08:28 -050088 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Trond Myklebustc065eee2017-02-19 16:08:28 -050090 err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen);
91 if (err < 0)
92 return cpu_to_be32(NFS4ERR_RESOURCE);
93 *len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
95}
96
Al Viroe6f684f2006-10-19 23:28:50 -070097static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Al Viro5704fde2006-10-19 23:28:51 -070099 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 p = read_buf(xdr, 4);
102 if (unlikely(p == NULL))
103 return htonl(NFS4ERR_RESOURCE);
104 fh->size = ntohl(*p);
105 if (fh->size > NFS4_FHSIZE)
106 return htonl(NFS4ERR_BADHANDLE);
107 p = read_buf(xdr, fh->size);
108 if (unlikely(p == NULL))
109 return htonl(NFS4ERR_RESOURCE);
110 memcpy(&fh->data[0], p, fh->size);
111 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
112 return 0;
113}
114
Al Viroe6f684f2006-10-19 23:28:50 -0700115static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Al Viro5704fde2006-10-19 23:28:51 -0700117 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned int attrlen;
119
120 p = read_buf(xdr, 4);
121 if (unlikely(p == NULL))
122 return htonl(NFS4ERR_RESOURCE);
123 attrlen = ntohl(*p);
124 p = read_buf(xdr, attrlen << 2);
125 if (unlikely(p == NULL))
126 return htonl(NFS4ERR_RESOURCE);
127 if (likely(attrlen > 0))
128 bitmap[0] = ntohl(*p++);
129 if (attrlen > 1)
130 bitmap[1] = ntohl(*p);
131 return 0;
132}
133
Al Viroe6f684f2006-10-19 23:28:50 -0700134static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Al Viro5704fde2006-10-19 23:28:51 -0700136 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500138 p = read_buf(xdr, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (unlikely(p == NULL))
140 return htonl(NFS4ERR_RESOURCE);
Trond Myklebust93b717f2016-05-16 17:42:43 -0400141 memcpy(stateid->data, p, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return 0;
143}
144
Trond Myklebust93b717f2016-05-16 17:42:43 -0400145static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
146{
147 stateid->type = NFS4_DELEGATION_STATEID_TYPE;
148 return decode_stateid(xdr, stateid);
149}
150
Al Viroe6f684f2006-10-19 23:28:50 -0700151static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Al Viro5704fde2006-10-19 23:28:51 -0700153 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700154 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Trond Myklebustc065eee2017-02-19 16:08:28 -0500156 status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (unlikely(status != 0))
158 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 p = read_buf(xdr, 12);
160 if (unlikely(p == NULL))
161 return htonl(NFS4ERR_RESOURCE);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400162 hdr->minorversion = ntohl(*p++);
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400163 /* Check for minor version support */
164 if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
Steve Dickson42c2c422013-05-22 12:50:38 -0400165 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
Benny Halevy48a9e2d2009-04-01 09:23:20 -0400166 } else {
Trond Myklebust9a3ba432012-03-12 18:01:48 -0400167 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400168 "illegal minor version %u!\n",
169 __func__, hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 hdr->nops = ntohl(*p);
173 return 0;
174}
175
Al Viroe6f684f2006-10-19 23:28:50 -0700176static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Al Viro5704fde2006-10-19 23:28:51 -0700178 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 p = read_buf(xdr, 4);
180 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500181 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *op = ntohl(*p);
183 return 0;
184}
185
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200186static __be32 decode_getattr_args(struct svc_rqst *rqstp,
187 struct xdr_stream *xdr, void *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200189 struct cb_getattrargs *args = argp;
Al Viroe6f684f2006-10-19 23:28:50 -0700190 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 status = decode_fh(xdr, &args->fh);
193 if (unlikely(status != 0))
Anna Schumaker56938bb2017-04-07 14:14:58 -0400194 return status;
195 return decode_bitmap(xdr, args->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200198static __be32 decode_recall_args(struct svc_rqst *rqstp,
199 struct xdr_stream *xdr, void *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200201 struct cb_recallargs *args = argp;
Al Viro5704fde2006-10-19 23:28:51 -0700202 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700203 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Trond Myklebust93b717f2016-05-16 17:42:43 -0400205 status = decode_delegation_stateid(xdr, &args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (unlikely(status != 0))
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400207 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 p = read_buf(xdr, 4);
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400209 if (unlikely(p == NULL))
210 return htonl(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 args->truncate = ntohl(*p);
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400212 return decode_fh(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Benny Halevy4aece6a2009-04-01 09:23:26 -0400215#if defined(CONFIG_NFS_V4_1)
Trond Myklebust93b717f2016-05-16 17:42:43 -0400216static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
217{
218 stateid->type = NFS4_LAYOUT_STATEID_TYPE;
219 return decode_stateid(xdr, stateid);
220}
Benny Halevy4aece6a2009-04-01 09:23:26 -0400221
Fred Isamanf2a62562011-01-06 11:36:29 +0000222static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200223 struct xdr_stream *xdr, void *argp)
Fred Isamanf2a62562011-01-06 11:36:29 +0000224{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200225 struct cb_layoutrecallargs *args = argp;
Fred Isamanf2a62562011-01-06 11:36:29 +0000226 __be32 *p;
227 __be32 status = 0;
228 uint32_t iomode;
229
Fred Isamanf2a62562011-01-06 11:36:29 +0000230 p = read_buf(xdr, 4 * sizeof(uint32_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400231 if (unlikely(p == NULL))
232 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000233
234 args->cbl_layout_type = ntohl(*p++);
235 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300236 * as it is unusable and ignored with the other types.
Fred Isamanf2a62562011-01-06 11:36:29 +0000237 */
238 iomode = ntohl(*p++);
239 args->cbl_layoutchanged = ntohl(*p++);
240 args->cbl_recall_type = ntohl(*p++);
241
242 if (args->cbl_recall_type == RETURN_FILE) {
243 args->cbl_range.iomode = iomode;
244 status = decode_fh(xdr, &args->cbl_fh);
245 if (unlikely(status != 0))
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400246 return status;
Fred Isamanf2a62562011-01-06 11:36:29 +0000247
248 p = read_buf(xdr, 2 * sizeof(uint64_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400249 if (unlikely(p == NULL))
250 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000251 p = xdr_decode_hyper(p, &args->cbl_range.offset);
252 p = xdr_decode_hyper(p, &args->cbl_range.length);
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400253 return decode_layout_stateid(xdr, &args->cbl_stateid);
Fred Isamanf2a62562011-01-06 11:36:29 +0000254 } else if (args->cbl_recall_type == RETURN_FSID) {
255 p = read_buf(xdr, 2 * sizeof(uint64_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400256 if (unlikely(p == NULL))
257 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000258 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
259 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400260 } else if (args->cbl_recall_type != RETURN_ALL)
261 return htonl(NFS4ERR_BADXDR);
262 return 0;
Fred Isamanf2a62562011-01-06 11:36:29 +0000263}
264
Marc Eshel1be56832011-05-22 19:47:09 +0300265static
266__be32 decode_devicenotify_args(struct svc_rqst *rqstp,
267 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200268 void *argp)
Marc Eshel1be56832011-05-22 19:47:09 +0300269{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200270 struct cb_devicenotifyargs *args = argp;
Marc Eshel1be56832011-05-22 19:47:09 +0300271 __be32 *p;
272 __be32 status = 0;
273 u32 tmp;
274 int n, i;
275 args->ndevs = 0;
276
277 /* Num of device notifications */
278 p = read_buf(xdr, sizeof(uint32_t));
279 if (unlikely(p == NULL)) {
280 status = htonl(NFS4ERR_BADXDR);
281 goto out;
282 }
283 n = ntohl(*p++);
284 if (n <= 0)
285 goto out;
Dan Carpenter363e0df02012-01-12 10:16:14 +0300286 if (n > ULONG_MAX / sizeof(*args->devs)) {
287 status = htonl(NFS4ERR_BADXDR);
288 goto out;
289 }
Marc Eshel1be56832011-05-22 19:47:09 +0300290
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500291 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
Marc Eshel1be56832011-05-22 19:47:09 +0300292 if (!args->devs) {
293 status = htonl(NFS4ERR_DELAY);
294 goto out;
295 }
296
297 /* Decode each dev notification */
298 for (i = 0; i < n; i++) {
299 struct cb_devicenotifyitem *dev = &args->devs[i];
300
301 p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
302 if (unlikely(p == NULL)) {
303 status = htonl(NFS4ERR_BADXDR);
304 goto err;
305 }
306
307 tmp = ntohl(*p++); /* bitmap size */
308 if (tmp != 1) {
309 status = htonl(NFS4ERR_INVAL);
310 goto err;
311 }
312 dev->cbd_notify_type = ntohl(*p++);
313 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
314 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
315 status = htonl(NFS4ERR_INVAL);
316 goto err;
317 }
318
319 tmp = ntohl(*p++); /* opaque size */
320 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
321 (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
322 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
323 (tmp != NFS4_DEVICEID4_SIZE + 4))) {
324 status = htonl(NFS4ERR_INVAL);
325 goto err;
326 }
327 dev->cbd_layout_type = ntohl(*p++);
328 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
329 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
330
331 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
332 p = read_buf(xdr, sizeof(uint32_t));
333 if (unlikely(p == NULL)) {
334 status = htonl(NFS4ERR_BADXDR);
335 goto err;
336 }
337 dev->cbd_immediate = ntohl(*p++);
338 } else {
339 dev->cbd_immediate = 0;
340 }
341
342 args->ndevs++;
343
344 dprintk("%s: type %d layout 0x%x immediate %d\n",
345 __func__, dev->cbd_notify_type, dev->cbd_layout_type,
346 dev->cbd_immediate);
347 }
348out:
349 dprintk("%s: status %d ndevs %d\n",
350 __func__, ntohl(status), args->ndevs);
351 return status;
352err:
353 kfree(args->devs);
354 goto out;
355}
356
Andy Adamson9733f0d2010-01-22 12:03:08 -0500357static __be32 decode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400358 struct nfs4_sessionid *sid)
359{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500360 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400361
Kinglong Mee590184a2015-09-24 20:57:37 +0800362 p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400363 if (unlikely(p == NULL))
Joe Perchesa419aef2009-08-18 11:18:35 -0700364 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400365
Kinglong Mee590184a2015-09-24 20:57:37 +0800366 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400367 return 0;
368}
369
Andy Adamson9733f0d2010-01-22 12:03:08 -0500370static __be32 decode_rc_list(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400371 struct referring_call_list *rc_list)
372{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500373 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400374 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500375 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400376
377 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
378 if (status)
379 goto out;
380
381 status = htonl(NFS4ERR_RESOURCE);
382 p = read_buf(xdr, sizeof(uint32_t));
383 if (unlikely(p == NULL))
384 goto out;
385
386 rc_list->rcl_nrefcalls = ntohl(*p++);
387 if (rc_list->rcl_nrefcalls) {
388 p = read_buf(xdr,
389 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
390 if (unlikely(p == NULL))
391 goto out;
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500392 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400393 sizeof(*rc_list->rcl_refcalls),
394 GFP_KERNEL);
395 if (unlikely(rc_list->rcl_refcalls == NULL))
396 goto out;
397 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
398 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
399 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
400 }
401 }
402 status = 0;
403
404out:
405 return status;
406}
407
Andy Adamson9733f0d2010-01-22 12:03:08 -0500408static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400409 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200410 void *argp)
Benny Halevy4aece6a2009-04-01 09:23:26 -0400411{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200412 struct cb_sequenceargs *args = argp;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500413 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400414 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500415 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400416
417 status = decode_sessionid(xdr, &args->csa_sessionid);
418 if (status)
Anna Schumaker17965492017-04-07 14:15:01 -0400419 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400420
Benny Halevy4aece6a2009-04-01 09:23:26 -0400421 p = read_buf(xdr, 5 * sizeof(uint32_t));
422 if (unlikely(p == NULL))
Anna Schumaker17965492017-04-07 14:15:01 -0400423 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400424
Ricardo Labiaga65fc64e2009-04-01 09:23:30 -0400425 args->csa_addr = svc_addr(rqstp);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400426 args->csa_sequenceid = ntohl(*p++);
427 args->csa_slotid = ntohl(*p++);
428 args->csa_highestslotid = ntohl(*p++);
429 args->csa_cachethis = ntohl(*p++);
430 args->csa_nrclists = ntohl(*p++);
431 args->csa_rclists = NULL;
432 if (args->csa_nrclists) {
Dan Carpenter0439f312012-06-12 10:37:08 +0300433 args->csa_rclists = kmalloc_array(args->csa_nrclists,
434 sizeof(*args->csa_rclists),
435 GFP_KERNEL);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400436 if (unlikely(args->csa_rclists == NULL))
Anna Schumaker17965492017-04-07 14:15:01 -0400437 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400438
439 for (i = 0; i < args->csa_nrclists; i++) {
440 status = decode_rc_list(xdr, &args->csa_rclists[i]);
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500441 if (status) {
442 args->csa_nrclists = i;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400443 goto out_free;
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500444 }
Benny Halevy4aece6a2009-04-01 09:23:26 -0400445 }
446 }
Anna Schumaker17965492017-04-07 14:15:01 -0400447 return 0;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400448
449out_free:
450 for (i = 0; i < args->csa_nrclists; i++)
451 kfree(args->csa_rclists[i].rcl_refcalls);
452 kfree(args->csa_rclists);
Anna Schumaker17965492017-04-07 14:15:01 -0400453 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400454}
455
Andy Adamson9733f0d2010-01-22 12:03:08 -0500456static __be32 decode_recallany_args(struct svc_rqst *rqstp,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500457 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200458 void *argp)
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500459{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200460 struct cb_recallanyargs *args = argp;
Peng Taod743c3c2011-10-23 20:22:38 -0700461 uint32_t bitmap[2];
462 __be32 *p, status;
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500463
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500464 p = read_buf(xdr, 4);
465 if (unlikely(p == NULL))
466 return htonl(NFS4ERR_BADXDR);
467 args->craa_objs_to_keep = ntohl(*p++);
Peng Taod743c3c2011-10-23 20:22:38 -0700468 status = decode_bitmap(xdr, bitmap);
469 if (unlikely(status))
470 return status;
471 args->craa_type_mask = bitmap[0];
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500472
473 return 0;
474}
475
Andy Adamson9733f0d2010-01-22 12:03:08 -0500476static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500477 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200478 void *argp)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500479{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200480 struct cb_recallslotargs *args = argp;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500481 __be32 *p;
482
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500483 p = read_buf(xdr, 4);
484 if (unlikely(p == NULL))
485 return htonl(NFS4ERR_BADXDR);
Trond Myklebustd5fb4ce2012-11-20 20:24:02 -0500486 args->crsa_target_highest_slotid = ntohl(*p++);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500487 return 0;
488}
489
Jeff Laytondb783682016-09-17 18:17:36 -0400490static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
491{
492 __be32 *p;
493 unsigned int len;
494
495 p = read_buf(xdr, 12);
496 if (unlikely(p == NULL))
497 return htonl(NFS4ERR_BADXDR);
498
499 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
500 len = be32_to_cpu(*p);
501
502 p = read_buf(xdr, len);
503 if (unlikely(p == NULL))
504 return htonl(NFS4ERR_BADXDR);
505
506 /* Only try to decode if the length is right */
507 if (len == 20) {
508 p += 2; /* skip "lock id:" */
509 args->cbnl_owner.s_dev = be32_to_cpu(*p++);
510 xdr_decode_hyper(p, &args->cbnl_owner.id);
511 args->cbnl_valid = true;
512 } else {
513 args->cbnl_owner.s_dev = 0;
514 args->cbnl_owner.id = 0;
515 args->cbnl_valid = false;
516 }
517 return 0;
518}
519
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200520static __be32 decode_notify_lock_args(struct svc_rqst *rqstp,
521 struct xdr_stream *xdr, void *argp)
Jeff Laytondb783682016-09-17 18:17:36 -0400522{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200523 struct cb_notify_lock_args *args = argp;
Jeff Laytondb783682016-09-17 18:17:36 -0400524 __be32 status;
525
526 status = decode_fh(xdr, &args->cbnl_fh);
527 if (unlikely(status != 0))
Anna Schumaker535ece22017-04-07 14:15:02 -0400528 return status;
529 return decode_lockowner(xdr, args);
Jeff Laytondb783682016-09-17 18:17:36 -0400530}
531
Benny Halevy4aece6a2009-04-01 09:23:26 -0400532#endif /* CONFIG_NFS_V4_1 */
Olga Kornievskaia5178a122018-07-09 15:13:28 -0400533#ifdef CONFIG_NFS_V4_2
534static __be32 decode_write_response(struct xdr_stream *xdr,
535 struct cb_offloadargs *args)
536{
537 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400538
Olga Kornievskaia5178a122018-07-09 15:13:28 -0400539 /* skip the always zero field */
540 p = read_buf(xdr, 4);
541 if (unlikely(!p))
542 goto out;
543 p++;
544
545 /* decode count, stable_how, verifier */
546 p = xdr_inline_decode(xdr, 8 + 4);
547 if (unlikely(!p))
548 goto out;
549 p = xdr_decode_hyper(p, &args->wr_count);
550 args->wr_writeverf.committed = be32_to_cpup(p);
551 p = xdr_inline_decode(xdr, NFS4_VERIFIER_SIZE);
552 if (likely(p)) {
553 memcpy(&args->wr_writeverf.verifier.data[0], p,
554 NFS4_VERIFIER_SIZE);
555 return 0;
556 }
557out:
558 return htonl(NFS4ERR_RESOURCE);
559}
560
561static __be32 decode_offload_args(struct svc_rqst *rqstp,
562 struct xdr_stream *xdr,
563 void *data)
564{
565 struct cb_offloadargs *args = data;
566 __be32 *p;
567 __be32 status;
568
569 /* decode fh */
570 status = decode_fh(xdr, &args->coa_fh);
571 if (unlikely(status != 0))
572 return status;
573
574 /* decode stateid */
575 status = decode_stateid(xdr, &args->coa_stateid);
576 if (unlikely(status != 0))
577 return status;
578
579 /* decode status */
580 p = read_buf(xdr, 4);
581 if (unlikely(!p))
582 goto out;
583 args->error = ntohl(*p++);
584 if (!args->error) {
585 status = decode_write_response(xdr, args);
586 if (unlikely(status != 0))
587 return status;
588 } else {
589 p = xdr_inline_decode(xdr, 8);
590 if (unlikely(!p))
591 goto out;
592 p = xdr_decode_hyper(p, &args->wr_count);
593 }
594 return 0;
595out:
596 return htonl(NFS4ERR_RESOURCE);
597}
598#endif /* CONFIG_NFS_V4_2 */
Al Viroe6f684f2006-10-19 23:28:50 -0700599static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Trond Myklebustab6e9aa2017-02-19 16:08:26 -0500601 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
602 return cpu_to_be32(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604}
605
Trond Myklebust8b064942018-03-20 17:03:12 -0400606static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, size_t sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Trond Myklebust8b064942018-03-20 17:03:12 -0400608 if (xdr_stream_encode_uint32_array(xdr, bitmap, sz) < 0)
609 return cpu_to_be32(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return 0;
611}
612
Al Viroe6f684f2006-10-19 23:28:50 -0700613static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Al Viro5704fde2006-10-19 23:28:51 -0700615 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
618 return 0;
619 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800620 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return htonl(NFS4ERR_RESOURCE);
622 p = xdr_encode_hyper(p, change);
623 return 0;
624}
625
Al Viroe6f684f2006-10-19 23:28:50 -0700626static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Al Viro5704fde2006-10-19 23:28:51 -0700628 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
631 return 0;
632 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800633 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return htonl(NFS4ERR_RESOURCE);
635 p = xdr_encode_hyper(p, size);
636 return 0;
637}
638
Al Viroe6f684f2006-10-19 23:28:50 -0700639static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Al Viro5704fde2006-10-19 23:28:51 -0700641 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 p = xdr_reserve_space(xdr, 12);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800644 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return htonl(NFS4ERR_RESOURCE);
646 p = xdr_encode_hyper(p, time->tv_sec);
647 *p = htonl(time->tv_nsec);
648 return 0;
649}
650
Al Viroe6f684f2006-10-19 23:28:50 -0700651static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
654 return 0;
655 return encode_attr_time(xdr,time);
656}
657
Al Viroe6f684f2006-10-19 23:28:50 -0700658static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
661 return 0;
662 return encode_attr_time(xdr,time);
663}
664
Al Viroe6f684f2006-10-19 23:28:50 -0700665static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Al Viroe6f684f2006-10-19 23:28:50 -0700667 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 hdr->status = xdr_reserve_space(xdr, 4);
670 if (unlikely(hdr->status == NULL))
671 return htonl(NFS4ERR_RESOURCE);
672 status = encode_string(xdr, hdr->taglen, hdr->tag);
673 if (unlikely(status != 0))
674 return status;
675 hdr->nops = xdr_reserve_space(xdr, 4);
676 if (unlikely(hdr->nops == NULL))
677 return htonl(NFS4ERR_RESOURCE);
678 return 0;
679}
680
Al Viroe6f684f2006-10-19 23:28:50 -0700681static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Al Viro5704fde2006-10-19 23:28:51 -0700683 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 p = xdr_reserve_space(xdr, 8);
686 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500687 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 *p++ = htonl(op);
689 *p = res;
690 return 0;
691}
692
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200693static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr,
694 const void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200696 const struct cb_getattrres *res = resp;
Al Viro5704fde2006-10-19 23:28:51 -0700697 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700698 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 if (unlikely(status != 0))
701 goto out;
Trond Myklebust8b064942018-03-20 17:03:12 -0400702 status = encode_attr_bitmap(xdr, res->bitmap, ARRAY_SIZE(res->bitmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (unlikely(status != 0))
704 goto out;
Trond Myklebust8b064942018-03-20 17:03:12 -0400705 status = cpu_to_be32(NFS4ERR_RESOURCE);
706 savep = xdr_reserve_space(xdr, sizeof(*savep));
707 if (unlikely(!savep))
708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
710 if (unlikely(status != 0))
711 goto out;
712 status = encode_attr_size(xdr, res->bitmap, res->size);
713 if (unlikely(status != 0))
714 goto out;
715 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
716 if (unlikely(status != 0))
717 goto out;
718 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
719 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return status;
722}
723
Benny Halevy34bc47c92009-04-01 09:23:22 -0400724#if defined(CONFIG_NFS_V4_1)
725
Andy Adamson9733f0d2010-01-22 12:03:08 -0500726static __be32 encode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400727 const struct nfs4_sessionid *sid)
728{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500729 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400730
Kinglong Mee590184a2015-09-24 20:57:37 +0800731 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400732 if (unlikely(p == NULL))
733 return htonl(NFS4ERR_RESOURCE);
734
Kinglong Mee590184a2015-09-24 20:57:37 +0800735 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400736 return 0;
737}
738
Andy Adamson9733f0d2010-01-22 12:03:08 -0500739static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400740 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200741 const void *resp)
Benny Halevy4aece6a2009-04-01 09:23:26 -0400742{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200743 const struct cb_sequenceres *res = resp;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500744 __be32 *p;
Dan Carpentere216c8c2012-06-12 10:37:39 +0300745 __be32 status = res->csr_status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400746
747 if (unlikely(status != 0))
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400748 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400749
Kinglong Meee0a63c02015-09-24 20:58:38 +0800750 status = encode_sessionid(xdr, &res->csr_sessionid);
751 if (status)
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400752 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400753
754 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
755 if (unlikely(p == NULL))
756 return htonl(NFS4ERR_RESOURCE);
757
758 *p++ = htonl(res->csr_sequenceid);
759 *p++ = htonl(res->csr_slotid);
760 *p++ = htonl(res->csr_highestslotid);
761 *p++ = htonl(res->csr_target_highestslotid);
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400762 return 0;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400763}
764
Benny Halevy34bc47c92009-04-01 09:23:22 -0400765static __be32
766preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
767{
Benny Halevy281fe152009-04-01 09:23:27 -0400768 if (op_nr == OP_CB_SEQUENCE) {
769 if (nop != 0)
770 return htonl(NFS4ERR_SEQUENCE_POS);
771 } else {
772 if (nop == 0)
773 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
774 }
775
Benny Halevy34bc47c92009-04-01 09:23:22 -0400776 switch (op_nr) {
777 case OP_CB_GETATTR:
778 case OP_CB_RECALL:
Benny Halevy4aece6a2009-04-01 09:23:26 -0400779 case OP_CB_SEQUENCE:
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500780 case OP_CB_RECALL_ANY:
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500781 case OP_CB_RECALL_SLOT:
Fred Isamanf2a62562011-01-06 11:36:29 +0000782 case OP_CB_LAYOUTRECALL:
Marc Eshel1be56832011-05-22 19:47:09 +0300783 case OP_CB_NOTIFY_DEVICEID:
Jeff Laytondb783682016-09-17 18:17:36 -0400784 case OP_CB_NOTIFY_LOCK:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400785 *op = &callback_ops[op_nr];
786 break;
787
Benny Halevy34bc47c92009-04-01 09:23:22 -0400788 case OP_CB_NOTIFY:
789 case OP_CB_PUSH_DELEG:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400790 case OP_CB_RECALLABLE_OBJ_AVAIL:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400791 case OP_CB_WANTS_CANCELLED:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400792 return htonl(NFS4ERR_NOTSUPP);
793
794 default:
795 return htonl(NFS4ERR_OP_ILLEGAL);
796 }
797
798 return htonl(NFS_OK);
799}
800
Trond Myklebust810d82e2016-01-23 15:18:18 -0500801static void nfs4_callback_free_slot(struct nfs4_session *session,
802 struct nfs4_slot *slot)
Andy Adamson42acd022011-01-06 02:04:34 +0000803{
804 struct nfs4_slot_table *tbl = &session->bc_slot_table;
805
806 spin_lock(&tbl->slot_tbl_lock);
807 /*
808 * Let the state manager know callback processing done.
809 * A single slot, so highest used slotid is either 0 or -1
810 */
Trond Myklebust810d82e2016-01-23 15:18:18 -0500811 nfs4_free_slot(tbl, slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000812 spin_unlock(&tbl->slot_tbl_lock);
813}
814
Trond Myklebust55a67392011-08-02 14:46:29 -0400815static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000816{
Trond Myklebust810d82e2016-01-23 15:18:18 -0500817 if (cps->slot) {
818 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
819 cps->slot = NULL;
820 }
Andy Adamson42acd022011-01-06 02:04:34 +0000821}
822
Benny Halevy34bc47c92009-04-01 09:23:22 -0400823#else /* CONFIG_NFS_V4_1 */
824
825static __be32
826preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
827{
828 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
829}
830
Trond Myklebust55a67392011-08-02 14:46:29 -0400831static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000832{
833}
Benny Halevy34bc47c92009-04-01 09:23:22 -0400834#endif /* CONFIG_NFS_V4_1 */
835
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400836#ifdef CONFIG_NFS_V4_2
837static __be32
838preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
839{
840 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
841 if (status != htonl(NFS4ERR_OP_ILLEGAL))
842 return status;
843
Olga Kornievskaia5178a122018-07-09 15:13:28 -0400844 if (op_nr == OP_CB_OFFLOAD) {
845 *op = &callback_ops[op_nr];
846 return htonl(NFS_OK);
847 } else
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400848 return htonl(NFS4ERR_NOTSUPP);
849 return htonl(NFS4ERR_OP_ILLEGAL);
850}
851#else /* CONFIG_NFS_V4_2 */
852static __be32
853preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
854{
855 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
856}
857#endif /* CONFIG_NFS_V4_2 */
858
Benny Halevy34bc47c92009-04-01 09:23:22 -0400859static __be32
860preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
861{
862 switch (op_nr) {
863 case OP_CB_GETATTR:
864 case OP_CB_RECALL:
865 *op = &callback_ops[op_nr];
866 break;
867 default:
868 return htonl(NFS4ERR_OP_ILLEGAL);
869 }
870
871 return htonl(NFS_OK);
872}
873
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400874static __be32 process_op(int nop, struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 struct xdr_stream *xdr_in, void *argp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000876 struct xdr_stream *xdr_out, void *resp,
877 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500879 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500880 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400881 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700883 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500886 if (unlikely(status))
887 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400889 switch (cps->minorversion) {
890 case 0:
891 status = preprocess_nfs4_op(op_nr, &op);
892 break;
893 case 1:
894 status = preprocess_nfs41_op(nop, op_nr, &op);
895 break;
896 case 2:
897 status = preprocess_nfs42_op(nop, op_nr, &op);
898 break;
899 default:
900 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
901 }
902
Benny Halevy34bc47c92009-04-01 09:23:22 -0400903 if (status == htonl(NFS4ERR_OP_ILLEGAL))
904 op_nr = OP_CB_ILLEGAL;
Andy Adamsonb92b3012010-01-14 17:45:05 -0500905 if (status)
906 goto encode_hdr;
Andy Adamson31d2b432010-01-14 17:45:04 -0500907
Andy Adamsonc36fca52011-01-06 02:04:32 +0000908 if (cps->drc_status) {
909 status = cps->drc_status;
Andy Adamson49110962010-01-14 17:45:08 -0500910 goto encode_hdr;
911 }
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 maxlen = xdr_out->end - xdr_out->p;
914 if (maxlen > 0 && maxlen < PAGE_SIZE) {
Andy Adamsone95e60d2010-01-14 17:45:06 -0500915 status = op->decode_args(rqstp, xdr_in, argp);
916 if (likely(status == 0))
Andy Adamsonc36fca52011-01-06 02:04:32 +0000917 status = op->process_op(argp, resp, cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 } else
919 status = htonl(NFS4ERR_RESOURCE);
920
Andy Adamsonb92b3012010-01-14 17:45:05 -0500921encode_hdr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 res = encode_op_hdr(xdr_out, op_nr, status);
Andy Adamson31d2b432010-01-14 17:45:04 -0500923 if (unlikely(res))
924 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (op->encode_res != NULL && status == 0)
926 status = op->encode_res(rqstp, xdr_out, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return status;
928}
929
930/*
931 * Decode, process and encode a COMPOUND
932 */
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200933static __be32 nfs4_callback_compound(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400935 struct cb_compound_hdr_arg hdr_arg = { 0 };
936 struct cb_compound_hdr_res hdr_res = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct xdr_stream xdr_in, xdr_out;
Andy Adamsonc36fca52011-01-06 02:04:32 +0000938 __be32 *p, status;
939 struct cb_process_state cps = {
940 .drc_status = 0,
941 .clp = NULL,
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400942 .net = SVC_NET(rqstp),
Andy Adamsonc36fca52011-01-06 02:04:32 +0000943 };
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400944 unsigned int nops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Trond Myklebust756b9b32015-12-07 12:52:23 -0800946 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Al Viro5704fde2006-10-19 23:28:51 -0700948 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
950
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400951 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
Vaishali Thakkar4ed0d832015-06-10 20:15:29 +0530952 if (status == htonl(NFS4ERR_RESOURCE))
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400953 return rpc_garbage_args;
954
Andy Adamsonc36fca52011-01-06 02:04:32 +0000955 if (hdr_arg.minorversion == 0) {
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400956 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
Olga Kornievskaia32cd3ee2018-07-26 16:04:47 -0400957 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp)) {
958 if (cps.clp)
959 nfs_put_client(cps.clp);
Chuck Levera4e187d2016-06-29 13:55:22 -0400960 goto out_invalidcred;
Olga Kornievskaia32cd3ee2018-07-26 16:04:47 -0400961 }
Andy Adamson778be232011-01-25 15:38:01 +0000962 }
Andy Adamsonc36fca52011-01-06 02:04:32 +0000963
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400964 cps.minorversion = hdr_arg.minorversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 hdr_res.taglen = hdr_arg.taglen;
966 hdr_res.tag = hdr_arg.tag;
Olga Kornievskaia32cd3ee2018-07-26 16:04:47 -0400967 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) {
968 if (cps.clp)
969 nfs_put_client(cps.clp);
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400970 return rpc_system_err;
Olga Kornievskaia32cd3ee2018-07-26 16:04:47 -0400971 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400972 while (status == 0 && nops != hdr_arg.nops) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400973 status = process_op(nops, rqstp, &xdr_in,
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200974 rqstp->rq_argp, &xdr_out, rqstp->rq_resp,
975 &cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 nops++;
977 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400978
Andy Adamson31d2b432010-01-14 17:45:04 -0500979 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
980 * resource error in cb_compound status without returning op */
981 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
982 status = htonl(NFS4ERR_RESOURCE);
983 nops--;
984 }
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *hdr_res.status = status;
987 *hdr_res.nops = htonl(nops);
Trond Myklebust55a67392011-08-02 14:46:29 -0400988 nfs4_cb_free_slot(&cps);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000989 nfs_put_client(cps.clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return rpc_success;
Chuck Levera4e187d2016-06-29 13:55:22 -0400991
992out_invalidcred:
993 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
994 return rpc_autherr_badcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997/*
998 * Define NFS4 callback COMPOUND ops.
999 */
1000static struct callback_op callback_ops[] = {
1001 [0] = {
1002 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
1003 },
1004 [OP_CB_GETATTR] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001005 .process_op = nfs4_callback_getattr,
1006 .decode_args = decode_getattr_args,
1007 .encode_res = encode_getattr_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
1009 },
1010 [OP_CB_RECALL] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001011 .process_op = nfs4_callback_recall,
1012 .decode_args = decode_recall_args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -04001014 },
1015#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +00001016 [OP_CB_LAYOUTRECALL] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001017 .process_op = nfs4_callback_layoutrecall,
1018 .decode_args = decode_layoutrecall_args,
Fred Isamanf2a62562011-01-06 11:36:29 +00001019 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
1020 },
Marc Eshel1be56832011-05-22 19:47:09 +03001021 [OP_CB_NOTIFY_DEVICEID] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001022 .process_op = nfs4_callback_devicenotify,
1023 .decode_args = decode_devicenotify_args,
Marc Eshel1be56832011-05-22 19:47:09 +03001024 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
1025 },
Benny Halevy4aece6a2009-04-01 09:23:26 -04001026 [OP_CB_SEQUENCE] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001027 .process_op = nfs4_callback_sequence,
1028 .decode_args = decode_cb_sequence_args,
1029 .encode_res = encode_cb_sequence_res,
Benny Halevy4aece6a2009-04-01 09:23:26 -04001030 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
1031 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -05001032 [OP_CB_RECALL_ANY] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001033 .process_op = nfs4_callback_recallany,
1034 .decode_args = decode_recallany_args,
Alexandros Batsakis31f09602009-12-05 13:27:02 -05001035 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
1036 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001037 [OP_CB_RECALL_SLOT] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001038 .process_op = nfs4_callback_recallslot,
1039 .decode_args = decode_recallslot_args,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001040 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
1041 },
Jeff Laytondb783682016-09-17 18:17:36 -04001042 [OP_CB_NOTIFY_LOCK] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +02001043 .process_op = nfs4_callback_notify_lock,
1044 .decode_args = decode_notify_lock_args,
Jeff Laytondb783682016-09-17 18:17:36 -04001045 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
1046 },
Benny Halevy4aece6a2009-04-01 09:23:26 -04001047#endif /* CONFIG_NFS_V4_1 */
Olga Kornievskaia5178a122018-07-09 15:13:28 -04001048#ifdef CONFIG_NFS_V4_2
1049 [OP_CB_OFFLOAD] = {
1050 .process_op = nfs4_callback_offload,
1051 .decode_args = decode_offload_args,
1052 .res_maxsize = CB_OP_OFFLOAD_RES_MAXSZ,
1053 },
1054#endif /* CONFIG_NFS_V4_2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055};
1056
1057/*
1058 * Define NFS4 callback procedures
1059 */
Christoph Hellwig860bda22017-05-12 16:11:49 +02001060static const struct svc_procedure nfs4_callback_procedures1[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 [CB_NULL] = {
1062 .pc_func = nfs4_callback_null,
Christoph Hellwig026fec72017-05-08 19:01:48 +02001063 .pc_decode = nfs4_decode_void,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001064 .pc_encode = nfs4_encode_void,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 .pc_xdrressize = 1,
1066 },
1067 [CB_COMPOUND] = {
1068 .pc_func = nfs4_callback_compound,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001069 .pc_encode = nfs4_encode_void,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 .pc_argsize = 256,
1071 .pc_ressize = 256,
1072 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1073 }
1074};
1075
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001076static unsigned int nfs4_callback_count1[ARRAY_SIZE(nfs4_callback_procedures1)];
Christoph Hellwige9679182017-05-12 16:21:37 +02001077const struct svc_version nfs4_callback_version1 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 .vs_vers = 1,
1079 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1080 .vs_proc = nfs4_callback_procedures1,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001081 .vs_count = nfs4_callback_count1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1083 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001084 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001085 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086};
1087
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001088static unsigned int nfs4_callback_count4[ARRAY_SIZE(nfs4_callback_procedures1)];
Christoph Hellwige9679182017-05-12 16:21:37 +02001089const struct svc_version nfs4_callback_version4 = {
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001090 .vs_vers = 4,
1091 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1092 .vs_proc = nfs4_callback_procedures1,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001093 .vs_count = nfs4_callback_count4,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001094 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1095 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001096 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001097 .vs_need_cong_ctrl = true,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001098};