blob: 09aeb9a47dac50f22ca01cac82f48544bc492f75 [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
21#define CB_OP_TAGLEN_MAXSZ (512)
22#define CB_OP_HDR_RES_MAXSZ (2 + CB_OP_TAGLEN_MAXSZ)
23#define CB_OP_GETATTR_BITMAP_MAXSZ (4)
24#define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
25 CB_OP_GETATTR_BITMAP_MAXSZ + \
26 2 + 2 + 3 + 3)
27#define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
28
Benny Halevy4aece6a2009-04-01 09:23:26 -040029#if defined(CONFIG_NFS_V4_1)
Fred Isamanf2a62562011-01-06 11:36:29 +000030#define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Marc Eshel1be56832011-05-22 19:47:09 +030031#define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Benny Halevy4aece6a2009-04-01 09:23:26 -040032#define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
33 4 + 1 + 3)
Alexandros Batsakis31f09602009-12-05 13:27:02 -050034#define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -050035#define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Benny Halevy4aece6a2009-04-01 09:23:26 -040036#endif /* CONFIG_NFS_V4_1 */
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define NFSDBG_FACILITY NFSDBG_CALLBACK
39
Andy Adamson31d2b432010-01-14 17:45:04 -050040/* Internal error code */
41#define NFS4ERR_RESOURCE_HDR 11050
42
Andy Adamsonc36fca52011-01-06 02:04:32 +000043typedef __be32 (*callback_process_op_t)(void *, void *,
44 struct cb_process_state *);
Al Viroe6f684f2006-10-19 23:28:50 -070045typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
46typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48
49struct callback_op {
50 callback_process_op_t process_op;
51 callback_decode_arg_t decode_args;
52 callback_encode_res_t encode_res;
53 long res_maxsize;
54};
55
56static struct callback_op callback_ops[];
57
Al Viro7111c662006-10-19 23:28:45 -070058static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 return htonl(NFS4_OK);
61}
62
Al Viro5704fde2006-10-19 23:28:51 -070063static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 return xdr_argsize_check(rqstp, p);
66}
67
Al Viro5704fde2006-10-19 23:28:51 -070068static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 return xdr_ressize_check(rqstp, p);
71}
72
Al Viro5704fde2006-10-19 23:28:51 -070073static __be32 *read_buf(struct xdr_stream *xdr, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Al Viro5704fde2006-10-19 23:28:51 -070075 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 p = xdr_inline_decode(xdr, nbytes);
78 if (unlikely(p == NULL))
Weston Andros Adamsona0308892012-01-26 13:32:23 -050079 printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return p;
81}
82
Al Viroe6f684f2006-10-19 23:28:50 -070083static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Al Viro5704fde2006-10-19 23:28:51 -070085 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 p = read_buf(xdr, 4);
88 if (unlikely(p == NULL))
89 return htonl(NFS4ERR_RESOURCE);
90 *len = ntohl(*p);
91
92 if (*len != 0) {
93 p = read_buf(xdr, *len);
94 if (unlikely(p == NULL))
95 return htonl(NFS4ERR_RESOURCE);
96 *str = (const char *)p;
97 } else
98 *str = NULL;
99
100 return 0;
101}
102
Al Viroe6f684f2006-10-19 23:28:50 -0700103static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Al Viro5704fde2006-10-19 23:28:51 -0700105 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 p = read_buf(xdr, 4);
108 if (unlikely(p == NULL))
109 return htonl(NFS4ERR_RESOURCE);
110 fh->size = ntohl(*p);
111 if (fh->size > NFS4_FHSIZE)
112 return htonl(NFS4ERR_BADHANDLE);
113 p = read_buf(xdr, fh->size);
114 if (unlikely(p == NULL))
115 return htonl(NFS4ERR_RESOURCE);
116 memcpy(&fh->data[0], p, fh->size);
117 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
118 return 0;
119}
120
Al Viroe6f684f2006-10-19 23:28:50 -0700121static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Al Viro5704fde2006-10-19 23:28:51 -0700123 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned int attrlen;
125
126 p = read_buf(xdr, 4);
127 if (unlikely(p == NULL))
128 return htonl(NFS4ERR_RESOURCE);
129 attrlen = ntohl(*p);
130 p = read_buf(xdr, attrlen << 2);
131 if (unlikely(p == NULL))
132 return htonl(NFS4ERR_RESOURCE);
133 if (likely(attrlen > 0))
134 bitmap[0] = ntohl(*p++);
135 if (attrlen > 1)
136 bitmap[1] = ntohl(*p);
137 return 0;
138}
139
Al Viroe6f684f2006-10-19 23:28:50 -0700140static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Al Viro5704fde2006-10-19 23:28:51 -0700142 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500144 p = read_buf(xdr, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (unlikely(p == NULL))
146 return htonl(NFS4ERR_RESOURCE);
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500147 memcpy(stateid, p, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
149}
150
Al Viroe6f684f2006-10-19 23:28:50 -0700151static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Al Viro5704fde2006-10-19 23:28:51 -0700153 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700154 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 status = decode_string(xdr, &hdr->taglen, &hdr->tag);
157 if (unlikely(status != 0))
158 return status;
159 /* We do not like overly long tags! */
Chuck Lever5cce4282007-10-26 13:33:01 -0400160 if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500161 printk("NFS: NFSv4 CALLBACK %s: client sent tag of length %u\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700162 __func__, hdr->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return htonl(NFS4ERR_RESOURCE);
164 }
165 p = read_buf(xdr, 12);
166 if (unlikely(p == NULL))
167 return htonl(NFS4ERR_RESOURCE);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400168 hdr->minorversion = ntohl(*p++);
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400169 /* Check for minor version support */
170 if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
Steve Dickson42c2c422013-05-22 12:50:38 -0400171 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
Benny Halevy48a9e2d2009-04-01 09:23:20 -0400172 } else {
Trond Myklebust9a3ba432012-03-12 18:01:48 -0400173 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400174 "illegal minor version %u!\n",
175 __func__, hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 hdr->nops = ntohl(*p);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400179 dprintk("%s: minorversion %d nops %d\n", __func__,
180 hdr->minorversion, hdr->nops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
182}
183
Al Viroe6f684f2006-10-19 23:28:50 -0700184static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Al Viro5704fde2006-10-19 23:28:51 -0700186 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 p = read_buf(xdr, 4);
188 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500189 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *op = ntohl(*p);
191 return 0;
192}
193
Al Viroe6f684f2006-10-19 23:28:50 -0700194static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Al Viroe6f684f2006-10-19 23:28:50 -0700196 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 status = decode_fh(xdr, &args->fh);
199 if (unlikely(status != 0))
200 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 status = decode_bitmap(xdr, args->bitmap);
202out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700203 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return status;
205}
206
Al Viroe6f684f2006-10-19 23:28:50 -0700207static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Al Viro5704fde2006-10-19 23:28:51 -0700209 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700210 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 status = decode_stateid(xdr, &args->stateid);
213 if (unlikely(status != 0))
214 goto out;
215 p = read_buf(xdr, 4);
216 if (unlikely(p == NULL)) {
217 status = htonl(NFS4ERR_RESOURCE);
218 goto out;
219 }
220 args->truncate = ntohl(*p);
221 status = decode_fh(xdr, &args->fh);
222out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700223 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Alexey Dobriyan3873bc52006-05-27 03:31:12 +0400224 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Benny Halevy4aece6a2009-04-01 09:23:26 -0400227#if defined(CONFIG_NFS_V4_1)
228
Fred Isamanf2a62562011-01-06 11:36:29 +0000229static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
230 struct xdr_stream *xdr,
231 struct cb_layoutrecallargs *args)
232{
233 __be32 *p;
234 __be32 status = 0;
235 uint32_t iomode;
236
Fred Isamanf2a62562011-01-06 11:36:29 +0000237 p = read_buf(xdr, 4 * sizeof(uint32_t));
238 if (unlikely(p == NULL)) {
239 status = htonl(NFS4ERR_BADXDR);
240 goto out;
241 }
242
243 args->cbl_layout_type = ntohl(*p++);
244 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300245 * as it is unusable and ignored with the other types.
Fred Isamanf2a62562011-01-06 11:36:29 +0000246 */
247 iomode = ntohl(*p++);
248 args->cbl_layoutchanged = ntohl(*p++);
249 args->cbl_recall_type = ntohl(*p++);
250
251 if (args->cbl_recall_type == RETURN_FILE) {
252 args->cbl_range.iomode = iomode;
253 status = decode_fh(xdr, &args->cbl_fh);
254 if (unlikely(status != 0))
255 goto out;
256
257 p = read_buf(xdr, 2 * sizeof(uint64_t));
258 if (unlikely(p == NULL)) {
259 status = htonl(NFS4ERR_BADXDR);
260 goto out;
261 }
262 p = xdr_decode_hyper(p, &args->cbl_range.offset);
263 p = xdr_decode_hyper(p, &args->cbl_range.length);
264 status = decode_stateid(xdr, &args->cbl_stateid);
265 if (unlikely(status != 0))
266 goto out;
267 } else if (args->cbl_recall_type == RETURN_FSID) {
268 p = read_buf(xdr, 2 * sizeof(uint64_t));
269 if (unlikely(p == NULL)) {
270 status = htonl(NFS4ERR_BADXDR);
271 goto out;
272 }
273 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
274 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
275 } else if (args->cbl_recall_type != RETURN_ALL) {
276 status = htonl(NFS4ERR_BADXDR);
277 goto out;
278 }
279 dprintk("%s: ltype 0x%x iomode %d changed %d recall_type %d\n",
280 __func__,
281 args->cbl_layout_type, iomode,
282 args->cbl_layoutchanged, args->cbl_recall_type);
283out:
284 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
285 return status;
286}
287
Marc Eshel1be56832011-05-22 19:47:09 +0300288static
289__be32 decode_devicenotify_args(struct svc_rqst *rqstp,
290 struct xdr_stream *xdr,
291 struct cb_devicenotifyargs *args)
292{
293 __be32 *p;
294 __be32 status = 0;
295 u32 tmp;
296 int n, i;
297 args->ndevs = 0;
298
299 /* Num of device notifications */
300 p = read_buf(xdr, sizeof(uint32_t));
301 if (unlikely(p == NULL)) {
302 status = htonl(NFS4ERR_BADXDR);
303 goto out;
304 }
305 n = ntohl(*p++);
306 if (n <= 0)
307 goto out;
Dan Carpenter363e0df02012-01-12 10:16:14 +0300308 if (n > ULONG_MAX / sizeof(*args->devs)) {
309 status = htonl(NFS4ERR_BADXDR);
310 goto out;
311 }
Marc Eshel1be56832011-05-22 19:47:09 +0300312
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500313 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
Marc Eshel1be56832011-05-22 19:47:09 +0300314 if (!args->devs) {
315 status = htonl(NFS4ERR_DELAY);
316 goto out;
317 }
318
319 /* Decode each dev notification */
320 for (i = 0; i < n; i++) {
321 struct cb_devicenotifyitem *dev = &args->devs[i];
322
323 p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
324 if (unlikely(p == NULL)) {
325 status = htonl(NFS4ERR_BADXDR);
326 goto err;
327 }
328
329 tmp = ntohl(*p++); /* bitmap size */
330 if (tmp != 1) {
331 status = htonl(NFS4ERR_INVAL);
332 goto err;
333 }
334 dev->cbd_notify_type = ntohl(*p++);
335 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
336 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
337 status = htonl(NFS4ERR_INVAL);
338 goto err;
339 }
340
341 tmp = ntohl(*p++); /* opaque size */
342 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
343 (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
344 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
345 (tmp != NFS4_DEVICEID4_SIZE + 4))) {
346 status = htonl(NFS4ERR_INVAL);
347 goto err;
348 }
349 dev->cbd_layout_type = ntohl(*p++);
350 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
351 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
352
353 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
354 p = read_buf(xdr, sizeof(uint32_t));
355 if (unlikely(p == NULL)) {
356 status = htonl(NFS4ERR_BADXDR);
357 goto err;
358 }
359 dev->cbd_immediate = ntohl(*p++);
360 } else {
361 dev->cbd_immediate = 0;
362 }
363
364 args->ndevs++;
365
366 dprintk("%s: type %d layout 0x%x immediate %d\n",
367 __func__, dev->cbd_notify_type, dev->cbd_layout_type,
368 dev->cbd_immediate);
369 }
370out:
371 dprintk("%s: status %d ndevs %d\n",
372 __func__, ntohl(status), args->ndevs);
373 return status;
374err:
375 kfree(args->devs);
376 goto out;
377}
378
Andy Adamson9733f0d2010-01-22 12:03:08 -0500379static __be32 decode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400380 struct nfs4_sessionid *sid)
381{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500382 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400383
Kinglong Mee590184a2015-09-24 20:57:37 +0800384 p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400385 if (unlikely(p == NULL))
Joe Perchesa419aef2009-08-18 11:18:35 -0700386 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400387
Kinglong Mee590184a2015-09-24 20:57:37 +0800388 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400389 return 0;
390}
391
Andy Adamson9733f0d2010-01-22 12:03:08 -0500392static __be32 decode_rc_list(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400393 struct referring_call_list *rc_list)
394{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500395 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400396 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500397 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400398
399 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
400 if (status)
401 goto out;
402
403 status = htonl(NFS4ERR_RESOURCE);
404 p = read_buf(xdr, sizeof(uint32_t));
405 if (unlikely(p == NULL))
406 goto out;
407
408 rc_list->rcl_nrefcalls = ntohl(*p++);
409 if (rc_list->rcl_nrefcalls) {
410 p = read_buf(xdr,
411 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
412 if (unlikely(p == NULL))
413 goto out;
Trond Myklebusta4f743a2015-02-11 17:49:13 -0500414 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400415 sizeof(*rc_list->rcl_refcalls),
416 GFP_KERNEL);
417 if (unlikely(rc_list->rcl_refcalls == NULL))
418 goto out;
419 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
420 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
421 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
422 }
423 }
424 status = 0;
425
426out:
427 return status;
428}
429
Andy Adamson9733f0d2010-01-22 12:03:08 -0500430static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400431 struct xdr_stream *xdr,
432 struct cb_sequenceargs *args)
433{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500434 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400435 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500436 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400437
438 status = decode_sessionid(xdr, &args->csa_sessionid);
439 if (status)
440 goto out;
441
442 status = htonl(NFS4ERR_RESOURCE);
443 p = read_buf(xdr, 5 * sizeof(uint32_t));
444 if (unlikely(p == NULL))
445 goto out;
446
Ricardo Labiaga65fc64e2009-04-01 09:23:30 -0400447 args->csa_addr = svc_addr(rqstp);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400448 args->csa_sequenceid = ntohl(*p++);
449 args->csa_slotid = ntohl(*p++);
450 args->csa_highestslotid = ntohl(*p++);
451 args->csa_cachethis = ntohl(*p++);
452 args->csa_nrclists = ntohl(*p++);
453 args->csa_rclists = NULL;
454 if (args->csa_nrclists) {
Dan Carpenter0439f312012-06-12 10:37:08 +0300455 args->csa_rclists = kmalloc_array(args->csa_nrclists,
456 sizeof(*args->csa_rclists),
457 GFP_KERNEL);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400458 if (unlikely(args->csa_rclists == NULL))
459 goto out;
460
461 for (i = 0; i < args->csa_nrclists; i++) {
462 status = decode_rc_list(xdr, &args->csa_rclists[i]);
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500463 if (status) {
464 args->csa_nrclists = i;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400465 goto out_free;
Trond Myklebustd8ba1f92015-02-11 17:27:55 -0500466 }
Benny Halevy4aece6a2009-04-01 09:23:26 -0400467 }
468 }
469 status = 0;
470
471 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
472 "highestslotid %u cachethis %d nrclists %u\n",
473 __func__,
474 ((u32 *)&args->csa_sessionid)[0],
475 ((u32 *)&args->csa_sessionid)[1],
476 ((u32 *)&args->csa_sessionid)[2],
477 ((u32 *)&args->csa_sessionid)[3],
478 args->csa_sequenceid, args->csa_slotid,
479 args->csa_highestslotid, args->csa_cachethis,
480 args->csa_nrclists);
481out:
482 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
483 return status;
484
485out_free:
486 for (i = 0; i < args->csa_nrclists; i++)
487 kfree(args->csa_rclists[i].rcl_refcalls);
488 kfree(args->csa_rclists);
489 goto out;
490}
491
Andy Adamson9733f0d2010-01-22 12:03:08 -0500492static __be32 decode_recallany_args(struct svc_rqst *rqstp,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500493 struct xdr_stream *xdr,
494 struct cb_recallanyargs *args)
495{
Peng Taod743c3c2011-10-23 20:22:38 -0700496 uint32_t bitmap[2];
497 __be32 *p, status;
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500498
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500499 p = read_buf(xdr, 4);
500 if (unlikely(p == NULL))
501 return htonl(NFS4ERR_BADXDR);
502 args->craa_objs_to_keep = ntohl(*p++);
Peng Taod743c3c2011-10-23 20:22:38 -0700503 status = decode_bitmap(xdr, bitmap);
504 if (unlikely(status))
505 return status;
506 args->craa_type_mask = bitmap[0];
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500507
508 return 0;
509}
510
Andy Adamson9733f0d2010-01-22 12:03:08 -0500511static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500512 struct xdr_stream *xdr,
513 struct cb_recallslotargs *args)
514{
515 __be32 *p;
516
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500517 p = read_buf(xdr, 4);
518 if (unlikely(p == NULL))
519 return htonl(NFS4ERR_BADXDR);
Trond Myklebustd5fb4ce2012-11-20 20:24:02 -0500520 args->crsa_target_highest_slotid = ntohl(*p++);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500521 return 0;
522}
523
Benny Halevy4aece6a2009-04-01 09:23:26 -0400524#endif /* CONFIG_NFS_V4_1 */
525
Al Viroe6f684f2006-10-19 23:28:50 -0700526static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Al Viro5704fde2006-10-19 23:28:51 -0700528 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 p = xdr_reserve_space(xdr, 4 + len);
531 if (unlikely(p == NULL))
532 return htonl(NFS4ERR_RESOURCE);
533 xdr_encode_opaque(p, str, len);
534 return 0;
535}
536
537#define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
538#define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
Al Viro5704fde2006-10-19 23:28:51 -0700539static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Al Viro5704fde2006-10-19 23:28:51 -0700541 __be32 bm[2];
542 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
545 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
546 if (bm[1] != 0) {
547 p = xdr_reserve_space(xdr, 16);
548 if (unlikely(p == NULL))
549 return htonl(NFS4ERR_RESOURCE);
550 *p++ = htonl(2);
551 *p++ = bm[0];
552 *p++ = bm[1];
553 } else if (bm[0] != 0) {
554 p = xdr_reserve_space(xdr, 12);
555 if (unlikely(p == NULL))
556 return htonl(NFS4ERR_RESOURCE);
557 *p++ = htonl(1);
558 *p++ = bm[0];
559 } else {
560 p = xdr_reserve_space(xdr, 8);
561 if (unlikely(p == NULL))
562 return htonl(NFS4ERR_RESOURCE);
563 *p++ = htonl(0);
564 }
565 *savep = p;
566 return 0;
567}
568
Al Viroe6f684f2006-10-19 23:28:50 -0700569static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Al Viro5704fde2006-10-19 23:28:51 -0700571 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
574 return 0;
575 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800576 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return htonl(NFS4ERR_RESOURCE);
578 p = xdr_encode_hyper(p, change);
579 return 0;
580}
581
Al Viroe6f684f2006-10-19 23:28:50 -0700582static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Al Viro5704fde2006-10-19 23:28:51 -0700584 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
587 return 0;
588 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800589 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return htonl(NFS4ERR_RESOURCE);
591 p = xdr_encode_hyper(p, size);
592 return 0;
593}
594
Al Viroe6f684f2006-10-19 23:28:50 -0700595static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Al Viro5704fde2006-10-19 23:28:51 -0700597 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 p = xdr_reserve_space(xdr, 12);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800600 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return htonl(NFS4ERR_RESOURCE);
602 p = xdr_encode_hyper(p, time->tv_sec);
603 *p = htonl(time->tv_nsec);
604 return 0;
605}
606
Al Viroe6f684f2006-10-19 23:28:50 -0700607static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
610 return 0;
611 return encode_attr_time(xdr,time);
612}
613
Al Viroe6f684f2006-10-19 23:28:50 -0700614static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
617 return 0;
618 return encode_attr_time(xdr,time);
619}
620
Al Viroe6f684f2006-10-19 23:28:50 -0700621static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Al Viroe6f684f2006-10-19 23:28:50 -0700623 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 hdr->status = xdr_reserve_space(xdr, 4);
626 if (unlikely(hdr->status == NULL))
627 return htonl(NFS4ERR_RESOURCE);
628 status = encode_string(xdr, hdr->taglen, hdr->tag);
629 if (unlikely(status != 0))
630 return status;
631 hdr->nops = xdr_reserve_space(xdr, 4);
632 if (unlikely(hdr->nops == NULL))
633 return htonl(NFS4ERR_RESOURCE);
634 return 0;
635}
636
Al Viroe6f684f2006-10-19 23:28:50 -0700637static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Al Viro5704fde2006-10-19 23:28:51 -0700639 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 p = xdr_reserve_space(xdr, 8);
642 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500643 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 *p++ = htonl(op);
645 *p = res;
646 return 0;
647}
648
Al Viroe6f684f2006-10-19 23:28:50 -0700649static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Al Viro5704fde2006-10-19 23:28:51 -0700651 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700652 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (unlikely(status != 0))
655 goto out;
656 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
657 if (unlikely(status != 0))
658 goto out;
659 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
660 if (unlikely(status != 0))
661 goto out;
662 status = encode_attr_size(xdr, res->bitmap, res->size);
663 if (unlikely(status != 0))
664 goto out;
665 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
666 if (unlikely(status != 0))
667 goto out;
668 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
669 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
670out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700671 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return status;
673}
674
Benny Halevy34bc47c92009-04-01 09:23:22 -0400675#if defined(CONFIG_NFS_V4_1)
676
Andy Adamson9733f0d2010-01-22 12:03:08 -0500677static __be32 encode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400678 const struct nfs4_sessionid *sid)
679{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500680 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400681
Kinglong Mee590184a2015-09-24 20:57:37 +0800682 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400683 if (unlikely(p == NULL))
684 return htonl(NFS4ERR_RESOURCE);
685
Kinglong Mee590184a2015-09-24 20:57:37 +0800686 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400687 return 0;
688}
689
Andy Adamson9733f0d2010-01-22 12:03:08 -0500690static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400691 struct xdr_stream *xdr,
692 const struct cb_sequenceres *res)
693{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500694 __be32 *p;
Dan Carpentere216c8c2012-06-12 10:37:39 +0300695 __be32 status = res->csr_status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400696
697 if (unlikely(status != 0))
698 goto out;
699
700 encode_sessionid(xdr, &res->csr_sessionid);
701
702 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
703 if (unlikely(p == NULL))
704 return htonl(NFS4ERR_RESOURCE);
705
706 *p++ = htonl(res->csr_sequenceid);
707 *p++ = htonl(res->csr_slotid);
708 *p++ = htonl(res->csr_highestslotid);
709 *p++ = htonl(res->csr_target_highestslotid);
710out:
711 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
712 return status;
713}
714
Benny Halevy34bc47c92009-04-01 09:23:22 -0400715static __be32
716preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
717{
Benny Halevy281fe152009-04-01 09:23:27 -0400718 if (op_nr == OP_CB_SEQUENCE) {
719 if (nop != 0)
720 return htonl(NFS4ERR_SEQUENCE_POS);
721 } else {
722 if (nop == 0)
723 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
724 }
725
Benny Halevy34bc47c92009-04-01 09:23:22 -0400726 switch (op_nr) {
727 case OP_CB_GETATTR:
728 case OP_CB_RECALL:
Benny Halevy4aece6a2009-04-01 09:23:26 -0400729 case OP_CB_SEQUENCE:
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500730 case OP_CB_RECALL_ANY:
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500731 case OP_CB_RECALL_SLOT:
Fred Isamanf2a62562011-01-06 11:36:29 +0000732 case OP_CB_LAYOUTRECALL:
Marc Eshel1be56832011-05-22 19:47:09 +0300733 case OP_CB_NOTIFY_DEVICEID:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400734 *op = &callback_ops[op_nr];
735 break;
736
Benny Halevy34bc47c92009-04-01 09:23:22 -0400737 case OP_CB_NOTIFY:
738 case OP_CB_PUSH_DELEG:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400739 case OP_CB_RECALLABLE_OBJ_AVAIL:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400740 case OP_CB_WANTS_CANCELLED:
741 case OP_CB_NOTIFY_LOCK:
742 return htonl(NFS4ERR_NOTSUPP);
743
744 default:
745 return htonl(NFS4ERR_OP_ILLEGAL);
746 }
747
748 return htonl(NFS_OK);
749}
750
Andy Adamson42acd022011-01-06 02:04:34 +0000751static void nfs4_callback_free_slot(struct nfs4_session *session)
752{
753 struct nfs4_slot_table *tbl = &session->bc_slot_table;
754
755 spin_lock(&tbl->slot_tbl_lock);
756 /*
757 * Let the state manager know callback processing done.
758 * A single slot, so highest used slotid is either 0 or -1
759 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500760 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson774d5f12013-05-20 14:13:50 -0400761 nfs4_slot_tbl_drain_complete(tbl);
Andy Adamson42acd022011-01-06 02:04:34 +0000762 spin_unlock(&tbl->slot_tbl_lock);
763}
764
Trond Myklebust55a67392011-08-02 14:46:29 -0400765static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000766{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500767 if (cps->slotid != NFS4_NO_SLOT)
Trond Myklebust55a67392011-08-02 14:46:29 -0400768 nfs4_callback_free_slot(cps->clp->cl_session);
Andy Adamson42acd022011-01-06 02:04:34 +0000769}
770
Benny Halevy34bc47c92009-04-01 09:23:22 -0400771#else /* CONFIG_NFS_V4_1 */
772
773static __be32
774preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
775{
776 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
777}
778
Trond Myklebust55a67392011-08-02 14:46:29 -0400779static void nfs4_cb_free_slot(struct cb_process_state *cps)
Andy Adamson42acd022011-01-06 02:04:34 +0000780{
781}
Benny Halevy34bc47c92009-04-01 09:23:22 -0400782#endif /* CONFIG_NFS_V4_1 */
783
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400784#ifdef CONFIG_NFS_V4_2
785static __be32
786preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
787{
788 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
789 if (status != htonl(NFS4ERR_OP_ILLEGAL))
790 return status;
791
792 if (op_nr == OP_CB_OFFLOAD)
793 return htonl(NFS4ERR_NOTSUPP);
794 return htonl(NFS4ERR_OP_ILLEGAL);
795}
796#else /* CONFIG_NFS_V4_2 */
797static __be32
798preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
799{
800 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
801}
802#endif /* CONFIG_NFS_V4_2 */
803
Benny Halevy34bc47c92009-04-01 09:23:22 -0400804static __be32
805preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
806{
807 switch (op_nr) {
808 case OP_CB_GETATTR:
809 case OP_CB_RECALL:
810 *op = &callback_ops[op_nr];
811 break;
812 default:
813 return htonl(NFS4ERR_OP_ILLEGAL);
814 }
815
816 return htonl(NFS_OK);
817}
818
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400819static __be32 process_op(int nop, struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct xdr_stream *xdr_in, void *argp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000821 struct xdr_stream *xdr_out, void *resp,
822 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500824 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500825 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400826 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700828 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Harvey Harrison3110ff82008-05-02 13:42:44 -0700830 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500832 if (unlikely(status))
833 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Benny Halevy34bc47c92009-04-01 09:23:22 -0400835 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400836 __func__, cps->minorversion, nop, op_nr);
Benny Halevy34bc47c92009-04-01 09:23:22 -0400837
Bryan Schumaker6b140b82013-06-05 11:15:02 -0400838 switch (cps->minorversion) {
839 case 0:
840 status = preprocess_nfs4_op(op_nr, &op);
841 break;
842 case 1:
843 status = preprocess_nfs41_op(nop, op_nr, &op);
844 break;
845 case 2:
846 status = preprocess_nfs42_op(nop, op_nr, &op);
847 break;
848 default:
849 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
850 }
851
Benny Halevy34bc47c92009-04-01 09:23:22 -0400852 if (status == htonl(NFS4ERR_OP_ILLEGAL))
853 op_nr = OP_CB_ILLEGAL;
Andy Adamsonb92b3012010-01-14 17:45:05 -0500854 if (status)
855 goto encode_hdr;
Andy Adamson31d2b432010-01-14 17:45:04 -0500856
Andy Adamsonc36fca52011-01-06 02:04:32 +0000857 if (cps->drc_status) {
858 status = cps->drc_status;
Andy Adamson49110962010-01-14 17:45:08 -0500859 goto encode_hdr;
860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 maxlen = xdr_out->end - xdr_out->p;
863 if (maxlen > 0 && maxlen < PAGE_SIZE) {
Andy Adamsone95e60d2010-01-14 17:45:06 -0500864 status = op->decode_args(rqstp, xdr_in, argp);
865 if (likely(status == 0))
Andy Adamsonc36fca52011-01-06 02:04:32 +0000866 status = op->process_op(argp, resp, cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 } else
868 status = htonl(NFS4ERR_RESOURCE);
869
Andy Adamsonb92b3012010-01-14 17:45:05 -0500870encode_hdr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 res = encode_op_hdr(xdr_out, op_nr, status);
Andy Adamson31d2b432010-01-14 17:45:04 -0500872 if (unlikely(res))
873 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (op->encode_res != NULL && status == 0)
875 status = op->encode_res(rqstp, xdr_out, resp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700876 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return status;
878}
879
880/*
881 * Decode, process and encode a COMPOUND
882 */
Al Viro7111c662006-10-19 23:28:45 -0700883static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400885 struct cb_compound_hdr_arg hdr_arg = { 0 };
886 struct cb_compound_hdr_res hdr_res = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct xdr_stream xdr_in, xdr_out;
Andy Adamsonc36fca52011-01-06 02:04:32 +0000888 __be32 *p, status;
889 struct cb_process_state cps = {
890 .drc_status = 0,
891 .clp = NULL,
Trond Myklebust45d43c22012-02-06 19:38:51 -0500892 .slotid = NFS4_NO_SLOT,
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400893 .net = SVC_NET(rqstp),
Andy Adamsonc36fca52011-01-06 02:04:32 +0000894 };
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400895 unsigned int nops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Harvey Harrison3110ff82008-05-02 13:42:44 -0700897 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
900
Al Viro5704fde2006-10-19 23:28:51 -0700901 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
903
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400904 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
Vaishali Thakkar4ed0d832015-06-10 20:15:29 +0530905 if (status == htonl(NFS4ERR_RESOURCE))
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400906 return rpc_garbage_args;
907
Andy Adamsonc36fca52011-01-06 02:04:32 +0000908 if (hdr_arg.minorversion == 0) {
Stanislav Kinsbursky9695c702012-07-25 16:57:06 +0400909 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
Andy Adamson778be232011-01-25 15:38:01 +0000910 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
Andy Adamsonc36fca52011-01-06 02:04:32 +0000911 return rpc_drop_reply;
Andy Adamson778be232011-01-25 15:38:01 +0000912 }
Andy Adamsonc36fca52011-01-06 02:04:32 +0000913
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400914 cps.minorversion = hdr_arg.minorversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 hdr_res.taglen = hdr_arg.taglen;
916 hdr_res.tag = hdr_arg.tag;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400917 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
918 return rpc_system_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400920 while (status == 0 && nops != hdr_arg.nops) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400921 status = process_op(nops, rqstp, &xdr_in,
922 argp, &xdr_out, resp, &cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 nops++;
924 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400925
Andy Adamson31d2b432010-01-14 17:45:04 -0500926 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
927 * resource error in cb_compound status without returning op */
928 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
929 status = htonl(NFS4ERR_RESOURCE);
930 nops--;
931 }
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 *hdr_res.status = status;
934 *hdr_res.nops = htonl(nops);
Trond Myklebust55a67392011-08-02 14:46:29 -0400935 nfs4_cb_free_slot(&cps);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000936 nfs_put_client(cps.clp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700937 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return rpc_success;
939}
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] = {
949 .process_op = (callback_process_op_t)nfs4_callback_getattr,
950 .decode_args = (callback_decode_arg_t)decode_getattr_args,
951 .encode_res = (callback_encode_res_t)encode_getattr_res,
952 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
953 },
954 [OP_CB_RECALL] = {
955 .process_op = (callback_process_op_t)nfs4_callback_recall,
956 .decode_args = (callback_decode_arg_t)decode_recall_args,
957 .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] = {
961 .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
962 .decode_args =
963 (callback_decode_arg_t)decode_layoutrecall_args,
964 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
965 },
Marc Eshel1be56832011-05-22 19:47:09 +0300966 [OP_CB_NOTIFY_DEVICEID] = {
967 .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
968 .decode_args =
969 (callback_decode_arg_t)decode_devicenotify_args,
970 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
971 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400972 [OP_CB_SEQUENCE] = {
973 .process_op = (callback_process_op_t)nfs4_callback_sequence,
974 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
975 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
976 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
977 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500978 [OP_CB_RECALL_ANY] = {
979 .process_op = (callback_process_op_t)nfs4_callback_recallany,
980 .decode_args = (callback_decode_arg_t)decode_recallany_args,
981 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
982 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500983 [OP_CB_RECALL_SLOT] = {
984 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
985 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
986 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
987 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400988#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989};
990
991/*
992 * Define NFS4 callback procedures
993 */
994static struct svc_procedure nfs4_callback_procedures1[] = {
995 [CB_NULL] = {
996 .pc_func = nfs4_callback_null,
997 .pc_decode = (kxdrproc_t)nfs4_decode_void,
998 .pc_encode = (kxdrproc_t)nfs4_encode_void,
999 .pc_xdrressize = 1,
1000 },
1001 [CB_COMPOUND] = {
1002 .pc_func = nfs4_callback_compound,
1003 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1004 .pc_argsize = 256,
1005 .pc_ressize = 256,
1006 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1007 }
1008};
1009
1010struct svc_version nfs4_callback_version1 = {
1011 .vs_vers = 1,
1012 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1013 .vs_proc = nfs4_callback_procedures1,
1014 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1015 .vs_dispatch = NULL,
Steve Dickson49697ee2009-10-13 16:07:33 -04001016 .vs_hidden = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017};
1018
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001019struct svc_version nfs4_callback_version4 = {
1020 .vs_vers = 4,
1021 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1022 .vs_proc = nfs4_callback_procedures1,
1023 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1024 .vs_dispatch = NULL,
Jeff Layton60702952011-11-04 07:04:10 -04001025 .vs_hidden = 1,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -05001026};