Merge remote-tracking branches 'asoc/topic/fsl', 'asoc/topic/hdmi', 'asoc/topic/hisi...
[sfrench/cifs-2.6.git] / fs / nfs / callback_xdr.c
1 /*
2  * linux/fs/nfs/callback_xdr.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFSv4 callback encode/decode procedures
7  */
8 #include <linux/kernel.h>
9 #include <linux/sunrpc/svc.h>
10 #include <linux/nfs4.h>
11 #include <linux/nfs_fs.h>
12 #include <linux/ratelimit.h>
13 #include <linux/printk.h>
14 #include <linux/slab.h>
15 #include <linux/sunrpc/bc_xprt.h>
16 #include "nfs4_fs.h"
17 #include "callback.h"
18 #include "internal.h"
19 #include "nfs4session.h"
20
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)
29
30 #if defined(CONFIG_NFS_V4_1)
31 #define CB_OP_LAYOUTRECALL_RES_MAXSZ    (CB_OP_HDR_RES_MAXSZ)
32 #define CB_OP_DEVICENOTIFY_RES_MAXSZ    (CB_OP_HDR_RES_MAXSZ)
33 #define CB_OP_SEQUENCE_RES_MAXSZ        (CB_OP_HDR_RES_MAXSZ + \
34                                          NFS4_MAX_SESSIONID_LEN + \
35                                          (1 + 3) * 4) // seqid, 3 slotids
36 #define CB_OP_RECALLANY_RES_MAXSZ       (CB_OP_HDR_RES_MAXSZ)
37 #define CB_OP_RECALLSLOT_RES_MAXSZ      (CB_OP_HDR_RES_MAXSZ)
38 #define CB_OP_NOTIFY_LOCK_RES_MAXSZ     (CB_OP_HDR_RES_MAXSZ)
39 #endif /* CONFIG_NFS_V4_1 */
40
41 #define NFSDBG_FACILITY NFSDBG_CALLBACK
42
43 /* Internal error code */
44 #define NFS4ERR_RESOURCE_HDR    11050
45
46 typedef __be32 (*callback_process_op_t)(void *, void *,
47                                         struct cb_process_state *);
48 typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
49 typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
50
51
52 struct 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
59 static struct callback_op callback_ops[];
60
61 static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
62 {
63         return htonl(NFS4_OK);
64 }
65
66 static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
67 {
68         return xdr_argsize_check(rqstp, p);
69 }
70
71 static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
72 {
73         return xdr_ressize_check(rqstp, p);
74 }
75
76 static __be32 *read_buf(struct xdr_stream *xdr, size_t nbytes)
77 {
78         __be32 *p;
79
80         p = xdr_inline_decode(xdr, nbytes);
81         if (unlikely(p == NULL))
82                 printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n");
83         return p;
84 }
85
86 static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len,
87                 const char **str, size_t maxlen)
88 {
89         ssize_t err;
90
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;
95         return 0;
96 }
97
98 static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
99 {
100         __be32 *p;
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
116 static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
117 {
118         __be32 *p;
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
135 static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
136 {
137         __be32 *p;
138
139         p = read_buf(xdr, NFS4_STATEID_SIZE);
140         if (unlikely(p == NULL))
141                 return htonl(NFS4ERR_RESOURCE);
142         memcpy(stateid->data, p, NFS4_STATEID_SIZE);
143         return 0;
144 }
145
146 static __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
152 static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
153 {
154         __be32 *p;
155         __be32 status;
156
157         status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ);
158         if (unlikely(status != 0))
159                 return status;
160         p = read_buf(xdr, 12);
161         if (unlikely(p == NULL))
162                 return htonl(NFS4ERR_RESOURCE);
163         hdr->minorversion = ntohl(*p++);
164         /* Check for minor version support */
165         if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
166                 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
167         } else {
168                 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
169                         "illegal minor version %u!\n",
170                         __func__, hdr->minorversion);
171                 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
172         }
173         hdr->nops = ntohl(*p);
174         return 0;
175 }
176
177 static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
178 {
179         __be32 *p;
180         p = read_buf(xdr, 4);
181         if (unlikely(p == NULL))
182                 return htonl(NFS4ERR_RESOURCE_HDR);
183         *op = ntohl(*p);
184         return 0;
185 }
186
187 static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
188 {
189         __be32 status;
190
191         status = decode_fh(xdr, &args->fh);
192         if (unlikely(status != 0))
193                 return status;
194         return decode_bitmap(xdr, args->bitmap);
195 }
196
197 static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
198 {
199         __be32 *p;
200         __be32 status;
201
202         status = decode_delegation_stateid(xdr, &args->stateid);
203         if (unlikely(status != 0))
204                 return status;
205         p = read_buf(xdr, 4);
206         if (unlikely(p == NULL))
207                 return htonl(NFS4ERR_RESOURCE);
208         args->truncate = ntohl(*p);
209         return decode_fh(xdr, &args->fh);
210 }
211
212 #if defined(CONFIG_NFS_V4_1)
213 static __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 }
218
219 static __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
227         p = read_buf(xdr, 4 * sizeof(uint32_t));
228         if (unlikely(p == NULL))
229                 return htonl(NFS4ERR_BADXDR);
230
231         args->cbl_layout_type = ntohl(*p++);
232         /* Depite the spec's xdr, iomode really belongs in the FILE switch,
233          * as it is unusable and ignored with the other types.
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))
243                         return status;
244
245                 p = read_buf(xdr, 2 * sizeof(uint64_t));
246                 if (unlikely(p == NULL))
247                         return htonl(NFS4ERR_BADXDR);
248                 p = xdr_decode_hyper(p, &args->cbl_range.offset);
249                 p = xdr_decode_hyper(p, &args->cbl_range.length);
250                 return decode_layout_stateid(xdr, &args->cbl_stateid);
251         } else if (args->cbl_recall_type == RETURN_FSID) {
252                 p = read_buf(xdr, 2 * sizeof(uint64_t));
253                 if (unlikely(p == NULL))
254                         return htonl(NFS4ERR_BADXDR);
255                 p = xdr_decode_hyper(p, &args->cbl_fsid.major);
256                 p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
257         } else if (args->cbl_recall_type != RETURN_ALL)
258                 return htonl(NFS4ERR_BADXDR);
259         return 0;
260 }
261
262 static
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;
282         if (n > ULONG_MAX / sizeof(*args->devs)) {
283                 status = htonl(NFS4ERR_BADXDR);
284                 goto out;
285         }
286
287         args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
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         }
344 out:
345         dprintk("%s: status %d ndevs %d\n",
346                 __func__, ntohl(status), args->ndevs);
347         return status;
348 err:
349         kfree(args->devs);
350         goto out;
351 }
352
353 static __be32 decode_sessionid(struct xdr_stream *xdr,
354                                  struct nfs4_sessionid *sid)
355 {
356         __be32 *p;
357
358         p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
359         if (unlikely(p == NULL))
360                 return htonl(NFS4ERR_RESOURCE);
361
362         memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
363         return 0;
364 }
365
366 static __be32 decode_rc_list(struct xdr_stream *xdr,
367                                struct referring_call_list *rc_list)
368 {
369         __be32 *p;
370         int i;
371         __be32 status;
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;
388                 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
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
400 out:
401         return status;
402 }
403
404 static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
405                                         struct xdr_stream *xdr,
406                                         struct cb_sequenceargs *args)
407 {
408         __be32 *p;
409         int i;
410         __be32 status;
411
412         status = decode_sessionid(xdr, &args->csa_sessionid);
413         if (status)
414                 return status;
415
416         p = read_buf(xdr, 5 * sizeof(uint32_t));
417         if (unlikely(p == NULL))
418                 return htonl(NFS4ERR_RESOURCE);
419
420         args->csa_addr = svc_addr(rqstp);
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) {
428                 args->csa_rclists = kmalloc_array(args->csa_nrclists,
429                                                   sizeof(*args->csa_rclists),
430                                                   GFP_KERNEL);
431                 if (unlikely(args->csa_rclists == NULL))
432                         return htonl(NFS4ERR_RESOURCE);
433
434                 for (i = 0; i < args->csa_nrclists; i++) {
435                         status = decode_rc_list(xdr, &args->csa_rclists[i]);
436                         if (status) {
437                                 args->csa_nrclists = i;
438                                 goto out_free;
439                         }
440                 }
441         }
442         return 0;
443
444 out_free:
445         for (i = 0; i < args->csa_nrclists; i++)
446                 kfree(args->csa_rclists[i].rcl_refcalls);
447         kfree(args->csa_rclists);
448         return status;
449 }
450
451 static __be32 decode_recallany_args(struct svc_rqst *rqstp,
452                                       struct xdr_stream *xdr,
453                                       struct cb_recallanyargs *args)
454 {
455         uint32_t bitmap[2];
456         __be32 *p, status;
457
458         p = read_buf(xdr, 4);
459         if (unlikely(p == NULL))
460                 return htonl(NFS4ERR_BADXDR);
461         args->craa_objs_to_keep = ntohl(*p++);
462         status = decode_bitmap(xdr, bitmap);
463         if (unlikely(status))
464                 return status;
465         args->craa_type_mask = bitmap[0];
466
467         return 0;
468 }
469
470 static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
471                                         struct xdr_stream *xdr,
472                                         struct cb_recallslotargs *args)
473 {
474         __be32 *p;
475
476         p = read_buf(xdr, 4);
477         if (unlikely(p == NULL))
478                 return htonl(NFS4ERR_BADXDR);
479         args->crsa_target_highest_slotid = ntohl(*p++);
480         return 0;
481 }
482
483 static __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
513 static __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))
519                 return status;
520         return decode_lockowner(xdr, args);
521 }
522
523 #endif /* CONFIG_NFS_V4_1 */
524
525 static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
526 {
527         if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
528                 return cpu_to_be32(NFS4ERR_RESOURCE);
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)
534 static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
535 {
536         __be32 bm[2];
537         __be32 *p;
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
564 static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
565 {
566         __be32 *p;
567
568         if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
569                 return 0;
570         p = xdr_reserve_space(xdr, 8);
571         if (unlikely(!p))
572                 return htonl(NFS4ERR_RESOURCE);
573         p = xdr_encode_hyper(p, change);
574         return 0;
575 }
576
577 static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
578 {
579         __be32 *p;
580
581         if (!(bitmap[0] & FATTR4_WORD0_SIZE))
582                 return 0;
583         p = xdr_reserve_space(xdr, 8);
584         if (unlikely(!p))
585                 return htonl(NFS4ERR_RESOURCE);
586         p = xdr_encode_hyper(p, size);
587         return 0;
588 }
589
590 static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
591 {
592         __be32 *p;
593
594         p = xdr_reserve_space(xdr, 12);
595         if (unlikely(!p))
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
602 static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
603 {
604         if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
605                 return 0;
606         return encode_attr_time(xdr,time);
607 }
608
609 static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
610 {
611         if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
612                 return 0;
613         return encode_attr_time(xdr,time);
614 }
615
616 static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
617 {
618         __be32 status;
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
632 static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
633 {
634         __be32 *p;
635         
636         p = xdr_reserve_space(xdr, 8);
637         if (unlikely(p == NULL))
638                 return htonl(NFS4ERR_RESOURCE_HDR);
639         *p++ = htonl(op);
640         *p = res;
641         return 0;
642 }
643
644 static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
645 {
646         __be32 *savep = NULL;
647         __be32 status = res->status;
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)));
665 out:
666         return status;
667 }
668
669 #if defined(CONFIG_NFS_V4_1)
670
671 static __be32 encode_sessionid(struct xdr_stream *xdr,
672                                  const struct nfs4_sessionid *sid)
673 {
674         __be32 *p;
675
676         p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
677         if (unlikely(p == NULL))
678                 return htonl(NFS4ERR_RESOURCE);
679
680         memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
681         return 0;
682 }
683
684 static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
685                                        struct xdr_stream *xdr,
686                                        const struct cb_sequenceres *res)
687 {
688         __be32 *p;
689         __be32 status = res->csr_status;
690
691         if (unlikely(status != 0))
692                 return status;
693
694         status = encode_sessionid(xdr, &res->csr_sessionid);
695         if (status)
696                 return status;
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);
706         return 0;
707 }
708
709 static __be32
710 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
711 {
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
720         switch (op_nr) {
721         case OP_CB_GETATTR:
722         case OP_CB_RECALL:
723         case OP_CB_SEQUENCE:
724         case OP_CB_RECALL_ANY:
725         case OP_CB_RECALL_SLOT:
726         case OP_CB_LAYOUTRECALL:
727         case OP_CB_NOTIFY_DEVICEID:
728         case OP_CB_NOTIFY_LOCK:
729                 *op = &callback_ops[op_nr];
730                 break;
731
732         case OP_CB_NOTIFY:
733         case OP_CB_PUSH_DELEG:
734         case OP_CB_RECALLABLE_OBJ_AVAIL:
735         case OP_CB_WANTS_CANCELLED:
736                 return htonl(NFS4ERR_NOTSUPP);
737
738         default:
739                 return htonl(NFS4ERR_OP_ILLEGAL);
740         }
741
742         return htonl(NFS_OK);
743 }
744
745 static void nfs4_callback_free_slot(struct nfs4_session *session,
746                 struct nfs4_slot *slot)
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          */
755         nfs4_free_slot(tbl, slot);
756         spin_unlock(&tbl->slot_tbl_lock);
757 }
758
759 static void nfs4_cb_free_slot(struct cb_process_state *cps)
760 {
761         if (cps->slot) {
762                 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
763                 cps->slot = NULL;
764         }
765 }
766
767 #else /* CONFIG_NFS_V4_1 */
768
769 static __be32
770 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
771 {
772         return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
773 }
774
775 static void nfs4_cb_free_slot(struct cb_process_state *cps)
776 {
777 }
778 #endif /* CONFIG_NFS_V4_1 */
779
780 #ifdef CONFIG_NFS_V4_2
781 static __be32
782 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
783 {
784         __be32 status = preprocess_nfs41_op(nop, op_nr, op);
785         if (status != htonl(NFS4ERR_OP_ILLEGAL))
786                 return status;
787
788         if (op_nr == OP_CB_OFFLOAD)
789                 return htonl(NFS4ERR_NOTSUPP);
790         return htonl(NFS4ERR_OP_ILLEGAL);
791 }
792 #else /* CONFIG_NFS_V4_2 */
793 static __be32
794 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
795 {
796         return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
797 }
798 #endif /* CONFIG_NFS_V4_2 */
799
800 static __be32
801 preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
802 {
803         switch (op_nr) {
804         case OP_CB_GETATTR:
805         case OP_CB_RECALL:
806                 *op = &callback_ops[op_nr];
807                 break;
808         default:
809                 return htonl(NFS4ERR_OP_ILLEGAL);
810         }
811
812         return htonl(NFS_OK);
813 }
814
815 static __be32 process_op(int nop, struct svc_rqst *rqstp,
816                 struct xdr_stream *xdr_in, void *argp,
817                 struct xdr_stream *xdr_out, void *resp,
818                 struct cb_process_state *cps)
819 {
820         struct callback_op *op = &callback_ops[0];
821         unsigned int op_nr;
822         __be32 status;
823         long maxlen;
824         __be32 res;
825
826         status = decode_op_hdr(xdr_in, &op_nr);
827         if (unlikely(status))
828                 return status;
829
830         switch (cps->minorversion) {
831         case 0:
832                 status = preprocess_nfs4_op(op_nr, &op);
833                 break;
834         case 1:
835                 status = preprocess_nfs41_op(nop, op_nr, &op);
836                 break;
837         case 2:
838                 status = preprocess_nfs42_op(nop, op_nr, &op);
839                 break;
840         default:
841                 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
842         }
843
844         if (status == htonl(NFS4ERR_OP_ILLEGAL))
845                 op_nr = OP_CB_ILLEGAL;
846         if (status)
847                 goto encode_hdr;
848
849         if (cps->drc_status) {
850                 status = cps->drc_status;
851                 goto encode_hdr;
852         }
853
854         maxlen = xdr_out->end - xdr_out->p;
855         if (maxlen > 0 && maxlen < PAGE_SIZE) {
856                 status = op->decode_args(rqstp, xdr_in, argp);
857                 if (likely(status == 0))
858                         status = op->process_op(argp, resp, cps);
859         } else
860                 status = htonl(NFS4ERR_RESOURCE);
861
862 encode_hdr:
863         res = encode_op_hdr(xdr_out, op_nr, status);
864         if (unlikely(res))
865                 return res;
866         if (op->encode_res != NULL && status == 0)
867                 status = op->encode_res(rqstp, xdr_out, resp);
868         return status;
869 }
870
871 /*
872  * Decode, process and encode a COMPOUND
873  */
874 static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
875 {
876         struct cb_compound_hdr_arg hdr_arg = { 0 };
877         struct cb_compound_hdr_res hdr_res = { NULL };
878         struct xdr_stream xdr_in, xdr_out;
879         __be32 *p, status;
880         struct cb_process_state cps = {
881                 .drc_status = 0,
882                 .clp = NULL,
883                 .net = SVC_NET(rqstp),
884         };
885         unsigned int nops = 0;
886
887         xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
888
889         p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
890         xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
891
892         status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
893         if (status == htonl(NFS4ERR_RESOURCE))
894                 return rpc_garbage_args;
895
896         if (hdr_arg.minorversion == 0) {
897                 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
898                 if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
899                         goto out_invalidcred;
900         }
901
902         cps.minorversion = hdr_arg.minorversion;
903         hdr_res.taglen = hdr_arg.taglen;
904         hdr_res.tag = hdr_arg.tag;
905         if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
906                 return rpc_system_err;
907
908         while (status == 0 && nops != hdr_arg.nops) {
909                 status = process_op(nops, rqstp, &xdr_in,
910                                     argp, &xdr_out, resp, &cps);
911                 nops++;
912         }
913
914         /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
915         * resource error in cb_compound status without returning op */
916         if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
917                 status = htonl(NFS4ERR_RESOURCE);
918                 nops--;
919         }
920
921         *hdr_res.status = status;
922         *hdr_res.nops = htonl(nops);
923         nfs4_cb_free_slot(&cps);
924         nfs_put_client(cps.clp);
925         return rpc_success;
926
927 out_invalidcred:
928         pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
929         return rpc_autherr_badcred;
930 }
931
932 /*
933  * Define NFS4 callback COMPOUND ops.
934  */
935 static struct callback_op callback_ops[] = {
936         [0] = {
937                 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
938         },
939         [OP_CB_GETATTR] = {
940                 .process_op = (callback_process_op_t)nfs4_callback_getattr,
941                 .decode_args = (callback_decode_arg_t)decode_getattr_args,
942                 .encode_res = (callback_encode_res_t)encode_getattr_res,
943                 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
944         },
945         [OP_CB_RECALL] = {
946                 .process_op = (callback_process_op_t)nfs4_callback_recall,
947                 .decode_args = (callback_decode_arg_t)decode_recall_args,
948                 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
949         },
950 #if defined(CONFIG_NFS_V4_1)
951         [OP_CB_LAYOUTRECALL] = {
952                 .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
953                 .decode_args =
954                         (callback_decode_arg_t)decode_layoutrecall_args,
955                 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
956         },
957         [OP_CB_NOTIFY_DEVICEID] = {
958                 .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
959                 .decode_args =
960                         (callback_decode_arg_t)decode_devicenotify_args,
961                 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
962         },
963         [OP_CB_SEQUENCE] = {
964                 .process_op = (callback_process_op_t)nfs4_callback_sequence,
965                 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
966                 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
967                 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
968         },
969         [OP_CB_RECALL_ANY] = {
970                 .process_op = (callback_process_op_t)nfs4_callback_recallany,
971                 .decode_args = (callback_decode_arg_t)decode_recallany_args,
972                 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
973         },
974         [OP_CB_RECALL_SLOT] = {
975                 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
976                 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
977                 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
978         },
979         [OP_CB_NOTIFY_LOCK] = {
980                 .process_op = (callback_process_op_t)nfs4_callback_notify_lock,
981                 .decode_args = (callback_decode_arg_t)decode_notify_lock_args,
982                 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
983         },
984 #endif /* CONFIG_NFS_V4_1 */
985 };
986
987 /*
988  * Define NFS4 callback procedures
989  */
990 static struct svc_procedure nfs4_callback_procedures1[] = {
991         [CB_NULL] = {
992                 .pc_func = nfs4_callback_null,
993                 .pc_decode = (kxdrproc_t)nfs4_decode_void,
994                 .pc_encode = (kxdrproc_t)nfs4_encode_void,
995                 .pc_xdrressize = 1,
996         },
997         [CB_COMPOUND] = {
998                 .pc_func = nfs4_callback_compound,
999                 .pc_encode = (kxdrproc_t)nfs4_encode_void,
1000                 .pc_argsize = 256,
1001                 .pc_ressize = 256,
1002                 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1003         }
1004 };
1005
1006 struct svc_version nfs4_callback_version1 = {
1007         .vs_vers = 1,
1008         .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1009         .vs_proc = nfs4_callback_procedures1,
1010         .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1011         .vs_dispatch = NULL,
1012         .vs_hidden = true,
1013         .vs_need_cong_ctrl = true,
1014 };
1015
1016 struct svc_version nfs4_callback_version4 = {
1017         .vs_vers = 4,
1018         .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1019         .vs_proc = nfs4_callback_procedures1,
1020         .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1021         .vs_dispatch = NULL,
1022         .vs_hidden = true,
1023         .vs_need_cong_ctrl = true,
1024 };