blob: 681dd642f1195a0ebe253f8c57cb3fa81888f0c7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070046struct callback_op {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +020047 __be32 (*process_op)(void *, void *, struct cb_process_state *);
48 __be32 (*decode_args)(struct svc_rqst *, struct xdr_stream *, void *);
49 __be32 (*encode_res)(struct svc_rqst *, struct xdr_stream *,
50 const void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 long res_maxsize;
52};
53
54static struct callback_op callback_ops[];
55
Christoph Hellwiga6beb732017-05-08 17:35:49 +020056static __be32 nfs4_callback_null(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 return htonl(NFS4_OK);
59}
60
Christoph Hellwig026fec72017-05-08 19:01:48 +020061static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 return xdr_argsize_check(rqstp, p);
64}
65
Christoph Hellwig63f8de32017-05-08 19:42:02 +020066static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 return xdr_ressize_check(rqstp, p);
69}
70
Jeff Laytonb60475c2016-09-17 18:17:31 -040071static __be32 *read_buf(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Al Viro5704fde2006-10-19 23:28:51 -070073 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 p = xdr_inline_decode(xdr, nbytes);
76 if (unlikely(p == NULL))
Trond Myklebust756b9b32015-12-07 12:52:23 -080077 printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return p;
79}
80
Trond Myklebustc065eee2017-02-19 16:08:28 -050081static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len,
82 const char **str, size_t maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Trond Myklebustc065eee2017-02-19 16:08:28 -050084 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Trond Myklebustc065eee2017-02-19 16:08:28 -050086 err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen);
87 if (err < 0)
88 return cpu_to_be32(NFS4ERR_RESOURCE);
89 *len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 0;
91}
92
Al Viroe6f684f2006-10-19 23:28:50 -070093static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Al Viro5704fde2006-10-19 23:28:51 -070095 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 p = read_buf(xdr, 4);
98 if (unlikely(p == NULL))
99 return htonl(NFS4ERR_RESOURCE);
100 fh->size = ntohl(*p);
101 if (fh->size > NFS4_FHSIZE)
102 return htonl(NFS4ERR_BADHANDLE);
103 p = read_buf(xdr, fh->size);
104 if (unlikely(p == NULL))
105 return htonl(NFS4ERR_RESOURCE);
106 memcpy(&fh->data[0], p, fh->size);
107 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
108 return 0;
109}
110
Al Viroe6f684f2006-10-19 23:28:50 -0700111static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Al Viro5704fde2006-10-19 23:28:51 -0700113 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int attrlen;
115
116 p = read_buf(xdr, 4);
117 if (unlikely(p == NULL))
118 return htonl(NFS4ERR_RESOURCE);
119 attrlen = ntohl(*p);
120 p = read_buf(xdr, attrlen << 2);
121 if (unlikely(p == NULL))
122 return htonl(NFS4ERR_RESOURCE);
123 if (likely(attrlen > 0))
124 bitmap[0] = ntohl(*p++);
125 if (attrlen > 1)
126 bitmap[1] = ntohl(*p);
127 return 0;
128}
129
Al Viroe6f684f2006-10-19 23:28:50 -0700130static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Al Viro5704fde2006-10-19 23:28:51 -0700132 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500134 p = read_buf(xdr, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (unlikely(p == NULL))
136 return htonl(NFS4ERR_RESOURCE);
Trond Myklebust93b717f2016-05-16 17:42:43 -0400137 memcpy(stateid->data, p, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140
Trond Myklebust93b717f2016-05-16 17:42:43 -0400141static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
142{
143 stateid->type = NFS4_DELEGATION_STATEID_TYPE;
144 return decode_stateid(xdr, stateid);
145}
146
Al Viroe6f684f2006-10-19 23:28:50 -0700147static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Al Viro5704fde2006-10-19 23:28:51 -0700149 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700150 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Trond Myklebustc065eee2017-02-19 16:08:28 -0500152 status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (unlikely(status != 0))
154 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 p = read_buf(xdr, 12);
156 if (unlikely(p == NULL))
157 return htonl(NFS4ERR_RESOURCE);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400158 hdr->minorversion = ntohl(*p++);
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400159 /* Check for minor version support */
160 if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
Steve Dickson42c2c422013-05-22 12:50:38 -0400161 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
Benny Halevy48a9e2d2009-04-01 09:23:20 -0400162 } else {
Trond Myklebust9a3ba432012-03-12 18:01:48 -0400163 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400164 "illegal minor version %u!\n",
165 __func__, hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 hdr->nops = ntohl(*p);
169 return 0;
170}
171
Al Viroe6f684f2006-10-19 23:28:50 -0700172static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Al Viro5704fde2006-10-19 23:28:51 -0700174 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 p = read_buf(xdr, 4);
176 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500177 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 *op = ntohl(*p);
179 return 0;
180}
181
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200182static __be32 decode_getattr_args(struct svc_rqst *rqstp,
183 struct xdr_stream *xdr, void *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200185 struct cb_getattrargs *args = argp;
Al Viroe6f684f2006-10-19 23:28:50 -0700186 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 status = decode_fh(xdr, &args->fh);
189 if (unlikely(status != 0))
Anna Schumaker56938bb2017-04-07 14:14:58 -0400190 return status;
191 return decode_bitmap(xdr, args->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200194static __be32 decode_recall_args(struct svc_rqst *rqstp,
195 struct xdr_stream *xdr, void *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200197 struct cb_recallargs *args = argp;
Al Viro5704fde2006-10-19 23:28:51 -0700198 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700199 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Trond Myklebust93b717f2016-05-16 17:42:43 -0400201 status = decode_delegation_stateid(xdr, &args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (unlikely(status != 0))
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400203 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 p = read_buf(xdr, 4);
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400205 if (unlikely(p == NULL))
206 return htonl(NFS4ERR_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 args->truncate = ntohl(*p);
Anna Schumaker135a4ea2017-04-07 14:14:59 -0400208 return decode_fh(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Benny Halevy4aece6a2009-04-01 09:23:26 -0400211#if defined(CONFIG_NFS_V4_1)
Trond Myklebust93b717f2016-05-16 17:42:43 -0400212static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
213{
214 stateid->type = NFS4_LAYOUT_STATEID_TYPE;
215 return decode_stateid(xdr, stateid);
216}
Benny Halevy4aece6a2009-04-01 09:23:26 -0400217
Fred Isamanf2a62562011-01-06 11:36:29 +0000218static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200219 struct xdr_stream *xdr, void *argp)
Fred Isamanf2a62562011-01-06 11:36:29 +0000220{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200221 struct cb_layoutrecallargs *args = argp;
Fred Isamanf2a62562011-01-06 11:36:29 +0000222 __be32 *p;
223 __be32 status = 0;
224 uint32_t iomode;
225
Fred Isamanf2a62562011-01-06 11:36:29 +0000226 p = read_buf(xdr, 4 * sizeof(uint32_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400227 if (unlikely(p == NULL))
228 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000229
230 args->cbl_layout_type = ntohl(*p++);
231 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300232 * as it is unusable and ignored with the other types.
Fred Isamanf2a62562011-01-06 11:36:29 +0000233 */
234 iomode = ntohl(*p++);
235 args->cbl_layoutchanged = ntohl(*p++);
236 args->cbl_recall_type = ntohl(*p++);
237
238 if (args->cbl_recall_type == RETURN_FILE) {
239 args->cbl_range.iomode = iomode;
240 status = decode_fh(xdr, &args->cbl_fh);
241 if (unlikely(status != 0))
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400242 return status;
Fred Isamanf2a62562011-01-06 11:36:29 +0000243
244 p = read_buf(xdr, 2 * sizeof(uint64_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400245 if (unlikely(p == NULL))
246 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000247 p = xdr_decode_hyper(p, &args->cbl_range.offset);
248 p = xdr_decode_hyper(p, &args->cbl_range.length);
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400249 return decode_layout_stateid(xdr, &args->cbl_stateid);
Fred Isamanf2a62562011-01-06 11:36:29 +0000250 } else if (args->cbl_recall_type == RETURN_FSID) {
251 p = read_buf(xdr, 2 * sizeof(uint64_t));
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400252 if (unlikely(p == NULL))
253 return htonl(NFS4ERR_BADXDR);
Fred Isamanf2a62562011-01-06 11:36:29 +0000254 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
255 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
Anna Schumakerc79d56d2017-04-07 14:15:00 -0400256 } else if (args->cbl_recall_type != RETURN_ALL)
257 return htonl(NFS4ERR_BADXDR);
258 return 0;
Fred Isamanf2a62562011-01-06 11:36:29 +0000259}
260
Marc Eshel1be56832011-05-22 19:47:09 +0300261static
262__be32 decode_devicenotify_args(struct svc_rqst *rqstp,
263 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200264 void *argp)
Marc Eshel1be56832011-05-22 19:47:09 +0300265{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200266 struct cb_devicenotifyargs *args = argp;
Marc Eshel1be56832011-05-22 19:47:09 +0300267 __be32 *p;
268 __be32 status = 0;
269 u32 tmp;
270 int n, i;
271 args->ndevs = 0;
272
273 /* Num of device notifications */
274 p = read_buf(xdr, sizeof(uint32_t));
275 if (unlikely(p == NULL)) {
276 status = htonl(NFS4ERR_BADXDR);
277 goto out;
278 }
279 n = ntohl(*p++);
280 if (n <= 0)
281 goto out;
Dan Carpenter363e0df02012-01-12 10:16:14 +0300282 if (n > ULONG_MAX / sizeof(*args->devs)) {
283 status = htonl(NFS4ERR_BADXDR);
284 goto out;
285 }
Marc Eshel1be56832011-05-22 19:47:09 +0300286
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500287 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
Marc Eshel1be56832011-05-22 19:47:09 +0300288 if (!args->devs) {
289 status = htonl(NFS4ERR_DELAY);
290 goto out;
291 }
292
293 /* Decode each dev notification */
294 for (i = 0; i < n; i++) {
295 struct cb_devicenotifyitem *dev = &args->devs[i];
296
297 p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
298 if (unlikely(p == NULL)) {
299 status = htonl(NFS4ERR_BADXDR);
300 goto err;
301 }
302
303 tmp = ntohl(*p++); /* bitmap size */
304 if (tmp != 1) {
305 status = htonl(NFS4ERR_INVAL);
306 goto err;
307 }
308 dev->cbd_notify_type = ntohl(*p++);
309 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
310 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
311 status = htonl(NFS4ERR_INVAL);
312 goto err;
313 }
314
315 tmp = ntohl(*p++); /* opaque size */
316 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
317 (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
318 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
319 (tmp != NFS4_DEVICEID4_SIZE + 4))) {
320 status = htonl(NFS4ERR_INVAL);
321 goto err;
322 }
323 dev->cbd_layout_type = ntohl(*p++);
324 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
325 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
326
327 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
328 p = read_buf(xdr, sizeof(uint32_t));
329 if (unlikely(p == NULL)) {
330 status = htonl(NFS4ERR_BADXDR);
331 goto err;
332 }
333 dev->cbd_immediate = ntohl(*p++);
334 } else {
335 dev->cbd_immediate = 0;
336 }
337
338 args->ndevs++;
339
340 dprintk("%s: type %d layout 0x%x immediate %d\n",
341 __func__, dev->cbd_notify_type, dev->cbd_layout_type,
342 dev->cbd_immediate);
343 }
344out:
345 dprintk("%s: status %d ndevs %d\n",
346 __func__, ntohl(status), args->ndevs);
347 return status;
348err:
349 kfree(args->devs);
350 goto out;
351}
352
Andy Adamson9733f0d2010-01-22 12:03:08 -0500353static __be32 decode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400354 struct nfs4_sessionid *sid)
355{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500356 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400357
Kinglong Mee590184a2015-09-24 20:57:37 +0800358 p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400359 if (unlikely(p == NULL))
Joe Perchesa419aef2009-08-18 11:18:35 -0700360 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400361
Kinglong Mee590184a2015-09-24 20:57:37 +0800362 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400363 return 0;
364}
365
Andy Adamson9733f0d2010-01-22 12:03:08 -0500366static __be32 decode_rc_list(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400367 struct referring_call_list *rc_list)
368{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500369 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400370 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500371 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400372
373 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
374 if (status)
375 goto out;
376
377 status = htonl(NFS4ERR_RESOURCE);
378 p = read_buf(xdr, sizeof(uint32_t));
379 if (unlikely(p == NULL))
380 goto out;
381
382 rc_list->rcl_nrefcalls = ntohl(*p++);
383 if (rc_list->rcl_nrefcalls) {
384 p = read_buf(xdr,
385 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
386 if (unlikely(p == NULL))
387 goto out;
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500388 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400389 sizeof(*rc_list->rcl_refcalls),
390 GFP_KERNEL);
391 if (unlikely(rc_list->rcl_refcalls == NULL))
392 goto out;
393 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
394 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
395 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
396 }
397 }
398 status = 0;
399
400out:
401 return status;
402}
403
Andy Adamson9733f0d2010-01-22 12:03:08 -0500404static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400405 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200406 void *argp)
Benny Halevy4aece6a2009-04-01 09:23:26 -0400407{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200408 struct cb_sequenceargs *args = argp;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500409 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400410 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500411 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400412
413 status = decode_sessionid(xdr, &args->csa_sessionid);
414 if (status)
Anna Schumaker17965492017-04-07 14:15:01 -0400415 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400416
Benny Halevy4aece6a2009-04-01 09:23:26 -0400417 p = read_buf(xdr, 5 * sizeof(uint32_t));
418 if (unlikely(p == NULL))
Anna Schumaker17965492017-04-07 14:15:01 -0400419 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400420
Ricardo Labiaga65fc64e2009-04-01 09:23:30 -0400421 args->csa_addr = svc_addr(rqstp);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400422 args->csa_sequenceid = ntohl(*p++);
423 args->csa_slotid = ntohl(*p++);
424 args->csa_highestslotid = ntohl(*p++);
425 args->csa_cachethis = ntohl(*p++);
426 args->csa_nrclists = ntohl(*p++);
427 args->csa_rclists = NULL;
428 if (args->csa_nrclists) {
Dan Carpenter0439f312012-06-12 10:37:08 +0300429 args->csa_rclists = kmalloc_array(args->csa_nrclists,
430 sizeof(*args->csa_rclists),
431 GFP_KERNEL);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400432 if (unlikely(args->csa_rclists == NULL))
Anna Schumaker17965492017-04-07 14:15:01 -0400433 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400434
435 for (i = 0; i < args->csa_nrclists; i++) {
436 status = decode_rc_list(xdr, &args->csa_rclists[i]);
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500437 if (status) {
438 args->csa_nrclists = i;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400439 goto out_free;
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500440 }
Benny Halevy4aece6a2009-04-01 09:23:26 -0400441 }
442 }
Anna Schumaker17965492017-04-07 14:15:01 -0400443 return 0;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400444
445out_free:
446 for (i = 0; i < args->csa_nrclists; i++)
447 kfree(args->csa_rclists[i].rcl_refcalls);
448 kfree(args->csa_rclists);
Anna Schumaker17965492017-04-07 14:15:01 -0400449 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400450}
451
Andy Adamson9733f0d2010-01-22 12:03:08 -0500452static __be32 decode_recallany_args(struct svc_rqst *rqstp,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500453 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200454 void *argp)
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500455{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200456 struct cb_recallanyargs *args = argp;
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,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200474 void *argp)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500475{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200476 struct cb_recallslotargs *args = argp;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500477 __be32 *p;
478
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500479 p = read_buf(xdr, 4);
480 if (unlikely(p == NULL))
481 return htonl(NFS4ERR_BADXDR);
Trond Myklebustd5fb4ce2012-11-20 20:24:02 -0500482 args->crsa_target_highest_slotid = ntohl(*p++);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500483 return 0;
484}
485
Jeff Laytondb783682016-09-17 18:17:36 -0400486static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
487{
488 __be32 *p;
489 unsigned int len;
490
491 p = read_buf(xdr, 12);
492 if (unlikely(p == NULL))
493 return htonl(NFS4ERR_BADXDR);
494
495 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
496 len = be32_to_cpu(*p);
497
498 p = read_buf(xdr, len);
499 if (unlikely(p == NULL))
500 return htonl(NFS4ERR_BADXDR);
501
502 /* Only try to decode if the length is right */
503 if (len == 20) {
504 p += 2; /* skip "lock id:" */
505 args->cbnl_owner.s_dev = be32_to_cpu(*p++);
506 xdr_decode_hyper(p, &args->cbnl_owner.id);
507 args->cbnl_valid = true;
508 } else {
509 args->cbnl_owner.s_dev = 0;
510 args->cbnl_owner.id = 0;
511 args->cbnl_valid = false;
512 }
513 return 0;
514}
515
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200516static __be32 decode_notify_lock_args(struct svc_rqst *rqstp,
517 struct xdr_stream *xdr, void *argp)
Jeff Laytondb783682016-09-17 18:17:36 -0400518{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200519 struct cb_notify_lock_args *args = argp;
Jeff Laytondb783682016-09-17 18:17:36 -0400520 __be32 status;
521
522 status = decode_fh(xdr, &args->cbnl_fh);
523 if (unlikely(status != 0))
Anna Schumaker535ece22017-04-07 14:15:02 -0400524 return status;
525 return decode_lockowner(xdr, args);
Jeff Laytondb783682016-09-17 18:17:36 -0400526}
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
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200649static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr,
650 const void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200652 const struct cb_getattrres *res = resp;
Al Viro5704fde2006-10-19 23:28:51 -0700653 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700654 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (unlikely(status != 0))
657 goto out;
658 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
659 if (unlikely(status != 0))
660 goto out;
661 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
662 if (unlikely(status != 0))
663 goto out;
664 status = encode_attr_size(xdr, res->bitmap, res->size);
665 if (unlikely(status != 0))
666 goto out;
667 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
668 if (unlikely(status != 0))
669 goto out;
670 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
671 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
672out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return status;
674}
675
Benny Halevy34bc47c92009-04-01 09:23:22 -0400676#if defined(CONFIG_NFS_V4_1)
677
Andy Adamson9733f0d2010-01-22 12:03:08 -0500678static __be32 encode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400679 const struct nfs4_sessionid *sid)
680{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500681 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400682
Kinglong Mee590184a2015-09-24 20:57:37 +0800683 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400684 if (unlikely(p == NULL))
685 return htonl(NFS4ERR_RESOURCE);
686
Kinglong Mee590184a2015-09-24 20:57:37 +0800687 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400688 return 0;
689}
690
Andy Adamson9733f0d2010-01-22 12:03:08 -0500691static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400692 struct xdr_stream *xdr,
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200693 const void *resp)
Benny Halevy4aece6a2009-04-01 09:23:26 -0400694{
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200695 const struct cb_sequenceres *res = resp;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500696 __be32 *p;
Dan Carpentere216c8c2012-06-12 10:37:39 +0300697 __be32 status = res->csr_status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400698
699 if (unlikely(status != 0))
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400700 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400701
Kinglong Meee0a63c02015-09-24 20:58:38 +0800702 status = encode_sessionid(xdr, &res->csr_sessionid);
703 if (status)
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400704 return status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400705
706 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
707 if (unlikely(p == NULL))
708 return htonl(NFS4ERR_RESOURCE);
709
710 *p++ = htonl(res->csr_sequenceid);
711 *p++ = htonl(res->csr_slotid);
712 *p++ = htonl(res->csr_highestslotid);
713 *p++ = htonl(res->csr_target_highestslotid);
Anna Schumaker3d0bfaa2017-04-07 14:15:03 -0400714 return 0;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400715}
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 Adamson42acd022011-01-06 02:04:34 +0000764 spin_unlock(&tbl->slot_tbl_lock);
765}
766
Trond Myklebust55a67392011-08-02 14:46:29 -0400767static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000768{
Trond Myklebust810d82e2016-01-23 15:18:18 -0500769 if (cps->slot) {
770 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
771 cps->slot = NULL;
772 }
Andy Adamson42acd022011-01-06 02:04:34 +0000773}
774
Benny Halevy34bc47c92009-04-01 09:23:22 -0400775#else /* CONFIG_NFS_V4_1 */
776
777static __be32
778preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
779{
780 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
781}
782
Trond Myklebust55a67392011-08-02 14:46:29 -0400783static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000784{
785}
Benny Halevy34bc47c92009-04-01 09:23:22 -0400786#endif /* CONFIG_NFS_V4_1 */
787
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400788#ifdef CONFIG_NFS_V4_2
789static __be32
790preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
791{
792 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
793 if (status != htonl(NFS4ERR_OP_ILLEGAL))
794 return status;
795
796 if (op_nr == OP_CB_OFFLOAD)
797 return htonl(NFS4ERR_NOTSUPP);
798 return htonl(NFS4ERR_OP_ILLEGAL);
799}
800#else /* CONFIG_NFS_V4_2 */
801static __be32
802preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
803{
804 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
805}
806#endif /* CONFIG_NFS_V4_2 */
807
Benny Halevy34bc47c92009-04-01 09:23:22 -0400808static __be32
809preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
810{
811 switch (op_nr) {
812 case OP_CB_GETATTR:
813 case OP_CB_RECALL:
814 *op = &callback_ops[op_nr];
815 break;
816 default:
817 return htonl(NFS4ERR_OP_ILLEGAL);
818 }
819
820 return htonl(NFS_OK);
821}
822
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400823static __be32 process_op(int nop, struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct xdr_stream *xdr_in, void *argp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000825 struct xdr_stream *xdr_out, void *resp,
826 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500828 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500829 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400830 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700832 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500835 if (unlikely(status))
836 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return status;
877}
878
879/*
880 * Decode, process and encode a COMPOUND
881 */
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200882static __be32 nfs4_callback_compound(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400884 struct cb_compound_hdr_arg hdr_arg = { 0 };
885 struct cb_compound_hdr_res hdr_res = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 struct xdr_stream xdr_in, xdr_out;
Andy Adamsonc36fca52011-01-06 02:04:32 +0000887 __be32 *p, status;
888 struct cb_process_state cps = {
889 .drc_status = 0,
890 .clp = NULL,
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400891 .net = SVC_NET(rqstp),
Andy Adamsonc36fca52011-01-06 02:04:32 +0000892 };
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400893 unsigned int nops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Trond Myklebust756b9b32015-12-07 12:52:23 -0800895 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Al Viro5704fde2006-10-19 23:28:51 -0700897 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
899
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400900 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
Vaishali Thakkar4ed0d832015-06-10 20:15:29 +0530901 if (status == htonl(NFS4ERR_RESOURCE))
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400902 return rpc_garbage_args;
903
Andy Adamsonc36fca52011-01-06 02:04:32 +0000904 if (hdr_arg.minorversion == 0) {
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400905 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
Andy Adamson778be232011-01-25 15:38:01 +0000906 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
Chuck Levera4e187d2016-06-29 13:55:22 -0400907 goto out_invalidcred;
Andy Adamson778be232011-01-25 15:38:01 +0000908 }
Andy Adamsonc36fca52011-01-06 02:04:32 +0000909
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400910 cps.minorversion = hdr_arg.minorversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 hdr_res.taglen = hdr_arg.taglen;
912 hdr_res.tag = hdr_arg.tag;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400913 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
914 return rpc_system_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400916 while (status == 0 && nops != hdr_arg.nops) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400917 status = process_op(nops, rqstp, &xdr_in,
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200918 rqstp->rq_argp, &xdr_out, rqstp->rq_resp,
919 &cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 nops++;
921 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400922
Andy Adamson31d2b432010-01-14 17:45:04 -0500923 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
924 * resource error in cb_compound status without returning op */
925 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
926 status = htonl(NFS4ERR_RESOURCE);
927 nops--;
928 }
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *hdr_res.status = status;
931 *hdr_res.nops = htonl(nops);
Trond Myklebust55a67392011-08-02 14:46:29 -0400932 nfs4_cb_free_slot(&cps);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000933 nfs_put_client(cps.clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return rpc_success;
Chuck Levera4e187d2016-06-29 13:55:22 -0400935
936out_invalidcred:
937 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
938 return rpc_autherr_badcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941/*
942 * Define NFS4 callback COMPOUND ops.
943 */
944static struct callback_op callback_ops[] = {
945 [0] = {
946 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
947 },
948 [OP_CB_GETATTR] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200949 .process_op = nfs4_callback_getattr,
950 .decode_args = decode_getattr_args,
951 .encode_res = encode_getattr_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
953 },
954 [OP_CB_RECALL] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200955 .process_op = nfs4_callback_recall,
956 .decode_args = decode_recall_args,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400958 },
959#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +0000960 [OP_CB_LAYOUTRECALL] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200961 .process_op = nfs4_callback_layoutrecall,
962 .decode_args = decode_layoutrecall_args,
Fred Isamanf2a62562011-01-06 11:36:29 +0000963 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
964 },
Marc Eshel1be56832011-05-22 19:47:09 +0300965 [OP_CB_NOTIFY_DEVICEID] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200966 .process_op = nfs4_callback_devicenotify,
967 .decode_args = decode_devicenotify_args,
Marc Eshel1be56832011-05-22 19:47:09 +0300968 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
969 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400970 [OP_CB_SEQUENCE] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200971 .process_op = nfs4_callback_sequence,
972 .decode_args = decode_cb_sequence_args,
973 .encode_res = encode_cb_sequence_res,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400974 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
975 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500976 [OP_CB_RECALL_ANY] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200977 .process_op = nfs4_callback_recallany,
978 .decode_args = decode_recallany_args,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500979 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
980 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500981 [OP_CB_RECALL_SLOT] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200982 .process_op = nfs4_callback_recallslot,
983 .decode_args = decode_recallslot_args,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500984 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
985 },
Jeff Laytondb783682016-09-17 18:17:36 -0400986 [OP_CB_NOTIFY_LOCK] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200987 .process_op = nfs4_callback_notify_lock,
988 .decode_args = decode_notify_lock_args,
Jeff Laytondb783682016-09-17 18:17:36 -0400989 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
990 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400991#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992};
993
994/*
995 * Define NFS4 callback procedures
996 */
Christoph Hellwig860bda22017-05-12 16:11:49 +0200997static const struct svc_procedure nfs4_callback_procedures1[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 [CB_NULL] = {
999 .pc_func = nfs4_callback_null,
Christoph Hellwig026fec72017-05-08 19:01:48 +02001000 .pc_decode = nfs4_decode_void,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001001 .pc_encode = nfs4_encode_void,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 .pc_xdrressize = 1,
1003 },
1004 [CB_COMPOUND] = {
1005 .pc_func = nfs4_callback_compound,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001006 .pc_encode = nfs4_encode_void,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 .pc_argsize = 256,
1008 .pc_ressize = 256,
1009 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1010 }
1011};
1012
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001013static unsigned int nfs4_callback_count1[ARRAY_SIZE(nfs4_callback_procedures1)];
Christoph Hellwige9679182017-05-12 16:21:37 +02001014const struct svc_version nfs4_callback_version1 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 .vs_vers = 1,
1016 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1017 .vs_proc = nfs4_callback_procedures1,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001018 .vs_count = nfs4_callback_count1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1020 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001021 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001022 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023};
1024
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001025static unsigned int nfs4_callback_count4[ARRAY_SIZE(nfs4_callback_procedures1)];
Christoph Hellwige9679182017-05-12 16:21:37 +02001026const struct svc_version nfs4_callback_version4 = {
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001027 .vs_vers = 4,
1028 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1029 .vs_proc = nfs4_callback_procedures1,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001030 .vs_count = nfs4_callback_count4,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001031 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1032 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001033 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001034 .vs_need_cong_ctrl = true,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001035};