blob: ce1e293b8a1840cd6e02daf118895046af7e74cb [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))
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400206 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 p = read_buf(xdr, 4);
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400208 if (unlikely(p == NULL))
209 return htonl(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 args->truncate = ntohl(*p);
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400211 return decode_fh(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Benny Halevy4aece6a2009-04-01 09:23:26 -0400214#if defined(CONFIG_NFS_V4_1)
Trond Myklebust93b717f2016-05-16 17:42:43 -0400215static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
216{
217 stateid->type = NFS4_LAYOUT_STATEID_TYPE;
218 return decode_stateid(xdr, stateid);
219}
Benny Halevy4aece6a2009-04-01 09:23:26 -0400220
Fred Isamanf2a62562011-01-06 11:36:29 +0000221static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
222 struct xdr_stream *xdr,
223 struct cb_layoutrecallargs *args)
224{
225 __be32 *p;
226 __be32 status = 0;
227 uint32_t iomode;
228
Fred Isamanf2a62562011-01-06 11:36:29 +0000229 p = read_buf(xdr, 4 * sizeof(uint32_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400230 if (unlikely(p == NULL))
231 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000232
233 args->cbl_layout_type = ntohl(*p++);
234 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300235 * as it is unusable and ignored with the other types.
Fred Isamanf2a62562011-01-06 11:36:29 +0000236 */
237 iomode = ntohl(*p++);
238 args->cbl_layoutchanged = ntohl(*p++);
239 args->cbl_recall_type = ntohl(*p++);
240
241 if (args->cbl_recall_type == RETURN_FILE) {
242 args->cbl_range.iomode = iomode;
243 status = decode_fh(xdr, &args->cbl_fh);
244 if (unlikely(status != 0))
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400245 return status;
Fred Isamanf2a62562011-01-06 11:36:29 +0000246
247 p = read_buf(xdr, 2 * sizeof(uint64_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400248 if (unlikely(p == NULL))
249 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000250 p = xdr_decode_hyper(p, &args->cbl_range.offset);
251 p = xdr_decode_hyper(p, &args->cbl_range.length);
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400252 return decode_layout_stateid(xdr, &args->cbl_stateid);
Fred Isamanf2a62562011-01-06 11:36:29 +0000253 } else if (args->cbl_recall_type == RETURN_FSID) {
254 p = read_buf(xdr, 2 * sizeof(uint64_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400255 if (unlikely(p == NULL))
256 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000257 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
258 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400259 } else if (args->cbl_recall_type != RETURN_ALL)
260 return htonl(NFS4ERR_BADXDR);
261 return 0;
Fred Isamanf2a62562011-01-06 11:36:29 +0000262}
263
Marc Eshel1be56832011-05-22 19:47:09 +0300264static
265__be32 decode_devicenotify_args(struct svc_rqst *rqstp,
266 struct xdr_stream *xdr,
267 struct cb_devicenotifyargs *args)
268{
269 __be32 *p;
270 __be32 status = 0;
271 u32 tmp;
272 int n, i;
273 args->ndevs = 0;
274
275 /* Num of device notifications */
276 p = read_buf(xdr, sizeof(uint32_t));
277 if (unlikely(p == NULL)) {
278 status = htonl(NFS4ERR_BADXDR);
279 goto out;
280 }
281 n = ntohl(*p++);
282 if (n <= 0)
283 goto out;
Dan Carpenter363e0df02012-01-12 10:16:14 +0300284 if (n > ULONG_MAX / sizeof(*args->devs)) {
285 status = htonl(NFS4ERR_BADXDR);
286 goto out;
287 }
Marc Eshel1be56832011-05-22 19:47:09 +0300288
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500289 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
Marc Eshel1be56832011-05-22 19:47:09 +0300290 if (!args->devs) {
291 status = htonl(NFS4ERR_DELAY);
292 goto out;
293 }
294
295 /* Decode each dev notification */
296 for (i = 0; i < n; i++) {
297 struct cb_devicenotifyitem *dev = &args->devs[i];
298
299 p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
300 if (unlikely(p == NULL)) {
301 status = htonl(NFS4ERR_BADXDR);
302 goto err;
303 }
304
305 tmp = ntohl(*p++); /* bitmap size */
306 if (tmp != 1) {
307 status = htonl(NFS4ERR_INVAL);
308 goto err;
309 }
310 dev->cbd_notify_type = ntohl(*p++);
311 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
312 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
313 status = htonl(NFS4ERR_INVAL);
314 goto err;
315 }
316
317 tmp = ntohl(*p++); /* opaque size */
318 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
319 (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
320 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
321 (tmp != NFS4_DEVICEID4_SIZE + 4))) {
322 status = htonl(NFS4ERR_INVAL);
323 goto err;
324 }
325 dev->cbd_layout_type = ntohl(*p++);
326 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
327 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
328
329 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
330 p = read_buf(xdr, sizeof(uint32_t));
331 if (unlikely(p == NULL)) {
332 status = htonl(NFS4ERR_BADXDR);
333 goto err;
334 }
335 dev->cbd_immediate = ntohl(*p++);
336 } else {
337 dev->cbd_immediate = 0;
338 }
339
340 args->ndevs++;
341
342 dprintk("%s: type %d layout 0x%x immediate %d\n",
343 __func__, dev->cbd_notify_type, dev->cbd_layout_type,
344 dev->cbd_immediate);
345 }
346out:
347 dprintk("%s: status %d ndevs %d\n",
348 __func__, ntohl(status), args->ndevs);
349 return status;
350err:
351 kfree(args->devs);
352 goto out;
353}
354
Andy Adamson9733f0d2010-01-22 12:03:08 -0500355static __be32 decode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400356 struct nfs4_sessionid *sid)
357{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500358 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400359
Kinglong Mee590184a2015-09-24 20:57:37 +0800360 p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400361 if (unlikely(p == NULL))
Joe Perchesa419aef2009-08-18 11:18:35 -0700362 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400363
Kinglong Mee590184a2015-09-24 20:57:37 +0800364 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400365 return 0;
366}
367
Andy Adamson9733f0d2010-01-22 12:03:08 -0500368static __be32 decode_rc_list(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400369 struct referring_call_list *rc_list)
370{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500371 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400372 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500373 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400374
375 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
376 if (status)
377 goto out;
378
379 status = htonl(NFS4ERR_RESOURCE);
380 p = read_buf(xdr, sizeof(uint32_t));
381 if (unlikely(p == NULL))
382 goto out;
383
384 rc_list->rcl_nrefcalls = ntohl(*p++);
385 if (rc_list->rcl_nrefcalls) {
386 p = read_buf(xdr,
387 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
388 if (unlikely(p == NULL))
389 goto out;
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500390 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400391 sizeof(*rc_list->rcl_refcalls),
392 GFP_KERNEL);
393 if (unlikely(rc_list->rcl_refcalls == NULL))
394 goto out;
395 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
396 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
397 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
398 }
399 }
400 status = 0;
401
402out:
403 return status;
404}
405
Andy Adamson9733f0d2010-01-22 12:03:08 -0500406static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400407 struct xdr_stream *xdr,
408 struct cb_sequenceargs *args)
409{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500410 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400411 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500412 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400413
414 status = decode_sessionid(xdr, &args->csa_sessionid);
415 if (status)
Anna Schumaker17965492017-04-07 14:15:01 -0400416 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400417
Benny Halevy4aece6a2009-04-01 09:23:26 -0400418 p = read_buf(xdr, 5 * sizeof(uint32_t));
419 if (unlikely(p == NULL))
Anna Schumaker17965492017-04-07 14:15:01 -0400420 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400421
Ricardo Labiaga65fc64e2009-04-01 09:23:30 -0400422 args->csa_addr = svc_addr(rqstp);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400423 args->csa_sequenceid = ntohl(*p++);
424 args->csa_slotid = ntohl(*p++);
425 args->csa_highestslotid = ntohl(*p++);
426 args->csa_cachethis = ntohl(*p++);
427 args->csa_nrclists = ntohl(*p++);
428 args->csa_rclists = NULL;
429 if (args->csa_nrclists) {
Dan Carpenter0439f312012-06-12 10:37:08 +0300430 args->csa_rclists = kmalloc_array(args->csa_nrclists,
431 sizeof(*args->csa_rclists),
432 GFP_KERNEL);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400433 if (unlikely(args->csa_rclists == NULL))
Anna Schumaker17965492017-04-07 14:15:01 -0400434 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400435
436 for (i = 0; i < args->csa_nrclists; i++) {
437 status = decode_rc_list(xdr, &args->csa_rclists[i]);
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500438 if (status) {
439 args->csa_nrclists = i;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400440 goto out_free;
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500441 }
Benny Halevy4aece6a2009-04-01 09:23:26 -0400442 }
443 }
Anna Schumaker17965492017-04-07 14:15:01 -0400444 return 0;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400445
446out_free:
447 for (i = 0; i < args->csa_nrclists; i++)
448 kfree(args->csa_rclists[i].rcl_refcalls);
449 kfree(args->csa_rclists);
Anna Schumaker17965492017-04-07 14:15:01 -0400450 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400451}
452
Andy Adamson9733f0d2010-01-22 12:03:08 -0500453static __be32 decode_recallany_args(struct svc_rqst *rqstp,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500454 struct xdr_stream *xdr,
455 struct cb_recallanyargs *args)
456{
Peng Taod743c3c2011-10-23 20:22:38 -0700457 uint32_t bitmap[2];
458 __be32 *p, status;
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500459
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500460 p = read_buf(xdr, 4);
461 if (unlikely(p == NULL))
462 return htonl(NFS4ERR_BADXDR);
463 args->craa_objs_to_keep = ntohl(*p++);
Peng Taod743c3c2011-10-23 20:22:38 -0700464 status = decode_bitmap(xdr, bitmap);
465 if (unlikely(status))
466 return status;
467 args->craa_type_mask = bitmap[0];
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500468
469 return 0;
470}
471
Andy Adamson9733f0d2010-01-22 12:03:08 -0500472static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500473 struct xdr_stream *xdr,
474 struct cb_recallslotargs *args)
475{
476 __be32 *p;
477
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500478 p = read_buf(xdr, 4);
479 if (unlikely(p == NULL))
480 return htonl(NFS4ERR_BADXDR);
Trond Myklebustd5fb4ce2012-11-20 20:24:02 -0500481 args->crsa_target_highest_slotid = ntohl(*p++);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500482 return 0;
483}
484
Jeff Laytondb783682016-09-17 18:17:36 -0400485static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
486{
487 __be32 *p;
488 unsigned int len;
489
490 p = read_buf(xdr, 12);
491 if (unlikely(p == NULL))
492 return htonl(NFS4ERR_BADXDR);
493
494 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
495 len = be32_to_cpu(*p);
496
497 p = read_buf(xdr, len);
498 if (unlikely(p == NULL))
499 return htonl(NFS4ERR_BADXDR);
500
501 /* Only try to decode if the length is right */
502 if (len == 20) {
503 p += 2; /* skip "lock id:" */
504 args->cbnl_owner.s_dev = be32_to_cpu(*p++);
505 xdr_decode_hyper(p, &args->cbnl_owner.id);
506 args->cbnl_valid = true;
507 } else {
508 args->cbnl_owner.s_dev = 0;
509 args->cbnl_owner.id = 0;
510 args->cbnl_valid = false;
511 }
512 return 0;
513}
514
515static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_notify_lock_args *args)
516{
517 __be32 status;
518
519 status = decode_fh(xdr, &args->cbnl_fh);
520 if (unlikely(status != 0))
521 goto out;
522 status = decode_lockowner(xdr, args);
523out:
524 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
525 return status;
526}
527
Benny Halevy4aece6a2009-04-01 09:23:26 -0400528#endif /* CONFIG_NFS_V4_1 */
529
Al Viroe6f684f2006-10-19 23:28:50 -0700530static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Trond Myklebustab6e9aa2017-02-19 16:08:26 -0500532 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
533 return cpu_to_be32(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535}
536
537#define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
538#define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
Al Viro5704fde2006-10-19 23:28:51 -0700539static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Al Viro5704fde2006-10-19 23:28:51 -0700541 __be32 bm[2];
542 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
545 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
546 if (bm[1] != 0) {
547 p = xdr_reserve_space(xdr, 16);
548 if (unlikely(p == NULL))
549 return htonl(NFS4ERR_RESOURCE);
550 *p++ = htonl(2);
551 *p++ = bm[0];
552 *p++ = bm[1];
553 } else if (bm[0] != 0) {
554 p = xdr_reserve_space(xdr, 12);
555 if (unlikely(p == NULL))
556 return htonl(NFS4ERR_RESOURCE);
557 *p++ = htonl(1);
558 *p++ = bm[0];
559 } else {
560 p = xdr_reserve_space(xdr, 8);
561 if (unlikely(p == NULL))
562 return htonl(NFS4ERR_RESOURCE);
563 *p++ = htonl(0);
564 }
565 *savep = p;
566 return 0;
567}
568
Al Viroe6f684f2006-10-19 23:28:50 -0700569static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Al Viro5704fde2006-10-19 23:28:51 -0700571 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
574 return 0;
575 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800576 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return htonl(NFS4ERR_RESOURCE);
578 p = xdr_encode_hyper(p, change);
579 return 0;
580}
581
Al Viroe6f684f2006-10-19 23:28:50 -0700582static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Al Viro5704fde2006-10-19 23:28:51 -0700584 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
587 return 0;
588 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800589 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return htonl(NFS4ERR_RESOURCE);
591 p = xdr_encode_hyper(p, size);
592 return 0;
593}
594
Al Viroe6f684f2006-10-19 23:28:50 -0700595static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Al Viro5704fde2006-10-19 23:28:51 -0700597 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 p = xdr_reserve_space(xdr, 12);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800600 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return htonl(NFS4ERR_RESOURCE);
602 p = xdr_encode_hyper(p, time->tv_sec);
603 *p = htonl(time->tv_nsec);
604 return 0;
605}
606
Al Viroe6f684f2006-10-19 23:28:50 -0700607static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
610 return 0;
611 return encode_attr_time(xdr,time);
612}
613
Al Viroe6f684f2006-10-19 23:28:50 -0700614static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
617 return 0;
618 return encode_attr_time(xdr,time);
619}
620
Al Viroe6f684f2006-10-19 23:28:50 -0700621static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Al Viroe6f684f2006-10-19 23:28:50 -0700623 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 hdr->status = xdr_reserve_space(xdr, 4);
626 if (unlikely(hdr->status == NULL))
627 return htonl(NFS4ERR_RESOURCE);
628 status = encode_string(xdr, hdr->taglen, hdr->tag);
629 if (unlikely(status != 0))
630 return status;
631 hdr->nops = xdr_reserve_space(xdr, 4);
632 if (unlikely(hdr->nops == NULL))
633 return htonl(NFS4ERR_RESOURCE);
634 return 0;
635}
636
Al Viroe6f684f2006-10-19 23:28:50 -0700637static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Al Viro5704fde2006-10-19 23:28:51 -0700639 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 p = xdr_reserve_space(xdr, 8);
642 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500643 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 *p++ = htonl(op);
645 *p = res;
646 return 0;
647}
648
Al Viroe6f684f2006-10-19 23:28:50 -0700649static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Al Viro5704fde2006-10-19 23:28:51 -0700651 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700652 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (unlikely(status != 0))
655 goto out;
656 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
657 if (unlikely(status != 0))
658 goto out;
659 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
660 if (unlikely(status != 0))
661 goto out;
662 status = encode_attr_size(xdr, res->bitmap, res->size);
663 if (unlikely(status != 0))
664 goto out;
665 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
666 if (unlikely(status != 0))
667 goto out;
668 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
669 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
670out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700671 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return status;
673}
674
Benny Halevy34bc47c92009-04-01 09:23:22 -0400675#if defined(CONFIG_NFS_V4_1)
676
Andy Adamson9733f0d2010-01-22 12:03:08 -0500677static __be32 encode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400678 const struct nfs4_sessionid *sid)
679{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500680 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400681
Kinglong Mee590184a2015-09-24 20:57:37 +0800682 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400683 if (unlikely(p == NULL))
684 return htonl(NFS4ERR_RESOURCE);
685
Kinglong Mee590184a2015-09-24 20:57:37 +0800686 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400687 return 0;
688}
689
Andy Adamson9733f0d2010-01-22 12:03:08 -0500690static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400691 struct xdr_stream *xdr,
692 const struct cb_sequenceres *res)
693{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500694 __be32 *p;
Dan Carpentere216c8c2012-06-12 10:37:39 +0300695 __be32 status = res->csr_status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400696
697 if (unlikely(status != 0))
698 goto out;
699
Kinglong Meee0a63c02015-09-24 20:58:38 +0800700 status = encode_sessionid(xdr, &res->csr_sessionid);
701 if (status)
702 goto out;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400703
704 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
705 if (unlikely(p == NULL))
706 return htonl(NFS4ERR_RESOURCE);
707
708 *p++ = htonl(res->csr_sequenceid);
709 *p++ = htonl(res->csr_slotid);
710 *p++ = htonl(res->csr_highestslotid);
711 *p++ = htonl(res->csr_target_highestslotid);
712out:
713 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
714 return status;
715}
716
Benny Halevy34bc47c92009-04-01 09:23:22 -0400717static __be32
718preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
719{
Benny Halevy281fe152009-04-01 09:23:27 -0400720 if (op_nr == OP_CB_SEQUENCE) {
721 if (nop != 0)
722 return htonl(NFS4ERR_SEQUENCE_POS);
723 } else {
724 if (nop == 0)
725 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
726 }
727
Benny Halevy34bc47c92009-04-01 09:23:22 -0400728 switch (op_nr) {
729 case OP_CB_GETATTR:
730 case OP_CB_RECALL:
Benny Halevy4aece6a2009-04-01 09:23:26 -0400731 case OP_CB_SEQUENCE:
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500732 case OP_CB_RECALL_ANY:
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500733 case OP_CB_RECALL_SLOT:
Fred Isamanf2a62562011-01-06 11:36:29 +0000734 case OP_CB_LAYOUTRECALL:
Marc Eshel1be56832011-05-22 19:47:09 +0300735 case OP_CB_NOTIFY_DEVICEID:
Jeff Laytondb783682016-09-17 18:17:36 -0400736 case OP_CB_NOTIFY_LOCK:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400737 *op = &callback_ops[op_nr];
738 break;
739
Benny Halevy34bc47c92009-04-01 09:23:22 -0400740 case OP_CB_NOTIFY:
741 case OP_CB_PUSH_DELEG:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400742 case OP_CB_RECALLABLE_OBJ_AVAIL:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400743 case OP_CB_WANTS_CANCELLED:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400744 return htonl(NFS4ERR_NOTSUPP);
745
746 default:
747 return htonl(NFS4ERR_OP_ILLEGAL);
748 }
749
750 return htonl(NFS_OK);
751}
752
Trond Myklebust810d82e2016-01-23 15:18:18 -0500753static void nfs4_callback_free_slot(struct nfs4_session *session,
754 struct nfs4_slot *slot)
Andy Adamson42acd022011-01-06 02:04:34 +0000755{
756 struct nfs4_slot_table *tbl = &session->bc_slot_table;
757
758 spin_lock(&tbl->slot_tbl_lock);
759 /*
760 * Let the state manager know callback processing done.
761 * A single slot, so highest used slotid is either 0 or -1
762 */
Trond Myklebust810d82e2016-01-23 15:18:18 -0500763 nfs4_free_slot(tbl, slot);
Andy Adamson774d5f12013-05-20 14:13:50 -0400764 nfs4_slot_tbl_drain_complete(tbl);
Andy Adamson42acd022011-01-06 02:04:34 +0000765 spin_unlock(&tbl->slot_tbl_lock);
766}
767
Trond Myklebust55a67392011-08-02 14:46:29 -0400768static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000769{
Trond Myklebust810d82e2016-01-23 15:18:18 -0500770 if (cps->slot) {
771 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
772 cps->slot = NULL;
773 }
Andy Adamson42acd022011-01-06 02:04:34 +0000774}
775
Benny Halevy34bc47c92009-04-01 09:23:22 -0400776#else /* CONFIG_NFS_V4_1 */
777
778static __be32
779preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
780{
781 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
782}
783
Trond Myklebust55a67392011-08-02 14:46:29 -0400784static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000785{
786}
Benny Halevy34bc47c92009-04-01 09:23:22 -0400787#endif /* CONFIG_NFS_V4_1 */
788
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400789#ifdef CONFIG_NFS_V4_2
790static __be32
791preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
792{
793 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
794 if (status != htonl(NFS4ERR_OP_ILLEGAL))
795 return status;
796
797 if (op_nr == OP_CB_OFFLOAD)
798 return htonl(NFS4ERR_NOTSUPP);
799 return htonl(NFS4ERR_OP_ILLEGAL);
800}
801#else /* CONFIG_NFS_V4_2 */
802static __be32
803preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
804{
805 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
806}
807#endif /* CONFIG_NFS_V4_2 */
808
Benny Halevy34bc47c92009-04-01 09:23:22 -0400809static __be32
810preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
811{
812 switch (op_nr) {
813 case OP_CB_GETATTR:
814 case OP_CB_RECALL:
815 *op = &callback_ops[op_nr];
816 break;
817 default:
818 return htonl(NFS4ERR_OP_ILLEGAL);
819 }
820
821 return htonl(NFS_OK);
822}
823
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400824static __be32 process_op(int nop, struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct xdr_stream *xdr_in, void *argp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000826 struct xdr_stream *xdr_out, void *resp,
827 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500829 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500830 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400831 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700833 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Harvey Harrison3110ff82008-05-02 13:42:44 -0700835 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500837 if (unlikely(status))
838 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Benny Halevy34bc47c92009-04-01 09:23:22 -0400840 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400841 __func__, cps->minorversion, nop, op_nr);
Benny Halevy34bc47c92009-04-01 09:23:22 -0400842
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400843 switch (cps->minorversion) {
844 case 0:
845 status = preprocess_nfs4_op(op_nr, &op);
846 break;
847 case 1:
848 status = preprocess_nfs41_op(nop, op_nr, &op);
849 break;
850 case 2:
851 status = preprocess_nfs42_op(nop, op_nr, &op);
852 break;
853 default:
854 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
855 }
856
Benny Halevy34bc47c92009-04-01 09:23:22 -0400857 if (status == htonl(NFS4ERR_OP_ILLEGAL))
858 op_nr = OP_CB_ILLEGAL;
Andy Adamsonb92b3012010-01-14 17:45:05 -0500859 if (status)
860 goto encode_hdr;
Andy Adamson31d2b432010-01-14 17:45:04 -0500861
Andy Adamsonc36fca52011-01-06 02:04:32 +0000862 if (cps->drc_status) {
863 status = cps->drc_status;
Andy Adamson49110962010-01-14 17:45:08 -0500864 goto encode_hdr;
865 }
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 maxlen = xdr_out->end - xdr_out->p;
868 if (maxlen > 0 && maxlen < PAGE_SIZE) {
Andy Adamsone95e60d2010-01-14 17:45:06 -0500869 status = op->decode_args(rqstp, xdr_in, argp);
870 if (likely(status == 0))
Andy Adamsonc36fca52011-01-06 02:04:32 +0000871 status = op->process_op(argp, resp, cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 } else
873 status = htonl(NFS4ERR_RESOURCE);
874
Andy Adamsonb92b3012010-01-14 17:45:05 -0500875encode_hdr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 res = encode_op_hdr(xdr_out, op_nr, status);
Andy Adamson31d2b432010-01-14 17:45:04 -0500877 if (unlikely(res))
878 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (op->encode_res != NULL && status == 0)
880 status = op->encode_res(rqstp, xdr_out, resp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700881 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return status;
883}
884
885/*
886 * Decode, process and encode a COMPOUND
887 */
Al Viro7111c662006-10-19 23:28:45 -0700888static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400890 struct cb_compound_hdr_arg hdr_arg = { 0 };
891 struct cb_compound_hdr_res hdr_res = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 struct xdr_stream xdr_in, xdr_out;
Andy Adamsonc36fca52011-01-06 02:04:32 +0000893 __be32 *p, status;
894 struct cb_process_state cps = {
895 .drc_status = 0,
896 .clp = NULL,
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400897 .net = SVC_NET(rqstp),
Andy Adamsonc36fca52011-01-06 02:04:32 +0000898 };
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400899 unsigned int nops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Harvey Harrison3110ff82008-05-02 13:42:44 -0700901 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Trond Myklebust756b9b32015-12-07 12:52:23 -0800903 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Al Viro5704fde2006-10-19 23:28:51 -0700905 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
907
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400908 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
Vaishali Thakkar4ed0d832015-06-10 20:15:29 +0530909 if (status == htonl(NFS4ERR_RESOURCE))
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400910 return rpc_garbage_args;
911
Andy Adamsonc36fca52011-01-06 02:04:32 +0000912 if (hdr_arg.minorversion == 0) {
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400913 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
Andy Adamson778be232011-01-25 15:38:01 +0000914 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
Chuck Levera4e187d2016-06-29 13:55:22 -0400915 goto out_invalidcred;
Andy Adamson778be232011-01-25 15:38:01 +0000916 }
Andy Adamsonc36fca52011-01-06 02:04:32 +0000917
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400918 cps.minorversion = hdr_arg.minorversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 hdr_res.taglen = hdr_arg.taglen;
920 hdr_res.tag = hdr_arg.tag;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400921 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
922 return rpc_system_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400924 while (status == 0 && nops != hdr_arg.nops) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400925 status = process_op(nops, rqstp, &xdr_in,
926 argp, &xdr_out, resp, &cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 nops++;
928 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400929
Andy Adamson31d2b432010-01-14 17:45:04 -0500930 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
931 * resource error in cb_compound status without returning op */
932 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
933 status = htonl(NFS4ERR_RESOURCE);
934 nops--;
935 }
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 *hdr_res.status = status;
938 *hdr_res.nops = htonl(nops);
Trond Myklebust55a67392011-08-02 14:46:29 -0400939 nfs4_cb_free_slot(&cps);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000940 nfs_put_client(cps.clp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700941 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return rpc_success;
Chuck Levera4e187d2016-06-29 13:55:22 -0400943
944out_invalidcred:
945 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
946 return rpc_autherr_badcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949/*
950 * Define NFS4 callback COMPOUND ops.
951 */
952static struct callback_op callback_ops[] = {
953 [0] = {
954 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
955 },
956 [OP_CB_GETATTR] = {
957 .process_op = (callback_process_op_t)nfs4_callback_getattr,
958 .decode_args = (callback_decode_arg_t)decode_getattr_args,
959 .encode_res = (callback_encode_res_t)encode_getattr_res,
960 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
961 },
962 [OP_CB_RECALL] = {
963 .process_op = (callback_process_op_t)nfs4_callback_recall,
964 .decode_args = (callback_decode_arg_t)decode_recall_args,
965 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400966 },
967#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +0000968 [OP_CB_LAYOUTRECALL] = {
969 .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
970 .decode_args =
971 (callback_decode_arg_t)decode_layoutrecall_args,
972 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
973 },
Marc Eshel1be56832011-05-22 19:47:09 +0300974 [OP_CB_NOTIFY_DEVICEID] = {
975 .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
976 .decode_args =
977 (callback_decode_arg_t)decode_devicenotify_args,
978 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
979 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400980 [OP_CB_SEQUENCE] = {
981 .process_op = (callback_process_op_t)nfs4_callback_sequence,
982 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
983 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
984 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
985 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500986 [OP_CB_RECALL_ANY] = {
987 .process_op = (callback_process_op_t)nfs4_callback_recallany,
988 .decode_args = (callback_decode_arg_t)decode_recallany_args,
989 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
990 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500991 [OP_CB_RECALL_SLOT] = {
992 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
993 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
994 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
995 },
Jeff Laytondb783682016-09-17 18:17:36 -0400996 [OP_CB_NOTIFY_LOCK] = {
997 .process_op = (callback_process_op_t)nfs4_callback_notify_lock,
998 .decode_args = (callback_decode_arg_t)decode_notify_lock_args,
999 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
1000 },
Benny Halevy4aece6a2009-04-01 09:23:26 -04001001#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002};
1003
1004/*
1005 * Define NFS4 callback procedures
1006 */
1007static struct svc_procedure nfs4_callback_procedures1[] = {
1008 [CB_NULL] = {
1009 .pc_func = nfs4_callback_null,
1010 .pc_decode = (kxdrproc_t)nfs4_decode_void,
1011 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1012 .pc_xdrressize = 1,
1013 },
1014 [CB_COMPOUND] = {
1015 .pc_func = nfs4_callback_compound,
1016 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1017 .pc_argsize = 256,
1018 .pc_ressize = 256,
1019 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1020 }
1021};
1022
1023struct svc_version nfs4_callback_version1 = {
1024 .vs_vers = 1,
1025 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1026 .vs_proc = nfs4_callback_procedures1,
1027 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1028 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001029 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001030 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031};
1032
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001033struct svc_version nfs4_callback_version4 = {
1034 .vs_vers = 4,
1035 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1036 .vs_proc = nfs4_callback_procedures1,
1037 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1038 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001039 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001040 .vs_need_cong_ctrl = true,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001041};