Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[pandora-kernel.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/slab.h>
13 #include "nfs4_fs.h"
14 #include "callback.h"
15
16 #define CB_OP_TAGLEN_MAXSZ      (512)
17 #define CB_OP_HDR_RES_MAXSZ     (2 + CB_OP_TAGLEN_MAXSZ)
18 #define CB_OP_GETATTR_BITMAP_MAXSZ      (4)
19 #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
20                                 CB_OP_GETATTR_BITMAP_MAXSZ + \
21                                 2 + 2 + 3 + 3)
22 #define CB_OP_RECALL_RES_MAXSZ  (CB_OP_HDR_RES_MAXSZ)
23
24 #if defined(CONFIG_NFS_V4_1)
25 #define CB_OP_SEQUENCE_RES_MAXSZ        (CB_OP_HDR_RES_MAXSZ + \
26                                         4 + 1 + 3)
27 #define CB_OP_RECALLANY_RES_MAXSZ       (CB_OP_HDR_RES_MAXSZ)
28 #define CB_OP_RECALLSLOT_RES_MAXSZ      (CB_OP_HDR_RES_MAXSZ)
29 #endif /* CONFIG_NFS_V4_1 */
30
31 #define NFSDBG_FACILITY NFSDBG_CALLBACK
32
33 /* Internal error code */
34 #define NFS4ERR_RESOURCE_HDR    11050
35
36 typedef __be32 (*callback_process_op_t)(void *, void *);
37 typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
38 typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
39
40
41 struct callback_op {
42         callback_process_op_t process_op;
43         callback_decode_arg_t decode_args;
44         callback_encode_res_t encode_res;
45         long res_maxsize;
46 };
47
48 static struct callback_op callback_ops[];
49
50 static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
51 {
52         return htonl(NFS4_OK);
53 }
54
55 static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
56 {
57         return xdr_argsize_check(rqstp, p);
58 }
59
60 static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
61 {
62         return xdr_ressize_check(rqstp, p);
63 }
64
65 static __be32 *read_buf(struct xdr_stream *xdr, int nbytes)
66 {
67         __be32 *p;
68
69         p = xdr_inline_decode(xdr, nbytes);
70         if (unlikely(p == NULL))
71                 printk(KERN_WARNING "NFSv4 callback reply buffer overflowed!\n");
72         return p;
73 }
74
75 static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
76 {
77         __be32 *p;
78
79         p = read_buf(xdr, 4);
80         if (unlikely(p == NULL))
81                 return htonl(NFS4ERR_RESOURCE);
82         *len = ntohl(*p);
83
84         if (*len != 0) {
85                 p = read_buf(xdr, *len);
86                 if (unlikely(p == NULL))
87                         return htonl(NFS4ERR_RESOURCE);
88                 *str = (const char *)p;
89         } else
90                 *str = NULL;
91
92         return 0;
93 }
94
95 static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
96 {
97         __be32 *p;
98
99         p = read_buf(xdr, 4);
100         if (unlikely(p == NULL))
101                 return htonl(NFS4ERR_RESOURCE);
102         fh->size = ntohl(*p);
103         if (fh->size > NFS4_FHSIZE)
104                 return htonl(NFS4ERR_BADHANDLE);
105         p = read_buf(xdr, fh->size);
106         if (unlikely(p == NULL))
107                 return htonl(NFS4ERR_RESOURCE);
108         memcpy(&fh->data[0], p, fh->size);
109         memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
110         return 0;
111 }
112
113 static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
114 {
115         __be32 *p;
116         unsigned int attrlen;
117
118         p = read_buf(xdr, 4);
119         if (unlikely(p == NULL))
120                 return htonl(NFS4ERR_RESOURCE);
121         attrlen = ntohl(*p);
122         p = read_buf(xdr, attrlen << 2);
123         if (unlikely(p == NULL))
124                 return htonl(NFS4ERR_RESOURCE);
125         if (likely(attrlen > 0))
126                 bitmap[0] = ntohl(*p++);
127         if (attrlen > 1)
128                 bitmap[1] = ntohl(*p);
129         return 0;
130 }
131
132 static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
133 {
134         __be32 *p;
135
136         p = read_buf(xdr, 16);
137         if (unlikely(p == NULL))
138                 return htonl(NFS4ERR_RESOURCE);
139         memcpy(stateid->data, p, 16);
140         return 0;
141 }
142
143 static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
144 {
145         __be32 *p;
146         __be32 status;
147
148         status = decode_string(xdr, &hdr->taglen, &hdr->tag);
149         if (unlikely(status != 0))
150                 return status;
151         /* We do not like overly long tags! */
152         if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) {
153                 printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
154                                 __func__, hdr->taglen);
155                 return htonl(NFS4ERR_RESOURCE);
156         }
157         p = read_buf(xdr, 12);
158         if (unlikely(p == NULL))
159                 return htonl(NFS4ERR_RESOURCE);
160         hdr->minorversion = ntohl(*p++);
161         /* Check minor version is zero or one. */
162         if (hdr->minorversion <= 1) {
163                 p++;    /* skip callback_ident */
164         } else {
165                 printk(KERN_WARNING "%s: NFSv4 server callback with "
166                         "illegal minor version %u!\n",
167                         __func__, hdr->minorversion);
168                 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
169         }
170         hdr->nops = ntohl(*p);
171         dprintk("%s: minorversion %d nops %d\n", __func__,
172                 hdr->minorversion, hdr->nops);
173         return 0;
174 }
175
176 static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
177 {
178         __be32 *p;
179         p = read_buf(xdr, 4);
180         if (unlikely(p == NULL))
181                 return htonl(NFS4ERR_RESOURCE_HDR);
182         *op = ntohl(*p);
183         return 0;
184 }
185
186 static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
187 {
188         __be32 status;
189
190         status = decode_fh(xdr, &args->fh);
191         if (unlikely(status != 0))
192                 goto out;
193         args->addr = svc_addr(rqstp);
194         status = decode_bitmap(xdr, args->bitmap);
195 out:
196         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
197         return status;
198 }
199
200 static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
201 {
202         __be32 *p;
203         __be32 status;
204
205         args->addr = svc_addr(rqstp);
206         status = decode_stateid(xdr, &args->stateid);
207         if (unlikely(status != 0))
208                 goto out;
209         p = read_buf(xdr, 4);
210         if (unlikely(p == NULL)) {
211                 status = htonl(NFS4ERR_RESOURCE);
212                 goto out;
213         }
214         args->truncate = ntohl(*p);
215         status = decode_fh(xdr, &args->fh);
216 out:
217         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
218         return status;
219 }
220
221 #if defined(CONFIG_NFS_V4_1)
222
223 static __be32 decode_sessionid(struct xdr_stream *xdr,
224                                  struct nfs4_sessionid *sid)
225 {
226         __be32 *p;
227         int len = NFS4_MAX_SESSIONID_LEN;
228
229         p = read_buf(xdr, len);
230         if (unlikely(p == NULL))
231                 return htonl(NFS4ERR_RESOURCE);
232
233         memcpy(sid->data, p, len);
234         return 0;
235 }
236
237 static __be32 decode_rc_list(struct xdr_stream *xdr,
238                                struct referring_call_list *rc_list)
239 {
240         __be32 *p;
241         int i;
242         __be32 status;
243
244         status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
245         if (status)
246                 goto out;
247
248         status = htonl(NFS4ERR_RESOURCE);
249         p = read_buf(xdr, sizeof(uint32_t));
250         if (unlikely(p == NULL))
251                 goto out;
252
253         rc_list->rcl_nrefcalls = ntohl(*p++);
254         if (rc_list->rcl_nrefcalls) {
255                 p = read_buf(xdr,
256                              rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
257                 if (unlikely(p == NULL))
258                         goto out;
259                 rc_list->rcl_refcalls = kmalloc(rc_list->rcl_nrefcalls *
260                                                 sizeof(*rc_list->rcl_refcalls),
261                                                 GFP_KERNEL);
262                 if (unlikely(rc_list->rcl_refcalls == NULL))
263                         goto out;
264                 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
265                         rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
266                         rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
267                 }
268         }
269         status = 0;
270
271 out:
272         return status;
273 }
274
275 static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
276                                         struct xdr_stream *xdr,
277                                         struct cb_sequenceargs *args)
278 {
279         __be32 *p;
280         int i;
281         __be32 status;
282
283         status = decode_sessionid(xdr, &args->csa_sessionid);
284         if (status)
285                 goto out;
286
287         status = htonl(NFS4ERR_RESOURCE);
288         p = read_buf(xdr, 5 * sizeof(uint32_t));
289         if (unlikely(p == NULL))
290                 goto out;
291
292         args->csa_addr = svc_addr(rqstp);
293         args->csa_sequenceid = ntohl(*p++);
294         args->csa_slotid = ntohl(*p++);
295         args->csa_highestslotid = ntohl(*p++);
296         args->csa_cachethis = ntohl(*p++);
297         args->csa_nrclists = ntohl(*p++);
298         args->csa_rclists = NULL;
299         if (args->csa_nrclists) {
300                 args->csa_rclists = kmalloc(args->csa_nrclists *
301                                             sizeof(*args->csa_rclists),
302                                             GFP_KERNEL);
303                 if (unlikely(args->csa_rclists == NULL))
304                         goto out;
305
306                 for (i = 0; i < args->csa_nrclists; i++) {
307                         status = decode_rc_list(xdr, &args->csa_rclists[i]);
308                         if (status)
309                                 goto out_free;
310                 }
311         }
312         status = 0;
313
314         dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
315                 "highestslotid %u cachethis %d nrclists %u\n",
316                 __func__,
317                 ((u32 *)&args->csa_sessionid)[0],
318                 ((u32 *)&args->csa_sessionid)[1],
319                 ((u32 *)&args->csa_sessionid)[2],
320                 ((u32 *)&args->csa_sessionid)[3],
321                 args->csa_sequenceid, args->csa_slotid,
322                 args->csa_highestslotid, args->csa_cachethis,
323                 args->csa_nrclists);
324 out:
325         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
326         return status;
327
328 out_free:
329         for (i = 0; i < args->csa_nrclists; i++)
330                 kfree(args->csa_rclists[i].rcl_refcalls);
331         kfree(args->csa_rclists);
332         goto out;
333 }
334
335 static __be32 decode_recallany_args(struct svc_rqst *rqstp,
336                                       struct xdr_stream *xdr,
337                                       struct cb_recallanyargs *args)
338 {
339         __be32 *p;
340
341         args->craa_addr = svc_addr(rqstp);
342         p = read_buf(xdr, 4);
343         if (unlikely(p == NULL))
344                 return htonl(NFS4ERR_BADXDR);
345         args->craa_objs_to_keep = ntohl(*p++);
346         p = read_buf(xdr, 4);
347         if (unlikely(p == NULL))
348                 return htonl(NFS4ERR_BADXDR);
349         args->craa_type_mask = ntohl(*p);
350
351         return 0;
352 }
353
354 static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
355                                         struct xdr_stream *xdr,
356                                         struct cb_recallslotargs *args)
357 {
358         __be32 *p;
359
360         args->crsa_addr = svc_addr(rqstp);
361         p = read_buf(xdr, 4);
362         if (unlikely(p == NULL))
363                 return htonl(NFS4ERR_BADXDR);
364         args->crsa_target_max_slots = ntohl(*p++);
365         return 0;
366 }
367
368 #endif /* CONFIG_NFS_V4_1 */
369
370 static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
371 {
372         __be32 *p;
373
374         p = xdr_reserve_space(xdr, 4 + len);
375         if (unlikely(p == NULL))
376                 return htonl(NFS4ERR_RESOURCE);
377         xdr_encode_opaque(p, str, len);
378         return 0;
379 }
380
381 #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
382 #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
383 static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
384 {
385         __be32 bm[2];
386         __be32 *p;
387
388         bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
389         bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
390         if (bm[1] != 0) {
391                 p = xdr_reserve_space(xdr, 16);
392                 if (unlikely(p == NULL))
393                         return htonl(NFS4ERR_RESOURCE);
394                 *p++ = htonl(2);
395                 *p++ = bm[0];
396                 *p++ = bm[1];
397         } else if (bm[0] != 0) {
398                 p = xdr_reserve_space(xdr, 12);
399                 if (unlikely(p == NULL))
400                         return htonl(NFS4ERR_RESOURCE);
401                 *p++ = htonl(1);
402                 *p++ = bm[0];
403         } else {
404                 p = xdr_reserve_space(xdr, 8);
405                 if (unlikely(p == NULL))
406                         return htonl(NFS4ERR_RESOURCE);
407                 *p++ = htonl(0);
408         }
409         *savep = p;
410         return 0;
411 }
412
413 static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
414 {
415         __be32 *p;
416
417         if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
418                 return 0;
419         p = xdr_reserve_space(xdr, 8);
420         if (unlikely(!p))
421                 return htonl(NFS4ERR_RESOURCE);
422         p = xdr_encode_hyper(p, change);
423         return 0;
424 }
425
426 static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
427 {
428         __be32 *p;
429
430         if (!(bitmap[0] & FATTR4_WORD0_SIZE))
431                 return 0;
432         p = xdr_reserve_space(xdr, 8);
433         if (unlikely(!p))
434                 return htonl(NFS4ERR_RESOURCE);
435         p = xdr_encode_hyper(p, size);
436         return 0;
437 }
438
439 static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
440 {
441         __be32 *p;
442
443         p = xdr_reserve_space(xdr, 12);
444         if (unlikely(!p))
445                 return htonl(NFS4ERR_RESOURCE);
446         p = xdr_encode_hyper(p, time->tv_sec);
447         *p = htonl(time->tv_nsec);
448         return 0;
449 }
450
451 static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
452 {
453         if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
454                 return 0;
455         return encode_attr_time(xdr,time);
456 }
457
458 static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
459 {
460         if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
461                 return 0;
462         return encode_attr_time(xdr,time);
463 }
464
465 static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
466 {
467         __be32 status;
468
469         hdr->status = xdr_reserve_space(xdr, 4);
470         if (unlikely(hdr->status == NULL))
471                 return htonl(NFS4ERR_RESOURCE);
472         status = encode_string(xdr, hdr->taglen, hdr->tag);
473         if (unlikely(status != 0))
474                 return status;
475         hdr->nops = xdr_reserve_space(xdr, 4);
476         if (unlikely(hdr->nops == NULL))
477                 return htonl(NFS4ERR_RESOURCE);
478         return 0;
479 }
480
481 static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
482 {
483         __be32 *p;
484         
485         p = xdr_reserve_space(xdr, 8);
486         if (unlikely(p == NULL))
487                 return htonl(NFS4ERR_RESOURCE_HDR);
488         *p++ = htonl(op);
489         *p = res;
490         return 0;
491 }
492
493 static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
494 {
495         __be32 *savep = NULL;
496         __be32 status = res->status;
497         
498         if (unlikely(status != 0))
499                 goto out;
500         status = encode_attr_bitmap(xdr, res->bitmap, &savep);
501         if (unlikely(status != 0))
502                 goto out;
503         status = encode_attr_change(xdr, res->bitmap, res->change_attr);
504         if (unlikely(status != 0))
505                 goto out;
506         status = encode_attr_size(xdr, res->bitmap, res->size);
507         if (unlikely(status != 0))
508                 goto out;
509         status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
510         if (unlikely(status != 0))
511                 goto out;
512         status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
513         *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
514 out:
515         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
516         return status;
517 }
518
519 #if defined(CONFIG_NFS_V4_1)
520
521 static __be32 encode_sessionid(struct xdr_stream *xdr,
522                                  const struct nfs4_sessionid *sid)
523 {
524         __be32 *p;
525         int len = NFS4_MAX_SESSIONID_LEN;
526
527         p = xdr_reserve_space(xdr, len);
528         if (unlikely(p == NULL))
529                 return htonl(NFS4ERR_RESOURCE);
530
531         memcpy(p, sid, len);
532         return 0;
533 }
534
535 static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
536                                        struct xdr_stream *xdr,
537                                        const struct cb_sequenceres *res)
538 {
539         __be32 *p;
540         unsigned status = res->csr_status;
541
542         if (unlikely(status != 0))
543                 goto out;
544
545         encode_sessionid(xdr, &res->csr_sessionid);
546
547         p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
548         if (unlikely(p == NULL))
549                 return htonl(NFS4ERR_RESOURCE);
550
551         *p++ = htonl(res->csr_sequenceid);
552         *p++ = htonl(res->csr_slotid);
553         *p++ = htonl(res->csr_highestslotid);
554         *p++ = htonl(res->csr_target_highestslotid);
555 out:
556         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
557         return status;
558 }
559
560 static __be32
561 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
562 {
563         if (op_nr == OP_CB_SEQUENCE) {
564                 if (nop != 0)
565                         return htonl(NFS4ERR_SEQUENCE_POS);
566         } else {
567                 if (nop == 0)
568                         return htonl(NFS4ERR_OP_NOT_IN_SESSION);
569         }
570
571         switch (op_nr) {
572         case OP_CB_GETATTR:
573         case OP_CB_RECALL:
574         case OP_CB_SEQUENCE:
575         case OP_CB_RECALL_ANY:
576         case OP_CB_RECALL_SLOT:
577                 *op = &callback_ops[op_nr];
578                 break;
579
580         case OP_CB_LAYOUTRECALL:
581         case OP_CB_NOTIFY_DEVICEID:
582         case OP_CB_NOTIFY:
583         case OP_CB_PUSH_DELEG:
584         case OP_CB_RECALLABLE_OBJ_AVAIL:
585         case OP_CB_WANTS_CANCELLED:
586         case OP_CB_NOTIFY_LOCK:
587                 return htonl(NFS4ERR_NOTSUPP);
588
589         default:
590                 return htonl(NFS4ERR_OP_ILLEGAL);
591         }
592
593         return htonl(NFS_OK);
594 }
595
596 #else /* CONFIG_NFS_V4_1 */
597
598 static __be32
599 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
600 {
601         return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
602 }
603
604 #endif /* CONFIG_NFS_V4_1 */
605
606 static __be32
607 preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
608 {
609         switch (op_nr) {
610         case OP_CB_GETATTR:
611         case OP_CB_RECALL:
612                 *op = &callback_ops[op_nr];
613                 break;
614         default:
615                 return htonl(NFS4ERR_OP_ILLEGAL);
616         }
617
618         return htonl(NFS_OK);
619 }
620
621 static __be32 process_op(uint32_t minorversion, int nop,
622                 struct svc_rqst *rqstp,
623                 struct xdr_stream *xdr_in, void *argp,
624                 struct xdr_stream *xdr_out, void *resp, int* drc_status)
625 {
626         struct callback_op *op = &callback_ops[0];
627         unsigned int op_nr;
628         __be32 status;
629         long maxlen;
630         __be32 res;
631
632         dprintk("%s: start\n", __func__);
633         status = decode_op_hdr(xdr_in, &op_nr);
634         if (unlikely(status))
635                 return status;
636
637         dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
638                 __func__, minorversion, nop, op_nr);
639
640         status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
641                                 preprocess_nfs4_op(op_nr, &op);
642         if (status == htonl(NFS4ERR_OP_ILLEGAL))
643                 op_nr = OP_CB_ILLEGAL;
644         if (status)
645                 goto encode_hdr;
646
647         if (*drc_status) {
648                 status = *drc_status;
649                 goto encode_hdr;
650         }
651
652         maxlen = xdr_out->end - xdr_out->p;
653         if (maxlen > 0 && maxlen < PAGE_SIZE) {
654                 status = op->decode_args(rqstp, xdr_in, argp);
655                 if (likely(status == 0))
656                         status = op->process_op(argp, resp);
657         } else
658                 status = htonl(NFS4ERR_RESOURCE);
659
660         /* Only set by OP_CB_SEQUENCE processing */
661         if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
662                 *drc_status = status;
663                 status = 0;
664         }
665
666 encode_hdr:
667         res = encode_op_hdr(xdr_out, op_nr, status);
668         if (unlikely(res))
669                 return res;
670         if (op->encode_res != NULL && status == 0)
671                 status = op->encode_res(rqstp, xdr_out, resp);
672         dprintk("%s: done, status = %d\n", __func__, ntohl(status));
673         return status;
674 }
675
676 /*
677  * Decode, process and encode a COMPOUND
678  */
679 static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
680 {
681         struct cb_compound_hdr_arg hdr_arg = { 0 };
682         struct cb_compound_hdr_res hdr_res = { NULL };
683         struct xdr_stream xdr_in, xdr_out;
684         __be32 *p;
685         __be32 status, drc_status = 0;
686         unsigned int nops = 0;
687
688         dprintk("%s: start\n", __func__);
689
690         xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
691
692         p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
693         xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
694
695         status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
696         if (status == __constant_htonl(NFS4ERR_RESOURCE))
697                 return rpc_garbage_args;
698
699         hdr_res.taglen = hdr_arg.taglen;
700         hdr_res.tag = hdr_arg.tag;
701         if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
702                 return rpc_system_err;
703
704         while (status == 0 && nops != hdr_arg.nops) {
705                 status = process_op(hdr_arg.minorversion, nops, rqstp,
706                                     &xdr_in, argp, &xdr_out, resp, &drc_status);
707                 nops++;
708         }
709
710         /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
711         * resource error in cb_compound status without returning op */
712         if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
713                 status = htonl(NFS4ERR_RESOURCE);
714                 nops--;
715         }
716
717         *hdr_res.status = status;
718         *hdr_res.nops = htonl(nops);
719         dprintk("%s: done, status = %u\n", __func__, ntohl(status));
720         return rpc_success;
721 }
722
723 /*
724  * Define NFS4 callback COMPOUND ops.
725  */
726 static struct callback_op callback_ops[] = {
727         [0] = {
728                 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
729         },
730         [OP_CB_GETATTR] = {
731                 .process_op = (callback_process_op_t)nfs4_callback_getattr,
732                 .decode_args = (callback_decode_arg_t)decode_getattr_args,
733                 .encode_res = (callback_encode_res_t)encode_getattr_res,
734                 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
735         },
736         [OP_CB_RECALL] = {
737                 .process_op = (callback_process_op_t)nfs4_callback_recall,
738                 .decode_args = (callback_decode_arg_t)decode_recall_args,
739                 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
740         },
741 #if defined(CONFIG_NFS_V4_1)
742         [OP_CB_SEQUENCE] = {
743                 .process_op = (callback_process_op_t)nfs4_callback_sequence,
744                 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
745                 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
746                 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
747         },
748         [OP_CB_RECALL_ANY] = {
749                 .process_op = (callback_process_op_t)nfs4_callback_recallany,
750                 .decode_args = (callback_decode_arg_t)decode_recallany_args,
751                 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
752         },
753         [OP_CB_RECALL_SLOT] = {
754                 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
755                 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
756                 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
757         },
758 #endif /* CONFIG_NFS_V4_1 */
759 };
760
761 /*
762  * Define NFS4 callback procedures
763  */
764 static struct svc_procedure nfs4_callback_procedures1[] = {
765         [CB_NULL] = {
766                 .pc_func = nfs4_callback_null,
767                 .pc_decode = (kxdrproc_t)nfs4_decode_void,
768                 .pc_encode = (kxdrproc_t)nfs4_encode_void,
769                 .pc_xdrressize = 1,
770         },
771         [CB_COMPOUND] = {
772                 .pc_func = nfs4_callback_compound,
773                 .pc_encode = (kxdrproc_t)nfs4_encode_void,
774                 .pc_argsize = 256,
775                 .pc_ressize = 256,
776                 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
777         }
778 };
779
780 struct svc_version nfs4_callback_version1 = {
781         .vs_vers = 1,
782         .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
783         .vs_proc = nfs4_callback_procedures1,
784         .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
785         .vs_dispatch = NULL,
786         .vs_hidden = 1,
787 };
788
789 struct svc_version nfs4_callback_version4 = {
790         .vs_vers = 4,
791         .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
792         .vs_proc = nfs4_callback_procedures1,
793         .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
794         .vs_dispatch = NULL,
795 };