Merge branches 'acpi-button' and 'acpi-tools'
[linux-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
LT
173 hdr->nops = ntohl(*p);
174 return 0;
175}
176
e6f684f6 177static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
1da177e4 178{
5704fdeb 179 __be32 *p;
1da177e4
LT
180 p = read_buf(xdr, 4);
181 if (unlikely(p == NULL))
31d2b435 182 return htonl(NFS4ERR_RESOURCE_HDR);
1da177e4
LT
183 *op = ntohl(*p);
184 return 0;
185}
186
e6f684f6 187static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
1da177e4 188{
e6f684f6 189 __be32 status;
1da177e4
LT
190
191 status = decode_fh(xdr, &args->fh);
192 if (unlikely(status != 0))
56938bb7
AS
193 return status;
194 return decode_bitmap(xdr, args->bitmap);
1da177e4
LT
195}
196
e6f684f6 197static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
1da177e4 198{
5704fdeb 199 __be32 *p;
e6f684f6 200 __be32 status;
1da177e4 201
93b717fd 202 status = decode_delegation_stateid(xdr, &args->stateid);
1da177e4 203 if (unlikely(status != 0))
135a4ea0 204 return status;
1da177e4 205 p = read_buf(xdr, 4);
135a4ea0
AS
206 if (unlikely(p == NULL))
207 return htonl(NFS4ERR_RESOURCE);
1da177e4 208 args->truncate = ntohl(*p);
135a4ea0 209 return decode_fh(xdr, &args->fh);
1da177e4
LT
210}
211
4aece6a1 212#if defined(CONFIG_NFS_V4_1)
93b717fd
TM
213static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
214{
215 stateid->type = NFS4_LAYOUT_STATEID_TYPE;
216 return decode_stateid(xdr, stateid);
217}
4aece6a1 218
f2a62561
FI
219static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
220 struct xdr_stream *xdr,
221 struct cb_layoutrecallargs *args)
222{
223 __be32 *p;
224 __be32 status = 0;
225 uint32_t iomode;
226
f2a62561 227 p = read_buf(xdr, 4 * sizeof(uint32_t));
c79d56d2
AS
228 if (unlikely(p == NULL))
229 return htonl(NFS4ERR_BADXDR);
f2a62561
FI
230
231 args->cbl_layout_type = ntohl(*p++);
232 /* Depite the spec's xdr, iomode really belongs in the FILE switch,
25985edc 233 * as it is unusable and ignored with the other types.
f2a62561
FI
234 */
235 iomode = ntohl(*p++);
236 args->cbl_layoutchanged = ntohl(*p++);
237 args->cbl_recall_type = ntohl(*p++);
238
239 if (args->cbl_recall_type == RETURN_FILE) {
240 args->cbl_range.iomode = iomode;
241 status = decode_fh(xdr, &args->cbl_fh);
242 if (unlikely(status != 0))
c79d56d2 243 return status;
f2a62561
FI
244
245 p = read_buf(xdr, 2 * sizeof(uint64_t));
c79d56d2
AS
246 if (unlikely(p == NULL))
247 return htonl(NFS4ERR_BADXDR);
f2a62561
FI
248 p = xdr_decode_hyper(p, &args->cbl_range.offset);
249 p = xdr_decode_hyper(p, &args->cbl_range.length);
c79d56d2 250 return decode_layout_stateid(xdr, &args->cbl_stateid);
f2a62561
FI
251 } else if (args->cbl_recall_type == RETURN_FSID) {
252 p = read_buf(xdr, 2 * sizeof(uint64_t));
c79d56d2
AS
253 if (unlikely(p == NULL))
254 return htonl(NFS4ERR_BADXDR);
f2a62561
FI
255 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
256 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
c79d56d2
AS
257 } else if (args->cbl_recall_type != RETURN_ALL)
258 return htonl(NFS4ERR_BADXDR);
259 return 0;
f2a62561
FI
260}
261
1be5683b
ME
262static
263__be32 decode_devicenotify_args(struct svc_rqst *rqstp,
264 struct xdr_stream *xdr,
265 struct cb_devicenotifyargs *args)
266{
267 __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;
363e0df0
DC
282 if (n > ULONG_MAX / sizeof(*args->devs)) {
283 status = htonl(NFS4ERR_BADXDR);
284 goto out;
285 }
1be5683b 286
a4f743a6 287 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
1be5683b
ME
288 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
9733f0d9 353static __be32 decode_sessionid(struct xdr_stream *xdr,
4aece6a1
BH
354 struct nfs4_sessionid *sid)
355{
9733f0d9 356 __be32 *p;
4aece6a1 357
590184a6 358 p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
4aece6a1 359 if (unlikely(p == NULL))
a419aef8 360 return htonl(NFS4ERR_RESOURCE);
4aece6a1 361
590184a6 362 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
4aece6a1
BH
363 return 0;
364}
365
9733f0d9 366static __be32 decode_rc_list(struct xdr_stream *xdr,
4aece6a1
BH
367 struct referring_call_list *rc_list)
368{
9733f0d9 369 __be32 *p;
4aece6a1 370 int i;
9733f0d9 371 __be32 status;
4aece6a1
BH
372
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;
a4f743a6 388 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
4aece6a1
BH
389 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
9733f0d9 404static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
4aece6a1
BH
405 struct xdr_stream *xdr,
406 struct cb_sequenceargs *args)
407{
9733f0d9 408 __be32 *p;
4aece6a1 409 int i;
9733f0d9 410 __be32 status;
4aece6a1
BH
411
412 status = decode_sessionid(xdr, &args->csa_sessionid);
413 if (status)
1796549a 414 return status;
4aece6a1 415
4aece6a1
BH
416 p = read_buf(xdr, 5 * sizeof(uint32_t));
417 if (unlikely(p == NULL))
1796549a 418 return htonl(NFS4ERR_RESOURCE);
4aece6a1 419
65fc64e5 420 args->csa_addr = svc_addr(rqstp);
4aece6a1
BH
421 args->csa_sequenceid = ntohl(*p++);
422 args->csa_slotid = ntohl(*p++);
423 args->csa_highestslotid = ntohl(*p++);
424 args->csa_cachethis = ntohl(*p++);
425 args->csa_nrclists = ntohl(*p++);
426 args->csa_rclists = NULL;
427 if (args->csa_nrclists) {
0439f31c
DC
428 args->csa_rclists = kmalloc_array(args->csa_nrclists,
429 sizeof(*args->csa_rclists),
430 GFP_KERNEL);
4aece6a1 431 if (unlikely(args->csa_rclists == NULL))
1796549a 432 return htonl(NFS4ERR_RESOURCE);
4aece6a1
BH
433
434 for (i = 0; i < args->csa_nrclists; i++) {
435 status = decode_rc_list(xdr, &args->csa_rclists[i]);
d8ba1f97
TM
436 if (status) {
437 args->csa_nrclists = i;
4aece6a1 438 goto out_free;
d8ba1f97 439 }
4aece6a1
BH
440 }
441 }
1796549a 442 return 0;
4aece6a1
BH
443
444out_free:
445 for (i = 0; i < args->csa_nrclists; i++)
446 kfree(args->csa_rclists[i].rcl_refcalls);
447 kfree(args->csa_rclists);
1796549a 448 return status;
4aece6a1
BH
449}
450
9733f0d9 451static __be32 decode_recallany_args(struct svc_rqst *rqstp,
31f09607
AB
452 struct xdr_stream *xdr,
453 struct cb_recallanyargs *args)
454{
d743c3c9
PT
455 uint32_t bitmap[2];
456 __be32 *p, status;
31f09607 457
31f09607
AB
458 p = read_buf(xdr, 4);
459 if (unlikely(p == NULL))
460 return htonl(NFS4ERR_BADXDR);
461 args->craa_objs_to_keep = ntohl(*p++);
d743c3c9
PT
462 status = decode_bitmap(xdr, bitmap);
463 if (unlikely(status))
464 return status;
465 args->craa_type_mask = bitmap[0];
31f09607
AB
466
467 return 0;
468}
469
9733f0d9 470static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
b9efa1b2
AA
471 struct xdr_stream *xdr,
472 struct cb_recallslotargs *args)
473{
474 __be32 *p;
475
b9efa1b2
AA
476 p = read_buf(xdr, 4);
477 if (unlikely(p == NULL))
478 return htonl(NFS4ERR_BADXDR);
d5fb4ce3 479 args->crsa_target_highest_slotid = ntohl(*p++);
b9efa1b2
AA
480 return 0;
481}
482
db783688
JL
483static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
484{
485 __be32 *p;
486 unsigned int len;
487
488 p = read_buf(xdr, 12);
489 if (unlikely(p == NULL))
490 return htonl(NFS4ERR_BADXDR);
491
492 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
493 len = be32_to_cpu(*p);
494
495 p = read_buf(xdr, len);
496 if (unlikely(p == NULL))
497 return htonl(NFS4ERR_BADXDR);
498
499 /* Only try to decode if the length is right */
500 if (len == 20) {
501 p += 2; /* skip "lock id:" */
502 args->cbnl_owner.s_dev = be32_to_cpu(*p++);
503 xdr_decode_hyper(p, &args->cbnl_owner.id);
504 args->cbnl_valid = true;
505 } else {
506 args->cbnl_owner.s_dev = 0;
507 args->cbnl_owner.id = 0;
508 args->cbnl_valid = false;
509 }
510 return 0;
511}
512
513static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_notify_lock_args *args)
514{
515 __be32 status;
516
517 status = decode_fh(xdr, &args->cbnl_fh);
518 if (unlikely(status != 0))
535ece2b
AS
519 return status;
520 return decode_lockowner(xdr, args);
db783688
JL
521}
522
4aece6a1
BH
523#endif /* CONFIG_NFS_V4_1 */
524
e6f684f6 525static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
1da177e4 526{
ab6e9aaf
TM
527 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
528 return cpu_to_be32(NFS4ERR_RESOURCE);
1da177e4
LT
529 return 0;
530}
531
532#define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
533#define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
5704fdeb 534static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
1da177e4 535{
5704fdeb
AV
536 __be32 bm[2];
537 __be32 *p;
1da177e4
LT
538
539 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
540 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
541 if (bm[1] != 0) {
542 p = xdr_reserve_space(xdr, 16);
543 if (unlikely(p == NULL))
544 return htonl(NFS4ERR_RESOURCE);
545 *p++ = htonl(2);
546 *p++ = bm[0];
547 *p++ = bm[1];
548 } else if (bm[0] != 0) {
549 p = xdr_reserve_space(xdr, 12);
550 if (unlikely(p == NULL))
551 return htonl(NFS4ERR_RESOURCE);
552 *p++ = htonl(1);
553 *p++ = bm[0];
554 } else {
555 p = xdr_reserve_space(xdr, 8);
556 if (unlikely(p == NULL))
557 return htonl(NFS4ERR_RESOURCE);
558 *p++ = htonl(0);
559 }
560 *savep = p;
561 return 0;
562}
563
e6f684f6 564static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
1da177e4 565{
5704fdeb 566 __be32 *p;
1da177e4
LT
567
568 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
569 return 0;
570 p = xdr_reserve_space(xdr, 8);
90dc7d27 571 if (unlikely(!p))
1da177e4
LT
572 return htonl(NFS4ERR_RESOURCE);
573 p = xdr_encode_hyper(p, change);
574 return 0;
575}
576
e6f684f6 577static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
1da177e4 578{
5704fdeb 579 __be32 *p;
1da177e4
LT
580
581 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
582 return 0;
583 p = xdr_reserve_space(xdr, 8);
90dc7d27 584 if (unlikely(!p))
1da177e4
LT
585 return htonl(NFS4ERR_RESOURCE);
586 p = xdr_encode_hyper(p, size);
587 return 0;
588}
589
e6f684f6 590static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
1da177e4 591{
5704fdeb 592 __be32 *p;
1da177e4
LT
593
594 p = xdr_reserve_space(xdr, 12);
90dc7d27 595 if (unlikely(!p))
1da177e4
LT
596 return htonl(NFS4ERR_RESOURCE);
597 p = xdr_encode_hyper(p, time->tv_sec);
598 *p = htonl(time->tv_nsec);
599 return 0;
600}
601
e6f684f6 602static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
1da177e4
LT
603{
604 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
605 return 0;
606 return encode_attr_time(xdr,time);
607}
608
e6f684f6 609static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
1da177e4
LT
610{
611 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
612 return 0;
613 return encode_attr_time(xdr,time);
614}
615
e6f684f6 616static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
1da177e4 617{
e6f684f6 618 __be32 status;
1da177e4
LT
619
620 hdr->status = xdr_reserve_space(xdr, 4);
621 if (unlikely(hdr->status == NULL))
622 return htonl(NFS4ERR_RESOURCE);
623 status = encode_string(xdr, hdr->taglen, hdr->tag);
624 if (unlikely(status != 0))
625 return status;
626 hdr->nops = xdr_reserve_space(xdr, 4);
627 if (unlikely(hdr->nops == NULL))
628 return htonl(NFS4ERR_RESOURCE);
629 return 0;
630}
631
e6f684f6 632static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
1da177e4 633{
5704fdeb 634 __be32 *p;
1da177e4
LT
635
636 p = xdr_reserve_space(xdr, 8);
637 if (unlikely(p == NULL))
31d2b435 638 return htonl(NFS4ERR_RESOURCE_HDR);
1da177e4
LT
639 *p++ = htonl(op);
640 *p = res;
641 return 0;
642}
643
e6f684f6 644static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
1da177e4 645{
5704fdeb 646 __be32 *savep = NULL;
e6f684f6 647 __be32 status = res->status;
1da177e4
LT
648
649 if (unlikely(status != 0))
650 goto out;
651 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
652 if (unlikely(status != 0))
653 goto out;
654 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
655 if (unlikely(status != 0))
656 goto out;
657 status = encode_attr_size(xdr, res->bitmap, res->size);
658 if (unlikely(status != 0))
659 goto out;
660 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
661 if (unlikely(status != 0))
662 goto out;
663 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
664 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
665out:
1da177e4
LT
666 return status;
667}
668
34bc47c9
BH
669#if defined(CONFIG_NFS_V4_1)
670
9733f0d9 671static __be32 encode_sessionid(struct xdr_stream *xdr,
4aece6a1
BH
672 const struct nfs4_sessionid *sid)
673{
9733f0d9 674 __be32 *p;
4aece6a1 675
590184a6 676 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
4aece6a1
BH
677 if (unlikely(p == NULL))
678 return htonl(NFS4ERR_RESOURCE);
679
590184a6 680 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
4aece6a1
BH
681 return 0;
682}
683
9733f0d9 684static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
4aece6a1
BH
685 struct xdr_stream *xdr,
686 const struct cb_sequenceres *res)
687{
9733f0d9 688 __be32 *p;
e216c8c7 689 __be32 status = res->csr_status;
4aece6a1
BH
690
691 if (unlikely(status != 0))
3d0bfaa6 692 return status;
4aece6a1 693
e0a63c0b
KM
694 status = encode_sessionid(xdr, &res->csr_sessionid);
695 if (status)
3d0bfaa6 696 return status;
4aece6a1
BH
697
698 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
699 if (unlikely(p == NULL))
700 return htonl(NFS4ERR_RESOURCE);
701
702 *p++ = htonl(res->csr_sequenceid);
703 *p++ = htonl(res->csr_slotid);
704 *p++ = htonl(res->csr_highestslotid);
705 *p++ = htonl(res->csr_target_highestslotid);
3d0bfaa6 706 return 0;
4aece6a1
BH
707}
708
34bc47c9
BH
709static __be32
710preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
711{
281fe15d
BH
712 if (op_nr == OP_CB_SEQUENCE) {
713 if (nop != 0)
714 return htonl(NFS4ERR_SEQUENCE_POS);
715 } else {
716 if (nop == 0)
717 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
718 }
719
34bc47c9
BH
720 switch (op_nr) {
721 case OP_CB_GETATTR:
722 case OP_CB_RECALL:
4aece6a1 723 case OP_CB_SEQUENCE:
31f09607 724 case OP_CB_RECALL_ANY:
b9efa1b2 725 case OP_CB_RECALL_SLOT:
f2a62561 726 case OP_CB_LAYOUTRECALL:
1be5683b 727 case OP_CB_NOTIFY_DEVICEID:
db783688 728 case OP_CB_NOTIFY_LOCK:
34bc47c9
BH
729 *op = &callback_ops[op_nr];
730 break;
731
34bc47c9
BH
732 case OP_CB_NOTIFY:
733 case OP_CB_PUSH_DELEG:
34bc47c9 734 case OP_CB_RECALLABLE_OBJ_AVAIL:
34bc47c9 735 case OP_CB_WANTS_CANCELLED:
34bc47c9
BH
736 return htonl(NFS4ERR_NOTSUPP);
737
738 default:
739 return htonl(NFS4ERR_OP_ILLEGAL);
740 }
741
742 return htonl(NFS_OK);
743}
744
810d82e6
TM
745static void nfs4_callback_free_slot(struct nfs4_session *session,
746 struct nfs4_slot *slot)
42acd021
AA
747{
748 struct nfs4_slot_table *tbl = &session->bc_slot_table;
749
750 spin_lock(&tbl->slot_tbl_lock);
751 /*
752 * Let the state manager know callback processing done.
753 * A single slot, so highest used slotid is either 0 or -1
754 */
810d82e6 755 nfs4_free_slot(tbl, slot);
774d5f14 756 nfs4_slot_tbl_drain_complete(tbl);
42acd021
AA
757 spin_unlock(&tbl->slot_tbl_lock);
758}
759
55a67399 760static void nfs4_cb_free_slot(struct cb_process_state *cps)
42acd021 761{
810d82e6
TM
762 if (cps->slot) {
763 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
764 cps->slot = NULL;
765 }
42acd021
AA
766}
767
34bc47c9
BH
768#else /* CONFIG_NFS_V4_1 */
769
770static __be32
771preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
772{
773 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
774}
775
55a67399 776static void nfs4_cb_free_slot(struct cb_process_state *cps)
42acd021
AA
777{
778}
34bc47c9
BH
779#endif /* CONFIG_NFS_V4_1 */
780
6b140b85
BS
781#ifdef CONFIG_NFS_V4_2
782static __be32
783preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
784{
785 __be32 status = preprocess_nfs41_op(nop, op_nr, op);
786 if (status != htonl(NFS4ERR_OP_ILLEGAL))
787 return status;
788
789 if (op_nr == OP_CB_OFFLOAD)
790 return htonl(NFS4ERR_NOTSUPP);
791 return htonl(NFS4ERR_OP_ILLEGAL);
792}
793#else /* CONFIG_NFS_V4_2 */
794static __be32
795preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
796{
797 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
798}
799#endif /* CONFIG_NFS_V4_2 */
800
34bc47c9
BH
801static __be32
802preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
803{
804 switch (op_nr) {
805 case OP_CB_GETATTR:
806 case OP_CB_RECALL:
807 *op = &callback_ops[op_nr];
808 break;
809 default:
810 return htonl(NFS4ERR_OP_ILLEGAL);
811 }
812
813 return htonl(NFS_OK);
814}
815
459de2ed 816static __be32 process_op(int nop, struct svc_rqst *rqstp,
1da177e4 817 struct xdr_stream *xdr_in, void *argp,
c36fca52
AA
818 struct xdr_stream *xdr_out, void *resp,
819 struct cb_process_state *cps)
1da177e4 820{
a162a6b8 821 struct callback_op *op = &callback_ops[0];
31d2b435 822 unsigned int op_nr;
34bc47c9 823 __be32 status;
1da177e4 824 long maxlen;
e6f684f6 825 __be32 res;
1da177e4 826
1da177e4 827 status = decode_op_hdr(xdr_in, &op_nr);
31d2b435
AA
828 if (unlikely(status))
829 return status;
1da177e4 830
6b140b85
BS
831 switch (cps->minorversion) {
832 case 0:
833 status = preprocess_nfs4_op(op_nr, &op);
834 break;
835 case 1:
836 status = preprocess_nfs41_op(nop, op_nr, &op);
837 break;
838 case 2:
839 status = preprocess_nfs42_op(nop, op_nr, &op);
840 break;
841 default:
842 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
843 }
34bc47c9 844
34bc47c9
BH
845 if (status == htonl(NFS4ERR_OP_ILLEGAL))
846 op_nr = OP_CB_ILLEGAL;
b92b3019
AA
847 if (status)
848 goto encode_hdr;
31d2b435 849
c36fca52
AA
850 if (cps->drc_status) {
851 status = cps->drc_status;
4911096f
AA
852 goto encode_hdr;
853 }
854
1da177e4
LT
855 maxlen = xdr_out->end - xdr_out->p;
856 if (maxlen > 0 && maxlen < PAGE_SIZE) {
e95e60da
AA
857 status = op->decode_args(rqstp, xdr_in, argp);
858 if (likely(status == 0))
c36fca52 859 status = op->process_op(argp, resp, cps);
1da177e4
LT
860 } else
861 status = htonl(NFS4ERR_RESOURCE);
862
b92b3019 863encode_hdr:
1da177e4 864 res = encode_op_hdr(xdr_out, op_nr, status);
31d2b435
AA
865 if (unlikely(res))
866 return res;
1da177e4
LT
867 if (op->encode_res != NULL && status == 0)
868 status = op->encode_res(rqstp, xdr_out, resp);
1da177e4
LT
869 return status;
870}
871
872/*
873 * Decode, process and encode a COMPOUND
874 */
7111c66e 875static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
1da177e4 876{
3a6258e1
TM
877 struct cb_compound_hdr_arg hdr_arg = { 0 };
878 struct cb_compound_hdr_res hdr_res = { NULL };
1da177e4 879 struct xdr_stream xdr_in, xdr_out;
c36fca52
AA
880 __be32 *p, status;
881 struct cb_process_state cps = {
882 .drc_status = 0,
883 .clp = NULL,
9695c705 884 .net = SVC_NET(rqstp),
c36fca52 885 };
3a6258e1 886 unsigned int nops = 0;
1da177e4 887
756b9b37 888 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
1da177e4 889
5704fdeb 890 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
1da177e4
LT
891 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
892
3a6258e1 893 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
4ed0d83d 894 if (status == htonl(NFS4ERR_RESOURCE))
3a6258e1
TM
895 return rpc_garbage_args;
896
c36fca52 897 if (hdr_arg.minorversion == 0) {
9695c705 898 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
778be232 899 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
a4e187d8 900 goto out_invalidcred;
778be232 901 }
c36fca52 902
459de2ed 903 cps.minorversion = hdr_arg.minorversion;
1da177e4
LT
904 hdr_res.taglen = hdr_arg.taglen;
905 hdr_res.tag = hdr_arg.tag;
3a6258e1
TM
906 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
907 return rpc_system_err;
1da177e4 908
3a6258e1 909 while (status == 0 && nops != hdr_arg.nops) {
459de2ed
BS
910 status = process_op(nops, rqstp, &xdr_in,
911 argp, &xdr_out, resp, &cps);
1da177e4
LT
912 nops++;
913 }
3a6258e1 914
31d2b435
AA
915 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
916 * resource error in cb_compound status without returning op */
917 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
918 status = htonl(NFS4ERR_RESOURCE);
919 nops--;
920 }
921
1da177e4
LT
922 *hdr_res.status = status;
923 *hdr_res.nops = htonl(nops);
55a67399 924 nfs4_cb_free_slot(&cps);
c36fca52 925 nfs_put_client(cps.clp);
1da177e4 926 return rpc_success;
a4e187d8
CL
927
928out_invalidcred:
929 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
930 return rpc_autherr_badcred;
1da177e4
LT
931}
932
933/*
934 * Define NFS4 callback COMPOUND ops.
935 */
936static struct callback_op callback_ops[] = {
937 [0] = {
938 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
939 },
940 [OP_CB_GETATTR] = {
941 .process_op = (callback_process_op_t)nfs4_callback_getattr,
942 .decode_args = (callback_decode_arg_t)decode_getattr_args,
943 .encode_res = (callback_encode_res_t)encode_getattr_res,
944 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
945 },
946 [OP_CB_RECALL] = {
947 .process_op = (callback_process_op_t)nfs4_callback_recall,
948 .decode_args = (callback_decode_arg_t)decode_recall_args,
949 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
4aece6a1
BH
950 },
951#if defined(CONFIG_NFS_V4_1)
f2a62561
FI
952 [OP_CB_LAYOUTRECALL] = {
953 .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
954 .decode_args =
955 (callback_decode_arg_t)decode_layoutrecall_args,
956 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
957 },
1be5683b
ME
958 [OP_CB_NOTIFY_DEVICEID] = {
959 .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
960 .decode_args =
961 (callback_decode_arg_t)decode_devicenotify_args,
962 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
963 },
4aece6a1
BH
964 [OP_CB_SEQUENCE] = {
965 .process_op = (callback_process_op_t)nfs4_callback_sequence,
966 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
967 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
968 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
969 },
31f09607
AB
970 [OP_CB_RECALL_ANY] = {
971 .process_op = (callback_process_op_t)nfs4_callback_recallany,
972 .decode_args = (callback_decode_arg_t)decode_recallany_args,
973 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
974 },
b9efa1b2
AA
975 [OP_CB_RECALL_SLOT] = {
976 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
977 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
978 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
979 },
db783688
JL
980 [OP_CB_NOTIFY_LOCK] = {
981 .process_op = (callback_process_op_t)nfs4_callback_notify_lock,
982 .decode_args = (callback_decode_arg_t)decode_notify_lock_args,
983 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
984 },
4aece6a1 985#endif /* CONFIG_NFS_V4_1 */
1da177e4
LT
986};
987
988/*
989 * Define NFS4 callback procedures
990 */
991static struct svc_procedure nfs4_callback_procedures1[] = {
992 [CB_NULL] = {
993 .pc_func = nfs4_callback_null,
994 .pc_decode = (kxdrproc_t)nfs4_decode_void,
995 .pc_encode = (kxdrproc_t)nfs4_encode_void,
996 .pc_xdrressize = 1,
997 },
998 [CB_COMPOUND] = {
999 .pc_func = nfs4_callback_compound,
1000 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1001 .pc_argsize = 256,
1002 .pc_ressize = 256,
1003 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1004 }
1005};
1006
1007struct svc_version nfs4_callback_version1 = {
1008 .vs_vers = 1,
1009 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1010 .vs_proc = nfs4_callback_procedures1,
1011 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1012 .vs_dispatch = NULL,
05a45a2d 1013 .vs_hidden = true,
5283b03e 1014 .vs_need_cong_ctrl = true,
1da177e4
LT
1015};
1016
07bccc2d
AB
1017struct svc_version nfs4_callback_version4 = {
1018 .vs_vers = 4,
1019 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1020 .vs_proc = nfs4_callback_procedures1,
1021 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1022 .vs_dispatch = NULL,
05a45a2d 1023 .vs_hidden = true,
5283b03e 1024 .vs_need_cong_ctrl = true,
07bccc2d 1025};