blob: ecd46b8c098517a83aace2d3556f59477d280af6 [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 Adamson774d5f12013-05-20 14:13:50 -0400764 nfs4_slot_tbl_drain_complete(tbl);
Andy Adamson42acd022011-01-06 02:04:34 +0000765 spin_unlock(&tbl->slot_tbl_lock);
766}
767
Trond Myklebust55a67392011-08-02 14:46:29 -0400768static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000769{
Trond Myklebust810d82e2016-01-23 15:18:18 -0500770 if (cps->slot) {
771 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
772 cps->slot = NULL;
773 }
Andy Adamson42acd022011-01-06 02:04:34 +0000774}
775
Benny Halevy34bc47c92009-04-01 09:23:22 -0400776#else /* CONFIG_NFS_V4_1 */
777
778static __be32
779preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
780{
781 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
782}
783
Trond Myklebust55a67392011-08-02 14:46:29 -0400784static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000785{
786}
Benny Halevy34bc47c92009-04-01 09:23:22 -0400787#endif /* CONFIG_NFS_V4_1 */
788
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400789#ifdef CONFIG_NFS_V4_2
790static __be32
791preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
792{
793 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
794 if (status != htonl(NFS4ERR_OP_ILLEGAL))
795 return status;
796
797 if (op_nr == OP_CB_OFFLOAD)
798 return htonl(NFS4ERR_NOTSUPP);
799 return htonl(NFS4ERR_OP_ILLEGAL);
800}
801#else /* CONFIG_NFS_V4_2 */
802static __be32
803preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
804{
805 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
806}
807#endif /* CONFIG_NFS_V4_2 */
808
Benny Halevy34bc47c92009-04-01 09:23:22 -0400809static __be32
810preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
811{
812 switch (op_nr) {
813 case OP_CB_GETATTR:
814 case OP_CB_RECALL:
815 *op = &callback_ops[op_nr];
816 break;
817 default:
818 return htonl(NFS4ERR_OP_ILLEGAL);
819 }
820
821 return htonl(NFS_OK);
822}
823
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400824static __be32 process_op(int nop, struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct xdr_stream *xdr_in, void *argp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000826 struct xdr_stream *xdr_out, void *resp,
827 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500829 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500830 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400831 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700833 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500836 if (unlikely(status))
837 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400839 switch (cps->minorversion) {
840 case 0:
841 status = preprocess_nfs4_op(op_nr, &op);
842 break;
843 case 1:
844 status = preprocess_nfs41_op(nop, op_nr, &op);
845 break;
846 case 2:
847 status = preprocess_nfs42_op(nop, op_nr, &op);
848 break;
849 default:
850 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
851 }
852
Benny Halevy34bc47c92009-04-01 09:23:22 -0400853 if (status == htonl(NFS4ERR_OP_ILLEGAL))
854 op_nr = OP_CB_ILLEGAL;
Andy Adamsonb92b3012010-01-14 17:45:05 -0500855 if (status)
856 goto encode_hdr;
Andy Adamson31d2b432010-01-14 17:45:04 -0500857
Andy Adamsonc36fca52011-01-06 02:04:32 +0000858 if (cps->drc_status) {
859 status = cps->drc_status;
Andy Adamson49110962010-01-14 17:45:08 -0500860 goto encode_hdr;
861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 maxlen = xdr_out->end - xdr_out->p;
864 if (maxlen > 0 && maxlen < PAGE_SIZE) {
Andy Adamsone95e60d2010-01-14 17:45:06 -0500865 status = op->decode_args(rqstp, xdr_in, argp);
866 if (likely(status == 0))
Andy Adamsonc36fca52011-01-06 02:04:32 +0000867 status = op->process_op(argp, resp, cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 } else
869 status = htonl(NFS4ERR_RESOURCE);
870
Andy Adamsonb92b3012010-01-14 17:45:05 -0500871encode_hdr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 res = encode_op_hdr(xdr_out, op_nr, status);
Andy Adamson31d2b432010-01-14 17:45:04 -0500873 if (unlikely(res))
874 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (op->encode_res != NULL && status == 0)
876 status = op->encode_res(rqstp, xdr_out, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return status;
878}
879
880/*
881 * Decode, process and encode a COMPOUND
882 */
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200883static __be32 nfs4_callback_compound(struct svc_rqst *rqstp)
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
Trond Myklebust756b9b32015-12-07 12:52:23 -0800896 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Al Viro5704fde2006-10-19 23:28:51 -0700898 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
900
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400901 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
Vaishali Thakkar4ed0d832015-06-10 20:15:29 +0530902 if (status == htonl(NFS4ERR_RESOURCE))
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400903 return rpc_garbage_args;
904
Andy Adamsonc36fca52011-01-06 02:04:32 +0000905 if (hdr_arg.minorversion == 0) {
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400906 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
Andy Adamson778be232011-01-25 15:38:01 +0000907 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
Chuck Levera4e187d2016-06-29 13:55:22 -0400908 goto out_invalidcred;
Andy Adamson778be232011-01-25 15:38:01 +0000909 }
Andy Adamsonc36fca52011-01-06 02:04:32 +0000910
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400911 cps.minorversion = hdr_arg.minorversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 hdr_res.taglen = hdr_arg.taglen;
913 hdr_res.tag = hdr_arg.tag;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400914 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
915 return rpc_system_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400917 while (status == 0 && nops != hdr_arg.nops) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400918 status = process_op(nops, rqstp, &xdr_in,
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200919 rqstp->rq_argp, &xdr_out, rqstp->rq_resp,
920 &cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 nops++;
922 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400923
Andy Adamson31d2b432010-01-14 17:45:04 -0500924 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
925 * resource error in cb_compound status without returning op */
926 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
927 status = htonl(NFS4ERR_RESOURCE);
928 nops--;
929 }
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 *hdr_res.status = status;
932 *hdr_res.nops = htonl(nops);
Trond Myklebust55a67392011-08-02 14:46:29 -0400933 nfs4_cb_free_slot(&cps);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000934 nfs_put_client(cps.clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return rpc_success;
Chuck Levera4e187d2016-06-29 13:55:22 -0400936
937out_invalidcred:
938 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
939 return rpc_autherr_badcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942/*
943 * Define NFS4 callback COMPOUND ops.
944 */
945static struct callback_op callback_ops[] = {
946 [0] = {
947 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
948 },
949 [OP_CB_GETATTR] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200950 .process_op = nfs4_callback_getattr,
951 .decode_args = decode_getattr_args,
952 .encode_res = encode_getattr_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
954 },
955 [OP_CB_RECALL] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200956 .process_op = nfs4_callback_recall,
957 .decode_args = decode_recall_args,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400959 },
960#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +0000961 [OP_CB_LAYOUTRECALL] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200962 .process_op = nfs4_callback_layoutrecall,
963 .decode_args = decode_layoutrecall_args,
Fred Isamanf2a62562011-01-06 11:36:29 +0000964 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
965 },
Marc Eshel1be56832011-05-22 19:47:09 +0300966 [OP_CB_NOTIFY_DEVICEID] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200967 .process_op = nfs4_callback_devicenotify,
968 .decode_args = decode_devicenotify_args,
Marc Eshel1be56832011-05-22 19:47:09 +0300969 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
970 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400971 [OP_CB_SEQUENCE] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200972 .process_op = nfs4_callback_sequence,
973 .decode_args = decode_cb_sequence_args,
974 .encode_res = encode_cb_sequence_res,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400975 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
976 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500977 [OP_CB_RECALL_ANY] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200978 .process_op = nfs4_callback_recallany,
979 .decode_args = decode_recallany_args,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500980 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
981 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500982 [OP_CB_RECALL_SLOT] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200983 .process_op = nfs4_callback_recallslot,
984 .decode_args = decode_recallslot_args,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500985 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
986 },
Jeff Laytondb783682016-09-17 18:17:36 -0400987 [OP_CB_NOTIFY_LOCK] = {
Christoph Hellwigf4dac4a2017-05-11 09:22:18 +0200988 .process_op = nfs4_callback_notify_lock,
989 .decode_args = decode_notify_lock_args,
Jeff Laytondb783682016-09-17 18:17:36 -0400990 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
991 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400992#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993};
994
995/*
996 * Define NFS4 callback procedures
997 */
998static struct svc_procedure nfs4_callback_procedures1[] = {
999 [CB_NULL] = {
1000 .pc_func = nfs4_callback_null,
Christoph Hellwig026fec72017-05-08 19:01:48 +02001001 .pc_decode = nfs4_decode_void,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001002 .pc_encode = nfs4_encode_void,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 .pc_xdrressize = 1,
1004 },
1005 [CB_COMPOUND] = {
1006 .pc_func = nfs4_callback_compound,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001007 .pc_encode = nfs4_encode_void,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 .pc_argsize = 256,
1009 .pc_ressize = 256,
1010 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1011 }
1012};
1013
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001014static unsigned int nfs4_callback_count1[ARRAY_SIZE(nfs4_callback_procedures1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015struct svc_version nfs4_callback_version1 = {
1016 .vs_vers = 1,
1017 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1018 .vs_proc = nfs4_callback_procedures1,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001019 .vs_count = nfs4_callback_count1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1021 .vs_dispatch = NULL,
Jeff Layton05a45a22017-02-24 13:25:22 -05001022 .vs_hidden = true,
Jeff Layton5283b032017-02-24 13:25:24 -05001023 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024};
1025
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001026static unsigned int nfs4_callback_count4[ARRAY_SIZE(nfs4_callback_procedures1)];
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001027struct svc_version nfs4_callback_version4 = {
1028 .vs_vers = 4,
1029 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1030 .vs_proc = nfs4_callback_procedures1,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02001031 .vs_count = nfs4_callback_count4,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001032 .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};