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