NFS: Clean up decode_recall_args()
[linux-2.6-block.git] / fs / nfs / callback_xdr.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/callback_xdr.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFSv4 callback encode/decode procedures
7 */
1da177e4
LT
8#include <linux/kernel.h>
9#include <linux/sunrpc/svc.h>
10#include <linux/nfs4.h>
11#include <linux/nfs_fs.h>
9a3ba432
TM
12#include <linux/ratelimit.h>
13#include <linux/printk.h>
5a0e3ad6 14#include <linux/slab.h>
c36fca52 15#include <linux/sunrpc/bc_xprt.h>
4ce79717 16#include "nfs4_fs.h"
1da177e4 17#include "callback.h"
c36fca52 18#include "internal.h"
76e697ba 19#include "nfs4session.h"
1da177e4 20
45724e8a
KM
21#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)
1da177e4 29
4aece6a1 30#if defined(CONFIG_NFS_V4_1)
f2a62561 31#define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
1be5683b 32#define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
4aece6a1 33#define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
45724e8a
KM
34 NFS4_MAX_SESSIONID_LEN + \
35 (1 + 3) * 4) // seqid, 3 slotids
31f09607 36#define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
b9efa1b2 37#define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
db783688 38#define CB_OP_NOTIFY_LOCK_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
4aece6a1
BH
39#endif /* CONFIG_NFS_V4_1 */
40
1da177e4
LT
41#define NFSDBG_FACILITY NFSDBG_CALLBACK
42
31d2b435
AA
43/* Internal error code */
44#define NFS4ERR_RESOURCE_HDR 11050
45
c36fca52
AA
46typedef __be32 (*callback_process_op_t)(void *, void *,
47 struct cb_process_state *);
e6f684f6
AV
48typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
49typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
1da177e4
LT
50
51
52struct callback_op {
53 callback_process_op_t process_op;
54 callback_decode_arg_t decode_args;
55 callback_encode_res_t encode_res;
56 long res_maxsize;
57};
58
59static struct callback_op callback_ops[];
60
7111c66e 61static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
1da177e4
LT
62{
63 return htonl(NFS4_OK);
64}
65
5704fdeb 66static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
1da177e4
LT
67{
68 return xdr_argsize_check(rqstp, p);
69}
70
5704fdeb 71static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
1da177e4
LT
72{
73 return xdr_ressize_check(rqstp, p);
74}
75
b60475c9 76static __be32 *read_buf(struct xdr_stream *xdr, size_t nbytes)
1da177e4 77{
5704fdeb 78 __be32 *p;
1da177e4
LT
79
80 p = xdr_inline_decode(xdr, nbytes);
81 if (unlikely(p == NULL))
756b9b37 82 printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n");
1da177e4
LT
83 return p;
84}
85
c065eeea
TM
86static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len,
87 const char **str, size_t maxlen)
1da177e4 88{
c065eeea 89 ssize_t err;
1da177e4 90
c065eeea
TM
91 err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen);
92 if (err < 0)
93 return cpu_to_be32(NFS4ERR_RESOURCE);
94 *len = err;
1da177e4
LT
95 return 0;
96}
97
e6f684f6 98static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
1da177e4 99{
5704fdeb 100 __be32 *p;
1da177e4
LT
101
102 p = read_buf(xdr, 4);
103 if (unlikely(p == NULL))
104 return htonl(NFS4ERR_RESOURCE);
105 fh->size = ntohl(*p);
106 if (fh->size > NFS4_FHSIZE)
107 return htonl(NFS4ERR_BADHANDLE);
108 p = read_buf(xdr, fh->size);
109 if (unlikely(p == NULL))
110 return htonl(NFS4ERR_RESOURCE);
111 memcpy(&fh->data[0], p, fh->size);
112 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
113 return 0;
114}
115
e6f684f6 116static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
1da177e4 117{
5704fdeb 118 __be32 *p;
1da177e4
LT
119 unsigned int attrlen;
120
121 p = read_buf(xdr, 4);
122 if (unlikely(p == NULL))
123 return htonl(NFS4ERR_RESOURCE);
124 attrlen = ntohl(*p);
125 p = read_buf(xdr, attrlen << 2);
126 if (unlikely(p == NULL))
127 return htonl(NFS4ERR_RESOURCE);
128 if (likely(attrlen > 0))
129 bitmap[0] = ntohl(*p++);
130 if (attrlen > 1)
131 bitmap[1] = ntohl(*p);
132 return 0;
133}
134
e6f684f6 135static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
1da177e4 136{
5704fdeb 137 __be32 *p;
1da177e4 138
2d2f24ad 139 p = read_buf(xdr, NFS4_STATEID_SIZE);
1da177e4
LT
140 if (unlikely(p == NULL))
141 return htonl(NFS4ERR_RESOURCE);
93b717fd 142 memcpy(stateid->data, p, NFS4_STATEID_SIZE);
1da177e4
LT
143 return 0;
144}
145
93b717fd
TM
146static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
147{
148 stateid->type = NFS4_DELEGATION_STATEID_TYPE;
149 return decode_stateid(xdr, stateid);
150}
151
e6f684f6 152static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
1da177e4 153{
5704fdeb 154 __be32 *p;
e6f684f6 155 __be32 status;
1da177e4 156
c065eeea 157 status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ);
1da177e4
LT
158 if (unlikely(status != 0))
159 return status;
1da177e4
LT
160 p = read_buf(xdr, 12);
161 if (unlikely(p == NULL))
162 return htonl(NFS4ERR_RESOURCE);
b8f2ef84 163 hdr->minorversion = ntohl(*p++);
459de2ed
BS
164 /* Check for minor version support */
165 if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
42c2c424 166 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
48a9e2d2 167 } else {
9a3ba432 168 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
b8f2ef84
BH
169 "illegal minor version %u!\n",
170 __func__, hdr->minorversion);
1da177e4
LT
171 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
172 }
1da177e4 173 hdr->nops = ntohl(*p);
b8f2ef84
BH
174 dprintk("%s: minorversion %d nops %d\n", __func__,
175 hdr->minorversion, hdr->nops);
1da177e4
LT
176 return 0;
177}
178
e6f684f6 179static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
1da177e4 180{
5704fdeb 181 __be32 *p;
1da177e4
LT
182 p = read_buf(xdr, 4);
183 if (unlikely(p == NULL))
31d2b435 184 return htonl(NFS4ERR_RESOURCE_HDR);
1da177e4
LT
185 *op = ntohl(*p);
186 return 0;
187}
188
e6f684f6 189static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
1da177e4 190{
e6f684f6 191 __be32 status;
1da177e4
LT
192
193 status = decode_fh(xdr, &args->fh);
194 if (unlikely(status != 0))
56938bb7
AS
195 return status;
196 return decode_bitmap(xdr, args->bitmap);
1da177e4
LT
197}
198
e6f684f6 199static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
1da177e4 200{
5704fdeb 201 __be32 *p;
e6f684f6 202 __be32 status;
1da177e4 203
93b717fd 204 status = decode_delegation_stateid(xdr, &args->stateid);
1da177e4 205 if (unlikely(status != 0))
135a4ea0 206 return status;
1da177e4 207 p = read_buf(xdr, 4);
135a4ea0
AS
208 if (unlikely(p == NULL))
209 return htonl(NFS4ERR_RESOURCE);
1da177e4 210 args->truncate = ntohl(*p);
135a4ea0 211 return decode_fh(xdr, &args->fh);
1da177e4
LT
212}
213
4aece6a1 214#if defined(CONFIG_NFS_V4_1)
93b717fd
TM
215static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
216{
217 stateid->type = NFS4_LAYOUT_STATEID_TYPE;
218 return decode_stateid(xdr, stateid);
219}
4aece6a1 220
f2a62561
FI
221static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
222 struct xdr_stream *xdr,
223 struct cb_layoutrecallargs *args)
224{
225 __be32 *p;
226 __be32 status = 0;
227 uint32_t iomode;
228
f2a62561
FI
229 p = read_buf(xdr, 4 * sizeof(uint32_t));
230 if (unlikely(p == NULL)) {
231 status = htonl(NFS4ERR_BADXDR);
232 goto out;
233 }
234
235 args->cbl_layout_type = ntohl(*p++);
236 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
25985edc 237 * as it is unusable and ignored with the other types.
f2a62561
FI
238 */
239 iomode = ntohl(*p++);
240 args->cbl_layoutchanged = ntohl(*p++);
241 args->cbl_recall_type = ntohl(*p++);
242
243 if (args->cbl_recall_type == RETURN_FILE) {
244 args->cbl_range.iomode = iomode;
245 status = decode_fh(xdr, &args->cbl_fh);
246 if (unlikely(status != 0))
247 goto out;
248
249 p = read_buf(xdr, 2 * sizeof(uint64_t));
250 if (unlikely(p == NULL)) {
251 status = htonl(NFS4ERR_BADXDR);
252 goto out;
253 }
254 p = xdr_decode_hyper(p, &args->cbl_range.offset);
255 p = xdr_decode_hyper(p, &args->cbl_range.length);
93b717fd 256 status = decode_layout_stateid(xdr, &args->cbl_stateid);
f2a62561
FI
257 if (unlikely(status != 0))
258 goto out;
259 } else if (args->cbl_recall_type == RETURN_FSID) {
260 p = read_buf(xdr, 2 * sizeof(uint64_t));
261 if (unlikely(p == NULL)) {
262 status = htonl(NFS4ERR_BADXDR);
263 goto out;
264 }
265 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
266 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
267 } else if (args->cbl_recall_type != RETURN_ALL) {
268 status = htonl(NFS4ERR_BADXDR);
269 goto out;
270 }
271 dprintk("%s: ltype 0x%x iomode %d changed %d recall_type %d\n",
272 __func__,
273 args->cbl_layout_type, iomode,
274 args->cbl_layoutchanged, args->cbl_recall_type);
275out:
276 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
277 return status;
278}
279
1be5683b
ME
280static
281__be32 decode_devicenotify_args(struct svc_rqst *rqstp,
282 struct xdr_stream *xdr,
283 struct cb_devicenotifyargs *args)
284{
285 __be32 *p;
286 __be32 status = 0;
287 u32 tmp;
288 int n, i;
289 args->ndevs = 0;
290
291 /* Num of device notifications */
292 p = read_buf(xdr, sizeof(uint32_t));
293 if (unlikely(p == NULL)) {
294 status = htonl(NFS4ERR_BADXDR);
295 goto out;
296 }
297 n = ntohl(*p++);
298 if (n <= 0)
299 goto out;
363e0df0
DC
300 if (n > ULONG_MAX / sizeof(*args->devs)) {
301 status = htonl(NFS4ERR_BADXDR);
302 goto out;
303 }
1be5683b 304
a4f743a6 305 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
1be5683b
ME
306 if (!args->devs) {
307 status = htonl(NFS4ERR_DELAY);
308 goto out;
309 }
310
311 /* Decode each dev notification */
312 for (i = 0; i < n; i++) {
313 struct cb_devicenotifyitem *dev = &args->devs[i];
314
315 p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
316 if (unlikely(p == NULL)) {
317 status = htonl(NFS4ERR_BADXDR);
318 goto err;
319 }
320
321 tmp = ntohl(*p++); /* bitmap size */
322 if (tmp != 1) {
323 status = htonl(NFS4ERR_INVAL);
324 goto err;
325 }
326 dev->cbd_notify_type = ntohl(*p++);
327 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
328 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
329 status = htonl(NFS4ERR_INVAL);
330 goto err;
331 }
332
333 tmp = ntohl(*p++); /* opaque size */
334 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
335 (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
336 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
337 (tmp != NFS4_DEVICEID4_SIZE + 4))) {
338 status = htonl(NFS4ERR_INVAL);
339 goto err;
340 }
341 dev->cbd_layout_type = ntohl(*p++);
342 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
343 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
344
345 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
346 p = read_buf(xdr, sizeof(uint32_t));
347 if (unlikely(p == NULL)) {
348 status = htonl(NFS4ERR_BADXDR);
349 goto err;
350 }
351 dev->cbd_immediate = ntohl(*p++);
352 } else {
353 dev->cbd_immediate = 0;
354 }
355
356 args->ndevs++;
357
358 dprintk("%s: type %d layout 0x%x immediate %d\n",
359 __func__, dev->cbd_notify_type, dev->cbd_layout_type,
360 dev->cbd_immediate);
361 }
362out:
363 dprintk("%s: status %d ndevs %d\n",
364 __func__, ntohl(status), args->ndevs);
365 return status;
366err:
367 kfree(args->devs);
368 goto out;
369}
370
9733f0d9 371static __be32 decode_sessionid(struct xdr_stream *xdr,
4aece6a1
BH
372 struct nfs4_sessionid *sid)
373{
9733f0d9 374 __be32 *p;
4aece6a1 375
590184a6 376 p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
4aece6a1 377 if (unlikely(p == NULL))
a419aef8 378 return htonl(NFS4ERR_RESOURCE);
4aece6a1 379
590184a6 380 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
4aece6a1
BH
381 return 0;
382}
383
9733f0d9 384static __be32 decode_rc_list(struct xdr_stream *xdr,
4aece6a1
BH
385 struct referring_call_list *rc_list)
386{
9733f0d9 387 __be32 *p;
4aece6a1 388 int i;
9733f0d9 389 __be32 status;
4aece6a1
BH
390
391 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
392 if (status)
393 goto out;
394
395 status = htonl(NFS4ERR_RESOURCE);
396 p = read_buf(xdr, sizeof(uint32_t));
397 if (unlikely(p == NULL))
398 goto out;
399
400 rc_list->rcl_nrefcalls = ntohl(*p++);
401 if (rc_list->rcl_nrefcalls) {
402 p = read_buf(xdr,
403 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
404 if (unlikely(p == NULL))
405 goto out;
a4f743a6 406 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
4aece6a1
BH
407 sizeof(*rc_list->rcl_refcalls),
408 GFP_KERNEL);
409 if (unlikely(rc_list->rcl_refcalls == NULL))
410 goto out;
411 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
412 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
413 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
414 }
415 }
416 status = 0;
417
418out:
419 return status;
420}
421
9733f0d9 422static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
4aece6a1
BH
423 struct xdr_stream *xdr,
424 struct cb_sequenceargs *args)
425{
9733f0d9 426 __be32 *p;
4aece6a1 427 int i;
9733f0d9 428 __be32 status;
4aece6a1
BH
429
430 status = decode_sessionid(xdr, &args->csa_sessionid);
431 if (status)
432 goto out;
433
434 status = htonl(NFS4ERR_RESOURCE);
435 p = read_buf(xdr, 5 * sizeof(uint32_t));
436 if (unlikely(p == NULL))
437 goto out;
438
65fc64e5 439 args->csa_addr = svc_addr(rqstp);
4aece6a1
BH
440 args->csa_sequenceid = ntohl(*p++);
441 args->csa_slotid = ntohl(*p++);
442 args->csa_highestslotid = ntohl(*p++);
443 args->csa_cachethis = ntohl(*p++);
444 args->csa_nrclists = ntohl(*p++);
445 args->csa_rclists = NULL;
446 if (args->csa_nrclists) {
0439f31c
DC
447 args->csa_rclists = kmalloc_array(args->csa_nrclists,
448 sizeof(*args->csa_rclists),
449 GFP_KERNEL);
4aece6a1
BH
450 if (unlikely(args->csa_rclists == NULL))
451 goto out;
452
453 for (i = 0; i < args->csa_nrclists; i++) {
454 status = decode_rc_list(xdr, &args->csa_rclists[i]);
d8ba1f97
TM
455 if (status) {
456 args->csa_nrclists = i;
4aece6a1 457 goto out_free;
d8ba1f97 458 }
4aece6a1
BH
459 }
460 }
461 status = 0;
462
463 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
464 "highestslotid %u cachethis %d nrclists %u\n",
465 __func__,
466 ((u32 *)&args->csa_sessionid)[0],
467 ((u32 *)&args->csa_sessionid)[1],
468 ((u32 *)&args->csa_sessionid)[2],
469 ((u32 *)&args->csa_sessionid)[3],
470 args->csa_sequenceid, args->csa_slotid,
471 args->csa_highestslotid, args->csa_cachethis,
472 args->csa_nrclists);
473out:
474 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
475 return status;
476
477out_free:
478 for (i = 0; i < args->csa_nrclists; i++)
479 kfree(args->csa_rclists[i].rcl_refcalls);
480 kfree(args->csa_rclists);
481 goto out;
482}
483
9733f0d9 484static __be32 decode_recallany_args(struct svc_rqst *rqstp,
31f09607
AB
485 struct xdr_stream *xdr,
486 struct cb_recallanyargs *args)
487{
d743c3c9
PT
488 uint32_t bitmap[2];
489 __be32 *p, status;
31f09607 490
31f09607
AB
491 p = read_buf(xdr, 4);
492 if (unlikely(p == NULL))
493 return htonl(NFS4ERR_BADXDR);
494 args->craa_objs_to_keep = ntohl(*p++);
d743c3c9
PT
495 status = decode_bitmap(xdr, bitmap);
496 if (unlikely(status))
497 return status;
498 args->craa_type_mask = bitmap[0];
31f09607
AB
499
500 return 0;
501}
502
9733f0d9 503static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
b9efa1b2
AA
504 struct xdr_stream *xdr,
505 struct cb_recallslotargs *args)
506{
507 __be32 *p;
508
b9efa1b2
AA
509 p = read_buf(xdr, 4);
510 if (unlikely(p == NULL))
511 return htonl(NFS4ERR_BADXDR);
d5fb4ce3 512 args->crsa_target_highest_slotid = ntohl(*p++);
b9efa1b2
AA
513 return 0;
514}
515
db783688
JL
516static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
517{
518 __be32 *p;
519 unsigned int len;
520
521 p = read_buf(xdr, 12);
522 if (unlikely(p == NULL))
523 return htonl(NFS4ERR_BADXDR);
524
525 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
526 len = be32_to_cpu(*p);
527
528 p = read_buf(xdr, len);
529 if (unlikely(p == NULL))
530 return htonl(NFS4ERR_BADXDR);
531
532 /* Only try to decode if the length is right */
533 if (len == 20) {
534 p += 2; /* skip "lock id:" */
535 args->cbnl_owner.s_dev = be32_to_cpu(*p++);
536 xdr_decode_hyper(p, &args->cbnl_owner.id);
537 args->cbnl_valid = true;
538 } else {
539 args->cbnl_owner.s_dev = 0;
540 args->cbnl_owner.id = 0;
541 args->cbnl_valid = false;
542 }
543 return 0;
544}
545
546static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_notify_lock_args *args)
547{
548 __be32 status;
549
550 status = decode_fh(xdr, &args->cbnl_fh);
551 if (unlikely(status != 0))
552 goto out;
553 status = decode_lockowner(xdr, args);
554out:
555 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
556 return status;
557}
558
4aece6a1
BH
559#endif /* CONFIG_NFS_V4_1 */
560
e6f684f6 561static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
1da177e4 562{
ab6e9aaf
TM
563 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
564 return cpu_to_be32(NFS4ERR_RESOURCE);
1da177e4
LT
565 return 0;
566}
567
568#define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
569#define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
5704fdeb 570static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
1da177e4 571{
5704fdeb
AV
572 __be32 bm[2];
573 __be32 *p;
1da177e4
LT
574
575 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
576 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
577 if (bm[1] != 0) {
578 p = xdr_reserve_space(xdr, 16);
579 if (unlikely(p == NULL))
580 return htonl(NFS4ERR_RESOURCE);
581 *p++ = htonl(2);
582 *p++ = bm[0];
583 *p++ = bm[1];
584 } else if (bm[0] != 0) {
585 p = xdr_reserve_space(xdr, 12);
586 if (unlikely(p == NULL))
587 return htonl(NFS4ERR_RESOURCE);
588 *p++ = htonl(1);
589 *p++ = bm[0];
590 } else {
591 p = xdr_reserve_space(xdr, 8);
592 if (unlikely(p == NULL))
593 return htonl(NFS4ERR_RESOURCE);
594 *p++ = htonl(0);
595 }
596 *savep = p;
597 return 0;
598}
599
e6f684f6 600static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
1da177e4 601{
5704fdeb 602 __be32 *p;
1da177e4
LT
603
604 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
605 return 0;
606 p = xdr_reserve_space(xdr, 8);
90dc7d27 607 if (unlikely(!p))
1da177e4
LT
608 return htonl(NFS4ERR_RESOURCE);
609 p = xdr_encode_hyper(p, change);
610 return 0;
611}
612
e6f684f6 613static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
1da177e4 614{
5704fdeb 615 __be32 *p;
1da177e4
LT
616
617 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
618 return 0;
619 p = xdr_reserve_space(xdr, 8);
90dc7d27 620 if (unlikely(!p))
1da177e4
LT
621 return htonl(NFS4ERR_RESOURCE);
622 p = xdr_encode_hyper(p, size);
623 return 0;
624}
625
e6f684f6 626static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
1da177e4 627{
5704fdeb 628 __be32 *p;
1da177e4
LT
629
630 p = xdr_reserve_space(xdr, 12);
90dc7d27 631 if (unlikely(!p))
1da177e4
LT
632 return htonl(NFS4ERR_RESOURCE);
633 p = xdr_encode_hyper(p, time->tv_sec);
634 *p = htonl(time->tv_nsec);
635 return 0;
636}
637
e6f684f6 638static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
1da177e4
LT
639{
640 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
641 return 0;
642 return encode_attr_time(xdr,time);
643}
644
e6f684f6 645static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
1da177e4
LT
646{
647 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
648 return 0;
649 return encode_attr_time(xdr,time);
650}
651
e6f684f6 652static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
1da177e4 653{
e6f684f6 654 __be32 status;
1da177e4
LT
655
656 hdr->status = xdr_reserve_space(xdr, 4);
657 if (unlikely(hdr->status == NULL))
658 return htonl(NFS4ERR_RESOURCE);
659 status = encode_string(xdr, hdr->taglen, hdr->tag);
660 if (unlikely(status != 0))
661 return status;
662 hdr->nops = xdr_reserve_space(xdr, 4);
663 if (unlikely(hdr->nops == NULL))
664 return htonl(NFS4ERR_RESOURCE);
665 return 0;
666}
667
e6f684f6 668static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
1da177e4 669{
5704fdeb 670 __be32 *p;
1da177e4
LT
671
672 p = xdr_reserve_space(xdr, 8);
673 if (unlikely(p == NULL))
31d2b435 674 return htonl(NFS4ERR_RESOURCE_HDR);
1da177e4
LT
675 *p++ = htonl(op);
676 *p = res;
677 return 0;
678}
679
e6f684f6 680static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
1da177e4 681{
5704fdeb 682 __be32 *savep = NULL;
e6f684f6 683 __be32 status = res->status;
1da177e4
LT
684
685 if (unlikely(status != 0))
686 goto out;
687 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
688 if (unlikely(status != 0))
689 goto out;
690 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
691 if (unlikely(status != 0))
692 goto out;
693 status = encode_attr_size(xdr, res->bitmap, res->size);
694 if (unlikely(status != 0))
695 goto out;
696 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
697 if (unlikely(status != 0))
698 goto out;
699 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
700 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
701out:
3110ff80 702 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
1da177e4
LT
703 return status;
704}
705
34bc47c9
BH
706#if defined(CONFIG_NFS_V4_1)
707
9733f0d9 708static __be32 encode_sessionid(struct xdr_stream *xdr,
4aece6a1
BH
709 const struct nfs4_sessionid *sid)
710{
9733f0d9 711 __be32 *p;
4aece6a1 712
590184a6 713 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
4aece6a1
BH
714 if (unlikely(p == NULL))
715 return htonl(NFS4ERR_RESOURCE);
716
590184a6 717 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
4aece6a1
BH
718 return 0;
719}
720
9733f0d9 721static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
4aece6a1
BH
722 struct xdr_stream *xdr,
723 const struct cb_sequenceres *res)
724{
9733f0d9 725 __be32 *p;
e216c8c7 726 __be32 status = res->csr_status;
4aece6a1
BH
727
728 if (unlikely(status != 0))
729 goto out;
730
e0a63c0b
KM
731 status = encode_sessionid(xdr, &res->csr_sessionid);
732 if (status)
733 goto out;
4aece6a1
BH
734
735 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
736 if (unlikely(p == NULL))
737 return htonl(NFS4ERR_RESOURCE);
738
739 *p++ = htonl(res->csr_sequenceid);
740 *p++ = htonl(res->csr_slotid);
741 *p++ = htonl(res->csr_highestslotid);
742 *p++ = htonl(res->csr_target_highestslotid);
743out:
744 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
745 return status;
746}
747
34bc47c9
BH
748static __be32
749preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
750{
281fe15d
BH
751 if (op_nr == OP_CB_SEQUENCE) {
752 if (nop != 0)
753 return htonl(NFS4ERR_SEQUENCE_POS);
754 } else {
755 if (nop == 0)
756 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
757 }
758
34bc47c9
BH
759 switch (op_nr) {
760 case OP_CB_GETATTR:
761 case OP_CB_RECALL:
4aece6a1 762 case OP_CB_SEQUENCE:
31f09607 763 case OP_CB_RECALL_ANY:
b9efa1b2 764 case OP_CB_RECALL_SLOT:
f2a62561 765 case OP_CB_LAYOUTRECALL:
1be5683b 766 case OP_CB_NOTIFY_DEVICEID:
db783688 767 case OP_CB_NOTIFY_LOCK:
34bc47c9
BH
768 *op = &callback_ops[op_nr];
769 break;
770
34bc47c9
BH
771 case OP_CB_NOTIFY:
772 case OP_CB_PUSH_DELEG:
34bc47c9 773 case OP_CB_RECALLABLE_OBJ_AVAIL:
34bc47c9 774 case OP_CB_WANTS_CANCELLED:
34bc47c9
BH
775 return htonl(NFS4ERR_NOTSUPP);
776
777 default:
778 return htonl(NFS4ERR_OP_ILLEGAL);
779 }
780
781 return htonl(NFS_OK);
782}
783
810d82e6
TM
784static void nfs4_callback_free_slot(struct nfs4_session *session,
785 struct nfs4_slot *slot)
42acd021
AA
786{
787 struct nfs4_slot_table *tbl = &session->bc_slot_table;
788
789 spin_lock(&tbl->slot_tbl_lock);
790 /*
791 * Let the state manager know callback processing done.
792 * A single slot, so highest used slotid is either 0 or -1
793 */
810d82e6 794 nfs4_free_slot(tbl, slot);
774d5f14 795 nfs4_slot_tbl_drain_complete(tbl);
42acd021
AA
796 spin_unlock(&tbl->slot_tbl_lock);
797}
798
55a67399 799static void nfs4_cb_free_slot(struct cb_process_state *cps)
42acd021 800{
810d82e6
TM
801 if (cps->slot) {
802 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
803 cps->slot = NULL;
804 }
42acd021
AA
805}
806
34bc47c9
BH
807#else /* CONFIG_NFS_V4_1 */
808
809static __be32
810preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
811{
812 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
813}
814
55a67399 815static void nfs4_cb_free_slot(struct cb_process_state *cps)
42acd021
AA
816{
817}
34bc47c9
BH
818#endif /* CONFIG_NFS_V4_1 */
819
6b140b85
BS
820#ifdef CONFIG_NFS_V4_2
821static __be32
822preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
823{
824 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
825 if (status != htonl(NFS4ERR_OP_ILLEGAL))
826 return status;
827
828 if (op_nr == OP_CB_OFFLOAD)
829 return htonl(NFS4ERR_NOTSUPP);
830 return htonl(NFS4ERR_OP_ILLEGAL);
831}
832#else /* CONFIG_NFS_V4_2 */
833static __be32
834preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
835{
836 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
837}
838#endif /* CONFIG_NFS_V4_2 */
839
34bc47c9
BH
840static __be32
841preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
842{
843 switch (op_nr) {
844 case OP_CB_GETATTR:
845 case OP_CB_RECALL:
846 *op = &callback_ops[op_nr];
847 break;
848 default:
849 return htonl(NFS4ERR_OP_ILLEGAL);
850 }
851
852 return htonl(NFS_OK);
853}
854
459de2ed 855static __be32 process_op(int nop, struct svc_rqst *rqstp,
1da177e4 856 struct xdr_stream *xdr_in, void *argp,
c36fca52
AA
857 struct xdr_stream *xdr_out, void *resp,
858 struct cb_process_state *cps)
1da177e4 859{
a162a6b8 860 struct callback_op *op = &callback_ops[0];
31d2b435 861 unsigned int op_nr;
34bc47c9 862 __be32 status;
1da177e4 863 long maxlen;
e6f684f6 864 __be32 res;
1da177e4 865
3110ff80 866 dprintk("%s: start\n", __func__);
1da177e4 867 status = decode_op_hdr(xdr_in, &op_nr);
31d2b435
AA
868 if (unlikely(status))
869 return status;
1da177e4 870
34bc47c9 871 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
459de2ed 872 __func__, cps->minorversion, nop, op_nr);
34bc47c9 873
6b140b85
BS
874 switch (cps->minorversion) {
875 case 0:
876 status = preprocess_nfs4_op(op_nr, &op);
877 break;
878 case 1:
879 status = preprocess_nfs41_op(nop, op_nr, &op);
880 break;
881 case 2:
882 status = preprocess_nfs42_op(nop, op_nr, &op);
883 break;
884 default:
885 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
886 }
34bc47c9 887
34bc47c9
BH
888 if (status == htonl(NFS4ERR_OP_ILLEGAL))
889 op_nr = OP_CB_ILLEGAL;
b92b3019
AA
890 if (status)
891 goto encode_hdr;
31d2b435 892
c36fca52
AA
893 if (cps->drc_status) {
894 status = cps->drc_status;
4911096f
AA
895 goto encode_hdr;
896 }
897
1da177e4
LT
898 maxlen = xdr_out->end - xdr_out->p;
899 if (maxlen > 0 && maxlen < PAGE_SIZE) {
e95e60da
AA
900 status = op->decode_args(rqstp, xdr_in, argp);
901 if (likely(status == 0))
c36fca52 902 status = op->process_op(argp, resp, cps);
1da177e4
LT
903 } else
904 status = htonl(NFS4ERR_RESOURCE);
905
b92b3019 906encode_hdr:
1da177e4 907 res = encode_op_hdr(xdr_out, op_nr, status);
31d2b435
AA
908 if (unlikely(res))
909 return res;
1da177e4
LT
910 if (op->encode_res != NULL && status == 0)
911 status = op->encode_res(rqstp, xdr_out, resp);
3110ff80 912 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
1da177e4
LT
913 return status;
914}
915
916/*
917 * Decode, process and encode a COMPOUND
918 */
7111c66e 919static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
1da177e4 920{
3a6258e1
TM
921 struct cb_compound_hdr_arg hdr_arg = { 0 };
922 struct cb_compound_hdr_res hdr_res = { NULL };
1da177e4 923 struct xdr_stream xdr_in, xdr_out;
c36fca52
AA
924 __be32 *p, status;
925 struct cb_process_state cps = {
926 .drc_status = 0,
927 .clp = NULL,
9695c705 928 .net = SVC_NET(rqstp),
c36fca52 929 };
3a6258e1 930 unsigned int nops = 0;
1da177e4 931
3110ff80 932 dprintk("%s: start\n", __func__);
1da177e4 933
756b9b37 934 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
1da177e4 935
5704fdeb 936 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
1da177e4
LT
937 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
938
3a6258e1 939 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
4ed0d83d 940 if (status == htonl(NFS4ERR_RESOURCE))
3a6258e1
TM
941 return rpc_garbage_args;
942
c36fca52 943 if (hdr_arg.minorversion == 0) {
9695c705 944 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
778be232 945 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
a4e187d8 946 goto out_invalidcred;
778be232 947 }
c36fca52 948
459de2ed 949 cps.minorversion = hdr_arg.minorversion;
1da177e4
LT
950 hdr_res.taglen = hdr_arg.taglen;
951 hdr_res.tag = hdr_arg.tag;
3a6258e1
TM
952 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
953 return rpc_system_err;
1da177e4 954
3a6258e1 955 while (status == 0 && nops != hdr_arg.nops) {
459de2ed
BS
956 status = process_op(nops, rqstp, &xdr_in,
957 argp, &xdr_out, resp, &cps);
1da177e4
LT
958 nops++;
959 }
3a6258e1 960
31d2b435
AA
961 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
962 * resource error in cb_compound status without returning op */
963 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
964 status = htonl(NFS4ERR_RESOURCE);
965 nops--;
966 }
967
1da177e4
LT
968 *hdr_res.status = status;
969 *hdr_res.nops = htonl(nops);
55a67399 970 nfs4_cb_free_slot(&cps);
c36fca52 971 nfs_put_client(cps.clp);
3110ff80 972 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
1da177e4 973 return rpc_success;
a4e187d8
CL
974
975out_invalidcred:
976 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
977 return rpc_autherr_badcred;
1da177e4
LT
978}
979
980/*
981 * Define NFS4 callback COMPOUND ops.
982 */
983static struct callback_op callback_ops[] = {
984 [0] = {
985 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
986 },
987 [OP_CB_GETATTR] = {
988 .process_op = (callback_process_op_t)nfs4_callback_getattr,
989 .decode_args = (callback_decode_arg_t)decode_getattr_args,
990 .encode_res = (callback_encode_res_t)encode_getattr_res,
991 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
992 },
993 [OP_CB_RECALL] = {
994 .process_op = (callback_process_op_t)nfs4_callback_recall,
995 .decode_args = (callback_decode_arg_t)decode_recall_args,
996 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
4aece6a1
BH
997 },
998#if defined(CONFIG_NFS_V4_1)
f2a62561
FI
999 [OP_CB_LAYOUTRECALL] = {
1000 .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
1001 .decode_args =
1002 (callback_decode_arg_t)decode_layoutrecall_args,
1003 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
1004 },
1be5683b
ME
1005 [OP_CB_NOTIFY_DEVICEID] = {
1006 .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
1007 .decode_args =
1008 (callback_decode_arg_t)decode_devicenotify_args,
1009 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
1010 },
4aece6a1
BH
1011 [OP_CB_SEQUENCE] = {
1012 .process_op = (callback_process_op_t)nfs4_callback_sequence,
1013 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
1014 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
1015 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
1016 },
31f09607
AB
1017 [OP_CB_RECALL_ANY] = {
1018 .process_op = (callback_process_op_t)nfs4_callback_recallany,
1019 .decode_args = (callback_decode_arg_t)decode_recallany_args,
1020 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
1021 },
b9efa1b2
AA
1022 [OP_CB_RECALL_SLOT] = {
1023 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
1024 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
1025 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
1026 },
db783688
JL
1027 [OP_CB_NOTIFY_LOCK] = {
1028 .process_op = (callback_process_op_t)nfs4_callback_notify_lock,
1029 .decode_args = (callback_decode_arg_t)decode_notify_lock_args,
1030 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
1031 },
4aece6a1 1032#endif /* CONFIG_NFS_V4_1 */
1da177e4
LT
1033};
1034
1035/*
1036 * Define NFS4 callback procedures
1037 */
1038static struct svc_procedure nfs4_callback_procedures1[] = {
1039 [CB_NULL] = {
1040 .pc_func = nfs4_callback_null,
1041 .pc_decode = (kxdrproc_t)nfs4_decode_void,
1042 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1043 .pc_xdrressize = 1,
1044 },
1045 [CB_COMPOUND] = {
1046 .pc_func = nfs4_callback_compound,
1047 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1048 .pc_argsize = 256,
1049 .pc_ressize = 256,
1050 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1051 }
1052};
1053
1054struct svc_version nfs4_callback_version1 = {
1055 .vs_vers = 1,
1056 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1057 .vs_proc = nfs4_callback_procedures1,
1058 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1059 .vs_dispatch = NULL,
05a45a2d 1060 .vs_hidden = true,
5283b03e 1061 .vs_need_cong_ctrl = true,
1da177e4
LT
1062};
1063
07bccc2d
AB
1064struct svc_version nfs4_callback_version4 = {
1065 .vs_vers = 4,
1066 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1067 .vs_proc = nfs4_callback_procedures1,
1068 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1069 .vs_dispatch = NULL,
05a45a2d 1070 .vs_hidden = true,
5283b03e 1071 .vs_need_cong_ctrl = true,
07bccc2d 1072};