blob: c782261d86d5f0a757ce4161808c9daca1919b66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/callback_xdr.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFSv4 callback encode/decode procedures
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
9#include <linux/sunrpc/svc.h>
10#include <linux/nfs4.h>
11#include <linux/nfs_fs.h>
Trond Myklebust9a3ba432012-03-12 18:01:48 -040012#include <linux/ratelimit.h>
13#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Andy Adamsonc36fca52011-01-06 02:04:32 +000015#include <linux/sunrpc/bc_xprt.h>
Trond Myklebust4ce79712005-06-22 17:16:21 +000016#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "callback.h"
Andy Adamsonc36fca52011-01-06 02:04:32 +000018#include "internal.h"
Trond Myklebust76e697b2012-11-26 14:20:49 -050019#include "nfs4session.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Kinglong Mee45724e82015-09-24 20:57:58 +080021#define CB_OP_TAGLEN_MAXSZ (512)
22#define CB_OP_HDR_RES_MAXSZ (2 * 4) // opcode, status
23#define CB_OP_GETATTR_BITMAP_MAXSZ (4 * 4) // bitmap length, 3 bitmaps
24#define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
25 CB_OP_GETATTR_BITMAP_MAXSZ + \
26 /* change, size, ctime, mtime */\
27 (2 + 2 + 3 + 3) * 4)
28#define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Benny Halevy4aece6a2009-04-01 09:23:26 -040030#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +000031#define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Marc Eshel1be56832011-05-22 19:47:09 +030032#define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Benny Halevy4aece6a2009-04-01 09:23:26 -040033#define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
Kinglong Mee45724e82015-09-24 20:57:58 +080034 NFS4_MAX_SESSIONID_LEN + \
35 (1 + 3) * 4) // seqid, 3 slotids
Alexandros Batsakis31f09602009-12-05 13:27:02 -050036#define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -050037#define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Jeff Laytondb783682016-09-17 18:17:36 -040038#define CB_OP_NOTIFY_LOCK_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Benny Halevy4aece6a2009-04-01 09:23:26 -040039#endif /* CONFIG_NFS_V4_1 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define NFSDBG_FACILITY NFSDBG_CALLBACK
42
Andy Adamson31d2b432010-01-14 17:45:04 -050043/* Internal error code */
44#define NFS4ERR_RESOURCE_HDR 11050
45
Andy Adamsonc36fca52011-01-06 02:04:32 +000046typedef __be32 (*callback_process_op_t)(void *, void *,
47 struct cb_process_state *);
Al Viroe6f684f2006-10-19 23:28:50 -070048typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
49typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51
52struct callback_op {
53 callback_process_op_t process_op;
54 callback_decode_arg_t decode_args;
55 callback_encode_res_t encode_res;
56 long res_maxsize;
57};
58
59static struct callback_op callback_ops[];
60
Al Viro7111c662006-10-19 23:28:45 -070061static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 return htonl(NFS4_OK);
64}
65
Al Viro5704fde2006-10-19 23:28:51 -070066static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 return xdr_argsize_check(rqstp, p);
69}
70
Al Viro5704fde2006-10-19 23:28:51 -070071static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 return xdr_ressize_check(rqstp, p);
74}
75
Jeff Laytonb60475c2016-09-17 18:17:31 -040076static __be32 *read_buf(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Al Viro5704fde2006-10-19 23:28:51 -070078 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 p = xdr_inline_decode(xdr, nbytes);
81 if (unlikely(p == NULL))
Trond Myklebust756b9b32015-12-07 12:52:23 -080082 printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return p;
84}
85
Trond Myklebustc065eee2017-02-19 16:08:28 -050086static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len,
87 const char **str, size_t maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Trond Myklebustc065eee2017-02-19 16:08:28 -050089 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Trond Myklebustc065eee2017-02-19 16:08:28 -050091 err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen);
92 if (err < 0)
93 return cpu_to_be32(NFS4ERR_RESOURCE);
94 *len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return 0;
96}
97
Al Viroe6f684f2006-10-19 23:28:50 -070098static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Al Viro5704fde2006-10-19 23:28:51 -0700100 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 p = read_buf(xdr, 4);
103 if (unlikely(p == NULL))
104 return htonl(NFS4ERR_RESOURCE);
105 fh->size = ntohl(*p);
106 if (fh->size > NFS4_FHSIZE)
107 return htonl(NFS4ERR_BADHANDLE);
108 p = read_buf(xdr, fh->size);
109 if (unlikely(p == NULL))
110 return htonl(NFS4ERR_RESOURCE);
111 memcpy(&fh->data[0], p, fh->size);
112 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
113 return 0;
114}
115
Al Viroe6f684f2006-10-19 23:28:50 -0700116static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Al Viro5704fde2006-10-19 23:28:51 -0700118 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned int attrlen;
120
121 p = read_buf(xdr, 4);
122 if (unlikely(p == NULL))
123 return htonl(NFS4ERR_RESOURCE);
124 attrlen = ntohl(*p);
125 p = read_buf(xdr, attrlen << 2);
126 if (unlikely(p == NULL))
127 return htonl(NFS4ERR_RESOURCE);
128 if (likely(attrlen > 0))
129 bitmap[0] = ntohl(*p++);
130 if (attrlen > 1)
131 bitmap[1] = ntohl(*p);
132 return 0;
133}
134
Al Viroe6f684f2006-10-19 23:28:50 -0700135static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Al Viro5704fde2006-10-19 23:28:51 -0700137 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500139 p = read_buf(xdr, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (unlikely(p == NULL))
141 return htonl(NFS4ERR_RESOURCE);
Trond Myklebust93b717f2016-05-16 17:42:43 -0400142 memcpy(stateid->data, p, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
144}
145
Trond Myklebust93b717f2016-05-16 17:42:43 -0400146static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
147{
148 stateid->type = NFS4_DELEGATION_STATEID_TYPE;
149 return decode_stateid(xdr, stateid);
150}
151
Al Viroe6f684f2006-10-19 23:28:50 -0700152static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Al Viro5704fde2006-10-19 23:28:51 -0700154 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700155 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Trond Myklebustc065eee2017-02-19 16:08:28 -0500157 status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (unlikely(status != 0))
159 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 p = read_buf(xdr, 12);
161 if (unlikely(p == NULL))
162 return htonl(NFS4ERR_RESOURCE);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400163 hdr->minorversion = ntohl(*p++);
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400164 /* Check for minor version support */
165 if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
Steve Dickson42c2c422013-05-22 12:50:38 -0400166 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
Benny Halevy48a9e2d2009-04-01 09:23:20 -0400167 } else {
Trond Myklebust9a3ba432012-03-12 18:01:48 -0400168 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400169 "illegal minor version %u!\n",
170 __func__, hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 hdr->nops = ntohl(*p);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400174 dprintk("%s: minorversion %d nops %d\n", __func__,
175 hdr->minorversion, hdr->nops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
Al Viroe6f684f2006-10-19 23:28:50 -0700179static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Al Viro5704fde2006-10-19 23:28:51 -0700181 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 p = read_buf(xdr, 4);
183 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500184 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *op = ntohl(*p);
186 return 0;
187}
188
Al Viroe6f684f2006-10-19 23:28:50 -0700189static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Al Viroe6f684f2006-10-19 23:28:50 -0700191 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 status = decode_fh(xdr, &args->fh);
194 if (unlikely(status != 0))
Anna Schumaker56938bb2017-04-07 14:14:58 -0400195 return status;
196 return decode_bitmap(xdr, args->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Al Viroe6f684f2006-10-19 23:28:50 -0700199static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Al Viro5704fde2006-10-19 23:28:51 -0700201 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700202 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Trond Myklebust93b717f2016-05-16 17:42:43 -0400204 status = decode_delegation_stateid(xdr, &args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (unlikely(status != 0))
206 goto out;
207 p = read_buf(xdr, 4);
208 if (unlikely(p == NULL)) {
209 status = htonl(NFS4ERR_RESOURCE);
210 goto out;
211 }
212 args->truncate = ntohl(*p);
213 status = decode_fh(xdr, &args->fh);
214out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700215 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Alexey Dobriyan3873bc52006-05-27 03:31:12 +0400216 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Benny Halevy4aece6a2009-04-01 09:23:26 -0400219#if defined(CONFIG_NFS_V4_1)
Trond Myklebust93b717f2016-05-16 17:42:43 -0400220static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
221{
222 stateid->type = NFS4_LAYOUT_STATEID_TYPE;
223 return decode_stateid(xdr, stateid);
224}
Benny Halevy4aece6a2009-04-01 09:23:26 -0400225
Fred Isamanf2a62562011-01-06 11:36:29 +0000226static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
227 struct xdr_stream *xdr,
228 struct cb_layoutrecallargs *args)
229{
230 __be32 *p;
231 __be32 status = 0;
232 uint32_t iomode;
233
Fred Isamanf2a62562011-01-06 11:36:29 +0000234 p = read_buf(xdr, 4 * sizeof(uint32_t));
235 if (unlikely(p == NULL)) {
236 status = htonl(NFS4ERR_BADXDR);
237 goto out;
238 }
239
240 args->cbl_layout_type = ntohl(*p++);
241 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300242 * as it is unusable and ignored with the other types.
Fred Isamanf2a62562011-01-06 11:36:29 +0000243 */
244 iomode = ntohl(*p++);
245 args->cbl_layoutchanged = ntohl(*p++);
246 args->cbl_recall_type = ntohl(*p++);
247
248 if (args->cbl_recall_type == RETURN_FILE) {
249 args->cbl_range.iomode = iomode;
250 status = decode_fh(xdr, &args->cbl_fh);
251 if (unlikely(status != 0))
252 goto out;
253
254 p = read_buf(xdr, 2 * sizeof(uint64_t));
255 if (unlikely(p == NULL)) {
256 status = htonl(NFS4ERR_BADXDR);
257 goto out;
258 }
259 p = xdr_decode_hyper(p, &args->cbl_range.offset);
260 p = xdr_decode_hyper(p, &args->cbl_range.length);
Trond Myklebust93b717f2016-05-16 17:42:43 -0400261 status = decode_layout_stateid(xdr, &args->cbl_stateid);
Fred Isamanf2a62562011-01-06 11:36:29 +0000262 if (unlikely(status != 0))
263 goto out;
264 } else if (args->cbl_recall_type == RETURN_FSID) {
265 p = read_buf(xdr, 2 * sizeof(uint64_t));
266 if (unlikely(p == NULL)) {
267 status = htonl(NFS4ERR_BADXDR);
268 goto out;
269 }
270 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
271 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
272 } else if (args->cbl_recall_type != RETURN_ALL) {
273 status = htonl(NFS4ERR_BADXDR);
274 goto out;
275 }
276 dprintk("%s: ltype 0x%x iomode %d changed %d recall_type %d\n",
277 __func__,
278 args->cbl_layout_type, iomode,
279 args->cbl_layoutchanged, args->cbl_recall_type);
280out:
281 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
282 return status;
283}
284
Marc Eshel1be56832011-05-22 19:47:09 +0300285static
286__be32 decode_devicenotify_args(struct svc_rqst *rqstp,
287 struct xdr_stream *xdr,
288 struct cb_devicenotifyargs *args)
289{
290 __be32 *p;
291 __be32 status = 0;
292 u32 tmp;
293 int n, i;
294 args->ndevs = 0;
295
296 /* Num of device notifications */
297 p = read_buf(xdr, sizeof(uint32_t));
298 if (unlikely(p == NULL)) {
299 status = htonl(NFS4ERR_BADXDR);
300 goto out;
301 }
302 n = ntohl(*p++);
303 if (n <= 0)
304 goto out;
Dan Carpenter363e0df02012-01-12 10:16:14 +0300305 if (n > ULONG_MAX / sizeof(*args->devs)) {
306 status = htonl(NFS4ERR_BADXDR);
307 goto out;
308 }
Marc Eshel1be56832011-05-22 19:47:09 +0300309
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500310 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
Marc Eshel1be56832011-05-22 19:47:09 +0300311 if (!args->devs) {
312 status = htonl(NFS4ERR_DELAY);
313 goto out;
314 }
315
316 /* Decode each dev notification */
317 for (i = 0; i < n; i++) {
318 struct cb_devicenotifyitem *dev = &args->devs[i];
319
320 p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
321 if (unlikely(p == NULL)) {
322 status = htonl(NFS4ERR_BADXDR);
323 goto err;
324 }
325
326 tmp = ntohl(*p++); /* bitmap size */
327 if (tmp != 1) {
328 status = htonl(NFS4ERR_INVAL);
329 goto err;
330 }
331 dev->cbd_notify_type = ntohl(*p++);
332 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
333 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
334 status = htonl(NFS4ERR_INVAL);
335 goto err;
336 }
337
338 tmp = ntohl(*p++); /* opaque size */
339 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
340 (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
341 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
342 (tmp != NFS4_DEVICEID4_SIZE + 4))) {
343 status = htonl(NFS4ERR_INVAL);
344 goto err;
345 }
346 dev->cbd_layout_type = ntohl(*p++);
347 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
348 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
349
350 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
351 p = read_buf(xdr, sizeof(uint32_t));
352 if (unlikely(p == NULL)) {
353 status = htonl(NFS4ERR_BADXDR);
354 goto err;
355 }
356 dev->cbd_immediate = ntohl(*p++);
357 } else {
358 dev->cbd_immediate = 0;
359 }
360
361 args->ndevs++;
362
363 dprintk("%s: type %d layout 0x%x immediate %d\n",
364 __func__, dev->cbd_notify_type, dev->cbd_layout_type,
365 dev->cbd_immediate);
366 }
367out:
368 dprintk("%s: status %d ndevs %d\n",
369 __func__, ntohl(status), args->ndevs);
370 return status;
371err:
372 kfree(args->devs);
373 goto out;
374}
375
Andy Adamson9733f0d2010-01-22 12:03:08 -0500376static __be32 decode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400377 struct nfs4_sessionid *sid)
378{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500379 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400380
Kinglong Mee590184a2015-09-24 20:57:37 +0800381 p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400382 if (unlikely(p == NULL))
Joe Perchesa419aef2009-08-18 11:18:35 -0700383 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400384
Kinglong Mee590184a2015-09-24 20:57:37 +0800385 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400386 return 0;
387}
388
Andy Adamson9733f0d2010-01-22 12:03:08 -0500389static __be32 decode_rc_list(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400390 struct referring_call_list *rc_list)
391{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500392 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400393 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500394 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400395
396 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
397 if (status)
398 goto out;
399
400 status = htonl(NFS4ERR_RESOURCE);
401 p = read_buf(xdr, sizeof(uint32_t));
402 if (unlikely(p == NULL))
403 goto out;
404
405 rc_list->rcl_nrefcalls = ntohl(*p++);
406 if (rc_list->rcl_nrefcalls) {
407 p = read_buf(xdr,
408 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
409 if (unlikely(p == NULL))
410 goto out;
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500411 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400412 sizeof(*rc_list->rcl_refcalls),
413 GFP_KERNEL);
414 if (unlikely(rc_list->rcl_refcalls == NULL))
415 goto out;
416 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
417 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
418 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
419 }
420 }
421 status = 0;
422
423out:
424 return status;
425}
426
Andy Adamson9733f0d2010-01-22 12:03:08 -0500427static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400428 struct xdr_stream *xdr,
429 struct cb_sequenceargs *args)
430{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500431 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400432 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500433 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400434
435 status = decode_sessionid(xdr, &args->csa_sessionid);
436 if (status)
437 goto out;
438
439 status = htonl(NFS4ERR_RESOURCE);
440 p = read_buf(xdr, 5 * sizeof(uint32_t));
441 if (unlikely(p == NULL))
442 goto out;
443
Ricardo Labiaga65fc64e2009-04-01 09:23:30 -0400444 args->csa_addr = svc_addr(rqstp);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400445 args->csa_sequenceid = ntohl(*p++);
446 args->csa_slotid = ntohl(*p++);
447 args->csa_highestslotid = ntohl(*p++);
448 args->csa_cachethis = ntohl(*p++);
449 args->csa_nrclists = ntohl(*p++);
450 args->csa_rclists = NULL;
451 if (args->csa_nrclists) {
Dan Carpenter0439f312012-06-12 10:37:08 +0300452 args->csa_rclists = kmalloc_array(args->csa_nrclists,
453 sizeof(*args->csa_rclists),
454 GFP_KERNEL);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400455 if (unlikely(args->csa_rclists == NULL))
456 goto out;
457
458 for (i = 0; i < args->csa_nrclists; i++) {
459 status = decode_rc_list(xdr, &args->csa_rclists[i]);
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500460 if (status) {
461 args->csa_nrclists = i;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400462 goto out_free;
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500463 }
Benny Halevy4aece6a2009-04-01 09:23:26 -0400464 }
465 }
466 status = 0;
467
468 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
469 "highestslotid %u cachethis %d nrclists %u\n",
470 __func__,
471 ((u32 *)&args->csa_sessionid)[0],
472 ((u32 *)&args->csa_sessionid)[1],
473 ((u32 *)&args->csa_sessionid)[2],
474 ((u32 *)&args->csa_sessionid)[3],
475 args->csa_sequenceid, args->csa_slotid,
476 args->csa_highestslotid, args->csa_cachethis,
477 args->csa_nrclists);
478out:
479 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
480 return status;
481
482out_free:
483 for (i = 0; i < args->csa_nrclists; i++)
484 kfree(args->csa_rclists[i].rcl_refcalls);
485 kfree(args->csa_rclists);
486 goto out;
487}
488
Andy Adamson9733f0d2010-01-22 12:03:08 -0500489static __be32 decode_recallany_args(struct svc_rqst *rqstp,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500490 struct xdr_stream *xdr,
491 struct cb_recallanyargs *args)
492{
Peng Taod743c3c2011-10-23 20:22:38 -0700493 uint32_t bitmap[2];
494 __be32 *p, status;
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500495
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500496 p = read_buf(xdr, 4);
497 if (unlikely(p == NULL))
498 return htonl(NFS4ERR_BADXDR);
499 args->craa_objs_to_keep = ntohl(*p++);
Peng Taod743c3c2011-10-23 20:22:38 -0700500 status = decode_bitmap(xdr, bitmap);
501 if (unlikely(status))
502 return status;
503 args->craa_type_mask = bitmap[0];
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500504
505 return 0;
506}
507
Andy Adamson9733f0d2010-01-22 12:03:08 -0500508static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500509 struct xdr_stream *xdr,
510 struct cb_recallslotargs *args)
511{
512 __be32 *p;
513
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500514 p = read_buf(xdr, 4);
515 if (unlikely(p == NULL))
516 return htonl(NFS4ERR_BADXDR);
Trond Myklebustd5fb4ce2012-11-20 20:24:02 -0500517 args->crsa_target_highest_slotid = ntohl(*p++);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500518 return 0;
519}
520
Jeff Laytondb783682016-09-17 18:17:36 -0400521static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
522{
523 __be32 *p;
524 unsigned int len;
525
526 p = read_buf(xdr, 12);
527 if (unlikely(p == NULL))
528 return htonl(NFS4ERR_BADXDR);
529
530 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
531 len = be32_to_cpu(*p);
532
533 p = read_buf(xdr, len);
534 if (unlikely(p == NULL))
535 return htonl(NFS4ERR_BADXDR);
536
537 /* Only try to decode if the length is right */
538 if (len == 20) {
539 p += 2; /* skip "lock id:" */
540 args->cbnl_owner.s_dev = be32_to_cpu(*p++);
541 xdr_decode_hyper(p, &args->cbnl_owner.id);
542 args->cbnl_valid = true;
543 } else {
544 args->cbnl_owner.s_dev = 0;
545 args->cbnl_owner.id = 0;
546 args->cbnl_valid = false;
547 }
548 return 0;
549}
550
551static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_notify_lock_args *args)
552{
553 __be32 status;
554
555 status = decode_fh(xdr, &args->cbnl_fh);
556 if (unlikely(status != 0))
557 goto out;
558 status = decode_lockowner(xdr, args);
559out:
560 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
561 return status;
562}
563
Benny Halevy4aece6a2009-04-01 09:23:26 -0400564#endif /* CONFIG_NFS_V4_1 */
565
Al Viroe6f684f2006-10-19 23:28:50 -0700566static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Trond Myklebustab6e9aa2017-02-19 16:08:26 -0500568 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
569 return cpu_to_be32(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571}
572
573#define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
574#define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
Al Viro5704fde2006-10-19 23:28:51 -0700575static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Al Viro5704fde2006-10-19 23:28:51 -0700577 __be32 bm[2];
578 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
581 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
582 if (bm[1] != 0) {
583 p = xdr_reserve_space(xdr, 16);
584 if (unlikely(p == NULL))
585 return htonl(NFS4ERR_RESOURCE);
586 *p++ = htonl(2);
587 *p++ = bm[0];
588 *p++ = bm[1];
589 } else if (bm[0] != 0) {
590 p = xdr_reserve_space(xdr, 12);
591 if (unlikely(p == NULL))
592 return htonl(NFS4ERR_RESOURCE);
593 *p++ = htonl(1);
594 *p++ = bm[0];
595 } else {
596 p = xdr_reserve_space(xdr, 8);
597 if (unlikely(p == NULL))
598 return htonl(NFS4ERR_RESOURCE);
599 *p++ = htonl(0);
600 }
601 *savep = p;
602 return 0;
603}
604
Al Viroe6f684f2006-10-19 23:28:50 -0700605static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Al Viro5704fde2006-10-19 23:28:51 -0700607 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
610 return 0;
611 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800612 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return htonl(NFS4ERR_RESOURCE);
614 p = xdr_encode_hyper(p, change);
615 return 0;
616}
617
Al Viroe6f684f2006-10-19 23:28:50 -0700618static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Al Viro5704fde2006-10-19 23:28:51 -0700620 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
623 return 0;
624 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800625 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return htonl(NFS4ERR_RESOURCE);
627 p = xdr_encode_hyper(p, size);
628 return 0;
629}
630
Al Viroe6f684f2006-10-19 23:28:50 -0700631static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Al Viro5704fde2006-10-19 23:28:51 -0700633 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 p = xdr_reserve_space(xdr, 12);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800636 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return htonl(NFS4ERR_RESOURCE);
638 p = xdr_encode_hyper(p, time->tv_sec);
639 *p = htonl(time->tv_nsec);
640 return 0;
641}
642
Al Viroe6f684f2006-10-19 23:28:50 -0700643static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
646 return 0;
647 return encode_attr_time(xdr,time);
648}
649
Al Viroe6f684f2006-10-19 23:28:50 -0700650static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
653 return 0;
654 return encode_attr_time(xdr,time);
655}
656
Al Viroe6f684f2006-10-19 23:28:50 -0700657static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Al Viroe6f684f2006-10-19 23:28:50 -0700659 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 hdr->status = xdr_reserve_space(xdr, 4);
662 if (unlikely(hdr->status == NULL))
663 return htonl(NFS4ERR_RESOURCE);
664 status = encode_string(xdr, hdr->taglen, hdr->tag);
665 if (unlikely(status != 0))
666 return status;
667 hdr->nops = xdr_reserve_space(xdr, 4);
668 if (unlikely(hdr->nops == NULL))
669 return htonl(NFS4ERR_RESOURCE);
670 return 0;
671}
672
Al Viroe6f684f2006-10-19 23:28:50 -0700673static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Al Viro5704fde2006-10-19 23:28:51 -0700675 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 p = xdr_reserve_space(xdr, 8);
678 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500679 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 *p++ = htonl(op);
681 *p = res;
682 return 0;
683}
684
Al Viroe6f684f2006-10-19 23:28:50 -0700685static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Al Viro5704fde2006-10-19 23:28:51 -0700687 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700688 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (unlikely(status != 0))
691 goto out;
692 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
693 if (unlikely(status != 0))
694 goto out;
695 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
696 if (unlikely(status != 0))
697 goto out;
698 status = encode_attr_size(xdr, res->bitmap, res->size);
699 if (unlikely(status != 0))
700 goto out;
701 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
702 if (unlikely(status != 0))
703 goto out;
704 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
705 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
706out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700707 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return status;
709}
710
Benny Halevy34bc47c92009-04-01 09:23:22 -0400711#if defined(CONFIG_NFS_V4_1)
712
Andy Adamson9733f0d2010-01-22 12:03:08 -0500713static __be32 encode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400714 const struct nfs4_sessionid *sid)
715{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500716 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400717
Kinglong Mee590184a2015-09-24 20:57:37 +0800718 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400719 if (unlikely(p == NULL))
720 return htonl(NFS4ERR_RESOURCE);
721
Kinglong Mee590184a2015-09-24 20:57:37 +0800722 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400723 return 0;
724}
725
Andy Adamson9733f0d2010-01-22 12:03:08 -0500726static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400727 struct xdr_stream *xdr,
728 const struct cb_sequenceres *res)
729{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500730 __be32 *p;
Dan Carpentere216c8c2012-06-12 10:37:39 +0300731 __be32 status = res->csr_status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400732
733 if (unlikely(status != 0))
734 goto out;
735
Kinglong Meee0a63c02015-09-24 20:58:38 +0800736 status = encode_sessionid(xdr, &res->csr_sessionid);
737 if (status)
738 goto out;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400739
740 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
741 if (unlikely(p == NULL))
742 return htonl(NFS4ERR_RESOURCE);
743
744 *p++ = htonl(res->csr_sequenceid);
745 *p++ = htonl(res->csr_slotid);
746 *p++ = htonl(res->csr_highestslotid);
747 *p++ = htonl(res->csr_target_highestslotid);
748out:
749 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
750 return status;
751}
752
Benny Halevy34bc47c92009-04-01 09:23:22 -0400753static __be32
754preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
755{
Benny Halevy281fe152009-04-01 09:23:27 -0400756 if (op_nr == OP_CB_SEQUENCE) {
757 if (nop != 0)
758 return htonl(NFS4ERR_SEQUENCE_POS);
759 } else {
760 if (nop == 0)
761 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
762 }
763
Benny Halevy34bc47c92009-04-01 09:23:22 -0400764 switch (op_nr) {
765 case OP_CB_GETATTR:
766 case OP_CB_RECALL:
Benny Halevy4aece6a2009-04-01 09:23:26 -0400767 case OP_CB_SEQUENCE:
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500768 case OP_CB_RECALL_ANY:
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500769 case OP_CB_RECALL_SLOT:
Fred Isamanf2a62562011-01-06 11:36:29 +0000770 case OP_CB_LAYOUTRECALL:
Marc Eshel1be56832011-05-22 19:47:09 +0300771 case OP_CB_NOTIFY_DEVICEID:
Jeff Laytondb783682016-09-17 18:17:36 -0400772 case OP_CB_NOTIFY_LOCK:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400773 *op = &callback_ops[op_nr];
774 break;
775
Benny Halevy34bc47c92009-04-01 09:23:22 -0400776 case OP_CB_NOTIFY:
777 case OP_CB_PUSH_DELEG:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400778 case OP_CB_RECALLABLE_OBJ_AVAIL:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400779 case OP_CB_WANTS_CANCELLED:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400780 return htonl(NFS4ERR_NOTSUPP);
781
782 default:
783 return htonl(NFS4ERR_OP_ILLEGAL);
784 }
785
786 return htonl(NFS_OK);
787}
788
Trond Myklebust810d82e2016-01-23 15:18:18 -0500789static void nfs4_callback_free_slot(struct nfs4_session *session,
790 struct nfs4_slot *slot)
Andy Adamson42acd022011-01-06 02:04:34 +0000791{
792 struct nfs4_slot_table *tbl = &session->bc_slot_table;
793
794 spin_lock(&tbl->slot_tbl_lock);
795 /*
796 * Let the state manager know callback processing done.
797 * A single slot, so highest used slotid is either 0 or -1
798 */
Trond Myklebust810d82e2016-01-23 15:18:18 -0500799 nfs4_free_slot(tbl, slot);
Andy Adamson774d5f12013-05-20 14:13:50 -0400800 nfs4_slot_tbl_drain_complete(tbl);
Andy Adamson42acd022011-01-06 02:04:34 +0000801 spin_unlock(&tbl->slot_tbl_lock);
802}
803
Trond Myklebust55a67392011-08-02 14:46:29 -0400804static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000805{
Trond Myklebust810d82e2016-01-23 15:18:18 -0500806 if (cps->slot) {
807 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
808 cps->slot = NULL;
809 }
Andy Adamson42acd022011-01-06 02:04:34 +0000810}
811
Benny Halevy34bc47c92009-04-01 09:23:22 -0400812#else /* CONFIG_NFS_V4_1 */
813
814static __be32
815preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
816{
817 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
818}
819
Trond Myklebust55a67392011-08-02 14:46:29 -0400820static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000821{
822}
Benny Halevy34bc47c92009-04-01 09:23:22 -0400823#endif /* CONFIG_NFS_V4_1 */
824
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400825#ifdef CONFIG_NFS_V4_2
826static __be32
827preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
828{
829 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
830 if (status != htonl(NFS4ERR_OP_ILLEGAL))
831 return status;
832
833 if (op_nr == OP_CB_OFFLOAD)
834 return htonl(NFS4ERR_NOTSUPP);
835 return htonl(NFS4ERR_OP_ILLEGAL);
836}
837#else /* CONFIG_NFS_V4_2 */
838static __be32
839preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
840{
841 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
842}
843#endif /* CONFIG_NFS_V4_2 */
844
Benny Halevy34bc47c92009-04-01 09:23:22 -0400845static __be32
846preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
847{
848 switch (op_nr) {
849 case OP_CB_GETATTR:
850 case OP_CB_RECALL:
851 *op = &callback_ops[op_nr];
852 break;
853 default:
854 return htonl(NFS4ERR_OP_ILLEGAL);
855 }
856
857 return htonl(NFS_OK);
858}
859
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400860static __be32 process_op(int nop, struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct xdr_stream *xdr_in, void *argp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000862 struct xdr_stream *xdr_out, void *resp,
863 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500865 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500866 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400867 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700869 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Harvey Harrison3110ff82008-05-02 13:42:44 -0700871 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500873 if (unlikely(status))
874 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Benny Halevy34bc47c92009-04-01 09:23:22 -0400876 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400877 __func__, cps->minorversion, nop, op_nr);
Benny Halevy34bc47c92009-04-01 09:23:22 -0400878
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400879 switch (cps->minorversion) {
880 case 0:
881 status = preprocess_nfs4_op(op_nr, &op);
882 break;
883 case 1:
884 status = preprocess_nfs41_op(nop, op_nr, &op);
885 break;
886 case 2:
887 status = preprocess_nfs42_op(nop, op_nr, &op);
888 break;
889 default:
890 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
891 }
892
Benny Halevy34bc47c92009-04-01 09:23:22 -0400893 if (status == htonl(NFS4ERR_OP_ILLEGAL))
894 op_nr = OP_CB_ILLEGAL;
Andy Adamsonb92b3012010-01-14 17:45:05 -0500895 if (status)
896 goto encode_hdr;
Andy Adamson31d2b432010-01-14 17:45:04 -0500897
Andy Adamsonc36fca52011-01-06 02:04:32 +0000898 if (cps->drc_status) {
899 status = cps->drc_status;
Andy Adamson49110962010-01-14 17:45:08 -0500900 goto encode_hdr;
901 }
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 maxlen = xdr_out->end - xdr_out->p;
904 if (maxlen > 0 && maxlen < PAGE_SIZE) {
Andy Adamsone95e60d2010-01-14 17:45:06 -0500905 status = op->decode_args(rqstp, xdr_in, argp);
906 if (likely(status == 0))
Andy Adamsonc36fca52011-01-06 02:04:32 +0000907 status = op->process_op(argp, resp, cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 } else
909 status = htonl(NFS4ERR_RESOURCE);
910
Andy Adamsonb92b3012010-01-14 17:45:05 -0500911encode_hdr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 res = encode_op_hdr(xdr_out, op_nr, status);
Andy Adamson31d2b432010-01-14 17:45:04 -0500913 if (unlikely(res))
914 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (op->encode_res != NULL && status == 0)
916 status = op->encode_res(rqstp, xdr_out, resp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700917 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return status;
919}
920
921/*
922 * Decode, process and encode a COMPOUND
923 */
Al Viro7111c662006-10-19 23:28:45 -0700924static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
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
Harvey Harrison3110ff82008-05-02 13:42:44 -0700937 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Trond Myklebust756b9b32015-12-07 12:52:23 -0800939 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Al Viro5704fde2006-10-19 23:28:51 -0700941 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
943
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400944 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
Vaishali Thakkar4ed0d832015-06-10 20:15:29 +0530945 if (status == htonl(NFS4ERR_RESOURCE))
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400946 return rpc_garbage_args;
947
Andy Adamsonc36fca52011-01-06 02:04:32 +0000948 if (hdr_arg.minorversion == 0) {
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400949 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
Andy Adamson778be232011-01-25 15:38:01 +0000950 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
Chuck Levera4e187d2016-06-29 13:55:22 -0400951 goto out_invalidcred;
Andy Adamson778be232011-01-25 15:38:01 +0000952 }
Andy Adamsonc36fca52011-01-06 02:04:32 +0000953
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400954 cps.minorversion = hdr_arg.minorversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 hdr_res.taglen = hdr_arg.taglen;
956 hdr_res.tag = hdr_arg.tag;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400957 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
958 return rpc_system_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400960 while (status == 0 && nops != hdr_arg.nops) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400961 status = process_op(nops, rqstp, &xdr_in,
962 argp, &xdr_out, resp, &cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 nops++;
964 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400965
Andy Adamson31d2b432010-01-14 17:45:04 -0500966 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
967 * resource error in cb_compound status without returning op */
968 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
969 status = htonl(NFS4ERR_RESOURCE);
970 nops--;
971 }
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *hdr_res.status = status;
974 *hdr_res.nops = htonl(nops);
Trond Myklebust55a67392011-08-02 14:46:29 -0400975 nfs4_cb_free_slot(&cps);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000976 nfs_put_client(cps.clp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700977 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return rpc_success;
Chuck Levera4e187d2016-06-29 13:55:22 -0400979
980out_invalidcred:
981 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
982 return rpc_autherr_badcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985/*
986 * Define NFS4 callback COMPOUND ops.
987 */
988static struct callback_op callback_ops[] = {
989 [0] = {
990 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
991 },
992 [OP_CB_GETATTR] = {
993 .process_op = (callback_process_op_t)nfs4_callback_getattr,
994 .decode_args = (callback_decode_arg_t)decode_getattr_args,
995 .encode_res = (callback_encode_res_t)encode_getattr_res,
996 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
997 },
998 [OP_CB_RECALL] = {
999 .process_op = (callback_process_op_t)nfs4_callback_recall,
1000 .decode_args = (callback_decode_arg_t)decode_recall_args,
1001 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -04001002 },
1003#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +00001004 [OP_CB_LAYOUTRECALL] = {
1005 .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
1006 .decode_args =
1007 (callback_decode_arg_t)decode_layoutrecall_args,
1008 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
1009 },
Marc Eshel1be56832011-05-22 19:47:09 +03001010 [OP_CB_NOTIFY_DEVICEID] = {
1011 .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
1012 .decode_args =
1013 (callback_decode_arg_t)decode_devicenotify_args,
1014 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
1015 },
Benny Halevy4aece6a2009-04-01 09:23:26 -04001016 [OP_CB_SEQUENCE] = {
1017 .process_op = (callback_process_op_t)nfs4_callback_sequence,
1018 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
1019 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
1020 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
1021 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -05001022 [OP_CB_RECALL_ANY] = {
1023 .process_op = (callback_process_op_t)nfs4_callback_recallany,
1024 .decode_args = (callback_decode_arg_t)decode_recallany_args,
1025 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
1026 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001027 [OP_CB_RECALL_SLOT] = {
1028 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
1029 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
1030 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
1031 },
Jeff Laytondb783682016-09-17 18:17:36 -04001032 [OP_CB_NOTIFY_LOCK] = {
1033 .process_op = (callback_process_op_t)nfs4_callback_notify_lock,
1034 .decode_args = (callback_decode_arg_t)decode_notify_lock_args,
1035 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
1036 },
Benny Halevy4aece6a2009-04-01 09:23:26 -04001037#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038};
1039
1040/*
1041 * Define NFS4 callback procedures
1042 */
1043static struct svc_procedure nfs4_callback_procedures1[] = {
1044 [CB_NULL] = {
1045 .pc_func = nfs4_callback_null,
1046 .pc_decode = (kxdrproc_t)nfs4_decode_void,
1047 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1048 .pc_xdrressize = 1,
1049 },
1050 [CB_COMPOUND] = {
1051 .pc_func = nfs4_callback_compound,
1052 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1053 .pc_argsize = 256,
1054 .pc_ressize = 256,
1055 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1056 }
1057};
1058
1059struct svc_version nfs4_callback_version1 = {
1060 .vs_vers = 1,
1061 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1062 .vs_proc = nfs4_callback_procedures1,
1063 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1064 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001065 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001066 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067};
1068
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001069struct svc_version nfs4_callback_version4 = {
1070 .vs_vers = 4,
1071 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1072 .vs_proc = nfs4_callback_procedures1,
1073 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1074 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001075 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001076 .vs_need_cong_ctrl = true,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001077};