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