blob: e54e697340a491a1aebe356ea227768328416e83 [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))
Anna Schumaker535ece22017-04-07 14:15:02 -0400521 return status;
522 return decode_lockowner(xdr, args);
Jeff Laytondb783682016-09-17 18:17:36 -0400523}
524
Benny Halevy4aece6a2009-04-01 09:23:26 -0400525#endif /* CONFIG_NFS_V4_1 */
526
Al Viroe6f684f2006-10-19 23:28:50 -0700527static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Trond Myklebustab6e9aa2017-02-19 16:08:26 -0500529 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
530 return cpu_to_be32(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return 0;
532}
533
534#define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
535#define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
Al Viro5704fde2006-10-19 23:28:51 -0700536static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Al Viro5704fde2006-10-19 23:28:51 -0700538 __be32 bm[2];
539 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
542 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
543 if (bm[1] != 0) {
544 p = xdr_reserve_space(xdr, 16);
545 if (unlikely(p == NULL))
546 return htonl(NFS4ERR_RESOURCE);
547 *p++ = htonl(2);
548 *p++ = bm[0];
549 *p++ = bm[1];
550 } else if (bm[0] != 0) {
551 p = xdr_reserve_space(xdr, 12);
552 if (unlikely(p == NULL))
553 return htonl(NFS4ERR_RESOURCE);
554 *p++ = htonl(1);
555 *p++ = bm[0];
556 } else {
557 p = xdr_reserve_space(xdr, 8);
558 if (unlikely(p == NULL))
559 return htonl(NFS4ERR_RESOURCE);
560 *p++ = htonl(0);
561 }
562 *savep = p;
563 return 0;
564}
565
Al Viroe6f684f2006-10-19 23:28:50 -0700566static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Al Viro5704fde2006-10-19 23:28:51 -0700568 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
571 return 0;
572 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800573 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return htonl(NFS4ERR_RESOURCE);
575 p = xdr_encode_hyper(p, change);
576 return 0;
577}
578
Al Viroe6f684f2006-10-19 23:28:50 -0700579static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Al Viro5704fde2006-10-19 23:28:51 -0700581 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
584 return 0;
585 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800586 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return htonl(NFS4ERR_RESOURCE);
588 p = xdr_encode_hyper(p, size);
589 return 0;
590}
591
Al Viroe6f684f2006-10-19 23:28:50 -0700592static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Al Viro5704fde2006-10-19 23:28:51 -0700594 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 p = xdr_reserve_space(xdr, 12);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800597 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return htonl(NFS4ERR_RESOURCE);
599 p = xdr_encode_hyper(p, time->tv_sec);
600 *p = htonl(time->tv_nsec);
601 return 0;
602}
603
Al Viroe6f684f2006-10-19 23:28:50 -0700604static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
607 return 0;
608 return encode_attr_time(xdr,time);
609}
610
Al Viroe6f684f2006-10-19 23:28:50 -0700611static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
614 return 0;
615 return encode_attr_time(xdr,time);
616}
617
Al Viroe6f684f2006-10-19 23:28:50 -0700618static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Al Viroe6f684f2006-10-19 23:28:50 -0700620 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 hdr->status = xdr_reserve_space(xdr, 4);
623 if (unlikely(hdr->status == NULL))
624 return htonl(NFS4ERR_RESOURCE);
625 status = encode_string(xdr, hdr->taglen, hdr->tag);
626 if (unlikely(status != 0))
627 return status;
628 hdr->nops = xdr_reserve_space(xdr, 4);
629 if (unlikely(hdr->nops == NULL))
630 return htonl(NFS4ERR_RESOURCE);
631 return 0;
632}
633
Al Viroe6f684f2006-10-19 23:28:50 -0700634static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Al Viro5704fde2006-10-19 23:28:51 -0700636 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 p = xdr_reserve_space(xdr, 8);
639 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500640 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 *p++ = htonl(op);
642 *p = res;
643 return 0;
644}
645
Al Viroe6f684f2006-10-19 23:28:50 -0700646static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Al Viro5704fde2006-10-19 23:28:51 -0700648 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700649 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (unlikely(status != 0))
652 goto out;
653 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
654 if (unlikely(status != 0))
655 goto out;
656 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
657 if (unlikely(status != 0))
658 goto out;
659 status = encode_attr_size(xdr, res->bitmap, res->size);
660 if (unlikely(status != 0))
661 goto out;
662 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
663 if (unlikely(status != 0))
664 goto out;
665 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
666 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
667out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700668 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return status;
670}
671
Benny Halevy34bc47c92009-04-01 09:23:22 -0400672#if defined(CONFIG_NFS_V4_1)
673
Andy Adamson9733f0d2010-01-22 12:03:08 -0500674static __be32 encode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400675 const struct nfs4_sessionid *sid)
676{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500677 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400678
Kinglong Mee590184a2015-09-24 20:57:37 +0800679 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400680 if (unlikely(p == NULL))
681 return htonl(NFS4ERR_RESOURCE);
682
Kinglong Mee590184a2015-09-24 20:57:37 +0800683 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400684 return 0;
685}
686
Andy Adamson9733f0d2010-01-22 12:03:08 -0500687static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400688 struct xdr_stream *xdr,
689 const struct cb_sequenceres *res)
690{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500691 __be32 *p;
Dan Carpentere216c8c2012-06-12 10:37:39 +0300692 __be32 status = res->csr_status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400693
694 if (unlikely(status != 0))
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400695 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400696
Kinglong Meee0a63c02015-09-24 20:58:38 +0800697 status = encode_sessionid(xdr, &res->csr_sessionid);
698 if (status)
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400699 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400700
701 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
702 if (unlikely(p == NULL))
703 return htonl(NFS4ERR_RESOURCE);
704
705 *p++ = htonl(res->csr_sequenceid);
706 *p++ = htonl(res->csr_slotid);
707 *p++ = htonl(res->csr_highestslotid);
708 *p++ = htonl(res->csr_target_highestslotid);
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400709 return 0;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400710}
711
Benny Halevy34bc47c92009-04-01 09:23:22 -0400712static __be32
713preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
714{
Benny Halevy281fe152009-04-01 09:23:27 -0400715 if (op_nr == OP_CB_SEQUENCE) {
716 if (nop != 0)
717 return htonl(NFS4ERR_SEQUENCE_POS);
718 } else {
719 if (nop == 0)
720 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
721 }
722
Benny Halevy34bc47c92009-04-01 09:23:22 -0400723 switch (op_nr) {
724 case OP_CB_GETATTR:
725 case OP_CB_RECALL:
Benny Halevy4aece6a2009-04-01 09:23:26 -0400726 case OP_CB_SEQUENCE:
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500727 case OP_CB_RECALL_ANY:
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500728 case OP_CB_RECALL_SLOT:
Fred Isamanf2a62562011-01-06 11:36:29 +0000729 case OP_CB_LAYOUTRECALL:
Marc Eshel1be56832011-05-22 19:47:09 +0300730 case OP_CB_NOTIFY_DEVICEID:
Jeff Laytondb783682016-09-17 18:17:36 -0400731 case OP_CB_NOTIFY_LOCK:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400732 *op = &callback_ops[op_nr];
733 break;
734
Benny Halevy34bc47c92009-04-01 09:23:22 -0400735 case OP_CB_NOTIFY:
736 case OP_CB_PUSH_DELEG:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400737 case OP_CB_RECALLABLE_OBJ_AVAIL:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400738 case OP_CB_WANTS_CANCELLED:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400739 return htonl(NFS4ERR_NOTSUPP);
740
741 default:
742 return htonl(NFS4ERR_OP_ILLEGAL);
743 }
744
745 return htonl(NFS_OK);
746}
747
Trond Myklebust810d82e2016-01-23 15:18:18 -0500748static void nfs4_callback_free_slot(struct nfs4_session *session,
749 struct nfs4_slot *slot)
Andy Adamson42acd022011-01-06 02:04:34 +0000750{
751 struct nfs4_slot_table *tbl = &session->bc_slot_table;
752
753 spin_lock(&tbl->slot_tbl_lock);
754 /*
755 * Let the state manager know callback processing done.
756 * A single slot, so highest used slotid is either 0 or -1
757 */
Trond Myklebust810d82e2016-01-23 15:18:18 -0500758 nfs4_free_slot(tbl, slot);
Andy Adamson774d5f12013-05-20 14:13:50 -0400759 nfs4_slot_tbl_drain_complete(tbl);
Andy Adamson42acd022011-01-06 02:04:34 +0000760 spin_unlock(&tbl->slot_tbl_lock);
761}
762
Trond Myklebust55a67392011-08-02 14:46:29 -0400763static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000764{
Trond Myklebust810d82e2016-01-23 15:18:18 -0500765 if (cps->slot) {
766 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
767 cps->slot = NULL;
768 }
Andy Adamson42acd022011-01-06 02:04:34 +0000769}
770
Benny Halevy34bc47c92009-04-01 09:23:22 -0400771#else /* CONFIG_NFS_V4_1 */
772
773static __be32
774preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
775{
776 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
777}
778
Trond Myklebust55a67392011-08-02 14:46:29 -0400779static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000780{
781}
Benny Halevy34bc47c92009-04-01 09:23:22 -0400782#endif /* CONFIG_NFS_V4_1 */
783
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400784#ifdef CONFIG_NFS_V4_2
785static __be32
786preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
787{
788 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
789 if (status != htonl(NFS4ERR_OP_ILLEGAL))
790 return status;
791
792 if (op_nr == OP_CB_OFFLOAD)
793 return htonl(NFS4ERR_NOTSUPP);
794 return htonl(NFS4ERR_OP_ILLEGAL);
795}
796#else /* CONFIG_NFS_V4_2 */
797static __be32
798preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
799{
800 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
801}
802#endif /* CONFIG_NFS_V4_2 */
803
Benny Halevy34bc47c92009-04-01 09:23:22 -0400804static __be32
805preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
806{
807 switch (op_nr) {
808 case OP_CB_GETATTR:
809 case OP_CB_RECALL:
810 *op = &callback_ops[op_nr];
811 break;
812 default:
813 return htonl(NFS4ERR_OP_ILLEGAL);
814 }
815
816 return htonl(NFS_OK);
817}
818
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400819static __be32 process_op(int nop, struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct xdr_stream *xdr_in, void *argp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000821 struct xdr_stream *xdr_out, void *resp,
822 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500824 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500825 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400826 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700828 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Harvey Harrison3110ff82008-05-02 13:42:44 -0700830 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500832 if (unlikely(status))
833 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Benny Halevy34bc47c92009-04-01 09:23:22 -0400835 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400836 __func__, cps->minorversion, nop, op_nr);
Benny Halevy34bc47c92009-04-01 09:23:22 -0400837
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400838 switch (cps->minorversion) {
839 case 0:
840 status = preprocess_nfs4_op(op_nr, &op);
841 break;
842 case 1:
843 status = preprocess_nfs41_op(nop, op_nr, &op);
844 break;
845 case 2:
846 status = preprocess_nfs42_op(nop, op_nr, &op);
847 break;
848 default:
849 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
850 }
851
Benny Halevy34bc47c92009-04-01 09:23:22 -0400852 if (status == htonl(NFS4ERR_OP_ILLEGAL))
853 op_nr = OP_CB_ILLEGAL;
Andy Adamsonb92b3012010-01-14 17:45:05 -0500854 if (status)
855 goto encode_hdr;
Andy Adamson31d2b432010-01-14 17:45:04 -0500856
Andy Adamsonc36fca52011-01-06 02:04:32 +0000857 if (cps->drc_status) {
858 status = cps->drc_status;
Andy Adamson49110962010-01-14 17:45:08 -0500859 goto encode_hdr;
860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 maxlen = xdr_out->end - xdr_out->p;
863 if (maxlen > 0 && maxlen < PAGE_SIZE) {
Andy Adamsone95e60d2010-01-14 17:45:06 -0500864 status = op->decode_args(rqstp, xdr_in, argp);
865 if (likely(status == 0))
Andy Adamsonc36fca52011-01-06 02:04:32 +0000866 status = op->process_op(argp, resp, cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 } else
868 status = htonl(NFS4ERR_RESOURCE);
869
Andy Adamsonb92b3012010-01-14 17:45:05 -0500870encode_hdr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 res = encode_op_hdr(xdr_out, op_nr, status);
Andy Adamson31d2b432010-01-14 17:45:04 -0500872 if (unlikely(res))
873 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (op->encode_res != NULL && status == 0)
875 status = op->encode_res(rqstp, xdr_out, resp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700876 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return status;
878}
879
880/*
881 * Decode, process and encode a COMPOUND
882 */
Al Viro7111c662006-10-19 23:28:45 -0700883static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400885 struct cb_compound_hdr_arg hdr_arg = { 0 };
886 struct cb_compound_hdr_res hdr_res = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct xdr_stream xdr_in, xdr_out;
Andy Adamsonc36fca52011-01-06 02:04:32 +0000888 __be32 *p, status;
889 struct cb_process_state cps = {
890 .drc_status = 0,
891 .clp = NULL,
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400892 .net = SVC_NET(rqstp),
Andy Adamsonc36fca52011-01-06 02:04:32 +0000893 };
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400894 unsigned int nops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Harvey Harrison3110ff82008-05-02 13:42:44 -0700896 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Trond Myklebust756b9b32015-12-07 12:52:23 -0800898 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Al Viro5704fde2006-10-19 23:28:51 -0700900 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
902
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400903 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
Vaishali Thakkar4ed0d832015-06-10 20:15:29 +0530904 if (status == htonl(NFS4ERR_RESOURCE))
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400905 return rpc_garbage_args;
906
Andy Adamsonc36fca52011-01-06 02:04:32 +0000907 if (hdr_arg.minorversion == 0) {
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400908 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
Andy Adamson778be232011-01-25 15:38:01 +0000909 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
Chuck Levera4e187d2016-06-29 13:55:22 -0400910 goto out_invalidcred;
Andy Adamson778be232011-01-25 15:38:01 +0000911 }
Andy Adamsonc36fca52011-01-06 02:04:32 +0000912
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400913 cps.minorversion = hdr_arg.minorversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 hdr_res.taglen = hdr_arg.taglen;
915 hdr_res.tag = hdr_arg.tag;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400916 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
917 return rpc_system_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400919 while (status == 0 && nops != hdr_arg.nops) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400920 status = process_op(nops, rqstp, &xdr_in,
921 argp, &xdr_out, resp, &cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 nops++;
923 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400924
Andy Adamson31d2b432010-01-14 17:45:04 -0500925 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
926 * resource error in cb_compound status without returning op */
927 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
928 status = htonl(NFS4ERR_RESOURCE);
929 nops--;
930 }
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 *hdr_res.status = status;
933 *hdr_res.nops = htonl(nops);
Trond Myklebust55a67392011-08-02 14:46:29 -0400934 nfs4_cb_free_slot(&cps);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000935 nfs_put_client(cps.clp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700936 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return rpc_success;
Chuck Levera4e187d2016-06-29 13:55:22 -0400938
939out_invalidcred:
940 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
941 return rpc_autherr_badcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/*
945 * Define NFS4 callback COMPOUND ops.
946 */
947static struct callback_op callback_ops[] = {
948 [0] = {
949 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
950 },
951 [OP_CB_GETATTR] = {
952 .process_op = (callback_process_op_t)nfs4_callback_getattr,
953 .decode_args = (callback_decode_arg_t)decode_getattr_args,
954 .encode_res = (callback_encode_res_t)encode_getattr_res,
955 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
956 },
957 [OP_CB_RECALL] = {
958 .process_op = (callback_process_op_t)nfs4_callback_recall,
959 .decode_args = (callback_decode_arg_t)decode_recall_args,
960 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400961 },
962#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +0000963 [OP_CB_LAYOUTRECALL] = {
964 .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
965 .decode_args =
966 (callback_decode_arg_t)decode_layoutrecall_args,
967 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
968 },
Marc Eshel1be56832011-05-22 19:47:09 +0300969 [OP_CB_NOTIFY_DEVICEID] = {
970 .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
971 .decode_args =
972 (callback_decode_arg_t)decode_devicenotify_args,
973 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
974 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400975 [OP_CB_SEQUENCE] = {
976 .process_op = (callback_process_op_t)nfs4_callback_sequence,
977 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
978 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
979 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
980 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500981 [OP_CB_RECALL_ANY] = {
982 .process_op = (callback_process_op_t)nfs4_callback_recallany,
983 .decode_args = (callback_decode_arg_t)decode_recallany_args,
984 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
985 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500986 [OP_CB_RECALL_SLOT] = {
987 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
988 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
989 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
990 },
Jeff Laytondb783682016-09-17 18:17:36 -0400991 [OP_CB_NOTIFY_LOCK] = {
992 .process_op = (callback_process_op_t)nfs4_callback_notify_lock,
993 .decode_args = (callback_decode_arg_t)decode_notify_lock_args,
994 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
995 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400996#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997};
998
999/*
1000 * Define NFS4 callback procedures
1001 */
1002static struct svc_procedure nfs4_callback_procedures1[] = {
1003 [CB_NULL] = {
1004 .pc_func = nfs4_callback_null,
1005 .pc_decode = (kxdrproc_t)nfs4_decode_void,
1006 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1007 .pc_xdrressize = 1,
1008 },
1009 [CB_COMPOUND] = {
1010 .pc_func = nfs4_callback_compound,
1011 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1012 .pc_argsize = 256,
1013 .pc_ressize = 256,
1014 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1015 }
1016};
1017
1018struct svc_version nfs4_callback_version1 = {
1019 .vs_vers = 1,
1020 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1021 .vs_proc = nfs4_callback_procedures1,
1022 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1023 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001024 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001025 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026};
1027
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001028struct svc_version nfs4_callback_version4 = {
1029 .vs_vers = 4,
1030 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1031 .vs_proc = nfs4_callback_procedures1,
1032 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1033 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001034 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001035 .vs_need_cong_ctrl = true,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001036};